Skip to content

Commit 6827559

Browse files
jtbaumannjohnlange2
authored andcommitted
shell: modem shell sms error made more descriptive
Made modem shell SMS truncation warning more informative. Signed-off-by: Jared Baumann <[email protected]>
1 parent abf4670 commit 6827559

File tree

1 file changed

+70
-104
lines changed

1 file changed

+70
-104
lines changed

drivers/modem/modem_shell.c

Lines changed: 70 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,23 @@ struct modem_shell_user_data {
3333

3434
#if defined(CONFIG_MODEM_CONTEXT)
3535
#include "modem_context.h"
36-
#define ms_context modem_context
37-
#define ms_max_context CONFIG_MODEM_CONTEXT_MAX_NUM
38-
#define ms_send(ctx_, buf_, size_) \
39-
(ctx_->iface.write(&ctx_->iface, buf_, size_))
40-
#define ms_context_from_id modem_context_from_id
41-
#define UART_DEV_NAME(ctx) (ctx->iface.dev->name)
36+
#define ms_context modem_context
37+
#define ms_max_context CONFIG_MODEM_CONTEXT_MAX_NUM
38+
#define ms_send(ctx_, buf_, size_) (ctx_->iface.write(&ctx_->iface, buf_, size_))
39+
#define ms_context_from_id modem_context_from_id
40+
#define UART_DEV_NAME(ctx) (ctx->iface.dev->name)
4241
#elif defined(CONFIG_MODEM_RECEIVER)
4342
#include "modem_receiver.h"
44-
#define ms_context mdm_receiver_context
45-
#define ms_max_context CONFIG_MODEM_RECEIVER_MAX_CONTEXTS
46-
#define ms_send mdm_receiver_send
47-
#define ms_context_from_id mdm_receiver_context_from_id
48-
#define UART_DEV_NAME(ctx_) (ctx_->uart_dev->name)
43+
#define ms_context mdm_receiver_context
44+
#define ms_max_context CONFIG_MODEM_RECEIVER_MAX_CONTEXTS
45+
#define ms_send mdm_receiver_send
46+
#define ms_context_from_id mdm_receiver_context_from_id
47+
#define UART_DEV_NAME(ctx_) (ctx_->uart_dev->name)
4948
#else
5049
#error "MODEM_CONTEXT or MODEM_RECEIVER need to be enabled"
5150
#endif
5251

53-
static int cmd_modem_list(const struct shell *sh, size_t argc,
54-
char *argv[])
52+
static int cmd_modem_list(const struct shell *sh, size_t argc, char *argv[])
5553
{
5654
struct ms_context *mdm_ctx;
5755
int i, count = 0;
@@ -63,39 +61,33 @@ static int cmd_modem_list(const struct shell *sh, size_t argc,
6361
if (mdm_ctx) {
6462
count++;
6563
shell_fprintf(sh, SHELL_NORMAL,
66-
"%d:\tIface Device: %s\n"
67-
"\tManufacturer: %s\n"
68-
"\tModel: %s\n"
69-
"\tRevision: %s\n"
70-
"\tIMEI: %s\n"
64+
"%d:\tIface Device: %s\n"
65+
"\tManufacturer: %s\n"
66+
"\tModel: %s\n"
67+
"\tRevision: %s\n"
68+
"\tIMEI: %s\n"
7169
#if defined(CONFIG_MODEM_SIM_NUMBERS)
72-
"\tIMSI: %s\n"
73-
"\tICCID: %s\n"
70+
"\tIMSI: %s\n"
71+
"\tICCID: %s\n"
7472
#endif
7573
#if defined(CONFIG_MODEM_CELL_INFO)
76-
"\tOperator: %d\n"
77-
"\tLAC: %d\n"
78-
"\tCellId: %d\n"
79-
"\tAcT: %d\n"
74+
"\tOperator: %d\n"
75+
"\tLAC: %d\n"
76+
"\tCellId: %d\n"
77+
"\tAcT: %d\n"
8078
#endif
81-
"\tRSSI: %d\n",
82-
i,
83-
UART_DEV_NAME(mdm_ctx),
84-
mdm_ctx->data_manufacturer,
85-
mdm_ctx->data_model,
86-
mdm_ctx->data_revision,
87-
mdm_ctx->data_imei,
79+
"\tRSSI: %d\n",
80+
i, UART_DEV_NAME(mdm_ctx), mdm_ctx->data_manufacturer,
81+
mdm_ctx->data_model, mdm_ctx->data_revision,
82+
mdm_ctx->data_imei,
8883
#if defined(CONFIG_MODEM_SIM_NUMBERS)
89-
mdm_ctx->data_imsi,
90-
mdm_ctx->data_iccid,
84+
mdm_ctx->data_imsi, mdm_ctx->data_iccid,
9185
#endif
9286
#if defined(CONFIG_MODEM_CELL_INFO)
93-
mdm_ctx->data_operator,
94-
mdm_ctx->data_lac,
95-
mdm_ctx->data_cellid,
96-
mdm_ctx->data_act,
87+
mdm_ctx->data_operator, mdm_ctx->data_lac,
88+
mdm_ctx->data_cellid, mdm_ctx->data_act,
9789
#endif
98-
mdm_ctx->data_rssi ? *mdm_ctx->data_rssi : 0);
90+
mdm_ctx->data_rssi ? *mdm_ctx->data_rssi : 0);
9991
}
10092
}
10193

@@ -106,25 +98,22 @@ static int cmd_modem_list(const struct shell *sh, size_t argc,
10698
return 0;
10799
}
108100

109-
static int cmd_modem_send(const struct shell *sh, size_t argc,
110-
char *argv[])
101+
static int cmd_modem_send(const struct shell *sh, size_t argc, char *argv[])
111102
{
112103
struct ms_context *mdm_ctx;
113104
char *endptr;
114105
int ret, i, arg = 1;
115106

116107
/* list */
117108
if (!argv[arg]) {
118-
shell_fprintf(sh, SHELL_ERROR,
119-
"Please enter a modem index\n");
109+
shell_fprintf(sh, SHELL_ERROR, "Please enter a modem index\n");
120110
return -EINVAL;
121111
}
122112

123113
/* <index> of modem receiver */
124114
i = (int)strtol(argv[arg], &endptr, 10);
125115
if (*endptr != '\0') {
126-
shell_fprintf(sh, SHELL_ERROR,
127-
"Please enter a modem index\n");
116+
shell_fprintf(sh, SHELL_ERROR, "Please enter a modem index\n");
128117
return -EINVAL;
129118
}
130119

@@ -137,8 +126,7 @@ static int cmd_modem_send(const struct shell *sh, size_t argc,
137126
for (i = arg + 1; i < argc; i++) {
138127
ret = ms_send(mdm_ctx, argv[i], strlen(argv[i]));
139128
if (ret < 0) {
140-
shell_fprintf(sh, SHELL_ERROR,
141-
"Error sending '%s': %d\n", argv[i], ret);
129+
shell_fprintf(sh, SHELL_ERROR, "Error sending '%s': %d\n", argv[i], ret);
142130
return 0;
143131
}
144132

@@ -149,9 +137,7 @@ static int cmd_modem_send(const struct shell *sh, size_t argc,
149137
}
150138

151139
if (ret < 0) {
152-
shell_fprintf(sh, SHELL_ERROR,
153-
"Error sending (CRLF or space): %d\n",
154-
ret);
140+
shell_fprintf(sh, SHELL_ERROR, "Error sending (CRLF or space): %d\n", ret);
155141
return 0;
156142
}
157143
}
@@ -160,17 +146,16 @@ static int cmd_modem_send(const struct shell *sh, size_t argc,
160146
}
161147

162148
#if defined(CONFIG_GSM_MUX)
163-
static void uart_mux_cb(const struct device *uart, const struct device *dev,
164-
int dlci_address, void *user_data)
149+
static void uart_mux_cb(const struct device *uart, const struct device *dev, int dlci_address,
150+
void *user_data)
165151
{
166152
struct modem_shell_user_data *data = user_data;
167153
const struct shell *shell = data->shell;
168154
int *count = data->user_data;
169155
const char *ch = "?";
170156

171157
if (*count == 0) {
172-
shell_fprintf(shell, SHELL_NORMAL,
173-
"\nReal UART\tMUX UART\tDLCI\n");
158+
shell_fprintf(shell, SHELL_NORMAL, "\nReal UART\tMUX UART\tDLCI\n");
174159
}
175160

176161
(*count)++;
@@ -183,9 +168,8 @@ static void uart_mux_cb(const struct device *uart, const struct device *dev,
183168
ch = "control";
184169
}
185170

186-
shell_fprintf(shell, SHELL_NORMAL,
187-
"%s\t\t%s\t\t%d (%s)\n",
188-
uart->name, dev->name, dlci_address, ch);
171+
shell_fprintf(shell, SHELL_NORMAL, "%s\t\t%s\t\t%d (%s)\n", uart->name, dev->name,
172+
dlci_address, ch);
189173
}
190174
#endif
191175

@@ -197,16 +181,14 @@ static int cmd_modem_info(const struct shell *shell, size_t argc, char *argv[])
197181

198182
/* info */
199183
if (!argv[arg]) {
200-
shell_fprintf(shell, SHELL_ERROR,
201-
"Please enter a modem index\n");
184+
shell_fprintf(shell, SHELL_ERROR, "Please enter a modem index\n");
202185
return -EINVAL;
203186
}
204187

205188
/* <index> of modem receiver */
206189
i = (int)strtol(argv[arg], &endptr, 10);
207190
if (*endptr != '\0') {
208-
shell_fprintf(shell, SHELL_ERROR,
209-
"Please enter a modem index\n");
191+
shell_fprintf(shell, SHELL_ERROR, "Please enter a modem index\n");
210192
return -EINVAL;
211193
}
212194

@@ -224,16 +206,11 @@ static int cmd_modem_info(const struct shell *shell, size_t argc, char *argv[])
224206
"Revision : %s\n"
225207
"IMEI : %s\n"
226208
"RSSI : %d\n",
227-
i,
228-
UART_DEV_NAME(mdm_ctx),
229-
mdm_ctx->data_manufacturer,
230-
mdm_ctx->data_model,
231-
mdm_ctx->data_revision,
232-
mdm_ctx->data_imei,
209+
i, UART_DEV_NAME(mdm_ctx), mdm_ctx->data_manufacturer, mdm_ctx->data_model,
210+
mdm_ctx->data_revision, mdm_ctx->data_imei,
233211
mdm_ctx->data_rssi ? *mdm_ctx->data_rssi : 0);
234212

235-
shell_fprintf(shell, SHELL_NORMAL,
236-
"GSM 07.10 muxing : %s\n",
213+
shell_fprintf(shell, SHELL_NORMAL, "GSM 07.10 muxing : %s\n",
237214
IS_ENABLED(CONFIG_GSM_MUX) ? "enabled" : "disabled");
238215

239216
#if defined(CONFIG_GSM_MUX)
@@ -250,8 +227,7 @@ static int cmd_modem_info(const struct shell *shell, size_t argc, char *argv[])
250227
}
251228

252229
#if defined(CONFIG_MODEM_SMS)
253-
static int cmd_modem_sms_send(const struct shell *shell, size_t argc,
254-
char *argv[])
230+
static int cmd_modem_sms_send(const struct shell *shell, size_t argc, char *argv[])
255231
{
256232
struct ms_context *ms_ctx;
257233
struct sms_out sms;
@@ -266,41 +242,36 @@ static int cmd_modem_sms_send(const struct shell *shell, size_t argc,
266242

267243
ms_ctx = ms_context_from_id(modem_index);
268244
if (ms_ctx == NULL) {
269-
shell_fprintf(
270-
shell, SHELL_ERROR,
271-
"Couldn't open context for modem index %d\n",
272-
modem_index);
245+
shell_fprintf(shell, SHELL_ERROR,
246+
"Couldn't open context for modem index %d\n", modem_index);
273247
return -ENOEXEC;
274248
}
275249

276250
if (ms_ctx->send_sms == NULL) {
277-
shell_fprintf(shell, SHELL_ERROR,
278-
"No function defined to send SMS\n");
251+
shell_fprintf(shell, SHELL_ERROR, "No function defined to send SMS\n");
279252
return -ENOEXEC;
280253
}
281254

282255
if (strlen(argv[3]) > (sizeof(sms.msg) - 1)) {
283256
shell_fprintf(shell, SHELL_WARNING,
284-
"Specified message longer than maximum, truncating message\n");
257+
"SMS message exceeds limit (%d>%d), truncating message",
258+
strlen(argv[3]), CONFIG_MODEM_SMS_OUT_MSG_MAX_LEN);
285259
}
286260

287261
snprintk(sms.phone, sizeof(sms.phone), "%s", argv[2]);
288262
snprintk(sms.msg, sizeof(sms.msg), "%s", argv[3]);
289263

290264
ret = ms_ctx->send_sms(&sms);
291265
if (ret == 0) {
292-
shell_fprintf(shell, SHELL_NORMAL,
293-
"SMS msg was sent\n");
266+
shell_fprintf(shell, SHELL_NORMAL, "SMS msg was sent\n");
294267
} else {
295-
shell_fprintf(shell, SHELL_ERROR,
296-
"error: send_sms returned %d\n", ret);
268+
shell_fprintf(shell, SHELL_ERROR, "error: send_sms returned %d\n", ret);
297269
}
298270
}
299271
return ret;
300272
}
301273

302-
static int cmd_modem_sms_recv(const struct shell *shell, size_t argc,
303-
char *argv[])
274+
static int cmd_modem_sms_recv(const struct shell *shell, size_t argc, char *argv[])
304275
{
305276
struct ms_context *ms_ctx;
306277
struct sms_in sms;
@@ -316,33 +287,27 @@ static int cmd_modem_sms_recv(const struct shell *shell, size_t argc,
316287

317288
ms_ctx = ms_context_from_id(modem_index);
318289
if (ms_ctx == NULL) {
319-
shell_fprintf(
320-
shell, SHELL_ERROR,
321-
"Couldn't open context for modem index %d\n",
322-
modem_index);
290+
shell_fprintf(shell, SHELL_ERROR,
291+
"Couldn't open context for modem index %d\n", modem_index);
323292
return -ENOEXEC;
324293
}
325294

326295
if (ms_ctx->recv_sms == NULL) {
327-
shell_fprintf(shell, SHELL_ERROR,
328-
"No function defined to receive SMS\n");
296+
shell_fprintf(shell, SHELL_ERROR, "No function defined to receive SMS\n");
329297
return -ENOEXEC;
330298
}
331299

332300
sms.timeout = K_SECONDS(wait);
333301
ret = ms_ctx->recv_sms(&sms);
334302
if (ret < 0) {
335-
shell_fprintf(shell, SHELL_ERROR,
336-
"recv_sms returned error %d, errno:%d\n",
303+
shell_fprintf(shell, SHELL_ERROR, "recv_sms returned error %d, errno:%d\n",
337304
ret, errno);
338305
} else if (ret == 0) {
339-
shell_fprintf(shell, SHELL_NORMAL,
340-
"No SMS msgs available\n");
306+
shell_fprintf(shell, SHELL_NORMAL, "No SMS msgs available\n");
341307
} else {
342-
shell_fprintf(
343-
shell, SHELL_NORMAL,
344-
"Received SMS msg from %s dated %s: '%s'\n",
345-
sms.phone, sms.time, sms.msg);
308+
shell_fprintf(shell, SHELL_NORMAL,
309+
"Received SMS msg from %s dated %s: '%s'\n", sms.phone,
310+
sms.time, sms.msg);
346311
}
347312
}
348313
return ret;
@@ -361,15 +326,16 @@ SHELL_STATIC_SUBCMD_SET_CREATE(modem_cmd_sms,
361326

362327
#endif /* CONFIG_MODEM_SMS */
363328

364-
SHELL_STATIC_SUBCMD_SET_CREATE(sub_modem,
365-
SHELL_CMD(info, NULL, "Show information for a modem", cmd_modem_info),
329+
SHELL_STATIC_SUBCMD_SET_CREATE(
330+
sub_modem, SHELL_CMD(info, NULL, "Show information for a modem", cmd_modem_info),
366331
SHELL_CMD(list, NULL, "List registered modems", cmd_modem_list),
367-
SHELL_CMD(send, NULL, "Send an AT <command> to a registered modem "
368-
"receiver", cmd_modem_send),
332+
SHELL_CMD(send, NULL,
333+
"Send an AT <command> to a registered modem "
334+
"receiver",
335+
cmd_modem_send),
369336
#if defined(CONFIG_MODEM_SMS)
370-
SHELL_CMD(sms, &modem_cmd_sms, "Send or receive SMS message via modem",
371-
NULL),
372-
#endif /* CONFIG_MODEM_SMS */
337+
SHELL_CMD(sms, &modem_cmd_sms, "Send or receive SMS message via modem", NULL),
338+
#endif /* CONFIG_MODEM_SMS */
373339
SHELL_SUBCMD_SET_END /* Array terminated. */
374340
);
375341

0 commit comments

Comments
 (0)