Landzo_K60Z_WebServer/src/ip_stack_helpers.c

117 lines
4.2 KiB
C

#include <stdio.h>
/* Device drivers */
#include "fsl_sysmpu.h"
/* FreeRTOS */
#include "FreeRTOS.h"
#include "task.h"
/* LwIP */
#include "lwip/apps/httpd.h"
#include "lwip/apps/lwiperf.h"
#include "lwip/apps/sntp.h"
#include "lwip/dhcp.h"
#include "lwip/dns.h"
#include "lwip/init.h"
#include "lwip/prot/dhcp.h"
#include "lwip/tcpip.h"
/* ENET */
#include "ethernetif.h"
/* Debug Console */
#include "fsl_debug_console.h"
#define NTP_POOL_ADDR "cn.pool.ntp.org"
#define NETIF_HOSTNAME "k60_server"
static char *s_netif_hostname = NETIF_HOSTNAME;
static struct netif fsl_netif0;
/* Report state => string */
static const char *report_type_str[] = {
"TCP_DONE_SERVER", /* LWIPERF_TCP_DONE_SERVER,*/
"TCP_DONE_CLIENT", /* LWIPERF_TCP_DONE_CLIENT,*/
"TCP_ABORTED_LOCAL", /* LWIPERF_TCP_ABORTED_LOCAL, */
"TCP_ABORTED_LOCAL_DATAERROR", /* LWIPERF_TCP_ABORTED_LOCAL_DATAERROR, */
"TCP_ABORTED_LOCAL_TXERROR", /* LWIPERF_TCP_ABORTED_LOCAL_TXERROR, */
"TCP_ABORTED_REMOTE", /* LWIPERF_TCP_ABORTED_REMOTE, */
"UDP_STARTED", /* LWIPERF_UDP_STARTED, */
"UDP_DONE", /* LWIPERF_UDP_DONE, */
"UDP_ABORTED_LOCAL", /* LWIPERF_UDP_ABORTED_LOCAL, */
"UDP_ABORTED_REMOTE" /* LWIPERF_UDP_ABORTED_REMOTE */
};
/** Prototype of a report function that is called when a session is finished.
This report function shows the test results. */
static void ip_stack_lwiperf_report(void *arg, enum lwiperf_report_type report_type, const ip_addr_t *local_addr,
u16_t local_port, const ip_addr_t *remote_addr, u16_t remote_port,
u32_t bytes_transferred, u32_t ms_duration, u32_t bandwidth_kbitpsec) {
printf("-------------------------------------------------\r\n");
if ((report_type < sizeof(report_type_str)) && local_addr && remote_addr) {
printf(" %s \r\n", report_type_str[report_type]);
printf(" Local address : %u.%u.%u.%u ", ((u8_t *)local_addr)[0], ((u8_t *)local_addr)[1],
((u8_t *)local_addr)[2], ((u8_t *)local_addr)[3]);
printf(" Port %d \r\n", local_port);
printf(" Remote address : %u.%u.%u.%u ", ((u8_t *)remote_addr)[0], ((u8_t *)remote_addr)[1],
((u8_t *)remote_addr)[2], ((u8_t *)remote_addr)[3]);
printf(" Port %d \r\n", remote_port);
printf(" Bytes Transferred %ld \r\n", bytes_transferred);
printf(" Duration (ms) %ld \r\n", ms_duration);
printf(" Bandwidth (kbitpsec) %ld \r\n", bandwidth_kbitpsec);
} else {
printf(" IPERF Report error\r\n");
}
}
static void ip_stack_sntp_address_found(const char *name, const ip_addr_t *ipaddr, void *callback_arg) {
printf("Resolved IP: %s\r\n", ipaddr_ntoa(ipaddr));
sntp_setserver(0, ipaddr);
sntp_init();
}
static void ip_stack_enable_sntp(void) {
/* We are not ESP32, we have to do the DNS resolution manually. */
ip_addr_t ntp_addr;
sntp_setoperatingmode(SNTP_OPMODE_POLL);
if (dns_gethostbyname(NTP_POOL_ADDR, &ntp_addr, ip_stack_sntp_address_found, NULL) == ERR_OK) {
/* If ERR_OK is returned, the result is cached in DNS. */
sntp_setserver(0, &ntp_addr);
sntp_init();
} else {
printf("No cached DNS result found, wait for callback.\r\n");
}
}
void ip_stack_setup(void) {
ip4_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw;
tcpip_init(NULL, NULL);
/* Initialize netif interface */
netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, NULL, ethernetif_init, tcpip_input);
netif_set_default(&fsl_netif0);
netif_set_up(&fsl_netif0);
netif_set_hostname(&fsl_netif0, s_netif_hostname);
/* Start DHCP, this configures both DNS and IP addresses. */
dhcp_start(&fsl_netif0);
/* Wait for a lease */
while (dhcp_supplied_address(&fsl_netif0) == 0) {
vTaskDelay(pdMS_TO_TICKS(500));
}
printf("IP Address: %s\r\n", ipaddr_ntoa(&fsl_netif0.ip_addr));
ip_stack_enable_sntp();
/* Start IPerf server */
lwiperf_start_tcp_server_default(ip_stack_lwiperf_report, NULL);
httpd_init();
}