Skip to content

Commit

Permalink
drivers/ninaw10: Fix timeout handling to match modusocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
iabdalkader authored and dpgeorge committed Jan 21, 2022
1 parent 9a61bc3 commit e401ff8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
30 changes: 19 additions & 11 deletions drivers/ninaw10/nina_wifi_drv.c
Expand Up @@ -770,7 +770,7 @@ int nina_socket_avail(int fd, int type, uint16_t *data) {
return 0;
}

int nina_socket_accept(int fd, uint8_t *ip, uint16_t *port, int *fd_out, uint32_t timeout) {
int nina_socket_accept(int fd, uint8_t *ip, uint16_t *port, int *fd_out, int32_t timeout) {
uint16_t sock = 0;

if (nina_server_socket_status(fd) != SOCKET_STATE_LISTEN) {
Expand All @@ -781,7 +781,7 @@ int nina_socket_accept(int fd, uint8_t *ip, uint16_t *port, int *fd_out, uint32_
if (nina_socket_avail(fd, NINA_SOCKET_TYPE_TCP, &sock) != 0) {
return -1;
}
if (timeout && (mp_hal_ticks_ms() - start) >= timeout) {
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
return NINA_ERROR_TIMEOUT;
}
}
Expand All @@ -798,7 +798,7 @@ int nina_socket_accept(int fd, uint8_t *ip, uint16_t *port, int *fd_out, uint32_
return 0;
}

int nina_socket_connect(int fd, uint8_t *ip, uint16_t port, uint32_t timeout) {
int nina_socket_connect(int fd, uint8_t *ip, uint16_t port, int32_t timeout) {
if (nina_send_command_read_ack(NINA_CMD_SOCKET_CONNECT,
4, ARG_8BITS,
NINA_ARGS(
Expand All @@ -819,15 +819,15 @@ int nina_socket_connect(int fd, uint8_t *ip, uint16_t port, uint32_t timeout) {
break;
}

if (timeout && (mp_hal_ticks_ms() - start) >= timeout) {
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
return NINA_ERROR_TIMEOUT;
}
}

return 0;
}

int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, uint32_t timeout) {
int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, int32_t timeout) {
uint16_t size = 2;
uint16_t bytes = 0;

Expand All @@ -853,7 +853,7 @@ int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, uint32_t timeout)
break;
}

if (timeout && (mp_hal_ticks_ms() - start) >= timeout) {
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
return NINA_ERROR_TIMEOUT;
}
mp_hal_delay_ms(1);
Expand All @@ -862,7 +862,7 @@ int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, uint32_t timeout)
return bytes;
}

int nina_socket_recv(int fd, uint8_t *buf, uint32_t len, uint32_t timeout) {
int nina_socket_recv(int fd, uint8_t *buf, uint32_t len, int32_t timeout) {
uint16_t bytes = 0;

if (nina_socket_status(fd) != SOCKET_STATE_ESTABLISHED) {
Expand All @@ -877,15 +877,19 @@ int nina_socket_recv(int fd, uint8_t *buf, uint32_t len, uint32_t timeout) {
return -1;
}

if (timeout && (mp_hal_ticks_ms() - start) >= timeout) {
if (bytes != 0) {
break;
}

if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
return NINA_ERROR_TIMEOUT;
}
}
return bytes;
}

// Check from the upper layer if the socket is bound, if not then auto-bind it first.
int nina_socket_sendto(int fd, const uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t port, uint32_t timeout) {
int nina_socket_sendto(int fd, const uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t port, int32_t timeout) {
// TODO do we need to split the packet somewhere?
if (nina_send_command_read_ack(NINA_CMD_SOCKET_CONNECT,
4, ARG_8BITS,
Expand All @@ -912,7 +916,7 @@ int nina_socket_sendto(int fd, const uint8_t *buf, uint32_t len, uint8_t *ip, ui
}

// Check from the upper layer if the socket is bound, if not then auto-bind it first.
int nina_socket_recvfrom(int fd, uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t *port, uint32_t timeout) {
int nina_socket_recvfrom(int fd, uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t *port, int32_t timeout) {
uint16_t bytes = 0;
uint16_t port_len = 2;
uint16_t ip_len = NINA_IPV4_ADDR_LEN;
Expand All @@ -925,7 +929,11 @@ int nina_socket_recvfrom(int fd, uint8_t *buf, uint32_t len, uint8_t *ip, uint16
return -1;
}

if (timeout && (mp_hal_ticks_ms() - start) >= timeout) {
if (bytes != 0) {
break;
}

if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
return NINA_ERROR_TIMEOUT;
}
}
Expand Down
12 changes: 6 additions & 6 deletions drivers/ninaw10/nina_wifi_drv.h
Expand Up @@ -110,12 +110,12 @@ int nina_socket_close(int fd);
int nina_socket_bind(int fd, uint8_t *ip, uint16_t port, int type);
int nina_socket_listen(int fd, uint32_t backlog);
int nina_socket_avail(int fd, int type, uint16_t *data);
int nina_socket_accept(int fd, uint8_t *ip, uint16_t *port, int *fd_out, uint32_t timeout);
int nina_socket_connect(int fd, uint8_t *ip, uint16_t port, uint32_t timeout);
int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, uint32_t timeout);
int nina_socket_recv(int fd, uint8_t *buf, uint32_t len, uint32_t timeout);
int nina_socket_sendto(int fd, const uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t port, uint32_t timeout);
int nina_socket_recvfrom(int fd, uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t *port, uint32_t timeout);
int nina_socket_accept(int fd, uint8_t *ip, uint16_t *port, int *fd_out, int32_t timeout);
int nina_socket_connect(int fd, uint8_t *ip, uint16_t port, int32_t timeout);
int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, int32_t timeout);
int nina_socket_recv(int fd, uint8_t *buf, uint32_t len, int32_t timeout);
int nina_socket_sendto(int fd, const uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t port, int32_t timeout);
int nina_socket_recvfrom(int fd, uint8_t *buf, uint32_t len, uint8_t *ip, uint16_t *port, int32_t timeout);
int nina_socket_setsockopt(int fd, uint32_t level, uint32_t opt, const void *optval, uint32_t optlen);

#endif // MICROPY_INCLUDED_DRIVERS_NINAW10_NINA_WIFI_DRV_H
16 changes: 10 additions & 6 deletions extmod/network_ninaw10.c
Expand Up @@ -406,7 +406,11 @@ STATIC int network_ninaw10_socket_accept(mod_network_socket_obj_t *socket,
int fd = 0;
// Call accept.
int ret = nina_socket_accept(socket->fileno, ip, (uint16_t *)port, &fd, socket->timeout);
if (ret < 0) {
if (ret == NINA_ERROR_TIMEOUT) {
// The socket is Not closed on timeout when calling functions that accept a timeout.
*_errno = MP_ETIMEDOUT;
return -1;
} else if (ret < 0) {
*_errno = ret;
network_ninaw10_socket_close(socket);
return -1;
Expand All @@ -420,7 +424,11 @@ STATIC int network_ninaw10_socket_accept(mod_network_socket_obj_t *socket,

STATIC int network_ninaw10_socket_connect(mod_network_socket_obj_t *socket, byte *ip, mp_uint_t port, int *_errno) {
int ret = nina_socket_connect(socket->fileno, ip, port, socket->timeout);
if (ret < 0) {
if (ret == NINA_ERROR_TIMEOUT) {
// The socket is Not closed on timeout when calling functions that accept a timeout.
*_errno = MP_ETIMEDOUT;
return -1;
} else if (ret < 0) {
*_errno = ret;
network_ninaw10_socket_close(socket);
return -1;
Expand Down Expand Up @@ -536,10 +544,6 @@ STATIC int network_ninaw10_socket_setsockopt(mod_network_socket_obj_t *socket, m
}

STATIC int network_ninaw10_socket_settimeout(mod_network_socket_obj_t *socket, mp_uint_t timeout_ms, int *_errno) {
if (timeout_ms == UINT32_MAX) {
// no timeout is given, set the socket to blocking mode.
timeout_ms = 0;
}
socket->timeout = timeout_ms;
return 0;
}
Expand Down

0 comments on commit e401ff8

Please sign in to comment.