Skip to content

Commit

Permalink
net: openthread: Remove dependency on CONFIG_POSIX_API
Browse files Browse the repository at this point in the history
For CoAP utils, `CONFIG_POSIX_API` is required only for wrapping
socket calls not to require `zsock` prefix. POSIX_API brings more
features than needed, is still experimental in Zephyr and causes
warnings on building.
This commit changes net socket calls to use `zsock_` prefix and
disables `CONFIG_POSIX_API` for OpenThread.

Signed-off-by: Maciej Baczmanski <[email protected]>
  • Loading branch information
maciejbaczmanski committed Dec 13, 2024
1 parent 435c9af commit 3fc6b40
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
1 change: 0 additions & 1 deletion samples/openthread/coap_client/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ CONFIG_PM_PARTITION_SIZE_SETTINGS_STORAGE=0x8000

# Network sockets
CONFIG_NET_SOCKETS=y
CONFIG_POSIX_API=y
CONFIG_NET_SOCKETS_POLL_MAX=4

# Same network Master Key for client and server
Expand Down
2 changes: 1 addition & 1 deletion samples/openthread/coap_client/src/coap_client_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ static int on_provisioning_reply(const struct coap_packet *response,

memcpy(&unique_local_addr.sin6_addr, payload, payload_size);

if (!inet_ntop(AF_INET6, payload, unique_local_addr_str,
if (!zsock_inet_ntop(AF_INET6, payload, unique_local_addr_str,
INET6_ADDRSTRLEN)) {
LOG_ERR("Received data is not IPv6 address: %d", errno);
ret = -errno;
Expand Down
1 change: 0 additions & 1 deletion samples/openthread/coap_server/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ CONFIG_PM_PARTITION_SIZE_SETTINGS_STORAGE=0x8000

# Network sockets
CONFIG_NET_SOCKETS=y
CONFIG_POSIX_API=y
CONFIG_NET_SOCKETS_POLL_MAX=4

# Same network Master Key for client and server
Expand Down
24 changes: 12 additions & 12 deletions subsys/net/lib/coap_utils/coap_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LOG_MODULE_REGISTER(coap_utils, CONFIG_COAP_UTILS_LOG_LEVEL);
#endif

const static int nfds = 1;
static struct pollfd fds;
static struct zsock_pollfd fds;
static struct coap_reply replies[COAP_MAX_REPLIES];
static int proto_family;
static struct sockaddr *bind_addr;
Expand All @@ -37,7 +37,7 @@ static int coap_open_socket(void)
int sock;

while (1) {
sock = socket(proto_family, SOCK_DGRAM, IPPROTO_UDP);
sock = zsock_socket(proto_family, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
LOG_ERR("Failed to create socket %d", errno);
k_sleep(K_MSEC(COAP_OPEN_SOCKET_SLEEP));
Expand All @@ -47,7 +47,7 @@ static int coap_open_socket(void)
}

if (bind_addr) {
if (bind(sock, bind_addr, sizeof(*bind_addr))) {
if (zsock_bind(sock, bind_addr, sizeof(*bind_addr))) {
LOG_ERR("Failed to bind socket, errno: %d", errno);
}
}
Expand All @@ -57,7 +57,7 @@ static int coap_open_socket(void)

static void coap_close_socket(int socket)
{
(void)close(socket);
(void)zsock_close(socket);
}

static void coap_receive(void)
Expand All @@ -73,25 +73,25 @@ static void coap_receive(void)
while (1) {
fds.revents = 0;

if (poll(&fds, nfds, -1) < 0) {
if (zsock_poll(&fds, nfds, -1) < 0) {
LOG_ERR("Error in poll:%d", errno);
errno = 0;
k_sleep(K_MSEC(COAP_POOL_SLEEP));
continue;
}

if (fds.revents & POLLERR) {
if (fds.revents & ZSOCK_POLLERR) {
LOG_ERR("Error in poll.. waiting a moment.");
k_sleep(K_MSEC(COAP_POOL_SLEEP));
continue;
}

if (fds.revents & POLLHUP) {
if (fds.revents & ZSOCK_POLLHUP) {
LOG_ERR("Error in poll: POLLHUP");
continue;
}

if (fds.revents & POLLNVAL) {
if (fds.revents & ZSOCK_POLLNVAL) {
LOG_ERR("Error in poll: POLLNVAL - fd not open");

coap_close_socket(fds.fd);
Expand All @@ -102,13 +102,13 @@ static void coap_receive(void)
continue;
}

if (!(fds.revents & POLLIN)) {
if (!(fds.revents & ZSOCK_POLLIN)) {
LOG_ERR("Unknown poll error");
continue;
}

from_addr_len = sizeof(from_addr);
len = recvfrom(fds.fd, buf, sizeof(buf) - 1, 0, &from_addr,
len = zsock_recvfrom(fds.fd, buf, sizeof(buf) - 1, 0, &from_addr,
&from_addr_len);

if (len < 0) {
Expand Down Expand Up @@ -184,7 +184,7 @@ static int coap_init_request(enum coap_method method,
static int coap_send_message(const struct sockaddr *addr,
struct coap_packet *request)
{
return sendto(fds.fd, request->data, request->offset, 0, addr,
return zsock_sendto(fds.fd, request->data, request->offset, 0, addr,
sizeof(*addr));
}

Expand All @@ -207,7 +207,7 @@ void coap_init(int ip_family, struct sockaddr *addr)
bind_addr = addr;
}

fds.events = POLLIN;
fds.events = ZSOCK_POLLIN;
fds.revents = 0;
fds.fd = coap_open_socket();

Expand Down

0 comments on commit 3fc6b40

Please sign in to comment.