use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Tue Jun 14 20:23:12 2016 +0000
Revision:
39:1eb0ca52af16
Parent:
30:db367366b1f5
Child:
42:d30ad7dec269
updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 13:9edad7677211 1 /**
ansond 13:9edad7677211 2 * @file DeviceManager.cpp
ansond 13:9edad7677211 3 * @brief mbed CoAP Endpoint Device Management class
ansond 13:9edad7677211 4 * @author Doug Anson
ansond 13:9edad7677211 5 * @version 1.0
ansond 13:9edad7677211 6 * @see
ansond 13:9edad7677211 7 *
ansond 13:9edad7677211 8 * Copyright (c) 2016
ansond 13:9edad7677211 9 *
ansond 13:9edad7677211 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 13:9edad7677211 11 * you may not use this file except in compliance with the License.
ansond 13:9edad7677211 12 * You may obtain a copy of the License at
ansond 13:9edad7677211 13 *
ansond 13:9edad7677211 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 13:9edad7677211 15 *
ansond 13:9edad7677211 16 * Unless required by applicable law or agreed to in writing, software
ansond 13:9edad7677211 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 13:9edad7677211 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 13:9edad7677211 19 * See the License for the specific language governing permissions and
ansond 13:9edad7677211 20 * limitations under the License.
ansond 13:9edad7677211 21 */
ansond 13:9edad7677211 22
ansond 13:9edad7677211 23 // configuration
ansond 13:9edad7677211 24 #include "mbed-connector-interface/mbedConnectorInterface.h"
ansond 13:9edad7677211 25
ansond 13:9edad7677211 26 // BaseClass
ansond 13:9edad7677211 27 #include "mbed-connector-interface/DeviceManager.h"
ansond 13:9edad7677211 28
ansond 13:9edad7677211 29 // Endpoint Class
ansond 13:9edad7677211 30 #include "mbed-connector-interface/ConnectorEndpoint.h"
ansond 13:9edad7677211 31
ansond 13:9edad7677211 32 // Options Builder
ansond 13:9edad7677211 33 #include "mbed-connector-interface/OptionsBuilder.h"
ansond 13:9edad7677211 34
ansond 13:9edad7677211 35 // Device Management Responder
ansond 13:9edad7677211 36 #include "mbed-connector-interface/DeviceManagementResponder.h"
ansond 13:9edad7677211 37
ansond 13:9edad7677211 38 // Constructor
ansond 13:9edad7677211 39 DeviceManager::DeviceManager(const Logger *logger,const void *dm_responder,const char *mfg,const char *dev_type,const char *model,const char *serial,const char *fw_vers,const char *hw_vers,const char *sw_vers) {
ansond 13:9edad7677211 40 // record the data management responder if we have one
ansond 13:9edad7677211 41 this->m_dm_responder = (void *)dm_responder;
ansond 13:9edad7677211 42
ansond 27:b8aaf7dc7023 43 // save off for later
ansond 13:9edad7677211 44 this->m_dev_type = (char *)dev_type;
ansond 13:9edad7677211 45 this->m_logger = (Logger *)logger;
ansond 27:b8aaf7dc7023 46 this->m_mfg = (char *)mfg;
ansond 27:b8aaf7dc7023 47 this->m_model = (char *)model;
ansond 27:b8aaf7dc7023 48 this->m_serial = (char *)serial;
ansond 27:b8aaf7dc7023 49 this->m_fw_vers = (char *)fw_vers;
ansond 27:b8aaf7dc7023 50 this->m_sw_vers = (char *)sw_vers;
ansond 27:b8aaf7dc7023 51 this->m_hw_vers = (char *)hw_vers;
ansond 13:9edad7677211 52
ansond 27:b8aaf7dc7023 53 // initialize
ansond 27:b8aaf7dc7023 54 this->m_device = NULL;
ansond 27:b8aaf7dc7023 55 for(int i=0;i<NUM_DEVICE_RESOURCES;++i) {
ansond 27:b8aaf7dc7023 56 this->m_dev_res[i] = NULL;
ansond 27:b8aaf7dc7023 57 }
ansond 27:b8aaf7dc7023 58 for(int i=0;i<NUM_FIRMWARE_RESOURCES;++i) {
ansond 27:b8aaf7dc7023 59 this->m_fw_res[i] = NULL;
ansond 27:b8aaf7dc7023 60 }
ansond 27:b8aaf7dc7023 61 this->m_deregister_resource = NULL;
ansond 27:b8aaf7dc7023 62 this->m_endpoint = NULL;
ansond 27:b8aaf7dc7023 63 this->m_config = NULL;
ansond 30:db367366b1f5 64 this->m_fw_manifest = NULL;
ansond 30:db367366b1f5 65 this->m_fw_image = NULL;
ansond 30:db367366b1f5 66 this->m_fw_image_length = 0;
ansond 13:9edad7677211 67 }
ansond 13:9edad7677211 68
ansond 13:9edad7677211 69 // Copy constructor
ansond 13:9edad7677211 70 DeviceManager::DeviceManager(const DeviceManager &manager) {
ansond 13:9edad7677211 71 for(int i=0;i<NUM_DEVICE_RESOURCES;++i) {
ansond 27:b8aaf7dc7023 72 this->m_dev_res[i] = manager.m_dev_res[i];
ansond 13:9edad7677211 73 }
ansond 27:b8aaf7dc7023 74 for(int i=0;i<NUM_FIRMWARE_RESOURCES;++i) {
ansond 27:b8aaf7dc7023 75 this->m_fw_res[i] = manager.m_fw_res[i];
ansond 27:b8aaf7dc7023 76 }
ansond 27:b8aaf7dc7023 77 this->m_dm_responder = manager.m_dm_responder;
ansond 27:b8aaf7dc7023 78 this->m_device = manager.m_device;
ansond 13:9edad7677211 79 this->m_dev_type = manager.m_dev_type;
ansond 13:9edad7677211 80 this->m_logger = manager.m_logger;
ansond 13:9edad7677211 81 this->m_endpoint = manager.m_endpoint;
ansond 27:b8aaf7dc7023 82 this->m_config = manager.m_config;
ansond 27:b8aaf7dc7023 83 this->m_mfg = manager.m_mfg;
ansond 27:b8aaf7dc7023 84 this->m_model = manager.m_model;
ansond 27:b8aaf7dc7023 85 this->m_serial = manager.m_serial;
ansond 27:b8aaf7dc7023 86 this->m_fw_vers = manager.m_fw_vers;
ansond 27:b8aaf7dc7023 87 this->m_sw_vers = manager.m_sw_vers;
ansond 27:b8aaf7dc7023 88 this->m_hw_vers = manager.m_hw_vers;
ansond 27:b8aaf7dc7023 89 this->m_deregister_resource = manager.m_deregister_resource;
ansond 30:db367366b1f5 90 this->m_fw_manifest = this->copyManifest(manager.m_fw_manifest);
ansond 30:db367366b1f5 91 this->m_fw_image = this->copyImage(manager.m_fw_image,manager.m_fw_image_length);
ansond 30:db367366b1f5 92 this->m_fw_image_length = manager.m_fw_image_length;
ansond 13:9edad7677211 93 }
ansond 13:9edad7677211 94
ansond 13:9edad7677211 95 // Destructor
ansond 13:9edad7677211 96 DeviceManager::~DeviceManager() {
ansond 30:db367366b1f5 97 if (this->m_fw_manifest != NULL) {
ansond 30:db367366b1f5 98 free(this->m_fw_manifest);
ansond 30:db367366b1f5 99 }
ansond 30:db367366b1f5 100 if (this->m_fw_image != NULL) {
ansond 30:db367366b1f5 101 free(this->m_fw_image);
ansond 30:db367366b1f5 102 }
ansond 30:db367366b1f5 103 this->m_fw_manifest = NULL;
ansond 30:db367366b1f5 104 this->m_fw_image = NULL;
ansond 30:db367366b1f5 105 this->m_fw_image_length = 0;
ansond 30:db367366b1f5 106 }
ansond 30:db367366b1f5 107
ansond 30:db367366b1f5 108 // copy the fota image
ansond 30:db367366b1f5 109 void DeviceManager::saveManifest(uint8_t *manifest,uint32_t manifest_length) {
ansond 30:db367366b1f5 110 if (manifest != NULL && manifest_length > 0) {
ansond 30:db367366b1f5 111 if (this->m_fw_manifest != NULL) {
ansond 30:db367366b1f5 112 free(this->m_fw_manifest);
ansond 30:db367366b1f5 113 }
ansond 30:db367366b1f5 114 this->m_fw_manifest = (char *)malloc(manifest_length+1);
ansond 30:db367366b1f5 115 memset(this->m_fw_manifest,0,manifest_length+1);
ansond 30:db367366b1f5 116 memcpy(this->m_fw_manifest,manifest,manifest_length);
ansond 30:db367366b1f5 117 }
ansond 30:db367366b1f5 118 }
ansond 30:db367366b1f5 119
ansond 30:db367366b1f5 120 // copy the fota manifest
ansond 30:db367366b1f5 121 char *DeviceManager::copyManifest(char *manifest) {
ansond 30:db367366b1f5 122 if (manifest != NULL) {
ansond 30:db367366b1f5 123 this->saveManifest((uint8_t *)manifest,(uint32_t)strlen(manifest));
ansond 30:db367366b1f5 124 }
ansond 30:db367366b1f5 125 return this->m_fw_manifest;
ansond 30:db367366b1f5 126 }
ansond 30:db367366b1f5 127
ansond 30:db367366b1f5 128 // copy the fota image
ansond 30:db367366b1f5 129 void *DeviceManager::copyImage(void *image,uint32_t image_length) {
ansond 30:db367366b1f5 130 if (image != NULL && image_length > 0) {
ansond 30:db367366b1f5 131 if (this->m_fw_image != NULL) {
ansond 30:db367366b1f5 132 free(this->m_fw_image);
ansond 30:db367366b1f5 133 }
ansond 30:db367366b1f5 134 this->m_fw_image = (char *)malloc(image_length);
ansond 30:db367366b1f5 135 memset(this->m_fw_image,0,image_length);
ansond 30:db367366b1f5 136 memcpy(this->m_fw_image,image,image_length);
ansond 30:db367366b1f5 137 }
ansond 30:db367366b1f5 138 return this->m_fw_image;
ansond 27:b8aaf7dc7023 139 }
ansond 27:b8aaf7dc7023 140
ansond 27:b8aaf7dc7023 141 // bind the device resources
ansond 27:b8aaf7dc7023 142 void DeviceManager::bindDeviceResources() {
ansond 27:b8aaf7dc7023 143 // our Endpoint configuration
ansond 27:b8aaf7dc7023 144 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 145
ansond 27:b8aaf7dc7023 146 // establish the default base LWM2M device info resource values
ansond 27:b8aaf7dc7023 147 this->m_device = M2MInterfaceFactory::create_device();
ansond 27:b8aaf7dc7023 148 if (this->m_device != NULL) {
ansond 27:b8aaf7dc7023 149 this->m_dev_res[0] = this->m_device->create_resource(M2MDevice::Manufacturer,this->m_mfg); // Manufacturer
ansond 27:b8aaf7dc7023 150 this->m_dev_res[1] = this->m_device->create_resource(M2MDevice::DeviceType,this->m_dev_type); // Device Type
ansond 27:b8aaf7dc7023 151 this->m_dev_res[2] = this->m_device->create_resource(M2MDevice::ModelNumber,this->m_model); // Device Model
ansond 27:b8aaf7dc7023 152 this->m_dev_res[3] = this->m_device->create_resource(M2MDevice::SerialNumber,this->m_serial); // Device Serial
ansond 27:b8aaf7dc7023 153 this->m_dev_res[4] = this->m_device->create_resource(M2MDevice::FirmwareVersion,this->m_fw_vers); // Firmware Version
ansond 27:b8aaf7dc7023 154 this->m_dev_res[5] = this->m_device->create_resource(M2MDevice::HardwareVersion,this->m_hw_vers); // Hardware Version
ansond 27:b8aaf7dc7023 155 this->m_dev_res[6] = this->m_device->create_resource(M2MDevice::SoftwareVersion,this->m_sw_vers); // Software Version
ansond 29:be035befb437 156 this->m_dev_res[7] = this->getDeviceRebootResource(); // Reboot
ansond 27:b8aaf7dc7023 157 this->m_dev_res[8] = this->m_device->create_resource(M2MDevice::FactoryReset); // Reset
ansond 27:b8aaf7dc7023 158
ansond 27:b8aaf7dc7023 159 // DEBUG
ansond 27:b8aaf7dc7023 160 if (this->m_dev_res[7] == NULL) this->m_logger->log("REBOOT RESOURCE IS NULL");
ansond 27:b8aaf7dc7023 161 if (this->m_dev_res[8] == NULL) this->m_logger->log("RESET RESOURCE IS NULL");
ansond 27:b8aaf7dc7023 162
ansond 27:b8aaf7dc7023 163 // set the callback functions for Reboot and Reset
ansond 29:be035befb437 164 if (this->m_dev_res[7] != NULL) {
ansond 29:be035befb437 165 this->m_dev_res[7]->set_operation(M2MBase::POST_ALLOWED);
ansond 29:be035befb437 166 this->m_dev_res[7]->set_execute_function(execute_callback(this,&DeviceManager::process_reboot_action));
ansond 29:be035befb437 167 }
ansond 29:be035befb437 168 if (this->m_dev_res[8] != NULL) {
ansond 29:be035befb437 169 this->m_dev_res[8]->set_operation(M2MBase::POST_ALLOWED);
ansond 29:be035befb437 170 this->m_dev_res[8]->set_execute_function(execute_callback(this,&DeviceManager::process_reset_action));
ansond 29:be035befb437 171 }
ansond 27:b8aaf7dc7023 172
ansond 27:b8aaf7dc7023 173 // set the Device Resources Object
ansond 27:b8aaf7dc7023 174 if (cfg != NULL) {
ansond 27:b8aaf7dc7023 175 cfg->setDeviceResourcesObject(this->m_device); // device resources created under single device object... so just set it
ansond 27:b8aaf7dc7023 176 }
ansond 27:b8aaf7dc7023 177 }
ansond 27:b8aaf7dc7023 178 else {
ansond 27:b8aaf7dc7023 179 // unable to create the device object
ansond 27:b8aaf7dc7023 180 this->m_logger->log("DeviceManager::bindDeviceResources(): Unable to create Device Object!");
ansond 27:b8aaf7dc7023 181 }
ansond 27:b8aaf7dc7023 182 }
ansond 27:b8aaf7dc7023 183
ansond 27:b8aaf7dc7023 184 // bind the firmware resources
ansond 27:b8aaf7dc7023 185 void DeviceManager::bindFirmwareResources() {
ansond 27:b8aaf7dc7023 186 // our Endpoint configuration
ansond 27:b8aaf7dc7023 187 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 188
ansond 27:b8aaf7dc7023 189 // establish the default base LWM2M firmware info resource values
ansond 27:b8aaf7dc7023 190 this->m_firmware = M2MInterfaceFactory::create_firmware();
ansond 27:b8aaf7dc7023 191 if (this->m_firmware != NULL) {
ansond 27:b8aaf7dc7023 192 // Create our optional resources
ansond 29:be035befb437 193 this->m_fw_res[0] = this->m_firmware->create_resource(M2MFirmware::PackageName,""); // Package Name
ansond 29:be035befb437 194 this->m_fw_res[1] = this->m_firmware->create_resource(M2MFirmware::PackageVersion,""); // Package Version
ansond 27:b8aaf7dc7023 195
ansond 29:be035befb437 196 // Get the speciality resources
ansond 29:be035befb437 197 this->m_fw_res[2] = this->getFirmwareUpdateResource();
ansond 29:be035befb437 198 this->m_fw_res[3] = this->getFirmwarePackageResource();
ansond 29:be035befb437 199 this->m_fw_res[4] = this->getFirmwarePackageURIResource();
ansond 29:be035befb437 200 this->m_fw_res[5] = this->getFirmwareStateResource();
ansond 29:be035befb437 201 this->m_fw_res[6] = this->getFirmwareUpdateResultResource();
ansond 27:b8aaf7dc7023 202
ansond 27:b8aaf7dc7023 203 // DEBUG
ansond 29:be035befb437 204 if (this->m_fw_res[2] == NULL) this->m_logger->log("FW UPDATE RESOURCE IS NULL");
ansond 29:be035befb437 205 if (this->m_fw_res[3] == NULL) this->m_logger->log("FW PACKAGE RESOURCE IS NULL");
ansond 29:be035befb437 206 if (this->m_fw_res[4] == NULL) this->m_logger->log("FW PACKAGE URI RESOURCE IS NULL");
ansond 29:be035befb437 207 if (this->m_fw_res[5] == NULL) this->m_logger->log("FW STATE RESOURCE IS NULL");
ansond 29:be035befb437 208 if (this->m_fw_res[6] == NULL) this->m_logger->log("FW UPDATE RESULT RESOURCE IS NULL");
ansond 27:b8aaf7dc7023 209
ansond 27:b8aaf7dc7023 210 // set the callback functions for Update
ansond 29:be035befb437 211 if (this->m_fw_res[2] != NULL) {
ansond 29:be035befb437 212 this->m_fw_res[2]->set_operation(M2MBase::POST_ALLOWED);
ansond 29:be035befb437 213 this->m_fw_res[2]->set_execute_function(execute_callback(this,&DeviceManager::process_firmware_update_action));
ansond 29:be035befb437 214 }
ansond 27:b8aaf7dc7023 215
ansond 27:b8aaf7dc7023 216 // set the Firmware Resources Object
ansond 27:b8aaf7dc7023 217 if (cfg != NULL) {
ansond 27:b8aaf7dc7023 218 cfg->setFirmwareResourcesObject(this->m_firmware); // firmware resources created under single firmware object... so just set it
ansond 27:b8aaf7dc7023 219 }
ansond 13:9edad7677211 220 }
ansond 27:b8aaf7dc7023 221 else {
ansond 27:b8aaf7dc7023 222 // unable to create the firmware object
ansond 27:b8aaf7dc7023 223 this->m_logger->log("DeviceManager::bindFirmwareResources(): Unable to create Firmware Object!");
ansond 27:b8aaf7dc7023 224 }
ansond 27:b8aaf7dc7023 225 }
ansond 27:b8aaf7dc7023 226
ansond 27:b8aaf7dc7023 227 // bind mbed Cloud resources
ansond 27:b8aaf7dc7023 228 void DeviceManager::bindMBEDCloudResources() {
ansond 27:b8aaf7dc7023 229 // our Endpoint configuration
ansond 27:b8aaf7dc7023 230 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 231
ansond 27:b8aaf7dc7023 232 // Add mbed Cloud resources here...
ansond 27:b8aaf7dc7023 233
ansond 27:b8aaf7dc7023 234 // mbed Cloud DeRegistration Resource
ansond 27:b8aaf7dc7023 235 this->m_deregister_resource = new DeviceDeRegisterResource(this->m_logger,LWM2M_DREGISTER_OBJ_ID,LWM2M_DEV_DEREGISTER_ID,this->m_dm_responder);
ansond 27:b8aaf7dc7023 236 if (this->m_deregister_resource != NULL) {
ansond 27:b8aaf7dc7023 237 cfg->addResource(this->m_deregister_resource); // de-registration action resource added
ansond 27:b8aaf7dc7023 238 }
ansond 27:b8aaf7dc7023 239 }
ansond 27:b8aaf7dc7023 240
ansond 27:b8aaf7dc7023 241 // setup the Device Manager
ansond 27:b8aaf7dc7023 242 void DeviceManager::bind() {
ansond 27:b8aaf7dc7023 243 // Bind our Device Resources
ansond 27:b8aaf7dc7023 244 this->bindDeviceResources();
ansond 27:b8aaf7dc7023 245
ansond 27:b8aaf7dc7023 246 // Bind our Firmware Resources
ansond 27:b8aaf7dc7023 247 this->bindFirmwareResources();
ansond 27:b8aaf7dc7023 248
ansond 27:b8aaf7dc7023 249 // Bind our mbed Cloud Resources
ansond 27:b8aaf7dc7023 250 this->bindMBEDCloudResources();
ansond 27:b8aaf7dc7023 251 }
ansond 27:b8aaf7dc7023 252
ansond 30:db367366b1f5 253 // process updated values (PUT): The only updatable device management resources are: Firmware::Package, Firmware::PackageURI
ansond 29:be035befb437 254 void DeviceManager::process(M2MBase *base, M2MBase::BaseType type) {
ansond 30:db367366b1f5 255 // DeviceManagementResponder
ansond 30:db367366b1f5 256 DeviceManagementResponder *dmr = (DeviceManagementResponder *)this->m_dm_responder;
ansond 30:db367366b1f5 257
ansond 30:db367366b1f5 258 // PackageURI handler
ansond 30:db367366b1f5 259 if (base == this->getFirmwareResource(PackageURI)) {
ansond 30:db367366b1f5 260 // PackageURI resource
ansond 30:db367366b1f5 261 M2MResource *res = this->getFirmwareResource(PackageURI);
ansond 30:db367366b1f5 262
ansond 30:db367366b1f5 263 // Save off the manifest
ansond 30:db367366b1f5 264 this->saveManifest(res->value(),res->value_length());
ansond 30:db367366b1f5 265
ansond 30:db367366b1f5 266 // DEBUG
ansond 39:1eb0ca52af16 267 this->m_logger->log("DeviceManager::process(PUT): Setting FOTA Manifest: [%s] type: %d",this->m_fw_manifest,type);
ansond 30:db367366b1f5 268
ansond 30:db367366b1f5 269 // Manifest Updated
ansond 30:db367366b1f5 270 dmr->setFOTAManifest(this->m_fw_manifest);
ansond 30:db367366b1f5 271 }
ansond 30:db367366b1f5 272
ansond 30:db367366b1f5 273 // Package handler
ansond 30:db367366b1f5 274 if (base == this->getFirmwareResource(Package)) {
ansond 30:db367366b1f5 275 // FOTA Image (direct) Updated
ansond 30:db367366b1f5 276 M2MResource *res = this->getFirmwareResource(PackageURI);
ansond 30:db367366b1f5 277
ansond 30:db367366b1f5 278 // DEBUG
ansond 39:1eb0ca52af16 279 this->m_logger->log("DeviceManager::process(PUT): Setting FOTA Image. Length=%d type: %d",res->value_length(),type);
ansond 30:db367366b1f5 280
ansond 30:db367366b1f5 281 // FOTA Image updated
ansond 30:db367366b1f5 282 dmr->setFOTAImage(res->value(),res->value_length());
ansond 30:db367366b1f5 283 }
ansond 29:be035befb437 284 }
ansond 29:be035befb437 285
ansond 29:be035befb437 286 // Get the Device Reboot Resource from the Device Object
ansond 29:be035befb437 287 M2MResource *DeviceManager::getDeviceRebootResource() {
ansond 29:be035befb437 288 if (this->m_device != NULL) {
ansond 29:be035befb437 289 // Get /3/0/4
ansond 29:be035befb437 290 return this->getResourceFromObject(this->m_device,0,4);
ansond 29:be035befb437 291 }
ansond 29:be035befb437 292 return NULL;
ansond 29:be035befb437 293 }
ansond 29:be035befb437 294
ansond 29:be035befb437 295 // Get the Firmware Update Resource from the Firmware Object
ansond 29:be035befb437 296 M2MResource *DeviceManager::getFirmwareUpdateResource() {
ansond 29:be035befb437 297 if (this->m_firmware != NULL) {
ansond 29:be035befb437 298 // Get /5/0/2
ansond 29:be035befb437 299 return this->getResourceFromObject(this->m_firmware,0,2);
ansond 29:be035befb437 300 }
ansond 29:be035befb437 301 return NULL;
ansond 29:be035befb437 302 }
ansond 29:be035befb437 303
ansond 29:be035befb437 304 // Get the Firmware Package Resource from the Firmware Object
ansond 29:be035befb437 305 M2MResource *DeviceManager::getFirmwarePackageResource() {
ansond 29:be035befb437 306 if (this->m_firmware != NULL) {
ansond 29:be035befb437 307 // Get /5/0/0
ansond 29:be035befb437 308 return this->getResourceFromObject(this->m_firmware,0,0);
ansond 29:be035befb437 309 }
ansond 29:be035befb437 310 return NULL;
ansond 29:be035befb437 311 }
ansond 29:be035befb437 312
ansond 29:be035befb437 313 // Get the Firmware Package URI Resource from the Firmware Object
ansond 29:be035befb437 314 M2MResource *DeviceManager::getFirmwarePackageURIResource() {
ansond 29:be035befb437 315 if (this->m_firmware != NULL) {
ansond 29:be035befb437 316 // Get /5/0/1
ansond 29:be035befb437 317 return this->getResourceFromObject(this->m_firmware,0,1);
ansond 29:be035befb437 318 }
ansond 29:be035befb437 319 return NULL;
ansond 29:be035befb437 320 }
ansond 29:be035befb437 321
ansond 29:be035befb437 322 // Get the Firmware State Resource from the Firmware Object
ansond 29:be035befb437 323 M2MResource *DeviceManager::getFirmwareStateResource() {
ansond 29:be035befb437 324 if (this->m_firmware != NULL) {
ansond 29:be035befb437 325 // Get /5/0/3
ansond 29:be035befb437 326 return this->getResourceFromObject(this->m_firmware,0,3);
ansond 29:be035befb437 327 }
ansond 29:be035befb437 328 return NULL;
ansond 29:be035befb437 329 }
ansond 29:be035befb437 330
ansond 29:be035befb437 331 // Get the Firmware UpdateResult Resource from the Firmware Object
ansond 29:be035befb437 332 M2MResource *DeviceManager::getFirmwareUpdateResultResource() {
ansond 29:be035befb437 333 if (this->m_firmware != NULL) {
ansond 29:be035befb437 334 // Get /5/0/5
ansond 29:be035befb437 335 return this->getResourceFromObject(this->m_firmware,0,5);
ansond 29:be035befb437 336 }
ansond 29:be035befb437 337 return NULL;
ansond 29:be035befb437 338 }
ansond 29:be035befb437 339 // Get a specific resource from a resource object
ansond 29:be035befb437 340 M2MResource *DeviceManager::getResourceFromObject(M2MObject *obj,int instanceID,int resID) {
ansond 29:be035befb437 341 if (obj != NULL) {
ansond 29:be035befb437 342 M2MObjectInstanceList instances = obj->instances();
ansond 29:be035befb437 343 if (instances.size() > 0 && instances.size() > instanceID) {
ansond 29:be035befb437 344 M2MObjectInstance *instance = instances[instanceID];
ansond 29:be035befb437 345 if (instance != NULL) {
ansond 29:be035befb437 346 M2MResourceList resources = instance->resources();
ansond 29:be035befb437 347 if (resources.size() > 0 && resources.size() > resID) {
ansond 29:be035befb437 348 M2MResource *resource = resources[resID];
ansond 29:be035befb437 349 return resource;
ansond 29:be035befb437 350 }
ansond 29:be035befb437 351 }
ansond 29:be035befb437 352 }
ansond 29:be035befb437 353 }
ansond 29:be035befb437 354 return NULL;
ansond 29:be035befb437 355 }
ansond 29:be035befb437 356
ansond 27:b8aaf7dc7023 357 // Get the Device Object
ansond 29:be035befb437 358 M2MDevice *DeviceManager::getDeviceObject() {
ansond 27:b8aaf7dc7023 359 return this->m_device;
ansond 27:b8aaf7dc7023 360 }
ansond 27:b8aaf7dc7023 361
ansond 27:b8aaf7dc7023 362 // Get the Firmware Object
ansond 27:b8aaf7dc7023 363 M2MFirmware *DeviceManager::getFirmwareObject() {
ansond 27:b8aaf7dc7023 364 return this->m_firmware;
ansond 27:b8aaf7dc7023 365 }
ansond 27:b8aaf7dc7023 366
ansond 27:b8aaf7dc7023 367 // extract a specific firmware resource
ansond 27:b8aaf7dc7023 368 M2MResource *DeviceManager::getFirmwareResource(FirmwareResources res) {
ansond 27:b8aaf7dc7023 369 // indexed optional resources
ansond 27:b8aaf7dc7023 370 int index = (int)res;
ansond 27:b8aaf7dc7023 371 if (index >= 0 && index <NUM_FIRMWARE_RESOURCES) {
ansond 27:b8aaf7dc7023 372 return this->m_fw_res[index];
ansond 27:b8aaf7dc7023 373 }
ansond 27:b8aaf7dc7023 374 return NULL;
ansond 27:b8aaf7dc7023 375 }
ansond 27:b8aaf7dc7023 376
ansond 27:b8aaf7dc7023 377 // extract a specific device resource
ansond 27:b8aaf7dc7023 378 M2MResource *DeviceManager::getDeviceResource(DeviceResources res) {
ansond 27:b8aaf7dc7023 379 // special case: DeRegistration
ansond 27:b8aaf7dc7023 380 if (res == DeRegistration){
ansond 27:b8aaf7dc7023 381 return this->m_deregister_resource->getResource();
ansond 27:b8aaf7dc7023 382 }
ansond 27:b8aaf7dc7023 383
ansond 27:b8aaf7dc7023 384 // indexed optional resources
ansond 27:b8aaf7dc7023 385 int index = (int)res;
ansond 27:b8aaf7dc7023 386 if (index >= 0 && index <NUM_DEVICE_RESOURCES) {
ansond 27:b8aaf7dc7023 387 return this->m_dev_res[index];
ansond 27:b8aaf7dc7023 388 }
ansond 27:b8aaf7dc7023 389 return NULL;
ansond 13:9edad7677211 390 }
ansond 13:9edad7677211 391
ansond 13:9edad7677211 392 // Install the device manager into the Connector Endpoint
ansond 13:9edad7677211 393 void DeviceManager::install(const void *endpoint,const void *config) {
ansond 13:9edad7677211 394 // record the configuration
ansond 13:9edad7677211 395 this->m_config = (void *)config;
ansond 13:9edad7677211 396
ansond 13:9edad7677211 397 // record the endpoint
ansond 13:9edad7677211 398 this->m_endpoint = (void *)endpoint;
ansond 13:9edad7677211 399
ansond 27:b8aaf7dc7023 400 // set the endpoint type
ansond 13:9edad7677211 401 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 402 if (cfg != NULL) {
ansond 27:b8aaf7dc7023 403 // set our device type
ansond 27:b8aaf7dc7023 404 cfg->setEndpointType(this->m_dev_type);
ansond 13:9edad7677211 405 }
ansond 27:b8aaf7dc7023 406
ansond 13:9edad7677211 407 // establish connection to our responder
ansond 13:9edad7677211 408 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 409 ((DeviceManagementResponder *)this->m_dm_responder)->setEndpoint(this->m_endpoint);
ansond 27:b8aaf7dc7023 410 }
ansond 13:9edad7677211 411 }
ansond 13:9edad7677211 412
ansond 13:9edad7677211 413 // get our device management responder
ansond 27:b8aaf7dc7023 414 void *DeviceManager::getResponder() {
ansond 27:b8aaf7dc7023 415 return this->m_dm_responder;
ansond 27:b8aaf7dc7023 416 }
ansond 27:b8aaf7dc7023 417
ansond 27:b8aaf7dc7023 418 // process our reboot action
ansond 27:b8aaf7dc7023 419 void DeviceManager::process_reboot_action(void *args) {
ansond 27:b8aaf7dc7023 420 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 421 ((DeviceManagementResponder *)this->m_dm_responder)->rebootDevice(args);
ansond 27:b8aaf7dc7023 422 }
ansond 27:b8aaf7dc7023 423 else {
ansond 27:b8aaf7dc7023 424 // no device management responder instance
ansond 30:db367366b1f5 425 this->m_logger->log("DeviceManager::process_reboot_action: DeviceManagementResponder is NULL. No reboot processed");
ansond 27:b8aaf7dc7023 426 }
ansond 27:b8aaf7dc7023 427 }
ansond 27:b8aaf7dc7023 428
ansond 27:b8aaf7dc7023 429 // process our reset action
ansond 27:b8aaf7dc7023 430 void DeviceManager::process_reset_action(void *args) {
ansond 27:b8aaf7dc7023 431 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 432 ((DeviceManagementResponder *)this->m_dm_responder)->resetDevice(args);
ansond 27:b8aaf7dc7023 433 }
ansond 27:b8aaf7dc7023 434 else {
ansond 27:b8aaf7dc7023 435 // no device management responder instance
ansond 27:b8aaf7dc7023 436 this->m_logger->log("DeviceManager:: process_reset_action: DeviceManagementResponder is NULL. No reset processed");
ansond 27:b8aaf7dc7023 437 }
ansond 27:b8aaf7dc7023 438 }
ansond 27:b8aaf7dc7023 439
ansond 27:b8aaf7dc7023 440 // process our firmware update action
ansond 27:b8aaf7dc7023 441 void DeviceManager::process_firmware_update_action(void *args) {
ansond 27:b8aaf7dc7023 442 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 443 ((DeviceManagementResponder *)this->m_dm_responder)->invokeFOTA(args);
ansond 27:b8aaf7dc7023 444 }
ansond 27:b8aaf7dc7023 445 else {
ansond 27:b8aaf7dc7023 446 // no device management responder instance
ansond 27:b8aaf7dc7023 447 this->m_logger->log("DeviceManager:: process_firmware_update_action: DeviceManagementResponder is NULL. No firmware update action processed");
ansond 27:b8aaf7dc7023 448 }
ansond 27:b8aaf7dc7023 449 }