Skip to content

Commit

Permalink
applications: serial_lte_modem: Switch to zsock_* API
Browse files Browse the repository at this point in the history
Use zsock_* socket functions and related symbols instead of POSIX ones
to avoid dependency on the POSIX subsystem.

Signed-off-by: Andrzej Głąbek <[email protected]>
  • Loading branch information
anangl committed Jan 21, 2025
1 parent dcd9488 commit 6e66f80
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 248 deletions.
44 changes: 24 additions & 20 deletions applications/serial_lte_modem/src/http_c/slm_at_httpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static int headers_cb(int sock, struct http_request *req, void *user_data)

len = strlen(httpc.headers);
while (offset < len) {
ret = send(sock, httpc.headers + offset, len - offset, 0);
ret = zsock_send(sock, httpc.headers + offset, len - offset, 0);
if (ret < 0) {
LOG_ERR("send header fail: %d", -errno);
return -errno;
Expand All @@ -153,7 +153,7 @@ int do_send_payload(const uint8_t *data, int len)

/* Start to send payload */
while (offset < len) {
ret = send(httpc.fd, data + offset, len - offset, 0);
ret = zsock_send(httpc.fd, data + offset, len - offset, 0);
if (ret < 0) {
LOG_ERR("Fail to send payload: %d, sent: %d", ret, offset);
httpc.total_sent = -errno;
Expand Down Expand Up @@ -223,9 +223,9 @@ static int do_http_connect(void)

/* Open socket */
if (httpc.sec_tag == INVALID_SEC_TAG) {
ret = socket(httpc.family, SOCK_STREAM, IPPROTO_TCP);
ret = zsock_socket(httpc.family, SOCK_STREAM, IPPROTO_TCP);
} else {
ret = socket(httpc.family, SOCK_STREAM, IPPROTO_TLS_1_2);
ret = zsock_socket(httpc.family, SOCK_STREAM, IPPROTO_TLS_1_2);
}
if (ret < 0) {
LOG_ERR("socket() failed: %d", -errno);
Expand All @@ -244,33 +244,35 @@ static int do_http_connect(void)
int tls_native = 1;

/* Must be the first socket option to set. */
ret = setsockopt(httpc.fd, SOL_TLS, TLS_NATIVE, &tls_native, sizeof(tls_native));
ret = zsock_setsockopt(httpc.fd, SOL_TLS, TLS_NATIVE,
&tls_native, sizeof(tls_native));
if (ret) {
ret = errno;
goto exit_cli;
}
#endif
sec_tag_t sec_tag_list[] = { httpc.sec_tag };

ret = setsockopt(httpc.fd, SOL_TLS, TLS_SEC_TAG_LIST, sec_tag_list,
sizeof(sec_tag_t));
ret = zsock_setsockopt(httpc.fd, SOL_TLS, TLS_SEC_TAG_LIST,
sec_tag_list, sizeof(sec_tag_t));
if (ret) {
LOG_ERR("setsockopt(TLS_SEC_TAG_LIST) error: %d", -errno);
ret = -errno;
goto exit_cli;
}
ret = setsockopt(httpc.fd, SOL_TLS, TLS_PEER_VERIFY, &httpc.peer_verify,
sizeof(httpc.peer_verify));
ret = zsock_setsockopt(httpc.fd, SOL_TLS, TLS_PEER_VERIFY,
&httpc.peer_verify, sizeof(httpc.peer_verify));
if (ret) {
LOG_ERR("setsockopt(TLS_PEER_VERIFY) error: %d", -errno);
ret = -errno;
goto exit_cli;
}
if (httpc.hostname_verify) {
ret = setsockopt(httpc.fd, SOL_TLS, TLS_HOSTNAME, httpc.host,
strlen(httpc.host));
ret = zsock_setsockopt(httpc.fd, SOL_TLS, TLS_HOSTNAME,
httpc.host, strlen(httpc.host));
} else {
ret = setsockopt(httpc.fd, SOL_TLS, TLS_HOSTNAME, NULL, 0);
ret = zsock_setsockopt(httpc.fd, SOL_TLS, TLS_HOSTNAME,
NULL, 0);
}
if (ret) {
LOG_ERR("setsockopt(TLS_HOSTNAME) error: %d", -errno);
Expand All @@ -280,8 +282,8 @@ static int do_http_connect(void)
if (!IS_ENABLED(CONFIG_SLM_NATIVE_TLS)) {
int session_cache = TLS_SESSION_CACHE_ENABLED;

ret = setsockopt(httpc.fd, SOL_TLS, TLS_SESSION_CACHE, &session_cache,
sizeof(session_cache));
ret = zsock_setsockopt(httpc.fd, SOL_TLS, TLS_SESSION_CACHE,
&session_cache, sizeof(session_cache));
if (ret) {
LOG_ERR("setsockopt(TLS_SESSION_CACHE) error: %d", -errno);
ret = -errno;
Expand All @@ -291,13 +293,15 @@ static int do_http_connect(void)
}

LOG_DBG("Configuring socket timeout (%lld s)", timeo.tv_sec);
ret = setsockopt(httpc.fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo));
ret = zsock_setsockopt(httpc.fd, SOL_SOCKET, SO_SNDTIMEO,
&timeo, sizeof(timeo));
if (ret) {
LOG_ERR("setsockopt(SO_SNDTIMEO) error: %d", -errno);
ret = -errno;
goto exit_cli;
}
ret = setsockopt(httpc.fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
ret = zsock_setsockopt(httpc.fd, SOL_SOCKET, SO_RCVTIMEO,
&timeo, sizeof(timeo));
if (ret) {
LOG_ERR("setsockopt(SO_SNDTIMEO) error: %d", -errno);
ret = -errno;
Expand All @@ -310,9 +314,9 @@ static int do_http_connect(void)
goto exit_cli;
}
if (sa.sa_family == AF_INET) {
ret = connect(httpc.fd, &sa, sizeof(struct sockaddr_in));
ret = zsock_connect(httpc.fd, &sa, sizeof(struct sockaddr_in));
} else {
ret = connect(httpc.fd, &sa, sizeof(struct sockaddr_in6));
ret = zsock_connect(httpc.fd, &sa, sizeof(struct sockaddr_in6));
}

if (ret) {
Expand All @@ -325,7 +329,7 @@ static int do_http_connect(void)
return 0;

exit_cli:
close(httpc.fd);
zsock_close(httpc.fd);
httpc.fd = INVALID_SOCKET;
rsp_send("\r\n#XHTTPCCON: 0\r\n");

Expand All @@ -338,7 +342,7 @@ static int do_http_disconnect(void)
if (httpc.fd == INVALID_SOCKET) {
return 0;
}
int ret = close(httpc.fd);
int ret = zsock_close(httpc.fd);

if (ret) {
LOG_WRN("close() failed: %d", -errno);
Expand Down
14 changes: 7 additions & 7 deletions applications/serial_lte_modem/src/mqtt_c/slm_at_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ void mqtt_evt_handler(struct mqtt_client *const c, const struct mqtt_evt *evt)
static void mqtt_thread_fn(void *arg1, void *arg2, void *arg3)
{
int err = 0;
struct pollfd fds;
struct zsock_pollfd fds;

ARG_UNUSED(arg1);
ARG_UNUSED(arg2);
Expand All @@ -225,20 +225,20 @@ static void mqtt_thread_fn(void *arg1, void *arg2, void *arg3)
fds.fd = client.transport.tls.sock;
}
#endif
fds.events = POLLIN;
fds.events = ZSOCK_POLLIN;
while (true) {
if (!ctx.connected) {
LOG_WRN("MQTT disconnected");
err = 0;
break;
}
err = poll(&fds, 1, mqtt_keepalive_time_left(&client));
err = zsock_poll(&fds, 1, mqtt_keepalive_time_left(&client));
if (err < 0) {
LOG_ERR("ERROR: poll %d", errno);
break;
}

if ((fds.revents & POLLIN) == POLLIN) {
if ((fds.revents & ZSOCK_POLLIN) == ZSOCK_POLLIN) {
err = mqtt_input(&client);
if (err != 0) {
LOG_ERR("ERROR: mqtt_input %d", err);
Expand All @@ -259,17 +259,17 @@ static void mqtt_thread_fn(void *arg1, void *arg2, void *arg3)
* determines to be inactive or non-responsive at any time, regardless of the
* Keep Alive value provided by that Client.
*/
if ((fds.revents & POLLERR) == POLLERR) {
if ((fds.revents & ZSOCK_POLLERR) == ZSOCK_POLLERR) {
LOG_ERR("POLLERR");
err = -EIO;
break;
}
if ((fds.revents & POLLHUP) == POLLHUP) {
if ((fds.revents & ZSOCK_POLLHUP) == ZSOCK_POLLHUP) {
LOG_ERR("POLLHUP");
err = -ECONNRESET;
break;
}
if ((fds.revents & POLLNVAL) == POLLNVAL) {
if ((fds.revents & ZSOCK_POLLNVAL) == ZSOCK_POLLNVAL) {
LOG_ERR("POLLNVAL");
err = -ENOTCONN;
break;
Expand Down
57 changes: 29 additions & 28 deletions applications/serial_lte_modem/src/slm_at_icmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ LOG_MODULE_REGISTER(slm_icmp, CONFIG_SLM_LOG_LEVEL);

/**@ ICMP Ping command arguments */
static struct ping_argv_t {
struct addrinfo *src;
struct addrinfo *dest;
struct zsock_addrinfo *src;
struct zsock_addrinfo *dest;
uint16_t len;
uint16_t waitms;
uint16_t count;
Expand Down Expand Up @@ -112,9 +112,9 @@ static uint32_t send_ping_wait_reply(void)
uint8_t *data = NULL;
uint8_t rep = 0;
uint8_t header_len = 0;
struct addrinfo *si = ping_argv.src;
struct zsock_addrinfo *si = ping_argv.src;
const int alloc_size = ICMP_DEFAULT_LINK_MTU;
struct pollfd fds[1];
struct zsock_pollfd fds[1];
int dpllen, pllen, len;
int fd;
int plseqnr;
Expand Down Expand Up @@ -237,7 +237,7 @@ static uint32_t send_ping_wait_reply(void)
errno = 0;
delta_t = 0;

fd = socket(AF_PACKET, SOCK_RAW, 0);
fd = zsock_socket(AF_PACKET, SOCK_RAW, 0);
if (fd < 0) {
LOG_ERR("socket() failed: (%d)", -errno);
free(buf);
Expand All @@ -249,7 +249,8 @@ static uint32_t send_ping_wait_reply(void)
if (ping_argv.pdn != 0) {
int pdn = ping_argv.pdn;

if (setsockopt(fd, SOL_SOCKET, SO_BINDTOPDN, &pdn, sizeof(int))) {
if (zsock_setsockopt(fd, SOL_SOCKET, SO_BINDTOPDN,
&pdn, sizeof(int))) {
LOG_WRN("Unable to set socket SO_BINDTOPDN, abort");
goto close_end;
}
Expand All @@ -264,24 +265,24 @@ static uint32_t send_ping_wait_reply(void)
tv.tv_sec = (ping_argv.waitms / 1000);
tv.tv_usec = (ping_argv.waitms % 1000) * 1000;

if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (struct timeval *)&tv,
sizeof(struct timeval)) < 0) {
if (zsock_setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
(struct timeval *)&tv, sizeof(struct timeval)) < 0) {
LOG_WRN("Unable to set socket SO_SNDTIMEO, continue");
}

/* Just for sure, let's put the timeout for rcv as well
* (should not be needed for non-blocking socket):
*/
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (struct timeval *)&tv,
sizeof(struct timeval)) < 0) {
if (zsock_setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO,
(struct timeval *)&tv, sizeof(struct timeval)) < 0) {
LOG_WRN("Unable to set socket SO_RCVTIMEO, continue");
}

/* Include also a sending time to measured RTT: */
start_t = k_uptime_get();
timeout = ping_argv.waitms;

ret = send(fd, buf, total_length, 0);
ret = zsock_send(fd, buf, total_length, 0);
if (ret <= 0) {
LOG_ERR("send() failed: (%d)", -errno);
goto close_end;
Expand All @@ -293,7 +294,7 @@ static uint32_t send_ping_wait_reply(void)

wait_for_data:
fds[0].fd = fd;
fds[0].events = POLLIN;
fds[0].events = ZSOCK_POLLIN;

do {
if (timeout <= 0) {
Expand All @@ -302,7 +303,7 @@ static uint32_t send_ping_wait_reply(void)
goto close_end;
}

ret = poll(fds, 1, timeout);
ret = zsock_poll(fds, 1, timeout);
if (ret == 0) {
LOG_WRN("Pinging result: no ping response in given timeout msec");
delta_t = 0;
Expand All @@ -313,7 +314,7 @@ static uint32_t send_ping_wait_reply(void)
goto close_end;
}

len = recv(fd, buf, alloc_size, 0);
len = zsock_recv(fd, buf, alloc_size, 0);

/* Calculate again, how much there's still time left */
delta_t = k_uptime_delta(&start_t);
Expand Down Expand Up @@ -406,15 +407,15 @@ static uint32_t send_ping_wait_reply(void)
(uint32_t)(delta_t)/1000, (uint32_t)(delta_t)%1000);

close_end:
(void)close(fd);
(void)zsock_close(fd);
free(buf);
return (uint32_t)delta_t;
}

void ping_task(struct k_work *item)
{
struct addrinfo *si = ping_argv.src;
struct addrinfo *di = ping_argv.dest;
struct zsock_addrinfo *si = ping_argv.src;
struct zsock_addrinfo *di = ping_argv.dest;
uint32_t sum = 0;
uint32_t count = 0;
uint32_t rtt_min = 0xFFFFFFFF;
Expand Down Expand Up @@ -452,19 +453,19 @@ void ping_task(struct k_work *item)
rtt_min, rtt_max, sum / count);
}

freeaddrinfo(si);
freeaddrinfo(di);
zsock_freeaddrinfo(si);
zsock_freeaddrinfo(di);
}

static int ping_test_handler(const char *target)
{
int ret;
struct addrinfo *res;
struct zsock_addrinfo *res;

ret = getaddrinfo(target, NULL, NULL, &res);
ret = zsock_getaddrinfo(target, NULL, NULL, &res);
if (ret != 0) {
LOG_ERR("getaddrinfo(dest) error: %d", ret);
rsp_send("\"%s\"\r\n", gai_strerror(ret));
rsp_send("\"%s\"\r\n", zsock_gai_strerror(ret));
return -EAGAIN;
}

Expand All @@ -476,16 +477,16 @@ static int ping_test_handler(const char *target)
util_get_ip_addr(ping_argv.pdn, ipv4_addr, NULL);
if (!*ipv4_addr) {
LOG_ERR("Unable to obtain local IPv4 address");
freeaddrinfo(res);
zsock_freeaddrinfo(res);
return -1;
}

ping_argv.dest = res;
res = NULL;
ret = getaddrinfo(ipv4_addr, NULL, NULL, &res);
ret = zsock_getaddrinfo(ipv4_addr, NULL, NULL, &res);
if (ret != 0) {
LOG_ERR("getaddrinfo(src) error: %d", ret);
freeaddrinfo(ping_argv.dest);
zsock_freeaddrinfo(ping_argv.dest);
return -ret;
}
ping_argv.src = res;
Expand All @@ -496,16 +497,16 @@ static int ping_test_handler(const char *target)
util_get_ip_addr(ping_argv.pdn, NULL, ipv6_addr);
if (!*ipv6_addr) {
LOG_ERR("Unable to obtain local IPv6 address");
freeaddrinfo(res);
zsock_freeaddrinfo(res);
return -1;
}

ping_argv.dest = res;
res = NULL;
ret = getaddrinfo(ipv6_addr, NULL, NULL, &res);
ret = zsock_getaddrinfo(ipv6_addr, NULL, NULL, &res);
if (ret != 0) {
LOG_ERR("getaddrinfo(src) error: %d", ret);
freeaddrinfo(ping_argv.dest);
zsock_freeaddrinfo(ping_argv.dest);
return -ret;
}
ping_argv.src = res;
Expand Down
Loading

0 comments on commit 6e66f80

Please sign in to comment.