Skip to content

Commit 5a86031

Browse files
iabdalkaderdpgeorge
authored andcommitted
extmod/network_ninaw10: Make recv/recvfrom interchangeable.
1 parent 73a6b53 commit 5a86031

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

extmod/network_ninaw10.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,15 @@ STATIC mp_uint_t network_ninaw10_socket_send(mod_network_socket_obj_t *socket, c
446446
}
447447

448448
STATIC mp_uint_t network_ninaw10_socket_recv(mod_network_socket_obj_t *socket, byte *buf, mp_uint_t len, int *_errno) {
449-
int ret = nina_socket_recv(socket->fileno, buf, len, socket->timeout);
449+
int ret = 0;
450+
if (socket->type == MOD_NETWORK_SOCK_DGRAM) {
451+
byte ip[4];
452+
uint16_t port;
453+
ret = nina_socket_recvfrom(socket->fileno, buf, len, ip, &port, socket->timeout);
454+
} else {
455+
ret = nina_socket_recv(socket->fileno, buf, len, socket->timeout);
456+
}
457+
450458
if (ret == NINA_ERROR_TIMEOUT) {
451459
// The socket is Not closed on timeout when calling functions that accept a timeout.
452460
*_errno = MP_ETIMEDOUT;
@@ -493,12 +501,18 @@ STATIC mp_uint_t network_ninaw10_socket_sendto(mod_network_socket_obj_t *socket,
493501

494502
STATIC mp_uint_t network_ninaw10_socket_recvfrom(mod_network_socket_obj_t *socket,
495503
byte *buf, mp_uint_t len, byte *ip, mp_uint_t *port, int *_errno) {
496-
// Auto-bind the socket first if the socket is unbound.
497-
if (network_ninaw10_socket_auto_bind(socket, _errno) != 0) {
498-
return -1;
504+
int ret = 0;
505+
if (socket->type == MOD_NETWORK_SOCK_STREAM) {
506+
*port = 0;
507+
*((uint32_t *)ip) = 0;
508+
ret = nina_socket_recv(socket->fileno, buf, len, socket->timeout);
509+
} else {
510+
// Auto-bind the socket first if the socket is unbound.
511+
if (network_ninaw10_socket_auto_bind(socket, _errno) != 0) {
512+
return -1;
513+
}
514+
ret = nina_socket_recvfrom(socket->fileno, buf, len, ip, (uint16_t *)port, socket->timeout);
499515
}
500-
501-
int ret = nina_socket_recvfrom(socket->fileno, buf, len, ip, (uint16_t *)port, socket->timeout);
502516
if (ret == NINA_ERROR_TIMEOUT) {
503517
// The socket is Not closed on timeout when calling functions that accept a timeout.
504518
*_errno = MP_ETIMEDOUT;

0 commit comments

Comments
 (0)