8000 zephyr/modusocket: Allow to use socketized net_context in upstream. · lable/micropython@642d9fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 642d9fd

Browse files
committed
zephyr/modusocket: Allow to use socketized net_context in upstream.
Accesses recv_q, accept_q directly in net_context.
1 parent 4dc7c56 commit 642d9fd

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

zephyr/modusocket.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@
4848
typedef struct _socket_obj_t {
4949
mp_obj_base_t base;
5050
struct net_context *ctx;
51+
#ifndef CONFIG_NET_SOCKETS
5152
union {
5253
struct k_fifo recv_q;
5354
struct k_fifo accept_q;
5455
};
56+
#endif
5557

5658
#define STATE_NEW 0
5759
#define STATE_CONNECTING 1
@@ -62,6 +64,12 @@ typedef struct _socket_obj_t {
6264

6365
STATIC const mp_obj_type_t socket_type;
6466

67+
#ifdef CONFIG_NET_SOCKETS
68+
#define SOCK_FIELD(ptr, field) ((ptr)->ctx->field)
69+
#else
70+
#define SOCK_FIELD(ptr, field) ((ptr)->field)
71+
#endif
72+
6573
// k_fifo extended API
6674

6775
static inline void *_k_fifo_peek_head(struct k_fifo *fifo)
@@ -171,10 +179,10 @@ static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, i
171179

172180
// if net_buf == NULL, EOF
173181
if (pkt == NULL) {
174-
struct net_pkt *last_pkt = _k_fifo_peek_tail(&socket->recv_q);
182+
struct net_pkt *last_pkt = _k_fifo_peek_tail(&SOCK_FIELD(socket, recv_q));
175183
if (last_pkt == NULL) {
176184
socket->state = STATE_PEER_CLOSED;
177-
k_fifo_cancel_wait(&socket->recv_q);
185+
k_fifo_cancel_wait(&SOCK_FIELD(socket, recv_q));
178186
DEBUG_printf("Marked socket %p as peer-closed\n", socket);
179187
} else {
180188
// We abuse "buf_sent" flag to store EOF flag
@@ -191,7 +199,7 @@ static void sock_received_cb(struct net_context *context, struct net_pkt *pkt, i
191199
unsigned header_len = net_pkt_appdata(pkt) - pkt->frags->data;
192200
net_buf_pull(pkt->frags, header_len);
193201

194-
k_fifo_put(&socket->recv_q, pkt);
202+
k_fifo_put(&SOCK_FIELD(socket, recv_q), pkt);
195203
}
196204

197205
// Callback for incoming connections.
@@ -200,13 +208,12 @@ static void sock_accepted_cb(struct net_context *new_ctx, struct sockaddr *addr,
200208
DEBUG_printf("accept cb: context: %p, status: %d, new ctx: %p\n", socket->ctx, status, new_ctx);
201209
DEBUG_printf("new_ctx ref_cnt: %d\n", new_ctx->refcount);
202210

203-
k_fifo_put(&socket->accept_q, new_ctx);
211+
k_fifo_put(&SOCK_FIELD(socket, accept_q), new_ctx);
204212
}
205213

206214
socket_obj_t *socket_new(void) {
207215
socket_obj_t *socket = m_new_obj_with_finaliser(socket_obj_t);
208216
socket->base.type = (mp_obj_t)&socket_type;
209-
k_fifo_init(&socket->recv_q);
210217
socket->state = STATE_NEW;
211218
return socket;
212219
}
@@ -250,6 +257,7 @@ STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, size_t
250257
}
251258

252259
RAISE_ERRNO(net_context_get(family, socktype, proto, &socket->ctx));
260+
k_fifo_init(&SOCK_FIELD(socket, recv_q));
253261

254262
return MP_OBJ_FROM_PTR(socket);
255263
}
@@ -302,7 +310,7 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
302310
socket_obj_t *socket = self_in;
303311
socket_check_closed(socket);
304312

305-
struct net_context *ctx = k_fifo_get(&socket->accept_q, K_FOREVER);
313+
struct net_context *ctx = k_fifo_get(&SOCK_FIELD(socket, accept_q), K_FOREVER);
306314
// Was overwritten by fifo
307315
ctx->refcount = 1;
308316

@@ -375,7 +383,7 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
375383

376384
if (sock_type == SOCK_DGRAM) {
377385

378-
struct net_pkt *pkt = k_fifo_get(&socket->recv_q, K_FOREVER);
386+
struct net_pkt *pkt = k_fifo_get(&SOCK_FIELD(socket, recv_q), K_FOREVER);
379387

380388
recv_len = net_pkt_appdatalen(pkt);
381389
DEBUG_printf("recv: pkt=%p, appdatalen: %d\n", pkt, recv_len);
@@ -395,8 +403,8 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
395403
return 0;
396404
}
397405

398-
_k_fifo_wait_non_empty(&socket->recv_q, K_FOREVER);
399-
struct net_pkt *pkt = _k_fifo_peek_head(&socket->recv_q);
406+
_k_fifo_wait_non_empty(&SOCK_FIELD(socket, recv_q), K_FOREVER);
407+
struct net_pkt *pkt = _k_fifo_peek_head(&SOCK_FIELD(socket, recv_q));
400408
if (pkt == NULL) {
401409
DEBUG_printf("TCP recv: NULL return from fifo\n");
402410
continue;
@@ -426,7 +434,7 @@ STATIC mp_uint_t sock_read(mp_obj_t self_in, void *buf, mp_uint_t max_len, int *
426434
if (frag == NULL) {
427435
DEBUG_printf("Finished processing pkt %p\n", pkt);
428436
// Drop head packet from queue
429-
k_fifo_get(&socket->recv_q, K_NO_WAIT);
437+
k_fifo_get(&SOCK_FIELD(socket, recv_q), K_NO_WAIT);
430438

431439
// If "sent" flag was set, it's last packet and we reached EOF
432440
if (net_pkt_sent(pkt)) {

zephyr/prj_base.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CONFIG_NET_IPV4=y
1414
CONFIG_NET_IPV6=y
1515
CONFIG_NET_UDP=y
1616
CONFIG_NET_TCP=y
17+
CONFIG_NET_SOCKETS=y
1718
CONFIG_TEST_RANDOM_GENERATOR=y
1819
CONFIG_NET_NBUF_RX_COUNT=5
1920

0 commit comments

Comments
 (0)
0