Skip to content

drivers: modem: Added SMS receive callback #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ drivers/i2c/i2c_gecko.c merge=ours
drivers/modem/CMakeLists.txt merge=ours
drivers/modem/Kconfig merge=ours
drivers/modem/Kconfig.murata-1sc merge=ours
drivers/modem/Kconfig.sms merge=ours
drivers/modem/modem_context.h merge=ours
drivers/modem/modem_shell.c merge=ours
drivers/modem/modem_sms.c merge=ours
drivers/modem/modem_sms.h merge=ours
drivers/modem/modem_socket.c merge=ours
drivers/modem/modem_socket.h merge=ours
Expand Down Expand Up @@ -175,6 +177,7 @@ include/zephyr/drivers/bluetooth/rs9116w.h merge=ours
include/zephyr/drivers/fuel_gauge.h merge=ours
include/zephyr/drivers/fuel_gauge/act81461.h merge=ours
include/zephyr/drivers/modem/murata-1sc.h merge=ours
include/zephyr/drivers/modem/sms.h merge=ours
include/zephyr/drivers/rtc/gecko_rtcc.h merge=ours
include/zephyr/drivers/sensor.h merge=ours
include/zephyr/drivers/sensor/cxd5605.h merge=ours
Expand Down
1 change: 1 addition & 0 deletions drivers/modem/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ zephyr_library_sources_ifdef(CONFIG_MODEM_IFACE_UART_INTERRUPT modem_iface_uart_
zephyr_library_sources_ifdef(CONFIG_MODEM_IFACE_UART_ASYNC modem_iface_uart_async.c)
zephyr_library_sources_ifdef(CONFIG_MODEM_CMD_HANDLER modem_cmd_handler.c)
zephyr_library_sources_ifdef(CONFIG_MODEM_SOCKET modem_socket.c)
zephyr_library_sources_ifdef(CONFIG_MODEM_SMS modem_sms.c)

if(CONFIG_MODEM_UBLOX_SARA)
zephyr_library_include_directories(${ZEPHYR_BASE}/subsys/net/ip)
Expand Down
19 changes: 1 addition & 18 deletions drivers/modem/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -189,28 +189,11 @@ config MODEM_CELL_INFO
help
Query for numerical operator id, location area code and cell id.

config MODEM_SMS_IN_MSG_MAX_LEN
int "Maximum length of inbound SMS messages"
default 160
help
This is the maximum length of inbound (received) SMS messages.
Inbound SMS messages longer than this will be truncated.
The absolute maximum value may be network- and/or modem-dependent,
but a smaller value may be preferred in order to conserve memory.

config MODEM_SMS_OUT_MSG_MAX_LEN
int "Maximum length of outbound SMS messages"
default MODEM_SMS_IN_MSG_MAX_LEN
help
This is the maximum length of outbound SMS messages (being sent).
SMS message send requests longer than this are not possible.
The absolute maximum value may be network- and/or modem-dependent,
but a smaller value may be preferred in order to conserve memory.

source "drivers/modem/Kconfig.ublox-sara-r4"
source "drivers/modem/Kconfig.quectel-bg9x"
source "drivers/modem/Kconfig.wncm14a2a"
source "drivers/modem/Kconfig.gsm"
source "drivers/modem/Kconfig.sms"

source "drivers/modem/Kconfig.hl7800"
source "drivers/modem/Kconfig.simcom-sim7080"
Expand Down
1 change: 1 addition & 0 deletions drivers/modem/Kconfig.murata-1sc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ menuconfig MODEM_MURATA_1SC
select MODEM_CMD_HANDLER
select MODEM_IFACE_UART
select MODEM_SOCKET
select NET_SOCKETS
select NET_SOCKETS_OFFLOAD
select BASE64
imply GPIO
Expand Down
43 changes: 43 additions & 0 deletions drivers/modem/Kconfig.sms
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Modem SMS options

# Copyright (c) 2023 T-Moblie
# SPDX-License-Identifier: Apache-2.0

menuconfig MODEM_SMS
bool "Modem SMS handler"
default y
depends on MODEM_CONTEXT
help
Choose this setting to enable SMS functionality for
modems.

if MODEM_SMS

config MODEM_SMS_IN_MSG_MAX_LEN
int "Maximum length of inbound SMS messages"
default 160
help
This is the maximum length of inbound (received) SMS messages.
Inbound SMS messages longer than this will be truncated.
The absolute maximum value may be network- and/or modem-dependent,
but a smaller value may be preferred in order to conserve memory.

config MODEM_SMS_OUT_MSG_MAX_LEN
int "Maximum length of outbound SMS messages"
default MODEM_SMS_IN_MSG_MAX_LEN
help
This is the maximum length of outbound SMS messages (being sent).
SMS message send requests longer than this are not possible.
The absolute maximum value may be network- and/or modem-dependent,
but a smaller value may be preferred in order to conserve memory.

config MODEM_SMS_CALLBACK
bool "Use SMS Receive callbacks"
default n
help
This option allows a callback to be registered for SMS receive
events. SMS messages reported via the callback will not be
buffered, and therefore client code must handle buffering if
required.

endif # MODEM_SMS
9 changes: 8 additions & 1 deletion drivers/modem/modem_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#include <zephyr/net/net_ip.h>
#include <zephyr/sys/ring_buffer.h>
#include <zephyr/drivers/gpio.h>
#include "modem_sms.h"

#include <zephyr/drivers/modem/sms.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -74,9 +75,15 @@ struct modem_context {
/* command handler config */
struct modem_cmd_handler cmd_handler;

/* modem device pointer */
const struct device *dev;

#if defined(CONFIG_MODEM_SMS)
/* SMS functions */
send_sms_func_t send_sms;
recv_sms_func_t recv_sms;
recv_sms_cb_en_func_t recv_sms_cb_en;
#endif

/* driver data */
void *driver_data;
Expand Down
15 changes: 11 additions & 4 deletions drivers/modem/modem_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@

#define LOG_MODULE_NAME modem_shell

#include "modem_sms.h"

#include <zephyr/kernel.h>
#include <stdlib.h>
#include <string.h>
#include <zephyr/device.h>
#include <zephyr/shell/shell.h>
#include <zephyr/drivers/console/uart_mux.h>

#if defined(CONFIG_MODEM_SMS)
#include <zephyr/drivers/modem/sms.h>
#endif /* CONFIG_MODEM_SMS */

#include <zephyr/sys/printk.h>

struct modem_shell_user_data {
Expand Down Expand Up @@ -241,6 +243,7 @@ static int cmd_modem_info(const struct shell *shell, size_t argc, char *argv[])
return 0;
}

#if defined(CONFIG_MODEM_SMS)
static int cmd_modem_sms_send(const struct shell *shell, size_t argc,
char *argv[])
{
Expand Down Expand Up @@ -278,7 +281,7 @@ static int cmd_modem_sms_send(const struct shell *shell, size_t argc,
snprintk(sms.phone, sizeof(sms.phone), "%s", argv[2]);
snprintk(sms.msg, sizeof(sms.msg), "%s", argv[3]);

ret = ms_ctx->send_sms(ms_ctx, &sms);
ret = ms_ctx->send_sms(&sms);
if (ret == 0) {
shell_fprintf(shell, SHELL_NORMAL,
"SMS msg was sent\n");
Expand Down Expand Up @@ -321,7 +324,7 @@ static int cmd_modem_sms_recv(const struct shell *shell, size_t argc,
}

sms.timeout = K_SECONDS(wait);
ret = ms_ctx->recv_sms(ms_ctx, &sms);
ret = ms_ctx->recv_sms(&sms);
if (ret < 0) {
shell_fprintf(shell, SHELL_ERROR,
"recv_sms returned error %d, errno:%d\n",
Expand Down Expand Up @@ -350,6 +353,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(modem_cmd_sms,
cmd_modem_sms_recv),
SHELL_SUBCMD_SET_END);

#endif /* CONFIG_MODEM_SMS */

SHELL_STATIC_SUBCMD_SET_CREATE(
sub_modem,
SHELL_CMD(info, NULL, "Show information for a modem", cmd_modem_info),
Expand All @@ -358,8 +363,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
"Send an AT <command> to a registered modem "
"receiver",
cmd_modem_send),
#if defined(CONFIG_MODEM_SMS)
SHELL_CMD(sms, &modem_cmd_sms, "Send or receive SMS message via modem",
NULL),
#endif /* CONFIG_MODEM_SMS */
SHELL_SUBCMD_SET_END /* Array terminated. */
);

Expand Down
117 changes: 117 additions & 0 deletions drivers/modem/modem_sms.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2023 T-Mobile USA, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <zephyr/kernel.h>
#include <zephyr/drivers/modem/sms.h>

#include "modem_sms.h"
#include "modem_context.h"

static sys_slist_t sms_recv_cbs = SYS_SLIST_STATIC_INIT(&sms_recv_cbs);

void notify_sms_recv(const struct device *dev, struct sms_in *sms, int csms_ref, int csms_idx,
int csms_tot)
{
struct sms_recv_cb *cb, *next;

SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&sms_recv_cbs, cb, next, node) {
if (cb->recv) {
cb->recv(dev, sms, csms_ref, csms_idx, csms_tot);
}
}
}

static struct modem_context *modem_context_from_modem_dev(const struct device *dev)
{
struct modem_context *mctx;

for (int i = 0; i < CONFIG_MODEM_CONTEXT_MAX_NUM; i++) {
mctx = modem_context_from_id(i);
if (mctx && mctx->dev == dev) {
return mctx;
}
}
return 0;
}

int sms_msg_send(const struct device *dev, const struct sms_out *sms)
{
struct modem_context *mctx;

mctx = modem_context_from_modem_dev(dev);

if (!mctx) {
return -ENODEV;
}

if (!mctx->send_sms) {
return -ENOSYS;
}

return mctx->send_sms(sms);
}

int sms_msg_recv(const struct device *dev, struct sms_in *sms)
{
struct modem_context *mctx;

mctx = modem_context_from_modem_dev(dev);

if (!mctx) {
return -ENODEV;
}

if (!mctx->recv_sms) {
return -ENOSYS;
}

return mctx->recv_sms(sms);
}

#if defined(CONFIG_MODEM_SMS_CALLBACK)

int sms_recv_cb_en(const struct device *dev, bool enable)
{
struct modem_context *mctx;

mctx = modem_context_from_modem_dev(dev);

if (!mctx) {
return -ENODEV;
}

if (!mctx->recv_sms_cb_en) {
return -ENOSYS;
}

return mctx->recv_sms_cb_en(enable);
}

int sms_recv_cb_register(struct sms_recv_cb *cb)
{
if (cb == NULL) {
return -EINVAL;
}

sys_slist_append(&sms_recv_cbs, &cb->node);

return 0;
}

int sms_recv_cb_unregister(struct sms_recv_cb *cb)
{
if (cb == NULL) {
return -EINVAL;
}

if (!sys_slist_find_and_remove(&sms_recv_cbs, &cb->node)) {
return -EALREADY;
}

return 0;
}

#endif /* CONFIG_MODEM_SMS_CALLBACK */
54 changes: 15 additions & 39 deletions drivers/modem/modem_sms.h
Original file line number Diff line number Diff line change
@@ -1,47 +1,23 @@
/** @file
* @brief Modem SMS for SMS common structure.
*
* Modem SMS handling for modem driver.
*/

/*
* Copyright (c) 2022 T-Mobile USA, Inc.
* Copyright (c) 2023 T-Mobile USA, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SMS_H_
#define ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SMS_H_
#ifndef ZEPHYR_DRIVERS_MODEM_MODEM_SMS_H_
#define ZEPHYR_DRIVERS_MODEM_MODEM_SMS_H_

#include <zephyr/kernel.h>

#define SMS_PHONE_MAX_LEN 16
#define SMS_TIME_MAX_LEN 26

/*
* All fields in sms_out and sms_in are NULL terminated
/**
* @brief Notify all registered callbacks of a received SMS message
*
* @param dev Device pointer of modem which received the SMS message
* @param sms Received SMS message
* @param csms_ref CSMS Reference number (if available, value is set to -1 if not)
* @param csms_idx CSMS Index number (if available, value is set to 0 if not)
* @param csms_tot CSMS Total segment count (if available, value is set to 1 if not)
*
*/
void notify_sms_recv(const struct device *dev, struct sms_in *sms, int csms_ref, int csms_idx,
int csms_tot);

struct sms_out {
char phone[SMS_PHONE_MAX_LEN];
char msg[CONFIG_MODEM_SMS_OUT_MSG_MAX_LEN + 1];
};

struct sms_in {
char phone[SMS_PHONE_MAX_LEN];
char time[SMS_TIME_MAX_LEN];
char msg[CONFIG_MODEM_SMS_IN_MSG_MAX_LEN + 1];
uint8_t csms_ref;
uint8_t csms_idx;
k_timeout_t timeout;
};

typedef int (*send_sms_func_t)(void *obj, const struct sms_out *sms);
typedef int (*recv_sms_func_t)(void *obj, struct sms_in *sms);

enum io_ctl {
SMS_SEND,
SMS_RECV,
};

#endif /* ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SMS_H_ */
#endif /* ZEPHYR_DRIVERS_MODEM_MODEM_SMS_H_ */
Loading