-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathesp_modem_command_library.cpp
582 lines (511 loc) · 19 KB
/
esp_modem_command_library.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
* SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <charconv>
#include <list>
#include "esp_log.h"
#include "cxx_include/esp_modem_dte.hpp"
#include "cxx_include/esp_modem_dce_module.hpp"
#include "cxx_include/esp_modem_command_library.hpp"
#include "cxx_include/esp_modem_command_library_utils.hpp"
namespace esp_modem::dce_commands {
static const char *TAG = "command_lib";
static command_result generic_command(CommandableIf *t, const std::string &command,
const std::list<std::string_view> &pass_phrase,
const std::list<std::string_view> &fail_phrase,
uint32_t timeout_ms)
{
ESP_LOGD(TAG, "%s command %s\n", __func__, command.c_str());
return t->command(command, [&](uint8_t *data, size_t len) {
std::string_view response((char *)data, len);
if (data == nullptr || len == 0 || response.empty()) {
return command_result::TIMEOUT;
}
ESP_LOGD(TAG, "Response: %.*s\n", (int)response.length(), response.data());
for (auto &it : pass_phrase)
if (response.find(it) != std::string::npos) {
return command_result::OK;
}
for (auto &it : fail_phrase)
if (response.find(it) != std::string::npos) {
return command_result::FAIL;
}
return command_result::TIMEOUT;
}, timeout_ms);
}
command_result generic_command(CommandableIf *t, const std::string &command,
const std::string &pass_phrase,
const std::string &fail_phrase, uint32_t timeout_ms)
{
ESP_LOGV(TAG, "%s", __func__ );
const auto pass = std::list<std::string_view>({pass_phrase});
const auto fail = std::list<std::string_view>({fail_phrase});
return generic_command(t, command, pass, fail, timeout_ms);
}
static command_result generic_get_string(CommandableIf *t, const std::string &command, std::string_view &output, uint32_t timeout_ms = 500)
{
ESP_LOGV(TAG, "%s", __func__ );
return t->command(command, [&](uint8_t *data, size_t len) {
size_t pos = 0;
std::string_view response((char *)data, len);
while ((pos = response.find('\n')) != std::string::npos) {
std::string_view token = response.substr(0, pos);
for (auto it = token.end() - 1; it > token.begin(); it--) // strip trailing CR or LF
if (*it == '\r' || *it == '\n') {
token.remove_suffix(1);
}
ESP_LOGV(TAG, "Token: {%.*s}\n", static_cast<int>(token.size()), token.data());
if (token.find("OK") != std::string::npos) {
return command_result::OK;
} else if (token.find("ERROR") != std::string::npos) {
return command_result::FAIL;
} else if (token.size() > 2) {
output = token;
}
response = response.substr(pos + 1);
}
return command_result::TIMEOUT;
}, timeout_ms);
}
command_result generic_get_string(CommandableIf *t, const std::string &command, std::string &output, uint32_t timeout_ms)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, command, out, timeout_ms);
if (ret == command_result::OK) {
output = out;
}
return ret;
}
command_result generic_command_common(CommandableIf *t, const std::string &command, uint32_t timeout_ms)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, command, "OK", "ERROR", timeout_ms);
}
command_result sync(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT\r");
}
command_result store_profile(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT&W\r");
}
command_result power_down(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, "AT+QPOWD=1\r", "POWERED DOWN", "ERROR", 1000);
}
command_result power_down_sim76xx(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CPOF\r", 1000);
}
command_result power_down_sim70xx(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, "AT+CPOWD=1\r", "POWER DOWN", "ERROR", 1000);
}
command_result power_down_sim8xx(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, "AT+CPOWD=1\r", "POWER DOWN", "ERROR", 1000);
}
command_result reset(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, "AT+CRESET\r", "PB DONE", "ERROR", 60000);
}
command_result set_baud(CommandableIf *t, int baud)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+IPR=" + std::to_string(baud) + "\r");
}
command_result hang_up(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "ATH\r", 90000);
}
command_result get_battery_status(CommandableIf *t, int &voltage, int &bcs, int &bcl)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CBC\r", out);
if (ret != command_result::OK) {
return ret;
}
constexpr std::string_view pattern = "+CBC: ";
if (out.find(pattern) == std::string_view::npos) {
return command_result::FAIL;
}
// Parsing +CBC: <bcs>,<bcl>,<voltage>
out = out.substr(pattern.size());
int pos, value, property = 0;
while ((pos = out.find(',')) != std::string::npos) {
if (std::from_chars(out.data(), out.data() + pos, value).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
switch (property++) {
case 0: bcs = value;
break;
case 1: bcl = value;
break;
default:
return command_result::FAIL;
}
out = out.substr(pos + 1);
}
if (std::from_chars(out.data(), out.data() + out.size(), voltage).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
return command_result::OK;
}
command_result get_battery_status_sim7xxx(CommandableIf *t, int &voltage, int &bcs, int &bcl)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CBC\r", out);
if (ret != command_result::OK) {
return ret;
}
// Parsing +CBC: <voltage in Volts> V
constexpr std::string_view pattern = "+CBC: ";
constexpr int num_pos = pattern.size();
int dot_pos;
if (out.find(pattern) == std::string::npos ||
(dot_pos = out.find('.')) == std::string::npos) {
return command_result::FAIL;
}
int volt, fraction;
if (std::from_chars(out.data() + num_pos, out.data() + dot_pos, volt).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + dot_pos + 1, out.data() + out.size() - 1, fraction).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
bcl = bcs = -1; // not available for these models
voltage = 1000 * volt + fraction;
return command_result::OK;
}
command_result set_flow_control(CommandableIf *t, int dce_flow, int dte_flow)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+IFC=" + std::to_string(dce_flow) + "," + std::to_string(dte_flow) + "\r");
}
command_result get_operator_name(CommandableIf *t, std::string &operator_name, int &act)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+COPS?\r", out, 75000);
if (ret != command_result::OK) {
return ret;
}
auto pos = out.find("+COPS");
auto property = 0;
while (pos != std::string::npos) {
// Looking for: +COPS: <mode>[, <format>[, <oper>[, <act>]]]
if (property++ == 2) { // operator name is after second comma (as a 3rd property of COPS string)
operator_name = out.substr(++pos);
auto additional_comma = operator_name.find(','); // check for the optional ACT
if (additional_comma != std::string::npos && std::from_chars(operator_name.data() + additional_comma + 1, operator_name.data() + operator_name.length(), act).ec != std::errc::invalid_argument) {
operator_name = operator_name.substr(0, additional_comma);
}
// and strip quotes if present
auto quote1 = operator_name.find('"');
auto quote2 = operator_name.rfind('"');
if (quote1 != std::string::npos && quote2 != std::string::npos) {
operator_name = operator_name.substr(quote1 + 1, quote2 - 1);
}
return command_result::OK;
}
pos = out.find(',', ++pos);
}
return command_result::FAIL;
}
command_result set_echo(CommandableIf *t, bool on)
{
ESP_LOGV(TAG, "%s", __func__ );
if (on) {
return generic_command_common(t, "ATE1\r");
}
return generic_command_common(t, "ATE0\r");
}
command_result set_pdp_context(CommandableIf *t, PdpContext &pdp, uint32_t timeout_ms)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string pdp_command = "AT+CGDCONT=" + std::to_string(pdp.context_id) +
",\"" + pdp.protocol_type + "\",\"" + pdp.apn + "\"\r";
return generic_command_common(t, pdp_command, timeout_ms);
}
command_result set_pdp_context(CommandableIf *t, PdpContext &pdp)
{
return set_pdp_context(t, pdp, 1000);
}
command_result set_data_mode(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, "ATD*99##\r", "CONNECT", "ERROR", 5000);
}
command_result set_data_mode_sim8xx(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, "ATD*99#\r", "CONNECT", "ERROR", 5000);
}
command_result resume_data_mode(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command(t, "ATO\r", "CONNECT", "ERROR", 5000);
}
command_result set_command_mode(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
const auto pass = std::list<std::string_view>({"NO CARRIER", "OK"});
const auto fail = std::list<std::string_view>({"ERROR"});
return generic_command(t, "+++", pass, fail, 5000);
}
command_result get_imsi(CommandableIf *t, std::string &imsi_number)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_get_string(t, "AT+CIMI\r", imsi_number, 5000);
}
command_result get_imei(CommandableIf *t, std::string &out)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_get_string(t, "AT+CGSN\r", out, 5000);
}
command_result get_module_name(CommandableIf *t, std::string &out)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_get_string(t, "AT+CGMM\r", out, 5000);
}
command_result sms_txt_mode(CommandableIf *t, bool txt = true)
{
ESP_LOGV(TAG, "%s", __func__ );
if (txt) {
return generic_command_common(t, "AT+CMGF=1\r"); // Text mode (default)
}
return generic_command_common(t, "AT+CMGF=0\r"); // PDU mode
}
command_result sms_character_set(CommandableIf *t)
{
// Sets the default GSM character set
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CSCS=\"GSM\"\r");
}
command_result send_sms(CommandableIf *t, const std::string &number, const std::string &message)
{
ESP_LOGV(TAG, "%s", __func__ );
auto ret = t->command("AT+CMGS=\"" + number + "\"\r", [&](uint8_t *data, size_t len) {
std::string_view response((char *)data, len);
ESP_LOGD(TAG, "Send SMS response %.*s", static_cast<int>(response.size()), response.data());
if (response.find('>') != std::string::npos) {
return command_result::OK;
}
return command_result::TIMEOUT;
}, 5000, ' ');
if (ret != command_result::OK) {
return ret;
}
return generic_command_common(t, message + "\x1A", 120000);
}
command_result set_cmux(CommandableIf *t)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CMUX=0\r");
}
command_result read_pin(CommandableIf *t, bool &pin_ok)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CPIN?\r", out);
if (ret != command_result::OK) {
return ret;
}
if (out.find("+CPIN:") == std::string::npos) {
return command_result::FAIL;
}
if (out.find("SIM PIN") != std::string::npos || out.find("SIM PUK") != std::string::npos) {
pin_ok = false;
return command_result::OK;
}
if (out.find("READY") != std::string::npos) {
pin_ok = true;
return command_result::OK;
}
return command_result::FAIL; // Neither pin-ok, nor waiting for pin/puk -> mark as error
}
command_result set_pin(CommandableIf *t, const std::string &pin)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string set_pin_command = "AT+CPIN=" + pin + "\r";
return generic_command_common(t, set_pin_command);
}
command_result at(CommandableIf *t, const std::string &cmd, std::string &out, int timeout = 500)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string at_command = cmd + "\r";
return generic_get_string(t, at_command, out, timeout);
}
command_result get_signal_quality(CommandableIf *t, int &rssi, int &ber)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CSQ\r", out);
if (ret != command_result::OK) {
return ret;
}
constexpr std::string_view pattern = "+CSQ: ";
constexpr int rssi_pos = pattern.size();
int ber_pos;
if (out.find(pattern) == std::string::npos ||
(ber_pos = out.find(',')) == std::string::npos) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + rssi_pos, out.data() + ber_pos, rssi).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + ber_pos + 1, out.data() + out.size(), ber).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
return command_result::OK;
}
command_result set_operator(CommandableIf *t, int mode, int format, const std::string &oper)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+COPS=" + std::to_string(mode) + "," + std::to_string(format) + ",\"" + oper + "\"\r", 90000);
}
command_result set_network_attachment_state(CommandableIf *t, int state)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CGATT=" + std::to_string(state) + "\r");
}
command_result get_network_attachment_state(CommandableIf *t, int &state)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CGATT?\r", out);
if (ret != command_result::OK) {
return ret;
}
constexpr std::string_view pattern = "+CGATT: ";
constexpr int pos = pattern.size();
if (out.find(pattern) == std::string::npos) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + pos, out.data() + out.size(), state).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
return command_result::OK;
}
command_result set_radio_state(CommandableIf *t, int state)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CFUN=" + std::to_string(state) + "\r", 15000);
}
command_result get_radio_state(CommandableIf *t, int &state)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CFUN?\r", out);
if (ret != command_result::OK) {
return ret;
}
constexpr std::string_view pattern = "+CFUN: ";
constexpr int pos = pattern.size();
if (out.find(pattern) == std::string::npos) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + pos, out.data() + out.size(), state).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
return command_result::OK;
}
command_result set_network_mode(CommandableIf *t, int mode)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CNMP=" + std::to_string(mode) + "\r");
}
command_result set_preferred_mode(CommandableIf *t, int mode)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CMNB=" + std::to_string(mode) + "\r");
}
command_result set_network_bands(CommandableIf *t, const std::string &mode, const int *bands, int size)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string band_string = "";
for (int i = 0; i < size - 1; ++i) {
band_string += std::to_string(bands[i]) + ",";
}
band_string += std::to_string(bands[size - 1]);
return generic_command_common(t, "AT+CBANDCFG=\"" + mode + "\"," + band_string + "\r");
}
// mode is expected to be 64bit string (in hex)
// any_mode = "0xFFFFFFFF7FFFFFFF";
command_result set_network_bands_sim76xx(CommandableIf *t, const std::string &mode, const int *bands, int size)
{
ESP_LOGV(TAG, "%s", __func__ );
static const char *hexDigits = "0123456789ABCDEF";
uint64_t band_bits = 0;
int hex_len = 16;
std::string band_string(hex_len, '0');
for (int i = 0; i < size; ++i) {
// OR-operation to add bands
auto band = bands[i] - 1; // Sim7600 has 0-indexed band selection (band 20 has to be shifted 19 places)
band_bits |= 1 << band;
}
for (int i = hex_len; i > 0; i--) {
band_string[i - 1] = hexDigits[(band_bits >> ((hex_len - i) * 4)) & 0xF];
}
return generic_command_common(t, "AT+CNBP=" + mode + ",0x" + band_string + "\r");
}
command_result get_network_system_mode(CommandableIf *t, int &mode)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CNSMOD?\r", out);
if (ret != command_result::OK) {
return ret;
}
constexpr std::string_view pattern = "+CNSMOD: ";
int mode_pos = out.find(",") + 1; // Skip "<n>,"
if (out.find(pattern) == std::string::npos) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + mode_pos, out.data() + out.size(), mode).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
return command_result::OK;
}
command_result set_gnss_power_mode(CommandableIf *t, int mode)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CGNSPWR=" + std::to_string(mode) + "\r");
}
command_result get_gnss_power_mode(CommandableIf *t, int &mode)
{
ESP_LOGV(TAG, "%s", __func__ );
std::string_view out;
auto ret = generic_get_string(t, "AT+CGNSPWR?\r", out);
if (ret != command_result::OK) {
return ret;
}
constexpr std::string_view pattern = "+CGNSPWR: ";
constexpr int pos = pattern.size();
if (out.find(pattern) == std::string::npos) {
return command_result::FAIL;
}
if (std::from_chars(out.data() + pos, out.data() + out.size(), mode).ec == std::errc::invalid_argument) {
return command_result::FAIL;
}
return command_result::OK;
}
command_result set_gnss_power_mode_sim76xx(CommandableIf *t, int mode)
{
ESP_LOGV(TAG, "%s", __func__ );
return generic_command_common(t, "AT+CGPS=" + std::to_string(mode) + "\r");
}
} // esp_modem::dce_commands