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/m2mconstants.h"
ram54288 0:dbad57390bd1 17 #include "mbed-client/m2mresource.h"
ram54288 0:dbad57390bd1 18 #include "mbed-client/m2mobservationhandler.h"
ram54288 0:dbad57390bd1 19 #include "include/m2mreporthandler.h"
ram54288 0:dbad57390bd1 20 #include "include/m2mtlvserializer.h"
ram54288 0:dbad57390bd1 21 #include "include/m2mtlvdeserializer.h"
ram54288 0:dbad57390bd1 22 #include "mbed-trace/mbed_trace.h"
ram54288 0:dbad57390bd1 23
ram54288 0:dbad57390bd1 24 #include <stdlib.h>
ram54288 0:dbad57390bd1 25
ram54288 0:dbad57390bd1 26 #define TRACE_GROUP "mClt"
ram54288 0:dbad57390bd1 27
ram54288 0:dbad57390bd1 28 M2MResource::M2MResource(M2MObjectInstance &parent,
ram54288 0:dbad57390bd1 29 const String &resource_name,
ram54288 0:dbad57390bd1 30 const String &resource_type,
ram54288 0:dbad57390bd1 31 M2MResourceInstance::ResourceType type,
ram54288 0:dbad57390bd1 32 const uint8_t *value,
ram54288 0:dbad57390bd1 33 const uint8_t value_length,
ram54288 0:dbad57390bd1 34 char *path,
ram54288 0:dbad57390bd1 35 const uint16_t object_instance_id,
ram54288 0:dbad57390bd1 36 bool multiple_instance,
ram54288 0:dbad57390bd1 37 bool external_blockwise_store)
ram54288 0:dbad57390bd1 38 : M2MResourceInstance(*this, resource_name, resource_type, type, value, value_length,
ram54288 0:dbad57390bd1 39 object_instance_id,
ram54288 0:dbad57390bd1 40 path, external_blockwise_store),
ram54288 0:dbad57390bd1 41 _parent(parent),
ram54288 0:dbad57390bd1 42 _delayed_token(NULL),
ram54288 0:dbad57390bd1 43 _delayed_token_len(0),
ram54288 0:dbad57390bd1 44 _has_multiple_instances(multiple_instance),
ram54288 0:dbad57390bd1 45 _delayed_response(false)
ram54288 0:dbad57390bd1 46 {
ram54288 0:dbad57390bd1 47 M2MBase::set_base_type(M2MBase::Resource);
ram54288 0:dbad57390bd1 48 M2MBase::set_operation(M2MBase::GET_ALLOWED);
ram54288 0:dbad57390bd1 49 M2MBase::set_observable(false);
ram54288 0:dbad57390bd1 50
ram54288 0:dbad57390bd1 51 }
ram54288 0:dbad57390bd1 52
ram54288 0:dbad57390bd1 53 M2MResource::M2MResource(M2MObjectInstance &parent,
ram54288 0:dbad57390bd1 54 const lwm2m_parameters_s* s,
ram54288 0:dbad57390bd1 55 M2MResourceInstance::ResourceType type,
ram54288 0:dbad57390bd1 56 const uint16_t object_instance_id)
ram54288 0:dbad57390bd1 57 : M2MResourceInstance(*this, s, type, object_instance_id),
ram54288 0:dbad57390bd1 58 _parent(parent),
ram54288 0:dbad57390bd1 59 _delayed_token(NULL),
ram54288 0:dbad57390bd1 60 _delayed_token_len(0),
ram54288 0:dbad57390bd1 61 _has_multiple_instances(false),
ram54288 0:dbad57390bd1 62 _delayed_response(false)
ram54288 0:dbad57390bd1 63 {
ram54288 0:dbad57390bd1 64 // tbd: _has_multiple_instances could be in flash, but no real benefit, because of current alignment.
ram54288 0:dbad57390bd1 65 }
ram54288 0:dbad57390bd1 66
ram54288 0:dbad57390bd1 67 M2MResource::M2MResource(M2MObjectInstance &parent,
ram54288 0:dbad57390bd1 68 const String &resource_name,
ram54288 0:dbad57390bd1 69 const String &resource_type,
ram54288 0:dbad57390bd1 70 M2MResourceInstance::ResourceType type,
ram54288 0:dbad57390bd1 71 bool observable,
ram54288 0:dbad57390bd1 72 char *path,
ram54288 0:dbad57390bd1 73 const uint16_t object_instance_id,
ram54288 0:dbad57390bd1 74 bool multiple_instance,
ram54288 0:dbad57390bd1 75 bool external_blockwise_store)
ram54288 0:dbad57390bd1 76 : M2MResourceInstance(*this, resource_name, resource_type, type,
ram54288 0:dbad57390bd1 77 object_instance_id,
ram54288 0:dbad57390bd1 78 path,
ram54288 0:dbad57390bd1 79 external_blockwise_store),
ram54288 0:dbad57390bd1 80 _parent(parent),
ram54288 0:dbad57390bd1 81 _delayed_token(NULL),
ram54288 0:dbad57390bd1 82 _delayed_token_len(0),
ram54288 0:dbad57390bd1 83 _has_multiple_instances(multiple_instance),
ram54288 0:dbad57390bd1 84 _delayed_response(false)
ram54288 0:dbad57390bd1 85 {
ram54288 0:dbad57390bd1 86 M2MBase::set_base_type(M2MBase::Resource);
ram54288 0:dbad57390bd1 87 M2MBase::set_operation(M2MBase::GET_PUT_ALLOWED);
ram54288 0:dbad57390bd1 88 M2MBase::set_observable(observable);
ram54288 0:dbad57390bd1 89 }
ram54288 0:dbad57390bd1 90
ram54288 0:dbad57390bd1 91
ram54288 0:dbad57390bd1 92 M2MResource::~M2MResource()
ram54288 0:dbad57390bd1 93 {
ram54288 0:dbad57390bd1 94 if(!_resource_instance_list.empty()) {
ram54288 0:dbad57390bd1 95 M2MResourceInstance* res = NULL;
ram54288 0:dbad57390bd1 96 M2MResourceInstanceList::const_iterator it;
ram54288 0:dbad57390bd1 97 it = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 98 for (; it!=_resource_instance_list.end(); it++ ) {
ram54288 0:dbad57390bd1 99 //Free allocated memory for resources.
ram54288 0:dbad57390bd1 100 res = *it;
ram54288 0:dbad57390bd1 101 delete res;
ram54288 0:dbad57390bd1 102 }
ram54288 0:dbad57390bd1 103 _resource_instance_list.clear();
ram54288 0:dbad57390bd1 104 }
ram54288 0:dbad57390bd1 105 free(_delayed_token);
ram54288 0:dbad57390bd1 106 }
ram54288 0:dbad57390bd1 107
ram54288 0:dbad57390bd1 108 bool M2MResource::supports_multiple_instances() const
ram54288 0:dbad57390bd1 109 {
ram54288 0:dbad57390bd1 110 return _has_multiple_instances;
ram54288 0:dbad57390bd1 111 }
ram54288 0:dbad57390bd1 112
ram54288 0:dbad57390bd1 113 void M2MResource::set_delayed_response(bool delayed_response)
ram54288 0:dbad57390bd1 114 {
ram54288 0:dbad57390bd1 115 _delayed_response = delayed_response;
ram54288 0:dbad57390bd1 116 }
ram54288 0:dbad57390bd1 117
ram54288 0:dbad57390bd1 118 bool M2MResource::send_delayed_post_response()
ram54288 0:dbad57390bd1 119 {
ram54288 0:dbad57390bd1 120 bool success = false;
ram54288 0:dbad57390bd1 121 if(_delayed_response) {
ram54288 0:dbad57390bd1 122 success = true;
ram54288 0:dbad57390bd1 123 observation_handler()->send_delayed_response(this);
ram54288 0:dbad57390bd1 124 }
ram54288 0:dbad57390bd1 125 return success;
ram54288 0:dbad57390bd1 126 }
ram54288 0:dbad57390bd1 127
ram54288 0:dbad57390bd1 128 void M2MResource::get_delayed_token(uint8_t *&token, uint8_t &token_length)
ram54288 0:dbad57390bd1 129 {
ram54288 0:dbad57390bd1 130 token_length = 0;
ram54288 0:dbad57390bd1 131 if(token) {
ram54288 0:dbad57390bd1 132 free(token);
ram54288 0:dbad57390bd1 133 token = NULL;
ram54288 0:dbad57390bd1 134 }
ram54288 0:dbad57390bd1 135 if(_delayed_token && _delayed_token_len > 0) {
ram54288 0:dbad57390bd1 136 token = alloc_copy(_delayed_token, _delayed_token_len);
ram54288 0:dbad57390bd1 137 if(token) {
ram54288 0:dbad57390bd1 138 token_length = _delayed_token_len;
ram54288 0:dbad57390bd1 139 }
ram54288 0:dbad57390bd1 140 }
ram54288 0:dbad57390bd1 141 }
ram54288 0:dbad57390bd1 142
ram54288 0:dbad57390bd1 143 bool M2MResource::remove_resource_instance(uint16_t inst_id)
ram54288 0:dbad57390bd1 144 {
ram54288 0:dbad57390bd1 145 tr_debug("M2MResource::remove_resource(inst_id %d)", inst_id);
ram54288 0:dbad57390bd1 146 bool success = false;
ram54288 0:dbad57390bd1 147 if(!_resource_instance_list.empty()) {
ram54288 0:dbad57390bd1 148 M2MResourceInstance* res = NULL;
ram54288 0:dbad57390bd1 149 M2MResourceInstanceList::const_iterator it;
ram54288 0:dbad57390bd1 150 it = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 151 int pos = 0;
ram54288 0:dbad57390bd1 152 for ( ; it != _resource_instance_list.end(); it++, pos++ ) {
ram54288 0:dbad57390bd1 153 if(((*it)->instance_id() == inst_id)) {
ram54288 0:dbad57390bd1 154 // Resource found and deleted.
ram54288 0:dbad57390bd1 155 res = *it;
ram54288 0:dbad57390bd1 156 delete res;
ram54288 0:dbad57390bd1 157 _resource_instance_list.erase(pos);
ram54288 0:dbad57390bd1 158 success = true;
ram54288 0:dbad57390bd1 159 break;
ram54288 0:dbad57390bd1 160 }
ram54288 0:dbad57390bd1 161 }
ram54288 0:dbad57390bd1 162 }
ram54288 0:dbad57390bd1 163 return success;
ram54288 0:dbad57390bd1 164 }
ram54288 0:dbad57390bd1 165
ram54288 0:dbad57390bd1 166 M2MResourceInstance* M2MResource::resource_instance(uint16_t inst_id) const
ram54288 0:dbad57390bd1 167 {
ram54288 0:dbad57390bd1 168 tr_debug("M2MResource::resource(resource_name inst_id %d)", inst_id);
ram54288 0:dbad57390bd1 169 M2MResourceInstance *res = NULL;
ram54288 0:dbad57390bd1 170 if(!_resource_instance_list.empty()) {
ram54288 0:dbad57390bd1 171 M2MResourceInstanceList::const_iterator it;
ram54288 0:dbad57390bd1 172 it = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 173 for ( ; it != _resource_instance_list.end(); it++ ) {
ram54288 0:dbad57390bd1 174 if(((*it)->instance_id() == inst_id)) {
ram54288 0:dbad57390bd1 175 // Resource found.
ram54288 0:dbad57390bd1 176 res = *it;
ram54288 0:dbad57390bd1 177 break;
ram54288 0:dbad57390bd1 178 }
ram54288 0:dbad57390bd1 179 }
ram54288 0:dbad57390bd1 180 }
ram54288 0:dbad57390bd1 181 return res;
ram54288 0:dbad57390bd1 182 }
ram54288 0:dbad57390bd1 183
ram54288 0:dbad57390bd1 184 const M2MResourceInstanceList& M2MResource::resource_instances() const
ram54288 0:dbad57390bd1 185 {
ram54288 0:dbad57390bd1 186 return _resource_instance_list;
ram54288 0:dbad57390bd1 187 }
ram54288 0:dbad57390bd1 188
ram54288 0:dbad57390bd1 189 uint16_t M2MResource::resource_instance_count() const
ram54288 0:dbad57390bd1 190 {
ram54288 0:dbad57390bd1 191 return (uint16_t)_resource_instance_list.size();
ram54288 0:dbad57390bd1 192 }
ram54288 0:dbad57390bd1 193
ram54288 0:dbad57390bd1 194 bool M2MResource::delayed_response() const
ram54288 0:dbad57390bd1 195 {
ram54288 0:dbad57390bd1 196 return _delayed_response;
ram54288 0:dbad57390bd1 197 }
ram54288 0:dbad57390bd1 198
ram54288 0:dbad57390bd1 199 bool M2MResource::handle_observation_attribute(const char *query)
ram54288 0:dbad57390bd1 200 {
ram54288 0:dbad57390bd1 201 tr_debug("M2MResource::handle_observation_attribute - is_under_observation(%d)", is_under_observation());
ram54288 0:dbad57390bd1 202 bool success = false;
ram54288 0:dbad57390bd1 203 M2MReportHandler *handler = M2MBase::report_handler();
ram54288 0:dbad57390bd1 204 if (!handler) {
ram54288 0:dbad57390bd1 205 handler = M2MBase::create_report_handler();
ram54288 0:dbad57390bd1 206 }
ram54288 0:dbad57390bd1 207
ram54288 0:dbad57390bd1 208 if (handler) {
ram54288 0:dbad57390bd1 209 success = handler->parse_notification_attribute(query,
ram54288 0:dbad57390bd1 210 M2MBase::base_type(), _resource_type);
ram54288 0:dbad57390bd1 211 if (success) {
ram54288 0:dbad57390bd1 212 if (is_under_observation()) {
ram54288 0:dbad57390bd1 213 handler->set_under_observation(true);
ram54288 0:dbad57390bd1 214 }
ram54288 0:dbad57390bd1 215 }
ram54288 0:dbad57390bd1 216 else {
ram54288 0:dbad57390bd1 217 handler->set_default_values();
ram54288 0:dbad57390bd1 218 }
ram54288 0:dbad57390bd1 219
ram54288 0:dbad57390bd1 220 if (success) {
ram54288 0:dbad57390bd1 221 if(!_resource_instance_list.empty()) {
ram54288 0:dbad57390bd1 222 M2MResourceInstanceList::const_iterator it;
ram54288 0:dbad57390bd1 223 it = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 224 for ( ; it != _resource_instance_list.end(); it++ ) {
ram54288 0:dbad57390bd1 225 M2MReportHandler *report_handler = (*it)->report_handler();
ram54288 0:dbad57390bd1 226 if(report_handler && is_under_observation()) {
ram54288 0:dbad57390bd1 227 report_handler->set_notification_trigger();
ram54288 0:dbad57390bd1 228 }
ram54288 0:dbad57390bd1 229 }
ram54288 0:dbad57390bd1 230 }
ram54288 0:dbad57390bd1 231 }
ram54288 0:dbad57390bd1 232 }
ram54288 0:dbad57390bd1 233 return success;
ram54288 0:dbad57390bd1 234 }
ram54288 0:dbad57390bd1 235
ram54288 0:dbad57390bd1 236 void M2MResource::add_observation_level(M2MBase::Observation observation_level)
ram54288 0:dbad57390bd1 237 {
ram54288 0:dbad57390bd1 238 M2MBase::add_observation_level(observation_level);
ram54288 0:dbad57390bd1 239 if(!_resource_instance_list.empty()) {
ram54288 0:dbad57390bd1 240 M2MResourceInstanceList::const_iterator inst;
ram54288 0:dbad57390bd1 241 inst = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 242 for ( ; inst != _resource_instance_list.end(); inst++ ) {
ram54288 0:dbad57390bd1 243 (*inst)->add_observation_level(observation_level);
ram54288 0:dbad57390bd1 244 }
ram54288 0:dbad57390bd1 245 }
ram54288 0:dbad57390bd1 246 }
ram54288 0:dbad57390bd1 247
ram54288 0:dbad57390bd1 248 void M2MResource::remove_observation_level(M2MBase::Observation observation_level)
ram54288 0:dbad57390bd1 249 {
ram54288 0:dbad57390bd1 250 M2MBase::remove_observation_level(observation_level);
ram54288 0:dbad57390bd1 251 if(!_resource_instance_list.empty()) {
ram54288 0:dbad57390bd1 252 M2MResourceInstanceList::const_iterator inst;
ram54288 0:dbad57390bd1 253 inst = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 254 for ( ; inst != _resource_instance_list.end(); inst++ ) {
ram54288 0:dbad57390bd1 255 (*inst)->remove_observation_level(observation_level);
ram54288 0:dbad57390bd1 256 }
ram54288 0:dbad57390bd1 257 }
ram54288 0:dbad57390bd1 258 }
ram54288 0:dbad57390bd1 259
ram54288 0:dbad57390bd1 260 void M2MResource::add_resource_instance(M2MResourceInstance *res)
ram54288 0:dbad57390bd1 261 {
ram54288 0:dbad57390bd1 262 tr_debug("M2MResource::add_resource_instance()");
ram54288 0:dbad57390bd1 263 if(res) {
ram54288 0:dbad57390bd1 264 _resource_instance_list.push_back(res);
ram54288 0:dbad57390bd1 265 }
ram54288 0:dbad57390bd1 266 }
ram54288 0:dbad57390bd1 267
ram54288 0:dbad57390bd1 268 sn_coap_hdr_s* M2MResource::handle_get_request(nsdl_s *nsdl,
ram54288 0:dbad57390bd1 269 sn_coap_hdr_s *received_coap_header,
ram54288 0:dbad57390bd1 270 M2MObservationHandler *observation_handler)
ram54288 0:dbad57390bd1 271 {
ram54288 0:dbad57390bd1 272 tr_debug("M2MResource::handle_get_request()");
ram54288 0:dbad57390bd1 273 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
ram54288 0:dbad57390bd1 274 sn_coap_hdr_s * coap_response = NULL;
ram54288 0:dbad57390bd1 275 if(_has_multiple_instances) {
ram54288 0:dbad57390bd1 276 coap_response = sn_nsdl_build_response(nsdl,
ram54288 0:dbad57390bd1 277 received_coap_header,
ram54288 0:dbad57390bd1 278 msg_code);
ram54288 0:dbad57390bd1 279 if(received_coap_header) {
ram54288 0:dbad57390bd1 280 // process the GET if we have registered a callback for it
ram54288 0:dbad57390bd1 281 if ((operation() & SN_GRS_GET_ALLOWED) != 0) {
ram54288 0:dbad57390bd1 282 if(coap_response) {
ram54288 0:dbad57390bd1 283 uint16_t coap_content_type = 0;
ram54288 0:dbad57390bd1 284 bool content_type_present = false;
ram54288 0:dbad57390bd1 285 if(received_coap_header->content_format != COAP_CT_NONE){
ram54288 0:dbad57390bd1 286 content_type_present = true;
ram54288 0:dbad57390bd1 287 coap_content_type = received_coap_header->content_format;
ram54288 0:dbad57390bd1 288 }
ram54288 0:dbad57390bd1 289
ram54288 0:dbad57390bd1 290 if(!content_type_present &&
ram54288 0:dbad57390bd1 291 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE) {
ram54288 0:dbad57390bd1 292 coap_content_type = COAP_CONTENT_OMA_TLV_TYPE;
ram54288 0:dbad57390bd1 293 }
ram54288 0:dbad57390bd1 294
ram54288 0:dbad57390bd1 295 tr_debug("M2MResource::handle_get_request() - Request Content-Type %d", coap_content_type);
ram54288 0:dbad57390bd1 296 if (coap_response->content_format == COAP_CT_NONE) {
ram54288 0:dbad57390bd1 297 coap_response->content_format = sn_coap_content_format_e(coap_content_type);
ram54288 0:dbad57390bd1 298 if (coap_response->content_format != COAP_CT_NONE) {
ram54288 0:dbad57390bd1 299 set_coap_content_type(coap_content_type);
ram54288 0:dbad57390bd1 300 }
ram54288 0:dbad57390bd1 301 }
ram54288 0:dbad57390bd1 302 uint8_t *data = NULL;
ram54288 0:dbad57390bd1 303 uint32_t data_length = 0;
ram54288 0:dbad57390bd1 304 // fill in the CoAP response payload
ram54288 0:dbad57390bd1 305 if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) {
ram54288 0:dbad57390bd1 306 M2MTLVSerializer serializer;
ram54288 0:dbad57390bd1 307 data = serializer.serialize(this, data_length);
ram54288 0:dbad57390bd1 308 } else {
ram54288 0:dbad57390bd1 309 msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; // Content format not supported
ram54288 0:dbad57390bd1 310 }
ram54288 0:dbad57390bd1 311
ram54288 0:dbad57390bd1 312 coap_response->payload_len = data_length;
ram54288 0:dbad57390bd1 313 coap_response->payload_ptr = data;
ram54288 0:dbad57390bd1 314
ram54288 0:dbad57390bd1 315 coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response);
ram54288 0:dbad57390bd1 316
ram54288 0:dbad57390bd1 317 coap_response->options_list_ptr->max_age = max_age();
ram54288 0:dbad57390bd1 318
ram54288 0:dbad57390bd1 319 if(received_coap_header->options_list_ptr) {
ram54288 0:dbad57390bd1 320 if(received_coap_header->options_list_ptr->observe != -1) {
ram54288 0:dbad57390bd1 321 if (is_observable()) {
ram54288 0:dbad57390bd1 322 uint32_t number = 0;
ram54288 0:dbad57390bd1 323 uint8_t observe_option = 0;
ram54288 0:dbad57390bd1 324 observe_option = received_coap_header->options_list_ptr->observe;
ram54288 0:dbad57390bd1 325 if(START_OBSERVATION == observe_option) {
ram54288 0:dbad57390bd1 326 tr_debug("M2MResource::handle_get_request - Starts Observation");
ram54288 0:dbad57390bd1 327 // If the observe length is 0 means register for observation.
ram54288 0:dbad57390bd1 328 if(received_coap_header->options_list_ptr->observe != -1) {
ram54288 0:dbad57390bd1 329 number = received_coap_header->options_list_ptr->observe;
ram54288 0:dbad57390bd1 330 }
ram54288 0:dbad57390bd1 331 if(received_coap_header->token_ptr) {
ram54288 0:dbad57390bd1 332 tr_debug("M2MResource::handle_get_request - Sets Observation Token to resource");
ram54288 0:dbad57390bd1 333 set_observation_token(received_coap_header->token_ptr,
ram54288 0:dbad57390bd1 334 received_coap_header->token_len);
ram54288 0:dbad57390bd1 335 }
ram54288 0:dbad57390bd1 336
ram54288 0:dbad57390bd1 337 // If the observe value is 0 means register for observation.
ram54288 0:dbad57390bd1 338 if(number == 0) {
ram54288 0:dbad57390bd1 339 tr_debug("M2MResource::handle_get_request - Put Resource under Observation");
ram54288 0:dbad57390bd1 340 M2MResourceInstanceList::const_iterator it;
ram54288 0:dbad57390bd1 341 it = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 342 for (; it!=_resource_instance_list.end(); it++ ) {
ram54288 0:dbad57390bd1 343 tr_debug("M2MResource::handle_get_request - set_resource_observer");
ram54288 0:dbad57390bd1 344 (*it)->set_resource_observer(this);
ram54288 0:dbad57390bd1 345 (*it)->add_observation_level(M2MBase::R_Attribute);
ram54288 0:dbad57390bd1 346 }
ram54288 0:dbad57390bd1 347 set_under_observation(true,observation_handler);
ram54288 0:dbad57390bd1 348 M2MBase::add_observation_level(M2MBase::R_Attribute);
ram54288 0:dbad57390bd1 349 coap_response->options_list_ptr->observe = observation_number();
ram54288 0:dbad57390bd1 350 }
ram54288 0:dbad57390bd1 351 } else if (STOP_OBSERVATION == observe_option) {
ram54288 0:dbad57390bd1 352 tr_debug("M2MResource::handle_get_request - Stops Observation");
ram54288 0:dbad57390bd1 353 set_under_observation(false,NULL);
ram54288 0:dbad57390bd1 354 M2MBase::remove_observation_level(M2MBase::R_Attribute);
ram54288 0:dbad57390bd1 355 M2MResourceInstanceList::const_iterator it;
ram54288 0:dbad57390bd1 356 it = _resource_instance_list.begin();
ram54288 0:dbad57390bd1 357 for (; it!=_resource_instance_list.end(); it++ ) {
ram54288 0:dbad57390bd1 358 (*it)->set_resource_observer(NULL);
ram54288 0:dbad57390bd1 359 }
ram54288 0:dbad57390bd1 360 }
ram54288 0:dbad57390bd1 361 msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
ram54288 0:dbad57390bd1 362 }
ram54288 0:dbad57390bd1 363 else {
ram54288 0:dbad57390bd1 364 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
ram54288 0:dbad57390bd1 365 }
ram54288 0:dbad57390bd1 366 }
ram54288 0:dbad57390bd1 367 }
ram54288 0:dbad57390bd1 368 }
ram54288 0:dbad57390bd1 369 } else {
ram54288 0:dbad57390bd1 370 tr_error("M2MResource::handle_get_request - Return COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
ram54288 0:dbad57390bd1 371 // Operation is not allowed.
ram54288 0:dbad57390bd1 372 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
ram54288 0:dbad57390bd1 373 }
ram54288 0:dbad57390bd1 374 }
ram54288 0:dbad57390bd1 375 if(coap_response) {
ram54288 0:dbad57390bd1 376 coap_response->msg_code = msg_code;
ram54288 0:dbad57390bd1 377 }
ram54288 0:dbad57390bd1 378 } else {
ram54288 0:dbad57390bd1 379 coap_response = M2MResourceInstance::handle_get_request(nsdl,
ram54288 0:dbad57390bd1 380 received_coap_header,
ram54288 0:dbad57390bd1 381 observation_handler);
ram54288 0:dbad57390bd1 382 }
ram54288 0:dbad57390bd1 383 return coap_response;
ram54288 0:dbad57390bd1 384 }
ram54288 0:dbad57390bd1 385
ram54288 0:dbad57390bd1 386 sn_coap_hdr_s* M2MResource::handle_put_request(nsdl_s *nsdl,
ram54288 0:dbad57390bd1 387 sn_coap_hdr_s *received_coap_header,
ram54288 0:dbad57390bd1 388 M2MObservationHandler *observation_handler,
ram54288 0:dbad57390bd1 389 bool &execute_value_updated)
ram54288 0:dbad57390bd1 390 {
ram54288 0:dbad57390bd1 391 tr_debug("M2MResource::handle_put_request()");
ram54288 0:dbad57390bd1 392 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
ram54288 0:dbad57390bd1 393 sn_coap_hdr_s * coap_response = NULL;
ram54288 0:dbad57390bd1 394 if(_has_multiple_instances) {
ram54288 0:dbad57390bd1 395 coap_response = sn_nsdl_build_response(nsdl,
ram54288 0:dbad57390bd1 396 received_coap_header,
ram54288 0:dbad57390bd1 397 msg_code);
ram54288 0:dbad57390bd1 398 // process the PUT if we have registered a callback for it
ram54288 0:dbad57390bd1 399 if(received_coap_header) {
ram54288 0:dbad57390bd1 400 uint16_t coap_content_type = 0;
ram54288 0:dbad57390bd1 401 bool content_type_present = false;
ram54288 0:dbad57390bd1 402 if(received_coap_header->content_format != COAP_CT_NONE && coap_response) {
ram54288 0:dbad57390bd1 403 content_type_present = true;
ram54288 0:dbad57390bd1 404 coap_content_type = received_coap_header->content_format;
ram54288 0:dbad57390bd1 405 }
ram54288 0:dbad57390bd1 406 if(received_coap_header->options_list_ptr &&
ram54288 0:dbad57390bd1 407 received_coap_header->options_list_ptr->uri_query_ptr) {
ram54288 0:dbad57390bd1 408 char *query = (char*)alloc_string_copy(received_coap_header->options_list_ptr->uri_query_ptr,
ram54288 0:dbad57390bd1 409 received_coap_header->options_list_ptr->uri_query_len);
ram54288 0:dbad57390bd1 410 if (query){
ram54288 0:dbad57390bd1 411 msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
ram54288 0:dbad57390bd1 412 tr_debug("M2MResource::handle_put_request() - Query %s", query);
ram54288 0:dbad57390bd1 413 // if anything was updated, re-initialize the stored notification attributes
ram54288 0:dbad57390bd1 414 if (!handle_observation_attribute(query)){
ram54288 0:dbad57390bd1 415 tr_debug("M2MResource::handle_put_request() - Invalid query");
ram54288 0:dbad57390bd1 416 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
ram54288 0:dbad57390bd1 417 }
ram54288 0:dbad57390bd1 418 free(query);
ram54288 0:dbad57390bd1 419 }
ram54288 0:dbad57390bd1 420 } else if ((operation() & SN_GRS_PUT_ALLOWED) != 0) {
ram54288 0:dbad57390bd1 421 if(!content_type_present &&
ram54288 0:dbad57390bd1 422 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE) {
ram54288 0:dbad57390bd1 423 coap_content_type = COAP_CONTENT_OMA_TLV_TYPE;
ram54288 0:dbad57390bd1 424 }
ram54288 0:dbad57390bd1 425
ram54288 0:dbad57390bd1 426 tr_debug("M2MResource::handle_put_request() - Request Content-Type %d", coap_content_type);
ram54288 0:dbad57390bd1 427
ram54288 0:dbad57390bd1 428 if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) {
ram54288 0:dbad57390bd1 429 M2MTLVDeserializer deserializer;
ram54288 0:dbad57390bd1 430 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
ram54288 0:dbad57390bd1 431 error = deserializer.deserialize_resource_instances(received_coap_header->payload_ptr,
ram54288 0:dbad57390bd1 432 received_coap_header->payload_len,
ram54288 0:dbad57390bd1 433 *this,
ram54288 0:dbad57390bd1 434 M2MTLVDeserializer::Put);
ram54288 0:dbad57390bd1 435 switch(error) {
ram54288 0:dbad57390bd1 436 case M2MTLVDeserializer::None:
ram54288 0:dbad57390bd1 437 if(observation_handler) {
ram54288 0:dbad57390bd1 438 String value = "";
ram54288 0:dbad57390bd1 439 if (received_coap_header->uri_path_ptr != NULL &&
ram54288 0:dbad57390bd1 440 received_coap_header->uri_path_len > 0) {
ram54288 0:dbad57390bd1 441
ram54288 0:dbad57390bd1 442 value.append_raw((char*)received_coap_header->uri_path_ptr,received_coap_header->uri_path_len);
ram54288 0:dbad57390bd1 443 }
ram54288 0:dbad57390bd1 444 execute_value_updated = true;
ram54288 0:dbad57390bd1 445 }
ram54288 0:dbad57390bd1 446 msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
ram54288 0:dbad57390bd1 447 break;
ram54288 0:dbad57390bd1 448 case M2MTLVDeserializer::NotFound:
ram54288 0:dbad57390bd1 449 msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
ram54288 0:dbad57390bd1 450 break;
ram54288 0:dbad57390bd1 451 case M2MTLVDeserializer::NotAllowed:
ram54288 0:dbad57390bd1 452 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
ram54288 0:dbad57390bd1 453 break;
ram54288 0:dbad57390bd1 454 case M2MTLVDeserializer::NotValid:
ram54288 0:dbad57390bd1 455 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
ram54288 0:dbad57390bd1 456 break;
ram54288 0:dbad57390bd1 457 }
ram54288 0:dbad57390bd1 458 } else {
ram54288 0:dbad57390bd1 459 msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
ram54288 0:dbad57390bd1 460 } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type)
ram54288 0:dbad57390bd1 461 } else {
ram54288 0:dbad57390bd1 462 // Operation is not allowed.
ram54288 0:dbad57390bd1 463 tr_error("M2MResource::handle_put_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
ram54288 0:dbad57390bd1 464 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
ram54288 0:dbad57390bd1 465 }
ram54288 0:dbad57390bd1 466 } else {
ram54288 0:dbad57390bd1 467 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
ram54288 0:dbad57390bd1 468 }
ram54288 0:dbad57390bd1 469 if(coap_response) {
ram54288 0:dbad57390bd1 470 coap_response->msg_code = msg_code;
ram54288 0:dbad57390bd1 471 }
ram54288 0:dbad57390bd1 472 } else {
ram54288 0:dbad57390bd1 473 coap_response = M2MResourceInstance::handle_put_request(nsdl,
ram54288 0:dbad57390bd1 474 received_coap_header,
ram54288 0:dbad57390bd1 475 observation_handler,
ram54288 0:dbad57390bd1 476 execute_value_updated);
ram54288 0:dbad57390bd1 477 }
ram54288 0:dbad57390bd1 478 return coap_response;
ram54288 0:dbad57390bd1 479 }
ram54288 0:dbad57390bd1 480
ram54288 0:dbad57390bd1 481 sn_coap_hdr_s* M2MResource::handle_post_request(nsdl_s *nsdl,
ram54288 0:dbad57390bd1 482 sn_coap_hdr_s *received_coap_header,
ram54288 0:dbad57390bd1 483 M2MObservationHandler */*observation_handler*/,
ram54288 0:dbad57390bd1 484 bool &/*execute_value_updated*/,
ram54288 0:dbad57390bd1 485 sn_nsdl_addr_s *address)
ram54288 0:dbad57390bd1 486 {
ram54288 0:dbad57390bd1 487 tr_debug("M2MResource::handle_post_request()");
ram54288 0:dbad57390bd1 488 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
ram54288 0:dbad57390bd1 489 sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
ram54288 0:dbad57390bd1 490 received_coap_header,
ram54288 0:dbad57390bd1 491 msg_code);
ram54288 0:dbad57390bd1 492 // process the POST if we have registered a callback for it
ram54288 0:dbad57390bd1 493 if(received_coap_header) {
ram54288 0:dbad57390bd1 494 if ((operation() & SN_GRS_POST_ALLOWED) != 0) {
ram54288 0:dbad57390bd1 495 M2MResource::M2MExecuteParameter *exec_params = new M2MResource::M2MExecuteParameter();
ram54288 0:dbad57390bd1 496 if (exec_params) {
ram54288 0:dbad57390bd1 497 exec_params->_object_name = object_name();
ram54288 0:dbad57390bd1 498 exec_params->_resource_name = name();
ram54288 0:dbad57390bd1 499 exec_params->_object_instance_id = object_instance_id();
ram54288 0:dbad57390bd1 500 }
ram54288 0:dbad57390bd1 501 uint16_t coap_content_type = 0;
ram54288 0:dbad57390bd1 502 if(received_coap_header->payload_ptr) {
ram54288 0:dbad57390bd1 503 if(received_coap_header->content_format != COAP_CT_NONE) {
ram54288 0:dbad57390bd1 504 coap_content_type = received_coap_header->content_format;
ram54288 0:dbad57390bd1 505 }
ram54288 0:dbad57390bd1 506 if(coap_content_type == 0) {
ram54288 0:dbad57390bd1 507 if (exec_params){
ram54288 0:dbad57390bd1 508 exec_params->_value = alloc_string_copy(received_coap_header->payload_ptr,
ram54288 0:dbad57390bd1 509 received_coap_header->payload_len);
ram54288 0:dbad57390bd1 510 if (exec_params->_value) {
ram54288 0:dbad57390bd1 511 exec_params->_value_length = received_coap_header->payload_len;
ram54288 0:dbad57390bd1 512 }
ram54288 0:dbad57390bd1 513 }
ram54288 0:dbad57390bd1 514 } else {
ram54288 0:dbad57390bd1 515 msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
ram54288 0:dbad57390bd1 516 }
ram54288 0:dbad57390bd1 517 }
ram54288 0:dbad57390bd1 518 if(COAP_MSG_CODE_RESPONSE_CHANGED == msg_code) {
ram54288 0:dbad57390bd1 519 tr_debug("M2MResource::handle_post_request - Execute resource function");
ram54288 0:dbad57390bd1 520 if(_delayed_response) {
ram54288 0:dbad57390bd1 521 msg_code = COAP_MSG_CODE_EMPTY;
ram54288 0:dbad57390bd1 522 coap_response->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
ram54288 0:dbad57390bd1 523 coap_response->msg_code = msg_code;
ram54288 0:dbad57390bd1 524 coap_response->msg_id = received_coap_header->msg_id;
ram54288 0:dbad57390bd1 525 if(received_coap_header->token_len) {
ram54288 0:dbad57390bd1 526 free(_delayed_token);
ram54288 0:dbad57390bd1 527 _delayed_token = NULL;
ram54288 0:dbad57390bd1 528 _delayed_token_len = 0;
ram54288 0:dbad57390bd1 529 _delayed_token = alloc_copy(received_coap_header->token_ptr, received_coap_header->token_len);
ram54288 0:dbad57390bd1 530 if(_delayed_token) {
ram54288 0:dbad57390bd1 531 _delayed_token_len = received_coap_header->token_len;
ram54288 0:dbad57390bd1 532 }
ram54288 0:dbad57390bd1 533 sn_nsdl_send_coap_message(nsdl, address, coap_response);
ram54288 0:dbad57390bd1 534 }
ram54288 0:dbad57390bd1 535 } else {
ram54288 0:dbad57390bd1 536 uint32_t length = 0;
ram54288 0:dbad57390bd1 537 get_value(coap_response->payload_ptr, length);
ram54288 0:dbad57390bd1 538 coap_response->payload_len = length;
ram54288 0:dbad57390bd1 539 }
ram54288 0:dbad57390bd1 540 execute(exec_params);
ram54288 0:dbad57390bd1 541 }
ram54288 0:dbad57390bd1 542 delete exec_params;
ram54288 0:dbad57390bd1 543 } else { // if ((object->operation() & SN_GRS_POST_ALLOWED) != 0)
ram54288 0:dbad57390bd1 544 tr_error("M2MResource::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
ram54288 0:dbad57390bd1 545 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.05
ram54288 0:dbad57390bd1 546 }
ram54288 0:dbad57390bd1 547 } else { //if(object && received_coap_header)
ram54288 0:dbad57390bd1 548 tr_error("M2MResource::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
ram54288 0:dbad57390bd1 549 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.01
ram54288 0:dbad57390bd1 550 }
ram54288 0:dbad57390bd1 551 if(coap_response) {
ram54288 0:dbad57390bd1 552 coap_response->msg_code = msg_code;
ram54288 0:dbad57390bd1 553 }
ram54288 0:dbad57390bd1 554 return coap_response;
ram54288 0:dbad57390bd1 555 }
ram54288 0:dbad57390bd1 556
ram54288 0:dbad57390bd1 557 void M2MResource::notification_update()
ram54288 0:dbad57390bd1 558 {
ram54288 0:dbad57390bd1 559 tr_debug("M2MResource::notification_update()");
ram54288 0:dbad57390bd1 560 M2MReportHandler *report_handler = M2MBase::report_handler();
ram54288 0:dbad57390bd1 561 if(report_handler && is_observable()) {
ram54288 0:dbad57390bd1 562 report_handler->set_notification_trigger();
ram54288 0:dbad57390bd1 563 }
ram54288 0:dbad57390bd1 564 }
ram54288 0:dbad57390bd1 565
ram54288 0:dbad57390bd1 566 M2MObjectInstance& M2MResource::get_parent_object_instance() const
ram54288 0:dbad57390bd1 567 {
ram54288 0:dbad57390bd1 568 return _parent;
ram54288 0:dbad57390bd1 569 }
ram54288 0:dbad57390bd1 570
ram54288 0:dbad57390bd1 571 const char* M2MResource::object_name() const
ram54288 0:dbad57390bd1 572 {
ram54288 0:dbad57390bd1 573 const M2MObjectInstance& parent_object_instance = _parent;
ram54288 0:dbad57390bd1 574 const M2MObject& parent_object = parent_object_instance.get_parent_object();
ram54288 0:dbad57390bd1 575
ram54288 0:dbad57390bd1 576 return parent_object.name();
ram54288 0:dbad57390bd1 577 }
ram54288 0:dbad57390bd1 578
ram54288 0:dbad57390bd1 579
ram54288 0:dbad57390bd1 580 M2MResource::M2MExecuteParameter::M2MExecuteParameter()
ram54288 0:dbad57390bd1 581 {
ram54288 0:dbad57390bd1 582 _value = NULL;
ram54288 0:dbad57390bd1 583 _value_length = 0;
ram54288 0:dbad57390bd1 584 _object_name = "";
ram54288 0:dbad57390bd1 585 _resource_name = "";
ram54288 0:dbad57390bd1 586 _object_instance_id = 0;
ram54288 0:dbad57390bd1 587 }
ram54288 0:dbad57390bd1 588
ram54288 0:dbad57390bd1 589 M2MResource::M2MExecuteParameter::~M2MExecuteParameter()
ram54288 0:dbad57390bd1 590 {
ram54288 0:dbad57390bd1 591 free(_value);
ram54288 0:dbad57390bd1 592 }
ram54288 0:dbad57390bd1 593
ram54288 0:dbad57390bd1 594 uint8_t *M2MResource::M2MExecuteParameter::get_argument_value() const
ram54288 0:dbad57390bd1 595 {
ram54288 0:dbad57390bd1 596 return _value;
ram54288 0:dbad57390bd1 597 }
ram54288 0:dbad57390bd1 598
ram54288 0:dbad57390bd1 599 uint16_t M2MResource::M2MExecuteParameter::get_argument_value_length() const
ram54288 0:dbad57390bd1 600 {
ram54288 0:dbad57390bd1 601 return _value_length;
ram54288 0:dbad57390bd1 602 }
ram54288 0:dbad57390bd1 603
ram54288 0:dbad57390bd1 604 const String& M2MResource::M2MExecuteParameter::get_argument_object_name() const
ram54288 0:dbad57390bd1 605 {
ram54288 0:dbad57390bd1 606 return _object_name;
ram54288 0:dbad57390bd1 607 }
ram54288 0:dbad57390bd1 608
ram54288 0:dbad57390bd1 609 const String& M2MResource::M2MExecuteParameter::get_argument_resource_name() const
ram54288 0:dbad57390bd1 610 {
ram54288 0:dbad57390bd1 611 return _resource_name;
ram54288 0:dbad57390bd1 612 }
ram54288 0:dbad57390bd1 613
ram54288 0:dbad57390bd1 614 uint16_t M2MResource::M2MExecuteParameter::get_argument_object_instance_id() const
ram54288 0:dbad57390bd1 615 {
ram54288 0:dbad57390bd1 616 return _object_instance_id;
ram54288 0:dbad57390bd1 617 }
ram54288 0:dbad57390bd1 618
ram54288 0:dbad57390bd1 619