static wiced_result_t receive_data(wiced_tcp_socket_t *sock, uint32_t timeout, char **p_buf, uint32_t *len) { wiced_packet_t *packet; uint16_t data_offset; uint16_t data_len; uint16_t total_data_len; uint8_t *data; char *curr; WICED_VERIFY(wiced_tcp_receive(sock, &packet, timeout)); data_offset = 0; wiced_packet_get_data(packet, data_offset, &data, &data_len, &total_data_len); *p_buf = (char *)malloc(sizeof(char) * ((size_t)total_data_len + 1)); curr = *p_buf; memcpy(*p_buf, data, data_len); data_offset = (uint16_t)(data_offset + data_len); curr += data_len; while (data_len < total_data_len) { wiced_packet_get_data(packet, data_offset, &data, &data_len, &total_data_len); memcpy(curr, data, data_len); data_offset = (uint16_t)(data_offset + data_len); curr += data_len; } *curr = '\0'; *len = total_data_len; wiced_packet_delete(packet); return WICED_SUCCESS; }
static wiced_result_t wiced_websocket_tls_connect( wiced_websocket_t* websocket,wiced_ip_address_t* address ) { wiced_result_t result = WICED_ERROR; wiced_tls_init_simple_context( &websocket_tls_context, NULL ); WICED_VERIFY( wiced_tcp_create_socket( &websocket->socket, WICED_STA_INTERFACE ) ); wiced_tcp_register_callbacks( &websocket->socket, websocket->on_open, NULL, websocket->on_close ); wiced_tcp_enable_tls( &websocket->socket, &websocket_tls_context ); wiced_tcp_bind( &websocket->socket, 443 ); result = wiced_tcp_connect( &websocket->socket, address, 443, 10000 ); if ( result != WICED_SUCCESS ) { websocket->error_type=WEBSOCKET_CLIENT_CONNECT_ERROR; websocket->on_error( websocket ); wiced_tcp_delete_socket( &websocket->socket ); return ( result ); } return ( result ); }
wiced_result_t xively_open_feed( xively_feed_t* feed ) { wiced_result_t result; if ( is_ip_address_resolved == WICED_FALSE ) { WICED_VERIFY( wiced_hostname_lookup( HOST_NAME, &xively_server_ip_address, DNS_TIMEOUT ) ); is_ip_address_resolved = WICED_TRUE; } wiced_tls_init_simple_context( &feed->tls_context, NULL ); WICED_VERIFY( wiced_tcp_create_socket( &feed->socket, WICED_STA_INTERFACE ) ); result = wiced_tcp_bind( &feed->socket, WICED_ANY_PORT ); if ( result != WICED_SUCCESS ) { wiced_tcp_delete_socket( &feed->socket ); return result; } result = wiced_tcp_enable_tls( &feed->socket, &feed->tls_context ); if ( result != WICED_SUCCESS ) { wiced_tls_deinit_context( &feed->tls_context ); wiced_tcp_delete_socket( &feed->socket ); return result; } result = wiced_tcp_connect( &feed->socket, &xively_server_ip_address, HTTPS_PORT, SOCKET_CONNECT_TIMEOUT ); if ( result != WICED_SUCCESS ) { wiced_tcp_delete_socket( &feed->socket ); return result; } return WICED_SUCCESS; }
wiced_result_t ota_server_start( ota_server_t* server, uint16_t port, const ota_http_page_t* page_database, wiced_interface_t interface ) { memset( server, 0, sizeof( *server ) ); /* Create the TCP socket */ WICED_VERIFY(wiced_tcp_create_socket( &server->socket, interface )); if (wiced_tcp_listen( (wiced_tcp_socket_t*) &server->socket, port ) != WICED_SUCCESS) { wiced_tcp_delete_socket(&server->socket); return WICED_ERROR; } server->page_database = page_database; return wiced_rtos_create_thread(&server->thread, OTA_SERVER_THREAD_PRIORITY, "HTTPserver", ota_server_thread_main, OTA_SERVER_STACK_SIZE, server); }
wiced_result_t wiced_start_dhcp_server(wiced_dhcp_server_t* server, wiced_interface_t interface) { DHCP_CHECK_PARAMS( (server == NULL) || ( (interface != WICED_STA_INTERFACE) && (interface != WICED_AP_INTERFACE) && (interface != WICED_P2P_INTERFACE) && (interface != WICED_CONFIG_INTERFACE) ), WICED_BADARG ); server->interface = interface; /* Clear cache */ memset( cached_mac_addresses, 0, sizeof( cached_mac_addresses ) ); memset( cached_ip_addresses, 0, sizeof( cached_ip_addresses ) ); /* Create DHCP socket */ WICED_VERIFY(wiced_udp_create_socket(&server->socket, IPPORT_DHCPS, interface)); /* Start DHCP server */ wiced_rtos_create_thread(&server->thread, DHCP_THREAD_PRIORITY, "DHCPserver", dhcp_thread, DHCP_STACK_SIZE, server); return WICED_SUCCESS; }
wiced_result_t xively_write_datapoint( xively_datastream_t* stream, const uint8_t* data, const wiced_iso8601_time_t* timestamp ) { /* Write datapoint */ WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)stream->channel_name, strlen( stream->channel_name ) ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)COMMA, sizeof( COMMA ) - 1 ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)timestamp, sizeof( *timestamp ) ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)COMMA, sizeof( COMMA ) - 1 ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)data, stream->data_size ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)NEWLINE, sizeof( NEWLINE ) - 1 ) ); return WICED_SUCCESS; }
wiced_result_t xively_flush_datastream( xively_datastream_t* stream ) { wiced_packet_t* response_packet; WICED_VERIFY( wiced_tcp_stream_flush( &stream->tcp_stream ) ); /* Receive and free response packet */ if ( wiced_tcp_receive( stream->tcp_stream.socket, &response_packet, 3000 ) == WICED_SUCCESS ) { uint8_t* data; uint16_t data_length; uint16_t available_data_length; char* is_ok_found; /* Parse HTTP response. */ wiced_packet_get_data( response_packet, 0, &data, &data_length, &available_data_length ); /* Ensure that packet payload is NUL-terminated */ *( data + data_length - 1 ) = 0; /* Find OK. Returns NULL of not found */ is_ok_found = strstr( (char*) data, (char*) "OK" ); wiced_packet_delete( response_packet ); /* OK found. */ if ( is_ok_found != NULL ) { return WICED_SUCCESS; } /* If Not OK, assert flag to perform DNS query in the next data sending */ is_ip_address_resolved = WICED_FALSE; } return WICED_ERROR; }
wiced_result_t xively_create_datastream( xively_feed_t* feed, xively_datastream_t* stream, const char* channel_name, uint32_t data_size, uint32_t number_of_datapoints ) { char content_length_str[ 5 ]; uint16_t content_length = 0; if ( wiced_tcp_stream_init( &stream->tcp_stream, &feed->socket ) != WICED_SUCCESS ) { return WICED_ERROR; } stream->channel_name = channel_name; stream->data_size = data_size; stream->number_of_datapoints = number_of_datapoints; /* Write METHOD and API Endpoint */ WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)PUT_START, sizeof( PUT_START ) - 1 ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)feed->id, strlen( feed->id ) ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)PUT_END, sizeof( PUT_END ) - 1 ) ); /* Write Host */ WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)HOST_TEMPLATE, sizeof( HOST_TEMPLATE ) - 1 ) ); /* Write API Key */ WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)API_KEY_HEADER, sizeof( API_KEY_HEADER ) - 1 ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)feed->api_key, strlen( feed->api_key ) ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)NEWLINE, sizeof( NEWLINE ) - 1 ) ); /* Write data stream content length */ content_length = (uint16_t)( number_of_datapoints * ( strlen( channel_name ) + ( sizeof( COMMA ) - 1 ) + sizeof(wiced_iso8601_time_t) + ( sizeof( COMMA ) - 1 ) + data_size + ( sizeof( NEWLINE ) - 1 ) ) ); memset( content_length_str, 0, sizeof( content_length_str ) ); xively_u16toa( content_length, content_length_str, sizeof( content_length_str ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)CONTENT_LENGTH, sizeof( CONTENT_LENGTH ) - 1 ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)content_length_str, sizeof( content_length_str ) ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)NEWLINE, sizeof( NEWLINE ) - 1 ) ); WICED_VERIFY( wiced_tcp_stream_write( &stream->tcp_stream, (void*)NEWLINE, sizeof( NEWLINE ) - 1 ) ); return WICED_SUCCESS; }
wiced_result_t wiced_wlu_server_serial_start( wiced_uart_t uart_id ) { wiced_assert("wlu_server already started", (wlu_server.started == WICED_FALSE)); wlu_server.started = WICED_TRUE; wlu_server.uart_id = uart_id; WWD_WLAN_KEEP_AWAKE( ); if ( uart_id != STDIO_UART ) { static const platform_uart_config_t uart_config = { .baud_rate = 115200, .data_width = DATA_WIDTH_8BIT, .parity = NO_PARITY, .stop_bits = STOP_BITS_1, .flow_control = FLOW_CONTROL_DISABLED, }; WICED_VERIFY( wiced_uart_init( uart_id, &uart_config, NULL ) ); } /* Start wlu server thread */ wiced_rtos_create_thread(&wlu_server.thread, WLU_SERVER_THREAD_PRIORITY, "wlu_server", wlu_server_thread, WLU_SERVER_STACK_SIZE, &wlu_server); return WICED_SUCCESS; } wiced_result_t wiced_wlu_server_serial_stop( void ) { if ( wiced_rtos_is_current_thread( &wlu_server.thread ) != WICED_SUCCESS ) { wiced_rtos_thread_force_awake( &wlu_server.thread ); wiced_rtos_thread_join( &wlu_server.thread ); wiced_rtos_delete_thread( &wlu_server.thread ); } WWD_WLAN_LET_SLEEP( ); wlu_server.started = WICED_FALSE; return WICED_SUCCESS; } static void wlu_server_thread( uint32_t thread_input ) { wiced_wlu_server_t* wlu = (wiced_wlu_server_t*) thread_input; int argc = 2; char *argv[] = { (char*)wlu->uart_id, "" }; #ifdef RWL_SOCKET char *arge[] = { (char *)wlu->eth_port, "" }; #endif wiced_assert( "invalid argument", wlu->uart_id < WICED_UART_MAX ); UNUSED_PARAMETER(thread_input); #ifdef RWL_SOCKET if (wlu->eth_started == WICED_TRUE) #endif { #ifdef RWL_SOCKET remote_server_exec( argc, (wlu->eth_started == WICED_TRUE)?arge:argv, NULL ); #else remote_server_exec(argc, argv, NULL); #endif } }
wiced_result_t wiced_establish_websocket_handshake( wiced_websocket_t* websocket, wiced_websocket_handshake_fields_t* websocket_header_fields ) { wiced_tcp_stream_t stream; wiced_packet_t* tcp_reply_packet; uint16_t tcp_data_available; uint16_t total_received_bytes; uint8_t* received_handshake; /*Initialise the tcp stream*/ wiced_tcp_stream_init(&stream, &websocket->socket); /*Initialise key array*/ memset(websocket_key_base64,0x0,CLIENT_WEBSOCKET_BASE64_KEY_LENGTH); /*generate a unique websocket key to send to server as part of initial handshake*/ WICED_VERIFY( wiced_generate_client_websocket_key( websocket_key_base64 ) ); /*build the handshaking headers*/ /*< GET /uri HTTP/1.1 >*/ wiced_tcp_stream_write( &stream, "GET ",( uint16_t )strlen("GET ") ); wiced_tcp_stream_write(&stream, websocket_header_fields->request_uri, ( uint16_t )strlen(websocket_header_fields->request_uri) ); wiced_tcp_stream_write( &stream, " HTTP/1.1\r\n",( uint16_t )strlen(" HTTP/1.1\r\n") ); /*< Host: ip1.ip2.ip3.ip4 >*/ wiced_tcp_stream_write( &stream, "Host: ",( uint16_t )strlen("Host: ") ); wiced_tcp_stream_write(&stream, websocket_header_fields->host, ( uint16_t )strlen(websocket_header_fields->host) ); wiced_tcp_stream_write( &stream, "\r\n",( uint16_t )strlen("\r\n") ); /*< Upgrade: websocket>*/ wiced_tcp_stream_write(&stream, "Upgrade: websocket\r\n",( uint16_t )strlen("Upgrade: websocket\r\n") ); /*< Connection: Upgrade >*/ wiced_tcp_stream_write(&stream, "Connection: Upgrade\r\n",( uint16_t )strlen("Connection: Upgrade\r\n") ); /*< Sec-WebSocket-Key: random_base4_value >*/ wiced_tcp_stream_write(&stream, "Sec-WebSocket-Key: ",( uint16_t )strlen("Sec-WebSocket-Key: ") ); wiced_tcp_stream_write(&stream, websocket_key_base64, ( uint16_t )strlen((char*)websocket_key_base64) ); wiced_tcp_stream_write(&stream, "\r\n", ( uint16_t )strlen("\r\n") ); /*< Origin: ip1.ip2.ip3.ip4 >*/ wiced_tcp_stream_write(&stream, "Origin: ", ( uint16_t )strlen("Origin: ") ); wiced_tcp_stream_write(&stream, websocket_header_fields->origin, ( uint16_t )strlen( websocket_header_fields->origin ) ); wiced_tcp_stream_write(&stream, "\r\n",( uint16_t )strlen("\r\n") ); /*The sec_websocket_protocol is optional, so check if it has been added, include in header if required*/ if ( websocket_header_fields->sec_websocket_protocol != NULL) { /*< Sec-WebSocket-Protocol: server_understood_protocol >*/ wiced_tcp_stream_write(&stream, "Sec-WebSocket-Protocol: ",( uint16_t )strlen("Sec-WebSocket-Protocol: ") ); wiced_tcp_stream_write(&stream, websocket_header_fields->sec_websocket_protocol, ( uint16_t )strlen( websocket_header_fields->sec_websocket_protocol ) ); wiced_tcp_stream_write(&stream, "\r\n",( uint16_t )strlen("\r\n") ); } /*< Sec-WebSocket-Version: 13 >*/ wiced_tcp_stream_write(&stream, "Sec-WebSocket-Version: 13\r\n\r\n",strlen("Sec-WebSocket-Version: 13\r\n\r\n") ); /*send the handshake to server*/ wiced_tcp_stream_flush( &stream ); wiced_tcp_stream_deinit( &stream ); WICED_VERIFY( wiced_tcp_receive( &websocket->socket, &tcp_reply_packet, WICED_WAIT_FOREVER ) ); wiced_packet_get_data( tcp_reply_packet, 0, (uint8_t**)&received_handshake, &total_received_bytes, &tcp_data_available ); WICED_VERIFY( wiced_verify_server_handshake( (char*)received_handshake ) ); return WICED_SUCCESS; }