优化tcp通信处理
authorkyz <kongyuzhong@google.com>
Sat, 10 May 2025 06:54:19 +0000 (14:54 +0800)
committerkyz <kongyuzhong@google.com>
Sat, 10 May 2025 06:54:19 +0000 (14:54 +0800)
user/user_noise_sensor.c
user/user_tcp.c

index 09d35e0d0afbed0099efa56a2483f0e13d885e9d..83b480b6b03e57414bf6d3483df2cdbc1e012bef 100644 (file)
@@ -100,12 +100,12 @@ static void user_noise_task(void *argument)
 
        while(1){
                osDelay(1000);
-               printf( "noise read ...\n" );
+               //printf( "noise read ...\n" );
                if( user_noise_sensor_read( &noise ) == 0 ){
                        err = 0;
                        noise_state = 1;
                        user_noise_info_update( noise, noise_state );
-                       printf( "noise db:%.1f ...\n", noise );
+                       //printf( "noise db:%.1f ...\n", noise );
                }else{
                        if( noise_state >= 0 ){
                                if( err++ > 10 ){
index 6d9f564831ef1ef5d34585960919aa3700cb50ed..bab15ad553ae990a35033d752ea8a70512ffa99c 100644 (file)
@@ -20,7 +20,8 @@ static int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data,
 
 struct tcp_pcb *tcp_server_pcb;
 static struct tcp_pcb *tcp_connected_pcb = NULL;
-static uint16_t tx_sn = 0;
+static uint16_t tx_pkt_sn = 0;
+static uint16_t rx_pkt_sn;
 
 void Tcp_Server_Init(void)
 {
@@ -54,8 +55,8 @@ int32_t user_tcp_write_pkt( uint16_t cmd, uint8_t *data, uint32_t data_sz )
 {
        uint8_t buf[256];
        uint16_t n;
-       tx_sn++;
-       n = wrap_pkt( buf, tx_sn, cmd, data, data_sz );
+       tx_pkt_sn++;
+       n = wrap_pkt( buf, tx_pkt_sn, cmd, data, data_sz );
        return user_tcp_write_data( buf, n );
 }
 
@@ -74,7 +75,7 @@ static err_t tcp_server_accept(void *arg, struct tcp_pcb *pcb, err_t err)
 
         /* ÅäÖýÓÊջص÷º¯Êý */
         tcp_recv(pcb, tcp_server_recv);
-
+       printf( "tcp accept, pcb:%p\n", pcb );
         return ERR_OK;
 }
 
@@ -99,7 +100,7 @@ CMD:
    3002: Laki Rx
 
 */
-static uint16_t rx_pkt_sn;
+
 int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint32_t data_sz )
 {
        uint8_t *p = buf;
@@ -140,9 +141,11 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len )
        
        sensor_info_t sensor;
 
-//     uint16_t noise;
-//     uint16_t sound_freq;
-//     uint16_t sound_amplitude;
+
+       if( tcp_connected_pcb == 0 ){
+               rx_pkt_sn = 0;
+               tx_pkt_sn = 0;
+       }
 
        resp_sz = 0;
        if( len >= 10 && p[0]==0x55 && p[1]==0xAA ){
@@ -190,7 +193,7 @@ static err_t tcp_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *tcp_re
         //struct pbuf *tcp_send_pbuf;
         struct name *name = (struct name *)arg;
                uint8_t *rx_data;
-
+       printf( "tcp recv, pcb:%p\n", pcb );
         if (tcp_recv_pbuf != NULL)
         {
                 /* À©´óÊÕ·¢Êý¾ÝµÄ´°¿Ú */
@@ -228,6 +231,8 @@ static err_t tcp_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *tcp_re
         {
                 /* ÊÍ·ÅÄÚ´æ */
                 mem_free(name);
+                               printf( "tcp_close: pcb:%p\n", tcp_connected_pcb );
+                               tcp_connected_pcb = 0;
                 return tcp_close(pcb);
         }