Skip to content

Commit f1d4dce

Browse files
committed
app: Allow targeting responses to a pipe
* Define rsp_send_to() that takes a pipe pointer * Also add pipe pointer to data_send(). * Refactor socket's to use the pipe they are created on. * Refactor MQTT to store the pipe pointer as well. Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
1 parent 83dfb85 commit f1d4dce

4 files changed

Lines changed: 59 additions & 36 deletions

File tree

app/src/sm_at_host.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,9 +1401,8 @@ void rsp_send_error(void)
14011401
rsp_send(ERROR_STR);
14021402
}
14031403

1404-
static void rsp_send_internal(bool urc, const char *fmt, va_list arg_ptr)
1404+
static void rsp_send_internal(struct sm_at_host_ctx *ctx, bool urc, const char *fmt, va_list arg_ptr)
14051405
{
1406-
struct sm_at_host_ctx *ctx = urc ? sm_at_host_get_urc_ctx() : sm_at_host_get_current();
14071406
static K_MUTEX_DEFINE(mutex_rsp_buf);
14081407
static char rsp_buf[SM_AT_MAX_RSP_LEN];
14091408
int rsp_len;
@@ -1418,27 +1417,40 @@ static void rsp_send_internal(bool urc, const char *fmt, va_list arg_ptr)
14181417
k_mutex_unlock(&mutex_rsp_buf);
14191418
}
14201419

1420+
void rsp_send_to(struct modem_pipe *pipe, const char *fmt, ...)
1421+
{
1422+
struct sm_at_host_ctx *ctx = sm_at_host_get_ctx_from(pipe);
1423+
va_list arg_ptr;
1424+
1425+
va_start(arg_ptr, fmt);
1426+
rsp_send_internal(ctx, false, fmt, arg_ptr);
1427+
va_end(arg_ptr);
1428+
}
1429+
1430+
14211431
void rsp_send(const char *fmt, ...)
14221432
{
1433+
struct sm_at_host_ctx *ctx = sm_at_host_get_current();
14231434
va_list arg_ptr;
14241435

14251436
va_start(arg_ptr, fmt);
1426-
rsp_send_internal(false, fmt, arg_ptr);
1437+
rsp_send_internal(ctx, false, fmt, arg_ptr);
14271438
va_end(arg_ptr);
14281439
}
14291440

14301441
void urc_send(const char *fmt, ...)
14311442
{
1443+
struct sm_at_host_ctx *ctx = sm_at_host_get_urc_ctx();
14321444
va_list arg_ptr;
14331445

14341446
va_start(arg_ptr, fmt);
1435-
rsp_send_internal(true, fmt, arg_ptr);
1447+
rsp_send_internal(ctx, true, fmt, arg_ptr);
14361448
va_end(arg_ptr);
14371449
}
14381450

1439-
void data_send(const uint8_t *data, size_t len)
1451+
void data_send(struct modem_pipe *pipe, const uint8_t *data, size_t len)
14401452
{
1441-
struct sm_at_host_ctx *ctx = sm_at_host_get_current();
1453+
struct sm_at_host_ctx *ctx = sm_at_host_get_ctx_from(pipe);
14421454

14431455
sm_at_send_internal(ctx, data, len, false, SM_DEBUG_PRINT_SHORT);
14441456
}

app/src/sm_at_host.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ void sm_at_host_uninit(void);
8383
*/
8484
void rsp_send(const char *fmt, ...);
8585

86+
/**
87+
* @brief Send AT command response
88+
*
89+
* @param fmt Response message format string
90+
*
91+
*/
92+
void rsp_send_to(struct modem_pipe *pipe, const char *fmt, ...);
93+
8694
/**
8795
* @brief Send URC message
8896
*
@@ -106,11 +114,12 @@ void rsp_send_error(void);
106114
/**
107115
* @brief Send raw data received in data mode
108116
*
117+
* @param pipe Modem pipe to send data through
109118
* @param data Raw data received
110119
* @param len Length of raw data
111120
*
112121
*/
113-
void data_send(const uint8_t *data, size_t len);
122+
void data_send(struct modem_pipe *pipe, const uint8_t *data, size_t len);
114123

