/* 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 } } }
/*-----------------------------------------------------------------------------------*/ err_t ethernetif_init(struct netif *netif) { static int ethernetif_index; int local_index; SYS_ARCH_DECL_PROTECT(lev); SYS_ARCH_PROTECT(lev); local_index = ethernetif_index++; SYS_ARCH_UNPROTECT(lev); netif->name[0] = IFNAME0; netif->name[1] = (char)(IFNAME1 + local_index); netif->linkoutput = low_level_output; #if LWIP_ARP netif->output = etharp_output; #else /* LWIP_ARP */ netif->output = NULL; /* not used for PPPoE */ #endif /* LWIP_ARP */ #if LWIP_NETIF_HOSTNAME /* Initialize interface hostname */ netif_set_hostname(netif, "lwip"); #endif /* LWIP_NETIF_HOSTNAME */ netif->mtu = 1500; netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP; netif->hwaddr_len = ETHARP_HWADDR_LEN; NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 100000000); /* sets link up or down based on current status */ low_level_init(netif); return ERR_OK; }
/** \brief Initialize the Ethernet subsystem. * */ void init_ethernet(const u8_t macAddress[], const char *hostname) { lwip_init(); ethernet_hardware_init(); ethernetif_set_mac_address(macAddress); netif_set_hostname(&gs_net_if, hostname); }
/** * Callback for tcpip_init() */ static void tcpip_init_cb(void *args){/*{{{*/ ip_addr_t ip_addr; ip_addr_t netmask; ip_addr_t gw_addr; BaseType_t retval; eth_int_q_handle = xQueueCreate(1, sizeof(uint32_t)); retval = xTaskCreate( eth_int_task, "eth_int", ETH_INT_STACK, NULL, tskIDLE_PRIORITY+ETH_PRIO, ð_int_task_handle); LOOP_ERRMSG(retval != pdPASS, "Could not start lwip eth_int task\n" ); #if XBH_IP4_STATIC ip_addr.addr = htonl(XBH_IP4_ADDR); netmask.addr = htonl(XBH_IP4_NETMASK); gw_addr.addr = 0; #else ip_addr.addr = 0; netmask.addr = 0; gw_addr.addr = 0; #endif netif_add(&lwip_netif, &ip_addr, &netmask, &gw_addr, NULL, tivaif_init, tcpip_input); //netif_add sets everything to defaults, so run after netif_add netif_set_hostname(&lwip_netif, XBH_HOSTNAME); netif_set_status_callback(&lwip_netif, link_status); netif_set_default(&lwip_netif); #if LWIP_IPV6 lwip_netif.ip6_autoconfig_enabled = 1; lwip_netif.output_ip6 = ethip6_output; //IPv6, enable linklocal addresses and SLAAC netif_create_ip6_linklocal_address(&lwip_netif, 1); netif_ip6_addr_set_state((&lwip_netif), 0, IP6_ADDR_TENTATIVE); #endif #if XBH_IP4_STATIC netif_set_up(&lwip_netif); #else dhcp_start(&lwip_netif); #endif #if DEBUG_STACK DEBUG_OUT("Stack Usage: %s: %d\n", __PRETTY_FUNCTION__, uxTaskGetStackHighWaterMark(NULL)); #endif }/*}}}*/
/** \brief Initialize the Ethernet subsystem. * */ void init_ethernet(const u8_t macAddress[], const char *hostname) { lwip_init(); ethernet_hardware_init(); ethernetif_set_mac_address(macAddress); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Waddress" /* netif_set_hostname() macro uses an always true conditional * for encapsulation. That, generates a compiler warning which * is why that particular trick isn't always apt. */ netif_set_hostname(&gs_net_if, hostname); #pragma GCC diagnostic pop }
int EthernetInterface::setName(const char * myname) { int i; strncpy(myName, myname, 32); myName[32] = '\0'; // ensure it is NULL terminated. // make the name 'safe' for (i=0; i<32 && myName[i]; i++) { if (!inRange(myName[i], '0', '9') && !inRange(myName[i], 'A', 'Z') && !inRange(myName[i], 'a', 'z')) myName[i] = '-'; } netif_set_hostname(&netif, myName); return 0; }
/** \brief Configure the Ethernet subsystem. Should be called after init_ethernet() * */ void ethernet_configure_interface(const u8_t macAddress[], const char *hostname) { ethernetif_set_mac_address(macAddress); netif_set_hostname(&gs_net_if, hostname); }
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 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); }