/* 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 } } }
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); }
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); }