Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Fri Mar 20 20:15:18 2020 +0000
Revision:
2:990c985a69ae
Parent:
0:977e87915078
Update to work with P2Scan runtime

Who changed what in which revision?

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