|
28 | 28 | #ifdef MICROPY_PY_USOCKET |
29 | 29 |
|
30 | 30 | #include "py/runtime.h" |
| 31 | +#include "py/stream.h" |
31 | 32 |
|
32 | 33 | #include <stdio.h> |
33 | 34 | #include <zephyr.h> |
@@ -300,29 +301,44 @@ STATIC mp_obj_t socket_accept(mp_obj_t self_in) { |
300 | 301 | } |
301 | 302 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(socket_accept_obj, socket_accept); |
302 | 303 |
|
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) { |
304 | 305 | 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 | + } |
309 | 311 |
|
310 | 312 | struct net_buf *send_buf = net_nbuf_get_tx(socket->ctx, K_FOREVER); |
311 | 313 |
|
312 | 314 | unsigned len = net_if_get_mtu(net_context_get_iface(socket->ctx)); |
313 | 315 | // Arbitrary value to account for protocol headers |
314 | 316 | len -= 64; |
315 | | - if (len > bufinfo.len) { |
316 | | - len = bufinfo.len; |
| 317 | + if (len > size) { |
| 318 | + len = size; |
317 | 319 | } |
318 | 320 |
|
319 | | - if (!net_nbuf_append(send_buf, len, bufinfo.buf, K_FOREVER)) { |
| 321 | + if (!net_nbuf_append(send_buf, len, buf, K_FOREVER)) { |
320 | 322 | len = net_buf_frags_len(send_buf); |
321 | | - //mp_raise_OSError(ENOSPC); |
322 | 323 | } |
323 | 324 |
|
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 | + } |
325 | 330 |
|
| 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 | + } |
326 | 342 | return mp_obj_new_int_from_uint(len); |
327 | 343 | } |
328 | 344 | 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[] = { |
436 | 452 | }; |
437 | 453 | STATIC MP_DEFINE_CONST_DICT(socket_locals_dict, socket_locals_dict_table); |
438 | 454 |
|
| 455 | +STATIC const mp_stream_p_t socket_stream_p = { |
| 456 | + //.read = sock_read, |
| 457 | + .write = sock_write, |
| 458 | + //.ioctl = sock_ioctl, |
| 459 | +}; |
| 460 | + |
439 | 461 | STATIC const mp_obj_type_t socket_type = { |
440 | 462 | { &mp_type_type }, |
441 | 463 | .name = MP_QSTR_socket, |
442 | 464 | .print = socket_print, |
443 | 465 | .make_new = socket_make_new, |
444 | | - //.protocol = &socket_stream_p, |
| 466 | + .protocol = &socket_stream_p, |
445 | 467 | .locals_dict = (mp_obj_t)&socket_locals_dict, |
446 | 468 | }; |
447 | 469 |
|
|
0 commit comments