Skip to content

Commit 6c55cda

Browse files
committed
zephyr/modusocket: socket, close: Switch to native Zephyr socket calls.
1 parent 642d9fd commit 6c55cda

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

zephyr/modusocket.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#include <net/net_context.h>
3838
#include <net/net_pkt.h>
3939
#include <net/dns_resolve.h>
40+
#ifdef CONFIG_NET_SOCKETS
41+
#include <net/socket.h>
42+
#endif
4043

4144
#define DEBUG_PRINT 0
4245
#if DEBUG_PRINT // print debugging info
@@ -103,6 +106,7 @@ static inline void _k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout)
103106
// Helper functions
104107

105108
#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); }
109+
#define RAISE_SOCK_ERRNO(x) { if ((int)(x) == -1) mp_raise_OSError(errno); }
106110

107111
STATIC void socket_check_closed(socket_obj_t *socket) {
108112
if (socket->ctx == NULL) {
@@ -256,8 +260,13 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t
256260
}
257261
}
258262

263+
#ifdef CONFIG_NET_SOCKETS
264+
socket->ctx = (void*)zsock_socket(family, socktype, proto);
265+
RAISE_SOCK_ERRNO(socket->ctx);
266+
#else
259267
RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx));
260268
k_fifo_init(&SOCK_FIELD(socket, recv_q));
269+
#endif
261270

262271
return MP_OBJ_FROM_PTR(socket);
263272
}
@@ -491,9 +500,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_mak
491500

492501
STATIC mp_obj_t socket_close(mp_obj_t self_in) {
493502
socket_obj_t *socket = self_in;
494-
if (socket->ctx != NULL) {
495-
RAISE_ERRNO(net_context_put(socket->ctx));
496-
socket->ctx = NULL;
503+
if (socket->ctx != -1) {
504+
int res = zsock_close(socket->ctx);
505+
RAISE_SOCK_ERRNO(res);
506+
socket->ctx = -1;
497507
}
498508
return mp_const_none;
499509
}

0 commit comments

Comments
 (0)