wiced_result_t process_received_udp_packet() { wiced_packet_t* packet; char* rx_data; static uint16_t rx_data_length; uint16_t available_data_length; static wiced_ip_address_t udp_src_ip_addr; static uint16_t udp_src_port; wiced_result_t result = wiced_udp_receive( &udp_socket, &packet, RX_WAIT_TIMEOUT ); if ( ( result == WICED_ERROR ) || ( result == WICED_TIMEOUT ) ) { return result; } wiced_udp_packet_get_info( packet, &udp_src_ip_addr, &udp_src_port ); wiced_packet_get_data( packet, 0, (uint8_t**) &rx_data, &rx_data_length, &available_data_length ); uint32_t data_len = MIN(rx_data_length, BUFFER_SIZE-(48)); write_ws2812(0, data_len, rx_data); // WPRINT_APP_INFO ( ("UDP Rx: \"%.2x\" from IP %u.%u.%u.%u:%d\n", *rx_data, // (unsigned char) ( ( GET_IPV4_ADDRESS(udp_src_ip_addr) >> 24 ) & 0xff ), // (unsigned char) ( ( GET_IPV4_ADDRESS(udp_src_ip_addr) >> 16 ) & 0xff ), // (unsigned char) ( ( GET_IPV4_ADDRESS(udp_src_ip_addr) >> 8 ) & 0xff ), // (unsigned char) ( ( GET_IPV4_ADDRESS(udp_src_ip_addr) >> 0 ) & 0xff ), // udp_src_port ) ); wiced_packet_delete( packet ); return WICED_SUCCESS; }
sock_result_t socket_receivefrom(sock_handle_t sd, void* buffer, socklen_t bufLen, uint32_t flags, sockaddr_t* addr, socklen_t* addrsize) { socket_t* socket = from_handle(sd); volatile wiced_result_t result = WICED_INVALID_SOCKET; uint16_t read_len = 0; if (is_open(socket) && is_udp(socket)) { std::lock_guard<socket_t> lk(*socket); wiced_packet_t* packet = NULL; // UDP receive timeout changed to 0 sec so as not to block if ((result=wiced_udp_receive(udp(socket), &packet, WICED_NO_WAIT))==WICED_SUCCESS) { wiced_ip_address_t wiced_ip_addr; uint16_t port; if ((result=wiced_udp_packet_get_info(packet, &wiced_ip_addr, &port))==WICED_SUCCESS) { uint32_t ipv4 = GET_IPV4_ADDRESS(wiced_ip_addr); addr->sa_data[0] = (port>>8) & 0xFF; addr->sa_data[1] = port & 0xFF; addr->sa_data[2] = (ipv4 >> 24) & 0xFF; addr->sa_data[3] = (ipv4 >> 16) & 0xFF; addr->sa_data[4] = (ipv4 >> 8) & 0xFF; addr->sa_data[5] = ipv4 & 0xFF; result=read_packet(packet, (uint8_t*)buffer, bufLen, &read_len); }
/* * Function: wilddog_receive * Description: wilddog receive function, it use the interface in wiced platform. * Input: socketId: The socket id. * addr: The pointer of Wilddog_Address_T * buf: The pointer of the send buffer * bufLen: The length of the send buffer. * timeout: The max timeout in recv process. * Output: N/A * Return: If success, return the number of bytes received; else return -1. */ int wilddog_receive ( int socketId, Wilddog_Address_T* addr_in, void* buf, s32 bufLen, s32 timeout ) { wiced_udp_socket_t* socket = (wiced_udp_socket_t*) socketId; wiced_packet_t* receive = NULL; u16 totalLeft = 0; u16 total = 0; uint16_t pos = 0; uint8_t* rxData; uint16_t fragLeft = 0; wiced_ip_address_t recieve_ip_addr; uint16_t receive_port; wiced_result_t result; result = wiced_udp_receive( socket, &receive, timeout ); if ( result == WICED_SUCCESS ) { if(NULL == receive) { wilddog_debug_level(WD_DEBUG_ERROR, "receive is NULL!"); return -1; } wiced_udp_packet_get_info( receive, &recieve_ip_addr, &receive_port ); if ( !recieve_ip_addr.ip.v4 == MAKE_IPV4_ADDRESS(addr_in->ip[0], \ addr_in->ip[1], addr_in->ip[2], addr_in->ip[3]) ) { wilddog_debug_level(WD_DEBUG_ERROR, "addr error!\n" ); wiced_packet_delete( receive ); return -1; } do { if ( wiced_packet_get_data( receive, 0, (uint8_t**) &rxData, \ &fragLeft, &totalLeft ) != WICED_SUCCESS ) { wilddog_debug_level(WD_DEBUG_ERROR, \ "get data from packet error \r\n" ); wiced_packet_delete( receive ); return -1; } else { if ( pos + fragLeft > bufLen ) { /* too large, drop*/ wiced_packet_delete( receive ); wilddog_debug_level(WD_DEBUG_ERROR, \ "too large receive end , get %d bytes\n", pos ); return 0; } if ( total == 0 ) { /*only get first data, next fragment's total is wrong*/ total = totalLeft; } memcpy( (uint8_t *) ( buf + pos ), rxData, fragLeft ); pos += fragLeft; fragLeft = 0; } if ( pos == total ) { /*end*/ #if WILDDOG_SELFTEST { performtest_getWaitSessionQueryTime(); performtest_getWaitRecvTime(); } #endif wilddog_debug_level(WD_DEBUG_LOG, "received %d data!", pos); wiced_packet_delete( receive ); return pos; } else if ( pos < total ) { /*get next fragment*/ wilddog_debug_level(WD_DEBUG_LOG, "more than one packet!"); if ( WICED_SUCCESS != _packet_get_next_fragment( receive, \ &receive ) ) { wilddog_debug_level(WD_DEBUG_ERROR, \ "get next fragment err! %d lost!", total); if(receive) wiced_packet_delete( receive ); return -1; } if ( NULL == receive ) { wilddog_debug_level(WD_DEBUG_ERROR, \ "get next fragment err! %d lost!", total); return -1; } } else { wiced_packet_delete( receive ); return -1; } } while ( pos < total ); } else { wilddog_debug_level(WD_DEBUG_LOG, "result = %d",result); } /*if(receive) wiced_packet_delete( receive );*/ return 0; }