-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGsmModemCommon.cpp
492 lines (445 loc) · 14.8 KB
/
GsmModemCommon.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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include "GsmModemCommon.h"
#include <Arduino.h>
// Public
GsmModemCommon::GsmModemCommon(Stream& stream) : stream(stream) {}
void GsmModemCommon::begin(const char access_point_name[],
PacketDataProtocolType pdp_type,
const char user_name[],
const char password[]) {
this->access_point_name = access_point_name ? access_point_name : "";
this->pdp_type = pdp_type;
this->user_name = user_name ? user_name : "";
this->password = password ? password : "";
const char* pdp_type_string =
this->pdp_type == PacketDataProtocolType::IPv4v6 ? "IPV4V6"
: this->pdp_type == PacketDataProtocolType::IPv6 ? "IPV6"
: "IP";
ADVGSM_LOG(GsmSeverity::Info, "GsmModemCommon",
GF("Begin connection to %s@%s (%s)"), this->user_name,
this->access_point_name, pdp_type_string);
this->active = true;
bool success = checkConnection();
if (!success) {
this->retry_count++;
int32_t delay = retry_base_delay_ms * (1 << (this->retry_count - 1));
ADVGSM_LOG(GsmSeverity::Debug, "GsmModemCommon",
"Connection not ready, retry %d delaying for %d ms",
this->retry_count, delay);
this->next_check = millis() + delay;
}
}
int8_t GsmModemCommon::compareIPAddress(const char ip_a[], const char ip_b[]) {
// For sorting IP Addresses in the order they are likely to be used as a
// server or appear in logs. Apply RFC 6724, assuming the destination is a
// public address scope (and then in reducing scope), and preferring stable
// (over temporary) addresses, e.g. if device is a server. Won't always be
// correct (e.g. if on local link, or if using IPv4 when v6 available), but
// provides a reasonable order of preference.
// Note: When IPAddress supports IPv6, then this can be easier done by byte,
// rather than string, comparison. (Also IPAddress might get a built in
// scope() property)
// IPv6 defined scope, or IPv4 scope equivalence from RFC 6724
int8_t scope_a;
int8_t scope_b;
if (strcmp(ip_a, "::") == 0 || strcmp(ip_a, "0.0.0.0") == 0) {
// Undefined
scope_a = 0;
} else if (strncmp(ip_a, "fe80:", 5) == 0 || strcmp(ip_a, "::1") == 0) {
// Link-local or loopback
scope_a = 0x2;
} else if (strncmp(ip_a, "ff0", 3) == 0) {
// IPv6 multicast scope
char scope_hex = tolower(ip_a[3]);
scope_a = scope_hex < 'a' ? scope_hex - '0' : scope_hex + 10 - 'a';
} else if (strncmp(ip_a, "169.254.", 8) == 0 ||
strncmp(ip_a, "127.", 4) == 0) {
// IPv4 link-local or loopback
scope_a = 0x2;
} else {
// global
scope_a = 0xe;
}
if (strcmp(ip_b, "::") == 0 || strcmp(ip_b, "0.0.0.0") == 0) {
// Undefined
scope_b = 0;
} else if (strncmp(ip_b, "fe80:", 5) == 0 || strcmp(ip_b, "::1") == 0) {
// Link-local or loopback
scope_b = 0x2;
} else if (strncmp(ip_b, "ff0", 3) == 0) {
// IPv6 multicast scope
char scope_hex = tolower(ip_b[3]);
scope_b = scope_hex < 'a' ? scope_hex - '0' : scope_hex + 10 - 'a';
} else if (strncmp(ip_b, "169.254.", 8) == 0 ||
strncmp(ip_b, "127.", 4) == 0) {
// IPv4 link-local or loopback
scope_b = 0x2;
} else {
// global
scope_b = 0xe;
}
ADVGSM_LOG(GsmSeverity::Trace, "GsmModemCommon", "Scope A %s %d vs B %s %d",
ip_a, scope_a, ip_b, scope_b);
// Prioritise larger scope (i.e. global) over smaller
if (scope_b - scope_a != 0) {
return scope_b - scope_a;
}
// From RFC 6724 default table
int8_t precedence_a;
int8_t precedence_b;
if (strcmp(ip_a, "::1") == 0) {
precedence_a = 50;
} else if (strncmp(ip_a, "::ffff:0:0:", 11) == 0 ||
strchr(ip_a, ':') == nullptr) {
// Any IPv4
precedence_a = 35;
} else if (strncmp(ip_a, "2002:", 5) == 0) {
// 6to4 tunnel 2002://16
precedence_a = 30;
} else if (strncmp(ip_a, "2001::", 6) == 0 ||
strncmp(ip_a, "2001:0:", 7) == 0) {
// Teredo tunnel 2001::/32
precedence_a = 5;
} else if (strncmp(ip_a, "fc", 2) == 0 || strncmp(ip_a, "fd", 2) == 0) {
// ULA fc00::/7, usually fd..
precedence_a = 3;
} else if (strncmp(ip_a, "3ffe:", 5) == 0 || strncmp(ip_a, "fec0:", 5) == 0 ||
strncmp(ip_a, "::", 2) == 0) {
// Note: Netmask not correctly checked
precedence_a = 1;
} else {
// Any IPv6
precedence_a = 40;
}
if (strcmp(ip_b, "::1") == 0) {
precedence_b = 50;
} else if (strncmp(ip_b, "::ffff:0:0:", 11) == 0 ||
strchr(ip_b, ':') == nullptr) {
// Any IPv4
precedence_b = 35;
} else if (strncmp(ip_b, "2002:", 5) == 0) {
// 6to4 tunnel 2002://16
precedence_b = 30;
} else if (strncmp(ip_b, "2001::", 6) == 0 ||
strncmp(ip_b, "2001:0:", 7) == 0) {
// Teredo tunnel 2001::/32
precedence_b = 5;
} else if (strncmp(ip_b, "fc", 2) == 0 || strncmp(ip_b, "fd", 2) == 0) {
// ULA fc00::/7, usually fd..
precedence_b = 3;
} else if (strncmp(ip_b, "3ffe:", 5) == 0 || strncmp(ip_b, "fec0:", 5) == 0 ||
strncmp(ip_b, "::", 2) == 0) {
// Note: Netmask not correctly checked
precedence_b = 1;
} else {
// Any IPv6
precedence_b = 40;
}
ADVGSM_LOG(GsmSeverity::Trace, "GsmModemCommon",
"Precedence A %s %d vs B %s %d", ip_a, precedence_a, ip_b,
precedence_b);
// Prioritise larger scope (i.e. global) over smaller
if (precedence_b - precedence_a != 0) {
return precedence_b - precedence_a;
}
// Tie-break by string comparison, although this could be weird as '9' < ':' <
// 'a', so '::' sorts in the middle
return strcmp(ip_a, ip_b);
}
int8_t GsmModemCommon::getLocalIPs(String addresses[], uint8_t max) {
// TS 27.007: no context should return addresses for all contexts
// NOTE: +CGPADDR gets the address assigned during the last activation (even
// if not currently connected)
this->sendAT(GF("+CGPADDR"));
bool response_finished = false;
int8_t address_index = 0;
while (address_index < max) {
int8_t response = waitResponse(GFP(GSM_OK), GFP(GSM_ERROR), "+CGPADDR:");
if (response != 3) {
response_finished = true;
break;
}
String address_line = this->stream.readStringUntil('\n');
// Check first returned address
int start1 = address_line.indexOf('"');
if (start1 == -1) {
continue;
}
int end1 = address_line.indexOf('"', start1 + 1);
if (end1 < start1 + 2) {
continue;
}
String address1 = address_line.substring(start1 + 1, end1);
// Insertion sort in priority order
int8_t insert1 = address_index;
while (insert1 > 0 && compareIPAddress(addresses[insert1 - 1].c_str(),
address1.c_str()) > 0) {
addresses[insert1] = addresses[insert1 - 1];
insert1--;
}
addresses[insert1] = address1;
if (++address_index >= max) {
break;
}
// Check if there is a second address (index 1)
int start2 = address_line.indexOf('"', end1 + 1);
if (start2 == -1) {
continue;
}
int end2 = address_line.indexOf('"', start2 + 1);
if (end2 < start1 + 2) {
continue;
}
String address2 = address_line.substring(start1 + 1, end1);
// Insertion sort in priority order
int8_t insert2 = address_index;
while (insert2 > 0 && compareIPAddress(addresses[insert2 - 1].c_str(),
address2.c_str()) > 0) {
addresses[insert2] = addresses[insert2 - 1];
insert2--;
}
addresses[insert2] = address2;
}
if (!response_finished) {
waitResponse();
}
return address_index;
}
String GsmModemCommon::ICCID() {
return "";
}
String GsmModemCommon::IMEI() {
this->sendAT(GF("+CGSN"));
String response;
if (waitResponse(1000, response) != 1) {
return "unknown";
}
response.replace("\r\nOK\r\n", "");
response.replace("\rOK\r", "");
response.trim();
return response;
}
String GsmModemCommon::IMSI() {
this->sendAT(GF("+CIMI"));
String response;
if (waitResponse(1000, response) != 1) {
return "unknown";
}
response.replace("\r\nOK\r\n", "");
response.replace("\rOK\r", "");
response.trim();
return response;
}
bool GsmModemCommon::isActive() {
return this->active;
}
String GsmModemCommon::localIP(uint8_t index) {
String addresses[index];
uint8_t count = getLocalIPs(addresses, index);
if (count == 0) {
return "";
}
return addresses[count];
}
void GsmModemCommon::loop() {
// Serial.print("GsmModemCommon::loop\n");
// TODO: Heartbeat check on connection
if (this->active) {
// If not ready, then check connection status, with back off delay
if (this->status < ModemStatus::PacketDataReady) {
if (this->next_check > -1 && millis() > this->next_check) {
bool success = checkConnection();
if (success) {
this->next_check = -1;
} else {
this->retry_count++;
if (this->retry_count > this->retry_max) {
ADVGSM_LOG(GsmSeverity::Fatal, "GsmModemCommon",
"Connection retry count exceeded; modem shutting down");
this->active = false;
this->next_check = -1;
// TODO: high level communication retry after 1 minute /
// communication sequence retry, with modem hard reset
} else {
int32_t delay =
retry_base_delay_ms * (1 << (this->retry_count - 1));
ADVGSM_LOG(GsmSeverity::Debug, "GsmModemCommon",
"Connection not ready, retry %d delaying for %d ms",
this->retry_count, delay);
this->next_check = millis() + delay;
}
}
}
}
// For any unsolicited responses
this->waitResponse(GSM_COMMAND_DELAY_MS, NULL, NULL);
}
}
String GsmModemCommon::manufacturer() {
this->sendAT(GF("+CGMI"));
String response;
if (waitResponse(1000, response) != 1) {
return "unknown";
}
response.replace("\r\nOK\r\n", "");
response.replace("\rOK\r", "");
response.trim();
return response;
}
String GsmModemCommon::model() {
this->sendAT(GF("+CGMM"));
String response;
if (waitResponse(1000, response) != 1) {
return "unknown";
}
response.replace("\r\nOK\r\n", "");
response.replace("\rOK\r", "");
response.trim();
return response;
}
ModemStatus GsmModemCommon::modemStatus() {
return this->status;
}
String GsmModemCommon::network() {
// Gets the PLMN (Public Land Mobile Network) operator details
this->sendAT(GF("+COPS?"));
if (waitResponse("+COPS:") != 1) {
return "";
}
String plmn_details = this->stream.readStringUntil('\n');
waitResponse();
int start = plmn_details.indexOf('"');
if (start == -1) {
return "";
}
int end = plmn_details.indexOf('"', start + 1);
if (end < start + 2) {
return "";
}
String network = plmn_details.substring(start + 1, end);
// TODO: Could include the Access Technology, e.g. "(NB-S1)"
return network;
}
String GsmModemCommon::readResponseLine() {
return this->stream.readStringUntil('\n');
}
RegistrationStatus GsmModemCommon::registrationStatus() {
// Registration status results are aligned across versions.
// Override if needed:
// +CREG?
// +CGREP? (GPRS)
// +CEREG? (EPS)
// +C5GREG? (5G)
this->sendAT(GF("+CEREG?"));
if (waitResponse("+CEREG:") != 1) {
return RegistrationStatus::UnknownRegistrationStatus;
}
streamSkipUntil(','); // skip mode
int16_t status = this->stream.parseInt();
if (waitResponse() != 1) {
return RegistrationStatus::UnknownRegistrationStatus;
}
return static_cast<RegistrationStatus>(status);
}
bool GsmModemCommon::resetDefaultConfiguration() {
ADVGSM_LOG(GsmSeverity::Info, "GsmModemCommon",
"Resetting default configuration");
sendAT(GF("Z"));
int8_t rc = waitResponse();
if (rc != 1) {
ADVGSM_LOG(GsmSeverity::Warn, "GsmModemCommon", "Reset %s",
(rc == 0) ? "timed out" : "error");
return false;
}
ADVGSM_LOG(GsmSeverity::Debug, "GsmModemCommon", "Reset success");
return true;
}
String GsmModemCommon::revision() {
this->sendAT(GF("+CGMR"));
String response;
if (waitResponse(1000, response) != 1) {
return "unknown";
}
response.replace("\r\nOK\r\n", "");
response.replace("\rOK\r", "");
response.trim();
return response;
}
int32_t GsmModemCommon::RSSI() {
this->sendAT(GF("+CSQ"));
if (waitResponse("+CSQ:") != 1) {
return 0;
}
int16_t rssi_index = streamGetIntBefore(',');
if (waitResponse() != 1) {
return 0;
}
if (rssi_index == 99) {
return 0;
}
double rssidBm = -113.0 + (rssi_index * 2);
return rssidBm;
}
void GsmModemCommon::sendATCommand(const char command[]) {
streamWrite("AT", command, this->gsmNL);
this->stream.flush();
}
bool GsmModemCommon::setDns(const char primaryDns[], const char secondaryDns[]) {
this->sendAT(GF("+CDNSCFG="), "\"", primaryDns, "\",\"", secondaryDns, "\"");
if (waitResponse() != 1) {
return false;
}
return true;
}
inline int16_t GsmModemCommon::streamGetIntBefore(char lastChar) {
char buf[7];
size_t bytesRead =
this->stream.readBytesUntil(lastChar, buf, static_cast<size_t>(7));
// if we read 7 or more bytes, it's an overflow
if (bytesRead && bytesRead < 7) {
buf[bytesRead] = '\0';
int16_t res = atoi(buf);
return res;
}
return -9999;
}
inline bool GsmModemCommon::streamSkipUntil(const char c,
const uint32_t timeout_ms) {
uint32_t startMillis = millis();
while (millis() - startMillis < timeout_ms) {
while (millis() - startMillis < timeout_ms && !this->stream.available()) {
TINY_GSM_YIELD();
}
if (this->stream.read() == c) {
return true;
}
}
return false;
}
int8_t GsmModemCommon::waitResponse() {
return waitResponse(GSM_OK);
}
int8_t GsmModemCommon::waitResponse(GsmConstStr r1,
GsmConstStr r2,
GsmConstStr r3,
GsmConstStr r4,
GsmConstStr r5) {
return waitResponse(1000, r1, r2, r3, r4, r5);
}
int8_t GsmModemCommon::waitResponse(uint32_t timeout_ms,
GsmConstStr r1,
GsmConstStr r2,
GsmConstStr r3,
GsmConstStr r4,
GsmConstStr r5) {
String s;
return waitResponse(timeout_ms, s, r1, r2, r3, r4, r5);
}
int8_t GsmModemCommon::waitResponse(uint32_t timeout_ms,
String& data,
GsmConstStr r1,
GsmConstStr r2,
GsmConstStr r3,
GsmConstStr r4,
GsmConstStr r5) {
return checkResponse(timeout_ms, data, r1, r2, r3, r4, r5);
}