forked from nrfconnect/ncs-serial-modem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsm_at_commands.c
More file actions
380 lines (325 loc) · 9.39 KB
/
Copy pathsm_at_commands.c
File metadata and controls
380 lines (325 loc) · 9.39 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
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <zephyr/init.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/sys/reboot.h>
#include <zephyr/sys/util.h>
#include <zephyr/types.h>
#include <dfu/dfu_target.h>
#include <tfm/tfm_ioctl_api.h>
#include <pm_config.h>
#include <modem/at_parser.h>
#include <modem/lte_lc.h>
#include <modem/modem_jwt.h>
#include <modem/nrf_modem_lib.h>
#include "nrf_modem.h"
#include "ncs_version.h"
#include "sm_util.h"
#include "sm_ctrl_pin.h"
#include "sm_settings.h"
#include "sm_at_host.h"
#include "sm_at_fota.h"
#include "sm_version.h"
#include "sm_at_nrfcloud.h"
#include "sm_log.h"
LOG_MODULE_REGISTER(sm_at, CONFIG_SM_LOG_LEVEL);
/** @brief Shutdown modes. */
enum sleep_modes {
SLEEP_MODE_INVALID,
SLEEP_MODE_DEEP,
SLEEP_MODE_IDLE
};
static void go_sleep_wk(struct k_work *);
static struct {
struct k_work_delayable work;
uint32_t mode;
} sleep_control = {
.work = Z_WORK_DELAYABLE_INITIALIZER(go_sleep_wk),
};
bool sm_is_modem_functional_mode(enum lte_lc_func_mode mode)
{
int cfun;
int rc = sm_util_at_scanf("AT+CFUN?", "+CFUN: %d", &cfun);
return (rc == 1 && cfun == mode);
}
int sm_power_off_modem(void)
{
/* "[...] there may be a delay until modem is disconnected from the network."
* https://docs.nordicsemi.com/bundle/ps_nrf9151/page/chapters/pmu/doc/operationmodes/system_off_mode.html
* This will return once the modem responds, which means it has actually stopped.
* This has been observed to take between 1 and 2 seconds when it is not already stopped.
*/
return sm_util_at_printf("AT+CFUN=0");
}
SM_AT_CMD_CUSTOM(xsmver, "AT#XSMVER", handle_at_smver);
STATIC int handle_at_smver(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
int ret = -EINVAL;
if (cmd_type == AT_PARSER_CMD_TYPE_SET) {
if (strlen(CONFIG_SM_CUSTOMER_VERSION) > 0) {
rsp_send("\r\n#XSMVER: %s,%s,\"%s\"\r\n",
STRINGIFY(SM_VERSION), STRINGIFY(NCS_VERSION_STRING),
CONFIG_SM_CUSTOMER_VERSION);
} else {
rsp_send("\r\n#XSMVER: %s,%s\r\n",
STRINGIFY(SM_VERSION), STRINGIFY(NCS_VERSION_STRING));
}
ret = 0;
}
return ret;
}
static void go_sleep_wk(struct k_work *)
{
if (sleep_control.mode == SLEEP_MODE_IDLE) {
if (sm_at_host_power_off() == 0) {
sm_ctrl_pin_enter_idle();
} else {
LOG_ERR("failed to power off UART");
}
} else if (sleep_control.mode == SLEEP_MODE_DEEP) {
sm_ctrl_pin_enter_sleep();
}
}
SM_AT_CMD_CUSTOM(xsleep, "AT#XSLEEP", handle_at_sleep);
STATIC int handle_at_sleep(enum at_parser_cmd_type cmd_type, struct at_parser *parser,
uint32_t)
{
int ret = -EINVAL;
if (cmd_type == AT_PARSER_CMD_TYPE_SET) {
ret = at_parser_num_get(parser, 1, &sleep_control.mode);
if (ret) {
return -EINVAL;
}
ret = sm_ctrl_pin_ready();
if (ret) {
return ret;
}
if (sleep_control.mode == SLEEP_MODE_DEEP ||
sleep_control.mode == SLEEP_MODE_IDLE) {
k_work_reschedule_for_queue(&sm_work_q, &sleep_control.work,
SM_UART_RESPONSE_DELAY);
} else {
ret = -EINVAL;
}
} else if (cmd_type == AT_PARSER_CMD_TYPE_TEST) {
rsp_send("\r\n#XSLEEP: (%d,%d)\r\n", SLEEP_MODE_DEEP, SLEEP_MODE_IDLE);
ret = 0;
}
return ret;
}
void final_call(void (*func)(void))
{
/* Delegate the final call to a worker so that the "OK" response is properly sent. */
static struct k_work_delayable worker;
k_work_init_delayable(&worker, (k_work_handler_t)func);
k_work_schedule_for_queue(&sm_work_q, &worker, SM_UART_RESPONSE_DELAY);
}
static void sm_shutdown(void)
{
sm_at_host_uninit();
sm_power_off_modem();
sm_ctrl_pin_enter_shutdown();
}
SM_AT_CMD_CUSTOM(xshutdown, "AT#XSHUTDOWN", handle_at_shutdown);
STATIC int handle_at_shutdown(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
if (cmd_type != AT_PARSER_CMD_TYPE_SET) {
return -EINVAL;
}
final_call(sm_shutdown);
return 0;
}
FUNC_NORETURN void sm_reset(void)
{
sm_at_host_uninit();
sm_power_off_modem();
sm_log_flush();
sys_reboot(SYS_REBOOT_COLD);
}
SM_AT_CMD_CUSTOM(xreset, "AT#XRESET", handle_at_reset);
STATIC int handle_at_reset(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
if (cmd_type != AT_PARSER_CMD_TYPE_SET) {
return -EINVAL;
}
final_call(sm_reset);
return 0;
}
static void sm_modemreset(void)
{
/* The modem must be put in minimal function mode before being shut down. */
sm_power_off_modem();
unsigned int step = 1;
int ret;
ret = nrf_modem_lib_shutdown();
if (ret != 0) {
goto out;
}
++step;
#if defined(CONFIG_SM_FULL_FOTA)
if (sm_modem_full_fota) {
sm_finish_modem_full_fota();
}
#endif
ret = nrf_modem_lib_init();
if (sm_fota_type == SM_FOTA_TYPE_MFW || sm_fota_type == SM_FOTA_TYPE_FULL_MFW) {
sm_fota_post_process();
}
out:
if (ret) {
/* Error; print the step that failed and its error code. */
rsp_send("\r\n#XMODEMRESET: %u,%d\r\n", step, ret);
} else {
rsp_send("\r\n#XMODEMRESET: 0\r\n");
}
rsp_send_ok();
}
SM_AT_CMD_CUSTOM(xmodemreset, "AT#XMODEMRESET", handle_at_modemreset);
STATIC int handle_at_modemreset(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
if (cmd_type != AT_PARSER_CMD_TYPE_SET) {
return -EINVAL;
}
/* Return immediately to allow the custom command handling in libmodem to finish processing,
* before restarting libmodem.
*/
final_call(sm_modemreset);
return -SILENT_AT_COMMAND_RET;
}
SM_AT_CMD_CUSTOM(xuuid, "AT#XUUID", handle_at_uuid);
STATIC int handle_at_uuid(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
int ret;
if (cmd_type != AT_PARSER_CMD_TYPE_SET) {
return -EINVAL;
}
struct nrf_device_uuid dev = {0};
ret = modem_jwt_get_uuids(&dev, NULL);
if (ret) {
LOG_ERR("Get device UUID error: %d", ret);
} else {
rsp_send("\r\n#XUUID: %s\r\n", dev.str);
}
return ret;
}
SM_AT_CMD_CUSTOM(xclac, "AT#XCLAC", handle_at_clac);
STATIC int handle_at_clac(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
if (cmd_type != AT_PARSER_CMD_TYPE_SET) {
return -EINVAL;
}
/* Use AT_CMD_CUSTOM listing for extracting Serial Modem AT commands. */
extern struct nrf_modem_at_cmd_custom _nrf_modem_at_cmd_custom_list_start[];
extern struct nrf_modem_at_cmd_custom _nrf_modem_at_cmd_custom_list_end[];
size_t cmd_custom_count = _nrf_modem_at_cmd_custom_list_end -
_nrf_modem_at_cmd_custom_list_start;
size_t base_cmd_len[cmd_custom_count];
memset(base_cmd_len, 0, cmd_custom_count * sizeof(size_t));
rsp_send("\r\n");
for (size_t i = 0; i < cmd_custom_count; i++) {
const char *cmd = _nrf_modem_at_cmd_custom_list_start[i].cmd;
/* Modem AT commands start with 'AT+' or AT%. Other commands are
* Serial Modem specific'. Skip modem AT commands.
* Exceptions that are implemented in Serial Modem:
* * AT+IPR
* * AT+CMUX
* * AT+CGDATA
*/
if ((strncasecmp(cmd, "AT+", strlen("AT+")) == 0 &&
strncasecmp(cmd, "AT+IPR", strlen("AT+IPR")) != 0 &&
strncasecmp(cmd, "AT+CMUX", strlen("AT+CMUX")) != 0 &&
strncasecmp(cmd, "AT+CGDATA", strlen("AT+CGDATA")) != 0) ||
strncasecmp(cmd, "AT%%", strlen("AT%%")) == 0) {
continue;
}
/* List commands without operations and list each command only once. */
base_cmd_len[i] = strcspn(_nrf_modem_at_cmd_custom_list_start[i].cmd, "?=");
bool duplicate = false;
for (size_t j = 0; j < i; j++) {
/* Compare length and command as we have AT commands such as
* AT#XSEND/AT#XSENDTO, AT#XBIND="whatever"
* and AT#XNRFCLOUD[=?]/AT#XNRFCLOUDPOS.
*/
if ((base_cmd_len[i] == base_cmd_len[j]) &&
!strncasecmp(_nrf_modem_at_cmd_custom_list_start[i].cmd,
_nrf_modem_at_cmd_custom_list_start[j].cmd,
base_cmd_len[i])) {
duplicate = true;
break;
}
}
if (!duplicate) {
rsp_send("%.*s\r\n", base_cmd_len[i],
_nrf_modem_at_cmd_custom_list_start[i].cmd);
}
}
return 0;
}
SM_AT_CMD_CUSTOM(ate0, "ATE0", handle_ate0);
STATIC int handle_ate0(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
sm_at_host_echo(false);
return 0;
}
SM_AT_CMD_CUSTOM(ate1, "ATE1", handle_ate1);
STATIC int handle_ate1(enum at_parser_cmd_type cmd_type, struct at_parser *, uint32_t)
{
sm_at_host_echo(true);
return 0;
}
/** @brief Operations for AT#XBOOTINFO. */
enum xbootinfo_op {
XBOOTINFO_OP_VERSION = 0,
XBOOTINFO_OP_SLOT = 1,
};
SM_AT_CMD_CUSTOM(xbootinfo, "AT#XBOOTINFO", handle_at_xbootinfo);
STATIC int handle_at_xbootinfo(enum at_parser_cmd_type cmd_type, struct at_parser *parser,
uint32_t param_count)
{
ARG_UNUSED(param_count);
#if !defined(PM_S1_ADDRESS)
return -ENOTSUP;
#else
switch (cmd_type) {
case AT_PARSER_CMD_TYPE_SET: {
uint32_t op = 0;
int err = at_parser_num_get(parser, 1, &op);
if (err) {
return -EINVAL;
}
if (op == XBOOTINFO_OP_VERSION) {
uint32_t version = 0;
err = sm_util_mcuboot_active_version(&version);
if (err) {
return err;
}
rsp_send("\r\n#XBOOTINFO: %u\r\n", version);
} else if (op == XBOOTINFO_OP_SLOT) {
bool s0_active = false;
err = tfm_platform_s0_active(PM_S0_ADDRESS, PM_S1_ADDRESS, &s0_active);
if (err != 0) {
return err;
}
rsp_send("\r\n#XBOOTINFO: %u\r\n", s0_active ? 0U : 1U);
} else {
return -EINVAL;
}
return 0;
}
case AT_PARSER_CMD_TYPE_TEST:
rsp_send("\r\n#XBOOTINFO: (%d,%d)\r\n", XBOOTINFO_OP_VERSION, XBOOTINFO_OP_SLOT);
return 0;
default:
return -EINVAL;
}
#endif
}