static void mdns_process_query(int fd, dns_name_t* name, dns_question_t* question, dns_message_iterator_t* source ) { dns_message_iterator_t response; net_para_st para; u32 myip; getNetPara(¶, _interface); myip = htonl(inet_addr(para.ip)); memset( &response, 0, sizeof(dns_message_iterator_t) ); switch ( question->question_type ) { case RR_QTYPE_ANY: case RR_TYPE_A: if ( dns_compare_name_to_string( name, available_services->hostname, __FUNCTION__, __LINE__) ){ _debug_out("UDP multicast test: Recv RR_TYPE_A.\r\n"); if(dns_create_message( &response, 256 )){ dns_write_header( &response, source->header->id, 0x8400, 0, 1, 0 ); dns_write_record( &response, available_services->hostname, RR_CLASS_IN | RR_CACHE_FLUSH, RR_TYPE_A, 300, (u8*)&myip); mdns_send_message(fd, &response ); dns_free_message( &response ); return; } } default: _debug_out("UDP multicast test: Request not support type: %d.---------------------\r\n", question->question_type); } }
/* Sysytem initilize */ void mxchipWNet_HA_init(void) { network_InitTypeDef_st wNetConfig; network_InitTypeDef_adv_st wNetConfigAdv; int err = MXCHIP_FAILED; net_para_st para; device_info = (mxchipWNet_HA_st *)malloc(sizeof(mxchipWNet_HA_st)); memset(device_info, 0, sizeof(mxchipWNet_HA_st)); SystemCoreClockUpdate(); mxchipInit(); hal_uart_init(); getNetPara(¶, Station); formatMACAddr((void *)device_info->status.mac, ¶.mac); strcpy((char *)device_info->status.ip, (char *)¶.ip); strcpy((char *)device_info->status.mask, (char *)¶.mask); strcpy((char *)device_info->status.gw, (char *)¶.gate); strcpy((char *)device_info->status.dns, (char *)¶.dns); readConfiguration(device_info); device_info->tcpServer_fd = -1; device_info->tcpClient_fd = (int *)malloc(4 * MAX_CLIENT); device_info->cloud_fd = -1; device_info->udpSearch_fd = -1; memset(device_info->tcpClient_fd, -1, sizeof(4 * MAX_CLIENT)); if(device_info->conf.fastLinkConf.availableRecord){ //Try fast link memcpy(&wNetConfigAdv.ap_info, &device_info->conf.fastLinkConf.ap_info, sizeof(ApList_adv_t)); memcpy(&wNetConfigAdv.key, &device_info->conf.fastLinkConf.key, device_info->conf.fastLinkConf.key_len); wNetConfigAdv.key_len = device_info->conf.fastLinkConf.key_len; wNetConfigAdv.dhcpMode = DHCP_Client; strcpy(wNetConfigAdv.local_ip_addr, (char*)device_info->conf.ip); strcpy(wNetConfigAdv.net_mask, (char*)device_info->conf.mask); strcpy(wNetConfigAdv.gateway_ip_addr, (char*)device_info->conf.gw); strcpy(wNetConfigAdv.dnsServer_ip_addr, (char*)device_info->conf.dns); wNetConfigAdv.wifi_retry_interval = 100; err = StartAdvNetwork(&wNetConfigAdv); } if(err == MXCHIP_FAILED){ wNetConfig.wifi_mode = Station; strcpy(wNetConfig.wifi_ssid, device_info->conf.sta_ssid); strcpy(wNetConfig.wifi_key, device_info->conf.sta_key); wNetConfig.dhcpMode = DHCP_Client; strcpy(wNetConfig.local_ip_addr, (char*)device_info->conf.ip); strcpy(wNetConfig.net_mask, (char*)device_info->conf.mask); strcpy(wNetConfig.gateway_ip_addr, (char*)device_info->conf.gw); strcpy(wNetConfig.dnsServer_ip_addr, (char*)device_info->conf.dns); wNetConfig.wifi_retry_interval = 500; StartNetwork(&wNetConfig); } ps_enable(); }
void easylink_user_data_result(int datalen, char* data) { net_para_st para; getNetPara(¶, Station); if(!datalen) { printf("No user input. %s\r\n", data); } else { printf("User input is %s\r\n", data); } }
void restoreConfig(void) { config_t config; net_para_st netPara; uint32_t paraStartAddress, paraEndAddress; paraStartAddress = PARA_START_ADDRESS; paraEndAddress = PARA_END_ADDRESS; getNetPara(&netPara,Station); memset(&config, 0x0, sizeof(config_t)); config.magicNumber = MAGIC_FLAG; sprintf((char*)config.wifi_ssid , "MXCHIP_%s", netPara.mac+6); sprintf((char*)config.wifi_key , ""); FLASH_If_Init(); FLASH_If_Erase(paraStartAddress, paraEndAddress); // 擦除16K 数据保存区; FLASH_If_Write(¶StartAddress, (void *)&config, sizeof(config_t)); FLASH_Lock(); }
OSStatus MICOStartBonjourService( WiFi_Interface interface, mico_Context_t * const inContext ) { char temp_txt[500]; char *temp_txt2; OSStatus err; net_para_st para; bonjour_init_t init; memset(&init, 0x0, sizeof(bonjour_init_t)); getNetPara(¶, Station); init.service_name = BONJOUR_SERVICE; /* name#xxxxxx.local. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c.local.", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.host_name = (char*)__strdup(temp_txt); /* name#xxxxxx. */ snprintf( temp_txt, 100, "%s#%c%c%c%c%c%c", inContext->flashContentInRam.micoSystemConfig.name, inContext->micoStatus.mac[9], inContext->micoStatus.mac[10], \ inContext->micoStatus.mac[12], inContext->micoStatus.mac[13], \ inContext->micoStatus.mac[15], inContext->micoStatus.mac[16] ); init.instance_name = (char*)__strdup(temp_txt); init.service_port = HA_SERVER_PORT; init.interface = interface; sprintf(temp_txt, "C#=%d.", 0x01); sprintf(temp_txt, "%sff=%d.", temp_txt, 0x10); temp_txt2 = __strdup_trans_dot(inContext->micoStatus.mac); sprintf(temp_txt, "%sid=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MODEL); sprintf(temp_txt, "%smd=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(HA_PV); sprintf(temp_txt, "%spv=%s.", temp_txt, temp_txt2); free(temp_txt2); sprintf(temp_txt, "%ss#=%d.", temp_txt, 0x01); sprintf(temp_txt, "%ssf=%d.", temp_txt, 0x05); temp_txt2 = __strdup_trans_dot(HA_SV); sprintf(temp_txt, "%ssv=%s.", temp_txt, temp_txt2); free(temp_txt2); sprintf(temp_txt, "%sSeed=%u.", temp_txt, inContext->flashContentInRam.micoSystemConfig.seed); init.txt_record = (char*)__strdup(temp_txt); bonjour_service_init(init); free(init.host_name); free(init.instance_name); free(init.txt_record); err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)BonjourNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_SYS_WILL_POWER_OFF, (void *)BonjourNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); start_bonjour_service(); _bonjourStarted = true; exit: return err; }
int application_start(void) { OSStatus err = kNoErr; net_para_st para; Platform_Init(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mxchipInit(); getNetPara(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); mico_log_trace(); mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version()); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #ifdef CONFIG_MODE_EASYLINK err = startEasyLink( context ); require_noerr( err, exit ); #endif #ifdef CONFIG_MODE_WAC err = startMfiWac( context ); require_noerr( err, exit ); #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ ps_enable(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ mico_mcu_powersave_config(true); } /*Bonjour service for searching*/ if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){ err = MICOStartBonjourService( Station, context ); require_noerr( err, exit ); } /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); PlatformSoftReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); wifi_power_down(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(100); wifi_power_down(); Platform_Enter_STANDBY(); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
int application_start(void) { OSStatus err = kNoErr; net_para_st para; Platform_Init(); /*Read current configurations*/ context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) ); require_action( context, exit, err = kNoMemoryErr ); memset(context, 0x0, sizeof(mico_Context_t)); mico_rtos_init_mutex(&context->flashContentInRam_mutex); mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); MICOReadConfiguration( context ); err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mxchipInit(); getNetPara(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); mico_log_trace(); mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version()); /*Start system monotor thread*/ err = MICOStartSystemMonitor(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") ); err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000); require_noerr( err, exit ); mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #ifdef CONFIG_MODE_EASYLINK err = startEasyLink( context ); require_noerr( err, exit ); #endif #ifdef CONFIG_MODE_WAC WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6); WAC_Params->isUnconfigured = 1; WAC_Params->supportsAirPlay = 0; WAC_Params->supportsAirPrint = 0; WAC_Params->supports2_4GHzWiFi = 1; WAC_Params->supports5GHzWiFi = 0; WAC_Params->supportsWakeOnWireless = 0; WAC_Params->firmwareRevision = FIRMWARE_REVISION; WAC_Params->hardwareRevision = HARDWARE_REVISION; WAC_Params->serialNumber = SERIAL_NUMBER; WAC_Params->name = context->flashContentInRam.micoSystemConfig.name; WAC_Params->model = MODEL; WAC_Params->manufacturer = MANUFACTURER; WAC_Params->numEAProtocols = 1; WAC_Params->eaBundleSeedID = BUNDLE_SEED_ID; WAC_Params->eaProtocols = (char **)eaProtocols;; err = startMFiWAC( context, WAC_Params, 1200); free(WAC_Params); require_noerr( err, exit ); #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler ); require_noerr( err, exit ); if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ ps_enable(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ mico_mcu_powersave_config(true); } /*Bonjour service for searching*/ // if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){ // err = MICOStartBonjourService( Station, context ); // require_noerr( err, exit ); // } err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mDNSResponder", mDNSResponder_thread, 0x1000, (void*)context ); require_noerr_action( err, exit, mico_log("ERROR: Unable to start mDNSResponder thread.") ); /*Local configuration server*/ if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){ err = MICOStartConfigServer(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") ); } /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } /*System status changed*/ while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){ switch(context->micoStatus.sys_state){ case eState_Normal: break; case eState_Software_Reset: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); PlatformSoftReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); wifi_power_down(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(200); wifi_power_down(); Platform_Enter_STANDBY(); break; default: break; } } require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
void process_dns_questions(int fd, dns_message_iterator_t* iter ) { dns_name_t name; dns_question_t question; dns_message_iterator_t response; net_para_st para; int a = 0; int question_processed; u32 myip; getNetPara(¶, _interface); myip = htonl(inet_addr(para.ip)); if(myip == 0) { _debug_out("UDP multicast test: IP error.\r\n"); return; } memset( &response, 0, sizeof(dns_message_iterator_t) ); for ( a = 0; a < htons(iter->header->question_count); ++a ) { if (iter->iter > iter->end) break; if(dns_get_next_question( iter, &question, &name )==0) break; question_processed = 0; switch ( question.question_type ){ case RR_TYPE_PTR: if ( available_services != NULL ){ // Check if its a query for all available services if ( dns_compare_name_to_string( &name, MFi_SERVICE_QUERY_NAME, __FUNCTION__, __LINE__ ) ){ int b = 0; _debug_out("UDP multicast test: Recv a SERVICE QUERY request.\r\n"); if(dns_create_message( &response, 512 )) { dns_write_header(&response, iter->header->id, 0x8400, 0, available_service_count, 0 ); for ( b = 0; b < available_service_count; ++b ){ dns_write_record( &response, MFi_SERVICE_QUERY_NAME, RR_CLASS_IN, RR_TYPE_PTR, 1500, (u8*) available_services[b].service_name ); } mdns_send_message(fd, &response ); dns_free_message( &response ); question_processed = 1; } } // else check if its one of our records else { int b = 0; for ( b = 0; b < available_service_count; ++b ){ //printf("UDP multicast test: Recv a SERVICE Detail request: %s.\r\n", name); if ( dns_compare_name_to_string( &name, available_services[b].service_name, __FUNCTION__, __LINE__ )){ // Send the PTR, TXT, SRV and A records if(dns_create_message( &response, 512 )){ dns_write_header( &response, iter->header->id, 0x8400, 0, 4, 0 ); //dns_write_record( &response, MFi_SERVICE_QUERY_NAME, RR_CLASS_IN, RR_TYPE_PTR, 1500, (u8*) available_services[b].service_name ); dns_write_record( &response, available_services[b].service_name, RR_CACHE_FLUSH|RR_CLASS_IN, RR_TYPE_PTR, 1500, (u8*) available_services[b].instance_name ); dns_write_record( &response, available_services[b].instance_name, RR_CACHE_FLUSH|RR_CLASS_IN, RR_TYPE_TXT, 1500, (u8*) available_services[b].txt_att ); dns_write_record( &response, available_services[b].instance_name, RR_CACHE_FLUSH|RR_CLASS_IN, RR_TYPE_SRV, 1500, (u8*) &available_services[b]); dns_write_record( &response, available_services[b].hostname, RR_CACHE_FLUSH|RR_CLASS_IN, RR_TYPE_A, 1500, (u8*) &myip); mdns_send_message(fd, &response ); dns_free_message( &response ); question_processed = 1; } else{ _debug_out("UDP multicast test: Send error.\r\n"); } } } } } break; } if (!question_processed ){ _debug_out("UDP multicast test: Recv a mDNS request.\r\n"); mdns_process_query(fd, &name, &question, iter); } } }