Skip to content

extmod: Add generic network.PPP to work with lwIP #14461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 29, 2024
98 changes: 98 additions & 0 deletions docs/library/network.PPP.rst
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions docs/library/network.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=================
Expand Down
30 changes: 30 additions & 0 deletions extmod/extmod.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -275,11 +276,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
Expand Down Expand Up @@ -310,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
Expand Down
30 changes: 30 additions & 0 deletions extmod/extmod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -332,6 +333,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 \
Expand All @@ -349,6 +352,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 \
Expand All @@ -367,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
Expand Down
4 changes: 4 additions & 0 deletions extmod/modnetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
4 changes: 4 additions & 0 deletions extmod/modnetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading