mirror of
https://github.com/polhenarejos/pico-keys-sdk
synced 2026-05-28 17:11:23 +02:00
Add support for LWIP esp32.
Signed-off-by: Pol Henarejos <pol.henarejos@cttc.es>
This commit is contained in:
@@ -43,36 +43,43 @@ The smartphone may be artificially picky about which Ethernet MAC address to rec
|
|||||||
try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00 (clearing bit 1).
|
try changing the first byte of tud_network_mac_address[] below from 0x02 to 0x00 (clearing bit 1).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#if !defined(ESP_PLATFORM)
|
#include "picokeys.h"
|
||||||
#include "bsp/board_api.h"
|
#if defined(PICO_PLATFORM)
|
||||||
#include "dhserver.h"
|
#include "dhserver.h"
|
||||||
#include "dnserver.h"
|
#include "dnserver.h"
|
||||||
#include "lwip/ethip6.h"
|
|
||||||
#include "lwip/init.h"
|
#include "lwip/init.h"
|
||||||
#include "lwip/timeouts.h"
|
#include "lwip/timeouts.h"
|
||||||
|
#elif defined(ESP_PLATFORM)
|
||||||
|
#include "apps/dhcpserver/dhcpserver.h"
|
||||||
|
#include "netif/ethernet.h"
|
||||||
|
#include "lwip/etharp.h"
|
||||||
|
#include "lwip/netif.h"
|
||||||
|
#include "lwip/pbuf.h"
|
||||||
#endif
|
#endif
|
||||||
#include "rest_server.h"
|
#include "rest_server.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
|
|
||||||
/* shared between tud_network_recv_cb() and service_traffic() */
|
|
||||||
static struct pbuf *received_frame;
|
|
||||||
|
|
||||||
#if !defined(ESP_PLATFORM)
|
|
||||||
/* 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};
|
|
||||||
|
|
||||||
#define INIT_IP4(a, b, c, d) \
|
#define INIT_IP4(a, b, c, d) \
|
||||||
{ PP_HTONL(LWIP_MAKEU32(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;
|
||||||
|
|
||||||
|
/* shared between tud_network_recv_cb() and service_traffic() */
|
||||||
|
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};
|
||||||
|
|
||||||
/* 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);
|
||||||
|
|
||||||
|
#if !defined(ESP_PLATFORM)
|
||||||
|
|
||||||
/* 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 */
|
||||||
@@ -84,28 +91,50 @@ static dhcp_entry_t entries[] = {
|
|||||||
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 = ipaddr, /* 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 */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* handle any DNS requests from dns-server */
|
||||||
|
bool dns_query_proc(const char *name, ip4_addr_t *addr) {
|
||||||
|
if (0 == strcmp(name, "tiny.usb")) {
|
||||||
|
*addr = ipaddr;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(ESP_PLATFORM)
|
||||||
|
static dhcps_t *dhcps = NULL;
|
||||||
|
|
||||||
|
static void dhcps_lease_cb(void *cb_arg, u8_t client_ip[4], u8_t client_mac[6]) {
|
||||||
|
(void) cb_arg;
|
||||||
|
(void) client_ip;
|
||||||
|
(void) client_mac;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
|
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p) {
|
||||||
(void) netif;
|
(void) netif;
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* 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 (tud_network_can_xmit(p->tot_len)) {
|
|
||||||
tud_network_xmit(p, 0 /* unused for this example */);
|
|
||||||
return ERR_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* transfer execution to TinyUSB in the hopes that it will finish transmitting the prior packet */
|
if (tud_network_can_xmit(p->tot_len)) {
|
||||||
|
tud_network_xmit(p, 0);
|
||||||
|
return ERR_OK;
|
||||||
|
}
|
||||||
|
#if defined(ESP_PLATFORM)
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1));
|
||||||
|
#else
|
||||||
tud_task();
|
tud_task();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,12 +142,6 @@ static err_t ip4_output_fn(struct netif *netif, struct pbuf *p, const ip4_addr_t
|
|||||||
return etharp_output(netif, p, 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) {
|
|
||||||
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));
|
LWIP_ASSERT("netif != NULL", (netif != NULL));
|
||||||
netif->mtu = CFG_TUD_NET_MTU;
|
netif->mtu = CFG_TUD_NET_MTU;
|
||||||
@@ -128,38 +151,25 @@ static err_t netif_init_cb(struct netif *netif) {
|
|||||||
netif->name[1] = 'X';
|
netif->name[1] = 'X';
|
||||||
netif->linkoutput = linkoutput_fn;
|
netif->linkoutput = linkoutput_fn;
|
||||||
netif->output = ip4_output_fn;
|
netif->output = ip4_output_fn;
|
||||||
#if LWIP_IPV6
|
|
||||||
netif->output_ip6 = ip6_output_fn;
|
|
||||||
#endif
|
|
||||||
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();
|
|
||||||
|
|
||||||
/* the lwip virtual MAC address must be different from the host's; to ensure this, we toggle the LSbit */
|
|
||||||
netif->hwaddr_len = sizeof(tud_network_mac_address);
|
netif->hwaddr_len = sizeof(tud_network_mac_address);
|
||||||
memcpy(netif->hwaddr, tud_network_mac_address, sizeof(tud_network_mac_address));
|
memcpy(netif->hwaddr, tud_network_mac_address, sizeof(tud_network_mac_address));
|
||||||
netif->hwaddr[5] ^= 0x01;
|
netif->hwaddr[5] ^= 0x01;
|
||||||
|
|
||||||
netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, netif_init_cb, ip_input);
|
#ifdef PICO_PLATFORM
|
||||||
#if LWIP_IPV6
|
lwip_init();
|
||||||
netif_create_ip6_linklocal_address(netif, 1);
|
|
||||||
#endif
|
#endif
|
||||||
|
netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, netif_init_cb, ethernet_input);
|
||||||
|
netif_set_link_up(netif);
|
||||||
|
netif_set_up(netif);
|
||||||
netif_set_default(netif);
|
netif_set_default(netif);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* handle any DNS requests from dns-server */
|
|
||||||
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
|
/* 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 */
|
||||||
@@ -169,8 +179,10 @@ bool tud_network_recv_cb(const uint8_t *src, uint16_t 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 */
|
if (pbuf_take(p, src, size) != ERR_OK) {
|
||||||
memcpy(p->payload, src, size);
|
pbuf_free(p);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/* store away the pointer for service_traffic() to later handle */
|
/* store away the pointer for service_traffic() to later handle */
|
||||||
received_frame = p;
|
received_frame = p;
|
||||||
@@ -212,15 +224,27 @@ void tud_network_init_cb(void) {
|
|||||||
pbuf_free(received_frame);
|
pbuf_free(received_frame);
|
||||||
received_frame = NULL;
|
received_frame = NULL;
|
||||||
}
|
}
|
||||||
|
/* Arm first receive buffer, otherwise host traffic (DHCP included) never enters lwIP. */
|
||||||
|
tud_network_recv_renew();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
int lwip_itf_init(void) {
|
int lwip_itf_init(void) {
|
||||||
#if !defined(ESP_PLATFORM)
|
|
||||||
init_lwip();
|
init_lwip();
|
||||||
while (!netif_is_up(&netif_data));
|
while (!netif_is_up(&netif_data));
|
||||||
|
#if !defined(ESP_PLATFORM)
|
||||||
while (dhserv_init(&dhcp_config) != ERR_OK);
|
while (dhserv_init(&dhcp_config) != ERR_OK);
|
||||||
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
|
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
|
||||||
|
#else
|
||||||
|
if (dhcps == NULL) {
|
||||||
|
dhcps = dhcps_new();
|
||||||
|
}
|
||||||
|
if (dhcps) {
|
||||||
|
(void) dhcps_set_new_lease_cb(dhcps, dhcps_lease_cb, NULL);
|
||||||
|
ip_addr_t dns_server;
|
||||||
|
ip_addr_copy_from_ip4(dns_server, ipaddr);
|
||||||
|
(void) dhcps_dns_setserver(dhcps, &dns_server);
|
||||||
|
while (dhcps_start(dhcps, &netif_data, ipaddr) != ERR_OK);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
while (rest_server_init(REST_CONN_ALL) != ERR_OK);
|
while (rest_server_init(REST_CONN_ALL) != ERR_OK);
|
||||||
|
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ static int rest_start_core1_job(rest_conn_t *conn, const rest_request_t *request
|
|||||||
rest_core1_job.handler = handler;
|
rest_core1_job.handler = handler;
|
||||||
rest_core1_job.request = *request;
|
rest_core1_job.request = *request;
|
||||||
|
|
||||||
card_start(ITF_LWIP, rest_core1_thread);
|
card_start(ITF_LWIP_NET, rest_core1_thread);
|
||||||
usb_send_event(EV_CMD_AVAILABLE);
|
usb_send_event(EV_CMD_AVAILABLE);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ void rest_task(void) {
|
|||||||
if (!rest_core1_job.pending) {
|
if (!rest_core1_job.pending) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
status = card_status(ITF_LWIP);
|
status = card_status(ITF_LWIP_NET);
|
||||||
if (status != PICOKEYS_OK) {
|
if (status != PICOKEYS_OK) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ queue_t card_to_usb_q = {0};
|
|||||||
|
|
||||||
#ifndef ENABLE_EMULATION
|
#ifndef ENABLE_EMULATION
|
||||||
extern tusb_desc_device_t desc_device;
|
extern tusb_desc_device_t desc_device;
|
||||||
extern char *string_desc_itf[4], *string_desc_arr[];
|
extern char *string_desc_itf[5], *string_desc_arr[];
|
||||||
#endif
|
#endif
|
||||||
void usb_init(void)
|
void usb_init(void)
|
||||||
{
|
{
|
||||||
@@ -170,7 +170,9 @@ void usb_init(void)
|
|||||||
#endif
|
#endif
|
||||||
#ifdef USB_ITF_LWIP
|
#ifdef USB_ITF_LWIP
|
||||||
if (ITF_LWIP_TOTAL > 0) {
|
if (ITF_LWIP_TOTAL > 0) {
|
||||||
|
#ifndef ESP_PLATFORM
|
||||||
lwip_itf_init();
|
lwip_itf_init();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef ESP_PLATFORM
|
#ifdef ESP_PLATFORM
|
||||||
|
|||||||
Reference in New Issue
Block a user