Skip to content

Commit

Permalink
Merge pull request #2227 from rgrr/ncm_device_new2
Browse files Browse the repository at this point in the history
Rewrite of NCM device driver
  • Loading branch information
HiFiPhile authored May 7, 2024
2 parents 104a5da + e5d92c4 commit e5b6f93
Show file tree
Hide file tree
Showing 9 changed files with 971 additions and 490 deletions.
1 change: 1 addition & 0 deletions examples/device/net_lwip_webserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ target_sources(${PROJECT} PUBLIC
${LWIP}/src/netif/slipif.c
${LWIP}/src/apps/http/httpd.c
${LWIP}/src/apps/http/fs.c
${LWIP}/src/apps/lwiperf/lwiperf.c
)

# due to warnings from other net source, we need to prevent error from some of the warnings options
Expand Down
1 change: 1 addition & 0 deletions examples/device/net_lwip_webserver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ SRC_C += \
lib/lwip/src/netif/slipif.c \
lib/lwip/src/apps/http/httpd.c \
lib/lwip/src/apps/http/fs.c \
lib/lwip/src/apps/lwiperf/lwiperf.c \
lib/networking/dhserver.c \
lib/networking/dnserver.c \
lib/networking/rndis_reports.c
Expand Down
2 changes: 2 additions & 0 deletions examples/device/net_lwip_webserver/skip.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mcu:LPC11UXX
mcu:LPC13XX
mcu:LPC15XX
mcu:MSP430x5xx
mcu:NUC121
mcu:SAMD11
mcu:STM32L0
mcu:STM32F0
mcu:KINETIS_KL
family:broadcom_64bit
family:broadcom_32bit
Expand Down
6 changes: 3 additions & 3 deletions examples/device/net_lwip_webserver/src/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67))

#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
#define TCP_SND_BUF (2 * TCP_MSS)
#define TCP_WND (TCP_MSS)
#define TCP_SND_BUF (4 * TCP_MSS)
#define TCP_WND (4 * TCP_MSS)

#define ETHARP_SUPPORT_STATIC_ENTRIES 1

Expand All @@ -59,7 +59,7 @@

#define LWIP_SINGLE_NETIF 1

#define PBUF_POOL_SIZE 2
#define PBUF_POOL_SIZE 4

#define HTTPD_USE_CUSTOM_FSDATA 0

Expand Down
121 changes: 54 additions & 67 deletions examples/device/net_lwip_webserver/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00

#include "dhserver.h"
#include "dnserver.h"
#include "httpd.h"
#include "lwip/ethip6.h"
#include "lwip/init.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)) }
#ifdef INCLUDE_IPERF
#include "lwip/apps/lwiperf.h"
#endif

#define INIT_IP4(a, b, c, d) \
{ PP_HTONL(LWIP_MAKEU32(a, b, c, d)) }

/* lwip context */
static struct netif netif_data;
Expand All @@ -64,44 +69,40 @@ static struct pbuf *received_frame;
/* 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) */
/* 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 */
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 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 */
static dhcp_entry_t entries[] =
{
/* mac ip address lease time */
{ {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, 4), 24 * 60 * 60 },
static dhcp_entry_t entries[] = {
/* mac ip address lease time */
{{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, 4), 24 * 60 * 60},
};

static const dhcp_config_t dhcp_config =
{
.router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */
.port = 67, /* listen port */
.dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */
"usb", /* dns suffix */
TU_ARRAY_SIZE(entries), /* num entry */
entries /* entries */
static const dhcp_config_t dhcp_config = {
.router = INIT_IP4(0, 0, 0, 0), /* router address (if any) */
.port = 67, /* listen port */
.dns = INIT_IP4(192, 168, 7, 1), /* dns server (if any) */
"usb", /* dns suffix */
TU_ARRAY_SIZE(entries), /* num entry */
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 (!tud_ready())
return ERR_USE;

/* 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 */);
return ERR_OK;
}
Expand All @@ -111,20 +112,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);
}

#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);
}
#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));
netif->mtu = CFG_TUD_NET_MTU;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
Expand All @@ -139,8 +137,7 @@ static err_t netif_init_cb(struct netif *netif)
return ERR_OK;
}

static void init_lwip(void)
{
static void init_lwip(void) {
struct netif *netif = &netif_data;

lwip_init();
Expand All @@ -158,28 +155,23 @@ static void init_lwip(void)
}

/* handle any DNS requests from dns-server */
bool dns_query_proc(const char *name, ip4_addr_t *addr)
{
if (0 == strcmp(name, "tiny.usb"))
{
bool dns_query_proc(const char *name, ip4_addr_t *addr) {
if (0 == strcmp(name, "tiny.usb")) {
*addr = ipaddr;
return true;
}
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
parsing the previous, we must signal our inability to accept it */
if (received_frame) return false;

if (size)
{
if (size) {
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 */
memcpy(p->payload, src, size);

Expand All @@ -191,20 +183,17 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
return true;
}

uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg)
{
struct pbuf *p = (struct pbuf *)ref;
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
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);
}

static void service_traffic(void)
{
static void service_traffic(void) {
/* handle any packet received by tud_network_recv_cb() */
if (received_frame)
{
if (received_frame) {
ethernet_input(received_frame, &netif_data);
pbuf_free(received_frame);
received_frame = NULL;
Expand All @@ -214,18 +203,15 @@ static void service_traffic(void)
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 (received_frame)
{
if (received_frame) {
pbuf_free(received_frame);
received_frame = NULL;
}
}

int main(void)
{
int main(void) {
/* initialize TinyUSB */
board_init();

Expand All @@ -243,8 +229,12 @@ int main(void)
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
httpd_init();

while (1)
{
#ifdef INCLUDE_IPERF
// test with: iperf -c 192.168.7.1 -e -i 1 -M 5000 -l 8192 -r
lwiperf_start_tcp_server_default(NULL, NULL);
#endif

while (1) {
tud_task();
service_traffic();
}
Expand All @@ -253,17 +243,14 @@ int main(void)
}

/* 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;
}
void sys_arch_unprotect(sys_prot_t pval)
{
(void)pval;
void sys_arch_unprotect(sys_prot_t pval) {
(void) pval;
}

/* 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();
}
Loading

0 comments on commit e5b6f93

Please sign in to comment.