example and dhcp debug (#447)
This commit is contained in:
parent
546cc47121
commit
68cc1451b2
3 changed files with 244 additions and 23 deletions
5
examples/tcp_non_blocking/Makefile
Normal file
5
examples/tcp_non_blocking/Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
# Makefile for tcp_non_blocking example
|
||||
PROGRAM=tcp_non_blocking
|
||||
EXTRA_COMPONENTS=extras/dhcpserver
|
||||
|
||||
include ../../common.mk
|
203
examples/tcp_non_blocking/tcp_non_blocking.c
Normal file
203
examples/tcp_non_blocking/tcp_non_blocking.c
Normal file
|
@ -0,0 +1,203 @@
|
|||
/*
|
||||
The ESP in the example runs a echo server on 172.16.0.1 (port 50 and 100 ) that
|
||||
outputs information about your ip/port then echo all text you write.
|
||||
It is manage multiple connection and multiple port with one task.
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <espressif/esp_common.h>
|
||||
#include <esp8266.h>
|
||||
#include <esp/uart.h>
|
||||
#include <FreeRTOS.h>
|
||||
#include <task.h>
|
||||
#include <queue.h>
|
||||
#include <dhcpserver.h>
|
||||
#include <lwip/api.h>
|
||||
|
||||
#define AP_SSID "esp-open-rtos AP"
|
||||
#define AP_PSK "esp-open-rtos"
|
||||
#define ECHO_PORT_1 50
|
||||
#define ECHO_PORT_2 100
|
||||
#define EVENTS_QUEUE_SIZE 10
|
||||
|
||||
#ifdef CALLBACK_DEBUG
|
||||
#define debug(s, ...) printf("%s: " s "\n", "Cb:", ## __VA_ARGS__)
|
||||
#else
|
||||
#define debug(s, ...)
|
||||
#endif
|
||||
|
||||
QueueHandle_t xQueue_events;
|
||||
typedef struct {
|
||||
struct netconn *nc ;
|
||||
uint8_t type ;
|
||||
} netconn_events;
|
||||
|
||||
/*
|
||||
* This function will be call in Lwip in each event on netconn
|
||||
*/
|
||||
static void netCallback(struct netconn *conn, enum netconn_evt evt, uint16_t length)
|
||||
{
|
||||
//Show some callback information (debug)
|
||||
debug("sock:%u\tsta:%u\tevt:%u\tlen:%u\ttyp:%u\tfla:%02X\terr:%d", \
|
||||
(uint32_t)conn,conn->state,evt,length,conn->type,conn->flags,conn->last_err);
|
||||
|
||||
netconn_events events ;
|
||||
|
||||
//If netconn got error, it is close or deleted, dont do treatments on it.
|
||||
if (conn->pending_err)
|
||||
{
|
||||
return;
|
||||
}
|
||||
//Treatments only on rcv events.
|
||||
switch (evt) {
|
||||
case NETCONN_EVT_RCVPLUS:
|
||||
events.nc = conn ;
|
||||
events.type = evt ;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
//Send the event to the queue
|
||||
xQueueSend(xQueue_events, &events, 1000);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize a server netconn and listen port
|
||||
*/
|
||||
static void set_tcp_server_netconn(struct netconn **nc, uint16_t port, netconn_callback callback)
|
||||
{
|
||||
if(nc == NULL)
|
||||
{
|
||||
printf("%s: netconn missing .\n",__FUNCTION__);
|
||||
return;
|
||||
}
|
||||
*nc = netconn_new_with_callback(NETCONN_TCP, netCallback);
|
||||
if(!*nc) {
|
||||
printf("Status monitor: Failed to allocate netconn.\n");
|
||||
return;
|
||||
}
|
||||
netconn_set_nonblocking(*nc,NETCONN_FLAG_NON_BLOCKING);
|
||||
//netconn_set_recvtimeout(*nc, 10);
|
||||
netconn_bind(*nc, IP_ADDR_ANY, port);
|
||||
netconn_listen(*nc);
|
||||
}
|
||||
|
||||
/*
|
||||
* Close and delete a socket properly
|
||||
*/
|
||||
static void close_tcp_netconn(struct netconn *nc)
|
||||
{
|
||||
nc->pending_err=ERR_CLSD; //It is hacky way to be sure than callback will don't do treatment on a netconn closed and deleted
|
||||
netconn_close(nc);
|
||||
netconn_delete(nc);
|
||||
}
|
||||
|
||||
/*
|
||||
* This task manage each netconn connection without block anything
|
||||
*/
|
||||
static void nonBlockingTCP(void *pvParameters)
|
||||
{
|
||||
|
||||
struct netconn *nc = NULL; // To create servers
|
||||
|
||||
set_tcp_server_netconn(&nc, ECHO_PORT_1, netCallback);
|
||||
printf("Server netconn %u ready on port %u.\n",(uint32_t)nc, ECHO_PORT_1);
|
||||
set_tcp_server_netconn(&nc, ECHO_PORT_2, netCallback);
|
||||
printf("Server netconn %u ready on port %u.\n",(uint32_t)nc, ECHO_PORT_2);
|
||||
|
||||
struct netbuf *netbuf = NULL; // To store incoming Data
|
||||
struct netconn *nc_in = NULL; // To accept incoming netconn
|
||||
//
|
||||
char buf[50];
|
||||
char* buffer;
|
||||
uint16_t len_buf;
|
||||
|
||||
while(1) {
|
||||
|
||||
netconn_events events;
|
||||
xQueueReceive(xQueue_events, &events, portMAX_DELAY); // Wait here an event on netconn
|
||||
|
||||
if (events.nc->state == NETCONN_LISTEN) // If netconn is a server and receive incoming event on it
|
||||
{
|
||||
printf("Client incoming on server %u.\n", (uint32_t)events.nc);
|
||||
int err = netconn_accept(events.nc, &nc_in);
|
||||
if (err != ERR_OK)
|
||||
{
|
||||
if(nc_in)
|
||||
netconn_delete(nc_in);
|
||||
}
|
||||
printf("New client is %u.\n",(uint32_t)nc_in);
|
||||
ip_addr_t client_addr; //Address port
|
||||
uint16_t client_port; //Client port
|
||||
netconn_peer(nc_in, &client_addr, &client_port);
|
||||
snprintf(buf, sizeof(buf), "Your address is %d.%d.%d.%d:%u.\r\n",
|
||||
ip4_addr1(&client_addr), ip4_addr2(&client_addr),
|
||||
ip4_addr3(&client_addr), ip4_addr4(&client_addr),
|
||||
client_port);
|
||||
netconn_write(nc_in, buf, strlen(buf), NETCONN_COPY);
|
||||
}
|
||||
else if(events.nc->state != NETCONN_LISTEN) // If netconn is the client and receive data
|
||||
{
|
||||
if ((netconn_recv(events.nc, &netbuf)) == ERR_OK) // data incoming ?
|
||||
{
|
||||
do
|
||||
{
|
||||
netbuf_data(netbuf, (void*)&buffer, &len_buf);
|
||||
netconn_write(events.nc, buffer, strlen(buffer), NETCONN_COPY);
|
||||
printf("Client %u send: %s\n",(uint32_t)events.nc,buffer);
|
||||
}
|
||||
while (netbuf_next(netbuf) >= 0);
|
||||
netbuf_delete(netbuf);
|
||||
}
|
||||
else
|
||||
{
|
||||
close_tcp_netconn(events.nc);
|
||||
printf("Error read netconn %u, close it \n",(uint32_t)events.nc);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void user_init(void)
|
||||
{
|
||||
uart_set_baud(0, 115200);
|
||||
sdk_os_delay_us(500); // Wait UART
|
||||
printf("SDK version:%s\n", sdk_system_get_sdk_version());
|
||||
|
||||
sdk_wifi_set_opmode(SOFTAP_MODE);
|
||||
struct ip_info ap_ip;
|
||||
IP4_ADDR(&ap_ip.ip, 172, 16, 0, 1);
|
||||
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
|
||||
IP4_ADDR(&ap_ip.netmask, 255, 255, 0, 0);
|
||||
sdk_wifi_set_ip_info(1, &ap_ip);
|
||||
|
||||
struct sdk_softap_config ap_config = {
|
||||
.ssid = AP_SSID,
|
||||
.ssid_hidden = 0,
|
||||
.channel = 3,
|
||||
.ssid_len = strlen(AP_SSID),
|
||||
.authmode = AUTH_WPA_WPA2_PSK,
|
||||
.password = AP_PSK,
|
||||
.max_connection = 3,
|
||||
.beacon_interval = 100,
|
||||
};
|
||||
sdk_wifi_softap_set_config(&ap_config);
|
||||
|
||||
ip_addr_t first_client_ip;
|
||||
IP4_ADDR(&first_client_ip, 172, 16, 0, 2);
|
||||
dhcpserver_start(&first_client_ip, 4);
|
||||
printf("DHCP started\n");
|
||||
|
||||
//Create a queue to store events on netconns
|
||||
xQueue_events = xQueueCreate( EVENTS_QUEUE_SIZE, sizeof(netconn_events));
|
||||
|
||||
xTaskCreate(nonBlockingTCP, "lwiptest_noblock", 512, NULL, 2, NULL);
|
||||
}
|
|
@ -22,6 +22,18 @@
|
|||
#include <lwip/api.h>
|
||||
#include "esplibs/libmain.h"
|
||||
|
||||
#if (DHCP_DEBUG == LWIP_DBG_ON)
|
||||
#define debug(s, ...) printf("%s: " s "\n", "DHCP", ## __VA_ARGS__)
|
||||
#else
|
||||
#define debug(s, ...)
|
||||
#endif
|
||||
|
||||
#if (DHCP_DEBUG == LWIP_DBG_ON)
|
||||
#define debug(s, ...) printf("%s: " s "\n", "DHCP", ## __VA_ARGS__)
|
||||
#else
|
||||
#define debug(s, ...)
|
||||
#endif
|
||||
|
||||
/* Grow the size of the lwip dhcp_msg struct's options field, as LWIP
|
||||
defaults to a 68 octet options field for its DHCP client, and most
|
||||
full-sized clients send us more than this. */
|
||||
|
@ -131,8 +143,8 @@ static void dhcpserver_task(void *pxParameter)
|
|||
state->server_if = netif_list; /* TODO: Make this configurable */
|
||||
|
||||
state->nc = netconn_new (NETCONN_UDP);
|
||||
if (!state->nc) {
|
||||
printf("DHCP Server Error: Failed to allocate socket.\r\n");
|
||||
if(!state->nc) {
|
||||
debug("DHCP Server Error: Failed to allocate socket.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -146,8 +158,8 @@ static void dhcpserver_task(void *pxParameter)
|
|||
|
||||
/* Receive a DHCP packet */
|
||||
err_t err = netconn_recv(state->nc, &netbuf);
|
||||
if (err != ERR_OK) {
|
||||
printf("DHCP Server Error: Failed to receive DHCP packet. err=%d\r\n", err);
|
||||
if(err != ERR_OK) {
|
||||
debug("DHCP Server Error: Failed to receive DHCP packet. err=%d", err);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -171,8 +183,8 @@ static void dhcpserver_task(void *pxParameter)
|
|||
netbuf_delete(netbuf);
|
||||
continue;
|
||||
}
|
||||
if (netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) {
|
||||
printf("DHCP Server Warning: Client sent more options than we know how to parse. len=%d\r\n", netbuf_len(netbuf));
|
||||
if(netbuf_len(netbuf) >= sizeof(struct dhcp_msg)) {
|
||||
debug("DHCP Server Warning: Client sent more options than we know how to parse. len=%d", netbuf_len(netbuf));
|
||||
}
|
||||
|
||||
netbuf_copy(netbuf, &received, sizeof(struct dhcp_msg));
|
||||
|
@ -180,19 +192,20 @@ static void dhcpserver_task(void *pxParameter)
|
|||
|
||||
uint8_t *message_type = find_dhcp_option(&received, DHCP_OPTION_MESSAGE_TYPE,
|
||||
DHCP_OPTION_MESSAGE_TYPE_LEN, NULL);
|
||||
if (!message_type) {
|
||||
printf("DHCP Server Error: No message type field found");
|
||||
if(!message_type) {
|
||||
debug("DHCP Server Error: No message type field found");
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("State dump. Message type %d\n", *message_type);
|
||||
for (int i = 0; i < state->max_leases; i++) {
|
||||
#if (DHCP_DEBUG == LWIP_DBG_ON)
|
||||
debug("State dump. Message type %d", *message_type);
|
||||
for(int i = 0; i < state->max_leases; i++) {
|
||||
dhcp_lease_t *lease = &state->leases[i];
|
||||
printf("lease slot %d active %d expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x\r\n", i,
|
||||
lease->active, lease->expires - now,
|
||||
lease->hwaddr[0], lease->hwaddr[1], lease->hwaddr[2],
|
||||
lease->hwaddr[3], lease->hwaddr[4], lease->hwaddr[5]);
|
||||
debug("lease slot %d expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x", i, lease->expires, lease->hwaddr[0],
|
||||
lease->hwaddr[1], lease->hwaddr[2], lease->hwaddr[3], lease->hwaddr[4],
|
||||
lease->hwaddr[5]);
|
||||
}
|
||||
#endif
|
||||
|
||||
switch(*message_type) {
|
||||
case DHCP_DISCOVER:
|
||||
|
@ -204,7 +217,7 @@ static void dhcpserver_task(void *pxParameter)
|
|||
case DHCP_RELEASE:
|
||||
handle_dhcp_release(&received);
|
||||
default:
|
||||
printf("DHCP Server Error: Unsupported message type %d\r\n", *message_type);
|
||||
debug("DHCP Server Error: Unsupported message type %d", *message_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -218,8 +231,8 @@ static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg)
|
|||
return;
|
||||
|
||||
dhcp_lease_t *freelease = find_lease_slot(dhcpmsg->chaddr);
|
||||
if (!freelease) {
|
||||
printf("DHCP Server: All leases taken.\r\n");
|
||||
if(!freelease) {
|
||||
debug("DHCP Server: All leases taken.");
|
||||
return; /* Nothing available, so do nothing */
|
||||
}
|
||||
|
||||
|
@ -264,7 +277,7 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg)
|
|||
} else if (ip4_addr_cmp(&requested_ip, IP4_ADDR_ANY4)) {
|
||||
ip4_addr_copy(requested_ip, dhcpmsg->ciaddr);
|
||||
} else {
|
||||
printf("DHCP Server Error: No requested IP\r\n");
|
||||
debug("DHCP Server Error: No requested IP");
|
||||
send_dhcp_nak(dhcpmsg);
|
||||
return;
|
||||
}
|
||||
|
@ -274,14 +287,14 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg)
|
|||
|| ip4_addr2(&requested_ip) != ip4_addr2(&state->first_client_addr)
|
||||
|| ip4_addr3(&requested_ip) != ip4_addr3(&state->first_client_addr)) {
|
||||
sprintf_ipaddr(&requested_ip, ipbuf);
|
||||
printf("DHCP Server Error: %s not an allowed IP\r\n", ipbuf);
|
||||
debug("DHCP Server Error: %s not an allowed IP", ipbuf);
|
||||
send_dhcp_nak(dhcpmsg);
|
||||
return;
|
||||
}
|
||||
/* Test the last octet is in the MAXCLIENTS range */
|
||||
int16_t octet_offs = ip4_addr4(&requested_ip) - ip4_addr4(&state->first_client_addr);
|
||||
if (octet_offs < 0 || octet_offs >= state->max_leases) {
|
||||
printf("DHCP Server Error: Address out of range\r\n");
|
||||
if(octet_offs < 0 || octet_offs >= state->max_leases) {
|
||||
debug("DHCP Server Error: Address out of range");
|
||||
send_dhcp_nak(dhcpmsg);
|
||||
return;
|
||||
}
|
||||
|
@ -289,14 +302,14 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg)
|
|||
dhcp_lease_t *requested_lease = state->leases + octet_offs;
|
||||
if (requested_lease->active && memcmp(requested_lease->hwaddr, dhcpmsg->chaddr,dhcpmsg->hlen))
|
||||
{
|
||||
printf("DHCP Server Error: Lease for address already taken\r\n");
|
||||
debug("DHCP Server Error: Lease for address already taken");
|
||||
send_dhcp_nak(dhcpmsg);
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(requested_lease->hwaddr, dhcpmsg->chaddr, dhcpmsg->hlen);
|
||||
sprintf_ipaddr(&requested_ip, ipbuf);
|
||||
printf("DHCP lease addr %s assigned to MAC %02x:%02x:%02x:%02x:%02x:%02x\r\n", ipbuf, requested_lease->hwaddr[0],
|
||||
debug("DHCP lease addr %s assigned to MAC %02x:%02x:%02x:%02x:%02x:%02x", ipbuf, requested_lease->hwaddr[0],
|
||||
requested_lease->hwaddr[1], requested_lease->hwaddr[2], requested_lease->hwaddr[3], requested_lease->hwaddr[4],
|
||||
requested_lease->hwaddr[5]);
|
||||
uint32_t now = xTaskGetTickCount();
|
||||
|
|
Loading…
Reference in a new issue