115124
/**
116125
* @brief Request Serial Modem AT host to enter data mode

app/src/sm_at_mqtt.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static struct sm_mqtt_ctx {
4646
struct sockaddr_in broker;
4747
struct sockaddr_in6 broker6;
4848
};
49+
struct modem_pipe *pipe;
4950
} ctx;
5051

5152
static char mqtt_broker_url[SM_MAX_URL + 1];
@@ -95,20 +96,20 @@ static int handle_mqtt_publish_evt(struct mqtt_client *const c, const struct mqt
9596
/* MQTT client does not track the packet identifiers, so MQTT_QOS_2_EXACTLY_ONCE
9697
* promise is not kept. This deviates from MQTT v3.1.1.
9798
*/
98-
rsp_send("\r\n#XMQTTMSG: %d,%d\r\n",
99+
rsp_send_to(ctx.pipe, "\r\n#XMQTTMSG: %d,%d\r\n",
99100
evt->param.publish.message.topic.topic.size,
100101
evt->param.publish.message.payload.len);
101-
data_send(evt->param.publish.message.topic.topic.utf8,
102+
data_send(ctx.pipe, evt->param.publish.message.topic.topic.utf8,
102103
evt->param.publish.message.topic.topic.size);
103-
data_send("\r\n", 2);
104+
data_send(ctx.pipe, "\r\n", 2);
104105
do {
105106
ret = mqtt_read_publish_payload_blocking(c, sm_data_buf, sizeof(sm_data_buf));
106107
if (ret > 0) {
107-
data_send(sm_data_buf, ret);
108+
data_send(ctx.pipe, sm_data_buf, ret);
108109
size_read += ret;
109110
}
110111
} while (ret >= 0 && size_read < evt->param.publish.message.payload.len);
111-
data_send("\r\n", 2);
112+
data_send(ctx.pipe, "\r\n", 2);
112113

113114

114115
return 0;
@@ -207,7 +208,7 @@ void mqtt_evt_handler(struct mqtt_client *const c, const struct mqtt_evt *evt)
207208
break;
208209
}
209210

210-
rsp_send("\r\n#XMQTTEVT: %d,%d\r\n", evt->type, ret);
211+
rsp_send_to(ctx.pipe, "\r\n#XMQTTEVT: %d,%d\r\n", evt->type, ret);
211212
}
212213

213214
static void mqtt_thread_fn(void *arg1, void *arg2, void *arg3)
@@ -367,6 +368,8 @@ static int do_mqtt_connect(void)
367368
return -EISCONN;
368369
}
369370

371+
ctx.pipe = sm_at_host_get_current_pipe();
372+
370373
/* Init MQTT broker */
371374
err = broker_init();
372375
if (err) {

app/src/sm_at_socket.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static struct sm_socket *find_avail_socket(void)
145145

146146
return NULL;
147147
}
148+
148149
static struct async_poll_ctx *poll_ctx_from_sock(struct sm_socket *sock)
149150
{
150151
if (sock == NULL) {
@@ -248,7 +249,7 @@ static void auto_reception(struct sm_socket *sock)
248249
}
249250
if (!in_datamode(sock->pipe)) {
250251
/* <CR><LF> after the data. */
251-
rsp_send("\r\n");
252+
rsp_send_to(sock->pipe, "\r\n");
252253
}
253254
}
254255

@@ -360,12 +361,7 @@ void sm_at_socket_poll_work_handler(struct k_work *work)
360361
/* Do not send URC for the same events twice, unless send/recv is done. */
361362
sock->async_poll.xapoll_events &= ~xapoll_events;
362363
if (xapoll_events) {
363-
/* TODO: If we are in sm_work_q context, and not an idle handler
364-
* then this "pipe" might be the wrong one.
365-
* rsp_send() goes into "current" pipe, not the one associated
366-
* with this poll_ctx.
367-
*/
368-
rsp_send("\r\n#XAPOLL: %d,%d\r\n", sock->fd, xapoll_events);
364+
rsp_send_to(pipe, "\r\n#XAPOLL: %d,%d\r\n", sock->fd, xapoll_events);
369365
}
370366
}
371367

