mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Committer:
ansond
Date:
Tue Sep 26 16:01:31 2017 +0000
Revision:
127:b4a661ff6fb9
Parent:
48:c02f2665cf76
minor re-ordering of FCC init

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 48:c02f2665cf76 65 this->m_fw_manifest_length = 0;
ansond 30:db367366b1f5 66 this->m_fw_image = NULL;
ansond 30:db367366b1f5 67 this->m_fw_image_length = 0;
ansond 13:9edad7677211 68 }
ansond 13:9edad7677211 69
ansond 13:9edad7677211 70 // Copy constructor
ansond 13:9edad7677211 71 DeviceManager::DeviceManager(const DeviceManager &manager) {
ansond 13:9edad7677211 72 for(int i=0;i<NUM_DEVICE_RESOURCES;++i) {
ansond 27:b8aaf7dc7023 73 this->m_dev_res[i] = manager.m_dev_res[i];
ansond 13:9edad7677211 74 }
ansond 27:b8aaf7dc7023 75 for(int i=0;i<NUM_FIRMWARE_RESOURCES;++i) {
ansond 27:b8aaf7dc7023 76 this->m_fw_res[i] = manager.m_fw_res[i];
ansond 27:b8aaf7dc7023 77 }
ansond 27:b8aaf7dc7023 78 this->m_dm_responder = manager.m_dm_responder;
ansond 27:b8aaf7dc7023 79 this->m_device = manager.m_device;
ansond 13:9edad7677211 80 this->m_dev_type = manager.m_dev_type;
ansond 13:9edad7677211 81 this->m_logger = manager.m_logger;
ansond 13:9edad7677211 82 this->m_endpoint = manager.m_endpoint;
ansond 27:b8aaf7dc7023 83 this->m_config = manager.m_config;
ansond 27:b8aaf7dc7023 84 this->m_mfg = manager.m_mfg;
ansond 27:b8aaf7dc7023 85 this->m_model = manager.m_model;
ansond 27:b8aaf7dc7023 86 this->m_serial = manager.m_serial;
ansond 27:b8aaf7dc7023 87 this->m_fw_vers = manager.m_fw_vers;
ansond 27:b8aaf7dc7023 88 this->m_sw_vers = manager.m_sw_vers;
ansond 27:b8aaf7dc7023 89 this->m_hw_vers = manager.m_hw_vers;
ansond 27:b8aaf7dc7023 90 this->m_deregister_resource = manager.m_deregister_resource;
ansond 48:c02f2665cf76 91 this->m_fw_manifest = this->saveManifest((uint8_t *)manager.m_fw_manifest,manager.m_fw_manifest_length);
ansond 48:c02f2665cf76 92 this->m_fw_manifest_length = manager.m_fw_manifest_length;
ansond 48:c02f2665cf76 93 this->m_fw_image = this->saveImage(manager.m_fw_image,manager.m_fw_image_length);
ansond 30:db367366b1f5 94 this->m_fw_image_length = manager.m_fw_image_length;
ansond 13:9edad7677211 95 }
ansond 13:9edad7677211 96
ansond 13:9edad7677211 97 // Destructor
ansond 13:9edad7677211 98 DeviceManager::~DeviceManager() {
ansond 30:db367366b1f5 99 if (this->m_fw_manifest != NULL) {
ansond 30:db367366b1f5 100 free(this->m_fw_manifest);
ansond 30:db367366b1f5 101 }
ansond 30:db367366b1f5 102 if (this->m_fw_image != NULL) {
ansond 30:db367366b1f5 103 free(this->m_fw_image);
ansond 30:db367366b1f5 104 }
ansond 30:db367366b1f5 105 this->m_fw_manifest = NULL;
ansond 48:c02f2665cf76 106 this->m_fw_manifest_length = 0;
ansond 30:db367366b1f5 107 this->m_fw_image = NULL;
ansond 30:db367366b1f5 108 this->m_fw_image_length = 0;
ansond 30:db367366b1f5 109 }
ansond 30:db367366b1f5 110
ansond 48:c02f2665cf76 111 // save the fota manifest
ansond 48:c02f2665cf76 112 char *DeviceManager::saveManifest(uint8_t *manifest,uint32_t manifest_length) {
ansond 30:db367366b1f5 113 if (manifest != NULL && manifest_length > 0) {
ansond 30:db367366b1f5 114 if (this->m_fw_manifest != NULL) {
ansond 30:db367366b1f5 115 free(this->m_fw_manifest);
ansond 30:db367366b1f5 116 }
ansond 30:db367366b1f5 117 this->m_fw_manifest = (char *)malloc(manifest_length+1);
ansond 30:db367366b1f5 118 memset(this->m_fw_manifest,0,manifest_length+1);
ansond 30:db367366b1f5 119 memcpy(this->m_fw_manifest,manifest,manifest_length);
ansond 48:c02f2665cf76 120 this->m_fw_manifest_length = manifest_length;
ansond 30:db367366b1f5 121 }
ansond 30:db367366b1f5 122 return this->m_fw_manifest;
ansond 30:db367366b1f5 123 }
ansond 30:db367366b1f5 124
ansond 48:c02f2665cf76 125 // save the fota image
ansond 48:c02f2665cf76 126 void *DeviceManager::saveImage(void *image,uint32_t image_length) {
ansond 30:db367366b1f5 127 if (image != NULL && image_length > 0) {
ansond 30:db367366b1f5 128 if (this->m_fw_image != NULL) {
ansond 30:db367366b1f5 129 free(this->m_fw_image);
ansond 30:db367366b1f5 130 }
ansond 30:db367366b1f5 131 this->m_fw_image = (char *)malloc(image_length);
ansond 30:db367366b1f5 132 memset(this->m_fw_image,0,image_length);
ansond 30:db367366b1f5 133 memcpy(this->m_fw_image,image,image_length);
ansond 30:db367366b1f5 134 }
ansond 30:db367366b1f5 135 return this->m_fw_image;
ansond 27:b8aaf7dc7023 136 }
ansond 27:b8aaf7dc7023 137
ansond 27:b8aaf7dc7023 138 // bind the device resources
ansond 27:b8aaf7dc7023 139 void DeviceManager::bindDeviceResources() {
ansond 27:b8aaf7dc7023 140 // our Endpoint configuration
ansond 27:b8aaf7dc7023 141 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 142
ansond 27:b8aaf7dc7023 143 // establish the default base LWM2M device info resource values
ansond 27:b8aaf7dc7023 144 this->m_device = M2MInterfaceFactory::create_device();
ansond 27:b8aaf7dc7023 145 if (this->m_device != NULL) {
ansond 27:b8aaf7dc7023 146 this->m_dev_res[0] = this->m_device->create_resource(M2MDevice::Manufacturer,this->m_mfg); // Manufacturer
ansond 27:b8aaf7dc7023 147 this->m_dev_res[1] = this->m_device->create_resource(M2MDevice::DeviceType,this->m_dev_type); // Device Type
ansond 27:b8aaf7dc7023 148 this->m_dev_res[2] = this->m_device->create_resource(M2MDevice::ModelNumber,this->m_model); // Device Model
ansond 27:b8aaf7dc7023 149 this->m_dev_res[3] = this->m_device->create_resource(M2MDevice::SerialNumber,this->m_serial); // Device Serial
ansond 27:b8aaf7dc7023 150 this->m_dev_res[4] = this->m_device->create_resource(M2MDevice::FirmwareVersion,this->m_fw_vers); // Firmware Version
ansond 27:b8aaf7dc7023 151 this->m_dev_res[5] = this->m_device->create_resource(M2MDevice::HardwareVersion,this->m_hw_vers); // Hardware Version
ansond 27:b8aaf7dc7023 152 this->m_dev_res[6] = this->m_device->create_resource(M2MDevice::SoftwareVersion,this->m_sw_vers); // Software Version
ansond 29:be035befb437 153 this->m_dev_res[7] = this->getDeviceRebootResource(); // Reboot
ansond 27:b8aaf7dc7023 154 this->m_dev_res[8] = this->m_device->create_resource(M2MDevice::FactoryReset); // Reset
ansond 42:d30ad7dec269 155
ansond 27:b8aaf7dc7023 156 // set the callback functions for Reboot and Reset
ansond 29:be035befb437 157 if (this->m_dev_res[7] != NULL) {
ansond 29:be035befb437 158 this->m_dev_res[7]->set_operation(M2MBase::POST_ALLOWED);
ansond 29:be035befb437 159 this->m_dev_res[7]->set_execute_function(execute_callback(this,&DeviceManager::process_reboot_action));
ansond 29:be035befb437 160 }
ansond 29:be035befb437 161 if (this->m_dev_res[8] != NULL) {
ansond 29:be035befb437 162 this->m_dev_res[8]->set_operation(M2MBase::POST_ALLOWED);
ansond 29:be035befb437 163 this->m_dev_res[8]->set_execute_function(execute_callback(this,&DeviceManager::process_reset_action));
ansond 29:be035befb437 164 }
ansond 27:b8aaf7dc7023 165
ansond 27:b8aaf7dc7023 166 // set the Device Resources Object
ansond 27:b8aaf7dc7023 167 if (cfg != NULL) {
ansond 27:b8aaf7dc7023 168 cfg->setDeviceResourcesObject(this->m_device); // device resources created under single device object... so just set it
ansond 27:b8aaf7dc7023 169 }
ansond 27:b8aaf7dc7023 170 }
ansond 27:b8aaf7dc7023 171 else {
ansond 27:b8aaf7dc7023 172 // unable to create the device object
ansond 27:b8aaf7dc7023 173 this->m_logger->log("DeviceManager::bindDeviceResources(): Unable to create Device Object!");
ansond 27:b8aaf7dc7023 174 }
ansond 27:b8aaf7dc7023 175 }
ansond 27:b8aaf7dc7023 176
ansond 27:b8aaf7dc7023 177 // bind the firmware resources
ansond 27:b8aaf7dc7023 178 void DeviceManager::bindFirmwareResources() {
ansond 27:b8aaf7dc7023 179 // our Endpoint configuration
ansond 27:b8aaf7dc7023 180 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 181
ansond 27:b8aaf7dc7023 182 // establish the default base LWM2M firmware info resource values
ansond 27:b8aaf7dc7023 183 this->m_firmware = M2MInterfaceFactory::create_firmware();
ansond 27:b8aaf7dc7023 184 if (this->m_firmware != NULL) {
ansond 27:b8aaf7dc7023 185 // Create our optional resources
ansond 29:be035befb437 186 this->m_fw_res[0] = this->m_firmware->create_resource(M2MFirmware::PackageName,""); // Package Name
ansond 29:be035befb437 187 this->m_fw_res[1] = this->m_firmware->create_resource(M2MFirmware::PackageVersion,""); // Package Version
ansond 27:b8aaf7dc7023 188
ansond 29:be035befb437 189 // Get the speciality resources
ansond 29:be035befb437 190 this->m_fw_res[2] = this->getFirmwareUpdateResource();
ansond 29:be035befb437 191 this->m_fw_res[3] = this->getFirmwarePackageResource();
ansond 29:be035befb437 192 this->m_fw_res[4] = this->getFirmwarePackageURIResource();
ansond 29:be035befb437 193 this->m_fw_res[5] = this->getFirmwareStateResource();
ansond 29:be035befb437 194 this->m_fw_res[6] = this->getFirmwareUpdateResultResource();
ansond 42:d30ad7dec269 195
ansond 27:b8aaf7dc7023 196 // set the callback functions for Update
ansond 29:be035befb437 197 if (this->m_fw_res[2] != NULL) {
ansond 29:be035befb437 198 this->m_fw_res[2]->set_operation(M2MBase::POST_ALLOWED);
ansond 29:be035befb437 199 this->m_fw_res[2]->set_execute_function(execute_callback(this,&DeviceManager::process_firmware_update_action));
ansond 29:be035befb437 200 }
ansond 27:b8aaf7dc7023 201
ansond 27:b8aaf7dc7023 202 // set the Firmware Resources Object
ansond 27:b8aaf7dc7023 203 if (cfg != NULL) {
ansond 27:b8aaf7dc7023 204 cfg->setFirmwareResourcesObject(this->m_firmware); // firmware resources created under single firmware object... so just set it
ansond 27:b8aaf7dc7023 205 }
ansond 13:9edad7677211 206 }
ansond 27:b8aaf7dc7023 207 else {
ansond 27:b8aaf7dc7023 208 // unable to create the firmware object
ansond 27:b8aaf7dc7023 209 this->m_logger->log("DeviceManager::bindFirmwareResources(): Unable to create Firmware Object!");
ansond 27:b8aaf7dc7023 210 }
ansond 27:b8aaf7dc7023 211 }
ansond 27:b8aaf7dc7023 212
ansond 27:b8aaf7dc7023 213 // bind mbed Cloud resources
ansond 27:b8aaf7dc7023 214 void DeviceManager::bindMBEDCloudResources() {
ansond 27:b8aaf7dc7023 215 // our Endpoint configuration
ansond 27:b8aaf7dc7023 216 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 217
ansond 27:b8aaf7dc7023 218 // Add mbed Cloud resources here...
ansond 27:b8aaf7dc7023 219
ansond 27:b8aaf7dc7023 220 // mbed Cloud DeRegistration Resource
ansond 27:b8aaf7dc7023 221 this->m_deregister_resource = new DeviceDeRegisterResource(this->m_logger,LWM2M_DREGISTER_OBJ_ID,LWM2M_DEV_DEREGISTER_ID,this->m_dm_responder);
ansond 27:b8aaf7dc7023 222 if (this->m_deregister_resource != NULL) {
ansond 27:b8aaf7dc7023 223 cfg->addResource(this->m_deregister_resource); // de-registration action resource added
ansond 27:b8aaf7dc7023 224 }
ansond 27:b8aaf7dc7023 225 }
ansond 27:b8aaf7dc7023 226
ansond 27:b8aaf7dc7023 227 // setup the Device Manager
ansond 27:b8aaf7dc7023 228 void DeviceManager::bind() {
ansond 27:b8aaf7dc7023 229 // Bind our Device Resources
ansond 27:b8aaf7dc7023 230 this->bindDeviceResources();
ansond 27:b8aaf7dc7023 231
ansond 27:b8aaf7dc7023 232 // Bind our Firmware Resources
ansond 27:b8aaf7dc7023 233 this->bindFirmwareResources();
ansond 27:b8aaf7dc7023 234
ansond 27:b8aaf7dc7023 235 // Bind our mbed Cloud Resources
ansond 27:b8aaf7dc7023 236 this->bindMBEDCloudResources();
ansond 27:b8aaf7dc7023 237 }
ansond 27:b8aaf7dc7023 238
ansond 30:db367366b1f5 239 // process updated values (PUT): The only updatable device management resources are: Firmware::Package, Firmware::PackageURI
ansond 29:be035befb437 240 void DeviceManager::process(M2MBase *base, M2MBase::BaseType type) {
ansond 30:db367366b1f5 241 // DeviceManagementResponder
ansond 30:db367366b1f5 242 DeviceManagementResponder *dmr = (DeviceManagementResponder *)this->m_dm_responder;
ansond 30:db367366b1f5 243
ansond 30:db367366b1f5 244 // PackageURI handler
ansond 30:db367366b1f5 245 if (base == this->getFirmwareResource(PackageURI)) {
ansond 30:db367366b1f5 246 // PackageURI resource
ansond 30:db367366b1f5 247 M2MResource *res = this->getFirmwareResource(PackageURI);
ansond 30:db367366b1f5 248
ansond 30:db367366b1f5 249 // Save off the manifest
ansond 30:db367366b1f5 250 this->saveManifest(res->value(),res->value_length());
ansond 30:db367366b1f5 251
ansond 30:db367366b1f5 252 // DEBUG
ansond 48:c02f2665cf76 253 this->m_logger->log("DeviceManager::process(PUT): Setting FOTA Manifest: [%s] type: %d length: %d",this->m_fw_manifest,type,this->m_fw_manifest_length);
ansond 30:db367366b1f5 254
ansond 30:db367366b1f5 255 // Manifest Updated
ansond 48:c02f2665cf76 256 dmr->setFOTAManifest(this->m_fw_manifest,this->m_fw_manifest_length);
ansond 30:db367366b1f5 257 }
ansond 30:db367366b1f5 258
ansond 30:db367366b1f5 259 // Package handler
ansond 30:db367366b1f5 260 if (base == this->getFirmwareResource(Package)) {
ansond 30:db367366b1f5 261 // FOTA Image (direct) Updated
ansond 30:db367366b1f5 262 M2MResource *res = this->getFirmwareResource(PackageURI);
ansond 30:db367366b1f5 263
ansond 30:db367366b1f5 264 // DEBUG
ansond 39:1eb0ca52af16 265 this->m_logger->log("DeviceManager::process(PUT): Setting FOTA Image. Length=%d type: %d",res->value_length(),type);
ansond 30:db367366b1f5 266
ansond 30:db367366b1f5 267 // FOTA Image updated
ansond 30:db367366b1f5 268 dmr->setFOTAImage(res->value(),res->value_length());
ansond 30:db367366b1f5 269 }
ansond 29:be035befb437 270 }
ansond 29:be035befb437 271
ansond 29:be035befb437 272 // Get the Device Reboot Resource from the Device Object
ansond 29:be035befb437 273 M2MResource *DeviceManager::getDeviceRebootResource() {
ansond 29:be035befb437 274 if (this->m_device != NULL) {
ansond 42:d30ad7dec269 275 // Get /3/0/3
ansond 42:d30ad7dec269 276 return this->getResourceFromObject(this->m_device,0,M2MDevice::Reboot);
ansond 29:be035befb437 277 }
ansond 29:be035befb437 278 return NULL;
ansond 29:be035befb437 279 }
ansond 29:be035befb437 280
ansond 29:be035befb437 281 // Get the Firmware Update Resource from the Firmware Object
ansond 29:be035befb437 282 M2MResource *DeviceManager::getFirmwareUpdateResource() {
ansond 29:be035befb437 283 if (this->m_firmware != NULL) {
ansond 29:be035befb437 284 // Get /5/0/2
ansond 42:d30ad7dec269 285 return this->getResourceFromObject(this->m_firmware,0,M2MFirmware::Update);
ansond 29:be035befb437 286 }
ansond 29:be035befb437 287 return NULL;
ansond 29:be035befb437 288 }
ansond 29:be035befb437 289
ansond 29:be035befb437 290 // Get the Firmware Package Resource from the Firmware Object
ansond 29:be035befb437 291 M2MResource *DeviceManager::getFirmwarePackageResource() {
ansond 29:be035befb437 292 if (this->m_firmware != NULL) {
ansond 29:be035befb437 293 // Get /5/0/0
ansond 42:d30ad7dec269 294 return this->getResourceFromObject(this->m_firmware,0,M2MFirmware::Package);
ansond 29:be035befb437 295 }
ansond 29:be035befb437 296 return NULL;
ansond 29:be035befb437 297 }
ansond 29:be035befb437 298
ansond 29:be035befb437 299 // Get the Firmware Package URI Resource from the Firmware Object
ansond 29:be035befb437 300 M2MResource *DeviceManager::getFirmwarePackageURIResource() {
ansond 29:be035befb437 301 if (this->m_firmware != NULL) {
ansond 29:be035befb437 302 // Get /5/0/1
ansond 42:d30ad7dec269 303 return this->getResourceFromObject(this->m_firmware,0,M2MFirmware::PackageUri);
ansond 29:be035befb437 304 }
ansond 29:be035befb437 305 return NULL;
ansond 29:be035befb437 306 }
ansond 29:be035befb437 307
ansond 29:be035befb437 308 // Get the Firmware State Resource from the Firmware Object
ansond 29:be035befb437 309 M2MResource *DeviceManager::getFirmwareStateResource() {
ansond 29:be035befb437 310 if (this->m_firmware != NULL) {
ansond 29:be035befb437 311 // Get /5/0/3
ansond 42:d30ad7dec269 312 return this->getResourceFromObject(this->m_firmware,0,M2MFirmware::State);
ansond 29:be035befb437 313 }
ansond 29:be035befb437 314 return NULL;
ansond 29:be035befb437 315 }
ansond 29:be035befb437 316
ansond 29:be035befb437 317 // Get the Firmware UpdateResult Resource from the Firmware Object
ansond 29:be035befb437 318 M2MResource *DeviceManager::getFirmwareUpdateResultResource() {
ansond 29:be035befb437 319 if (this->m_firmware != NULL) {
ansond 29:be035befb437 320 // Get /5/0/5
ansond 42:d30ad7dec269 321 return this->getResourceFromObject(this->m_firmware,0,M2MFirmware::UpdateResult);
ansond 29:be035befb437 322 }
ansond 29:be035befb437 323 return NULL;
ansond 29:be035befb437 324 }
ansond 29:be035befb437 325 // Get a specific resource from a resource object
ansond 29:be035befb437 326 M2MResource *DeviceManager::getResourceFromObject(M2MObject *obj,int instanceID,int resID) {
ansond 29:be035befb437 327 if (obj != NULL) {
ansond 29:be035befb437 328 M2MObjectInstanceList instances = obj->instances();
ansond 29:be035befb437 329 if (instances.size() > 0 && instances.size() > instanceID) {
ansond 29:be035befb437 330 M2MObjectInstance *instance = instances[instanceID];
ansond 29:be035befb437 331 if (instance != NULL) {
ansond 29:be035befb437 332 M2MResourceList resources = instance->resources();
ansond 29:be035befb437 333 if (resources.size() > 0 && resources.size() > resID) {
ansond 29:be035befb437 334 M2MResource *resource = resources[resID];
ansond 29:be035befb437 335 return resource;
ansond 29:be035befb437 336 }
ansond 29:be035befb437 337 }
ansond 29:be035befb437 338 }
ansond 29:be035befb437 339 }
ansond 29:be035befb437 340 return NULL;
ansond 29:be035befb437 341 }
ansond 29:be035befb437 342
ansond 27:b8aaf7dc7023 343 // Get the Device Object
ansond 29:be035befb437 344 M2MDevice *DeviceManager::getDeviceObject() {
ansond 27:b8aaf7dc7023 345 return this->m_device;
ansond 27:b8aaf7dc7023 346 }
ansond 27:b8aaf7dc7023 347
ansond 27:b8aaf7dc7023 348 // Get the Firmware Object
ansond 27:b8aaf7dc7023 349 M2MFirmware *DeviceManager::getFirmwareObject() {
ansond 27:b8aaf7dc7023 350 return this->m_firmware;
ansond 27:b8aaf7dc7023 351 }
ansond 27:b8aaf7dc7023 352
ansond 27:b8aaf7dc7023 353 // extract a specific firmware resource
ansond 27:b8aaf7dc7023 354 M2MResource *DeviceManager::getFirmwareResource(FirmwareResources res) {
ansond 27:b8aaf7dc7023 355 // indexed optional resources
ansond 27:b8aaf7dc7023 356 int index = (int)res;
ansond 27:b8aaf7dc7023 357 if (index >= 0 && index <NUM_FIRMWARE_RESOURCES) {
ansond 27:b8aaf7dc7023 358 return this->m_fw_res[index];
ansond 27:b8aaf7dc7023 359 }
ansond 27:b8aaf7dc7023 360 return NULL;
ansond 27:b8aaf7dc7023 361 }
ansond 27:b8aaf7dc7023 362
ansond 27:b8aaf7dc7023 363 // extract a specific device resource
ansond 27:b8aaf7dc7023 364 M2MResource *DeviceManager::getDeviceResource(DeviceResources res) {
ansond 27:b8aaf7dc7023 365 // special case: DeRegistration
ansond 27:b8aaf7dc7023 366 if (res == DeRegistration){
ansond 27:b8aaf7dc7023 367 return this->m_deregister_resource->getResource();
ansond 27:b8aaf7dc7023 368 }
ansond 27:b8aaf7dc7023 369
ansond 27:b8aaf7dc7023 370 // indexed optional resources
ansond 27:b8aaf7dc7023 371 int index = (int)res;
ansond 27:b8aaf7dc7023 372 if (index >= 0 && index <NUM_DEVICE_RESOURCES) {
ansond 27:b8aaf7dc7023 373 return this->m_dev_res[index];
ansond 27:b8aaf7dc7023 374 }
ansond 27:b8aaf7dc7023 375 return NULL;
ansond 13:9edad7677211 376 }
ansond 13:9edad7677211 377
ansond 13:9edad7677211 378 // Install the device manager into the Connector Endpoint
ansond 13:9edad7677211 379 void DeviceManager::install(const void *endpoint,const void *config) {
ansond 13:9edad7677211 380 // record the configuration
ansond 13:9edad7677211 381 this->m_config = (void *)config;
ansond 13:9edad7677211 382
ansond 13:9edad7677211 383 // record the endpoint
ansond 13:9edad7677211 384 this->m_endpoint = (void *)endpoint;
ansond 13:9edad7677211 385
ansond 27:b8aaf7dc7023 386 // set the endpoint type
ansond 13:9edad7677211 387 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 27:b8aaf7dc7023 388 if (cfg != NULL) {
ansond 27:b8aaf7dc7023 389 // set our device type
ansond 27:b8aaf7dc7023 390 cfg->setEndpointType(this->m_dev_type);
ansond 13:9edad7677211 391 }
ansond 27:b8aaf7dc7023 392
ansond 13:9edad7677211 393 // establish connection to our responder
ansond 13:9edad7677211 394 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 395 ((DeviceManagementResponder *)this->m_dm_responder)->setEndpoint(this->m_endpoint);
ansond 27:b8aaf7dc7023 396 }
ansond 13:9edad7677211 397 }
ansond 13:9edad7677211 398
ansond 13:9edad7677211 399 // get our device management responder
ansond 27:b8aaf7dc7023 400 void *DeviceManager::getResponder() {
ansond 27:b8aaf7dc7023 401 return this->m_dm_responder;
ansond 27:b8aaf7dc7023 402 }
ansond 27:b8aaf7dc7023 403
ansond 27:b8aaf7dc7023 404 // process our reboot action
ansond 27:b8aaf7dc7023 405 void DeviceManager::process_reboot_action(void *args) {
ansond 27:b8aaf7dc7023 406 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 407 ((DeviceManagementResponder *)this->m_dm_responder)->rebootDevice(args);
ansond 27:b8aaf7dc7023 408 }
ansond 27:b8aaf7dc7023 409 else {
ansond 27:b8aaf7dc7023 410 // no device management responder instance
ansond 30:db367366b1f5 411 this->m_logger->log("DeviceManager::process_reboot_action: DeviceManagementResponder is NULL. No reboot processed");
ansond 27:b8aaf7dc7023 412 }
ansond 27:b8aaf7dc7023 413 }
ansond 27:b8aaf7dc7023 414
ansond 27:b8aaf7dc7023 415 // process our reset action
ansond 27:b8aaf7dc7023 416 void DeviceManager::process_reset_action(void *args) {
ansond 27:b8aaf7dc7023 417 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 418 ((DeviceManagementResponder *)this->m_dm_responder)->resetDevice(args);
ansond 27:b8aaf7dc7023 419 }
ansond 27:b8aaf7dc7023 420 else {
ansond 27:b8aaf7dc7023 421 // no device management responder instance
ansond 27:b8aaf7dc7023 422 this->m_logger->log("DeviceManager:: process_reset_action: DeviceManagementResponder is NULL. No reset processed");
ansond 27:b8aaf7dc7023 423 }
ansond 27:b8aaf7dc7023 424 }
ansond 27:b8aaf7dc7023 425
ansond 27:b8aaf7dc7023 426 // process our firmware update action
ansond 27:b8aaf7dc7023 427 void DeviceManager::process_firmware_update_action(void *args) {
ansond 27:b8aaf7dc7023 428 if (this->m_dm_responder != NULL) {
ansond 27:b8aaf7dc7023 429 ((DeviceManagementResponder *)this->m_dm_responder)->invokeFOTA(args);
ansond 27:b8aaf7dc7023 430 }
ansond 27:b8aaf7dc7023 431 else {
ansond 27:b8aaf7dc7023 432 // no device management responder instance
ansond 27:b8aaf7dc7023 433 this->m_logger->log("DeviceManager:: process_firmware_update_action: DeviceManagementResponder is NULL. No firmware update action processed");
ansond 27:b8aaf7dc7023 434 }
ansond 27:b8aaf7dc7023 435 }