From 024ed93d4645fe4a1dc96207b8d87ed9424455ac Mon Sep 17 00:00:00 2001 From: Giovanni Di Sirio Date: Sun, 24 Sep 2017 16:36:05 +0000 Subject: lwIP preparation patches. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@10709 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- os/various/lwip_bindings/arch/cc.h | 44 ++++++++++--- os/various/lwip_bindings/arch/sys_arch.c | 4 +- os/various/lwip_bindings/lwipthread.c | 104 ++++++++++++++++++++++++------- os/various/lwip_bindings/lwipthread.h | 94 ++++++++++++++++++++++++++-- os/various/various.dox | 10 +++ 5 files changed, 221 insertions(+), 35 deletions(-) diff --git a/os/various/lwip_bindings/arch/cc.h b/os/various/lwip_bindings/arch/cc.h index 18c2b3635..4c1d4f379 100644 --- a/os/various/lwip_bindings/arch/cc.h +++ b/os/various/lwip_bindings/arch/cc.h @@ -61,17 +61,47 @@ typedef uint32_t u32_t; typedef int32_t s32_t; typedef uint32_t mem_ptr_t; -#define PACK_STRUCT_STRUCT __attribute__((packed)) - -#define LWIP_PLATFORM_DIAG(x) -#define LWIP_PLATFORM_ASSERT(x) { \ - osalSysHalt(x); \ -} +#define PACK_STRUCT_STRUCT __attribute__((packed)) #ifndef BYTE_ORDER #define BYTE_ORDER LITTLE_ENDIAN #endif -#define LWIP_PROVIDE_ERRNO +/** + * @brief Use lwIP provided error codes by default. + */ +#ifndef LWIP_PROVIDE_ERRNO +#define LWIP_PROVIDE_ERRNO 1 +#endif + +/** + * @brief Use system provided struct timeval by default. + */ +#ifndef LWIP_TIMEVAL_PRIVATE +#define LWIP_TIMEVAL_PRIVATE 0 +#include +#endif + +/** + * @brief Use a no-op diagnostic output macro by default. + */ +#if !defined(LWIP_PLATFORM_DIAG) +#define LWIP_PLATFORM_DIAG(x) +#endif + +/** + * @brief Halt the system on lwIP assert failure by default. + */ +#if !defined(LWIP_PLATFORM_ASSERT) +#define LWIP_PLATFORM_ASSERT(x) osalSysHalt(x) +#endif + +/** + * @brief The NETIF API is required by lwipthread. + */ +#ifdef LWIP_NETIF_API +#undef LWIP_NETIF_API +#endif +#define LWIP_NETIF_API 1 #endif /* __CC_H__ */ diff --git a/os/various/lwip_bindings/arch/sys_arch.c b/os/various/lwip_bindings/arch/sys_arch.c index 091c9ab96..33c8b9f9c 100644 --- a/os/various/lwip_bindings/arch/sys_arch.c +++ b/os/various/lwip_bindings/arch/sys_arch.c @@ -48,7 +48,9 @@ * */ -// see http://lwip.wikia.com/wiki/Porting_for_an_OS for instructions +/* + * See http://lwip.wikia.com/wiki/Porting_for_an_OS for instructions. + */ #include "hal.h" diff --git a/os/various/lwip_bindings/lwipthread.c b/os/various/lwip_bindings/lwipthread.c index 56442bab3..b638c7a7e 100644 --- a/os/various/lwip_bindings/lwipthread.c +++ b/os/various/lwip_bindings/lwipthread.c @@ -60,22 +60,25 @@ #include "lwipthread.h" -#include "lwip/opt.h" - -#include "lwip/def.h" -#include "lwip/mem.h" -#include "lwip/pbuf.h" -#include "lwip/sys.h" +#include +#include +#include +#include +#include #include #include #include -#include "netif/etharp.h" -#include "netif/ppp_oe.h" +#include +#include #if LWIP_DHCP #include #endif +#if LWIP_AUTOIP +#include +#endif + #define PERIODIC_TIMER_ID 1 #define FRAME_RECEIVED_ID 2 @@ -97,7 +100,7 @@ static void low_level_init(struct netif *netif) { netif->hwaddr_len = ETHARP_HWADDR_LEN; /* maximum transfer unit */ - netif->mtu = 1500; + netif->mtu = LWIP_NETIF_MTU; /* device capabilities */ /* don't set NETIF_FLAG_ETHARP if this device is not an Ethernet one */ @@ -107,7 +110,19 @@ static void low_level_init(struct netif *netif) { } /* - * Transmits a frame. + * This function does the actual transmission of the packet. The packet is + * contained in the pbuf that is passed to the function. This pbuf + * might be chained. + * + * @param netif the lwip network interface structure for this ethernetif + * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) + * @return ERR_OK if the packet could be sent + * an err_t value if the packet couldn't be sent + * + * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to + * strange results. You might consider waiting for space in the DMA queue + * to become available since the stack doesn't retry to send a packet + * dropped because of memory failure (except for the TCP timers). */ static err_t low_level_output(struct netif *netif, struct pbuf *p) { struct pbuf *q; @@ -182,13 +197,19 @@ static struct pbuf *low_level_input(struct netif *netif) { } /* - * Initialization. + * Called at the beginning of the program to set up the + * network interface. It calls the function low_level_init() to do the + * actual setup of the hardware. + * + * This function should be passed as a parameter to netifapi_netif_add(). + * + * @param netif the lwip network interface structure for this ethernetif + * @return ERR_OK if the loopif is initialised + * ERR_MEM if private data couldn't be allocated + * any other err_t on error */ static err_t ethernetif_init(struct netif *netif) { -#if LWIP_NETIF_HOSTNAME - /* Initialize interface hostname */ - netif->hostname = "lwip"; -#endif /* LWIP_NETIF_HOSTNAME */ + osalDbgAssert((netif != NULL), "netif != NULL"); /* * Initialize the snmp variables and counters inside the struct netif. @@ -222,11 +243,13 @@ static err_t ethernetif_init(struct netif *netif) { static THD_FUNCTION(lwip_thread, p) { event_timer_t evt; event_listener_t el0, el1; - struct ip_addr ip, gateway, netmask; - static struct netif thisif; + ip_addr_t ip, gateway, netmask; + static struct netif thisif = { 0 }; static const MACConfig mac_config = {thisif.hwaddr}; + net_addr_mode_t addressMode; + err_t result; - chRegSetThreadName("lwipthread"); + chRegSetThreadName(LWIP_THREAD_NAME); /* Initializes the thing.*/ tcpip_init(NULL, NULL); @@ -241,6 +264,10 @@ static THD_FUNCTION(lwip_thread, p) { ip.addr = opts->address; gateway.addr = opts->gateway; netmask.addr = opts->netmask; + addressMode = opts->addrMode; +#if LWIP_NETIF_HOSTNAME + thisif.hostname = opts->ourHostName; +#endif } else { thisif.hwaddr[0] = LWIP_ETHADDR_0; @@ -252,12 +279,41 @@ static THD_FUNCTION(lwip_thread, p) { LWIP_IPADDR(&ip); LWIP_GATEWAY(&gateway); LWIP_NETMASK(&netmask); + addressMode = NET_ADDRESS_STATIC; +#if LWIP_NETIF_HOSTNAME + thisif.hostname = NULL; +#endif } + +#if LWIP_NETIF_HOSTNAME + if (thisif.hostname == NULL) + thisif.hostname = LWIP_NETIF_HOSTNAME_STRING; +#endif + macStart(ÐD1, &mac_config); - netif_add(&thisif, &ip, &netmask, &gateway, NULL, ethernetif_init, tcpip_input); + + /* Add interface. */ + result = netifapi_netif_add(&thisif, &ip, &netmask, &gateway, NULL, ethernetif_init, tcpip_input); + if (result != ERR_OK) + { + chThdSleepMilliseconds(1000); // Give some time to print any other diagnostics. + osalSysHalt("netif_add error"); // Not sure what else we can do if an error occurs here. + }; netif_set_default(&thisif); - netif_set_up(&thisif); + + switch (addressMode) + { +#if LWIP_AUTOIP + case NET_ADDRESS_AUTO: + autoip_start(&thisif); + break; +#endif + + default: + netif_set_up(&thisif); + break; + } /* Setup event sources.*/ evtObjectInit(&evt, LWIP_LINK_POLL_INTERVAL); @@ -279,14 +335,16 @@ static THD_FUNCTION(lwip_thread, p) { tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_up, &thisif, 0); #if LWIP_DHCP - dhcp_start(&thisif); + if (addressMode == NET_ADDRESS_DHCP) + dhcp_start(&thisif); #endif } else { tcpip_callback_with_block((tcpip_callback_fn) netif_set_link_down, &thisif, 0); #if LWIP_DHCP - dhcp_stop(&thisif); + if (addressMode == NET_ADDRESS_DHCP) + dhcp_stop(&thisif); #endif } } @@ -308,6 +366,7 @@ static THD_FUNCTION(lwip_thread, p) { if (thisif.input(p, &thisif) == ERR_OK) break; LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); + /* Falls through */ default: pbuf_free(p); } @@ -324,7 +383,6 @@ static THD_FUNCTION(lwip_thread, p) { * then the static configuration is used. */ void lwipInit(const lwipthread_opts_t *opts) { - /* Creating the lwIP thread (it changes priority internally).*/ chThdCreateStatic(wa_lwip_thread, sizeof (wa_lwip_thread), chThdGetPriorityX() - 1, lwip_thread, (void *)opts); diff --git a/os/various/lwip_bindings/lwipthread.h b/os/various/lwip_bindings/lwipthread.h index 8cd1c2e5d..947d0afd7 100644 --- a/os/various/lwip_bindings/lwipthread.h +++ b/os/various/lwip_bindings/lwipthread.h @@ -26,6 +26,27 @@ #include +/** + * @brief lwIP default network interface maximum transmission unit (MTU). + */ +#if !defined(LWIP_NETIF_MTU) || defined(__DOXYGEN__) +#define LWIP_NETIF_MTU 1500 +#endif + +/** + * @brief Default network interface hostname. + */ +#if !defined(LWIP_NETIF_HOSTNAME_STRING) || defined(__DOXYGEN__) +#define LWIP_NETIF_HOSTNAME_STRING "lwip" +#endif + +/** + * @brief Default network interface hostname. + */ +#if !defined(LWIP_THREAD_NAME) || defined(__DOXYGEN__) +#define LWIP_THREAD_NAME "lwipthread" +#endif + /** * @brief lwIP thread priority. */ @@ -138,14 +159,79 @@ #define LWIP_IFNAME1 's' #endif +/** + * @brief Utility macro to define an IPv4 address. + * + * @note Within the networking subsystem, IPv4 network addresses are + * stored with LS byte of network address in MS byte of unsigned int. + */ +#if BYTE_ORDER == LITTLE_ENDIAN +#define IP4_ADDR_VALUE(a,b,c,d) \ + (((u32_t)((d) & 0xff) << 24) | \ + ((u32_t)((c) & 0xff) << 16) | \ + ((u32_t)((b) & 0xff) << 8) | \ + (u32_t)((a) & 0xff)) +#else +#define IP4_ADDR_VALUE(a,b,c,d) \ + (((u32_t)((a) & 0xff) << 24) | \ + ((u32_t)((b) & 0xff) << 16) | \ + ((u32_t)((c) & 0xff) << 8) | \ + (u32_t)((d) & 0xff)) +#endif + +/** + * @brief Startup network assigning modes. + */ +typedef enum { +#if LWIP_DHCP || defined(__DOXYGEN__) + /** + * @brief Assign a DHCP given address. + */ + NET_ADDRESS_DHCP = 1, +#endif + /** + * @brief Assign a statically IPv4 address. + */ + NET_ADDRESS_STATIC = 2, +#if LWIP_AUTOIP || defined(__DOXYGEN__) + /** + * @brief Assign an IPv4 link-Local address. + */ + NET_ADDRESS_AUTO = 3 +#endif +} net_addr_mode_t; + /** * @brief Runtime TCP/IP settings. */ typedef struct lwipthread_opts { - uint8_t *macaddress; - uint32_t address; - uint32_t netmask; - uint32_t gateway; + /** + * @brief Pointer to MAC address as an array of 6 unsigned bytes. + */ + uint8_t *macaddress; + /** + * @brief Network address as 32-bit unsigned integer. + */ + uint32_t address; + /** + * @brief Network subnet mask as 32-bit unsigned integer. + */ + uint32_t netmask; + /** + * @brief Network gateway as 32-bit unsigned integer. + */ + uint32_t gateway; + /** + * @brief Startup network addressing mode - static, DHCP, auto. + */ + net_addr_mode_t addrMode; + /** + * @brief Host name. If NULL, a default string is used. + * @note Not checked for validity. In particular, spaces not allowed. + */ +#if LWIP_NETIF_HOSTNAME || defined(__DOXYGEN__) + const char *ourHostName; +#endif } lwipthread_opts_t; #ifdef __cplusplus diff --git a/os/various/various.dox b/os/various/various.dox index f71e7dfb1..86bfba524 100644 --- a/os/various/various.dox +++ b/os/various/various.dox @@ -79,3 +79,13 @@ * * @ingroup various */ + +/** + * @defgroup LWIP_THREAD LWIP bindings + * + * @brief lwIP port and wrapper thread. + * @details This module implements the lwIP system abstraction and wrapper + * thread. + * + * @ingroup various + */ -- cgit v1.2.3