@@ -459,12 +455,8 @@ static void send_cb(const struct nrf_modem_sendcb_params *params)
459455
sock->send_ntf.bytes_sent = params->bytes_sent;
460456
atomic_set(&sock->send_ntf.ready, 1);
461457

462-
if (sock->pipe) {
463-
/* Schedule work to execute in AT context */
464-
sm_at_host_queue_idle_work(sock->pipe, &work);
465-
} else {
466-
k_work_submit_to_queue(&sm_work_q, &work);
467-
}
458+
/* Schedule work to execute in AT context */
459+
sm_at_host_queue_idle_work(sock->pipe, &work);
468460
}
469461

470462
static int set_so_send_cb(struct sm_socket *socket)
@@ -1052,7 +1044,7 @@ static int do_send(struct sm_socket *sock, const uint8_t *data, int len, int fla
10521044
sent += ret;
10531045
}
10541046

1055-
if (!in_datamode(sock->pipe)) {
1047+
if (!in_datamode(sm_at_host_get_current())) {
10561048
rsp_send("\r\n#XSEND: %d,%d,%d\r\n", sock->fd,
10571049
send_ntf ? AT_SOCKET_SEND_RESULT_NW_ACK_URC
10581050
: AT_SOCKET_SEND_RESULT_DEFAULT,
@@ -1080,7 +1072,7 @@ static int data_send_hex(struct sm_socket *sock, const uint8_t *buf, int recv_le
10801072
LOG_ERR("Failed to convert binary data to hex string");
10811073
return -EINVAL;
10821074
}
1083-
data_send(hex_buf, size);
1075+
data_send(sock->pipe, hex_buf, size);
10841076
consumed += size / 2; /* size is in hex string length */
10851077
if (recv_len - consumed < data_len) {
10861078
data_len = recv_len - consumed;
@@ -1117,7 +1109,7 @@ static int do_recv(struct sm_socket *sock, int timeout, int flags,
11171109
LOG_WRN("nrf_recv() return 0");
11181110
} else {
11191111
if (!in_datamode(sock->pipe)) {
1120-
rsp_send("\r\n#XRECV: %d,%d,%d\r\n", sock->fd, mode, ret);
1112+
rsp_send_to(sock->pipe, "\r\n#XRECV: %d,%d,%d\r\n", sock->fd, mode, ret);
11211113
}
11221114

11231115
if (mode == AT_SOCKET_MODE_HEX) {
@@ -1126,7 +1118,7 @@ static int do_recv(struct sm_socket *sock, int timeout, int flags,
11261118
return ret;
11271119
}
11281120
} else {
1129-
data_send(sm_data_buf, ret);
1121+
data_send(sock->pipe, sm_data_buf, ret);
11301122
}
11311123
ret = 0;
11321124

@@ -1189,7 +1181,7 @@ static int do_sendto(struct sm_socket *sock, const char *url, uint16_t port, con
11891181
}
11901182

11911183
if (!in_datamode(sock->pipe)) {
1192-
rsp_send("\r\n#XSENDTO: %d,%d,%d\r\n", sock->fd,
1184+
rsp_send_to(sock->pipe, "\r\n#XSENDTO: %d,%d,%d\r\n", sock->fd,
11931185
send_ntf ? AT_SOCKET_SEND_RESULT_NW_ACK_URC
11941186
: AT_SOCKET_SEND_RESULT_DEFAULT,
11951187
sent);
@@ -1233,7 +1225,7 @@ static int do_recvfrom(struct sm_socket *sock, int timeout, int flags,
12331225
uint16_t peer_port = 0;
12341226

12351227
util_get_peer_addr(&remote, peer_addr, &peer_port);
1236-
rsp_send("\r\n#XRECVFROM: %d,%d,%d,\"%s\",%d\r\n", sock->fd, mode,
1228+
rsp_send_to(sock->pipe, "\r\n#XRECVFROM: %d,%d,%d,\"%s\",%d\r\n", sock->fd, mode,
12371229
ret, peer_addr, peer_port);
12381230
}
12391231

@@ -1243,7 +1235,7 @@ static int do_recvfrom(struct sm_socket *sock, int timeout, int flags,
12431235
return ret;
12441236
}
12451237
} else {
1246-
data_send(sm_data_buf, ret);
1238+
data_send(sock->pipe, sm_data_buf, ret);
12471239
}
12481240

12491241
update_poll_events(sock, NRF_POLLIN, true);
@@ -1709,6 +1701,7 @@ STATIC int handle_at_connect(enum at_parser_cmd_type cmd_type, struct at_parser
17091701
if (err) {
17101702
return err;
17111703
}
1704+
sock->pipe = sm_at_host_get_current_pipe();
17121705
err = do_connect(sock, url, port);
17131706
break;
17141707

@@ -1749,6 +1742,7 @@ STATIC int handle_at_send(enum at_parser_cmd_type cmd_type, struct at_parser *pa
17491742
if (err) {
17501743
return err;
17511744
}
1745+
sock->pipe = sm_at_host_get_current_pipe();
17521746
if (mode == AT_SOCKET_MODE_UNFORMATTED || mode == AT_SOCKET_MODE_HEX) {
17531747
if (param_count > 4) {
17541748
err = at_parser_string_ptr_get(parser, 4, &str_ptr, &size);
@@ -1846,6 +1840,7 @@ STATIC int handle_at_recv(enum at_parser_cmd_type cmd_type, struct at_parser *pa
18461840
return -ENOBUFS;
18471841
}
18481842
}
1843+
sock->pipe = sm_at_host_get_current_pipe();
18491844
err = do_recv(sock, timeout, flags, mode, data_len);
18501845
break;
18511846

@@ -1896,6 +1891,7 @@ STATIC int handle_at_sendto(enum at_parser_cmd_type cmd_type, struct at_parser *
18961891
if (err) {
18971892
return err;
18981893
}
1894+
sock->pipe = sm_at_host_get_current_pipe();
18991895
if (mode == AT_SOCKET_MODE_UNFORMATTED || mode == AT_SOCKET_MODE_HEX) {
19001896
if (param_count > 6) {
19011897
err = at_parser_string_ptr_get(parser, 6, &str_ptr, &size);
@@ -1995,6 +1991,7 @@ STATIC int handle_at_recvfrom(enum at_parser_cmd_type cmd_type, struct at_parser
19951991
return -ENOBUFS;
19961992
}
19971993
}
1994+
sock->pipe = sm_at_host_get_current_pipe();
19981995
err = do_recvfrom(sock, timeout, flags, mode, data_len);
19991996
break;
20001997

@@ -2123,17 +2120,18 @@ static void xapoll_read_response(void)
21232120
static int set_xapoll_events(struct sm_socket *sock, uint8_t events)
21242121
{
21252122
int ret;
2123+
struct modem_pipe *pipe = sm_at_host_get_current_pipe();
2124+
struct async_poll_ctx *poll_ctx =
2125+
sm_at_host_get_async_poll_ctx(pipe);
21262126

21272127
if (sock) {
2128+
sock->pipe = pipe;
21282129
/* Set events for a specific socket. */
21292130
sock->async_poll.xapoll_events_requested = events;
21302131
return update_poll_events(sock, events, true);
21312132
}
21322133

21332134
/* Set events for all sockets in this context */
2134-
struct modem_pipe *pipe = sm_at_host_get_current_pipe();
2135-
struct async_poll_ctx *poll_ctx =
2136-
sm_at_host_get_async_poll_ctx(pipe);
21372135
poll_ctx->xapoll_events_requested = events;
21382136
for (int i = 0; i < SM_MAX_SOCKET_COUNT; i++) {
21392137
if (socks[i].fd != INVALID_SOCKET && socks[i].pipe == pipe) {
@@ -2258,6 +2256,7 @@ STATIC int handle_at_recvcfg(enum at_parser_cmd_type cmd_type, struct at_parser
22582256
return -EINVAL;
22592257
}
22602258
if (sock) {
2259+
sock->pipe = pipe;
22612260
sock->async_poll.adr_flags = flags;
22622261
sock->async_poll.adr_hex = hex_mode != 0;
22632262
err = update_poll_events(sock, NRF_POLLIN, false);

0 commit comments

Comments
 (0)