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_RECOVERY,
60 MODEM_CELLULAR_STATE_RESET_PULSE,
61 MODEM_CELLULAR_STATE_AWAIT_RESET,
62 MODEM_CELLULAR_STATE_POWER_ON_PULSE,
63 MODEM_CELLULAR_STATE_AWAIT_POWER_ON,
64 MODEM_CELLULAR_STATE_SET_BAUDRATE,
65 MODEM_CELLULAR_STATE_RUN_INIT_SCRIPT,
66 MODEM_CELLULAR_STATE_CONNECT_CMUX,
67 MODEM_CELLULAR_STATE_OPEN_DLCI1,
68 MODEM_CELLULAR_STATE_OPEN_DLCI2,
69 MODEM_CELLULAR_STATE_RUN_BOARD_INIT_SCRIPT,
70 MODEM_CELLULAR_STATE_WAIT_FOR_APN,
71 MODEM_CELLULAR_STATE_RUN_APN_SCRIPT,
72 MODEM_CELLULAR_STATE_RUN_NETWORK_SCRIPT,
73 MODEM_CELLULAR_STATE_RUN_DIAL_SCRIPT,
74 MODEM_CELLULAR_STATE_AWAIT_REGISTERED,
75 MODEM_CELLULAR_STATE_REGISTERED,
76 MODEM_CELLULAR_STATE_AWAIT_PPP_DEAD,
77 MODEM_CELLULAR_STATE_INIT_POWER_OFF,
78 MODEM_CELLULAR_STATE_RUN_SHUTDOWN_SCRIPT,
79 MODEM_CELLULAR_STATE_POWER_OFF_PULSE,
80 MODEM_CELLULAR_STATE_AWAIT_POWER_OFF,
81 MODEM_CELLULAR_STATE_AWAIT_DIAL,
84enum modem_cellular_event {
85 MODEM_CELLULAR_EVENT_RESUME = 0,
86 MODEM_CELLULAR_EVENT_SUSPEND,
87 MODEM_CELLULAR_EVENT_SCRIPT_SUCCESS,
88 MODEM_CELLULAR_EVENT_SCRIPT_FAILED,
89 MODEM_CELLULAR_EVENT_CMUX_CONNECTED,
90 MODEM_CELLULAR_EVENT_CMUX_DISCONNECTED,
91 MODEM_CELLULAR_EVENT_DLCI1_OPENED,
92 MODEM_CELLULAR_EVENT_DLCI2_OPENED,
93 MODEM_CELLULAR_EVENT_TIMEOUT,
94 MODEM_CELLULAR_EVENT_REGISTERED,
95 MODEM_CELLULAR_EVENT_DEREGISTERED,
96 MODEM_CELLULAR_EVENT_BUS_OPENED,
97 MODEM_CELLULAR_EVENT_BUS_CLOSED,
98 MODEM_CELLULAR_EVENT_PPP_DEAD,
99 MODEM_CELLULAR_EVENT_MODEM_READY,
100 MODEM_CELLULAR_EVENT_APN_SET,
101 MODEM_CELLULAR_EVENT_RING,
102 MODEM_CELLULAR_EVENT_PERIODIC_KICK,
103 MODEM_CELLULAR_EVENT_DIAL,
104 MODEM_CELLULAR_EVENT_HANGUP,
107struct modem_cellular_event_cb {
126 struct modem_pipe *uart_pipe;
128 uint8_t uart_backend_receive_buf[CONFIG_MODEM_CELLULAR_UART_BUFFER_SIZES];
129 uint8_t uart_backend_transmit_buf[CONFIG_MODEM_CELLULAR_UART_BUFFER_SIZES];
133 struct modem_cmux cmux;
134 uint8_t cmux_receive_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
135 uint8_t cmux_transmit_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
137 struct modem_cmux_dlci dlci1;
138 struct modem_cmux_dlci dlci2;
139 struct modem_pipe *dlci1_pipe;
140 struct modem_pipe *dlci2_pipe;
142 struct modem_pipe *cmd_pipe;
143 uint8_t dlci1_receive_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
145 uint8_t dlci2_receive_buf[MODEM_CMUX_WORK_BUFFER_SIZE];
149 uint8_t chat_receive_buf[CONFIG_MODEM_CELLULAR_CHAT_BUFFER_SIZE];
151 uint8_t script_failure_counter;
163 bool network_status_valid;
164 uint8_t imei[MODEM_CELLULAR_DATA_IMEI_LEN];
165 uint8_t model_id[MODEM_CELLULAR_DATA_MODEL_ID_LEN];
166 uint8_t imsi[MODEM_CELLULAR_DATA_IMSI_LEN];
167 uint8_t iccid[MODEM_CELLULAR_DATA_ICCID_LEN];
168 uint8_t manufacturer[MODEM_CELLULAR_DATA_MANUFACTURER_LEN];
169 uint8_t fw_version[MODEM_CELLULAR_DATA_FW_VERSION_LEN];
170 uint8_t apn[MODEM_CELLULAR_DATA_APN_LEN];
174 char apn_buf[MODEM_CELLULAR_MAX_APN_CMDS][MODEM_CELLULAR_APN_BUF_SIZE];
179#if defined(CONFIG_MODEM_CELLULAR_ON_DEMAND_CONNECT)
183 enum modem_cellular_state
state;
188 struct k_sem suspended_sem;
191 struct k_work event_dispatch_work;
196 struct modem_cellular_event_cb cb;
204 bool periodic_timeout_skipped;
208 bool power_on_skipped;
210#if defined(CONFIG_MODEM_CELLULAR_STATS)
221struct modem_cellular_user_pipe {
222 struct modem_cmux_dlci dlci;
226 struct modem_pipe *pipe;
227 struct modem_pipelink *pipelink;
310struct modem_cellular_config {
311 const struct device *uart;
313 struct modem_ppp *ppp;
321 bool hold_reset_on_suspend;
322 bool reset_on_resume;
323 bool reset_on_recovery;
324 bool cmux_enable_runtime_power_save;
325 bool cmux_close_pipe_on_power_save;
326 bool cmux_no_powersave_handshake;
327 bool use_default_pdp_context;
328 bool use_default_apn;
330 struct modem_cellular_user_pipe *user_pipes;
337int modem_cellular_init(
const struct device *dev);
344 const void *payload);
374void modem_cellular_chat_on_modem_ready(
struct modem_chat *chat,
char **argv,
uint16_t argc,
410#define MODEM_CELLULAR_INST_NAME(name, inst) \
411 CONCAT(name, _, DT_DRV_COMPAT, inst)
415#define MODEM_CELLULAR_DEFINE_USER_PIPE_DATA(inst, name, size) \
416 MODEM_PIPELINK_DT_INST_DEFINE(inst, name); \
417 static uint8_t MODEM_CELLULAR_INST_NAME(name, inst)[size] \
419#define MODEM_CELLULAR_INIT_USER_PIPE(_inst, _name, _dlci_address) \
421 .dlci_address = _dlci_address, \
422 .dlci_receive_buf = MODEM_CELLULAR_INST_NAME(_name, _inst), \
423 .dlci_receive_buf_size = sizeof(MODEM_CELLULAR_INST_NAME(_name, _inst)), \
424 .pipelink = MODEM_PIPELINK_DT_INST_GET(_inst, _name), \
427#define MODEM_CELLULAR_DEFINE_USER_PIPES(inst, ...) \
428 static struct modem_cellular_user_pipe MODEM_CELLULAR_INST_NAME(user_pipes, inst)[] = { \
432#define MODEM_CELLULAR_GET_USER_PIPES(inst) \
433 MODEM_CELLULAR_INST_NAME(user_pipes, inst)
436#define MODEM_CELLULAR_GET_PIPE_NAME_ARG(arg1, ...) arg1
439#define MODEM_CELLULAR_GET_DLCI_ADDRESS_ARG(arg1, arg2, ...) arg2
442#define MODEM_CELLULAR_DEFINE_USER_PIPE_DATA_HELPER(_args, inst) \
443 MODEM_CELLULAR_DEFINE_USER_PIPE_DATA(inst, \
444 MODEM_CELLULAR_GET_PIPE_NAME_ARG _args, \
445 CONFIG_MODEM_CELLULAR_USER_PIPE_BUFFER_SIZES)
448#define MODEM_CELLULAR_INIT_USER_PIPE_HELPER(_args, inst) \
449 MODEM_CELLULAR_INIT_USER_PIPE(inst, \
450 MODEM_CELLULAR_GET_PIPE_NAME_ARG _args, \
451 MODEM_CELLULAR_GET_DLCI_ADDRESS_ARG _args)
465#define MODEM_CELLULAR_DEFINE_AND_INIT_USER_PIPES(inst, ...) \
466 FOR_EACH_FIXED_ARG(MODEM_CELLULAR_DEFINE_USER_PIPE_DATA_HELPER, \
467 (;), inst, __VA_ARGS__); \
468 MODEM_CELLULAR_DEFINE_USER_PIPES( \
470 FOR_EACH_FIXED_ARG(MODEM_CELLULAR_INIT_USER_PIPE_HELPER, \
471 (,), inst, __VA_ARGS__) \
484#define MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(_sym, _match, _separators, _callback) \
485 MODEM_CHAT_MATCHES_DEFINE(_sym, \
486 MODEM_CHAT_MATCH_INITIALIZER(_match, _separators, _callback, false, true), \
487 MODEM_CHAT_MATCH("OK", "", NULL))
496#define MODEM_CELLULAR_COMMON_CHAT_MATCHES() \
497 MODEM_CHAT_MATCH_DEFINE(ok_match, "OK", "", NULL); \
498 MODEM_CHAT_MATCHES_DEFINE(__maybe_unused allow_match, \
499 MODEM_CHAT_MATCH("OK", "", NULL), \
500 MODEM_CHAT_MATCH("ERROR", "", NULL)); \
501 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused imei_match, \
502 "", "", modem_cellular_chat_on_imei); \
503 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused cgmm_match, \
504 "", "", modem_cellular_chat_on_cgmm); \
505 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused csq_match, \
506 "+CSQ: ", ",", modem_cellular_chat_on_csq); \
507 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused cesq_match, \
508 "+CESQ: ", ",", modem_cellular_chat_on_cesq); \
509 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused qccid_match, \
510 "+QCCID: ", "", modem_cellular_chat_on_iccid); \
511 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused iccid_match, \
512 "+ICCID: ", "", modem_cellular_chat_on_iccid); \
513 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused ccid_match, \
514 "+CCID: ", "", modem_cellular_chat_on_iccid); \
515 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused cimi_match, \
516 "", "", modem_cellular_chat_on_imsi); \
517 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused cgmi_match, \
518 "", "", modem_cellular_chat_on_cgmi); \
519 MODEM_CELLULAR_OK_CHAT_MATCH_DEFINE(__maybe_unused cgmr_match, \
520 "", "", modem_cellular_chat_on_cgmr); \
521 MODEM_CHAT_MATCH_DEFINE(connect_match __maybe_unused, \
522 "CONNECT", "", NULL); \
523 MODEM_CHAT_MATCHES_DEFINE(__maybe_unused abort_matches, \
524 MODEM_CHAT_MATCH("ERROR", "", NULL)); \
525 MODEM_CHAT_MATCHES_DEFINE(__maybe_unused dial_abort_matches, \
526 MODEM_CHAT_MATCH("ERROR", "", NULL), \
527 MODEM_CHAT_MATCH("BUSY", "", NULL), \
528 MODEM_CHAT_MATCH("NO ANSWER", "", NULL), \
529 MODEM_CHAT_MATCH("NO CARRIER", "", NULL), \
530 MODEM_CHAT_MATCH("NO DIALTONE", "", NULL))
538#define MODEM_CELLULAR_COMMON_UNSOL_MATCHES \
539 MODEM_CHAT_MATCH("+CREG: ", ",", modem_cellular_chat_on_cxreg), \
540 MODEM_CHAT_MATCH("+CEREG: ", ",", modem_cellular_chat_on_cxreg), \
541 MODEM_CHAT_MATCH("+CGREG: ", ",", modem_cellular_chat_on_cxreg), \
542 MODEM_CHAT_MATCH("+CGEV: ", ",", modem_cellular_chat_on_cgev), \
543 MODEM_CHAT_MATCH("APP RDY", "", modem_cellular_chat_on_modem_ready), \
544 MODEM_CHAT_MATCH("Ready", "", modem_cellular_chat_on_modem_ready)
557#define MODEM_CELLULAR_DEFINE_INSTANCE(inst, vendor_config) \
558 BUILD_ASSERT(vendor_config != NULL, "vendor_config must be non-NULL"); \
559 static const struct modem_cellular_config MODEM_CELLULAR_INST_NAME(config, inst) = { \
560 .uart = DEVICE_DT_GET(DT_INST_BUS(inst)), \
561 .vendor = vendor_config, \
562 .ppp = &MODEM_CELLULAR_INST_NAME(ppp, inst), \
563 .power_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_power_gpios, {}), \
564 .reset_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_reset_gpios, {}), \
565 .wake_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_wake_gpios, {}), \
566 .ring_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_ring_gpios, {}), \
567 .dtr_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_dtr_gpios, {}), \
568 .status_gpio = GPIO_DT_SPEC_INST_GET_OR(inst, mdm_status_gpios, {}), \
569 .autostarts = DT_INST_PROP(inst, autostarts), \
570 .hold_reset_on_suspend = \
571 DT_INST_ENUM_HAS_VALUE(inst, zephyr_mdm_reset_behavior, hold_on_suspend), \
572 .reset_on_resume = DT_INST_ENUM_HAS_VALUE(inst, zephyr_mdm_reset_behavior, \
574 .reset_on_recovery = DT_INST_ENUM_HAS_VALUE(inst, zephyr_mdm_reset_behavior, \
575 toggle_on_recovery), \
576 .cmux_enable_runtime_power_save = \
577 DT_INST_PROP_OR(inst, cmux_enable_runtime_power_save, 0), \
578 .cmux_close_pipe_on_power_save = \
579 DT_INST_PROP_OR(inst, cmux_close_pipe_on_power_save, 0), \
580 .cmux_no_powersave_handshake = \
581 DT_INST_PROP_OR(inst, cmux_no_powersave_handshake, 0), \
582 .use_default_pdp_context = DT_INST_PROP_OR(inst, zephyr_use_default_pdp_ctx, 0), \
583 .use_default_apn = DT_INST_PROP_OR(inst, zephyr_use_default_apn, 0), \
584 .cmux_idle_timeout = K_MSEC(DT_INST_PROP_OR(inst, cmux_idle_timeout_ms, 0)), \
585 .user_pipes = MODEM_CELLULAR_GET_USER_PIPES(inst), \
586 .user_pipes_size = ARRAY_SIZE(MODEM_CELLULAR_GET_USER_PIPES(inst)), \
589 PM_DEVICE_DT_INST_DEFINE(inst, modem_cellular_pm_action); \
591 DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
592 &MODEM_CELLULAR_INST_NAME(data, inst), \
593 &MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, \
594 CONFIG_MODEM_CELLULAR_INIT_PRIORITY, &modem_cellular_api);
618#if defined(CONFIG_MODEM_CELLULAR)
619#define MODEM_CELLULAR_BOARD_INIT_DEFINE(node_id, _script) \
620 static const STRUCT_SECTION_ITERABLE( \
621 modem_cellular_board_init, \
622 CONCAT(modem_cellular_board_init_, DT_DEP_ORD(node_id))) = { \
623 .dev = DEVICE_DT_GET(node_id), \
624 .script = (_script), \
627#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:39
cellular_event
Events emitted asynchronously by a cellular driver.
Definition cellular.h:143
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:115
uint32_t cellular_event_mask_t
Definition cellular.h:157
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:232
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:249
pm_device_action
Device PM actions.
Definition device.h:154
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:282
Payload for CELLULAR_EVENT_NETWORK_STATUS_CHANGED.
Definition cellular.h:180
Cellular link operational statistics.
Definition cellular.h:203
Runtime device structure (in ROM) per driver instance.
Definition device.h:525
GPIO callback structure.
Definition gpio.h:771
Container for GPIO pin information specified in devicetree.
Definition gpio.h:296
Kernel mutex structure.
Definition kernel.h:3492
Kernel pipe structure.
Definition kernel.h:5820
Semaphore structure.
Definition kernel.h:3726
Kernel timeout type.
Definition clock.h:65
A structure used to submit work after a delay.
Definition kernel.h:4678
A structure used to submit work.
Definition kernel.h:4636
Descriptor binding a board-supplied init script to a modem instance.
Definition modem_cellular.h:601
const struct modem_chat_script * script
Definition modem_cellular.h:603
const struct device * dev
Definition modem_cellular.h:602
Chat scripts for cellular modem.
Definition modem_cellular.h:245
const struct modem_chat_script * periodic
Optional script that periodically polls modem state while registered.
Definition modem_cellular.h:255
const struct modem_chat_script * dial
Script that starts the PPP data connection.
Definition modem_cellular.h:253
const struct modem_chat_script * network
Optional script that waits for network registration before dialing.
Definition modem_cellular.h:251
const struct modem_chat_script * set_baudrate
Optional script that configures the modem's UART baud rate.
Definition modem_cellular.h:247
const struct modem_chat_script * shutdown
Optional script that prepares the modem for power-off.
Definition modem_cellular.h:257
const struct modem_chat_script * init
Script that initializes the modem and enables CMUX.
Definition modem_cellular.h:249
const struct modem_chat_script * dlci_setup
Optional script for configuring DLCI channels after opening.
Definition modem_cellular.h:259
Runtime data for a cellular modem driver instance.
Definition modem_cellular.h:123
Vendor-specific cellular modem configuration.
Definition modem_cellular.h:268
uint16_t shutdown_time_ms
Maximum modem shutdown delay, in milliseconds.
Definition modem_cellular.h:303
uint16_t reset_pulse_duration_ms
Duration of the modem reset pulse, in milliseconds.
Definition modem_cellular.h:294
bool force_autostart
Force autostart regardless of the devicetree autostarts property.
Definition modem_cellular.h:305
const char * chat_delimiter
Command delimiter used by the modem, as a NULL-terminated string.
Definition modem_cellular.h:283
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:270
const struct modem_chat_match * matches
Array of unsolicited response matches, or NULL when size is zero.
Definition modem_cellular.h:274
uint16_t size
Number of elements in matches.
Definition modem_cellular.h:276
uint16_t power_pulse_duration_ms
Duration of the modem power-key pulse, in milliseconds.
Definition modem_cellular.h:292
const char * chat_filter
Characters filtered from modem responses, as a NULL-terminated string.
Definition modem_cellular.h:290
uint16_t cmux_disconnect_timeout_ms
Timeout for the modem to revert from CMUX to AT mode after a CMUX disconnect, in milliseconds.
Definition modem_cellular.h:299
uint16_t startup_time_ms
Maximum modem startup delay, in milliseconds.
Definition modem_cellular.h:301
Modem chat match.
Definition chat.h:57
Modem chat script chat.
Definition chat.h:124
Modem chat script.
Definition chat.h:268
Chat instance internal context.
Definition chat.h:314
Network Management event callback structure Used to register a callback into the network management e...
Definition net_mgmt.h:164
Header file for the Atomic operations API.
Iterable sections helpers.