leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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