static int get_connection_info_from_profile(rtw_security_t security_mode, rtw_network_info_t *wifi) { printf("\r\n======= Connection Information =======\n"); check_and_set_security_in_connection(security_mode, wifi); wifi->password = backup_sc_ctx->password; wifi->password_len = (int)strlen((char const *)backup_sc_ctx->password); if ( g_ssid_len > 0 && g_ssid_len < 33){ wifi->ssid.len = g_ssid_len; rtw_memcpy(wifi->ssid.val, g_ssid, wifi->ssid.len); }else if(strlen(backup_sc_ctx->ssid) > 0 && strlen(backup_sc_ctx->ssid) < 33){ wifi->ssid.len = strlen(backup_sc_ctx->ssid); rtw_memcpy(wifi->ssid.val, backup_sc_ctx->ssid, wifi->ssid.len); }else{ printf("\r\n SSID is NULL or SSID length is over 32!!"); return -1; } if(wifi->security_type == RTW_SECURITY_WEP_PSK) { if(wifi->password_len == 10) { u32 p[5]; u8 pwd[6], i = 0; sscanf((const char*)backup_sc_ctx->password, "%02x%02x%02x%02x%02x", &p[0], &p[1], &p[2], &p[3], &p[4]); for(i=0; i< 5; i++) pwd[i] = (u8)p[i]; pwd[5] = '\0'; memset(backup_sc_ctx->password, 0, 65); strcpy((char*)backup_sc_ctx->password, (char*)pwd); wifi->password_len = 5; }else if(wifi->password_len == 26){ u32 p[13]; u8 pwd[14], i = 0; sscanf((const char*)backup_sc_ctx->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++) pwd[i] = (u8)p[i]; pwd[13] = '\0'; memset(backup_sc_ctx->password, 0, 64); strcpy((char*)backup_sc_ctx->password, (char*)pwd); wifi->password_len = 13; } } printf("\r\nwifi.password = %s\n", wifi->password); printf("\r\nwifi.password_len = %d\n", wifi->password_len); printf("\r\nwifi.ssid = %s\n", wifi->ssid.val); printf("\r\nwifi.ssid_len = %d\n", wifi->ssid.len); printf("\r\nwifi.channel = %d\n", fixed_channel_num); printf("\r\n===== start to connect target AP =====\n"); return 0; }
static void wifi_scan_done_hdl( char* buf, int buf_len, int flags, void* userdata) { int i = 0; rtw_scan_handler_result_t scan_result_report; for(i=0; i<scan_result_handler_ptr.scan_cnt; i++){ rtw_memcpy(&scan_result_report.ap_details, scan_result_handler_ptr.pap_details[i], sizeof(rtw_scan_result_t)); scan_result_report.scan_complete = scan_result_handler_ptr.scan_complete; scan_result_report.user_data = scan_result_handler_ptr.user_data; (*scan_result_handler_ptr.gscan_result_handler)(&scan_result_report); } scan_result_handler_ptr.scan_complete = RTW_TRUE; scan_result_report.scan_complete = RTW_TRUE; (*scan_result_handler_ptr.gscan_result_handler)(&scan_result_report); rtw_free(scan_result_handler_ptr.ap_details); rtw_free(scan_result_handler_ptr.pap_details); #if SCAN_USE_SEMAPHORE rtw_up_sema(&scan_result_handler_ptr.scan_semaphore); #else scan_result_handler_ptr.scan_running = 0; #endif wifi_unreg_event_handler(WIFI_EVENT_SCAN_RESULT_REPORT, wifi_scan_each_report_hdl); wifi_unreg_event_handler(WIFI_EVENT_SCAN_DONE, wifi_scan_done_hdl); return; }
/** * rltk_wlan_set_netif_info - set netif hw address and register dev pointer to netif device * @idx_wlan: netif index * 0 for STA only or SoftAP only or STA in STA+SoftAP concurrent mode, * 1 for SoftAP in STA+SoftAP concurrent mode * @dev: register netdev pointer to LWIP. Reserved. * @dev_addr: set netif hw address * * Return Value: None */ void rltk_wlan_set_netif_info(int idx_wlan, void * dev, unsigned char * dev_addr) { #if (CONFIG_LWIP_LAYER == 1) rtw_memcpy(xnetif[idx_wlan].hwaddr, dev_addr, 6); xnetif[idx_wlan].state = dev; #endif }
int wifi_get_mac_address(char * mac) { int ret = 0; char buf[32]; rtw_memset(buf, 0, sizeof(buf)); rtw_memcpy(buf, "read_mac", 8); ret = wext_private_command_with_retval(WLAN0_NAME, buf, buf, 32); strcpy(mac, buf); return ret; }
static void wifi_scan_each_report_hdl( char* buf, int buf_len, int flags, void* userdata) { int i =0; int insert_pos = 0; rtw_scan_result_t** result_ptr = (rtw_scan_result_t**)buf; rtw_scan_result_t* temp = NULL; for(i=0; i<scan_result_handler_ptr.scan_cnt; i++){ if(CMP_MAC(scan_result_handler_ptr.ap_details[i].BSSID.octet, (*result_ptr)->BSSID.octet)){ memset(*result_ptr, 0, sizeof(rtw_scan_result_t)); return; } } scan_result_handler_ptr.scan_cnt++; if(scan_result_handler_ptr.scan_cnt > scan_result_handler_ptr.max_ap_size){ scan_result_handler_ptr.scan_cnt = scan_result_handler_ptr.max_ap_size; if((*result_ptr)->signal_strength > scan_result_handler_ptr.pap_details[scan_result_handler_ptr.max_ap_size-1]->signal_strength){ rtw_memcpy(scan_result_handler_ptr.pap_details[scan_result_handler_ptr.max_ap_size-1], *result_ptr, sizeof(rtw_scan_result_t)); temp = scan_result_handler_ptr.pap_details[scan_result_handler_ptr.max_ap_size -1]; }else return; }else{ rtw_memcpy(&scan_result_handler_ptr.ap_details[scan_result_handler_ptr.scan_cnt-1], *result_ptr, sizeof(rtw_scan_result_t)); } for(i=0; i< scan_result_handler_ptr.scan_cnt-1; i++){ if((*result_ptr)->signal_strength > scan_result_handler_ptr.pap_details[i]->signal_strength) break; } insert_pos = i; for(i = scan_result_handler_ptr.scan_cnt-1; i>insert_pos; i--) scan_result_handler_ptr.pap_details[i] = scan_result_handler_ptr.pap_details[i-1]; if(temp != NULL) scan_result_handler_ptr.pap_details[insert_pos] = temp; else scan_result_handler_ptr.pap_details[insert_pos] = &scan_result_handler_ptr.ap_details[scan_result_handler_ptr.scan_cnt-1]; rtw_memset(*result_ptr, 0, sizeof(rtw_scan_result_t)); }
//----------------------------------------------------------------------------// int wifi_get_txpower(int *poweridx) { int ret = 0; char buf[11]; rtw_memset(buf, 0, sizeof(buf)); rtw_memcpy(buf, "txpower", 11); ret = wext_private_command_with_retval(WLAN0_NAME, buf, buf, 11); sscanf(buf, "%d", poweridx); return ret; }
/** * rltk_wlan_send - send IP packets to WLAN. Called by low_level_output(). * @idx: netif index * @sg_list: data buffer list * @sg_len: size of each data buffer * @total_len: total data len * * Return Value: None */ int rltk_wlan_send(int idx, struct eth_drv_sg *sg_list, int sg_len, int total_len) { struct eth_drv_sg *last_sg; struct sk_buff *skb = NULL; int ret = 0; if(idx == -1){ DBG_ERR("netif is DOWN"); return -1; } DBG_TRACE("%s is called", __FUNCTION__); save_and_cli(); if(rltk_wlan_check_isup(idx)) rltk_wlan_tx_inc(idx); else { DBG_ERR("netif is DOWN"); restore_flags(); return -1; } restore_flags(); skb = rltk_wlan_alloc_skb(total_len); if (skb == NULL) { //DBG_ERR("rltk_wlan_alloc_skb() for data len=%d failed!", total_len); ret = -1; goto exit; } for (last_sg = &sg_list[sg_len]; sg_list < last_sg; ++sg_list) { rtw_memcpy(skb->tail, (void *)(sg_list->buf), sg_list->len); skb_put(skb, sg_list->len); } rltk_wlan_send_skb(idx, skb); exit: save_and_cli(); rltk_wlan_tx_dec(idx); restore_flags(); return ret; }
/** * rltk_wlan_recv - indicate packets to LWIP. Called by ethernetif_recv(). * @idx: netif index * @sg_list: data buffer list * @sg_len: size of each data buffer * * Return Value: None */ void rltk_wlan_recv(int idx, struct eth_drv_sg *sg_list, int sg_len) { struct eth_drv_sg *last_sg; struct sk_buff *skb; DBG_TRACE("%s is called", __FUNCTION__); if(idx == -1){ DBG_ERR("skb is NULL"); return; } skb = rltk_wlan_get_recv_skb(idx); DBG_ASSERT(skb, "No pending rx skb"); for (last_sg = &sg_list[sg_len]; sg_list < last_sg; ++sg_list) { if (sg_list->buf != 0) { rtw_memcpy((void *)(sg_list->buf), skb->data, sg_list->len); skb_pull(skb, sg_list->len); } } }
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; }
void restore_wifi_info_to_flash() { struct wlan_fast_reconnect * data_to_flash; u32 channel = 0; u8 index = 0; u8 *ifname[1] = {WLAN0_NAME}; rtw_wifi_setting_t setting; //struct security_priv *psecuritypriv = &padapter->securitypriv; //WLAN_BSSID_EX *pcur_bss = pmlmepriv->cur_network.network; data_to_flash = (struct wlan_fast_reconnect *)rtw_zmalloc(sizeof(struct wlan_fast_reconnect)); if(data_to_flash && p_write_reconnect_ptr){ if(wifi_get_setting((const char*)ifname[0],&setting) || setting.mode == RTW_MODE_AP){ printf("\r\n %s():wifi_get_setting fail or ap mode", __func__); return; } channel = setting.channel; rtw_memset(psk_essid[index], 0, sizeof(psk_essid[index])); strncpy(psk_essid[index], setting.ssid, strlen(setting.ssid)); switch(setting.security_type){ case RTW_SECURITY_OPEN: rtw_memset(psk_passphrase[index], 0, sizeof(psk_passphrase[index])); rtw_memset(wpa_global_PSK[index], 0, sizeof(wpa_global_PSK[index])); data_to_flash->security_type = RTW_SECURITY_OPEN; break; case RTW_SECURITY_WEP_PSK: channel |= (setting.key_idx) << 28; rtw_memset(psk_passphrase[index], 0, sizeof(psk_passphrase[index])); rtw_memset(wpa_global_PSK[index], 0, sizeof(wpa_global_PSK[index])); rtw_memcpy(psk_passphrase[index], setting.password, sizeof(psk_passphrase[index])); data_to_flash->security_type = RTW_SECURITY_WEP_PSK; break; case RTW_SECURITY_WPA_TKIP_PSK: data_to_flash->security_type = RTW_SECURITY_WPA_TKIP_PSK; break; case RTW_SECURITY_WPA2_AES_PSK: data_to_flash->security_type = RTW_SECURITY_WPA2_AES_PSK; break; default: break; } memcpy(data_to_flash->psk_essid, psk_essid[index], sizeof(data_to_flash->psk_essid)); if (strlen(psk_passphrase64) == 64) { memcpy(data_to_flash->psk_passphrase, psk_passphrase64, sizeof(data_to_flash->psk_passphrase)); } else { memcpy(data_to_flash->psk_passphrase, psk_passphrase[index], sizeof(data_to_flash->psk_passphrase)); } memcpy(data_to_flash->wpa_global_PSK, wpa_global_PSK[index], sizeof(data_to_flash->wpa_global_PSK)); memcpy(&(data_to_flash->channel), &channel, 4); //call callback function in user program p_write_reconnect_ptr((u8 *)data_to_flash, sizeof(struct wlan_fast_reconnect)); } if(data_to_flash) rtw_free(data_to_flash); }
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; }