/* 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 } } }
// For BCT hut-plugging test void cmd_interface(int argc, char **argv) { if(argc == 1) goto error; if(strcmp(argv[1], "set") == 0) { if((argc == 4) || (argc == 5)) { struct ip_addr ip_addr; struct ip_addr netmask_addr; struct ip_addr gw_addr; uint32_t ip[4], netmask[4], gw[4]; char *ptr, *head, *tail; int i; ptr = argv[2]; for(i = 0; i < 4; i ++) { head = ptr; while(*ptr && *ptr != '.') ptr ++; tail = ptr ++; *tail = 0; ip[i] = atoi(head); } ptr = argv[3]; for(i = 0; i < 4; i ++) { head = ptr; while(*ptr && *ptr != '.') ptr ++; tail = ptr ++; *tail = 0; netmask[i] = atoi(head); } if(argc == 5) { ptr = argv[4]; for(i = 0; i < 4; i ++) { head = ptr; while(*ptr && *ptr != '.') ptr ++; tail = ptr ++; *tail = 0; gw[i] = atoi(head); } } else { memset(gw, 0, sizeof(gw)); } IP4_ADDR(&ip_addr, ip[0], ip[1], ip[2], ip[3]); IP4_ADDR(&netmask_addr, netmask[0], netmask[1], netmask[2], netmask[3]); IP4_ADDR(&gw_addr, gw[0], gw[1], gw[2], gw[3]); netif_set_addr(&xnetif[0], &ip_addr , &netmask_addr, &gw_addr); printf("\nSet IP : %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); printf( " MASK: %d.%d.%d.%d\n", netmask[0], netmask[1], netmask[2], netmask[3]); printf( " GW : %d.%d.%d.%d\n", gw[0], gw[1], gw[2], gw[3]); } else { printf("Usage: interface set IP NETMASK [GATEWAY]\n"); } } else if(strcmp(argv[1], "info") == 0) { u8 *mac = LwIP_GetMAC(&xnetif[0]); u8 *ip = LwIP_GetIP(&xnetif[0]); u8 *netmask = LwIP_GetMASK(&xnetif[0]); u8 *gw = LwIP_GetGW(&xnetif[0]); printf("\n MAC Address : %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); printf("\nIPv4 Address : %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); printf( " Netmask : %d.%d.%d.%d\n", netmask[0], netmask[1], netmask[2], netmask[3]); printf( " Gateway : %d.%d.%d.%d\n", gw[0], gw[1], gw[2], gw[3]); #if LWIP_IPV6 uint8_t *ipv6 = (uint8_t *) &(xnetif[0].ip6_addr[0].addr[0]); printf("\nIPv6 Address : %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", ipv6[0], ipv6[1], ipv6[2], ipv6[3], ipv6[4], ipv6[5], ipv6[6], ipv6[7], ipv6[8], ipv6[9], ipv6[10], ipv6[11], ipv6[12], ipv6[13], ipv6[14], ipv6[15]); #endif } else if(strcmp(argv[1], "disconnect") == 0) { int timeout = 20; char essid[33]; if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) { printf("\nWIFI disconnected\n"); return; } wifi_disconnect(); while(1) { if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) { printf("\nWIFI disconnected\n"); break; } if(timeout == 0) { printf("\nERROR: Operation failed!\n"); break; } vTaskDelay(1000); timeout --; } } else if(strcmp(argv[1], "reconnect") == 0) { HomeKitNetworkConnection(); } else { error: printf("Usage: interface set|info|disconnect|reconnect\n"); } }
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 cmd_mdns(int argc, char **argv) { static DNSServiceRef dnsServiceRef = NULL; static char hostname[32]; if(argc == 1) goto error; if(strcmp(argv[1], "init") == 0) { WACPersistentConfig_t config; WACPlatformReadConfig(&config); if((config.name_len > 0) && (config.name_len < 32)) { strcpy(hostname, config.name); } else { uint8_t *mac = LwIP_GetMAC(&xnetif[0]); sprintf(hostname, "AMEBA%02x%02x%02x", mac[3], mac[4], mac[5]); } netif_set_hostname(&xnetif[0], hostname); if(mDNSResponderInit() == 0) printf("mDNS initialized\n"); else printf("mDNS init failed\n"); } else if(strcmp(argv[1], "deinit") == 0) { mDNSResponderDeinit(); printf("mDNS de-initialized\n"); } else if(strcmp(argv[1], "reg") == 0) { if(argc != 5) { printf("Usage: mdns reg NAME TYPE PORT\n"); return; } if(atoi(argv[4]) == 0) printf("mDNS service incorrect port\n"); if(dnsServiceRef) { mDNSDeregisterService(dnsServiceRef); dnsServiceRef = NULL; } if((dnsServiceRef = mDNSRegisterService(argv[2], argv[3], "local", atoi(argv[4]), NULL)) != NULL) printf("mDNS service %s.%s registered\n", argv[2], argv[3]); else printf("mDNS service %s.%s register failed\n", argv[2], argv[3]); } else if(strcmp(argv[1], "dereg") == 0) { if(dnsServiceRef) { mDNSDeregisterService(dnsServiceRef); dnsServiceRef = NULL; printf("mDNS service de-registered\n"); } else { printf("mDNS no service\n"); } } else if(strcmp(argv[1], "update") == 0) { if(argc != 5) { printf("Usage: mdns update TXT_KEY TXT_VALUE TTL\n"); return; } if(dnsServiceRef) { TXTRecordRef txtRecord; unsigned char txt_buf[100]; TXTRecordCreate(&txtRecord, sizeof(txt_buf), txt_buf); TXTRecordSetValue(&txtRecord, argv[2], strlen(argv[3]), argv[3]); mDNSUpdateService(dnsServiceRef, &txtRecord, atoi(argv[4])); TXTRecordDeallocate(&txtRecord); printf("mDNS service updated\n"); } else { printf("mDNS no service\n"); } } // For BCT mDNS manual name change test else if(strcmp(argv[1], "name_test") == 0) { char *new_name = "New - Bonjour Service Name"; printf("Rename HAP service to \"%s\"._hap._tcp.local. %s\n", new_name, (RenameHAPBonjourService(new_name) == 0) ? "success":"fail"); } else { error: printf("Usage: mdns init|deinit|reg|dereg|name_test\n"); } }
void fATWx(void *arg){ int i = 0; #if CONFIG_LWIP_LAYER u8 *mac = LwIP_GetMAC(&xnetif[0]); u8 *ip = LwIP_GetIP(&xnetif[0]); u8 *gw = LwIP_GetGW(&xnetif[0]); #endif u8 *ifname[2] = {WLAN0_NAME,WLAN1_NAME}; rtw_wifi_setting_t setting; printf("[ATW?]: _AT_WLAN_INFO_\n\r"); for(i=0;i<NET_IF_NUM;i++){ if(rltk_wlan_running(i)){ #if CONFIG_LWIP_LAYER mac = LwIP_GetMAC(&xnetif[i]); ip = LwIP_GetIP(&xnetif[i]); gw = LwIP_GetGW(&xnetif[i]); #endif printf("\n\r\nWIFI %s Status: Running", ifname[i]); printf("\n\r=============================="); rltk_wlan_statistic(i); wifi_get_setting((const char*)ifname[i],&setting); wifi_show_setting((const char*)ifname[i],&setting); #if CONFIG_LWIP_LAYER printf("\n\rInterface (%s)", ifname[i]); printf("\n\r=============================="); printf("\n\r\tMAC => %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) ; printf("\n\r\tIP => %d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); printf("\n\r\tGW => %d.%d.%d.%d\n\r", gw[0], gw[1], gw[2], gw[3]); #endif if(setting.mode == RTW_MODE_AP || i == 1) { int client_number; struct { int count; rtw_mac_t mac_list[3]; } client_info; client_info.count = 3; wifi_get_associated_client_list(&client_info, sizeof(client_info)); printf("\n\rAssociated Client List:"); printf("\n\r=============================="); if(client_info.count == 0) printf("\n\rClient Num: 0\n\r", client_info.count); else { printf("\n\rClient Num: %d", client_info.count); for( client_number=0; client_number < client_info.count; client_number++ ) { printf("\n\rClient %d:", client_number + 1); printf("\n\r\tMAC => "MAC_FMT"", MAC_ARG(client_info.mac_list[client_number].octet)); } printf("\n\r"); } } } } #if defined(configUSE_TRACE_FACILITY) && (configUSE_TRACE_FACILITY == 1) && (configUSE_STATS_FORMATTING_FUNCTIONS == 1) { signed char pcWriteBuffer[1024]; vTaskList((char*)pcWriteBuffer); printf("\n\rTask List: \n\r%s", pcWriteBuffer); } #endif }