1 | #include <GSM3ShieldV1AccessProvider.h>
|
---|
2 | #include <Arduino.h>
|
---|
3 | #include "GSM3IO.h"
|
---|
4 |
|
---|
5 | #define __TOUTSHUTDOWN__ 5000
|
---|
6 | #define __TOUTMODEMCONFIGURATION__ 5000//equivalent to 30000 because of time in interrupt routine.
|
---|
7 | #define __TOUTAT__ 1000
|
---|
8 |
|
---|
9 | const char _command_AT[] PROGMEM = "AT";
|
---|
10 | const char _command_CGREG[] PROGMEM = "AT+CGREG?";
|
---|
11 |
|
---|
12 |
|
---|
13 | GSM3ShieldV1AccessProvider::GSM3ShieldV1AccessProvider(bool debug)
|
---|
14 | {
|
---|
15 | theGSM3ShieldV1ModemCore.setDebug(debug);
|
---|
16 |
|
---|
17 | }
|
---|
18 |
|
---|
19 | void GSM3ShieldV1AccessProvider::manageResponse(byte from, byte to)
|
---|
20 | {
|
---|
21 | switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
|
---|
22 | {
|
---|
23 | case MODEMCONFIG:
|
---|
24 | ModemConfigurationContinue();
|
---|
25 | break;
|
---|
26 | case ALIVETEST:
|
---|
27 | isModemAliveContinue();
|
---|
28 | break;
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | ///////////////////////////////////////////////////////CONFIGURATION FUNCTIONS///////////////////////////////////////////////////////////////////
|
---|
33 |
|
---|
34 | // Begin
|
---|
35 | // Restart or start the modem
|
---|
36 | // May be synchronous
|
---|
37 | GSM3_NetworkStatus_t GSM3ShieldV1AccessProvider::begin(char* pin, bool restart, bool synchronous)
|
---|
38 | {
|
---|
39 | pinMode(__RESETPIN__, OUTPUT);
|
---|
40 |
|
---|
41 | #ifdef TTOPEN_V1
|
---|
42 | pinMode(__POWERPIN__, OUTPUT);
|
---|
43 | digitalWrite(__POWERPIN__, HIGH);
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | // If asked for modem restart, restart
|
---|
47 | if (restart)
|
---|
48 | HWrestart();
|
---|
49 | else
|
---|
50 | HWstart();
|
---|
51 |
|
---|
52 | theGSM3ShieldV1ModemCore.gss.begin(9600);
|
---|
53 | // Launch modem configuration commands
|
---|
54 | ModemConfiguration(pin);
|
---|
55 | // If synchronous, wait till ModemConfiguration is over
|
---|
56 | if(synchronous)
|
---|
57 | {
|
---|
58 | // if we shorten this delay, the command fails
|
---|
59 | while(ready()==0)
|
---|
60 | delay(1000);
|
---|
61 | }
|
---|
62 | return getStatus();
|
---|
63 | }
|
---|
64 |
|
---|
65 | //HWrestart.
|
---|
66 | int GSM3ShieldV1AccessProvider::HWrestart()
|
---|
67 | {
|
---|
68 | #ifdef TTOPEN_V1
|
---|
69 | digitalWrite(__POWERPIN__, HIGH);
|
---|
70 | delay(1000);
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | theGSM3ShieldV1ModemCore.setStatus(IDLE);
|
---|
74 | digitalWrite(__RESETPIN__, HIGH);
|
---|
75 | delay(12000);
|
---|
76 | digitalWrite(__RESETPIN__, LOW);
|
---|
77 | delay(1000);
|
---|
78 | return 1; //configandwait(pin);
|
---|
79 | }
|
---|
80 |
|
---|
81 | //HWrestart.
|
---|
82 | int GSM3ShieldV1AccessProvider::HWstart()
|
---|
83 | {
|
---|
84 |
|
---|
85 | theGSM3ShieldV1ModemCore.setStatus(IDLE);
|
---|
86 | digitalWrite(__RESETPIN__, HIGH);
|
---|
87 | delay(2000);
|
---|
88 | digitalWrite(__RESETPIN__, LOW);
|
---|
89 | //delay(1000);
|
---|
90 |
|
---|
91 | return 1; //configandwait(pin);
|
---|
92 | }
|
---|
93 |
|
---|
94 | //Initial configuration main function.
|
---|
95 | int GSM3ShieldV1AccessProvider::ModemConfiguration(char* pin)
|
---|
96 | {
|
---|
97 | theGSM3ShieldV1ModemCore.setPhoneNumber(pin);
|
---|
98 | theGSM3ShieldV1ModemCore.openCommand(this,MODEMCONFIG);
|
---|
99 | theGSM3ShieldV1ModemCore.setStatus(CONNECTING);
|
---|
100 | ModemConfigurationContinue();
|
---|
101 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
102 | }
|
---|
103 |
|
---|
104 | //Initial configuration continue function.
|
---|
105 | void GSM3ShieldV1AccessProvider::ModemConfigurationContinue()
|
---|
106 | {
|
---|
107 | bool resp;
|
---|
108 |
|
---|
109 | // 1: Send AT
|
---|
110 | // 2: Wait AT OK and SetPin or CGREG
|
---|
111 | // 3: Wait Pin OK and CGREG
|
---|
112 | // 4: Wait CGREG and Flow SW control or CGREG
|
---|
113 | // 5: Wait IFC OK and SMS Text Mode
|
---|
114 | // 6: Wait SMS text Mode OK and Calling line identification
|
---|
115 | // 7: Wait Calling Line Id OK and Echo off
|
---|
116 | // 8: Wait for OK and COLP command for connecting line identification.
|
---|
117 | // 9: Wait for OK.
|
---|
118 | int ct=theGSM3ShieldV1ModemCore.getCommandCounter();
|
---|
119 | if(ct==1)
|
---|
120 | {
|
---|
121 | // Launch AT
|
---|
122 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
123 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_AT);
|
---|
124 | }
|
---|
125 | else if(ct==2)
|
---|
126 | {
|
---|
127 | // Wait for AT - OK.
|
---|
128 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
129 | {
|
---|
130 | if(resp)
|
---|
131 | {
|
---|
132 | // OK received
|
---|
133 | if(theGSM3ShieldV1ModemCore.getPhoneNumber() && (theGSM3ShieldV1ModemCore.getPhoneNumber()[0]!=0))
|
---|
134 | {
|
---|
135 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CPIN="), false);
|
---|
136 | theGSM3ShieldV1ModemCore.setCommandCounter(3);
|
---|
137 | theGSM3ShieldV1ModemCore.genericCommand_rqc(theGSM3ShieldV1ModemCore.getPhoneNumber());
|
---|
138 | }
|
---|
139 | else
|
---|
140 | {
|
---|
141 | //DEBUG
|
---|
142 | //Serial.println("AT+CGREG?");
|
---|
143 | theGSM3ShieldV1ModemCore.setCommandCounter(4);
|
---|
144 | theGSM3ShieldV1ModemCore.takeMilliseconds();
|
---|
145 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGREG);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
149 | }
|
---|
150 | }
|
---|
151 | else if(ct==3)
|
---|
152 | {
|
---|
153 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
154 | {
|
---|
155 | if(resp)
|
---|
156 | {
|
---|
157 | theGSM3ShieldV1ModemCore.setCommandCounter(4);
|
---|
158 | theGSM3ShieldV1ModemCore.takeMilliseconds();
|
---|
159 | theGSM3ShieldV1ModemCore.delayInsideInterrupt(2000);
|
---|
160 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGREG);
|
---|
161 | }
|
---|
162 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | else if(ct==4)
|
---|
166 | {
|
---|
167 | char auxLocate1 [12];
|
---|
168 | char auxLocate2 [12];
|
---|
169 | prepareAuxLocate(PSTR("+CGREG: 0,1"), auxLocate1);
|
---|
170 | prepareAuxLocate(PSTR("+CGREG: 0,5"), auxLocate2);
|
---|
171 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp, auxLocate1, auxLocate2))
|
---|
172 | {
|
---|
173 | if(resp)
|
---|
174 | {
|
---|
175 | theGSM3ShieldV1ModemCore.setCommandCounter(5);
|
---|
176 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+IFC=1,1"));
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | // If not, launch command again
|
---|
181 | if(theGSM3ShieldV1ModemCore.takeMilliseconds() > __TOUTMODEMCONFIGURATION__)
|
---|
182 | {
|
---|
183 | theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
184 | }
|
---|
185 | else
|
---|
186 | {
|
---|
187 | theGSM3ShieldV1ModemCore.delayInsideInterrupt(2000);
|
---|
188 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGREG);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | else if(ct==5)
|
---|
194 | {
|
---|
195 | // 5: Wait IFC OK
|
---|
196 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
197 | {
|
---|
198 | //Delay for SW flow control being active.
|
---|
199 | theGSM3ShieldV1ModemCore.delayInsideInterrupt(2000);
|
---|
200 | // 9: SMS Text Mode
|
---|
201 | theGSM3ShieldV1ModemCore.setCommandCounter(6);
|
---|
202 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CMGF=1"));
|
---|
203 | }
|
---|
204 | }
|
---|
205 | else if(ct==6)
|
---|
206 | {
|
---|
207 | // 6: Wait SMS text Mode OK
|
---|
208 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
209 | {
|
---|
210 | //Calling line identification
|
---|
211 | theGSM3ShieldV1ModemCore.setCommandCounter(7);
|
---|
212 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CLIP=1"));
|
---|
213 | }
|
---|
214 | }
|
---|
215 | else if(ct==7)
|
---|
216 | {
|
---|
217 | // 7: Wait Calling Line Id OK
|
---|
218 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
219 | {
|
---|
220 | // Echo off
|
---|
221 | theGSM3ShieldV1ModemCore.setCommandCounter(8);
|
---|
222 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("ATE0"));
|
---|
223 | }
|
---|
224 | }
|
---|
225 | else if(ct==8)
|
---|
226 | {
|
---|
227 | // 8: Wait ATEO OK, send COLP
|
---|
228 | // In Arduino Mega, attention, take away the COLP step
|
---|
229 | // It looks as we can only have 8 steps
|
---|
230 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
231 | {
|
---|
232 | theGSM3ShieldV1ModemCore.setCommandCounter(9);
|
---|
233 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+COLP=1"));
|
---|
234 | }
|
---|
235 | }
|
---|
236 | else if(ct==9)
|
---|
237 | {
|
---|
238 | // 9: Wait ATCOLP OK
|
---|
239 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
240 | {
|
---|
241 | if (resp)
|
---|
242 | {
|
---|
243 | theGSM3ShieldV1ModemCore.setStatus(GSM_READY);
|
---|
244 | theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
245 | }
|
---|
246 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | //Alive Test main function.
|
---|
252 | int GSM3ShieldV1AccessProvider::isAccessAlive()
|
---|
253 | {
|
---|
254 | theGSM3ShieldV1ModemCore.setCommandError(0);
|
---|
255 | theGSM3ShieldV1ModemCore.setCommandCounter(1);
|
---|
256 | theGSM3ShieldV1ModemCore.openCommand(this,ALIVETEST);
|
---|
257 | isModemAliveContinue();
|
---|
258 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
259 | }
|
---|
260 |
|
---|
261 | //Alive Test continue function.
|
---|
262 | void GSM3ShieldV1AccessProvider::isModemAliveContinue()
|
---|
263 | {
|
---|
264 | bool rsp;
|
---|
265 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
266 | case 1:
|
---|
267 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_AT);
|
---|
268 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
269 | break;
|
---|
270 | case 2:
|
---|
271 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(rsp))
|
---|
272 | {
|
---|
273 | if (rsp) theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
274 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
275 | }
|
---|
276 | break;
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | //Shutdown.
|
---|
281 | bool GSM3ShieldV1AccessProvider::shutdown()
|
---|
282 | {
|
---|
283 | unsigned long m;
|
---|
284 | bool resp;
|
---|
285 | char auxLocate [18];
|
---|
286 |
|
---|
287 | // It makes no sense to have an asynchronous shutdown
|
---|
288 | pinMode(__RESETPIN__, OUTPUT);
|
---|
289 | digitalWrite(__RESETPIN__, HIGH);
|
---|
290 | delay(1500);
|
---|
291 | digitalWrite(__RESETPIN__, LOW);
|
---|
292 | theGSM3ShieldV1ModemCore.setStatus(IDLE);
|
---|
293 | theGSM3ShieldV1ModemCore.gss.close();
|
---|
294 |
|
---|
295 | m=millis();
|
---|
296 | prepareAuxLocate(PSTR("POWER DOWN"), auxLocate);
|
---|
297 | while((millis()-m) < __TOUTSHUTDOWN__)
|
---|
298 | {
|
---|
299 | delay(1);
|
---|
300 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp, auxLocate))
|
---|
301 | return resp;
|
---|
302 | }
|
---|
303 | return false;
|
---|
304 | }
|
---|
305 |
|
---|
306 | //Secure shutdown.
|
---|
307 | bool GSM3ShieldV1AccessProvider::secureShutdown()
|
---|
308 | {
|
---|
309 | // It makes no sense to have an asynchronous shutdown
|
---|
310 | pinMode(__RESETPIN__, OUTPUT);
|
---|
311 | digitalWrite(__RESETPIN__, HIGH);
|
---|
312 | delay(900);
|
---|
313 | digitalWrite(__RESETPIN__, LOW);
|
---|
314 | theGSM3ShieldV1ModemCore.setStatus(OFF);
|
---|
315 | theGSM3ShieldV1ModemCore.gss.close();
|
---|
316 |
|
---|
317 | #ifdef TTOPEN_V1
|
---|
318 | _delay_ms(12000);
|
---|
319 | digitalWrite(__POWERPIN__, LOW);
|
---|
320 | #endif
|
---|
321 |
|
---|
322 | return true;
|
---|
323 | }
|
---|