以太网OTA功能
authorkyz <kyz@kyz.pub>
Tue, 30 Apr 2024 14:41:00 +0000 (22:41 +0800)
committerkyz <kyz@kyz.pub>
Wed, 1 May 2024 01:30:35 +0000 (09:30 +0800)
Core/Src/freertos.c
user/user_eth.c
user/user_ota.c
user/user_report.c
user/user_wifi.c

index b7c907344ee489f0aff9afb019490922ad89aa26..85881d4ef62ec935b9e2bf215ee2debbe8158abf 100644 (file)
@@ -51,7 +51,7 @@
 osThreadId_t appTaskHandle;
 const osThreadAttr_t appTask_attributes = {
   .name = "appTask",
-  .stack_size = 512 * 4,
+  .stack_size = 512*2,
   .priority = (osPriority_t) osPriorityNormal,
 };
 
index e37a341b361ba766d835f83c01f08ba1b7422459..edd4672f28d2ed2f23c3d2915a12eae2b778786c 100644 (file)
 
 
 typedef struct{
-//    char* host;
-//    uint16_t port;
-
+    char host[100];
+    uint16_t port;
     ip_addr_t addr;
+
     struct tcp_pcb *pcb;
     int32_t connected;  //0 wait connect, 1: connected, -1:connect fail
     int32_t connection_close; //1: to close tcp connection
@@ -33,7 +33,8 @@ static const osMutexAttr_t clientMutex_attributes = {
   .name = "devinfoMutex"
 };
 
-static tcp_cli_t  g_client;
+static tcp_cli_t  g_iot_client;
+static tcp_cli_t  g_ota_client;
 
 extern char * devinfo_report_str( void );
 
@@ -42,24 +43,24 @@ int32_t eth_devinfo_report( tcp_cli_t *client )
     char * rpt_data;
     err_t err;
 
-    if( !g_client.pcb ){
-        printf( "---dev info report  fail , pcb is NULL\n" );
+    if( !g_iot_client.pcb ){
+        debug_printf( "---dev info report  fail , pcb is NULL\n" );
         return -1;
     }
 
     rpt_data = devinfo_report_str();
 
-    if( strlen( rpt_data) > tcp_sndbuf(g_client.pcb) ){
-        printf( "tx len[%d] > sendbuf[%d]\n", strlen(rpt_data), tcp_sndbuf(g_client.pcb) );
+    if( strlen( rpt_data) > tcp_sndbuf(g_iot_client.pcb) ){
+        debug_printf( "tx len[%d] > sendbuf[%d]\n", strlen(rpt_data), tcp_sndbuf(g_iot_client.pcb) );
         return -1;
     }
 
-    err = tcp_write( g_client.pcb, rpt_data, strlen(rpt_data), 1 );
+    err = tcp_write( g_iot_client.pcb, rpt_data, strlen(rpt_data), 1 );
     if( err == ERR_OK ){
-        err = tcp_output( g_client.pcb );
-        //printf( "--tcp output ret:%d-%s, pcb:%p, state:%d-%s\n", err, lwip_strerr(err),  g_client.pcb, g_client.pcb->state, tcp_debug_state_str( g_client.pcb->state) );
+        err = tcp_output( g_iot_client.pcb );
+        //debug_printf( "--tcp output ret:%d-%s, pcb:%p, state:%d-%s\n", err, lwip_strerr(err),  g_iot_client.pcb, g_iot_client.pcb->state, tcp_debug_state_str( g_iot_client.pcb->state) );
     }else{
-        printf( "--tcp write ret:%d-%s, pcb:%p, state:%d-%s\n", err, lwip_strerr(err),  g_client.pcb, g_client.pcb->state, tcp_debug_state_str( g_client.pcb->state) );
+        debug_printf( "--tcp write ret:%d-%s, pcb:%p, state:%d-%s\n", err, lwip_strerr(err),  g_iot_client.pcb, g_iot_client.pcb->state, tcp_debug_state_str( g_iot_client.pcb->state) );
     }
     return err;
 }
@@ -67,40 +68,50 @@ int32_t eth_devinfo_report( tcp_cli_t *client )
 void DNS_found_cb(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
 {
     //uint32_t* ip = (uint32_t*)ipaddr;
-    //printf("DNS found: IP %d:%d:%d:%d\n",((*ip)&0xff),(((*ip)>>8)&0xff),(((*ip)>>16)&0xff),(((*ip)>>24)&0xff));
+    //debug_printf("DNS found: IP %d:%d:%d:%d\n",((*ip)&0xff),(((*ip)>>8)&0xff),(((*ip)>>16)&0xff),(((*ip)>>24)&0xff));
 }
 
+#if 0
 
-int32_t tcp_eth_connection_close( tcp_cli_t *cli )
+void user_client_lock( tcp_cli_t *client )
 {
-    struct tcp_pcb *tpcb = cli->pcb;
-    err_t err;
+    osMutexAcquire( client->mutex,osWaitForever );
+}
+
+void user_client_unlock( tcp_cli_t *client )
+{
+    osMutexRelease( client->mutex );
+}
+
+#else
+
 
-    printf( "tcp_eth_connection_close pcb:%p, state:%d-%s\n", tpcb, tpcb->state, tcp_debug_state_str( tpcb->state) );
-    if( tpcb ){
-        /* remove callbacks */
-        tcp_recv(tpcb, NULL);
-        tcp_sent(tpcb, NULL);
-        tcp_poll(tpcb, NULL,0);
-        /* close tcp connection */
-        printf( "tcp abort tpcb:%p, state:%d-%s\n", tpcb, tpcb->state, tcp_debug_state_str( tpcb->state) );
-        //err = tcp_close(tpcb);
-        tcp_abort( tpcb );
-        cli->pcb = NULL;
-        printf( "tcp abort return:%d, tpcb:%p, state:%d-%s\n", err, tpcb, tpcb->state, tcp_debug_state_str( tpcb->state) );
+void tcp_eth_close( tcp_cli_t *client )
+{
+    if( client && client->pcb ){
+        tcp_arg( client->pcb, NULL);
+        tcp_sent( client->pcb, NULL);
+        tcp_recv( client->pcb, NULL);
+        tcp_err( client->pcb, NULL);
+        //tcp_poll( client->pcb, NULL);
+        tcp_close( client->pcb );
+        client->pcb = NULL;
     }
-    cli->connected = 0;
-    return ERR_OK;
+    client->connected = 0;
 }
 
+
 static err_t tcp_eth_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
 {
+    tcp_cli_t *cli;
+    cli = ( tcp_cli_t *)arg;
+
     if( err == ERR_OK ){
-        g_client.connected = 1;
-        printf( "cb -- tcp connect ok!\n" );
+        cli->connected = 1;
+        debug_printf( "cb -- tcp connect ok!\n" );
     }else{
-        g_client.connected = -1;
-        printf( "cb -- tcp connect fail, err:%s!\n", lwip_strerr( err ) );
+        cli->connected = -1;
+        debug_printf( "cb -- tcp connect fail, err:%s!\n", lwip_strerr( err ) );
     }
     return ERR_OK;
 }
@@ -111,13 +122,14 @@ static err_t tcp_eth_poll(void *arg, struct tcp_pcb *tpcb)
     err_t ret_err = ERR_OK;
 
     if( !client || client->pcb != tpcb ){
-        printf( "--- tcp abort pcb:%p - state:%s, cli.pcb:%p\n", tpcb, tcp_debug_state_str( tpcb->state), client->pcb ); 
-        tcp_abort( tpcb );
+        debug_printf( "--- tcp abort pcb:%p - state:%s, cli.pcb:%p\n", tpcb, tcp_debug_state_str( tpcb->state), client->pcb ); 
+        //tcp_abort( tpcb );
+               tcp_close( tpcb );
     }else{
-        if( client->connection_close ){
-            client->connection_close = 0;
-            tcp_eth_connection_close( client );
-        }
+        ///if( client->connection_close ){
+        ///    client->connection_close = 0;
+        ///    tcp_eth_connection_close( client );
+        ///}
         //printf( "tcp poll:%p - state:%s, cli.pcb:%p\n", tpcb, tcp_debug_state_str( tpcb->state), client->pcb ); 
     }
     return ret_err;
@@ -125,23 +137,27 @@ static err_t tcp_eth_poll(void *arg, struct tcp_pcb *tpcb)
 
 static err_t tcp_eth_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
 {
-    //printf( "tcp sent %d Bytes\n", len );
+    debug_printf( "tcp sent %d Bytes\n", len );
     return ERR_OK;
 }
-
+static int32_t iot_act_resp = 0;
 static err_t tcp_eth_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
 { 
     tcp_cli_t *client = (tcp_cli_t*)arg;
     err_t ret_err; 
     if (p == NULL){
         /* if we receive an empty tcp frame from server => close connection */
-        printf( "remote close connection!\n" );
-        tcp_eth_connection_close( client );
+        debug_printf( "---------------remote close, tcp close( %p )\n", tpcb );
+        tcp_eth_close( client );
+        //tcp_close( tpcb );
+        //client->connected = 0;
+        //client->pcb = NULL;
+        //tcp_eth_connection_close( client );
         ret_err = ERR_OK;
     }else if(err != ERR_OK){
         /* else : a non empty frame was received from server but for some reason err != ERR_OK */
         /* free received pbuf*/
-        printf( "tcp recv err:%d-%s, pbuf:%p!\n", err, lwip_strerr(err), p );
+        debug_printf( "tcp recv err:%d-%s, pbuf:%p!\n", err, lwip_strerr(err), p );
         if (p != NULL){
             pbuf_free(p);
         }
@@ -151,14 +167,27 @@ static err_t tcp_eth_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t
         tcp_recved(tpcb, p->tot_len);
 
         //TODO: handle rx data
-        printf( "tcp recv %d Bytes!\n", p->tot_len );
+        debug_printf( "tcp recv %d Bytes!\n", p->tot_len );
+#if 1
+               if( client == &g_iot_client ){
+            extern int32_t iot_cmd_parse( uint32_t link, char *cmd );
+                       int32_t act = iot_cmd_parse( 1, (char *)p->payload );
+                       if( act == 1 || act == 2 ){
+                               iot_act_resp = 1;
+            }
+               }else if( client == &g_ota_client ){
+            //TODO:
+            extern int32_t ota_handle_http_resp_eth( uint8_t *rx_data, uint32_t rx_sz );
+            ota_handle_http_resp_eth( p->payload, p->tot_len );
+        }
+#endif
         pbuf_free(p);
         ret_err = ERR_OK;
     }else{
         /* data received when connection already closed */
         /* Acknowledge data reception */
         tcp_recved(tpcb, p->tot_len);
-        printf( "tcp data received when connection already closed\n" );
+        debug_printf( "tcp data received when connection already closed\n" );
 
         /* free pbuf and do nothing */
         pbuf_free(p);
@@ -167,6 +196,16 @@ static err_t tcp_eth_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t
     return ret_err;
 }
 
+void tcp_eth_err(void *arg, err_t err)
+{
+       debug_printf( "tcp eth err arg:%p- err:%d-%s!", arg, err, lwip_strerr( err ) );
+    if( arg ){
+        tcp_cli_t *client = (tcp_cli_t *)arg;
+        debug_printf( "--------------tcp_eth_err, close client->pcb:%p\n", client->pcb );
+        tcp_eth_close( client );
+    }
+}
+
 int32_t tcp_eth_connect( tcp_cli_t *client, ip_addr_t *addr, uint16_t port )
 {
     TickType_t tick;
@@ -177,9 +216,10 @@ int32_t tcp_eth_connect( tcp_cli_t *client, ip_addr_t *addr, uint16_t port )
     err_t err;
 
     if (!tpcb){
+        debug_printf( "tcp new fail!\n" );
         return ERR_MEM;
     }
-
+    debug_printf( "--------------------tcp_eth_connect: ------tcp new:%p\n", tpcb );
     client->connected = 0;
     client->pcb = NULL;
 
@@ -196,26 +236,17 @@ int32_t tcp_eth_connect( tcp_cli_t *client, ip_addr_t *addr, uint16_t port )
         tcp_arg(tpcb, client);
         tcp_sent( tpcb, tcp_eth_sent );
         tcp_recv( tpcb, tcp_eth_recv );
-        tcp_poll( tpcb, tcp_eth_poll, 2 );
-        printf( "tcp connect success, tpcb:%p, state:%d-%s\n", tpcb, tpcb->state,  tcp_debug_state_str(tpcb->state) );
+        tcp_poll( tpcb, tcp_eth_poll, 5 );
+        tcp_err( tpcb, tcp_eth_err );
+        debug_printf( "tcp connect success, tpcb:%p, state:%d-%s\n", tpcb, tpcb->state,  tcp_debug_state_str(tpcb->state) );
         return ERR_OK;
     }else{
-        tcp_close( tpcb );
-        printf( "tcp connect fail, close tpcb:%p\n", tpcb );
+//        tcp_close( tpcb );
+        debug_printf( "---------------tcp connect fail, tcp close( %p )\n", tpcb );
         return ERR_CONN;
     }
 }
 
-void user_client_lock( tcp_cli_t *client )
-{
-    osMutexAcquire( client->mutex,osWaitForever );
-}
-
-void user_client_unlock( tcp_cli_t *client )
-{
-    osMutexRelease( client->mutex );
-}
-
 void user_eth_task( void* unused )
 {
     TickType_t  last_ticks;
@@ -223,10 +254,14 @@ void user_eth_task( void* unused )
     ip_addr_t addr;
     uint16_t port;
     err_t err;
+    tcp_cli_t *iot;
 
-    memset( &g_client, 0, sizeof( g_client ) );
-    g_client.p_netif = &gnetif;
-    g_client.mutex = osMutexNew(&clientMutex_attributes);
+    iot = &g_iot_client;
+    memset( iot, 0, sizeof( *iot ) );
+
+    iot->p_netif = &gnetif;
+    
+    iot->mutex = osMutexNew(&clientMutex_attributes);
     /* init code for LWIP */
     MX_LWIP_Init();
     osDelay(2000);
@@ -236,9 +271,10 @@ void user_eth_task( void* unused )
     while(1){
 
         //1. wait linkup
-        if( !netif_is_link_up(g_client.p_netif) || !g_client.p_netif->ip_addr.addr){
+        if( !netif_is_link_up(g_iot_client.p_netif) || !g_iot_client.p_netif->ip_addr.addr){
             g_dev.eth_connected = 0;
-            osDelay( 1000 );
+            osDelay( 2000 );
+                       //debug_printf( "eth task: wait netif linkup\n" );
             continue;
         }
         g_dev.eth_connected = 1;
@@ -254,56 +290,71 @@ void user_eth_task( void* unused )
         addr.addr = 0;
         if( dns_gethostbyname( g_dev.cfg.net.tcp_server, &addr, DNS_found_cb, NULL) || !addr.addr ){
             osDelay( 1000 );
-            printf( "dns %s -- fail!\n", g_dev.cfg.net.tcp_server );
+            debug_printf( "dns %s -- fail!\n", g_dev.cfg.net.tcp_server );
             continue;
         }
-        printf( "-- dns %s -- %d.%d.%d.%d\n", g_dev.cfg.net.tcp_server, addr.addr&0xff, (addr.addr>>8)&0xff, (addr.addr>>16)&0xff, (addr.addr>>24)&0xff );
+        debug_printf( "-- dns %s -- %d.%d.%d.%d\n", g_dev.cfg.net.tcp_server, addr.addr&0xff, (addr.addr>>8)&0xff, (addr.addr>>16)&0xff, (addr.addr>>24)&0xff );
 
         //4.连接
-        if( tcp_eth_connect( &g_client, &addr, port ) != ERR_OK ){
+        if( tcp_eth_connect( &g_iot_client, &addr, port ) != ERR_OK ){
             osDelay( 5000 );
             continue;
         }
-        
+
         g_dev.eth_connected = 2;
         //5.定时上报
         last_ticks = xTaskGetTickCount();
-        while( g_client.connected > 0 ){
+        while( g_iot_client.connected > 0 ){
             //连接状态,定时上报数据
             vTaskDelayUntil(&last_ticks, 1000 ); 
             //定时上报
-            if( !netif_is_link_up(g_client.p_netif)){
+            if( !netif_is_link_up(g_iot_client.p_netif)){
                 //链路中断
-                //tcp_eth_connection_close( &g_client );
-                //tcp_abort( g_client.pcb );
-                //g_client.pcb = NULL;
-                g_client.connection_close = 1;
-                break;
+                //tcp_eth_connection_close( &g_iot_client );
+                //tcp_abort( g_iot_client.pcb );
+                //g_iot_client.pcb = NULL;
+                //g_iot_client.connection_close = 1;
+                //break;
             }
-            err = eth_devinfo_report( &g_client);
+
+            if( iot_act_resp == 1 ){
+                char * lock_resp_str( void );
+                char * s = lock_resp_str();
+                if( tcp_write( g_iot_client.pcb, s, strlen(s), 1 ) == ERR_OK ){
+                    tcp_output( g_iot_client.pcb );
+                }
+                iot_act_resp = 0;
+            }
+
+                                               if( g_ota_client.connected == 1 ){
+                                                       continue;
+                                               }
+
+            err = eth_devinfo_report( &g_iot_client);
             if( err == ERR_OK  ){
                 tcp_err_cnt = 0;
             }else if( err == ERR_MEM  ){
                 if( tcp_err_cnt++ > 3 ){
                     tcp_err_cnt = 0;
-                    //tcp_eth_connection_close( &g_client );
-                    printf( "tcp err , close ...\n" );
-                    g_client.connection_close = 1;
+                    //tcp_eth_connection_close( &g_iot_client );
+                    debug_printf( "tcp err , close ...\n" );
+                    g_iot_client.connection_close = 1;
                     break;
                 }
             }else{
                 tcp_err_cnt = 0;
-//                tcp_eth_connection_close( &g_client );
-                g_client.connection_close = 1;
+//                tcp_eth_connection_close( &g_iot_client );
+                g_iot_client.connection_close = 1;
                 break;
             }
         }
+        g_iot_client.connected = 0;
+                               g_iot_client.pcb = NULL;
         g_dev.eth_connected = 0;
         osDelay(1000);
     }
 }
-
-//static osThreadId_t ethTaskHandle;
+#endif
 
 const osThreadAttr_t ethTask_attributes = {
   .name = "ethTask",
@@ -316,3 +367,58 @@ void eth_task_start( void )
     // ethTaskHandle = osThreadNew(user_eth_task, NULL, &ethTask_attributes);
     osThreadNew(user_eth_task, NULL, &ethTask_attributes);
 }
+
+//ota
+
+int32_t eth_ota_requst( char *host, uint16_t port, char *req )
+{
+       int32_t ret = -1;
+    uint32_t tick = xTaskGetTickCount();
+debug_printf( "eth ota req: [%s]\n", req );
+
+    while( xTaskGetTickCount() - tick < 30*1000 ){
+
+        if( g_ota_client.connected  && ( strcmp( g_ota_client.host, host) || port != g_ota_client.port ) ){
+            tcp_eth_close( &g_ota_client );
+        }
+
+        if( strcmp( g_ota_client.host, host) ){
+            strcpy( g_ota_client.host, host );
+            g_ota_client.addr.addr = 0;
+        }
+
+        if( g_ota_client.port != port ){
+            g_ota_client.port = port;
+        }
+
+        if( g_ota_client.addr.addr == 0 ){
+
+            if( dns_gethostbyname( host, &g_ota_client.addr, DNS_found_cb, NULL)==ERR_OK || g_ota_client.addr.addr == 0 ){
+                osDelay( 2000 );
+                printf( "dns %s -- fail!\n", g_dev.cfg.net.tcp_server );
+                continue;
+            }
+            g_ota_client.connected = 0;
+        }
+
+        if( !g_ota_client.connected ){
+            //connect
+            if( tcp_eth_connect( &g_ota_client, &g_ota_client.addr, port ) != ERR_OK ){
+                osDelay(2000);
+                continue;
+            }
+        }
+
+        //send
+        err_t err;
+        err = tcp_write( g_ota_client.pcb, req, strlen(req), 1 );
+        if(err == ERR_OK ){
+            tcp_output( g_ota_client.pcb );
+            ret = 0;
+            break;
+        }else{
+            debug_printf( "--tcp write ret:%d-%s, pcb:%p, state:%d-%s\n", err, lwip_strerr(err),  g_ota_client.pcb, g_ota_client.pcb->state, tcp_debug_state_str( g_ota_client.pcb->state) );
+        }
+    }
+    return ret;        
+}
index 014c1937deee796d388592154003f4fd37876a62..7f5540669e68efb9afd748a23ab7ce58860a554c 100644 (file)
@@ -13,6 +13,8 @@ Range:bytes=%d-%d\r\n\
 Connection:keep-alive\r\n\r\n"
 
 extern int32_t wifi_ota_requst( char *host, uint16_t port, char *req );
+extern int32_t eth_ota_requst( char *host, uint16_t port, char *req );
+
 extern void wifi_ota_close( void );
 extern uint16_t user_sflash_crc( uint32_t addr, uint32_t sz );
 extern int32_t user_sflash_copy( uint32_t dst, uint32_t src, uint32_t sz );
@@ -66,7 +68,7 @@ typedef struct{
 
 static const osThreadAttr_t otaTask_attributes = {
   .name = "otaTask",
-  .stack_size = 2048,
+  .stack_size = 2048*2,
   .priority = (osPriority_t) osPriorityNormal,
 };
 
@@ -107,6 +109,24 @@ int32_t wifi_ota_start( char * url, char *ver )
 int32_t eth_ota_start( char * url, char *ver )
 {
        return ota_start( OTA_LINK_ETH, url, ver );
+       //return 0;
+}
+
+int32_t ota_handle_http_resp_eth( uint8_t *rx_data, uint32_t rx_sz )
+{
+       static uint8_t http_data[2048];
+       static uint32_t data_sz = 0;
+       int32_t ret = 0;
+
+       if( data_sz + rx_sz > sizeof(http_data ))
+               data_sz = 0;
+       memcpy( http_data+data_sz, rx_data, rx_sz );
+       data_sz += rx_sz;
+       ret = ota_handle_http_resp( http_data, data_sz );
+       if(ret != -2 ){
+               data_sz = 0;
+       }
+       return 0;
 }
 
 int32_t ota_handle_http_resp( uint8_t *rx_data, uint32_t rx_sz )
@@ -152,6 +172,8 @@ int32_t ota_handle_http_resp( uint8_t *rx_data, uint32_t rx_sz )
                                        g_ota.blk_info.data_len = content_sz;
                                        g_ota.resp = content_sz;
                                        ret = 0;
+                       }else if(content_sz < range_end-range_start+1 ){
+                                       ret = -2; //wait other data
                        }
                }
        }
@@ -189,12 +211,11 @@ int32_t http_get_blk( void )
                                  return ret;
                         }
     }else if( g_ota.link == OTA_LINK_ETH ){
-                         //ret = eth_ota_requst( g_ota.host, g_ota.port, http_req );
-                         //if(ret < 0){
+                         ret = eth_ota_requst( g_ota.host, g_ota.port, http_req );
+                         if(ret < 0){
                                  //tcp connect fail, ota fail
-                                 //return ret;
-                         //}
-                         return -1;
+                                 return ret;
+                         }
     }else{
                          return -1;
                }
@@ -343,7 +364,7 @@ static void ota_task( void * unused )
                                                                                  if( g_ota.fw_received == g_ota.fw_size ){//下载完成
 
                                                                                                uint16_t crc = user_sflash_crc( OTA_PART_ADDR+blk_sz, g_ota.fw_size-blk_sz ); //skip fw_head_info
-                                                                                               debug_printf( "ota:success, flash_crc:%04X, fw_crc:%04X\n", crc, g_ota.fw_head.fw_crc );
+                                                                                               debug_printf( "--- ota:success, flash_crc:%04X, fw_crc:%04X\n", crc, g_ota.fw_head.fw_crc );
                                                                                                //TODO copy data from ota part to app part
                                                                                                if( crc == g_ota.fw_head.fw_crc ){
                                                                                                        uint32_t fwinfo[4];
index 89dded2fb639629ddee636cbedf6a93dfc52cddf..35d3f6c7159518055221970ddf1bef2dce334fe1 100644 (file)
@@ -1,4 +1,5 @@
 #include "user.h"
+#include "cJSON.h"
 
 static char mac_str_buf[20]; //"00:00:00:00:00:00";
 static char *g_mac_str;
@@ -78,3 +79,85 @@ char * devinfo_report_str( void )
 
     return tx_buf;
 }
+
+
+//return value
+//1:action lock, 2:action unlock, 3:action ota
+//0:
+#define IOT_ACTION_LOCK 1
+#define IOT_ACTION_UNLOCK 2
+#define IOT_ACTION_OTA 3
+
+extern int32_t wifi_tcp_tx( uint8_t conn_id, uint8_t *data, uint32_t sz );
+extern int32_t wifi_ota_start( char * url, char *ver );
+extern int32_t eth_ota_start( char * url, char *ver );
+
+char * lock_resp_str( void )
+{
+    static char tx_buf[100];
+
+    sprintf( (char*)tx_buf, 
+                       "{ \"cmd\":\"op\", \"mac\":\"%s\", \"lock\":\"%d\" }",
+                               g_mac_str, 
+        g_dev.cfg.remote_locked==1?0:1 );
+    return tx_buf;
+}
+
+int32_t iot_cmd_parse( uint32_t link, char *cmd )
+{
+                       int32_t act = 0;
+                       cJSON*root = cJSON_Parse( (char*)cmd );
+                       if( root ){
+                               cJSON *action = cJSON_GetObjectItem( root, "action" );
+                               if(action && cJSON_IsString( action ) ){
+                                       //lock/unlock 设备响应数据暂定如下: op代表操作数据 lock代表当前机器锁机状态 0锁机 1正常
+                                       //{cmd:"op", mac:"", lock:"0"
+                                       if( strcmp( action->valuestring, "lockDevice" )== 0 ){
+                                               debug_printf( "lockdevice %d->1\n", g_dev.cfg.remote_locked );
+                                               g_dev.cfg.remote_locked = 1;
+                                               dev_cfg_store( &g_dev.cfg );
+                                               act = 1;
+                                       }else if( strcmp( action->valuestring, "unlockDevice" )== 0 ){
+                                               debug_printf( "unlockdevice %d->0\n", g_dev.cfg.remote_locked );
+                                               g_dev.cfg.remote_locked = 0;
+                                               dev_cfg_store( &g_dev.cfg );
+                                               act = 2;
+                                       }else if( strcmp( action->valuestring, "update" )== 0 ){
+                                               //ota;
+                                               cJSON * data;
+                                               cJSON * ver;
+                                               cJSON * url;
+                                               data = cJSON_GetObjectItem( root, "data" );
+                                               debug_printf( "-----ota----- " );
+                                               if(data){
+                                                       ver = cJSON_GetObjectItem( data, "version" );
+                                                       url = cJSON_GetObjectItem( data, "downloadUrl" );
+                                                       if( ver ) debug_printf( "--version:[%s]", ver->valuestring );
+                                                       if( url ) debug_printf( "--url:[%s]", url->valuestring );
+                                                       if(  ver &&url ){
+                                                                       if(link==0){
+                                                                               wifi_ota_start( url->valuestring, ver->valuestring );
+                                                                       }else if(link==1){
+                                                                               eth_ota_start( url->valuestring, ver->valuestring );
+                                                                       }
+                                                               act = 3;
+                                                       }
+                                               }
+                                               debug_printf( "\n" );
+                                       }
+                               }
+                               cJSON_Delete( root );
+                       }else{
+#if 0
+                               if( link == 1 ){
+                                       //debug
+                                       debug_printf( "test eth ota ...\n");
+                                       if( APP_VER_MINOR == 7 )
+                                               eth_ota_start( "http://kyz.pub/download/test_v08.bin", "v1.1");
+                                       else if( APP_VER_MINOR == 8 )
+                                               eth_ota_start( "http://kyz.pub/download/test_v07.bin", "v1.1");
+                               }
+#endif
+                       }
+                       return act;
+}
index 55688ae5fe5c6aaaef9fc3dad6134e69c47d6415..bbb6509afee55809349118a50d8eddf914e778c3 100644 (file)
@@ -40,7 +40,7 @@ static uint16_t tcp_rx_n;
 __attribute__((section(".DTCM"),zero_init)) static uint8_t ota_rx_buf[2048];
 static uint16_t ota_rx_n;
 
-static int32_t wifi_tcp_tx( uint8_t link, uint8_t *data, uint32_t sz );
+int32_t wifi_tcp_tx( uint8_t link, uint8_t *data, uint32_t sz );
 
 void wifi_uart_rx_start( void )
 {
@@ -243,6 +243,7 @@ int32_t wifi_devinfo_report( void )
     return 0;
 }
 
+static int32_t iot_act_resp = 0;
 static void tcp_rx_data_handle(void *arg, char *rspinfo, int rsplen)
 {
     unsigned char unusesymbol = 0;
@@ -295,7 +296,18 @@ static void tcp_rx_data_handle(void *arg, char *rspinfo, int rsplen)
                        at_read( (char*)(tcp_rx_buf+tcp_rx_n), len );
                        tcp_rx_n += len;
                        tcp_rx_buf[tcp_rx_n] = 0;
-                       
+#if 1
+                       { extern int32_t iot_cmd_parse( uint32_t link, char *cmd );
+                       int32_t act = iot_cmd_parse( 0, (char *)tcp_rx_buf );
+                                                               
+                               if( act == 1 || act == 2 ){
+                                               //char * s = lock_resp_str();
+                                               //wifi_tcp_tx(0, (uint8_t*)s, strlen(s) );
+                                               iot_act_resp = 1;
+                               }
+                       }
+                       tcp_rx_n = 0;
+#else
                        cJSON*root = cJSON_Parse( (char*)tcp_rx_buf );
                        if( root ){
                                cJSON *action = cJSON_GetObjectItem( root, "action" );
@@ -332,6 +344,7 @@ static void tcp_rx_data_handle(void *arg, char *rspinfo, int rsplen)
                                tcp_rx_n = 0;
                                cJSON_Delete( root );
                        }
+#endif
                }else if( link_id == '0'+CONN_ID_OTA ){
                        if( len <= sizeof(ota_rx_buf ) ){
                                 int n =at_read( (char*)(ota_rx_buf), len );
@@ -429,6 +442,12 @@ void user_wifi_task( void* unused )
         while( g_dev.wifi_connected == 2 ){
             vTaskDelayUntil(&last_ticks, 1000 );
 
+                                               if( iot_act_resp == 1 ){
+                                                       char * lock_resp_str( void );
+                                                       char * s = lock_resp_str();
+                                                       wifi_tcp_tx( CONN_ID_IOT, (uint8_t*)s, strlen(s) );
+                                                       iot_act_resp = 0;
+                                               }
             if( g_dev.eth_connected == 2 ){
                 //如果有线已经连接,wifi不上传数据
                 continue;