static void _EasyLinkNotify_WifiStatusHandler(WiFiEvent event, mico_Context_t * const inContext) { easylink_uap_log_trace(); IPStatusTypedef para; require(inContext, exit); switch (event) { case NOTIFY_STATION_UP: micoWlanGetIPStatus(¶, Station); strncpy(inContext->flashContentInRam.micoSystemConfig.localIp, para.ip, maxIpLen); strncpy(inContext->flashContentInRam.micoSystemConfig.netMask, para.mask, maxIpLen); strncpy(inContext->flashContentInRam.micoSystemConfig.gateWay, para.gate, maxIpLen); strncpy(inContext->flashContentInRam.micoSystemConfig.dnsServer, para.dns, maxIpLen); _initBonjourForEasyLink( Station, inContext ); suspend_bonjour_service(false); break; case NOTIFY_STATION_DOWN: suspend_bonjour_service(true); break; default: break; } exit: return; }
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; IPStatusTypedef para; uint32_t myip; int b = 0; memset( &response, 0, sizeof(dns_message_iterator_t) ); switch ( question->question_type ) { case RR_QTYPE_ANY: case RR_TYPE_A: for ( b = 0; b < available_service_count; ++b ){ if ( dns_compare_name_to_string( name, available_services[b].hostname ) ){ micoWlanGetIPStatus(¶, available_services[b].interface); myip = htonl(inet_addr(para.ip)); if( myip == 0 || myip == 0xFFFFFFFF) continue; if(dns_create_message( &response, 256 )){ dns_write_header( &response, source->header->id, 0x8400, 0, 1, 0 ); dns_write_record( &response, available_services[b].hostname, RR_CLASS_IN | RR_CACHE_FLUSH, RR_TYPE_A, available_services[b].ttl, (uint8_t *)&myip); mdns_send_message(fd, &response ); dns_free_message( &response ); return; } } } default: break;; } }
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; IPStatusTypedef para; uint32_t myip; micoWlanGetIPStatus(¶, _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, (uint8_t *)&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); } }
static int lwifi_ap_getip( lua_State* L ) { net_para_st para; micoWlanGetIPStatus(¶, Soft_AP); lua_pushstring(L,para.ip); return 1; }
int application_start( void ) { OSStatus err = kNoErr; IPStatusTypedef para; MicoInit( ); /*The notification message for the registered WiFi status change*/ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = mico_rtos_init_semaphore(&tcp_sem, 1); require_noerr( err, exit ); connect_ap( ); mico_rtos_get_semaphore(&tcp_sem, MICO_WAIT_FOREVER); micoWlanGetIPStatus(¶, Station); tcp_server_log("tcp server ip: %s", para.ip); err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "TCP_server", tcp_server_thread, 0x800, NULL ); require_noerr_action( err, exit, tcp_server_log("ERROR: Unable to start the tcp server thread.") ); return err; exit: tcp_server_log("ERROR, err: %d", err); return err; }
void micoNotify_WifiStatusHandler(WiFiEvent event, void* const inContext) { IPStatusTypedef para; switch (event) { case NOTIFY_STATION_UP: micoWlanGetIPStatus(¶, Station); tcp_server_log("Server established at ip: %s port: %d",para.ip, SERVER_PORT); break; case NOTIFY_STATION_DOWN: break; } }
void micoNotify_WifiStatusHandler(WiFiEvent event, void* const inContext) { IPStatusTypedef para; switch (event) { case NOTIFY_STATION_UP: micoWlanGetIPStatus(¶, Station); udp_unicast_log( "Wlan connected, Local ip address: %s", para.ip ); break; case NOTIFY_STATION_DOWN: break; } }
static int mcu_wifiinfo( lua_State* L ) { char wifi_ver[64] = {0}; IPStatusTypedef para; char mac[18]; MicoGetRfVer(wifi_ver, sizeof(wifi_ver)); micoWlanGetIPStatus(¶, Station); formatMACAddr(mac, (char *)¶.mac); lua_pushstring(L,MicoGetVer());//mxchipWNet library version lua_pushstring(L,mac);//mac //lua_pushstring(L,mico_get_bootloader_ver());//bootloader_ver lua_pushstring(L,wifi_ver);//Wi-Fi driver version return 3; }
static int lwifi_ap_getipadv( lua_State* L ) { net_para_st para; micoWlanGetIPStatus(¶, Soft_AP); char temp[32]={0}; strcpy(temp,"DHCP_Server"); lua_pushstring(L,temp); lua_pushstring(L,para.ip); lua_pushstring(L,para.gate); lua_pushstring(L,para.mask); lua_pushstring(L,para.dns); lua_pushstring(L,para.mac); lua_pushstring(L,para.broadcastip); return 7; }
static int lwifi_station_getipadv( lua_State* L ) { net_para_st para; micoWlanGetIPStatus(¶, Station); char temp[32]={0}; if(para.dhcp ==DHCP_Client) strcpy(temp,"DHCP_Client"); else if(para.dhcp ==DHCP_Server) strcpy(temp,"DHCP_Server"); else strcpy(temp,"DHCP_Disable"); lua_pushstring(L,temp); lua_pushstring(L,para.ip); lua_pushstring(L,para.gate); lua_pushstring(L,para.mask); lua_pushstring(L,para.dns); lua_pushstring(L,para.mac); lua_pushstring(L,para.broadcastip); return 7; }
OSStatus system_easylink_wac_start( mico_Context_t * const inContext ) { OSStatus err = kNoErr; IPStatusTypedef para; uint8_t major_ver, minor_ver, revision; mfi_wac_lib_version( &major_ver, &minor_ver, &revision ); system_log( "Import MFi WAC library v%d.%d.%d", major_ver, minor_ver, revision ); WACPlatformParameters_t* WAC_Params = NULL; WAC_Params = calloc(1, sizeof(WACPlatformParameters_t)); require(WAC_Params, exit); micoWlanGetIPStatus(¶, Station); 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 = inContext->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 = mfi_wac_start( inContext, WAC_Params, MICO_I2C_CP, 1200 ); require_noerr(err, exit); exit: free(WAC_Params); return err; }
void EasyLinkNotify_WifiStatusHandler(WiFiEvent event, mico_Context_t * const inContext) { easylink_log_trace(); IPStatusTypedef para; require(inContext, exit); switch (event) { case NOTIFY_STATION_UP: mico_rtos_set_semaphore(&easylink_sem); micoWlanGetIPStatus(¶, Station); strncpy(inContext->flashContentInRam.micoSystemConfig.localIp, para.ip, maxIpLen); strncpy(inContext->flashContentInRam.micoSystemConfig.netMask, para.mask, maxIpLen); strncpy(inContext->flashContentInRam.micoSystemConfig.gateWay, para.gate, maxIpLen); strncpy(inContext->flashContentInRam.micoSystemConfig.dnsServer, para.dns, maxIpLen); break; default: break; } exit: return; }
OSStatus system_network_daemen_start( mico_Context_t * const inContext ) { IPStatusTypedef para; MicoInit(); MicoSysLed(true); system_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory); micoWlanGetIPStatus(¶, Station); formatMACAddr(inContext->micoStatus.mac, (char *)¶.mac); MicoGetRfVer(inContext->micoStatus.rf_version, sizeof(inContext->micoStatus.rf_version)); inContext->micoStatus.rf_version[49] = 0x0; system_log("MiCO library version: %s", MicoGetVer()); system_log("Wi-Fi driver version %s, mac %s", inContext->micoStatus.rf_version, inContext->micoStatus.mac); if(inContext->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){ micoWlanEnablePowerSave(); } if(inContext->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ MicoMcuPowerSaveConfig(true); } return kNoErr; }
static OSStatus _initBonjourForEasyLink( WiFi_Interface interface, mico_Context_t * const inContext ) { char *temp_txt= NULL; char *temp_txt2; OSStatus err; net_para_st para; bonjour_init_t init; temp_txt = malloc(500); require_action(temp_txt, exit, err = kNoMemoryErr); memset(&init, 0x0, sizeof(bonjour_init_t)); micoWlanGetIPStatus(¶, Station); init.service_name = "_easylink_config._tcp.local."; /* 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 = FTC_PORT; init.interface = interface; temp_txt2 = __strdup_trans_dot(inContext->micoStatus.mac); sprintf(temp_txt, "MAC=%s.", temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(FIRMWARE_REVISION); sprintf(temp_txt, "%sFirmware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(HARDWARE_REVISION); sprintf(temp_txt, "%sHardware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MicoGetVer()); sprintf(temp_txt, "%sMICO OS Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MODEL); sprintf(temp_txt, "%sModel=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(PROTOCOL); sprintf(temp_txt, "%sProtocol=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MANUFACTURER); sprintf(temp_txt, "%sManufacturer=%s.", temp_txt, temp_txt2); free(temp_txt2); if(interface == Soft_AP) sprintf(temp_txt, "%swlan unconfigured=T.", temp_txt); else sprintf(temp_txt, "%swlan unconfigured=F.", temp_txt); 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_SYS_WILL_POWER_OFF, (void *)_EasyLinkNotify_SYSWillPoerOffHandler ); require_noerr( err, exit ); exit: if(temp_txt) free(temp_txt); return err; }
void process_dns_questions(int fd, dns_message_iterator_t* iter ) { dns_name_t name; dns_question_t question; dns_message_iterator_t response; IPStatusTypedef para; int a = 0; int question_processed; uint32_t myip; 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, SERVICE_QUERY_NAME ) ){ int b = 0; 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, SERVICE_QUERY_NAME, RR_CLASS_IN, RR_TYPE_PTR, 1500, (uint8_t*) 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 )){ if( available_services[b].state != RECORD_NORMAL ) continue; micoWlanGetIPStatus(¶, available_services[b].interface); myip = htonl(inet_addr(para.ip)); if( myip == 0 || myip == 0xFFFFFFFF) continue; // 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_CLASS_IN, RR_TYPE_PTR, available_services[b].ttl, (uint8_t*) available_services[b].instance_name ); dns_write_record( &response, available_services[b].instance_name, RR_CACHE_FLUSH|RR_CLASS_IN, RR_TYPE_TXT, available_services[b].ttl, (uint8_t*) available_services[b].txt_att ); dns_write_record( &response, available_services[b].instance_name, RR_CACHE_FLUSH|RR_CLASS_IN, RR_TYPE_SRV, available_services[b].ttl, (uint8_t*) &available_services[b]); dns_write_record( &response, available_services[b].hostname, RR_CACHE_FLUSH|RR_CLASS_IN, RR_TYPE_A, available_services[b].ttl, (uint8_t*) &myip); mdns_send_message(fd, &response ); dns_free_message( &response ); question_processed = 1; } } } } } break; } if (!question_processed ){ mdns_process_query(fd, &name, &question, iter); } } }
OSStatus MICOStartBonjourService( WiFi_Interface interface, mico_Context_t * const inContext ) { char *temp_txt= NULL; char *temp_txt2; OSStatus err; net_para_st para; bonjour_init_t init; temp_txt = malloc(500); require_action(temp_txt, exit, err = kNoMemoryErr); memset(&init, 0x0, sizeof(bonjour_init_t)); micoWlanGetIPStatus(¶, 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 = inContext->flashContentInRam.appConfig.localServerPort; init.interface = interface; temp_txt2 = __strdup_trans_dot(inContext->micoStatus.mac); sprintf(temp_txt, "MAC=%s.", temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(FIRMWARE_REVISION); sprintf(temp_txt, "%sFirmware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(HARDWARE_REVISION); sprintf(temp_txt, "%sHardware Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MicoGetVer()); sprintf(temp_txt, "%sMICO OS Rev=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MODEL); sprintf(temp_txt, "%sModel=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(PROTOCOL); sprintf(temp_txt, "%sProtocol=%s.", temp_txt, temp_txt2); free(temp_txt2); temp_txt2 = __strdup_trans_dot(MANUFACTURER); sprintf(temp_txt, "%sManufacturer=%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: if(temp_txt) free(temp_txt); return err; }
int application_start(void) { OSStatus err = kNoErr; IPStatusTypedef para; struct tm currentTime; mico_rtc_time_t time; char wifi_ver[64]; mico_log_trace(); #if 1 /*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 ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_Fatal_ERROR, (void *)micoNotify_WlanFatalErrHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_Stack_Overflow_ERROR, (void *)micoNotify_StackOverflowErrHandler ); require_noerr( err, exit ); #endif /*wlan driver and tcpip init*/ MicoInit(); MicoSysLed(true); /**********************add ethernet**********************/ add_ethernet(); //sleep(5000); /*********************************************************/ mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; /* Enter test mode, call a build-in test function amd output on STDIO */ if(MicoShouldEnterMFGMode()==true) mico_mfg_test(); /*Read current time from RTC.*/ MicoRtcGetTime(&time); currentTime.tm_sec = time.sec; currentTime.tm_min = time.min; currentTime.tm_hour = time.hr; currentTime.tm_mday = time.date; currentTime.tm_wday = time.weekday; currentTime.tm_mon = time.month - 1; currentTime.tm_year = time.year + 100; mico_log("Current Time: %s",asctime(¤tTime)); micoWlanGetIPStatus(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)¶.mac); MicoGetRfVer(wifi_ver, sizeof(wifi_ver)); mico_log("%s mxchipWNet library version: %s", APP_INFO, MicoGetVer()); mico_log("Wi-Fi driver version %s, mac %s", wifi_ver, context->micoStatus.mac); /*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); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); /*************add ethernet********Why add twice?***********/ //add_ethernet(); if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){ mico_log("Empty configuration. Starting configuration mode..."); #if (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK) || (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) err = startEasyLink( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_SOFT_AP) err = startEasyLinkSoftAP( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_AIRKISS) err = startAirkiss( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_WPS) || MICO_CONFIG_MODE == defined (CONFIG_MODE_WPS_WITH_SOFTAP) err = startWPS( context ); require_noerr( err, exit ); #elif ( MICO_CONFIG_MODE == 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 ); #else #error "Wi-Fi configuration mode is not defined"? #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); 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){ micoWlanEnablePowerSave(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ MicoMcuPowerSaveConfig(true); } /*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.") ); } err = MICOStartNTPClient(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the NTP client thread.") ); /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); _ConnectToAP( context ); } mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; /*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); MicoSystemReboot(); break; case eState_Wlan_Powerdown: sendNotifySYSWillPowerOff(); mico_thread_msleep(500); micoWlanPowerOff(); break; case eState_Standby: mico_log("Enter standby mode"); sendNotifySYSWillPowerOff(); mico_thread_msleep(200); micoWlanPowerOff(); MicoSystemStandBy(MICO_WAIT_FOREVER); 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; IPStatusTypedef para; struct tm currentTime; mico_rtc_time_t time; char wifi_ver[64] = {0}; mico_log_trace(); /*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);//ram互斥初始化 mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1);//系统状态信号量 mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "sys", _sys_state_thread, 800, NULL ); MICOReadConfiguration( context );//读flash数据 err = MICOInitNotificationCenter ( context ); err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_WIFI_Fatal_ERROR, (void *)micoNotify_WlanFatalErrHandler ); require_noerr( err, exit ); err = MICOAddNotification( mico_notify_Stack_Overflow_ERROR, (void *)micoNotify_StackOverflowErrHandler ); require_noerr( err, exit ); /*wlan driver and tcpip init*/ mico_log( "MiCO starting..." ); MicoInit(); #ifdef MICO_CLI_ENABLE MicoCliInit(); #endif MicoSysLed(true); mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory); micoWlanGetIPStatus(¶, Station); formatMACAddr(context->micoStatus.mac, (char *)para.mac); MicoGetRfVer(wifi_ver, sizeof(wifi_ver)); mico_log("ip = %s,mac=%s",para.ip,para.mac); mico_log("%s mxchipWNet library version: %s", APP_INFO, MicoGetVer()); mico_log("Wi-Fi driver version %s, mac %s", wifi_ver, context->micoStatus.mac); /*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/2, _watchdog_reload_timer_handler, NULL); mico_start_timer(&_watchdog_reload_timer); /* Enter test mode, call a build-in test function amd output on MFG UART */ if(MicoShouldEnterMFGMode()==true){ mico_log( "Enter MFG mode by MFG button" ); mico_mfg_test(context); } /*Read current time from RTC.*/ if( MicoRtcGetTime(&time) == kNoErr ){ currentTime.tm_sec = time.sec; currentTime.tm_min = time.min; currentTime.tm_hour = time.hr; currentTime.tm_mday = time.date; currentTime.tm_wday = time.weekday; currentTime.tm_mon = time.month - 1; currentTime.tm_year = time.year + 100; mico_log("Current Time: %s",asctime(¤tTime)); }else mico_log("RTC function unsupported"); /* Regisist notifications */ err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler ); require_noerr( err, exit ); if( context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured || context->flashContentInRam.micoSystemConfig.configured == unConfigured){ mico_log("Empty configuration. Starting configuration mode..."); //HERE TO config network #if (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK) || (MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) err = startEasyLink( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_SOFT_AP) err = startEasyLinkSoftAP( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_AIRKISS) err = startAirkiss( context ); require_noerr( err, exit ); #elif (MICO_CONFIG_MODE == CONFIG_MODE_WPS) || MICO_CONFIG_MODE == defined (CONFIG_MODE_WPS_WITH_SOFTAP) err = startWPS( context ); require_noerr( err, exit ); #elif ( MICO_CONFIG_MODE == 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, MICO_I2C_CP, 1200 ); free(WAC_Params); require_noerr( err, exit ); #else #error "Wi-Fi configuration mode is not defined" #endif } else{ mico_log("Available configuration. Starting Wi-Fi connection..."); _ConnectToAP( context ); } #ifdef MFG_MODE_AUTO if( context->flashContentInRam.micoSystemConfig.configured == mfgConfigured ){ mico_log( "Enter MFG mode automatically" ); mico_mfg_test(context); mico_thread_sleep(MICO_NEVER_TIMEOUT); } #endif 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){ micoWlanEnablePowerSave(); } if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){ MicoMcuPowerSaveConfig(true); } /*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.") ); } err = MICOStartNTPClient(context); require_noerr_action( err, exit, mico_log("ERROR: Unable to start the NTP client thread.") ); /*Start mico application*/ err = MICOStartApplication( context ); require_noerr( err, exit ); mico_log("Free memory %d bytes", MicoGetMemoryInfo()->free_memory) ; require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) ); exit: mico_rtos_delete_thread(NULL); return kNoErr; }
//--------------------------------------------------------------------- static void _getIPStatus(net_para_st* para, WiFi_Interface inInterface) { memset(para, 0x00, sizeof(net_para_st)); micoWlanGetIPStatus(para, inInterface); }