OSStatus fogCloudResetCloudDevInfo(app_context_t* const inContext, MVDResetRequestData_t devResetRequestData) { OSStatus err = kUnknownErr; cloud_if_log("Delete device info from cloud..."); err = FogCloudDeviceReset(&easyCloudContext); require_noerr_action( err, exit, cloud_if_log("ERROR: FogCloudDeviceReset failed! err=%d", err) ); inContext->appStatus.fogcloudStatus.isActivated = false; mico_rtos_lock_mutex(&inContext->mico_context->flashContentInRam_mutex); inContext->appConfig->fogcloudConfig.isActivated = false; // need to reActivate inContext->appConfig->fogcloudConfig.owner_binding = false; // no owner binding sprintf(inContext->appConfig->fogcloudConfig.deviceId, DEFAULT_DEVICE_ID); sprintf(inContext->appConfig->fogcloudConfig.masterDeviceKey, DEFAULT_DEVICE_KEY); sprintf(inContext->appConfig->fogcloudConfig.loginId, DEFAULT_LOGIN_ID); sprintf(inContext->appConfig->fogcloudConfig.devPasswd, DEFAULT_DEV_PASSWD); inContext->appStatus.fogcloudStatus.isCloudConnected = false; err = mico_system_context_update(inContext->mico_context); mico_rtos_unlock_mutex(&inContext->mico_context->flashContentInRam_mutex); exit: return err; }
OSStatus fogCloudDevActivate(app_context_t* const inContext, MVDActivateRequestData_t devActivateRequestData) { cloud_if_log_trace(); OSStatus err = kUnknownErr; cloud_if_log("Device activate..."); //ok, set cloud context strncpy(easyCloudContext.service_config_info.loginId, devActivateRequestData.loginId, MAX_SIZE_LOGIN_ID); strncpy(easyCloudContext.service_config_info.devPasswd, devActivateRequestData.devPasswd, MAX_SIZE_DEV_PASSWD); strncpy(easyCloudContext.service_config_info.userToken, devActivateRequestData.user_token, MAX_SIZE_USER_TOKEN); // activate request err = FogCloudActivate(&easyCloudContext); require_noerr_action(err, exit, cloud_if_log("ERROR: fogCloudDevActivate failed! err=%d", err) ); inContext->appStatus.fogcloudStatus.isActivated = true; // write activate data back to flash mico_rtos_lock_mutex(&inContext->mico_context->flashContentInRam_mutex); inContext->appConfig->fogcloudConfig.isActivated = true; strncpy(inContext->appConfig->fogcloudConfig.deviceId, easyCloudContext.service_status.deviceId, MAX_SIZE_DEVICE_ID); strncpy(inContext->appConfig->fogcloudConfig.masterDeviceKey, easyCloudContext.service_status.masterDeviceKey, MAX_SIZE_DEVICE_KEY); strncpy(inContext->appConfig->fogcloudConfig.loginId, easyCloudContext.service_config_info.loginId, MAX_SIZE_LOGIN_ID); strncpy(inContext->appConfig->fogcloudConfig.devPasswd, easyCloudContext.service_config_info.devPasswd, MAX_SIZE_DEV_PASSWD); strncpy(inContext->appConfig->fogcloudConfig.userToken, easyCloudContext.service_config_info.userToken, MAX_SIZE_USER_TOKEN); err = mico_system_context_update(inContext->mico_context); mico_rtos_unlock_mutex(&inContext->mico_context->flashContentInRam_mutex); require_noerr_action(err, exit, cloud_if_log("ERROR: activate write flash failed! err=%d", err) ); return kNoErr; exit: return err; }
OSStatus HKPairInfoRemove(char * name) { uint32_t i; OSStatus err = kNotFoundErr; pair_list_in_flash_t *pairList = &app_context->appConfig->pairList; for(i=0; i < MaxPairRecord; i++){ if(strncmp(pairList->pairInfo[i].controllerName, name, 64) == 0){ pairList->pairInfo[i].controllerName[0] = 0x0; //Clear the controller name record err = mico_system_context_update( mico_system_context_get() ); break; } } return err; }
static void micoNotify_WiFIParaChangedHandler(apinfo_adv_t *ap_info, char *key, int key_len, mico_Context_t * const inContext) { system_log_trace(); bool _needsUpdate = false; require(inContext, exit); mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex); if(strncmp(inContext->flashContentInRam.micoSystemConfig.ssid, ap_info->ssid, maxSsidLen)!=0){ strncpy(inContext->flashContentInRam.micoSystemConfig.ssid, ap_info->ssid, maxSsidLen); _needsUpdate = true; } if(memcmp(inContext->flashContentInRam.micoSystemConfig.bssid, ap_info->bssid, 6)!=0){ memcpy(inContext->flashContentInRam.micoSystemConfig.bssid, ap_info->bssid, 6); _needsUpdate = true; } if(inContext->flashContentInRam.micoSystemConfig.channel != ap_info->channel){ inContext->flashContentInRam.micoSystemConfig.channel = ap_info->channel; _needsUpdate = true; } if(inContext->flashContentInRam.micoSystemConfig.security != ap_info->security){ inContext->flashContentInRam.micoSystemConfig.security = ap_info->security; _needsUpdate = true; } if(memcmp(inContext->flashContentInRam.micoSystemConfig.key, key, maxKeyLen)!=0){ memcpy(inContext->flashContentInRam.micoSystemConfig.key, key, maxKeyLen); _needsUpdate = true; } if(inContext->flashContentInRam.micoSystemConfig.keyLength != key_len){ inContext->flashContentInRam.micoSystemConfig.keyLength = key_len; _needsUpdate = true; } if(_needsUpdate== true) mico_system_context_update( inContext ); mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex); exit: 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 ); }
OSStatus HKPairInfoInsert(char controllerIdentifier[64], uint8_t controllerLTPK[32], bool admin) { OSStatus err = kNoErr; pair_list_in_flash_t *pairList = &app_context->appConfig->pairList; uint32_t i; /* Looking for controller pair record */ for(i=0; i < MaxPairRecord; i++){ if(strncmp(pairList->pairInfo[i].controllerName, controllerIdentifier, 64)==0) break; } /* This is a new record, find a empty slot */ if(i == MaxPairRecord){ for(i=0; i < MaxPairRecord; i++){ if(pairList->pairInfo[i].controllerName[0] == 0x0) break; } } /* No empty slot for new record */ require_action(i < MaxPairRecord, exit, err = kNoSpaceErr); /* Write pair info to flash */ strcpy(pairList->pairInfo[i].controllerName, controllerIdentifier); memcpy(pairList->pairInfo[i].controllerLTPK, controllerLTPK, 32); if(admin) pairList->pairInfo[i].permission = pairList->pairInfo[i].permission|0x00000001; else pairList->pairInfo[i].permission = pairList->pairInfo[i].permission&0xFFFFFFFE; mico_system_context_update( mico_system_context_get() ); exit: return err; }
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 web_send_result_page(httpd_request_t *req) { OSStatus err = kNoErr; bool para_succ = false; int buf_size = 512; char *buf; char value_ssid[maxSsidLen]; char value_pass[maxKeyLen]; mico_Context_t* context = NULL; context = mico_system_context_get( ); buf = malloc(buf_size); err = httpd_get_data(req, buf, buf_size); require_noerr( err, Save_Out ); err = httpd_get_tag_from_post_data(buf, "SSID", value_ssid, maxSsidLen); require_noerr( err, Save_Out ); if(!strncmp(value_ssid, "\0", 1)) goto Save_Out; strncpy(context->flashContentInRam.micoSystemConfig.ssid, value_ssid, maxSsidLen); err = httpd_get_tag_from_post_data(buf, "PASS", value_pass, maxKeyLen); require_noerr( err, Save_Out ); strncpy(context->flashContentInRam.micoSystemConfig.key, value_pass, maxKeyLen); strncpy(context->flashContentInRam.micoSystemConfig.user_key, value_pass, maxKeyLen); context->flashContentInRam.micoSystemConfig.keyLength = strlen(context->flashContentInRam.micoSystemConfig.key); context->flashContentInRam.micoSystemConfig.user_keyLength = strlen(context->flashContentInRam.micoSystemConfig.key); context->flashContentInRam.micoSystemConfig.channel = 0; memset(context->flashContentInRam.micoSystemConfig.bssid, 0x0, 6); context->flashContentInRam.micoSystemConfig.security = SECURITY_TYPE_AUTO; context->flashContentInRam.micoSystemConfig.dhcpEnable = true; para_succ = true; Save_Out: if(para_succ == true) { err = httpd_send_all_header(req, HTTP_RES_200, sizeof(wifisuccess), HTTP_CONTENT_HTML_STR); require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wifisuccess headers.") ); err = httpd_send_body(req->sock, wifisuccess, sizeof(wifisuccess)); require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wifisuccess body.") ); context->flashContentInRam.micoSystemConfig.configured = allConfigured; mico_system_context_update(context); mico_system_power_perform( context, eState_Software_Reset ); } else { err = httpd_send_all_header(req, HTTP_RES_200, sizeof(wififail), HTTP_CONTENT_HTML_STR); require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wififail headers.") ); err = httpd_send_body(req->sock, wififail, sizeof(wififail)); require_noerr_action( err, exit, app_httpd_log("ERROR: Unable to send http wififail body.") ); } exit: if(buf) free(buf); return err; }
OSStatus fogCloudDevFirmwareUpdate(app_context_t* const inContext, MVDOTARequestData_t devOTARequestData) { cloud_if_log_trace(); OSStatus err = kUnknownErr; ecs_ota_flash_params_t ota_flash_params = { MICO_PARTITION_OTA_TEMP, 0x0, }; md5_context md5; unsigned char md5_16[16] = {0}; char *pmd5_32 = NULL; char rom_file_md5[32] = {0}; uint8_t data[SizePerRW] = {0}; uint32_t updateStartAddress = 0; uint32_t readLength = 0; uint32_t i = 0, size = 0; uint32_t romStringLen = 0; // crc16 CRC16_Context contex; cloud_if_log("fogCloudDevFirmwareUpdate: start ..."); //get latest rom version, file_path, md5 cloud_if_log("fogCloudDevFirmwareUpdate: get latest rom version from server ..."); err = FogCloudGetLatestRomVersion(&easyCloudContext); require_noerr_action( err, exit_with_error, cloud_if_log("ERROR: FogCloudGetLatestRomVersion failed! err=%d", err) ); //FW version compare cloud_if_log("currnt_version=%s", inContext->appConfig->fogcloudConfig.romVersion); cloud_if_log("latestRomVersion=%s", easyCloudContext.service_status.latestRomVersion); cloud_if_log("bin_file=%s", easyCloudContext.service_status.bin_file); cloud_if_log("bin_md5=%s", easyCloudContext.service_status.bin_md5); romStringLen = strlen(easyCloudContext.service_status.latestRomVersion) > strlen(inContext->appConfig->fogcloudConfig.romVersion) ? strlen(easyCloudContext.service_status.latestRomVersion):strlen(inContext->appConfig->fogcloudConfig.romVersion); if(0 == strncmp(inContext->appConfig->fogcloudConfig.romVersion, easyCloudContext.service_status.latestRomVersion, romStringLen)) { cloud_if_log("the current firmware version[%s] is up-to-date!", inContext->appConfig->fogcloudConfig.romVersion); inContext->appStatus.fogcloudStatus.RecvRomFileSize = 0; err = kNoErr; goto exit_with_no_error; } cloud_if_log("fogCloudDevFirmwareUpdate: new firmware[%s] found on server, downloading ...", easyCloudContext.service_status.latestRomVersion); inContext->appStatus.fogcloudStatus.isOTAInProgress = true; OTAWillStart(inContext); //get rom data err = FogCloudGetRomData(&easyCloudContext, ota_flash_params); require_noerr_action( err, exit_with_error, cloud_if_log("ERROR: FogCloudGetRomData failed! err=%d", err) ); //------------------------------ OTA DATA VERIFY ----------------------------- // md5 init InitMd5(&md5); CRC16_Init( &contex ); memset(rom_file_md5, 0, 32); memset(data, 0xFF, SizePerRW); updateStartAddress = ota_flash_params.update_offset; size = (easyCloudContext.service_status.bin_file_size)/SizePerRW; // read flash, md5 update for(i = 0; i <= size; i++) { if( i == size ) { if( (easyCloudContext.service_status.bin_file_size)%SizePerRW ) { readLength = (easyCloudContext.service_status.bin_file_size)%SizePerRW; } else { break; } } else { readLength = SizePerRW; } err = MicoFlashRead(ota_flash_params.update_partion, &updateStartAddress, data, readLength); require_noerr(err, exit_with_error); Md5Update(&md5, (uint8_t *)data, readLength); CRC16_Update( &contex, data, readLength ); } // read done, calc MD5 Md5Final(&md5, md5_16); CRC16_Final( &contex, &ota_crc ); pmd5_32 = ECS_DataToHexStringLowercase(md5_16, sizeof(md5_16)); //convert hex data to hex string cloud_if_log("ota_data_in_flash_md5[%d]=%s", strlen(pmd5_32), pmd5_32); if (NULL != pmd5_32) { strncpy(rom_file_md5, pmd5_32, strlen(pmd5_32)); free(pmd5_32); pmd5_32 = NULL; } else { err = kNoMemoryErr; goto exit_with_error; } // check md5 if(0 != strncmp( easyCloudContext.service_status.bin_md5, (char*)&(rom_file_md5[0]), strlen( easyCloudContext.service_status.bin_md5))) { cloud_if_log("ERROR: ota data wrote in flash md5 checksum err!!!"); err = kChecksumErr; goto exit_with_error; } else { cloud_if_log("OTA data in flash md5 check success, crc16=%d.", ota_crc); } //---------------------------------------------------------------------------- //update rom version in flash cloud_if_log("fogCloudDevFirmwareUpdate: return rom version && file size."); mico_rtos_lock_mutex(&inContext->mico_context->flashContentInRam_mutex); memset(inContext->appConfig->fogcloudConfig.romVersion, 0, MAX_SIZE_FW_VERSION); strncpy(inContext->appConfig->fogcloudConfig.romVersion, easyCloudContext.service_status.latestRomVersion, strlen(easyCloudContext.service_status.latestRomVersion)); inContext->appStatus.fogcloudStatus.RecvRomFileSize = easyCloudContext.service_status.bin_file_size; err = mico_system_context_update(inContext->mico_context); mico_rtos_unlock_mutex(&inContext->mico_context->flashContentInRam_mutex); OTASuccess(inContext); err = kNoErr; goto exit_with_no_error; exit_with_no_error: cloud_if_log("fogCloudDevFirmwareUpdate exit with no error."); inContext->appStatus.fogcloudStatus.isOTAInProgress = false; return err; exit_with_error: cloud_if_log("fogCloudDevFirmwareUpdate exit with err=%d.", err); OTAFailed(inContext); inContext->appStatus.fogcloudStatus.isOTAInProgress = false; return err; }