13#ifndef ZEPHYR_INCLUDE_DRIVERS_CELLULAR_INTERNAL_H_
14#define ZEPHYR_INCLUDE_DRIVERS_CELLULAR_INTERNAL_H_
43#define MODEM_CELLULAR_DATA_IMEI_LEN (16)
44#define MODEM_CELLULAR_DATA_MODEL_ID_LEN (65)
45#define MODEM_CELLULAR_DATA_IMSI_LEN (23)
46#define MODEM_CELLULAR_DATA_ICCID_LEN (22)
47#define MODEM_CELLULAR_DATA_MANUFACTURER_LEN (65)
48#define MODEM_CELLULAR_DATA_FW_VERSION_LEN (65)
49#define MODEM_CELLULAR_DATA_APN_LEN (32)
50#define MODEM_CELLULAR_MAX_APN_CMDS (2)
51#define MODEM_CELLULAR_APN_BUF_SIZE (64)
57enum modem_cellular_state {
58 MODEM_CELLULAR_STATE_IDLE = 0,
59 MODEM_CELLULAR_STATE_RESET_PULSE,
60 MODEM_CELLULAR_STATE_AWAIT_RESET,
61 MODEM_CELLULAR_STATE_POWER_ON_PULSE,
62 MODEM_CELLULAR_STATE_AWAIT_POWER_ON,
63 MODEM_CELLULAR_STATE_SET_BAUDRATE,
64 MODEM_CELLULAR_STATE_RUN_INIT_SCRIPT,
65 MODEM_CELLULAR_STATE_CONNECT_CMUX,
66 MODEM_CELLULAR_STATE_OPEN_DLCI1,
67 MODEM_CELLULAR_STATE_OPEN_DLCI2,
68 MODEM_CELLULAR_STATE_RUN_BOARD_INIT_SCRIPT,
69 MODEM_CELLULAR_STATE_WAIT_FOR_APN,
70 MODEM_CELLULAR_STATE_RUN_APN_SCRIPT,
71 MODEM_CELLULAR_STATE_RUN_NETWORK_SCRIPT,
72 MODEM_CELLULAR_STATE_RUN_DIAL_SCRIPT,
73 MODEM_CELLULAR_STATE_AWAIT_REGISTERED,
74 MODEM_CELLULAR_STATE_REGISTERED,
75 MODEM_CELLULAR_STATE_AWAIT_PPP_DEAD,
76 MODEM_CELLULAR_STATE_INIT_POWER_OFF,
77 MODEM_CELLULAR_STATE_RUN_SHUTDOWN_SCRIPT,
78 MODEM_CELLULAR_STATE_POWER_OFF_PULSE,
79 MODEM_CELLULAR_STATE_AWAIT_POWER_OFF,
82enum modem_cellular_event {
83 MODEM_CELLULAR_EVENT_RESUME = 0,
84 MODEM_CELLULAR_EVENT_SUSPEND,
85 MODEM_CELLULAR_EVENT_SCRIPT_SUCCESS,
86 MODEM_CELLULAR_EVENT_SCRIPT_FAILED,
87 MODEM_CELLULAR_EVENT_CMUX_CONNECTED,
88 MODEM_CELLULAR_EVENT_CMUX_DISCONNECTED,
89 MODEM_CELLULAR_EVENT_DLCI1_OPENED,
90 MODEM_CELLULAR_EVENT_DLCI2_OPENED,
91 MODEM_CELLULAR_EVENT_TIMEOUT,
92 MODEM_CELLULAR_EVENT_REGISTERED,
93 MODEM_CELLULAR_EVENT_DEREGISTERED,
94 MODEM_CELLULAR_EVENT_BUS_OPENED,
95 MODEM_CELLULAR_EVENT_BUS_CLOSED,
96 MODEM_CELLULAR_EVENT_PPP_DEAD,
97 MODEM_CELLULAR_EVENT_MODEM_READY,
98 MODEM_CELLULAR_EVENT_APN_SET,
99 MODEM_CELLULAR_EVENT_RING,
100 MODEM_CELLULAR_EVENT_PERIODIC_KICK,
103struct modem_cellular_event_cb {
122 struct modem_pipe *uart_pipe;
124 uint8_t uart_backend_receive_buf[CONFIG_MODEM_CELLULAR_UART_BUFFER_SIZES];
125 uint8_t uart_backend_transmit_buf[CONFIG_MODEM_CELLULAR_UART_BUFFER_SIZES];
129 struct modem_cmux cmux;
130 uint8_t cmux_receive_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
131 uint8_t cmux_transmit_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
133 struct modem_cmux_dlci dlci1;
134 struct modem_cmux_dlci dlci2;
135 struct modem_pipe *dlci1_pipe;
136 struct modem_pipe *dlci2_pipe;
138 struct modem_pipe *cmd_pipe;
139 uint8_t dlci1_receive_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
141 uint8_t dlci2_receive_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
145 uint8_t chat_receive_buf[CONFIG_MODEM_CELLULAR_CHAT_BUFFER_SIZE];
164 uint8_t script_failure_counter;
174 uint8_t imei[MODEM_CELLULAR_DATA_IMEI_LEN];
175 uint8_t model_id[MODEM_CELLULAR_DATA_MODEL_ID_LEN];
176 uint8_t imsi[MODEM_CELLULAR_DATA_IMSI_LEN];
177 uint8_t iccid[MODEM_CELLULAR_DATA_ICCID_LEN];
178 uint8_t manufacturer[MODEM_CELLULAR_DATA_MANUFACTURER_LEN];
179 uint8_t fw_version[MODEM_CELLULAR_DATA_FW_VERSION_LEN];
180 uint8_t apn[MODEM_CELLULAR_DATA_APN_LEN];
184 char apn_buf[MODEM_CELLULAR_MAX_APN_CMDS][MODEM_CELLULAR_APN_BUF_SIZE];
197 enum modem_cellular_state
state;
202 struct k_sem suspended_sem;
205 struct k_work event_dispatch_work;
210 struct modem_cellular_event_cb cb;
218 bool periodic_timeout_skipped;
224struct modem_cellular_user_pipe {
225 struct modem_cmux_dlci dlci;
229 struct modem_pipe *pipe;
230 struct modem_pipelink *pipelink;
293struct modem_cellular_config {
294 const struct device *uart;
302 bool hold_reset_on_suspend;
303 bool reset_on_resume;
304 bool reset_on_recovery;
305 bool cmux_enable_runtime_power_save;
306 bool cmux_close_pipe_on_power_save;
307 bool cmux_no_powersave_handshake;
308 bool use_default_pdp_context;
309 bool use_default_apn;
311 struct modem_cellular_user_pipe *user_pipes;
318int modem_cellular_init(
const struct device *dev);
325 const void *payload);
347void modem_cellular_chat_on_modem_ready(
struct modem_chat *chat,
char **argv,
uint16_t argc,
383#define MODEM_CELLULAR_INST_NAME(name, inst) \
384 CONCAT(name, _, DT_DRV_COMPAT, inst)
388#define MODEM_CELLULAR_DEFINE_USER_PIPE_DATA(inst, name, size) \
389 MODEM_PIPELINK_DT_INST_DEFINE(inst, name); \
390 static uint8_t MODEM_CELLULAR_INST_NAME(name, inst)[size] \
392#define MODEM_CELLULAR_INIT_USER_PIPE(_inst, _name, _dlci_address) \
394 .dlci_address = _dlci_address, \
395 .dlci_receive_buf = MODEM_CELLULAR_INST_NAME(_name, _inst), \
396 .dlci_receive_buf_size = sizeof(MODEM_CELLULAR_INST_NAME(_name, _inst)), \
397 .pipelink = MODEM_PIPELINK_DT_INST_GET(_inst, _name), \
400#define MODEM_CELLULAR_DEFINE_USER_PIPES(inst, ...) \
401 static struct modem_cellular_user_pipe MODEM_CELLULAR_INST_NAME(user_pipes, inst)[] = { \
405#define MODEM_CELLULAR_GET_USER_PIPES(inst) \
406 MODEM_CELLULAR_INST_NAME(user_pipes, inst)
409#define MODEM_CELLULAR_GET_PIPE_NAME_ARG(arg1, ...) arg1
412#define MODEM_CELLULAR_GET_DLCI_ADDRESS_ARG(arg1, arg2, ...) arg2
415#define MODEM_CELLULAR_DEFINE_USER_PIPE_DATA_HELPER(_args, inst) \
416 MODEM_CELLULAR_DEFINE_USER_PIPE_DATA(inst, \
417 MODEM_CELLULAR_GET_PIPE_NAME_ARG _args, \
418 CONFIG_MODEM_CELLULAR_USER_PIPE_BUFFER_SIZES)
421#define MODEM_CELLULAR_INIT_USER_PIPE_HELPER(_args, inst) \
422 MODEM_CELLULAR_INIT_USER_PIPE(inst, \
423 MODEM_CELLULAR_GET_PIPE_NAME_ARG _args, \
424 MODEM_CELLULAR_GET_DLCI_ADDRESS_ARG _args)
438#define MODEM_CELLULAR_DEFINE_AND_INIT_USER_PIPES(inst, ...) \
439 FOR_EACH_FIXED_ARG(MODEM_CELLULAR_DEFINE_USER_PIPE_DATA_HELPER, \
440 (;), inst, __VA_ARGS__); \
441 MODEM_CELLULAR_DEFINE_USER_PIPES( \
443 FOR_EACH_FIXED_ARG(MODEM_CELLULAR_INIT_USER_PIPE_HELPER, \
444 (,), inst, __VA_ARGS__) \
454#define MODEM_CELLULAR_COMMON_CHAT_MATCHES() \
455 MODEM_CHAT_MATCH_DEFINE(ok_match, "OK", "", NULL); \
456 MODEM_CHAT_MATCHES_DEFINE(__maybe_unused allow_match, \
457 MODEM_CHAT_MATCH("OK", "", NULL), \
458 MODEM_CHAT_MATCH("ERROR", "", NULL)); \
459 MODEM_CHAT_MATCH_DEFINE(imei_match __maybe_unused, \
460 "", "", modem_cellular_chat_on_imei); \
461 MODEM_CHAT_MATCH_DEFINE(cgmm_match __maybe_unused, \
462 "", "", modem_cellular_chat_on_cgmm); \
463 MODEM_CHAT_MATCH_DEFINE(csq_match __maybe_unused, \
464 "+CSQ: ", ",", modem_cellular_chat_on_csq); \
465 MODEM_CHAT_MATCH_DEFINE(cesq_match __maybe_unused, \
466 "+CESQ: ", ",", modem_cellular_chat_on_cesq); \
467 MODEM_CHAT_MATCH_DEFINE(qccid_match __maybe_unused, \
468 "+QCCID: ", "", modem_cellular_chat_on_iccid); \
469 MODEM_CHAT_MATCH_DEFINE(iccid_match __maybe_unused, \
470 "+ICCID: ", "", modem_cellular_chat_on_iccid); \
471 MODEM_CHAT_MATCH_DEFINE(ccid_match __maybe_unused, \
472 "+CCID: ", "", modem_cellular_chat_on_iccid); \
473 MODEM_CHAT_MATCH_DEFINE(cimi_match __maybe_unused, \
474 "", "", modem_cellular_chat_on_imsi); \
475 MODEM_CHAT_MATCH_DEFINE(cgmi_match __maybe_unused, \
476 "", "", modem_cellular_chat_on_cgmi); \
477 MODEM_CHAT_MATCH_DEFINE(cgmr_match __maybe_unused, \
478 "", "", modem_cellular_chat_on_cgmr); \
479 MODEM_CHAT_MATCH_DEFINE(connect_match __maybe_unused, \
480 "CONNECT", "", NULL); \
481 MODEM_CHAT_MATCHES_DEFINE(__maybe_unused abort_matches, \
482 MODEM_CHAT_MATCH("ERROR", "", NULL)); \
483 MODEM_CHAT_MATCHES_DEFINE(__maybe_unused dial_abort_matches, \
484 MODEM_CHAT_MATCH("ERROR", "", NULL), \
485 MODEM_CHAT_MATCH("BUSY", "", NULL), \
486 MODEM_CHAT_MATCH("NO ANSWER", "", NULL), \
487 MODEM_CHAT_MATCH("NO CARRIER", "", NULL), \
488 MODEM_CHAT_MATCH("NO DIALTONE", "", NULL))
496#define MODEM_CELLULAR_COMMON_UNSOL_MATCHES \
497 MODEM_CHAT_MATCH("+CREG: ", ",", modem_cellular_chat_on_cxreg), \
498 MODEM_CHAT_MATCH("+CEREG: ", ",", modem_cellular_chat_on_cxreg), \
499 MODEM_CHAT_MATCH("+CGREG: ", ",", modem_cellular_chat_on_cxreg), \
500 MODEM_CHAT_MATCH("+CGEV: ", ",", modem_cellular_chat_on_cgev), \
501 MODEM_CHAT_MATCH("APP RDY", "", modem_cellular_chat_on_modem_ready), \
502 MODEM_CHAT_MATCH("Ready", "", modem_cellular_chat_on_modem_ready)
515#define MODEM_CELLULAR_DEFINE_INSTANCE(inst, vendor_config) \
516 BUILD_ASSERT(vendor_config != NULL, "vendor_config must be non-NULL"); \
517 static const struct modem_cellular_config MODEM_CELLULAR_INST_NAME(config, inst) = { \
518 .uart = DEVICE_DT_GET(DT_INST_BUS(inst)), \
519 .vendor = vendor_config, \
520 .power_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_power_gpios, {}), \
521 .reset_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_reset_gpios, {}), \
522 .wake_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_wake_gpios, {}), \
523 .ring_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_ring_gpios, {}), \
524 .dtr_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_dtr_gpios, {}), \
525 .autostarts = DT_INST_PROP(inst, autostarts), \
526 .hold_reset_on_suspend = \
527 DT_INST_ENUM_HAS_VALUE(inst, zephyr_mdm_reset_behavior, hold_on_suspend), \
528 .reset_on_resume = DT_INST_ENUM_HAS_VALUE(inst, zephyr_mdm_reset_behavior, \
530 .reset_on_recovery = DT_INST_ENUM_HAS_VALUE(inst, zephyr_mdm_reset_behavior, \
531 toggle_on_recovery), \
532 .cmux_enable_runtime_power_save = \
533 DT_INST_PROP_OR(inst, cmux_enable_runtime_power_save, 0), \
534 .cmux_close_pipe_on_power_save = \
535 DT_INST_PROP_OR(inst, cmux_close_pipe_on_power_save, 0), \
536 .cmux_no_powersave_handshake = \
537 DT_INST_PROP_OR(inst, cmux_no_powersave_handshake, 0), \
538 .use_default_pdp_context = DT_INST_PROP_OR(inst, zephyr_use_default_pdp_ctx, 0), \
539 .use_default_apn = DT_INST_PROP_OR(inst, zephyr_use_default_apn, 0), \
540 .cmux_idle_timeout = K_MSEC(DT_INST_PROP_OR(inst, cmux_idle_timeout_ms, 0)), \
541 .user_pipes = MODEM_CELLULAR_GET_USER_PIPES(inst), \
542 .user_pipes_size = ARRAY_SIZE(MODEM_CELLULAR_GET_USER_PIPES(inst)), \
545 PM_DEVICE_DT_INST_DEFINE(inst, modem_cellular_pm_action); \
547 DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
548 &MODEM_CELLULAR_INST_NAME(data, inst), \
549 &MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, \
550 CONFIG_MODEM_CELLULAR_INIT_PRIORITY, &modem_cellular_api);
574#if defined(CONFIG_MODEM_CELLULAR)
575#define MODEM_CELLULAR_BOARD_INIT_DEFINE(node_id, _script) \
576 static const STRUCT_SECTION_ITERABLE( \
577 modem_cellular_board_init, \
578 CONCAT(modem_cellular_board_init_, DT_DEP_ORD(node_id))) = { \
579 .dev = DEVICE_DT_GET(node_id), \
580 .script = (_script), \
583#define MODEM_CELLULAR_BOARD_INIT_DEFINE(node_id, _script)
Main header file for cellular modem driver API.
Main header file for GPIO driver API.
Main header file for UART driver API.
long atomic_t
Atomic integer variable.
Definition atomic_types.h:31
cellular_access_technology
Cellular access technologies (3GPP TS 27.007 AcT).
Definition cellular.h:33
cellular_event
Events emitted asynchronously by a cellular driver.
Definition cellular.h:137
int cellular_modem_pause_periodic_script(const struct device *dev)
Pause the cellular_modem driver's periodic chat script.
int cellular_modem_resume_periodic_script(const struct device *dev)
Resume the cellular_modem driver's periodic chat script.
cellular_registration_status
Cellular registration status (3GPP TS 27.007).
Definition cellular.h:109
uint32_t cellular_event_mask_t
Definition cellular.h:149
void(* cellular_event_cb_t)(const struct device *dev, enum cellular_event event, const void *payload, void *user_data)
Prototype for cellular event callbacks.
Definition cellular.h:202
void modem_cellular_chat_callback_handler(struct modem_chat *chat, enum modem_chat_script_result result, void *user_data)
Handle completion of a modem chat script.
modem_chat_script_result
Definition chat.h:156
pm_device_action
Device PM actions.
Definition device.h:150
state
Definition parser_state.h:29
Header file for the Device Power Management API.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
@driver_ops{Cellular}
Definition cellular.h:245
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
GPIO callback structure.
Definition gpio.h:747
Container for GPIO pin information specified in devicetree.
Definition gpio.h:296
Kernel mutex structure.
Definition kernel.h:3477
Kernel pipe structure.
Definition kernel.h:5717
Semaphore structure.
Definition kernel.h:3703
Kernel timeout type.
Definition clock.h:65
A structure used to submit work after a delay.
Definition kernel.h:4643
A structure used to submit work.
Definition kernel.h:4601
Descriptor binding a board-supplied init script to a modem instance.
Definition modem_cellular.h:557
const struct modem_chat_script * script
Definition modem_cellular.h:559
const struct device * dev
Definition modem_cellular.h:558
Chat scripts for cellular modem.
Definition modem_cellular.h:248
const struct modem_chat_script * periodic
Optional script that periodically polls modem state while registered.
Definition modem_cellular.h:258
const struct modem_chat_script * dial
Script that starts the PPP data connection.
Definition modem_cellular.h:256
const struct modem_chat_script * network
Optional script that waits for network registration before dialing.
Definition modem_cellular.h:254
const struct modem_chat_script * set_baudrate
Optional script that configures the modem's UART baud rate.
Definition modem_cellular.h:250
const struct modem_chat_script * shutdown
Optional script that prepares the modem for power-off.
Definition modem_cellular.h:260
const struct modem_chat_script * init
Script that initializes the modem and enables CMUX.
Definition modem_cellular.h:252
Runtime data for a cellular modem driver instance.
Definition modem_cellular.h:119
uint8_t * chat_delimiter
Command delimiter used by the modem, as a NULL-terminated string.
Definition modem_cellular.h:153
struct modem_ppp * ppp
PPP context used for the modem data connection.
Definition modem_cellular.h:192
uint8_t * chat_filter
Characters filtered from modem responses, as a NULL-terminated string.
Definition modem_cellular.h:160
Vendor-specific cellular modem configuration.
Definition modem_cellular.h:269
uint16_t shutdown_time_ms
Maximum modem shutdown delay, in milliseconds.
Definition modem_cellular.h:286
uint16_t reset_pulse_duration_ms
Duration of the modem reset pulse, in milliseconds.
Definition modem_cellular.h:282
bool force_autostart
Force autostart regardless of the devicetree autostarts property.
Definition modem_cellular.h:288
struct modem_cellular_vendor_config::@125173161324242306355007001312260252170162237167 unsol_matches
Unsolicited modem response matches.
struct modem_cellular_config_scripts scripts
Chat scripts implementing the modem lifecycle.
Definition modem_cellular.h:271
const struct modem_chat_match * matches
Array of unsolicited response matches, or NULL when size is zero.
Definition modem_cellular.h:275
uint16_t size
Number of elements in matches.
Definition modem_cellular.h:277
uint16_t power_pulse_duration_ms
Duration of the modem power-key pulse, in milliseconds.
Definition modem_cellular.h:280
uint16_t startup_time_ms
Maximum modem startup delay, in milliseconds.
Definition modem_cellular.h:284
Modem chat match.
Definition chat.h:47
Modem chat script chat.
Definition chat.h:110
Modem chat script.
Definition chat.h:175
Chat instance internal context.
Definition chat.h:223
Network Management event callback structure Used to register a callback into the network management e...
Definition net_mgmt.h:163
Header file for the Atomic operations API.
Iterable sections helpers.