-
Notifications
You must be signed in to change notification settings - Fork 713
/
TinyGsmClientSIM70xx.h
459 lines (407 loc) · 14.4 KB
/
TinyGsmClientSIM70xx.h
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/**
* @file TinyGsmClientSIM70xx.h
* @author Volodymyr Shymanskyy
* @license LGPL-3.0
* @copyright Copyright (c) 2016 Volodymyr Shymanskyy
* @date Nov 2016
*/
#ifndef SRC_TINYGSMCLIENTSIM70XX_H_
#define SRC_TINYGSMCLIENTSIM70XX_H_
// #define TINY_GSM_DEBUG Serial
// #define TINY_GSM_USE_HEX
#include "TinyGsmBattery.tpp"
#include "TinyGsmGPRS.tpp"
#include "TinyGsmGPS.tpp"
#include "TinyGsmModem.tpp"
#include "TinyGsmSMS.tpp"
#include "TinyGsmTime.tpp"
#include "TinyGsmNTP.tpp"
#include "TinyGsmGSMLocation.tpp"
#define GSM_NL "\r\n"
static const char GSM_OK[] TINY_GSM_PROGMEM = "OK" GSM_NL;
static const char GSM_ERROR[] TINY_GSM_PROGMEM = "ERROR" GSM_NL;
#if defined TINY_GSM_DEBUG
static const char GSM_CME_ERROR[] TINY_GSM_PROGMEM = GSM_NL "+CME ERROR:";
static const char GSM_CMS_ERROR[] TINY_GSM_PROGMEM = GSM_NL "+CMS ERROR:";
#endif
enum RegStatus {
REG_NO_RESULT = -1,
REG_UNREGISTERED = 0,
REG_SEARCHING = 2,
REG_DENIED = 3,
REG_OK_HOME = 1,
REG_OK_ROAMING = 5,
REG_UNKNOWN = 4,
};
template <class modemType>
class TinyGsmSim70xx : public TinyGsmModem<TinyGsmSim70xx<modemType>>,
public TinyGsmGPRS<TinyGsmSim70xx<modemType>>,
public TinyGsmSMS<TinyGsmSim70xx<modemType>>,
public TinyGsmGPS<TinyGsmSim70xx<modemType>>,
public TinyGsmTime<TinyGsmSim70xx<modemType>>,
public TinyGsmNTP<TinyGsmSim70xx<modemType>>,
public TinyGsmBattery<TinyGsmSim70xx<modemType>>,
public TinyGsmGSMLocation<TinyGsmSim70xx<modemType>> {
friend class TinyGsmModem<TinyGsmSim70xx<modemType>>;
friend class TinyGsmGPRS<TinyGsmSim70xx<modemType>>;
friend class TinyGsmSMS<TinyGsmSim70xx<modemType>>;
friend class TinyGsmGPS<TinyGsmSim70xx<modemType>>;
friend class TinyGsmTime<TinyGsmSim70xx<modemType>>;
friend class TinyGsmNTP<TinyGsmSim70xx<modemType>>;
friend class TinyGsmBattery<TinyGsmSim70xx<modemType>>;
friend class TinyGsmGSMLocation<TinyGsmSim70xx<modemType>>;
/*
* CRTP Helper
*/
protected:
inline const modemType& thisModem() const {
return static_cast<const modemType&>(*this);
}
inline modemType& thisModem() {
return static_cast<modemType&>(*this);
}
/*
* Constructor
*/
public:
explicit TinyGsmSim70xx(Stream& stream) : stream(stream) {}
/*
* Basic functions
*/
protected:
bool initImpl(const char* pin = NULL) {
return thisModem().initImpl(pin);
}
String getModemNameImpl() {
String name = "SIMCom SIM7000";
thisModem().sendAT(GF("+GMM"));
String res2;
if (thisModem().waitResponse(5000L, res2) != 1) { return name; }
res2.replace(GSM_NL "OK" GSM_NL, "");
res2.replace("_", " ");
res2.trim();
name = res2;
return name;
}
bool factoryDefaultImpl() { // these commands aren't supported
thisModem().sendAT(GF("&FZE0&W")); // Factory + Reset + Echo Off + Write
thisModem().waitResponse();
thisModem().sendAT(GF("+IPR=0")); // Auto-baud
thisModem().waitResponse();
thisModem().sendAT(GF("+IFC=0,0")); // No Flow Control
thisModem().waitResponse();
thisModem().sendAT(GF("+ICF=3,3")); // 8 data 0 parity 1 stop
thisModem().waitResponse();
thisModem().sendAT(GF("+CSCLK=0")); // Disable Slow Clock
thisModem().waitResponse();
thisModem().sendAT(GF("&W")); // Write configuration
return thisModem().waitResponse() == 1;
}
/*
* Power functions
*/
protected:
bool restartImpl(const char* pin = NULL) {
thisModem().sendAT(GF("E0")); // Echo Off
thisModem().waitResponse();
if (!thisModem().setPhoneFunctionality(0)) { return false; }
if (!thisModem().setPhoneFunctionality(1, true)) { return false; }
thisModem().waitResponse(30000L, GF("SMS Ready"));
return thisModem().initImpl(pin);
}
bool powerOffImpl() {
thisModem().sendAT(GF("+CPOWD=1"));
return thisModem().waitResponse(GF("NORMAL POWER DOWN")) == 1;
}
// During sleep, the SIM70xx module has its serial communication disabled.
// In order to reestablish communication pull the DRT-pin of the SIM70xx
// module LOW for at least 50ms. Then use this function to disable sleep
// mode. The DTR-pin can then be released again.
bool sleepEnableImpl(bool enable = true) {
thisModem().sendAT(GF("+CSCLK="), enable);
return thisModem().waitResponse() == 1;
}
bool setPhoneFunctionalityImpl(uint8_t fun, bool reset = false) {
thisModem().sendAT(GF("+CFUN="), fun, reset ? ",1" : "");
return thisModem().waitResponse(10000L) == 1;
}
/*
* Generic network functions
*/
public:
RegStatus getRegistrationStatus() {
RegStatus epsStatus =
(RegStatus)thisModem().getRegistrationStatusXREG("CEREG");
// If we're connected on EPS, great!
if (epsStatus == REG_OK_HOME || epsStatus == REG_OK_ROAMING) {
return epsStatus;
} else {
// Otherwise, check GPRS network status
// We could be using GPRS fall-back or the board could be being moody
return (RegStatus)thisModem().getRegistrationStatusXREG("CGREG");
}
}
protected:
bool isNetworkConnectedImpl() {
RegStatus s = getRegistrationStatus();
return (s == REG_OK_HOME || s == REG_OK_ROAMING);
}
public:
String getNetworkModes() {
// Get the help string, not the setting value
thisModem().sendAT(GF("+CNMP=?"));
if (thisModem().waitResponse(GF(GSM_NL "+CNMP:")) != 1) { return ""; }
String res = stream.readStringUntil('\n');
thisModem().waitResponse();
return res;
}
int16_t getNetworkMode() {
thisModem().sendAT(GF("+CNMP?"));
if (thisModem().waitResponse(GF(GSM_NL "+CNMP:")) != 1) { return false; }
int16_t mode = thisModem().streamGetIntBefore('\n');
thisModem().waitResponse();
return mode;
}
bool setNetworkMode(uint8_t mode) {
// 2 Automatic
// 13 GSM only
// 38 LTE only
// 51 GSM and LTE only
thisModem().sendAT(GF("+CNMP="), mode);
return thisModem().waitResponse() == 1;
}
String getPreferredModes() {
// Get the help string, not the setting value
thisModem().sendAT(GF("+CMNB=?"));
if (thisModem().waitResponse(GF(GSM_NL "+CMNB:")) != 1) { return ""; }
String res = stream.readStringUntil('\n');
thisModem().waitResponse();
return res;
}
int16_t getPreferredMode() {
thisModem().sendAT(GF("+CMNB?"));
if (thisModem().waitResponse(GF(GSM_NL "+CMNB:")) != 1) { return false; }
int16_t mode = thisModem().streamGetIntBefore('\n');
thisModem().waitResponse();
return mode;
}
bool setPreferredMode(uint8_t mode) {
// 1 CAT-M
// 2 NB-IoT
// 3 CAT-M and NB-IoT
thisModem().sendAT(GF("+CMNB="), mode);
return thisModem().waitResponse() == 1;
}
bool getNetworkSystemMode(bool& n, int16_t& stat) {
// n: whether to automatically report the system mode info
// stat: the current service. 0 if it not connected
thisModem().sendAT(GF("+CNSMOD?"));
if (thisModem().waitResponse(GF(GSM_NL "+CNSMOD:")) != 1) { return false; }
n = thisModem().streamGetIntBefore(',') != 0;
stat = thisModem().streamGetIntBefore('\n');
thisModem().waitResponse();
return true;
}
bool setNetworkSystemMode(bool n) {
// n: whether to automatically report the system mode info
thisModem().sendAT(GF("+CNSMOD="), int8_t(n));
return thisModem().waitResponse() == 1;
}
String getLocalIPImpl() {
return thisModem().getLocalIPImpl();
}
/*
* GPRS functions
*/
protected:
// should implement in sub-classes
bool gprsConnectImpl(const char* apn, const char* user = NULL,
const char* pwd = NULL) {
return thisModem().gprsConnectImpl(apn, user, pwd);
}
bool gprsDisconnectImpl() {
return thisModem().gprsDisconnectImpl();
}
/*
* SIM card functions
*/
protected:
// Doesn't return the "+CCID" before the number
String getSimCCIDImpl() {
thisModem().sendAT(GF("+CCID"));
if (thisModem().waitResponse(GF(GSM_NL)) != 1) { return ""; }
String res = stream.readStringUntil('\n');
thisModem().waitResponse();
res.trim();
return res;
}
/*
* Messaging functions
*/
protected:
// Follows all messaging functions per template
/*
* GPS/GNSS/GLONASS location functions
*/
protected:
// enable GPS
bool enableGPSImpl() {
thisModem().sendAT(GF("+CGNSPWR=1"));
if (thisModem().waitResponse() != 1) { return false; }
return true;
}
bool disableGPSImpl() {
thisModem().sendAT(GF("+CGNSPWR=0"));
if (thisModem().waitResponse() != 1) { return false; }
return true;
}
// get the RAW GPS output
String getGPSrawImpl() {
thisModem().sendAT(GF("+CGNSINF"));
if (thisModem().waitResponse(10000L, GF(GSM_NL "+CGNSINF:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
thisModem().waitResponse();
res.trim();
return res;
}
// get GPS informations
bool getGPSImpl(float* lat, float* lon, float* speed = 0, float* alt = 0,
int* vsat = 0, int* usat = 0, float* accuracy = 0,
int* year = 0, int* month = 0, int* day = 0, int* hour = 0,
int* minute = 0, int* second = 0) {
thisModem().sendAT(GF("+CGNSINF"));
if (thisModem().waitResponse(10000L, GF(GSM_NL "+CGNSINF:")) != 1) {
return false;
}
thisModem().streamSkipUntil(','); // GNSS run status
if (thisModem().streamGetIntBefore(',') == 1) { // fix status
// init variables
float ilat = 0;
float ilon = 0;
float ispeed = 0;
float ialt = 0;
int ivsat = 0;
int iusat = 0;
float iaccuracy = 0;
int iyear = 0;
int imonth = 0;
int iday = 0;
int ihour = 0;
int imin = 0;
float secondWithSS = 0;
// UTC date & Time
iyear = thisModem().streamGetIntLength(4); // Four digit year
imonth = thisModem().streamGetIntLength(2); // Two digit month
iday = thisModem().streamGetIntLength(2); // Two digit day
ihour = thisModem().streamGetIntLength(2); // Two digit hour
imin = thisModem().streamGetIntLength(2); // Two digit minute
secondWithSS = thisModem().streamGetFloatBefore(
','); // 6 digit second with subseconds
ilat = thisModem().streamGetFloatBefore(','); // Latitude
ilon = thisModem().streamGetFloatBefore(','); // Longitude
ialt = thisModem().streamGetFloatBefore(
','); // MSL Altitude. Unit is meters
ispeed = thisModem().streamGetFloatBefore(
','); // Speed Over Ground. Unit is knots.
thisModem().streamSkipUntil(','); // Course Over Ground. Degrees.
thisModem().streamSkipUntil(','); // Fix Mode
thisModem().streamSkipUntil(','); // Reserved1
iaccuracy = thisModem().streamGetFloatBefore(
','); // Horizontal Dilution Of Precision
thisModem().streamSkipUntil(','); // Position Dilution Of Precision
thisModem().streamSkipUntil(','); // Vertical Dilution Of Precision
thisModem().streamSkipUntil(','); // Reserved2
ivsat = thisModem().streamGetIntBefore(','); // GNSS Satellites in View
iusat = thisModem().streamGetIntBefore(','); // GNSS Satellites Used
thisModem().streamSkipUntil(','); // GLONASS Satellites Used
thisModem().streamSkipUntil(','); // Reserved3
thisModem().streamSkipUntil(','); // C/N0 max
thisModem().streamSkipUntil(','); // HPA
thisModem().streamSkipUntil('\n'); // VPA
// Set pointers
if (lat != NULL) *lat = ilat;
if (lon != NULL) *lon = ilon;
if (speed != NULL) *speed = ispeed;
if (alt != NULL) *alt = ialt;
if (vsat != NULL) *vsat = ivsat;
if (usat != NULL) *usat = iusat;
if (accuracy != NULL) *accuracy = iaccuracy;
if (iyear < 2000) iyear += 2000;
if (year != NULL) *year = iyear;
if (month != NULL) *month = imonth;
if (day != NULL) *day = iday;
if (hour != NULL) *hour = ihour;
if (minute != NULL) *minute = imin;
if (second != NULL) *second = static_cast<int>(secondWithSS);
thisModem().waitResponse();
return true;
}
thisModem().streamSkipUntil('\n'); // toss the row of commas
thisModem().waitResponse();
return false;
}
/*
* Time functions
*/
// Can follow CCLK as per template
/*
* NTP server functions
*/
// Can sync with server using CNTP as per template
/*
* Battery functions
*/
protected:
// Follows all battery functions per template
/*
* Client related functions
*/
// should implement in sub-classes
/*
* Utilities
*/
public:
// should implement in sub-classes
int8_t waitResponse(uint32_t timeout_ms, String& data,
GsmConstStr r1 = GFP(GSM_OK),
GsmConstStr r2 = GFP(GSM_ERROR),
#if defined TINY_GSM_DEBUG
GsmConstStr r3 = GFP(GSM_CME_ERROR),
GsmConstStr r4 = GFP(GSM_CMS_ERROR),
#else
GsmConstStr r3 = NULL, GsmConstStr r4 = NULL,
#endif
GsmConstStr r5 = NULL) {
return thisModem().waitResponse(timeout_ms, data, r1, r2, r3, r4, r5);
}
int8_t waitResponse(uint32_t timeout_ms, GsmConstStr r1 = GFP(GSM_OK),
GsmConstStr r2 = GFP(GSM_ERROR),
#if defined TINY_GSM_DEBUG
GsmConstStr r3 = GFP(GSM_CME_ERROR),
GsmConstStr r4 = GFP(GSM_CMS_ERROR),
#else
GsmConstStr r3 = NULL, GsmConstStr r4 = NULL,
#endif
GsmConstStr r5 = NULL) {
String data;
return thisModem().waitResponse(timeout_ms, data, r1, r2, r3, r4, r5);
}
int8_t waitResponse(GsmConstStr r1 = GFP(GSM_OK),
GsmConstStr r2 = GFP(GSM_ERROR),
#if defined TINY_GSM_DEBUG
GsmConstStr r3 = GFP(GSM_CME_ERROR),
GsmConstStr r4 = GFP(GSM_CMS_ERROR),
#else
GsmConstStr r3 = NULL, GsmConstStr r4 = NULL,
#endif
GsmConstStr r5 = NULL) {
return thisModem().waitResponse(1000, r1, r2, r3, r4, r5);
}
public:
Stream& stream;
protected:
const char* gsmNL = GSM_NL;
};
#endif // SRC_TINYGSMCLIENTSIM70XX_H_