Dependencies:   MMA7660 LM75B

Committer:
MACRUM
Date:
Sat Jun 30 01:40:30 2018 +0000
Revision:
0:119624335925
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:119624335925 1 /*
MACRUM 0:119624335925 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
MACRUM 0:119624335925 3 * SPDX-License-Identifier: Apache-2.0
MACRUM 0:119624335925 4 * Licensed under the Apache License, Version 2.0 (the License); you may
MACRUM 0:119624335925 5 * not use this file except in compliance with the License.
MACRUM 0:119624335925 6 * You may obtain a copy of the License at
MACRUM 0:119624335925 7 *
MACRUM 0:119624335925 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:119624335925 9 *
MACRUM 0:119624335925 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:119624335925 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
MACRUM 0:119624335925 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:119624335925 13 * See the License for the specific language governing permissions and
MACRUM 0:119624335925 14 * limitations under the License.
MACRUM 0:119624335925 15 */
MACRUM 0:119624335925 16 #include "mbed-client/m2mobjectinstance.h"
MACRUM 0:119624335925 17 #include "mbed-client/m2mobject.h"
MACRUM 0:119624335925 18 #include "mbed-client/m2mconstants.h"
MACRUM 0:119624335925 19 #include "mbed-client/m2mresource.h"
MACRUM 0:119624335925 20 #include "mbed-client/m2mresource.h"
MACRUM 0:119624335925 21 #include "mbed-client/m2mobservationhandler.h"
MACRUM 0:119624335925 22 #include "mbed-client/m2mstring.h"
MACRUM 0:119624335925 23 #include "mbed-client/m2mstringbuffer.h"
MACRUM 0:119624335925 24 #include "include/m2mtlvserializer.h"
MACRUM 0:119624335925 25 #include "include/m2mtlvdeserializer.h"
MACRUM 0:119624335925 26 #include "include/m2mreporthandler.h"
MACRUM 0:119624335925 27 #include "mbed-trace/mbed_trace.h"
MACRUM 0:119624335925 28 #include "include/m2mcallbackstorage.h"
MACRUM 0:119624335925 29 #include <stdlib.h>
MACRUM 0:119624335925 30
MACRUM 0:119624335925 31 #define BUFFER_SIZE 10
MACRUM 0:119624335925 32 #define TRACE_GROUP "mClt"
MACRUM 0:119624335925 33
MACRUM 0:119624335925 34 M2MObjectInstance::M2MObjectInstance(M2MObject& parent,
MACRUM 0:119624335925 35 const String &resource_type,
MACRUM 0:119624335925 36 char *path,
MACRUM 0:119624335925 37 bool external_blockwise_store)
MACRUM 0:119624335925 38 : M2MBase("",
MACRUM 0:119624335925 39 M2MBase::Dynamic,
MACRUM 0:119624335925 40 #ifndef DISABLE_RESOURCE_TYPE
MACRUM 0:119624335925 41 resource_type,
MACRUM 0:119624335925 42 #endif
MACRUM 0:119624335925 43 path,
MACRUM 0:119624335925 44 external_blockwise_store,
MACRUM 0:119624335925 45 false),
MACRUM 0:119624335925 46 _parent(parent)
MACRUM 0:119624335925 47 {
MACRUM 0:119624335925 48 M2MBase::set_base_type(M2MBase::ObjectInstance);
MACRUM 0:119624335925 49 M2MBase::set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
MACRUM 0:119624335925 50 M2MBase::set_operation(M2MBase::GET_ALLOWED);
MACRUM 0:119624335925 51 }
MACRUM 0:119624335925 52
MACRUM 0:119624335925 53 M2MObjectInstance::M2MObjectInstance(M2MObject& parent, const lwm2m_parameters_s* static_res)
MACRUM 0:119624335925 54 : M2MBase(static_res), _parent(parent)
MACRUM 0:119624335925 55 {
MACRUM 0:119624335925 56 M2MBase::set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
MACRUM 0:119624335925 57 M2MBase::set_operation(M2MBase::GET_ALLOWED);
MACRUM 0:119624335925 58 }
MACRUM 0:119624335925 59
MACRUM 0:119624335925 60 M2MObjectInstance::~M2MObjectInstance()
MACRUM 0:119624335925 61 {
MACRUM 0:119624335925 62 if(!_resource_list.empty()) {
MACRUM 0:119624335925 63 M2MResource* res = NULL;
MACRUM 0:119624335925 64 M2MResourceList::const_iterator it;
MACRUM 0:119624335925 65 it = _resource_list.begin();
MACRUM 0:119624335925 66 for (; it!=_resource_list.end(); it++ ) {
MACRUM 0:119624335925 67 //Free allocated memory for resources.
MACRUM 0:119624335925 68 res = *it;
MACRUM 0:119624335925 69 delete res;
MACRUM 0:119624335925 70 }
MACRUM 0:119624335925 71 _resource_list.clear();
MACRUM 0:119624335925 72 }
MACRUM 0:119624335925 73
MACRUM 0:119624335925 74 free_resources();
MACRUM 0:119624335925 75 }
MACRUM 0:119624335925 76
MACRUM 0:119624335925 77 // TBD, ResourceType to the base class struct?? TODO!
MACRUM 0:119624335925 78 M2MResource* M2MObjectInstance::create_static_resource(const lwm2m_parameters_s* static_res,
MACRUM 0:119624335925 79 M2MResourceInstance::ResourceType type)
MACRUM 0:119624335925 80 {
MACRUM 0:119624335925 81 tr_debug("M2MObjectInstance::create_static_resource(lwm2m_parameters_s resource_name %s)", static_res->identifier.name);
MACRUM 0:119624335925 82 M2MResource *res = NULL;
MACRUM 0:119624335925 83 if (validate_string_length(static_res->identifier.name, 1, MAX_ALLOWED_STRING_LENGTH) == false) {
MACRUM 0:119624335925 84 return res;
MACRUM 0:119624335925 85 }
MACRUM 0:119624335925 86 if(!resource(static_res->identifier.name)) {
MACRUM 0:119624335925 87 res = new M2MResource(*this, static_res, convert_resource_type(type));
MACRUM 0:119624335925 88 if(res) {
MACRUM 0:119624335925 89 res->add_observation_level(observation_level());
MACRUM 0:119624335925 90 //if (multiple_instance) {
MACRUM 0:119624335925 91 //res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
MACRUM 0:119624335925 92 //}
MACRUM 0:119624335925 93 _resource_list.push_back(res);
MACRUM 0:119624335925 94 }
MACRUM 0:119624335925 95 }
MACRUM 0:119624335925 96 return res;
MACRUM 0:119624335925 97 }
MACRUM 0:119624335925 98
MACRUM 0:119624335925 99 M2MResource* M2MObjectInstance::create_static_resource(const String &resource_name,
MACRUM 0:119624335925 100 const String &resource_type,
MACRUM 0:119624335925 101 M2MResourceInstance::ResourceType type,
MACRUM 0:119624335925 102 const uint8_t *value,
MACRUM 0:119624335925 103 const uint8_t value_length,
MACRUM 0:119624335925 104 bool multiple_instance,
MACRUM 0:119624335925 105 bool external_blockwise_store)
MACRUM 0:119624335925 106 {
MACRUM 0:119624335925 107 tr_debug("M2MObjectInstance::create_static_resource(resource_name %s)",resource_name.c_str());
MACRUM 0:119624335925 108 M2MResource *res = NULL;
MACRUM 0:119624335925 109 if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) {
MACRUM 0:119624335925 110 return res;
MACRUM 0:119624335925 111 }
MACRUM 0:119624335925 112 if(!resource(resource_name)) {
MACRUM 0:119624335925 113 char *path = create_path(*this, resource_name.c_str());
MACRUM 0:119624335925 114
MACRUM 0:119624335925 115 if (path) {
MACRUM 0:119624335925 116 res = new M2MResource(*this, resource_name, M2MBase::Static, resource_type, convert_resource_type(type),
MACRUM 0:119624335925 117 value, value_length, path,
MACRUM 0:119624335925 118 multiple_instance, external_blockwise_store);
MACRUM 0:119624335925 119 if(res) {
MACRUM 0:119624335925 120 res->add_observation_level(observation_level());
MACRUM 0:119624335925 121 if (multiple_instance) {
MACRUM 0:119624335925 122 res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
MACRUM 0:119624335925 123 }
MACRUM 0:119624335925 124 _resource_list.push_back(res);
MACRUM 0:119624335925 125 }
MACRUM 0:119624335925 126 }
MACRUM 0:119624335925 127 }
MACRUM 0:119624335925 128 return res;
MACRUM 0:119624335925 129 }
MACRUM 0:119624335925 130
MACRUM 0:119624335925 131 M2MResource* M2MObjectInstance::create_dynamic_resource(const lwm2m_parameters_s* static_res,
MACRUM 0:119624335925 132 M2MResourceInstance::ResourceType type,
MACRUM 0:119624335925 133 bool observable)
MACRUM 0:119624335925 134 {
MACRUM 0:119624335925 135 tr_debug("M2MObjectInstance::create_dynamic_resource(resource_name %s)", static_res->identifier.name);
MACRUM 0:119624335925 136 M2MResource *res = NULL;
MACRUM 0:119624335925 137
MACRUM 0:119624335925 138 if (validate_string_length(static_res->identifier.name, 1, MAX_ALLOWED_STRING_LENGTH) == false) {
MACRUM 0:119624335925 139 return res;
MACRUM 0:119624335925 140 }
MACRUM 0:119624335925 141 if(!resource(static_res->identifier.name)) {
MACRUM 0:119624335925 142 res = new M2MResource(*this, static_res, convert_resource_type(type));
MACRUM 0:119624335925 143 if(res) {
MACRUM 0:119624335925 144 //if (multiple_instance) { // TODO!
MACRUM 0:119624335925 145 // res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE);
MACRUM 0:119624335925 146 //}
MACRUM 0:119624335925 147 res->add_observation_level(observation_level());
MACRUM 0:119624335925 148 _resource_list.push_back(res);
MACRUM 0:119624335925 149 }
MACRUM 0:119624335925 150 }
MACRUM 0:119624335925 151 return res;
MACRUM 0:119624335925 152 }
MACRUM 0:119624335925 153
MACRUM 0:119624335925 154 M2MResource* M2MObjectInstance::create_dynamic_resource(const String &resource_name,
MACRUM 0:119624335925 155 const String &resource_type,
MACRUM 0:119624335925 156 M2MResourceInstance::ResourceType type,
MACRUM 0:119624335925 157 bool observable,
MACRUM 0:119624335925 158 bool multiple_instance,
MACRUM 0:119624335925 159 bool external_blockwise_store)
MACRUM 0:119624335925 160 {
MACRUM 0:119624335925 161 tr_debug("M2MObjectInstance::create_dynamic_resource(resource_name %s)",resource_name.c_str());
MACRUM 0:119624335925 162 M2MResource *res = NULL;
MACRUM 0:119624335925 163 if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) {
MACRUM 0:119624335925 164 return res;
MACRUM 0:119624335925 165 }
MACRUM 0:119624335925 166 if(!resource(resource_name)) {
MACRUM 0:119624335925 167 char *path = create_path(*this, resource_name.c_str());
MACRUM 0:119624335925 168 if (path) {
MACRUM 0:119624335925 169 res = new M2MResource(*this, resource_name, M2MBase::Dynamic, resource_type, convert_resource_type(type),
MACRUM 0:119624335925 170 observable, path,
MACRUM 0:119624335925 171 multiple_instance, external_blockwise_store);
MACRUM 0:119624335925 172 if(res) {
MACRUM 0:119624335925 173 if (multiple_instance) {
MACRUM 0:119624335925 174 res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
MACRUM 0:119624335925 175 }
MACRUM 0:119624335925 176 res->add_observation_level(observation_level());
MACRUM 0:119624335925 177 _resource_list.push_back(res);
MACRUM 0:119624335925 178 }
MACRUM 0:119624335925 179 }
MACRUM 0:119624335925 180 }
MACRUM 0:119624335925 181 return res;
MACRUM 0:119624335925 182 }
MACRUM 0:119624335925 183
MACRUM 0:119624335925 184 M2MResourceInstance* M2MObjectInstance::create_static_resource_instance(const String &resource_name,
MACRUM 0:119624335925 185 const String &resource_type,
MACRUM 0:119624335925 186 M2MResourceInstance::ResourceType type,
MACRUM 0:119624335925 187 const uint8_t *value,
MACRUM 0:119624335925 188 const uint8_t value_length,
MACRUM 0:119624335925 189 uint16_t instance_id,
MACRUM 0:119624335925 190 bool external_blockwise_store)
MACRUM 0:119624335925 191 {
MACRUM 0:119624335925 192 tr_debug("M2MObjectInstance::create_static_resource_instance(resource_name %s)",resource_name.c_str());
MACRUM 0:119624335925 193 M2MResourceInstance *instance = NULL;
MACRUM 0:119624335925 194 if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) {
MACRUM 0:119624335925 195
MACRUM 0:119624335925 196 return instance;
MACRUM 0:119624335925 197 }
MACRUM 0:119624335925 198 M2MResource *res = resource(resource_name);
MACRUM 0:119624335925 199 if(!res) {
MACRUM 0:119624335925 200 char *path = create_path(*this, resource_name.c_str());
MACRUM 0:119624335925 201 if (path) {
MACRUM 0:119624335925 202 res = new M2MResource(*this, resource_name, M2MBase::Static, resource_type, convert_resource_type(type),
MACRUM 0:119624335925 203 value, value_length, path,
MACRUM 0:119624335925 204 true, external_blockwise_store);
MACRUM 0:119624335925 205 _resource_list.push_back(res);
MACRUM 0:119624335925 206 res->set_operation(M2MBase::GET_ALLOWED);
MACRUM 0:119624335925 207 res->set_observable(false);
MACRUM 0:119624335925 208 res->set_register_uri(false);
MACRUM 0:119624335925 209 }
MACRUM 0:119624335925 210 }
MACRUM 0:119624335925 211 if(res && res->supports_multiple_instances()&& (res->resource_instance(instance_id) == NULL)) {
MACRUM 0:119624335925 212 char *path = M2MBase::create_path(*res, instance_id);
MACRUM 0:119624335925 213 if (path) {
MACRUM 0:119624335925 214 instance = new M2MResourceInstance(*res, "", M2MBase::Static, resource_type, convert_resource_type(type),
MACRUM 0:119624335925 215 value, value_length,
MACRUM 0:119624335925 216 path, external_blockwise_store,true);
MACRUM 0:119624335925 217 if(instance) {
MACRUM 0:119624335925 218 instance->set_operation(M2MBase::GET_ALLOWED);
MACRUM 0:119624335925 219 instance->set_instance_id(instance_id);
MACRUM 0:119624335925 220 res->add_resource_instance(instance);
MACRUM 0:119624335925 221 }
MACRUM 0:119624335925 222 }
MACRUM 0:119624335925 223 }
MACRUM 0:119624335925 224 return instance;
MACRUM 0:119624335925 225 }
MACRUM 0:119624335925 226
MACRUM 0:119624335925 227 M2MResourceInstance* M2MObjectInstance::create_dynamic_resource_instance(const String &resource_name,
MACRUM 0:119624335925 228 const String &resource_type,
MACRUM 0:119624335925 229 M2MResourceInstance::ResourceType type,
MACRUM 0:119624335925 230 bool observable,
MACRUM 0:119624335925 231 uint16_t instance_id,
MACRUM 0:119624335925 232 bool external_blockwise_store)
MACRUM 0:119624335925 233 {
MACRUM 0:119624335925 234 tr_debug("M2MObjectInstance::create_dynamic_resource_instance(resource_name %s)",resource_name.c_str());
MACRUM 0:119624335925 235 M2MResourceInstance *instance = NULL;
MACRUM 0:119624335925 236 if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) {
MACRUM 0:119624335925 237 return instance;
MACRUM 0:119624335925 238 }
MACRUM 0:119624335925 239 M2MResource *res = resource(resource_name);
MACRUM 0:119624335925 240 if(!res) {
MACRUM 0:119624335925 241 char *path = create_path(*this, resource_name.c_str());
MACRUM 0:119624335925 242 if (path) {
MACRUM 0:119624335925 243 res = new M2MResource(*this, resource_name, M2MBase::Dynamic, resource_type, convert_resource_type(type),
MACRUM 0:119624335925 244 false, path, true, external_blockwise_store);
MACRUM 0:119624335925 245 _resource_list.push_back(res);
MACRUM 0:119624335925 246 res->set_register_uri(false);
MACRUM 0:119624335925 247 res->set_operation(M2MBase::GET_ALLOWED);
MACRUM 0:119624335925 248 }
MACRUM 0:119624335925 249 }
MACRUM 0:119624335925 250 if (res && res->supports_multiple_instances() && (res->resource_instance(instance_id) == NULL)) {
MACRUM 0:119624335925 251 char *path = create_path(*res, instance_id);
MACRUM 0:119624335925 252 if (path) {
MACRUM 0:119624335925 253 instance = new M2MResourceInstance(*res, "", M2MBase::Dynamic, resource_type, convert_resource_type(type),
MACRUM 0:119624335925 254 path, external_blockwise_store,true);
MACRUM 0:119624335925 255 if(instance) {
MACRUM 0:119624335925 256 instance->set_operation(M2MBase::GET_ALLOWED);
MACRUM 0:119624335925 257 instance->set_observable(observable);
MACRUM 0:119624335925 258 instance->set_instance_id(instance_id);
MACRUM 0:119624335925 259 res->add_resource_instance(instance);
MACRUM 0:119624335925 260 }
MACRUM 0:119624335925 261 }
MACRUM 0:119624335925 262 }
MACRUM 0:119624335925 263 return instance;
MACRUM 0:119624335925 264 }
MACRUM 0:119624335925 265
MACRUM 0:119624335925 266 bool M2MObjectInstance::remove_resource(const String &resource_name)
MACRUM 0:119624335925 267 {
MACRUM 0:119624335925 268 return remove_resource(resource_name.c_str());
MACRUM 0:119624335925 269 }
MACRUM 0:119624335925 270
MACRUM 0:119624335925 271 bool M2MObjectInstance::remove_resource(const char *resource_name)
MACRUM 0:119624335925 272 {
MACRUM 0:119624335925 273 tr_debug("M2MObjectInstance::remove_resource(resource_name %s)", resource_name);
MACRUM 0:119624335925 274
MACRUM 0:119624335925 275 bool success = false;
MACRUM 0:119624335925 276 if(!_resource_list.empty()) {
MACRUM 0:119624335925 277 M2MResource* res = NULL;
MACRUM 0:119624335925 278 M2MResourceList::const_iterator it;
MACRUM 0:119624335925 279 it = _resource_list.begin();
MACRUM 0:119624335925 280 int pos = 0;
MACRUM 0:119624335925 281 for ( ; it != _resource_list.end(); it++, pos++ ) {
MACRUM 0:119624335925 282 if(strcmp((*it)->name(), resource_name) == 0) {
MACRUM 0:119624335925 283 // Resource found and deleted.
MACRUM 0:119624335925 284 res = *it;
MACRUM 0:119624335925 285 delete res;
MACRUM 0:119624335925 286 _resource_list.erase(pos);
MACRUM 0:119624335925 287 success = true;
MACRUM 0:119624335925 288 break;
MACRUM 0:119624335925 289 }
MACRUM 0:119624335925 290 }
MACRUM 0:119624335925 291 }
MACRUM 0:119624335925 292 return success;
MACRUM 0:119624335925 293 }
MACRUM 0:119624335925 294
MACRUM 0:119624335925 295 bool M2MObjectInstance::remove_resource_instance(const String &resource_name,
MACRUM 0:119624335925 296 uint16_t inst_id)
MACRUM 0:119624335925 297 {
MACRUM 0:119624335925 298 tr_debug("M2MObjectInstance::remove_resource_instance(resource_name %s inst_id %d)",
MACRUM 0:119624335925 299 resource_name.c_str(), inst_id);
MACRUM 0:119624335925 300 bool success = false;
MACRUM 0:119624335925 301 M2MResource *res = resource(resource_name);
MACRUM 0:119624335925 302 if(res) {
MACRUM 0:119624335925 303 M2MResourceInstanceList list = res->resource_instances();
MACRUM 0:119624335925 304 M2MResourceInstanceList::const_iterator it;
MACRUM 0:119624335925 305 it = list.begin();
MACRUM 0:119624335925 306 for ( ; it != list.end(); it++) {
MACRUM 0:119624335925 307 if((*it)->instance_id() == inst_id) {
MACRUM 0:119624335925 308 success = res->remove_resource_instance(inst_id);
MACRUM 0:119624335925 309 if(res->resource_instance_count() == 0) {
MACRUM 0:119624335925 310 M2MResourceList::const_iterator itr;
MACRUM 0:119624335925 311 itr = _resource_list.begin();
MACRUM 0:119624335925 312 int pos = 0;
MACRUM 0:119624335925 313 for ( ; itr != _resource_list.end(); itr++, pos++ ) {
MACRUM 0:119624335925 314 if(strcmp((*itr)->name(),resource_name.c_str()) == 0) {
MACRUM 0:119624335925 315 delete res;
MACRUM 0:119624335925 316 _resource_list.erase(pos);
MACRUM 0:119624335925 317 break;
MACRUM 0:119624335925 318 }
MACRUM 0:119624335925 319 }
MACRUM 0:119624335925 320 }
MACRUM 0:119624335925 321 break;
MACRUM 0:119624335925 322 }
MACRUM 0:119624335925 323 }
MACRUM 0:119624335925 324 }
MACRUM 0:119624335925 325 return success;
MACRUM 0:119624335925 326 }
MACRUM 0:119624335925 327
MACRUM 0:119624335925 328 M2MResource* M2MObjectInstance::resource(const String &resource_name) const
MACRUM 0:119624335925 329 {
MACRUM 0:119624335925 330 return resource(resource_name.c_str());
MACRUM 0:119624335925 331 }
MACRUM 0:119624335925 332
MACRUM 0:119624335925 333 M2MResource* M2MObjectInstance::resource(const char *resource_name) const
MACRUM 0:119624335925 334 {
MACRUM 0:119624335925 335 M2MResource *res = NULL;
MACRUM 0:119624335925 336 if(!_resource_list.empty()) {
MACRUM 0:119624335925 337 M2MResourceList::const_iterator it;
MACRUM 0:119624335925 338 it = _resource_list.begin();
MACRUM 0:119624335925 339 for (; it!=_resource_list.end(); it++ ) {
MACRUM 0:119624335925 340 if(strcmp((*it)->name(), resource_name) == 0) {
MACRUM 0:119624335925 341 res = *it;
MACRUM 0:119624335925 342 break;
MACRUM 0:119624335925 343 }
MACRUM 0:119624335925 344 }
MACRUM 0:119624335925 345 }
MACRUM 0:119624335925 346 return res;
MACRUM 0:119624335925 347 }
MACRUM 0:119624335925 348
MACRUM 0:119624335925 349 const M2MResourceList& M2MObjectInstance::resources() const
MACRUM 0:119624335925 350 {
MACRUM 0:119624335925 351 return _resource_list;
MACRUM 0:119624335925 352 }
MACRUM 0:119624335925 353
MACRUM 0:119624335925 354 uint16_t M2MObjectInstance::resource_count() const
MACRUM 0:119624335925 355 {
MACRUM 0:119624335925 356 uint16_t count = 0;
MACRUM 0:119624335925 357 if(!_resource_list.empty()) {
MACRUM 0:119624335925 358 M2MResourceList::const_iterator it;
MACRUM 0:119624335925 359 it = _resource_list.begin();
MACRUM 0:119624335925 360 for ( ; it != _resource_list.end(); it++ ) {
MACRUM 0:119624335925 361 if((*it)->supports_multiple_instances()) {
MACRUM 0:119624335925 362 count += (*it)->resource_instance_count();
MACRUM 0:119624335925 363 } else {
MACRUM 0:119624335925 364 count++;
MACRUM 0:119624335925 365 }
MACRUM 0:119624335925 366 }
MACRUM 0:119624335925 367 }
MACRUM 0:119624335925 368 return count;
MACRUM 0:119624335925 369 }
MACRUM 0:119624335925 370
MACRUM 0:119624335925 371 uint16_t M2MObjectInstance::resource_count(const String& resource) const
MACRUM 0:119624335925 372 {
MACRUM 0:119624335925 373
MACRUM 0:119624335925 374 return resource_count(resource.c_str());
MACRUM 0:119624335925 375 }
MACRUM 0:119624335925 376
MACRUM 0:119624335925 377 uint16_t M2MObjectInstance::resource_count(const char *resource) const
MACRUM 0:119624335925 378 {
MACRUM 0:119624335925 379 uint16_t count = 0;
MACRUM 0:119624335925 380 if(!_resource_list.empty()) {
MACRUM 0:119624335925 381 M2MResourceList::const_iterator it;
MACRUM 0:119624335925 382 it = _resource_list.begin();
MACRUM 0:119624335925 383 for ( ; it != _resource_list.end(); it++ ) {
MACRUM 0:119624335925 384 if(strcmp((*it)->name(), resource) == 0) {
MACRUM 0:119624335925 385 if((*it)->supports_multiple_instances()) {
MACRUM 0:119624335925 386 count += (*it)->resource_instance_count();
MACRUM 0:119624335925 387 } else {
MACRUM 0:119624335925 388 count++;
MACRUM 0:119624335925 389 }
MACRUM 0:119624335925 390 }
MACRUM 0:119624335925 391 }
MACRUM 0:119624335925 392 }
MACRUM 0:119624335925 393 return count;
MACRUM 0:119624335925 394 }
MACRUM 0:119624335925 395
MACRUM 0:119624335925 396 M2MObservationHandler* M2MObjectInstance::observation_handler() const
MACRUM 0:119624335925 397 {
MACRUM 0:119624335925 398 // XXX: need to check the flag too
MACRUM 0:119624335925 399 return _parent.observation_handler();
MACRUM 0:119624335925 400 }
MACRUM 0:119624335925 401
MACRUM 0:119624335925 402 void M2MObjectInstance::set_observation_handler(M2MObservationHandler *handler)
MACRUM 0:119624335925 403 {
MACRUM 0:119624335925 404 // XXX: need to set the flag too
MACRUM 0:119624335925 405 _parent.set_observation_handler(handler);
MACRUM 0:119624335925 406 }
MACRUM 0:119624335925 407
MACRUM 0:119624335925 408 void M2MObjectInstance::add_observation_level(M2MBase::Observation observation_level)
MACRUM 0:119624335925 409 {
MACRUM 0:119624335925 410 M2MBase::add_observation_level(observation_level);
MACRUM 0:119624335925 411 if(!_resource_list.empty()) {
MACRUM 0:119624335925 412 M2MResourceList::const_iterator it;
MACRUM 0:119624335925 413 it = _resource_list.begin();
MACRUM 0:119624335925 414 for ( ; it != _resource_list.end(); it++ ) {
MACRUM 0:119624335925 415 (*it)->add_observation_level(observation_level);
MACRUM 0:119624335925 416 }
MACRUM 0:119624335925 417 }
MACRUM 0:119624335925 418 }
MACRUM 0:119624335925 419
MACRUM 0:119624335925 420 void M2MObjectInstance::remove_observation_level(M2MBase::Observation observation_level)
MACRUM 0:119624335925 421 {
MACRUM 0:119624335925 422 M2MBase::remove_observation_level(observation_level);
MACRUM 0:119624335925 423 if(!_resource_list.empty()) {
MACRUM 0:119624335925 424 M2MResourceList::const_iterator it;
MACRUM 0:119624335925 425 it = _resource_list.begin();
MACRUM 0:119624335925 426 for ( ; it != _resource_list.end(); it++ ) {
MACRUM 0:119624335925 427 (*it)->remove_observation_level(observation_level);
MACRUM 0:119624335925 428 }
MACRUM 0:119624335925 429 }
MACRUM 0:119624335925 430 }
MACRUM 0:119624335925 431
MACRUM 0:119624335925 432 sn_coap_hdr_s* M2MObjectInstance::handle_get_request(nsdl_s *nsdl,
MACRUM 0:119624335925 433 sn_coap_hdr_s *received_coap_header,
MACRUM 0:119624335925 434 M2MObservationHandler *observation_handler)
MACRUM 0:119624335925 435 {
MACRUM 0:119624335925 436 tr_info("M2MObjectInstance::handle_get_request()");
MACRUM 0:119624335925 437 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
MACRUM 0:119624335925 438 sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
MACRUM 0:119624335925 439 received_coap_header,
MACRUM 0:119624335925 440 msg_code);
MACRUM 0:119624335925 441 uint8_t * data = NULL;
MACRUM 0:119624335925 442 uint32_t data_length = 0;
MACRUM 0:119624335925 443
MACRUM 0:119624335925 444 if (received_coap_header) {
MACRUM 0:119624335925 445 // process the GET if we have registered a callback for it
MACRUM 0:119624335925 446 if ((operation() & SN_GRS_GET_ALLOWED) != 0) {
MACRUM 0:119624335925 447 if (coap_response) {
MACRUM 0:119624335925 448 bool content_type_present = false;
MACRUM 0:119624335925 449 bool is_content_type_supported = true;
MACRUM 0:119624335925 450
MACRUM 0:119624335925 451 if (received_coap_header->options_list_ptr &&
MACRUM 0:119624335925 452 received_coap_header->options_list_ptr->accept != COAP_CT_NONE) {
MACRUM 0:119624335925 453 content_type_present = true;
MACRUM 0:119624335925 454 coap_response->content_format = received_coap_header->options_list_ptr->accept;
MACRUM 0:119624335925 455
MACRUM 0:119624335925 456 }
MACRUM 0:119624335925 457
MACRUM 0:119624335925 458 // Check if preferred content type is supported
MACRUM 0:119624335925 459 if (content_type_present) {
MACRUM 0:119624335925 460 if (coap_response->content_format != COAP_CONTENT_OMA_TLV_TYPE_OLD &&
MACRUM 0:119624335925 461 coap_response->content_format != COAP_CONTENT_OMA_TLV_TYPE) {
MACRUM 0:119624335925 462 is_content_type_supported = false;
MACRUM 0:119624335925 463 }
MACRUM 0:119624335925 464 }
MACRUM 0:119624335925 465
MACRUM 0:119624335925 466 if (is_content_type_supported) {
MACRUM 0:119624335925 467 if (!content_type_present &&
MACRUM 0:119624335925 468 (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE ||
MACRUM 0:119624335925 469 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) {
MACRUM 0:119624335925 470 coap_response->content_format = sn_coap_content_format_e(M2MBase::coap_content_type());
MACRUM 0:119624335925 471 }
MACRUM 0:119624335925 472
MACRUM 0:119624335925 473 // fill in the CoAP response payload
MACRUM 0:119624335925 474 if (COAP_CONTENT_OMA_TLV_TYPE == coap_response->content_format ||
MACRUM 0:119624335925 475 COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_response->content_format) {
MACRUM 0:119624335925 476 set_coap_content_type(coap_response->content_format);
MACRUM 0:119624335925 477 data = M2MTLVSerializer::serialize(_resource_list, data_length);
MACRUM 0:119624335925 478 }
MACRUM 0:119624335925 479
MACRUM 0:119624335925 480 coap_response->payload_len = data_length;
MACRUM 0:119624335925 481 coap_response->payload_ptr = data;
MACRUM 0:119624335925 482
MACRUM 0:119624335925 483 if (data) {
MACRUM 0:119624335925 484 coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response);
MACRUM 0:119624335925 485 if (coap_response->options_list_ptr) {
MACRUM 0:119624335925 486 coap_response->options_list_ptr->max_age = max_age();
MACRUM 0:119624335925 487 }
MACRUM 0:119624335925 488
MACRUM 0:119624335925 489 if (received_coap_header->options_list_ptr) {
MACRUM 0:119624335925 490 if (received_coap_header->options_list_ptr->observe != -1) {
MACRUM 0:119624335925 491 if (is_observable()) {
MACRUM 0:119624335925 492 uint32_t number = 0;
MACRUM 0:119624335925 493 uint8_t observe_option = 0;
MACRUM 0:119624335925 494 observe_option = received_coap_header->options_list_ptr->observe;
MACRUM 0:119624335925 495 if (START_OBSERVATION == observe_option) {
MACRUM 0:119624335925 496 // If the observe length is 0 means register for observation.
MACRUM 0:119624335925 497 if (received_coap_header->options_list_ptr->observe != -1) {
MACRUM 0:119624335925 498 number = received_coap_header->options_list_ptr->observe;
MACRUM 0:119624335925 499 }
MACRUM 0:119624335925 500
MACRUM 0:119624335925 501 // If the observe value is 0 means register for observation.
MACRUM 0:119624335925 502 if (number == 0) {
MACRUM 0:119624335925 503 tr_info("M2MObjectInstance::handle_get_request - put resource under observation");
MACRUM 0:119624335925 504 set_under_observation(true,observation_handler);
MACRUM 0:119624335925 505 add_observation_level(M2MBase::OI_Attribute);
MACRUM 0:119624335925 506 send_notification_delivery_status(*this, NOTIFICATION_STATUS_SUBSCRIBED);
MACRUM 0:119624335925 507 if (coap_response->options_list_ptr) {
MACRUM 0:119624335925 508 coap_response->options_list_ptr->observe = observation_number();
MACRUM 0:119624335925 509 }
MACRUM 0:119624335925 510 }
MACRUM 0:119624335925 511
MACRUM 0:119624335925 512 if (received_coap_header->token_ptr) {
MACRUM 0:119624335925 513 set_observation_token(received_coap_header->token_ptr,
MACRUM 0:119624335925 514 received_coap_header->token_len);
MACRUM 0:119624335925 515 }
MACRUM 0:119624335925 516
MACRUM 0:119624335925 517 } else if (STOP_OBSERVATION == observe_option) {
MACRUM 0:119624335925 518 tr_info("M2MObjectInstance::handle_get_request - stops observation");
MACRUM 0:119624335925 519 set_under_observation(false,NULL);
MACRUM 0:119624335925 520 remove_observation_level(M2MBase::OI_Attribute);
MACRUM 0:119624335925 521 send_notification_delivery_status(*this, NOTIFICATION_STATUS_UNSUBSCRIBED);
MACRUM 0:119624335925 522 }
MACRUM 0:119624335925 523 msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
MACRUM 0:119624335925 524 }
MACRUM 0:119624335925 525 else {
MACRUM 0:119624335925 526 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 527 }
MACRUM 0:119624335925 528 }
MACRUM 0:119624335925 529 }
MACRUM 0:119624335925 530 } else {
MACRUM 0:119624335925 531 msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; // Content format not supported
MACRUM 0:119624335925 532 }
MACRUM 0:119624335925 533 } else {
MACRUM 0:119624335925 534 tr_error("M2MObjectInstance::handle_get_request() - Content-type: %d not supported", coap_response->content_format);
MACRUM 0:119624335925 535 msg_code = COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE;
MACRUM 0:119624335925 536 }
MACRUM 0:119624335925 537 }
MACRUM 0:119624335925 538 } else {
MACRUM 0:119624335925 539 tr_error("M2MObjectInstance::handle_get_request - Return COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
MACRUM 0:119624335925 540 // Operation is not allowed.
MACRUM 0:119624335925 541 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 542 }
MACRUM 0:119624335925 543 } else {
MACRUM 0:119624335925 544 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 545 }
MACRUM 0:119624335925 546 if(coap_response) {
MACRUM 0:119624335925 547 coap_response->msg_code = msg_code;
MACRUM 0:119624335925 548 }
MACRUM 0:119624335925 549 return coap_response;
MACRUM 0:119624335925 550 }
MACRUM 0:119624335925 551
MACRUM 0:119624335925 552 sn_coap_hdr_s* M2MObjectInstance::handle_put_request(nsdl_s *nsdl,
MACRUM 0:119624335925 553 sn_coap_hdr_s *received_coap_header,
MACRUM 0:119624335925 554 M2MObservationHandler *observation_handler,
MACRUM 0:119624335925 555 bool &/*execute_value_updated*/)
MACRUM 0:119624335925 556 {
MACRUM 0:119624335925 557 tr_info("M2MObjectInstance::handle_put_request()");
MACRUM 0:119624335925 558 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
MACRUM 0:119624335925 559 sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
MACRUM 0:119624335925 560 received_coap_header,
MACRUM 0:119624335925 561 msg_code);;
MACRUM 0:119624335925 562 if(received_coap_header) {
MACRUM 0:119624335925 563 uint16_t coap_content_type = 0;
MACRUM 0:119624335925 564 bool content_type_present = false;
MACRUM 0:119624335925 565
MACRUM 0:119624335925 566 if(received_coap_header->content_format != COAP_CT_NONE) {
MACRUM 0:119624335925 567 content_type_present = true;
MACRUM 0:119624335925 568 set_coap_content_type(received_coap_header->content_format);
MACRUM 0:119624335925 569 if(coap_response) {
MACRUM 0:119624335925 570 coap_content_type = received_coap_header->content_format;
MACRUM 0:119624335925 571 }
MACRUM 0:119624335925 572 }
MACRUM 0:119624335925 573 if(received_coap_header->options_list_ptr &&
MACRUM 0:119624335925 574 received_coap_header->options_list_ptr->uri_query_ptr) {
MACRUM 0:119624335925 575 char *query = (char*)alloc_string_copy(received_coap_header->options_list_ptr->uri_query_ptr,
MACRUM 0:119624335925 576 received_coap_header->options_list_ptr->uri_query_len);
MACRUM 0:119624335925 577 if (query){
MACRUM 0:119624335925 578 tr_info("M2MObjectInstance::handle_put_request() - query %s", query);
MACRUM 0:119624335925 579 // if anything was updated, re-initialize the stored notification attributes
MACRUM 0:119624335925 580 if (!handle_observation_attribute(query)){
MACRUM 0:119624335925 581 tr_debug("M2MObjectInstance::handle_put_request() - Invalid query");
MACRUM 0:119624335925 582 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
MACRUM 0:119624335925 583 } else {
MACRUM 0:119624335925 584 msg_code =COAP_MSG_CODE_RESPONSE_CHANGED;
MACRUM 0:119624335925 585 }
MACRUM 0:119624335925 586 free(query);
MACRUM 0:119624335925 587 }
MACRUM 0:119624335925 588 } else if ((operation() & SN_GRS_PUT_ALLOWED) != 0) {
MACRUM 0:119624335925 589 if(!content_type_present &&
MACRUM 0:119624335925 590 (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE ||
MACRUM 0:119624335925 591 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) {
MACRUM 0:119624335925 592 coap_content_type = M2MBase::coap_content_type();
MACRUM 0:119624335925 593 }
MACRUM 0:119624335925 594
MACRUM 0:119624335925 595 tr_debug("M2MObjectInstance::handle_put_request() - Request Content-type: %d", coap_content_type);
MACRUM 0:119624335925 596
MACRUM 0:119624335925 597 if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type ||
MACRUM 0:119624335925 598 COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_content_type ) {
MACRUM 0:119624335925 599 set_coap_content_type(coap_content_type);
MACRUM 0:119624335925 600 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
MACRUM 0:119624335925 601 if(received_coap_header->payload_ptr) {
MACRUM 0:119624335925 602 error = M2MTLVDeserializer::deserialize_resources(
MACRUM 0:119624335925 603 received_coap_header->payload_ptr,
MACRUM 0:119624335925 604 received_coap_header->payload_len, *this,
MACRUM 0:119624335925 605 M2MTLVDeserializer::Put);
MACRUM 0:119624335925 606 switch(error) {
MACRUM 0:119624335925 607 case M2MTLVDeserializer::None:
MACRUM 0:119624335925 608 if(observation_handler) {
MACRUM 0:119624335925 609 observation_handler->value_updated(this);
MACRUM 0:119624335925 610 }
MACRUM 0:119624335925 611 msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
MACRUM 0:119624335925 612 break;
MACRUM 0:119624335925 613 case M2MTLVDeserializer::NotFound:
MACRUM 0:119624335925 614 msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
MACRUM 0:119624335925 615 break;
MACRUM 0:119624335925 616 case M2MTLVDeserializer::NotAllowed:
MACRUM 0:119624335925 617 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 618 break;
MACRUM 0:119624335925 619 case M2MTLVDeserializer::NotValid:
MACRUM 0:119624335925 620 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
MACRUM 0:119624335925 621 break;
MACRUM 0:119624335925 622 case M2MTLVDeserializer::OutOfMemory:
MACRUM 0:119624335925 623 msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
MACRUM 0:119624335925 624 break;
MACRUM 0:119624335925 625 }
MACRUM 0:119624335925 626 }
MACRUM 0:119624335925 627 } else {
MACRUM 0:119624335925 628 msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
MACRUM 0:119624335925 629 } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type)
MACRUM 0:119624335925 630 } else {
MACRUM 0:119624335925 631 // Operation is not allowed.
MACRUM 0:119624335925 632 tr_error("M2MObjectInstance::handle_put_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
MACRUM 0:119624335925 633 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 634 }
MACRUM 0:119624335925 635 } else {
MACRUM 0:119624335925 636 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 637 }
MACRUM 0:119624335925 638 if(coap_response) {
MACRUM 0:119624335925 639 coap_response->msg_code = msg_code;
MACRUM 0:119624335925 640 }
MACRUM 0:119624335925 641 return coap_response;
MACRUM 0:119624335925 642 }
MACRUM 0:119624335925 643
MACRUM 0:119624335925 644 sn_coap_hdr_s* M2MObjectInstance::handle_post_request(nsdl_s *nsdl,
MACRUM 0:119624335925 645 sn_coap_hdr_s *received_coap_header,
MACRUM 0:119624335925 646 M2MObservationHandler *observation_handler,
MACRUM 0:119624335925 647 bool &execute_value_updated,
MACRUM 0:119624335925 648 sn_nsdl_addr_s *)
MACRUM 0:119624335925 649 {
MACRUM 0:119624335925 650 tr_info("M2MObjectInstance::handle_post_request()");
MACRUM 0:119624335925 651 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
MACRUM 0:119624335925 652 sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
MACRUM 0:119624335925 653 received_coap_header,
MACRUM 0:119624335925 654 msg_code);
MACRUM 0:119624335925 655 if(received_coap_header) {
MACRUM 0:119624335925 656 if ((operation() & SN_GRS_POST_ALLOWED) != 0) {
MACRUM 0:119624335925 657 uint16_t coap_content_type = 0;
MACRUM 0:119624335925 658 bool content_type_present = false;
MACRUM 0:119624335925 659 if(received_coap_header->content_format != COAP_CT_NONE) {
MACRUM 0:119624335925 660 set_coap_content_type(received_coap_header->content_format);
MACRUM 0:119624335925 661 content_type_present = true;
MACRUM 0:119624335925 662 if(coap_response) {
MACRUM 0:119624335925 663 coap_content_type = received_coap_header->content_format;
MACRUM 0:119624335925 664 }
MACRUM 0:119624335925 665 }
MACRUM 0:119624335925 666 if(!content_type_present &&
MACRUM 0:119624335925 667 (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE ||
MACRUM 0:119624335925 668 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) {
MACRUM 0:119624335925 669 coap_content_type = M2MBase::coap_content_type();
MACRUM 0:119624335925 670 }
MACRUM 0:119624335925 671
MACRUM 0:119624335925 672 tr_debug("M2MObjectInstance::handle_post_request() - Request Content-type: %d", coap_content_type);
MACRUM 0:119624335925 673
MACRUM 0:119624335925 674 if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type ||
MACRUM 0:119624335925 675 COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_content_type) {
MACRUM 0:119624335925 676 set_coap_content_type(coap_content_type);
MACRUM 0:119624335925 677 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
MACRUM 0:119624335925 678 error = M2MTLVDeserializer::deserialize_resources(
MACRUM 0:119624335925 679 received_coap_header->payload_ptr,
MACRUM 0:119624335925 680 received_coap_header->payload_len, *this,
MACRUM 0:119624335925 681 M2MTLVDeserializer::Post);
MACRUM 0:119624335925 682
MACRUM 0:119624335925 683 uint16_t instance_id = M2MTLVDeserializer::instance_id(received_coap_header->payload_ptr);
MACRUM 0:119624335925 684 switch(error) {
MACRUM 0:119624335925 685 case M2MTLVDeserializer::None:
MACRUM 0:119624335925 686 if(observation_handler) {
MACRUM 0:119624335925 687 execute_value_updated = true;
MACRUM 0:119624335925 688 }
MACRUM 0:119624335925 689 coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response);
MACRUM 0:119624335925 690
MACRUM 0:119624335925 691 if (coap_response->options_list_ptr) {
MACRUM 0:119624335925 692
MACRUM 0:119624335925 693 StringBuffer<MAX_PATH_SIZE_3> obj_name;
MACRUM 0:119624335925 694 if(!build_path(obj_name, _parent.name(), M2MBase::instance_id(), instance_id)) {
MACRUM 0:119624335925 695 msg_code = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
MACRUM 0:119624335925 696 break;
MACRUM 0:119624335925 697 }
MACRUM 0:119624335925 698
MACRUM 0:119624335925 699 coap_response->options_list_ptr->location_path_len = obj_name.get_size();
MACRUM 0:119624335925 700 coap_response->options_list_ptr->location_path_ptr =
MACRUM 0:119624335925 701 alloc_string_copy((uint8_t*)obj_name.c_str(),
MACRUM 0:119624335925 702 coap_response->options_list_ptr->location_path_len);
MACRUM 0:119624335925 703 // todo: handle allocation error
MACRUM 0:119624335925 704 }
MACRUM 0:119624335925 705 msg_code = COAP_MSG_CODE_RESPONSE_CREATED;
MACRUM 0:119624335925 706 break;
MACRUM 0:119624335925 707 case M2MTLVDeserializer::NotAllowed:
MACRUM 0:119624335925 708 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 709 break;
MACRUM 0:119624335925 710 case M2MTLVDeserializer::NotValid:
MACRUM 0:119624335925 711 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
MACRUM 0:119624335925 712 break;
MACRUM 0:119624335925 713 case M2MTLVDeserializer::NotFound:
MACRUM 0:119624335925 714 msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
MACRUM 0:119624335925 715 break;
MACRUM 0:119624335925 716 case M2MTLVDeserializer::OutOfMemory:
MACRUM 0:119624335925 717 msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
MACRUM 0:119624335925 718 break;
MACRUM 0:119624335925 719 default:
MACRUM 0:119624335925 720 break;
MACRUM 0:119624335925 721 }
MACRUM 0:119624335925 722 } else {
MACRUM 0:119624335925 723 msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
MACRUM 0:119624335925 724 } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type)
MACRUM 0:119624335925 725 } else {
MACRUM 0:119624335925 726 // Operation is not allowed.
MACRUM 0:119624335925 727 tr_error("M2MObjectInstance::handle_post_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
MACRUM 0:119624335925 728 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 729 }
MACRUM 0:119624335925 730 } else {
MACRUM 0:119624335925 731 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
MACRUM 0:119624335925 732 }
MACRUM 0:119624335925 733 if(coap_response) {
MACRUM 0:119624335925 734 coap_response->msg_code = msg_code;
MACRUM 0:119624335925 735 }
MACRUM 0:119624335925 736 return coap_response;
MACRUM 0:119624335925 737 }
MACRUM 0:119624335925 738
MACRUM 0:119624335925 739 void M2MObjectInstance::notification_update(M2MBase::Observation observation_level)
MACRUM 0:119624335925 740 {
MACRUM 0:119624335925 741 tr_debug("M2MObjectInstance::notification_update() - level(%d)", observation_level);
MACRUM 0:119624335925 742 if((M2MBase::O_Attribute & observation_level) == M2MBase::O_Attribute) {
MACRUM 0:119624335925 743 tr_debug("M2MObjectInstance::notification_update() - object callback");
MACRUM 0:119624335925 744 _parent.notification_update(instance_id());
MACRUM 0:119624335925 745 }
MACRUM 0:119624335925 746 if((M2MBase::OI_Attribute & observation_level) == M2MBase::OI_Attribute) {
MACRUM 0:119624335925 747 tr_debug("M2MObjectInstance::notification_update() - object instance callback");
MACRUM 0:119624335925 748 M2MReportHandler *report_handler = M2MBase::report_handler();
MACRUM 0:119624335925 749 if(report_handler && is_under_observation()) {
MACRUM 0:119624335925 750 report_handler->set_notification_trigger();
MACRUM 0:119624335925 751 }
MACRUM 0:119624335925 752
MACRUM 0:119624335925 753 }
MACRUM 0:119624335925 754 }
MACRUM 0:119624335925 755
MACRUM 0:119624335925 756 M2MBase::DataType M2MObjectInstance::convert_resource_type(M2MResourceInstance::ResourceType type)
MACRUM 0:119624335925 757 {
MACRUM 0:119624335925 758 M2MBase::DataType data_type = M2MBase::OBJLINK;
MACRUM 0:119624335925 759 switch(type) {
MACRUM 0:119624335925 760 case M2MResourceInstance::STRING:
MACRUM 0:119624335925 761 data_type = M2MBase::STRING;
MACRUM 0:119624335925 762 break;
MACRUM 0:119624335925 763 case M2MResourceInstance::INTEGER:
MACRUM 0:119624335925 764 data_type = M2MBase::INTEGER;
MACRUM 0:119624335925 765 break;
MACRUM 0:119624335925 766 case M2MResourceInstance::FLOAT:
MACRUM 0:119624335925 767 data_type = M2MBase::FLOAT;
MACRUM 0:119624335925 768 break;
MACRUM 0:119624335925 769 case M2MResourceInstance::OPAQUE:
MACRUM 0:119624335925 770 data_type = M2MBase::OPAQUE;
MACRUM 0:119624335925 771 break;
MACRUM 0:119624335925 772 case M2MResourceInstance::BOOLEAN:
MACRUM 0:119624335925 773 data_type = M2MBase::BOOLEAN;
MACRUM 0:119624335925 774 break;
MACRUM 0:119624335925 775 case M2MResourceInstance::TIME:
MACRUM 0:119624335925 776 data_type = M2MBase::TIME;
MACRUM 0:119624335925 777 break;
MACRUM 0:119624335925 778 case M2MResourceInstance::OBJLINK:
MACRUM 0:119624335925 779 data_type = M2MBase::OBJLINK;
MACRUM 0:119624335925 780 break;
MACRUM 0:119624335925 781 }
MACRUM 0:119624335925 782 return data_type;
MACRUM 0:119624335925 783 }