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:
0:1f1f55e73248
updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:1f1f55e73248 1 /**
ansond 0:1f1f55e73248 2 * @file DeviceResource.h
ansond 0:1f1f55e73248 3 * @brief mbed CoAP Endpoint Device Resource class
ansond 0:1f1f55e73248 4 * @author Doug Anson/Chris Paola
ansond 0:1f1f55e73248 5 * @version 1.0
ansond 0:1f1f55e73248 6 * @see
ansond 0:1f1f55e73248 7 *
ansond 0:1f1f55e73248 8 * Copyright (c) 2014
ansond 0:1f1f55e73248 9 *
ansond 0:1f1f55e73248 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 0:1f1f55e73248 11 * you may not use this file except in compliance with the License.
ansond 0:1f1f55e73248 12 * You may obtain a copy of the License at
ansond 0:1f1f55e73248 13 *
ansond 0:1f1f55e73248 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 0:1f1f55e73248 15 *
ansond 0:1f1f55e73248 16 * Unless required by applicable law or agreed to in writing, software
ansond 0:1f1f55e73248 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 0:1f1f55e73248 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 0:1f1f55e73248 19 * See the License for the specific language governing permissions and
ansond 0:1f1f55e73248 20 * limitations under the License.
ansond 0:1f1f55e73248 21 */
ansond 0:1f1f55e73248 22
ansond 0:1f1f55e73248 23 // configuration
ansond 0:1f1f55e73248 24 #include "mbed-connector-interface/mbedConnectorInterface.h"
ansond 0:1f1f55e73248 25
ansond 0:1f1f55e73248 26 // mbed-client support
ansond 0:1f1f55e73248 27 #include "mbed-client/m2mconstants.h"
ansond 0:1f1f55e73248 28
ansond 0:1f1f55e73248 29 // BaseClass
ansond 0:1f1f55e73248 30 #include "mbed-connector-interface/DeviceResource.h"
ansond 0:1f1f55e73248 31
ansond 0:1f1f55e73248 32 // Constructor
ansond 0:1f1f55e73248 33 DeviceResource::DeviceResource(const Logger *logger,M2MDevice::DeviceResource name, const char *value) : Resource<string>(logger,string(M2M_DEVICE_RES_ROOT),this->resource_name(name),string(value)), m_data_wrapper()
ansond 0:1f1f55e73248 34 {
ansond 0:1f1f55e73248 35 this->m_object = NULL;
ansond 0:1f1f55e73248 36 this->m_dev_name = name;
ansond 0:1f1f55e73248 37 }
ansond 0:1f1f55e73248 38
ansond 0:1f1f55e73248 39 // Constructor
ansond 0:1f1f55e73248 40 DeviceResource::DeviceResource(const Logger *logger,M2MDevice::DeviceResource name,const string value) : Resource<string>(logger,string(M2M_DEVICE_RES_ROOT),this->resource_name(name),string(value))
ansond 0:1f1f55e73248 41 {
ansond 0:1f1f55e73248 42 this->m_data_wrapper = NULL;
ansond 0:1f1f55e73248 43 this->m_object = NULL;
ansond 0:1f1f55e73248 44 this->m_dev_name = name;
ansond 0:1f1f55e73248 45 }
ansond 0:1f1f55e73248 46
ansond 0:1f1f55e73248 47 // Copy constructor
ansond 0:1f1f55e73248 48 DeviceResource::DeviceResource(const DeviceResource &resource) : Resource<string>((const Resource<string> &)resource)
ansond 0:1f1f55e73248 49 {
ansond 0:1f1f55e73248 50 this->m_data_wrapper = resource.m_data_wrapper;
ansond 0:1f1f55e73248 51 this->m_object = resource.m_object;
ansond 0:1f1f55e73248 52 this->m_dev_name = resource.m_dev_name;
ansond 0:1f1f55e73248 53 }
ansond 0:1f1f55e73248 54
ansond 0:1f1f55e73248 55 // Destructor
ansond 0:1f1f55e73248 56 DeviceResource::~DeviceResource() {
ansond 0:1f1f55e73248 57 }
ansond 0:1f1f55e73248 58
ansond 0:1f1f55e73248 59 // bind CoAP Resource..
ansond 0:1f1f55e73248 60 M2MObject *DeviceResource::bind(void *p) {
ansond 0:1f1f55e73248 61 if (p != NULL) {
ansond 0:1f1f55e73248 62 M2MDevice *resource = M2MInterfaceFactory::create_device();
ansond 0:1f1f55e73248 63 if (resource != NULL) {
ansond 0:1f1f55e73248 64 if (this->getDataWrapper() != NULL) {
ansond 0:1f1f55e73248 65 // wrap the data...
ansond 0:1f1f55e73248 66 this->getDataWrapper()->wrap((uint8_t *)this->getValue().c_str(),(int)this->getValue().size());
ansond 0:1f1f55e73248 67 string str_value = string((char *)this->getDataWrapper()->get());
ansond 0:1f1f55e73248 68 resource->create_resource(this->m_dev_name,str_value.c_str());
ansond 0:1f1f55e73248 69 this->logger()->log("DeviceResource: [%s] value: [%s] bound",this->getFullName().c_str(),this->getDataWrapper()->get());
ansond 0:1f1f55e73248 70 }
ansond 0:1f1f55e73248 71 else {
ansond 0:1f1f55e73248 72 // do not wrap the data...
ansond 0:1f1f55e73248 73 resource->create_resource(this->m_dev_name,this->getValue().c_str());
ansond 0:1f1f55e73248 74 this->logger()->log("DeviceResource: [%s] value: [%s] bound",this->getFullName().c_str(),this->getValue().c_str());
ansond 0:1f1f55e73248 75 }
ansond 0:1f1f55e73248 76 this->m_object = (M2MObject *)resource;
ansond 0:1f1f55e73248 77 }
ansond 0:1f1f55e73248 78 else {
ansond 0:1f1f55e73248 79 this->logger()->log("DeviceResource: M2MInterfaceFactory::create_device() FAILED");
ansond 0:1f1f55e73248 80 }
ansond 0:1f1f55e73248 81 }
ansond 0:1f1f55e73248 82 else {
ansond 0:1f1f55e73248 83 this->logger()->log("DeviceResource: NULL value parameter in bind() request...");
ansond 0:1f1f55e73248 84 }
ansond 0:1f1f55e73248 85 return this->m_object;
ansond 0:1f1f55e73248 86 }
ansond 0:1f1f55e73248 87
ansond 0:1f1f55e73248 88 // no reason why this should not be public in M2MDevice...
ansond 0:1f1f55e73248 89 string DeviceResource::resource_name(M2MDevice::DeviceResource name) const {
ansond 0:1f1f55e73248 90 m2m::String res_name = "";
ansond 0:1f1f55e73248 91 switch(name) {
ansond 0:1f1f55e73248 92 case M2MDevice::Manufacturer:
ansond 0:1f1f55e73248 93 res_name = DEVICE_MANUFACTURER;
ansond 0:1f1f55e73248 94 break;
ansond 0:1f1f55e73248 95 case M2MDevice::DeviceType:
ansond 0:1f1f55e73248 96 res_name = DEVICE_DEVICE_TYPE;
ansond 0:1f1f55e73248 97 break;
ansond 0:1f1f55e73248 98 case M2MDevice::ModelNumber:
ansond 0:1f1f55e73248 99 res_name = DEVICE_MODEL_NUMBER;
ansond 0:1f1f55e73248 100 break;
ansond 0:1f1f55e73248 101 case M2MDevice::SerialNumber:
ansond 0:1f1f55e73248 102 res_name = DEVICE_SERIAL_NUMBER;
ansond 0:1f1f55e73248 103 break;
ansond 0:1f1f55e73248 104 case M2MDevice::HardwareVersion:
ansond 0:1f1f55e73248 105 res_name = DEVICE_HARDWARE_VERSION;
ansond 0:1f1f55e73248 106 break;
ansond 0:1f1f55e73248 107 case M2MDevice::FirmwareVersion:
ansond 0:1f1f55e73248 108 res_name = DEVICE_FIRMWARE_VERSION;
ansond 0:1f1f55e73248 109 break;
ansond 0:1f1f55e73248 110 case M2MDevice::SoftwareVersion:
ansond 0:1f1f55e73248 111 res_name = DEVICE_SOFTWARE_VERSION;
ansond 0:1f1f55e73248 112 break;
ansond 0:1f1f55e73248 113 case M2MDevice::Reboot:
ansond 0:1f1f55e73248 114 res_name = DEVICE_REBOOT;
ansond 0:1f1f55e73248 115 break;
ansond 0:1f1f55e73248 116 case M2MDevice::FactoryReset:
ansond 0:1f1f55e73248 117 res_name = DEVICE_FACTORY_RESET;
ansond 0:1f1f55e73248 118 break;
ansond 0:1f1f55e73248 119 case M2MDevice::AvailablePowerSources:
ansond 0:1f1f55e73248 120 res_name = DEVICE_AVAILABLE_POWER_SOURCES;
ansond 0:1f1f55e73248 121 break;
ansond 0:1f1f55e73248 122 case M2MDevice::PowerSourceVoltage:
ansond 0:1f1f55e73248 123 res_name = DEVICE_POWER_SOURCE_VOLTAGE;
ansond 0:1f1f55e73248 124 break;
ansond 0:1f1f55e73248 125 case M2MDevice::PowerSourceCurrent:
ansond 0:1f1f55e73248 126 res_name = DEVICE_POWER_SOURCE_CURRENT;
ansond 0:1f1f55e73248 127 break;
ansond 0:1f1f55e73248 128 case M2MDevice::BatteryLevel:
ansond 0:1f1f55e73248 129 res_name = DEVICE_BATTERY_LEVEL;
ansond 0:1f1f55e73248 130 break;
ansond 0:1f1f55e73248 131 case M2MDevice::BatteryStatus:
ansond 0:1f1f55e73248 132 res_name = DEVICE_BATTERY_STATUS;
ansond 0:1f1f55e73248 133 break;
ansond 0:1f1f55e73248 134 case M2MDevice::MemoryFree:
ansond 0:1f1f55e73248 135 res_name = DEVICE_MEMORY_FREE;
ansond 0:1f1f55e73248 136 break;
ansond 0:1f1f55e73248 137 case M2MDevice::MemoryTotal:
ansond 0:1f1f55e73248 138 res_name = DEVICE_MEMORY_TOTAL;
ansond 0:1f1f55e73248 139 break;
ansond 0:1f1f55e73248 140 case M2MDevice::ErrorCode:
ansond 0:1f1f55e73248 141 res_name = DEVICE_ERROR_CODE;
ansond 0:1f1f55e73248 142 break;
ansond 0:1f1f55e73248 143 case M2MDevice::ResetErrorCode:
ansond 0:1f1f55e73248 144 res_name = DEVICE_RESET_ERROR_CODE;
ansond 0:1f1f55e73248 145 break;
ansond 0:1f1f55e73248 146 case M2MDevice::CurrentTime:
ansond 0:1f1f55e73248 147 res_name = DEVICE_CURRENT_TIME;
ansond 0:1f1f55e73248 148 break;
ansond 0:1f1f55e73248 149 case M2MDevice::UTCOffset:
ansond 0:1f1f55e73248 150 res_name = DEVICE_UTC_OFFSET;
ansond 0:1f1f55e73248 151 break;
ansond 0:1f1f55e73248 152 case M2MDevice::Timezone:
ansond 0:1f1f55e73248 153 res_name = DEVICE_TIMEZONE;
ansond 0:1f1f55e73248 154 break;
ansond 0:1f1f55e73248 155 case M2MDevice::SupportedBindingMode:
ansond 0:1f1f55e73248 156 res_name = DEVICE_SUPPORTED_BINDING_MODE;
ansond 0:1f1f55e73248 157 break;
ansond 0:1f1f55e73248 158 }
ansond 0:1f1f55e73248 159 string result = string("") + res_name.c_str();
ansond 0:1f1f55e73248 160 return result;
ansond 0:1f1f55e73248 161 }
ansond 0:1f1f55e73248 162