wwd_result_t host_buffer_get( wiced_buffer_t * buffer, wwd_buffer_dir_t direction, unsigned short size, wiced_bool_t wait ) { volatile UINT status; NX_PACKET **nx_buffer = (NX_PACKET **) buffer; NX_PACKET_POOL* pool = ( direction == WWD_NETWORK_TX ) ? tx_pool : rx_pool; wiced_assert("Error: pools have not been set up\n", pool != NULL); if ( size > WICED_LINK_MTU ) { WPRINT_NETWORK_DEBUG(("Attempt to allocate a buffer larger than the MTU of the link\n")); return WWD_BUFFER_UNAVAILABLE_PERMANENT; } if ( NX_SUCCESS != ( status = nx_packet_allocate( pool, nx_buffer, 0, ( wait == WICED_TRUE ) ? NX_WAIT_FOREVER : NX_NO_WAIT ) ) ) { /* If there is no packet in the system TX pool, check whether there is any packet available in the application defined tx pool */ /* and try to allocate from there */ if( status == NX_NO_PACKET ) { if( ( direction == WWD_NETWORK_TX ) && ( application_defined_tx_pool != NULL ) ) { if ( NX_SUCCESS != ( status = nx_packet_allocate( application_defined_tx_pool, nx_buffer, 0, ( wait == WICED_TRUE ) ? NX_WAIT_FOREVER : NX_NO_WAIT ) ) ) { return WWD_BUFFER_UNAVAILABLE_TEMPORARY; } else { ( *nx_buffer )->nx_packet_length = size; ( *nx_buffer )->nx_packet_append_ptr = ( *nx_buffer )->nx_packet_prepend_ptr + size; } } else if( ( direction == WWD_NETWORK_RX ) && ( application_defined_rx_pool != NULL ) ) { if ( NX_SUCCESS != ( status = nx_packet_allocate( application_defined_rx_pool, nx_buffer, 0, ( wait == WICED_TRUE ) ? NX_WAIT_FOREVER : NX_NO_WAIT ) ) ) { return WWD_BUFFER_UNAVAILABLE_TEMPORARY; } else { ( *nx_buffer )->nx_packet_length = size; ( *nx_buffer )->nx_packet_append_ptr = ( *nx_buffer )->nx_packet_prepend_ptr + size; } } else { return WWD_BUFFER_UNAVAILABLE_TEMPORARY; } } else { return WWD_BUFFER_ALLOC_FAIL; } } ( *nx_buffer )->nx_packet_length = size; ( *nx_buffer )->nx_packet_append_ptr = ( *nx_buffer )->nx_packet_prepend_ptr + size; return WWD_SUCCESS; }
INT send( INT sockID, const CHAR *msg, INT msgLength, INT flags ) { switch ( sockets[sockID].type ) { case BSD_SOCKET_TCP: { NX_PACKET* packet; INT progress = 0; while ( progress != msgLength ) { nx_packet_allocate( &wiced_packet_pools[0], &packet, NX_TCP_PACKET, NX_WAIT_FOREVER ); INT data_length = MIN( msgLength, sockets[sockID].socket.tcp.nx_tcp_socket_mss ); nx_packet_data_append( packet, (VOID*) msg, data_length, &wiced_packet_pools[0], NX_WAIT_FOREVER ); if ( nx_tcp_socket_send(&sockets[sockID].socket.tcp, packet, NX_WAIT_FOREVER) != NX_SUCCESS ) { nx_packet_release( packet ); return BSD_ERROR; } else { progress += data_length; msg += data_length; } } return progress; break; } case BSD_SOCKET_UDP: { NX_PACKET* packet; nx_packet_allocate( &wiced_packet_pools[0], &packet, NX_UDP_PACKET, NX_WAIT_FOREVER ); nx_packet_data_append( packet, (VOID*) msg, msgLength, &wiced_packet_pools[0], NX_WAIT_FOREVER ); if ( nx_udp_socket_send(&sockets[sockID].socket.udp, packet, htonl(SOCK4_ADDRESS(&sockets[sockID].last_received_peer)), hton16(SOCK4_PORT(&sockets[sockID].last_received_peer))) != NX_SUCCESS ) { nx_packet_release( packet ); } else { return msgLength; } break; } } return BSD_ERROR; }
INT sendto( INT sockID, CHAR *msg, INT msgLength, INT flags, struct sockaddr *destAddr, INT destAddrLen ) { switch ( sockets[sockID].type ) { case BSD_SOCKET_TCP: { NX_PACKET* packet; nx_packet_allocate( &wiced_packet_pools[0], &packet, NX_TCP_PACKET, NX_WAIT_FOREVER ); INT data_length = MIN( msgLength, sockets[sockID].socket.tcp.nx_tcp_socket_mss ); nx_packet_data_append( packet, (VOID*) msg, data_length, &wiced_packet_pools[0], NX_WAIT_FOREVER ); if ( nx_tcp_socket_send(&sockets[sockID].socket.tcp, packet, NX_WAIT_FOREVER) != NX_SUCCESS ) { nx_packet_release( packet ); } else { return data_length; } break; } case BSD_SOCKET_UDP: { NX_PACKET* packet; /* Note that it's important to specify the packet as NX_IPv4_UDP_PACKET otherwise NetX Duo allocates another 20 bytes for IPv6 header and iperf won't work properly */ nx_packet_allocate( &wiced_packet_pools[0], &packet, NX_UDP_PACKET, NX_WAIT_FOREVER ); nx_packet_data_append( packet, msg, msgLength, &wiced_packet_pools[0], NX_WAIT_FOREVER ); if ( nx_udp_socket_send(&sockets[sockID].socket.udp, packet, htonl(SOCK4_ADDRESS(destAddr)), hton16(SOCK4_PORT(destAddr))) != NX_SUCCESS ) { nx_packet_release( packet ); } else { return msgLength; } break; } } return BSD_ERROR; }
int s7g2_write(Network* n, unsigned char* buffer, int len, int timeout_ms) { UINT status; NX_PACKET *packet_ptr; status = nx_packet_allocate(n->my_packet_pool, &packet_ptr, NX_TCP_PACKET, timeout_ms); if(status) return -1; status = nx_packet_data_append(packet_ptr, buffer, len, n->my_packet_pool, timeout_ms); if(status) return -1; status = nx_tcp_socket_send(&n->my_socket, packet_ptr, timeout_ms); if(status) return -1; nx_packet_release(packet_ptr); return len; }
/* The NetX send callback * return : bytes sent, or error */ int NetX_Send(CYASSL* ssl, char *buf, int sz, void *ctx) { NetX_Ctx* nxCtx = (NetX_Ctx*)ctx; NX_PACKET* packet; NX_PACKET_POOL* pool; /* shorthand */ UINT status; if (nxCtx == NULL || nxCtx->nxSocket == NULL) { CYASSL_MSG("NetX Send NULL parameters"); return CYASSL_CBIO_ERR_GENERAL; } pool = nxCtx->nxSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool; status = nx_packet_allocate(pool, &packet, NX_TCP_PACKET, nxCtx->nxWait); if (status != NX_SUCCESS) { CYASSL_MSG("NetX Send packet alloc error"); return CYASSL_CBIO_ERR_GENERAL; } status = nx_packet_data_append(packet, buf, sz, pool, nxCtx->nxWait); if (status != NX_SUCCESS) { nx_packet_release(packet); CYASSL_MSG("NetX Send data append error"); return CYASSL_CBIO_ERR_GENERAL; } status = nx_tcp_socket_send(nxCtx->nxSocket, packet, nxCtx->nxWait); if (status != NX_SUCCESS) { nx_packet_release(packet); CYASSL_MSG("NetX Send socket send error"); return CYASSL_CBIO_ERR_GENERAL; } return sz; }
wwd_result_t host_buffer_add_remove_at_front( wiced_buffer_t * buffer, int32_t add_remove_amount ) { NX_PACKET **nx_buffer = (NX_PACKET **) buffer; UCHAR * new_start = ( *nx_buffer )->nx_packet_prepend_ptr + add_remove_amount; wiced_assert("Error: Invalid buffer\n", buffer != NULL); if ( new_start < ( *nx_buffer )->nx_packet_data_start ) { #ifdef SUPPORT_BUFFER_CHAINING /* Requested position will not fit in current buffer - need to chain one in front of current buffer */ NX_PACKET *new_nx_buffer; if ( NX_SUCCESS != ( status = nx_packet_allocate( (*nx_buffer)->nx_packet_pool_owner, &new_nx_buffer, 0, NX_NO_WAIT ) ) ) { WPRINT_NETWORK_DEBUG(("Could not allocate another buffer to prepend in front of existing buffer\n")); return -1; } /* New buffer has been allocated - set it up at front of chain */ (*new_nx_buffer)->nx_packet_length = -add_remove_amount; (*new_nx_buffer)->nx_packet_append_ptr = (*nx_buffer)->nx_packet_prepend_ptr - add_remove_amount; (*new_nx_buffer)->nx_packet_next = nx_buffer; *nx_buffer = new_nx_buffer; new_start = (*nx_buffer)->nx_packet_prepend_ptr; #else /* ifdef SUPPORT_BUFFER_CHAINING */ /* Trying to move to a location before start - not supported without buffer chaining*/ WPRINT_NETWORK_ERROR(("Attempt to move to a location before start - not supported without buffer chaining\n")); return WWD_BUFFER_POINTER_MOVE_ERROR; #endif /* ifdef SUPPORT_BUFFER_CHAINING */ } else if ( new_start > ( *nx_buffer )->nx_packet_data_end ) { #ifdef SUPPORT_BUFFER_CHAINING /* moving to a location after end of current buffer - remove buffer from chain */ NX_PACKET *new_head_nx_buffer = (*nx_buffer)->nx_packet_next; if ( new_head_nx_buffer == NULL ) { /* there are no buffers after current buffer - can't move to requested location */ WPRINT_NETWORK_DEBUG(("Can't move to requested location - there are no buffers after current buffer\n")); return -3; } new_head_nx_buffer->nx_packet_length -= (new_start - nx_buffer->nx_packet_append_ptr); new_head_nx_buffer->nx_packet_prepend_ptr += (new_start - nx_buffer->nx_packet_append_ptr); (*nx_buffer)->nx_packet_next = NULL; if ( NX_SUCCESS != (status = nx_packet_release( *nx_buffer ) ) ) { WPRINT_NETWORK_DEBUG(("Could not release packet after removal from chain- leaking buffer\n")); return -4; } *nx_buffer = new_head_nx_buffer; new_start = (*nx_buffer)->nx_packet_prepend_ptr; #else /* ifdef SUPPORT_BUFFER_CHAINING */ /* Trying to move to a location after end of buffer - not supported without buffer chaining */ WPRINT_NETWORK_ERROR(("Attempt to move to a location after end of buffer - not supported without buffer chaining\n")); return WWD_BUFFER_POINTER_MOVE_ERROR; #endif /* ifdef SUPPORT_BUFFER_CHAINING */ } else { ( *nx_buffer )->nx_packet_prepend_ptr = new_start; if (( *nx_buffer )->nx_packet_append_ptr < ( *nx_buffer )->nx_packet_prepend_ptr ) { ( *nx_buffer )->nx_packet_append_ptr = ( *nx_buffer )->nx_packet_prepend_ptr; } ( *nx_buffer )->nx_packet_length = (ULONG) ( ( *nx_buffer )->nx_packet_length - (ULONG) add_remove_amount ); } return WWD_SUCCESS; }