1 /*
2 * Copyright (c) 2020 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT zephyr_gsm_ppp
8
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(modem_gsm, CONFIG_MODEM_LOG_LEVEL);
11
12 #include <stdlib.h>
13 #include <zephyr/kernel.h>
14 #include <zephyr/device.h>
15 #include <zephyr/sys/ring_buffer.h>
16 #include <zephyr/sys/util.h>
17 #include <zephyr/net/ppp.h>
18 #include <zephyr/drivers/modem/gsm_ppp.h>
19 #include <zephyr/drivers/uart.h>
20 #include <zephyr/drivers/console/uart_mux.h>
21
22 #include "modem_context.h"
23 #include "modem_iface_uart.h"
24 #include "modem_cmd_handler.h"
25 #include "../console/gsm_mux.h"
26
27 #include <stdio.h>
28
29 #define GSM_UART_NODE DT_INST_BUS(0)
30 #define GSM_CMD_READ_BUF 128
31 #define GSM_CMD_AT_TIMEOUT K_SECONDS(2)
32 #define GSM_CMD_SETUP_TIMEOUT K_SECONDS(6)
33 /* GSM_CMD_LOCK_TIMEOUT should be longer than GSM_CMD_AT_TIMEOUT & GSM_CMD_SETUP_TIMEOUT,
34 * otherwise the gsm_ppp_stop might fail to lock tx.
35 */
36 #define GSM_CMD_LOCK_TIMEOUT K_SECONDS(10)
37 #define GSM_RECV_MAX_BUF 30
38 #define GSM_RECV_BUF_SIZE 128
39 #define GSM_ATTACH_RETRY_DELAY_MSEC 1000
40 #define GSM_REGISTER_DELAY_MSEC 1000
41 #define GSM_RETRY_DELAY K_SECONDS(1)
42
43 #define GSM_RSSI_RETRY_DELAY_MSEC 2000
44 #define GSM_RSSI_RETRIES 10
45 #define GSM_RSSI_INVALID -1000
46
47 #if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
48 #define GSM_RSSI_MAXVAL 0
49 #else
50 #define GSM_RSSI_MAXVAL -51
51 #endif
52
53 /* Modem network registration state */
54 enum network_state {
55 GSM_NET_INIT = -1,
56 GSM_NET_NOT_REGISTERED,
57 GSM_NET_HOME_NETWORK,
58 GSM_NET_SEARCHING,
59 GSM_NET_REGISTRATION_DENIED,
60 GSM_NET_UNKNOWN,
61 GSM_NET_ROAMING,
62 };
63
64 static struct gsm_modem {
65 struct k_mutex lock;
66 const struct device *dev;
67 struct modem_context context;
68
69 struct modem_cmd_handler_data cmd_handler_data;
70 uint8_t cmd_match_buf[GSM_CMD_READ_BUF];
71 struct k_sem sem_response;
72 struct k_sem sem_if_down;
73
74 struct modem_iface_uart_data gsm_data;
75 struct k_work_delayable gsm_configure_work;
76 char gsm_rx_rb_buf[PPP_MRU * 3];
77
78 uint8_t *ppp_recv_buf;
79 size_t ppp_recv_buf_len;
80
81 enum gsm_ppp_state {
82 GSM_PPP_START,
83 GSM_PPP_WAIT_AT,
84 GSM_PPP_AT_RDY,
85 GSM_PPP_STATE_INIT,
86 GSM_PPP_STATE_CONTROL_CHANNEL = GSM_PPP_STATE_INIT,
87 GSM_PPP_STATE_PPP_CHANNEL,
88 GSM_PPP_STATE_AT_CHANNEL,
89 GSM_PPP_STATE_DONE,
90 GSM_PPP_SETUP = GSM_PPP_STATE_DONE,
91 GSM_PPP_REGISTERING,
92 GSM_PPP_ATTACHING,
93 GSM_PPP_ATTACHED,
94 GSM_PPP_SETUP_DONE,
95 GSM_PPP_STOP,
96 GSM_PPP_STATE_ERROR,
97 } state;
98
99 const struct device *ppp_dev;
100 const struct device *at_dev;
101 const struct device *control_dev;
102
103 struct net_if *iface;
104
105 struct k_thread rx_thread;
106 struct k_work_q workq;
107 struct k_work_delayable rssi_work_handle;
108 struct gsm_ppp_modem_info minfo;
109
110 enum network_state net_state;
111
112 int retries;
113 bool modem_info_queried : 1;
114
115 void *user_data;
116
117 gsm_modem_power_cb modem_on_cb;
118 gsm_modem_power_cb modem_off_cb;
119 struct net_mgmt_event_callback gsm_mgmt_cb;
120 } modem;
121
122 NET_BUF_POOL_DEFINE(gsm_recv_pool, GSM_RECV_MAX_BUF, GSM_RECV_BUF_SIZE, 0, NULL);
123 K_KERNEL_STACK_DEFINE(gsm_rx_stack, CONFIG_MODEM_GSM_RX_STACK_SIZE);
124 K_KERNEL_STACK_DEFINE(gsm_workq_stack, CONFIG_MODEM_GSM_WORKQ_STACK_SIZE);
125
gsm_ppp_lock(struct gsm_modem * gsm)126 static inline void gsm_ppp_lock(struct gsm_modem *gsm)
127 {
128 (void)k_mutex_lock(&gsm->lock, K_FOREVER);
129 }
130
gsm_ppp_unlock(struct gsm_modem * gsm)131 static inline void gsm_ppp_unlock(struct gsm_modem *gsm)
132 {
133 (void)k_mutex_unlock(&gsm->lock);
134 }
135
gsm_work_reschedule(struct k_work_delayable * dwork,k_timeout_t delay)136 static inline int gsm_work_reschedule(struct k_work_delayable *dwork, k_timeout_t delay)
137 {
138 return k_work_reschedule_for_queue(&modem.workq, dwork, delay);
139 }
140
141 #if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
142 /* helper macro to keep readability */
143 #define ATOI(s_, value_, desc_) modem_atoi(s_, value_, desc_, __func__)
144
145 /**
146 * @brief Convert string to long integer, but handle errors
147 *
148 * @param s: string with representation of integer number
149 * @param err_value: on error return this value instead
150 * @param desc: name the string being converted
151 * @param func: function where this is called (typically __func__)
152 *
153 * @retval return integer conversion on success, or err_value on error
154 */
modem_atoi(const char * s,const int err_value,const char * desc,const char * func)155 static int modem_atoi(const char *s, const int err_value,
156 const char *desc, const char *func)
157 {
158 int ret;
159 char *endptr;
160
161 ret = (int)strtol(s, &endptr, 10);
162 if ((endptr == NULL) || (*endptr != '\0')) {
163 LOG_ERR("bad %s '%s' in %s", s,
164 desc, func);
165 return err_value;
166 }
167
168 return ret;
169 }
170 #endif
171
gsm_rx(struct gsm_modem * gsm)172 static void gsm_rx(struct gsm_modem *gsm)
173 {
174 LOG_DBG("starting");
175
176 while (true) {
177 modem_iface_uart_rx_wait(&gsm->context.iface, K_FOREVER);
178
179 /* The handler will listen AT channel */
180 modem_cmd_handler_process(&gsm->context.cmd_handler, &gsm->context.iface);
181 }
182 }
183
MODEM_CMD_DEFINE(gsm_cmd_ok)184 MODEM_CMD_DEFINE(gsm_cmd_ok)
185 {
186 (void)modem_cmd_handler_set_error(data, 0);
187 LOG_DBG("ok");
188 k_sem_give(&modem.sem_response);
189 return 0;
190 }
191
MODEM_CMD_DEFINE(gsm_cmd_error)192 MODEM_CMD_DEFINE(gsm_cmd_error)
193 {
194 (void)modem_cmd_handler_set_error(data, -EINVAL);
195 LOG_DBG("error");
196 k_sem_give(&modem.sem_response);
197 return 0;
198 }
199
200 /* Handler: +CME Error: <err>[0] */
MODEM_CMD_DEFINE(gsm_cmd_exterror)201 MODEM_CMD_DEFINE(gsm_cmd_exterror)
202 {
203 /* TODO: map extended error codes to values */
204 (void)modem_cmd_handler_set_error(data, -EIO);
205 k_sem_give(&modem.sem_response);
206 return 0;
207 }
208
209 static const struct modem_cmd response_cmds[] = {
210 MODEM_CMD("OK", gsm_cmd_ok, 0U, ""),
211 MODEM_CMD("ERROR", gsm_cmd_error, 0U, ""),
212 MODEM_CMD("+CME ERROR: ", gsm_cmd_exterror, 1U, ""),
213 MODEM_CMD("CONNECT", gsm_cmd_ok, 0U, ""),
214 };
215
unquoted_atoi(const char * s,int base)216 static int unquoted_atoi(const char *s, int base)
217 {
218 if (*s == '"') {
219 s++;
220 }
221
222 return strtol(s, NULL, base);
223 }
224
225 /*
226 * Handler: +COPS: <mode>[0],<format>[1],<oper>[2]
227 */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cops)228 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cops)
229 {
230 if (argc >= 1) {
231 #if defined(CONFIG_MODEM_CELL_INFO)
232 if (argc >= 3) {
233 modem.context.data_operator = unquoted_atoi(argv[2], 10);
234 LOG_INF("operator: %u",
235 modem.context.data_operator);
236 }
237 #endif
238 if (unquoted_atoi(argv[0], 10) == 0) {
239 modem.context.is_automatic_oper = true;
240 } else {
241 modem.context.is_automatic_oper = false;
242 }
243 }
244
245 return 0;
246 }
247
248 /*
249 * Provide modem info if modem shell is enabled. This can be shown with
250 * "modem list" shell command.
251 */
252
253 /* Handler: <manufacturer> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_manufacturer)254 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_manufacturer)
255 {
256 size_t out_len;
257
258 out_len = net_buf_linearize(modem.minfo.mdm_manufacturer,
259 sizeof(modem.minfo.mdm_manufacturer) - 1,
260 data->rx_buf, 0, len);
261 modem.minfo.mdm_manufacturer[out_len] = '\0';
262 LOG_INF("Manufacturer: %s", modem.minfo.mdm_manufacturer);
263
264 return 0;
265 }
266
267 /* Handler: <model> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_model)268 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_model)
269 {
270 size_t out_len;
271
272 out_len = net_buf_linearize(modem.minfo.mdm_model,
273 sizeof(modem.minfo.mdm_model) - 1,
274 data->rx_buf, 0, len);
275 modem.minfo.mdm_model[out_len] = '\0';
276 LOG_INF("Model: %s", modem.minfo.mdm_model);
277
278 return 0;
279 }
280
281 /* Handler: <rev> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_revision)282 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_revision)
283 {
284 size_t out_len;
285
286 out_len = net_buf_linearize(modem.minfo.mdm_revision,
287 sizeof(modem.minfo.mdm_revision) - 1,
288 data->rx_buf, 0, len);
289 modem.minfo.mdm_revision[out_len] = '\0';
290 LOG_INF("Revision: %s", modem.minfo.mdm_revision);
291
292 return 0;
293 }
294
295 /* Handler: <IMEI> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imei)296 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imei)
297 {
298 size_t out_len;
299
300 out_len = net_buf_linearize(modem.minfo.mdm_imei, sizeof(modem.minfo.mdm_imei) - 1,
301 data->rx_buf, 0, len);
302 modem.minfo.mdm_imei[out_len] = '\0';
303 LOG_INF("IMEI: %s", modem.minfo.mdm_imei);
304
305 return 0;
306 }
307
308 #if defined(CONFIG_MODEM_SIM_NUMBERS)
309 /* Handler: <IMSI> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imsi)310 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_imsi)
311 {
312 size_t out_len;
313
314 out_len = net_buf_linearize(modem.minfo.mdm_imsi, sizeof(modem.minfo.mdm_imsi) - 1,
315 data->rx_buf, 0, len);
316 modem.minfo.mdm_imsi[out_len] = '\0';
317 LOG_INF("IMSI: %s", modem.minfo.mdm_imsi);
318
319 return 0;
320 }
321
322 /* Handler: <ICCID> */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_iccid)323 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_iccid)
324 {
325 size_t out_len;
326
327 out_len = net_buf_linearize(modem.minfo.mdm_iccid, sizeof(modem.minfo.mdm_iccid) - 1,
328 data->rx_buf, 0, len);
329 modem.minfo.mdm_iccid[out_len] = '\0';
330 if (modem.minfo.mdm_iccid[0] == '+') {
331 /* Seen on U-blox SARA: "+CCID: nnnnnnnnnnnnnnnnnnnn".
332 * Skip over the +CCID bit, which other modems omit.
333 */
334 char *p = strchr(modem.minfo.mdm_iccid, ' ');
335
336 if (p) {
337 size_t iccid_len = strlen(p+1);
338
339 (void)memmove(modem.minfo.mdm_iccid, p+1, iccid_len+1);
340 }
341 }
342 LOG_INF("ICCID: %s", modem.minfo.mdm_iccid);
343
344 return 0;
345 }
346 #endif /* CONFIG_MODEM_SIM_NUMBERS */
347
MODEM_CMD_DEFINE(on_cmd_net_reg_sts)348 MODEM_CMD_DEFINE(on_cmd_net_reg_sts)
349 {
350 modem.net_state = (enum network_state)atoi(argv[1]);
351
352 switch (modem.net_state) {
353 case GSM_NET_NOT_REGISTERED:
354 LOG_DBG("Network %s.", "not registered");
355 break;
356 case GSM_NET_HOME_NETWORK:
357 LOG_DBG("Network %s.", "registered, home network");
358 break;
359 case GSM_NET_SEARCHING:
360 LOG_DBG("Searching for network...");
361 break;
362 case GSM_NET_REGISTRATION_DENIED:
363 LOG_DBG("Network %s.", "registration denied");
364 break;
365 case GSM_NET_UNKNOWN:
366 LOG_DBG("Network %s.", "unknown");
367 break;
368 case GSM_NET_ROAMING:
369 LOG_DBG("Network %s.", "registered, roaming");
370 break;
371 default:
372 break;
373 }
374
375 return 0;
376 }
377
378 #if defined(CONFIG_MODEM_CELL_INFO)
379
380 /*
381 * Handler: +CEREG: <n>[0],<stat>[1],<tac>[2],<ci>[3],<AcT>[4]
382 */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cereg)383 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_cereg)
384 {
385 if (argc >= 4) {
386 modem.context.data_lac = unquoted_atoi(argv[2], 16);
387 modem.context.data_cellid = unquoted_atoi(argv[3], 16);
388 LOG_INF("lac: %u, cellid: %u",
389 modem.context.data_lac,
390 modem.context.data_cellid);
391 }
392
393 if (argc >= 5) {
394 modem.context.data_act = unquoted_atoi(argv[4], 10);
395 LOG_INF("act: %u", modem.context.data_act);
396 }
397
398 return 0;
399 }
400
401 static const struct setup_cmd query_cellinfo_cmds[] = {
402 SETUP_CMD_NOHANDLE("AT+CEREG=2"),
403 SETUP_CMD("AT+CEREG?", "", on_cmd_atcmdinfo_cereg, 5U, ","),
404 SETUP_CMD_NOHANDLE("AT+COPS=3,2"),
405 SETUP_CMD("AT+COPS?", "", on_cmd_atcmdinfo_cops, 3U, ","),
406 };
407
gsm_query_cellinfo(struct gsm_modem * gsm)408 static int gsm_query_cellinfo(struct gsm_modem *gsm)
409 {
410 int ret;
411
412 ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
413 &gsm->context.cmd_handler,
414 query_cellinfo_cmds,
415 ARRAY_SIZE(query_cellinfo_cmds),
416 &gsm->sem_response,
417 GSM_CMD_SETUP_TIMEOUT);
418 if (ret < 0) {
419 LOG_WRN("modem query for cell info returned %d", ret);
420 }
421
422 return ret;
423 }
424 #endif /* CONFIG_MODEM_CELL_INFO */
425
426 #if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
427 /*
428 * Handler: +CESQ: <rxlev>[0],<ber>[1],<rscp>[2],<ecn0>[3],<rsrq>[4],<rsrp>[5]
429 */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_cesq)430 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_cesq)
431 {
432 int rsrp, rscp, rxlev;
433
434 rsrp = ATOI(argv[5], 0, "rsrp");
435 rscp = ATOI(argv[2], 0, "rscp");
436 rxlev = ATOI(argv[0], 0, "rxlev");
437
438 if ((rsrp >= 0) && (rsrp <= 97)) {
439 modem.minfo.mdm_rssi = -140 + (rsrp - 1);
440 LOG_DBG("RSRP: %d", modem.minfo.mdm_rssi);
441 } else if ((rscp >= 0) && (rscp <= 96)) {
442 modem.minfo.mdm_rssi = -120 + (rscp - 1);
443 LOG_DBG("RSCP: %d", modem.minfo.mdm_rssi);
444 } else if ((rxlev >= 0) && (rxlev <= 63)) {
445 modem.minfo.mdm_rssi = -110 + (rxlev - 1);
446 LOG_DBG("RSSI: %d", modem.minfo.mdm_rssi);
447 } else {
448 modem.minfo.mdm_rssi = GSM_RSSI_INVALID;
449 LOG_DBG("RSRP/RSCP/RSSI not known");
450 }
451
452 return 0;
453 }
454 #else
455 /* Handler: +CSQ: <signal_power>[0],<qual>[1] */
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_csq)456 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_rssi_csq)
457 {
458 /* Expected response is "+CSQ: <signal_power>,<qual>" */
459 if (argc > 0) {
460 int rssi = atoi(argv[0]);
461
462 if ((rssi >= 0) && (rssi <= 31)) {
463 rssi = -113 + (rssi * 2);
464 } else {
465 rssi = GSM_RSSI_INVALID;
466 }
467
468 modem.minfo.mdm_rssi = rssi;
469 LOG_DBG("RSSI: %d", rssi);
470 }
471
472 return 0;
473 }
474 #endif
475
476 #if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
477 static const struct modem_cmd read_rssi_cmd =
478 MODEM_CMD("+CESQ:", on_cmd_atcmdinfo_rssi_cesq, 6U, ",");
479 #else
480 static const struct modem_cmd read_rssi_cmd =
481 MODEM_CMD("+CSQ:", on_cmd_atcmdinfo_rssi_csq, 2U, ",");
482 #endif
483
484 static const struct setup_cmd setup_modem_info_cmds[] = {
485 /* query modem info */
486 SETUP_CMD("AT+CGMI", "", on_cmd_atcmdinfo_manufacturer, 0U, ""),
487 SETUP_CMD("AT+CGMM", "", on_cmd_atcmdinfo_model, 0U, ""),
488 SETUP_CMD("AT+CGMR", "", on_cmd_atcmdinfo_revision, 0U, ""),
489 SETUP_CMD("AT+CGSN", "", on_cmd_atcmdinfo_imei, 0U, ""),
490 #if defined(CONFIG_MODEM_SIM_NUMBERS)
491 SETUP_CMD("AT+CIMI", "", on_cmd_atcmdinfo_imsi, 0U, ""),
492 SETUP_CMD("AT+CCID", "", on_cmd_atcmdinfo_iccid, 0U, ""),
493 #endif
494 };
495
496 static const struct setup_cmd setup_cmds[] = {
497 /* no echo */
498 SETUP_CMD_NOHANDLE("ATE0"),
499 /* hang up */
500 SETUP_CMD_NOHANDLE("ATH"),
501 /* extended errors in numeric form */
502 SETUP_CMD_NOHANDLE("AT+CMEE=1"),
503 /* disable unsolicited network registration codes */
504 SETUP_CMD_NOHANDLE("AT+CREG=0"),
505 /* create PDP context */
506 SETUP_CMD_NOHANDLE("AT+CGDCONT=1,\"IP\",\"" CONFIG_MODEM_GSM_APN "\""),
507 #if IS_ENABLED(DT_PROP(GSM_UART_NODE, hw_flow_control))
508 /* enable hardware flow control */
509 SETUP_CMD_NOHANDLE("AT+IFC=2,2"),
510 #endif
511 };
512
MODEM_CMD_DEFINE(on_cmd_atcmdinfo_attached)513 MODEM_CMD_DEFINE(on_cmd_atcmdinfo_attached)
514 {
515 /* Expected response is "+CGATT: 0|1" so simply look for '1' */
516 if ((argc > 0) && (atoi(argv[0]) == 1)) {
517 LOG_INF("Attached to packet service!");
518 }
519
520 return 0;
521 }
522
523
524 static const struct modem_cmd read_cops_cmd =
525 MODEM_CMD_ARGS_MAX("+COPS:", on_cmd_atcmdinfo_cops, 1U, 4U, ",");
526
527 static const struct modem_cmd check_net_reg_cmd =
528 MODEM_CMD("+CREG: ", on_cmd_net_reg_sts, 2U, ",");
529
530 static const struct modem_cmd check_attached_cmd =
531 MODEM_CMD("+CGATT:", on_cmd_atcmdinfo_attached, 1U, ",");
532
533 static const struct setup_cmd connect_cmds[] = {
534 /* connect to network */
535 SETUP_CMD_NOHANDLE("ATD*99#"),
536 };
537
gsm_query_modem_info(struct gsm_modem * gsm)538 static int gsm_query_modem_info(struct gsm_modem *gsm)
539 {
540 int ret;
541
542 if (gsm->modem_info_queried) {
543 return 0;
544 }
545
546 ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
547 &gsm->context.cmd_handler,
548 setup_modem_info_cmds,
549 ARRAY_SIZE(setup_modem_info_cmds),
550 &gsm->sem_response,
551 GSM_CMD_SETUP_TIMEOUT);
552
553 if (ret < 0) {
554 return ret;
555 }
556
557 gsm->modem_info_queried = true;
558
559 return 0;
560 }
561
gsm_setup_mccmno(struct gsm_modem * gsm)562 static int gsm_setup_mccmno(struct gsm_modem *gsm)
563 {
564 int ret = 0;
565
566 if (CONFIG_MODEM_GSM_MANUAL_MCCMNO[0] != '\0') {
567 /* use manual MCC/MNO entry */
568 ret = modem_cmd_send_nolock(&gsm->context.iface,
569 &gsm->context.cmd_handler,
570 NULL, 0,
571 "AT+COPS=1,2,\""
572 CONFIG_MODEM_GSM_MANUAL_MCCMNO
573 "\"",
574 &gsm->sem_response,
575 GSM_CMD_AT_TIMEOUT);
576 } else {
577
578 /* First AT+COPS? is sent to check if automatic selection for operator
579 * is already enabled, if yes we do not send the command AT+COPS= 0,0.
580 */
581
582 ret = modem_cmd_send_nolock(&gsm->context.iface,
583 &gsm->context.cmd_handler,
584 &read_cops_cmd,
585 1, "AT+COPS?",
586 &gsm->sem_response,
587 GSM_CMD_SETUP_TIMEOUT);
588
589 if (ret < 0) {
590 return ret;
591 }
592
593 if (!gsm->context.is_automatic_oper) {
594 /* register operator automatically */
595 ret = modem_cmd_send_nolock(&gsm->context.iface,
596 &gsm->context.cmd_handler,
597 NULL, 0, "AT+COPS=0,0",
598 &gsm->sem_response,
599 GSM_CMD_AT_TIMEOUT);
600 }
601 }
602
603 if (ret < 0) {
604 LOG_ERR("AT+COPS ret:%d", ret);
605 }
606
607 return ret;
608 }
609
ppp_net_if(void)610 static struct net_if *ppp_net_if(void)
611 {
612 return net_if_get_first_by_type(&NET_L2_GET_NAME(PPP));
613 }
614
set_ppp_carrier_on(struct gsm_modem * gsm)615 static void set_ppp_carrier_on(struct gsm_modem *gsm)
616 {
617 const struct device *ppp_dev = device_get_binding(CONFIG_NET_PPP_DRV_NAME);
618 struct net_if *iface = gsm->iface;
619
620 if (ppp_dev == NULL) {
621 LOG_ERR("Cannot find PPP %s!", CONFIG_NET_PPP_DRV_NAME);
622 return;
623 }
624
625 net_if_up(iface);
626 }
627
query_rssi(struct gsm_modem * gsm,bool lock)628 static void query_rssi(struct gsm_modem *gsm, bool lock)
629 {
630 int ret;
631
632 #if defined(CONFIG_MODEM_GSM_ENABLE_CESQ_RSSI)
633 ret = modem_cmd_send_ext(&gsm->context.iface, &gsm->context.cmd_handler, &read_rssi_cmd, 1,
634 "AT+CESQ", &gsm->sem_response, GSM_CMD_SETUP_TIMEOUT,
635 lock ? 0 : MODEM_NO_TX_LOCK);
636 #else
637 ret = modem_cmd_send_ext(&gsm->context.iface, &gsm->context.cmd_handler, &read_rssi_cmd, 1,
638 "AT+CSQ", &gsm->sem_response, GSM_CMD_SETUP_TIMEOUT,
639 lock ? 0 : MODEM_NO_TX_LOCK);
640 #endif
641
642 if (ret < 0) {
643 LOG_DBG("No answer to RSSI readout, %s", "ignoring...");
644 }
645 }
646
query_rssi_lock(struct gsm_modem * gsm)647 static inline void query_rssi_lock(struct gsm_modem *gsm)
648 {
649 query_rssi(gsm, true);
650 }
651
query_rssi_nolock(struct gsm_modem * gsm)652 static inline void query_rssi_nolock(struct gsm_modem *gsm)
653 {
654 query_rssi(gsm, false);
655 }
656
rssi_handler(struct k_work * work)657 static void rssi_handler(struct k_work *work)
658 {
659 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
660 struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem, rssi_work_handle);
661
662 gsm_ppp_lock(gsm);
663 query_rssi_lock(gsm);
664
665 #if defined(CONFIG_MODEM_CELL_INFO)
666 (void)gsm_query_cellinfo(gsm);
667 #endif
668 (void)gsm_work_reschedule(&gsm->rssi_work_handle,
669 K_SECONDS(CONFIG_MODEM_GSM_RSSI_POLLING_PERIOD));
670 gsm_ppp_unlock(gsm);
671 }
672
gsm_finalize_connection(struct k_work * work)673 static void gsm_finalize_connection(struct k_work *work)
674 {
675 int ret = 0;
676 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
677 struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem, gsm_configure_work);
678
679 gsm_ppp_lock(gsm);
680
681 /* If already attached, jump right to RSSI readout */
682 if (gsm->state == GSM_PPP_ATTACHED) {
683 goto attached;
684 }
685
686 /* If attach check failed, we should not redo every setup step */
687 if (gsm->state == GSM_PPP_ATTACHING) {
688 goto attaching;
689 }
690
691 /* If modem is searching for network, we should skip the setup step */
692 if (gsm->state == GSM_PPP_REGISTERING) {
693 goto registering;
694 }
695
696 if (IS_ENABLED(CONFIG_GSM_MUX)) {
697 ret = modem_cmd_send_nolock(&gsm->context.iface,
698 &gsm->context.cmd_handler,
699 &response_cmds[0],
700 ARRAY_SIZE(response_cmds),
701 "AT", &gsm->sem_response,
702 GSM_CMD_AT_TIMEOUT);
703 if (ret < 0) {
704 LOG_ERR("%s returned %d, %s", "AT", ret, "retrying...");
705 (void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
706 goto unlock;
707 }
708 }
709 gsm->state = GSM_PPP_SETUP;
710
711 if (IS_ENABLED(CONFIG_MODEM_GSM_FACTORY_RESET_AT_BOOT)) {
712 (void)modem_cmd_send_nolock(&gsm->context.iface,
713 &gsm->context.cmd_handler,
714 &response_cmds[0],
715 ARRAY_SIZE(response_cmds),
716 "AT&F", &gsm->sem_response,
717 GSM_CMD_AT_TIMEOUT);
718 (void)k_sleep(K_SECONDS(1));
719 }
720
721 ret = gsm_setup_mccmno(gsm);
722 if (ret < 0) {
723 LOG_ERR("%s returned %d, %s", "gsm_setup_mccmno", ret, "retrying...");
724
725 (void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
726 goto unlock;
727 }
728
729 ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
730 &gsm->context.cmd_handler,
731 setup_cmds,
732 ARRAY_SIZE(setup_cmds),
733 &gsm->sem_response,
734 GSM_CMD_SETUP_TIMEOUT);
735 if (ret < 0) {
736 LOG_DBG("%s returned %d, %s", "setup_cmds", ret, "retrying...");
737 (void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
738 goto unlock;
739 }
740
741 ret = gsm_query_modem_info(gsm);
742 if (ret < 0) {
743 LOG_DBG("Unable to query modem information %d", ret);
744 (void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
745 goto unlock;
746 }
747
748 gsm->state = GSM_PPP_REGISTERING;
749 registering:
750 /* Wait for cell tower registration */
751 ret = modem_cmd_send_nolock(&gsm->context.iface,
752 &gsm->context.cmd_handler,
753 &check_net_reg_cmd, 1,
754 "AT+CREG?",
755 &gsm->sem_response,
756 GSM_CMD_SETUP_TIMEOUT);
757 if ((ret < 0) || ((gsm->net_state != GSM_NET_ROAMING) &&
758 (gsm->net_state != GSM_NET_HOME_NETWORK))) {
759 if (gsm->retries == 0) {
760 gsm->retries = CONFIG_MODEM_GSM_REGISTER_TIMEOUT *
761 (MSEC_PER_SEC / GSM_REGISTER_DELAY_MSEC);
762 } else {
763 gsm->retries--;
764 }
765
766 (void)gsm_work_reschedule(&gsm->gsm_configure_work,
767 K_MSEC(GSM_REGISTER_DELAY_MSEC));
768 goto unlock;
769 }
770
771 gsm->retries = 0;
772 gsm->state = GSM_PPP_ATTACHING;
773 attaching:
774 /* Don't initialize PPP until we're attached to packet service */
775 ret = modem_cmd_send_nolock(&gsm->context.iface,
776 &gsm->context.cmd_handler,
777 &check_attached_cmd, 1,
778 "AT+CGATT?",
779 &gsm->sem_response,
780 GSM_CMD_SETUP_TIMEOUT);
781 if (ret < 0) {
782 /*
783 * retries not set -> trigger N attach retries
784 * retries set -> decrement and retry
785 * retries set, becomes 0 -> trigger full retry
786 */
787 if (gsm->retries == 0) {
788 gsm->retries = CONFIG_MODEM_GSM_ATTACH_TIMEOUT *
789 (MSEC_PER_SEC / GSM_ATTACH_RETRY_DELAY_MSEC);
790 } else {
791 gsm->retries--;
792 }
793
794 LOG_DBG("Not attached, %s", "retrying...");
795
796 (void)gsm_work_reschedule(&gsm->gsm_configure_work,
797 K_MSEC(GSM_ATTACH_RETRY_DELAY_MSEC));
798 goto unlock;
799 }
800
801 /* Attached, clear retry counter */
802 LOG_DBG("modem attach returned %d, %s", ret, "read RSSI");
803 gsm->state = GSM_PPP_ATTACHED;
804 gsm->retries = GSM_RSSI_RETRIES;
805
806 attached:
807
808 if (!IS_ENABLED(CONFIG_GSM_MUX)) {
809 /* Read connection quality (RSSI) before PPP carrier is ON */
810 query_rssi_nolock(gsm);
811
812 if (!((gsm->minfo.mdm_rssi) && (gsm->minfo.mdm_rssi != GSM_RSSI_INVALID) &&
813 (gsm->minfo.mdm_rssi < GSM_RSSI_MAXVAL))) {
814
815 LOG_DBG("Not valid RSSI, %s", "retrying...");
816 if (gsm->retries-- > 0) {
817 (void)gsm_work_reschedule(&gsm->gsm_configure_work,
818 K_MSEC(GSM_RSSI_RETRY_DELAY_MSEC));
819 goto unlock;
820 }
821 }
822 #if defined(CONFIG_MODEM_CELL_INFO)
823 (void)gsm_query_cellinfo(gsm);
824 #endif
825 }
826
827 LOG_DBG("modem RSSI: %d, %s", gsm->minfo.mdm_rssi, "enable PPP");
828
829 ret = modem_cmd_handler_setup_cmds_nolock(&gsm->context.iface,
830 &gsm->context.cmd_handler,
831 connect_cmds,
832 ARRAY_SIZE(connect_cmds),
833 &gsm->sem_response,
834 GSM_CMD_SETUP_TIMEOUT);
835 if (ret < 0) {
836 LOG_DBG("%s returned %d, %s", "connect_cmds", ret, "retrying...");
837 (void)gsm_work_reschedule(&gsm->gsm_configure_work, GSM_RETRY_DELAY);
838 goto unlock;
839 }
840
841 gsm->state = GSM_PPP_SETUP_DONE;
842 set_ppp_carrier_on(gsm);
843
844 if (IS_ENABLED(CONFIG_GSM_MUX)) {
845 /* Re-use the original iface for AT channel */
846 ret = modem_iface_uart_init_dev(&gsm->context.iface,
847 gsm->at_dev);
848 if (ret < 0) {
849 LOG_DBG("iface %suart error %d", "AT ", ret);
850 gsm->state = GSM_PPP_STATE_ERROR;
851 } else {
852 /* Do a test and try to send AT command to modem */
853 ret = modem_cmd_send_nolock(
854 &gsm->context.iface,
855 &gsm->context.cmd_handler,
856 &response_cmds[0],
857 ARRAY_SIZE(response_cmds),
858 "AT", &gsm->sem_response,
859 GSM_CMD_AT_TIMEOUT);
860 if (ret < 0) {
861 LOG_WRN("%s returned %d, %s", "AT", ret, "iface failed");
862 gsm->state = GSM_PPP_STATE_ERROR;
863 } else {
864 LOG_INF("AT channel %d connected to %s",
865 DLCI_AT, gsm->at_dev->name);
866 }
867 }
868
869 modem_cmd_handler_tx_unlock(&gsm->context.cmd_handler);
870 if (gsm->state != GSM_PPP_STATE_ERROR) {
871 (void)gsm_work_reschedule(&gsm->rssi_work_handle,
872 K_SECONDS(CONFIG_MODEM_GSM_RSSI_POLLING_PERIOD));
873 }
874 }
875
876 unlock:
877 gsm_ppp_unlock(gsm);
878 }
879
mux_enable(struct gsm_modem * gsm)880 static int mux_enable(struct gsm_modem *gsm)
881 {
882 int ret;
883
884 /* Turn on muxing */
885 if (IS_ENABLED(CONFIG_MODEM_GSM_SIMCOM)) {
886 ret = modem_cmd_send_nolock(
887 &gsm->context.iface,
888 &gsm->context.cmd_handler,
889 &response_cmds[0],
890 ARRAY_SIZE(response_cmds),
891 #if defined(SIMCOM_LTE)
892 /* FIXME */
893 /* Some SIMCOM modems can set the channels */
894 /* Control channel always at DLCI 0 */
895 "AT+CMUXSRVPORT=0,0;"
896 /* PPP should be at DLCI 1 */
897 "+CMUXSRVPORT=" STRINGIFY(DLCI_PPP) ",1;"
898 /* AT should be at DLCI 2 */
899 "+CMUXSRVPORT=" STRINGIFY(DLCI_AT) ",1;"
900 #else
901 "AT"
902 #endif
903 "+CMUX=0,0,5,"
904 STRINGIFY(CONFIG_GSM_MUX_MRU_DEFAULT_LEN),
905 &gsm->sem_response,
906 GSM_CMD_AT_TIMEOUT);
907 } else if (IS_ENABLED(CONFIG_MODEM_GSM_QUECTEL)) {
908 ret = modem_cmd_send_nolock(&gsm->context.iface,
909 &gsm->context.cmd_handler,
910 &response_cmds[0],
911 ARRAY_SIZE(response_cmds),
912 "AT+CMUX=0,0,5,"
913 STRINGIFY(CONFIG_GSM_MUX_MRU_DEFAULT_LEN),
914 &gsm->sem_response,
915 GSM_CMD_AT_TIMEOUT);
916
917 /* Arbitrary delay for Quectel modems to initialize the CMUX,
918 * without this the AT cmd will fail.
919 */
920 (void)k_sleep(K_SECONDS(1));
921 } else {
922 /* Generic GSM modem */
923 ret = modem_cmd_send_nolock(&gsm->context.iface,
924 &gsm->context.cmd_handler,
925 &response_cmds[0],
926 ARRAY_SIZE(response_cmds),
927 "AT+CMUX=0", &gsm->sem_response,
928 GSM_CMD_AT_TIMEOUT);
929 }
930
931 if (ret < 0) {
932 LOG_ERR("AT+CMUX ret:%d", ret);
933 }
934
935 return ret;
936 }
937
mux_setup_next(struct gsm_modem * gsm)938 static void mux_setup_next(struct gsm_modem *gsm)
939 {
940 (void)gsm_work_reschedule(&gsm->gsm_configure_work, K_MSEC(1));
941 }
942
mux_attach_cb(const struct device * mux,int dlci_address,bool connected,void * user_data)943 static void mux_attach_cb(const struct device *mux, int dlci_address,
944 bool connected, void *user_data)
945 {
946 LOG_DBG("DLCI %d to %s %s", dlci_address, mux->name,
947 connected ? "connected" : "disconnected");
948
949 if (connected) {
950 uart_irq_rx_enable(mux);
951 uart_irq_tx_enable(mux);
952 }
953
954 mux_setup_next(user_data);
955 }
956
mux_attach(const struct device * mux,const struct device * uart,int dlci_address,void * user_data)957 static int mux_attach(const struct device *mux, const struct device *uart,
958 int dlci_address, void *user_data)
959 {
960 int ret = uart_mux_attach(mux, uart, dlci_address, mux_attach_cb,
961 user_data);
962 if (ret < 0) {
963 LOG_ERR("Cannot attach DLCI %d (%s) to %s (%d)", dlci_address,
964 mux->name, uart->name, ret);
965 return ret;
966 }
967
968 return 0;
969 }
970
mux_setup(struct k_work * work)971 static void mux_setup(struct k_work *work)
972 {
973 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
974 struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem,
975 gsm_configure_work);
976 const struct device *const uart = DEVICE_DT_GET(GSM_UART_NODE);
977 int ret;
978
979 gsm_ppp_lock(gsm);
980
981 switch (gsm->state) {
982 case GSM_PPP_STATE_CONTROL_CHANNEL:
983 /* We need to call this to reactivate mux ISR. Note: This is only called
984 * after re-initing gsm_ppp.
985 */
986 if (gsm->ppp_dev != NULL) {
987 uart_mux_enable(gsm->ppp_dev);
988 }
989
990 /* Get UART device. There is one dev / DLCI */
991 if (gsm->control_dev == NULL) {
992 gsm->control_dev = uart_mux_alloc();
993 if (gsm->control_dev == NULL) {
994 LOG_DBG("Cannot get UART mux for %s channel",
995 "control");
996 goto fail;
997 }
998 }
999
1000 ret = mux_attach(gsm->control_dev, uart, DLCI_CONTROL, gsm);
1001 if (ret < 0) {
1002 goto fail;
1003 }
1004
1005 gsm->state = GSM_PPP_STATE_PPP_CHANNEL;
1006 goto unlock;
1007
1008 case GSM_PPP_STATE_PPP_CHANNEL:
1009 if (gsm->ppp_dev == NULL) {
1010 gsm->ppp_dev = uart_mux_alloc();
1011 if (gsm->ppp_dev == NULL) {
1012 LOG_DBG("Cannot get UART mux for %s channel",
1013 "PPP");
1014 goto fail;
1015 }
1016 }
1017
1018 ret = mux_attach(gsm->ppp_dev, uart, DLCI_PPP, gsm);
1019 if (ret < 0) {
1020 goto fail;
1021 }
1022
1023 gsm->state = GSM_PPP_STATE_AT_CHANNEL;
1024 goto unlock;
1025
1026 case GSM_PPP_STATE_AT_CHANNEL:
1027 if (gsm->at_dev == NULL) {
1028 gsm->at_dev = uart_mux_alloc();
1029 if (gsm->at_dev == NULL) {
1030 LOG_DBG("Cannot get UART mux for %s channel",
1031 "AT");
1032 goto fail;
1033 }
1034 }
1035
1036 ret = mux_attach(gsm->at_dev, uart, DLCI_AT, gsm);
1037 if (ret < 0) {
1038 goto fail;
1039 }
1040
1041 gsm->state = GSM_PPP_STATE_DONE;
1042 goto unlock;
1043
1044 case GSM_PPP_STATE_DONE:
1045 /* At least the SIMCOM modem expects that the Internet
1046 * connection is created in PPP channel. We will need
1047 * to attach the AT channel to context iface after the
1048 * PPP connection is established in order to give AT commands
1049 * to the modem.
1050 */
1051 ret = modem_iface_uart_init_dev(&gsm->context.iface,
1052 gsm->ppp_dev);
1053 if (ret < 0) {
1054 LOG_DBG("iface %suart error %d", "PPP ", ret);
1055 goto fail;
1056 }
1057
1058 LOG_INF("PPP channel %d connected to %s",
1059 DLCI_PPP, gsm->ppp_dev->name);
1060
1061 k_work_init_delayable(&gsm->gsm_configure_work, gsm_finalize_connection);
1062 (void)gsm_work_reschedule(&gsm->gsm_configure_work, K_NO_WAIT);
1063 goto unlock;
1064 default:
1065 __ASSERT(0, "%s while in state: %d", "mux_setup", gsm->state);
1066 /* In case CONFIG_ASSERT is off, goto fail */
1067 goto fail;
1068 }
1069
1070 fail:
1071 gsm->state = GSM_PPP_STATE_ERROR;
1072 unlock:
1073 gsm_ppp_unlock(gsm);
1074 }
1075
gsm_configure(struct k_work * work)1076 static void gsm_configure(struct k_work *work)
1077 {
1078 struct k_work_delayable *dwork = k_work_delayable_from_work(work);
1079 struct gsm_modem *gsm = CONTAINER_OF(dwork, struct gsm_modem,
1080 gsm_configure_work);
1081 int ret = -1;
1082
1083 gsm_ppp_lock(gsm);
1084
1085 if (gsm->state == GSM_PPP_WAIT_AT) {
1086 goto wait_at;
1087 }
1088
1089 if (gsm->state == GSM_PPP_START) {
1090 LOG_DBG("Starting modem %p configuration", gsm);
1091
1092 if (gsm->modem_on_cb != NULL) {
1093 gsm->modem_on_cb(gsm->dev, gsm->user_data);
1094 }
1095
1096 gsm->state = GSM_PPP_WAIT_AT;
1097 }
1098
1099 wait_at:
1100 ret = modem_cmd_send_nolock(&gsm->context.iface,
1101 &gsm->context.cmd_handler,
1102 &response_cmds[0],
1103 ARRAY_SIZE(response_cmds),
1104 "AT", &gsm->sem_response,
1105 GSM_CMD_AT_TIMEOUT);
1106 if (ret < 0) {
1107 LOG_DBG("modem not ready %d", ret);
1108 goto retry;
1109 }
1110
1111 gsm->state = GSM_PPP_AT_RDY;
1112
1113 if (IS_ENABLED(CONFIG_GSM_MUX)) {
1114 if (mux_enable(gsm) == 0) {
1115 LOG_DBG("GSM muxing %s", "enabled");
1116 } else {
1117 LOG_DBG("GSM muxing %s", "disabled");
1118 goto retry;
1119 }
1120
1121 gsm->state = GSM_PPP_STATE_INIT;
1122
1123 k_work_init_delayable(&gsm->gsm_configure_work, mux_setup);
1124 } else {
1125 k_work_init_delayable(&gsm->gsm_configure_work, gsm_finalize_connection);
1126 }
1127
1128 retry:
1129 (void)gsm_work_reschedule(&gsm->gsm_configure_work, K_NO_WAIT);
1130 gsm_ppp_unlock(gsm);
1131 }
1132
gsm_ppp_start(const struct device * dev)1133 void gsm_ppp_start(const struct device *dev)
1134 {
1135 int ret;
1136 struct gsm_modem *gsm = dev->data;
1137
1138 gsm_ppp_lock(gsm);
1139
1140 if (gsm->state != GSM_PPP_STOP) {
1141 LOG_ERR("gsm_ppp is already %s", "started");
1142 goto unlock;
1143 }
1144
1145 gsm->state = GSM_PPP_START;
1146
1147 /* Re-init underlying UART comms */
1148 ret = modem_iface_uart_init_dev(&gsm->context.iface, DEVICE_DT_GET(GSM_UART_NODE));
1149 if (ret < 0) {
1150 LOG_ERR("modem_iface_uart_init returned %d", ret);
1151 gsm->state = GSM_PPP_STATE_ERROR;
1152 goto unlock;
1153 }
1154
1155 k_work_init_delayable(&gsm->gsm_configure_work, gsm_configure);
1156 (void)gsm_work_reschedule(&gsm->gsm_configure_work, K_NO_WAIT);
1157
1158 unlock:
1159 gsm_ppp_unlock(gsm);
1160 }
1161
gsm_ppp_stop(const struct device * dev)1162 void gsm_ppp_stop(const struct device *dev)
1163 {
1164 struct gsm_modem *gsm = dev->data;
1165 struct net_if *iface = gsm->iface;
1166 struct k_work_sync work_sync;
1167
1168 if (gsm->state == GSM_PPP_STOP) {
1169 LOG_ERR("gsm_ppp is already %s", "stopped");
1170 return;
1171 }
1172
1173 (void)k_work_cancel_delayable_sync(&gsm->gsm_configure_work, &work_sync);
1174 if (IS_ENABLED(CONFIG_GSM_MUX)) {
1175 (void)k_work_cancel_delayable_sync(&gsm->rssi_work_handle, &work_sync);
1176 }
1177
1178 gsm_ppp_lock(gsm);
1179
1180 /* wait for the interface to be properly down */
1181 if (net_if_is_up(iface)) {
1182 net_if_down(ppp_net_if());
1183 (void)k_sem_take(&gsm->sem_if_down, K_FOREVER);
1184 }
1185
1186 if (IS_ENABLED(CONFIG_GSM_MUX)) {
1187 if (gsm->ppp_dev != NULL) {
1188 uart_mux_disable(gsm->ppp_dev);
1189 }
1190
1191 if (modem_cmd_handler_tx_lock(&gsm->context.cmd_handler,
1192 GSM_CMD_LOCK_TIMEOUT) < 0) {
1193 LOG_WRN("Failed locking modem cmds!");
1194 }
1195 }
1196
1197 if (gsm->modem_off_cb != NULL) {
1198 gsm->modem_off_cb(gsm->dev, gsm->user_data);
1199 }
1200
1201 gsm->state = GSM_PPP_STOP;
1202 gsm->net_state = GSM_NET_INIT;
1203 gsm_ppp_unlock(gsm);
1204 }
1205
gsm_ppp_register_modem_power_callback(const struct device * dev,gsm_modem_power_cb modem_on,gsm_modem_power_cb modem_off,void * user_data)1206 void gsm_ppp_register_modem_power_callback(const struct device *dev,
1207 gsm_modem_power_cb modem_on,
1208 gsm_modem_power_cb modem_off,
1209 void *user_data)
1210 {
1211 struct gsm_modem *gsm = dev->data;
1212
1213 gsm_ppp_lock(gsm);
1214
1215 gsm->modem_on_cb = modem_on;
1216 gsm->modem_off_cb = modem_off;
1217
1218 gsm->user_data = user_data;
1219 gsm_ppp_unlock(gsm);
1220 }
1221
gsm_ppp_modem_info(const struct device * dev)1222 const struct gsm_ppp_modem_info *gsm_ppp_modem_info(const struct device *dev)
1223 {
1224 struct gsm_modem *gsm = dev->data;
1225
1226 return &gsm->minfo;
1227 }
1228
gsm_mgmt_event_handler(struct net_mgmt_event_callback * cb,uint32_t mgmt_event,struct net_if * iface)1229 static void gsm_mgmt_event_handler(struct net_mgmt_event_callback *cb,
1230 uint32_t mgmt_event, struct net_if *iface)
1231 {
1232 if ((mgmt_event & NET_EVENT_IF_DOWN) != mgmt_event) {
1233 return;
1234 }
1235
1236 /* Right now we only support 1 GSM instance */
1237 if (iface != modem.iface) {
1238 return;
1239 }
1240
1241 if (mgmt_event == NET_EVENT_IF_DOWN) {
1242 LOG_INF("GSM network interface down");
1243 /* raise semaphore to indicate the interface is down */
1244 k_sem_give(&modem.sem_if_down);
1245 return;
1246 }
1247 }
1248
gsm_init(const struct device * dev)1249 static int gsm_init(const struct device *dev)
1250 {
1251 struct gsm_modem *gsm = dev->data;
1252 int ret;
1253
1254 LOG_DBG("Generic GSM modem (%p)", gsm);
1255
1256 (void)k_mutex_init(&gsm->lock);
1257 gsm->dev = dev;
1258
1259 const struct modem_cmd_handler_config cmd_handler_config = {
1260 .match_buf = &gsm->cmd_match_buf[0],
1261 .match_buf_len = sizeof(gsm->cmd_match_buf),
1262 .buf_pool = &gsm_recv_pool,
1263 .alloc_timeout = K_NO_WAIT,
1264 .eol = "\r",
1265 .user_data = NULL,
1266 .response_cmds = response_cmds,
1267 .response_cmds_len = ARRAY_SIZE(response_cmds),
1268 .unsol_cmds = NULL,
1269 .unsol_cmds_len = 0,
1270 };
1271
1272 (void)k_sem_init(&gsm->sem_response, 0, 1);
1273 (void)k_sem_init(&gsm->sem_if_down, 0, 1);
1274
1275 ret = modem_cmd_handler_init(&gsm->context.cmd_handler, &gsm->cmd_handler_data,
1276 &cmd_handler_config);
1277 if (ret < 0) {
1278 LOG_DBG("cmd handler error %d", ret);
1279 return ret;
1280 }
1281
1282 #if defined(CONFIG_MODEM_SHELL)
1283 /* modem information storage */
1284 gsm->context.data_manufacturer = gsm->minfo.mdm_manufacturer;
1285 gsm->context.data_model = gsm->minfo.mdm_model;
1286 gsm->context.data_revision = gsm->minfo.mdm_revision;
1287 gsm->context.data_imei = gsm->minfo.mdm_imei;
1288 #if defined(CONFIG_MODEM_SIM_NUMBERS)
1289 gsm->context.data_imsi = gsm->minfo.mdm_imsi;
1290 gsm->context.data_iccid = gsm->minfo.mdm_iccid;
1291 #endif /* CONFIG_MODEM_SIM_NUMBERS */
1292 gsm->context.data_rssi = &gsm->minfo.mdm_rssi;
1293 #endif /* CONFIG_MODEM_SHELL */
1294
1295 gsm->context.is_automatic_oper = false;
1296
1297 const struct modem_iface_uart_config uart_config = {
1298 .rx_rb_buf = &gsm->gsm_rx_rb_buf[0],
1299 .rx_rb_buf_len = sizeof(gsm->gsm_rx_rb_buf),
1300 .hw_flow_control = DT_PROP(GSM_UART_NODE, hw_flow_control),
1301 .dev = DEVICE_DT_GET(GSM_UART_NODE),
1302 };
1303
1304 ret = modem_iface_uart_init(&gsm->context.iface, &gsm->gsm_data, &uart_config);
1305 if (ret < 0) {
1306 LOG_DBG("iface uart error %d", ret);
1307 return ret;
1308 }
1309
1310 ret = modem_context_register(&gsm->context);
1311 if (ret < 0) {
1312 LOG_DBG("context error %d", ret);
1313 return ret;
1314 }
1315
1316 /* Initialize to stop state so that it can be started later */
1317 gsm->state = GSM_PPP_STOP;
1318
1319 gsm->net_state = GSM_NET_INIT;
1320
1321 LOG_DBG("iface->read %p iface->write %p",
1322 gsm->context.iface.read, gsm->context.iface.write);
1323
1324 (void)k_thread_create(&gsm->rx_thread, gsm_rx_stack,
1325 K_KERNEL_STACK_SIZEOF(gsm_rx_stack),
1326 (k_thread_entry_t) gsm_rx,
1327 gsm, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT);
1328 (void)k_thread_name_set(&gsm->rx_thread, "gsm_rx");
1329
1330 /* initialize the work queue */
1331 k_work_queue_init(&gsm->workq);
1332 k_work_queue_start(&gsm->workq, gsm_workq_stack, K_KERNEL_STACK_SIZEOF(gsm_workq_stack),
1333 K_PRIO_COOP(7), NULL);
1334 (void)k_thread_name_set(&gsm->workq.thread, "gsm_workq");
1335
1336 if (IS_ENABLED(CONFIG_GSM_MUX)) {
1337 k_work_init_delayable(&gsm->rssi_work_handle, rssi_handler);
1338 }
1339
1340 gsm->iface = ppp_net_if();
1341 if (gsm->iface == NULL) {
1342 LOG_ERR("Couldn't find ppp net_if!");
1343 return -ENODEV;
1344 }
1345
1346 net_mgmt_init_event_callback(&gsm->gsm_mgmt_cb, gsm_mgmt_event_handler,
1347 NET_EVENT_IF_DOWN);
1348 net_mgmt_add_event_callback(&gsm->gsm_mgmt_cb);
1349
1350 if (IS_ENABLED(CONFIG_GSM_PPP_AUTOSTART)) {
1351 gsm_ppp_start(dev);
1352 }
1353
1354 return 0;
1355 }
1356
1357 DEVICE_DT_DEFINE(DT_DRV_INST(0), gsm_init, NULL, &modem, NULL,
1358 POST_KERNEL, CONFIG_MODEM_GSM_INIT_PRIORITY, NULL);
1359