/****************************************************************************** * FunctionName : espconn_close * Description : The connection has been successfully closed. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR espconn_tcp_disconnect(espconn_msg *pdiscon) { if (pdiscon != NULL){ if (pdiscon->preverse != NULL) espconn_server_close(pdiscon, pdiscon->pcommon.pcb); else espconn_client_close(pdiscon, pdiscon->pcommon.pcb); } else{ espconn_printf("espconn_server_disconnect err.\n"); } }
/****************************************************************************** * FunctionName : espconn_close * Description : The connection has been successfully closed. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR espconn_tcp_disconnect(espconn_msg *pdiscon,uint8 type) { if (pdiscon != NULL){ /*disconnect with the host by send the FIN frame*/ if (pdiscon->preverse != NULL) espconn_server_close(pdiscon, pdiscon->pcommon.pcb,type); else espconn_client_close(pdiscon, pdiscon->pcommon.pcb,type); } else{ espconn_printf("espconn_tcp_disconnect err.\n"); } }
/****************************************************************************** * FunctionName : espconn_client_recv * Description : Data has been received on this pcb. * Parameters : arg -- Additional argument to pass to the callback function * pcb -- The connection pcb which received data * p -- The received data (or NULL when the connection has been closed!) * err -- An error code if there has been an error receiving * Returns : ERR_ABRT: if you have called tcp_abort from within the function! *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { espconn_msg *precv_cb = arg; tcp_arg(pcb, arg); if (p != NULL) { /*To update and advertise a larger window*/ if(precv_cb->recv_hold_flag == 0) tcp_recved(pcb, p->tot_len); else precv_cb->recv_holded_buf_Len += p->tot_len; } if (err == ERR_OK && p != NULL) { char *pdata = NULL; u16_t length = 0; /*Copy the contents of a packet buffer to an application buffer. *to prevent memory leaks, ensure that each allocated is deleted*/ pdata = (char *)malloc(p ->tot_len + 1); length = pbuf_copy_partial(p, pdata, p ->tot_len, 0); pbuf_free(p); if (length != 0) { /*switch the state of espconn for application process*/ precv_cb->pespconn ->state = ESPCONN_READ; precv_cb->pcommon.pcb = pcb; if (precv_cb->pespconn->recv_callback != NULL) { precv_cb->pespconn->recv_callback(precv_cb->pespconn, pdata, length); } /*switch the state of espconn for next packet copy*/ if (pcb->state == ESTABLISHED) precv_cb->pespconn ->state = ESPCONN_CONNECT; } /*to prevent memory leaks, ensure that each allocated is deleted*/ free(pdata); pdata = NULL; } if (err == ERR_OK && p == NULL) { espconn_client_close(precv_cb, pcb,0); } return ERR_OK; }
/****************************************************************************** * FunctionName : espconn_client_poll * Description : The poll function is called every 2nd second. * If there has been no data sent (which resets the retries) in 8 seconds, close. * If the last portion of a file has not been sent in 2 seconds, close. * * This could be increased, but we don't want to waste resources for bad connections. * Parameters : arg -- Additional argument to pass to the callback function * pcb -- The connection pcb for which data has been acknowledged * Returns : ERR_OK: try to send some data by calling tcp_output * ERR_ABRT: if you have called tcp_abort from within the function! *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_client_poll(void *arg, struct tcp_pcb *pcb) { espconn_msg *ppoll_cb = arg; espconn_printf("espconn_client_poll pcb %p %p %d\n", pcb, arg, pcb->state); if (arg == NULL) { tcp_abandon(pcb, 0); tcp_poll(pcb, NULL, 0); return ERR_OK; } ppoll_cb->pcommon.pcb = pcb; if (pcb->state == ESTABLISHED) { } else { tcp_poll(pcb, espconn_client_poll, 0); espconn_client_close(ppoll_cb->pespconn, pcb); } return ERR_OK; }
/****************************************************************************** * FunctionName : espconn_client_recv * Description : Data has been received on this pcb. * Parameters : arg -- Additional argument to pass to the callback function * pcb -- The connection pcb which received data * p -- The received data (or NULL when the connection has been closed!) * err -- An error code if there has been an error receiving * Returns : ERR_ABRT: if you have called tcp_abort from within the function! *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_client_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { espconn_msg *precv_cb = arg; tcp_arg(pcb, arg); if (p != NULL) { tcp_recved(pcb, p ->tot_len); } if (err == ERR_OK && p != NULL) { char *pdata = NULL; u16_t length = 0; pdata = (char *)os_zalloc(p ->tot_len + 1); length = pbuf_copy_partial(p, pdata, p ->tot_len, 0); pbuf_free(p); if (length != 0) { precv_cb->pespconn ->state = ESPCONN_READ; precv_cb->pcommon.pcb = pcb; if (precv_cb->pespconn->recv_callback != NULL) { precv_cb->pespconn->recv_callback(precv_cb->pespconn, pdata, length); } precv_cb->pespconn ->state = ESPCONN_CONNECT; } os_free(pdata); pdata = NULL; } if (err == ERR_OK && p == NULL) { espconn_client_close(precv_cb, pcb); } return ERR_OK; }