use TCP to connect to mbed connector
Fork of mbedConnectorInterfaceWithDM by
Diff: source/DeviceManager.cpp
- Revision:
- 30:db367366b1f5
- Parent:
- 29:be035befb437
- Child:
- 39:1eb0ca52af16
--- a/source/DeviceManager.cpp Tue Jun 14 05:54:20 2016 +0000 +++ b/source/DeviceManager.cpp Tue Jun 14 18:40:15 2016 +0000 @@ -61,6 +61,9 @@ this->m_deregister_resource = NULL; this->m_endpoint = NULL; this->m_config = NULL; + this->m_fw_manifest = NULL; + this->m_fw_image = NULL; + this->m_fw_image_length = 0; } // Copy constructor @@ -84,10 +87,55 @@ this->m_sw_vers = manager.m_sw_vers; this->m_hw_vers = manager.m_hw_vers; this->m_deregister_resource = manager.m_deregister_resource; + this->m_fw_manifest = this->copyManifest(manager.m_fw_manifest); + this->m_fw_image = this->copyImage(manager.m_fw_image,manager.m_fw_image_length); + this->m_fw_image_length = manager.m_fw_image_length; } // Destructor DeviceManager::~DeviceManager() { + if (this->m_fw_manifest != NULL) { + free(this->m_fw_manifest); + } + if (this->m_fw_image != NULL) { + free(this->m_fw_image); + } + this->m_fw_manifest = NULL; + this->m_fw_image = NULL; + this->m_fw_image_length = 0; +} + +// copy the fota image +void DeviceManager::saveManifest(uint8_t *manifest,uint32_t manifest_length) { + if (manifest != NULL && manifest_length > 0) { + if (this->m_fw_manifest != NULL) { + free(this->m_fw_manifest); + } + this->m_fw_manifest = (char *)malloc(manifest_length+1); + memset(this->m_fw_manifest,0,manifest_length+1); + memcpy(this->m_fw_manifest,manifest,manifest_length); + } +} + +// copy the fota manifest +char *DeviceManager::copyManifest(char *manifest) { + if (manifest != NULL) { + this->saveManifest((uint8_t *)manifest,(uint32_t)strlen(manifest)); + } + return this->m_fw_manifest; +} + +// copy the fota image +void *DeviceManager::copyImage(void *image,uint32_t image_length) { + if (image != NULL && image_length > 0) { + if (this->m_fw_image != NULL) { + free(this->m_fw_image); + } + this->m_fw_image = (char *)malloc(image_length); + memset(this->m_fw_image,0,image_length); + memcpy(this->m_fw_image,image,image_length); + } + return this->m_fw_image; } // bind the device resources @@ -202,10 +250,37 @@ this->bindMBEDCloudResources(); } -// process updated values +// process updated values (PUT): The only updatable device management resources are: Firmware::Package, Firmware::PackageURI void DeviceManager::process(M2MBase *base, M2MBase::BaseType type) { - // The only updatable device management resources are: Firmware::Package, Firmware::PackageURI - ; + // DeviceManagementResponder + DeviceManagementResponder *dmr = (DeviceManagementResponder *)this->m_dm_responder; + + // PackageURI handler + if (base == this->getFirmwareResource(PackageURI)) { + // PackageURI resource + M2MResource *res = this->getFirmwareResource(PackageURI); + + // Save off the manifest + this->saveManifest(res->value(),res->value_length()); + + // DEBUG + this->m_logger->log("DeviceManager::process(PUT): Setting FOTA Manifest: [%s]",this->m_fw_manifest); + + // Manifest Updated + dmr->setFOTAManifest(this->m_fw_manifest); + } + + // Package handler + if (base == this->getFirmwareResource(Package)) { + // FOTA Image (direct) Updated + M2MResource *res = this->getFirmwareResource(PackageURI); + + // DEBUG + this->m_logger->log("DeviceManager::process(PUT): Setting FOTA Image. Length=%d",res->value_length()); + + // FOTA Image updated + dmr->setFOTAImage(res->value(),res->value_length()); + } } // Get the Device Reboot Resource from the Device Object @@ -347,7 +422,7 @@ } else { // no device management responder instance - this->m_logger->log("DeviceManager:: process_reboot_action: DeviceManagementResponder is NULL. No reboot processed"); + this->m_logger->log("DeviceManager::process_reboot_action: DeviceManagementResponder is NULL. No reboot processed"); } }