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/
mbed-client/source/m2mbase.cpp@2:b894b3508057, 2017-09-05 (annotated)
- Committer:
- Ren Boting
- Date:
- Tue Sep 05 11:56:13 2017 +0900
- Revision:
- 2:b894b3508057
- Parent:
- 0:29983394c6b6
Update all libraries and reform main.cpp
Who changed what in which revision?
User | Revision | Line number | New 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/m2mbase.h" |
edamame22 | 0:29983394c6b6 | 18 | #include "mbed-client/m2mobservationhandler.h" |
edamame22 | 0:29983394c6b6 | 19 | #include "mbed-client/m2mconstants.h" |
edamame22 | 0:29983394c6b6 | 20 | #include "mbed-client/m2mtimer.h" |
edamame22 | 0:29983394c6b6 | 21 | |
edamame22 | 0:29983394c6b6 | 22 | #include "mbed-client/m2mobject.h" |
edamame22 | 0:29983394c6b6 | 23 | #include "mbed-client/m2mobjectinstance.h" |
edamame22 | 0:29983394c6b6 | 24 | #include "mbed-client/m2mresource.h" |
edamame22 | 0:29983394c6b6 | 25 | |
edamame22 | 0:29983394c6b6 | 26 | #include "include/m2mreporthandler.h" |
edamame22 | 0:29983394c6b6 | 27 | #include "include/nsdlaccesshelper.h" |
edamame22 | 0:29983394c6b6 | 28 | #include "mbed-trace/mbed_trace.h" |
edamame22 | 0:29983394c6b6 | 29 | #include <assert.h> |
edamame22 | 0:29983394c6b6 | 30 | #include <ctype.h> |
edamame22 | 0:29983394c6b6 | 31 | #include <string.h> |
edamame22 | 0:29983394c6b6 | 32 | #include <stdlib.h> |
edamame22 | 0:29983394c6b6 | 33 | |
edamame22 | 0:29983394c6b6 | 34 | #define TRACE_GROUP "mClt" |
edamame22 | 0:29983394c6b6 | 35 | |
edamame22 | 0:29983394c6b6 | 36 | M2MBase::M2MBase(const String& resource_name, |
edamame22 | 0:29983394c6b6 | 37 | M2MBase::Mode mode, |
edamame22 | 0:29983394c6b6 | 38 | const String &resource_type, |
edamame22 | 0:29983394c6b6 | 39 | char *path, |
edamame22 | 0:29983394c6b6 | 40 | bool external_blockwise_store) |
edamame22 | 0:29983394c6b6 | 41 | : |
edamame22 | 0:29983394c6b6 | 42 | _sn_resource(NULL), |
edamame22 | 0:29983394c6b6 | 43 | _report_handler(NULL), |
edamame22 | 0:29983394c6b6 | 44 | _observation_handler(NULL), |
edamame22 | 0:29983394c6b6 | 45 | _token(NULL), |
edamame22 | 0:29983394c6b6 | 46 | _function_pointer(NULL), |
edamame22 | 0:29983394c6b6 | 47 | _value_updated_callback(NULL), |
edamame22 | 0:29983394c6b6 | 48 | _observation_number(0), |
edamame22 | 0:29983394c6b6 | 49 | _token_length(0), |
edamame22 | 0:29983394c6b6 | 50 | _observation_level(M2MBase::None), |
edamame22 | 0:29983394c6b6 | 51 | _is_under_observation(false) |
edamame22 | 0:29983394c6b6 | 52 | { |
edamame22 | 0:29983394c6b6 | 53 | // Checking the name length properly, i.e returning error is impossible from constructor without exceptions |
edamame22 | 0:29983394c6b6 | 54 | assert(resource_name.length() <= MAX_ALLOWED_STRING_LENGTH); |
edamame22 | 0:29983394c6b6 | 55 | |
edamame22 | 0:29983394c6b6 | 56 | _sn_resource = (lwm2m_parameters_s*)memory_alloc(sizeof(lwm2m_parameters_s)); |
edamame22 | 0:29983394c6b6 | 57 | if(_sn_resource) { |
edamame22 | 0:29983394c6b6 | 58 | memset(_sn_resource, 0, sizeof(lwm2m_parameters_s)); |
edamame22 | 0:29983394c6b6 | 59 | _sn_resource->free_on_delete = true; |
edamame22 | 0:29983394c6b6 | 60 | _sn_resource->dynamic_resource_params = |
edamame22 | 0:29983394c6b6 | 61 | (sn_nsdl_dynamic_resource_parameters_s*)memory_alloc(sizeof(sn_nsdl_dynamic_resource_parameters_s)); |
edamame22 | 0:29983394c6b6 | 62 | if(_sn_resource->dynamic_resource_params) { |
edamame22 | 0:29983394c6b6 | 63 | memset(_sn_resource->dynamic_resource_params, |
edamame22 | 0:29983394c6b6 | 64 | 0, sizeof(sn_nsdl_dynamic_resource_parameters_s)); |
edamame22 | 0:29983394c6b6 | 65 | _sn_resource->dynamic_resource_params->static_resource_parameters = |
edamame22 | 0:29983394c6b6 | 66 | (sn_nsdl_static_resource_parameters_s*)memory_alloc(sizeof(sn_nsdl_static_resource_parameters_s)); |
edamame22 | 0:29983394c6b6 | 67 | |
edamame22 | 0:29983394c6b6 | 68 | // Set callback function in case of dynamic resource |
edamame22 | 0:29983394c6b6 | 69 | if (M2MBase::Dynamic == mode) { |
edamame22 | 0:29983394c6b6 | 70 | _sn_resource->dynamic_resource_params->sn_grs_dyn_res_callback = __nsdl_c_callback; |
edamame22 | 0:29983394c6b6 | 71 | } |
edamame22 | 0:29983394c6b6 | 72 | |
edamame22 | 0:29983394c6b6 | 73 | if(_sn_resource->dynamic_resource_params->static_resource_parameters) { |
edamame22 | 0:29983394c6b6 | 74 | // Cast const away to able to compile using MEMORY_OPTIMIZED_API flag |
edamame22 | 0:29983394c6b6 | 75 | sn_nsdl_static_resource_parameters_s *params = |
edamame22 | 0:29983394c6b6 | 76 | const_cast<sn_nsdl_static_resource_parameters_s *>(_sn_resource->dynamic_resource_params->static_resource_parameters); |
edamame22 | 0:29983394c6b6 | 77 | memset(params, 0, sizeof(sn_nsdl_static_resource_parameters_s)); |
edamame22 | 0:29983394c6b6 | 78 | const size_t len = strlen(resource_type.c_str()); |
edamame22 | 0:29983394c6b6 | 79 | if (len > 0) { |
edamame22 | 0:29983394c6b6 | 80 | params->resource_type_ptr = (char*) |
edamame22 | 0:29983394c6b6 | 81 | alloc_string_copy((uint8_t*) resource_type.c_str(), len); |
edamame22 | 0:29983394c6b6 | 82 | } |
edamame22 | 0:29983394c6b6 | 83 | params->path = (uint8_t*)path; |
edamame22 | 0:29983394c6b6 | 84 | params->pathlen = strlen(path); |
edamame22 | 0:29983394c6b6 | 85 | |
edamame22 | 0:29983394c6b6 | 86 | |
edamame22 | 0:29983394c6b6 | 87 | params->mode = (const uint8_t)mode; |
edamame22 | 0:29983394c6b6 | 88 | params->free_on_delete = true; |
edamame22 | 0:29983394c6b6 | 89 | params->external_memory_block = external_blockwise_store; |
edamame22 | 0:29983394c6b6 | 90 | _sn_resource->dynamic_resource_params->static_resource_parameters = params; |
edamame22 | 0:29983394c6b6 | 91 | } |
edamame22 | 0:29983394c6b6 | 92 | } |
edamame22 | 0:29983394c6b6 | 93 | |
edamame22 | 0:29983394c6b6 | 94 | _sn_resource->name = stringdup((char*)resource_name.c_str()); |
edamame22 | 0:29983394c6b6 | 95 | _sn_resource->dynamic_resource_params->publish_uri = true; |
edamame22 | 0:29983394c6b6 | 96 | _sn_resource->dynamic_resource_params->free_on_delete = true; |
edamame22 | 0:29983394c6b6 | 97 | |
edamame22 | 0:29983394c6b6 | 98 | if(is_integer(resource_name) && resource_name.size() <= MAX_ALLOWED_STRING_LENGTH) { |
edamame22 | 0:29983394c6b6 | 99 | _sn_resource->name_id = strtoul(resource_name.c_str(), NULL, 10); |
edamame22 | 0:29983394c6b6 | 100 | if(_sn_resource->name_id > 65535){ |
edamame22 | 0:29983394c6b6 | 101 | _sn_resource->name_id = -1; |
edamame22 | 0:29983394c6b6 | 102 | } |
edamame22 | 0:29983394c6b6 | 103 | } else { |
edamame22 | 0:29983394c6b6 | 104 | _sn_resource->name_id = -1; |
edamame22 | 0:29983394c6b6 | 105 | } |
edamame22 | 0:29983394c6b6 | 106 | } |
edamame22 | 0:29983394c6b6 | 107 | } |
edamame22 | 0:29983394c6b6 | 108 | |
edamame22 | 0:29983394c6b6 | 109 | M2MBase::M2MBase(const lwm2m_parameters_s *s): |
edamame22 | 0:29983394c6b6 | 110 | _sn_resource((lwm2m_parameters_s*) s), |
edamame22 | 0:29983394c6b6 | 111 | _report_handler(NULL), |
edamame22 | 0:29983394c6b6 | 112 | _observation_handler(NULL), |
edamame22 | 0:29983394c6b6 | 113 | _token(NULL), |
edamame22 | 0:29983394c6b6 | 114 | _function_pointer(NULL), |
edamame22 | 0:29983394c6b6 | 115 | _value_updated_callback(NULL), |
edamame22 | 0:29983394c6b6 | 116 | _observation_number(0), |
edamame22 | 0:29983394c6b6 | 117 | _token_length(0), |
edamame22 | 0:29983394c6b6 | 118 | _observation_level(M2MBase::None), |
edamame22 | 0:29983394c6b6 | 119 | _is_under_observation(false) |
edamame22 | 0:29983394c6b6 | 120 | { |
edamame22 | 0:29983394c6b6 | 121 | // Set callback function in case of dynamic resource |
edamame22 | 0:29983394c6b6 | 122 | if (M2MBase::Dynamic == _sn_resource->dynamic_resource_params->static_resource_parameters->mode) { |
edamame22 | 0:29983394c6b6 | 123 | _sn_resource->dynamic_resource_params->sn_grs_dyn_res_callback = __nsdl_c_callback; |
edamame22 | 0:29983394c6b6 | 124 | } |
edamame22 | 0:29983394c6b6 | 125 | } |
edamame22 | 0:29983394c6b6 | 126 | |
edamame22 | 0:29983394c6b6 | 127 | M2MBase::~M2MBase() |
edamame22 | 0:29983394c6b6 | 128 | { |
edamame22 | 0:29983394c6b6 | 129 | delete _report_handler; |
edamame22 | 0:29983394c6b6 | 130 | free_resources(); |
edamame22 | 0:29983394c6b6 | 131 | free(_token); |
edamame22 | 0:29983394c6b6 | 132 | delete _function_pointer; |
edamame22 | 0:29983394c6b6 | 133 | delete _value_updated_callback; |
edamame22 | 0:29983394c6b6 | 134 | } |
edamame22 | 0:29983394c6b6 | 135 | |
edamame22 | 0:29983394c6b6 | 136 | char* M2MBase::create_path(const M2MObject &parent, uint16_t object_instance) |
edamame22 | 0:29983394c6b6 | 137 | { |
edamame22 | 0:29983394c6b6 | 138 | StringBuffer<5> obj_inst_id; |
edamame22 | 0:29983394c6b6 | 139 | obj_inst_id.append_int(object_instance); |
edamame22 | 0:29983394c6b6 | 140 | |
edamame22 | 0:29983394c6b6 | 141 | return create_path(parent, obj_inst_id.c_str()); |
edamame22 | 0:29983394c6b6 | 142 | } |
edamame22 | 0:29983394c6b6 | 143 | |
edamame22 | 0:29983394c6b6 | 144 | char* M2MBase::create_path(const M2MObject &parent, const char *name) |
edamame22 | 0:29983394c6b6 | 145 | { |
edamame22 | 0:29983394c6b6 | 146 | char * result = NULL; |
edamame22 | 0:29983394c6b6 | 147 | StringBuffer<(MAX_NAME_SIZE * 2 + (2 + 1))> path; |
edamame22 | 0:29983394c6b6 | 148 | |
edamame22 | 0:29983394c6b6 | 149 | const char* obj_name = parent.name(); |
edamame22 | 0:29983394c6b6 | 150 | |
edamame22 | 0:29983394c6b6 | 151 | // XXX: ensure space |
edamame22 | 0:29983394c6b6 | 152 | path.append(obj_name); |
edamame22 | 0:29983394c6b6 | 153 | path.append('/'); |
edamame22 | 0:29983394c6b6 | 154 | path.append(name); |
edamame22 | 0:29983394c6b6 | 155 | |
edamame22 | 0:29983394c6b6 | 156 | result = stringdup(path.c_str()); |
edamame22 | 0:29983394c6b6 | 157 | return result; |
edamame22 | 0:29983394c6b6 | 158 | } |
edamame22 | 0:29983394c6b6 | 159 | |
edamame22 | 0:29983394c6b6 | 160 | char* M2MBase::create_path(const M2MResource &parent, uint16_t resource_instance) |
edamame22 | 0:29983394c6b6 | 161 | { |
edamame22 | 0:29983394c6b6 | 162 | StringBuffer<5> res_inst; |
edamame22 | 0:29983394c6b6 | 163 | res_inst.append_int(resource_instance); |
edamame22 | 0:29983394c6b6 | 164 | |
edamame22 | 0:29983394c6b6 | 165 | return create_path(parent, res_inst.c_str()); |
edamame22 | 0:29983394c6b6 | 166 | } |
edamame22 | 0:29983394c6b6 | 167 | |
edamame22 | 0:29983394c6b6 | 168 | char* M2MBase::create_path(const M2MResource &parent, const char *name) |
edamame22 | 0:29983394c6b6 | 169 | { |
edamame22 | 0:29983394c6b6 | 170 | char * result = NULL; |
edamame22 | 0:29983394c6b6 | 171 | StringBuffer<(MAX_NAME_SIZE * 4 + (3 + 1))> path; |
edamame22 | 0:29983394c6b6 | 172 | M2MObjectInstance& parent_object_instance = parent.get_parent_object_instance(); |
edamame22 | 0:29983394c6b6 | 173 | M2MObject& parent_object = parent_object_instance.get_parent_object(); |
edamame22 | 0:29983394c6b6 | 174 | |
edamame22 | 0:29983394c6b6 | 175 | const char* obj_name = parent_object.name(); |
edamame22 | 0:29983394c6b6 | 176 | |
edamame22 | 0:29983394c6b6 | 177 | // Note: the parent_object_instance.name() contains name of its parent object, not the name of instance, |
edamame22 | 0:29983394c6b6 | 178 | // so we need to skip that here |
edamame22 | 0:29983394c6b6 | 179 | const uint16_t obj_inst_id = parent_object_instance.instance_id(); |
edamame22 | 0:29983394c6b6 | 180 | |
edamame22 | 0:29983394c6b6 | 181 | const char* resource_name = parent.name(); |
edamame22 | 0:29983394c6b6 | 182 | |
edamame22 | 0:29983394c6b6 | 183 | // XXX: ensure space |
edamame22 | 0:29983394c6b6 | 184 | path.append(obj_name); |
edamame22 | 0:29983394c6b6 | 185 | path.append('/'); |
edamame22 | 0:29983394c6b6 | 186 | path.append_int(obj_inst_id); |
edamame22 | 0:29983394c6b6 | 187 | path.append('/'); |
edamame22 | 0:29983394c6b6 | 188 | path.append(resource_name); |
edamame22 | 0:29983394c6b6 | 189 | path.append('/'); |
edamame22 | 0:29983394c6b6 | 190 | path.append(name); |
edamame22 | 0:29983394c6b6 | 191 | |
edamame22 | 0:29983394c6b6 | 192 | result = stringdup(path.c_str()); |
edamame22 | 0:29983394c6b6 | 193 | return result; |
edamame22 | 0:29983394c6b6 | 194 | } |
edamame22 | 0:29983394c6b6 | 195 | |
edamame22 | 0:29983394c6b6 | 196 | char* M2MBase::create_path(const M2MObjectInstance &parent, const char *name) |
edamame22 | 0:29983394c6b6 | 197 | { |
edamame22 | 0:29983394c6b6 | 198 | char * result = NULL; |
edamame22 | 0:29983394c6b6 | 199 | StringBuffer<(MAX_NAME_SIZE * 3 + (2 + 1))> path; |
edamame22 | 0:29983394c6b6 | 200 | M2MObject& parent_object = parent.get_parent_object(); |
edamame22 | 0:29983394c6b6 | 201 | |
edamame22 | 0:29983394c6b6 | 202 | const char* obj_name = parent_object.name(); |
edamame22 | 0:29983394c6b6 | 203 | // Note: the parent_object_instance.name() contains name of its parent object, not the name of instance, |
edamame22 | 0:29983394c6b6 | 204 | // so we need to skip that here |
edamame22 | 0:29983394c6b6 | 205 | const uint16_t obj_inst_id = parent.instance_id(); |
edamame22 | 0:29983394c6b6 | 206 | |
edamame22 | 0:29983394c6b6 | 207 | // XXX: ensure space |
edamame22 | 0:29983394c6b6 | 208 | path.append(obj_name); |
edamame22 | 0:29983394c6b6 | 209 | path.append('/'); |
edamame22 | 0:29983394c6b6 | 210 | path.append_int(obj_inst_id); |
edamame22 | 0:29983394c6b6 | 211 | path.append('/'); |
edamame22 | 0:29983394c6b6 | 212 | path.append(name); |
edamame22 | 0:29983394c6b6 | 213 | |
edamame22 | 0:29983394c6b6 | 214 | result = stringdup(path.c_str()); |
edamame22 | 0:29983394c6b6 | 215 | return result; |
edamame22 | 0:29983394c6b6 | 216 | } |
edamame22 | 0:29983394c6b6 | 217 | |
edamame22 | 0:29983394c6b6 | 218 | |
edamame22 | 0:29983394c6b6 | 219 | void M2MBase::set_operation(M2MBase::Operation opr) |
edamame22 | 0:29983394c6b6 | 220 | { |
edamame22 | 0:29983394c6b6 | 221 | // If the mode is Static, there is only GET_ALLOWED supported. |
edamame22 | 0:29983394c6b6 | 222 | if(M2MBase::Static == mode()) { |
edamame22 | 0:29983394c6b6 | 223 | _sn_resource->dynamic_resource_params->access = M2MBase::GET_ALLOWED; |
edamame22 | 0:29983394c6b6 | 224 | } else { |
edamame22 | 0:29983394c6b6 | 225 | _sn_resource->dynamic_resource_params->access = opr; |
edamame22 | 0:29983394c6b6 | 226 | } |
edamame22 | 0:29983394c6b6 | 227 | } |
edamame22 | 0:29983394c6b6 | 228 | |
edamame22 | 0:29983394c6b6 | 229 | #ifndef MEMORY_OPTIMIZED_API |
edamame22 | 0:29983394c6b6 | 230 | void M2MBase::set_interface_description(const char *desc) |
edamame22 | 0:29983394c6b6 | 231 | { |
edamame22 | 0:29983394c6b6 | 232 | assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete); |
edamame22 | 0:29983394c6b6 | 233 | free(_sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr); |
edamame22 | 0:29983394c6b6 | 234 | _sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr = NULL; |
edamame22 | 0:29983394c6b6 | 235 | const size_t len = strlen(desc); |
edamame22 | 0:29983394c6b6 | 236 | if (len > 0 ) { |
edamame22 | 0:29983394c6b6 | 237 | _sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr = |
edamame22 | 0:29983394c6b6 | 238 | (char*)alloc_string_copy((uint8_t*) desc, len); |
edamame22 | 0:29983394c6b6 | 239 | } |
edamame22 | 0:29983394c6b6 | 240 | } |
edamame22 | 0:29983394c6b6 | 241 | |
edamame22 | 0:29983394c6b6 | 242 | void M2MBase::set_interface_description(const String &desc) |
edamame22 | 0:29983394c6b6 | 243 | { |
edamame22 | 0:29983394c6b6 | 244 | assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete); |
edamame22 | 0:29983394c6b6 | 245 | set_interface_description(desc.c_str()); |
edamame22 | 0:29983394c6b6 | 246 | } |
edamame22 | 0:29983394c6b6 | 247 | |
edamame22 | 0:29983394c6b6 | 248 | void M2MBase::set_resource_type(const String &res_type) |
edamame22 | 0:29983394c6b6 | 249 | { |
edamame22 | 0:29983394c6b6 | 250 | assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete); |
edamame22 | 0:29983394c6b6 | 251 | set_resource_type(res_type.c_str()); |
edamame22 | 0:29983394c6b6 | 252 | } |
edamame22 | 0:29983394c6b6 | 253 | |
edamame22 | 0:29983394c6b6 | 254 | void M2MBase::set_resource_type(const char *res_type) |
edamame22 | 0:29983394c6b6 | 255 | { |
edamame22 | 0:29983394c6b6 | 256 | assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete); |
edamame22 | 0:29983394c6b6 | 257 | free(_sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr); |
edamame22 | 0:29983394c6b6 | 258 | _sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr = NULL; |
edamame22 | 0:29983394c6b6 | 259 | const size_t len = strlen(res_type); |
edamame22 | 0:29983394c6b6 | 260 | if (len > 0) { |
edamame22 | 0:29983394c6b6 | 261 | _sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr = (char*) |
edamame22 | 0:29983394c6b6 | 262 | alloc_string_copy((uint8_t*) res_type, len); |
edamame22 | 0:29983394c6b6 | 263 | } |
edamame22 | 0:29983394c6b6 | 264 | } |
edamame22 | 0:29983394c6b6 | 265 | #endif |
edamame22 | 0:29983394c6b6 | 266 | |
edamame22 | 0:29983394c6b6 | 267 | void M2MBase::set_coap_content_type(const uint8_t con_type) |
edamame22 | 0:29983394c6b6 | 268 | { |
edamame22 | 0:29983394c6b6 | 269 | _sn_resource->dynamic_resource_params->coap_content_type = con_type; |
edamame22 | 0:29983394c6b6 | 270 | } |
edamame22 | 0:29983394c6b6 | 271 | |
edamame22 | 0:29983394c6b6 | 272 | void M2MBase::set_observable(bool observable) |
edamame22 | 0:29983394c6b6 | 273 | { |
edamame22 | 0:29983394c6b6 | 274 | _sn_resource->dynamic_resource_params->observable = observable; |
edamame22 | 0:29983394c6b6 | 275 | } |
edamame22 | 0:29983394c6b6 | 276 | |
edamame22 | 0:29983394c6b6 | 277 | void M2MBase::add_observation_level(M2MBase::Observation obs_level) |
edamame22 | 0:29983394c6b6 | 278 | { |
edamame22 | 0:29983394c6b6 | 279 | _observation_level = (M2MBase::Observation)(_observation_level | obs_level); |
edamame22 | 0:29983394c6b6 | 280 | } |
edamame22 | 0:29983394c6b6 | 281 | |
edamame22 | 0:29983394c6b6 | 282 | void M2MBase::remove_observation_level(M2MBase::Observation obs_level) |
edamame22 | 0:29983394c6b6 | 283 | { |
edamame22 | 0:29983394c6b6 | 284 | _observation_level = (M2MBase::Observation)(_observation_level & ~obs_level); |
edamame22 | 0:29983394c6b6 | 285 | } |
edamame22 | 0:29983394c6b6 | 286 | |
edamame22 | 0:29983394c6b6 | 287 | void M2MBase::set_observation_handler(M2MObservationHandler *handler) |
edamame22 | 0:29983394c6b6 | 288 | { |
edamame22 | 0:29983394c6b6 | 289 | tr_debug("M2MBase::set_observation_handler - handler: 0x%x", handler); |
edamame22 | 0:29983394c6b6 | 290 | _observation_handler = handler; |
edamame22 | 0:29983394c6b6 | 291 | } |
edamame22 | 0:29983394c6b6 | 292 | |
edamame22 | 0:29983394c6b6 | 293 | |
edamame22 | 0:29983394c6b6 | 294 | void M2MBase::set_under_observation(bool observed, |
edamame22 | 0:29983394c6b6 | 295 | M2MObservationHandler *handler) |
edamame22 | 0:29983394c6b6 | 296 | { |
edamame22 | 0:29983394c6b6 | 297 | tr_debug("M2MBase::set_under_observation - observed: %d", observed); |
edamame22 | 0:29983394c6b6 | 298 | tr_debug("M2MBase::set_under_observation - base_type: %d", base_type()); |
edamame22 | 0:29983394c6b6 | 299 | _is_under_observation = observed; |
edamame22 | 0:29983394c6b6 | 300 | _observation_handler = handler; |
edamame22 | 0:29983394c6b6 | 301 | if (handler) { |
edamame22 | 0:29983394c6b6 | 302 | if (base_type() != M2MBase::ResourceInstance) { |
edamame22 | 0:29983394c6b6 | 303 | // Create report handler only if it does not exist and one wants observation |
edamame22 | 0:29983394c6b6 | 304 | // This saves 76 bytes of memory on most usual case. |
edamame22 | 0:29983394c6b6 | 305 | if (observed) { |
edamame22 | 0:29983394c6b6 | 306 | if(!_report_handler) { |
edamame22 | 0:29983394c6b6 | 307 | _report_handler = new M2MReportHandler(*this); |
edamame22 | 0:29983394c6b6 | 308 | } |
edamame22 | 0:29983394c6b6 | 309 | } |
edamame22 | 0:29983394c6b6 | 310 | if (_report_handler) { |
edamame22 | 0:29983394c6b6 | 311 | _report_handler->set_under_observation(observed); |
edamame22 | 0:29983394c6b6 | 312 | } |
edamame22 | 0:29983394c6b6 | 313 | } |
edamame22 | 0:29983394c6b6 | 314 | } else { |
edamame22 | 0:29983394c6b6 | 315 | delete _report_handler; |
edamame22 | 0:29983394c6b6 | 316 | _report_handler = NULL; |
edamame22 | 0:29983394c6b6 | 317 | } |
edamame22 | 0:29983394c6b6 | 318 | } |
edamame22 | 0:29983394c6b6 | 319 | |
edamame22 | 0:29983394c6b6 | 320 | void M2MBase::set_observation_token(const uint8_t *token, const uint8_t length) |
edamame22 | 0:29983394c6b6 | 321 | { |
edamame22 | 0:29983394c6b6 | 322 | free(_token); |
edamame22 | 0:29983394c6b6 | 323 | _token = NULL; |
edamame22 | 0:29983394c6b6 | 324 | _token_length = 0; |
edamame22 | 0:29983394c6b6 | 325 | |
edamame22 | 0:29983394c6b6 | 326 | if( token != NULL && length > 0 ) { |
edamame22 | 0:29983394c6b6 | 327 | _token = alloc_string_copy((uint8_t *)token, length); |
edamame22 | 0:29983394c6b6 | 328 | if(_token) { |
edamame22 | 0:29983394c6b6 | 329 | _token_length = length; |
edamame22 | 0:29983394c6b6 | 330 | } |
edamame22 | 0:29983394c6b6 | 331 | } |
edamame22 | 0:29983394c6b6 | 332 | } |
edamame22 | 0:29983394c6b6 | 333 | |
edamame22 | 0:29983394c6b6 | 334 | void M2MBase::set_instance_id(const uint16_t inst_id) |
edamame22 | 0:29983394c6b6 | 335 | { |
edamame22 | 0:29983394c6b6 | 336 | _sn_resource->instance_id = inst_id; |
edamame22 | 0:29983394c6b6 | 337 | } |
edamame22 | 0:29983394c6b6 | 338 | |
edamame22 | 0:29983394c6b6 | 339 | void M2MBase::set_observation_number(const uint16_t /*observation_number*/) |
edamame22 | 0:29983394c6b6 | 340 | { |
edamame22 | 0:29983394c6b6 | 341 | } |
edamame22 | 0:29983394c6b6 | 342 | |
edamame22 | 0:29983394c6b6 | 343 | void M2MBase::set_max_age(const uint32_t max_age) |
edamame22 | 0:29983394c6b6 | 344 | { |
edamame22 | 0:29983394c6b6 | 345 | _sn_resource->max_age = max_age; |
edamame22 | 0:29983394c6b6 | 346 | } |
edamame22 | 0:29983394c6b6 | 347 | |
edamame22 | 0:29983394c6b6 | 348 | M2MBase::BaseType M2MBase::base_type() const |
edamame22 | 0:29983394c6b6 | 349 | { |
edamame22 | 0:29983394c6b6 | 350 | return (M2MBase::BaseType)_sn_resource->base_type; |
edamame22 | 0:29983394c6b6 | 351 | } |
edamame22 | 0:29983394c6b6 | 352 | |
edamame22 | 0:29983394c6b6 | 353 | M2MBase::Operation M2MBase::operation() const |
edamame22 | 0:29983394c6b6 | 354 | { |
edamame22 | 0:29983394c6b6 | 355 | return (M2MBase::Operation)_sn_resource->dynamic_resource_params->access; |
edamame22 | 0:29983394c6b6 | 356 | } |
edamame22 | 0:29983394c6b6 | 357 | |
edamame22 | 0:29983394c6b6 | 358 | const char* M2MBase::name() const |
edamame22 | 0:29983394c6b6 | 359 | { |
edamame22 | 0:29983394c6b6 | 360 | return _sn_resource->name; |
edamame22 | 0:29983394c6b6 | 361 | } |
edamame22 | 0:29983394c6b6 | 362 | |
edamame22 | 0:29983394c6b6 | 363 | int32_t M2MBase::name_id() const |
edamame22 | 0:29983394c6b6 | 364 | { |
edamame22 | 0:29983394c6b6 | 365 | return _sn_resource->name_id; |
edamame22 | 0:29983394c6b6 | 366 | } |
edamame22 | 0:29983394c6b6 | 367 | |
edamame22 | 0:29983394c6b6 | 368 | uint16_t M2MBase::instance_id() const |
edamame22 | 0:29983394c6b6 | 369 | { |
edamame22 | 0:29983394c6b6 | 370 | return _sn_resource->instance_id; |
edamame22 | 0:29983394c6b6 | 371 | } |
edamame22 | 0:29983394c6b6 | 372 | |
edamame22 | 0:29983394c6b6 | 373 | const char* M2MBase::interface_description() const |
edamame22 | 0:29983394c6b6 | 374 | { |
edamame22 | 0:29983394c6b6 | 375 | return (reinterpret_cast<char*>( |
edamame22 | 0:29983394c6b6 | 376 | _sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr)); |
edamame22 | 0:29983394c6b6 | 377 | } |
edamame22 | 0:29983394c6b6 | 378 | |
edamame22 | 0:29983394c6b6 | 379 | const char* M2MBase::resource_type() const |
edamame22 | 0:29983394c6b6 | 380 | { |
edamame22 | 0:29983394c6b6 | 381 | return (reinterpret_cast<char*>( |
edamame22 | 0:29983394c6b6 | 382 | _sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr)); |
edamame22 | 0:29983394c6b6 | 383 | } |
edamame22 | 0:29983394c6b6 | 384 | |
edamame22 | 0:29983394c6b6 | 385 | const char* M2MBase::uri_path() const |
edamame22 | 0:29983394c6b6 | 386 | { |
edamame22 | 0:29983394c6b6 | 387 | return (reinterpret_cast<char*>( |
edamame22 | 0:29983394c6b6 | 388 | _sn_resource->dynamic_resource_params->static_resource_parameters->path)); |
edamame22 | 0:29983394c6b6 | 389 | } |
edamame22 | 0:29983394c6b6 | 390 | |
edamame22 | 0:29983394c6b6 | 391 | uint8_t M2MBase::coap_content_type() const |
edamame22 | 0:29983394c6b6 | 392 | { |
edamame22 | 0:29983394c6b6 | 393 | return _sn_resource->dynamic_resource_params->coap_content_type; |
edamame22 | 0:29983394c6b6 | 394 | } |
edamame22 | 0:29983394c6b6 | 395 | |
edamame22 | 0:29983394c6b6 | 396 | bool M2MBase::is_observable() const |
edamame22 | 0:29983394c6b6 | 397 | { |
edamame22 | 0:29983394c6b6 | 398 | return _sn_resource->dynamic_resource_params->observable; |
edamame22 | 0:29983394c6b6 | 399 | } |
edamame22 | 0:29983394c6b6 | 400 | |
edamame22 | 0:29983394c6b6 | 401 | M2MBase::Observation M2MBase::observation_level() const |
edamame22 | 0:29983394c6b6 | 402 | { |
edamame22 | 0:29983394c6b6 | 403 | return _observation_level; |
edamame22 | 0:29983394c6b6 | 404 | } |
edamame22 | 0:29983394c6b6 | 405 | |
edamame22 | 0:29983394c6b6 | 406 | void M2MBase::get_observation_token(uint8_t *&token, uint32_t &token_length) |
edamame22 | 0:29983394c6b6 | 407 | { |
edamame22 | 0:29983394c6b6 | 408 | token_length = 0; |
edamame22 | 0:29983394c6b6 | 409 | free(token); |
edamame22 | 0:29983394c6b6 | 410 | if (_token) { |
edamame22 | 0:29983394c6b6 | 411 | token = alloc_string_copy((uint8_t *)_token, _token_length); |
edamame22 | 0:29983394c6b6 | 412 | if(token) { |
edamame22 | 0:29983394c6b6 | 413 | token_length = _token_length; |
edamame22 | 0:29983394c6b6 | 414 | } |
edamame22 | 0:29983394c6b6 | 415 | } |
edamame22 | 0:29983394c6b6 | 416 | } |
edamame22 | 0:29983394c6b6 | 417 | |
edamame22 | 0:29983394c6b6 | 418 | M2MBase::Mode M2MBase::mode() const |
edamame22 | 0:29983394c6b6 | 419 | { |
edamame22 | 0:29983394c6b6 | 420 | return (M2MBase::Mode)_sn_resource->dynamic_resource_params->static_resource_parameters->mode; |
edamame22 | 0:29983394c6b6 | 421 | } |
edamame22 | 0:29983394c6b6 | 422 | |
edamame22 | 0:29983394c6b6 | 423 | uint16_t M2MBase::observation_number() const |
edamame22 | 0:29983394c6b6 | 424 | { |
edamame22 | 0:29983394c6b6 | 425 | return _observation_number; |
edamame22 | 0:29983394c6b6 | 426 | } |
edamame22 | 0:29983394c6b6 | 427 | |
edamame22 | 0:29983394c6b6 | 428 | uint32_t M2MBase::max_age() const |
edamame22 | 0:29983394c6b6 | 429 | { |
edamame22 | 0:29983394c6b6 | 430 | return _sn_resource->max_age; |
edamame22 | 0:29983394c6b6 | 431 | } |
edamame22 | 0:29983394c6b6 | 432 | |
edamame22 | 0:29983394c6b6 | 433 | bool M2MBase::handle_observation_attribute(const char *query) |
edamame22 | 0:29983394c6b6 | 434 | { |
edamame22 | 0:29983394c6b6 | 435 | tr_debug("M2MBase::handle_observation_attribute - under observation(%d)", is_under_observation()); |
edamame22 | 0:29983394c6b6 | 436 | bool success = false; |
edamame22 | 0:29983394c6b6 | 437 | // Create handler if not already exists. Client must able to parse write attributes even when |
edamame22 | 0:29983394c6b6 | 438 | // observation is not yet set |
edamame22 | 0:29983394c6b6 | 439 | if (!_report_handler) { |
edamame22 | 0:29983394c6b6 | 440 | _report_handler = new M2MReportHandler(*this); |
edamame22 | 0:29983394c6b6 | 441 | } |
edamame22 | 0:29983394c6b6 | 442 | |
edamame22 | 0:29983394c6b6 | 443 | success = _report_handler->parse_notification_attribute(query,base_type()); |
edamame22 | 0:29983394c6b6 | 444 | if (success) { |
edamame22 | 0:29983394c6b6 | 445 | if (is_under_observation()) { |
edamame22 | 0:29983394c6b6 | 446 | _report_handler->set_under_observation(true); |
edamame22 | 0:29983394c6b6 | 447 | } |
edamame22 | 0:29983394c6b6 | 448 | } else { |
edamame22 | 0:29983394c6b6 | 449 | _report_handler->set_default_values(); |
edamame22 | 0:29983394c6b6 | 450 | } |
edamame22 | 0:29983394c6b6 | 451 | return success; |
edamame22 | 0:29983394c6b6 | 452 | } |
edamame22 | 0:29983394c6b6 | 453 | |
edamame22 | 0:29983394c6b6 | 454 | void M2MBase::observation_to_be_sent(m2m::Vector<uint16_t> changed_instance_ids, bool send_object) |
edamame22 | 0:29983394c6b6 | 455 | { |
edamame22 | 0:29983394c6b6 | 456 | //TODO: Move this to M2MResourceInstance |
edamame22 | 0:29983394c6b6 | 457 | if(_observation_handler) { |
edamame22 | 0:29983394c6b6 | 458 | _observation_number++; |
edamame22 | 0:29983394c6b6 | 459 | _observation_handler->observation_to_be_sent(this, |
edamame22 | 0:29983394c6b6 | 460 | _observation_number, |
edamame22 | 0:29983394c6b6 | 461 | changed_instance_ids, |
edamame22 | 0:29983394c6b6 | 462 | send_object); |
edamame22 | 0:29983394c6b6 | 463 | } |
edamame22 | 0:29983394c6b6 | 464 | } |
edamame22 | 0:29983394c6b6 | 465 | |
edamame22 | 0:29983394c6b6 | 466 | void M2MBase::set_base_type(M2MBase::BaseType type) |
edamame22 | 0:29983394c6b6 | 467 | { |
edamame22 | 0:29983394c6b6 | 468 | assert(_sn_resource->free_on_delete); |
edamame22 | 0:29983394c6b6 | 469 | _sn_resource->base_type = type; |
edamame22 | 0:29983394c6b6 | 470 | } |
edamame22 | 0:29983394c6b6 | 471 | |
edamame22 | 0:29983394c6b6 | 472 | sn_coap_hdr_s* M2MBase::handle_get_request(nsdl_s */*nsdl*/, |
edamame22 | 0:29983394c6b6 | 473 | sn_coap_hdr_s */*received_coap_header*/, |
edamame22 | 0:29983394c6b6 | 474 | M2MObservationHandler */*observation_handler*/) |
edamame22 | 0:29983394c6b6 | 475 | { |
edamame22 | 0:29983394c6b6 | 476 | //Handled in M2MResource, M2MObjectInstance and M2MObject classes |
edamame22 | 0:29983394c6b6 | 477 | return NULL; |
edamame22 | 0:29983394c6b6 | 478 | } |
edamame22 | 0:29983394c6b6 | 479 | |
edamame22 | 0:29983394c6b6 | 480 | sn_coap_hdr_s* M2MBase::handle_put_request(nsdl_s */*nsdl*/, |
edamame22 | 0:29983394c6b6 | 481 | sn_coap_hdr_s */*received_coap_header*/, |
edamame22 | 0:29983394c6b6 | 482 | M2MObservationHandler */*observation_handler*/, |
edamame22 | 0:29983394c6b6 | 483 | bool &) |
edamame22 | 0:29983394c6b6 | 484 | { |
edamame22 | 0:29983394c6b6 | 485 | //Handled in M2MResource, M2MObjectInstance and M2MObject classes |
edamame22 | 0:29983394c6b6 | 486 | return NULL; |
edamame22 | 0:29983394c6b6 | 487 | } |
edamame22 | 0:29983394c6b6 | 488 | |
edamame22 | 0:29983394c6b6 | 489 | sn_coap_hdr_s* M2MBase::handle_post_request(nsdl_s */*nsdl*/, |
edamame22 | 0:29983394c6b6 | 490 | sn_coap_hdr_s */*received_coap_header*/, |
edamame22 | 0:29983394c6b6 | 491 | M2MObservationHandler */*observation_handler*/, |
edamame22 | 0:29983394c6b6 | 492 | bool &, |
edamame22 | 0:29983394c6b6 | 493 | sn_nsdl_addr_s *) |
edamame22 | 0:29983394c6b6 | 494 | { |
edamame22 | 0:29983394c6b6 | 495 | //Handled in M2MResource, M2MObjectInstance and M2MObject classes |
edamame22 | 0:29983394c6b6 | 496 | return NULL; |
edamame22 | 0:29983394c6b6 | 497 | } |
edamame22 | 0:29983394c6b6 | 498 | |
edamame22 | 0:29983394c6b6 | 499 | void *M2MBase::memory_alloc(uint32_t size) |
edamame22 | 0:29983394c6b6 | 500 | { |
edamame22 | 0:29983394c6b6 | 501 | if(size) |
edamame22 | 0:29983394c6b6 | 502 | return malloc(size); |
edamame22 | 0:29983394c6b6 | 503 | else |
edamame22 | 0:29983394c6b6 | 504 | return 0; |
edamame22 | 0:29983394c6b6 | 505 | } |
edamame22 | 0:29983394c6b6 | 506 | |
edamame22 | 0:29983394c6b6 | 507 | void M2MBase::memory_free(void *ptr) |
edamame22 | 0:29983394c6b6 | 508 | { |
edamame22 | 0:29983394c6b6 | 509 | free(ptr); |
edamame22 | 0:29983394c6b6 | 510 | } |
edamame22 | 0:29983394c6b6 | 511 | |
edamame22 | 0:29983394c6b6 | 512 | char* M2MBase::alloc_string_copy(const char* source) |
edamame22 | 0:29983394c6b6 | 513 | { |
edamame22 | 0:29983394c6b6 | 514 | assert(source != NULL); |
edamame22 | 0:29983394c6b6 | 515 | |
edamame22 | 0:29983394c6b6 | 516 | // Note: the armcc's libc does not have strdup, so we need to implement it here |
edamame22 | 0:29983394c6b6 | 517 | const size_t len = strlen(source); |
edamame22 | 0:29983394c6b6 | 518 | |
edamame22 | 0:29983394c6b6 | 519 | return (char*)alloc_string_copy((uint8_t*)source, len); |
edamame22 | 0:29983394c6b6 | 520 | } |
edamame22 | 0:29983394c6b6 | 521 | |
edamame22 | 0:29983394c6b6 | 522 | uint8_t* M2MBase::alloc_string_copy(const uint8_t* source, uint32_t size) |
edamame22 | 0:29983394c6b6 | 523 | { |
edamame22 | 0:29983394c6b6 | 524 | assert(source != NULL); |
edamame22 | 0:29983394c6b6 | 525 | |
edamame22 | 0:29983394c6b6 | 526 | uint8_t* result = (uint8_t*)memory_alloc(size + 1); |
edamame22 | 0:29983394c6b6 | 527 | if (result) { |
edamame22 | 0:29983394c6b6 | 528 | memcpy(result, source, size); |
edamame22 | 0:29983394c6b6 | 529 | result[size] = '\0'; |
edamame22 | 0:29983394c6b6 | 530 | } |
edamame22 | 0:29983394c6b6 | 531 | return result; |
edamame22 | 0:29983394c6b6 | 532 | } |
edamame22 | 0:29983394c6b6 | 533 | |
edamame22 | 0:29983394c6b6 | 534 | uint8_t* M2MBase::alloc_copy(const uint8_t* source, uint32_t size) |
edamame22 | 0:29983394c6b6 | 535 | { |
edamame22 | 0:29983394c6b6 | 536 | assert(source != NULL); |
edamame22 | 0:29983394c6b6 | 537 | |
edamame22 | 0:29983394c6b6 | 538 | uint8_t* result = (uint8_t*)memory_alloc(size); |
edamame22 | 0:29983394c6b6 | 539 | if (result) { |
edamame22 | 0:29983394c6b6 | 540 | memcpy(result, source, size); |
edamame22 | 0:29983394c6b6 | 541 | } |
edamame22 | 0:29983394c6b6 | 542 | return result; |
edamame22 | 0:29983394c6b6 | 543 | } |
edamame22 | 0:29983394c6b6 | 544 | |
edamame22 | 0:29983394c6b6 | 545 | bool M2MBase::validate_string_length(const String &string, size_t min_length, size_t max_length) |
edamame22 | 0:29983394c6b6 | 546 | { |
edamame22 | 0:29983394c6b6 | 547 | bool valid = false; |
edamame22 | 0:29983394c6b6 | 548 | |
edamame22 | 0:29983394c6b6 | 549 | const size_t len = string.length(); |
edamame22 | 0:29983394c6b6 | 550 | if ((len >= min_length) && (len <= max_length)) { |
edamame22 | 0:29983394c6b6 | 551 | valid = true; |
edamame22 | 0:29983394c6b6 | 552 | } |
edamame22 | 0:29983394c6b6 | 553 | |
edamame22 | 0:29983394c6b6 | 554 | return valid; |
edamame22 | 0:29983394c6b6 | 555 | } |
edamame22 | 0:29983394c6b6 | 556 | |
edamame22 | 0:29983394c6b6 | 557 | bool M2MBase::validate_string_length(const char* string, size_t min_length, size_t max_length) |
edamame22 | 0:29983394c6b6 | 558 | { |
edamame22 | 0:29983394c6b6 | 559 | bool valid = false; |
edamame22 | 0:29983394c6b6 | 560 | |
edamame22 | 0:29983394c6b6 | 561 | if (string != NULL) { |
edamame22 | 0:29983394c6b6 | 562 | const size_t len = strlen(string); |
edamame22 | 0:29983394c6b6 | 563 | if ((len >= min_length) && (len <= max_length)) { |
edamame22 | 0:29983394c6b6 | 564 | valid = true; |
edamame22 | 0:29983394c6b6 | 565 | } |
edamame22 | 0:29983394c6b6 | 566 | } |
edamame22 | 0:29983394c6b6 | 567 | |
edamame22 | 0:29983394c6b6 | 568 | return valid; |
edamame22 | 0:29983394c6b6 | 569 | } |
edamame22 | 0:29983394c6b6 | 570 | |
edamame22 | 0:29983394c6b6 | 571 | M2MReportHandler* M2MBase::create_report_handler() |
edamame22 | 0:29983394c6b6 | 572 | { |
edamame22 | 0:29983394c6b6 | 573 | if (!_report_handler) { |
edamame22 | 0:29983394c6b6 | 574 | _report_handler = new M2MReportHandler(*this); |
edamame22 | 0:29983394c6b6 | 575 | } |
edamame22 | 0:29983394c6b6 | 576 | return _report_handler; |
edamame22 | 0:29983394c6b6 | 577 | } |
edamame22 | 0:29983394c6b6 | 578 | |
edamame22 | 0:29983394c6b6 | 579 | M2MReportHandler* M2MBase::report_handler() |
edamame22 | 0:29983394c6b6 | 580 | { |
edamame22 | 0:29983394c6b6 | 581 | return _report_handler; |
edamame22 | 0:29983394c6b6 | 582 | } |
edamame22 | 0:29983394c6b6 | 583 | |
edamame22 | 0:29983394c6b6 | 584 | M2MObservationHandler* M2MBase::observation_handler() |
edamame22 | 0:29983394c6b6 | 585 | { |
edamame22 | 0:29983394c6b6 | 586 | return _observation_handler; |
edamame22 | 0:29983394c6b6 | 587 | } |
edamame22 | 0:29983394c6b6 | 588 | |
edamame22 | 0:29983394c6b6 | 589 | void M2MBase::set_register_uri(bool register_uri) |
edamame22 | 0:29983394c6b6 | 590 | { |
edamame22 | 0:29983394c6b6 | 591 | _sn_resource->dynamic_resource_params->publish_uri = register_uri; |
edamame22 | 0:29983394c6b6 | 592 | } |
edamame22 | 0:29983394c6b6 | 593 | |
edamame22 | 0:29983394c6b6 | 594 | bool M2MBase::register_uri() |
edamame22 | 0:29983394c6b6 | 595 | { |
edamame22 | 0:29983394c6b6 | 596 | return _sn_resource->dynamic_resource_params->publish_uri; |
edamame22 | 0:29983394c6b6 | 597 | } |
edamame22 | 0:29983394c6b6 | 598 | |
edamame22 | 0:29983394c6b6 | 599 | bool M2MBase::is_integer(const String &value) |
edamame22 | 0:29983394c6b6 | 600 | { |
edamame22 | 0:29983394c6b6 | 601 | const char *s = value.c_str(); |
edamame22 | 0:29983394c6b6 | 602 | if(value.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) { |
edamame22 | 0:29983394c6b6 | 603 | return false; |
edamame22 | 0:29983394c6b6 | 604 | } |
edamame22 | 0:29983394c6b6 | 605 | char * p; |
edamame22 | 0:29983394c6b6 | 606 | strtol(value.c_str(), &p, 10); |
edamame22 | 0:29983394c6b6 | 607 | return (*p == 0); |
edamame22 | 0:29983394c6b6 | 608 | } |
edamame22 | 0:29983394c6b6 | 609 | |
edamame22 | 0:29983394c6b6 | 610 | bool M2MBase::is_integer(const char *value) |
edamame22 | 0:29983394c6b6 | 611 | { |
edamame22 | 0:29983394c6b6 | 612 | assert(value != NULL); |
edamame22 | 0:29983394c6b6 | 613 | |
edamame22 | 0:29983394c6b6 | 614 | if((strlen(value) < 1) || ((!isdigit(value[0])) && (value[0] != '-') && (value[0] != '+'))) { |
edamame22 | 0:29983394c6b6 | 615 | return false; |
edamame22 | 0:29983394c6b6 | 616 | } |
edamame22 | 0:29983394c6b6 | 617 | char * p; |
edamame22 | 0:29983394c6b6 | 618 | strtol(value, &p, 10); |
edamame22 | 0:29983394c6b6 | 619 | return (*p == 0); |
edamame22 | 0:29983394c6b6 | 620 | } |
edamame22 | 0:29983394c6b6 | 621 | |
edamame22 | 0:29983394c6b6 | 622 | bool M2MBase::is_under_observation() const |
edamame22 | 0:29983394c6b6 | 623 | { |
edamame22 | 0:29983394c6b6 | 624 | return _is_under_observation; |
edamame22 | 0:29983394c6b6 | 625 | } |
edamame22 | 0:29983394c6b6 | 626 | |
edamame22 | 0:29983394c6b6 | 627 | void M2MBase::set_value_updated_function(value_updated_callback callback) |
edamame22 | 0:29983394c6b6 | 628 | { |
edamame22 | 0:29983394c6b6 | 629 | delete _value_updated_callback; |
edamame22 | 0:29983394c6b6 | 630 | // XXX: create a copy of the copy of callback object. Perhaps it would better to |
edamame22 | 0:29983394c6b6 | 631 | // give a reference as parameter and just store that, as it would save some memory. |
edamame22 | 0:29983394c6b6 | 632 | _value_updated_callback = new value_updated_callback(callback); |
edamame22 | 0:29983394c6b6 | 633 | } |
edamame22 | 0:29983394c6b6 | 634 | |
edamame22 | 0:29983394c6b6 | 635 | void M2MBase::set_value_updated_function(value_updated_callback2 callback) |
edamame22 | 0:29983394c6b6 | 636 | { |
edamame22 | 0:29983394c6b6 | 637 | delete _function_pointer; |
edamame22 | 0:29983394c6b6 | 638 | _function_pointer = new FP1<void, const char*>(callback); |
edamame22 | 0:29983394c6b6 | 639 | set_value_updated_function(value_updated_callback(_function_pointer, |
edamame22 | 0:29983394c6b6 | 640 | &FP1<void, const char*>::call)); |
edamame22 | 0:29983394c6b6 | 641 | } |
edamame22 | 0:29983394c6b6 | 642 | |
edamame22 | 0:29983394c6b6 | 643 | bool M2MBase::is_value_updated_function_set() |
edamame22 | 0:29983394c6b6 | 644 | { |
edamame22 | 0:29983394c6b6 | 645 | return (_value_updated_callback) ? true : false; |
edamame22 | 0:29983394c6b6 | 646 | } |
edamame22 | 0:29983394c6b6 | 647 | |
edamame22 | 0:29983394c6b6 | 648 | void M2MBase::execute_value_updated(const String& name) |
edamame22 | 0:29983394c6b6 | 649 | { |
edamame22 | 0:29983394c6b6 | 650 | if(_value_updated_callback) { |
edamame22 | 0:29983394c6b6 | 651 | (*_value_updated_callback)(name.c_str()); |
edamame22 | 0:29983394c6b6 | 652 | } |
edamame22 | 0:29983394c6b6 | 653 | } |
edamame22 | 0:29983394c6b6 | 654 | |
edamame22 | 0:29983394c6b6 | 655 | bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE> &buffer, const char *s1, uint16_t i1, const char *s2, uint16_t i2) |
edamame22 | 0:29983394c6b6 | 656 | { |
edamame22 | 0:29983394c6b6 | 657 | |
edamame22 | 0:29983394c6b6 | 658 | if(!buffer.ensure_space(strlen(s1) + strlen(s2) + (MAX_INSTANCE_SIZE * 2) + 3 + 1)){ |
edamame22 | 0:29983394c6b6 | 659 | return false; |
edamame22 | 0:29983394c6b6 | 660 | } |
edamame22 | 0:29983394c6b6 | 661 | |
edamame22 | 0:29983394c6b6 | 662 | buffer.append(s1); |
edamame22 | 0:29983394c6b6 | 663 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 664 | buffer.append_int(i1); |
edamame22 | 0:29983394c6b6 | 665 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 666 | buffer.append(s2); |
edamame22 | 0:29983394c6b6 | 667 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 668 | buffer.append_int(i2); |
edamame22 | 0:29983394c6b6 | 669 | |
edamame22 | 0:29983394c6b6 | 670 | return true; |
edamame22 | 0:29983394c6b6 | 671 | |
edamame22 | 0:29983394c6b6 | 672 | } |
edamame22 | 0:29983394c6b6 | 673 | |
edamame22 | 0:29983394c6b6 | 674 | bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE_2> &buffer, const char *s1, uint16_t i1, const char *s2) |
edamame22 | 0:29983394c6b6 | 675 | { |
edamame22 | 0:29983394c6b6 | 676 | if(!buffer.ensure_space(strlen(s1) + strlen(s2) + MAX_INSTANCE_SIZE + 2 + 1)){ |
edamame22 | 0:29983394c6b6 | 677 | return false; |
edamame22 | 0:29983394c6b6 | 678 | } |
edamame22 | 0:29983394c6b6 | 679 | |
edamame22 | 0:29983394c6b6 | 680 | buffer.append(s1); |
edamame22 | 0:29983394c6b6 | 681 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 682 | buffer.append_int(i1); |
edamame22 | 0:29983394c6b6 | 683 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 684 | buffer.append(s2); |
edamame22 | 0:29983394c6b6 | 685 | |
edamame22 | 0:29983394c6b6 | 686 | return true; |
edamame22 | 0:29983394c6b6 | 687 | } |
edamame22 | 0:29983394c6b6 | 688 | |
edamame22 | 0:29983394c6b6 | 689 | bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE_3> &buffer, const char *s1, uint16_t i1, uint16_t i2) |
edamame22 | 0:29983394c6b6 | 690 | { |
edamame22 | 0:29983394c6b6 | 691 | if(!buffer.ensure_space(strlen(s1) + (MAX_INSTANCE_SIZE * 2) + 2 + 1)){ |
edamame22 | 0:29983394c6b6 | 692 | return false; |
edamame22 | 0:29983394c6b6 | 693 | } |
edamame22 | 0:29983394c6b6 | 694 | |
edamame22 | 0:29983394c6b6 | 695 | buffer.append(s1); |
edamame22 | 0:29983394c6b6 | 696 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 697 | buffer.append_int(i1); |
edamame22 | 0:29983394c6b6 | 698 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 699 | buffer.append_int(i2); |
edamame22 | 0:29983394c6b6 | 700 | |
edamame22 | 0:29983394c6b6 | 701 | return true; |
edamame22 | 0:29983394c6b6 | 702 | } |
edamame22 | 0:29983394c6b6 | 703 | |
edamame22 | 0:29983394c6b6 | 704 | bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE_4> &buffer, const char *s1, uint16_t i1) |
edamame22 | 0:29983394c6b6 | 705 | { |
edamame22 | 0:29983394c6b6 | 706 | if(!buffer.ensure_space(strlen(s1) + MAX_INSTANCE_SIZE + 1 + 1)){ |
edamame22 | 0:29983394c6b6 | 707 | return false; |
edamame22 | 0:29983394c6b6 | 708 | } |
edamame22 | 0:29983394c6b6 | 709 | |
edamame22 | 0:29983394c6b6 | 710 | buffer.append(s1); |
edamame22 | 0:29983394c6b6 | 711 | buffer.append('/'); |
edamame22 | 0:29983394c6b6 | 712 | buffer.append_int(i1); |
edamame22 | 0:29983394c6b6 | 713 | |
edamame22 | 0:29983394c6b6 | 714 | return true; |
edamame22 | 0:29983394c6b6 | 715 | } |
edamame22 | 0:29983394c6b6 | 716 | |
edamame22 | 0:29983394c6b6 | 717 | char* M2MBase::stringdup(const char* src) |
edamame22 | 0:29983394c6b6 | 718 | { |
edamame22 | 0:29983394c6b6 | 719 | assert(src != NULL); |
edamame22 | 0:29983394c6b6 | 720 | |
edamame22 | 0:29983394c6b6 | 721 | const size_t len = strlen(src) + 1; |
edamame22 | 0:29983394c6b6 | 722 | |
edamame22 | 0:29983394c6b6 | 723 | char *dest = (char*)malloc(len); |
edamame22 | 0:29983394c6b6 | 724 | |
edamame22 | 0:29983394c6b6 | 725 | if (dest) { |
edamame22 | 0:29983394c6b6 | 726 | memcpy(dest, src, len); |
edamame22 | 0:29983394c6b6 | 727 | } |
edamame22 | 0:29983394c6b6 | 728 | return dest; |
edamame22 | 0:29983394c6b6 | 729 | } |
edamame22 | 0:29983394c6b6 | 730 | |
edamame22 | 0:29983394c6b6 | 731 | void M2MBase::free_resources() |
edamame22 | 0:29983394c6b6 | 732 | { |
edamame22 | 0:29983394c6b6 | 733 | // remove the nsdl structures from the nsdlinterface's lists. |
edamame22 | 0:29983394c6b6 | 734 | if (_observation_handler) { |
edamame22 | 0:29983394c6b6 | 735 | _observation_handler->resource_to_be_deleted(this); |
edamame22 | 0:29983394c6b6 | 736 | } |
edamame22 | 0:29983394c6b6 | 737 | |
edamame22 | 0:29983394c6b6 | 738 | if (_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete) { |
edamame22 | 0:29983394c6b6 | 739 | sn_nsdl_static_resource_parameters_s *params = |
edamame22 | 0:29983394c6b6 | 740 | const_cast<sn_nsdl_static_resource_parameters_s *>(_sn_resource->dynamic_resource_params->static_resource_parameters); |
edamame22 | 0:29983394c6b6 | 741 | |
edamame22 | 0:29983394c6b6 | 742 | free(params->path); |
edamame22 | 0:29983394c6b6 | 743 | free(params->resource); |
edamame22 | 0:29983394c6b6 | 744 | free(params->resource_type_ptr); |
edamame22 | 0:29983394c6b6 | 745 | free(params->interface_description_ptr); |
edamame22 | 0:29983394c6b6 | 746 | free(params); |
edamame22 | 0:29983394c6b6 | 747 | } |
edamame22 | 0:29983394c6b6 | 748 | if (_sn_resource->dynamic_resource_params->free_on_delete) { |
edamame22 | 0:29983394c6b6 | 749 | free(_sn_resource->dynamic_resource_params); |
edamame22 | 0:29983394c6b6 | 750 | } |
edamame22 | 0:29983394c6b6 | 751 | |
edamame22 | 0:29983394c6b6 | 752 | if (_sn_resource->free_on_delete) { |
edamame22 | 0:29983394c6b6 | 753 | free(_sn_resource->name); |
edamame22 | 0:29983394c6b6 | 754 | free(_sn_resource); |
edamame22 | 0:29983394c6b6 | 755 | } |
edamame22 | 0:29983394c6b6 | 756 | } |
edamame22 | 0:29983394c6b6 | 757 | |
edamame22 | 0:29983394c6b6 | 758 | size_t M2MBase::resource_name_length() const |
edamame22 | 0:29983394c6b6 | 759 | { |
edamame22 | 0:29983394c6b6 | 760 | return strlen(_sn_resource->name); |
edamame22 | 0:29983394c6b6 | 761 | } |
edamame22 | 0:29983394c6b6 | 762 | |
edamame22 | 0:29983394c6b6 | 763 | sn_nsdl_dynamic_resource_parameters_s* M2MBase::get_nsdl_resource() |
edamame22 | 0:29983394c6b6 | 764 | { |
edamame22 | 0:29983394c6b6 | 765 | return _sn_resource->dynamic_resource_params; |
edamame22 | 0:29983394c6b6 | 766 | } |