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