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