8000 zephyr/modusocket: Fully switch to native Zephyr sockets. · lable/micropython@63edc2e · GitHub
[go: up one dir, main page]

Skip to content

Commit 63edc2e

Browse files
committed
zephyr/modusocket: Fully switch to native Zephyr sockets.
1 parent 3d25d9c commit 63edc2e

File tree

1 file changed

+8
-134
lines changed

1 file changed

+8
-134
lines changed

zephyr/modusocket.c

Lines changed: 8 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@
5050

5151
typedef struct _socket_obj_t {
5252
mp_obj_base_t base;
53-
struct net_context *ctx;
54-
#ifndef CONFIG_NET_SOCKETS
55-
union {
56-
struct k_fifo recv_q;
57-
struct k_fifo accept_q;
58-
};
59-
#endif
53+
int ctx;
6054

6155
#define STATE_NEW 0
6256
#define STATE_CONNECTING 1
@@ -67,49 +61,13 @@ typedef struct _socket_obj_t {
6761

6862
STATIC const mp_obj_type_t socket_type;
6963

70-
#ifdef CONFIG_NET_SOCKETS
71-
#define SOCK_FIELD(ptr, field) ((ptr)->ctx->field)
72-
#else
73-
#define SOCK_FIELD(ptr, field) ((ptr)->field)
74-
#endif
75-
76-
// k_fifo extended API
77-
78-
static inline void *_k_fifo_peek_head(struct k_fifo *fifo)
79-
{
80-
#if KERNEL_VERSION_NUMBER < 0x010763 /* 1.7.99 */
81-
return sys_slist_peek_head(&fifo->data_q);
82-
#else
83-
return sys_slist_peek_head(&fifo->_queue.data_q);
84-
#endif
85-
}
86-
87-
static inline void *_k_fifo_peek_tail(struct k_fifo *fifo)
88-
{
89-
#if KERNEL_VERSION_NUMBER < 0x010763 /* 1.7.99 */
90-
return sys_slist_peek_tail(&fifo->data_q);
91-
#else
92-
return sys_slist_peek_tail(&fifo->_queue.data_q);
93-
#endif
94-
}
95-
96-
static inline void _k_fifo_wait_non_empty(struct k_fifo *fifo, int32_t timeout)
97-
{
98-
struct k_poll_event events[] = {
99-
K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_FIFO_DATA_AVAILABLE, K_POLL_MODE_NOTIFY_ONLY, fifo),
100-
};
101-
102-
k_poll(events, MP_ARRAY_SIZE(events), timeout);
103-
DEBUG_printf("poll res: %d\n", events[0].state);
104-
}
105-
10664
// Helper functions
10765

10866
#define RAISE_ERRNO(x) { int _err = x; if (_err < 0) mp_raise_OSError(-_err); }
10967
#define RAISE_SOCK_ERRNO(x) { if ((int)(x) == -1) mp_raise_OSError(errno); }
11068

11169
STATIC void socket_check_closed(socket_obj_t *socket) {
112-
if (socket->ctx == NULL) {
70+
if (socket->ctx == -1) {
11371
// already closed
11472
mp_raise_OSError(EBADF);
11573
}
@@ -121,7 +79,7 @@ STATIC void parse_inet_addr(socket_obj_t *socket, mp_obj_t addr_in, struct socka
12179

12280
mp_obj_t *addr_items;
12381
mp_obj_get_array_fixed_n(addr_in, 2, &addr_items);
124-
sockaddr_in->sin_family = net_context_get_family(socket->ctx);
82+
sockaddr_in->sin_family = net_context_get_family((void*)socket->ctx);
12583
RAISE_ERRNO(net_addr_pton(sockaddr_in->sin_family, mp_obj_str_get_str(addr_items[0]), &sockaddr_in->sin_addr));
12684
sockaddr_in->sin_port = htons(mp_obj_get_int(addr_items[1]));
12785
}
@@ -147,74 +105,6 @@ STATIC mp_obj_t format_inet_addr(struct sockaddr *addr, mp_obj_t port) {
147105
return MP_OBJ_FROM_PTR(tuple);
148106
}
149107

150-
// Copy data from Zephyr net_buf chain into linear buffer.
151-
// We don't use net_pkt_read(), because it's weird (e.g., we'd like to
152-
// free processed data fragment ASAP, while net_pkt_read() holds onto
153-
// the whole fragment chain to do its deeds, and that's minor comparing
154-
// to the fact that it copies data byte by byte).
155-
static char *net_pkt_gather(struct net_pkt *pkt, char *to, unsigned max_len) {
156-
struct net_buf *tmp = pkt->frags;
157-
158-
while (tmp && max_len) {
159-
unsigned len = tmp->len;
160-
if (len > max_len) {
161-
len = max_len;
162-
}
163-
memcpy(to, tmp->data, len);
164-
to += len;
165-
max_len -= len;
166-
tmp = net_pkt_frag_del(pkt, NULL, tmp);
167-
}
168-
169-
return to;
170-
}
171-
172-
// Callback for incoming packets.
173-
static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, int < 9E81 span class=pl-s1>status, void *user_data) {
174-
socket_obj_t *socket = (socket_obj_t*)user_data;
175-
DEBUG_printf("recv cb: context: %p, status: %d, pkt: %p", context, status, pkt);
176-
if (pkt) {
177-
DEBUG_printf(" (appdatalen=%d), token: %p", pkt->appdatalen, net_pkt_token(pkt));
178-
}
179-
DEBUG_printf("\n");
180-
#if DEBUG_PRINT > 1
181-
net_pkt_print_frags(pkt);
182-
#endif
183-
184-
// if net_buf == NULL, EOF
185-
if (pkt == NULL) {
186-
struct net_pkt *last_pkt = _k_fifo_peek_tail(&SOCK_FIELD(socket, recv_q));
187-
if (last_pkt == NULL) {
188-
socket->state = STATE_PEER_CLOSED;
189-
k_fifo_cancel_wait(&SOCK_FIELD(socket, recv_q));
190-
DEBUG_printf("Marked socket %p as peer-closed\n", socket);
191-
} else {
192-
// We abuse "buf_sent" flag to store EOF flag
193-
net_pkt_set_sent(last_pkt, true);
194-
DEBUG_printf("Set EOF flag on %p\n", last_pkt);
195-
}
196-
return;
197-
}
198-
199-
// Make sure that "EOF flag" is not set
200 F438 -
net_pkt_set_sent(pkt, false);
201-
202-
// We don't care about packet header, so get rid of it asap
203-
unsigned header_len = net_pkt_appdata(pkt) - pkt->frags->data;
204-
net_buf_pull(pkt->frags, header_len);
205-
206-
k_fifo_put(&SOCK_FIELD(socket, recv_q), pkt);
207-
}
208-
209-
// Callback for incoming connections.
210-
static void sock_accepted_cb(struct net_context *new_ctx, struct sockaddr *addr, socklen_t addrlen, int status, void *user_data) {
211-
socket_obj_t *socket = (socket_obj_t*)user_data;
212-
DEBUG_printf("accept cb: context: %p, status: %d, new ctx: %p\n", socket->ctx, status, new_ctx);
213-
DEBUG_printf("new_ctx ref_cnt: %d\n", new_ctx->refcount);
214-
215-
k_fifo_put(&SOCK_FIELD(socket, accept_q), new_ctx);
216-
}
217-
218108
socket_obj_t *socket_new(void) {
219109
socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t);
220110
socket->base.type = (mp_obj_t)&socket_type;
@@ -226,10 +116,10 @@ socket_obj_t *socket_new(void) {
226116

227117
STATIC void socket_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
228118
socket_obj_t *self = self_in;
229-
if (self->ctx == NULL) {
119+
if (self->ctx == -1) {
230120
mp_printf(print, "<socket NULL>");
231121
} else {
232-
struct net_context *ctx = self->ctx;
122+
struct net_context *ctx = (void*)self->ctx;
233123
mp_printf(print, "<socket %p type=%d>", ctx, net_context_get_type(ctx));
234124
}
235125
}
@@ -260,13 +150,8 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t
260150
}
261151
}
262152

263-
#ifdef CONFIG_NET_SOCKETS
264-
socket->ctx = (void*)zsock_socket(family, socktype, proto);
153+
socket->ctx = zsock_socket(family, socktype, proto);
265154
RAISE_SOCK_ERRNO(socket->< 10000 /span>ctx);
266-
#else
267-
RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx));
268-
k_fifo_init(&SOCK_FIELD(socket, recv_q));
269-
#endif
270155

