1 /** @file
2  * @brief Modem shell module
3  *
4  * Provide some modem shell commands that can be useful to applications.
5  */
6 
7 /*
8  * Copyright (c) 2018 Foundries.io
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #define LOG_MODULE_NAME modem_shell
14 
15 #include <zephyr/kernel.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <zephyr/device.h>
19 #include <zephyr/shell/shell.h>
20 #include <zephyr/drivers/console/uart_mux.h>
21 
22 #include <zephyr/sys/printk.h>
23 
24 struct modem_shell_user_data {
25 	const struct shell *sh;
26 	void *user_data;
27 };
28 
29 #if defined(CONFIG_MODEM_CONTEXT)
30 #include "modem_context.h"
31 #define ms_context		modem_context
32 #define ms_max_context		CONFIG_MODEM_CONTEXT_MAX_NUM
33 #define ms_send(ctx_, buf_, size_) \
34 			(ctx_->iface.write(&ctx_->iface, buf_, size_))
35 #define ms_context_from_id	modem_context_from_id
36 #define UART_DEV_NAME(ctx)	(ctx->iface.dev->name)
37 #elif defined(CONFIG_MODEM_RECEIVER)
38 #include "modem_receiver.h"
39 #define ms_context		mdm_receiver_context
40 #define ms_max_context		CONFIG_MODEM_RECEIVER_MAX_CONTEXTS
41 #define ms_send			mdm_receiver_send
42 #define ms_context_from_id	mdm_receiver_context_from_id
43 #define UART_DEV_NAME(ctx_)	(ctx_->uart_dev->name)
44 #else
45 #error "MODEM_CONTEXT or MODEM_RECEIVER need to be enabled"
46 #endif
47 
cmd_modem_list(const struct shell * sh,size_t argc,char * argv[])48 static int cmd_modem_list(const struct shell *sh, size_t argc,
49 			  char *argv[])
50 {
51 	struct ms_context *mdm_ctx;
52 	int i, count = 0;
53 
54 	shell_fprintf(sh, SHELL_NORMAL, "Modem receivers:\n");
55 
56 	for (i = 0; i < ms_max_context; i++) {
57 		mdm_ctx = ms_context_from_id(i);
58 		if (mdm_ctx) {
59 			count++;
60 			shell_fprintf(sh, SHELL_NORMAL,
61 			     "%d:\tIface Device: %s\n"
62 				"\tManufacturer: %s\n"
63 				"\tModel:        %s\n"
64 				"\tRevision:     %s\n"
65 				"\tIMEI:         %s\n"
66 #if defined(CONFIG_MODEM_SIM_NUMBERS)
67 				"\tIMSI:         %s\n"
68 				"\tICCID:        %s\n"
69 #endif
70 #if defined(CONFIG_MODEM_CELL_INFO)
71 				"\tOperator:     %d\n"
72 				"\tLAC:          %d\n"
73 				"\tCellId:       %d\n"
74 				"\tAcT:          %d\n"
75 #endif
76 				"\tRSSI:         %d\n",
77 			       i,
78 			       UART_DEV_NAME(mdm_ctx),
79 			       mdm_ctx->data_manufacturer,
80 			       mdm_ctx->data_model,
81 			       mdm_ctx->data_revision,
82 			       mdm_ctx->data_imei,
83 #if defined(CONFIG_MODEM_SIM_NUMBERS)
84 			       mdm_ctx->data_imsi,
85 			       mdm_ctx->data_iccid,
86 #endif
87 #if defined(CONFIG_MODEM_CELL_INFO)
88 			       mdm_ctx->data_operator,
89 			       mdm_ctx->data_lac,
90 			       mdm_ctx->data_cellid,
91 			       mdm_ctx->data_act,
92 #endif
93 			       mdm_ctx->data_rssi ? *mdm_ctx->data_rssi : 0);
94 		}
95 	}
96 
97 	if (!count) {
98 		shell_fprintf(sh, SHELL_NORMAL, "None found.\n");
99 	}
100 
101 	return 0;
102 }
103 
cmd_modem_send(const struct shell * sh,size_t argc,char * argv[])104 static int cmd_modem_send(const struct shell *sh, size_t argc,
105 			  char *argv[])
106 {
107 	struct ms_context *mdm_ctx;
108 	char *endptr;
109 	int ret, i, arg = 1;
110 
111 	/* list */
112 	if (!argv[arg]) {
113 		shell_fprintf(sh, SHELL_ERROR,
114 			      "Please enter a modem index\n");
115 		return -EINVAL;
116 	}
117 
118 	/* <index> of modem receiver */
119 	i = (int)strtol(argv[arg], &endptr, 10);
120 	if (*endptr != '\0') {
121 		shell_fprintf(sh, SHELL_ERROR,
122 			      "Please enter a modem index\n");
123 		return -EINVAL;
124 	}
125 
126 	mdm_ctx = ms_context_from_id(i);
127 	if (!mdm_ctx) {
128 		shell_fprintf(sh, SHELL_ERROR, "Modem receiver not found!");
129 		return 0;
130 	}
131 
132 	for (i = arg + 1; i < argc; i++) {
133 		ret = ms_send(mdm_ctx, argv[i], strlen(argv[i]));
134 		if (ret < 0) {
135 			shell_fprintf(sh, SHELL_ERROR,
136 				      "Error sending '%s': %d\n", argv[i], ret);
137 			return 0;
138 		}
139 
140 		if (i == argc - 1) {
141 			ret = ms_send(mdm_ctx, "\r", 1);
142 		} else {
143 			ret = ms_send(mdm_ctx, " ", 1);
144 		}
145 
146 		if (ret < 0) {
147 			shell_fprintf(sh, SHELL_ERROR,
148 				      "Error sending (CRLF or space): %d\n",
149 				      ret);
150 			return 0;
151 		}
152 	}
153 
154 	return 0;
155 }
156 
157 #if defined(CONFIG_GSM_MUX)
uart_mux_cb(const struct device * uart,const struct device * dev,int dlci_address,void * user_data)158 static void uart_mux_cb(const struct device *uart, const struct device *dev,
159 			int dlci_address, void *user_data)
160 {
161 	struct modem_shell_user_data *data = user_data;
162 	const struct shell *sh = data->sh;
163 	int *count = data->user_data;
164 	const char *ch = "?";
165 
166 	if (*count == 0) {
167 		shell_fprintf(sh, SHELL_NORMAL,
168 			      "\nReal UART\tMUX UART\tDLCI\n");
169 	}
170 
171 	(*count)++;
172 
173 	if (dlci_address == CONFIG_GSM_MUX_DLCI_AT) {
174 		ch = "AT";
175 	} else if (dlci_address == CONFIG_GSM_MUX_DLCI_PPP) {
176 		ch = "PPP";
177 	} else if (dlci_address == 0) {
178 		ch = "control";
179 	}
180 
181 	shell_fprintf(sh, SHELL_NORMAL,
182 		      "%s\t\t%s\t\t%d (%s)\n",
183 		      uart->name, dev->name, dlci_address, ch);
184 }
185 #endif
186 
cmd_modem_info(const struct shell * sh,size_t argc,char * argv[])187 static int cmd_modem_info(const struct shell *sh, size_t argc, char *argv[])
188 {
189 	struct ms_context *mdm_ctx;
190 	char *endptr;
191 	int i, arg = 1;
192 
193 	/* info */
194 	if (!argv[arg]) {
195 		shell_fprintf(sh, SHELL_ERROR,
196 			      "Please enter a modem index\n");
197 		return -EINVAL;
198 	}
199 
200 	/* <index> of modem receiver */
201 	i = (int)strtol(argv[arg], &endptr, 10);
202 	if (*endptr != '\0') {
203 		shell_fprintf(sh, SHELL_ERROR,
204 			      "Please enter a modem index\n");
205 		return -EINVAL;
206 	}
207 
208 	mdm_ctx = ms_context_from_id(i);
209 	if (!mdm_ctx) {
210 		shell_fprintf(sh, SHELL_ERROR, "Modem receiver not found!");
211 		return 0;
212 	}
213 
214 	shell_fprintf(sh, SHELL_NORMAL,
215 		      "Modem index      : %d\n"
216 		      "Iface Device     : %s\n"
217 		      "Manufacturer     : %s\n"
218 		      "Model            : %s\n"
219 		      "Revision         : %s\n"
220 		      "IMEI             : %s\n"
221 		      "RSSI             : %d\n",
222 		      i,
223 		      UART_DEV_NAME(mdm_ctx),
224 		      mdm_ctx->data_manufacturer,
225 		      mdm_ctx->data_model,
226 		      mdm_ctx->data_revision,
227 		      mdm_ctx->data_imei,
228 		      mdm_ctx->data_rssi ? *mdm_ctx->data_rssi : 0);
229 
230 	shell_fprintf(sh, SHELL_NORMAL,
231 		      "GSM 07.10 muxing : %s\n",
232 		      IS_ENABLED(CONFIG_GSM_MUX) ? "enabled" : "disabled");
233 
234 #if defined(CONFIG_GSM_MUX)
235 	struct modem_shell_user_data user_data;
236 	int count = 0;
237 
238 	user_data.sh = sh;
239 	user_data.user_data = &count;
240 
241 	uart_mux_foreach(uart_mux_cb, &user_data);
242 #endif
243 
244 	return 0;
245 }
246 
247 SHELL_STATIC_SUBCMD_SET_CREATE(sub_modem,
248 	SHELL_CMD(info, NULL, "Show information for a modem", cmd_modem_info),
249 	SHELL_CMD(list, NULL, "List registered modems", cmd_modem_list),
250 	SHELL_CMD(send, NULL, "Send an AT <command> to a registered modem "
251 			      "receiver", cmd_modem_send),
252 	SHELL_SUBCMD_SET_END /* Array terminated. */
253 );
254 
255 SHELL_CMD_REGISTER(modem, &sub_modem, "Modem commands", NULL);
256