1 | #include <GSM3ShieldV1DataNetworkProvider.h>
|
---|
2 | #include <Arduino.h>
|
---|
3 |
|
---|
4 | const char _command_CGATT[] PROGMEM = "AT+CGATT=";
|
---|
5 | const char _command_SEPARATOR[] PROGMEM = "\",\"";
|
---|
6 |
|
---|
7 | //Attach GPRS main function.
|
---|
8 | GSM3_NetworkStatus_t GSM3ShieldV1DataNetworkProvider::attachGPRS(char* apn, char* user_name, char* password, bool synchronous)
|
---|
9 | {
|
---|
10 | user = user_name;
|
---|
11 | passwd = password;
|
---|
12 | // A sad use of byte reuse
|
---|
13 | theGSM3ShieldV1ModemCore.setPhoneNumber(apn);
|
---|
14 |
|
---|
15 | theGSM3ShieldV1ModemCore.openCommand(this,ATTACHGPRS);
|
---|
16 | theGSM3ShieldV1ModemCore.setStatus(CONNECTING);
|
---|
17 |
|
---|
18 | attachGPRSContinue();
|
---|
19 |
|
---|
20 | // If synchronous, wait till attach is over, or not.
|
---|
21 | if(synchronous)
|
---|
22 | {
|
---|
23 | // if we shorten this delay, the command fails
|
---|
24 | while(ready()==0)
|
---|
25 | delay(100);
|
---|
26 | }
|
---|
27 |
|
---|
28 | return theGSM3ShieldV1ModemCore.getStatus();
|
---|
29 | }
|
---|
30 |
|
---|
31 | //Atthach GPRS continue function.
|
---|
32 | void GSM3ShieldV1DataNetworkProvider::attachGPRSContinue()
|
---|
33 | {
|
---|
34 | bool resp;
|
---|
35 | // 1: Attach to GPRS service "AT+CGATT=1"
|
---|
36 | // 2: Wait attach OK and Set the context 0 as FGCNT "AT+QIFGCNT=0"
|
---|
37 | // 3: Wait context OK and Set bearer type as GPRS, APN, user name and pasword "AT+QICSGP=1..."
|
---|
38 | // 4: Wait bearer OK and Enable the function of MUXIP "AT+QIMUX=1"
|
---|
39 | // 5: Wait for disable MUXIP OK and Set the session mode as non transparent "AT+QIMODE=0"
|
---|
40 | // 6: Wait for session mode OK and Enable notification when data received "AT+QINDI=1"
|
---|
41 | // 8: Wait domain name OK and Register the TCP/IP stack "AT+QIREGAPP"
|
---|
42 | // 9: Wait for Register OK and Activate FGCNT "AT+QIACT"
|
---|
43 | // 10: Wait for activate OK
|
---|
44 |
|
---|
45 | int ct=theGSM3ShieldV1ModemCore.getCommandCounter();
|
---|
46 | if(ct==1)
|
---|
47 | {
|
---|
48 | //AT+CGATT
|
---|
49 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGATT,false);
|
---|
50 | theGSM3ShieldV1ModemCore.print(1);
|
---|
51 | theGSM3ShieldV1ModemCore.print('\r');
|
---|
52 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
53 | }
|
---|
54 | else if(ct==2)
|
---|
55 | {
|
---|
56 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
57 | {
|
---|
58 | if(resp)
|
---|
59 | {
|
---|
60 | //AT+QIFGCNT
|
---|
61 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIFGCNT=0"));
|
---|
62 | theGSM3ShieldV1ModemCore.setCommandCounter(3);
|
---|
63 | }
|
---|
64 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | else if(ct==3)
|
---|
68 | {
|
---|
69 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
70 | {
|
---|
71 | if(resp)
|
---|
72 | {
|
---|
73 | // Great. Go for the next step
|
---|
74 | //DEBUG
|
---|
75 | //Serial.println("AT+QICSGP.");
|
---|
76 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QICSGP=1,\""),false);
|
---|
77 | theGSM3ShieldV1ModemCore.print(theGSM3ShieldV1ModemCore.getPhoneNumber());
|
---|
78 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_SEPARATOR,false);
|
---|
79 | theGSM3ShieldV1ModemCore.print(user);
|
---|
80 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_SEPARATOR,false);
|
---|
81 | theGSM3ShieldV1ModemCore.print(passwd);
|
---|
82 | theGSM3ShieldV1ModemCore.print("\"\r");
|
---|
83 | theGSM3ShieldV1ModemCore.setCommandCounter(4);
|
---|
84 | }
|
---|
85 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | else if(ct==4)
|
---|
89 | {
|
---|
90 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
91 | {
|
---|
92 | if(resp)
|
---|
93 | {
|
---|
94 | // AT+QIMUX=1 for multisocket
|
---|
95 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIMUX=0"));
|
---|
96 | theGSM3ShieldV1ModemCore.setCommandCounter(5);
|
---|
97 | }
|
---|
98 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | else if(ct==5)
|
---|
102 | {
|
---|
103 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
104 | {
|
---|
105 | if(resp)
|
---|
106 | {
|
---|
107 | //AT+QIMODE=0 for multisocket
|
---|
108 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIMODE=1"));
|
---|
109 | theGSM3ShieldV1ModemCore.setCommandCounter(6);
|
---|
110 | }
|
---|
111 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
112 | }
|
---|
113 | }
|
---|
114 | else if(ct==6)
|
---|
115 | {
|
---|
116 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
117 | {
|
---|
118 | if(resp)
|
---|
119 | {
|
---|
120 | // AT+QINDI=1
|
---|
121 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QINDI=1"));
|
---|
122 | theGSM3ShieldV1ModemCore.setCommandCounter(8);
|
---|
123 | }
|
---|
124 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | else if(ct==8)
|
---|
128 | {
|
---|
129 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
130 | {
|
---|
131 | if(resp)
|
---|
132 | {
|
---|
133 | // AT+QIREGAPP
|
---|
134 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIREGAPP"));
|
---|
135 | theGSM3ShieldV1ModemCore.setCommandCounter(9);
|
---|
136 | }
|
---|
137 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | else if(ct==9)
|
---|
141 | {
|
---|
142 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
143 | {
|
---|
144 | if(resp)
|
---|
145 | {
|
---|
146 | // AT+QIACT
|
---|
147 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIACT"));
|
---|
148 | theGSM3ShieldV1ModemCore.setCommandCounter(10);
|
---|
149 | }
|
---|
150 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else if(ct==10)
|
---|
154 | {
|
---|
155 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
156 | {
|
---|
157 | if (resp)
|
---|
158 | {
|
---|
159 | theGSM3ShieldV1ModemCore.setStatus(GPRS_READY);
|
---|
160 | theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
161 | }
|
---|
162 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | //Detach GPRS main function.
|
---|
168 | GSM3_NetworkStatus_t GSM3ShieldV1DataNetworkProvider::detachGPRS(bool synchronous)
|
---|
169 | {
|
---|
170 | theGSM3ShieldV1ModemCore.openCommand(this,DETACHGPRS);
|
---|
171 | theGSM3ShieldV1ModemCore.setStatus(CONNECTING);
|
---|
172 | detachGPRSContinue();
|
---|
173 |
|
---|
174 | if(synchronous)
|
---|
175 | {
|
---|
176 | while(ready()==0)
|
---|
177 | delay(1);
|
---|
178 | }
|
---|
179 |
|
---|
180 | return theGSM3ShieldV1ModemCore.getStatus();
|
---|
181 | }
|
---|
182 |
|
---|
183 | void GSM3ShieldV1DataNetworkProvider::detachGPRSContinue()
|
---|
184 | {
|
---|
185 | bool resp;
|
---|
186 | // 1: Detach to GPRS service "AT+CGATT=0"
|
---|
187 | // 2: Wait dettach +PDP DEACT
|
---|
188 | // 3: Wait for OK
|
---|
189 |
|
---|
190 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
191 | case 1:
|
---|
192 | //AT+CGATT=0
|
---|
193 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGATT,false);
|
---|
194 | theGSM3ShieldV1ModemCore.print(0);
|
---|
195 | theGSM3ShieldV1ModemCore.print('\r');
|
---|
196 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
197 | break;
|
---|
198 | case 2:
|
---|
199 | char auxLocate[12];
|
---|
200 | prepareAuxLocate(PSTR("+PDP DEACT"), auxLocate);
|
---|
201 | if(theGSM3ShieldV1ModemCore.theBuffer().locate(auxLocate))
|
---|
202 | {
|
---|
203 | if(resp)
|
---|
204 | {
|
---|
205 | // Received +PDP DEACT;
|
---|
206 | theGSM3ShieldV1ModemCore.setCommandCounter(3);
|
---|
207 | }
|
---|
208 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
209 | }
|
---|
210 | break;
|
---|
211 | case 3:
|
---|
212 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
213 | {
|
---|
214 | // OK received
|
---|
215 | if (resp)
|
---|
216 | {
|
---|
217 | theGSM3ShieldV1ModemCore.setStatus(GSM_READY);
|
---|
218 | theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
219 | }
|
---|
220 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
221 | }
|
---|
222 | theGSM3ShieldV1ModemCore.theBuffer().flush();
|
---|
223 | theGSM3ShieldV1ModemCore.gss.spaceAvailable();
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | //QILOCIP parse.
|
---|
229 | bool GSM3ShieldV1DataNetworkProvider::parseQILOCIP_rsp(char* LocalIP, int LocalIPlength, bool& rsp)
|
---|
230 | {
|
---|
231 | if (!(theGSM3ShieldV1ModemCore.theBuffer().extractSubstring("\r\n","\r\n", LocalIP, LocalIPlength)))
|
---|
232 | rsp = false;
|
---|
233 | else
|
---|
234 | rsp = true;
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | //Get IP main function.
|
---|
239 | int GSM3ShieldV1DataNetworkProvider::getIP(char* LocalIP, int LocalIPlength)
|
---|
240 | {
|
---|
241 | theGSM3ShieldV1ModemCore.setPhoneNumber(LocalIP);
|
---|
242 | theGSM3ShieldV1ModemCore.setPort(LocalIPlength);
|
---|
243 | theGSM3ShieldV1ModemCore.openCommand(this,GETIP);
|
---|
244 | getIPContinue();
|
---|
245 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
246 | }
|
---|
247 |
|
---|
248 | void GSM3ShieldV1DataNetworkProvider::getIPContinue()
|
---|
249 | {
|
---|
250 |
|
---|
251 | bool resp;
|
---|
252 | // 1: Read Local IP "AT+QILOCIP"
|
---|
253 | // 2: Waiting for IP.
|
---|
254 |
|
---|
255 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
256 | case 1:
|
---|
257 | //AT+QILOCIP
|
---|
258 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QILOCIP"));
|
---|
259 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
260 | break;
|
---|
261 | case 2:
|
---|
262 | if(parseQILOCIP_rsp(theGSM3ShieldV1ModemCore.getPhoneNumber(), theGSM3ShieldV1ModemCore.getPort(), resp))
|
---|
263 | {
|
---|
264 | if (resp)
|
---|
265 | theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
266 | else
|
---|
267 | theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
268 | }
|
---|
269 | theGSM3ShieldV1ModemCore.theBuffer().flush();
|
---|
270 | theGSM3ShieldV1ModemCore.gss.spaceAvailable();
|
---|
271 | break;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | //Get IP with IPAddress object
|
---|
276 | IPAddress GSM3ShieldV1DataNetworkProvider::getIPAddress() {
|
---|
277 | char ip_temp[15]="";
|
---|
278 | getIP(ip_temp, 15);
|
---|
279 | unsigned long m=millis();
|
---|
280 |
|
---|
281 | while((millis()-m)<10*1000 && (!ready())){
|
---|
282 | // wait for a response from the modem:
|
---|
283 | delay(100);
|
---|
284 | }
|
---|
285 | IPAddress ip;
|
---|
286 | inet_aton(ip_temp, ip);
|
---|
287 | return ip;
|
---|
288 | }
|
---|
289 |
|
---|
290 | int GSM3ShieldV1DataNetworkProvider::inet_aton(const char* aIPAddrString, IPAddress& aResult)
|
---|
291 | {
|
---|
292 | // See if we've been given a valid IP address
|
---|
293 | const char* p =aIPAddrString;
|
---|
294 | while (*p &&
|
---|
295 | ( (*p == '.') || (*p >= '0') || (*p <= '9') ))
|
---|
296 | {
|
---|
297 | p++;
|
---|
298 | }
|
---|
299 |
|
---|
300 | if (*p == '\0')
|
---|
301 | {
|
---|
302 | // It's looking promising, we haven't found any invalid characters
|
---|
303 | p = aIPAddrString;
|
---|
304 | int segment =0;
|
---|
305 | int segmentValue =0;
|
---|
306 | while (*p && (segment < 4))
|
---|
307 | {
|
---|
308 | if (*p == '.')
|
---|
309 | {
|
---|
310 | // We've reached the end of a segment
|
---|
311 | if (segmentValue > 255)
|
---|
312 | {
|
---|
313 | // You can't have IP address segments that don't fit in a byte
|
---|
314 | return 0;
|
---|
315 | }
|
---|
316 | else
|
---|
317 | {
|
---|
318 | aResult[segment] = (byte)segmentValue;
|
---|
319 | segment++;
|
---|
320 | segmentValue = 0;
|
---|
321 | }
|
---|
322 | }
|
---|
323 | else
|
---|
324 | {
|
---|
325 | // Next digit
|
---|
326 | segmentValue = (segmentValue*10)+(*p - '0');
|
---|
327 | }
|
---|
328 | p++;
|
---|
329 | }
|
---|
330 | // We've reached the end of address, but there'll still be the last
|
---|
331 | // segment to deal with
|
---|
332 | if ((segmentValue > 255) || (segment > 3))
|
---|
333 | {
|
---|
334 | // You can't have IP address segments that don't fit in a byte,
|
---|
335 | // or more than four segments
|
---|
336 | return 0;
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | aResult[segment] = (byte)segmentValue;
|
---|
341 | return 1;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | else
|
---|
345 | {
|
---|
346 | return 0;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | //Response management.
|
---|
351 | void GSM3ShieldV1DataNetworkProvider::manageResponse(byte from, byte to)
|
---|
352 | {
|
---|
353 | switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
|
---|
354 | {
|
---|
355 | case ATTACHGPRS:
|
---|
356 | attachGPRSContinue();
|
---|
357 | break;
|
---|
358 | case DETACHGPRS:
|
---|
359 | detachGPRSContinue();
|
---|
360 | break;
|
---|
361 | case GETIP:
|
---|
362 | getIPContinue();
|
---|
363 | break;
|
---|
364 | }
|
---|
365 | }
|
---|