wiced_result_t wiced_network_up( wiced_interface_t interface, wiced_network_config_t config, const wiced_ip_setting_t* ip_settings ) { wiced_result_t result = WICED_SUCCESS; if ( wiced_network_is_up( WICED_TO_WWD_INTERFACE( interface ) ) == WICED_FALSE ) { if ( interface == WICED_CONFIG_INTERFACE ) { wiced_config_soft_ap_t* config_ap; wiced_result_t retval = wiced_dct_read_lock( (void**) &config_ap, WICED_FALSE, DCT_WIFI_CONFIG_SECTION, OFFSETOF(platform_dct_wifi_config_t, config_ap_settings), sizeof(wiced_config_soft_ap_t) ); if ( retval != WICED_SUCCESS ) { return retval; } /* Check config DCT is valid */ if ( config_ap->details_valid == CONFIG_VALIDITY_VALUE ) { result = wiced_start_ap( &config_ap->SSID, config_ap->security, config_ap->security_key, config_ap->channel ); } else { wiced_ssid_t ssid = { .length = sizeof("Wiced Config")-1, .value = "Wiced Config", }; result = wiced_start_ap( &ssid, WICED_SECURITY_OPEN, "", 1 ); } wiced_dct_read_unlock( config_ap, WICED_FALSE ); } else if ( interface == WICED_AP_INTERFACE ) { wiced_config_soft_ap_t* soft_ap; result = wiced_dct_read_lock( (void**) &soft_ap, WICED_FALSE, DCT_WIFI_CONFIG_SECTION, OFFSETOF(platform_dct_wifi_config_t, soft_ap_settings), sizeof(wiced_config_soft_ap_t) ); if ( result != WICED_SUCCESS ) { return result; } result = (wiced_result_t) wwd_wifi_start_ap( &soft_ap->SSID, soft_ap->security, (uint8_t*) soft_ap->security_key, soft_ap->security_key_length, soft_ap->channel ); wiced_dct_read_unlock( soft_ap, WICED_FALSE ); } else { result = wiced_join_ap( ); } }
/** * Determine if the DCT contains wifi credentials. * @return 0 if the device has credentials. 1 otherwise. (yes, it's backwards! The intent is that negative values indicate some kind of error.) */ int wlan_has_credentials() { int has_credentials = 0; platform_dct_wifi_config_t* wifi_config = NULL; wiced_result_t result = wiced_dct_read_lock( (void**) &wifi_config, WICED_FALSE, DCT_WIFI_CONFIG_SECTION, 0, sizeof(*wifi_config)); if (result==WICED_SUCCESS) { has_credentials = wifi_config->device_configured==WICED_TRUE && is_ap_config_set(wifi_config->stored_ap_list[0]); } wiced_dct_read_unlock(wifi_config, WICED_FALSE); return !has_credentials; }
void application_start( void ) { user_dct_data_t* dct; uint32_t sample_interval; uint16_t max_sockets = 10; wiced_init( ); /* Configure device */ wiced_configure_device( app_config ); /* Initialise Xively data. Xively data is shared among multiple threads; therefore a mutex is required */ memset( &xively_data, 0, sizeof( xively_data ) ); wiced_rtos_init_mutex( &xively_data.mutex ); /* Initialise temperature set point. Set point is shared among multiple threads; therefore a mutex is required */ wiced_rtos_init_mutex( &setpoint.mutex ); setpoint.temperature = DEFAULT_SETPOINT; adjust_setpoint_led_brightness( ); /* Initialise Thermistor */ wiced_adc_init( THERMISTOR_ADC, 5 ); /* Initialise Set Point Control keypad and assigns it's callback function to run on hardware_io_worker_thread's context */ gpio_keypad_enable( &setpoint_control_keypad, WICED_HARDWARE_IO_WORKER_THREAD, setpoint_control_keypad_handler, 250, 2, setpoint_control_key_list ); /* Disable roaming to other access points */ wiced_wifi_set_roam_trigger( -99 ); /* -99dBm ie. extremely low signal level */ /* Bringup the network interface */ wiced_network_up( WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL ); /* Timestamp is needed for sending data to Xively. * Start automatic time synchronisation and synchronise once every day. */ sntp_start_auto_time_sync( 1 * DAYS ); wiced_dct_read_lock( (void**) &dct, WICED_FALSE, DCT_APP_SECTION, 0, sizeof(user_dct_data_t) ); sample_interval = dct->sample_interval; wiced_dct_read_unlock( dct, WICED_FALSE ); /* Setup timed events that will take a measurement & activate Xively thread to send measurements to Xively */ wiced_rtos_register_timed_event( &xively_timed_event, WICED_NETWORKING_WORKER_THREAD, send_data_to_xively, 10 * SECONDS, 0 ); wiced_rtos_register_timed_event( &temperature_timed_event, WICED_HARDWARE_IO_WORKER_THREAD, take_temperature_reading, sample_interval, 0 ); /* Start web server to display current temperature & setpoint */ wiced_http_server_start( &http_server, 80, max_sockets, web_pages, WICED_STA_INTERFACE, DEFAULT_URL_PROCESSOR_STACK_SIZE ); /* Advertise webpage services using Gedday */ gedday_init( WICED_STA_INTERFACE, "wiced-temp-controller" ); gedday_add_service( "temp_control web server", "_http._tcp.local", 80, 300, "" ); }
/** * Clears the WLAN credentials by erasing the DCT data and taking down the STA * network interface. * @return */ int wlan_clear_credentials() { // write to DCT credentials // clear current IP platform_dct_wifi_config_t* wifi_config = NULL; wiced_result_t result = wiced_dct_read_lock( (void**) &wifi_config, WICED_TRUE, DCT_WIFI_CONFIG_SECTION, 0, sizeof(*wifi_config)); if (!result) { memset(wifi_config->stored_ap_list, 0, sizeof(wifi_config->stored_ap_list)); result = wiced_dct_write( (const void*) wifi_config, DCT_WIFI_CONFIG_SECTION, 0, sizeof(*wifi_config) ); wiced_dct_read_unlock(wifi_config, WICED_TRUE); } return result; }
/** * Initializes the DCT area if required. * @return */ wiced_result_t wlan_initialize_dct() { // find the next available slot, or use the first platform_dct_wifi_config_t* wifi_config = NULL; wiced_result_t result = wiced_dct_read_lock( (void**) &wifi_config, WICED_TRUE, DCT_WIFI_CONFIG_SECTION, 0, sizeof(*wifi_config)); if (result==WICED_SUCCESS) { // the storage may not have been initialized, so device_configured will be 0xFF if (initialize_dct(wifi_config)) result = wiced_dct_write( (const void*) wifi_config, DCT_WIFI_CONFIG_SECTION, 0, sizeof(*wifi_config) ); wiced_dct_read_unlock(wifi_config, WICED_TRUE); } return result; }
static wiced_result_t device_init() { smart_home_app_dct_t* dct_app; wiced_result_t res; if( wiced_dct_read_lock( (void**) &dct_app, WICED_TRUE, DCT_APP_SECTION, 0, sizeof( *dct_app ) ) != WICED_SUCCESS) { return WICED_ERROR; } this_dev.configured = dct_app->device_configured; this_dev.dev_type = dct_app->dev_type; this_dev.dev_index = dct_app->dev_index; strncpy(this_dev.dev_name, dct_app->dev_name, sizeof(this_dev.dev_name)); WPRINT_APP_INFO(("this_dev.dev_name is %s\n", this_dev.dev_name)); wiced_dct_read_unlock( dct_app, WICED_TRUE ); if(this_dev.configured == WICED_FALSE) { return WICED_ERROR; } if(this_dev.dev_type == DEV_TYPE_MASTER) { this_dev.parse_socket_msg_fun = master_parse_socket_msg; uart_receive_enable(master_process_uart_msg); user_receive_enable(); }else if(this_dev.dev_type == DEV_TYPE_LIGHT || this_dev.dev_type == DEV_TYPE_CURTAIN) { if(this_dev.dev_type == DEV_TYPE_LIGHT) { res = light_dev_init(&this_dev.specific.light_dev, WICED_HARDWARE_IO_WORKER_THREAD, report_light_status); if(res != WICED_SUCCESS) { return WICED_ERROR; } this_dev.parse_socket_msg_fun = light_parse_socket_msg; //this_dev.device_keypad_handler = light_keypad_handler; uart_keypad_enable( &device_keypad, WICED_HARDWARE_IO_WORKER_THREAD, light_keypad_handler, 3000); } else if(this_dev.dev_type == DEV_TYPE_CURTAIN) { res = curtain_init(&this_dev.specific.curtain, WICED_HARDWARE_IO_WORKER_THREAD, report_curtain_pos); if( res != WICED_SUCCESS) { return WICED_ERROR; } this_dev.parse_socket_msg_fun = curtain_parse_socket_msg; //this_dev.device_keypad_handler = curtain_keypad_handler; uart_keypad_enable( &device_keypad, WICED_HARDWARE_IO_WORKER_THREAD, curtain_keypad_handler, 3000); } pre_receive_enable(); } next_receive_enable(); return WICED_SUCCESS; }
wiced_result_t add_wiced_wifi_credentials(const char *ssid, uint16_t ssidLen, const char *password, uint16_t passwordLen, wiced_security_t security, unsigned channel) { platform_dct_wifi_config_t* wifi_config = NULL; wiced_result_t result = wiced_dct_read_lock( (void**) &wifi_config, WICED_TRUE, DCT_WIFI_CONFIG_SECTION, 0, sizeof(*wifi_config)); if (!result) { // the storage may not have been initialized, so device_configured will be 0xFF initialize_dct(wifi_config); // shuffle all slots along memmove(wifi_config->stored_ap_list+1, wifi_config->stored_ap_list, sizeof(wiced_config_ap_entry_t)*(CONFIG_AP_LIST_SIZE-1)); const int empty = 0; wiced_config_ap_entry_t& entry = wifi_config->stored_ap_list[empty]; memset(&entry, 0, sizeof(entry)); passwordLen = std::min(passwordLen, uint16_t(64)); ssidLen = std::min(ssidLen, uint16_t(32)); memcpy(entry.details.SSID.value, ssid, ssidLen); entry.details.SSID.length = ssidLen; if (security==WICED_SECURITY_WEP_PSK && passwordLen>1 && password[0]>4) { // convert from hex to binary entry.security_key_length = hex_decode((uint8_t*)entry.security_key, sizeof(entry.security_key), password); } else { memcpy(entry.security_key, password, passwordLen); entry.security_key_length = passwordLen; } entry.details.security = security; entry.details.channel = channel; result = wiced_dct_write( (const void*) wifi_config, DCT_WIFI_CONFIG_SECTION, 0, sizeof(*wifi_config) ); if (!result) wifi_creds_changed = true; wiced_dct_read_unlock(wifi_config, WICED_TRUE); } return result; }
/* * Sends temperature readings to Xively */ static wiced_result_t send_data_to_xively( void* arg ) { xively_feed_t sensor_feed; xively_datastream_t stream; uint16_t data_points; wiced_result_t result; /* Setup sensor feed info */ memset( &sensor_feed, 0, sizeof( sensor_feed ) ); user_dct_data_t* dct; wiced_dct_read_lock( (void**) &dct, WICED_FALSE, DCT_APP_SECTION, 0, sizeof(user_dct_data_t) ); if ( ( dct->xively_feed_id[0] == '\x00' ) || ( dct->xively_channel_id[0] == '\x00' ) || ( dct->xively_api_key[0] == '\x00' ) ) { WPRINT_APP_INFO(("Xively feed details not in DCT\n")); wiced_dct_read_unlock( dct, WICED_FALSE ); return WICED_ERROR; } sensor_feed.id = dct->xively_feed_id; sensor_feed.api_key = dct->xively_api_key; /* Open Xively feed */ result = xively_open_feed( &sensor_feed ); if ( result != WICED_SUCCESS ) { WPRINT_APP_INFO(("Failed to open Xively feed\n")); wiced_dct_read_unlock( dct, WICED_FALSE ); return result; } /* Get the amount of temperature readings to send (required for creating Xively datastream). Mutex protection required. */ wiced_rtos_lock_mutex( &xively_data.mutex ); data_points = ( xively_data.temperature_reading_index - xively_data.last_sent_temperature_index ) & ( MAX_TEMPERATURE_READINGS - 1 ); wiced_rtos_unlock_mutex( &xively_data.mutex ); /* Create Xively datastream */ result = xively_create_datastream( &sensor_feed, &stream, dct->xively_channel_id, 4, data_points ); wiced_dct_read_unlock( dct, WICED_FALSE ); if ( result != WICED_SUCCESS ) { xively_close_feed( &sensor_feed ); WPRINT_APP_INFO( ("Failed to init Xively datastream\n") ); return result; } /* Write data to TCP stream (compensating for looping around the end of the buffer). Mutex protection required */ wiced_rtos_lock_mutex( &xively_data.mutex ); while ( data_points > 0 ) { xively_write_datapoint( &stream, (const uint8_t*) &xively_data.temperature_readings[ xively_data.last_sent_temperature_index ].sample, &xively_data.temperature_readings[ xively_data.last_sent_temperature_index ].timestamp ); xively_data.last_sent_temperature_index = ( xively_data.last_sent_temperature_index + 1 ) & ( MAX_TEMPERATURE_READINGS - 1 ); data_points--; } wiced_rtos_unlock_mutex( &xively_data.mutex ); /* Flush (send) temperature readings in the TCP stream to Xively */ if ( xively_flush_datastream( &stream ) == WICED_SUCCESS ) { WPRINT_APP_INFO( ("data sent!\n") ); } else { WPRINT_APP_INFO( ("Failed to send data!\n") ); } /* Close Xively */ return xively_close_feed( &sensor_feed ); }
int start_smartconfig(void) { wiced_result_t result; wiced_config_ap_entry_t* dct_ap_entry; WPRINT_APP_INFO(("smart\n")); result = 1; /* enable protocols */ easy_setup_enable_cooee(); /* broadcom cooee */ easy_setup_enable_neeze(); /* broadcom neeze */ //easy_setup_enable_akiss(); /* wechat akiss */ //easy_setup_enable_changhong(); /* changhong */ /* set cooee key */ //cooee_set_key("1111111111111111"); /* set airkiss key */ //akiss_set_key("1111111111111111"); /* start easy setup */ if (easy_setup_start() != WICED_SUCCESS) { WPRINT_APP_INFO(("easy setup failed.\r\n")); } else { WPRINT_APP_INFO(("easy setup done.\r\n")); } int up_tries = 3; while (up_tries--) { result = wiced_network_up( WICED_STA_INTERFACE,\ WICED_USE_EXTERNAL_DHCP_SERVER, NULL ); if (result == WICED_SUCCESS) { break; } else { WPRINT_APP_INFO(("Network up failed, try again (%d left)\r\n", up_tries)); } } if ( result != WICED_SUCCESS ) { wiced_dct_read_lock( (void**) &dct_ap_entry, WICED_TRUE, \ DCT_WIFI_CONFIG_SECTION, offsetof(platform_dct_wifi_config_t, stored_ap_list),\ sizeof(wiced_config_ap_entry_t) ); if (dct_ap_entry->details.security == WICED_SECURITY_WEP_PSK ) // Now try shared instead of open authentication { dct_ap_entry->details.security = WICED_SECURITY_WEP_SHARED; wiced_dct_write( dct_ap_entry, DCT_WIFI_CONFIG_SECTION, \ offsetof(platform_dct_wifi_config_t, stored_ap_list),\ sizeof(wiced_config_ap_entry_t) ); result = wiced_network_up( WICED_STA_INTERFACE,\ WICED_USE_EXTERNAL_DHCP_SERVER, NULL ); if ( result != WICED_SUCCESS ) // Restore old value { //wiced_dct_read_lock( (void**) &dct_ap_entry, WICED_TRUE, DCT_WIFI_CONFIG_SECTION, offsetof(platform_dct_wifi_config_t, stored_ap_list), sizeof(wiced_config_ap_entry_t) ); dct_ap_entry->details.security = WICED_SECURITY_WEP_PSK; wiced_dct_write( dct_ap_entry, DCT_WIFI_CONFIG_SECTION, \ offsetof(platform_dct_wifi_config_t, stored_ap_list),\ sizeof(wiced_config_ap_entry_t) ); } } wiced_dct_read_unlock( dct_ap_entry, WICED_TRUE ); } if ( result != WICED_SUCCESS ) { WPRINT_APP_INFO( ("Network up failed\n") ); } else { WPRINT_APP_INFO( ("Network up success\n") ); } return result; }
static wiced_result_t set_wifi_security( your_security_t security_type ) { wiced_result_t result; char security_key[MAX_KEY_LENGTH]; uint8_t key_length = 0; wiced_security_t wiced_security_type; platform_dct_wifi_config_t* wifi_config_dct_local; /* Check if the security type permits us to simply use the default AP passphrase */ if ( (security_type != YOUR_SECURITY_OPEN ) && ( security_type != YOUR_SECURITY_WEP40_PSK ) && (security_type != YOUR_SECURITY_WEP104_PSK ) ) { memcpy(security_key, YOUR_AP_PASSPHRASE, sizeof( YOUR_AP_PASSPHRASE )); key_length = strlen((char*)&security_key); } switch ( security_type ) { case YOUR_SECURITY_OPEN: memset( security_key, 0, sizeof( security_key ) ); wiced_security_type = WICED_SECURITY_OPEN; break; case YOUR_SECURITY_WEP40_PSK: wiced_security_type = WICED_SECURITY_WEP_PSK; key_length = strlen( YOUR_WEP40_KEY ); format_wep_keys( security_key, YOUR_WEP40_KEY, &key_length, WEP_KEY_TYPE ); break; case YOUR_SECURITY_WEP104_PSK: wiced_security_type = WICED_SECURITY_WEP_PSK; key_length = strlen( YOUR_WEP104_KEY ); format_wep_keys( security_key, YOUR_WEP104_KEY, &key_length, WEP_KEY_TYPE ); break; case YOUR_SECURITY_WPA_TKIP_PSK: wiced_security_type = WICED_SECURITY_WPA_TKIP_PSK; break; case YOUR_SECURITY_WPA_AES_PSK: wiced_security_type = WICED_SECURITY_WPA_AES_PSK; break; case YOUR_SECURITY_WPA2_AES_PSK: wiced_security_type = WICED_SECURITY_WPA2_AES_PSK; break; case YOUR_SECURITY_WPA2_TKIP_PSK: wiced_security_type = WICED_SECURITY_WPA2_TKIP_PSK; break; case YOUR_SECURITY_WPA2_MIXED_PSK: wiced_security_type = WICED_SECURITY_WPA2_MIXED_PSK; break; default: WPRINT_APP_INFO(( "Unrecognised security type\r\n" )); return WICED_ERROR; break; } /* Read the Wi-Fi config from the DCT */ result = wiced_dct_read_lock( (void**) &wifi_config_dct_local, WICED_TRUE, DCT_WIFI_CONFIG_SECTION, 0, sizeof( platform_dct_wifi_config_t ) ); if ( result != WICED_SUCCESS ) { return result; } /* Write the new security settings into the config */ wifi_config_dct_local->stored_ap_list[0].details.SSID.length = strlen(YOUR_SSID); strncpy((char*)&wifi_config_dct_local->stored_ap_list[0].details.SSID.value, YOUR_SSID, MAX_SSID_LENGTH); wifi_config_dct_local->stored_ap_list[0].details.security = wiced_security_type; memcpy((char*)wifi_config_dct_local->stored_ap_list[0].security_key, (char*)security_key, MAX_PASSPHRASE_LENGTH); wifi_config_dct_local->stored_ap_list[0].security_key_length = key_length; /* Write the modified config back into the DCT */ result = wiced_dct_write ( (const void*)wifi_config_dct_local, DCT_WIFI_CONFIG_SECTION, 0, sizeof (platform_dct_wifi_config_t) ); wiced_dct_read_unlock( (void*)wifi_config_dct_local, WICED_TRUE ); if ( result != WICED_SUCCESS ) { return result; } return WICED_SUCCESS; }
void application_start(void) { const your_security_t your_security_type = YOUR_SECURITY_WEP40_PSK; platform_dct_wifi_config_t* wifi_config_dct_local; /* Initialise the WICED device */ wiced_init(); /* Configure security for the device and join the AP */ if ( set_wifi_security( your_security_type ) != WICED_SUCCESS ) { WPRINT_APP_INFO( ( "Failed to update DCT with security type\r\n" ) ); return; } /* Try join the network */ if ( wiced_network_up( WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL ) != WICED_SUCCESS ) { /* Check if we were using WEP. It is not possible to tell WEP Open from WEP Shared in a scan so we try Open first and Shared second */ if ( your_security_type == YOUR_SECURITY_WEP40_PSK || your_security_type == YOUR_SECURITY_WEP104_PSK ) { WPRINT_APP_INFO( ("WEP with open authentication failed, trying WEP with shared authentication...\r\n") ); /* Modify the Wi-Fi config to use Shared WEP */ wiced_dct_read_lock( (void**) &wifi_config_dct_local, WICED_TRUE, DCT_WIFI_CONFIG_SECTION, 0, sizeof( platform_dct_wifi_config_t ) ); wifi_config_dct_local->stored_ap_list[0].details.security = WICED_SECURITY_WEP_SHARED; wiced_dct_write ( (const void*)wifi_config_dct_local, DCT_WIFI_CONFIG_SECTION, 0, sizeof (platform_dct_wifi_config_t) ); wiced_dct_read_unlock( (void*)wifi_config_dct_local, WICED_TRUE ); /* Try join the network again */ if ( wiced_network_up( WICED_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL ) != WICED_SUCCESS ) { WPRINT_APP_INFO( ("WEP with Shared authentication failed as well\r\n") ); /* Restore the Wi-Fi config back to the default Open WEP */ wiced_dct_read_lock( (void**) &wifi_config_dct_local, WICED_TRUE, DCT_WIFI_CONFIG_SECTION, 0, sizeof( platform_dct_wifi_config_t ) ); wifi_config_dct_local->stored_ap_list[0].details.security = WICED_SECURITY_WEP_PSK; wiced_dct_write ( (const void*)wifi_config_dct_local, DCT_WIFI_CONFIG_SECTION, 0, sizeof (platform_dct_wifi_config_t) ); wiced_dct_read_unlock( (void*)wifi_config_dct_local, WICED_TRUE ); /* Nothing more we can do.. Give up */ WPRINT_APP_INFO(( "Unable to join AP\r\n" )); return; } } else { WPRINT_APP_INFO(( "Unable to join AP\r\n" )); return; } } while (1) { /* Send an ICMP ping to the gateway */ send_ping( ); /* Wait between pings */ wiced_rtos_delay_milliseconds( PING_INTERVAL ); } }
static void tcp_server_thread_main(uint32_t arg) { wiced_result_t result; // 5 Connections maximum at a time platform_dct_security_t* dct_security = NULL; wiced_tls_identity_t tls_identity; #if 0 wiced_tcp_server_start(&tcp_server,INTERFACE,TCP_SERVER_LISTEN_PORT,5, client_connected_callback, received_data_callback, client_disconnected_callback, NULL ); /* 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 WPRINT_APP_INFO(("Init identity succeeded\n")); result = wiced_tcp_server_enable_tls (&tcp_server, &tls_identity); if(result != WICED_SUCCESS) { WPRINT_APP_INFO(( "Failed to enable TLS. Error = [%d]\n", result )); return; } else WPRINT_APP_INFO(("Enable TLS succeeded\n")); #endif WPRINT_APP_INFO(("IP\t\tPort\tC\tDEVICE\tREGID\tVALUE\tDBSIZE\n")); WPRINT_APP_INFO(("----------------------------------------------------------------------\n")); char receiveChar; uint32_t expected_data_size=1; while(1) { wiced_uart_receive_bytes( STDIO_UART, &receiveChar, &expected_data_size, WICED_NEVER_TIMEOUT ); switch(receiveChar) { case 'p': printStatus(); break; case '?': WPRINT_APP_INFO(("p: print status of server sockets\n")); break; } } }
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 } }