FRDM K64F Metronome

Committer:
ram54288
Date:
Sun May 14 18:37:05 2017 +0000
Revision:
0:dbad57390bd1
Initial commit

Who changed what in which revision?

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