Skip to content

Commit cb63b66

Browse files
authored
drivers: modem: Added SMS receive callback (#145)
Improved SMS interface, and added funcitonality to support an SMS receive callback. Signed-off-by: Jared Baumann <[email protected]>
1 parent 920aeea commit cb63b66

File tree

11 files changed

+1339
-892
lines changed

11 files changed

+1339
-892
lines changed

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ drivers/i2c/i2c_gecko.c merge=ours
109109
drivers/modem/CMakeLists.txt merge=ours
110110
drivers/modem/Kconfig merge=ours
111111
drivers/modem/Kconfig.murata-1sc merge=ours
112+
drivers/modem/Kconfig.sms merge=ours
112113
drivers/modem/modem_context.h merge=ours
113114
drivers/modem/modem_shell.c merge=ours
115+
drivers/modem/modem_sms.c merge=ours
114116
drivers/modem/modem_sms.h merge=ours
115117
drivers/modem/modem_socket.c merge=ours
116118
drivers/modem/modem_socket.h merge=ours
@@ -175,6 +177,7 @@ include/zephyr/drivers/bluetooth/rs9116w.h merge=ours
175177
include/zephyr/drivers/fuel_gauge.h merge=ours
176178
include/zephyr/drivers/fuel_gauge/act81461.h merge=ours
177179
include/zephyr/drivers/modem/murata-1sc.h merge=ours
180+
include/zephyr/drivers/modem/sms.h merge=ours
178181
include/zephyr/drivers/rtc/gecko_rtcc.h merge=ours
179182
include/zephyr/drivers/sensor.h merge=ours
180183
include/zephyr/drivers/sensor/cxd5605.h merge=ours

drivers/modem/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources_ifdef(CONFIG_MODEM_IFACE_UART_INTERRUPT modem_iface_uart_
1111
zephyr_library_sources_ifdef(CONFIG_MODEM_IFACE_UART_ASYNC modem_iface_uart_async.c)
1212
zephyr_library_sources_ifdef(CONFIG_MODEM_CMD_HANDLER modem_cmd_handler.c)
1313
zephyr_library_sources_ifdef(CONFIG_MODEM_SOCKET modem_socket.c)
14+
zephyr_library_sources_ifdef(CONFIG_MODEM_SMS modem_sms.c)
1415

1516
if(CONFIG_MODEM_UBLOX_SARA)
1617
zephyr_library_include_directories(${ZEPHYR_BASE}/subsys/net/ip)

drivers/modem/Kconfig

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,28 +189,11 @@ config MODEM_CELL_INFO
189189
help
190190
Query for numerical operator id, location area code and cell id.
191191

192-
config MODEM_SMS_IN_MSG_MAX_LEN
193-
int "Maximum length of inbound SMS messages"
194-
default 160
195-
help
196-
This is the maximum length of inbound (received) SMS messages.
197-
Inbound SMS messages longer than this will be truncated.
198-
The absolute maximum value may be network- and/or modem-dependent,
199-
but a smaller value may be preferred in order to conserve memory.
200-
201-
config MODEM_SMS_OUT_MSG_MAX_LEN
202-
int "Maximum length of outbound SMS messages"
203-
default MODEM_SMS_IN_MSG_MAX_LEN
204-
help
205-
This is the maximum length of outbound SMS messages (being sent).
206-
SMS message send requests longer than this are not possible.
207-
The absolute maximum value may be network- and/or modem-dependent,
208-
but a smaller value may be preferred in order to conserve memory.
209-
210192
source "drivers/modem/Kconfig.ublox-sara-r4"
211193
source "drivers/modem/Kconfig.quectel-bg9x"
212194
source "drivers/modem/Kconfig.wncm14a2a"
213195
source "drivers/modem/Kconfig.gsm"
196+
source "drivers/modem/Kconfig.sms"
214197

215198
source "drivers/modem/Kconfig.hl7800"
216199
source "drivers/modem/Kconfig.simcom-sim7080"

drivers/modem/Kconfig.murata-1sc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ menuconfig MODEM_MURATA_1SC
99
select MODEM_CMD_HANDLER
1010
select MODEM_IFACE_UART
1111
select MODEM_SOCKET
12+
select NET_SOCKETS
1213
select NET_SOCKETS_OFFLOAD
1314
select BASE64
1415
imply GPIO

drivers/modem/Kconfig.sms

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Modem SMS options
2+
3+
# Copyright (c) 2023 T-Moblie
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
menuconfig MODEM_SMS
7+
bool "Modem SMS handler"
8+
default y
9+
depends on MODEM_CONTEXT
10+
help
11+
Choose this setting to enable SMS functionality for
12+
modems.
13+
14+
if MODEM_SMS
15+
16+
config MODEM_SMS_IN_MSG_MAX_LEN
17+
int "Maximum length of inbound SMS messages"
18+
default 160
19+
help
20+
This is the maximum length of inbound (received) SMS messages.
21+
Inbound SMS messages longer than this will be truncated.
22+
The absolute maximum value may be network- and/or modem-dependent,
23+
but a smaller value may be preferred in order to conserve memory.
24+
25+
config MODEM_SMS_OUT_MSG_MAX_LEN
26+
int "Maximum length of outbound SMS messages"
27+
default MODEM_SMS_IN_MSG_MAX_LEN
28+
help
29+
This is the maximum length of outbound SMS messages (being sent).
30+
SMS message send requests longer than this are not possible.
31+
The absolute maximum value may be network- and/or modem-dependent,
32+
but a smaller value may be preferred in order to conserve memory.
33+
34+
config MODEM_SMS_CALLBACK
35+
bool "Use SMS Receive callbacks"
36+
default n
37+
help
38+
This option allows a callback to be registered for SMS receive
39+
events. SMS messages reported via the callback will not be
40+
buffered, and therefore client code must handle buffering if
41+
required.
42+
43+
endif # MODEM_SMS

drivers/modem/modem_context.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
#include <zephyr/net/net_ip.h>
2121
#include <zephyr/sys/ring_buffer.h>
2222
#include <zephyr/drivers/gpio.h>
23-
#include "modem_sms.h"
23+
24+
#include <zephyr/drivers/modem/sms.h>
2425

2526
#ifdef __cplusplus
2627
extern "C" {
@@ -74,9 +75,15 @@ struct modem_context {
7475
/* command handler config */
7576
struct modem_cmd_handler cmd_handler;
7677

78+
/* modem device pointer */
79+
const struct device *dev;
80+
81+
#if defined(CONFIG_MODEM_SMS)
7782
/* SMS functions */
7883
send_sms_func_t send_sms;
7984
recv_sms_func_t recv_sms;
85+
recv_sms_cb_en_func_t recv_sms_cb_en;
86+
#endif
8087

8188
/* driver data */
8289
void *driver_data;

drivers/modem/modem_shell.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313

1414
#define LOG_MODULE_NAME modem_shell
1515

16-
#include "modem_sms.h"
17-
1816
#include <zephyr/kernel.h>
1917
#include <stdlib.h>
2018
#include <string.h>
2119
#include <zephyr/device.h>
2220
#include <zephyr/shell/shell.h>
2321
#include <zephyr/drivers/console/uart_mux.h>
2422

23+
#if defined(CONFIG_MODEM_SMS)
24+
#include <zephyr/drivers/modem/sms.h>
25+
#endif /* CONFIG_MODEM_SMS */
26+
2527
#include <zephyr/sys/printk.h>
2628

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

246+
#if defined(CONFIG_MODEM_SMS)
244247
static int cmd_modem_sms_send(const struct shell *shell, size_t argc,
245248
char *argv[])
246249
{
@@ -278,7 +281,7 @@ static int cmd_modem_sms_send(const struct shell *shell, size_t argc,
278281
snprintk(sms.phone, sizeof(sms.phone), "%s", argv[2]);
279282
snprintk(sms.msg, sizeof(sms.msg), "%s", argv[3]);
280283

281-
ret = ms_ctx->send_sms(ms_ctx, &sms);
284+
ret = ms_ctx->send_sms(&sms);
282285
if (ret == 0) {
283286
shell_fprintf(shell, SHELL_NORMAL,
284287
"SMS msg was sent\n");
@@ -321,7 +324,7 @@ static int cmd_modem_sms_recv(const struct shell *shell, size_t argc,
321324
}
322325

323326
sms.timeout = K_SECONDS(wait);
324-
ret = ms_ctx->recv_sms(ms_ctx, &sms);
327+
ret = ms_ctx->recv_sms(&sms);
325328
if (ret < 0) {
326329
shell_fprintf(shell, SHELL_ERROR,
327330
"recv_sms returned error %d, errno:%d\n",
@@ -350,6 +353,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(modem_cmd_sms,
350353
cmd_modem_sms_recv),
351354
SHELL_SUBCMD_SET_END);
352355

356+
#endif /* CONFIG_MODEM_SMS */
357+
353358
SHELL_STATIC_SUBCMD_SET_CREATE(
354359
sub_modem,
355360
SHELL_CMD(info, NULL, "Show information for a modem", cmd_modem_info),
@@ -358,8 +363,10 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
358363
"Send an AT <command> to a registered modem "
359364
"receiver",
360365
cmd_modem_send),
366+
#if defined(CONFIG_MODEM_SMS)
361367
SHELL_CMD(sms, &modem_cmd_sms, "Send or receive SMS message via modem",
362368
NULL),
369+
#endif /* CONFIG_MODEM_SMS */
363370
SHELL_SUBCMD_SET_END /* Array terminated. */
364371
);
365372

drivers/modem/modem_sms.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2023 T-Mobile USA, Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/drivers/modem/sms.h>
9+
10+
#include "modem_sms.h"
11+
#include "modem_context.h"
12+
13+
static sys_slist_t sms_recv_cbs = SYS_SLIST_STATIC_INIT(&sms_recv_cbs);
14+
15+
void notify_sms_recv(const struct device *dev, struct sms_in *sms, int csms_ref, int csms_idx,
16+
int csms_tot)
17+
{
18+
struct sms_recv_cb *cb, *next;
19+
20+
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&sms_recv_cbs, cb, next, node) {
21+
if (cb->recv) {
22+
cb->recv(dev, sms, csms_ref, csms_idx, csms_tot);
23+
}
24+
}
25+
}
26+
27+
static struct modem_context *modem_context_from_modem_dev(const struct device *dev)
28+
{
29+
struct modem_context *mctx;
30+
31+
for (int i = 0; i < CONFIG_MODEM_CONTEXT_MAX_NUM; i++) {
32+
mctx = modem_context_from_id(i);
33+
if (mctx && mctx->dev == dev) {
34+
return mctx;
35+
}
36+
}
37+
return 0;
38+
}
39+
40+
int sms_msg_send(const struct device *dev, const struct sms_out *sms)
41+
{
42+
struct modem_context *mctx;
43+
44+
mctx = modem_context_from_modem_dev(dev);
45+
46+
if (!mctx) {
47+
return -ENODEV;
48+
}
49+
50+
if (!mctx->send_sms) {
51+
return -ENOSYS;
52+
}
53+
54+
return mctx->send_sms(sms);
55+
}
56+
57+
int sms_msg_recv(const struct device *dev, struct sms_in *sms)
58+
{
59+
struct modem_context *mctx;
60+
61+
mctx = modem_context_from_modem_dev(dev);
62+
63+
if (!mctx) {
64+
return -ENODEV;
65+
}
66+
67+
if (!mctx->recv_sms) {
68+
return -ENOSYS;
69+
}
70+
71+
return mctx->recv_sms(sms);
72+
}
73+
74+
#if defined(CONFIG_MODEM_SMS_CALLBACK)
75+
76+
int sms_recv_cb_en(const struct device *dev, bool enable)
77+
{
78+
struct modem_context *mctx;
79+
80+
mctx = modem_context_from_modem_dev(dev);
81+
82+
if (!mctx) {
83+
return -ENODEV;
84+
}
85+
86+
if (!mctx->recv_sms_cb_en) {
87+
return -ENOSYS;
88+
}
89+
90+
return mctx->recv_sms_cb_en(enable);
91+
}
92+
93+
int sms_recv_cb_register(struct sms_recv_cb *cb)
94+
{
95+
if (cb == NULL) {
96+
return -EINVAL;
97+
}
98+
99+
sys_slist_append(&sms_recv_cbs, &cb->node);
100+
101+
return 0;
102+
}
103+
104+
int sms_recv_cb_unregister(struct sms_recv_cb *cb)
105+
{
106+
if (cb == NULL) {
107+
return -EINVAL;
108+
}
109+
110+
if (!sys_slist_find_and_remove(&sms_recv_cbs, &cb->node)) {
111+
return -EALREADY;
112+
}
113+
114+
return 0;
115+
}
116+
117+
#endif /* CONFIG_MODEM_SMS_CALLBACK */

drivers/modem/modem_sms.h

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,23 @@
1-
/** @file
2-
* @brief Modem SMS for SMS common structure.
3-
*
4-
* Modem SMS handling for modem driver.
5-
*/
6-
71
/*
8-
* Copyright (c) 2022 T-Mobile USA, Inc.
2+
* Copyright (c) 2023 T-Mobile USA, Inc.
93
*
104
* SPDX-License-Identifier: Apache-2.0
115
*/
126

13-
#ifndef ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SMS_H_
14-
#define ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SMS_H_
7+
#ifndef ZEPHYR_DRIVERS_MODEM_MODEM_SMS_H_
8+
#define ZEPHYR_DRIVERS_MODEM_MODEM_SMS_H_
159

16-
#include <zephyr/kernel.h>
17-
18-
#define SMS_PHONE_MAX_LEN 16
19-
#define SMS_TIME_MAX_LEN 26
20-
21-
/*
22-
* All fields in sms_out and sms_in are NULL terminated
10+
/**
11+
* @brief Notify all registered callbacks of a received SMS message
12+
*
13+
* @param dev Device pointer of modem which received the SMS message
14+
* @param sms Received SMS message
15+
* @param csms_ref CSMS Reference number (if available, value is set to -1 if not)
16+
* @param csms_idx CSMS Index number (if available, value is set to 0 if not)
17+
* @param csms_tot CSMS Total segment count (if available, value is set to 1 if not)
18+
*
2319
*/
20+
void notify_sms_recv(const struct device *dev, struct sms_in *sms, int csms_ref, int csms_idx,
21+
int csms_tot);
2422

25-
struct sms_out {
26-
char phone[SMS_PHONE_MAX_LEN];
27-
char msg[CONFIG_MODEM_SMS_OUT_MSG_MAX_LEN + 1];
28-
};
29-
30-
struct sms_in {
31-
char phone[SMS_PHONE_MAX_LEN];
32-
char time[SMS_TIME_MAX_LEN];
33-
char msg[CONFIG_MODEM_SMS_IN_MSG_MAX_LEN + 1];
34-
uint8_t csms_ref;
35-
uint8_t csms_idx;
36-
k_timeout_t timeout;
37-
};
38-
39-
typedef int (*send_sms_func_t)(void *obj, const struct sms_out *sms);
40-
typedef int (*recv_sms_func_t)(void *obj, struct sms_in *sms);
41-
42-
enum io_ctl {
43-
SMS_SEND,
44-
SMS_RECV,
45-
};
46-
47-
#endif /* ZEPHYR_INCLUDE_DRIVERS_MODEM_MODEM_SMS_H_ */
23+
#endif /* ZEPHYR_DRIVERS_MODEM_MODEM_SMS_H_ */

0 commit comments

Comments
 (0)