OSStatus MICORestoreDefault(mico_Context_t *inContext) { OSStatus err = kNoErr; uint32_t paraStartAddress, paraEndAddress; paraStartAddress = PARA_START_ADDRESS; paraEndAddress = PARA_END_ADDRESS; /*wlan configration is not need to change to a default state, use easylink to do that*/ sprintf(inContext->flashContentInRam.micoSystemConfig.name, DEFAULT_NAME); inContext->flashContentInRam.micoSystemConfig.configured = unConfigured; inContext->flashContentInRam.micoSystemConfig.easyLinkByPass = EASYLINK_BYPASS_NO; inContext->flashContentInRam.micoSystemConfig.rfPowerSaveEnable = false; inContext->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable = false; inContext->flashContentInRam.micoSystemConfig.bonjourEnable = true; inContext->flashContentInRam.micoSystemConfig.configServerEnable = true; inContext->flashContentInRam.micoSystemConfig.seed = seedNum; /*Application's default configuration*/ appRestoreDefault_callback(inContext); err = MicoFlashInitialize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, (void *)inContext, sizeof(flash_content_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); exit: return err; }
OSStatus MICORestoreMFG(mico_Context_t *inContext) { OSStatus err = kNoErr; uint32_t paraStartAddress, paraEndAddress; paraStartAddress = PARA_START_ADDRESS; paraEndAddress = PARA_END_ADDRESS; /*wlan configration is not need to change to a default state, use easylink to do that*/ sprintf(inContext->flashContentInRam.micoSystemConfig.name, DEFAULT_NAME); inContext->flashContentInRam.micoSystemConfig.configured = mfgConfigured; /*Application's default configuration*/ appRestoreDefault_callback(inContext); err = MicoFlashInitialize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, (void *)inContext, sizeof(flash_content_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); exit: return err; }
static OSStatus onReceivedData(struct _HTTPHeader_t * inHeader, uint32_t inPos, uint8_t * inData, size_t inLen, void * inUserContext ) { OSStatus err = kUnknownErr; const char * value; size_t valueSize; configContext_t *context = (configContext_t *)inUserContext; err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL ); if(err == kNoErr && strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0){ config_log("OTA data %d, %d to: %x", inPos, inLen, context->flashStorageAddress); #ifdef MICO_FLASH_FOR_UPDATE if(inPos == 0){ context->flashStorageAddress = UPDATE_START_ADDRESS; mico_rtos_lock_mutex(&Context->flashContentInRam_mutex); //We are write the Flash content, no other write is possiable context->isFlashLocked = true; err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE ); require_noerr(err, flashErrExit); err = MicoFlashErase(MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS); require_noerr(err, flashErrExit); err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &context->flashStorageAddress, (uint8_t *)inData, inLen); require_noerr(err, flashErrExit); }else{ err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &context->flashStorageAddress, (uint8_t *)inData, inLen); require_noerr(err, flashErrExit); } #else config_log("OTA storage is not exist"); return kUnsupportedErr; #endif } else if(inHeader->chunkedData == true){ config_log("ChunkedData: %d, %d:", inPos, inLen); for(uint32_t i = 0; i<inLen; i++) printf("%c", inData[i]); printf("\r\n"); } else{ return kUnsupportedErr; } if(err!=kNoErr) config_log("onReceivedData"); return err; #ifdef MICO_FLASH_FOR_UPDATE flashErrExit: MicoFlashFinalize(MICO_FLASH_FOR_UPDATE); mico_rtos_unlock_mutex(&Context->flashContentInRam_mutex); return err; #endif }
int application_start( void ) { #if MCU_POWERSAVE_ENABLED MicoMcuPowerSaveConfig(true); #endif power_log( "Power measure program: RTOS initialized and erase flash" ); MicoFlashInitialize( MICO_FLASH_FOR_UPDATE ); MicoFlashErase( MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS ); MicoFlashFinalize( MICO_FLASH_FOR_UPDATE ); mico_rtos_delete_thread( NULL ); return 0; }
/************************************************* * Function: HF_WriteDataToFlash * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ void MX_WriteDataToFlash(u8 *pu8Data, u16 u16Len) { mico_logic_partition_t *para_partition_info; OSStatus err = kNoErr; uint32_t para_offset = PARA_OFFSET; para_partition_info = MicoFlashGetInfo(MICO_PARTITION_PARAMETER_2); require_action( para_partition_info->partition_owner != MICO_FLASH_NONE, exit, err = kUnsupportedErr ); err = MicoFlashErase(MICO_PARTITION_PARAMETER_2 ,para_offset, para_partition_info->partition_length-PARA_OFFSET); require_noerr(err, exit); err = MicoFlashWrite( MICO_PARTITION_PARAMETER_2, ¶_offset, pu8Data, u16Len ); require_noerr(err, exit); exit: if(err != kNoErr) update_log("Update exit with err = %d", err); }
/************************************************* * Function: MX_FirmwareUpdate * Description: * Author: cxy * Returns: * Parameter: * History: *************************************************/ u32 MX_FirmwareUpdate(u8 *pu8FileData, u32 u32Offset, u32 u32DataLen) { #if 1 mico_logic_partition_t *para_partition_info; OSStatus err = kNoErr; if (0 == u32Offset) { para_partition_info = MicoFlashGetInfo(MICO_PARTITION_OTA_TEMP); require_action( para_partition_info->partition_owner != MICO_FLASH_NONE, exit, err = kUnsupportedErr ); err = MicoFlashErase(MICO_PARTITION_OTA_TEMP , 0, para_partition_info->partition_length ); require_noerr(err, exit); } err = MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &u32Offset, pu8FileData, u32DataLen ); require_noerr(err, exit); return ZC_RET_OK; exit: if(err != kNoErr) update_log("Update exit with err = %d", err); return ZC_RET_ERROR; #endif }
OSStatus HMUpdatePairList(pair_list_in_flash_t *pPairList) { OSStatus err = kNoErr; uint32_t exParaStartAddress, exParaEndAddress; exParaStartAddress = EX_PARA_START_ADDRESS; exParaEndAddress = EX_PARA_END_ADDRESS; err = MicoFlashInitialize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_EX_PARA, exParaStartAddress, exParaEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_EX_PARA, &exParaStartAddress, (uint8_t *)pPairList, sizeof(pair_list_in_flash_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); exit: return err; }
OSStatus MICOUpdateConfiguration(mico_Context_t *inContext) { OSStatus err = kNoErr; uint32_t paraStartAddress, paraEndAddress; paraStartAddress = PARA_START_ADDRESS; paraEndAddress = PARA_END_ADDRESS; inContext->flashContentInRam.micoSystemConfig.seed = ++seedNum; err = MicoFlashInitialize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_PARA, paraStartAddress, paraEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, (uint8_t *)&inContext->flashContentInRam, sizeof(flash_content_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_PARA); require_noerr(err, exit); exit: return err; }
OSStatus HMClearPairList(void) { OSStatus err = kNoErr; uint32_t exParaStartAddress, exParaEndAddress; exParaStartAddress = EX_PARA_START_ADDRESS; exParaEndAddress = EX_PARA_END_ADDRESS; pair_list_in_flash_t *pairList = NULL; pairList = calloc(1, sizeof(pair_list_in_flash_t)); require_action(pairList, exit, err = kNoMemoryErr); err = MicoFlashInitialize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_EX_PARA, exParaStartAddress, exParaEndAddress); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_EX_PARA, &exParaStartAddress, (uint8_t *)pairList, sizeof(pair_list_in_flash_t)); require_noerr(err, exit); err = MicoFlashFinalize(MICO_FLASH_FOR_EX_PARA); require_noerr(err, exit); exit: if(pairList) free(pairList); return err; }
//-------------------------------------------------- static s32_t lspiffs_erase(u32_t addr, u32_t size) { MicoFlashErase(MICO_PARTITION_LUA,addr,addr+size-1); luaWdgReload(); //in case wathdog return SPIFFS_OK; }
/** * @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; } } }
teZcbStatus ePDM_Init(mico_Context_t* mico_context) { OSStatus err; user_zigbeePDM_log("Create PDM lock"); mico_rtos_init_mutex(&sLock); mico_logic_partition_t *zigbeePDM_partition_info; //mico_logic_partition_t *p1_info; uint8_t read_test[100]= {0}; uint8_t i = 0; uint32_t dest_offset = 0; uint8_t data_write[6]= {0x06,0x05,0x04,0x03,0x02,0x01}; mico_rtos_lock_mutex(&sLock); #if 0 //init MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashInitialize(MICO_PARTITION_ZIGBEEPDM_TEMP); require_noerr(err, exit); // Get Info MICO_PARTITION_ZIGBEEPDM_TEMP zigbeePDM_partition_info = MicoFlashGetInfo(MICO_PARTITION_ZIGBEEPDM_TEMP); user_zigbeePDM_log("ZigBee PDM Partition info:start_addr:%x ,length:%x",zigbeePDM_partition_info->partition_start_addr,zigbeePDM_partition_info->partition_length); //Erase MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashErase( MICO_PARTITION_ZIGBEEPDM_TEMP, 0x0, zigbeePDM_partition_info->partition_length); require_noerr(err, exit); mico_thread_msleep(100); //sleep //MicoFlashWrite(mico_partition_t partition, volatile uint32_t * off_set, uint8_t * inBuffer, uint32_t inBufferLength); //Write MICO_PARTITION_ZIGBEEPDM_TEMP err = MicoFlashWrite(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, (uint8_t *)data_write, sizeof(data_write)); require_noerr(err, exit); #endif #if 0 mico_context -> user_config_data = (void*)data_write; mico_context -> user_config_data_size = 10; err = mico_system_context_update(mico_context); require_noerr(err, exit); mico_thread_msleep(1000); #endif #if 0 //Read dest_offset = 0; err = MicoFlashRead(MICO_PARTITION_ZIGBEEPDM_TEMP, &dest_offset, read_test, 5); require_noerr(err, exit); #endif #if 0 err = MicoFlashErase( MICO_PARTITION_PARAMETER_1, 0x0, 60); require_noerr(err, exit); mico_thread_msleep(10); err = MicoFlashWrite( MICO_PARTITION_PARAMETER_1, &dest_offset, "aaaaa", 5); require_noerr(err, exit); p1_info = MicoFlashGetInfo(MICO_PARTITION_PARAMETER_1); err = MicoFlashRead(MICO_PARTITION_PARAMETER_1, &dest_offset, read_test, 60); require_noerr(err, exit); #endif #if 0 //Output for(i = 0; i<5; i++) { printf("0x%x ",read_test[i]); } printf("\r\n"); #endif //MicoFlashWrite( MICO_PARTITION_OTA_TEMP, &context->offset, (uint8_t *)inData, inLen); //MicoFlashRead(MICO_PARTITION_OTA_TEMP, &flashaddr, (uint8_t *)md5_recv, 16); //err = MicoFlashDisableSecurity( MICO_PARTITION_OTA_TEMP, 0x0, ota_partition_info->partition_length ); //if (sqlite3_open_v2(pcPDMFile, &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) != SQLITE_OK) //{ // daemon_log(LOG_ERR, "Error initialising PDM database (%s)", sqlite3_errmsg(pDb)); // return E_ZCB_ERROR; //} //user_zigbeePDM_log("PDM Database opened\n"); { //const char *pcTableDef = "CREATE TABLE IF NOT EXISTS pdm (id INTEGER, size INTEGER, numblocks INTEGER, block INTEGER, blocksize INTEGER, data BLOB, PRIMARY KEY (id,block))"; //char *pcErr; //user_zigbeePDM_log("Execute SQL: '%s'\n", pcTableDef); //if (sqlite3_exec(pDb, pcTableDef, NULL, NULL, &pcErr) != SQLITE_OK) //{ // mico_log("Error creating table (%s)", pcErr); //sqlite3_free(pcErr); //mico_rtos_unlock_mutex(&sLock); //return E_ZCB_ERROR; //} } //user_zigbeePDM_log("PDM Database initialised\n"); //eSL_AddListener(E_SL_MSG_PDM_AVAILABLE_REQUEST, PDM_HandleAvailableRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_LOAD_RECORD_REQUEST, PDM_HandleLoadRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_SAVE_RECORD_REQUEST, PDM_HandleSaveRequest, NULL); //eSL_AddListener(E_SL_MSG_PDM_DELETE_ALL_RECORDS_REQUEST,PDM_HandleDeleteAllRequest, NULL); mico_rtos_unlock_mutex(&sLock); return E_ZCB_OK; exit: mico_rtos_unlock_mutex(&sLock); return err; }
//==================================== static int set_sparams( lua_State* L ) { uint8_t change = 0; const char* buf; uint32_t lua_param_offset = 0x0; uint8_t *p_id = &lua_system_param.ID; char *p_f = &lua_system_param.init_file[0]; if (!lua_istable(L, 1)) { l_message( NULL, "table arg needed" ); return 0; } lua_getfield(L, 1, "use_wwdg"); if (!lua_isnil(L, -1)) { // found? if( lua_isstring(L, -1) ) // deal with the string { uint8_t wd = luaL_checkinteger( L, -1 ); if (wd == 0) lua_system_param.use_wwdg = 0; else lua_system_param.use_wwdg = 1; l_message( NULL, "updated: use_wwdg" ); change++; } else { l_message( NULL, "wrong arg type: use_wwdg" ); } } lua_getfield(L, 1, "wdg_tmo"); if (!lua_isnil(L, -1)) { // found? if( lua_isstring(L, -1) ) // deal with the string { uint32_t wdtmo = luaL_checkinteger( L, -1 ); if (wdtmo < 2000 || wdtmo > 3600000) wdtmo = 10000; lua_system_param.wdg_tmo = wdtmo; l_message( NULL, "updated: wdg_tmo" ); change++; } else { l_message( NULL, "wrong arg type: wdg_tmo" ); } } lua_getfield(L, 1, "stack_size"); if (!lua_isnil(L, -1)) { // found? if( lua_isstring(L, -1) ) // deal with the string { uint16_t stksz = luaL_checkinteger( L, -1 ); if (stksz < 8192 || stksz > 24576) { l_message( NULL, "stack_size: 8192 ~ 24576, not updated" ); } else { lua_system_param.stack_size = stksz; l_message( NULL, "updated: stack_size" ); change++; } } else { l_message( NULL, "wrong arg type: stack_size" ); } } lua_getfield(L, 1, "inbuf_size"); if (!lua_isnil(L, -1)) { // found? if( lua_isstring(L, -1) ) // deal with the string { uint16_t inbsz = luaL_checkinteger( L, -1 ); if (inbsz < 128 || inbsz > 1024) inbsz = 256; lua_system_param.inbuf_size = inbsz; l_message( NULL, "updated: inbuf_size" ); change++; } else { l_message( NULL, "wrong arg type: inbuf_size" ); } } lua_getfield(L, 1, "baud_rate"); if (!lua_isnil(L, -1)) { // found? if( lua_isstring(L, -1) ) // deal with the string { uint32_t bdr = luaL_checkinteger( L, -1 ); lua_system_param.baud_rate = bdr; l_message( NULL, "updated: baud_rate" ); change++; } else { l_message( NULL, "wrong arg type: baud_rate" ); } } lua_getfield(L, 1, "parity"); if (!lua_isnil(L, -1)) { // found? if( lua_isstring(L, -1) ) // deal with the string { buf = luaL_checkstring( L, -1 ); if (strlen(buf) == 1) { change++; if(strcmp(buf, "n") == 0) lua_system_param.parity = NO_PARITY; else if(strcmp(buf, "o") == 0) lua_system_param.parity = ODD_PARITY; else if(strcmp(buf, "e") == 0) lua_system_param.parity = EVEN_PARITY; else { l_message( NULL, "arg parity should be 'n' or 'o' or 'e' " ); change--; } l_message( NULL, "updated: parity" ); } else { l_message( NULL, "arg parity should be 'n' or 'o' or 'e' " ); } } else { l_message( NULL, "wrong arg type: parity" ); } } lua_getfield(L, 1, "init_file"); if (!lua_isnil(L, -1)) { // found? if( lua_isstring(L, -1) ) // deal with the string { buf = luaL_checkstring( L, -1 ); if (strlen(buf) < 16) { sprintf(p_f, buf); change++; l_message( NULL, "updated: init_file" ); } else { l_message( NULL, "file name too long" ); } } else { l_message( NULL, "wrong arg type: init_file" ); } } if (change) { lua_system_param.crc = _get_luaparamsCRC(); MicoFlashErase(MICO_PARTITION_PARAMETER_1, 0, sizeof(lua_system_param_t)); lua_param_offset = 0; MicoFlashWrite(MICO_PARTITION_PARAMETER_1, &lua_param_offset, p_id, sizeof(lua_system_param_t)); l_message( NULL, "New params saved." ); } else { l_message( NULL, "Params to change: use_wwdg,baud_rate,parity,inbuf_size,init_file,stack_size,wdg_tmo" ); } return 0; }
OSStatus update(void) { boot_table_t updateLog; uint32_t i, j, size; uint32_t updateStartAddress; uint32_t destStartAddress_tmp; uint32_t paraStartAddress; OSStatus err = kNoErr; MicoFlashInitialize( (mico_flash_t)MICO_FLASH_FOR_UPDATE ); memset(data, 0xFF, SizePerRW); memset(newData, 0xFF, SizePerRW); memset(paraSaveInRam, 0xFF, PARA_FLASH_SIZE); updateStartAddress = UPDATE_START_ADDRESS; paraStartAddress = PARA_START_ADDRESS; err = MicoFlashRead(MICO_FLASH_FOR_PARA, ¶StartAddress, (uint8_t *)&updateLog, sizeof(boot_table_t)); require_noerr(err, exit); /*Not a correct record*/ if(updateLogCheck(&updateLog) != Log_NeedUpdate){ size = UPDATE_FLASH_SIZE/SizePerRW; for(i = 0; i <= size; i++){ if( i==size ){ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , UPDATE_FLASH_SIZE%SizePerRW); require_noerr(err, exit); } else{ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , SizePerRW); require_noerr(err, exit); } for(j=0; j<SizePerRW; j++){ if(data[j] != 0xFF){ update_log("Update data need to be erased"); err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE ); require_noerr(err, exit); err = MicoFlashErase( MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS ); require_noerr(err, exit); err = MicoFlashFinalize( MICO_FLASH_FOR_UPDATE ); require_noerr(err, exit); break; } } } goto exit; } update_log("Write OTA data to destination, type:%d, from 0x%08x to 0x%08x, length 0x%x", destFlashType, destStartAddress, destEndAddress, updateLog.length); destStartAddress_tmp = destStartAddress; updateStartAddress = UPDATE_START_ADDRESS; err = MicoFlashInitialize( destFlashType ); require_noerr(err, exit); err = MicoFlashErase( destFlashType, destStartAddress, destEndAddress ); require_noerr(err, exit); size = (updateLog.length)/SizePerRW; for(i = 0; i <= size; i++){ if( i==size && (updateLog.length)%SizePerRW){ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , (updateLog.length)%SizePerRW); require_noerr(err, exit); err = MicoFlashInitialize( destFlashType ); require_noerr(err, exit); err = MicoFlashWrite(destFlashType, &destStartAddress_tmp, data, (updateLog.length)%SizePerRW); require_noerr(err, exit); destStartAddress_tmp -= (updateLog.length)%SizePerRW; err = MicoFlashRead(destFlashType, &destStartAddress_tmp, newData , (updateLog.length)%SizePerRW); require_noerr(err, exit); err = memcmp(data, newData, (updateLog.length)%SizePerRW); require_noerr_action(err, exit, err = kWriteErr); } else{ err = MicoFlashRead(MICO_FLASH_FOR_UPDATE, &updateStartAddress, data , SizePerRW); require_noerr(err, exit); err = MicoFlashInitialize( destFlashType ); require_noerr(err, exit); err = MicoFlashWrite(destFlashType, &destStartAddress_tmp, data, SizePerRW); require_noerr(err, exit); destStartAddress_tmp -= SizePerRW; err = MicoFlashRead(destFlashType, &destStartAddress_tmp, newData , SizePerRW); require_noerr(err, exit); err = memcmp(data, newData, SizePerRW); require_noerr_action(err, exit, err = kWriteErr); } } update_log("Update start to clear data..."); paraStartAddress = PARA_START_ADDRESS; err = MicoFlashRead(MICO_FLASH_FOR_PARA, ¶StartAddress, paraSaveInRam, PARA_FLASH_SIZE); require_noerr(err, exit); memset(paraSaveInRam, 0xff, sizeof(boot_table_t)); err = MicoFlashErase(MICO_FLASH_FOR_PARA, PARA_START_ADDRESS, PARA_END_ADDRESS); require_noerr(err, exit); paraStartAddress = PARA_START_ADDRESS; err = MicoFlashWrite(MICO_FLASH_FOR_PARA, ¶StartAddress, paraSaveInRam, PARA_FLASH_SIZE); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS); require_noerr(err, exit); update_log("Update success"); exit: if(err != kNoErr) update_log("Update exit with err = %d", err); MicoFlashFinalize(MICO_FLASH_FOR_UPDATE); MicoFlashFinalize(destFlashType); return err; }
/** * @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 init_platform_bootloader( void ) { OSStatus err = kNoErr; MicoGpioInitialize( (mico_gpio_t)MICO_SYS_LED, OUTPUT_PUSH_PULL ); MicoGpioOutputLow( (mico_gpio_t)MICO_SYS_LED ); MicoGpioInitialize( (mico_gpio_t)MICO_RF_LED, OUTPUT_OPEN_DRAIN_NO_PULL ); MicoGpioOutputHigh( (mico_gpio_t)MICO_RF_LED ); MicoGpioInitialize((mico_gpio_t)BOOT_SEL, INPUT_PULL_UP); MicoGpioInitialize((mico_gpio_t)MFG_SEL, INPUT_PULL_UP); /* Specific operations used in EMW3165 production */ #define NEED_RF_DRIVER_COPY_BASE ((uint32_t)0x08008000) #define TEMP_RF_DRIVER_BASE ((uint32_t)0x08040000) #define TEMP_RF_DRIVER_END ((uint32_t)0x0807FFFF) const uint8_t isDriverNeedCopy = *(uint8_t *)(NEED_RF_DRIVER_COPY_BASE); const uint32_t totalLength = ( DRIVER_FLASH_SIZE < 0x40000)? DRIVER_FLASH_SIZE:0x40000; const uint8_t crcResult = *(uint8_t *)(TEMP_RF_DRIVER_END); uint8_t targetCrcResult = 0; uint32_t copyLength; uint32_t destStartAddress_tmp = DRIVER_START_ADDRESS; uint32_t sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE; uint32_t i; if ( isDriverNeedCopy != 0x0 ) return; platform_log( "Bootloader start to copy RF driver..." ); /* Copy RF driver to SPI flash */ err = MicoFlashInitialize( (mico_flash_t)MICO_FLASH_FOR_DRIVER ); require_noerr(err, exit); err = MicoFlashInitialize( (mico_flash_t)MICO_INTERNAL_FLASH ); require_noerr(err, exit); err = MicoFlashErase( MICO_FLASH_FOR_DRIVER, DRIVER_START_ADDRESS, DRIVER_END_ADDRESS ); require_noerr(err, exit); platform_log( "Time: %d", mico_get_time_no_os() ); for(i = 0; i <= totalLength/SizePerRW; i++){ if( i == totalLength/SizePerRW ){ if(totalLength%SizePerRW) copyLength = totalLength%SizePerRW; else break; }else{ copyLength = SizePerRW; } printf("."); err = MicoFlashRead( MICO_INTERNAL_FLASH, &sourceStartAddress_tmp, data , copyLength ); require_noerr( err, exit ); err = MicoFlashWrite( MICO_FLASH_FOR_DRIVER, &destStartAddress_tmp, data, copyLength); require_noerr(err, exit); } printf("\r\n"); /* Check CRC-8 check-sum */ platform_log( "Bootloader start to verify RF driver..." ); sourceStartAddress_tmp = TEMP_RF_DRIVER_BASE; destStartAddress_tmp = DRIVER_START_ADDRESS; for(i = 0; i <= totalLength/SizePerRW; i++){ if( i == totalLength/SizePerRW ){ if(totalLength%SizePerRW) copyLength = totalLength%SizePerRW; else break; }else{ copyLength = SizePerRW; } printf("."); err = MicoFlashRead( MICO_FLASH_FOR_DRIVER, &destStartAddress_tmp, data, copyLength ); require_noerr( err, exit ); targetCrcResult = CRC8_Table(targetCrcResult, data, copyLength); } printf("\r\n"); //require_string( crcResult == targetCrcResult, exit, "Check-sum error" ); if( crcResult != targetCrcResult ){ platform_log("Check-sum error"); while(1); } /* Clear RF driver from temperary storage */ platform_log("Bootloader start to clear RF driver temporary storage..."); err = MicoFlashInitialize( (mico_flash_t)MICO_INTERNAL_FLASH ); require_noerr(err, exit); /* Clear copy tag */ err = MicoFlashErase(MICO_INTERNAL_FLASH, NEED_RF_DRIVER_COPY_BASE, NEED_RF_DRIVER_COPY_BASE); require_noerr(err, exit); exit: MicoFlashFinalize( MICO_INTERNAL_FLASH ); MicoFlashFinalize( MICO_FLASH_FOR_DRIVER ); }
static s32_t lspiffs_erase(u32_t addr, u32_t size) { MicoFlashErase(MICO_FLASH_FOR_LUA,addr,addr+size-1); MicoWdgReload();//in case wathdog return SPIFFS_OK; }
int SocketReadHTTPHeader( int inSock, HTTPHeader_t *inHeader ) { int err =0; char * buf; char * dst; char * lim; char * end; size_t len; ssize_t n; const char * value; size_t valueSize; buf = inHeader->buf; dst = buf + inHeader->len; lim = buf + sizeof( inHeader->buf ); for( ;; ) { if(findHeader( inHeader, &end )) break ; n = read( inSock, dst, (size_t)( lim - dst ) ); if( n > 0 ) len = (size_t) n; else { err = kConnectionErr; goto exit; } dst += len; inHeader->len += len; } inHeader->len = (size_t)( end - buf ); err = HTTPHeaderParse( inHeader ); require_noerr( err, exit ); inHeader->extraDataLen = (size_t)( dst - end ); if(inHeader->extraDataPtr) { free((uint8_t *)inHeader->extraDataPtr); inHeader->extraDataPtr = 0; } if(inHeader->otaDataPtr) { free((uint8_t *)inHeader->otaDataPtr); inHeader->otaDataPtr = 0; } /* For MXCHIP OTA function, store extra data to OTA data temporary */ err = HTTPGetHeaderField( inHeader->buf, inHeader->len, "Content-Type", NULL, NULL, &value, &valueSize, NULL ); if(err == kNoErr && strnicmpx( value, valueSize, kMIMEType_MXCHIP_OTA ) == 0){ #ifdef MICO_FLASH_FOR_UPDATE http_utils_log("Receive OTA data!"); flashStorageAddress = UPDATE_START_ADDRESS; err = MicoFlashInitialize( MICO_FLASH_FOR_UPDATE ); require_noerr(err, exit); err = MicoFlashErase(MICO_FLASH_FOR_UPDATE, UPDATE_START_ADDRESS, UPDATE_END_ADDRESS); require_noerr(err, exit); err = MicoFlashWrite(MICO_FLASH_FOR_UPDATE, &flashStorageAddress, (uint8_t *)end, inHeader->extraDataLen); require_noerr(err, exit); #else http_utils_log("OTA flash memory is not existed!"); err = kUnsupportedErr; #endif goto exit; } /* For chunked extra data without content length */ if(inHeader->chunkedData == true){ inHeader->chunkedDataBufferLen = (inHeader->extraDataLen > 256)? inHeader->extraDataLen:256; inHeader->chunkedDataBufferPtr = calloc(inHeader->chunkedDataBufferLen, sizeof(uint8_t)); //Make extra data buffer larger than chunk length require_action(inHeader->chunkedDataBufferPtr, exit, err = kNoMemoryErr); memcpy((uint8_t *)inHeader->chunkedDataBufferPtr, end, inHeader->extraDataLen); inHeader->extraDataPtr = inHeader->chunkedDataBufferPtr; return kNoErr; } /* Extra data with content length */ if (inHeader->contentLength != 0){ //Content length >0, create a memory buffer (Content length) and store extra data size_t copyDataLen = (inHeader->contentLength >= inHeader->extraDataLen)? inHeader->contentLength:inHeader->extraDataLen; inHeader->extraDataPtr = calloc(copyDataLen , sizeof(uint8_t)); require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr); memcpy((uint8_t *)inHeader->extraDataPtr, end, copyDataLen); err = kNoErr; } /* Extra data without content length, data is ended by conntection close */ else if(inHeader->extraDataLen != 0){ //Content length =0, but extra data length >0, create a memory buffer (1500)and store extra data inHeader->dataEndedbyClose = true; inHeader->extraDataPtr = calloc(1500, sizeof(uint8_t)); require_action(inHeader->extraDataPtr, exit, err = kNoMemoryErr); memcpy((uint8_t *)inHeader->extraDataPtr, end, inHeader->extraDataLen); err = kNoErr; } else return kNoErr; exit: return err; }
/**************************************************************************** * Function : uartHandle_thread * Description : 处理 UART 消息 * Input Para : * Output Para : * Return Value: ****************************************************************************/ void ZCB_MessageHandle_thread(void *inContext) { uint8_t i; uint8_t zigbee_device_announce_flag = 255; //uint8_t zigbee_device_current_recover = ZIGBEE_NODE_MAX; uint8_t zigbee_device_recover_index = 0; //uint8_t zigbee_device_isRecoering = 0; uint8_t zigbee_device_hasStart = 0; #pragma pack(1) tsZCB_NodePDM NodePdm_Temp; #pragma pack() while(1) { //恢复节点信息 //1.每隔 1s 执行一次设备announce 如果是第一次上电??? if(zigbee_device_hasStart && ((--zigbee_device_announce_flag)==0)) { //查看 node status :查看 Flash 中的记录,恢复节点信息/////////////////////////// uint16_t u16nodeStatus = 0; uint16_t u16nodeAliveStatus = 0; uint8_t *message = malloc(11); ePDM_GetNodeStatus(&u16nodeStatus); ePDM_GetNodeAliveStatus(&u16nodeAliveStatus); //查看节点alive情况,如果节点在网络中并且不在线,那么执行 device announce if(zigbee_device_recover_index >= ZIGBEE_NODE_MAX) { zigbee_device_recover_index = 0; } for(; zigbee_device_recover_index<ZIGBEE_NODE_MAX; zigbee_device_recover_index++) { //在网络中,但是未在线的设备 ///不知道设备id????????????? if((((1<<zigbee_device_recover_index)& u16nodeStatus)==0) && (((1<<zigbee_device_recover_index)& u16nodeAliveStatus)!=0)) { memset((void*)(&NodePdm_Temp),0x00,ZIGBEE_NODE_LENGTH); ePDM_ReadOneNode(&NodePdm_Temp,zigbee_device_recover_index); if((NodePdm_Temp.u16DeviceID == 0x00) || (NodePdm_Temp.u8MacCapability != 0x80)) { user_uart_log("try to recover one node:%d",zigbee_device_recover_index); user_uart_log("u16ShortAddress:%x",NodePdm_Temp.u16ShortAddress); user_uart_log("u32IEEEAddressH:%x",NodePdm_Temp.u32IEEEAddressH); user_uart_log("u32IEEEAddressL:%x",NodePdm_Temp.u32IEEEAddressL); user_uart_log("device id:%d",NodePdm_Temp.u16DeviceID); NodePdm_Temp.u16ShortAddress = ntohs(NodePdm_Temp.u16ShortAddress); NodePdm_Temp.u32IEEEAddressH = ntohl(NodePdm_Temp.u32IEEEAddressH); NodePdm_Temp.u32IEEEAddressL = ntohl(NodePdm_Temp.u32IEEEAddressL); memcpy(message,&NodePdm_Temp.u16ShortAddress,2); memcpy(message+2,&NodePdm_Temp.u32IEEEAddressH,4); memcpy(message+6,&NodePdm_Temp.u32IEEEAddressL,4); memcpy(message+10,&NodePdm_Temp.u8MacCapability,1); //zigbee_device_current_recover = i; ZCB_HandleDeviceAnnounce(NULL, 11, message,NodePdm_Temp.u16DeviceID); zigbee_device_announce_flag = 255; zigbee_device_recover_index++; break; } else { user_uart_log("find enddevice,no recover"); } } } if(message) free(message); } mico_thread_msleep(10); //延时20ms for(i = 0; i<ZCB_MAX_MESSAGE_QUEUES; i++) { switch(zcbReceivedMessageQueue[i].u16Type) { case 0: //user_uart_log("msg empty"); break; case E_SL_MSG_LOG: //0x8001 user_uart_log("log msg"); zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_NODE_CLUSTER_LIST: //0x8003 //user_uart_log("nod cluster"); ZCB_HandleNodeClusterList(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_NODE_ATTRIBUTE_LIST: //0x8004 //user_uart_log("nod atr"); ZCB_HandleNodeClusterAttributeList(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_NODE_COMMAND_ID_LIST: //0x8005 //user_uart_log("nod cmd"); ZCB_HandleNodeCommandIDList(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_RESTART_PROVISIONED: //0x8006 user_uart_log("NoN Factory new Restart"); //非初始化重启 //当前设备的状态:不在线 一个一个改?? ePDM_UpdateNodeAliveStatus(0xFFFF); ZCB_HandleRestartProvisioned(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zigbee_device_hasStart = 1; zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_RESTART_FACTORY_NEW: //0x8007 user_uart_log("Factory New Restart"); ZCB_HandleRestartFactoryNew(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); MicoFlashErase( MICO_PARTITION_ZIGBEEPDM_TEMP, 0x0, 256); mico_thread_msleep(20); eZCB_ConfigureControlBridge(); zigbee_device_hasStart = 1; zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_LEAVE_INDICATION: //0x8048 user_uart_log("device Leave"); uint64_t u64IEEEAddress; ZCB_HandleDeviceLeave(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; memcpy(&u64IEEEAddress,zcbReceivedMessageQueue[i].au8Message,8); user_uart_log("ieee addr 0x%016llX",u64IEEEAddress); //u64IEEEAddress = ntoh64(u64IEEEAddress); ePDM_DelOneNode(&u64IEEEAddress); break; case E_SL_MSG_NETWORK_JOINED_FORMED: //0x8024 ZCB_HandleNetworkJoined(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_MATCH_DESCRIPTOR_RESPONSE://0x8046 user_uart_log("match resp"); ZCB_HandleMatchDescriptorResponse(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; user_uart_log("match resp over"); break; case E_SL_MSG_DEFAULT_RESPONSE: //0x8101 user_uart_log("default response"); zcbReceivedMessageQueue[i].u16Type = 0; break; case E_SL_MSG_ATTRIBUTE_REPORT: //0x8102 user_uart_log("attr report"); ZCB_HandleAttributeReport(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; user_uart_log("attr report done"); break; case E_SL_MSG_DEVICE_ANNOUNCE: //0x004D user_uart_log("device anc"); //ZCB_HandleDeviceAnnounce(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message,0x00); //收到device announce //1.保存设备信息到 flash 中 //2.如何处理 rejoin 时announce的设备信息???????????? 待处理============================= ePDM_SaveDeviceAnnounce(zcbReceivedMessageQueue[i].au8Message);//保存一个节点:根据 ZCB_Node zcbReceivedMessageQueue[i].u16Type = 0; user_uart_log("device anc done"); break; case E_SL_MSG_IAS_ZONE_STATUS_CHANGE_NOTIFY: //0x8401 user_uart_log("get ias change notify"); ZCB_HandleIASZoneStatusChangeNotify(NULL,zcbReceivedMessageQueue[i].u16Length,zcbReceivedMessageQueue[i].au8Message); zcbReceivedMessageQueue[i].u16Type = 0; break; //case E_SL_MSG_DATA_INDICATION: // break; default: //user_uart_log("not zcb initiative msg"); break; } } //user_uart_log("zcbMsg Hdl over"); } }
/** * @brief Receive a file using the ymodem protocol. * @param buf: Address of the first byte. * @retval The size of the file. */ int32_t Ymodem_Receive (uint8_t *buf, mico_flash_t flash, uint32_t flashdestination, int32_t maxRecvSize) { uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], file_size[FILE_SIZE_LENGTH], *file_ptr, *buf_ptr; int32_t i, packet_length, session_done, file_done, packets_received, errors, session_begin, size = 0; uint32_t ramsource; MicoFlashInitialize(flash); for (session_done = 0, errors = 0, session_begin = 0; ;) { for (packets_received = 0, file_done = 0, buf_ptr = buf; ;) { switch (Receive_Packet(packet_data, &packet_length, NAK_TIMEOUT)) { case 0: errors = 0; switch (packet_length) { /* Abort by sender */ case - 1: Send_Byte(ACK); MicoFlashFinalize(flash); return 0; /* End of transmission */ case 0: Send_Byte(ACK); file_done = 1; break; /* Normal packet */ default: if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff)) { Send_Byte(NAK); } else { if (packets_received == 0) { /* Filename packet */ if (packet_data[PACKET_HEADER] != 0) { /* Filename packet has valid data */ for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < FILE_NAME_LENGTH);) { FileName[i++] = *file_ptr++; } FileName[i++] = '\0'; for (i = 0, file_ptr ++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH);) { file_size[i++] = *file_ptr++; } file_size[i++] = '\0'; Str2Int(file_size, &size); /* Test the size of the image to be sent */ /* Image size is greater than Flash size */ if (size > (maxRecvSize + 1)) { /* End session */ Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return -1; } /* erase user application area */ MicoFlashErase(flash, flashdestination, flashdestination + maxRecvSize - 1); Send_Byte(ACK); Send_Byte(CRC16); } /* Filename packet is empty, end session */ else { Send_Byte(ACK); file_done = 1; session_done = 1; break; } } /* Data packet */ else { memcpy(buf_ptr, packet_data + PACKET_HEADER, packet_length); ramsource = (uint32_t)buf; /* Write received data in Flash */ if (MicoFlashWrite(flash, &flashdestination, (uint8_t*) ramsource, (uint32_t) packet_length) == 0) { Send_Byte(ACK); } else /* An error occurred while writing to Flash memory */ { /* End session */ Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return -2; } } packets_received ++; session_begin = 1; } } break; case 1: Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return -3; default: if (session_begin > 0) { errors ++; } if (errors > MAX_ERRORS) { Send_Byte(CA); Send_Byte(CA); MicoFlashFinalize(flash); return 0; } Send_Byte(CRC16); break; } if (file_done != 0) { break; } } if (session_done != 0) { break; } } MicoFlashFinalize(flash); return (int32_t)size; }