int wifi_start_ap( char *ssid, rtw_security_t security_type, char *password, int ssid_len, int password_len, int channel) { const char *ifname = WLAN0_NAME; int ret = 0; if(wifi_mode == RTW_MODE_STA_AP) { ifname = WLAN1_NAME; } if(is_promisc_enabled()) promisc_set(0, NULL, 0); wifi_reg_event_handler(WIFI_EVENT_STA_ASSOC, wifi_ap_sta_assoc_hdl, NULL); wifi_reg_event_handler(WIFI_EVENT_STA_DISASSOC, wifi_ap_sta_disassoc_hdl, NULL); ret = wext_set_mode(ifname, IW_MODE_MASTER); if(ret < 0) goto exit; ret = wext_set_channel(ifname, channel); //Set channel before starting ap if(ret < 0) goto exit; switch(security_type) { case RTW_SECURITY_OPEN: break; case RTW_SECURITY_WPA2_AES_PSK: ret = wext_set_auth_param(ifname, IW_AUTH_80211_AUTH_ALG, IW_AUTH_ALG_OPEN_SYSTEM); if(ret == 0) ret = wext_set_key_ext(ifname, IW_ENCODE_ALG_CCMP, NULL, 0, 0, 0, 0, NULL, 0); if(ret == 0) ret = wext_set_passphrase(ifname, (u8*)password, password_len); break; default: ret = -1; printf("\n\rWIFICONF: security type is not supported"); break; } if(ret < 0) goto exit; #if 0 //hide beacon SSID, add by serena_li u8 value = 1; // 1: ssid = NUll, 2: ssid = ......, other: ssid = ssid ret = set_hidden_ssid(ifname, value); if(ret < 0) goto exit; #endif ret = wext_set_ap_ssid(ifname, (u8*)ssid, ssid_len); #ifdef CONFIG_WPS //construct WPS IE #if CONFIG_WPS_AP wpas_wps_init(ifname); #endif #endif exit: return ret; }
//int rtk_wifi_scan(char *buf, int buf_len, xSemaphoreHandle * semaphore) int wifi_scan(rtw_scan_type_t scan_type, rtw_bss_type_t bss_type, void* result_ptr) { int ret; scan_buf_arg * pscan_buf; u16 flags = scan_type | (bss_type << 8); if(result_ptr != NULL){ pscan_buf = (scan_buf_arg *)result_ptr; ret = wext_set_scan(WLAN0_NAME, (char*)pscan_buf->buf, pscan_buf->buf_len, flags); }else{ wifi_reg_event_handler(WIFI_EVENT_SCAN_RESULT_REPORT, wifi_scan_each_report_hdl, NULL); wifi_reg_event_handler(WIFI_EVENT_SCAN_DONE, wifi_scan_done_hdl, NULL); ret = wext_set_scan(WLAN0_NAME, NULL, 0, flags); } if(ret == 0) { if(result_ptr != NULL){ ret = wext_get_scan(WLAN0_NAME, pscan_buf->buf, pscan_buf->buf_len); } } return ret; }
int wifi_connect_bssid( unsigned char bssid[ETH_ALEN], char *ssid, rtw_security_t security_type, char *password, int bssid_len, int ssid_len, int password_len, int key_id, void *semaphore) { xSemaphoreHandle join_semaphore; rtw_result_t result = RTW_SUCCESS; #if CONFIG_JD_SMART send_wifi_network_status(2);//connecting with ap #endif rtw_join_status = 0;//clear for last connect status error_flag = 0 ;//clear for last connect status internal_join_result_t *join_result = (internal_join_result_t *)rtw_zmalloc(sizeof(internal_join_result_t)); if(!join_result) { return RTW_NOMEM; } if(ssid_len && ssid){ join_result->network_info.ssid.len = ssid_len > 32 ? 32 : ssid_len; rtw_memcpy(join_result->network_info.ssid.val, ssid, ssid_len); } rtw_memcpy(join_result->network_info.bssid.octet, bssid, bssid_len); if ( ( ( ( password_len > RTW_MAX_PSK_LEN ) || ( password_len < RTW_MIN_PSK_LEN ) ) && ( ( security_type == RTW_SECURITY_WPA_TKIP_PSK ) || ( security_type == RTW_SECURITY_WPA_AES_PSK ) || ( security_type == RTW_SECURITY_WPA2_AES_PSK ) || ( security_type == RTW_SECURITY_WPA2_TKIP_PSK ) || ( security_type == RTW_SECURITY_WPA2_MIXED_PSK ) ) )|| (((password_len != 5)&& (password_len != 13))&& ((security_type == RTW_SECURITY_WEP_PSK)|| (security_type ==RTW_SECURITY_WEP_SHARED ) ))) { return RTW_INVALID_KEY; } join_result->network_info.password_len = password_len; if(password_len) { /* add \0 to the end */ join_result->network_info.password = rtw_zmalloc(password_len + 1); if(!join_result->network_info.password) { return RTW_NOMEM; } rtw_memcpy(join_result->network_info.password, password, password_len); } join_result->network_info.security_type = security_type; join_result->network_info.key_id = key_id; if(semaphore == NULL) { rtw_init_sema( &join_result->join_sema, 0 ); if(!join_result->join_sema){ return RTW_NORESOURCE; } join_semaphore = join_result->join_sema; } else { join_result->join_sema = semaphore; } wifi_reg_event_handler(WIFI_EVENT_NO_NETWORK,wifi_no_network_hdl,NULL); wifi_reg_event_handler(WIFI_EVENT_CONNECT, wifi_connected_hdl, NULL); wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, wifi_disconn_hdl, NULL); wifi_reg_event_handler(WIFI_EVENT_FOURWAY_HANDSHAKE_DONE, wifi_handshake_done_hdl, NULL); wifi_connect_bssid_local(&join_result->network_info); join_user_data = join_result; if(semaphore == NULL) { if(rtw_down_timeout_sema( &join_result->join_sema, RTW_JOIN_TIMEOUT ) == RTW_FALSE) { printf("RTW API: Join bss timeout\r\n"); if(password_len) { rtw_free(join_result->network_info.password); } rtw_free((u8*)join_result); rtw_free_sema( &join_semaphore); result = RTW_TIMEOUT; goto error; } else { rtw_free_sema( &join_semaphore ); if(join_result->network_info.password_len) { rtw_free(join_result->network_info.password); } rtw_free((u8*)join_result); if(wifi_is_connected_to_ap( ) != RTW_SUCCESS) { result = RTW_ERROR; goto error; } } } result = RTW_SUCCESS; #if CONFIG_EXAMPLE_WLAN_FAST_CONNECT || CONFIG_JD_SMART restore_wifi_info_to_flash(); #endif error: join_user_data = NULL; wifi_unreg_event_handler(WIFI_EVENT_CONNECT, wifi_connected_hdl); wifi_unreg_event_handler(WIFI_EVENT_NO_NETWORK,wifi_no_network_hdl); wifi_unreg_event_handler(WIFI_EVENT_FOURWAY_HANDSHAKE_DONE, wifi_handshake_done_hdl); return result; }
//----------------------------------------------------------------------------// int wifi_connect( char *ssid, rtw_security_t security_type, char *password, int ssid_len, int password_len, int key_id, void *semaphore) { xSemaphoreHandle join_semaphore; rtw_result_t result = RTW_SUCCESS; u8 wep_hex = 0; u8 wep_pwd[14] = {0}; #if CONFIG_JD_SMART send_wifi_network_status(2);//connecting with ap #endif rtw_join_status = 0;//clear for last connect status error_flag = RTW_UNKNOWN ;//clear for last connect status internal_join_result_t *join_result = (internal_join_result_t *)rtw_zmalloc(sizeof(internal_join_result_t)); if(!join_result) { return RTW_NOMEM; } join_result->network_info.ssid.len = ssid_len > 32 ? 32 : ssid_len; rtw_memcpy(join_result->network_info.ssid.val, ssid, ssid_len); if ( ( ( ( password_len > RTW_MAX_PSK_LEN ) || ( password_len < RTW_MIN_PSK_LEN ) ) && ( ( security_type == RTW_SECURITY_WPA_TKIP_PSK ) || ( security_type == RTW_SECURITY_WPA_AES_PSK ) || ( security_type == RTW_SECURITY_WPA2_AES_PSK ) || ( security_type == RTW_SECURITY_WPA2_TKIP_PSK ) || ( security_type == RTW_SECURITY_WPA2_MIXED_PSK ) ) )) { error_flag = RTW_WRONG_PASSWORD; return RTW_INVALID_KEY; } if ((security_type == RTW_SECURITY_WEP_PSK)|| (security_type ==RTW_SECURITY_WEP_SHARED)) { if ((password_len != 5) && (password_len != 13) && (password_len != 10)&& (password_len != 26)) { error_flag = RTW_WRONG_PASSWORD; return RTW_INVALID_KEY; } else { if(password_len == 10) { u32 p[5]; u8 i = 0; sscanf((const char*)password, "%02x%02x%02x%02x%02x", &p[0], &p[1], &p[2], &p[3], &p[4]); for(i=0; i< 5; i++) wep_pwd[i] = (u8)p[i]; wep_pwd[5] = '\0'; password_len = 5; wep_hex = 1; } else if (password_len == 26) { u32 p[13]; u8 i = 0; sscanf((const char*)password, "%02x%02x%02x%02x%02x%02x%02x"\ "%02x%02x%02x%02x%02x%02x", &p[0], &p[1], &p[2], &p[3], &p[4],\ &p[5], &p[6], &p[7], &p[8], &p[9], &p[10], &p[11], &p[12]); for(i=0; i< 13; i++) wep_pwd[i] = (u8)p[i]; wep_pwd[13] = '\0'; password_len = 13; wep_hex = 1; } } } join_result->network_info.password_len = password_len; if(password_len) { /* add \0 to the end */ join_result->network_info.password = rtw_zmalloc(password_len + 1); if(!join_result->network_info.password) { return RTW_NOMEM; } if (0 == wep_hex) rtw_memcpy(join_result->network_info.password, password, password_len); else rtw_memcpy(join_result->network_info.password, wep_pwd, password_len); } join_result->network_info.security_type = security_type; join_result->network_info.key_id = key_id; if(semaphore == NULL) { rtw_init_sema( &join_result->join_sema, 0 ); if(!join_result->join_sema) { return RTW_NORESOURCE; } join_semaphore = join_result->join_sema; } else { join_result->join_sema = semaphore; } wifi_reg_event_handler(WIFI_EVENT_NO_NETWORK,wifi_no_network_hdl,NULL); wifi_reg_event_handler(WIFI_EVENT_CONNECT, wifi_connected_hdl, NULL); wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, wifi_disconn_hdl, NULL); wifi_reg_event_handler(WIFI_EVENT_FOURWAY_HANDSHAKE_DONE, wifi_handshake_done_hdl, NULL); wifi_connect_local(&join_result->network_info); join_user_data = join_result; if(semaphore == NULL) { if(rtw_down_timeout_sema( &join_result->join_sema, RTW_JOIN_TIMEOUT ) == RTW_FALSE) { printf("RTW API: Join bss timeout\r\n"); if(password_len) { rtw_free(join_result->network_info.password); } result = RTW_TIMEOUT; goto error; } else { if(join_result->network_info.password_len) { rtw_free(join_result->network_info.password); } if(wifi_is_connected_to_ap( ) != RTW_SUCCESS) { result = RTW_ERROR; goto error; } } } result = RTW_SUCCESS; #if CONFIG_EXAMPLE_WLAN_FAST_CONNECT || CONFIG_JD_SMART restore_wifi_info_to_flash(); #endif error: if(semaphore == NULL){ join_user_data = NULL; rtw_free((u8*)join_result); rtw_free_sema( &join_semaphore); wifi_unreg_event_handler(WIFI_EVENT_CONNECT, wifi_connected_hdl); wifi_unreg_event_handler(WIFI_EVENT_NO_NETWORK,wifi_no_network_hdl); wifi_unreg_event_handler(WIFI_EVENT_FOURWAY_HANDSHAKE_DONE, wifi_handshake_done_hdl); } return result; }
int wps_start(u16 wps_config, char *pin, u8 channel, char *ssid) { struct dev_credential dev_cred; rtw_network_info_t wifi = {0}; char target_ssid[64]; int is_overlap = -1; u32 start_time = rtw_get_current_time(); int ret = 0; memset(&dev_cred, 0, sizeof(struct dev_credential)); memset(target_ssid, 0, 64); if((wps_config != WPS_CONFIG_PUSHBUTTON) && (wps_config != WPS_CONFIG_DISPLAY) && (wps_config != WPS_CONFIG_KEYPAD)){ printf("\n\rWPS: Wps method(%d) is wrong. Not triger WPS.\n", wps_config); return -1; } config_method = wps_config; if(wps_config == WPS_CONFIG_DISPLAY || wps_config == WPS_CONFIG_KEYPAD) { if(pin) strcpy(wps_pin_code, pin); else{ printf("\n\rWPS: PIN is NULL. Not triger WPS.\n"); return -1; } } if(!ssid) { while (1) { unsigned int current_time = rtw_get_current_time(); if (rtw_systime_to_sec(current_time - start_time) < 120) { is_overlap = wps_find_out_triger_wps_AP(&target_ssid[0], wps_config); if ((is_overlap == 0) || (is_overlap > 0)) break; } else { printf("\r\nWPS: WPS Walking Time Out\n"); return 0; } } if (is_overlap > 0) { printf("\r\nWPS: WPS session overlap. Not triger WPS.\n"); return 0; } }else{ rtw_memcpy(target_ssid, ssid, strlen(ssid)); } if (queue_for_credential != NULL) { os_xqueue_delete(queue_for_credential); queue_for_credential = NULL; } queue_for_credential = os_xqueue_create(1, sizeof(struct dev_credential)); if(!queue_for_credential) return -1; wifi_reg_event_handler(WIFI_EVENT_WPS_FINISH, wpas_wps_notify_wps_finish_hdl, NULL); wifi_reg_event_handler(WIFI_EVENT_EAPOL_RECVD, wpas_wsc_eapol_recvd_hdl, NULL); wifi_set_wps_phase(ENABLE); ret = wps_connect_to_AP_by_open_system(target_ssid); if(ret < 0){ printf("\n\rWPS: WPS Fail!!\n"); goto exit; } os_xqueue_receive(queue_for_credential, &dev_cred, 120); if (dev_cred.ssid[0] != 0 && dev_cred.ssid_len <= 32) { wps_config_wifi_setting(&wifi, &dev_cred); wifi_set_wps_phase(DISABLE); wps_connect_to_AP_by_certificate(&wifi); goto exit1; } else { printf("\n\rWPS: WPS FAIL!!!\n"); } exit: wifi_set_wps_phase(DISABLE); exit1: if (queue_for_credential != NULL) { os_xqueue_delete(queue_for_credential); queue_for_credential = NULL; } wifi_unreg_event_handler(WIFI_EVENT_WPS_FINISH, wpas_wps_notify_wps_finish_hdl); wifi_unreg_event_handler(WIFI_EVENT_EAPOL_RECVD, wpas_wsc_eapol_recvd_hdl); return 0; }