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