8000 extmod/modlwip: slip: Use stream protocol and be port-independent. · micropython/micropython@e0d7740 · GitHub
[go: up one dir, main page]

Skip to content

Commit e0d7740

Browse files
author
Paul Sokolovsky
committed
extmod/modlwip: slip: Use stream protocol and be port-independent.
Based on the original patch by Galen Hazelwood: #1517 .
1 parent f3e46d0 commit e0d7740

File tree

6 files changed

+109
-3
lines changed

6 files changed

+109
-3
lines changed

extmod/modlwip.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
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

@@ -43,6 +45,7 @@
4345

4446
#ifdef MICROPY_PY_LWIP_SLIP
4547
#include "netif/slipif.h"
48+
#include "lwip/sio.h"
4649
#endif
4750

4851
// FIXME FIXME FIXME
@@ -55,7 +58,6 @@
5558

5659
typedef 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

7375
STATIC 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)
76104
STATIC 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);

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,6 @@ extern const mp_obj_module_t mp_module_uhashlib;
103103
extern const mp_obj_module_t mp_module_ubinascii;
104104
extern const mp_obj_module_t mp_module_ussl;
105105
extern const mp_obj_module_t mp_module_machine;
106+
extern const mp_obj_module_t mp_module_lwip;
106107

107108
#endif // __MICROPY_INCLUDED_PY_BUILTIN_H__

py/mpstate.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ typedef struct _mp_state_vm_t {
127127
// include any root pointers defined by a port
128128
MICROPY_PORT_ROOT_POINTERS
129129

130+
// root pointers for extmod
131+
#if MICROPY_PY_LWIP_SLIP
132+
mp_obj_t lwip_slip_stream;
133+
#endif
134+
130135
//
131136
// END ROOT POINTER SECTION
132137
////////////////////////////////////////////////////////////

py/objmodule.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
189189
#if MICROPY_PY_USSL
190190
{ MP_OBJ_NEW_QSTR(MP_QSTR_ussl), (mp_obj_t)&mp_module_ussl },
191191
#endif
192+
#if MICROPY_PY_LWIP
193+
{ MP_OBJ_NEW_QSTR(MP_QSTR_lwip), (mp_obj_t)&mp_module_lwip },
194+
#endif
192195

193196
// extra builtin modules as defined by a port
194197
MICROPY_PORT_BUILTIN_MODULES

py/py.mk

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,46 @@ CFLAGS_MOD += -DMICROPY_PY_USSL=1 -I../lib/axtls/ssl -I../lib/axtls/crypto -I../
1717
LDFLAGS_MOD += -L../lib/axtls/_stage -laxtls
1818
endif
1919

20+
#ifeq ($(MICROPY_PY_LWIP),1)
21+
#CFLAGS_MOD += -DMICROPY_PY_LWIP=1 -I../lib/lwip/src/include -I../lib/lwip/src/include/ipv4 -I../extmod/lwip-include
22+
#endif
23+
24+
ifeq ($(MICROPY_PY_LWIP),1)
25+
LWIP_DIR = lib/lwip/src
26+
INC += -I../lib/lwip/src/include -I../lib/lwip/src/include/ipv4 -I../extmod/lwip-include
27+
CFLAGS_MOD += -DMICROPY_PY_LWIP=1
28+
SRC_MOD += extmod/modlwip.c lib/netutils/netutils.c
29+
SRC_MOD += $(addprefix $(LWIP_DIR)/,\
30+
core/def.c \
31+
core/dns.c \
32+
core/init.c \
33+
core/mem.c \
34+
core/memp.c \
35+
core/netif.c \
36+
core/pbuf.c \
37+
core/raw.c \
38+
core/stats.c \
39+
core/sys.c \
40+
core/tcp.c \
41+
core/tcp_in.c \
42+
core/tcp_out.c \
43+
core/timers.c \
44+
core/udp.c \
45+
core/ipv4/autoip.c \
46+
core/ipv4/icmp.c \
47+
core/ipv4/igmp.c \
48+
core/ipv4/inet.c \
49+
core/ipv4/inet_chksum.c \
50+
core/ipv4/ip_addr.c \
51+
core/ipv4/ip.c \
52+
core/ipv4/ip_frag.c \
53+
)
54+
ifeq ($(MICROPY_PY_LWIP_SLIP),1)
55+
CFLAGS_MOD += -DMICROPY_PY_LWIP_SLIP=1
56+
SRC_MOD += $(LWIP_DIR)/netif/slipif.c
57+
endif
58+
endif
59+
2060
# py object files
2161
PY_O_BASENAME = \
2262
mpstate.o \

py/qstrdefs.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,32 @@ Q(mem32)
614614
Q(ussl)
615615
Q(wrap_socket)
616616
#endif
617+
618+
#if MICROPY_PY_LWIP
619+
// for lwip module
620+
Q(lwip)
621+
Q(reset)
622+
Q(callback)
623+
Q(socket)
624+
Q(AF_INET)
625+
Q(AF_INET6)
626+
Q(SOCK_STREAM)
627+
Q(SOCK_DGRAM)
628+
Q(SOCK_RAW)
629+
// for lwip.socket
630+
Q(close)
631+
Q(bind)
632+
Q(listen)
633+
Q(accept)
634+
Q(connect)
635+
Q(send)
636+
Q(recv)
637+
Q(sendto)
638+
Q(recvfrom)
639+
Q(settimeout)
640+
#if MICROPY_PY_LWIP_SLIP
641+
// for lwip.slip
642+
Q(slip)
643+
Q(status)
644+
#endif
645+
#endif

0 commit comments

Comments
 (0)
0