#include "bsp/board.h"
#include "tusb.h"
#include "dhserver.h"
#include "dnserver.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)) }
static struct netif netif_data;
static struct pbuf *received_frame;
const uint8_t tud_network_mac_address[6] = {0x02,0x02,0x84,0x6A,0x96,0x00};
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);
static dhcp_entry_t entries[] =
{
{ {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),
.port = 67,
.dns = INIT_IP4(192, 168, 7, 1),
"usb",
TU_ARRAY_SIZE(entries),
entries
};
static err_t linkoutput_fn(struct netif *netif, struct pbuf *p)
{
(void)netif;
for (;;)
{
if (!tud_ready())
return ERR_USE;
if (tud_network_can_xmit(p->tot_len))
{
tud_network_xmit(p, 0 );
return ERR_OK;
}
tud_task();
}
}
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)
{
return ethip6_output(netif, p, addr);
}
#endif
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;
netif->state = NULL;
netif->name[0] = 'E';
netif->name[1] = 'X';
netif->linkoutput = linkoutput_fn;
netif->output = ip4_output_fn;
#if LWIP_IPV6
netif->output_ip6 = ip6_output_fn;
#endif
return ERR_OK;
}
static void init_lwip(void)
{
struct netif *netif = &netif_data;
lwip_init();
netif->hwaddr_len = sizeof(tud_network_mac_address);
memcpy(netif->hwaddr, tud_network_mac_address, sizeof(tud_network_mac_address));
netif->hwaddr[5] ^= 0x01;
netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, netif_init_cb, ip_input);
#if LWIP_IPV6
netif_create_ip6_linklocal_address(netif, 1);
#endif
netif_set_default(netif);
}
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)
{
if (received_frame) return false;
if (size)
{
struct pbuf *p = pbuf_alloc(PBUF_RAW, size, PBUF_POOL);
if (p)
{
memcpy(p->payload, src, size);
received_frame = p;
}
}
return true;
}
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg)
{
struct pbuf *p = (struct pbuf *)ref;
(void)arg;
return pbuf_copy_partial(p, dst, p->tot_len, 0);
}
static void service_traffic(void)
{
if (received_frame)
{
ethernet_input(received_frame, &netif_data);
pbuf_free(received_frame);
received_frame = NULL;
tud_network_recv_renew();
}
sys_check_timeouts();
}
void tud_network_init_cb(void)
{
if (received_frame)
{
pbuf_free(received_frame);
received_frame = NULL;
}
}
int main(void)
{
board_init();
tusb_init();
init_lwip();
while (!netif_is_up(&netif_data));
while (dhserv_init(&dhcp_config) != ERR_OK);
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc) != ERR_OK);
httpd_init();
while (1)
{
tud_task();
service_traffic();
}
return 0;
}
sys_prot_t sys_arch_protect(void)
{
return 0;
}
void sys_arch_unprotect(sys_prot_t pval)
{
(void)pval;
}
uint32_t sys_now(void)
{
return board_millis();
}