This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
edamame22
Date:
Thu Apr 13 04:48:11 2017 +0000
Revision:
0:29983394c6b6
Initial commit

Who changed what in which revision?

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