From 687929b43da2e933706936b07abc6f44b4028226 Mon Sep 17 00:00:00 2001 From: windowsair Date: Sun, 14 Nov 2021 22:04:32 +0800 Subject: [PATCH] feat(lwip) & build: Imporve netconn code, modify the default idf tool to use the sdk under the project path --- idf.py | 2 +- main/main.c | 7 +++++-- main/tcp_netconn.c | 24 +++++++++++++++++------- main/tcp_netconn.h | 1 + main/usbip_server.c | 6 ++++-- main/wifi_configuration.h | 12 ++++++++++++ 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/idf.py b/idf.py index 76bbfde..c31f0cb 100644 --- a/idf.py +++ b/idf.py @@ -118,7 +118,7 @@ def check_environment(): if not executable_exists(["cmake", "--version"]): raise FatalError("'cmake' must be available on the PATH to use %s" % PROG) # find the directory idf.py is in, then the parent directory of this, and assume this is IDF_PATH - detected_idf_path = _realpath(os.path.join(os.path.dirname(__file__), "..")) + detected_idf_path = _realpath(os.path.join(os.path.dirname(__file__), "./ESP8266_RTOS_SDK")) if "IDF_PATH" in os.environ: set_idf_path = _realpath(os.environ["IDF_PATH"]) if set_idf_path != detected_idf_path: diff --git a/main/main.c b/main/main.c index 9a53f75..0618f37 100644 --- a/main/main.c +++ b/main/main.c @@ -173,13 +173,16 @@ void app_main() DAP_Setup(); timer_init(); + // Specify the usbip server task #if (USE_KCP == 1) xTaskCreate(kcp_server_task, "kcp_server", 4096, NULL, 7, NULL); -#else - //xTaskCreate(tcp_netconn_task, "tcp_server", 4096, NULL, 14, NULL); +#elif (USE_TCP_NETCONN == 1) + xTaskCreate(tcp_netconn_task, "tcp_server", 4096, NULL, 14, NULL); +#else // BSD style xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 14, NULL); #endif + // DAP handle task xTaskCreate(DAP_Thread, "DAP_Task", 2048, NULL, 10, &kDAPTaskHandle); // SWO Trace Task diff --git a/main/tcp_netconn.c b/main/tcp_netconn.c index 4fe8fdd..1c58e83 100644 --- a/main/tcp_netconn.c +++ b/main/tcp_netconn.c @@ -57,8 +57,8 @@ typedef struct extern TaskHandle_t kDAPTaskHandle; extern int kRestartDAPHandle; +extern uint8_t kState; -uint8_t kStateNetconn = ACCEPTING; struct netconn *kNetconn = NULL; int tcp_netconn_send(const void *buffer, size_t len) @@ -72,8 +72,8 @@ int tcp_netconn_send(const void *buffer, size_t len) 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); + 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->pending_err); netconn_events events; @@ -173,14 +173,14 @@ void tcp_netconn_task() { netbuf_data(netbuf, (void *)&buffer, &len_buf); kNetconn = events.nc; - switch (kStateNetconn) + switch (kState) { case ACCEPTING: - kStateNetconn = ATTACHING; + kState = ATTACHING; case ATTACHING: attach((uint8_t *)buffer, len_buf); - kStateNetconn = EMULATING; // FIXME + kState = EMULATING; break; case EMULATING: @@ -194,8 +194,18 @@ void tcp_netconn_task() } else { + if (events.nc->pending_err == ERR_CLSD) + { + continue; // The same hacky way to treat a closed connection + } + os_printf("Shutting down socket and restarting...\r\n"); close_tcp_netconn(events.nc); - printf("Error read netconn %u, close it \n", (uint32_t)events.nc); + if (kState == EMULATING) + kState = ACCEPTING; + // Restart DAP Handle + kRestartDAPHandle = 1; + if (kDAPTaskHandle) + xTaskNotifyGive(kDAPTaskHandle); } } } diff --git a/main/tcp_netconn.h b/main/tcp_netconn.h index c166cf7..a7fd54d 100644 --- a/main/tcp_netconn.h +++ b/main/tcp_netconn.h @@ -4,5 +4,6 @@ #include int tcp_netconn_send(const void *buffer, size_t len); +void tcp_netconn_task(); #endif \ No newline at end of file diff --git a/main/usbip_server.c b/main/usbip_server.c index eac6dfd..e41c00f 100644 --- a/main/usbip_server.c +++ b/main/usbip_server.c @@ -3,6 +3,7 @@ #include "main/usbip_server.h" #include "main/kcp_server.h" +#include "main/tcp_netconn.h" #include "main/DAP_handle.h" #include "main/wifi_configuration.h" @@ -37,8 +38,9 @@ static void send_stage2_unlink(usbip_stage2_header *req_header); int usbip_network_send(int s, const void *dataptr, size_t size, int flags) { #if (USE_KCP == 1) return kcp_network_send(dataptr, size); -#else - //return tcp_netconn_send(dataptr, size); +#elif (USE_TCP_NETCONN == 1) + return tcp_netconn_send(dataptr, size); +#else // BSD style return send(s, dataptr, size, flags); #endif } diff --git a/main/wifi_configuration.h b/main/wifi_configuration.h index d6375dc..4ce53f2 100644 --- a/main/wifi_configuration.h +++ b/main/wifi_configuration.h @@ -19,11 +19,23 @@ #define DAP_IP_ADDRESS 192, 168, 137, 123 #define DAP_IP_GATEWAY 192, 168, 137, 1 #define DAP_IP_NETMASK 255, 255, 255, 0 +// + +#define USE_TCP_NETCONN 0 // DO NOT CHANGE #define PORT 3240 #define CONFIG_EXAMPLE_IPV4 1 #define USE_KCP 0 #define MTU_SIZE 1500 +// + +#if (USE_TCP_NETCONN == 1 && USE_KCP == 1) +#error Can not use KCP and TCP at the same time! +#endif + +#if (USE_KCP == 1) +#warning KCP is a very experimental feature, and it should not be used under any circumstances. Please make sure what you are doing. Related usbip version: https://github.com/windowsair/usbip-win +#endif #endif \ No newline at end of file