From a89ac9e24a566e45bd3ffa7ef9e61e1d994616ea Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Apr 2024 16:50:26 +1000 Subject: [PATCH 01/10] lib/lwip: Update lwIP to STABLE-2_2_0_RELEASE. This updates lwIP from STABLE-2_1_3_RELEASE, which was released in November 2021. The latest STABLE-2_2_0_RELEASE was released in September 2023. Signed-off-by: Damien George --- lib/lwip | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/lwip b/lib/lwip index 6ca936f6b588c..0a0452b2c39bd 160000 --- a/lib/lwip +++ b/lib/lwip @@ -1 +1 @@ -Subproject commit 6ca936f6b588cee702c638eee75c2436e6cf75de +Subproject commit 0a0452b2c39bdd91e252aef045c115f88f6ca773 From 664dd7b54a220ba6081a8a2d28103cdd1c74b034 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 16 Apr 2024 16:50:52 +1000 Subject: [PATCH 02/10] extmod: Update make and cmake scripts to work with latest lwIP. Signed-off-by: Damien George --- extmod/extmod.cmake | 3 +++ extmod/extmod.mk | 3 +++ 2 files changed, 6 insertions(+) diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index 60f1a23ad6b22..69ee0e3ae460d 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -275,11 +275,14 @@ if(MICROPY_PY_LWIP) target_sources(micropy_lib_lwip INTERFACE ${MICROPY_DIR}/shared/netutils/netutils.c ${MICROPY_LIB_LWIP_DIR}/apps/mdns/mdns.c + ${MICROPY_LIB_LWIP_DIR}/apps/mdns/mdns_domain.c + ${MICROPY_LIB_LWIP_DIR}/apps/mdns/mdns_out.c ${MICROPY_LIB_LWIP_DIR}/core/def.c ${MICROPY_LIB_LWIP_DIR}/core/dns.c ${MICROPY_LIB_LWIP_DIR}/core/inet_chksum.c ${MICROPY_LIB_LWIP_DIR}/core/init.c ${MICROPY_LIB_LWIP_DIR}/core/ip.c + ${MICROPY_LIB_LWIP_DIR}/core/ipv4/acd.c ${MICROPY_LIB_LWIP_DIR}/core/ipv4/autoip.c ${MICROPY_LIB_LWIP_DIR}/core/ipv4/dhcp.c ${MICROPY_LIB_LWIP_DIR}/core/ipv4/etharp.c diff --git a/extmod/extmod.mk b/extmod/extmod.mk index c2298bc52130f..3bed5961f87c2 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -332,6 +332,8 @@ $(BUILD)/$(LWIP_DIR)/core/ipv4/dhcp.o: CFLAGS += -Wno-address SRC_THIRDPARTY_C += shared/netutils/netutils.c SRC_THIRDPARTY_C += $(addprefix $(LWIP_DIR)/,\ apps/mdns/mdns.c \ + apps/mdns/mdns_domain.c \ + apps/mdns/mdns_out.c \ core/def.c \ core/dns.c \ core/inet_chksum.c \ @@ -349,6 +351,7 @@ SRC_THIRDPARTY_C += $(addprefix $(LWIP_DIR)/,\ core/tcp_out.c \ core/timeouts.c \ core/udp.c \ + core/ipv4/acd.c \ core/ipv4/autoip.c \ core/ipv4/dhcp.c \ core/ipv4/etharp.c \ From bc952d37fe957adf0165b3403a97d5ada7e8e1a4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 13 Mar 2024 17:09:51 +1100 Subject: [PATCH 03/10] extmod/network_ppp_lwip: Add network.PPP via lwIP. This commit adds a new `network.PPP` interface which works on any port that has bare-metal lwIP, eg rp2, stm32, mimxrt. It has been tested on stm32. A board needs to enable `MICROPY_PY_NETWORK_PPP_LWIP` and then it can use it as follows: import network ppp = network.PPP(uart) ppp.connect() while not ppp.isconnected(): pass # use `socket` module as usual, etc ppp.disconnect() Usually the application must first configure the cellular/etc UART link to get it connected and in to PPP mode first (eg ATD*99#), before handing over control to `network.PPP`. The PPP interface automatically configures the UART IRQ callback to call PPP.poll() on incoming data. Signed-off-by: Damien George --- extmod/extmod.cmake | 27 ++++ extmod/extmod.mk | 27 ++++ extmod/modnetwork.c | 4 + extmod/modnetwork.h | 4 + extmod/network_ppp_lwip.c | 326 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 388 insertions(+) create mode 100644 extmod/network_ppp_lwip.c diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index 69ee0e3ae460d..98e8a84608a56 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -49,6 +49,7 @@ set(MICROPY_SOURCE_EXTMOD ${MICROPY_EXTMOD_DIR}/network_cyw43.c ${MICROPY_EXTMOD_DIR}/network_lwip.c ${MICROPY_EXTMOD_DIR}/network_ninaw10.c + ${MICROPY_EXTMOD_DIR}/network_ppp_lwip.c ${MICROPY_EXTMOD_DIR}/network_wiznet5k.c ${MICROPY_EXTMOD_DIR}/os_dupterm.c ${MICROPY_EXTMOD_DIR}/vfs.c @@ -313,6 +314,32 @@ if(MICROPY_PY_LWIP) ${MICROPY_LIB_LWIP_DIR}/core/timeouts.c ${MICROPY_LIB_LWIP_DIR}/core/udp.c ${MICROPY_LIB_LWIP_DIR}/netif/ethernet.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/auth.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/ccp.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/chap-md5.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/chap_ms.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/chap-new.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/demand.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/eap.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/ecp.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/eui64.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/fsm.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/ipcp.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/ipv6cp.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/lcp.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/magic.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/mppe.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/multilink.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/polarssl/md5.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/pppapi.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/ppp.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/pppcrypt.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/pppoe.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/pppol2tp.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/pppos.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/upap.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/utils.c + ${MICROPY_LIB_LWIP_DIR}/netif/ppp/vj.c ) list(APPEND MICROPY_INC_CORE diff --git a/extmod/extmod.mk b/extmod/extmod.mk index 3bed5961f87c2..c132fd89ce887 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -51,6 +51,7 @@ SRC_EXTMOD_C += \ extmod/network_esp_hosted.c \ extmod/network_lwip.c \ extmod/network_ninaw10.c \ + extmod/network_ppp_lwip.c \ extmod/network_wiznet5k.c \ extmod/os_dupterm.c \ extmod/vfs.c \ @@ -370,6 +371,32 @@ SRC_THIRDPARTY_C += $(addprefix $(LWIP_DIR)/,\ core/ipv6/mld6.c \ core/ipv6/nd6.c \ netif/ethernet.c \ + netif/ppp/auth.c \ + netif/ppp/ccp.c \ + netif/ppp/chap-md5.c \ + netif/ppp/chap_ms.c \ + netif/ppp/chap-new.c \ + netif/ppp/demand.c \ + netif/ppp/eap.c \ + netif/ppp/ecp.c \ + netif/ppp/eui64.c \ + netif/ppp/fsm.c \ + netif/ppp/ipcp.c \ + netif/ppp/ipv6cp.c \ + netif/ppp/lcp.c \ + netif/ppp/magic.c \ + netif/ppp/mppe.c \ + netif/ppp/multilink.c \ + netif/ppp/polarssl/md5.c \ + netif/ppp/pppapi.c \ + netif/ppp/ppp.c \ + netif/ppp/pppcrypt.c \ + netif/ppp/pppoe.c \ + netif/ppp/pppol2tp.c \ + netif/ppp/pppos.c \ + netif/ppp/upap.c \ + netif/ppp/utils.c \ + netif/ppp/vj.c \ ) ifeq ($(MICROPY_PY_LWIP_LOOPBACK),1) CFLAGS_EXTMOD += -DLWIP_NETIF_LOOPBACK=1 diff --git a/extmod/modnetwork.c b/extmod/modnetwork.c index f3d7d0faa8e1d..336836b6b8694 100644 --- a/extmod/modnetwork.c +++ b/extmod/modnetwork.c @@ -156,6 +156,10 @@ static const mp_rom_map_elem_t mp_module_network_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&mod_network_ipconfig_obj) }, #endif + #if MICROPY_PY_NETWORK_PPP_LWIP + { MP_ROM_QSTR(MP_QSTR_PPP), MP_ROM_PTR(&mp_network_ppp_lwip_type) }, + #endif + // Defined per port in mpconfigport.h #ifdef MICROPY_PORT_NETWORK_INTERFACES { MP_ROM_QSTR(MP_QSTR_route), MP_ROM_PTR(&network_route_obj) }, diff --git a/extmod/modnetwork.h b/extmod/modnetwork.h index 1a4aa7797e455..7e5a283353724 100644 --- a/extmod/modnetwork.h +++ b/extmod/modnetwork.h @@ -72,6 +72,10 @@ mp_obj_t mod_network_hostname(size_t n_args, const mp_obj_t *args); #include "lwip/init.h" +#if MICROPY_PY_NETWORK_PPP_LWIP +extern const struct _mp_obj_type_t mp_network_ppp_lwip_type; +#endif + struct netif; void mod_network_lwip_init(void); void mod_network_lwip_poll_wrapper(uint32_t ticks_ms); diff --git a/extmod/network_ppp_lwip.c b/extmod/network_ppp_lwip.c new file mode 100644 index 0000000000000..2b77662a24f26 --- /dev/null +++ b/extmod/network_ppp_lwip.c @@ -0,0 +1,326 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2024 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" +#include "py/mphal.h" +#include "py/stream.h" +#include "extmod/modnetwork.h" + +#if MICROPY_PY_NETWORK_PPP_LWIP + +#include "lwip/dns.h" +#include "netif/ppp/ppp.h" +#include "netif/ppp/pppapi.h" +#include "netif/ppp/pppos.h" + +// Enable this to see the serial data going between the PPP layer. +#define PPP_TRACE_IN_OUT (0) + +typedef enum { + STATE_INACTIVE, + STATE_ACTIVE, + STATE_ERROR, + STATE_CONNECTING, + STATE_CONNECTED, +} network_ppp_state_t; + +typedef struct _network_ppp_obj_t { + mp_obj_base_t base; + network_ppp_state_t state; + int error_code; + mp_obj_t stream; + ppp_pcb *pcb; + struct netif netif; +} network_ppp_obj_t; + +const mp_obj_type_t mp_network_ppp_lwip_type; + +static mp_obj_t network_ppp___del__(mp_obj_t self_in); + +static void network_ppp_status_cb(ppp_pcb *pcb, int err_code, void *ctx) { + network_ppp_obj_t *self = ctx; + switch (err_code) { + case PPPERR_NONE: + self->state = STATE_CONNECTED; + break; + case PPPERR_USER: + if (self->state >= STATE_ERROR) { + // Disable UART IRQ. + mp_obj_t dest[3]; + mp_load_method(self->stream, MP_QSTR_irq, dest); + dest[2] = mp_const_none; + mp_call_method_n_kw(1, 0, dest); + // Indicate that the IRQ is disabled. + self->state = STATE_ACTIVE; + } + // Clean up the PPP PCB. + network_ppp___del__(MP_OBJ_FROM_PTR(self)); + break; + default: + self->state = STATE_ERROR; + self->error_code = err_code; + break; + } +} + +static mp_obj_t network_ppp_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + mp_arg_check_num(n_args, n_kw, 1, 1, false); + + mp_obj_t stream = all_args[0]; + + mp_get_stream_raise(stream, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE); + + network_ppp_obj_t *self = mp_obj_malloc_with_finaliser(network_ppp_obj_t, type); + self->state = STATE_INACTIVE; + self->stream = stream; + self->pcb = NULL; + + return MP_OBJ_FROM_PTR(self); +} + +static mp_obj_t network_ppp___del__(mp_obj_t self_in) { + network_ppp_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self->state >= STATE_ACTIVE) { + if (self->state >= STATE_ERROR) { + // Still connected over the UART stream. + // Force the connection to close, with nocarrier=1. + self->state = STATE_INACTIVE; + ppp_close(self->pcb, 1); + } + // Free PPP PCB and reset state. + self->state = STATE_INACTIVE; + ppp_free(self->pcb); + self->pcb = NULL; + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(network_ppp___del___obj, network_ppp___del__); + +static mp_obj_t network_ppp_poll(size_t n_args, const mp_obj_t *args) { + network_ppp_obj_t *self = MP_OBJ_TO_PTR(args[0]); + + if (self->state <= STATE_ERROR) { + return MP_OBJ_NEW_SMALL_INT(-MP_EPERM); + } + + mp_int_t total_len = 0; + for (;;) { + uint8_t buf[256]; + int err; + mp_uint_t len = mp_stream_rw(self->stream, buf, sizeof(buf), &err, 0); + if (len == 0) { + break; + } + #if PPP_TRACE_IN_OUT + mp_printf(&mp_plat_print, "ppp_in(n=%u,data=", len); + for (size_t i = 0; i < len; ++i) { + mp_printf(&mp_plat_print, "%02x:", buf[i]); + } + mp_printf(&mp_plat_print, ")\n"); + #endif + pppos_input(self->pcb, (u8_t *)buf, len); + total_len += len; + } + + return MP_OBJ_NEW_SMALL_INT(total_len); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_ppp_poll_obj, 1, 2, network_ppp_poll); + +static mp_obj_t network_ppp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { + if (n_args != 1 && kwargs->used != 0) { + mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed")); + } + // network_ppp_obj_t *self = MP_OBJ_TO_PTR(args[0]); + + if (kwargs->used != 0) { + for (size_t i = 0; i < kwargs->alloc; i++) { + if (mp_map_slot_is_filled(kwargs, i)) { + switch (mp_obj_str_get_qstr(kwargs->table[i].key)) { + default: + break; + } + } + } + return mp_const_none; + } + + if (n_args != 2) { + mp_raise_TypeError(MP_ERROR_TEXT("can query only one param")); + } + + mp_obj_t val = mp_const_none; + + switch (mp_obj_str_get_qstr(args[1])) { + default: + mp_raise_ValueError(MP_ERROR_TEXT("unknown config param")); + } + + return val; +} +static MP_DEFINE_CONST_FUN_OBJ_KW(network_ppp_config_obj, 1, network_ppp_config); + +static mp_obj_t network_ppp_status(mp_obj_t self_in) { + network_ppp_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self->state == STATE_ERROR) { + return MP_OBJ_NEW_SMALL_INT(-self->error_code); + } else { + return MP_OBJ_NEW_SMALL_INT(self->state); + } +} +static MP_DEFINE_CONST_FUN_OBJ_1(network_ppp_status_obj, network_ppp_status); + +static u32_t network_ppp_output_callback(ppp_pcb *pcb, const void *data, u32_t len, void *ctx) { + network_ppp_obj_t *self = ctx; + #if PPP_TRACE_IN_OUT + mp_printf(&mp_plat_print, "ppp_out(n=%u,data=", len); + for (size_t i = 0; i < len; ++i) { + mp_printf(&mp_plat_print, "%02x:", ((const uint8_t *)data)[i]); + } + mp_printf(&mp_plat_print, ")\n"); + #endif + int err; + // The return value from this output callback is the number of bytes written out. + // If it's less than the requested number of bytes then lwIP will propagate out an error. + return mp_stream_rw(self->stream, (void *)data, len, &err, MP_STREAM_RW_WRITE); +} + +static mp_obj_t network_ppp_connect(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { + enum { ARG_security, ARG_user, ARG_key }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_security, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PPPAUTHTYPE_NONE} }, + { MP_QSTR_user, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + { MP_QSTR_key, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} }, + }; + + mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args); + + network_ppp_obj_t *self = MP_OBJ_TO_PTR(args[0]); + + if (self->state == STATE_INACTIVE) { + self->pcb = pppos_create(&self->netif, network_ppp_output_callback, network_ppp_status_cb, self); + if (self->pcb == NULL) { + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("pppos_create failed")); + } + self->state = STATE_ACTIVE; + + // Enable UART IRQ to call PPP.poll() when incoming data is ready. + mp_obj_t dest[4]; + mp_load_method(self->stream, MP_QSTR_irq, dest); + dest[2] = mp_obj_new_bound_meth(MP_OBJ_FROM_PTR(&network_ppp_poll_obj), MP_OBJ_FROM_PTR(self)); + dest[3] = mp_load_attr(self->stream, MP_QSTR_IRQ_RXIDLE); + mp_call_method_n_kw(2, 0, dest); + } + + if (self->state == STATE_CONNECTING || self->state == STATE_CONNECTED) { + mp_raise_OSError(MP_EALREADY); + } + + switch (parsed_args[ARG_security].u_int) { + case PPPAUTHTYPE_NONE: + case PPPAUTHTYPE_PAP: + case PPPAUTHTYPE_CHAP: + break; + default: + mp_raise_ValueError(MP_ERROR_TEXT("invalid auth")); + } + + if (parsed_args[ARG_security].u_int != PPPAUTHTYPE_NONE) { + const char *user_str = mp_obj_str_get_str(parsed_args[ARG_user].u_obj); + const char *key_str = mp_obj_str_get_str(parsed_args[ARG_key].u_obj); + ppp_set_auth(self->pcb, parsed_args[ARG_security].u_int, user_str, key_str); + } + + netif_set_default(self->pcb->netif); + ppp_set_usepeerdns(self->pcb, true); + + if (ppp_connect(self->pcb, 0) != ERR_OK) { + mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("ppp_connect failed")); + } + + self->state = STATE_CONNECTING; + + // Do a poll in case there is data waiting on the input stream. + network_ppp_poll(1, args); + + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_KW(network_ppp_connect_obj, 1, network_ppp_connect); + +static mp_obj_t network_ppp_disconnect(mp_obj_t self_in) { + network_ppp_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self->state == STATE_CONNECTING || self->state == STATE_CONNECTED) { + // Initiate close and wait for PPPERR_USER callback. + ppp_close(self->pcb, 0); + } + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(network_ppp_disconnect_obj, network_ppp_disconnect); + +static mp_obj_t network_ppp_isconnected(mp_obj_t self_in) { + network_ppp_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(self->state == STATE_CONNECTED); +} +static MP_DEFINE_CONST_FUN_OBJ_1(network_ppp_isconnected_obj, network_ppp_isconnected); + +static mp_obj_t network_ppp_ifconfig(size_t n_args, const mp_obj_t *args) { + network_ppp_obj_t *self = MP_OBJ_TO_PTR(args[0]); + return mod_network_nic_ifconfig(&self->netif, n_args - 1, args + 1); +} +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(network_ppp_ifconfig_obj, 1, 2, network_ppp_ifconfig); + +static mp_obj_t network_ppp_ipconfig(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { + network_ppp_obj_t *self = MP_OBJ_TO_PTR(args[0]); + return mod_network_nic_ipconfig(&self->netif, n_args - 1, args + 1, kwargs); +} +static MP_DEFINE_CONST_FUN_OBJ_KW(network_ppp_ipconfig_obj, 1, network_ppp_ipconfig); + +static const mp_rom_map_elem_t network_ppp_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&network_ppp___del___obj) }, + { MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&network_ppp_config_obj) }, + { MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&network_ppp_status_obj) }, + { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&network_ppp_connect_obj) }, + { MP_ROM_QSTR(MP_QSTR_disconnect), MP_ROM_PTR(&network_ppp_disconnect_obj) }, + { MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&network_ppp_isconnected_obj) }, + { MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&network_ppp_ifconfig_obj) }, + { MP_ROM_QSTR(MP_QSTR_ipconfig), MP_ROM_PTR(&network_ppp_ipconfig_obj) }, + { MP_ROM_QSTR(MP_QSTR_poll), MP_ROM_PTR(&network_ppp_poll_obj) }, + + { MP_ROM_QSTR(MP_QSTR_SEC_NONE), MP_ROM_INT(PPPAUTHTYPE_NONE) }, + { MP_ROM_QSTR(MP_QSTR_SEC_PAP), MP_ROM_INT(PPPAUTHTYPE_PAP) }, + { MP_ROM_QSTR(MP_QSTR_SEC_CHAP), MP_ROM_INT(PPPAUTHTYPE_CHAP) }, +}; +static MP_DEFINE_CONST_DICT(network_ppp_locals_dict, network_ppp_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + mp_network_ppp_lwip_type, + MP_QSTR_PPP, + MP_TYPE_FLAG_NONE, + make_new, network_ppp_make_new, + locals_dict, &network_ppp_locals_dict + ); + +#endif From a5cc4d46237bf5c905bdddd0a117b8fea35b7f7d Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 Mar 2024 09:27:16 +1100 Subject: [PATCH 04/10] stm32: Integrate optional network.PPP. Can be enabled by a board by enabling `MICROPY_PY_NETWORK_PPP_LWIP`. Signed-off-by: Damien George --- ports/stm32/lwip_inc/lwipopts.h | 11 ++++++++++- ports/stm32/mpconfigport.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ports/stm32/lwip_inc/lwipopts.h b/ports/stm32/lwip_inc/lwipopts.h index f641cf515add2..b779b558c9ec7 100644 --- a/ports/stm32/lwip_inc/lwipopts.h +++ b/ports/stm32/lwip_inc/lwipopts.h @@ -1,7 +1,7 @@ #ifndef MICROPY_INCLUDED_STM32_LWIP_LWIPOPTS_H #define MICROPY_INCLUDED_STM32_LWIP_LWIPOPTS_H -#include +#include "py/mpconfig.h" // This protection is not needed, instead we execute all lwIP code at PendSV priority #define SYS_ARCH_DECL_PROTECT(lev) do { } while (0) @@ -36,6 +36,12 @@ #define LWIP_MDNS_RESPONDER 1 #define LWIP_IGMP 1 +#if MICROPY_PY_LWIP_PPP +#define PPP_SUPPORT 1 +#define PAP_SUPPORT 1 +#define CHAP_SUPPORT 1 +#endif + #define LWIP_NUM_NETIF_CLIENT_DATA LWIP_MDNS_RESPONDER #define MEMP_NUM_UDP_PCB (4 + LWIP_MDNS_RESPONDER) #define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + LWIP_MDNS_RESPONDER) @@ -77,4 +83,7 @@ extern uint32_t rng_get(void); typedef uint32_t sys_prot_t; +// Needed for PPP. +#define sys_jiffies sys_now + #endif // MICROPY_INCLUDED_STM32_LWIP_LWIPOPTS_H diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index 25fc9e11f9d68..c6ba83d1bd293 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -106,6 +106,7 @@ #define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1) #define MICROPY_PY_TIME_TIME_TIME_NS (1) #define MICROPY_PY_TIME_INCLUDEFILE "ports/stm32/modtime.c" +#define MICROPY_PY_LWIP_PPP (MICROPY_PY_NETWORK_PPP_LWIP) #define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP) #ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (1) From c94a3205b044fb27fa703d5c280fb02a094f12e3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Aug 2024 17:47:46 +1000 Subject: [PATCH 05/10] stm32/machine_uart: Allow changing only the baudrate. Signed-off-by: Damien George --- ports/stm32/machine_uart.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 277ae67ef2c54..9bd4c5e012527 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -154,6 +154,12 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, { MP_QSTR_read_buf_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} }, // legacy }; + if (self->is_enabled && n_args == 1 && kw_args->used == 0) { + // Only change the baudrate if that's all that is given. + uart_set_baudrate(self, mp_obj_get_int(pos_args[0])); + return; + } + // parse args struct { mp_arg_val_t baudrate, bits, parity, stop, flow, timeout, timeout_char, rxbuf, read_buf_len; From d8b033776e59dad8cc32d7381e0186b7677440c0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Aug 2024 17:48:13 +1000 Subject: [PATCH 06/10] stm32/machine_uart: Return number of bytes written even with timeout. The errcode should be cleared so the caller sees a successful write, even if it's a short write. Signed-off-by: Damien George --- ports/stm32/machine_uart.c | 1 + 1 file changed, 1 insertion(+) diff --git a/ports/stm32/machine_uart.c b/ports/stm32/machine_uart.c index 9bd4c5e012527..6be6bcdac1a12 100644 --- a/ports/stm32/machine_uart.c +++ b/ports/stm32/machine_uart.c @@ -533,6 +533,7 @@ static mp_uint_t mp_machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_ if (*errcode == 0 || *errcode == MP_ETIMEDOUT) { // return number of bytes written, even if there was a timeout + *errcode = 0; return num_tx << self->char_width; } else { return MP_STREAM_ERROR; From a1a16ffd75e1cc5ca977a3d799d5c7de0da5e585 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 7 Aug 2024 17:48:33 +1000 Subject: [PATCH 07/10] stm32/uart: Use timeout_char even with CTS enabled. When timeout=0 (non-blocking mode) the UART should still wait for each character to go out. Otherwise non-blocking mode with CTS enabled is useless because it can only write one character at a time. Signed-off-by: Damien George --- ports/stm32/uart.c | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/ports/stm32/uart.c b/ports/stm32/uart.c index 886cf1ab340ba..855f44f0f2345 100644 --- a/ports/stm32/uart.c +++ b/ports/stm32/uart.c @@ -1069,26 +1069,20 @@ size_t uart_tx_data(machine_uart_obj_t *self, const void *src_in, size_t num_cha } uint32_t timeout; - if (self->uartx->CR3 & USART_CR3_CTSE) { - // CTS can hold off transmission for an arbitrarily long time. Apply - // the overall timeout rather than the character timeout. - timeout = self->timeout; - } else { - #if defined(STM32G4) - // With using UART FIFO, the timeout should be long enough that FIFO becomes empty. - // Since previous data transfer may be ongoing, the timeout must be multiplied - // timeout_char by FIFO size + 1. - // STM32G4 has 8 words FIFO. - timeout = (8 + 1) * self->timeout_char; - #else - // The timeout specified here is for waiting for the TX data register to - // become empty (ie between chars), as well as for the final char to be - // completely transferred. The default value for timeout_char is long - // enough for 1 char, but we need to double it to wait for the last char - // to be transferred to the data register, and then to be transmitted. - timeout = 2 * self->timeout_char; - #endif - } + #if defined(STM32G4) + // With using UART FIFO, the timeout should be long enough that FIFO becomes empty. + // Since previous data transfer may be ongoing, the timeout must be multiplied + // timeout_char by FIFO size + 1. + // STM32G4 has 8 words FIFO. + timeout = (8 + 1) * self->timeout_char; + #else + // The timeout specified here is for waiting for the TX data register to + // become empty (ie between chars), as well as for the final char to be + // completely transferred. The default value for timeout_char is long + // enough for 1 char, but we need to double it to wait for the last char + // to be transferred to the data register, and then to be transmitted. + timeout = 2 * self->timeout_char; + #endif const uint8_t *src = (const uint8_t *)src_in; size_t num_tx = 0; From aee002dd80ad8829ae247950f784b1f145fb822e Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 20 Aug 2024 15:55:07 +1000 Subject: [PATCH 08/10] stm32/lwip_inc: Implement LWIP_PLATFORM_DIAG macro in terms of printf. This allows enabling lwIP debugging output. For example, to enable PPP debugging add the following to `mpconfigboard.h`: #define LWIP_DEBUG 1 #define PPP_DEBUG LWIP_DBG_ON Signed-off-by: Damien George --- ports/stm32/lwip_inc/arch/cc.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ports/stm32/lwip_inc/arch/cc.h b/ports/stm32/lwip_inc/arch/cc.h index fc5230ef723ea..a818d6d6a0b61 100644 --- a/ports/stm32/lwip_inc/arch/cc.h +++ b/ports/stm32/lwip_inc/arch/cc.h @@ -2,7 +2,9 @@ #define MICROPY_INCLUDED_STM32_LWIP_ARCH_CC_H #include -#define LWIP_PLATFORM_DIAG(x) +#include + +#define LWIP_PLATFORM_DIAG(x) do { printf x; } while (0) #define LWIP_PLATFORM_ASSERT(x) { assert(1); } #define LWIP_NO_CTYPE_H 1 From 851aa06461fb5b25e52329021ccaa11008c0f77a Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Aug 2024 16:59:42 +1000 Subject: [PATCH 09/10] rp2: Integrate optional network.PPP. Can be enabled by a board by enabling `MICROPY_PY_NETWORK_PPP_LWIP`. Signed-off-by: Damien George --- ports/rp2/lwip_inc/lwipopts.h | 11 ++++++++++- ports/rp2/mpconfigport.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ports/rp2/lwip_inc/lwipopts.h b/ports/rp2/lwip_inc/lwipopts.h index 5f37ecdac2e9d..f42bea528e0a4 100644 --- a/ports/rp2/lwip_inc/lwipopts.h +++ b/ports/rp2/lwip_inc/lwipopts.h @@ -1,7 +1,7 @@ #ifndef MICROPY_INCLUDED_RP2_LWIP_LWIPOPTS_H #define MICROPY_INCLUDED_RP2_LWIP_LWIPOPTS_H -#include +#include "py/mpconfig.h" // This protection is not needed, instead protect lwIP code with flags #define SYS_ARCH_DECL_PROTECT(lev) do { } while (0) @@ -38,6 +38,12 @@ #define LWIP_MDNS_RESPONDER 1 #define LWIP_IGMP 1 +#if MICROPY_PY_LWIP_PPP +#define PPP_SUPPORT 1 +#define PAP_SUPPORT 1 +#define CHAP_SUPPORT 1 +#endif + #define LWIP_NUM_NETIF_CLIENT_DATA LWIP_MDNS_RESPONDER #define MEMP_NUM_UDP_PCB (4 + LWIP_MDNS_RESPONDER) #define MEMP_NUM_SYS_TIMEOUT (LWIP_NUM_SYS_TIMEOUT_INTERNAL + LWIP_MDNS_RESPONDER) @@ -57,4 +63,7 @@ extern uint32_t rosc_random_u32(void); typedef uint32_t sys_prot_t; +// Needed for PPP. +#define sys_jiffies sys_now + #endif // MICROPY_INCLUDED_RP2_LWIP_LWIPOPTS_H diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index a428721d763da..49acafe239b9d 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -155,6 +155,7 @@ #define MICROPY_VFS_LFS2 (1) #define MICROPY_VFS_FAT (1) #define MICROPY_SSL_MBEDTLS (1) +#define MICROPY_PY_LWIP_PPP (MICROPY_PY_NETWORK_PPP_LWIP) #define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP) // Hardware timer alarm index. Available range 0-3. From 35b6a66b0b0ea00ad6b4e83f2dcbeffc349f2b04 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 28 Aug 2024 17:28:48 +1000 Subject: [PATCH 10/10] docs/library: Document the network.PPP class. Signed-off-by: Damien George --- docs/library/network.PPP.rst | 98 ++++++++++++++++++++++++++++++++++++ docs/library/network.rst | 1 + 2 files changed, 99 insertions(+) create mode 100644 docs/library/network.PPP.rst diff --git a/docs/library/network.PPP.rst b/docs/library/network.PPP.rst new file mode 100644 index 0000000000000..85f580ce540ed --- /dev/null +++ b/docs/library/network.PPP.rst @@ -0,0 +1,98 @@ +.. currentmodule:: network +.. _network.PPP: + +class PPP -- create network connections over serial PPP +======================================================= + +This class allows you to create a network connection over a serial port using +the PPP protocol. It is only available on selected ports and boards. + +Example usage:: + + import network + + ppp = network.PPP(uart) + ppp.connect() + + while not ppp.isconnected(): + pass + + print(ppp.ipconfig("addr4")) + + # use the socket module as usual, etc + + ppp.disconnect() + +Constructors +------------ + +.. class:: PPP(stream) + + Create a PPP driver object. + + Arguments are: + + - *stream* is any object that supports the stream protocol, but is most commonly a + :class:`machine.UART` instance. This stream object must have an ``irq()`` method + and an ``IRQ_RXIDLE`` constant, for use by `PPP.connect`. + +Methods +------- + +.. method:: PPP.connect(security=SEC_NONE, user=None, key=None) + + Initiate a PPP connection with the given parameters: + + - *security* is the type of security, either ``PPP.SEC_NONE``, ``PPP.SEC_PAP``, + or ``PPP.SEC_CHAP``. + - *user* is an optional user name to use with the security mode. + - *key* is an optional password to use with the security mode. + + When this method is called the underlying stream has its interrupt configured to call + `PPP.poll` via ``stream.irq(ppp.poll, stream.IRQ_RXIDLE)``. This makes sure the + stream is polled, and data passed up the PPP stack, wheverver data becomes available + on the stream. + + The connection proceeds asynchronously, in the background. + +.. method:: PPP.disconnect() + + Terminate the connection. This must be called to cleanly close the PPP connection. + +.. method:: PPP.isconnected() + + Returns ``True`` if the PPP link is connected and up. + Returns ``False`` otherwise. + +.. method:: PPP.status() + + Returns the PPP status. + +.. method:: PPP.config(config_parameters) + + Sets or gets parameters of the PPP interface. There are currently no parameter that + can be set or retrieved. + +.. method:: PPP.ipconfig('param') + PPP.ipconfig(param=value, ...) + + See `AbstractNIC.ipconfig`. + +.. method:: PPP.ifconfig([(ip, subnet, gateway, dns)]) + + See `AbstractNIC.ifconfig`. + +.. method:: PPP.poll() + + Poll the underlying stream for data, and pass it up the PPP stack. + This is called automatically if the stream is a UART with a RXIDLE interrupt, + so it's not usually necessary to call it manually. + +Constants +--------- + +.. data:: PPP.SEC_NONE + PPP.SEC_PAP + PPP.SEC_CHAP + + The type of connection security. diff --git a/docs/library/network.rst b/docs/library/network.rst index 6a436fa85e052..d05d17132dc67 100644 --- a/docs/library/network.rst +++ b/docs/library/network.rst @@ -192,6 +192,7 @@ provide a way to control networking interfaces of various kinds. network.WLANWiPy.rst network.WIZNET5K.rst network.LAN.rst + network.PPP.rst Network functions =================