Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 * Licensed under the Apache License, Version 2.0 (the License); you may
nexpaq 0:6c56fb4bc5f0 5 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 6 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 7 *
nexpaq 0:6c56fb4bc5f0 8 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 9 *
nexpaq 0:6c56fb4bc5f0 10 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 13 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 14 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 15 */
nexpaq 0:6c56fb4bc5f0 16
nexpaq 0:6c56fb4bc5f0 17 #include "mbed-client/m2mdevice.h"
nexpaq 0:6c56fb4bc5f0 18 #include "mbed-client/m2mconstants.h"
nexpaq 0:6c56fb4bc5f0 19 #include "mbed-client/m2mobject.h"
nexpaq 0:6c56fb4bc5f0 20 #include "mbed-client/m2mobjectinstance.h"
nexpaq 0:6c56fb4bc5f0 21 #include "mbed-client/m2mresource.h"
nexpaq 0:6c56fb4bc5f0 22 #include "mbed-trace/mbed_trace.h"
nexpaq 0:6c56fb4bc5f0 23
nexpaq 0:6c56fb4bc5f0 24 #define BUFFER_SIZE 21
nexpaq 0:6c56fb4bc5f0 25 #define TRACE_GROUP "mClt"
nexpaq 0:6c56fb4bc5f0 26
nexpaq 0:6c56fb4bc5f0 27 M2MDevice* M2MDevice::_instance = NULL;
nexpaq 0:6c56fb4bc5f0 28
nexpaq 0:6c56fb4bc5f0 29 M2MDevice* M2MDevice::get_instance()
nexpaq 0:6c56fb4bc5f0 30 {
nexpaq 0:6c56fb4bc5f0 31 if(_instance == NULL) {
nexpaq 0:6c56fb4bc5f0 32 _instance = new M2MDevice();
nexpaq 0:6c56fb4bc5f0 33 }
nexpaq 0:6c56fb4bc5f0 34 return _instance;
nexpaq 0:6c56fb4bc5f0 35 }
nexpaq 0:6c56fb4bc5f0 36
nexpaq 0:6c56fb4bc5f0 37 void M2MDevice::delete_instance()
nexpaq 0:6c56fb4bc5f0 38 {
nexpaq 0:6c56fb4bc5f0 39 delete _instance;
nexpaq 0:6c56fb4bc5f0 40 _instance = NULL;
nexpaq 0:6c56fb4bc5f0 41 }
nexpaq 0:6c56fb4bc5f0 42
nexpaq 0:6c56fb4bc5f0 43 M2MDevice::M2MDevice()
nexpaq 0:6c56fb4bc5f0 44 : M2MObject(M2M_DEVICE_ID)
nexpaq 0:6c56fb4bc5f0 45 {
nexpaq 0:6c56fb4bc5f0 46 M2MBase::set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 47 M2MBase::set_operation(M2MBase::GET_ALLOWED);
nexpaq 0:6c56fb4bc5f0 48
nexpaq 0:6c56fb4bc5f0 49 _device_instance = M2MObject::create_object_instance();
nexpaq 0:6c56fb4bc5f0 50 _device_instance->set_operation(M2MBase::GET_ALLOWED);
nexpaq 0:6c56fb4bc5f0 51 _device_instance->set_register_uri(true);
nexpaq 0:6c56fb4bc5f0 52 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 53 _device_instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE);
nexpaq 0:6c56fb4bc5f0 54 M2MResource* res = _device_instance->create_dynamic_resource(DEVICE_REBOOT,
nexpaq 0:6c56fb4bc5f0 55 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 56 M2MResourceInstance::OPAQUE,
nexpaq 0:6c56fb4bc5f0 57 false);
nexpaq 0:6c56fb4bc5f0 58 if(res) {
nexpaq 0:6c56fb4bc5f0 59 res->set_operation(M2MBase::POST_ALLOWED);
nexpaq 0:6c56fb4bc5f0 60 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 61 }
nexpaq 0:6c56fb4bc5f0 62
nexpaq 0:6c56fb4bc5f0 63 M2MResourceInstance* instance = _device_instance->create_dynamic_resource_instance(DEVICE_ERROR_CODE,
nexpaq 0:6c56fb4bc5f0 64 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 65 M2MResourceInstance::INTEGER,
nexpaq 0:6c56fb4bc5f0 66 true,0);
nexpaq 0:6c56fb4bc5f0 67 if(instance) {
nexpaq 0:6c56fb4bc5f0 68 M2MResource * dev_res = _device_instance->resource(DEVICE_ERROR_CODE);
nexpaq 0:6c56fb4bc5f0 69 if(dev_res) {
nexpaq 0:6c56fb4bc5f0 70 dev_res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 71 }
nexpaq 0:6c56fb4bc5f0 72 instance->set_operation(M2MBase::GET_ALLOWED);
nexpaq 0:6c56fb4bc5f0 73
nexpaq 0:6c56fb4bc5f0 74 instance->set_value(0);
nexpaq 0:6c56fb4bc5f0 75
nexpaq 0:6c56fb4bc5f0 76 instance->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 77 }
nexpaq 0:6c56fb4bc5f0 78 res = _device_instance->create_dynamic_resource(DEVICE_SUPPORTED_BINDING_MODE,
nexpaq 0:6c56fb4bc5f0 79 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 80 M2MResourceInstance::STRING,
nexpaq 0:6c56fb4bc5f0 81 true);
nexpaq 0:6c56fb4bc5f0 82 if(res) {
nexpaq 0:6c56fb4bc5f0 83 res->set_operation(M2MBase::GET_ALLOWED);
nexpaq 0:6c56fb4bc5f0 84 res->set_value((const uint8_t*)BINDING_MODE_UDP,sizeof(BINDING_MODE_UDP)-1);
nexpaq 0:6c56fb4bc5f0 85 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 86 }
nexpaq 0:6c56fb4bc5f0 87 }
nexpaq 0:6c56fb4bc5f0 88 }
nexpaq 0:6c56fb4bc5f0 89
nexpaq 0:6c56fb4bc5f0 90 M2MDevice::~M2MDevice()
nexpaq 0:6c56fb4bc5f0 91 {
nexpaq 0:6c56fb4bc5f0 92 }
nexpaq 0:6c56fb4bc5f0 93
nexpaq 0:6c56fb4bc5f0 94 M2MResource* M2MDevice::create_resource(DeviceResource resource, const String &value)
nexpaq 0:6c56fb4bc5f0 95 {
nexpaq 0:6c56fb4bc5f0 96 M2MResource* res = NULL;
nexpaq 0:6c56fb4bc5f0 97 const char* device_id_ptr = "";
nexpaq 0:6c56fb4bc5f0 98 M2MBase::Operation operation = M2MBase::GET_ALLOWED;
nexpaq 0:6c56fb4bc5f0 99 if(!is_resource_present(resource) && value.size() <= MAX_ALLOWED_STRING_LENGTH) {
nexpaq 0:6c56fb4bc5f0 100 switch(resource) {
nexpaq 0:6c56fb4bc5f0 101 case Manufacturer:
nexpaq 0:6c56fb4bc5f0 102 device_id_ptr = DEVICE_MANUFACTURER;
nexpaq 0:6c56fb4bc5f0 103 break;
nexpaq 0:6c56fb4bc5f0 104 case DeviceType:
nexpaq 0:6c56fb4bc5f0 105 device_id_ptr = DEVICE_DEVICE_TYPE;
nexpaq 0:6c56fb4bc5f0 106 break;
nexpaq 0:6c56fb4bc5f0 107 case ModelNumber:
nexpaq 0:6c56fb4bc5f0 108 device_id_ptr = DEVICE_MODEL_NUMBER;
nexpaq 0:6c56fb4bc5f0 109 break;
nexpaq 0:6c56fb4bc5f0 110 case SerialNumber:
nexpaq 0:6c56fb4bc5f0 111 device_id_ptr = DEVICE_SERIAL_NUMBER;
nexpaq 0:6c56fb4bc5f0 112 break;
nexpaq 0:6c56fb4bc5f0 113 case HardwareVersion:
nexpaq 0:6c56fb4bc5f0 114 device_id_ptr = DEVICE_HARDWARE_VERSION;
nexpaq 0:6c56fb4bc5f0 115 break;
nexpaq 0:6c56fb4bc5f0 116 case FirmwareVersion:
nexpaq 0:6c56fb4bc5f0 117 device_id_ptr = DEVICE_FIRMWARE_VERSION;
nexpaq 0:6c56fb4bc5f0 118 break;
nexpaq 0:6c56fb4bc5f0 119 case SoftwareVersion:
nexpaq 0:6c56fb4bc5f0 120 device_id_ptr = DEVICE_SOFTWARE_VERSION;
nexpaq 0:6c56fb4bc5f0 121 break;
nexpaq 0:6c56fb4bc5f0 122 case UTCOffset:
nexpaq 0:6c56fb4bc5f0 123 device_id_ptr = DEVICE_UTC_OFFSET;
nexpaq 0:6c56fb4bc5f0 124 operation = M2MBase::GET_PUT_ALLOWED;
nexpaq 0:6c56fb4bc5f0 125 break;
nexpaq 0:6c56fb4bc5f0 126 case Timezone:
nexpaq 0:6c56fb4bc5f0 127 device_id_ptr = DEVICE_TIMEZONE;
nexpaq 0:6c56fb4bc5f0 128 operation = M2MBase::GET_PUT_ALLOWED;
nexpaq 0:6c56fb4bc5f0 129 break;
nexpaq 0:6c56fb4bc5f0 130 default:
nexpaq 0:6c56fb4bc5f0 131 break;
nexpaq 0:6c56fb4bc5f0 132 }
nexpaq 0:6c56fb4bc5f0 133 }
nexpaq 0:6c56fb4bc5f0 134 const String device_id(device_id_ptr);
nexpaq 0:6c56fb4bc5f0 135
nexpaq 0:6c56fb4bc5f0 136 if(!device_id.empty()) {
nexpaq 0:6c56fb4bc5f0 137 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 138 res = _device_instance->create_dynamic_resource(device_id,
nexpaq 0:6c56fb4bc5f0 139 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 140 M2MResourceInstance::STRING,
nexpaq 0:6c56fb4bc5f0 141 true);
nexpaq 0:6c56fb4bc5f0 142
nexpaq 0:6c56fb4bc5f0 143 if(res ) {
nexpaq 0:6c56fb4bc5f0 144 res->set_operation(operation);
nexpaq 0:6c56fb4bc5f0 145 if (value.empty()) {
nexpaq 0:6c56fb4bc5f0 146 res->clear_value();
nexpaq 0:6c56fb4bc5f0 147 } else {
nexpaq 0:6c56fb4bc5f0 148 res->set_value((const uint8_t*)value.c_str(),
nexpaq 0:6c56fb4bc5f0 149 (uint32_t)value.length());
nexpaq 0:6c56fb4bc5f0 150 }
nexpaq 0:6c56fb4bc5f0 151 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 152 }
nexpaq 0:6c56fb4bc5f0 153 }
nexpaq 0:6c56fb4bc5f0 154 }
nexpaq 0:6c56fb4bc5f0 155 return res;
nexpaq 0:6c56fb4bc5f0 156 }
nexpaq 0:6c56fb4bc5f0 157
nexpaq 0:6c56fb4bc5f0 158 M2MResource* M2MDevice::create_resource(DeviceResource resource, int64_t value)
nexpaq 0:6c56fb4bc5f0 159 {
nexpaq 0:6c56fb4bc5f0 160 M2MResource* res = NULL;
nexpaq 0:6c56fb4bc5f0 161 const char* device_id_ptr = "";
nexpaq 0:6c56fb4bc5f0 162 M2MBase::Operation operation = M2MBase::GET_ALLOWED;
nexpaq 0:6c56fb4bc5f0 163 if(!is_resource_present(resource)) {
nexpaq 0:6c56fb4bc5f0 164 switch(resource) {
nexpaq 0:6c56fb4bc5f0 165 case BatteryLevel:
nexpaq 0:6c56fb4bc5f0 166 if(check_value_range(resource, value)) {
nexpaq 0:6c56fb4bc5f0 167 device_id_ptr = DEVICE_BATTERY_LEVEL;
nexpaq 0:6c56fb4bc5f0 168 }
nexpaq 0:6c56fb4bc5f0 169 break;
nexpaq 0:6c56fb4bc5f0 170 case BatteryStatus:
nexpaq 0:6c56fb4bc5f0 171 if(check_value_range(resource, value)) {
nexpaq 0:6c56fb4bc5f0 172 device_id_ptr = DEVICE_BATTERY_STATUS;
nexpaq 0:6c56fb4bc5f0 173 }
nexpaq 0:6c56fb4bc5f0 174 break;
nexpaq 0:6c56fb4bc5f0 175 case MemoryFree:
nexpaq 0:6c56fb4bc5f0 176 device_id_ptr = DEVICE_MEMORY_FREE;
nexpaq 0:6c56fb4bc5f0 177 break;
nexpaq 0:6c56fb4bc5f0 178 case MemoryTotal:
nexpaq 0:6c56fb4bc5f0 179 device_id_ptr = DEVICE_MEMORY_TOTAL;
nexpaq 0:6c56fb4bc5f0 180 break;
nexpaq 0:6c56fb4bc5f0 181 case CurrentTime:
nexpaq 0:6c56fb4bc5f0 182 device_id_ptr = DEVICE_CURRENT_TIME;
nexpaq 0:6c56fb4bc5f0 183 operation = M2MBase::GET_PUT_ALLOWED;
nexpaq 0:6c56fb4bc5f0 184 break;
nexpaq 0:6c56fb4bc5f0 185 default:
nexpaq 0:6c56fb4bc5f0 186 break;
nexpaq 0:6c56fb4bc5f0 187 }
nexpaq 0:6c56fb4bc5f0 188 }
nexpaq 0:6c56fb4bc5f0 189
nexpaq 0:6c56fb4bc5f0 190 const String device_id(device_id_ptr);
nexpaq 0:6c56fb4bc5f0 191
nexpaq 0:6c56fb4bc5f0 192 if(!device_id.empty()) {
nexpaq 0:6c56fb4bc5f0 193 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 194 res = _device_instance->create_dynamic_resource(device_id,
nexpaq 0:6c56fb4bc5f0 195 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 196 M2MResourceInstance::INTEGER,
nexpaq 0:6c56fb4bc5f0 197 true);
nexpaq 0:6c56fb4bc5f0 198
nexpaq 0:6c56fb4bc5f0 199 if(res) {
nexpaq 0:6c56fb4bc5f0 200
nexpaq 0:6c56fb4bc5f0 201
nexpaq 0:6c56fb4bc5f0 202 res->set_operation(operation);
nexpaq 0:6c56fb4bc5f0 203 res->set_value(value);
nexpaq 0:6c56fb4bc5f0 204
nexpaq 0:6c56fb4bc5f0 205 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 206 }
nexpaq 0:6c56fb4bc5f0 207 }
nexpaq 0:6c56fb4bc5f0 208 }
nexpaq 0:6c56fb4bc5f0 209 return res;
nexpaq 0:6c56fb4bc5f0 210 }
nexpaq 0:6c56fb4bc5f0 211
nexpaq 0:6c56fb4bc5f0 212 M2MResourceInstance* M2MDevice::create_resource_instance(DeviceResource resource, int64_t value,
nexpaq 0:6c56fb4bc5f0 213 uint16_t instance_id)
nexpaq 0:6c56fb4bc5f0 214 {
nexpaq 0:6c56fb4bc5f0 215 M2MResourceInstance* res = NULL;
nexpaq 0:6c56fb4bc5f0 216 const char* device_id_ptr = "";
nexpaq 0:6c56fb4bc5f0 217 // For these resources multiple instance can exist
nexpaq 0:6c56fb4bc5f0 218 if(AvailablePowerSources == resource) {
nexpaq 0:6c56fb4bc5f0 219 if(check_value_range(resource, value)) {
nexpaq 0:6c56fb4bc5f0 220 device_id_ptr = DEVICE_AVAILABLE_POWER_SOURCES;
nexpaq 0:6c56fb4bc5f0 221 }
nexpaq 0:6c56fb4bc5f0 222 } else if(PowerSourceVoltage == resource) {
nexpaq 0:6c56fb4bc5f0 223 device_id_ptr = DEVICE_POWER_SOURCE_VOLTAGE;
nexpaq 0:6c56fb4bc5f0 224 } else if(PowerSourceCurrent == resource) {
nexpaq 0:6c56fb4bc5f0 225 device_id_ptr = DEVICE_POWER_SOURCE_CURRENT;
nexpaq 0:6c56fb4bc5f0 226 } else if(ErrorCode == resource) {
nexpaq 0:6c56fb4bc5f0 227 if(check_value_range(resource, value)) {
nexpaq 0:6c56fb4bc5f0 228 device_id_ptr = DEVICE_ERROR_CODE;
nexpaq 0:6c56fb4bc5f0 229 }
nexpaq 0:6c56fb4bc5f0 230 }
nexpaq 0:6c56fb4bc5f0 231
nexpaq 0:6c56fb4bc5f0 232 const String device_id(device_id_ptr);
nexpaq 0:6c56fb4bc5f0 233
nexpaq 0:6c56fb4bc5f0 234 if(!device_id.empty()) {
nexpaq 0:6c56fb4bc5f0 235 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 236 res = _device_instance->create_dynamic_resource_instance(device_id,OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 237 M2MResourceInstance::INTEGER,
nexpaq 0:6c56fb4bc5f0 238 true, instance_id);
nexpaq 0:6c56fb4bc5f0 239
nexpaq 0:6c56fb4bc5f0 240 M2MResource *resource = _device_instance->resource(device_id);
nexpaq 0:6c56fb4bc5f0 241 if(resource) {
nexpaq 0:6c56fb4bc5f0 242 resource->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 243 }
nexpaq 0:6c56fb4bc5f0 244 if(res) {
nexpaq 0:6c56fb4bc5f0 245 res->set_value(value);
nexpaq 0:6c56fb4bc5f0 246 // Only read operation is allowed for above resources
nexpaq 0:6c56fb4bc5f0 247 res->set_operation(M2MBase::GET_ALLOWED);
nexpaq 0:6c56fb4bc5f0 248
nexpaq 0:6c56fb4bc5f0 249 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 250 }
nexpaq 0:6c56fb4bc5f0 251 }
nexpaq 0:6c56fb4bc5f0 252 }
nexpaq 0:6c56fb4bc5f0 253 return res;
nexpaq 0:6c56fb4bc5f0 254 }
nexpaq 0:6c56fb4bc5f0 255 M2MResource* M2MDevice::create_resource(DeviceResource resource)
nexpaq 0:6c56fb4bc5f0 256 {
nexpaq 0:6c56fb4bc5f0 257 M2MResource* res = NULL;
nexpaq 0:6c56fb4bc5f0 258 if(!is_resource_present(resource)) {
nexpaq 0:6c56fb4bc5f0 259 const char* device_Id_ptr = "";
nexpaq 0:6c56fb4bc5f0 260 if(FactoryReset == resource) {
nexpaq 0:6c56fb4bc5f0 261 device_Id_ptr = DEVICE_FACTORY_RESET;
nexpaq 0:6c56fb4bc5f0 262 } else if(ResetErrorCode == resource) {
nexpaq 0:6c56fb4bc5f0 263 device_Id_ptr = DEVICE_RESET_ERROR_CODE;
nexpaq 0:6c56fb4bc5f0 264 }
nexpaq 0:6c56fb4bc5f0 265 const String device_Id(device_Id_ptr);
nexpaq 0:6c56fb4bc5f0 266
nexpaq 0:6c56fb4bc5f0 267 if(_device_instance && !device_Id.empty()) {
nexpaq 0:6c56fb4bc5f0 268 res = _device_instance->create_dynamic_resource(device_Id,
nexpaq 0:6c56fb4bc5f0 269 OMA_RESOURCE_TYPE,
nexpaq 0:6c56fb4bc5f0 270 M2MResourceInstance::OPAQUE,
nexpaq 0:6c56fb4bc5f0 271 true);
nexpaq 0:6c56fb4bc5f0 272 M2MResource *resource = _device_instance->resource(device_Id);
nexpaq 0:6c56fb4bc5f0 273 if(resource) {
nexpaq 0:6c56fb4bc5f0 274 resource->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 275 }
nexpaq 0:6c56fb4bc5f0 276 if(res) {
nexpaq 0:6c56fb4bc5f0 277 res->set_operation(M2MBase::POST_ALLOWED);
nexpaq 0:6c56fb4bc5f0 278 res->set_register_uri(false);
nexpaq 0:6c56fb4bc5f0 279 }
nexpaq 0:6c56fb4bc5f0 280 }
nexpaq 0:6c56fb4bc5f0 281 }
nexpaq 0:6c56fb4bc5f0 282 return res;
nexpaq 0:6c56fb4bc5f0 283 }
nexpaq 0:6c56fb4bc5f0 284
nexpaq 0:6c56fb4bc5f0 285 bool M2MDevice::delete_resource(DeviceResource resource)
nexpaq 0:6c56fb4bc5f0 286 {
nexpaq 0:6c56fb4bc5f0 287 bool success = false;
nexpaq 0:6c56fb4bc5f0 288 if(M2MDevice::Reboot != resource &&
nexpaq 0:6c56fb4bc5f0 289 M2MDevice::ErrorCode != resource &&
nexpaq 0:6c56fb4bc5f0 290 M2MDevice::SupportedBindingMode != resource) {
nexpaq 0:6c56fb4bc5f0 291 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 292 success = _device_instance->remove_resource(resource_name(resource));
nexpaq 0:6c56fb4bc5f0 293 }
nexpaq 0:6c56fb4bc5f0 294 }
nexpaq 0:6c56fb4bc5f0 295 return success;
nexpaq 0:6c56fb4bc5f0 296 }
nexpaq 0:6c56fb4bc5f0 297
nexpaq 0:6c56fb4bc5f0 298 bool M2MDevice::delete_resource_instance(DeviceResource resource,
nexpaq 0:6c56fb4bc5f0 299 uint16_t instance_id)
nexpaq 0:6c56fb4bc5f0 300 {
nexpaq 0:6c56fb4bc5f0 301 bool success = false;
nexpaq 0:6c56fb4bc5f0 302 if(M2MDevice::Reboot != resource &&
nexpaq 0:6c56fb4bc5f0 303 M2MDevice::ErrorCode != resource &&
nexpaq 0:6c56fb4bc5f0 304 M2MDevice::SupportedBindingMode != resource) {
nexpaq 0:6c56fb4bc5f0 305 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 306 success = _device_instance->remove_resource_instance(resource_name(resource),instance_id);
nexpaq 0:6c56fb4bc5f0 307 }
nexpaq 0:6c56fb4bc5f0 308 }
nexpaq 0:6c56fb4bc5f0 309 return success;
nexpaq 0:6c56fb4bc5f0 310 }
nexpaq 0:6c56fb4bc5f0 311
nexpaq 0:6c56fb4bc5f0 312 bool M2MDevice::set_resource_value(DeviceResource resource,
nexpaq 0:6c56fb4bc5f0 313 const String &value,
nexpaq 0:6c56fb4bc5f0 314 uint16_t instance_id)
nexpaq 0:6c56fb4bc5f0 315 {
nexpaq 0:6c56fb4bc5f0 316 bool success = false;
nexpaq 0:6c56fb4bc5f0 317 M2MResourceInstance* res = get_resource_instance(resource,instance_id);
nexpaq 0:6c56fb4bc5f0 318 if(res && value.size() <= MAX_ALLOWED_STRING_LENGTH) {
nexpaq 0:6c56fb4bc5f0 319 if(M2MDevice::Manufacturer == resource ||
nexpaq 0:6c56fb4bc5f0 320 M2MDevice::ModelNumber == resource ||
nexpaq 0:6c56fb4bc5f0 321 M2MDevice::DeviceType == resource ||
nexpaq 0:6c56fb4bc5f0 322 M2MDevice::SerialNumber == resource ||
nexpaq 0:6c56fb4bc5f0 323 M2MDevice::HardwareVersion == resource ||
nexpaq 0:6c56fb4bc5f0 324 M2MDevice::FirmwareVersion == resource ||
nexpaq 0:6c56fb4bc5f0 325 M2MDevice::SoftwareVersion == resource ||
nexpaq 0:6c56fb4bc5f0 326 M2MDevice::UTCOffset == resource ||
nexpaq 0:6c56fb4bc5f0 327 M2MDevice::Timezone == resource) {
nexpaq 0:6c56fb4bc5f0 328 if (value.empty()) {
nexpaq 0:6c56fb4bc5f0 329 res->clear_value();
nexpaq 0:6c56fb4bc5f0 330 success = true;
nexpaq 0:6c56fb4bc5f0 331 } else {
nexpaq 0:6c56fb4bc5f0 332 success = res->set_value((const uint8_t*)value.c_str(),(uint32_t)value.length());
nexpaq 0:6c56fb4bc5f0 333 }
nexpaq 0:6c56fb4bc5f0 334 }
nexpaq 0:6c56fb4bc5f0 335 }
nexpaq 0:6c56fb4bc5f0 336 return success;
nexpaq 0:6c56fb4bc5f0 337 }
nexpaq 0:6c56fb4bc5f0 338
nexpaq 0:6c56fb4bc5f0 339 bool M2MDevice::set_resource_value(DeviceResource resource,
nexpaq 0:6c56fb4bc5f0 340 int64_t value,
nexpaq 0:6c56fb4bc5f0 341 uint16_t instance_id)
nexpaq 0:6c56fb4bc5f0 342 {
nexpaq 0:6c56fb4bc5f0 343 bool success = false;
nexpaq 0:6c56fb4bc5f0 344 M2MResourceInstance* res = get_resource_instance(resource,instance_id);
nexpaq 0:6c56fb4bc5f0 345 if(res) {
nexpaq 0:6c56fb4bc5f0 346 if(M2MDevice::BatteryLevel == resource ||
nexpaq 0:6c56fb4bc5f0 347 M2MDevice::BatteryStatus == resource ||
nexpaq 0:6c56fb4bc5f0 348 M2MDevice::MemoryFree == resource ||
nexpaq 0:6c56fb4bc5f0 349 M2MDevice::MemoryTotal == resource ||
nexpaq 0:6c56fb4bc5f0 350 M2MDevice::ErrorCode == resource ||
nexpaq 0:6c56fb4bc5f0 351 M2MDevice::CurrentTime == resource ||
nexpaq 0:6c56fb4bc5f0 352 M2MDevice::AvailablePowerSources == resource ||
nexpaq 0:6c56fb4bc5f0 353 M2MDevice::PowerSourceVoltage == resource ||
nexpaq 0:6c56fb4bc5f0 354 M2MDevice::PowerSourceCurrent == resource) {
nexpaq 0:6c56fb4bc5f0 355 // If it is any of the above resource
nexpaq 0:6c56fb4bc5f0 356 // set the value of the resource.
nexpaq 0:6c56fb4bc5f0 357 if (check_value_range(resource, value)) {
nexpaq 0:6c56fb4bc5f0 358
nexpaq 0:6c56fb4bc5f0 359 success = res->set_value(value);
nexpaq 0:6c56fb4bc5f0 360 }
nexpaq 0:6c56fb4bc5f0 361 }
nexpaq 0:6c56fb4bc5f0 362 }
nexpaq 0:6c56fb4bc5f0 363 return success;
nexpaq 0:6c56fb4bc5f0 364 }
nexpaq 0:6c56fb4bc5f0 365
nexpaq 0:6c56fb4bc5f0 366 String M2MDevice::resource_value_string(DeviceResource resource,
nexpaq 0:6c56fb4bc5f0 367 uint16_t instance_id) const
nexpaq 0:6c56fb4bc5f0 368 {
nexpaq 0:6c56fb4bc5f0 369 String value = "";
nexpaq 0:6c56fb4bc5f0 370 M2MResourceInstance* res = get_resource_instance(resource,instance_id);
nexpaq 0:6c56fb4bc5f0 371 if(res) {
nexpaq 0:6c56fb4bc5f0 372 if(M2MDevice::Manufacturer == resource ||
nexpaq 0:6c56fb4bc5f0 373 M2MDevice::ModelNumber == resource ||
nexpaq 0:6c56fb4bc5f0 374 M2MDevice::DeviceType == resource ||
nexpaq 0:6c56fb4bc5f0 375 M2MDevice::SerialNumber == resource ||
nexpaq 0:6c56fb4bc5f0 376 M2MDevice::HardwareVersion == resource ||
nexpaq 0:6c56fb4bc5f0 377 M2MDevice::FirmwareVersion == resource ||
nexpaq 0:6c56fb4bc5f0 378 M2MDevice::SoftwareVersion == resource ||
nexpaq 0:6c56fb4bc5f0 379 M2MDevice::UTCOffset == resource ||
nexpaq 0:6c56fb4bc5f0 380 M2MDevice::Timezone == resource) {
nexpaq 0:6c56fb4bc5f0 381
nexpaq 0:6c56fb4bc5f0 382
nexpaq 0:6c56fb4bc5f0 383 value = res->get_value_string();
nexpaq 0:6c56fb4bc5f0 384 }
nexpaq 0:6c56fb4bc5f0 385 }
nexpaq 0:6c56fb4bc5f0 386 return value;
nexpaq 0:6c56fb4bc5f0 387 }
nexpaq 0:6c56fb4bc5f0 388
nexpaq 0:6c56fb4bc5f0 389 int64_t M2MDevice::resource_value_int(DeviceResource resource,
nexpaq 0:6c56fb4bc5f0 390 uint16_t instance_id) const
nexpaq 0:6c56fb4bc5f0 391 {
nexpaq 0:6c56fb4bc5f0 392 int64_t value = -1;
nexpaq 0:6c56fb4bc5f0 393 M2MResourceInstance* res = get_resource_instance(resource,instance_id);
nexpaq 0:6c56fb4bc5f0 394 if(res) {
nexpaq 0:6c56fb4bc5f0 395 if(M2MDevice::BatteryLevel == resource ||
nexpaq 0:6c56fb4bc5f0 396 M2MDevice::BatteryStatus == resource ||
nexpaq 0:6c56fb4bc5f0 397 M2MDevice::MemoryFree == resource ||
nexpaq 0:6c56fb4bc5f0 398 M2MDevice::MemoryTotal == resource ||
nexpaq 0:6c56fb4bc5f0 399 M2MDevice::ErrorCode == resource ||
nexpaq 0:6c56fb4bc5f0 400 M2MDevice::CurrentTime == resource ||
nexpaq 0:6c56fb4bc5f0 401 M2MDevice::AvailablePowerSources == resource ||
nexpaq 0:6c56fb4bc5f0 402 M2MDevice::PowerSourceVoltage == resource ||
nexpaq 0:6c56fb4bc5f0 403 M2MDevice::PowerSourceCurrent == resource) {
nexpaq 0:6c56fb4bc5f0 404
nexpaq 0:6c56fb4bc5f0 405 // note: the value may be 32bit int on 32b archs.
nexpaq 0:6c56fb4bc5f0 406 value = res->get_value_int();
nexpaq 0:6c56fb4bc5f0 407 }
nexpaq 0:6c56fb4bc5f0 408 }
nexpaq 0:6c56fb4bc5f0 409 return value;
nexpaq 0:6c56fb4bc5f0 410 }
nexpaq 0:6c56fb4bc5f0 411
nexpaq 0:6c56fb4bc5f0 412 bool M2MDevice::is_resource_present(DeviceResource resource) const
nexpaq 0:6c56fb4bc5f0 413 {
nexpaq 0:6c56fb4bc5f0 414 bool success = false;
nexpaq 0:6c56fb4bc5f0 415 M2MResourceInstance* res = get_resource_instance(resource,0);
nexpaq 0:6c56fb4bc5f0 416 if(res) {
nexpaq 0:6c56fb4bc5f0 417 success = true;
nexpaq 0:6c56fb4bc5f0 418 }
nexpaq 0:6c56fb4bc5f0 419 return success;
nexpaq 0:6c56fb4bc5f0 420 }
nexpaq 0:6c56fb4bc5f0 421
nexpaq 0:6c56fb4bc5f0 422 uint16_t M2MDevice::per_resource_count(DeviceResource res) const
nexpaq 0:6c56fb4bc5f0 423 {
nexpaq 0:6c56fb4bc5f0 424 uint16_t count = 0;
nexpaq 0:6c56fb4bc5f0 425 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 426 count = _device_instance->resource_count(resource_name(res));
nexpaq 0:6c56fb4bc5f0 427 }
nexpaq 0:6c56fb4bc5f0 428 return count;
nexpaq 0:6c56fb4bc5f0 429 }
nexpaq 0:6c56fb4bc5f0 430
nexpaq 0:6c56fb4bc5f0 431 uint16_t M2MDevice::total_resource_count() const
nexpaq 0:6c56fb4bc5f0 432 {
nexpaq 0:6c56fb4bc5f0 433 uint16_t count = 0;
nexpaq 0:6c56fb4bc5f0 434 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 435 count = _device_instance->resources().size();
nexpaq 0:6c56fb4bc5f0 436 }
nexpaq 0:6c56fb4bc5f0 437 return count;
nexpaq 0:6c56fb4bc5f0 438 }
nexpaq 0:6c56fb4bc5f0 439
nexpaq 0:6c56fb4bc5f0 440 M2MResourceInstance* M2MDevice::get_resource_instance(DeviceResource dev_res,
nexpaq 0:6c56fb4bc5f0 441 uint16_t instance_id) const
nexpaq 0:6c56fb4bc5f0 442 {
nexpaq 0:6c56fb4bc5f0 443 M2MResource* res = NULL;
nexpaq 0:6c56fb4bc5f0 444 M2MResourceInstance* inst = NULL;
nexpaq 0:6c56fb4bc5f0 445 if(_device_instance) {
nexpaq 0:6c56fb4bc5f0 446 res = _device_instance->resource(resource_name(dev_res));
nexpaq 0:6c56fb4bc5f0 447 if(res) {
nexpaq 0:6c56fb4bc5f0 448 if(res->supports_multiple_instances()) {
nexpaq 0:6c56fb4bc5f0 449 inst = res->resource_instance(instance_id);
nexpaq 0:6c56fb4bc5f0 450 } else {
nexpaq 0:6c56fb4bc5f0 451 inst = res;
nexpaq 0:6c56fb4bc5f0 452 }
nexpaq 0:6c56fb4bc5f0 453 }
nexpaq 0:6c56fb4bc5f0 454 }
nexpaq 0:6c56fb4bc5f0 455 return inst;
nexpaq 0:6c56fb4bc5f0 456 }
nexpaq 0:6c56fb4bc5f0 457
nexpaq 0:6c56fb4bc5f0 458 const String M2MDevice::resource_name(DeviceResource resource)
nexpaq 0:6c56fb4bc5f0 459 {
nexpaq 0:6c56fb4bc5f0 460 const char* res_name = "";
nexpaq 0:6c56fb4bc5f0 461 switch(resource) {
nexpaq 0:6c56fb4bc5f0 462 case Manufacturer:
nexpaq 0:6c56fb4bc5f0 463 res_name = DEVICE_MANUFACTURER;
nexpaq 0:6c56fb4bc5f0 464 break;
nexpaq 0:6c56fb4bc5f0 465 case DeviceType:
nexpaq 0:6c56fb4bc5f0 466 res_name = DEVICE_DEVICE_TYPE;
nexpaq 0:6c56fb4bc5f0 467 break;
nexpaq 0:6c56fb4bc5f0 468 case ModelNumber:
nexpaq 0:6c56fb4bc5f0 469 res_name = DEVICE_MODEL_NUMBER;
nexpaq 0:6c56fb4bc5f0 470 break;
nexpaq 0:6c56fb4bc5f0 471 case SerialNumber:
nexpaq 0:6c56fb4bc5f0 472 res_name = DEVICE_SERIAL_NUMBER;
nexpaq 0:6c56fb4bc5f0 473 break;
nexpaq 0:6c56fb4bc5f0 474 case HardwareVersion:
nexpaq 0:6c56fb4bc5f0 475 res_name = DEVICE_HARDWARE_VERSION;
nexpaq 0:6c56fb4bc5f0 476 break;
nexpaq 0:6c56fb4bc5f0 477 case FirmwareVersion:
nexpaq 0:6c56fb4bc5f0 478 res_name = DEVICE_FIRMWARE_VERSION;
nexpaq 0:6c56fb4bc5f0 479 break;
nexpaq 0:6c56fb4bc5f0 480 case SoftwareVersion:
nexpaq 0:6c56fb4bc5f0 481 res_name = DEVICE_SOFTWARE_VERSION;
nexpaq 0:6c56fb4bc5f0 482 break;
nexpaq 0:6c56fb4bc5f0 483 case Reboot:
nexpaq 0:6c56fb4bc5f0 484 res_name = DEVICE_REBOOT;
nexpaq 0:6c56fb4bc5f0 485 break;
nexpaq 0:6c56fb4bc5f0 486 case FactoryReset:
nexpaq 0:6c56fb4bc5f0 487 res_name = DEVICE_FACTORY_RESET;
nexpaq 0:6c56fb4bc5f0 488 break;
nexpaq 0:6c56fb4bc5f0 489 case AvailablePowerSources:
nexpaq 0:6c56fb4bc5f0 490 res_name = DEVICE_AVAILABLE_POWER_SOURCES;
nexpaq 0:6c56fb4bc5f0 491 break;
nexpaq 0:6c56fb4bc5f0 492 case PowerSourceVoltage:
nexpaq 0:6c56fb4bc5f0 493 res_name = DEVICE_POWER_SOURCE_VOLTAGE;
nexpaq 0:6c56fb4bc5f0 494 break;
nexpaq 0:6c56fb4bc5f0 495 case PowerSourceCurrent:
nexpaq 0:6c56fb4bc5f0 496 res_name = DEVICE_POWER_SOURCE_CURRENT;
nexpaq 0:6c56fb4bc5f0 497 break;
nexpaq 0:6c56fb4bc5f0 498 case BatteryLevel:
nexpaq 0:6c56fb4bc5f0 499 res_name = DEVICE_BATTERY_LEVEL;
nexpaq 0:6c56fb4bc5f0 500 break;
nexpaq 0:6c56fb4bc5f0 501 case BatteryStatus:
nexpaq 0:6c56fb4bc5f0 502 res_name = DEVICE_BATTERY_STATUS;
nexpaq 0:6c56fb4bc5f0 503 break;
nexpaq 0:6c56fb4bc5f0 504 case MemoryFree:
nexpaq 0:6c56fb4bc5f0 505 res_name = DEVICE_MEMORY_FREE;
nexpaq 0:6c56fb4bc5f0 506 break;
nexpaq 0:6c56fb4bc5f0 507 case MemoryTotal:
nexpaq 0:6c56fb4bc5f0 508 res_name = DEVICE_MEMORY_TOTAL;
nexpaq 0:6c56fb4bc5f0 509 break;
nexpaq 0:6c56fb4bc5f0 510 case ErrorCode:
nexpaq 0:6c56fb4bc5f0 511 res_name = DEVICE_ERROR_CODE;
nexpaq 0:6c56fb4bc5f0 512 break;
nexpaq 0:6c56fb4bc5f0 513 case ResetErrorCode:
nexpaq 0:6c56fb4bc5f0 514 res_name = DEVICE_RESET_ERROR_CODE;
nexpaq 0:6c56fb4bc5f0 515 break;
nexpaq 0:6c56fb4bc5f0 516 case CurrentTime:
nexpaq 0:6c56fb4bc5f0 517 res_name = DEVICE_CURRENT_TIME;
nexpaq 0:6c56fb4bc5f0 518 break;
nexpaq 0:6c56fb4bc5f0 519 case UTCOffset:
nexpaq 0:6c56fb4bc5f0 520 res_name = DEVICE_UTC_OFFSET;
nexpaq 0:6c56fb4bc5f0 521 break;
nexpaq 0:6c56fb4bc5f0 522 case Timezone:
nexpaq 0:6c56fb4bc5f0 523 res_name = DEVICE_TIMEZONE;
nexpaq 0:6c56fb4bc5f0 524 break;
nexpaq 0:6c56fb4bc5f0 525 case SupportedBindingMode:
nexpaq 0:6c56fb4bc5f0 526 res_name = DEVICE_SUPPORTED_BINDING_MODE;
nexpaq 0:6c56fb4bc5f0 527 break;
nexpaq 0:6c56fb4bc5f0 528 }
nexpaq 0:6c56fb4bc5f0 529 return String(res_name);
nexpaq 0:6c56fb4bc5f0 530 }
nexpaq 0:6c56fb4bc5f0 531
nexpaq 0:6c56fb4bc5f0 532 bool M2MDevice::check_value_range(DeviceResource resource, int64_t value) const
nexpaq 0:6c56fb4bc5f0 533 {
nexpaq 0:6c56fb4bc5f0 534 bool success = false;
nexpaq 0:6c56fb4bc5f0 535 switch (resource) {
nexpaq 0:6c56fb4bc5f0 536 case AvailablePowerSources:
nexpaq 0:6c56fb4bc5f0 537 if(value >= 0 && value <= 7) {
nexpaq 0:6c56fb4bc5f0 538 success = true;
nexpaq 0:6c56fb4bc5f0 539 }
nexpaq 0:6c56fb4bc5f0 540 break;
nexpaq 0:6c56fb4bc5f0 541 case BatteryLevel:
nexpaq 0:6c56fb4bc5f0 542 if (value >= 0 && value <= 100) {
nexpaq 0:6c56fb4bc5f0 543 success = true;
nexpaq 0:6c56fb4bc5f0 544 }
nexpaq 0:6c56fb4bc5f0 545 break;
nexpaq 0:6c56fb4bc5f0 546 case BatteryStatus:
nexpaq 0:6c56fb4bc5f0 547 if (value >= 0 && value <= 6) {
nexpaq 0:6c56fb4bc5f0 548 success = true;
nexpaq 0:6c56fb4bc5f0 549 }
nexpaq 0:6c56fb4bc5f0 550 break;
nexpaq 0:6c56fb4bc5f0 551 case ErrorCode:
nexpaq 0:6c56fb4bc5f0 552 if (value >= 0 && value <= 8) {
nexpaq 0:6c56fb4bc5f0 553 success = true;
nexpaq 0:6c56fb4bc5f0 554 }
nexpaq 0:6c56fb4bc5f0 555 break;
nexpaq 0:6c56fb4bc5f0 556 default:
nexpaq 0:6c56fb4bc5f0 557 success = true;
nexpaq 0:6c56fb4bc5f0 558 break;
nexpaq 0:6c56fb4bc5f0 559 }
nexpaq 0:6c56fb4bc5f0 560 return success;
nexpaq 0:6c56fb4bc5f0 561 }
nexpaq 0:6c56fb4bc5f0 562