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 {
typedef struct {
char* data;
int len;
+ int linkid;
} tcp_response_t;
#define TCP_RESPONSE_QUEUE_SIZE 10
// Éú³ÉÏìÓ¦Êý¾Ý
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);
}
}
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) {
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;
// ÉèÖÃʼþ֪ͨ
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;
}
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);
}
}
}
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;
}
}
{ "+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 = {
}
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;
* @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;
}
}
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;
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
while(1){
at_process(&wifi_at);
vTaskDelay(10);
-
}
}
{
tcp_response_t resp;
EventBits_t uxBits;
+ int i;
// ´´½¨Ê¼þ×é
wifi_event_group = xEventGroupCreate();
// ÓÐÏìÓ¦ÐèÒª·¢ËÍ
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
}