/****************************************************************************** * FunctionName : espconn_udp_sent * Description : sent data for client or server * Parameters : void *arg -- client or server to send * uint8* psent -- Data to send * uint16 length -- Length of data to send * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR espconn_udp_sent(void *arg, uint8 *psent, uint16 length) { espconn_msg *pudp_sent = arg; struct udp_pcb *upcb = pudp_sent->pcommon.pcb; struct pbuf *p, *q; u8_t *data = NULL; u16_t cnt = 0; u16_t datalen = 0; u16_t i = 0; err_t err; LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d %p\n", __LINE__, length, upcb)); if (pudp_sent == NULL || upcb == NULL || psent == NULL || length == 0) { return; } if (1024 < length) { datalen = 1024; } else { datalen = length; } p = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p)); if (p != NULL) { q = p; while (q != NULL) { data = (u8_t *)q->payload; LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, data)); for (i = 0; i < q->len; i++) { data[i] = ((u8_t *) psent)[cnt++]; } q = q->next; } } else { return; } upcb->remote_port = pudp_sent->pespconn->proto.udp->remote_port; IP4_ADDR(&upcb->remote_ip, pudp_sent->pespconn->proto.udp->remote_ip[0], pudp_sent->pespconn->proto.udp->remote_ip[1], pudp_sent->pespconn->proto.udp->remote_ip[2], pudp_sent->pespconn->proto.udp->remote_ip[3]); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %x %d\n", __LINE__, upcb->remote_ip, upcb->remote_port)); err = udp_send(upcb, p); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d\n", __LINE__, err)); if (p->ref != 0) { LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p)); pbuf_free(p); pudp_sent->pcommon.ptrbuf = psent + datalen; pudp_sent->pcommon.cntr = length - datalen; espconn_data_sent(pudp_sent); } }
/****************************************************************************** * FunctionName : espconn_udp_sent * Description : sent data for client or server * Parameters : void *arg -- client or server to send * uint8* psent -- Data to send * uint16 length -- Length of data to send * Returns : return espconn error code. * - ESPCONN_OK. Successful. No error occured. * - ESPCONN_MEM. Out of memory. * - ESPCONN_RTE. Could not find route to destination address. * - More errors could be returned by lower protocol layers. *******************************************************************************/ err_t ICACHE_FLASH_ATTR espconn_udp_sent(void *arg, uint8 *psent, uint16 length) { espconn_msg *pudp_sent = arg; struct udp_pcb *upcb = pudp_sent->pcommon.pcb; struct pbuf *p, *q ,*p_temp; u8_t *data = NULL; u16_t cnt = 0; u16_t datalen = 0; u16_t i = 0; err_t err; LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d %p\n", __LINE__, length, upcb)); if (pudp_sent == NULL || upcb == NULL || psent == NULL || length == 0) { return ESPCONN_ARG; } if ((IP_FRAG_MAX_MTU - 20 - 8) < length) { datalen = IP_FRAG_MAX_MTU - 20 - 8; } else { datalen = length; } p = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p)); if (p != NULL) { q = p; while (q != NULL) { data = (u8_t *)q->payload; LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, data)); for (i = 0; i < q->len; i++) { data[i] = ((u8_t *) psent)[cnt++]; } q = q->next; } } else { return ESPCONN_MEM; } upcb->remote_port = pudp_sent->pespconn->proto.udp->remote_port; IP4_ADDR(&upcb->remote_ip, pudp_sent->pespconn->proto.udp->remote_ip[0], pudp_sent->pespconn->proto.udp->remote_ip[1], pudp_sent->pespconn->proto.udp->remote_ip[2], pudp_sent->pespconn->proto.udp->remote_ip[3]); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %x %d\n", __LINE__, upcb->remote_ip, upcb->remote_port)); struct netif *sta_netif = (struct netif *)eagle_lwip_getif(0x00); struct netif *ap_netif = (struct netif *)eagle_lwip_getif(0x01); if(wifi_get_opmode() == ESPCONN_AP_STA && default_interface == ESPCONN_AP_STA && sta_netif != NULL && ap_netif != NULL) { if(netif_is_up(sta_netif) && netif_is_up(ap_netif) && \ ip_addr_isbroadcast(&upcb->remote_ip, sta_netif) && \ ip_addr_isbroadcast(&upcb->remote_ip, ap_netif)) { p_temp = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM); if (pbuf_copy (p_temp,p) != ERR_OK) { LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent: copying to new pbuf failed\n")); return ESPCONN_ARG; } netif_set_default(sta_netif); err = udp_send(upcb, p_temp); pbuf_free(p_temp); netif_set_default(ap_netif); } } err = udp_send(upcb, p); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d\n", __LINE__, err)); if (p->ref != 0) { LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p)); pbuf_free(p); pudp_sent->pcommon.ptrbuf = psent + datalen; pudp_sent->pcommon.cntr = length - datalen; pudp_sent->pcommon.err = err; espconn_data_sent(pudp_sent, ESPCONN_SEND); if (err > 0) return ESPCONN_IF; return err; } else { pbuf_free(p); return ESPCONN_RTE; } }
/****************************************************************************** * FunctionName : espconn_udp_sendto * Description : sent data for UDP * Parameters : void *arg -- UDP to send * uint8* psent -- Data to send * uint16 length -- Length of data to send * Returns : return espconn error code. * - ESPCONN_OK. Successful. No error occured. * - ESPCONN_MEM. Out of memory. * - ESPCONN_RTE. Could not find route to destination address. * - More errors could be returned by lower protocol layers. *******************************************************************************/ err_t ICACHE_FLASH_ATTR espconn_udp_sendto(void* arg, uint8* psent, uint16 length) { espconn_msg* pudp_sent = arg; struct udp_pcb* upcb = pudp_sent->pcommon.pcb; struct espconn* pespconn = pudp_sent->pespconn; struct pbuf* p, *q , *p_temp; struct ip_addr dst_ip; u16_t dst_port; u8_t* data = NULL; u16_t cnt = 0; u16_t datalen = 0; u16_t i = 0; err_t err; LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %d %p\n", __LINE__, length, upcb)); if (pudp_sent == NULL || upcb == NULL || psent == NULL || length == 0) { return ESPCONN_ARG; } if ((IP_MAX_MTU - 20 - 8) < length) { datalen = IP_MAX_MTU - 20 - 8; } else { datalen = length; } p = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, p)); if (p != NULL) { q = p; while (q != NULL) { data = (u8_t*)q->payload; LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %p\n", __LINE__, data)); for (i = 0; i < q->len; i++) { data[i] = ((u8_t*) psent)[cnt++]; } q = q->next; } } else { return ESPCONN_MEM; } dst_port = pespconn->proto.udp->remote_port; IP4_ADDR(&dst_ip, pespconn->proto.udp->remote_ip[0], pespconn->proto.udp->remote_ip[1], pespconn->proto.udp->remote_ip[2], pespconn->proto.udp->remote_ip[3]); LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sent %d %x %d\n", __LINE__, upcb->remote_ip, upcb->remote_port)); #if LWIP_IPV6 || LWIP_IGMP ipX_addr_t* dst_ip_route = ip_2_ipX(&dst_ip); if (ipX_addr_ismulticast(PCB_ISIPV6(upcb), dst_ip_route)) { /* For multicast, find a netif based on source address. */ #if LWIP_IPV6 if (PCB_ISIPV6(upcb)) { dst_ip_route = &upcb->local_ip; } else #endif /* LWIP_IPV6 */ { #if LWIP_IGMP upcb->multicast_ip.addr = dst_ip.addr; #endif /* LWIP_IGMP */ } } #endif /* LWIP_IPV6 || LWIP_IGMP */ struct netif* sta_netif = (struct netif*)eagle_lwip_getif(0x00); struct netif* ap_netif = (struct netif*)eagle_lwip_getif(0x01); if (wifi_get_opmode() == ESPCONN_AP_STA && default_interface == ESPCONN_AP_STA && sta_netif != NULL && ap_netif != NULL) { if (netif_is_up(sta_netif) && netif_is_up(ap_netif) && \ ip_addr_isbroadcast(&(upcb->remote_ip.ip4), sta_netif) && \ ip_addr_isbroadcast(&(upcb->remote_ip.ip4), ap_netif)) { p_temp = pbuf_alloc(PBUF_TRANSPORT, datalen, PBUF_RAM); if (pbuf_copy(p_temp, p) != ERR_OK) { LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("espconn_udp_sendto: copying to new pbuf failed\n")); return ESPCONN_ARG; } netif_set_default(sta_netif); err = udp_sendto(upcb, p_temp, &dst_ip, dst_port); pbuf_free(p_temp); netif_set_default(ap_netif); } } err = udp_sendto(upcb, p, &dst_ip, dst_port); if (p->ref != 0) { pbuf_free(p); pudp_sent->pcommon.ptrbuf = psent + datalen; pudp_sent->pcommon.cntr = length - datalen; pudp_sent->pcommon.err = err; espconn_data_sent(pudp_sent, ESPCONN_SENDTO); if (err > 0) { return ESPCONN_IF; } return err; } else { pbuf_free(p); return ESPCONN_RTE; } }