|
37 | 37 | #include <net/net_context.h> |
38 | 38 | #include <net/net_pkt.h> |
39 | 39 | #include <net/dns_resolve.h> |
| 40 | +#ifdef CONFIG_NET_SOCKETS |
| 41 | +#include <net/socket.h> |
| 42 | +#endif |
40 | 43 |
|
41 | 44 | #define DEBUG_PRINT 0 |
42 | 45 | #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) |
103 | 106 | // Helper functions |
104 | 107 |
|
105 | 108 | #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); } |
106 | 110 |
|
107 | 111 | STATIC void socket_check_closed(socket_obj_t *socket) { |
108 | 112 | 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 |
256 | 260 | } |
257 | 261 | } |
258 | 262 |
|
| 263 | + #ifdef CONFIG_NET_SOCKETS |
| 264 | + socket->ctx = (void*)zsock_socket(family, socktype, proto); |
| 265 | + RAISE_SOCK_ERRNO(socket->ctx); |
| 266 | + #else |
259 | 267 | RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx)); |
260 | 268 | k_fifo_init(&SOCK_FIELD(socket, recv_q)); |
| 269 | + #endif |
261 | 270 |
|
262 | 271 | return MP_OBJ_FROM_PTR(socket); |
263 | 272 | } |
@@ -491,9 +500,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(socket_makefile_obj, 1, 3, socket_mak |
491 | 500 |
|
492 | 501 | STATIC mp_obj_t socket_close(mp_obj_t self_in) { |
493 | 502 | 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; |
497 | 507 | } |
498 | 508 | return mp_const_none; |
499 | 509 | } |
|
0 commit comments