static void lua_main_thread(void *data) { //lua setup char *argv[] = {"lua", NULL}; lua_main(1, argv); //if error happened lua_printf("lua exited,will reboot\r\n"); free(lua_rx_data); lua_rx_data = NULL; mico_rtos_delete_thread(NULL); MicoSystemReboot(); }
/* user main thread created by MICO APP thread */ void user_main_thread(void* arg) { OSStatus err = kUnknownErr; mico_Context_t *mico_context = (mico_Context_t *)arg; #if (MICO_CLOUD_TYPE != CLOUD_DISABLED) // wait semaphore for cloud connection //mico_fogcloud_waitfor_connect(mico_context, MICO_WAIT_FOREVER); // block to wait fogcloud connect //app_log("Cloud connected, call user_main function."); #endif // loop in user mian function && must not return err = user_main(mico_context); UNUSED_PARAMETER(err); // never get here only if user work error. app_log("ERROR: user_main thread exit err=%d, system reboot...", err); MicoSystemReboot(); }
void EasyLinkNotify_EasyLinkCompleteHandler(network_InitTypeDef_st *nwkpara, mico_Context_t * const inContext) { OSStatus err; easylink_log_trace(); easylink_log("EasyLink return"); require_action(inContext, exit, err = kParamErr); require_action(nwkpara, exit, err = kTimeoutErr); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); memcpy(inContext->flashContentInRam.micoSystemConfig.ssid, nwkpara->wifi_ssid, maxSsidLen); memset(inContext->flashContentInRam.micoSystemConfig.bssid, 0x0, 6); memcpy(inContext->flashContentInRam.micoSystemConfig.user_key, nwkpara->wifi_key, maxKeyLen); inContext->flashContentInRam.micoSystemConfig.user_keyLength = strlen(nwkpara->wifi_key); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); easylink_log("Get SSID: %s, Key: %s", inContext->flashContentInRam.micoSystemConfig.ssid, inContext->flashContentInRam.micoSystemConfig.user_key); return; /*EasyLink timeout or error*/ exit: easylink_log("ERROR, err: %d", err); #if ( MICO_CONFIG_MODE == CONFIG_MODE_EASYLINK_WITH_SOFTAP) EasylinkFailed = true; mico_rtos_set_semaphore(&easylink_sem); #else ConfigWillStop(inContext); /*so roll back to previous settings (if it has) and reboot*/ mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); if(inContext->flashContentInRam.micoSystemConfig.configured != unConfigured){ inContext->flashContentInRam.micoSystemConfig.configured = allConfigured; MICOUpdateConfiguration(inContext); MicoSystemReboot(); } mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); /*module should powd down in default setting*/ micoWlanPowerOff(); #endif return; }
static void _sys_state_thread(void *arg) { UNUSED_PARAMETER(arg); mico_Context_t* context = arg; /*System status changed*/ while(mico_rtos_get_semaphore( &context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER) == kNoErr ) { if(needs_update == true) mico_system_context_update( context ); switch( context->micoStatus.current_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: sendNotifySYSWillPowerOff( ); mico_thread_msleep( 500 ); micoWlanPowerOff( ); MicoSystemStandBy( MICO_WAIT_FOREVER ); break; default: break; } } mico_rtos_delete_thread( NULL ); }
/** * @brief Display the Main Menu on HyperTerminal * @param None * @retval None */ void Main_Menu(void) { char cmdbuf [CMD_STRING_SIZE] = {0}, cmdname[15] = {0}; /* command input buffer */ int i, j; /* index for command buffer */ int targetFlash; char startAddressStr[10], endAddressStr[10]; int32_t startAddress, endAddress; bool inputFlashArea = false; while (1) { /* loop forever */ printf ("\n\rWiFiMCU> "); getline (&cmdbuf[0], sizeof (cmdbuf)); /* input command line */ for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for (; cmdbuf[i] != 0; i++) { /* convert to upper characters */ cmdbuf[i] = toupper(cmdbuf[i]); } for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for(j=0; cmdbuf[i] != ' '&&cmdbuf[i] != 0; i++,j++) { /* find command name */ cmdname[j] = cmdbuf[i]; } cmdname[j] = '\0'; /***************** Command "0" or "BOOTUPDATE": Update the application *************************/ if(strcmp(cmdname, "BOOTUPDATE") == 0 || strcmp(cmdname, "0") == 0) { if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead Bootloader only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_BOOT); SerialUpload(MICO_FLASH_FOR_BOOT, BOOT_START_ADDRESS, "BootLoaderImage.bin", BOOT_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_BOOT); continue; } printf ("\n\rUpdating Bootloader...\n\r"); SerialDownload(MICO_FLASH_FOR_BOOT, BOOT_START_ADDRESS, BOOT_FLASH_SIZE); } /***************** Command "1" or "FWUPDATE": Update the MICO application *************************/ else if(strcmp(cmdname, "FWUPDATE") == 0 || strcmp(cmdname, "1") == 0) { if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead MICO application only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_APPLICATION); SerialUpload(MICO_FLASH_FOR_APPLICATION, APPLICATION_START_ADDRESS, "ApplicationImage.bin", APPLICATION_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_APPLICATION); continue; } printf ("\n\rUpdating MICO application...\n\r"); SerialDownload(MICO_FLASH_FOR_APPLICATION, APPLICATION_START_ADDRESS, APPLICATION_FLASH_SIZE); } /***************** Command "2" or "DRIVERUPDATE": Update the RF driver *************************/ else if(strcmp(cmdname, "DRIVERUPDATE") == 0 || strcmp(cmdname, "2") == 0) { #ifdef MICO_FLASH_FOR_DRIVER if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead RF driver only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_DRIVER); SerialUpload(MICO_FLASH_FOR_DRIVER, DRIVER_START_ADDRESS, "DriverImage.bin", DRIVER_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_DRIVER); continue; } printf ("\n\rUpdating RF driver...\n\r"); SerialDownload(MICO_FLASH_FOR_DRIVER, DRIVER_START_ADDRESS, DRIVER_FLASH_SIZE); #else printf ("\n\rNo independ flash memory for RF driver, exiting...\n\r"); #endif } /***************** Command "3" or "PARAUPDATE": Update the application *************************/ else if(strcmp(cmdname, "PARAUPDATE") == 0 || strcmp(cmdname, "3") == 0) { if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing MICO settings only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_PARA); MicoFlashErase(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, PARA_END_ADDRESS); MicoFlashFinalize(MICO_FLASH_FOR_PARA); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead MICO settings only...\n\r"); MicoFlashInitialize(MICO_FLASH_FOR_PARA); SerialUpload(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, "DriverImage.bin", PARA_FLASH_SIZE); MicoFlashFinalize(MICO_FLASH_FOR_PARA); continue; } printf ("\n\rUpdating MICO settings...\n\r"); SerialDownload(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, PARA_FLASH_SIZE); } /***************** Command "4" or "FLASHUPDATE": : Update the Flash *************************/ else if(strcmp(cmdname, "FLASHUPDATE") == 0 || strcmp(cmdname, "4") == 0) { if (findCommandPara(cmdbuf, "i", NULL, 0) != -1){ targetFlash = MICO_INTERNAL_FLASH; }else if(findCommandPara(cmdbuf, "s", NULL, 200) != -1){ targetFlash = MICO_SPI_FLASH; }else{ printf ("\n\rUnkown target type! Exiting...\n\r"); continue; } inputFlashArea = false; if (findCommandPara(cmdbuf, "start", startAddressStr, 10) != -1){ if(Str2Int((uint8_t *)startAddressStr, &startAddress)==0){ //Found Flash start address printf ("\n\rIllegal start address.\n\r"); continue; }else{ if (findCommandPara(cmdbuf, "end", endAddressStr, 10) != -1){ //Found Flash end address if(Str2Int((uint8_t *)endAddressStr, &endAddress)==0){ printf ("\n\rIllegal end address.\n\r"); continue; }else{ inputFlashArea = true; } }else{ printf ("\n\rFlash end address not found.\n\r"); continue; } } } if(endAddress<startAddress && inputFlashArea == true) { printf ("\n\rIllegal flash address.\n\r"); continue; } if(inputFlashArea != true){ if(targetFlash == MICO_INTERNAL_FLASH){ startAddress = platform_flash_peripherals[MICO_INTERNAL_FLASH].flash_start_addr ; endAddress = platform_flash_peripherals[MICO_INTERNAL_FLASH].flash_start_addr + platform_flash_peripherals[MICO_INTERNAL_FLASH].flash_length - 1; }else{ #ifdef USE_MICO_SPI_FLASH startAddress = platform_flash_peripherals[MICO_SPI_FLASH].flash_start_addr ; endAddress = platform_flash_peripherals[MICO_SPI_FLASH].flash_start_addr + platform_flash_peripherals[MICO_SPI_FLASH].flash_length - 1; #else printf ("\n\rSPI Flash not exist\n\r"); continue; #endif } } if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing flash content From 0x%x to 0x%x\n\r", startAddress, endAddress); MicoFlashInitialize((mico_flash_t)targetFlash); MicoFlashErase((mico_flash_t)targetFlash, startAddress, endAddress); MicoFlashFinalize((mico_flash_t)targetFlash); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead flash content From 0x%x to 0x%x\n\r", startAddress, endAddress); MicoFlashInitialize((mico_flash_t)targetFlash); SerialUpload((mico_flash_t)targetFlash, startAddress, "FlashImage.bin", endAddress-startAddress+1); MicoFlashFinalize((mico_flash_t)targetFlash); continue; } printf ("\n\rUpdating flash content From 0x%x to 0x%x\n\r", startAddress, endAddress); SerialDownload((mico_flash_t)targetFlash, startAddress, endAddress-startAddress+1); } /***************** Command: Reboot *************************/ else if(strcmp(cmdname, "MEMORYMAP") == 0 || strcmp(cmdname, "5") == 0) { #if defined MICO_FLASH_FOR_UPDATE && defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_UPDATE], UPDATE_START_ADDRESS, UPDATE_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_DRIVER], DRIVER_START_ADDRESS, DRIVER_END_ADDRESS); #endif #if !defined MICO_FLASH_FOR_UPDATE && defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_DRIVER], DRIVER_START_ADDRESS, DRIVER_END_ADDRESS); #endif #if defined MICO_FLASH_FOR_UPDATE && !defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS); #endif #if !defined MICO_FLASH_FOR_UPDATE && !defined MICO_FLASH_FOR_DRIVER printf(MEMMAP, flash_name[MICO_FLASH_FOR_BOOT],BOOT_START_ADDRESS,BOOT_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_PARA], PARA_START_ADDRESS, PARA_END_ADDRESS,\ flash_name[MICO_FLASH_FOR_APPLICATION], APPLICATION_START_ADDRESS, APPLICATION_END_ADDRESS); #endif } /***************** Command: Excute the application *************************/ else if(strcmp(cmdname, "BOOT") == 0 || strcmp(cmdname, "6") == 0) { printf ("\n\rBooting.......\n\r"); startApplication(); } /***************** Command: Reboot *************************/ else if(strcmp(cmdname, "REBOOT") == 0 || strcmp(cmdname, "7") == 0) { printf ("\n\rReBooting.......\n\r"); MicoSystemReboot(); break; } else if(strcmp(cmdname, "HELP") == 0 || strcmp(cmdname, "?") == 0) { printf ( menu, MODEL, Bootloader_REVISION ); /* display command menu */ break; } else if(strcmp(cmdname, "") == 0 ) { break; } else{ printf (ERROR_STR, "UNKNOWN COMMAND"); break; } } }
static int mcu_reboot( lua_State* L ) { MicoSystemReboot(); return 0; }
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; }
/** * @brief Display the Main Menu on HyperTerminal * @param None * @retval None */ void Main_Menu(void) { char cmdbuf [CMD_STRING_SIZE] = {0}, cmdname[15] = {0}; /* command input buffer */ int i, j; /* index for command buffer */ char startAddressStr[10], endAddressStr[10], flash_dev_str[4]; int32_t startAddress, endAddress; bool inputFlashArea = false; mico_logic_partition_t *partition; mico_flash_t flash_dev; OSStatus err = kNoErr; while (1) { /* loop forever */ printf ("\n\rMXCHIP> "); getline (&cmdbuf[0], sizeof (cmdbuf)); /* input command line */ for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for (; cmdbuf[i] != 0; i++) { /* convert to upper characters */ cmdbuf[i] = toupper(cmdbuf[i]); } for (i = 0; cmdbuf[i] == ' '; i++); /* skip blanks on head */ for(j=0; cmdbuf[i] != ' '&&cmdbuf[i] != 0; i++,j++) { /* find command name */ cmdname[j] = cmdbuf[i]; } cmdname[j] = '\0'; /***************** Command "0" or "BOOTUPDATE": Update the application *************************/ if(strcmp(cmdname, "BOOTUPDATE") == 0 || strcmp(cmdname, "0") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_BOOTLOADER ); if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead Bootloader...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "BootLoaderImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating Bootloader...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_BOOTLOADER, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "1" or "FWUPDATE": Update the MICO application *************************/ else if(strcmp(cmdname, "FWUPDATE") == 0 || strcmp(cmdname, "1") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_APPLICATION ); if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead application...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "ApplicationImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating application...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_APPLICATION, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "2" or "DRIVERUPDATE": Update the RF driver *************************/ else if(strcmp(cmdname, "DRIVERUPDATE") == 0 || strcmp(cmdname, "2") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_RF_FIRMWARE ); if( partition == NULL ){ printf ("\n\rNo flash memory for RF firmware, exiting...\n\r"); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead RF firmware...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "DriverImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating RF driver...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_RF_FIRMWARE, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "3" or "PARAUPDATE": Update the application *************************/ else if(strcmp(cmdname, "PARAUPDATE") == 0 || strcmp(cmdname, "3") == 0) { partition = MicoFlashGetInfo( MICO_PARTITION_PARAMETER_1 ); if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing settings...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_PARAMETER_1, 0x0, partition->partition_length ); require_noerr( err, exit); MicoFlashErase( MICO_PARTITION_PARAMETER_1, 0x0, partition->partition_length ); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead settings...\n\r"); SerialUpload( partition->partition_owner, partition->partition_start_addr, "DriverImage.bin", partition->partition_length ); continue; } printf ("\n\rUpdating settings...\n\r"); err = MicoFlashDisableSecurity( MICO_PARTITION_PARAMETER_1, 0x0, partition->partition_length ); require_noerr( err, exit); SerialDownload( partition->partition_owner, partition->partition_start_addr, partition->partition_length ); } /***************** Command "4" or "FLASHUPDATE": : Update the Flash *************************/ else if(strcmp(cmdname, "FLASHUPDATE") == 0 || strcmp(cmdname, "4") == 0) { if (findCommandPara(cmdbuf, "dev", flash_dev_str, 1) == -1 ){ printf ("\n\rUnkown target type! Exiting...\n\r"); continue; } if(Str2Int((uint8_t *)flash_dev_str, (int32_t *)&flash_dev)==0){ printf ("\n\rDevice Number Err! Exiting...\n\r"); continue; } if( flash_dev >= MICO_FLASH_MAX ){ printf ("\n\rDevice Err! Exiting...\n\r"); continue; } inputFlashArea = false; if (findCommandPara(cmdbuf, "start", startAddressStr, 10) != -1){ if(Str2Int((uint8_t *)startAddressStr, &startAddress)==0){ //Found Flash start address printf ("\n\rIllegal start address.\n\r"); continue; }else{ if (findCommandPara(cmdbuf, "end", endAddressStr, 10) != -1){ //Found Flash end address if(Str2Int((uint8_t *)endAddressStr, &endAddress)==0){ printf ("\n\rIllegal end address.\n\r"); continue; }else{ inputFlashArea = true; } }else{ printf ("\n\rFlash end address not found.\n\r"); continue; } } } if(endAddress<startAddress && inputFlashArea == true) { printf ("\n\rIllegal address.\n\r"); continue; } if(inputFlashArea != true){ startAddress = platform_flash_peripherals[ flash_dev ].flash_start_addr ; endAddress = platform_flash_peripherals[ flash_dev ].flash_start_addr + platform_flash_peripherals[ flash_dev ].flash_length - 1; } if (findCommandPara(cmdbuf, "e", NULL, 0) != -1){ printf ("\n\rErasing dev%d content From 0x%x to 0x%x\n\r", flash_dev, startAddress, endAddress); platform_flash_init( &platform_flash_peripherals[ flash_dev ] ); platform_flash_disable_protect( &platform_flash_peripherals[ flash_dev ], startAddress, endAddress ); platform_flash_erase( &platform_flash_peripherals[ flash_dev ], startAddress, endAddress ); continue; } if (findCommandPara(cmdbuf, "r", NULL, 0) != -1){ printf ("\n\rRead dev%d content From 0x%x to 0x%x\n\r", flash_dev, startAddress, endAddress); SerialUpload(flash_dev, startAddress, "FlashImage.bin", endAddress-startAddress+1); continue; } printf ("\n\rUpdating dev%d content From 0x%x to 0x%x\n\r", flash_dev, startAddress, endAddress); platform_flash_disable_protect( &platform_flash_peripherals[ flash_dev ], startAddress, endAddress ); SerialDownload(flash_dev, startAddress, endAddress-startAddress+1); } /***************** Command: MEMORYMAP *************************/ else if(strcmp(cmdname, "MEMORYMAP") == 0 || strcmp(cmdname, "5") == 0) { printf("\r"); for( i = 0; i <= MICO_PARTITION_PARAMETER_2; i++ ){ partition = MicoFlashGetInfo( (mico_partition_t)i ); if (partition->partition_owner == MICO_FLASH_NONE) continue; printf( "| %11s | Dev:%d | 0x%08x | 0x%08x |\r\n", partition->partition_description, partition->partition_owner, partition->partition_start_addr, partition->partition_length); } } /***************** Command: Excute the application *************************/ else if(strcmp(cmdname, "BOOT") == 0 || strcmp(cmdname, "6") == 0) { printf ("\n\rBooting.......\n\r"); partition = MicoFlashGetInfo( MICO_PARTITION_APPLICATION ); bootloader_start_app( partition->partition_start_addr ); } /***************** Command: Reboot *************************/ else if(strcmp(cmdname, "REBOOT") == 0 || strcmp(cmdname, "7") == 0) { printf ("\n\rReBooting.......\n\r"); MicoSystemReboot(); break; } else if(strcmp(cmdname, "HELP") == 0 || strcmp(cmdname, "?") == 0) { printf ( menu, MODEL, Bootloader_REVISION, HARDWARE_REVISION ); /* display command menu */ break; } else if(strcmp(cmdname, "") == 0 ) { break; } else{ printf (ERROR_STR, "UNKNOWN COMMAND"); break; } exit: continue; } }
void easylink_thread(void *inContext) { OSStatus err = kNoErr; mico_Context_t *Context = inContext; fd_set readfds; int reConnCount = 0; int clientFdIsSet; easylink_log_trace(); require_action(easylink_sem, threadexit, err = kNotPreparedErr); if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_BYPASS){ Context->flashContentInRam.micoSystemConfig.easyLinkByPass = EASYLINK_BYPASS_NO; MICOUpdateConfiguration(Context); _easylinkConnectWiFi_fast(Context); }else if(Context->flashContentInRam.micoSystemConfig.easyLinkByPass == EASYLINK_SOFT_AP_BYPASS){ ConfigWillStop( Context ); _easylinkStartSoftAp(Context); mico_rtos_delete_thread(NULL); return; }else{ #ifdef EasyLink_Plus easylink_log("Start easylink plus mode"); micoWlanStartEasyLinkPlus(EasyLink_TimeOut/1000); #else easylink_log("Start easylink V2"); micoWlanStartEasyLink(EasyLink_TimeOut/1000); #endif mico_rtos_get_semaphore(&easylink_sem, MICO_WAIT_FOREVER); if(EasylinkFailed == false) _easylinkConnectWiFi(Context); else{ msleep(20); _cleanEasyLinkResource( Context ); ConfigWillStop( Context ); _easylinkStartSoftAp(Context); mico_rtos_delete_thread(NULL); return; } } err = mico_rtos_get_semaphore(&easylink_sem, EasyLink_ConnectWlan_Timeout); require_noerr(err, reboot); httpHeader = HTTPHeaderCreate(); require_action( httpHeader, threadexit, err = kNoMemoryErr ); HTTPHeaderClear( httpHeader ); while(1){ if(easylinkClient_fd == -1){ err = _connectFTCServer(inContext, &easylinkClient_fd); require_noerr(err, Reconn); }else{ FD_ZERO(&readfds); FD_SET(easylinkClient_fd, &readfds); if(httpHeader->len == 0){ err = select(1, &readfds, NULL, NULL, NULL); require(err>=1, threadexit); clientFdIsSet = FD_ISSET(easylinkClient_fd, &readfds); } if(clientFdIsSet||httpHeader->len){ err = SocketReadHTTPHeader( easylinkClient_fd, httpHeader ); switch ( err ) { case kNoErr: // Read the rest of the HTTP body if necessary do{ err = SocketReadHTTPBody( easylinkClient_fd, httpHeader ); require_noerr(err, Reconn); PrintHTTPHeader(httpHeader); // Call the HTTPServer owner back with the acquired HTTP header err = _FTCRespondInComingMessage( easylinkClient_fd, httpHeader, Context ); require_noerr( err, Reconn ); if(httpHeader->contentLength == 0) break; } while( httpHeader->chunkedData == true || httpHeader->dataEndedbyClose == true); // Reuse HTTPHeader HTTPHeaderClear( httpHeader ); break; case EWOULDBLOCK: // NO-OP, keep reading break; case kNoSpaceErr: easylink_log("ERROR: Cannot fit HTTPHeader."); goto Reconn; case kConnectionErr: // NOTE: kConnectionErr from SocketReadHTTPHeader means it's closed easylink_log("ERROR: Connection closed."); /*Roll back to previous settings (if it has) and reboot*/ if(Context->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured ){ Context->flashContentInRam.micoSystemConfig.configured = allConfigured; MICOUpdateConfiguration( Context ); MicoSystemReboot(); }else{ micoWlanPowerOff(); msleep(20); } goto threadexit; default: easylink_log("ERROR: HTTP Header parse internal error: %d", err); goto Reconn; } } } continue; Reconn: HTTPHeaderClear( httpHeader ); SocketClose(&easylinkClient_fd); easylinkClient_fd = -1; require(reConnCount < 6, reboot); reConnCount++; sleep(5); } /*Module is ignored by FTC server, */ threadexit: _cleanEasyLinkResource( Context ); ConfigWillStop( Context ); mico_rtos_delete_thread( NULL ); return; /*SSID or Password is not correct, module cannot connect to wlan, so reboot and enter EasyLink again*/ reboot: MicoSystemReboot(); return; }