/* Example of network connection in HomeKit * Wi-Fi connection with the stored WAC configuration * Set LWIP hostname to accessory name before DHCP * Setup IPv4 and IPv6 address. Suggest to set IPv6 address before IPv4 when mDNS is working */ void HomeKitNetworkConnection(void) { static char hostname[32]; WACPersistentConfig_t config; WACPlatformReadConfig(&config); if((config.name_len > 0) && (config.name_len < 32)) { memset(hostname, 0, sizeof(hostname)); memcpy(hostname, config.name, config.name_len); } else { uint8_t *mac = LwIP_GetMAC(&xnetif[0]); sprintf(hostname, "RealtekAmeba%02x%02x%02x", mac[3], mac[4], mac[5]); } netif_set_hostname(&xnetif[0], hostname); if((config.ssid_len > 0) && (config.ssid_len < 32) && (config.password_len >= 0) && (config.password_len < 32)) { if(wifi_connect((unsigned char *) config.ssid, (config.password_len) ? RTW_SECURITY_WPA2_AES_PSK : RTW_SECURITY_OPEN, (unsigned char *) config.password, config.ssid_len, config.password_len, 0, NULL) == RTW_SUCCESS) { #if LWIP_IPV6 LwIP_AUTOIP_IPv6(&xnetif[0]); #endif printf("\nIPv4 DHCP ..."); LwIP_DHCP(0, DHCP_START); #if LWIP_AUTOIP uint8_t *ip = LwIP_GetIP(&xnetif[0]); if((ip[0] == 0) && (ip[1] == 0) && (ip[2] == 0) && (ip[3] == 0)) { printf("\n\nIPv4 AUTOIP ..."); LwIP_AUTOIP(&xnetif[0]); } #endif } } }
void wps_check_and_show_connection_info(void) { rtw_wifi_setting_t setting; #if CONFIG_LWIP_LAYER /* Start DHCP Client */ LwIP_DHCP(0, DHCP_START); #endif wifi_get_setting(WLAN0_NAME, &setting); wifi_show_setting(WLAN0_NAME, &setting); }
static int SC_check_and_show_connection_info(void) { rtw_wifi_setting_t setting; int ret = -1; /* If not rise priority, LwIP DHCP may timeout */ vTaskPrioritySet(NULL, tskIDLE_PRIORITY + 3); /* Start DHCP Client */ ret = LwIP_DHCP(0, DHCP_START); vTaskPrioritySet(NULL, tskIDLE_PRIORITY + 1); wifi_get_setting(WLAN0_NAME, &setting); wifi_show_setting(WLAN0_NAME, &setting); if (ret == DHCP_ADDRESS_ASSIGNED) return SC_SUCCESS; else return SC_DHCP_FAIL; }
int wifi_off(void) { int ret = 0; int timeout = 20; if((rltk_wlan_running(WLAN0_IDX) == 0) && (rltk_wlan_running(WLAN1_IDX) == 0)) { printf("\n\rWIFI is not running"); return 0; } #if CONFIG_LWIP_LAYER dhcps_deinit(); LwIP_DHCP(0, DHCP_STOP); #endif printf("\n\rDeinitializing WIFI ..."); rltk_wlan_deinit(); while(1) { if((rltk_wlan_running(WLAN0_IDX) == 0) && (rltk_wlan_running(WLAN1_IDX) == 0)) { printf("\n\rWIFI deinitialized"); break; } if(timeout == 0) { printf("\n\rERROR: Deinit WIFI timeout!"); break; } vTaskDelay(1 * configTICK_RATE_HZ); timeout --; } wifi_mode = RTW_MODE_NONE; #if CONFIG_INIC_EN inic_stop(); #endif return ret; }
static void wifi_autoreconnect_thread(void *param) { int ret = RTW_ERROR; struct wifi_autoreconnect_param *reconnect_param = (struct wifi_autoreconnect_param *) param; printf("\n\rauto reconnect ...\n"); ret = wifi_connect(reconnect_param->ssid, reconnect_param->security_type, reconnect_param->password, reconnect_param->ssid_len, reconnect_param->password_len, reconnect_param->key_id, NULL); #if CONFIG_LWIP_LAYER if(ret == RTW_SUCCESS) { LwIP_DHCP(0, DHCP_START); #if LWIP_AUTOIP uint8_t *ip = LwIP_GetIP(&xnetif[0]); if((ip[0] == 0) && (ip[1] == 0) && (ip[2] == 0) && (ip[3] == 0)) { printf("\n\nIPv4 AUTOIP ..."); LwIP_AUTOIP(&xnetif[0]); } #endif } #endif vTaskDelete(NULL); }
int uartadapter_connect_wifi(rtw_network_info_t *p_wifi, uint32_t channel, uint8_t pscan_config) { int retry = 3; rtw_wifi_setting_t setting; int ret; while (1) { if(wifi_set_pscan_chan((uint8_t *)&channel, &pscan_config, 1) < 0){ printf("\n\rERROR: wifi set partial scan channel fail"); ret = SC_TARGET_CHANNEL_SCAN_FAIL; return ret; } ret = wifi_connect((char*)p_wifi->ssid.val, p_wifi->security_type, (char*)p_wifi->password, p_wifi->ssid.len, p_wifi->password_len, p_wifi->key_id, NULL); if (ret == RTW_SUCCESS) { ret = LwIP_DHCP(0, DHCP_START); wifi_get_setting(WLAN0_NAME, &setting); wifi_show_setting(WLAN0_NAME, &setting); if (ret == DHCP_ADDRESS_ASSIGNED) return SC_SUCCESS; else return SC_DHCP_FAIL; } if (retry == 0) { ret = SC_JOIN_BSS_FAIL; break; } retry --; } return ret; }
int wifi_restart_ap( unsigned char *ssid, rtw_security_t security_type, unsigned char *password, int ssid_len, int password_len, int channel) { unsigned char idx = 0; struct ip_addr ipaddr; struct ip_addr netmask; struct ip_addr gw; struct netif * pnetif = &xnetif[0]; #ifdef CONFIG_CONCURRENT_MODE rtw_wifi_setting_t setting; int sta_linked = 0; #endif if(rltk_wlan_running(WLAN1_IDX)){ idx = 1; } // stop dhcp server dhcps_deinit(); #ifdef CONFIG_CONCURRENT_MODE if(idx > 0){ sta_linked = wifi_get_setting(WLAN0_NAME, &setting); wifi_off(); vTaskDelay(20); wifi_on(RTW_MODE_STA_AP); } else #endif { IP4_ADDR(&ipaddr, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3); IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3); netif_set_addr(pnetif, &ipaddr, &netmask,&gw); wifi_off(); vTaskDelay(20); wifi_on(RTW_MODE_AP); } // start ap if(wifi_start_ap((char*)ssid, security_type, (char*)password, ssid_len, password_len, channel) < 0) { printf("\n\rERROR: Operation failed!"); return -1; } #if (INCLUDE_uxTaskGetStackHighWaterMark == 1) printf("\r\nWebServer Thread: High Water Mark is %ld\n", uxTaskGetStackHighWaterMark(NULL)); #endif #ifdef CONFIG_CONCURRENT_MODE // connect to ap if wlan0 was linked with ap if(idx > 0 && sta_linked == 0){ int ret; printf("\r\nAP: ssid=%s", (char*)setting.ssid); printf("\r\nAP: security_type=%d", setting.security_type); printf("\r\nAP: password=%s", (char*)setting.password); printf("\r\nAP: key_idx =%d\n", setting.key_idx); ret = wifi_connect((char*)setting.ssid, setting.security_type, (char*)setting.password, strlen((char*)setting.ssid), strlen((char*)setting.password), setting.key_idx, NULL); #if CONFIG_DHCP_CLIENT if(ret == RTW_SUCCESS) { /* Start DHCPClient */ LwIP_DHCP(0, DHCP_START); } #endif } #endif #if (INCLUDE_uxTaskGetStackHighWaterMark == 1) printf("\r\nWebServer Thread: High Water Mark is %ld\n", uxTaskGetStackHighWaterMark(NULL)); #endif // start dhcp server dhcps_init(&xnetif[idx]); return 0; }
void init_sequence(void *param) { int wac_unconfigured = 0; WACPersistentConfig_t config; // Clear WLAN init done callback to prevent re-enter init sequence at each wlan init p_wlan_init_done_callback = NULL; // Get existed WAC configuration WACPlatformReadConfig(&config); memset(dev_name, 0, sizeof(dev_name)); if((config.name_len > 0) && (config.name_len < 32)) { memcpy(dev_name, config.name, config.name_len); } else { uint8_t *mac = LwIP_GetMAC(&xnetif[0]); sprintf(dev_name, "RealtekAmeba%02x%02x%02x", mac[3], mac[4], mac[5]); wac_unconfigured = 1; } // Setup LWIP hostname before connecting netowrk netif_set_hostname(&xnetif[0], dev_name); if(wac_unconfigured) { // Start WAC server WACDevice_t dev = {0}; dev.name = dev_name; dev.manufacturer = "Realtek"; dev.model = "Ameba"; WACSetupDebug(1); WACStart(&dev); } else { if((config.ssid_len > 0) && (config.ssid_len < 32) && (config.password_len >= 0) && (config.password_len < 32)) { if(wifi_connect((unsigned char *) config.ssid, (config.password_len) ? RTW_SECURITY_WPA2_AES_PSK : RTW_SECURITY_OPEN, (unsigned char *) config.password, config.ssid_len, config.password_len, 0, NULL) == RTW_SUCCESS) { #if LWIP_IPV6 LwIP_AUTOIP_IPv6(&xnetif[0]); #endif printf("\nIPv4 DHCP ..."); LwIP_DHCP(0, DHCP_START); uint8_t *ip = LwIP_GetIP(&xnetif[0]); if((ip[0] == IP_ADDR0) && (ip[1] == IP_ADDR1) && (ip[2] == IP_ADDR2) && (ip[3] == IP_ADDR3)) { printf("\n\nIPv4 AUTOIP ..."); LwIP_AUTOIP(&xnetif[0]); } char setup_code[11]; FlashSetupcodeRead(setup_code); if(strlen(setup_code) != 10) { random_setupcode(setup_code); printf("\nNo setupcode in flash, use random setupcode: %s\n", setup_code); } printf("\nSETUP CODE for Test: %s\n", setup_code); // Start HAP server HAPParameter_t hap_param; hap_param.name = dev_name; hap_param.model = "Ameba"; hap_param.setupcode = setup_code; hap_param.config_number = 1; hap_param.use_MFi = 1; WACSetupDebug(1); HAPSetupDebug(1); HAPStart(&hap_param); } } else { printf("\nInvalid network config\n"); } } vTaskDelete(NULL); }
void fATWC(void *arg){ int mode, ret; unsigned long tick1 = xTaskGetTickCount(); unsigned long tick2, tick3; char empty_bssid[6] = {0}, assoc_by_bssid = 0; printf("[ATWC]: _AT_WLAN_JOIN_NET_\n\r"); if(memcmp (wifi.bssid.octet, empty_bssid, 6)) assoc_by_bssid = 1; else if(wifi.ssid.val[0] == 0){ printf("[ATWC]Error: SSID can't be empty\n\r"); goto EXIT; } if(wifi.password != NULL){ if((wifi.key_id >= 0)&&(wifi.key_id <= 3)) { wifi.security_type = RTW_SECURITY_WEP_PSK; } else{ wifi.security_type = RTW_SECURITY_WPA2_AES_PSK; } } else{ wifi.security_type = RTW_SECURITY_OPEN; } //Check if in AP mode wext_get_mode(WLAN0_NAME, &mode); if(mode == IW_MODE_MASTER) { #if CONFIG_LWIP_LAYER dhcps_deinit(); #endif wifi_off(); vTaskDelay(20); if (wifi_on(RTW_MODE_STA) < 0){ printf("\n\rERROR: Wifi on failed!"); goto EXIT; } } if(assoc_by_bssid){ printf("\n\rJoining BSS by BSSID "MAC_FMT" ...\n\r", MAC_ARG(wifi.bssid.octet)); ret = wifi_connect_bssid(wifi.bssid.octet, (char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, ETH_ALEN, wifi.ssid.len, wifi.password_len, wifi.key_id, NULL); } else { printf("\n\rJoining BSS by SSID %s...\n\r", (char*)wifi.ssid.val); ret = wifi_connect((char*)wifi.ssid.val, wifi.security_type, (char*)wifi.password, wifi.ssid.len, wifi.password_len, wifi.key_id, NULL); } if(ret!= RTW_SUCCESS){ printf("\n\rERROR: Operation failed!"); goto EXIT; } tick2 = xTaskGetTickCount(); printf("\r\nConnected after %dms.\n", (tick2-tick1)); #if CONFIG_LWIP_LAYER /* Start DHCPClient */ LwIP_DHCP(0, DHCP_START); tick3 = xTaskGetTickCount(); printf("\r\n\nGot IP after %dms.\n", (tick3-tick1)); #endif printf("\n\r"); EXIT: init_wifi_struct( ); }