271156
return MP_OBJ_FROM_PTR(socket);
272157
}
@@ -281,13 +166,6 @@ STATIC mp_obj_t socket_bind(mp_obj_t self_in, mp_obj_t addr_in) {
281166
int res = zsock_bind(socket->ctx, &sockaddr, sizeof(sockaddr));
282167
RAISE_SOCK_ERRNO(res);
283168

284-
// For DGRAM socket, we expect to receive packets after call to bind(),
285-
// but for STREAM socket, next expected operation is listen(), which
286-
// doesn't work if recv callback is set.
287-
if (net_context_get_type(socket->ctx) == SOCK_DGRAM) {
288-
DEBUG_printf("Setting recv cb after bind\n");
289-
RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket));
290-
}
291169
return mp_const_none;
292170
}
293171
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_bind_obj, socket_bind);
@@ -302,8 +180,6 @@ STATIC mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
302180
int res = zsock_connect(socket->ctx, &sockaddr, sizeof(sockaddr));
303181
RAISE_SOCK_ERRNO(res);
304182

305-
DEBUG_printf("Setting recv cb after connect()\n");
306-
RAISE_ERRNO(net_context_recv(socket->ctx, sock_received_cb, K_NO_WAIT, socket));
307183
return mp_const_none;
308184
}
309185
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_connect_obj, socket_connect);
@@ -326,12 +202,10 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
326202

327203
struct sockaddr sockaddr;
328204
socklen_t addrlen = sizeof(sockaddr);
329-
void *ctx = zsock_accept(socket->ctx, &sockaddr, &addrlen);
205+
int ctx = zsock_accept(socket->ctx, &sockaddr, &addrlen);
330206

331207
socket_obj_t *socket2 = socket_new();
332208
socket2->ctx = ctx;
333-
DEBUG_printf("Setting recv cb after accept()\n");
334-
RAISE_ERRNO(net_context_recv(ctx, sock_received_cb, K_NO_WAIT, socket2));
335209

336210
mp_obj_tuple_t *client = mp_obj_new_tuple(2, NULL);
337211
client->items[0] = MP_OBJ_FROM_PTR(socket2);
@@ -373,7 +247,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
373247

374248
STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *errcode) {
375249
socket_obj_t *socket = self_in;
376-
if (socket->ctx == NULL) {
250+
if (socket->ctx == -1) {
377251
// already closed
378252
*errcode = EBADF;
379253
return MP_STREAM_ERROR;

0 commit comments

Comments
 (0)
0