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
.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 );
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;
}
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;
}
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;
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);
}
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);
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;
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;
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;
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);
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;
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",
// ethTaskHandle = osThreadNew(user_eth_task, NULL, ðTask_attributes);
osThreadNew(user_eth_task, NULL, ðTask_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;
+}