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

Who changed what in which revision?

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