3131#include "py/nlr.h"
3232#include "py/objlist.h"
3333#include "py/runtime.h"
34+ #include "py/stream.h"
35+ #include MICROPY_HAL_H
3436
3537#include "netutils.h"
3638
4345
4446#ifdef MICROPY_PY_LWIP_SLIP
4547#include "netif/slipif.h"
48+ #include "lwip/sio.h"
4649#endif
4750
4851// FIXME FIXME FIXME
5558
5659typedef struct _lwip_slip_obj_t {
5760 mp_obj_base_t base ;
58- u8_t uart_id ;
5961 struct netif lwip_netif ;
6062} lwip_slip_obj_t ;
6163
@@ -72,13 +74,39 @@ STATIC void slip_lwip_poll(void *netif) {
7274
7375STATIC const mp_obj_type_t lwip_slip_type ;
7476
77+ // lwIP SLIP callback functions
78+ sio_fd_t sio_open (u8_t dvnum ) {
79+ // We support singleton SLIP interface, so just return any truish value.
80+ return (sio_fd_t )1 ;
81+ }
82+
83+ void sio_send (u8_t c , sio_fd_t fd ) {
84+ mp_obj_type_t * type = mp_obj_get_type (MP_STATE_VM (lwip_slip_stream ));
85+ int error ;
86+ type -> stream_p -> write (MP_STATE_VM (lwip_slip_stream ), & c , 1 , & error );
87+ }
88+
89+ u32_t sio_tryread (sio_fd_t fd , u8_t * data , u32_t len ) {
90+ mp_obj_type_t * type = mp_obj_get_type (MP_STATE_VM (lwip_slip_stream ));
91+ int error ;
92+ mp_uint_t out_sz = type -> stream_p -> read (MP_STATE_VM (lwip_slip_stream ), data , len , & error );
93+ if (out_sz == MP_STREAM_ERROR ) {
94+ if (mp_is_nonblocking_error (error )) {
95+ return 0 ;
96+ }
97+ // Can't do much else, can we?
98+ return 0 ;
99+ }
100+ return out_sz ;
101+ }
102+
75103// constructor lwip.slip(device=integer, iplocal=string, ipremote=string)
76104STATIC mp_obj_t lwip_slip_make_new (mp_obj_t type_in , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * args ) {
77105 mp_arg_check_num (n_args , n_kw , 3 , 3 , false);
78106
79107 lwip_slip_obj .base .type = & lwip_slip_type ;
80108
81- lwip_slip_obj . uart_id = ( u8_t ) mp_obj_get_int ( args [0 ]) ;
109+ MP_STATE_VM ( lwip_slip_stream ) = args [0 ];
82110
83111 ip_addr_t iplocal , ipremote ;
84112 if (!ipaddr_aton (mp_obj_str_get_str (args [1 ]), & iplocal )) {
@@ -89,7 +117,7 @@ STATIC mp_obj_t lwip_slip_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
89117 }
90118
91119 struct netif * n = & (lwip_slip_obj .lwip_netif );
92- if (netif_add (n , & iplocal , IP_ADDR_BROADCAST , & ipremote , ( void * ) & lwip_slip_obj . uart_id , slipif_init , ip_input ) == NULL ) {
120+ if (netif_add (n , & iplocal , IP_ADDR_BROADCAST , & ipremote , NULL , slipif_init , ip_input ) == NULL ) {
93121 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , "out of memory" ));
94122 }
95123 netif_set_up (n );
0 commit comments