Committer:
leothedragon
Date:
Sun Apr 18 15:20:23 2021 +0000
Revision:
0:25fa8795676b
DS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:25fa8795676b 1 /*
leothedragon 0:25fa8795676b 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
leothedragon 0:25fa8795676b 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:25fa8795676b 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:25fa8795676b 5 * not use this file except in compliance with the License.
leothedragon 0:25fa8795676b 6 * You may obtain a copy of the License at
leothedragon 0:25fa8795676b 7 *
leothedragon 0:25fa8795676b 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:25fa8795676b 9 *
leothedragon 0:25fa8795676b 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:25fa8795676b 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:25fa8795676b 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:25fa8795676b 13 * See the License for the specific language governing permissions and
leothedragon 0:25fa8795676b 14 * limitations under the License.
leothedragon 0:25fa8795676b 15 */
leothedragon 0:25fa8795676b 16
leothedragon 0:25fa8795676b 17 #include "mbed-client/m2mbase.h"
leothedragon 0:25fa8795676b 18 #include "mbed-client/m2mobservationhandler.h"
leothedragon 0:25fa8795676b 19 #include "mbed-client/m2mconstants.h"
leothedragon 0:25fa8795676b 20 #include "mbed-client/m2mtimer.h"
leothedragon 0:25fa8795676b 21
leothedragon 0:25fa8795676b 22 #include "mbed-client/m2mendpoint.h"
leothedragon 0:25fa8795676b 23 #include "mbed-client/m2mobject.h"
leothedragon 0:25fa8795676b 24 #include "mbed-client/m2mobjectinstance.h"
leothedragon 0:25fa8795676b 25 #include "mbed-client/m2mresource.h"
leothedragon 0:25fa8795676b 26
leothedragon 0:25fa8795676b 27 #include "include/m2mreporthandler.h"
leothedragon 0:25fa8795676b 28 #include "include/nsdlaccesshelper.h"
leothedragon 0:25fa8795676b 29 #include "include/m2mcallbackstorage.h"
leothedragon 0:25fa8795676b 30 #include "mbed-trace/mbed_trace.h"
leothedragon 0:25fa8795676b 31
leothedragon 0:25fa8795676b 32 #include "sn_nsdl_lib.h"
leothedragon 0:25fa8795676b 33 #include <assert.h>
leothedragon 0:25fa8795676b 34 #include <ctype.h>
leothedragon 0:25fa8795676b 35 #include <string.h>
leothedragon 0:25fa8795676b 36 #include <stdlib.h>
leothedragon 0:25fa8795676b 37 #include "common_functions.h"
leothedragon 0:25fa8795676b 38
leothedragon 0:25fa8795676b 39 #define TRACE_GROUP "mClt"
leothedragon 0:25fa8795676b 40
leothedragon 0:25fa8795676b 41 M2MBase::M2MBase(const String& resource_name,
leothedragon 0:25fa8795676b 42 M2MBase::Mode mode,
leothedragon 0:25fa8795676b 43 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 44 const String &resource_type,
leothedragon 0:25fa8795676b 45 #endif
leothedragon 0:25fa8795676b 46 char *path,
leothedragon 0:25fa8795676b 47 bool external_blockwise_store,
leothedragon 0:25fa8795676b 48 bool multiple_instance,
leothedragon 0:25fa8795676b 49 M2MBase::DataType type)
leothedragon 0:25fa8795676b 50 :
leothedragon 0:25fa8795676b 51 _sn_resource(NULL),
leothedragon 0:25fa8795676b 52 _report_handler(NULL)
leothedragon 0:25fa8795676b 53 {
leothedragon 0:25fa8795676b 54 // Checking the name length properly, i.e returning error is impossible from constructor without exceptions
leothedragon 0:25fa8795676b 55 assert(resource_name.length() <= MAX_ALLOWED_STRING_LENGTH);
leothedragon 0:25fa8795676b 56
leothedragon 0:25fa8795676b 57 _sn_resource = (lwm2m_parameters_s*)memory_alloc(sizeof(lwm2m_parameters_s));
leothedragon 0:25fa8795676b 58 if(_sn_resource) {
leothedragon 0:25fa8795676b 59 memset(_sn_resource, 0, sizeof(lwm2m_parameters_s));
leothedragon 0:25fa8795676b 60 _sn_resource->free_on_delete = true;
leothedragon 0:25fa8795676b 61 _sn_resource->multiple_instance = multiple_instance;
leothedragon 0:25fa8795676b 62 _sn_resource->data_type = type;
leothedragon 0:25fa8795676b 63 _sn_resource->read_write_callback_set = false;
leothedragon 0:25fa8795676b 64 _sn_resource->dynamic_resource_params =
leothedragon 0:25fa8795676b 65 (sn_nsdl_dynamic_resource_parameters_s*)memory_alloc(sizeof(sn_nsdl_dynamic_resource_parameters_s));
leothedragon 0:25fa8795676b 66 if(_sn_resource->dynamic_resource_params) {
leothedragon 0:25fa8795676b 67 memset(_sn_resource->dynamic_resource_params,
leothedragon 0:25fa8795676b 68 0, sizeof(sn_nsdl_dynamic_resource_parameters_s));
leothedragon 0:25fa8795676b 69 _sn_resource->dynamic_resource_params->static_resource_parameters =
leothedragon 0:25fa8795676b 70 (sn_nsdl_static_resource_parameters_s*)memory_alloc(sizeof(sn_nsdl_static_resource_parameters_s));
leothedragon 0:25fa8795676b 71
leothedragon 0:25fa8795676b 72 // Set callback function in case of both dynamic and static resource
leothedragon 0:25fa8795676b 73 _sn_resource->dynamic_resource_params->sn_grs_dyn_res_callback = __nsdl_c_callback;
leothedragon 0:25fa8795676b 74
leothedragon 0:25fa8795676b 75 if(_sn_resource->dynamic_resource_params->static_resource_parameters) {
leothedragon 0:25fa8795676b 76 // Cast const away to able to compile using MEMORY_OPTIMIZED_API flag
leothedragon 0:25fa8795676b 77 sn_nsdl_static_resource_parameters_s *params =
leothedragon 0:25fa8795676b 78 const_cast<sn_nsdl_static_resource_parameters_s *>(_sn_resource->dynamic_resource_params->static_resource_parameters);
leothedragon 0:25fa8795676b 79 memset(params, 0, sizeof(sn_nsdl_static_resource_parameters_s));
leothedragon 0:25fa8795676b 80 const size_t len = strlen(resource_type.c_str());
leothedragon 0:25fa8795676b 81 params->free_on_delete = true;
leothedragon 0:25fa8795676b 82 if (len > 0) {
leothedragon 0:25fa8795676b 83 #ifndef RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 84 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 85 params->resource_type_ptr = (char*)alloc_string_copy((uint8_t*) resource_type.c_str(), len);
leothedragon 0:25fa8795676b 86 #endif
leothedragon 0:25fa8795676b 87 #else
leothedragon 0:25fa8795676b 88 sn_nsdl_attribute_item_s item;
leothedragon 0:25fa8795676b 89 item.attribute_name = ATTR_RESOURCE_TYPE;
leothedragon 0:25fa8795676b 90 item.value = (char*)alloc_string_copy((uint8_t*) resource_type.c_str(), len);
leothedragon 0:25fa8795676b 91 sn_nsdl_set_resource_attribute(_sn_resource->dynamic_resource_params->static_resource_parameters, &item);
leothedragon 0:25fa8795676b 92 #endif
leothedragon 0:25fa8795676b 93 }
leothedragon 0:25fa8795676b 94 params->path = path;
leothedragon 0:25fa8795676b 95 params->mode = (unsigned)mode;
leothedragon 0:25fa8795676b 96 params->external_memory_block = external_blockwise_store;
leothedragon 0:25fa8795676b 97 _sn_resource->dynamic_resource_params->static_resource_parameters = params;
leothedragon 0:25fa8795676b 98 }
leothedragon 0:25fa8795676b 99 }
leothedragon 0:25fa8795676b 100
leothedragon 0:25fa8795676b 101 if((!resource_name.empty())) {
leothedragon 0:25fa8795676b 102 _sn_resource->identifier_int_type = false;
leothedragon 0:25fa8795676b 103 _sn_resource->identifier.name = stringdup((char*)resource_name.c_str());
leothedragon 0:25fa8795676b 104 } else {
leothedragon 0:25fa8795676b 105 tr_debug("M2MBase::M2Mbase resource name is EMPTY ===========");
leothedragon 0:25fa8795676b 106 _sn_resource->identifier_int_type = true;
leothedragon 0:25fa8795676b 107 _sn_resource->identifier.instance_id = 0;
leothedragon 0:25fa8795676b 108 }
leothedragon 0:25fa8795676b 109 _sn_resource->dynamic_resource_params->publish_uri = true;
leothedragon 0:25fa8795676b 110 _sn_resource->dynamic_resource_params->free_on_delete = true;
leothedragon 0:25fa8795676b 111 _sn_resource->dynamic_resource_params->auto_observable = false;
leothedragon 0:25fa8795676b 112 _sn_resource->dynamic_resource_params->publish_value = false;
leothedragon 0:25fa8795676b 113 }
leothedragon 0:25fa8795676b 114 }
leothedragon 0:25fa8795676b 115
leothedragon 0:25fa8795676b 116 M2MBase::M2MBase(const lwm2m_parameters_s *s):
leothedragon 0:25fa8795676b 117 _sn_resource((lwm2m_parameters_s*) s),
leothedragon 0:25fa8795676b 118 _report_handler(NULL)
leothedragon 0:25fa8795676b 119 {
leothedragon 0:25fa8795676b 120 tr_debug("M2MBase::M2MBase(const lwm2m_parameters_s *s)");
leothedragon 0:25fa8795676b 121 // Set callback function in case of both dynamic and static resource
leothedragon 0:25fa8795676b 122 _sn_resource->dynamic_resource_params->sn_grs_dyn_res_callback = __nsdl_c_callback;
leothedragon 0:25fa8795676b 123 }
leothedragon 0:25fa8795676b 124
leothedragon 0:25fa8795676b 125 M2MBase::~M2MBase()
leothedragon 0:25fa8795676b 126 {
leothedragon 0:25fa8795676b 127 tr_debug("M2MBase::~M2MBase() %p", this);
leothedragon 0:25fa8795676b 128 delete _report_handler;
leothedragon 0:25fa8795676b 129
leothedragon 0:25fa8795676b 130 value_updated_callback* callback = (value_updated_callback*)M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MBaseValueUpdatedCallback);
leothedragon 0:25fa8795676b 131 delete callback;
leothedragon 0:25fa8795676b 132
leothedragon 0:25fa8795676b 133 M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MBaseValueUpdatedCallback2);
leothedragon 0:25fa8795676b 134 M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MBaseNotificationDeliveryStatusCallback);
leothedragon 0:25fa8795676b 135 #ifdef ENABLE_ASYNC_REST_RESPONSE
leothedragon 0:25fa8795676b 136 M2MCallbackStorage::remove_callback(*this,M2MCallbackAssociation::M2MBaseAsyncCoapRequestCallback);
leothedragon 0:25fa8795676b 137 #endif
leothedragon 0:25fa8795676b 138 }
leothedragon 0:25fa8795676b 139
leothedragon 0:25fa8795676b 140 char* M2MBase::create_path_base(const M2MBase &parent, const char *name)
leothedragon 0:25fa8795676b 141 {
leothedragon 0:25fa8795676b 142 char * result = NULL;
leothedragon 0:25fa8795676b 143 // Expectation is that every element can be MAX_NAME_SZE, + 4 /'s + \0
leothedragon 0:25fa8795676b 144 StringBuffer<(MAX_NAME_SIZE * 4 + (4 + 1))> path;
leothedragon 0:25fa8795676b 145 path.append(parent.uri_path());
leothedragon 0:25fa8795676b 146 path.append('/');
leothedragon 0:25fa8795676b 147 path.append(name);
leothedragon 0:25fa8795676b 148 result = stringdup(path.c_str());
leothedragon 0:25fa8795676b 149
leothedragon 0:25fa8795676b 150 return result;
leothedragon 0:25fa8795676b 151 }
leothedragon 0:25fa8795676b 152
leothedragon 0:25fa8795676b 153 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 154 char* M2MBase::create_path(const M2MEndpoint &parent, const char *name)
leothedragon 0:25fa8795676b 155 {
leothedragon 0:25fa8795676b 156 return create_path_base(parent, name);
leothedragon 0:25fa8795676b 157 }
leothedragon 0:25fa8795676b 158 #endif
leothedragon 0:25fa8795676b 159
leothedragon 0:25fa8795676b 160 char* M2MBase::create_path(const M2MObject &parent, uint16_t object_instance)
leothedragon 0:25fa8795676b 161 {
leothedragon 0:25fa8795676b 162 StringBuffer<6> obj_inst_id;
leothedragon 0:25fa8795676b 163 obj_inst_id.append_int(object_instance);
leothedragon 0:25fa8795676b 164
leothedragon 0:25fa8795676b 165 return create_path_base(parent, obj_inst_id.c_str());
leothedragon 0:25fa8795676b 166 }
leothedragon 0:25fa8795676b 167
leothedragon 0:25fa8795676b 168 char* M2MBase::create_path(const M2MObject &parent, const char *name)
leothedragon 0:25fa8795676b 169 {
leothedragon 0:25fa8795676b 170 return create_path_base(parent, name);
leothedragon 0:25fa8795676b 171 }
leothedragon 0:25fa8795676b 172
leothedragon 0:25fa8795676b 173 char* M2MBase::create_path(const M2MResource &parent, uint16_t resource_instance)
leothedragon 0:25fa8795676b 174 {
leothedragon 0:25fa8795676b 175 StringBuffer<6> res_inst;
leothedragon 0:25fa8795676b 176 res_inst.append_int(resource_instance);
leothedragon 0:25fa8795676b 177
leothedragon 0:25fa8795676b 178 return create_path_base(parent, res_inst.c_str());
leothedragon 0:25fa8795676b 179 }
leothedragon 0:25fa8795676b 180
leothedragon 0:25fa8795676b 181 char* M2MBase::create_path(const M2MResource &parent, const char *name)
leothedragon 0:25fa8795676b 182 {
leothedragon 0:25fa8795676b 183 return create_path_base(parent, name);
leothedragon 0:25fa8795676b 184 }
leothedragon 0:25fa8795676b 185
leothedragon 0:25fa8795676b 186 char* M2MBase::create_path(const M2MObjectInstance &parent, const char *name)
leothedragon 0:25fa8795676b 187 {
leothedragon 0:25fa8795676b 188 return create_path_base(parent, name);
leothedragon 0:25fa8795676b 189 }
leothedragon 0:25fa8795676b 190
leothedragon 0:25fa8795676b 191 void M2MBase::set_operation(M2MBase::Operation opr)
leothedragon 0:25fa8795676b 192 {
leothedragon 0:25fa8795676b 193 // If the mode is Static, there is only GET_ALLOWED supported.
leothedragon 0:25fa8795676b 194 if(M2MBase::Static == mode()) {
leothedragon 0:25fa8795676b 195 _sn_resource->dynamic_resource_params->access = M2MBase::GET_ALLOWED;
leothedragon 0:25fa8795676b 196 } else {
leothedragon 0:25fa8795676b 197 _sn_resource->dynamic_resource_params->access = opr;
leothedragon 0:25fa8795676b 198 }
leothedragon 0:25fa8795676b 199 }
leothedragon 0:25fa8795676b 200
leothedragon 0:25fa8795676b 201 #ifndef RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 202 #ifndef MEMORY_OPTIMIZED_API
leothedragon 0:25fa8795676b 203 #ifndef DISABLE_INTERFACE_DESCRIPTION
leothedragon 0:25fa8795676b 204 void M2MBase::set_interface_description(const char *desc)
leothedragon 0:25fa8795676b 205 {
leothedragon 0:25fa8795676b 206 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 207 free(_sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr);
leothedragon 0:25fa8795676b 208 _sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr = NULL;
leothedragon 0:25fa8795676b 209 const size_t len = strlen(desc);
leothedragon 0:25fa8795676b 210 if (len > 0 ) {
leothedragon 0:25fa8795676b 211 _sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr =
leothedragon 0:25fa8795676b 212 (char*)alloc_string_copy((uint8_t*) desc, len);
leothedragon 0:25fa8795676b 213 }
leothedragon 0:25fa8795676b 214 set_changed();
leothedragon 0:25fa8795676b 215 }
leothedragon 0:25fa8795676b 216
leothedragon 0:25fa8795676b 217 void M2MBase::set_interface_description(const String &desc)
leothedragon 0:25fa8795676b 218 {
leothedragon 0:25fa8795676b 219 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 220 set_interface_description(desc.c_str());
leothedragon 0:25fa8795676b 221 }
leothedragon 0:25fa8795676b 222 #endif // DISABLE_INTERFACE_DESCRIPTION
leothedragon 0:25fa8795676b 223
leothedragon 0:25fa8795676b 224 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 225 void M2MBase::set_resource_type(const String &res_type)
leothedragon 0:25fa8795676b 226 {
leothedragon 0:25fa8795676b 227 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 228 set_resource_type(res_type.c_str());
leothedragon 0:25fa8795676b 229 }
leothedragon 0:25fa8795676b 230
leothedragon 0:25fa8795676b 231 void M2MBase::set_resource_type(const char *res_type)
leothedragon 0:25fa8795676b 232 {
leothedragon 0:25fa8795676b 233 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 234 free(_sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr);
leothedragon 0:25fa8795676b 235 _sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr = NULL;
leothedragon 0:25fa8795676b 236 const size_t len = strlen(res_type);
leothedragon 0:25fa8795676b 237 if (len > 0) {
leothedragon 0:25fa8795676b 238 _sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr = (char*)
leothedragon 0:25fa8795676b 239 alloc_string_copy((uint8_t*) res_type, len);
leothedragon 0:25fa8795676b 240 }
leothedragon 0:25fa8795676b 241 set_changed();
leothedragon 0:25fa8795676b 242 }
leothedragon 0:25fa8795676b 243 #endif // DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 244 #endif //MEMORY_OPTIMIZED_API
leothedragon 0:25fa8795676b 245 #else // RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 246 void M2MBase::set_interface_description(const char *desc)
leothedragon 0:25fa8795676b 247 {
leothedragon 0:25fa8795676b 248 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 249 const size_t len = strlen(desc);
leothedragon 0:25fa8795676b 250 if (len > 0 ) {
leothedragon 0:25fa8795676b 251 sn_nsdl_attribute_item_s item;
leothedragon 0:25fa8795676b 252 item.attribute_name = ATTR_INTERFACE_DESCRIPTION;
leothedragon 0:25fa8795676b 253 item.value = (char*)alloc_string_copy((uint8_t*) desc, len);
leothedragon 0:25fa8795676b 254 sn_nsdl_set_resource_attribute(_sn_resource->dynamic_resource_params->static_resource_parameters, &item);
leothedragon 0:25fa8795676b 255 set_changed();
leothedragon 0:25fa8795676b 256 }
leothedragon 0:25fa8795676b 257 }
leothedragon 0:25fa8795676b 258
leothedragon 0:25fa8795676b 259 void M2MBase::set_interface_description(const String &desc)
leothedragon 0:25fa8795676b 260 {
leothedragon 0:25fa8795676b 261 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 262 set_interface_description(desc.c_str());
leothedragon 0:25fa8795676b 263 }
leothedragon 0:25fa8795676b 264
leothedragon 0:25fa8795676b 265 void M2MBase::set_resource_type(const String &res_type)
leothedragon 0:25fa8795676b 266 {
leothedragon 0:25fa8795676b 267 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 268 set_resource_type(res_type.c_str());
leothedragon 0:25fa8795676b 269 }
leothedragon 0:25fa8795676b 270
leothedragon 0:25fa8795676b 271 void M2MBase::set_resource_type(const char *res_type)
leothedragon 0:25fa8795676b 272 {
leothedragon 0:25fa8795676b 273 assert(_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete);
leothedragon 0:25fa8795676b 274 const size_t len = strlen(res_type);
leothedragon 0:25fa8795676b 275 if (len > 0) {
leothedragon 0:25fa8795676b 276 sn_nsdl_attribute_item_s item;
leothedragon 0:25fa8795676b 277 item.attribute_name = ATTR_RESOURCE_TYPE;
leothedragon 0:25fa8795676b 278 item.value = (char*)alloc_string_copy((uint8_t*) res_type, len);
leothedragon 0:25fa8795676b 279 sn_nsdl_set_resource_attribute(_sn_resource->dynamic_resource_params->static_resource_parameters, &item);
leothedragon 0:25fa8795676b 280 set_changed();
leothedragon 0:25fa8795676b 281 }
leothedragon 0:25fa8795676b 282 }
leothedragon 0:25fa8795676b 283 #endif // RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 284
leothedragon 0:25fa8795676b 285 void M2MBase::set_coap_content_type(const uint16_t con_type)
leothedragon 0:25fa8795676b 286 {
leothedragon 0:25fa8795676b 287 _sn_resource->dynamic_resource_params->coap_content_type = con_type;
leothedragon 0:25fa8795676b 288 set_changed();
leothedragon 0:25fa8795676b 289 }
leothedragon 0:25fa8795676b 290
leothedragon 0:25fa8795676b 291 void M2MBase::set_observable(bool observable)
leothedragon 0:25fa8795676b 292 {
leothedragon 0:25fa8795676b 293 _sn_resource->dynamic_resource_params->observable = observable;
leothedragon 0:25fa8795676b 294 set_changed();
leothedragon 0:25fa8795676b 295 }
leothedragon 0:25fa8795676b 296
leothedragon 0:25fa8795676b 297 void M2MBase::set_auto_observable(bool auto_observable)
leothedragon 0:25fa8795676b 298 {
leothedragon 0:25fa8795676b 299 _sn_resource->dynamic_resource_params->auto_observable = auto_observable;
leothedragon 0:25fa8795676b 300 set_changed();
leothedragon 0:25fa8795676b 301 }
leothedragon 0:25fa8795676b 302
leothedragon 0:25fa8795676b 303 void M2MBase::add_observation_level(M2MBase::Observation obs_level)
leothedragon 0:25fa8795676b 304 {
leothedragon 0:25fa8795676b 305 if(_report_handler) {
leothedragon 0:25fa8795676b 306 _report_handler->add_observation_level(obs_level);
leothedragon 0:25fa8795676b 307 }
leothedragon 0:25fa8795676b 308 }
leothedragon 0:25fa8795676b 309
leothedragon 0:25fa8795676b 310 void M2MBase::remove_observation_level(M2MBase::Observation obs_level)
leothedragon 0:25fa8795676b 311 {
leothedragon 0:25fa8795676b 312 if(_report_handler) {
leothedragon 0:25fa8795676b 313 _report_handler->remove_observation_level(obs_level);
leothedragon 0:25fa8795676b 314 }
leothedragon 0:25fa8795676b 315 }
leothedragon 0:25fa8795676b 316
leothedragon 0:25fa8795676b 317
leothedragon 0:25fa8795676b 318 void M2MBase::set_under_observation(bool observed,
leothedragon 0:25fa8795676b 319 M2MObservationHandler *handler)
leothedragon 0:25fa8795676b 320 {
leothedragon 0:25fa8795676b 321 tr_debug("M2MBase::set_under_observation - observed: %d", observed);
leothedragon 0:25fa8795676b 322 tr_debug("M2MBase::set_under_observation - base_type: %d", base_type());
leothedragon 0:25fa8795676b 323 if(_report_handler) {
leothedragon 0:25fa8795676b 324 _report_handler->set_under_observation(observed);
leothedragon 0:25fa8795676b 325 }
leothedragon 0:25fa8795676b 326
leothedragon 0:25fa8795676b 327 set_observation_handler(handler);
leothedragon 0:25fa8795676b 328
leothedragon 0:25fa8795676b 329 if (handler) {
leothedragon 0:25fa8795676b 330 if (base_type() != M2MBase::ResourceInstance) {
leothedragon 0:25fa8795676b 331 // Create report handler only if it does not exist and one wants observation
leothedragon 0:25fa8795676b 332 // This saves 76 bytes of memory on most usual case.
leothedragon 0:25fa8795676b 333 if (observed) {
leothedragon 0:25fa8795676b 334 if(!_report_handler) {
leothedragon 0:25fa8795676b 335 _report_handler = new M2MReportHandler(*this, _sn_resource->data_type);
leothedragon 0:25fa8795676b 336 }
leothedragon 0:25fa8795676b 337 }
leothedragon 0:25fa8795676b 338 if (_report_handler) {
leothedragon 0:25fa8795676b 339 _report_handler->set_under_observation(observed);
leothedragon 0:25fa8795676b 340 }
leothedragon 0:25fa8795676b 341 }
leothedragon 0:25fa8795676b 342 } else {
leothedragon 0:25fa8795676b 343 delete _report_handler;
leothedragon 0:25fa8795676b 344 _report_handler = NULL;
leothedragon 0:25fa8795676b 345 }
leothedragon 0:25fa8795676b 346 }
leothedragon 0:25fa8795676b 347
leothedragon 0:25fa8795676b 348 void M2MBase::set_observation_token(const uint8_t *token, const uint8_t length)
leothedragon 0:25fa8795676b 349 {
leothedragon 0:25fa8795676b 350 if (_report_handler) {
leothedragon 0:25fa8795676b 351 _report_handler->set_observation_token(token, length);
leothedragon 0:25fa8795676b 352 // This relates to sn_nsdl_auto_obs_token_callback in sn_nsdl.c
leothedragon 0:25fa8795676b 353 set_changed();
leothedragon 0:25fa8795676b 354 }
leothedragon 0:25fa8795676b 355 }
leothedragon 0:25fa8795676b 356
leothedragon 0:25fa8795676b 357 void M2MBase::set_instance_id(const uint16_t inst_id)
leothedragon 0:25fa8795676b 358 {
leothedragon 0:25fa8795676b 359 _sn_resource->identifier_int_type = true;
leothedragon 0:25fa8795676b 360 _sn_resource->identifier.instance_id = inst_id;
leothedragon 0:25fa8795676b 361 }
leothedragon 0:25fa8795676b 362
leothedragon 0:25fa8795676b 363 void M2MBase::set_max_age(const uint32_t max_age)
leothedragon 0:25fa8795676b 364 {
leothedragon 0:25fa8795676b 365 _sn_resource->max_age = max_age;
leothedragon 0:25fa8795676b 366 }
leothedragon 0:25fa8795676b 367
leothedragon 0:25fa8795676b 368 M2MBase::BaseType M2MBase::base_type() const
leothedragon 0:25fa8795676b 369 {
leothedragon 0:25fa8795676b 370 return (M2MBase::BaseType)_sn_resource->base_type;
leothedragon 0:25fa8795676b 371 }
leothedragon 0:25fa8795676b 372
leothedragon 0:25fa8795676b 373 M2MBase::Operation M2MBase::operation() const
leothedragon 0:25fa8795676b 374 {
leothedragon 0:25fa8795676b 375 return (M2MBase::Operation)_sn_resource->dynamic_resource_params->access;
leothedragon 0:25fa8795676b 376 }
leothedragon 0:25fa8795676b 377
leothedragon 0:25fa8795676b 378 const char* M2MBase::name() const
leothedragon 0:25fa8795676b 379 {
leothedragon 0:25fa8795676b 380 assert(_sn_resource->identifier_int_type == false);
leothedragon 0:25fa8795676b 381 return _sn_resource->identifier.name;
leothedragon 0:25fa8795676b 382 }
leothedragon 0:25fa8795676b 383
leothedragon 0:25fa8795676b 384 int32_t M2MBase::name_id() const
leothedragon 0:25fa8795676b 385 {
leothedragon 0:25fa8795676b 386 int32_t name_id = -1;
leothedragon 0:25fa8795676b 387 assert(_sn_resource->identifier_int_type == false);
leothedragon 0:25fa8795676b 388 if(is_integer(_sn_resource->identifier.name) && strlen(_sn_resource->identifier.name) <= MAX_ALLOWED_STRING_LENGTH) {
leothedragon 0:25fa8795676b 389 name_id = strtoul(_sn_resource->identifier.name, NULL, 10);
leothedragon 0:25fa8795676b 390 if(name_id > 65535){
leothedragon 0:25fa8795676b 391 name_id = -1;
leothedragon 0:25fa8795676b 392 }
leothedragon 0:25fa8795676b 393 }
leothedragon 0:25fa8795676b 394 return name_id;
leothedragon 0:25fa8795676b 395 }
leothedragon 0:25fa8795676b 396
leothedragon 0:25fa8795676b 397 uint16_t M2MBase::instance_id() const
leothedragon 0:25fa8795676b 398 {
leothedragon 0:25fa8795676b 399 assert(_sn_resource->identifier_int_type == true);
leothedragon 0:25fa8795676b 400 return _sn_resource->identifier.instance_id;
leothedragon 0:25fa8795676b 401 }
leothedragon 0:25fa8795676b 402
leothedragon 0:25fa8795676b 403 #ifndef RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 404 #ifndef DISABLE_INTERFACE_DESCRIPTION
leothedragon 0:25fa8795676b 405 #ifndef MEMORY_OPTIMIZED_API
leothedragon 0:25fa8795676b 406 const char* M2MBase::interface_description() const
leothedragon 0:25fa8795676b 407 {
leothedragon 0:25fa8795676b 408 return (reinterpret_cast<char*>(
leothedragon 0:25fa8795676b 409 _sn_resource->dynamic_resource_params->static_resource_parameters->interface_description_ptr));
leothedragon 0:25fa8795676b 410 }
leothedragon 0:25fa8795676b 411 #endif
leothedragon 0:25fa8795676b 412 #endif
leothedragon 0:25fa8795676b 413
leothedragon 0:25fa8795676b 414 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 415 #ifndef MEMORY_OPTIMIZED_API
leothedragon 0:25fa8795676b 416 const char* M2MBase::resource_type() const
leothedragon 0:25fa8795676b 417 {
leothedragon 0:25fa8795676b 418 return (reinterpret_cast<char*>(
leothedragon 0:25fa8795676b 419 _sn_resource->dynamic_resource_params->static_resource_parameters->resource_type_ptr));
leothedragon 0:25fa8795676b 420 }
leothedragon 0:25fa8795676b 421 #endif
leothedragon 0:25fa8795676b 422 #endif
leothedragon 0:25fa8795676b 423 #else // RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 424 #ifndef DISABLE_INTERFACE_DESCRIPTION
leothedragon 0:25fa8795676b 425 const char* M2MBase::interface_description() const
leothedragon 0:25fa8795676b 426 {
leothedragon 0:25fa8795676b 427 return sn_nsdl_get_resource_attribute(_sn_resource->dynamic_resource_params->static_resource_parameters, ATTR_INTERFACE_DESCRIPTION);
leothedragon 0:25fa8795676b 428 }
leothedragon 0:25fa8795676b 429 #endif
leothedragon 0:25fa8795676b 430
leothedragon 0:25fa8795676b 431 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 432 const char* M2MBase::resource_type() const
leothedragon 0:25fa8795676b 433 {
leothedragon 0:25fa8795676b 434 return sn_nsdl_get_resource_attribute(_sn_resource->dynamic_resource_params->static_resource_parameters, ATTR_RESOURCE_TYPE);
leothedragon 0:25fa8795676b 435 }
leothedragon 0:25fa8795676b 436 #endif
leothedragon 0:25fa8795676b 437 #endif // RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 438 const char* M2MBase::uri_path() const
leothedragon 0:25fa8795676b 439 {
leothedragon 0:25fa8795676b 440 return (reinterpret_cast<char*>(
leothedragon 0:25fa8795676b 441 _sn_resource->dynamic_resource_params->static_resource_parameters->path));
leothedragon 0:25fa8795676b 442 }
leothedragon 0:25fa8795676b 443
leothedragon 0:25fa8795676b 444 uint16_t M2MBase::coap_content_type() const
leothedragon 0:25fa8795676b 445 {
leothedragon 0:25fa8795676b 446 return _sn_resource->dynamic_resource_params->coap_content_type;
leothedragon 0:25fa8795676b 447 }
leothedragon 0:25fa8795676b 448
leothedragon 0:25fa8795676b 449 bool M2MBase::is_observable() const
leothedragon 0:25fa8795676b 450 {
leothedragon 0:25fa8795676b 451 return _sn_resource->dynamic_resource_params->observable;
leothedragon 0:25fa8795676b 452 }
leothedragon 0:25fa8795676b 453
leothedragon 0:25fa8795676b 454 bool M2MBase::is_auto_observable() const
leothedragon 0:25fa8795676b 455 {
leothedragon 0:25fa8795676b 456 return _sn_resource->dynamic_resource_params->auto_observable;
leothedragon 0:25fa8795676b 457 }
leothedragon 0:25fa8795676b 458
leothedragon 0:25fa8795676b 459 M2MBase::Observation M2MBase::observation_level() const
leothedragon 0:25fa8795676b 460 {
leothedragon 0:25fa8795676b 461 M2MBase::Observation obs_level = M2MBase::None;
leothedragon 0:25fa8795676b 462 if(_report_handler) {
leothedragon 0:25fa8795676b 463 obs_level = _report_handler->observation_level();
leothedragon 0:25fa8795676b 464 }
leothedragon 0:25fa8795676b 465 return obs_level;
leothedragon 0:25fa8795676b 466 }
leothedragon 0:25fa8795676b 467
leothedragon 0:25fa8795676b 468 void M2MBase::get_observation_token(uint8_t *token, uint8_t &token_length) const
leothedragon 0:25fa8795676b 469 {
leothedragon 0:25fa8795676b 470 if(_report_handler) {
leothedragon 0:25fa8795676b 471 _report_handler->get_observation_token(token, token_length);
leothedragon 0:25fa8795676b 472 }
leothedragon 0:25fa8795676b 473 }
leothedragon 0:25fa8795676b 474
leothedragon 0:25fa8795676b 475 M2MBase::Mode M2MBase::mode() const
leothedragon 0:25fa8795676b 476 {
leothedragon 0:25fa8795676b 477 return (M2MBase::Mode)_sn_resource->dynamic_resource_params->static_resource_parameters->mode;
leothedragon 0:25fa8795676b 478 }
leothedragon 0:25fa8795676b 479
leothedragon 0:25fa8795676b 480 uint16_t M2MBase::observation_number() const
leothedragon 0:25fa8795676b 481 {
leothedragon 0:25fa8795676b 482 uint16_t obs_number = 0;
leothedragon 0:25fa8795676b 483 if(_report_handler) {
leothedragon 0:25fa8795676b 484 obs_number = _report_handler->observation_number();
leothedragon 0:25fa8795676b 485 }
leothedragon 0:25fa8795676b 486 return obs_number;
leothedragon 0:25fa8795676b 487 }
leothedragon 0:25fa8795676b 488
leothedragon 0:25fa8795676b 489 uint32_t M2MBase::max_age() const
leothedragon 0:25fa8795676b 490 {
leothedragon 0:25fa8795676b 491 return _sn_resource->max_age;
leothedragon 0:25fa8795676b 492 }
leothedragon 0:25fa8795676b 493
leothedragon 0:25fa8795676b 494 bool M2MBase::handle_observation_attribute(const char *query)
leothedragon 0:25fa8795676b 495 {
leothedragon 0:25fa8795676b 496 tr_debug("M2MBase::handle_observation_attribute - under observation(%d)", is_under_observation());
leothedragon 0:25fa8795676b 497 bool success = false;
leothedragon 0:25fa8795676b 498 // Create handler if not already exists. Client must able to parse write attributes even when
leothedragon 0:25fa8795676b 499 // observation is not yet set
leothedragon 0:25fa8795676b 500 if (!_report_handler) {
leothedragon 0:25fa8795676b 501 _report_handler = new M2MReportHandler(*this, _sn_resource->data_type);
leothedragon 0:25fa8795676b 502 }
leothedragon 0:25fa8795676b 503
leothedragon 0:25fa8795676b 504 success = _report_handler->parse_notification_attribute(query,base_type());
leothedragon 0:25fa8795676b 505 if (success) {
leothedragon 0:25fa8795676b 506 if (is_under_observation()) {
leothedragon 0:25fa8795676b 507 _report_handler->set_under_observation(true);
leothedragon 0:25fa8795676b 508 }
leothedragon 0:25fa8795676b 509 } else {
leothedragon 0:25fa8795676b 510 _report_handler->set_default_values();
leothedragon 0:25fa8795676b 511 }
leothedragon 0:25fa8795676b 512 return success;
leothedragon 0:25fa8795676b 513 }
leothedragon 0:25fa8795676b 514
leothedragon 0:25fa8795676b 515 bool M2MBase::observation_to_be_sent(const m2m::Vector<uint16_t> &changed_instance_ids,
leothedragon 0:25fa8795676b 516 uint16_t obs_number,
leothedragon 0:25fa8795676b 517 bool send_object)
leothedragon 0:25fa8795676b 518 {
leothedragon 0:25fa8795676b 519 //TODO: Move this to M2MResourceInstance
leothedragon 0:25fa8795676b 520 M2MObservationHandler *obs_handler = observation_handler();
leothedragon 0:25fa8795676b 521 if (obs_handler) {
leothedragon 0:25fa8795676b 522 return obs_handler->observation_to_be_sent(this,
leothedragon 0:25fa8795676b 523 obs_number,
leothedragon 0:25fa8795676b 524 changed_instance_ids,
leothedragon 0:25fa8795676b 525 send_object);
leothedragon 0:25fa8795676b 526 }
leothedragon 0:25fa8795676b 527 return false;
leothedragon 0:25fa8795676b 528 }
leothedragon 0:25fa8795676b 529
leothedragon 0:25fa8795676b 530 void M2MBase::set_base_type(M2MBase::BaseType type)
leothedragon 0:25fa8795676b 531 {
leothedragon 0:25fa8795676b 532 assert(_sn_resource->free_on_delete);
leothedragon 0:25fa8795676b 533 _sn_resource->base_type = type;
leothedragon 0:25fa8795676b 534 }
leothedragon 0:25fa8795676b 535
leothedragon 0:25fa8795676b 536 sn_coap_hdr_s* M2MBase::handle_get_request(nsdl_s */*nsdl*/,
leothedragon 0:25fa8795676b 537 sn_coap_hdr_s */*received_coap_header*/,
leothedragon 0:25fa8795676b 538 M2MObservationHandler */*observation_handler*/)
leothedragon 0:25fa8795676b 539 {
leothedragon 0:25fa8795676b 540 //Handled in M2MResource, M2MObjectInstance and M2MObject classes
leothedragon 0:25fa8795676b 541 return NULL;
leothedragon 0:25fa8795676b 542 }
leothedragon 0:25fa8795676b 543
leothedragon 0:25fa8795676b 544 sn_coap_hdr_s* M2MBase::handle_put_request(nsdl_s */*nsdl*/,
leothedragon 0:25fa8795676b 545 sn_coap_hdr_s */*received_coap_header*/,
leothedragon 0:25fa8795676b 546 M2MObservationHandler */*observation_handler*/,
leothedragon 0:25fa8795676b 547 bool &)
leothedragon 0:25fa8795676b 548 {
leothedragon 0:25fa8795676b 549 //Handled in M2MResource, M2MObjectInstance and M2MObject classes
leothedragon 0:25fa8795676b 550 return NULL;
leothedragon 0:25fa8795676b 551 }
leothedragon 0:25fa8795676b 552
leothedragon 0:25fa8795676b 553 sn_coap_hdr_s* M2MBase::handle_post_request(nsdl_s */*nsdl*/,
leothedragon 0:25fa8795676b 554 sn_coap_hdr_s */*received_coap_header*/,
leothedragon 0:25fa8795676b 555 M2MObservationHandler */*observation_handler*/,
leothedragon 0:25fa8795676b 556 bool &,
leothedragon 0:25fa8795676b 557 sn_nsdl_addr_s *)
leothedragon 0:25fa8795676b 558 {
leothedragon 0:25fa8795676b 559 //Handled in M2MResource, M2MObjectInstance and M2MObject classes
leothedragon 0:25fa8795676b 560 return NULL;
leothedragon 0:25fa8795676b 561 }
leothedragon 0:25fa8795676b 562
leothedragon 0:25fa8795676b 563 void *M2MBase::memory_alloc(uint32_t size)
leothedragon 0:25fa8795676b 564 {
leothedragon 0:25fa8795676b 565 if(size)
leothedragon 0:25fa8795676b 566 return malloc(size);
leothedragon 0:25fa8795676b 567 else
leothedragon 0:25fa8795676b 568 return 0;
leothedragon 0:25fa8795676b 569 }
leothedragon 0:25fa8795676b 570
leothedragon 0:25fa8795676b 571 void M2MBase::memory_free(void *ptr)
leothedragon 0:25fa8795676b 572 {
leothedragon 0:25fa8795676b 573 free(ptr);
leothedragon 0:25fa8795676b 574 }
leothedragon 0:25fa8795676b 575
leothedragon 0:25fa8795676b 576 char* M2MBase::alloc_string_copy(const char* source)
leothedragon 0:25fa8795676b 577 {
leothedragon 0:25fa8795676b 578 assert(source != NULL);
leothedragon 0:25fa8795676b 579
leothedragon 0:25fa8795676b 580 // Note: the armcc's libc does not have strdup, so we need to implement it here
leothedragon 0:25fa8795676b 581 const size_t len = strlen(source);
leothedragon 0:25fa8795676b 582
leothedragon 0:25fa8795676b 583 return (char*)alloc_string_copy((uint8_t*)source, len);
leothedragon 0:25fa8795676b 584 }
leothedragon 0:25fa8795676b 585
leothedragon 0:25fa8795676b 586 uint8_t* M2MBase::alloc_string_copy(const uint8_t* source, uint32_t size)
leothedragon 0:25fa8795676b 587 {
leothedragon 0:25fa8795676b 588 assert(source != NULL);
leothedragon 0:25fa8795676b 589
leothedragon 0:25fa8795676b 590 uint8_t* result = (uint8_t*)memory_alloc(size + 1);
leothedragon 0:25fa8795676b 591 if (result) {
leothedragon 0:25fa8795676b 592 memcpy(result, source, size);
leothedragon 0:25fa8795676b 593 result[size] = '\0';
leothedragon 0:25fa8795676b 594 }
leothedragon 0:25fa8795676b 595 return result;
leothedragon 0:25fa8795676b 596 }
leothedragon 0:25fa8795676b 597
leothedragon 0:25fa8795676b 598 uint8_t* M2MBase::alloc_copy(const uint8_t* source, uint32_t size)
leothedragon 0:25fa8795676b 599 {
leothedragon 0:25fa8795676b 600 assert(source != NULL);
leothedragon 0:25fa8795676b 601
leothedragon 0:25fa8795676b 602 uint8_t* result = (uint8_t*)memory_alloc(size);
leothedragon 0:25fa8795676b 603 if (result) {
leothedragon 0:25fa8795676b 604 memcpy(result, source, size);
leothedragon 0:25fa8795676b 605 }
leothedragon 0:25fa8795676b 606 return result;
leothedragon 0:25fa8795676b 607 }
leothedragon 0:25fa8795676b 608
leothedragon 0:25fa8795676b 609 bool M2MBase::validate_string_length(const String &string, size_t min_length, size_t max_length)
leothedragon 0:25fa8795676b 610 {
leothedragon 0:25fa8795676b 611 bool valid = false;
leothedragon 0:25fa8795676b 612
leothedragon 0:25fa8795676b 613 const size_t len = string.length();
leothedragon 0:25fa8795676b 614 if ((len >= min_length) && (len <= max_length)) {
leothedragon 0:25fa8795676b 615 valid = true;
leothedragon 0:25fa8795676b 616 }
leothedragon 0:25fa8795676b 617
leothedragon 0:25fa8795676b 618 return valid;
leothedragon 0:25fa8795676b 619 }
leothedragon 0:25fa8795676b 620
leothedragon 0:25fa8795676b 621 bool M2MBase::validate_string_length(const char* string, size_t min_length, size_t max_length)
leothedragon 0:25fa8795676b 622 {
leothedragon 0:25fa8795676b 623 bool valid = false;
leothedragon 0:25fa8795676b 624
leothedragon 0:25fa8795676b 625 if (string != NULL) {
leothedragon 0:25fa8795676b 626 const size_t len = strlen(string);
leothedragon 0:25fa8795676b 627 if ((len >= min_length) && (len <= max_length)) {
leothedragon 0:25fa8795676b 628 valid = true;
leothedragon 0:25fa8795676b 629 }
leothedragon 0:25fa8795676b 630 }
leothedragon 0:25fa8795676b 631 return valid;
leothedragon 0:25fa8795676b 632 }
leothedragon 0:25fa8795676b 633
leothedragon 0:25fa8795676b 634 M2MReportHandler* M2MBase::create_report_handler()
leothedragon 0:25fa8795676b 635 {
leothedragon 0:25fa8795676b 636 if (!_report_handler) {
leothedragon 0:25fa8795676b 637 _report_handler = new M2MReportHandler(*this, _sn_resource->data_type);
leothedragon 0:25fa8795676b 638 }
leothedragon 0:25fa8795676b 639 return _report_handler;
leothedragon 0:25fa8795676b 640 }
leothedragon 0:25fa8795676b 641
leothedragon 0:25fa8795676b 642 M2MReportHandler* M2MBase::report_handler() const
leothedragon 0:25fa8795676b 643 {
leothedragon 0:25fa8795676b 644 return _report_handler;
leothedragon 0:25fa8795676b 645 }
leothedragon 0:25fa8795676b 646
leothedragon 0:25fa8795676b 647 void M2MBase::set_register_uri(bool register_uri)
leothedragon 0:25fa8795676b 648 {
leothedragon 0:25fa8795676b 649 _sn_resource->dynamic_resource_params->publish_uri = register_uri;
leothedragon 0:25fa8795676b 650 }
leothedragon 0:25fa8795676b 651
leothedragon 0:25fa8795676b 652 bool M2MBase::register_uri()
leothedragon 0:25fa8795676b 653 {
leothedragon 0:25fa8795676b 654 return _sn_resource->dynamic_resource_params->publish_uri;
leothedragon 0:25fa8795676b 655 }
leothedragon 0:25fa8795676b 656
leothedragon 0:25fa8795676b 657 bool M2MBase::is_integer(const String &value)
leothedragon 0:25fa8795676b 658 {
leothedragon 0:25fa8795676b 659 const char *s = value.c_str();
leothedragon 0:25fa8795676b 660 if(value.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) {
leothedragon 0:25fa8795676b 661 return false;
leothedragon 0:25fa8795676b 662 }
leothedragon 0:25fa8795676b 663 char * p;
leothedragon 0:25fa8795676b 664 strtol(value.c_str(), &p, 10);
leothedragon 0:25fa8795676b 665 return (*p == 0);
leothedragon 0:25fa8795676b 666 }
leothedragon 0:25fa8795676b 667
leothedragon 0:25fa8795676b 668 bool M2MBase::is_integer(const char *value)
leothedragon 0:25fa8795676b 669 {
leothedragon 0:25fa8795676b 670 assert(value != NULL);
leothedragon 0:25fa8795676b 671
leothedragon 0:25fa8795676b 672 if((strlen(value) < 1) || ((!isdigit(value[0])) && (value[0] != '-') && (value[0] != '+'))) {
leothedragon 0:25fa8795676b 673 return false;
leothedragon 0:25fa8795676b 674 }
leothedragon 0:25fa8795676b 675 char * p;
leothedragon 0:25fa8795676b 676 strtol(value, &p, 10);
leothedragon 0:25fa8795676b 677 return (*p == 0);
leothedragon 0:25fa8795676b 678 }
leothedragon 0:25fa8795676b 679
leothedragon 0:25fa8795676b 680 bool M2MBase::is_under_observation() const
leothedragon 0:25fa8795676b 681 {
leothedragon 0:25fa8795676b 682 bool under_observation = false;
leothedragon 0:25fa8795676b 683 if(_report_handler) {
leothedragon 0:25fa8795676b 684 under_observation = _report_handler->is_under_observation();
leothedragon 0:25fa8795676b 685 }
leothedragon 0:25fa8795676b 686 return under_observation;
leothedragon 0:25fa8795676b 687 }
leothedragon 0:25fa8795676b 688
leothedragon 0:25fa8795676b 689 bool M2MBase::set_value_updated_function(value_updated_callback callback)
leothedragon 0:25fa8795676b 690 {
leothedragon 0:25fa8795676b 691 value_updated_callback* old_callback = (value_updated_callback*)M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MBaseValueUpdatedCallback);
leothedragon 0:25fa8795676b 692 delete old_callback;
leothedragon 0:25fa8795676b 693 // XXX: create a copy of the copy of callback object. Perhaps it would better to
leothedragon 0:25fa8795676b 694 // give a reference as parameter and just store that, as it would save some memory.
leothedragon 0:25fa8795676b 695 value_updated_callback* new_callback = new value_updated_callback(callback);
leothedragon 0:25fa8795676b 696
leothedragon 0:25fa8795676b 697 return M2MCallbackStorage::add_callback(*this, new_callback, M2MCallbackAssociation::M2MBaseValueUpdatedCallback);
leothedragon 0:25fa8795676b 698 }
leothedragon 0:25fa8795676b 699
leothedragon 0:25fa8795676b 700 bool M2MBase::set_value_updated_function(value_updated_callback2 callback)
leothedragon 0:25fa8795676b 701 {
leothedragon 0:25fa8795676b 702 M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MBaseValueUpdatedCallback2);
leothedragon 0:25fa8795676b 703
leothedragon 0:25fa8795676b 704 return M2MCallbackStorage::add_callback(*this, (void*)callback, M2MCallbackAssociation::M2MBaseValueUpdatedCallback2);
leothedragon 0:25fa8795676b 705 }
leothedragon 0:25fa8795676b 706
leothedragon 0:25fa8795676b 707 bool M2MBase::is_value_updated_function_set() const
leothedragon 0:25fa8795676b 708 {
leothedragon 0:25fa8795676b 709 bool func_set = false;
leothedragon 0:25fa8795676b 710 if ((M2MCallbackStorage::does_callback_exist(*this, M2MCallbackAssociation::M2MBaseValueUpdatedCallback) == true) ||
leothedragon 0:25fa8795676b 711 (M2MCallbackStorage::does_callback_exist(*this, M2MCallbackAssociation::M2MBaseValueUpdatedCallback2) == true)) {
leothedragon 0:25fa8795676b 712
leothedragon 0:25fa8795676b 713 func_set = true;
leothedragon 0:25fa8795676b 714 }
leothedragon 0:25fa8795676b 715 return func_set;
leothedragon 0:25fa8795676b 716 }
leothedragon 0:25fa8795676b 717
leothedragon 0:25fa8795676b 718 void M2MBase::execute_value_updated(const String& name)
leothedragon 0:25fa8795676b 719 {
leothedragon 0:25fa8795676b 720 // Q: is there a point to call both callback types? Or should we call just one of them?
leothedragon 0:25fa8795676b 721
leothedragon 0:25fa8795676b 722 value_updated_callback* callback = (value_updated_callback*)M2MCallbackStorage::get_callback(*this,
leothedragon 0:25fa8795676b 723 M2MCallbackAssociation::M2MBaseValueUpdatedCallback);
leothedragon 0:25fa8795676b 724 if (callback) {
leothedragon 0:25fa8795676b 725 (*callback)(name.c_str());
leothedragon 0:25fa8795676b 726 }
leothedragon 0:25fa8795676b 727
leothedragon 0:25fa8795676b 728 value_updated_callback2 callback2 = (value_updated_callback2)M2MCallbackStorage::get_callback(*this, M2MCallbackAssociation::M2MBaseValueUpdatedCallback2);
leothedragon 0:25fa8795676b 729 if (callback2) {
leothedragon 0:25fa8795676b 730 (*callback2)(name.c_str());
leothedragon 0:25fa8795676b 731 }
leothedragon 0:25fa8795676b 732 }
leothedragon 0:25fa8795676b 733
leothedragon 0:25fa8795676b 734 bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE> &buffer, const char *s1, uint16_t i1, const char *s2, uint16_t i2)
leothedragon 0:25fa8795676b 735 {
leothedragon 0:25fa8795676b 736
leothedragon 0:25fa8795676b 737 if(!buffer.ensure_space(strlen(s1) + strlen(s2) + (MAX_INSTANCE_SIZE * 2) + 3 + 1)){
leothedragon 0:25fa8795676b 738 return false;
leothedragon 0:25fa8795676b 739 }
leothedragon 0:25fa8795676b 740
leothedragon 0:25fa8795676b 741 buffer.append(s1);
leothedragon 0:25fa8795676b 742 buffer.append('/');
leothedragon 0:25fa8795676b 743 buffer.append_int(i1);
leothedragon 0:25fa8795676b 744 buffer.append('/');
leothedragon 0:25fa8795676b 745 buffer.append(s2);
leothedragon 0:25fa8795676b 746 buffer.append('/');
leothedragon 0:25fa8795676b 747 buffer.append_int(i2);
leothedragon 0:25fa8795676b 748
leothedragon 0:25fa8795676b 749 return true;
leothedragon 0:25fa8795676b 750
leothedragon 0:25fa8795676b 751 }
leothedragon 0:25fa8795676b 752
leothedragon 0:25fa8795676b 753 bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE_2> &buffer, const char *s1, uint16_t i1, const char *s2)
leothedragon 0:25fa8795676b 754 {
leothedragon 0:25fa8795676b 755 if(!buffer.ensure_space(strlen(s1) + strlen(s2) + MAX_INSTANCE_SIZE + 2 + 1)){
leothedragon 0:25fa8795676b 756 return false;
leothedragon 0:25fa8795676b 757 }
leothedragon 0:25fa8795676b 758
leothedragon 0:25fa8795676b 759 buffer.append(s1);
leothedragon 0:25fa8795676b 760 buffer.append('/');
leothedragon 0:25fa8795676b 761 buffer.append_int(i1);
leothedragon 0:25fa8795676b 762 buffer.append('/');
leothedragon 0:25fa8795676b 763 buffer.append(s2);
leothedragon 0:25fa8795676b 764
leothedragon 0:25fa8795676b 765 return true;
leothedragon 0:25fa8795676b 766 }
leothedragon 0:25fa8795676b 767
leothedragon 0:25fa8795676b 768 bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE_3> &buffer, const char *s1, uint16_t i1, uint16_t i2)
leothedragon 0:25fa8795676b 769 {
leothedragon 0:25fa8795676b 770 if(!buffer.ensure_space(strlen(s1) + (MAX_INSTANCE_SIZE * 2) + 2 + 1)){
leothedragon 0:25fa8795676b 771 return false;
leothedragon 0:25fa8795676b 772 }
leothedragon 0:25fa8795676b 773
leothedragon 0:25fa8795676b 774 buffer.append(s1);
leothedragon 0:25fa8795676b 775 buffer.append('/');
leothedragon 0:25fa8795676b 776 buffer.append_int(i1);
leothedragon 0:25fa8795676b 777 buffer.append('/');
leothedragon 0:25fa8795676b 778 buffer.append_int(i2);
leothedragon 0:25fa8795676b 779
leothedragon 0:25fa8795676b 780 return true;
leothedragon 0:25fa8795676b 781 }
leothedragon 0:25fa8795676b 782
leothedragon 0:25fa8795676b 783 bool M2MBase::build_path(StringBuffer<MAX_PATH_SIZE_4> &buffer, const char *s1, uint16_t i1)
leothedragon 0:25fa8795676b 784 {
leothedragon 0:25fa8795676b 785 if(!buffer.ensure_space(strlen(s1) + MAX_INSTANCE_SIZE + 1 + 1)){
leothedragon 0:25fa8795676b 786 return false;
leothedragon 0:25fa8795676b 787 }
leothedragon 0:25fa8795676b 788
leothedragon 0:25fa8795676b 789 buffer.append(s1);
leothedragon 0:25fa8795676b 790 buffer.append('/');
leothedragon 0:25fa8795676b 791 buffer.append_int(i1);
leothedragon 0:25fa8795676b 792
leothedragon 0:25fa8795676b 793 return true;
leothedragon 0:25fa8795676b 794 }
leothedragon 0:25fa8795676b 795
leothedragon 0:25fa8795676b 796 char* M2MBase::stringdup(const char* src)
leothedragon 0:25fa8795676b 797 {
leothedragon 0:25fa8795676b 798 assert(src != NULL);
leothedragon 0:25fa8795676b 799
leothedragon 0:25fa8795676b 800 const size_t len = strlen(src) + 1;
leothedragon 0:25fa8795676b 801
leothedragon 0:25fa8795676b 802 char *dest = (char*)malloc(len);
leothedragon 0:25fa8795676b 803
leothedragon 0:25fa8795676b 804 if (dest) {
leothedragon 0:25fa8795676b 805 memcpy(dest, src, len);
leothedragon 0:25fa8795676b 806 }
leothedragon 0:25fa8795676b 807 return dest;
leothedragon 0:25fa8795676b 808 }
leothedragon 0:25fa8795676b 809
leothedragon 0:25fa8795676b 810 void M2MBase::free_resources()
leothedragon 0:25fa8795676b 811 {
leothedragon 0:25fa8795676b 812 // remove the nsdl structures from the nsdlinterface's lists.
leothedragon 0:25fa8795676b 813 M2MObservationHandler *obs_handler = observation_handler();
leothedragon 0:25fa8795676b 814 if (obs_handler) {
leothedragon 0:25fa8795676b 815 tr_debug("M2MBase::free_resources()");
leothedragon 0:25fa8795676b 816 obs_handler->resource_to_be_deleted(this);
leothedragon 0:25fa8795676b 817 }
leothedragon 0:25fa8795676b 818
leothedragon 0:25fa8795676b 819 if (_sn_resource->dynamic_resource_params->static_resource_parameters->free_on_delete) {
leothedragon 0:25fa8795676b 820 sn_nsdl_static_resource_parameters_s *params =
leothedragon 0:25fa8795676b 821 const_cast<sn_nsdl_static_resource_parameters_s *>(_sn_resource->dynamic_resource_params->static_resource_parameters);
leothedragon 0:25fa8795676b 822
leothedragon 0:25fa8795676b 823 free(params->path);
leothedragon 0:25fa8795676b 824 //free(params->resource);
leothedragon 0:25fa8795676b 825 #ifndef RESOURCE_ATTRIBUTES_LIST
leothedragon 0:25fa8795676b 826 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 827 free(params->resource_type_ptr);
leothedragon 0:25fa8795676b 828 #endif
leothedragon 0:25fa8795676b 829 #ifndef DISABLE_INTERFACE_DESCRIPTION
leothedragon 0:25fa8795676b 830 free(params->interface_description_ptr);
leothedragon 0:25fa8795676b 831 #endif
leothedragon 0:25fa8795676b 832 #else
leothedragon 0:25fa8795676b 833 sn_nsdl_free_resource_attributes_list(_sn_resource->dynamic_resource_params->static_resource_parameters);
leothedragon 0:25fa8795676b 834 #endif
leothedragon 0:25fa8795676b 835 free(params);
leothedragon 0:25fa8795676b 836 }
leothedragon 0:25fa8795676b 837 if (_sn_resource->dynamic_resource_params->free_on_delete) {
leothedragon 0:25fa8795676b 838 free(_sn_resource->dynamic_resource_params->resource);
leothedragon 0:25fa8795676b 839 free(_sn_resource->dynamic_resource_params);
leothedragon 0:25fa8795676b 840 }
leothedragon 0:25fa8795676b 841
leothedragon 0:25fa8795676b 842 if(_sn_resource->free_on_delete && _sn_resource->identifier_int_type == false) {
leothedragon 0:25fa8795676b 843 tr_debug("M2MBase::free_resources()");
leothedragon 0:25fa8795676b 844 free(_sn_resource->identifier.name);
leothedragon 0:25fa8795676b 845 }
leothedragon 0:25fa8795676b 846 if(_sn_resource->free_on_delete) {
leothedragon 0:25fa8795676b 847 free(_sn_resource);
leothedragon 0:25fa8795676b 848 }
leothedragon 0:25fa8795676b 849 }
leothedragon 0:25fa8795676b 850
leothedragon 0:25fa8795676b 851 size_t M2MBase::resource_name_length() const
leothedragon 0:25fa8795676b 852 {
leothedragon 0:25fa8795676b 853 assert(_sn_resource->identifier_int_type == false);
leothedragon 0:25fa8795676b 854 return strlen(_sn_resource->identifier.name);
leothedragon 0:25fa8795676b 855 }
leothedragon 0:25fa8795676b 856
leothedragon 0:25fa8795676b 857 sn_nsdl_dynamic_resource_parameters_s* M2MBase::get_nsdl_resource() const
leothedragon 0:25fa8795676b 858 {
leothedragon 0:25fa8795676b 859 return _sn_resource->dynamic_resource_params;
leothedragon 0:25fa8795676b 860 }
leothedragon 0:25fa8795676b 861
leothedragon 0:25fa8795676b 862 M2MBase::lwm2m_parameters_s* M2MBase::get_lwm2m_parameters() const
leothedragon 0:25fa8795676b 863 {
leothedragon 0:25fa8795676b 864 return _sn_resource;
leothedragon 0:25fa8795676b 865 }
leothedragon 0:25fa8795676b 866
leothedragon 0:25fa8795676b 867 #ifdef ENABLE_ASYNC_REST_RESPONSE
leothedragon 0:25fa8795676b 868 bool M2MBase::send_async_response_with_code(const uint8_t *payload,
leothedragon 0:25fa8795676b 869 size_t payload_len,
leothedragon 0:25fa8795676b 870 const uint8_t* token,
leothedragon 0:25fa8795676b 871 const uint8_t token_len,
leothedragon 0:25fa8795676b 872 coap_response_code_e code)
leothedragon 0:25fa8795676b 873 {
leothedragon 0:25fa8795676b 874 bool success = false;
leothedragon 0:25fa8795676b 875 if(is_async_coap_request_callback_set()) {
leothedragon 0:25fa8795676b 876 success = true;
leothedragon 0:25fa8795676b 877 // At least on some unit tests the resource object is not fully constructed, which would
leothedragon 0:25fa8795676b 878 // cause issues if the observation_handler is NULL. So do the check before dereferencing pointer.
leothedragon 0:25fa8795676b 879 M2MObservationHandler* obs = observation_handler();
leothedragon 0:25fa8795676b 880 if (obs) {
leothedragon 0:25fa8795676b 881 obs->send_asynchronous_response(this, payload, payload_len, token, token_len, code);
leothedragon 0:25fa8795676b 882 }
leothedragon 0:25fa8795676b 883 }
leothedragon 0:25fa8795676b 884 return success;
leothedragon 0:25fa8795676b 885 }
leothedragon 0:25fa8795676b 886
leothedragon 0:25fa8795676b 887 bool M2MBase::set_async_coap_request_cb(handle_async_coap_request_cb callback, void *client_args)
leothedragon 0:25fa8795676b 888 {
leothedragon 0:25fa8795676b 889 M2MCallbackStorage::remove_callback(*this,
leothedragon 0:25fa8795676b 890 M2MCallbackAssociation::M2MBaseAsyncCoapRequestCallback);
leothedragon 0:25fa8795676b 891
leothedragon 0:25fa8795676b 892 return M2MCallbackStorage::add_callback(*this,
leothedragon 0:25fa8795676b 893 (void*)callback,
leothedragon 0:25fa8795676b 894 M2MCallbackAssociation::M2MBaseAsyncCoapRequestCallback,
leothedragon 0:25fa8795676b 895 client_args);
leothedragon 0:25fa8795676b 896 }
leothedragon 0:25fa8795676b 897
leothedragon 0:25fa8795676b 898 void M2MBase::call_async_coap_request_callback(sn_coap_hdr_s *coap_request,
leothedragon 0:25fa8795676b 899 M2MBase::Operation operation,
leothedragon 0:25fa8795676b 900 bool &handled)
leothedragon 0:25fa8795676b 901 {
leothedragon 0:25fa8795676b 902 M2MCallbackAssociation* item = M2MCallbackStorage::get_association_item(*this,
leothedragon 0:25fa8795676b 903 M2MCallbackAssociation::M2MBaseAsyncCoapRequestCallback);
leothedragon 0:25fa8795676b 904 if (item) {
leothedragon 0:25fa8795676b 905 handle_async_coap_request_cb callback = (handle_async_coap_request_cb)item->_callback;
leothedragon 0:25fa8795676b 906 assert(callback);
leothedragon 0:25fa8795676b 907 assert(coap_request);
leothedragon 0:25fa8795676b 908 handled = true;
leothedragon 0:25fa8795676b 909 (*callback)(*this,
leothedragon 0:25fa8795676b 910 operation,
leothedragon 0:25fa8795676b 911 coap_request->token_ptr,
leothedragon 0:25fa8795676b 912 coap_request->token_len,
leothedragon 0:25fa8795676b 913 coap_request->payload_ptr,
leothedragon 0:25fa8795676b 914 coap_request->payload_len,
leothedragon 0:25fa8795676b 915 item->_client_args);
leothedragon 0:25fa8795676b 916 }
leothedragon 0:25fa8795676b 917 }
leothedragon 0:25fa8795676b 918
leothedragon 0:25fa8795676b 919 bool M2MBase::is_async_coap_request_callback_set()
leothedragon 0:25fa8795676b 920 {
leothedragon 0:25fa8795676b 921 M2MCallbackAssociation* item = M2MCallbackStorage::get_association_item(*this, M2MCallbackAssociation::M2MBaseAsyncCoapRequestCallback);
leothedragon 0:25fa8795676b 922 return (item) ? true : false;
leothedragon 0:25fa8795676b 923
leothedragon 0:25fa8795676b 924 }
leothedragon 0:25fa8795676b 925 #endif // ENABLE_ASYNC_REST_RESPONSE
leothedragon 0:25fa8795676b 926
leothedragon 0:25fa8795676b 927 uint16_t M2MBase::get_notification_msgid() const
leothedragon 0:25fa8795676b 928 {
leothedragon 0:25fa8795676b 929 return 0;
leothedragon 0:25fa8795676b 930 }
leothedragon 0:25fa8795676b 931
leothedragon 0:25fa8795676b 932 void M2MBase::set_notification_msgid(uint16_t /*msgid*/)
leothedragon 0:25fa8795676b 933 {
leothedragon 0:25fa8795676b 934
leothedragon 0:25fa8795676b 935 }
leothedragon 0:25fa8795676b 936
leothedragon 0:25fa8795676b 937 bool M2MBase::set_notification_delivery_status_cb(notification_delivery_status_cb callback, void *client_args)
leothedragon 0:25fa8795676b 938 {
leothedragon 0:25fa8795676b 939 M2MCallbackStorage::remove_callback(*this,
leothedragon 0:25fa8795676b 940 M2MCallbackAssociation::M2MBaseNotificationDeliveryStatusCallback);
leothedragon 0:25fa8795676b 941
leothedragon 0:25fa8795676b 942 return M2MCallbackStorage::add_callback(*this,
leothedragon 0:25fa8795676b 943 (void*)callback,
leothedragon 0:25fa8795676b 944 M2MCallbackAssociation::M2MBaseNotificationDeliveryStatusCallback,
leothedragon 0:25fa8795676b 945 client_args);
leothedragon 0:25fa8795676b 946 }
leothedragon 0:25fa8795676b 947
leothedragon 0:25fa8795676b 948 bool M2MBase::set_message_delivery_status_cb(message_delivery_status_cb callback, void *client_args)
leothedragon 0:25fa8795676b 949 {
leothedragon 0:25fa8795676b 950 M2MCallbackStorage::remove_callback(*this, M2MCallbackAssociation::M2MBaseMessageDeliveryStatusCallback);
leothedragon 0:25fa8795676b 951
leothedragon 0:25fa8795676b 952 return M2MCallbackStorage::add_callback(*this,
leothedragon 0:25fa8795676b 953 (void*)callback,
leothedragon 0:25fa8795676b 954 M2MCallbackAssociation::M2MBaseMessageDeliveryStatusCallback,
leothedragon 0:25fa8795676b 955 client_args);
leothedragon 0:25fa8795676b 956 }
leothedragon 0:25fa8795676b 957
leothedragon 0:25fa8795676b 958 void M2MBase::send_notification_delivery_status(const M2MBase& object, const NotificationDeliveryStatus status)
leothedragon 0:25fa8795676b 959 {
leothedragon 0:25fa8795676b 960 M2MCallbackAssociation* item = M2MCallbackStorage::get_association_item(object,
leothedragon 0:25fa8795676b 961 M2MCallbackAssociation::M2MBaseNotificationDeliveryStatusCallback);
leothedragon 0:25fa8795676b 962 if (item) {
leothedragon 0:25fa8795676b 963 notification_delivery_status_cb callback = (notification_delivery_status_cb)item->_callback;
leothedragon 0:25fa8795676b 964 if (callback) {
leothedragon 0:25fa8795676b 965 (*callback)(object, status, item->_client_args);
leothedragon 0:25fa8795676b 966 }
leothedragon 0:25fa8795676b 967 }
leothedragon 0:25fa8795676b 968 }
leothedragon 0:25fa8795676b 969
leothedragon 0:25fa8795676b 970 void M2MBase::send_message_delivery_status(const M2MBase& object, const MessageDeliveryStatus status, const MessageType type)
leothedragon 0:25fa8795676b 971 {
leothedragon 0:25fa8795676b 972 M2MCallbackAssociation* item = M2MCallbackStorage::get_association_item(object,
leothedragon 0:25fa8795676b 973 M2MCallbackAssociation::M2MBaseMessageDeliveryStatusCallback);
leothedragon 0:25fa8795676b 974 if (item) {
leothedragon 0:25fa8795676b 975 message_delivery_status_cb callback = (message_delivery_status_cb)item->_callback;
leothedragon 0:25fa8795676b 976 if (callback) {
leothedragon 0:25fa8795676b 977 (*callback)(object, status, type, item->_client_args);
leothedragon 0:25fa8795676b 978 }
leothedragon 0:25fa8795676b 979 }
leothedragon 0:25fa8795676b 980 }
leothedragon 0:25fa8795676b 981
leothedragon 0:25fa8795676b 982 void M2MBase::set_changed()
leothedragon 0:25fa8795676b 983 {
leothedragon 0:25fa8795676b 984 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 985 M2MBase *parent = get_parent();
leothedragon 0:25fa8795676b 986 if (parent) {
leothedragon 0:25fa8795676b 987 parent->set_changed();
leothedragon 0:25fa8795676b 988 }
leothedragon 0:25fa8795676b 989 #endif
leothedragon 0:25fa8795676b 990 }
leothedragon 0:25fa8795676b 991
leothedragon 0:25fa8795676b 992 M2MBase *M2MBase::get_parent() const
leothedragon 0:25fa8795676b 993 {
leothedragon 0:25fa8795676b 994 return NULL;
leothedragon 0:25fa8795676b 995 }
leothedragon 0:25fa8795676b 996
leothedragon 0:25fa8795676b 997 bool M2MBase::is_blockwise_needed(const nsdl_s *nsdl, uint32_t payload_len)
leothedragon 0:25fa8795676b 998 {
leothedragon 0:25fa8795676b 999
leothedragon 0:25fa8795676b 1000 uint16_t block_size = sn_nsdl_get_block_size(nsdl);
leothedragon 0:25fa8795676b 1001
leothedragon 0:25fa8795676b 1002 if (payload_len > block_size && block_size > 0) {
leothedragon 0:25fa8795676b 1003 return true;
leothedragon 0:25fa8795676b 1004 } else {
leothedragon 0:25fa8795676b 1005 return false;
leothedragon 0:25fa8795676b 1006 }
leothedragon 0:25fa8795676b 1007 }
leothedragon 0:25fa8795676b 1008
leothedragon 0:25fa8795676b 1009 void M2MBase::handle_observation(nsdl_s *nsdl,
leothedragon 0:25fa8795676b 1010 const sn_coap_hdr_s &received_coap_header,
leothedragon 0:25fa8795676b 1011 sn_coap_hdr_s &coap_response,
leothedragon 0:25fa8795676b 1012 M2MObservationHandler *observation_handler,
leothedragon 0:25fa8795676b 1013 sn_coap_msg_code_e &response_code)
leothedragon 0:25fa8795676b 1014 {
leothedragon 0:25fa8795676b 1015 tr_debug("M2MBase::handle_observation()");
leothedragon 0:25fa8795676b 1016 assert(nsdl);
leothedragon 0:25fa8795676b 1017 assert(received_coap_header.options_list_ptr);
leothedragon 0:25fa8795676b 1018
leothedragon 0:25fa8795676b 1019 response_code = COAP_MSG_CODE_RESPONSE_CONTENT;
leothedragon 0:25fa8795676b 1020
leothedragon 0:25fa8795676b 1021 if (is_auto_observable() || received_coap_header.token_ptr == NULL) {
leothedragon 0:25fa8795676b 1022 tr_error("M2MBase::handle_observation() - already auto-observable or missing token!");
leothedragon 0:25fa8795676b 1023 response_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
leothedragon 0:25fa8795676b 1024 return;
leothedragon 0:25fa8795676b 1025 }
leothedragon 0:25fa8795676b 1026
leothedragon 0:25fa8795676b 1027 if (!is_observable()) {
leothedragon 0:25fa8795676b 1028 tr_error("M2MBase::handle_observation() - not observable!");
leothedragon 0:25fa8795676b 1029 response_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:25fa8795676b 1030 return;
leothedragon 0:25fa8795676b 1031 }
leothedragon 0:25fa8795676b 1032
leothedragon 0:25fa8795676b 1033 // Add observe value to response
leothedragon 0:25fa8795676b 1034 if (coap_response.options_list_ptr) {
leothedragon 0:25fa8795676b 1035 coap_response.options_list_ptr->observe = observation_number();
leothedragon 0:25fa8795676b 1036 }
leothedragon 0:25fa8795676b 1037
leothedragon 0:25fa8795676b 1038 // In case of blockwise, delivery status callback is handled in m2mnsdlinterface after all the block have been transfered
leothedragon 0:25fa8795676b 1039 if (M2MBase::is_blockwise_needed(nsdl, coap_response.payload_len)) {
leothedragon 0:25fa8795676b 1040 tr_debug("M2MBase::handle_observation() - block message");
leothedragon 0:25fa8795676b 1041 return;
leothedragon 0:25fa8795676b 1042 }
leothedragon 0:25fa8795676b 1043
leothedragon 0:25fa8795676b 1044 uint32_t obs_number = received_coap_header.options_list_ptr->observe;
leothedragon 0:25fa8795676b 1045
leothedragon 0:25fa8795676b 1046 // If the observe number is 0 means register for observation.
leothedragon 0:25fa8795676b 1047 if (START_OBSERVATION == obs_number) {
leothedragon 0:25fa8795676b 1048
leothedragon 0:25fa8795676b 1049 start_observation(received_coap_header, observation_handler);
leothedragon 0:25fa8795676b 1050
leothedragon 0:25fa8795676b 1051 } else if (STOP_OBSERVATION == obs_number) {
leothedragon 0:25fa8795676b 1052 tr_info("M2MBase::handle_observation() - stops observation");
leothedragon 0:25fa8795676b 1053
leothedragon 0:25fa8795676b 1054 set_under_observation(false, NULL);
leothedragon 0:25fa8795676b 1055 send_notification_delivery_status(*this, NOTIFICATION_STATUS_UNSUBSCRIBED);
leothedragon 0:25fa8795676b 1056 send_message_delivery_status(*this, M2MBase::MESSAGE_STATUS_UNSUBSCRIBED, M2MBase::NOTIFICATION);
leothedragon 0:25fa8795676b 1057
leothedragon 0:25fa8795676b 1058 switch (base_type()) {
leothedragon 0:25fa8795676b 1059 case M2MBase::Object:
leothedragon 0:25fa8795676b 1060 M2MBase::remove_observation_level(M2MBase::O_Attribute);
leothedragon 0:25fa8795676b 1061 break;
leothedragon 0:25fa8795676b 1062
leothedragon 0:25fa8795676b 1063 case M2MBase::ObjectInstance:
leothedragon 0:25fa8795676b 1064 M2MBase::remove_observation_level(M2MBase::OI_Attribute);
leothedragon 0:25fa8795676b 1065 break;
leothedragon 0:25fa8795676b 1066
leothedragon 0:25fa8795676b 1067 case M2MBase::Resource:
leothedragon 0:25fa8795676b 1068 case M2MBase::ResourceInstance:
leothedragon 0:25fa8795676b 1069 M2MBase::remove_observation_level(M2MBase::R_Attribute);
leothedragon 0:25fa8795676b 1070 break;
leothedragon 0:25fa8795676b 1071 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 1072 case M2MBase::ObjectDirectory:
leothedragon 0:25fa8795676b 1073 // Observation not supported!
leothedragon 0:25fa8795676b 1074 break;
leothedragon 0:25fa8795676b 1075 #endif
leothedragon 0:25fa8795676b 1076 }
leothedragon 0:25fa8795676b 1077 }
leothedragon 0:25fa8795676b 1078 }
leothedragon 0:25fa8795676b 1079
leothedragon 0:25fa8795676b 1080 void M2MBase::start_observation(const sn_coap_hdr_s &received_coap_header, M2MObservationHandler *observation_handler)
leothedragon 0:25fa8795676b 1081 {
leothedragon 0:25fa8795676b 1082 set_under_observation(true, observation_handler);
leothedragon 0:25fa8795676b 1083
leothedragon 0:25fa8795676b 1084 switch (base_type()) {
leothedragon 0:25fa8795676b 1085 case M2MBase::Object:
leothedragon 0:25fa8795676b 1086 M2MBase::add_observation_level(M2MBase::O_Attribute);
leothedragon 0:25fa8795676b 1087 break;
leothedragon 0:25fa8795676b 1088
leothedragon 0:25fa8795676b 1089 case M2MBase::ObjectInstance:
leothedragon 0:25fa8795676b 1090 M2MBase::add_observation_level(M2MBase::OI_Attribute);
leothedragon 0:25fa8795676b 1091 break;
leothedragon 0:25fa8795676b 1092
leothedragon 0:25fa8795676b 1093 case M2MBase::Resource:
leothedragon 0:25fa8795676b 1094 case M2MBase::ResourceInstance:
leothedragon 0:25fa8795676b 1095 M2MBase::add_observation_level(M2MBase::R_Attribute);
leothedragon 0:25fa8795676b 1096 break;
leothedragon 0:25fa8795676b 1097 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 1098 case M2MBase::ObjectDirectory:
leothedragon 0:25fa8795676b 1099 // Observation not supported!
leothedragon 0:25fa8795676b 1100 break;
leothedragon 0:25fa8795676b 1101 #endif
leothedragon 0:25fa8795676b 1102 }
leothedragon 0:25fa8795676b 1103
leothedragon 0:25fa8795676b 1104 send_notification_delivery_status(*this, NOTIFICATION_STATUS_SUBSCRIBED);
leothedragon 0:25fa8795676b 1105 send_message_delivery_status(*this, M2MBase::MESSAGE_STATUS_SUBSCRIBED, M2MBase::NOTIFICATION);
leothedragon 0:25fa8795676b 1106
leothedragon 0:25fa8795676b 1107 set_observation_token(received_coap_header.token_ptr,
leothedragon 0:25fa8795676b 1108 received_coap_header.token_len);
leothedragon 0:25fa8795676b 1109
leothedragon 0:25fa8795676b 1110 }
leothedragon 0:25fa8795676b 1111
leothedragon 0:25fa8795676b 1112 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 1113
leothedragon 0:25fa8795676b 1114 void M2MBase::set_deleted()
leothedragon 0:25fa8795676b 1115 {
leothedragon 0:25fa8795676b 1116 // no-op
leothedragon 0:25fa8795676b 1117 }
leothedragon 0:25fa8795676b 1118
leothedragon 0:25fa8795676b 1119 bool M2MBase::is_deleted()
leothedragon 0:25fa8795676b 1120 {
leothedragon 0:25fa8795676b 1121 return false;
leothedragon 0:25fa8795676b 1122 }
leothedragon 0:25fa8795676b 1123
leothedragon 0:25fa8795676b 1124 #endif // MBED_CLOUD_CLIENT_EDGE_EXTENSION