add wifi connect timeout
authorkyz <kongyuzhong@google.com>
Wed, 10 Dec 2025 10:01:55 +0000 (18:01 +0800)
committerkyz <kongyuzhong@google.com>
Fri, 12 Dec 2025 00:53:26 +0000 (08:53 +0800)
20240801完整版/扳手下位机软件/cpbs-202103-20240423/APP/uishow.c
20240801完整版/扳手下位机软件/cpbs-202103-20240423/APP/wifi.c

index 27eb6938c4dc77b5c85b398715d385b16c37151f..a588ca0657f3ea9b421a37cc5a4d147df9d73fdb 100644 (file)
@@ -55,6 +55,7 @@ static void Versions_Interface(void);
 
 static void TorqueAngle_Interface(SetParameter_t* pdata, InterfaceState_t* InterfaceState);  // Å¤¾Ø½Ç¶Èģʽ½çÃæ
 
+extern int Get_Wifi_Status( void );
 void wifi_icon_update( int force )
 {
     static int last_wifi_state = -1;
index f8725558b84ac11416787700d520316b43771f48..1bff044438240f8af4267dafd647abecb9ceac09 100644 (file)
@@ -22,8 +22,16 @@ TaskHandle_t WifiTask_Handler = NULL;
 TaskHandle_t AtTask_Handler = NULL;
 
 typedef struct{
-       uint8_t data;
-}wrench_server_t;
+    int connected;
+    uint32_t active_ts;
+}link_dev_t;
+
+typedef struct{
+       link_dev_t dev[5]; //0:disconnect, 1:connect
+}wrench_srv_t;
+
+static wrench_srv_t g_srv;
+
 
 // ÃüÁî½âÎöÏà¹Ø¶¨Òå
 typedef struct {
@@ -38,6 +46,7 @@ typedef struct {
 typedef struct {
     char* data;
     int len;
+    int linkid;
 } tcp_response_t;
 
 #define TCP_RESPONSE_QUEUE_SIZE 10
@@ -185,45 +194,43 @@ static int parse_tcp_command(const char* data, int len, tcp_command_t* cmd)
 // Éú³ÉÏìÓ¦Êý¾Ý
 static char* generate_response(tcp_command_t* cmd)
 {
-
     static char response[512];
     int len;
        char* result;
-    
 
     if (strcmp(cmd->mid, "RSVI") == 0) {
         // ¶ÁÐÅÏ¢ÃüÁîÏìÓ¦
-        len = snprintf(response, sizeof(response), 
+        len = snprintf(response, sizeof(response),
             "ASVI;%s;%s;%s;OK;SuperWrench;1;1.1.1;120;10;\r\n",
             cmd->ver, cmd->sn, cmd->tg);
     } else if (strcmp(cmd->mid, "RSTS") == 0) {
         // ¶Á״̬ÃüÁîÏìÓ¦
-        len = snprintf(response, sizeof(response), 
+        len = snprintf(response, sizeof(response),
             "ASTS;%s;%s;%s;0;0;0;1;\r\n",
             cmd->ver, cmd->sn, cmd->tg);
     } else if (strcmp(cmd->mid, "RSTD") == 0) {
         // ¶ÁÊý¾ÝÃüÁîÏìÓ¦
-        len = snprintf(response, sizeof(response), 
+        len = snprintf(response, sizeof(response),
             "ASTD;%s;%s;%s;0;;1;1234;2024-01-01 12:00:00;10;1;1;Pset1;2;3;4;0;0;90;1;1;1;3.5;60;\r\n",
             cmd->ver, cmd->sn, cmd->tg);
     } else if (strcmp(cmd->mid, "RSSP") == 0) {
         // Ñ¡JobÃüÁîÏìÓ¦
-        len = snprintf(response, sizeof(response), 
+        len = snprintf(response, sizeof(response),
             "ASSP;%s;%s;%s;0;\r\n",
             cmd->ver, cmd->sn, cmd->tg);
     } else if (strcmp(cmd->mid, "RSEN") == 0) {
         // Ê¹Äܹ¤¾ßÃüÁîÏìÓ¦
-        len = snprintf(response, sizeof(response), 
+        len = snprintf(response, sizeof(response),
             "ASEN;%s;%s;%s;0;\r\n",
             cmd->ver, cmd->sn, cmd->tg);
     } else if (strcmp(cmd->mid, "RSTG") == 0) {
         // ¶ÁÈ¡ÇúÏßÃüÁîÏìÓ¦
-        len = snprintf(response, sizeof(response), 
+        len = snprintf(response, sizeof(response),
             "ASTG;%s;%s;%s;0;;1;1234;2024-01-01 12:00:00;10;1;1;Pset1;2;3;4;0;0;90;1;1;1;3.5;60;0;500;1;2;\r\n",
             cmd->ver, cmd->sn, cmd->tg);
     } else {
         // Î´ÖªÃüÁîÏìÓ¦
-        len = snprintf(response, sizeof(response), 
+        len = snprintf(response, sizeof(response),
             "ERROR;%s;%s;%s;CmdNotFounded;\r\n",
             cmd->ver, cmd->sn, cmd->tg);
     }
@@ -241,11 +248,10 @@ static char* generate_response(tcp_command_t* cmd)
     }
 
     return result;
-
 }
 
 // ½«ÏìÓ¦Êý¾ÝÌí¼Óµ½¶ÓÁÐ
-static int add_tcp_response(char* data, int len)
+static int add_tcp_response(char* data, int len, int linkid)
 {
     int next_tail = (tcp_response_tail + 1) % TCP_RESPONSE_QUEUE_SIZE;
     if (next_tail == tcp_response_head) {
@@ -256,6 +262,7 @@ static int add_tcp_response(char* data, int len)
 
     tcp_response_queue[tcp_response_tail].data = data;
     tcp_response_queue[tcp_response_tail].len = len;
+    tcp_response_queue[tcp_response_tail].linkid = linkid;
     tcp_response_tail = next_tail;
 
     // ÉèÖÃʼþ֪ͨ
@@ -278,6 +285,16 @@ static int get_tcp_response(tcp_response_t* resp)
     return 0;
 }
 
+// »ñÈ¡MCU UUIDº¯Êý
+static void get_mcu_uuid(char* uuid_str, int max_len)
+{
+    // STM32F10xϵÁеÄUUIDµØÖ·Îª0x1FFFF7E8
+    uint32_t* uuid = (uint32_t*)0x1FFFF7E8;
+
+    // UUIDÓÉ3¸ö32λÕûÊý×é³É
+    snprintf(uuid_str, max_len, "%08X%08X%08X", uuid[0], uuid[1], uuid[2]);
+}
+
 void ipd_handler(at_urc_ctx_t *ctx)
 {
        int i;
@@ -299,16 +316,21 @@ void ipd_handler(at_urc_ctx_t *ctx)
             }
             printf("]\n");
 
+            // ¸üÐÂÁ¬½ÓµÄ»î¶¯Ê±¼ä
+            if (ch >= 0 && ch < 5) {
+                g_srv.dev[ch].active_ts = xTaskGetTickCount();
+            }
+
             // ½âÎöTCPÃüÁî
             if (parse_tcp_command(p, len, &cmd) > 0) {
-                printf("Parsed command: MID=%s, VER=%s, SN=%s, TG=%s\n", 
+                printf("Parsed command: MID=%s, VER=%s, SN=%s, TG=%s\n",
                        cmd.mid, cmd.ver, cmd.sn, cmd.tg);
 
                 // Éú³ÉÏìÓ¦
                 response = generate_response(&cmd);
                 if (response) {
-                    // ½«ÏìÓ¦Ìí¼Óµ½¶ÓÁÐ
-                    add_tcp_response(response, strlen(response));
+                    // ½«ÏìÓ¦Ìí¼Óµ½¶ÓÁУ¬Ö¸¶¨linkid
+                    add_tcp_response(response, strlen(response), ch);
                 }
             }
         }
@@ -322,16 +344,38 @@ void connect_handler(at_urc_ctx_t *ctx)
        if( sscanf( ctx->buf, "%d,CONNECT", &linkid ) == 1 ){
                printf( "LINK:%d    ---> CONNECT!\n", linkid );
         g_wifi_st = 1;
+
+        // ¼Ç¼Á¬½Ó״̬
+        if (linkid >= 0 && linkid < 5) {
+            g_srv.dev[linkid].connected = 1;
+            g_srv.dev[linkid].active_ts = xTaskGetTickCount();
+        }
        }
 }
 
 void closed_handler(at_urc_ctx_t *ctx)
 {
+       int i;
        int linkid;
+    int connected = 0;
     printf("closed handler, ctx:%p\n", ctx);
        if( sscanf( ctx->buf, "%d,CLOSED", &linkid ) == 1 ){
                printf( "LINK:%d    ---> CLOSED!\n", linkid );
-        g_wifi_st = 0;
+
+        // ¸üÐÂÁ¬½Ó״̬
+        if (linkid >= 0 && linkid < 5) {
+            g_srv.dev[linkid].connected = 0;
+        }
+        // ¼ì²éÊÇ·ñ»¹ÓÐÆäËûÁ¬½Ó
+
+        connected = 0;
+        for (i = 0; i < 5; i++) {
+            if (g_srv.dev[i].connected) {
+                connected = 1;
+                break;
+            }
+        }
+        g_wifi_st = connected;
        }
 }
 
@@ -339,10 +383,8 @@ urc_item_t wifi_utc_tbl[]={
        { "+IPD", "\r\n", ipd_handler },
        { "CONNECT", "\r\n", connect_handler },
     { "CLOSED", "\r\n", closed_handler },
-
 };
 
-
 at_obj_t wifi_at;
 
 const at_adapter_t wifi_adap = {
@@ -382,22 +424,22 @@ int read_wifi_mode(at_obj_t *at )
        }
        return 0;
 }
+
 /*
  * @brief       Êý¾Ý·¢ËÍ´¦Àí
  * @retval      none
  */
-typedef struct{
+typedef struct {
        char *data;
        uint32_t len;
+       int linkid;
 }tcp_tx_data_t;
 
 static bool tcp_send_handler(at_work_ctx_t *e)
 {
-
        tcp_tx_data_t *p_tx_data = (tcp_tx_data_t *)e->params;
 
-
-       e->printf( e, "AT+CIPSEND=%d,%d\r\n", 0, p_tx_data->len );
+       e->printf( e, "AT+CIPSEND=%d,%d\r\n", p_tx_data->linkid, p_tx_data->len );
 
        if (e->wait_resp(e, ">", 5000) != AT_RET_OK) {      //µÈ´ýÌáʾ·û
                goto Error;
@@ -418,12 +460,12 @@ Error:
  * @param[in]   buf - Êý¾Ý»º³åÇø
  * @param[in]   len - »º³åÇø³¤¶È
  */
-
-static bool wifi_tcp_send( const void *buf, unsigned int len)
+static bool wifi_tcp_send( const void *buf, unsigned int len, int linkid)
 {
        tcp_tx_data_t tx_data;
        tx_data.data = (char *)buf;
        tx_data.len = len;
+       tx_data.linkid = linkid;
 
        if (len == 0)
                return false;
@@ -462,13 +504,14 @@ int wifi_cipstate( at_obj_t *at )
                }
        }
     return ret;
-
 }
 
 int wifi_init(at_obj_t *at )
 {
-       //½ÓÊÕ»º³åÇø
-       char recvbuf[32];
+       char ssid[64];
+       char uuid_str[25];
+       char recvbuf[32];       //½ÓÊÕ»º³åÇø
+
        //ATÓ¦´ð
        at_respond_t r = {"OK", NULL, sizeof(recvbuf), 3000};
        r.recvbuf = recvbuf;
@@ -482,8 +525,13 @@ int wifi_init(at_obj_t *at )
                return -1; //AT+CIPAP=<"ip">[,<"gateway">,<"netmask">]
        else if (at_do_cmd(at, &r, "AT+CWDHCPS=1,2880,\"192.168.4.4\",\"192.168.4.10\"" ) != AT_RET_OK)
                return -1; //AT+CWDHCPS=<enable>,<lease time>,<start IP>,<end IP>
-       else if (at_do_cmd(at, &r, "AT+CWSAP=\"ESP32_softAP\",\"12345678\",5,3") != AT_RET_OK)
+
+       // ÉèÖôøMCU UUIDµÄsoftAPÃû³Æ
+       get_mcu_uuid(uuid_str, sizeof(uuid_str));
+       snprintf(ssid, sizeof(ssid), "AT+CWSAP=\"BTCDBS_%s\",\"12345678\",5,3", uuid_str);
+       if (at_do_cmd(at, &r, ssid) != AT_RET_OK)
                return -1;
+
        else if (at_do_cmd(at, &r, "AT+CIPSERVER=1,8888") != AT_RET_OK)
                return -1;      //AT+CIPSERVER=1
 
@@ -498,7 +546,6 @@ void at_task(void *pvParameters)
        while(1){
                at_process(&wifi_at);
                vTaskDelay(10);
-
        }
 }
 
@@ -511,6 +558,7 @@ void wifi_task(void *pvParameters)
 {
     tcp_response_t resp;
     EventBits_t uxBits;
+       int i;
 
     // ´´½¨Ê¼þ×é
     wifi_event_group = xEventGroupCreate();
@@ -539,18 +587,38 @@ void wifi_task(void *pvParameters)
             // ÓÐÏìÓ¦ÐèÒª·¢ËÍ
             while (get_tcp_response(&resp) == 0) {
                 if (resp.data && resp.len > 0) {
-                    printf("Sending response: %s\n", resp.data);
-                    wifi_tcp_send(resp.data, resp.len);
+                    printf("Sending response to link %d: %s\n", resp.linkid, resp.data);
+                    wifi_tcp_send(resp.data, resp.len, resp.linkid);
                                        printf( "-------generate_response, resp to free: %p\n", resp.data );
                     vPortFree(resp.data); // ÊÍ·ÅÄÚ´æ
                 }
             }
         } else {
-            // ³¬Ê±´¥·¢£¬´òÓ¡¶ÑÄÚ´æÊ¹ÓÃÇé¿ö
-            size_t free_heap = xPortGetFreeHeapSize();
-            size_t min_free_heap = xPortGetMinimumEverFreeHeapSize();
-            printf("FreeRTOS Heap Status - Free: %u bytes, Min Free: %u bytes\n", 
-                   (unsigned int)free_heap, (unsigned int)min_free_heap);
+            // ³¬Ê±´¥·¢£¬¼ì²éÁ¬½Ó³¬Ê±
+            uint32_t current_ts = xTaskGetTickCount();
+            const uint32_t timeout_ms = 10 * 60 * 1000; // 10·ÖÖÓ³¬Ê±
+
+            for (i = 0; i < 5; i++) {
+                if (g_srv.dev[i].connected) {
+                    if ((current_ts - g_srv.dev[i].active_ts) > pdMS_TO_TICKS(timeout_ms)) {
+                        char cmd[32];
+                                               at_respond_t r = {"OK", NULL, 0, 3000};
+                        // Á¬½Ó³¬Ê±£¬¹Ø±ÕÁ¬½Ó
+                        printf("Link %d timeout, closing...\n", i);
+
+                        snprintf(cmd, sizeof(cmd), "AT+CIPCLOSE=%d", i);
+
+                        at_do_cmd(&wifi_at, &r, cmd);
+
+                        // ¸üÐÂÁ¬½Ó״̬
+                        g_srv.dev[i].connected = 0;
+                    }
+                }
+            }
+
+            // ´òÓ¡¶ÑÄÚ´æÊ¹ÓÃÇé¿ö
+            printf("FreeRTOS Heap Status - Free: %u bytes, Min Free: %u bytes\n",
+                   (unsigned int)xPortGetFreeHeapSize(), (unsigned int)xPortGetMinimumEverFreeHeapSize());
 
             wifi_cipstate(&wifi_at); //check wifi connection status
         }