Reformat net_lwip_webserver example.

This commit is contained in:
HiFiPhile 2024-05-07 00:17:09 +02:00
parent 91d69fa942
commit d25ee82d3f
2 changed files with 78 additions and 85 deletions

View File

@ -48,12 +48,13 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00
#include "dhserver.h" #include "dhserver.h"
#include "dnserver.h" #include "dnserver.h"
#include "httpd.h"
#include "lwip/ethip6.h"
#include "lwip/init.h" #include "lwip/init.h"
#include "lwip/timeouts.h" #include "lwip/timeouts.h"
#include "lwip/ethip6.h"
#include "httpd.h"
#define INIT_IP4(a,b,c,d) { PP_HTONL(LWIP_MAKEU32(a,b,c,d)) } #define INIT_IP4(a, b, c, d) \
{ PP_HTONL(LWIP_MAKEU32(a, b, c, d)) }
/* lwip context */ /* lwip context */
static struct netif netif_data; static struct netif netif_data;
@ -64,44 +65,40 @@ static struct pbuf *received_frame;
/* this is used by this code, ./class/net/net_driver.c, and usb_descriptors.c */ /* this is used by this code, ./class/net/net_driver.c, and usb_descriptors.c */
/* ideally speaking, this should be generated from the hardware's unique ID (if available) */ /* ideally speaking, this should be generated from the hardware's unique ID (if available) */
/* it is suggested that the first byte is 0x02 to indicate a link-local address */ /* it is suggested that the first byte is 0x02 to indicate a link-local address */
uint8_t tud_network_mac_address[6] = {0x02,0x02,0x84,0x6A,0x96,0x00}; uint8_t tud_network_mac_address[6] = {0x02, 0x02, 0x84, 0x6A, 0x96, 0x00};
/* network parameters of this MCU */ /* network parameters of this MCU */
static const ip4_addr_t ipaddr = INIT_IP4(192, 168, 7, 1); static const ip4_addr_t ipaddr = INIT_IP4(192, 168, 7, 1);
static const ip4_addr_t netmask = INIT_IP4(255, 255, 255, 0); static const ip4_addr_t netmask = INIT_IP4(255, 255, 255, 0);
static const ip4_addr_t gateway = INIT_IP4(0, 0, 0, 0); static const ip4_addr_t gateway = INIT_IP4(0, 0, 0, 0);
/* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */ /* database IP addresses that can be offered to the host; this must be in RAM to store assigned MAC addresses */
static dhcp_entry_t entries[] = static dhcp_entry_t entries[] = {
{ /* mac ip address lease time */
/* mac ip address lease time */ {{0}, INIT_IP4(192, 168, 7, 2), 24 * 60 * 60},
{ {0}, INIT_IP4(192, 168, 7, 2), 24 * 60 * 60 }, {{0}, INIT_IP4(192, 168, 7, 3), 24 * 60 * 60},
{ {0}, INIT_IP4(192, 168, 7, 3), 24 * 60 * 60 }, {{0}, INIT_IP4(192, 168, 7, 4), 24 * 60 * 60},
{ {0}, INIT_IP4(192, 168, 7, 4), 24 * 60 * 60 },
}; };
static const dhcp_config_t dhcp_config = static const dhcp_config_t dhcp_config = {
{ .router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */
.router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */ .port = 67, /* listen port */
.port = 67, /* listen port */ .dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */
.dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */ "usb", /* dns suffix */
"usb", /* dns suffix */ TU_ARRAY_SIZE(entries), /* num entry */
TU_ARRAY_SIZE(entries), /* num entry */ entries /* entries */
entries /* entries */
}; };
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
{
(void)netif;
for (;;) static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
{ (void) netif;
for (;;) {
/* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */ /* if TinyUSB isn't ready, we must signal back to lwip that there is nothing we can do */
if (!tud_ready()) if (!tud_ready())
return ERR_USE; return ERR_USE;
/* if the network driver can accept another packet, we make it happen */ /* if the network driver can accept another packet, we make it happen */
if (tud_network_can_xmit(p->tot_len)) if (tud_network_can_xmit(p->tot_len)) {
{
tud_network_xmit(p, 0 /* unused for this example */); tud_network_xmit(p, 0 /* unused for this example */);
return ERR_OK; return ERR_OK;
} }
@ -111,20 +108,17 @@ static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
} }
} }
static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr) static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t *addr) {
{
return etharp_output(netif, p, addr); return etharp_output(netif, p, addr);
} }
#if LWIP_IPV6 #if LWIP_IPV6
static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) static err_t ip6_output_fn(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) {
{
return ethip6_output(netif, p, addr); return ethip6_output(netif, p, addr);
} }
#endif #endif
static err_t netif_init_cb(struct netif *netif) static err_t netif_init_cb(struct netif *netif) {
{
LWIP_ASSERT("netif != NULL", (netif != NULL)); LWIP_ASSERT("netif != NULL", (netif != NULL));
netif->mtu = CFG_TUD_NET_MTU; netif->mtu = CFG_TUD_NET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP; netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
@ -139,8 +133,7 @@ static err_t netif_init_cb(struct netif *netif)
return ERR_OK; return ERR_OK;
} }
static void init_lwip(void) static void init_lwip(void) {
{
struct netif *netif = &netif_data; struct netif *netif = &netif_data;
lwip_init(); lwip_init();
@ -158,28 +151,23 @@ static void init_lwip(void)
} }
/* handle any DNS requests from dns-server */ /* handle any DNS requests from dns-server */
bool dns_query_proc(const char *name, ip4_addr_t *addr) bool dns_query_proc(const char *name, ip4_addr_t *addr) {
{ if (0 == strcmp(name, "tiny.usb")) {
if (0 == strcmp(name, "tiny.usb"))
{
*addr = ipaddr; *addr = ipaddr;
return true; return true;
} }
return false; return false;
} }
bool tud_network_recv_cb(const uint8_t *src, uint16_t size) bool tud_network_recv_cb(const uint8_t *src, uint16_t size) {
{
/* this shouldn't happen, but if we get another packet before /* this shouldn't happen, but if we get another packet before
parsing the previous, we must signal our inability to accept it */ parsing the previous, we must signal our inability to accept it */
if (received_frame) return false; if (received_frame) return false;
if (size) if (size) {
{
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL); struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
if (p) if (p) {
{
/* pbuf_alloc() has already initialized struct; all we need to do is copy the data */ /* pbuf_alloc() has already initialized struct; all we need to do is copy the data */
memcpy(p->payload, src, size); memcpy(p->payload, src, size);
@ -191,20 +179,17 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
return true; return true;
} }
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
{ struct pbuf *p = (struct pbuf *) ref;
struct pbuf *p = (struct pbuf *)ref;
(void)arg; /* unused for this example */ (void) arg; /* unused for this example */
return pbuf_copy_partial(p, dst, p->tot_len, 0); return pbuf_copy_partial(p, dst, p->tot_len, 0);
} }
static void service_traffic(void) static void service_traffic(void) {
{
/* handle any packet received by tud_network_recv_cb() */ /* handle any packet received by tud_network_recv_cb() */
if (received_frame) if (received_frame) {
{
ethernet_input(received_frame, &netif_data); ethernet_input(received_frame, &netif_data);
pbuf_free(received_frame); pbuf_free(received_frame);
received_frame = NULL; received_frame = NULL;
@ -214,18 +199,15 @@ static void service_traffic(void)
sys_check_timeouts(); sys_check_timeouts();
} }
void tud_network_init_cb(void) void tud_network_init_cb(void) {
{
/* if the network is re-initializing and we have a leftover packet, we must do a cleanup */ /* if the network is re-initializing and we have a leftover packet, we must do a cleanup */
if (received_frame) if (received_frame) {
{
pbuf_free(received_frame); pbuf_free(received_frame);
received_frame = NULL; received_frame = NULL;
} }
} }
int main(void) int main(void) {
{
/* initialize TinyUSB */ /* initialize TinyUSB */
board_init(); board_init();
@ -243,8 +225,8 @@ int main(void)
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK); while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
httpd_init(); httpd_init();
while (1)
{ while (1) {
tud_task(); tud_task();
service_traffic(); service_traffic();
} }
@ -253,17 +235,14 @@ int main(void)
} }
/* lwip has provision for using a mutex, when applicable */ /* lwip has provision for using a mutex, when applicable */
sys_prot_t sys_arch_protect(void) sys_prot_t sys_arch_protect(void) {
{
return 0; return 0;
} }
void sys_arch_unprotect(sys_prot_t pval) void sys_arch_unprotect(sys_prot_t pval) {
{ (void) pval;
(void)pval;
} }
/* lwip needs a millisecond time source, and the TinyUSB board support code has one available */ /* lwip needs a millisecond time source, and the TinyUSB board support code has one available */
uint32_t sys_now(void) uint32_t sys_now(void) {
{
return board_millis(); return board_millis();
} }

View File

@ -27,21 +27,23 @@
#define _TUSB_CONFIG_H_ #define _TUSB_CONFIG_H_
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include "lwipopts.h"
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Board Specific Configuration // Board Specific Configuration
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// RHPort number used for device can be defined by board.mk, default to port 0 // RHPort number used for device can be defined by board.mk, default to port 0
#ifndef BOARD_TUD_RHPORT #ifndef BOARD_TUD_RHPORT
#define BOARD_TUD_RHPORT 0 #define BOARD_TUD_RHPORT 0
#endif #endif
// RHPort max operational speed can defined by board.mk // RHPort max operational speed can defined by board.mk
#ifndef BOARD_TUD_MAX_SPEED #ifndef BOARD_TUD_MAX_SPEED
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED #define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
#endif #endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -50,22 +52,22 @@
// defined by compiler flags for flexibility // defined by compiler flags for flexibility
#ifndef CFG_TUSB_MCU #ifndef CFG_TUSB_MCU
#error CFG_TUSB_MCU must be defined #error CFG_TUSB_MCU must be defined
#endif #endif
#ifndef CFG_TUSB_OS #ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_NONE #define CFG_TUSB_OS OPT_OS_NONE
#endif #endif
#ifndef CFG_TUSB_DEBUG #ifndef CFG_TUSB_DEBUG
#define CFG_TUSB_DEBUG 0 #define CFG_TUSB_DEBUG 0
#endif #endif
// Enable Device stack // Enable Device stack
#define CFG_TUD_ENABLED 1 #define CFG_TUD_ENABLED 1
// Default is max speed that hardware controller could support with on-chip PHY // Default is max speed that hardware controller could support with on-chip PHY
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED #define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
* Tinyusb use follows macros to declare transferring memory so that they can be put * Tinyusb use follows macros to declare transferring memory so that they can be put
@ -75,21 +77,33 @@
* - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
*/ */
#ifndef CFG_TUSB_MEM_SECTION #ifndef CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_SECTION #define CFG_TUSB_MEM_SECTION
#endif #endif
#ifndef CFG_TUSB_MEM_ALIGN #ifndef CFG_TUSB_MEM_ALIGN
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #define CFG_TUSB_MEM_ALIGN __attribute__((aligned(4)))
#endif #endif
// number of NCM transfer blocks for reception side (only valid if NCM is selected below) //--------------------------------------------------------------------
// NCM CLASS CONFIGURATION, SEE "ncm.h" FOR PERFORMANCE TUNNING
//--------------------------------------------------------------------
// Must be >> MTU
// Can be set to 2048 without impact
#define CFG_TUD_NCM_IN_NTB_MAX_SIZE (2 * TCP_MSS + 100)
// Must be >> MTU
// Can be set to smaller values if wNtbOutMaxDatagrams==1
#define CFG_TUD_NCM_OUT_NTB_MAX_SIZE (2 * TCP_MSS + 100)
// Number of NCM transfer blocks for reception side
#ifndef CFG_TUD_NCM_OUT_NTB_N #ifndef CFG_TUD_NCM_OUT_NTB_N
#define CFG_TUD_NCM_OUT_NTB_N 2 #define CFG_TUD_NCM_OUT_NTB_N 1
#endif #endif
// number of NCM transfer blocks for transmission side (only valid if NCM is selected below) // Number of NCM transfer blocks for transmission side
#ifndef CFG_TUD_NCM_IN_NTB_N #ifndef CFG_TUD_NCM_IN_NTB_N
#define CFG_TUD_NCM_IN_NTB_N 3 #define CFG_TUD_NCM_IN_NTB_N 1
#endif #endif
//-------------------------------------------------------------------- //--------------------------------------------------------------------
@ -97,18 +111,18 @@
//-------------------------------------------------------------------- //--------------------------------------------------------------------
#ifndef CFG_TUD_ENDPOINT0_SIZE #ifndef CFG_TUD_ENDPOINT0_SIZE
#define CFG_TUD_ENDPOINT0_SIZE 64 #define CFG_TUD_ENDPOINT0_SIZE 64
#endif #endif
//------------- CLASS -------------// //------------- CLASS -------------//
// Network class has 2 drivers: ECM/RNDIS and NCM. // Network class has 2 drivers: ECM/RNDIS and NCM.
// Only one of the drivers can be enabled // Only one of the drivers can be enabled
#define CFG_TUD_ECM_RNDIS 1 #define CFG_TUD_ECM_RNDIS 1
#define CFG_TUD_NCM (1-CFG_TUD_ECM_RNDIS) #define CFG_TUD_NCM (1 - CFG_TUD_ECM_RNDIS)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* _TUSB_CONFIG_H_ */ #endif /* _TUSB_CONFIG_H_ */