mirror of
https://github.com/licsber/micropython.git
synced 2024-09-20 09:00:23 +08:00
drivers/ninaw10: Return standard error numbers.
This commit is contained in:
parent
a63875d5ad
commit
1aac151d68
@ -28,6 +28,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
|
#include "py/mperrno.h"
|
||||||
|
|
||||||
#if MICROPY_PY_NETWORK_NINAW10
|
#if MICROPY_PY_NETWORK_NINAW10
|
||||||
|
|
||||||
@ -480,7 +481,7 @@ int nina_connected_sta(uint32_t *sta_ip) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int nina_wait_for_sta(uint32_t *sta_ip, uint32_t timeout) {
|
int nina_wait_for_sta(uint32_t *sta_ip, uint32_t timeout) {
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
int nina_ifconfig(nina_ifconfig_t *ifconfig, bool set) {
|
int nina_ifconfig(nina_ifconfig_t *ifconfig, bool set) {
|
||||||
@ -597,7 +598,7 @@ int nina_scan(nina_scan_callback_t scan_callback, void *arg, uint32_t timeout) {
|
|||||||
|
|
||||||
if (timeout && (mp_hal_ticks_ms() - start) >= timeout) {
|
if (timeout && (mp_hal_ticks_ms() - start) >= timeout) {
|
||||||
// Timeout, no networks.
|
// Timeout, no networks.
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_hal_delay_ms(100);
|
mp_hal_delay_ms(100);
|
||||||
@ -723,7 +724,7 @@ int nina_socket_close(int fd) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ((mp_hal_ticks_ms() - start) >= 5000) {
|
if ((mp_hal_ticks_ms() - start) >= 5000) {
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -777,12 +778,15 @@ int nina_socket_accept(int fd, uint8_t *ip, uint16_t *port, int *fd_out, int32_t
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (mp_uint_t start = mp_hal_ticks_ms(); !sock; mp_hal_delay_ms(10)) {
|
for (mp_uint_t start = mp_hal_ticks_ms(); ; mp_hal_delay_ms(10)) {
|
||||||
if (nina_socket_avail(fd, NINA_SOCKET_TYPE_TCP, &sock) != 0) {
|
if (nina_socket_avail(fd, NINA_SOCKET_TYPE_TCP, &sock) != 0) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (sock != 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -820,7 +824,7 @@ int nina_socket_connect(int fd, uint8_t *ip, uint16_t port, int32_t timeout) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -832,7 +836,7 @@ int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, int32_t timeout)
|
|||||||
uint16_t bytes = 0;
|
uint16_t bytes = 0;
|
||||||
|
|
||||||
if (nina_socket_status(fd) != SOCKET_STATE_ESTABLISHED) {
|
if (nina_socket_status(fd) != SOCKET_STATE_ESTABLISHED) {
|
||||||
return -1;
|
return -MP_ENOTCONN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nina_send_command_read_vals(NINA_CMD_TCP_SEND,
|
if (nina_send_command_read_vals(NINA_CMD_TCP_SEND,
|
||||||
@ -854,7 +858,7 @@ int nina_socket_send(int fd, const uint8_t *buf, uint32_t len, int32_t timeout)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
mp_hal_delay_ms(1);
|
mp_hal_delay_ms(1);
|
||||||
}
|
}
|
||||||
@ -866,7 +870,7 @@ int nina_socket_recv(int fd, uint8_t *buf, uint32_t len, int32_t timeout) {
|
|||||||
uint16_t bytes = 0;
|
uint16_t bytes = 0;
|
||||||
|
|
||||||
if (nina_socket_status(fd) != SOCKET_STATE_ESTABLISHED) {
|
if (nina_socket_status(fd) != SOCKET_STATE_ESTABLISHED) {
|
||||||
return -1;
|
return -MP_ENOTCONN;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (mp_uint_t start = mp_hal_ticks_ms(); bytes == 0; mp_hal_delay_ms(1)) {
|
for (mp_uint_t start = mp_hal_ticks_ms(); bytes == 0; mp_hal_delay_ms(1)) {
|
||||||
@ -882,7 +886,7 @@ int nina_socket_recv(int fd, uint8_t *buf, uint32_t len, int32_t timeout) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
@ -934,7 +938,7 @@ int nina_socket_recvfrom(int fd, uint8_t *buf, uint32_t len, uint8_t *ip, uint16
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
if (timeout == 0 || (timeout > 0 && (mp_hal_ticks_ms() - start) >= timeout)) {
|
||||||
return NINA_ERROR_TIMEOUT;
|
return -MP_ETIMEDOUT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nina_send_command_read_vals(NINA_CMD_SOCKET_REMOTE_ADDR,
|
if (nina_send_command_read_vals(NINA_CMD_SOCKET_REMOTE_ADDR,
|
||||||
|
@ -61,11 +61,6 @@ typedef enum {
|
|||||||
NINA_SOCKET_TYPE_TLS_BEARSSL
|
NINA_SOCKET_TYPE_TLS_BEARSSL
|
||||||
} nina_socket_type_t;
|
} nina_socket_type_t;
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
NINA_ERROR_IO = -1,
|
|
||||||
NINA_ERROR_TIMEOUT = -2,
|
|
||||||
} nina_error_t;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t ip_addr[NINA_IPV4_ADDR_LEN];
|
uint8_t ip_addr[NINA_IPV4_ADDR_LEN];
|
||||||
uint8_t subnet_addr[NINA_IPV4_ADDR_LEN];
|
uint8_t subnet_addr[NINA_IPV4_ADDR_LEN];
|
||||||
|
@ -406,13 +406,12 @@ STATIC int network_ninaw10_socket_accept(mod_network_socket_obj_t *socket,
|
|||||||
int fd = 0;
|
int fd = 0;
|
||||||
// Call accept.
|
// Call accept.
|
||||||
int ret = nina_socket_accept(socket->fileno, ip, (uint16_t *)port, &fd, socket->timeout);
|
int ret = nina_socket_accept(socket->fileno, ip, (uint16_t *)port, &fd, socket->timeout);
|
||||||
if (ret == NINA_ERROR_TIMEOUT) {
|
if (ret < 0) {
|
||||||
// The socket is Not closed on timeout when calling functions that accept a timeout.
|
*_errno = -ret;
|
||||||
*_errno = MP_ETIMEDOUT;
|
// Close socket if not a timeout error.
|
||||||
return -1;
|
if (*_errno != MP_ETIMEDOUT) {
|
||||||
} else if (ret < 0) {
|
network_ninaw10_socket_close(socket);
|
||||||
*_errno = ret;
|
}
|
||||||
network_ninaw10_socket_close(socket);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -424,13 +423,12 @@ 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) {
|
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);
|
int ret = nina_socket_connect(socket->fileno, ip, port, socket->timeout);
|
||||||
if (ret == NINA_ERROR_TIMEOUT) {
|
if (ret < 0) {
|
||||||
// The socket is Not closed on timeout when calling functions that accept a timeout.
|
*_errno = -ret;
|
||||||
*_errno = MP_ETIMEDOUT;
|
// Close socket if not a timeout error.
|
||||||
return -1;
|
if (*_errno != MP_ETIMEDOUT) {
|
||||||
} else if (ret < 0) {
|
network_ninaw10_socket_close(socket);
|
||||||
*_errno = ret;
|
}
|
||||||
network_ninaw10_socket_close(socket);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -438,14 +436,12 @@ STATIC int network_ninaw10_socket_connect(mod_network_socket_obj_t *socket, byte
|
|||||||
|
|
||||||
STATIC mp_uint_t network_ninaw10_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) {
|
STATIC mp_uint_t network_ninaw10_socket_send(mod_network_socket_obj_t *socket, const byte *buf, mp_uint_t len, int *_errno) {
|
||||||
int ret = nina_socket_send(socket->fileno, buf, len, socket->timeout);
|
int ret = nina_socket_send(socket->fileno, buf, len, socket->timeout);
|
||||||
if (ret == NINA_ERROR_TIMEOUT) {
|
if (ret < 0) {
|
||||||
// The socket is Not closed on timeout when calling functions that accept a timeout.
|
*_errno = -ret;
|
||||||
*_errno = MP_ETIMEDOUT;
|
// Close socket if not a timeout error.
|
||||||
return -1;
|
if (*_errno != MP_ETIMEDOUT) {
|
||||||
} else if (ret < 0) {
|
network_ninaw10_socket_close(socket);
|
||||||
// Close the socket on any other errors.
|
}
|
||||||
*_errno = ret;
|
|
||||||
network_ninaw10_socket_close(socket);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
@ -460,15 +456,12 @@ STATIC mp_uint_t network_ninaw10_socket_recv(mod_network_socket_obj_t *socket, b
|
|||||||
} else {
|
} else {
|
||||||
ret = nina_socket_recv(socket->fileno, buf, len, socket->timeout);
|
ret = nina_socket_recv(socket->fileno, buf, len, socket->timeout);
|
||||||
}
|
}
|
||||||
|
if (ret < 0) {
|
||||||
if (ret == NINA_ERROR_TIMEOUT) {
|
*_errno = -ret;
|
||||||
// The socket is Not closed on timeout when calling functions that accept a timeout.
|
// Close socket if not a timeout error.
|
||||||
*_errno = MP_ETIMEDOUT;
|
if (*_errno != MP_ETIMEDOUT) {
|
||||||
return -1;
|
network_ninaw10_socket_close(socket);
|
||||||
} else if (ret < 0) {
|
}
|
||||||
// Close the socket on any other errors.
|
|
||||||
*_errno = ret;
|
|
||||||
network_ninaw10_socket_close(socket);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
@ -493,13 +486,12 @@ STATIC mp_uint_t network_ninaw10_socket_sendto(mod_network_socket_obj_t *socket,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ret = nina_socket_sendto(socket->fileno, buf, len, ip, port, socket->timeout);
|
int ret = nina_socket_sendto(socket->fileno, buf, len, ip, port, socket->timeout);
|
||||||
if (ret == NINA_ERROR_TIMEOUT) {
|
if (ret < 0) {
|
||||||
// The socket is Not closed on timeout when calling functions that accept a timeout.
|
*_errno = -ret;
|
||||||
*_errno = MP_ETIMEDOUT;
|
// Close socket if not a timeout error.
|
||||||
return -1;
|
if (*_errno != MP_ETIMEDOUT) {
|
||||||
} else if (ret < 0) {
|
network_ninaw10_socket_close(socket);
|
||||||
*_errno = ret;
|
}
|
||||||
network_ninaw10_socket_close(socket);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
@ -519,14 +511,12 @@ STATIC mp_uint_t network_ninaw10_socket_recvfrom(mod_network_socket_obj_t *socke
|
|||||||
}
|
}
|
||||||
ret = nina_socket_recvfrom(socket->fileno, buf, len, ip, (uint16_t *)port, socket->timeout);
|
ret = nina_socket_recvfrom(socket->fileno, buf, len, ip, (uint16_t *)port, socket->timeout);
|
||||||
}
|
}
|
||||||
if (ret == NINA_ERROR_TIMEOUT) {
|
if (ret < 0) {
|
||||||
// The socket is Not closed on timeout when calling functions that accept a timeout.
|
*_errno = -ret;
|
||||||
*_errno = MP_ETIMEDOUT;
|
// Close socket if not a timeout error.
|
||||||
return -1;
|
if (*_errno != MP_ETIMEDOUT) {
|
||||||
} else if (ret < 0) {
|
network_ninaw10_socket_close(socket);
|
||||||
// Close the socket on any other errors.
|
}
|
||||||
*_errno = ret;
|
|
||||||
network_ninaw10_socket_close(socket);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
Loading…
Reference in New Issue
Block a user