Skip to content

Commit 0a444c6

Browse files
petrosyan-vancfriedt
authored andcommitted
drivers: cellular: add event notification API
Introduces cellular_set_callback() and the first event CELLULAR_EVENT_MODEM_INFO_CHANGED. Drivers call the registered callback whenever any modem-info field is updated. Signed-off-by: Van Petrosyan <[email protected]>
1 parent 26492d8 commit 0a444c6

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

include/zephyr/drivers/cellular.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,35 @@ enum cellular_registration_status {
8585
CELLULAR_REGISTRATION_REGISTERED_ROAMING,
8686
};
8787

88+
/** Events emitted asynchronously by a cellular driver */
89+
enum cellular_event {
90+
/** One or more modem-info field changed (e.g. IMSI became available). */
91+
CELLULAR_EVENT_MODEM_INFO_CHANGED = BIT(0),
92+
};
93+
94+
/* Opaque bit-mask large enough for all current & future events */
95+
typedef uint32_t cellular_event_mask_t;
96+
97+
/** Payload for @ref CELLULAR_EVENT_MODEM_INFO_CHANGED. */
98+
struct cellular_evt_modem_info {
99+
enum cellular_modem_info_type field; /**< Which field changed */
100+
};
101+
102+
/**
103+
* @brief Prototype for cellular event callbacks.
104+
*
105+
* @param dev Cellular device that generated the event
106+
* @param event Which @ref cellular_event occurred
107+
* @param payload Pointer to the kind-specific payload
108+
* (`NULL` if the kind defines no payload).
109+
* @param user_data Pointer supplied when the callback was registered
110+
*
111+
* @note The driver calls the callback directly from its own context.
112+
* Off-load heavy processing to a work-queue if required.
113+
*/
114+
typedef void (*cellular_event_cb_t)(const struct device *dev, enum cellular_event event,
115+
const void *payload, void *user_data);
116+
88117
/** API for configuring networks */
89118
typedef int (*cellular_api_configure_networks)(const struct device *dev,
90119
const struct cellular_network *networks,
@@ -101,8 +130,8 @@ typedef int (*cellular_api_get_signal)(const struct device *dev,
101130

102131
/** API for getting modem information */
103132
typedef int (*cellular_api_get_modem_info)(const struct device *dev,
104-
const enum cellular_modem_info_type type,
105-
char *info, size_t size);
133+
const enum cellular_modem_info_type type, char *info,
134+
size_t size);
106135

107136
/** API for getting registration status */
108137
typedef int (*cellular_api_get_registration_status)(const struct device *dev,
@@ -112,6 +141,10 @@ typedef int (*cellular_api_get_registration_status)(const struct device *dev,
112141
/** API for programming APN */
113142
typedef int (*cellular_api_set_apn)(const struct device *dev, const char *apn);
114143

144+
/** API for registering an asynchronous callback */
145+
typedef int (*cellular_api_set_callback)(const struct device *dev, cellular_event_mask_t mask,
146+
cellular_event_cb_t cb, void *user_data);
147+
115148
/** Cellular driver API */
116149
__subsystem struct cellular_driver_api {
117150
cellular_api_configure_networks configure_networks;
@@ -120,6 +153,7 @@ __subsystem struct cellular_driver_api {
120153
cellular_api_get_modem_info get_modem_info;
121154
cellular_api_get_registration_status get_registration_status;
122155
cellular_api_set_apn set_apn;
156+
cellular_api_set_callback set_callback;
123157
};
124158

125159
/**
@@ -281,6 +315,32 @@ static inline int cellular_set_apn(const struct device *dev, const char *apn)
281315
return api->set_apn(dev, apn);
282316
}
283317

318+
/**
319+
* @brief Subscribe to asynchronous cellular events.
320+
*
321+
* @param dev Cellular device
322+
* @param mask Event mask to subscribe to
323+
* @param cb Callback to call when the event occurs, or NULL to unsubscribe
324+
* @param user_data Pointer to user data that will be passed to the callback
325+
*
326+
* @retval 0 Success
327+
* @retval -ENOSYS Driver does not support event callbacks
328+
* @retval -EINVAL Bad parameters
329+
* @retval -ENOMEM No space left for another subscriber
330+
* @retval <0 Driver-specific error
331+
*/
332+
static inline int cellular_set_callback(const struct device *dev, cellular_event_mask_t mask,
333+
cellular_event_cb_t cb, void *user_data)
334+
{
335+
const struct cellular_driver_api *api = (const struct cellular_driver_api *)dev->api;
336+
337+
if (api->set_callback == NULL) {
338+
return -ENOSYS;
339+
}
340+
341+
return api->set_callback(dev, mask, cb, user_data);
342+
}
343+
284344
#ifdef __cplusplus
285345
}
286346
#endif

0 commit comments

Comments
 (0)