mbedConnectorInterface back port from mbedOS v3 using mbed-client C++ call interface

Committer:
ansond
Date:
Sun Jun 12 03:18:25 2016 +0000
Revision:
26:d7b009313e3b
Parent:
13:9edad7677211
updates

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 13:9edad7677211 43 // establish the default base LWM2M device info resource values
ansond 13:9edad7677211 44 this->m_dev[0] = new DeviceResource(logger,M2MDevice::Manufacturer,mfg); // Manufacturer
ansond 13:9edad7677211 45 this->m_dev[1] = new DeviceResource(logger,M2MDevice::DeviceType,dev_type); // Device Type
ansond 13:9edad7677211 46 this->m_dev[2] = new DeviceResource(logger,M2MDevice::ModelNumber,model); // Device Model
ansond 13:9edad7677211 47 this->m_dev[3] = new DeviceResource(logger,M2MDevice::SerialNumber,serial); // Device Serial
ansond 13:9edad7677211 48 this->m_dev[4] = new DeviceResource(logger,M2MDevice::FirmwareVersion,fw_vers); // Firmware Version
ansond 13:9edad7677211 49 this->m_dev[5] = new DeviceResource(logger,M2MDevice::HardwareVersion,hw_vers); // Hardware Version
ansond 13:9edad7677211 50 this->m_dev[6] = new DeviceResource(logger,M2MDevice::SoftwareVersion,sw_vers); // Software Version
ansond 13:9edad7677211 51
ansond 13:9edad7677211 52 // record the dev type and logger for later...
ansond 13:9edad7677211 53 this->m_dev_type = (char *)dev_type;
ansond 13:9edad7677211 54 this->m_logger = (Logger *)logger;
ansond 13:9edad7677211 55
ansond 13:9edad7677211 56 // Device DeRegistration Resource
ansond 13:9edad7677211 57 this->m_deregister_resource = new DeviceDeRegisterResource(logger,LWM2M_DEVICE_OBJ_ID,LWM2M_DEV_DEREGISTER_ID,dm_responder);
ansond 13:9edad7677211 58
ansond 13:9edad7677211 59 // Device Reboot Resource
ansond 13:9edad7677211 60 this->m_reboot_resource = new DeviceRebootResource(logger,LWM2M_DEVICE_OBJ_ID,this->createResName(M2MDevice::Reboot).c_str(),dm_responder);
ansond 13:9edad7677211 61
ansond 13:9edad7677211 62 // Device Reset Resource
ansond 13:9edad7677211 63 this->m_reset_resource = new DeviceResetResource(logger,LWM2M_DEVICE_OBJ_ID,this->createResName(M2MDevice::FactoryReset).c_str(),dm_responder);
ansond 13:9edad7677211 64
ansond 13:9edad7677211 65 // Device Firmware Composite Resource
ansond 13:9edad7677211 66 this->m_firmware_composite_resource = new DeviceFirmwareCompositeResource(logger,LWM2M_FIRMWARE_OBJ_ID,dm_responder);
ansond 13:9edad7677211 67 this->m_firmware_composite_resource->buildResources();
ansond 13:9edad7677211 68 }
ansond 13:9edad7677211 69
ansond 13:9edad7677211 70 // create a string form of our device resource value
ansond 13:9edad7677211 71 string DeviceManager::createResName(M2MDevice::DeviceResource res) {
ansond 13:9edad7677211 72 char buf[10];
ansond 13:9edad7677211 73 memset(buf,0,10);
ansond 13:9edad7677211 74 sprintf(buf,"%d",res);
ansond 13:9edad7677211 75 return string(buf);
ansond 13:9edad7677211 76 }
ansond 13:9edad7677211 77
ansond 13:9edad7677211 78 // Copy constructor
ansond 13:9edad7677211 79 DeviceManager::DeviceManager(const DeviceManager &manager) {
ansond 13:9edad7677211 80 for(int i=0;i<NUM_DEVICE_RESOURCES;++i) {
ansond 13:9edad7677211 81 this->m_dev[i] = new DeviceResource(*manager.m_dev[i]);
ansond 13:9edad7677211 82 }
ansond 13:9edad7677211 83 this->m_dev_type = manager.m_dev_type;
ansond 13:9edad7677211 84 this->m_logger = manager.m_logger;
ansond 13:9edad7677211 85 this->m_endpoint = manager.m_endpoint;
ansond 13:9edad7677211 86
ansond 13:9edad7677211 87 // copy the additional resources
ansond 13:9edad7677211 88 }
ansond 13:9edad7677211 89
ansond 13:9edad7677211 90 // Destructor
ansond 13:9edad7677211 91 DeviceManager::~DeviceManager() {
ansond 13:9edad7677211 92 for(int i=0;i<NUM_DEVICE_RESOURCES;++i) {
ansond 13:9edad7677211 93 delete this->m_dev[i];
ansond 13:9edad7677211 94 }
ansond 13:9edad7677211 95 }
ansond 13:9edad7677211 96
ansond 13:9edad7677211 97 // Install the device manager into the Connector Endpoint
ansond 13:9edad7677211 98 void DeviceManager::install(const void *endpoint,const void *config) {
ansond 13:9edad7677211 99 // record the configuration
ansond 13:9edad7677211 100 this->m_config = (void *)config;
ansond 13:9edad7677211 101
ansond 13:9edad7677211 102 // record the endpoint
ansond 13:9edad7677211 103 this->m_endpoint = (void *)endpoint;
ansond 13:9edad7677211 104
ansond 13:9edad7677211 105 // our Endpoint
ansond 13:9edad7677211 106 Connector::Endpoint *ep = (Connector::Endpoint *)this->m_endpoint;
ansond 13:9edad7677211 107
ansond 13:9edad7677211 108 // our Endpoint configuration
ansond 13:9edad7677211 109 Connector::OptionsBuilder *cfg = (Connector::OptionsBuilder *)this->m_config;
ansond 13:9edad7677211 110
ansond 13:9edad7677211 111 // add the base device resources
ansond 13:9edad7677211 112 for(int i=0;i<NUM_DEVICE_RESOURCES;++i) {
ansond 13:9edad7677211 113 // DEBUG
ansond 13:9edad7677211 114 this->m_logger->log("Adding Base Device Resource: %s = %s...",this->m_dev[i]->getFullName().c_str(),this->m_dev[i]->getValue().c_str());
ansond 13:9edad7677211 115
ansond 13:9edad7677211 116 // add the resource
ansond 13:9edad7677211 117 cfg->addResource(this->m_dev[i]);
ansond 13:9edad7677211 118 }
ansond 13:9edad7677211 119
ansond 13:9edad7677211 120 // set our device type
ansond 13:9edad7677211 121 cfg->setEndpointType(this->m_dev_type);
ansond 13:9edad7677211 122
ansond 13:9edad7677211 123 // establish connection to our responder
ansond 13:9edad7677211 124 if (this->m_dm_responder != NULL) {
ansond 13:9edad7677211 125 ((DeviceManagementResponder *)this->m_dm_responder)->setEndpoint((const void *)ep);
ansond 13:9edad7677211 126 }
ansond 13:9edad7677211 127
ansond 13:9edad7677211 128 // add the device action resources
ansond 13:9edad7677211 129 cfg->addResource(this->m_deregister_resource); // de-registration action
ansond 13:9edad7677211 130 cfg->addResource(this->m_reboot_resource); // reboot action
ansond 13:9edad7677211 131 cfg->addResource(this->m_reset_resource); // reset action
ansond 13:9edad7677211 132 this->m_firmware_composite_resource->addResources(cfg); // Firmware Composite Resource adds internally...
ansond 13:9edad7677211 133 }
ansond 13:9edad7677211 134
ansond 13:9edad7677211 135 // get our device management responder
ansond 13:9edad7677211 136 void *DeviceManager::getResponder() { return this->m_dm_responder; }