wiced_result_t wiced_tcp_stream_flush( wiced_tcp_stream_t* tcp_stream ) { wiced_result_t result = WICED_TCPIP_SUCCESS; wiced_assert("Bad args", tcp_stream != NULL); WICED_LINK_CHECK_TCP_SOCKET( tcp_stream->socket ); /* Check if there is a packet to send */ if ( tcp_stream->tx_packet != NULL ) { wiced_packet_set_data_end(tcp_stream->tx_packet, tcp_stream->tx_packet_data); result = wiced_tcp_send_packet( tcp_stream->socket, tcp_stream->tx_packet ); tcp_stream->tx_packet_data = NULL; tcp_stream->tx_packet_space_available = 0; if ( result != WICED_TCPIP_SUCCESS ) { wiced_packet_delete( tcp_stream->tx_packet ); } tcp_stream->tx_packet = NULL; } return result; }
wiced_result_t wiced_tcp_stream_write( wiced_tcp_stream_t* tcp_stream, const void* data, uint32_t data_length ) { wiced_assert("Bad args", tcp_stream != NULL); WICED_LINK_CHECK_TCP_SOCKET( tcp_stream->socket ); while ( data_length != 0 ) { uint16_t amount_to_write; /* Check if we don't have a packet */ if ( tcp_stream->tx_packet == NULL ) { wiced_result_t result; result = wiced_packet_create_tcp( tcp_stream->socket, (uint16_t) MIN( data_length, 0xffff ), &tcp_stream->tx_packet, &tcp_stream->tx_packet_data , &tcp_stream->tx_packet_space_available ); if ( result != WICED_TCPIP_SUCCESS ) { return result; } } /* Write data */ amount_to_write = (uint16_t) MIN( data_length, tcp_stream->tx_packet_space_available ); tcp_stream->tx_packet_data = MEMCAT( tcp_stream->tx_packet_data, data, amount_to_write ); /* Update variables */ data_length = (uint16_t)(data_length - amount_to_write); tcp_stream->tx_packet_space_available = (uint16_t) ( tcp_stream->tx_packet_space_available - amount_to_write ); data = (void*)((uint32_t)data + amount_to_write); /* Check if the packet is full */ if ( tcp_stream->tx_packet_space_available == 0 ) { wiced_result_t result; /* Send the packet */ wiced_packet_set_data_end( tcp_stream->tx_packet, (uint8_t*)tcp_stream->tx_packet_data ); result = wiced_tcp_send_packet( tcp_stream->socket, tcp_stream->tx_packet ); tcp_stream->tx_packet_data = NULL; tcp_stream->tx_packet_space_available = 0; if ( result != WICED_TCPIP_SUCCESS ) { wiced_packet_delete( tcp_stream->tx_packet ); tcp_stream->tx_packet = NULL; return result; } tcp_stream->tx_packet = NULL; } } return WICED_TCPIP_SUCCESS; }
int rwl_write_eth_port(void* hndle, char* write_buf, unsigned long size, unsigned long *numwritten) { uint16_t available_data_length; wiced_packet_t* tx_packet; char* tx_data; unsigned long temp_size = 0; unsigned long temp=0; if ( size == 0 ) { return SUCCESS; } while (size > 0) { if (wiced_packet_create_tcp(&tcp_client_socket, 128, &tx_packet, (uint8_t **)&tx_data, &available_data_length) != WICED_SUCCESS) { WPRINT_APP_INFO(("TCP packet creation failed\n")); return WICED_ERROR; } if (size > available_data_length) { temp_size = available_data_length; } else { temp_size =size; } /* Write the message into tx_data"*/ memcpy(tx_data, (write_buf + temp), temp_size); wiced_packet_set_data_end( tx_packet, (uint8_t*)tx_data + temp_size ); /* Send the TCP packet*/ if ( wiced_tcp_send_packet( &tcp_client_socket, tx_packet ) != WICED_SUCCESS ) { WPRINT_APP_INFO(("TCP packet send failed \n")); /*Delete packet, since the send failed*/ wiced_packet_delete(tx_packet); return WICED_ERROR; } wiced_packet_delete(tx_packet); size = size - temp_size; temp = temp+temp_size; } return available_data_length; }
static wiced_result_t tcp_server_process( tcp_server_handle_t* server, wiced_packet_t* rx_packet ) { char* request; uint16_t request_length; uint16_t available_data_length; wiced_packet_t* tx_packet; char* tx_data; wiced_packet_get_data( rx_packet, 0, (uint8_t**) &request, &request_length, &available_data_length ); /* Null terminate the received string */ request[request_length] = '\x0'; WPRINT_APP_INFO(("Received data: %s \n", request)); /* Send echo back */ if (wiced_packet_create_tcp(&server->socket, TCP_PACKET_MAX_DATA_LENGTH, &tx_packet, (uint8_t**)&tx_data, &available_data_length) != WICED_SUCCESS) { WPRINT_APP_INFO(("TCP packet creation failed\n")); return WICED_ERROR; } /* Write the message into tx_data" */ tx_data[request_length] = '\x0'; memcpy(tx_data, request, request_length); /* Set the end of the data portion */ wiced_packet_set_data_end(tx_packet, (uint8_t*)tx_data + request_length); /* Send the TCP packet */ if (wiced_tcp_send_packet(&server->socket, tx_packet) != WICED_SUCCESS) { WPRINT_APP_INFO(("TCP packet send failed\n")); /* Delete packet, since the send failed */ wiced_packet_delete(tx_packet); server->quit=WICED_TRUE; return WICED_ERROR; } WPRINT_APP_INFO(("Echo data: %s\n", tx_data)); return WICED_SUCCESS; }
wiced_result_t wiced_tcp_send_buffer( wiced_tcp_socket_t* socket, const void* buffer, uint16_t buffer_length ) { wiced_packet_t* packet = NULL; uint8_t* data_ptr = (uint8_t*)buffer; uint8_t* packet_data_ptr; uint16_t available_space; wiced_assert("Bad args", socket != NULL); WICED_LINK_CHECK_TCP_SOCKET( socket ); /* Create a packet, copy the data and send it off */ while ( buffer_length != 0 ) { uint16_t data_to_write; wiced_result_t result = wiced_packet_create_tcp( socket, buffer_length, &packet, &packet_data_ptr, &available_space ); if ( result != WICED_TCPIP_SUCCESS ) { return result; } /* Write data */ data_to_write = MIN(buffer_length, available_space); packet_data_ptr = MEMCAT(packet_data_ptr, data_ptr, data_to_write); wiced_packet_set_data_end( packet, packet_data_ptr ); /* Update variables */ data_ptr += data_to_write; buffer_length = (uint16_t) ( buffer_length - data_to_write ); available_space = (uint16_t) ( available_space - data_to_write ); result = wiced_tcp_send_packet( socket, packet ); if ( result != WICED_TCPIP_SUCCESS ) { wiced_packet_delete( packet ); return result; } } return WICED_TCPIP_SUCCESS; }
static wiced_result_t received_data_callback( wiced_tcp_socket_t* socket, void* arg ) { uint16_t request_length; uint16_t available_data_length; wiced_packet_t* temp_packet = NULL; if(wiced_tcp_receive( socket, &temp_packet, 500 ) == WICED_SUCCESS) // get the data from the client { char *rbuffer; dbEntry_t receive; char commandId; int err=1; char returnMessage[15]; // get the pointer to the packet sent by the client and the data wiced_packet_get_data( temp_packet, 0, (uint8_t**) &rbuffer, &request_length, &available_data_length ); sscanf(rbuffer,"%c%4x%2x%4x",&commandId,(unsigned int *)&receive.deviceId,(unsigned int *)&receive.regId,(unsigned int *)&receive.value); wiced_packet_delete( temp_packet ); // free the packet if(request_length >= 11 && request_length <= 13 ) //11 if no end 12 if CR 13 if CRLF { dbEntry_t *newDbEntry; switch(commandId) { case 'R': // they sent a Read command newDbEntry = dbFind(&receive); // look through the database to find a previous write of the deviceId/regId if(newDbEntry) { err=0; sprintf(returnMessage,"A%04X%02X%04X",(unsigned int)newDbEntry->deviceId,(unsigned int)newDbEntry->regId,(unsigned int)newDbEntry->value); } else err = 1; break; case 'W': // they sent a Write command sprintf(returnMessage,"A%04X%02X%04X",(unsigned int)receive.deviceId,(unsigned int)receive.regId,(unsigned int)receive.value); dbEntry_t *newDB; newDB = malloc(sizeof(dbEntry_t)); // make a new entry to put in the database memcpy(newDB,&receive,sizeof(dbEntry_t)); // copy the received data into the new entry dbSetValue(newDB); // save it. err = 0; break; default: // if they don't send a legal command then it is an error err = 1; break; } } // Print IP address of the client (peer) that sent the data and print to terminal uint32_t peerAddressV4; peerAddressV4 = (*socket).socket.nx_tcp_socket_connect_ip.nxd_ip_address.v4; WPRINT_APP_INFO(("%u.%u.%u.%u\t", (uint8_t)(peerAddressV4 >> 24), (uint8_t)(peerAddressV4 >> 16), (uint8_t)(peerAddressV4 >> 8), (uint8_t)(peerAddressV4 >> 0))); // Print the port that the peer connected from uint16_t peerPort; peerPort = (*socket).socket.nx_tcp_socket_connect_port; WPRINT_APP_INFO(("%d\t",peerPort)); // Print the data that was sent by the client (peer) if(err) { strcpy(returnMessage,"X"); WPRINT_APP_INFO(("X length=%d\n",available_data_length)); } else { uint32_t count; linked_list_get_count(&db,&count); WPRINT_APP_INFO(("%c\t%4X\t%2X\t%4X\t%d\n",commandId,(unsigned int)receive.deviceId,(unsigned int)receive.regId,(unsigned int)receive.value,(int)count)); } // send response packet wiced_packet_t* tx_packet; uint8_t *tx_data; wiced_packet_create_tcp(socket, strlen(returnMessage), &tx_packet, (uint8_t**)&tx_data, &available_data_length); memcpy(tx_data, returnMessage, strlen(returnMessage)); wiced_packet_set_data_end(tx_packet, (uint8_t*)&tx_data[strlen(returnMessage)]); wiced_tcp_send_packet(socket, tx_packet); wiced_packet_delete(tx_packet); wiced_tcp_server_disconnect_socket(&tcp_server,socket); }