forked from nrfconnect/ncs-serial-modem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsm_util.c
More file actions
464 lines (389 loc) · 12.1 KB
/
Copy pathsm_util.c
File metadata and controls
464 lines (389 loc) · 12.1 KB
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
/*
* Copyright (c) 2019 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zephyr/net/net_ip.h>
#include <nrf_errno.h>
#include <nrf_modem_at.h>
#include "sm_util.h"
LOG_MODULE_REGISTER(sm_util, CONFIG_SM_LOG_LEVEL);
static bool cmd_name_has_lower(const char *cmd)
{
for (size_t i = 0; cmd[i] != '\0'; ++i) {
const char c = cmd[i];
/* Set/test command names are delimited by '=', read by '?'. */
if (c == '=' || c == '?') {
break;
} else if (islower(c)) {
LOG_ERR("FIX ME: AT command \"%s\" must be all-uppercase.", cmd);
return true;
}
}
return false;
}
int sm_util_at_printf(const char *fmt, ...)
{
int ret;
char buf[128];
va_list args;
va_start(args, fmt);
ret = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (ret >= sizeof(buf)) {
LOG_ERR("AT command \"%.16s...\" would get truncated from %u to %u bytes. "
"The buffer needs to be made bigger.", buf, ret, sizeof(buf) - 1);
return -E2BIG;
}
/* AT command interception is case-sensitive. */
if (cmd_name_has_lower(buf)) {
return -EINVAL;
}
/* Even though we are not interested in the AT response, it must fit
* into the provided buffer so that the response code is returned.
*/
ret = nrf_modem_at_cmd(buf, sizeof(buf), "%s", buf);
if (ret == -NRF_E2BIG) {
/* Unlikely, but in that case the response code most likely didn't
* make it into the buffer, so searching for it would be fruitless.
*/
LOG_ERR("AT response to \"%s\" didn't fit into %u bytes. "
"The buffer needs to be made bigger.", fmt, sizeof(buf) - 1);
}
return ret;
}
int sm_util_at_scanf(const char *cmd, const char *fmt, ...)
{
char buf[256];
va_list args;
int ret;
/* AT command interception is case-sensitive. */
if (cmd_name_has_lower(cmd)) {
return -EINVAL;
}
ret = nrf_modem_at_cmd(buf, sizeof(buf), "%s", cmd);
if (ret == -NRF_E2BIG) {
LOG_ERR("AT response to \"%s\" truncated to %u bytes. "
"The buffer needs to be made bigger.", cmd, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
} else if (ret < 0) {
return ret;
}
va_start(args, fmt);
ret = vsscanf(buf, fmt, args);
va_end(args);
if (ret == 0) {
ret = -NRF_EBADMSG;
}
return ret;
}
static int terminate_at_response(char *buf, size_t len,
const char *termination_str, int success_ret)
{
len -= strlen(buf);
const int printf_ret = snprintf(buf + strlen(buf), len, "%s", termination_str);
if (printf_ret >= len) {
LOG_ERR("%u bytes of the AT response were truncated."
" The buffer needs to be made bigger.", printf_ret - len + 1);
return -NRF_E2BIG;
}
return success_ret;
}
int sm_util_at_cmd_no_intercept(char *buf, size_t len, const char *at_cmd)
{
int ret;
char second_line[strlen("+CMx ERROR: xxxx")];
char format_str[24];
/* Construct the format string to read two response lines (into buf and second_line).
* The buffer sizes are put in the format string to prevent overflows.
* Negated scansets are used to read whole lines (everything up to '\r').
* See the scanf() format specifer documentation for more information.
*/
ret = snprintf(format_str, sizeof(format_str),
"%%%u[^\r]\r\n%%%u[^\r]", len - 1, sizeof(second_line) - 1);
assert(ret < sizeof(format_str));
LOG_DBG("Forwarding \"%s\" to the modem.", at_cmd);
/* This function gets called by interception callbacks invoked by nrf_modem_at_cmd().
* Here nrf_modem_at_cmd() must be bypassed because it would otherwise call
* the same interception callbacks and infinite recursion would happen.
*/
#undef nrf_modem_at_scanf
const int line_count = nrf_modem_at_scanf(at_cmd, format_str, buf, second_line);
/* Restore the trap macro after the only place where this function is
* allowed to be used so that other code does not accidentally use it.
*/
#define nrf_modem_at_scanf nrf_modem_at_printf
if (line_count < 1) {
LOG_ERR("Forwarding of \"%s\" failed (%d).", at_cmd, line_count);
return line_count;
}
/* The line that is expected to contain the response code. */
const char *const resp_str = (line_count == 1) ? buf : second_line;
#define AT_ERROR (NRF_MODEM_AT_ERROR << 16)
assert(nrf_modem_at_err_type(AT_ERROR) == NRF_MODEM_AT_ERROR);
/* Reconstruct the response string and code from the parsed response line(s). */
if (!strcmp(resp_str, "OK")) {
/* The command succeeded and its response is no longer than two lines. */
ret = terminate_at_response(buf, len, (line_count == 1) ? "\r\n" : "\r\nOK\r\n", 0);
} else {
/* Is there an error in the first response line? */
if (!(line_count == 1 && strstr(resp_str, "ERROR"))) {
/* No. Overwrite the whole response with "ERROR". */
buf[0] = '\0';
}
ret = terminate_at_response(buf, len, buf[0] ? "\r\n" : "ERROR\r\n", AT_ERROR);
}
return ret;
}
bool sm_util_casecmp(const char *str1, const char *str2)
{
int str2_len = strlen(str2);
if (strlen(str1) != str2_len) {
return false;
}
for (int i = 0; i < str2_len; i++) {
if (toupper((int)*(str1 + i)) != toupper((int)*(str2 + i))) {
return false;
}
}
return true;
}
int util_string_get(struct at_parser *parser, size_t index, char *value, size_t *len)
{
int ret;
size_t size = *len;
/* at_parser_string_get calls "memcpy" instead of "strcpy" */
ret = at_parser_string_get(parser, index, value, len);
if (ret) {
return ret;
}
if (*len < size) {
*(value + *len) = '\0';
return 0;
}
return -ENOMEM;
}
int util_string_to_float_get(struct at_parser *parser, size_t index, float *value)
{
int ret;
char str[32];
size_t len = sizeof(str);
ret = util_string_get(parser, index, str, &len);
if (ret) {
return ret;
}
*value = strtof(str, NULL);
return 0;
}
int util_string_to_double_get(struct at_parser *parser, size_t index, double *value)
{
int ret;
char str[32];
size_t len = sizeof(str);
ret = util_string_get(parser, index, str, &len);
if (ret) {
return ret;
}
*value = strtod(str, NULL);
return 0;
}
void util_get_ip_addr(int cid, char addr4[INET_ADDRSTRLEN], char addr6[INET6_ADDRSTRLEN])
{
int ret;
char cmd[32];
char tmp[sizeof(struct in6_addr)];
char addr1[INET6_ADDRSTRLEN];
char addr2[INET6_ADDRSTRLEN];
if (addr4) {
addr4[0] = '\0';
}
if (addr6) {
addr6[0] = '\0';
}
sprintf(cmd, "AT+CGPADDR=%d", cid);
/** parse +CGPADDR: <cid>,<PDP_addr_1>,<PDP_addr_2>
* PDN type "IP": PDP_addr_1 is <IPv4>, max 16(INET_ADDRSTRLEN), '.' and digits
* PDN type "IPV6": PDP_addr_1 is <IPv6>, max 46(INET6_ADDRSTRLEN),':', digits, 'A'~'F'
* PDN type "IPV4V6": <IPv4>,<IPv6> or <IPV4> or <IPv6>
*/
ret = sm_util_at_scanf(cmd, "+CGPADDR: %*d,\"%45[.:0-9A-F]\",\"%45[:0-9A-F]\"",
addr1, addr2);
if (ret <= 0) {
return;
}
if (addr4 != NULL && zsock_inet_pton(AF_INET, addr1, tmp) == 1) {
strcpy(addr4, addr1);
} else if (addr6 != NULL && zsock_inet_pton(AF_INET6, addr1, tmp) == 1) {
strcpy(addr6, addr1);
return;
}
/* parse second IP string, IPv6 only */
if (addr6 == NULL) {
return;
}
if (ret > 1 && zsock_inet_pton(AF_INET6, addr2, tmp) == 1) {
strcpy(addr6, addr2);
}
}
int util_str_to_int(const char *str_buf, int base, int *output)
{
int temp;
char *end_ptr = NULL;
errno = 0;
temp = strtol(str_buf, &end_ptr, base);
if (end_ptr == str_buf || *end_ptr != '\0' ||
((temp == LONG_MAX || temp == LONG_MIN) && errno == ERANGE)) {
return -ENODATA;
}
*output = temp;
return 0;
}
#define PORT_MAX_SIZE 5 /* 0xFFFF = 65535 */
#define PDN_ID_MAX_SIZE 2 /* 0..10 */
int util_resolve_host(int cid, const char *host, uint16_t port, int family, struct sockaddr *sa)
{
int err;
char service[PORT_MAX_SIZE + PDN_ID_MAX_SIZE + 2];
struct zsock_addrinfo *ai = NULL;
struct zsock_addrinfo hints = {
.ai_flags = AI_NUMERICSERV | AI_PDNSERV,
.ai_family = family
};
if (sa == NULL) {
return DNS_EAI_AGAIN;
}
/* "service" shall be formatted as follows: "port:pdn_id" */
snprintf(service, sizeof(service), "%hu:%d", port, cid);
err = zsock_getaddrinfo(host, service, &hints, &ai);
if (!err) {
*sa = *(ai->ai_addr);
zsock_freeaddrinfo(ai);
if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6) {
err = DNS_EAI_ADDRFAMILY;
}
}
if (err) {
const char *errstr;
if (err == DNS_EAI_SYSTEM) {
errstr = strerror(errno);
err = errno;
} else {
errstr = zsock_gai_strerror(err);
}
LOG_ERR("zsock_getaddrinfo() error (%d): %s", err, errstr);
}
return err;
}
int util_get_peer_addr(struct sockaddr *peer, char addr[static INET6_ADDRSTRLEN], uint16_t *port)
{
const char *ret = NULL;
if (peer->sa_family == AF_INET) {
ret = zsock_inet_ntop(AF_INET, &((struct sockaddr_in *)peer)->sin_addr,
addr, INET6_ADDRSTRLEN);
*port = ntohs(((struct sockaddr_in *)peer)->sin_port);
} else {
ret = zsock_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)peer)->sin6_addr,
addr, INET6_ADDRSTRLEN);
*port = ntohs(((struct sockaddr_in6 *)peer)->sin6_port);
}
if (ret == NULL) {
LOG_ERR("zsock_inet_ntop error (%d)", -errno);
return -errno;
}
return 0;
}
int sm_util_pdn_id_get(uint8_t cid)
{
int ret;
char cmd[32];
int pdn_id;
sprintf(cmd, "AT%%XGETPDNID=%u", cid);
ret = sm_util_at_scanf(cmd, "%%XGETPDNID: %d", &pdn_id);
if (ret < 0) {
LOG_ERR("Failed to read PDN ID for CID %d, err %d", cid, ret);
return ret;
}
return pdn_id;
}
static int pdn_sa_family_from_ip_string(const char *src)
{
char buf[INET6_ADDRSTRLEN];
if (zsock_inet_pton(AF_INET, src, buf)) {
return AF_INET;
} else if (zsock_inet_pton(AF_INET6, src, buf)) {
return AF_INET6;
}
return -1;
}
/** @brief Fill PDN dynamic info with DNS addresses adnd mtu. */
static void pdn_dynamic_info_dns_addr_fill(struct sm_pdn_dynamic_info *pdn_info, uint32_t mtu,
const char *dns_addr_str_primary,
const char *dns_addr_str_secondary)
{
const int family = pdn_sa_family_from_ip_string(dns_addr_str_primary);
if (family == AF_INET) {
(void)zsock_inet_pton(AF_INET, dns_addr_str_primary,
&(pdn_info->dns_addr4_primary));
(void)zsock_inet_pton(AF_INET, dns_addr_str_secondary,
&(pdn_info->dns_addr4_secondary));
pdn_info->ipv4_mtu = mtu;
} else if (family == AF_INET6) {
(void)zsock_inet_pton(AF_INET6, dns_addr_str_primary,
&(pdn_info->dns_addr6_primary));
(void)zsock_inet_pton(AF_INET6, dns_addr_str_secondary,
&(pdn_info->dns_addr6_secondary));
pdn_info->ipv6_mtu = mtu;
}
}
#define AT_CMD_PDN_CONTEXT_READ_INFO "AT+CGCONTRDP=%u"
#define AT_CMD_PDN_CONTEXT_READ_INFO_PARSE_LINE1 \
"+CGCONTRDP: %*u,,\"%*[^\"]\",\"\",\"\",\"%15[0-9.]\",\"%15[0-9.]\",,,,,%u"
#define AT_CMD_PDN_CONTEXT_READ_INFO_PARSE_LINE2 \
"+%*[^+]"\
"+CGCONTRDP: %*u,,\"%*[^\"]\",\"\",\"\",\"%39[0-9A-Fa-f:]\",\"%39[0-9A-Fa-f:]\",,,,,%u"
int sm_util_pdn_dynamic_info_get(uint8_t cid, struct sm_pdn_dynamic_info *pdn_info)
{
int ret;
char at_cmd_buf[sizeof("AT+CGCONTRDP=###")];
char dns_addr_str_primary[INET6_ADDRSTRLEN];
char dns_addr_str_secondary[INET6_ADDRSTRLEN];
uint32_t mtu = 0;
if (!pdn_info) {
return -EINVAL;
}
/* Reset PDN dynamic info. */
memset(pdn_info, 0, sizeof(struct sm_pdn_dynamic_info));
/* Clear secondary DNS address buffer. */
memset(dns_addr_str_secondary, 0, sizeof(dns_addr_str_secondary));
(void)snprintf(at_cmd_buf, sizeof(at_cmd_buf), AT_CMD_PDN_CONTEXT_READ_INFO, cid);
ret = sm_util_at_scanf(at_cmd_buf, AT_CMD_PDN_CONTEXT_READ_INFO_PARSE_LINE1,
dns_addr_str_primary, dns_addr_str_secondary, &mtu);
if (ret < 1) {
/* Don't log an error if no PDN connections are active, this may not be considered
* an error by the caller.
*/
if (ret != -NRF_EBADMSG) {
LOG_ERR("nrf_modem_at_scanf failed, ret: %d", ret);
}
return ret;
}
pdn_dynamic_info_dns_addr_fill(pdn_info, mtu, dns_addr_str_primary, dns_addr_str_secondary);
/* Reset secondary DNS address buffer and mtu. */
memset(dns_addr_str_secondary, 0, sizeof(dns_addr_str_secondary));
mtu = 0;
/* Scan second line if PDN has dual stack capabilities. */
ret = sm_util_at_scanf(at_cmd_buf, AT_CMD_PDN_CONTEXT_READ_INFO_PARSE_LINE2,
dns_addr_str_primary, dns_addr_str_secondary, &mtu);
if (ret < 1) {
/* We only got one entry, but that is ok. */
return 0;
}
pdn_dynamic_info_dns_addr_fill(pdn_info, mtu, dns_addr_str_primary, dns_addr_str_secondary);
return 0;
}