...... void InterNet_TCP_SendData(uint8 *buf,uint16 len); void User_TCP_Client_Init(char * ipAddress, int32_t port); LOCAL os_timer_t test_timer; ip_addr_t tcp_server_ip; typedef enum{ teClient, teServer }teType; esp_tcp user_tcp; struct ip_info client_ipinfo; typedef struct { BOOL linkEn; //是否自动重联 BOOL teToff; uint8 linkId; teType teType; uint8 repeaTime; struct espconn uConn; } espConnectionType; espConnectionType user_contype; static ip_addr_t host_ip; char data[1024] = { "test ok!n" }; /** * @brief 接收回调函数 * @param arg: contain the ip link information * @param pdata: received data * @param len: the lenght of received data * @retval None */ static void ICACHE_FLASH_ATTR InterNet_Receive(void *arg, char *pdata, unsigned short len) { os_printf("rnReceive Data:%srn",pdata); os_printf("rn"); InterNet_TCP_SendData((uint8 *)pdata,(uint16)len); //收到什么回发什么 return; } ...... /** * @brief 断开连接成功后的回调函数 * @param arg: contain the ip link information * @retval None */ static void ICACHE_FLASH_ATTR InterNet_Disconnect_Cb(void *arg) { /* struct espconn *pespconn = (struct espconn *)arg; espConnectionType *linkTemp = (espConnectionType *)pespconn->reserve; if(pespconn == NULL) { return; } if(pespconn->proto.tcp != NULL) { os_free(pespconn->proto.tcp); } os_free(pespconn); linkTemp->linkEn = FALSE;*/ os_printf("rnDisConnect! try to reconnect......rn"); //try to reconnect to server espconn_connect(&user_contype.uConn); } /** * @brief 连接成功后的回调函数 * @param arg: contain the ip link information * @retval None */ static void ICACHE_FLASH_ATTR InterNet_Connect_Cb(void *arg) { ...... espconn_regist_recvcb(pespconn, InterNet_Receive); espconn_regist_sentcb(pespconn, InterNet_Send_Cb); espconn_regist_disconcb(pespconn, InterNet_Disconnect_Cb); espconn_sent(pespconn, (uint8 *) data, (uint16)strlen(data)); } /** * @brief 重联回调函数 * @param arg: contain the ip link information * @retval None */ static void ICACHE_FLASH_ATTR InterNet_Reconnect_Cb(void *arg, sint8 errType) { ...... } /** * @brief dns查询回调函数 * @param name -- pointer to the name that was looked up. * @param ipaddr -- pointer to an ip_addr_t containing the IP address of * the hostname, or NULL if the name could not be found (or on any * other error). * @param callback_arg -- a user-specified callback argument passed to * dns_gethostbyname * @retval None */ LOCAL void ICACHE_FLASH_ATTR InterNet_Dns_Cb(const char *name, ip_addr_t *ipaddr, void *arg) { ...... } user_dns_check_cb(void *arg) { ...... } /** Station模式下 * @brief 配置tcp 客户端 * @param ipAddress -- 远程主机IP 地址 * @param port -- 远程主机端口号 */ void ICACHE_FLASH_ATTR User_TCP_Client_Init(char * ipAddress, sint32 port) { char ipTemp[128]; uint32 ip = 0; sint8 result = 0; user_contype.uConn.state = ESPCONN_NONE; user_contype.linkId = 0; ip = ipaddr_addr(ipAddress); os_printf("tcp client init!(server ip:%s,server port:%d)n",ipAddress,port); user_contype.uConn.type = ESPCONN_TCP; user_contype.uConn.proto.tcp = malloc(sizeof(esp_tcp)); user_contype.uConn.proto.tcp->local_port = espconn_port(); wifi_get_ip_info(0,&client_ipinfo); memcpy(&user_contype.uConn.proto.tcp->local_ip,&client_ipinfo.ip,4); user_contype.uConn.proto.tcp->remote_port = port; if("" != NET_DOMAIN)//DNS { espconn_gethostbyname(&user_contype.uConn, NET_DOMAIN, &tcp_server_ip, InterNet_Dns_Cb); // DNS function os_timer_disARM(&test_timer); os_timer_setfn(&test_timer, (os_timer_func_t *)user_dns_check_cb, &user_contype.uConn); os_timer_arm(&test_timer, 1000, 0); }else{ //IP memcpy(&user_contype.uConn.proto.tcp->remote_ip,&ip,4); espconn_regist_connectcb(&user_contype.uConn, InterNet_Connect_Cb); espconn_regist_reconcb(&user_contype.uConn, InterNet_Reconnect_Cb); result = espconn_connect(&user_contype.uConn); os_printf("espconn remote IP:%d.%d.%d.%dn",user_contype.uConn.proto.tcp->remote_ip[0],user_contype.uConn.proto.tcp->remote_ip[1],user_contype.uConn.proto.tcp->remote_ip[2],user_contype.uConn.proto.tcp->remote_ip[3]); os_printf("espconn remote port:%dn",user_contype.uConn.proto.tcp->remote_port); os_printf("espconn local IP:%d.%d.%d.%dn",user_contype.uConn.proto.tcp->local_ip[0],user_contype.uConn.proto.tcp->local_ip[1],user_contype.uConn.proto.tcp->local_ip[2],user_contype.uConn.proto.tcp->local_ip[3]); // os_printf("espconn local IP:%dn",user_contype.uConn.proto.tcp->local_ip); os_printf("espconn local port:%dn",user_contype.uConn.proto.tcp->local_port); if(result == 0){ os_printf("espconn connect success!n"); }else { os_printf("espconn connect failed!(code:%d)n",result); } } } void ICACHE_FLASH_ATTR InterNet_TCP_SendData(uint8 *buf,uint16 len) { espconn_sent(&user_contype.uConn,buf,len); } |