-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathGSM3ShieldV1AccessProvider.cpp
356 lines (323 loc) · 9.31 KB
/
GSM3ShieldV1AccessProvider.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
/*
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 <GSM3ShieldV1AccessProvider.h>
#include <Arduino.h>
#include "GSM3IO.h"
#define __TOUTSHUTDOWN__ 5000
#define __TOUTMODEMCONFIGURATION__ 5000//equivalent to 30000 because of time in interrupt routine.
#define __TOUTAT__ 1000
const char _command_AT[] PROGMEM = "AT";
const char _command_CGREG[] PROGMEM = "AT+CGREG?";
GSM3ShieldV1AccessProvider::GSM3ShieldV1AccessProvider(bool debug)
{
theGSM3ShieldV1ModemCore.setDebug(debug);
}
void GSM3ShieldV1AccessProvider::manageResponse(byte from, byte to)
{
switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
{
case MODEMCONFIG:
ModemConfigurationContinue();
break;
case ALIVETEST:
isModemAliveContinue();
break;
}
}
///////////////////////////////////////////////////////CONFIGURATION FUNCTIONS///////////////////////////////////////////////////////////////////
// Begin
// Restart or start the modem
// May be synchronous
GSM3_NetworkStatus_t GSM3ShieldV1AccessProvider::begin(char* pin, bool restart, bool synchronous)
{
pinMode(__RESETPIN__, OUTPUT);
#ifdef TTOPEN_V1
pinMode(__POWERPIN__, OUTPUT);
digitalWrite(__POWERPIN__, HIGH);
#endif
// If asked for modem restart, restart
if (restart)
HWrestart();
else
HWstart();
theGSM3ShieldV1ModemCore.gss.begin(9600);
// Launch modem configuration commands
ModemConfiguration(pin);
// If synchronous, wait till ModemConfiguration is over
if(synchronous)
{
// if we shorten this delay, the command fails
while(ready()==0)
delay(1000);
}
return getStatus();
}
//HWrestart.
int GSM3ShieldV1AccessProvider::HWrestart()
{
#ifdef TTOPEN_V1
digitalWrite(__POWERPIN__, HIGH);
delay(1000);
#endif
theGSM3ShieldV1ModemCore.setStatus(IDLE);
digitalWrite(__RESETPIN__, HIGH);
delay(12000);
digitalWrite(__RESETPIN__, LOW);
delay(1000);
return 1; //configandwait(pin);
}
//HWrestart.
int GSM3ShieldV1AccessProvider::HWstart()
{
theGSM3ShieldV1ModemCore.setStatus(IDLE);
digitalWrite(__RESETPIN__, HIGH);
delay(2000);
digitalWrite(__RESETPIN__, LOW);
//delay(1000);
return 1; //configandwait(pin);
}
//Initial configuration main function.
int GSM3ShieldV1AccessProvider::ModemConfiguration(char* pin)
{
theGSM3ShieldV1ModemCore.setPhoneNumber(pin);
theGSM3ShieldV1ModemCore.openCommand(this,MODEMCONFIG);
theGSM3ShieldV1ModemCore.setStatus(CONNECTING);
ModemConfigurationContinue();
return theGSM3ShieldV1ModemCore.getCommandError();
}
//Initial configuration continue function.
void GSM3ShieldV1AccessProvider::ModemConfigurationContinue()
{
bool resp;
// 1: Send AT
// 2: Wait AT OK and SetPin or CGREG
// 3: Wait Pin OK and CGREG
// 4: Wait CGREG and Flow SW control or CGREG
// 5: Wait IFC OK and SMS Text Mode
// 6: Wait SMS text Mode OK and Calling line identification
// 7: Wait Calling Line Id OK and Echo off
// 8: Wait for OK and COLP command for connecting line identification.
// 9: Wait for OK.
int ct=theGSM3ShieldV1ModemCore.getCommandCounter();
if(ct==1)
{
// Launch AT
theGSM3ShieldV1ModemCore.setCommandCounter(2);
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_AT);
}
else if(ct==2)
{
// Wait for AT - OK.
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
// OK received
if(theGSM3ShieldV1ModemCore.getPhoneNumber() && (theGSM3ShieldV1ModemCore.getPhoneNumber()[0]!=0))
{
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CPIN="), false);
theGSM3ShieldV1ModemCore.setCommandCounter(3);
theGSM3ShieldV1ModemCore.genericCommand_rqc(theGSM3ShieldV1ModemCore.getPhoneNumber());
}
else
{
//DEBUG
//Serial.println("AT+CGREG?");
theGSM3ShieldV1ModemCore.setCommandCounter(4);
theGSM3ShieldV1ModemCore.takeMilliseconds();
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGREG);
}
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==3)
{
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if(resp)
{
theGSM3ShieldV1ModemCore.setCommandCounter(4);
theGSM3ShieldV1ModemCore.takeMilliseconds();
theGSM3ShieldV1ModemCore.delayInsideInterrupt(2000);
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGREG);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
else if(ct==4)
{
char auxLocate1 [12];
char auxLocate2 [12];
prepareAuxLocate(PSTR("+CGREG: 0,1"), auxLocate1);
prepareAuxLocate(PSTR("+CGREG: 0,5"), auxLocate2);
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp, auxLocate1, auxLocate2))
{
if(resp)
{
theGSM3ShieldV1ModemCore.setCommandCounter(5);
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+IFC=1,1"));
}
else
{
// If not, launch command again
if(theGSM3ShieldV1ModemCore.takeMilliseconds() > __TOUTMODEMCONFIGURATION__)
{
theGSM3ShieldV1ModemCore.closeCommand(3);
}
else
{
theGSM3ShieldV1ModemCore.delayInsideInterrupt(2000);
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_CGREG);
}
}
}
}
else if(ct==5)
{
// 5: Wait IFC OK
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
//Delay for SW flow control being active.
theGSM3ShieldV1ModemCore.delayInsideInterrupt(2000);
// 9: SMS Text Mode
theGSM3ShieldV1ModemCore.setCommandCounter(6);
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CMGF=1"));
}
}
else if(ct==6)
{
// 6: Wait SMS text Mode OK
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
//Calling line identification
theGSM3ShieldV1ModemCore.setCommandCounter(7);
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CLIP=1"));
}
}
else if(ct==7)
{
// 7: Wait Calling Line Id OK
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
// Echo off
theGSM3ShieldV1ModemCore.setCommandCounter(8);
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("ATE0"));
}
}
else if(ct==8)
{
// 8: Wait ATEO OK, send COLP
// In Arduino Mega, attention, take away the COLP step
// It looks as we can only have 8 steps
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
theGSM3ShieldV1ModemCore.setCommandCounter(9);
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+COLP=1"));
}
}
else if(ct==9)
{
// 9: Wait ATCOLP OK
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
if (resp)
{
theGSM3ShieldV1ModemCore.setStatus(GSM_READY);
theGSM3ShieldV1ModemCore.closeCommand(1);
}
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
}
//Alive Test main function.
int GSM3ShieldV1AccessProvider::isAccessAlive()
{
theGSM3ShieldV1ModemCore.setCommandError(0);
theGSM3ShieldV1ModemCore.setCommandCounter(1);
theGSM3ShieldV1ModemCore.openCommand(this,ALIVETEST);
isModemAliveContinue();
return theGSM3ShieldV1ModemCore.getCommandError();
}
//Alive Test continue function.
void GSM3ShieldV1AccessProvider::isModemAliveContinue()
{
bool rsp;
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_AT);
theGSM3ShieldV1ModemCore.setCommandCounter(2);
break;
case 2:
if(theGSM3ShieldV1ModemCore.genericParse_rsp(rsp))
{
if (rsp) theGSM3ShieldV1ModemCore.closeCommand(1);
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
break;
}
}
//Shutdown.
bool GSM3ShieldV1AccessProvider::shutdown()
{
unsigned long m;
bool resp;
char auxLocate [18];
// It makes no sense to have an asynchronous shutdown
pinMode(__RESETPIN__, OUTPUT);
digitalWrite(__RESETPIN__, HIGH);
delay(1500);
digitalWrite(__RESETPIN__, LOW);
theGSM3ShieldV1ModemCore.setStatus(IDLE);
theGSM3ShieldV1ModemCore.gss.close();
m=millis();
prepareAuxLocate(PSTR("POWER DOWN"), auxLocate);
while((millis()-m) < __TOUTSHUTDOWN__)
{
delay(1);
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp, auxLocate))
return resp;
}
return false;
}
//Secure shutdown.
bool GSM3ShieldV1AccessProvider::secureShutdown()
{
// It makes no sense to have an asynchronous shutdown
pinMode(__RESETPIN__, OUTPUT);
digitalWrite(__RESETPIN__, HIGH);
delay(900);
digitalWrite(__RESETPIN__, LOW);
theGSM3ShieldV1ModemCore.setStatus(OFF);
theGSM3ShieldV1ModemCore.gss.close();
#ifdef TTOPEN_V1
_delay_ms(12000);
digitalWrite(__POWERPIN__, LOW);
#endif
return true;
}