wiced_result_t write_reply_header(wiced_tcp_stream_t* stream, const char* mime_type, wiced_bool_t nocache ) { /* Copy HTTP OK header into packet and adjust the write pointers */ wiced_tcp_stream_write( stream, ok_header, sizeof( ok_header ) - 1 ); /* Add Mime type */ wiced_tcp_stream_write( stream, mime_type, strlen( mime_type ) ); /* Output No-Cache headers if required */ if ( nocache == WICED_TRUE ) { wiced_tcp_stream_write( stream, NO_CACHE_HEADER, sizeof( NO_CACHE_HEADER ) - 1 ); } /* Add double carriage return, line feed */ wiced_tcp_stream_write( stream, crlfcrlf, sizeof( crlfcrlf ) - 1 ); return WICED_SUCCESS; }
static int process_button_handler( const char* url, wiced_tcp_stream_t* stream, void* arg ) { int params_len = strlen(url); char * found_loc = NULL; char end_found = 0; /* Process the GET parameter list to determine if buttons have been pressed */ /* Cycle through parameter list string until end or newline */ while ( end_found == 0 ) { /* Check if parameter is "led" */ if ( 0 == strncmp( url, "btname", 6 ) ) { found_loc = (char*)&url[7]; } /* Scan ahead to the next parameter or the end of the parameter list */ while ( ( *url != '&' ) && ( *url != '\n' ) && ( params_len > 0 ) ) { url++; params_len--; } if ( *url != '&' ) { end_found = 1; } if ( found_loc != NULL ) { char* tmp = (char*)url; *tmp = '\x00'; printf( "\r\nTeam Zanker : Detected button press: %s\r\n\r\n", found_loc ); } if ( end_found == 0 ) { /* Skip over the "&" which joins parameters if found */ url++; } } /* Send the html page back to the client */ wiced_tcp_stream_write( stream, resource_apps_DIR_wicedhack_DIR_top_web_page_top_html, sizeof(resource_apps_DIR_wicedhack_DIR_top_web_page_top_html) ); return 0; }
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; }
int platform_network_write(Network* n, unsigned char* buffer, int length, int timeout) { wiced_result_t rc = wiced_tcp_stream_write(&n->stream, buffer, length); if (rc != WICED_SUCCESS) { platform_printf("unable to write data to tcp stream, rc = %d\n", rc); return -1; } rc = wiced_tcp_stream_flush(&n->stream); if (rc != WICED_SUCCESS) { platform_printf("unable to flush tcp stream, rc = %d\n", rc); return -1; } //platform_printf("%s: successfully sent %d bytes to tcp stream\n", __func__, length); return length; }
wiced_result_t wiced_tcp_stream_write_resource( wiced_tcp_stream_t* tcp_stream, const resource_hnd_t* res_id ) { const void* data; uint32_t res_size; wiced_result_t result; uint32_t pos = 0; do { result = resource_get_readonly_buffer ( res_id, pos, 0x7fffffff, &res_size, &data ); if ( result != WICED_SUCCESS ) { return result; } result = wiced_tcp_stream_write( tcp_stream, data, (uint16_t) res_size ); resource_free_readonly_buffer( res_id, data ); pos += res_size; } while ( res_size > 0 ); return result; }
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; }
// The nonsecure server thread static void tcp_server_nonsecure_thread_main(wiced_thread_arg_t arg) { wiced_result_t result; wiced_tcp_stream_t stream; // The TCP stream wiced_tcp_socket_t socket; uint8_t rbuffer[MAX_LEGAL_MSG]; char returnMessage[128]; // better use less than 128 bytes // setup the server by creating the socket and hooking it to the correct TCP Port result = wiced_tcp_create_socket(&socket, INTERFACE); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Create socket failed\n")); return; // this is a bad outcome } wiced_tcp_stream_init(&stream,&socket); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Init stream failed\n")); return; // this is a bad outcome } result = wiced_tcp_listen( &socket, TCP_SERVER_NONSECURE_LISTEN_PORT ); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Listen socket failed\n")); return; } while (1 ) { result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection if(result != WICED_SUCCESS) // this occurs if the accept times out continue; nonsecureConnectionCount += 1; /// Figure out which client is talking to us... and on which port wiced_ip_address_t peerAddress; uint16_t peerPort; wiced_tcp_server_peer(&socket,&peerAddress,&peerPort); uint32_t dataReadCount; wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms processClientCommand(rbuffer, dataReadCount ,returnMessage); displayResult(peerAddress,peerPort,returnMessage); // send response and close things up wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage)); wiced_tcp_stream_flush(&stream); wiced_tcp_disconnect(&socket); // disconnect the connection wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left wiced_tcp_stream_init(&stream,&socket); // setup for next connection } }
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; }
static wiced_result_t process_request( ota_http_request_message_t* request, wiced_tcp_socket_t* socket, const ota_http_page_t* server_url_list ) { /* Search the url to find the question mark if there is one */ int i = 0; wiced_tcp_stream_t stream; wiced_bool_t found = WICED_FALSE; /* Init the tcp stream */ wiced_tcp_stream_init(&stream, socket); /* Search URL list to determine if request matches one of our pages */ while ( server_url_list[i].ota_url != NULL ) { /* Compare request to a path */ if ( 0 == strncmp( server_url_list[i].ota_url, (const char*)request->url_ptr, request->url_length ) ) { /* Matching path found */ found = WICED_TRUE; /* Call the content handler function to write the page content into the packet and adjust the write pointers */ switch (server_url_list[i].ota_url_content_type) { case OTA_DYNAMIC_URL_CONTENT: write_reply_header(&stream, server_url_list[i].ota_mime_type, WICED_TRUE); /* Fall through */ case OTA_RAW_DYNAMIC_URL_CONTENT: server_url_list[i].ota_url_content.ota_dynamic_data.generator( request, &stream, server_url_list[i].ota_url_content.ota_dynamic_data.arg ); wiced_tcp_stream_flush(&stream); break; case OTA_STATIC_URL_CONTENT: write_reply_header(&stream, server_url_list[i].ota_mime_type, WICED_FALSE); /* Fall through */ case OTA_RAW_STATIC_URL_CONTENT: wiced_tcp_stream_write(&stream, server_url_list[i].ota_url_content.ota_static_data.ptr, server_url_list[i].ota_url_content.ota_static_data.length); wiced_tcp_stream_flush(&stream); break; case OTA_RESOURCE_URL_CONTENT: write_reply_header(&stream, server_url_list[i].ota_mime_type, WICED_FALSE); /* Fall through */ case OTA_RAW_RESOURCE_URL_CONTENT: wiced_tcp_stream_write_resource( &stream, server_url_list[i].ota_url_content.ota_resource_data ); wiced_tcp_stream_flush( &stream ); break; default: wiced_assert("Unknown entry in URL list", 0 != 0 ); break; } break; } i++; } /* Check if page was not found */ if ( found == WICED_FALSE ) { /* Send back 404 */ wiced_tcp_stream_write(&stream, not_found_header, sizeof(not_found_header)-1); wiced_tcp_stream_flush(&stream); } wiced_assert( "Page Serve finished with data still in stream", stream.tx_packet == NULL ); return WICED_SUCCESS; }
static void tcp_server_thread_main(wiced_thread_arg_t arg) { wiced_bool_t wwepSecurity = (wiced_bool_t)arg; wiced_result_t result; wiced_tcp_stream_t stream; // The TCP stream wiced_tcp_socket_t socket; platform_dct_security_t *dct_security; wiced_tls_identity_t tls_identity; wiced_tls_context_t tls_context; uint8_t rbuffer[MAX_LEGAL_MSG]; char returnMessage[128]; // better use less than 128 bytes // setup the server by creating the socket and hooking it to the correct TCP Port result = wiced_tcp_create_socket(&socket, INTERFACE); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Create socket failed\n")); return; // this is a bad outcome } if(wwepSecurity == WICED_TRUE) { WPRINT_APP_INFO(("Starting secure\n")); } else { WPRINT_APP_INFO(("Starting non-secure\n")); } result = wiced_tcp_listen( &socket, (wwepSecurity == WICED_TRUE)?TCP_SERVER_SECURE_LISTEN_PORT:TCP_SERVER_NONSECURE_LISTEN_PORT ); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Listen socket failed\n")); return; } if(wwepSecurity == WICED_TRUE) { /* Lock the DCT to allow us to access the certificate and key */ WPRINT_APP_INFO(( "Read the certificate Key from DCT\n" )); result = wiced_dct_read_lock( (void**) &dct_security, WICED_FALSE, DCT_SECURITY_SECTION, 0, sizeof( *dct_security ) ); if ( result != WICED_SUCCESS ) { WPRINT_APP_INFO(("Unable to lock DCT to read certificate\n")); return; } /* Setup TLS identity */ result = wiced_tls_init_identity( &tls_identity, dct_security->private_key, strlen( dct_security->private_key ), (uint8_t*) dct_security->certificate, strlen( dct_security->certificate ) ); if ( result != WICED_SUCCESS ) { WPRINT_APP_INFO(( "Unable to initialize TLS identity. Error = [%d]\n", result )); return; } } else { wiced_tcp_stream_init(&stream,&socket); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Init stream failed\n")); return; // this is a bad outcome } } while (1 ) { if(wwepSecurity == WICED_TRUE) { result = wiced_tls_init_context( &tls_context, &tls_identity, NULL ); if(result != WICED_SUCCESS) { WPRINT_APP_INFO(("Init context failed %d",result)); return; } result = wiced_tcp_enable_tls(&socket,&tls_context); if(result != WICED_SUCCESS) { WPRINT_APP_INFO(("Enabling TLS failed %d",result)); return; } wiced_tcp_stream_init(&stream,&socket); if(WICED_SUCCESS != result) { WPRINT_APP_INFO(("Init stream failed\n")); return; // this is a bad outcome } } result = wiced_tcp_accept( &socket ); // this halts the thread until there is a connection if(result != WICED_SUCCESS) // this occurs if the accept times out continue; if(wwepSecurity == WICED_TRUE) secureConnectionCount += 1; else nonsecureConnectionCount += 1; /// Figure out which client is talking to us... and on which port wiced_ip_address_t peerAddress; uint16_t peerPort; wiced_tcp_server_peer(&socket,&peerAddress,&peerPort); uint32_t dataReadCount; wiced_tcp_stream_read_with_count(&stream,&rbuffer,MAX_LEGAL_MSG,100,&dataReadCount); // timeout in 100 ms processClientCommand(rbuffer, dataReadCount ,returnMessage); displayResult(peerAddress,peerPort,returnMessage); // send response and close things up wiced_tcp_stream_write(&stream,returnMessage,strlen(returnMessage)); wiced_tcp_stream_flush(&stream); wiced_tcp_disconnect(&socket); // disconnect the connection if(wwepSecurity == WICED_TRUE) { wiced_tls_deinit_context(&tls_context); } wiced_tcp_stream_deinit(&stream); // clear the stream if any crap left wiced_tcp_stream_init(&stream,&socket); // setup for next connection } }