FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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