modem: modem_cellular: Update driver to implement cellular API

Move the implementations of the cellular API in modem_cellular.c
to an cellular_driver_structure, and implement this API structure
withing the device drivers.

Signed-off-by: Bjarki Arge Andreasen <bjarki@arge-andreasen.me>
This commit is contained in:
Bjarki Arge Andreasen 2023-12-29 10:48:44 +01:00 committed by Carles Cufí
parent 6f55309659
commit f9491034be
2 changed files with 104 additions and 107 deletions

View file

@ -1263,6 +1263,80 @@ static void modem_cellular_cmux_handler(struct modem_cmux *cmux, enum modem_cmux
}
}
static int modem_cellular_get_signal(const struct device *dev,
const enum cellular_signal_type type,
int16_t *value)
{
int ret;
struct modem_cellular_data *data = (struct modem_cellular_data *)dev->data;
const struct modem_cellular_config *config = (struct modem_cellular_config *)dev->config;
if (config->get_signal_chat_script == NULL) {
return -ENOTSUP;
}
if ((data->state != MODEM_CELLULAR_STATE_AWAIT_REGISTERED) &&
(data->state != MODEM_CELLULAR_STATE_CARRIER_ON)) {
return -ENODATA;
}
ret = modem_chat_run_script(&data->chat, config->get_signal_chat_script);
if (ret != 0) {
return ret;
}
switch (type) {
case CELLULAR_SIGNAL_RSSI: {
*value = data->rssi;
} break;
case CELLULAR_SIGNAL_RSRP:
return -ENOTSUP;
case CELLULAR_SIGNAL_RSRQ:
return -ENOTSUP;
}
return ret;
}
static int modem_cellular_get_modem_info(const struct device *dev,
enum cellular_modem_info_type type,
char *info, size_t size)
{
int ret = 0;
struct modem_cellular_data *data = (struct modem_cellular_data *)dev->data;
switch (type) {
case CELLULAR_MODEM_INFO_IMEI:
strncpy(info, &data->imei[0], MIN(size, sizeof(data->imei)));
break;
case CELLULAR_MODEM_INFO_SIM_IMSI:
strncpy(info, &data->imsi[0], MIN(size, sizeof(data->imsi)));
break;
case CELLULAR_MODEM_INFO_SIM_ICCID:
strncpy(info, &data->iccid[0], MIN(size, sizeof(data->iccid)));
break;
case CELLULAR_MODEM_INFO_MANUFACTURER:
strncpy(info, &data->manufacturer[0], MIN(size, sizeof(data->manufacturer)));
break;
case CELLULAR_MODEM_INFO_FW_VERSION:
strncpy(info, &data->fw_version[0], MIN(size, sizeof(data->fw_version)));
break;
case CELLULAR_MODEM_INFO_MODEL_ID:
strncpy(info, &data->model_id[0], MIN(size, sizeof(data->model_id)));
break;
default:
ret = -ENODATA;
break;
}
return ret;
}
const static struct cellular_driver_api modem_cellular_api = {
.get_signal = modem_cellular_get_signal,
.get_modem_info = modem_cellular_get_modem_info,
};
#ifdef CONFIG_PM_DEVICE
static int modem_cellular_pm_action(const struct device *dev, enum pm_device_action action)
{
@ -1828,7 +1902,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define MODEM_CELLULAR_DEVICE_QUECTEL_EG25_G(inst) \
MODEM_PPP_DEFINE(MODEM_CELLULAR_INST_NAME(ppp, inst), NULL, 98, 1500, 64); \
@ -1857,7 +1932,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define MODEM_CELLULAR_DEVICE_GSM_PPP(inst) \
MODEM_PPP_DEFINE(MODEM_CELLULAR_INST_NAME(ppp, inst), NULL, 98, 1500, 64); \
@ -1885,7 +1961,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define MODEM_CELLULAR_DEVICE_SIMCOM_SIM7080(inst) \
MODEM_PPP_DEFINE(MODEM_CELLULAR_INST_NAME(ppp, inst), NULL, 98, 1500, 64); \
@ -1913,7 +1990,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define MODEM_CELLULAR_DEVICE_U_BLOX_SARA_R4(inst) \
MODEM_PPP_DEFINE(MODEM_CELLULAR_INST_NAME(ppp, inst), NULL, 98, 1500, 64); \
@ -1941,7 +2019,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define MODEM_CELLULAR_DEVICE_U_BLOX_SARA_R5(inst) \
MODEM_PPP_DEFINE(MODEM_CELLULAR_INST_NAME(ppp, inst), NULL, 98, 1500, 64); \
@ -1970,7 +2049,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define MODEM_CELLULAR_DEVICE_SWIR_HL7800(inst) \
MODEM_PPP_DEFINE(MODEM_CELLULAR_INST_NAME(ppp, inst), NULL, 98, 1500, 64); \
@ -1998,7 +2078,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define MODEM_CELLULAR_DEVICE_TELIT_ME910G1(inst) \
MODEM_PPP_DEFINE(MODEM_CELLULAR_INST_NAME(ppp, inst), NULL, 98, 1500, 64); \
@ -2026,7 +2107,8 @@ MODEM_CHAT_SCRIPT_DEFINE(telit_me910g1_periodic_chat_script,
\
DEVICE_DT_INST_DEFINE(inst, modem_cellular_init, PM_DEVICE_DT_INST_GET(inst), \
&MODEM_CELLULAR_INST_NAME(data, inst), \
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, NULL);
&MODEM_CELLULAR_INST_NAME(config, inst), POST_KERNEL, 99, \
&modem_cellular_api);
#define DT_DRV_COMPAT quectel_bg95
DT_INST_FOREACH_STATUS_OKAY(MODEM_CELLULAR_DEVICE_QUECTEL_BG95)
@ -2059,70 +2141,3 @@ DT_INST_FOREACH_STATUS_OKAY(MODEM_CELLULAR_DEVICE_SWIR_HL7800)
#define DT_DRV_COMPAT telit_me910g1
DT_INST_FOREACH_STATUS_OKAY(MODEM_CELLULAR_DEVICE_TELIT_ME910G1)
#undef DT_DRV_COMPAT
int cellular_get_modem_info(const struct device *dev, enum cellular_modem_info_type type,
char *info, size_t size)
{
int ret = 0;
struct modem_cellular_data *data = (struct modem_cellular_data *)dev->data;
switch (type) {
case CELLULAR_MODEM_INFO_IMEI:
strncpy(info, &data->imei[0], MIN(size, sizeof(data->imei)));
break;
case CELLULAR_MODEM_INFO_SIM_IMSI:
strncpy(info, &data->imsi[0], MIN(size, sizeof(data->imsi)));
break;
case CELLULAR_MODEM_INFO_SIM_ICCID:
strncpy(info, &data->iccid[0], MIN(size, sizeof(data->iccid)));
break;
case CELLULAR_MODEM_INFO_MANUFACTURER:
strncpy(info, &data->manufacturer[0], MIN(size, sizeof(data->manufacturer)));
break;
case CELLULAR_MODEM_INFO_FW_VERSION:
strncpy(info, &data->fw_version[0], MIN(size, sizeof(data->fw_version)));
break;
case CELLULAR_MODEM_INFO_MODEL_ID:
strncpy(info, &data->model_id[0], MIN(size, sizeof(data->model_id)));
break;
default:
ret = -ENODATA;
break;
}
return ret;
}
int cellular_get_signal(const struct device *dev, const enum cellular_signal_type type,
int16_t *value)
{
int ret;
struct modem_cellular_data *data = (struct modem_cellular_data *)dev->data;
const struct modem_cellular_config *config = (struct modem_cellular_config *)dev->config;
if (config->get_signal_chat_script == NULL) {
return -ENOTSUP;
}
if ((data->state != MODEM_CELLULAR_STATE_AWAIT_REGISTERED) &&
(data->state != MODEM_CELLULAR_STATE_CARRIER_ON)) {
return -ENODATA;
}
ret = modem_chat_run_script(&data->chat, config->get_signal_chat_script);
if (ret != 0) {
return ret;
}
switch (type) {
case CELLULAR_SIGNAL_RSSI: {
*value = data->rssi;
} break;
case CELLULAR_SIGNAL_RSRP:
return -ENOTSUP;
case CELLULAR_SIGNAL_RSRQ:
return -ENOTSUP;
}
return ret;
}

View file

@ -124,17 +124,13 @@ __subsystem struct cellular_driver_api {
* @retval -ENOTSUP if API is not supported by cellular network device.
* @retval Negative errno-code otherwise.
*/
__syscall int cellular_configure_networks(const struct device *dev,
const struct cellular_network *networks, uint8_t size);
static inline int z_impl_cellular_configure_networks(const struct device *dev,
const struct cellular_network *networks,
uint8_t size)
static inline int cellular_configure_networks(const struct device *dev,
const struct cellular_network *networks, uint8_t size)
{
const struct cellular_driver_api *api = (const struct cellular_driver_api *)dev->api;
if (api->configure_networks == NULL) {
return -ENOTSUP;
return -ENOSYS;
}
return api->configure_networks(dev, networks, size);
@ -151,18 +147,14 @@ static inline int z_impl_cellular_configure_networks(const struct device *dev,
* @retval -ENOTSUP if API is not supported by cellular network device.
* @retval Negative errno-code otherwise.
*/
__syscall int cellular_get_supported_networks(const struct device *dev,
const struct cellular_network **networks,
uint8_t *size);
static inline int z_impl_cellular_get_supported_networks(const struct device *dev,
const struct cellular_network **networks,
uint8_t *size)
static inline int cellular_get_supported_networks(const struct device *dev,
const struct cellular_network **networks,
uint8_t *size)
{
const struct cellular_driver_api *api = (const struct cellular_driver_api *)dev->api;
if (api->get_supported_networks == NULL) {
return -ENOTSUP;
return -ENOSYS;
}
return api->get_supported_networks(dev, networks, size);
@ -180,17 +172,13 @@ static inline int z_impl_cellular_get_supported_networks(const struct device *de
* @retval -ENODATA if device is not in a state where signal can be polled
* @retval Negative errno-code otherwise.
*/
__syscall int cellular_get_signal(const struct device *dev, const enum cellular_signal_type type,
int16_t *value);
static inline int z_impl_cellular_get_signal(const struct device *dev,
const enum cellular_signal_type type,
int16_t *value)
static inline int cellular_get_signal(const struct device *dev,
const enum cellular_signal_type type, int16_t *value)
{
const struct cellular_driver_api *api = (const struct cellular_driver_api *)dev->api;
if (api->get_signal == NULL) {
return -ENOTSUP;
return -ENOSYS;
}
return api->get_signal(dev, type, value);
@ -209,18 +197,14 @@ static inline int z_impl_cellular_get_signal(const struct device *dev,
* @retval -ENODATA if modem does not provide info requested
* @retval Negative errno-code from chat module otherwise.
*/
__syscall int cellular_get_modem_info(const struct device *dev,
const enum cellular_modem_info_type type,
char *info, size_t size);
static inline int z_impl_cellular_get_modem_info(const struct device *dev,
const enum cellular_modem_info_type type,
char *info, size_t size)
static inline int cellular_get_modem_info(const struct device *dev,
const enum cellular_modem_info_type type, char *info,
size_t size)
{
const struct cellular_driver_api *api = (const struct cellular_driver_api *)dev->api;
if (api->get_modem_info == NULL) {
return -ENOTSUP;
return -ENOSYS;
}
return api->get_modem_info(dev, type, info, size);
@ -234,6 +218,4 @@ static inline int z_impl_cellular_get_modem_info(const struct device *dev,
* @}
*/
#include <syscalls/cellular.h>
#endif /* ZEPHYR_INCLUDE_DRIVERS_CELLULAR_H_ */