From ce67dcd097fc34b0b5e52a019caa386a22b2c2d5 Mon Sep 17 00:00:00 2001 From: lilian Date: Thu, 28 Sep 2017 10:07:16 +0200 Subject: [PATCH 1/6] Add posssibility to disable debug in dhcp.c --- extras/dhcpserver/dhcpserver.c | 35 ++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/extras/dhcpserver/dhcpserver.c b/extras/dhcpserver/dhcpserver.c index d87daab..db07cd8 100644 --- a/extras/dhcpserver/dhcpserver.c +++ b/extras/dhcpserver/dhcpserver.c @@ -20,6 +20,12 @@ #include #include +#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. */ @@ -108,7 +114,7 @@ static void dhcpserver_task(void *pxParameter) state->nc = netconn_new (NETCONN_UDP); if(!state->nc) { - printf("DHCP Server Error: Failed to allocate socket.\r\n"); + debug("DHCP Server Error: Failed to allocate socket."); return; } @@ -122,7 +128,7 @@ 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); + debug("DHCP Server Error: Failed to receive DHCP packet. err=%d", err); continue; } @@ -144,7 +150,7 @@ static void dhcpserver_task(void *pxParameter) 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)); + 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)); @@ -153,18 +159,19 @@ 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"); + debug("DHCP Server Error: No message type field found"); continue; } - - printf("State dump. Message type %d\n", *message_type); +#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 expiry %d hwaddr %02x:%02x:%02x:%02x:%02x:%02x\r\n", i, lease->expires, lease->hwaddr[0], + 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: @@ -176,7 +183,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; } } @@ -191,7 +198,7 @@ static void handle_dhcp_discover(struct dhcp_msg *dhcpmsg) dhcp_lease_t *freelease = find_lease_slot(dhcpmsg->chaddr); if(!freelease) { - printf("DHCP Server: All leases taken.\r\n"); + debug("DHCP Server: All leases taken."); return; /* Nothing available, so do nothing */ } @@ -230,7 +237,7 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) } else if(ip_addr_cmp(&requested_ip, IP_ADDR_ANY)) { ip_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; } @@ -240,14 +247,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"); + debug("DHCP Server Error: Address out of range"); send_dhcp_nak(dhcpmsg); return; } @@ -255,14 +262,14 @@ static void handle_dhcp_request(struct dhcp_msg *dhcpmsg) dhcp_lease_t *requested_lease = state->leases + octet_offs; if(requested_lease->expires != 0 && 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]); requested_lease->expires = DHCPSERVER_LEASE_TIME * configTICK_RATE_HZ; From ac6381f1acb4d13c8b58b7ceaf67f1bbed4b4771 Mon Sep 17 00:00:00 2001 From: lilian Date: Thu, 28 Sep 2017 10:15:58 +0200 Subject: [PATCH 2/6] Example with tcp non blocking and events managements --- examples/tcp_non_blocking/Makefile | 5 + examples/tcp_non_blocking/tcp_non_blocking.c | 181 +++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 examples/tcp_non_blocking/Makefile create mode 100644 examples/tcp_non_blocking/tcp_non_blocking.c diff --git a/examples/tcp_non_blocking/Makefile b/examples/tcp_non_blocking/Makefile new file mode 100644 index 0000000..c88b576 --- /dev/null +++ b/examples/tcp_non_blocking/Makefile @@ -0,0 +1,5 @@ +# Makefile for tcp_non_blocking example +PROGRAM=tcp_non_blocking +EXTRA_COMPONENTS=extras/dhcpserver + +include ../../common.mk diff --git a/examples/tcp_non_blocking/tcp_non_blocking.c b/examples/tcp_non_blocking/tcp_non_blocking.c new file mode 100644 index 0000000..0f70d66 --- /dev/null +++ b/examples/tcp_non_blocking/tcp_non_blocking.c @@ -0,0 +1,181 @@ +/* + 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 +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#define AP_SSID "esp-open-rtos AP" +#define AP_PSK "esp-open-rtos" +#define ECHO_PORT_1 50 +#define ECHO_PORT_2 100 + +QueueHandle_t xQueue_events; +typedef struct { + struct netconn *nc ; + uint8_t evt ; +} socket_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) + printf("C:sock:%u\tsta:%u\tevt:%u\tlen:%u\ttyp:%u\tfla:%02X\terr:%d\n", \ + (uint32_t)conn,conn->state,evt,length,conn->type,conn->flags,conn->last_err); + + socket_events events ; + + //If socket got error, it is close or deleted, dont do treatments on it. + if (conn->last_err) + { + return; + } + + //Send the event to the queue + events.nc = conn ; + events.evt = evt ; + xQueueSend(xQueue_events, &events, 1000); + +} + +/* + * Initialize a server netconn and listen port + */ +static void set_tcp_server_socket(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 socket.\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); +} + +/* + * This task manage each netconn connection without block anything + */ +static void nonBlockingTCP(void *pvParameters) +{ + + struct netconn *nc = NULL; // To create servers + + set_tcp_server_socket(&nc, ECHO_PORT_1, netCallback); + printf("Server netconn ready: %u\n",(uint32_t)nc); + set_tcp_server_socket(&nc, ECHO_PORT_2, netCallback); + printf("Server netconn ready: %u\n",(uint32_t)nc); + + 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) { + + socket_events events; + xQueueReceive(xQueue_events, &events, portMAX_DELAY); // Wait here an event on netconn + + if (events.nc->state == NETCONN_LISTEN && events.evt == NETCONN_EVT_RCVPLUS) // If socket 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; + 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.evt == NETCONN_EVT_RCVPLUS && events.nc != nc) + { + printf("Client %u send a message.\n", (uint32_t)events.nc); + 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("%s\n",buf); + } + while (netbuf_next(netbuf) >= 0); + netbuf_delete(netbuf); + } + else + { + printf("error read socket %u, close it \n",(uint32_t)events.nc); + netconn_close(events.nc); + netconn_delete(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( 20, sizeof(socket_events)); + + xTaskCreate(nonBlockingTCP, "lwiptest_noblock", 512, NULL, 2, NULL); +} From 5e4a6ffc38cd84037d338ddec04ffd7653f3bed3 Mon Sep 17 00:00:00 2001 From: lilian Date: Thu, 28 Sep 2017 10:38:41 +0200 Subject: [PATCH 3/6] Improvement on print/debug --- examples/tcp_non_blocking/tcp_non_blocking.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/examples/tcp_non_blocking/tcp_non_blocking.c b/examples/tcp_non_blocking/tcp_non_blocking.c index 0f70d66..3ca181a 100644 --- a/examples/tcp_non_blocking/tcp_non_blocking.c +++ b/examples/tcp_non_blocking/tcp_non_blocking.c @@ -24,6 +24,12 @@ #define ECHO_PORT_1 50 #define ECHO_PORT_2 100 +#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 ; @@ -36,7 +42,7 @@ typedef struct { static void netCallback(struct netconn *conn, enum netconn_evt evt, uint16_t length) { //Show some callback information (debug) - printf("C:sock:%u\tsta:%u\tevt:%u\tlen:%u\ttyp:%u\tfla:%02X\terr:%d\n", \ + 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); socket_events events ; @@ -122,14 +128,13 @@ static void nonBlockingTCP(void *pvParameters) } else if(events.evt == NETCONN_EVT_RCVPLUS && events.nc != nc) { - printf("Client %u send a message.\n", (uint32_t)events.nc); 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("%s\n",buf); + printf("Client %u send: %s\n",(uint32_t)events.nc,buffer); } while (netbuf_next(netbuf) >= 0); netbuf_delete(netbuf); From db1760807463681f67a995ac9de1fdafdedcacdd Mon Sep 17 00:00:00 2001 From: lilian Date: Thu, 28 Sep 2017 10:54:17 +0200 Subject: [PATCH 4/6] Fix loop and print improvements --- examples/tcp_non_blocking/tcp_non_blocking.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/tcp_non_blocking/tcp_non_blocking.c b/examples/tcp_non_blocking/tcp_non_blocking.c index 3ca181a..0fb1f59 100644 --- a/examples/tcp_non_blocking/tcp_non_blocking.c +++ b/examples/tcp_non_blocking/tcp_non_blocking.c @@ -90,9 +90,9 @@ static void nonBlockingTCP(void *pvParameters) struct netconn *nc = NULL; // To create servers set_tcp_server_socket(&nc, ECHO_PORT_1, netCallback); - printf("Server netconn ready: %u\n",(uint32_t)nc); + printf("Server netconn %u ready on port %u.\n",(uint32_t)nc, ECHO_PORT_1); set_tcp_server_socket(&nc, ECHO_PORT_2, netCallback); - printf("Server netconn ready: %u\n",(uint32_t)nc); + 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 @@ -108,25 +108,25 @@ static void nonBlockingTCP(void *pvParameters) if (events.nc->state == NETCONN_LISTEN && events.evt == NETCONN_EVT_RCVPLUS) // If socket is a server and receive incoming event on it { - printf("client incoming on server %u\n", (uint32_t)events.nc); + 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); + printf("New client is %u.\n",(uint32_t)nc_in); ip_addr_t client_addr; 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", + 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.evt == NETCONN_EVT_RCVPLUS && events.nc != nc) + else if(events.evt == NETCONN_EVT_RCVPLUS && events.nc->state != NETCONN_LISTEN) // If socket is the client and receive data { if ((netconn_recv(events.nc, &netbuf)) == ERR_OK) // data incoming ? { @@ -141,7 +141,7 @@ static void nonBlockingTCP(void *pvParameters) } else { - printf("error read socket %u, close it \n",(uint32_t)events.nc); + printf("Error read socket %u, close it \n",(uint32_t)events.nc); netconn_close(events.nc); netconn_delete(events.nc); } From 256237dd5c1df5ddea22f23d97c69244c108803e Mon Sep 17 00:00:00 2001 From: lilian Date: Thu, 28 Sep 2017 21:10:40 +0200 Subject: [PATCH 5/6] add a way to prevent error on socket closed manually + some improvements --- examples/tcp_non_blocking/tcp_non_blocking.c | 37 ++++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/examples/tcp_non_blocking/tcp_non_blocking.c b/examples/tcp_non_blocking/tcp_non_blocking.c index 0fb1f59..6ae0102 100644 --- a/examples/tcp_non_blocking/tcp_non_blocking.c +++ b/examples/tcp_non_blocking/tcp_non_blocking.c @@ -23,6 +23,7 @@ #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__) @@ -33,7 +34,7 @@ QueueHandle_t xQueue_events; typedef struct { struct netconn *nc ; - uint8_t evt ; + uint8_t type ; } socket_events; /* @@ -52,10 +53,18 @@ static void netCallback(struct netconn *conn, enum netconn_evt evt, uint16_t len { 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 - events.nc = conn ; - events.evt = evt ; xQueueSend(xQueue_events, &events, 1000); } @@ -63,7 +72,7 @@ static void netCallback(struct netconn *conn, enum netconn_evt evt, uint16_t len /* * Initialize a server netconn and listen port */ -static void set_tcp_server_socket(struct netconn **nc, uint16_t port, netconn_callback callback) +static void set_tcp_server_netconn(struct netconn **nc, uint16_t port, netconn_callback callback) { if(nc == NULL) { @@ -81,6 +90,13 @@ static void set_tcp_server_socket(struct netconn **nc, uint16_t port, netconn_ca netconn_listen(*nc); } +static void close_tcp_netconn(struct netconn *nc) +{ + nc->last_err=ERR_CLSD; //It is hacky way to be sure than callback will don't do treatment on a socket closed and deleted + netconn_close(nc); + netconn_delete(nc); +} + /* * This task manage each netconn connection without block anything */ @@ -89,9 +105,9 @@ static void nonBlockingTCP(void *pvParameters) struct netconn *nc = NULL; // To create servers - set_tcp_server_socket(&nc, ECHO_PORT_1, netCallback); + 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_socket(&nc, ECHO_PORT_2, netCallback); + 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 @@ -106,7 +122,7 @@ static void nonBlockingTCP(void *pvParameters) socket_events events; xQueueReceive(xQueue_events, &events, portMAX_DELAY); // Wait here an event on netconn - if (events.nc->state == NETCONN_LISTEN && events.evt == NETCONN_EVT_RCVPLUS) // If socket is a server and receive incoming event on it + if (events.nc->state == NETCONN_LISTEN) // If socket 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); @@ -126,7 +142,7 @@ static void nonBlockingTCP(void *pvParameters) client_port); netconn_write(nc_in, buf, strlen(buf), NETCONN_COPY); } - else if(events.evt == NETCONN_EVT_RCVPLUS && events.nc->state != NETCONN_LISTEN) // If socket is the client and receive data + else if(events.nc->state != NETCONN_LISTEN) // If socket is the client and receive data { if ((netconn_recv(events.nc, &netbuf)) == ERR_OK) // data incoming ? { @@ -142,8 +158,7 @@ static void nonBlockingTCP(void *pvParameters) else { printf("Error read socket %u, close it \n",(uint32_t)events.nc); - netconn_close(events.nc); - netconn_delete(events.nc); + close_tcp_netconn(events.nc); } } } @@ -180,7 +195,7 @@ void user_init(void) printf("DHCP started\n"); //Create a queue to store events on netconns - xQueue_events = xQueueCreate( 20, sizeof(socket_events)); + xQueue_events = xQueueCreate( EVENTS_QUEUE_SIZE, sizeof(socket_events)); xTaskCreate(nonBlockingTCP, "lwiptest_noblock", 512, NULL, 2, NULL); } From 895da97f5cd33f2830360324c62d875b2f876ee9 Mon Sep 17 00:00:00 2001 From: lilian Date: Tue, 3 Oct 2017 10:57:19 +0200 Subject: [PATCH 6/6] rename some comments --- examples/tcp_non_blocking/tcp_non_blocking.c | 26 +++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/tcp_non_blocking/tcp_non_blocking.c b/examples/tcp_non_blocking/tcp_non_blocking.c index 6ae0102..2693305 100644 --- a/examples/tcp_non_blocking/tcp_non_blocking.c +++ b/examples/tcp_non_blocking/tcp_non_blocking.c @@ -35,7 +35,7 @@ QueueHandle_t xQueue_events; typedef struct { struct netconn *nc ; uint8_t type ; -} socket_events; +} netconn_events; /* * This function will be call in Lwip in each event on netconn @@ -46,9 +46,9 @@ static void netCallback(struct netconn *conn, enum netconn_evt evt, uint16_t len 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); - socket_events events ; + netconn_events events ; - //If socket got error, it is close or deleted, dont do treatments on it. + //If netconn got error, it is close or deleted, dont do treatments on it. if (conn->last_err) { return; @@ -81,7 +81,7 @@ static void set_tcp_server_netconn(struct netconn **nc, uint16_t port, netconn_c } *nc = netconn_new_with_callback(NETCONN_TCP, netCallback); if(!*nc) { - printf("Status monitor: Failed to allocate socket.\n"); + printf("Status monitor: Failed to allocate netconn.\n"); return; } netconn_set_nonblocking(*nc,NETCONN_FLAG_NON_BLOCKING); @@ -90,9 +90,12 @@ static void set_tcp_server_netconn(struct netconn **nc, uint16_t port, netconn_c netconn_listen(*nc); } +/* + * Close and delete a socket properly + */ static void close_tcp_netconn(struct netconn *nc) { - nc->last_err=ERR_CLSD; //It is hacky way to be sure than callback will don't do treatment on a socket closed and deleted + nc->last_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); } @@ -119,10 +122,10 @@ static void nonBlockingTCP(void *pvParameters) while(1) { - socket_events events; + netconn_events events; xQueueReceive(xQueue_events, &events, portMAX_DELAY); // Wait here an event on netconn - if (events.nc->state == NETCONN_LISTEN) // If socket is a server and receive incoming event on it + 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); @@ -132,8 +135,7 @@ static void nonBlockingTCP(void *pvParameters) netconn_delete(nc_in); } printf("New client is %u.\n",(uint32_t)nc_in); - - ip_addr_t client_addr; + 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", @@ -142,7 +144,7 @@ static void nonBlockingTCP(void *pvParameters) client_port); netconn_write(nc_in, buf, strlen(buf), NETCONN_COPY); } - else if(events.nc->state != NETCONN_LISTEN) // If socket is the client and receive data + 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 ? { @@ -157,8 +159,8 @@ static void nonBlockingTCP(void *pvParameters) } else { - printf("Error read socket %u, close it \n",(uint32_t)events.nc); close_tcp_netconn(events.nc); + printf("Error read netconn %u, close it \n",(uint32_t)events.nc); } } } @@ -195,7 +197,7 @@ void user_init(void) printf("DHCP started\n"); //Create a queue to store events on netconns - xQueue_events = xQueueCreate( EVENTS_QUEUE_SIZE, sizeof(socket_events)); + xQueue_events = xQueueCreate( EVENTS_QUEUE_SIZE, sizeof(netconn_events)); xTaskCreate(nonBlockingTCP, "lwiptest_noblock", 512, NULL, 2, NULL); }