@@ -51,7 +51,35 @@ command_result generic_command(CommandableIf *t, const std::string &command,
51
51
return generic_command (t, command, pass, fail, timeout_ms);
52
52
}
53
53
54
- static command_result generic_get_string (CommandableIf *t, const std::string &command, std::string_view &output, uint32_t timeout_ms = 500 )
54
+ /*
55
+ * Purpose of this namespace is to provide different means of assigning the result to a string-like parameter.
56
+ * By default we assign strings, which comes with an allocation. Alternatively we could take `std::span`
57
+ * with user's buffer and directly copy the result, thus avoiding allocations (this is not used as of now)
58
+ */
59
+ namespace str_copy {
60
+
61
+ bool set (std::string &dest, std::string_view &src)
62
+ {
63
+ dest = src;
64
+ return true ;
65
+ }
66
+
67
+ /* This is an example of using std::span output in generic_get_string()
68
+ bool set(std::span<char> &dest, std::string_view &src)
69
+ {
70
+ if (dest.size() >= src.size()) {
71
+ std::copy(src.begin(), src.end(), dest.data());
72
+ dest = dest.subspan(0, src.size());
73
+ return true;
74
+ }
75
+ ESP_LOGE(TAG, "Cannot set result of size %d (to span of size %d)", dest.size(), src.size());
76
+ return false;
77
+ }
78
+ */
79
+
80
+ } // str_copy
81
+
82
+ template <typename T> command_result generic_get_string (CommandableIf *t, const std::string &command, T &output, uint32_t timeout_ms)
55
83
{
56
84
ESP_LOGV (TAG, " %s" , __func__ );
57
85
return t->command (command, [&](uint8_t *data, size_t len) {
@@ -70,26 +98,16 @@ static command_result generic_get_string(CommandableIf *t, const std::string &co
70
98
} else if (token.find (" ERROR" ) != std::string::npos) {
71
99
return command_result::FAIL;
72
100
} else if (token.size () > 2 ) {
73
- output = token;
101
+ if (!str_copy::set (output, token)) {
102
+ return command_result::FAIL;
103
+ }
74
104
}
75
105
response = response.substr (pos + 1 );
76
106
}
77
107
return command_result::TIMEOUT;
78
108
}, timeout_ms);
79
109
}
80
110
81
- command_result generic_get_string (CommandableIf *t, const std::string &command, std::string &output, uint32_t timeout_ms)
82
- {
83
- ESP_LOGV (TAG, " %s" , __func__ );
84
- std::string_view out;
85
- auto ret = generic_get_string (t, command, out, timeout_ms);
86
- if (ret == command_result::OK) {
87
- output = out;
88
- }
89
- return ret;
90
- }
91
-
92
-
93
111
command_result generic_command_common (CommandableIf *t, const std::string &command, uint32_t timeout_ms)
94
112
{
95
113
ESP_LOGV (TAG, " %s" , __func__ );
@@ -153,7 +171,7 @@ command_result hang_up(CommandableIf *t)
153
171
command_result get_battery_status (CommandableIf *t, int &voltage, int &bcs, int &bcl)
154
172
{
155
173
ESP_LOGV (TAG, " %s" , __func__ );
156
- std::string_view out;
174
+ std::string out;
157
175
auto ret = generic_get_string (t, " AT+CBC\r " , out);
158
176
if (ret != command_result::OK) {
159
177
return ret;
@@ -189,7 +207,7 @@ command_result get_battery_status(CommandableIf *t, int &voltage, int &bcs, int
189
207
command_result get_battery_status_sim7xxx (CommandableIf *t, int &voltage, int &bcs, int &bcl)
190
208
{
191
209
ESP_LOGV (TAG, " %s" , __func__ );
192
- std::string_view out;
210
+ std::string out;
193
211
auto ret = generic_get_string (t, " AT+CBC\r " , out);
194
212
if (ret != command_result::OK) {
195
213
return ret;
@@ -224,7 +242,7 @@ command_result set_flow_control(CommandableIf *t, int dce_flow, int dte_flow)
224
242
command_result get_operator_name (CommandableIf *t, std::string &operator_name, int &act)
225
243
{
226
244
ESP_LOGV (TAG, " %s" , __func__ );
227
- std::string_view out;
245
+ std::string out;
228
246
auto ret = generic_get_string (t, " AT+COPS?\r " , out, 75000 );
229
247
if (ret != command_result::OK) {
230
248
return ret;
@@ -361,7 +379,7 @@ command_result set_cmux(CommandableIf *t)
361
379
command_result read_pin (CommandableIf *t, bool &pin_ok)
362
380
{
363
381
ESP_LOGV (TAG, " %s" , __func__ );
364
- std::string_view out;
382
+ std::string out;
365
383
auto ret = generic_get_string (t, " AT+CPIN?\r " , out);
366
384
if (ret != command_result::OK) {
367
385
return ret;
@@ -413,7 +431,7 @@ command_result at_raw(CommandableIf *t, const std::string &cmd, std::string &out
413
431
command_result get_signal_quality (CommandableIf *t, int &rssi, int &ber)
414
432
{
415
433
ESP_LOGV (TAG, " %s" , __func__ );
416
- std::string_view out;
434
+ std::string out;
417
435
auto ret = generic_get_string (t, " AT+CSQ\r " , out);
418
436
if (ret != command_result::OK) {
419
437
return ret;
@@ -451,7 +469,7 @@ command_result set_network_attachment_state(CommandableIf *t, int state)
451
469
command_result get_network_attachment_state (CommandableIf *t, int &state)
452
470
{
453
471
ESP_LOGV (TAG, " %s" , __func__ );
454
- std::string_view out;
472
+ std::string out;
455
473
auto ret = generic_get_string (t, " AT+CGATT?\r " , out);
456
474
if (ret != command_result::OK) {
457
475
return ret;
@@ -478,7 +496,7 @@ command_result set_radio_state(CommandableIf *t, int state)
478
496
command_result get_radio_state (CommandableIf *t, int &state)
479
497
{
480
498
ESP_LOGV (TAG, " %s" , __func__ );
481
- std::string_view out;
499
+ std::string out;
482
500
auto ret = generic_get_string (t, " AT+CFUN?\r " , out);
483
501
if (ret != command_result::OK) {
484
502
return ret;
@@ -543,7 +561,7 @@ command_result set_network_bands_sim76xx(CommandableIf *t, const std::string &mo
543
561
command_result get_network_system_mode (CommandableIf *t, int &mode)
544
562
{
545
563
ESP_LOGV (TAG, " %s" , __func__ );
546
- std::string_view out;
564
+ std::string out;
547
565
auto ret = generic_get_string (t, " AT+CNSMOD?\r " , out);
548
566
if (ret != command_result::OK) {
549
567
return ret;
@@ -571,7 +589,7 @@ command_result set_gnss_power_mode(CommandableIf *t, int mode)
571
589
command_result get_gnss_power_mode (CommandableIf *t, int &mode)
572
590
{
573
591
ESP_LOGV (TAG, " %s" , __func__ );
574
- std::string_view out;
592
+ std::string out;
575
593
auto ret = generic_get_string (t, " AT+CGNSPWR?\r " , out);
576
594
if (ret != command_result::OK) {
577
595
return ret;
0 commit comments