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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:25fa8795676b 1 /*
leothedragon 0:25fa8795676b 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
leothedragon 0:25fa8795676b 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:25fa8795676b 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:25fa8795676b 5 * not use this file except in compliance with the License.
leothedragon 0:25fa8795676b 6 * You may obtain a copy of the License at
leothedragon 0:25fa8795676b 7 *
leothedragon 0:25fa8795676b 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:25fa8795676b 9 *
leothedragon 0:25fa8795676b 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:25fa8795676b 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:25fa8795676b 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:25fa8795676b 13 * See the License for the specific language governing permissions and
leothedragon 0:25fa8795676b 14 * limitations under the License.
leothedragon 0:25fa8795676b 15 */
leothedragon 0:25fa8795676b 16
leothedragon 0:25fa8795676b 17 // Note: this macro is needed on armcc to get the the PRI*32 macros
leothedragon 0:25fa8795676b 18 // from inttypes.h in a C++ code.
leothedragon 0:25fa8795676b 19 #ifndef __STDC_FORMAT_MACROS
leothedragon 0:25fa8795676b 20 #define __STDC_FORMAT_MACROS
leothedragon 0:25fa8795676b 21 #endif
leothedragon 0:25fa8795676b 22 #include <inttypes.h>
leothedragon 0:25fa8795676b 23
leothedragon 0:25fa8795676b 24 #include "mbed-client/m2mobject.h"
leothedragon 0:25fa8795676b 25 #include "mbed-client/m2mendpoint.h"
leothedragon 0:25fa8795676b 26 #include "mbed-client/m2mconstants.h"
leothedragon 0:25fa8795676b 27 #include "include/m2mtlvserializer.h"
leothedragon 0:25fa8795676b 28 #include "include/m2mtlvdeserializer.h"
leothedragon 0:25fa8795676b 29 #include "include/m2mreporthandler.h"
leothedragon 0:25fa8795676b 30 #include "mbed-trace/mbed_trace.h"
leothedragon 0:25fa8795676b 31 #include "mbed-client/m2mstringbuffer.h"
leothedragon 0:25fa8795676b 32 #include "include/m2mcallbackstorage.h"
leothedragon 0:25fa8795676b 33
leothedragon 0:25fa8795676b 34 #include <stdlib.h>
leothedragon 0:25fa8795676b 35
leothedragon 0:25fa8795676b 36 #define BUFFER_SIZE 10
leothedragon 0:25fa8795676b 37 #define TRACE_GROUP "mClt"
leothedragon 0:25fa8795676b 38
leothedragon 0:25fa8795676b 39 M2MObject::M2MObject(const String &object_name, char *path, bool external_blockwise_store)
leothedragon 0:25fa8795676b 40 : M2MBase(object_name,
leothedragon 0:25fa8795676b 41 M2MBase::Dynamic,
leothedragon 0:25fa8795676b 42 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:25fa8795676b 43 "",
leothedragon 0:25fa8795676b 44 #endif
leothedragon 0:25fa8795676b 45 path,
leothedragon 0:25fa8795676b 46 external_blockwise_store,
leothedragon 0:25fa8795676b 47 false),
leothedragon 0:25fa8795676b 48 _observation_handler(NULL)
leothedragon 0:25fa8795676b 49 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 50 ,_endpoint(NULL)
leothedragon 0:25fa8795676b 51 #endif
leothedragon 0:25fa8795676b 52 {
leothedragon 0:25fa8795676b 53 M2MBase::set_base_type(M2MBase::Object);
leothedragon 0:25fa8795676b 54 M2MBase::set_operation(M2MBase::GET_ALLOWED);
leothedragon 0:25fa8795676b 55 if(M2MBase::name_id() != -1) {
leothedragon 0:25fa8795676b 56 M2MBase::set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
leothedragon 0:25fa8795676b 57 }
leothedragon 0:25fa8795676b 58 }
leothedragon 0:25fa8795676b 59
leothedragon 0:25fa8795676b 60 M2MObject::M2MObject(const M2MBase::lwm2m_parameters_s* static_res)
leothedragon 0:25fa8795676b 61 : M2MBase(static_res),
leothedragon 0:25fa8795676b 62 _observation_handler(NULL)
leothedragon 0:25fa8795676b 63 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 64 ,_endpoint(NULL)
leothedragon 0:25fa8795676b 65 #endif
leothedragon 0:25fa8795676b 66 {
leothedragon 0:25fa8795676b 67 M2MBase::set_operation(M2MBase::GET_ALLOWED);
leothedragon 0:25fa8795676b 68 if(M2MBase::name_id() != -1) {
leothedragon 0:25fa8795676b 69 M2MBase::set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
leothedragon 0:25fa8795676b 70 }
leothedragon 0:25fa8795676b 71 }
leothedragon 0:25fa8795676b 72
leothedragon 0:25fa8795676b 73 M2MObject::~M2MObject()
leothedragon 0:25fa8795676b 74 {
leothedragon 0:25fa8795676b 75 if(!_instance_list.empty()) {
leothedragon 0:25fa8795676b 76
leothedragon 0:25fa8795676b 77 M2MObjectInstanceList::const_iterator it;
leothedragon 0:25fa8795676b 78 it = _instance_list.begin();
leothedragon 0:25fa8795676b 79 M2MObjectInstance* obj = NULL;
leothedragon 0:25fa8795676b 80 uint16_t index = 0;
leothedragon 0:25fa8795676b 81 for (; it!=_instance_list.end(); it++, index++ ) {
leothedragon 0:25fa8795676b 82 //Free allocated memory for object instances.
leothedragon 0:25fa8795676b 83 obj = *it;
leothedragon 0:25fa8795676b 84 delete obj;
leothedragon 0:25fa8795676b 85 }
leothedragon 0:25fa8795676b 86
leothedragon 0:25fa8795676b 87 _instance_list.clear();
leothedragon 0:25fa8795676b 88 }
leothedragon 0:25fa8795676b 89
leothedragon 0:25fa8795676b 90 free_resources();
leothedragon 0:25fa8795676b 91 }
leothedragon 0:25fa8795676b 92
leothedragon 0:25fa8795676b 93 M2MObjectInstance* M2MObject::create_object_instance(uint16_t instance_id)
leothedragon 0:25fa8795676b 94 {
leothedragon 0:25fa8795676b 95 tr_debug("M2MObject::create_object_instance - id: %d", instance_id);
leothedragon 0:25fa8795676b 96 M2MObjectInstance *instance = NULL;
leothedragon 0:25fa8795676b 97 if(!object_instance(instance_id)) {
leothedragon 0:25fa8795676b 98 char* path = create_path(*this, instance_id);
leothedragon 0:25fa8795676b 99 if (path) {
leothedragon 0:25fa8795676b 100 // Note: the object instance's name contains actually object's name.
leothedragon 0:25fa8795676b 101 instance = new M2MObjectInstance(*this, "", path);
leothedragon 0:25fa8795676b 102 if(instance) {
leothedragon 0:25fa8795676b 103 instance->add_observation_level(observation_level());
leothedragon 0:25fa8795676b 104 instance->set_instance_id(instance_id);
leothedragon 0:25fa8795676b 105 if(M2MBase::name_id() != -1) {
leothedragon 0:25fa8795676b 106 instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
leothedragon 0:25fa8795676b 107 }
leothedragon 0:25fa8795676b 108 _instance_list.push_back(instance);
leothedragon 0:25fa8795676b 109 set_changed();
leothedragon 0:25fa8795676b 110 }
leothedragon 0:25fa8795676b 111 }
leothedragon 0:25fa8795676b 112 }
leothedragon 0:25fa8795676b 113 return instance;
leothedragon 0:25fa8795676b 114 }
leothedragon 0:25fa8795676b 115
leothedragon 0:25fa8795676b 116
leothedragon 0:25fa8795676b 117 M2MObjectInstance* M2MObject::create_object_instance(const lwm2m_parameters_s* s)
leothedragon 0:25fa8795676b 118 {
leothedragon 0:25fa8795676b 119 tr_debug("M2MObject::create_object_instance - id: %d", s->identifier.instance_id);
leothedragon 0:25fa8795676b 120 M2MObjectInstance *instance = NULL;
leothedragon 0:25fa8795676b 121 if(!object_instance(s->identifier.instance_id)) {
leothedragon 0:25fa8795676b 122
leothedragon 0:25fa8795676b 123 instance = new M2MObjectInstance(*this, s);
leothedragon 0:25fa8795676b 124 if(instance) {
leothedragon 0:25fa8795676b 125 instance->add_observation_level(observation_level());
leothedragon 0:25fa8795676b 126 //instance->set_instance_id(instance_id);
leothedragon 0:25fa8795676b 127 //if(M2MBase::name_id() != -1) {
leothedragon 0:25fa8795676b 128 // instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD);
leothedragon 0:25fa8795676b 129 //}
leothedragon 0:25fa8795676b 130 _instance_list.push_back(instance);
leothedragon 0:25fa8795676b 131 set_changed();
leothedragon 0:25fa8795676b 132 }
leothedragon 0:25fa8795676b 133 }
leothedragon 0:25fa8795676b 134 return instance;
leothedragon 0:25fa8795676b 135 }
leothedragon 0:25fa8795676b 136
leothedragon 0:25fa8795676b 137 bool M2MObject::remove_object_instance(uint16_t inst_id)
leothedragon 0:25fa8795676b 138 {
leothedragon 0:25fa8795676b 139 tr_debug("M2MObject::remove_object_instance(inst_id %d)", inst_id);
leothedragon 0:25fa8795676b 140 bool success = false;
leothedragon 0:25fa8795676b 141 if(!_instance_list.empty()) {
leothedragon 0:25fa8795676b 142 M2MObjectInstance* obj = NULL;
leothedragon 0:25fa8795676b 143 M2MObjectInstanceList::const_iterator it;
leothedragon 0:25fa8795676b 144 it = _instance_list.begin();
leothedragon 0:25fa8795676b 145 int pos = 0;
leothedragon 0:25fa8795676b 146 for ( ; it != _instance_list.end(); it++, pos++ ) {
leothedragon 0:25fa8795676b 147 if((*it)->instance_id() == inst_id) {
leothedragon 0:25fa8795676b 148 // Instance found and deleted.
leothedragon 0:25fa8795676b 149 obj = *it;
leothedragon 0:25fa8795676b 150
leothedragon 0:25fa8795676b 151 _instance_list.erase(pos);
leothedragon 0:25fa8795676b 152 delete obj;
leothedragon 0:25fa8795676b 153 success = true;
leothedragon 0:25fa8795676b 154 set_changed();
leothedragon 0:25fa8795676b 155 break;
leothedragon 0:25fa8795676b 156 }
leothedragon 0:25fa8795676b 157 }
leothedragon 0:25fa8795676b 158 }
leothedragon 0:25fa8795676b 159 return success;
leothedragon 0:25fa8795676b 160 }
leothedragon 0:25fa8795676b 161
leothedragon 0:25fa8795676b 162 M2MObjectInstance* M2MObject::object_instance(uint16_t inst_id) const
leothedragon 0:25fa8795676b 163 {
leothedragon 0:25fa8795676b 164 tr_debug("M2MObject::object_instance(inst_id %d)", inst_id);
leothedragon 0:25fa8795676b 165 M2MObjectInstance *obj = NULL;
leothedragon 0:25fa8795676b 166 if(!_instance_list.empty()) {
leothedragon 0:25fa8795676b 167 M2MObjectInstanceList::const_iterator it;
leothedragon 0:25fa8795676b 168 it = _instance_list.begin();
leothedragon 0:25fa8795676b 169 for ( ; it != _instance_list.end(); it++ ) {
leothedragon 0:25fa8795676b 170 if((*it)->instance_id() == inst_id) {
leothedragon 0:25fa8795676b 171 // Instance found.
leothedragon 0:25fa8795676b 172 obj = *it;
leothedragon 0:25fa8795676b 173 break;
leothedragon 0:25fa8795676b 174 }
leothedragon 0:25fa8795676b 175 }
leothedragon 0:25fa8795676b 176 }
leothedragon 0:25fa8795676b 177 return obj;
leothedragon 0:25fa8795676b 178 }
leothedragon 0:25fa8795676b 179
leothedragon 0:25fa8795676b 180 const M2MObjectInstanceList& M2MObject::instances() const
leothedragon 0:25fa8795676b 181 {
leothedragon 0:25fa8795676b 182 return _instance_list;
leothedragon 0:25fa8795676b 183 }
leothedragon 0:25fa8795676b 184
leothedragon 0:25fa8795676b 185 uint16_t M2MObject::instance_count() const
leothedragon 0:25fa8795676b 186 {
leothedragon 0:25fa8795676b 187 return (uint16_t)_instance_list.size();
leothedragon 0:25fa8795676b 188 }
leothedragon 0:25fa8795676b 189
leothedragon 0:25fa8795676b 190 M2MObservationHandler* M2MObject::observation_handler() const
leothedragon 0:25fa8795676b 191 {
leothedragon 0:25fa8795676b 192 // XXX: need to check the flag too
leothedragon 0:25fa8795676b 193 return _observation_handler;
leothedragon 0:25fa8795676b 194 }
leothedragon 0:25fa8795676b 195
leothedragon 0:25fa8795676b 196 void M2MObject::set_observation_handler(M2MObservationHandler *handler)
leothedragon 0:25fa8795676b 197 {
leothedragon 0:25fa8795676b 198 tr_debug("M2MObject::set_observation_handler - handler: 0x%p", (void*)handler);
leothedragon 0:25fa8795676b 199 _observation_handler = handler;
leothedragon 0:25fa8795676b 200 }
leothedragon 0:25fa8795676b 201
leothedragon 0:25fa8795676b 202 void M2MObject::add_observation_level(M2MBase::Observation observation_level)
leothedragon 0:25fa8795676b 203 {
leothedragon 0:25fa8795676b 204 M2MBase::add_observation_level(observation_level);
leothedragon 0:25fa8795676b 205 if(!_instance_list.empty()) {
leothedragon 0:25fa8795676b 206 M2MObjectInstanceList::const_iterator it;
leothedragon 0:25fa8795676b 207 it = _instance_list.begin();
leothedragon 0:25fa8795676b 208 for ( ; it != _instance_list.end(); it++ ) {
leothedragon 0:25fa8795676b 209 (*it)->add_observation_level(observation_level);
leothedragon 0:25fa8795676b 210 }
leothedragon 0:25fa8795676b 211 }
leothedragon 0:25fa8795676b 212 }
leothedragon 0:25fa8795676b 213
leothedragon 0:25fa8795676b 214 void M2MObject::remove_observation_level(M2MBase::Observation observation_level)
leothedragon 0:25fa8795676b 215 {
leothedragon 0:25fa8795676b 216 M2MBase::remove_observation_level(observation_level);
leothedragon 0:25fa8795676b 217 if(!_instance_list.empty()) {
leothedragon 0:25fa8795676b 218 M2MObjectInstanceList::const_iterator it;
leothedragon 0:25fa8795676b 219 it = _instance_list.begin();
leothedragon 0:25fa8795676b 220 for ( ; it != _instance_list.end(); it++ ) {
leothedragon 0:25fa8795676b 221 (*it)->remove_observation_level(observation_level);
leothedragon 0:25fa8795676b 222 }
leothedragon 0:25fa8795676b 223 }
leothedragon 0:25fa8795676b 224 }
leothedragon 0:25fa8795676b 225
leothedragon 0:25fa8795676b 226 sn_coap_hdr_s* M2MObject::handle_get_request(nsdl_s *nsdl,
leothedragon 0:25fa8795676b 227 sn_coap_hdr_s *received_coap_header,
leothedragon 0:25fa8795676b 228 M2MObservationHandler *observation_handler)
leothedragon 0:25fa8795676b 229 {
leothedragon 0:25fa8795676b 230 tr_info("M2MObject::handle_get_request()");
leothedragon 0:25fa8795676b 231 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
leothedragon 0:25fa8795676b 232 sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
leothedragon 0:25fa8795676b 233 received_coap_header,
leothedragon 0:25fa8795676b 234 msg_code);
leothedragon 0:25fa8795676b 235 uint8_t *data = NULL;
leothedragon 0:25fa8795676b 236 uint32_t data_length = 0;
leothedragon 0:25fa8795676b 237 if(received_coap_header) {
leothedragon 0:25fa8795676b 238 // process the GET if we have registered a callback for it
leothedragon 0:25fa8795676b 239 if ((operation() & M2MBase::GET_ALLOWED) != 0) {
leothedragon 0:25fa8795676b 240 if(coap_response) {
leothedragon 0:25fa8795676b 241 bool content_type_present = false;
leothedragon 0:25fa8795676b 242 bool is_content_type_supported = true;
leothedragon 0:25fa8795676b 243
leothedragon 0:25fa8795676b 244 if (received_coap_header->options_list_ptr &&
leothedragon 0:25fa8795676b 245 received_coap_header->options_list_ptr->accept != COAP_CT_NONE) {
leothedragon 0:25fa8795676b 246 content_type_present = true;
leothedragon 0:25fa8795676b 247 coap_response->content_format = received_coap_header->options_list_ptr->accept;
leothedragon 0:25fa8795676b 248
leothedragon 0:25fa8795676b 249 }
leothedragon 0:25fa8795676b 250
leothedragon 0:25fa8795676b 251 // Check if preferred content type is supported
leothedragon 0:25fa8795676b 252 if (content_type_present) {
leothedragon 0:25fa8795676b 253 if (coap_response->content_format != COAP_CONTENT_OMA_TLV_TYPE_OLD &&
leothedragon 0:25fa8795676b 254 coap_response->content_format != COAP_CONTENT_OMA_TLV_TYPE) {
leothedragon 0:25fa8795676b 255 is_content_type_supported = false;
leothedragon 0:25fa8795676b 256 }
leothedragon 0:25fa8795676b 257 }
leothedragon 0:25fa8795676b 258
leothedragon 0:25fa8795676b 259 if (is_content_type_supported) {
leothedragon 0:25fa8795676b 260 if(!content_type_present &&
leothedragon 0:25fa8795676b 261 (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE ||
leothedragon 0:25fa8795676b 262 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) {
leothedragon 0:25fa8795676b 263 coap_response->content_format = sn_coap_content_format_e(M2MBase::coap_content_type());
leothedragon 0:25fa8795676b 264 }
leothedragon 0:25fa8795676b 265
leothedragon 0:25fa8795676b 266 // fill in the CoAP response payload
leothedragon 0:25fa8795676b 267 if(COAP_CONTENT_OMA_TLV_TYPE == coap_response->content_format ||
leothedragon 0:25fa8795676b 268 COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_response->content_format) {
leothedragon 0:25fa8795676b 269 set_coap_content_type(coap_response->content_format);
leothedragon 0:25fa8795676b 270 data = M2MTLVSerializer::serialize(_instance_list, data_length);
leothedragon 0:25fa8795676b 271 }
leothedragon 0:25fa8795676b 272
leothedragon 0:25fa8795676b 273 coap_response->payload_len = data_length;
leothedragon 0:25fa8795676b 274 coap_response->payload_ptr = data;
leothedragon 0:25fa8795676b 275 if(data){
leothedragon 0:25fa8795676b 276 coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response);
leothedragon 0:25fa8795676b 277 if(coap_response->options_list_ptr){
leothedragon 0:25fa8795676b 278 coap_response->options_list_ptr->max_age = max_age();
leothedragon 0:25fa8795676b 279 }
leothedragon 0:25fa8795676b 280 if(received_coap_header->options_list_ptr) {
leothedragon 0:25fa8795676b 281 if(received_coap_header->options_list_ptr->observe != -1) {
leothedragon 0:25fa8795676b 282 handle_observation(nsdl, *received_coap_header, *coap_response, observation_handler, msg_code);
leothedragon 0:25fa8795676b 283 }
leothedragon 0:25fa8795676b 284 }
leothedragon 0:25fa8795676b 285 } else {
leothedragon 0:25fa8795676b 286 msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; // Content format not supported
leothedragon 0:25fa8795676b 287 }
leothedragon 0:25fa8795676b 288 } else {
leothedragon 0:25fa8795676b 289 tr_error("M2MObject::handle_get_request() - Content-Type %d not supported", coap_response->content_format);
leothedragon 0:25fa8795676b 290 msg_code = COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE;
leothedragon 0:25fa8795676b 291 }
leothedragon 0:25fa8795676b 292 }
leothedragon 0:25fa8795676b 293 }else {
leothedragon 0:25fa8795676b 294 tr_error("M2MResource::handle_get_request - Return COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
leothedragon 0:25fa8795676b 295 // Operation is not allowed.
leothedragon 0:25fa8795676b 296 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:25fa8795676b 297 }
leothedragon 0:25fa8795676b 298 } else {
leothedragon 0:25fa8795676b 299 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:25fa8795676b 300 }
leothedragon 0:25fa8795676b 301 if(coap_response) {
leothedragon 0:25fa8795676b 302 coap_response->msg_code = msg_code;
leothedragon 0:25fa8795676b 303 }
leothedragon 0:25fa8795676b 304 return coap_response;
leothedragon 0:25fa8795676b 305 }
leothedragon 0:25fa8795676b 306
leothedragon 0:25fa8795676b 307 sn_coap_hdr_s* M2MObject::handle_put_request(nsdl_s *nsdl,
leothedragon 0:25fa8795676b 308 sn_coap_hdr_s *received_coap_header,
leothedragon 0:25fa8795676b 309 M2MObservationHandler */*observation_handler*/,
leothedragon 0:25fa8795676b 310 bool &/*execute_value_updated*/)
leothedragon 0:25fa8795676b 311 {
leothedragon 0:25fa8795676b 312 tr_info("M2MObject::handle_put_request()");
leothedragon 0:25fa8795676b 313 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
leothedragon 0:25fa8795676b 314 sn_coap_hdr_s *coap_response = sn_nsdl_build_response(nsdl,
leothedragon 0:25fa8795676b 315 received_coap_header,
leothedragon 0:25fa8795676b 316 msg_code);
leothedragon 0:25fa8795676b 317 if(received_coap_header) {
leothedragon 0:25fa8795676b 318 if(received_coap_header->content_format != COAP_CT_NONE) {
leothedragon 0:25fa8795676b 319 set_coap_content_type(received_coap_header->content_format);
leothedragon 0:25fa8795676b 320 }
leothedragon 0:25fa8795676b 321 if(received_coap_header->options_list_ptr &&
leothedragon 0:25fa8795676b 322 received_coap_header->options_list_ptr->uri_query_ptr) {
leothedragon 0:25fa8795676b 323 char *query = (char*)alloc_string_copy(received_coap_header->options_list_ptr->uri_query_ptr,
leothedragon 0:25fa8795676b 324 received_coap_header->options_list_ptr->uri_query_len);
leothedragon 0:25fa8795676b 325
leothedragon 0:25fa8795676b 326 if (query){
leothedragon 0:25fa8795676b 327 tr_info("M2MObject::handle_put_request() - query %s", query);
leothedragon 0:25fa8795676b 328 // if anything was updated, re-initialize the stored notification attributes
leothedragon 0:25fa8795676b 329 if (!handle_observation_attribute(query)){
leothedragon 0:25fa8795676b 330 tr_debug("M2MObject::handle_put_request() - Invalid query");
leothedragon 0:25fa8795676b 331 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
leothedragon 0:25fa8795676b 332 }
leothedragon 0:25fa8795676b 333 free(query);
leothedragon 0:25fa8795676b 334 }
leothedragon 0:25fa8795676b 335 } else {
leothedragon 0:25fa8795676b 336 tr_error("M2MObject::handle_put_request() - COAP_MSG_CODE_RESPONSE_BAD_REQUEST - Empty URI_QUERY");
leothedragon 0:25fa8795676b 337 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
leothedragon 0:25fa8795676b 338 }
leothedragon 0:25fa8795676b 339 } else {
leothedragon 0:25fa8795676b 340 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:25fa8795676b 341 }
leothedragon 0:25fa8795676b 342 if(coap_response) {
leothedragon 0:25fa8795676b 343 coap_response->msg_code = msg_code;
leothedragon 0:25fa8795676b 344 }
leothedragon 0:25fa8795676b 345 return coap_response;
leothedragon 0:25fa8795676b 346 }
leothedragon 0:25fa8795676b 347
leothedragon 0:25fa8795676b 348
leothedragon 0:25fa8795676b 349 sn_coap_hdr_s* M2MObject::handle_post_request(nsdl_s *nsdl,
leothedragon 0:25fa8795676b 350 sn_coap_hdr_s *received_coap_header,
leothedragon 0:25fa8795676b 351 M2MObservationHandler *observation_handler,
leothedragon 0:25fa8795676b 352 bool &execute_value_updated,
leothedragon 0:25fa8795676b 353 sn_nsdl_addr_s *)
leothedragon 0:25fa8795676b 354 {
leothedragon 0:25fa8795676b 355 tr_info("M2MObject::handle_post_request()");
leothedragon 0:25fa8795676b 356 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
leothedragon 0:25fa8795676b 357 // process the POST if we have registered a callback for it
leothedragon 0:25fa8795676b 358 sn_coap_hdr_s *coap_response = sn_nsdl_build_response(nsdl,
leothedragon 0:25fa8795676b 359 received_coap_header,
leothedragon 0:25fa8795676b 360 msg_code);
leothedragon 0:25fa8795676b 361
leothedragon 0:25fa8795676b 362 if (received_coap_header) {
leothedragon 0:25fa8795676b 363 if ((operation() & M2MBase::POST_ALLOWED) != 0) {
leothedragon 0:25fa8795676b 364 if (received_coap_header->content_format != COAP_CT_NONE) {
leothedragon 0:25fa8795676b 365 set_coap_content_type(received_coap_header->content_format);
leothedragon 0:25fa8795676b 366 }
leothedragon 0:25fa8795676b 367 if(received_coap_header->payload_ptr) {
leothedragon 0:25fa8795676b 368 tr_debug("M2MObject::handle_post_request() - Update Object with new values");
leothedragon 0:25fa8795676b 369 uint16_t coap_content_type = 0;
leothedragon 0:25fa8795676b 370 bool content_type_present = false;
leothedragon 0:25fa8795676b 371 if(received_coap_header->content_format != COAP_CT_NONE) {
leothedragon 0:25fa8795676b 372 content_type_present = true;
leothedragon 0:25fa8795676b 373 if(coap_response) {
leothedragon 0:25fa8795676b 374 coap_content_type = received_coap_header->content_format;
leothedragon 0:25fa8795676b 375 }
leothedragon 0:25fa8795676b 376 } // if(received_coap_header->content_format)
leothedragon 0:25fa8795676b 377 if(!content_type_present &&
leothedragon 0:25fa8795676b 378 (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE ||
leothedragon 0:25fa8795676b 379 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) {
leothedragon 0:25fa8795676b 380 coap_content_type = M2MBase::coap_content_type();
leothedragon 0:25fa8795676b 381 }
leothedragon 0:25fa8795676b 382
leothedragon 0:25fa8795676b 383 tr_debug("M2MObject::handle_post_request() - Request Content-type: %d", coap_content_type);
leothedragon 0:25fa8795676b 384
leothedragon 0:25fa8795676b 385 if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type ||
leothedragon 0:25fa8795676b 386 COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_content_type) {
leothedragon 0:25fa8795676b 387 set_coap_content_type(coap_content_type);
leothedragon 0:25fa8795676b 388 uint32_t instance_id = 0;
leothedragon 0:25fa8795676b 389 // Check next free instance id
leothedragon 0:25fa8795676b 390 for(instance_id = 0; instance_id <= MAX_UNINT_16_COUNT; instance_id++) {
leothedragon 0:25fa8795676b 391 if(NULL == object_instance(instance_id)) {
leothedragon 0:25fa8795676b 392 break;
leothedragon 0:25fa8795676b 393 }
leothedragon 0:25fa8795676b 394 if(instance_id == MAX_UNINT_16_COUNT) {
leothedragon 0:25fa8795676b 395 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:25fa8795676b 396 break;
leothedragon 0:25fa8795676b 397 }
leothedragon 0:25fa8795676b 398 }
leothedragon 0:25fa8795676b 399 if(COAP_MSG_CODE_RESPONSE_CHANGED == msg_code) {
leothedragon 0:25fa8795676b 400 bool is_obj_instance = false;
leothedragon 0:25fa8795676b 401 bool obj_instance_exists = false;
leothedragon 0:25fa8795676b 402 is_obj_instance = M2MTLVDeserializer::is_object_instance(received_coap_header->payload_ptr);
leothedragon 0:25fa8795676b 403 if (is_obj_instance) {
leothedragon 0:25fa8795676b 404 instance_id = M2MTLVDeserializer::instance_id(received_coap_header->payload_ptr);
leothedragon 0:25fa8795676b 405 tr_debug("M2MObject::handle_post_request() - instance id in TLV: %" PRIu32, instance_id);
leothedragon 0:25fa8795676b 406 // Check if instance id already exists
leothedragon 0:25fa8795676b 407 if (object_instance(instance_id)){
leothedragon 0:25fa8795676b 408 obj_instance_exists = true;
leothedragon 0:25fa8795676b 409 }
leothedragon 0:25fa8795676b 410 }
leothedragon 0:25fa8795676b 411 if (!obj_instance_exists && coap_response) {
leothedragon 0:25fa8795676b 412 M2MObjectInstance *obj_instance = create_object_instance(instance_id);
leothedragon 0:25fa8795676b 413 if (obj_instance) {
leothedragon 0:25fa8795676b 414 obj_instance->set_operation(M2MBase::GET_PUT_ALLOWED);
leothedragon 0:25fa8795676b 415 }
leothedragon 0:25fa8795676b 416
leothedragon 0:25fa8795676b 417 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
leothedragon 0:25fa8795676b 418 if(is_obj_instance) {
leothedragon 0:25fa8795676b 419 tr_debug("M2MObject::handle_post_request() - TLV data contains ObjectInstance");
leothedragon 0:25fa8795676b 420 error = M2MTLVDeserializer::deserialise_object_instances(received_coap_header->payload_ptr,
leothedragon 0:25fa8795676b 421 received_coap_header->payload_len,
leothedragon 0:25fa8795676b 422 *this,
leothedragon 0:25fa8795676b 423 M2MTLVDeserializer::Post);
leothedragon 0:25fa8795676b 424 } else if(obj_instance &&
leothedragon 0:25fa8795676b 425 (M2MTLVDeserializer::is_resource(received_coap_header->payload_ptr) ||
leothedragon 0:25fa8795676b 426 M2MTLVDeserializer::is_multiple_resource(received_coap_header->payload_ptr))) {
leothedragon 0:25fa8795676b 427 tr_debug("M2MObject::handle_post_request() - TLV data contains Resources");
leothedragon 0:25fa8795676b 428 error = M2MTLVDeserializer::deserialize_resources(received_coap_header->payload_ptr,
leothedragon 0:25fa8795676b 429 received_coap_header->payload_len,
leothedragon 0:25fa8795676b 430 *obj_instance,
leothedragon 0:25fa8795676b 431 M2MTLVDeserializer::Post);
leothedragon 0:25fa8795676b 432 } else {
leothedragon 0:25fa8795676b 433 error = M2MTLVDeserializer::NotValid;
leothedragon 0:25fa8795676b 434 }
leothedragon 0:25fa8795676b 435 switch(error) {
leothedragon 0:25fa8795676b 436 case M2MTLVDeserializer::None:
leothedragon 0:25fa8795676b 437 if(observation_handler) {
leothedragon 0:25fa8795676b 438 execute_value_updated = true;
leothedragon 0:25fa8795676b 439 }
leothedragon 0:25fa8795676b 440 coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response);
leothedragon 0:25fa8795676b 441
leothedragon 0:25fa8795676b 442 if (coap_response->options_list_ptr) {
leothedragon 0:25fa8795676b 443
leothedragon 0:25fa8795676b 444 StringBuffer<MAX_OBJECT_PATH_NAME> obj_name;
leothedragon 0:25fa8795676b 445
leothedragon 0:25fa8795676b 446 if (obj_name.ensure_space(M2MBase::resource_name_length() + (1 + 5 + 1))) {
leothedragon 0:25fa8795676b 447 obj_name.append(M2MBase::name());
leothedragon 0:25fa8795676b 448 obj_name.append('/');
leothedragon 0:25fa8795676b 449 obj_name.append_int(instance_id);
leothedragon 0:25fa8795676b 450
leothedragon 0:25fa8795676b 451 coap_response->options_list_ptr->location_path_len = obj_name.get_size();
leothedragon 0:25fa8795676b 452 coap_response->options_list_ptr->location_path_ptr =
leothedragon 0:25fa8795676b 453 alloc_copy((uint8_t*)obj_name.c_str(), obj_name.get_size());
leothedragon 0:25fa8795676b 454 // todo: else return error
leothedragon 0:25fa8795676b 455 }
leothedragon 0:25fa8795676b 456 }
leothedragon 0:25fa8795676b 457 // todo: else return error
leothedragon 0:25fa8795676b 458 msg_code = COAP_MSG_CODE_RESPONSE_CREATED;
leothedragon 0:25fa8795676b 459 break;
leothedragon 0:25fa8795676b 460 case M2MTLVDeserializer::NotAllowed:
leothedragon 0:25fa8795676b 461 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:25fa8795676b 462 break;
leothedragon 0:25fa8795676b 463 case M2MTLVDeserializer::NotValid:
leothedragon 0:25fa8795676b 464 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
leothedragon 0:25fa8795676b 465 break;
leothedragon 0:25fa8795676b 466 case M2MTLVDeserializer::NotFound:
leothedragon 0:25fa8795676b 467 msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
leothedragon 0:25fa8795676b 468 break;
leothedragon 0:25fa8795676b 469 case M2MTLVDeserializer::OutOfMemory:
leothedragon 0:25fa8795676b 470 msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
leothedragon 0:25fa8795676b 471 break;
leothedragon 0:25fa8795676b 472 }
leothedragon 0:25fa8795676b 473
leothedragon 0:25fa8795676b 474 } else {
leothedragon 0:25fa8795676b 475 tr_error("M2MObject::handle_post_request() - COAP_MSG_CODE_RESPONSE_BAD_REQUEST");
leothedragon 0:25fa8795676b 476 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
leothedragon 0:25fa8795676b 477 }
leothedragon 0:25fa8795676b 478 }
leothedragon 0:25fa8795676b 479 } else {
leothedragon 0:25fa8795676b 480 msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
leothedragon 0:25fa8795676b 481 } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type)
leothedragon 0:25fa8795676b 482 } else {
leothedragon 0:25fa8795676b 483 tr_error("M2MObject::handle_post_request - COAP_MSG_CODE_RESPONSE_BAD_REQUEST - Missing Payload");
leothedragon 0:25fa8795676b 484 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; //
leothedragon 0:25fa8795676b 485 }
leothedragon 0:25fa8795676b 486 } else { // if ((object->operation() & SN_GRS_POST_ALLOWED) != 0)
leothedragon 0:25fa8795676b 487 tr_error("M2MObject::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
leothedragon 0:25fa8795676b 488 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.05
leothedragon 0:25fa8795676b 489 }
leothedragon 0:25fa8795676b 490 } else { //if(received_coap_header)
leothedragon 0:25fa8795676b 491 tr_error("M2MObject::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
leothedragon 0:25fa8795676b 492 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.05
leothedragon 0:25fa8795676b 493 }
leothedragon 0:25fa8795676b 494
leothedragon 0:25fa8795676b 495 if(coap_response) {
leothedragon 0:25fa8795676b 496 coap_response->msg_code = msg_code;
leothedragon 0:25fa8795676b 497 }
leothedragon 0:25fa8795676b 498 return coap_response;
leothedragon 0:25fa8795676b 499 }
leothedragon 0:25fa8795676b 500
leothedragon 0:25fa8795676b 501 void M2MObject::notification_update(uint16_t obj_instance_id)
leothedragon 0:25fa8795676b 502 {
leothedragon 0:25fa8795676b 503 tr_debug("M2MObject::notification_update - id: %d", obj_instance_id);
leothedragon 0:25fa8795676b 504 M2MReportHandler *report_handler = M2MBase::report_handler();
leothedragon 0:25fa8795676b 505 if(report_handler && is_under_observation()) {
leothedragon 0:25fa8795676b 506 report_handler->set_notification_trigger(obj_instance_id);
leothedragon 0:25fa8795676b 507 }
leothedragon 0:25fa8795676b 508 }
leothedragon 0:25fa8795676b 509
leothedragon 0:25fa8795676b 510 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 511 void M2MObject::set_endpoint(M2MEndpoint *endpoint)
leothedragon 0:25fa8795676b 512 {
leothedragon 0:25fa8795676b 513 _endpoint = endpoint;
leothedragon 0:25fa8795676b 514 }
leothedragon 0:25fa8795676b 515
leothedragon 0:25fa8795676b 516 M2MEndpoint* M2MObject::get_endpoint() const
leothedragon 0:25fa8795676b 517 {
leothedragon 0:25fa8795676b 518 return _endpoint;
leothedragon 0:25fa8795676b 519 }
leothedragon 0:25fa8795676b 520 #endif
leothedragon 0:25fa8795676b 521
leothedragon 0:25fa8795676b 522 M2MBase *M2MObject::get_parent() const
leothedragon 0:25fa8795676b 523 {
leothedragon 0:25fa8795676b 524 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
leothedragon 0:25fa8795676b 525 return (M2MBase *) get_endpoint();
leothedragon 0:25fa8795676b 526 #else
leothedragon 0:25fa8795676b 527 return NULL;
leothedragon 0:25fa8795676b 528 #endif
leothedragon 0:25fa8795676b 529 }