forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSM3ShieldV1DataNetworkProvider.cpp
398 lines (369 loc) · 10.7 KB
/
GSM3ShieldV1DataNetworkProvider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
This file is part of the GSM3 communications library for Arduino
-- Multi-transport communications platform
-- Fully asynchronous
-- Includes code for the Arduino-Telefonica GSM/GPRS Shield V1
-- Voice calls
-- SMS
-- TCP/IP connections
-- HTTP basic clients
This library has been developed by Telefónica Digital - PDI -
- Physical Internet Lab, as part as its collaboration with
Arduino and the Open Hardware Community.
September-December 2012
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The latest version of this library can always be found at
https://github.com/BlueVia/Official-Arduino
*/
#include <GSM3ShieldV1DataNetworkProvider.h>
#include <Arduino.h>
const char _command_CGATT[] PROGMEM = "AT+CGATT=";
const char _command_SEPARATOR[] PROGMEM = "\",\"";
//Attach GPRS main function.
GSM3_NetworkStatus_t GSM3ShieldV1DataNetworkProvider::attachGPRS(char* apn, char* user_name, char* password, bool synchronous)
{
user = user_name;
passwd = password;
// A sad use of byte reuse
theGSM3ShieldV1ModemCore.setPhoneNumber(apn);
theGSM3ShieldV1ModemCore.openCommand(this,ATTACHGPRS);
theGSM3ShieldV1ModemCore.setStatus(CONNECTING);
attachGPRSContinue();
// If synchronous, wait till attach is over, or not.
if(synchronous)
{
// if we shorten this delay, the command fails
while(ready()==0)
delay(100);
}
return theGSM3ShieldV1ModemCore.getStatus();
}
//Atthach GPRS continue function.
void GSM3ShieldV1DataNetworkProvider::attachGPRSContinue()
{
bool resp;
// 1: Attach to GPRS service "AT+CGATT=1"
// 2: Wait attach OK and Set the context 0 as FGCNT "AT+QIFGCNT=0"
// 3: Wait context OK and Set bearer type as GPRS, APN, user name and pasword "AT+QICSGP=1..."
// 4: Wait bearer OK and Enable the function of MUXIP "AT+QIMUX=1"
// 5: Wait for disable MUXIP OK and Set the session mode as non transparent "AT+QIMODE=0"
// 6: Wait for session mode OK and Enable notification when data received "AT+QINDI=1"
// 8: Wait domain name OK and Register the TCP/IP stack "AT+QIREGAPP"
// 9: Wait for Register OK and Activate FGCNT "AT+QIACT"
// 10: Wait for activate OK
int ct=theGSM3ShieldV1ModemCore.getCommandCounter();
if(ct==1)
{
//AT+CGATT
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGATT,false);
theGSM3ShieldV1ModemCore.print(1);
theGSM3ShieldV1ModemCore.print('\r');
theGSM3ShieldV1ModemCore.setCommandCounter(2);
}
else if(ct==2)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
//AT+QIFGCNT
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIFGCNT=0"));
theGSM3ShieldV1ModemCore.setCommandCounter(3);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==3)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
// Great. Go for the next step
//DEBUG
//Serial.println("AT+QICSGP.");
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QICSGP=1,\""),false);
theGSM3ShieldV1ModemCore.print(theGSM3ShieldV1ModemCore.getPhoneNumber());
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_SEPARATOR,false);
theGSM3ShieldV1ModemCore.print(user);
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_SEPARATOR,false);
theGSM3ShieldV1ModemCore.print(passwd);
theGSM3ShieldV1ModemCore.print("\"\r");
theGSM3ShieldV1ModemCore.setCommandCounter(4);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==4)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
// AT+QIMUX=1 for multisocket
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIMUX=0"));
theGSM3ShieldV1ModemCore.setCommandCounter(5);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==5)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
//AT+QIMODE=0 for multisocket
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIMODE=1"));
theGSM3ShieldV1ModemCore.setCommandCounter(6);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==6)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
// AT+QINDI=1
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QINDI=1"));
theGSM3ShieldV1ModemCore.setCommandCounter(8);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==8)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
// AT+QIREGAPP
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIREGAPP"));
theGSM3ShieldV1ModemCore.setCommandCounter(9);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==9)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
// AT+QIACT
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIACT"));
theGSM3ShieldV1ModemCore.setCommandCounter(10);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==10)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if (resp)
{
theGSM3ShieldV1ModemCore.setStatus(GPRS_READY);
theGSM3ShieldV1ModemCore.closeCommand(1);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
}
//Detach GPRS main function.
GSM3_NetworkStatus_t GSM3ShieldV1DataNetworkProvider::detachGPRS(bool synchronous)
{
theGSM3ShieldV1ModemCore.openCommand(this,DETACHGPRS);
theGSM3ShieldV1ModemCore.setStatus(CONNECTING);
detachGPRSContinue();
if(synchronous)
{
while(ready()==0)
delay(1);
}
return theGSM3ShieldV1ModemCore.getStatus();
}
void GSM3ShieldV1DataNetworkProvider::detachGPRSContinue()
{
bool resp;
// 1: Detach to GPRS service "AT+CGATT=0"
// 2: Wait dettach +PDP DEACT
// 3: Wait for OK
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
//AT+CGATT=0
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGATT,false);
theGSM3ShieldV1ModemCore.print(0);
theGSM3ShieldV1ModemCore.print('\r');
theGSM3ShieldV1ModemCore.setCommandCounter(2);
break;
case 2:
char auxLocate[12];
prepareAuxLocate(PSTR("+PDP DEACT"), auxLocate);
if(theGSM3ShieldV1ModemCore.theBuffer().locate(auxLocate))
{
if(resp)
{
// Received +PDP DEACT;
theGSM3ShieldV1ModemCore.setCommandCounter(3);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
break;
case 3:
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
// OK received
if (resp)
{
theGSM3ShieldV1ModemCore.setStatus(GSM_READY);
theGSM3ShieldV1ModemCore.closeCommand(1);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
theGSM3ShieldV1ModemCore.theBuffer().flush();
theGSM3ShieldV1ModemCore.gss.spaceAvailable();
break;
}
}
//QILOCIP parse.
bool GSM3ShieldV1DataNetworkProvider::parseQILOCIP_rsp(char* LocalIP, int LocalIPlength, bool& rsp)
{
if (!(theGSM3ShieldV1ModemCore.theBuffer().extractSubstring("\r\n","\r\n", LocalIP, LocalIPlength)))
rsp = false;
else
rsp = true;
return true;
}
//Get IP main function.
int GSM3ShieldV1DataNetworkProvider::getIP(char* LocalIP, int LocalIPlength)
{
theGSM3ShieldV1ModemCore.setPhoneNumber(LocalIP);
theGSM3ShieldV1ModemCore.setPort(LocalIPlength);
theGSM3ShieldV1ModemCore.openCommand(this,GETIP);
getIPContinue();
return theGSM3ShieldV1ModemCore.getCommandError();
}
void GSM3ShieldV1DataNetworkProvider::getIPContinue()
{
bool resp;
// 1: Read Local IP "AT+QILOCIP"
// 2: Waiting for IP.
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
//AT+QILOCIP
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QILOCIP"));
theGSM3ShieldV1ModemCore.setCommandCounter(2);
break;
case 2:
if(parseQILOCIP_rsp(theGSM3ShieldV1ModemCore.getPhoneNumber(), theGSM3ShieldV1ModemCore.getPort(), resp))
{
if (resp)
theGSM3ShieldV1ModemCore.closeCommand(1);
else
theGSM3ShieldV1ModemCore.closeCommand(3);
}
theGSM3ShieldV1ModemCore.theBuffer().flush();
theGSM3ShieldV1ModemCore.gss.spaceAvailable();
break;
}
}
//Get IP with IPAddress object
IPAddress GSM3ShieldV1DataNetworkProvider::getIPAddress() {
char ip_temp[15]="";
getIP(ip_temp, 15);
unsigned long m=millis();
while((millis()-m)<10*1000 && (!ready())){
// wait for a response from the modem:
delay(100);
}
IPAddress ip;
inet_aton(ip_temp, ip);
return ip;
}
int GSM3ShieldV1DataNetworkProvider::inet_aton(const char* aIPAddrString, IPAddress& aResult)
{
// See if we've been given a valid IP address
const char* p =aIPAddrString;
while (*p &&
( (*p == '.') || (*p >= '0') || (*p <= '9') ))
{
p++;
}
if (*p == '\0')
{
// It's looking promising, we haven't found any invalid characters
p = aIPAddrString;
int segment =0;
int segmentValue =0;
while (*p && (segment < 4))
{
if (*p == '.')
{
// We've reached the end of a segment
if (segmentValue > 255)
{
// You can't have IP address segments that don't fit in a byte
return 0;
}
else
{
aResult[segment] = (byte)segmentValue;
segment++;
segmentValue = 0;
}
}
else
{
// Next digit
segmentValue = (segmentValue*10)+(*p - '0');
}
p++;
}
// We've reached the end of address, but there'll still be the last
// segment to deal with
if ((segmentValue > 255) || (segment > 3))
{
// You can't have IP address segments that don't fit in a byte,
// or more than four segments
return 0;
}
else
{
aResult[segment] = (byte)segmentValue;
return 1;
}
}
else
{
return 0;
}
}
//Response management.
void GSM3ShieldV1DataNetworkProvider::manageResponse(byte from, byte to)
{
switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
{
case ATTACHGPRS:
attachGPRSContinue();
break;
case DETACHGPRS:
detachGPRSContinue();
break;
case GETIP:
getIPContinue();
break;
}
}