TA的每日心情 | 衰 2016-12-7 16:21 |
---|
签到天数: 2 天 连续签到: 1 天 [LV.1]初来乍到
|
一、目标
之后不管是要TCP通信,UDP通信,得先连上一个WIFI,获取个IP。由于AIRKISS没有调好,那就连接一个固定的WIFI吧。
连上WIFI,获取IP。
二、思路整理
新建一个事件标志
——》按下按键进行扫描
——》扫描到指定的SSID后记录下获取的信息(连接WIFI的时候使用)
——》发送事件停止扫描
——》利用按键线程发送一个连接事件
——》连接WIFI,获取IP
三、代码搞起
/* * Copyright 2015, Broadcom Corporation * All Rights Reserved. * * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Broadcom Corporation; * the contents of this file may not be disclosed to third parties, copied * or duplicated in any form, in whole or in part, without the prior * written permission of Broadcom Corporation. *//** @file * * Scan Application * * Features demonstrated * - WICED scan API * * This application snippet regularly scans for nearby Wi-Fi access points * * Application Instructions * Connect a PC terminal to the serial port of the WICED Eval board, * then build and download the application as described in the WICED * Quick Start Guide * * Each time the application scans, a list of Wi-Fi access points in * range is printed to the UART * */#include <stdlib.h>#include "wiced.h"#include "platform.h"/****************************************************** * Macros ******************************************************//****************************************************** * Constants ******************************************************/#define DELAY_BETWEEN_SCANS (5000)/****************************************************** * Enumerations ******************************************************//****************************************************** * Type Definitions ******************************************************/typedef struct{ wiced_bool_t stWifiConfigFlag; uint8_t stTcpStatus;}wiced_network_info_t;/****************************************************** * Structures ******************************************************/wiced_scan_result_t stWifiInfo;wiced_network_info_t stNetworkInfo;/****************************************************** * Static Function Declarations ******************************************************/wiced_result_t scan_result_handler( wiced_scan_handler_result_t* malloced_scan_result );/****************************************************** * Variable Definitions ******************************************************/static int record_count;static wiced_time_t scan_start_time;wiced_event_flags_t stMainLoopEvent;#define WIFI_SSID_SCAN_OK_EVENT 0X00000001#define WIFI_NETWORK_CONNECT_EVENT 0X00000002#define SSID_USER "lifesense-network"#define SSID_USER_KEY "Life86358868"wiced_semaphore_t stIcylinkerButtonSem;wiced_thread_t stIcylinkerButtonThread;#define BUTTON_PRIORITY (9)#define BUTTON_STACK_SIZE (1*1024)void vButtonFunction(void *);/****************************************************** * Function Definitions ******************************************************/extern wiced_result_t wiced_join_ap_specific( wiced_ap_info_t* details, uint8_t security_key_length, const char security_key[ 64 ] );extern wiced_result_t wiced_ip_up( wiced_interface_t interface, wiced_network_config_t config, const wiced_ip_setting_t* ip_settings );void application_start( ){ int8_t ret; uint32_t event_value; wiced_ap_info_t lifesense_info; wiced_init( ); memset(&stWifiInfo, 0, sizeof(wiced_scan_result_t)); memset(&stNetworkInfo, 0, sizeof(wiced_network_info_t)); wiced_rtos_init_event_flags(&stMainLoopEvent); WPRINT_APP_INFO( ("----------------------------------------------------------------------------------------------\n" ) ); wiced_gpio_init(WICED_GPIO_2, INPUT_PULL_UP); /* * wiced_result_t wiced_gpio_init( wiced_gpio_t gpio, wiced_gpio_config_t configuration ); * wiced_result_t wiced_rtos_create_thread( wiced_thread_t* thread, uint8_t priority, const char* name, wiced_thread_function_t function, uint32_t stack_size, void* arg ) * */ wiced_rtos_create_thread(&stIcylinkerButtonThread, BUTTON_PRIORITY, "BUTTON", vButtonFunction, BUTTON_STACK_SIZE, NULL); wiced_rtos_init_semaphore(&stIcylinkerButtonSem); WPRINT_APP_INFO(("https://www.eeboard.com/bbs/forum.php\n")); WPRINT_APP_INFO(("user: icylinker\n")); WPRINT_APP_INFO(("board: BCM943364WCD1\n")); WPRINT_APP_INFO( ("----------------------------------------------------------------------------------------------\n" ) ); wiced_rtos_get_semaphore(&stIcylinkerButtonSem, NEVER_TIMEOUT); record_count = 0; WPRINT_APP_INFO( ( "Waiting for scan results...\n" ) ); WPRINT_APP_INFO( (" # Type BSSID RSSI Rate Chan Security SSID\n" ) ); WPRINT_APP_INFO( ("----------------------------------------------------------------------------------------------\n" ) ); wiced_time_get_time(&scan_start_time); wiced_wifi_scan_networks(scan_result_handler, NULL ); while(1) { /* wiced_result_t wiced_rtos_wait_for_event_flags( wiced_event_flags_t* event_flags, uint32_t flags_to_wait_for, uint32_t* flags_set, wiced_bool_t clear_set_flags, wiced_event_flags_wait_option_t wait_option, uint32_t timeout_ms ) */ ret = wiced_rtos_wait_for_event_flags(&stMainLoopEvent, 0xFFFFFFFFL, &event_value, WICED_TRUE, WAIT_FOR_ANY_EVENT, 18000 ); if(WICED_SUCCESS == ret) { if(WIFI_SSID_SCAN_OK_EVENT & event_value) { wwd_wifi_abort_scan(); event_value &= ~WIFI_SSID_SCAN_OK_EVENT; } if(WIFI_NETWORK_CONNECT_EVENT & event_value) { WPRINT_APP_INFO(("CONNECT:\n")); print_scan_result(&stWifiInfo); event_value &= ~WIFI_NETWORK_CONNECT_EVENT; memcpy(&lifesense_info, &stWifiInfo, sizeof(wiced_ap_info_t) - sizeof(struct wiced_ap_info*)); ret = wiced_join_ap_specific( &lifesense_info, strlen(SSID_USER_KEY), SSID_USER_KEY); if(ret== WICED_SUCCESS) { ret = wiced_ip_up(WWD_STA_INTERFACE, WICED_USE_EXTERNAL_DHCP_SERVER, NULL); if(ret == WICED_SUCCESS) { ; } else { stNetworkInfo.stTcpStatus = 0; } } else { stNetworkInfo.stTcpStatus = 0; } } } else wiced_rtos_delay_milliseconds(DELAY_BETWEEN_SCANS); }}/* * Callback function to handle scan results */wiced_result_t scan_result_handler( wiced_scan_handler_result_t* malloced_scan_result ){ int8_t ret; if ( malloced_scan_result != NULL ) { malloc_transfer_to_curr_thread( malloced_scan_result ); if ( malloced_scan_result->status == WICED_SCAN_INCOMPLETE ) { wiced_scan_result_t* record = &malloced_scan_result->ap_details;#if 1 if(record->SSID.length >= strlen(SSID_USER)) { ret = memcmp(record->SSID.value, SSID_USER, strlen(SSID_USER)); if(ret == 0) { print_scan_result(record); memcpy(&stWifiInfo, record, sizeof(wiced_scan_result_t)); stNetworkInfo.stWifiConfigFlag = WICED_TRUE; wiced_rtos_set_event_flags(&stMainLoopEvent, WIFI_SSID_SCAN_OK_EVENT); } }#endif } else { wiced_time_t scan_end_time; wiced_time_get_time(&scan_end_time); WPRINT_APP_INFO( ("\nScan complete in %lu milliseconds\n", (unsigned long )(scan_end_time - scan_start_time) ) ); } free( malloced_scan_result ); malloced_scan_result = NULL; } return WICED_SUCCESS;}void vButtonFunction(void *arg){ uint8_t button_status = 0; while(1) { switch(button_status) { case 0: if(!wiced_gpio_input_get(WICED_GPIO_2)) { button_status = 1; } break; case 1: wiced_rtos_delay_milliseconds(100); button_status = 2; break; case 2: if(!wiced_gpio_input_get(WICED_GPIO_2)) { button_status = 3; } else button_status = 0; break; case 3: wiced_rtos_set_semaphore(&stIcylinkerButtonSem); button_status = 4; break; case 4: if(wiced_gpio_input_get(WICED_GPIO_2)) { button_status = 0; } break; default: break; }/* ****************************************************************/ if(stNetworkInfo.stWifiConfigFlag && stNetworkInfo.stTcpStatus == 0) { wiced_rtos_set_event_flags(&stMainLoopEvent, WIFI_NETWORK_CONNECT_EVENT); stNetworkInfo.stTcpStatus = 1; } }}
四、测试结果
|
|