/****************************************************************************** * FunctionName : espconn_tcp_client * Description : Initialize the client: set up a connect PCB and bind it to * the defined port * Parameters : espconn -- the espconn used to build client * Returns : none *******************************************************************************/ sint8 ICACHE_FLASH_ATTR espconn_tcp_client(struct espconn *espconn) { struct tcp_pcb *pcb = NULL; struct ip_addr ipaddr; espconn_msg *pclient = NULL; /*Creates a new client control message*/ pclient = (espconn_msg *)os_zalloc(sizeof(espconn_msg)); if (pclient == NULL){ return ESPCONN_MEM; } /*Set an IP address given for Little-endian.*/ IP4_ADDR(&ipaddr, espconn->proto.tcp->remote_ip[0], espconn->proto.tcp->remote_ip[1], espconn->proto.tcp->remote_ip[2], espconn->proto.tcp->remote_ip[3]); /*Creates a new TCP protocol control block*/ pcb = tcp_new(); if (pcb == NULL) { /*to prevent memory leaks, ensure that each allocated is deleted*/ os_free(pclient); pclient = NULL; return ESPCONN_MEM; } else { /*insert the node to the active connection list*/ espconn_list_creat(&plink_active, pclient); tcp_arg(pcb, (void *)pclient); tcp_err(pcb, espconn_client_err); pclient->preverse = NULL; pclient->pespconn = espconn; pclient->pespconn->state = ESPCONN_WAIT; pclient->pcommon.pcb = pcb; tcp_bind(pcb, IP_ADDR_ANY, pclient->pespconn->proto.tcp->local_port); /*Establish the connection*/ pclient->pcommon.err = tcp_connect(pcb, &ipaddr, pclient->pespconn->proto.tcp->remote_port, espconn_client_connect); if (pclient->pcommon.err == ERR_RTE){ /*remove the node from the client's active connection list*/ espconn_list_delete(&plink_active, pclient); espconn_kill_pcb(pcb->local_port); os_free(pclient); pclient = NULL; return ESPCONN_RTE; } return pclient->pcommon.err; } }
/****************************************************************************** * FunctionName : espconn_tcp_delete * Description : delete the server: delete a listening PCB and free it * Parameters : pdeletecon -- the espconn used to delete a server * Returns : none *******************************************************************************/ sint8 ICACHE_FLASH_ATTR espconn_tcp_delete(struct espconn *pdeletecon) { err_t err=ESPCONN_ARG; remot_info *pinfo = NULL; espconn_msg *pdelete_msg = NULL; struct tcp_pcb *pcb = NULL; if (pdeletecon == NULL) return ESPCONN_ARG; espconn_get_connection_info(pdeletecon, &pinfo , 0); /*make sure all the active connection have been disconnect*/ if (pdeletecon->link_cnt != 0) return ESPCONN_INPROGRESS; else { espconn_printf("espconn_tcp_delete %p\n",pdeletecon); pdelete_msg = pserver_list; while (pdelete_msg != NULL){ if (pdelete_msg->pespconn == pdeletecon){ /*remove the node from the client's active connection list*/ espconn_list_delete(&pserver_list, pdelete_msg); pcb = pdelete_msg->preverse; printf("espconn_tcp_delete %d, %d\n",pcb->state, pcb->local_port); espconn_kill_pcb(pcb->local_port); err = tcp_close(pcb); free(pdelete_msg); pdelete_msg = NULL; break; } pdelete_msg = pdelete_msg->pnext; } if (err == ERR_OK) return err; else return ESPCONN_ARG; } }
sint8 ICACHE_FLASH_ATTR espconn_tcp_client(struct espconn *espconn) { struct tcp_pcb *pcb = NULL; ip_addr_t ipaddr; espconn_msg *pclient = NULL; /*Creates a new client control message*/ pclient = (espconn_msg *)malloc(sizeof(espconn_msg)); memset(pclient,0,sizeof(espconn_msg)); if (pclient == NULL){ return ESPCONN_MEM; } /*Set an IP address given for Little-endian.*/ ipaddr.type = IPADDR_TYPE_V4; IP4_ADDR(&ipaddr.u_addr.ip4, espconn->proto.tcp->remote_ip[0], espconn->proto.tcp->remote_ip[1], espconn->proto.tcp->remote_ip[2], espconn->proto.tcp->remote_ip[3]); // printf("espconn_tcp_client ipaddr %d:%d:%d:%d\n" // ,(uint8_t)(ipaddr.u_addr.ip4.addr) // ,(uint8_t)(ipaddr.u_addr.ip4.addr>>8) // ,(uint8_t)(ipaddr.u_addr.ip4.addr>>16) // ,(uint8_t)(ipaddr.u_addr.ip4.addr>>24)); /*Creates a new TCP protocol control block*/ pcb = tcp_new(); if (pcb == NULL) { /*to prevent memory leaks, ensure that each allocated is deleted*/ free(pclient); pclient = NULL; return ESPCONN_MEM; } else { /*insert the node to the active connection list*/ espconn_list_creat(&plink_active, pclient); //printf("espconn_msg 3: %p\n", pclient); tcp_arg(pcb, (void *)pclient); tcp_err(pcb, espconn_client_err); pclient->preverse = NULL; pclient->pespconn = espconn; pclient->pespconn->state = ESPCONN_WAIT; pclient->pcommon.pcb = pcb; tcp_bind(pcb, IP_ADDR_ANY, pclient->pespconn->proto.tcp->local_port); #if 0 pclient->pcommon.err = tcp_bind(pcb, IP_ADDR_ANY, pclient->pespconn->proto.tcp->local_port); if (pclient->pcommon.err != ERR_OK){ /*remove the node from the client's active connection list*/ espconn_list_delete(&plink_active, pclient); memp_free(MEMP_TCP_PCB, pcb); free(pclient); pclient = NULL; return ERR_USE; } #endif /*Establish the connection*/ pclient->pcommon.err = tcp_connect(pcb, &ipaddr, pclient->pespconn->proto.tcp->remote_port, espconn_client_connect); if (pclient->pcommon.err == ERR_RTE){ /*remove the node from the client's active connection list*/ printf("fail to connect %s\n",__FILE__); espconn_list_delete(&plink_active, pclient); espconn_kill_pcb(pcb->local_port); free(pclient); pclient = NULL; return ESPCONN_RTE; } return pclient->pcommon.err; } }