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