Skip to content

Commit 0e177e0

Browse files
committed
zephyr/modusocket: Refactor send() into stream write() method.
1 parent 083cd21 commit 0e177e0

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

zephyr/modusocket.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#ifdef MICROPY_PY_USOCKET
2929

3030
#include "py/runtime.h"
31+
#include "py/stream.h"
3132

3233
#include <stdio.h>
3334
#include <zephyr.h>
@@ -300,29 +301,44 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) {
300301
}
301302
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept);
302303

303-
STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
304+
STATIC mp_uint_t sock_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
304305
socket_obj_t *socket = self_in;
305-
socket_check_closed(socket);
306-
307-
mp_buffer_info_t bufinfo;
308-
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
306+
if (socket->ctx == NULL) {
307+
// already closed
308+
*errcode = EBADF;
309+
return MP_STREAM_ERROR;
310+
}
309311

310312
struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER);
311313

312314
unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx));
313315
// Arbitrary value to account for protocol headers
314316
len -= 64;
315-
if (len > bufinfo.len) {
316-
len = bufinfo.len;
317+
if (len > size) {
318+
len = size;
317319
}
318320

319-
if (!net_nbuf_append(send_buf, len, bufinfo.buf, K_FOREVER)) {
321+
if (!net_nbuf_append(send_buf, len, buf, K_FOREVER)) {
320322
len = net_buf_frags_len(send_buf);
321-
//mp_raise_OSError(ENOSPC);
322323
}
323324

324-
RAISE_ERRNO(net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL));
325+
int err = net_context_send(send_buf, /*cb*/NULL, K_FOREVER, NULL, NULL);
326+
if (err < 0) {
327+
*errcode = -err;
328+
return MP_STREAM_ERROR;
329+
}
325330

331+
return len;
332+
}
333+
334+
STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) {
335+
mp_buffer_info_t bufinfo;
336+
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
337+
int err = 0;
338+
mp_uint_t len = sock_write(self_in, bufinfo.buf, bufinfo.len, &err);
339+
if (len == MP_STREAM_ERROR) {
340+
mp_raise_OSError(err);
341+
}
326342
return mp_obj_new_int_from_uint(len);
327343
}
328344
STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_send_obj, socket_send);
@@ -436,12 +452,18 @@ STATIC const mp_map_elem_t socket_locals_dict_table[] = {
436452
};
437453
STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table);
438454

455+
STATIC const mp_stream_p_t socket_stream_p = {
456+
//.read = sock_read,
457+
.write = sock_write,
458+
//.ioctl = sock_ioctl,
459+
};
460+
439461
STATIC const mp_obj_type_t socket_type = {
440462
{ &mp_type_type },
441463
.name = MP_QSTR_socket,
442464
.print = socket_print,
443465
.make_new = socket_make_new,
444-
//.protocol = &socket_stream_p,
466+
.protocol = &socket_stream_p,
445467
.locals_dict = (mp_obj_t)&socket_locals_dict,
446468
};
447469

0 commit comments

Comments
 (0)