mbed client on ethernet with LWIP

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by sandbox

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 11:cada08fc8a70 1 /*
mbedAustin 11:cada08fc8a70 2 * Copyright (c) 2015 ARM Limited. All rights reserved.
mbedAustin 11:cada08fc8a70 3 * SPDX-License-Identifier: Apache-2.0
mbedAustin 11:cada08fc8a70 4 * Licensed under the Apache License, Version 2.0 (the License); you may
mbedAustin 11:cada08fc8a70 5 * not use this file except in compliance with the License.
mbedAustin 11:cada08fc8a70 6 * You may obtain a copy of the License at
mbedAustin 11:cada08fc8a70 7 *
mbedAustin 11:cada08fc8a70 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 11:cada08fc8a70 9 *
mbedAustin 11:cada08fc8a70 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 11:cada08fc8a70 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
mbedAustin 11:cada08fc8a70 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 11:cada08fc8a70 13 * See the License for the specific language governing permissions and
mbedAustin 11:cada08fc8a70 14 * limitations under the License.
mbedAustin 11:cada08fc8a70 15 */
mbedAustin 11:cada08fc8a70 16 #include "mbed-client/m2mconstants.h"
mbedAustin 11:cada08fc8a70 17 #include "mbed-client/m2mresource.h"
mbedAustin 11:cada08fc8a70 18 #include "mbed-client/m2mobservationhandler.h"
mbedAustin 11:cada08fc8a70 19 #include "include/m2mreporthandler.h"
mbedAustin 11:cada08fc8a70 20 #include "include/m2mtlvserializer.h"
mbedAustin 11:cada08fc8a70 21 #include "include/m2mtlvdeserializer.h"
mbedAustin 11:cada08fc8a70 22 #include "include/nsdllinker.h"
mbedAustin 11:cada08fc8a70 23 #include "ns_trace.h"
mbedAustin 11:cada08fc8a70 24
mbedAustin 11:cada08fc8a70 25 M2MResource& M2MResource::operator=(const M2MResource& other)
mbedAustin 11:cada08fc8a70 26 {
mbedAustin 11:cada08fc8a70 27 if (this != &other) { // protect against invalid self-assignment
mbedAustin 11:cada08fc8a70 28 _has_multiple_instances = other._has_multiple_instances;
mbedAustin 11:cada08fc8a70 29 if(!other._resource_instance_list.empty()){
mbedAustin 11:cada08fc8a70 30 M2MResourceInstance* ins = NULL;
mbedAustin 11:cada08fc8a70 31 M2MResourceInstanceList::const_iterator it;
mbedAustin 11:cada08fc8a70 32 it = other._resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 33 for (; it!=other._resource_instance_list.end(); it++ ) {
mbedAustin 11:cada08fc8a70 34 ins = *it;
mbedAustin 11:cada08fc8a70 35 _resource_instance_list.push_back(new M2MResourceInstance(*ins));
mbedAustin 11:cada08fc8a70 36 }
mbedAustin 11:cada08fc8a70 37 }
mbedAustin 11:cada08fc8a70 38 }
mbedAustin 11:cada08fc8a70 39 return *this;
mbedAustin 11:cada08fc8a70 40 }
mbedAustin 11:cada08fc8a70 41
mbedAustin 11:cada08fc8a70 42 M2MResource::M2MResource(const M2MResource& other)
mbedAustin 11:cada08fc8a70 43 : M2MResourceInstance(other)
mbedAustin 11:cada08fc8a70 44 {
mbedAustin 11:cada08fc8a70 45 this->operator=(other);
mbedAustin 11:cada08fc8a70 46 }
mbedAustin 11:cada08fc8a70 47
mbedAustin 11:cada08fc8a70 48 M2MResource::M2MResource(M2MObjectInstanceCallback &object_instance_callback,
mbedAustin 11:cada08fc8a70 49 const String &resource_name,
mbedAustin 11:cada08fc8a70 50 const String &resource_type,
mbedAustin 11:cada08fc8a70 51 M2MResourceInstance::ResourceType type,
mbedAustin 11:cada08fc8a70 52 const uint8_t *value,
mbedAustin 11:cada08fc8a70 53 const uint8_t value_length,
mbedAustin 11:cada08fc8a70 54 bool multiple_instance)
mbedAustin 11:cada08fc8a70 55 : M2MResourceInstance(resource_name, resource_type, type, value, value_length,
mbedAustin 11:cada08fc8a70 56 object_instance_callback),
mbedAustin 11:cada08fc8a70 57 _has_multiple_instances(multiple_instance)
mbedAustin 11:cada08fc8a70 58 {
mbedAustin 11:cada08fc8a70 59 M2MBase::set_base_type(M2MBase::Resource);
mbedAustin 11:cada08fc8a70 60 M2MBase::set_operation(M2MBase::GET_ALLOWED);
mbedAustin 11:cada08fc8a70 61 M2MBase::set_observable(false);
mbedAustin 11:cada08fc8a70 62 }
mbedAustin 11:cada08fc8a70 63
mbedAustin 11:cada08fc8a70 64 M2MResource::M2MResource(M2MObjectInstanceCallback &object_instance_callback,
mbedAustin 11:cada08fc8a70 65 const String &resource_name,
mbedAustin 11:cada08fc8a70 66 const String &resource_type,
mbedAustin 11:cada08fc8a70 67 M2MResourceInstance::ResourceType type,
mbedAustin 11:cada08fc8a70 68 bool observable,
mbedAustin 11:cada08fc8a70 69 bool multiple_instance)
mbedAustin 11:cada08fc8a70 70 : M2MResourceInstance(resource_name, resource_type, type,
mbedAustin 11:cada08fc8a70 71 object_instance_callback),
mbedAustin 11:cada08fc8a70 72 _has_multiple_instances(multiple_instance)
mbedAustin 11:cada08fc8a70 73 {
mbedAustin 11:cada08fc8a70 74 M2MBase::set_base_type(M2MBase::Resource);
mbedAustin 11:cada08fc8a70 75 M2MBase::set_operation(M2MBase::GET_PUT_ALLOWED);
mbedAustin 11:cada08fc8a70 76 M2MBase::set_observable(observable);
mbedAustin 11:cada08fc8a70 77 }
mbedAustin 11:cada08fc8a70 78
mbedAustin 11:cada08fc8a70 79 M2MResource::~M2MResource()
mbedAustin 11:cada08fc8a70 80 {
mbedAustin 11:cada08fc8a70 81 if(!_resource_instance_list.empty()) {
mbedAustin 11:cada08fc8a70 82 M2MResourceInstance* res = NULL;
mbedAustin 11:cada08fc8a70 83 M2MResourceInstanceList::const_iterator it;
mbedAustin 11:cada08fc8a70 84 it = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 85 for (; it!=_resource_instance_list.end(); it++ ) {
mbedAustin 11:cada08fc8a70 86 //Free allocated memory for resources.
mbedAustin 11:cada08fc8a70 87 res = *it;
mbedAustin 11:cada08fc8a70 88 delete res;
mbedAustin 11:cada08fc8a70 89 res = NULL;
mbedAustin 11:cada08fc8a70 90 }
mbedAustin 11:cada08fc8a70 91 _resource_instance_list.clear();
mbedAustin 11:cada08fc8a70 92 }
mbedAustin 11:cada08fc8a70 93 }
mbedAustin 11:cada08fc8a70 94
mbedAustin 11:cada08fc8a70 95 bool M2MResource::supports_multiple_instances() const
mbedAustin 11:cada08fc8a70 96 {
mbedAustin 11:cada08fc8a70 97 return _has_multiple_instances;
mbedAustin 11:cada08fc8a70 98 }
mbedAustin 11:cada08fc8a70 99
mbedAustin 11:cada08fc8a70 100 bool M2MResource::remove_resource_instance(uint16_t inst_id)
mbedAustin 11:cada08fc8a70 101 {
mbedAustin 11:cada08fc8a70 102 tr_debug("M2MResource::remove_resource(inst_id %d)", inst_id);
mbedAustin 11:cada08fc8a70 103 bool success = false;
mbedAustin 11:cada08fc8a70 104 if(!_resource_instance_list.empty()) {
mbedAustin 11:cada08fc8a70 105 M2MResourceInstance* res = NULL;
mbedAustin 11:cada08fc8a70 106 M2MResourceInstanceList::const_iterator it;
mbedAustin 11:cada08fc8a70 107 it = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 108 int pos = 0;
mbedAustin 11:cada08fc8a70 109 for ( ; it != _resource_instance_list.end(); it++, pos++ ) {
mbedAustin 11:cada08fc8a70 110 if(((*it)->instance_id() == inst_id)) {
mbedAustin 11:cada08fc8a70 111 // Resource found and deleted.
mbedAustin 11:cada08fc8a70 112 res = *it;
mbedAustin 11:cada08fc8a70 113 delete res;
mbedAustin 11:cada08fc8a70 114 res = NULL;
mbedAustin 11:cada08fc8a70 115 _resource_instance_list.erase(pos);
mbedAustin 11:cada08fc8a70 116 success = true;
mbedAustin 11:cada08fc8a70 117 break;
mbedAustin 11:cada08fc8a70 118 }
mbedAustin 11:cada08fc8a70 119 }
mbedAustin 11:cada08fc8a70 120 }
mbedAustin 11:cada08fc8a70 121 return success;
mbedAustin 11:cada08fc8a70 122 }
mbedAustin 11:cada08fc8a70 123
mbedAustin 11:cada08fc8a70 124 M2MResourceInstance* M2MResource::resource_instance(uint16_t inst_id) const
mbedAustin 11:cada08fc8a70 125 {
mbedAustin 11:cada08fc8a70 126 tr_debug("M2MResource::resource(resource_name inst_id %d)", inst_id);
mbedAustin 11:cada08fc8a70 127 M2MResourceInstance *res = NULL;
mbedAustin 11:cada08fc8a70 128 if(!_resource_instance_list.empty()) {
mbedAustin 11:cada08fc8a70 129 M2MResourceInstanceList::const_iterator it;
mbedAustin 11:cada08fc8a70 130 it = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 131 for ( ; it != _resource_instance_list.end(); it++ ) {
mbedAustin 11:cada08fc8a70 132 if(((*it)->instance_id() == inst_id)) {
mbedAustin 11:cada08fc8a70 133 // Resource found.
mbedAustin 11:cada08fc8a70 134 res = *it;
mbedAustin 11:cada08fc8a70 135 break;
mbedAustin 11:cada08fc8a70 136 }
mbedAustin 11:cada08fc8a70 137 }
mbedAustin 11:cada08fc8a70 138 }
mbedAustin 11:cada08fc8a70 139 return res;
mbedAustin 11:cada08fc8a70 140 }
mbedAustin 11:cada08fc8a70 141
mbedAustin 11:cada08fc8a70 142 const M2MResourceInstanceList& M2MResource::resource_instances() const
mbedAustin 11:cada08fc8a70 143 {
mbedAustin 11:cada08fc8a70 144 return _resource_instance_list;
mbedAustin 11:cada08fc8a70 145 }
mbedAustin 11:cada08fc8a70 146
mbedAustin 11:cada08fc8a70 147 uint16_t M2MResource::resource_instance_count() const
mbedAustin 11:cada08fc8a70 148 {
mbedAustin 11:cada08fc8a70 149 return (uint16_t)_resource_instance_list.size();
mbedAustin 11:cada08fc8a70 150 }
mbedAustin 11:cada08fc8a70 151
mbedAustin 11:cada08fc8a70 152 bool M2MResource::handle_observation_attribute(char *&query)
mbedAustin 11:cada08fc8a70 153 {
mbedAustin 11:cada08fc8a70 154 tr_debug("M2MResource::handle_observation_attribute");
mbedAustin 11:cada08fc8a70 155 bool success = false;
mbedAustin 11:cada08fc8a70 156 M2MReportHandler *handler = M2MBase::report_handler();
mbedAustin 11:cada08fc8a70 157 if (handler) {
mbedAustin 11:cada08fc8a70 158 success = handler->parse_notification_attribute(query,
mbedAustin 11:cada08fc8a70 159 M2MBase::base_type(), _resource_type);
mbedAustin 11:cada08fc8a70 160 if (success) {
mbedAustin 11:cada08fc8a70 161 if ((handler->attribute_flags() & M2MReportHandler::Cancel) == 0) {
mbedAustin 11:cada08fc8a70 162 handler->set_under_observation(true);
mbedAustin 11:cada08fc8a70 163 } else {
mbedAustin 11:cada08fc8a70 164 handler->set_under_observation(false);
mbedAustin 11:cada08fc8a70 165 }
mbedAustin 11:cada08fc8a70 166 }
mbedAustin 11:cada08fc8a70 167 else {
mbedAustin 11:cada08fc8a70 168 handler->set_default_values();
mbedAustin 11:cada08fc8a70 169 }
mbedAustin 11:cada08fc8a70 170
mbedAustin 11:cada08fc8a70 171 if (success) {
mbedAustin 11:cada08fc8a70 172 if(!_resource_instance_list.empty()) {
mbedAustin 11:cada08fc8a70 173 M2MResourceInstanceList::const_iterator it;
mbedAustin 11:cada08fc8a70 174 it = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 175 for ( ; it != _resource_instance_list.end(); it++ ) {
mbedAustin 11:cada08fc8a70 176 M2MReportHandler *report_handler = (*it)->report_handler();
mbedAustin 11:cada08fc8a70 177 if(report_handler && is_observable()) {
mbedAustin 11:cada08fc8a70 178 report_handler->set_notification_trigger();
mbedAustin 11:cada08fc8a70 179 }
mbedAustin 11:cada08fc8a70 180 }
mbedAustin 11:cada08fc8a70 181 }
mbedAustin 11:cada08fc8a70 182 }
mbedAustin 11:cada08fc8a70 183 }
mbedAustin 11:cada08fc8a70 184 return success;
mbedAustin 11:cada08fc8a70 185 }
mbedAustin 11:cada08fc8a70 186
mbedAustin 11:cada08fc8a70 187 void M2MResource::add_observation_level(M2MBase::Observation observation_level)
mbedAustin 11:cada08fc8a70 188 {
mbedAustin 11:cada08fc8a70 189 M2MBase::add_observation_level(observation_level);
mbedAustin 11:cada08fc8a70 190 if(!_resource_instance_list.empty()) {
mbedAustin 11:cada08fc8a70 191 M2MResourceInstanceList::const_iterator inst;
mbedAustin 11:cada08fc8a70 192 inst = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 193 for ( ; inst != _resource_instance_list.end(); inst++ ) {
mbedAustin 11:cada08fc8a70 194 (*inst)->add_observation_level(observation_level);
mbedAustin 11:cada08fc8a70 195 }
mbedAustin 11:cada08fc8a70 196 }
mbedAustin 11:cada08fc8a70 197 }
mbedAustin 11:cada08fc8a70 198
mbedAustin 11:cada08fc8a70 199 void M2MResource::remove_observation_level(M2MBase::Observation observation_level)
mbedAustin 11:cada08fc8a70 200 {
mbedAustin 11:cada08fc8a70 201 M2MBase::remove_observation_level(observation_level);
mbedAustin 11:cada08fc8a70 202 if(!_resource_instance_list.empty()) {
mbedAustin 11:cada08fc8a70 203 M2MResourceInstanceList::const_iterator inst;
mbedAustin 11:cada08fc8a70 204 inst = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 205 for ( ; inst != _resource_instance_list.end(); inst++ ) {
mbedAustin 11:cada08fc8a70 206 (*inst)->remove_observation_level(observation_level);
mbedAustin 11:cada08fc8a70 207 }
mbedAustin 11:cada08fc8a70 208 }
mbedAustin 11:cada08fc8a70 209 }
mbedAustin 11:cada08fc8a70 210
mbedAustin 11:cada08fc8a70 211 void M2MResource::add_resource_instance(M2MResourceInstance *res)
mbedAustin 11:cada08fc8a70 212 {
mbedAustin 11:cada08fc8a70 213 tr_debug("M2MResource::add_resource_instance()");
mbedAustin 11:cada08fc8a70 214 if(res) {
mbedAustin 11:cada08fc8a70 215 _resource_instance_list.push_back(res);
mbedAustin 11:cada08fc8a70 216 }
mbedAustin 11:cada08fc8a70 217 }
mbedAustin 11:cada08fc8a70 218
mbedAustin 11:cada08fc8a70 219 sn_coap_hdr_s* M2MResource::handle_get_request(nsdl_s *nsdl,
mbedAustin 11:cada08fc8a70 220 sn_coap_hdr_s *received_coap_header,
mbedAustin 11:cada08fc8a70 221 M2MObservationHandler *observation_handler)
mbedAustin 11:cada08fc8a70 222 {
mbedAustin 11:cada08fc8a70 223 tr_debug("M2MResource::handle_get_request()");
mbedAustin 11:cada08fc8a70 224 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
mbedAustin 11:cada08fc8a70 225 sn_coap_hdr_s * coap_response = NULL;
mbedAustin 11:cada08fc8a70 226 if(_has_multiple_instances) {
mbedAustin 11:cada08fc8a70 227 coap_response = sn_nsdl_build_response(nsdl,
mbedAustin 11:cada08fc8a70 228 received_coap_header,
mbedAustin 11:cada08fc8a70 229 msg_code);
mbedAustin 11:cada08fc8a70 230 if(received_coap_header) {
mbedAustin 11:cada08fc8a70 231 // process the GET if we have registered a callback for it
mbedAustin 11:cada08fc8a70 232 if ((operation() & SN_GRS_GET_ALLOWED) != 0) {
mbedAustin 11:cada08fc8a70 233 if(coap_response) {
mbedAustin 11:cada08fc8a70 234 uint16_t coap_content_type = 0;
mbedAustin 11:cada08fc8a70 235 bool content_type_present = false;
mbedAustin 11:cada08fc8a70 236 if(received_coap_header->content_type_ptr){
mbedAustin 11:cada08fc8a70 237 content_type_present = true;
mbedAustin 11:cada08fc8a70 238 coap_response->content_type_ptr = (uint8_t*)malloc(received_coap_header->content_type_len);
mbedAustin 11:cada08fc8a70 239 if(coap_response->content_type_ptr) {
mbedAustin 11:cada08fc8a70 240 memset(coap_response->content_type_ptr, 0, received_coap_header->content_type_len);
mbedAustin 11:cada08fc8a70 241 memcpy(coap_response->content_type_ptr,
mbedAustin 11:cada08fc8a70 242 received_coap_header->content_type_ptr,
mbedAustin 11:cada08fc8a70 243 received_coap_header->content_type_len);
mbedAustin 11:cada08fc8a70 244 coap_response->content_type_len = received_coap_header->content_type_len;
mbedAustin 11:cada08fc8a70 245 for(uint8_t i = 0; i < coap_response->content_type_len; i++) {
mbedAustin 11:cada08fc8a70 246 coap_content_type = (coap_content_type << 8) + (coap_response->content_type_ptr[i] & 0xFF);
mbedAustin 11:cada08fc8a70 247 }
mbedAustin 11:cada08fc8a70 248 }
mbedAustin 11:cada08fc8a70 249 }
mbedAustin 11:cada08fc8a70 250
mbedAustin 11:cada08fc8a70 251 if(!content_type_present &&
mbedAustin 11:cada08fc8a70 252 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE) {
mbedAustin 11:cada08fc8a70 253 coap_content_type = COAP_CONTENT_OMA_TLV_TYPE;
mbedAustin 11:cada08fc8a70 254 }
mbedAustin 11:cada08fc8a70 255
mbedAustin 11:cada08fc8a70 256 tr_debug("M2MResource::handle_get_request() - Request Content-Type %d", coap_content_type);
mbedAustin 11:cada08fc8a70 257
mbedAustin 11:cada08fc8a70 258 uint8_t *data = NULL;
mbedAustin 11:cada08fc8a70 259 uint32_t data_length = 0;
mbedAustin 11:cada08fc8a70 260 // fill in the CoAP response payload
mbedAustin 11:cada08fc8a70 261 if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) {
mbedAustin 11:cada08fc8a70 262 M2MTLVSerializer *serializer = new M2MTLVSerializer();
mbedAustin 11:cada08fc8a70 263 if(serializer) {
mbedAustin 11:cada08fc8a70 264 data = serializer->serialize(this, data_length);
mbedAustin 11:cada08fc8a70 265 delete serializer;
mbedAustin 11:cada08fc8a70 266 }
mbedAustin 11:cada08fc8a70 267 } else {
mbedAustin 11:cada08fc8a70 268 msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; // Content format not supported
mbedAustin 11:cada08fc8a70 269 }
mbedAustin 11:cada08fc8a70 270
mbedAustin 11:cada08fc8a70 271 coap_response->payload_len = data_length;
mbedAustin 11:cada08fc8a70 272 coap_response->payload_ptr = data;
mbedAustin 11:cada08fc8a70 273
mbedAustin 11:cada08fc8a70 274 coap_response->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
mbedAustin 11:cada08fc8a70 275 memset(coap_response->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
mbedAustin 11:cada08fc8a70 276
mbedAustin 11:cada08fc8a70 277 coap_response->options_list_ptr->max_age_ptr = (uint8_t*)malloc(1);
mbedAustin 11:cada08fc8a70 278 memset(coap_response->options_list_ptr->max_age_ptr,0,1);
mbedAustin 11:cada08fc8a70 279 coap_response->options_list_ptr->max_age_len = 1;
mbedAustin 11:cada08fc8a70 280
mbedAustin 11:cada08fc8a70 281 if(received_coap_header->token_ptr) {
mbedAustin 11:cada08fc8a70 282 tr_debug("M2MResource::handle_get_request - Sets Observation Token to resource");
mbedAustin 11:cada08fc8a70 283 set_observation_token(received_coap_header->token_ptr,
mbedAustin 11:cada08fc8a70 284 received_coap_header->token_len);
mbedAustin 11:cada08fc8a70 285 }
mbedAustin 11:cada08fc8a70 286
mbedAustin 11:cada08fc8a70 287 if(received_coap_header->options_list_ptr) {
mbedAustin 11:cada08fc8a70 288 if(received_coap_header->options_list_ptr->observe) {
mbedAustin 11:cada08fc8a70 289 if (is_observable()) {
mbedAustin 11:cada08fc8a70 290 uint32_t number = 0;
mbedAustin 11:cada08fc8a70 291 uint8_t observe_option = 0;
mbedAustin 11:cada08fc8a70 292 if(received_coap_header->options_list_ptr->observe_ptr) {
mbedAustin 11:cada08fc8a70 293 observe_option = *received_coap_header->options_list_ptr->observe_ptr;
mbedAustin 11:cada08fc8a70 294 }
mbedAustin 11:cada08fc8a70 295 if(START_OBSERVATION == observe_option) {
mbedAustin 11:cada08fc8a70 296 tr_debug("M2MResource::handle_get_request - Starts Observation");
mbedAustin 11:cada08fc8a70 297 // If the observe length is 0 means register for observation.
mbedAustin 11:cada08fc8a70 298 if(received_coap_header->options_list_ptr->observe_len != 0) {
mbedAustin 11:cada08fc8a70 299 for(int i=0;i < received_coap_header->options_list_ptr->observe_len; i++) {
mbedAustin 11:cada08fc8a70 300 number = (*(received_coap_header->options_list_ptr->observe_ptr + i) & 0xff) <<
mbedAustin 11:cada08fc8a70 301 8*(received_coap_header->options_list_ptr->observe_len- 1 - i);
mbedAustin 11:cada08fc8a70 302 }
mbedAustin 11:cada08fc8a70 303 }
mbedAustin 11:cada08fc8a70 304 // If the observe value is 0 means register for observation.
mbedAustin 11:cada08fc8a70 305 if(number == 0) {
mbedAustin 11:cada08fc8a70 306 tr_debug("M2MResource::handle_get_request - Put Resource under Observation");
mbedAustin 11:cada08fc8a70 307 M2MResourceInstanceList::const_iterator it;
mbedAustin 11:cada08fc8a70 308 it = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 309 for (; it!=_resource_instance_list.end(); it++ ) {
mbedAustin 11:cada08fc8a70 310 tr_debug("M2MResource::handle_get_request - set_resource_observer");
mbedAustin 11:cada08fc8a70 311 (*it)->set_resource_observer(this);
mbedAustin 11:cada08fc8a70 312 }
mbedAustin 11:cada08fc8a70 313
mbedAustin 11:cada08fc8a70 314 set_under_observation(true,observation_handler);
mbedAustin 11:cada08fc8a70 315 M2MBase::add_observation_level(M2MBase::R_Attribute);
mbedAustin 11:cada08fc8a70 316
mbedAustin 11:cada08fc8a70 317 uint8_t *obs_number = (uint8_t*)malloc(3);
mbedAustin 11:cada08fc8a70 318 memset(obs_number,0,3);
mbedAustin 11:cada08fc8a70 319 uint8_t observation_number_length = 1;
mbedAustin 11:cada08fc8a70 320
mbedAustin 11:cada08fc8a70 321 uint16_t number = observation_number();
mbedAustin 11:cada08fc8a70 322
mbedAustin 11:cada08fc8a70 323 tr_debug("M2MResource::handle_get_request - Observation Number %d", number);
mbedAustin 11:cada08fc8a70 324 obs_number[0] = ((number>>8) & 0xFF);
mbedAustin 11:cada08fc8a70 325 obs_number[1] = (number & 0xFF);
mbedAustin 11:cada08fc8a70 326
mbedAustin 11:cada08fc8a70 327 if(number > 0xFF) {
mbedAustin 11:cada08fc8a70 328 observation_number_length = 2;
mbedAustin 11:cada08fc8a70 329 }
mbedAustin 11:cada08fc8a70 330 coap_response->options_list_ptr->observe_ptr = obs_number;
mbedAustin 11:cada08fc8a70 331 coap_response->options_list_ptr->observe_len = observation_number_length;
mbedAustin 11:cada08fc8a70 332 }
mbedAustin 11:cada08fc8a70 333 } else if (STOP_OBSERVATION == observe_option) {
mbedAustin 11:cada08fc8a70 334 tr_debug("M2MResource::handle_get_request - Stops Observation");
mbedAustin 11:cada08fc8a70 335 set_under_observation(false,NULL);
mbedAustin 11:cada08fc8a70 336 M2MBase::remove_observation_level(M2MBase::R_Attribute);
mbedAustin 11:cada08fc8a70 337 M2MResourceInstanceList::const_iterator it;
mbedAustin 11:cada08fc8a70 338 it = _resource_instance_list.begin();
mbedAustin 11:cada08fc8a70 339 for (; it!=_resource_instance_list.end(); it++ ) {
mbedAustin 11:cada08fc8a70 340 (*it)->set_resource_observer(NULL);
mbedAustin 11:cada08fc8a70 341 }
mbedAustin 11:cada08fc8a70 342 }
mbedAustin 11:cada08fc8a70 343 msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
mbedAustin 11:cada08fc8a70 344 }
mbedAustin 11:cada08fc8a70 345 else {
mbedAustin 11:cada08fc8a70 346 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
mbedAustin 11:cada08fc8a70 347 }
mbedAustin 11:cada08fc8a70 348 }
mbedAustin 11:cada08fc8a70 349 }
mbedAustin 11:cada08fc8a70 350 }
mbedAustin 11:cada08fc8a70 351 }else {
mbedAustin 11:cada08fc8a70 352 tr_error("M2MResource::handle_get_request - Return COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
mbedAustin 11:cada08fc8a70 353 // Operation is not allowed.
mbedAustin 11:cada08fc8a70 354 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
mbedAustin 11:cada08fc8a70 355 }
mbedAustin 11:cada08fc8a70 356 }
mbedAustin 11:cada08fc8a70 357 if(coap_response) {
mbedAustin 11:cada08fc8a70 358 coap_response->msg_code = msg_code;
mbedAustin 11:cada08fc8a70 359 }
mbedAustin 11:cada08fc8a70 360 } else {
mbedAustin 11:cada08fc8a70 361 coap_response = M2MResourceInstance::handle_get_request(nsdl,
mbedAustin 11:cada08fc8a70 362 received_coap_header,
mbedAustin 11:cada08fc8a70 363 observation_handler);
mbedAustin 11:cada08fc8a70 364 }
mbedAustin 11:cada08fc8a70 365 return coap_response;
mbedAustin 11:cada08fc8a70 366 }
mbedAustin 11:cada08fc8a70 367
mbedAustin 11:cada08fc8a70 368 sn_coap_hdr_s* M2MResource::handle_put_request(nsdl_s *nsdl,
mbedAustin 11:cada08fc8a70 369 sn_coap_hdr_s *received_coap_header,
mbedAustin 11:cada08fc8a70 370 M2MObservationHandler *observation_handler)
mbedAustin 11:cada08fc8a70 371 {
mbedAustin 11:cada08fc8a70 372 tr_debug("M2MResource::handle_put_request()");
mbedAustin 11:cada08fc8a70 373 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
mbedAustin 11:cada08fc8a70 374 sn_coap_hdr_s * coap_response = NULL;
mbedAustin 11:cada08fc8a70 375 if(_has_multiple_instances) {
mbedAustin 11:cada08fc8a70 376 coap_response = sn_nsdl_build_response(nsdl,
mbedAustin 11:cada08fc8a70 377 received_coap_header,
mbedAustin 11:cada08fc8a70 378 msg_code);
mbedAustin 11:cada08fc8a70 379 // process the PUT if we have registered a callback for it
mbedAustin 11:cada08fc8a70 380 if(received_coap_header) {
mbedAustin 11:cada08fc8a70 381 if ((operation() & SN_GRS_PUT_ALLOWED) != 0) {
mbedAustin 11:cada08fc8a70 382 uint16_t coap_content_type = 0;
mbedAustin 11:cada08fc8a70 383 bool content_type_present = false;
mbedAustin 11:cada08fc8a70 384 if(received_coap_header->content_type_ptr) {
mbedAustin 11:cada08fc8a70 385 if(coap_response) {
mbedAustin 11:cada08fc8a70 386 content_type_present = true;
mbedAustin 11:cada08fc8a70 387 coap_response->content_type_ptr = (uint8_t*)malloc(received_coap_header->content_type_len);
mbedAustin 11:cada08fc8a70 388 if(coap_response->content_type_ptr) {
mbedAustin 11:cada08fc8a70 389 memset(coap_response->content_type_ptr, 0, received_coap_header->content_type_len);
mbedAustin 11:cada08fc8a70 390 memcpy(coap_response->content_type_ptr,
mbedAustin 11:cada08fc8a70 391 received_coap_header->content_type_ptr,
mbedAustin 11:cada08fc8a70 392 received_coap_header->content_type_len);
mbedAustin 11:cada08fc8a70 393 coap_response->content_type_len = received_coap_header->content_type_len;
mbedAustin 11:cada08fc8a70 394 for(uint8_t i = 0; i < coap_response->content_type_len; i++) {
mbedAustin 11:cada08fc8a70 395 coap_content_type = (coap_content_type << 8) + (coap_response->content_type_ptr[i] & 0xFF);
mbedAustin 11:cada08fc8a70 396 }
mbedAustin 11:cada08fc8a70 397 }
mbedAustin 11:cada08fc8a70 398 }
mbedAustin 11:cada08fc8a70 399 }
mbedAustin 11:cada08fc8a70 400 if(!content_type_present &&
mbedAustin 11:cada08fc8a70 401 M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE) {
mbedAustin 11:cada08fc8a70 402 coap_content_type = COAP_CONTENT_OMA_TLV_TYPE;
mbedAustin 11:cada08fc8a70 403 }
mbedAustin 11:cada08fc8a70 404
mbedAustin 11:cada08fc8a70 405 tr_debug("M2MResource::handle_put_request() - Request Content-Type %d", coap_content_type);
mbedAustin 11:cada08fc8a70 406
mbedAustin 11:cada08fc8a70 407 if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) {
mbedAustin 11:cada08fc8a70 408 M2MTLVDeserializer *deserializer = new M2MTLVDeserializer();
mbedAustin 11:cada08fc8a70 409 if(deserializer) {
mbedAustin 11:cada08fc8a70 410 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
mbedAustin 11:cada08fc8a70 411 error = deserializer->deserialize_resource_instances(received_coap_header->payload_ptr,
mbedAustin 11:cada08fc8a70 412 received_coap_header->payload_len,
mbedAustin 11:cada08fc8a70 413 *this,
mbedAustin 11:cada08fc8a70 414 M2MTLVDeserializer::Put);
mbedAustin 11:cada08fc8a70 415 switch(error) {
mbedAustin 11:cada08fc8a70 416 case M2MTLVDeserializer::None:
mbedAustin 11:cada08fc8a70 417 if(observation_handler) {
mbedAustin 11:cada08fc8a70 418 String value = "";
mbedAustin 11:cada08fc8a70 419 if (received_coap_header->uri_path_ptr != NULL &&
mbedAustin 11:cada08fc8a70 420 received_coap_header->uri_path_len > 0) {
mbedAustin 11:cada08fc8a70 421 char* buf = (char*)malloc(received_coap_header->uri_path_len+1);
mbedAustin 11:cada08fc8a70 422 if(buf) {
mbedAustin 11:cada08fc8a70 423 memset(buf,0,received_coap_header->uri_path_len+1);
mbedAustin 11:cada08fc8a70 424 memcpy(buf,received_coap_header->uri_path_ptr,received_coap_header->uri_path_len);
mbedAustin 11:cada08fc8a70 425 value = String(buf);
mbedAustin 11:cada08fc8a70 426 free(buf);
mbedAustin 11:cada08fc8a70 427 }
mbedAustin 11:cada08fc8a70 428 }
mbedAustin 11:cada08fc8a70 429 observation_handler->value_updated(this,value);
mbedAustin 11:cada08fc8a70 430 }
mbedAustin 11:cada08fc8a70 431 msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
mbedAustin 11:cada08fc8a70 432 break;
mbedAustin 11:cada08fc8a70 433 case M2MTLVDeserializer::NotFound:
mbedAustin 11:cada08fc8a70 434 msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
mbedAustin 11:cada08fc8a70 435 break;
mbedAustin 11:cada08fc8a70 436 case M2MTLVDeserializer::NotAllowed:
mbedAustin 11:cada08fc8a70 437 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
mbedAustin 11:cada08fc8a70 438 break;
mbedAustin 11:cada08fc8a70 439 case M2MTLVDeserializer::NotValid:
mbedAustin 11:cada08fc8a70 440 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
mbedAustin 11:cada08fc8a70 441 break;
mbedAustin 11:cada08fc8a70 442 }
mbedAustin 11:cada08fc8a70 443 delete deserializer;
mbedAustin 11:cada08fc8a70 444 }
mbedAustin 11:cada08fc8a70 445 } else {
mbedAustin 11:cada08fc8a70 446 msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
mbedAustin 11:cada08fc8a70 447 } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type)
mbedAustin 11:cada08fc8a70 448
mbedAustin 11:cada08fc8a70 449 if(received_coap_header->options_list_ptr &&
mbedAustin 11:cada08fc8a70 450 received_coap_header->options_list_ptr->uri_query_ptr) {
mbedAustin 11:cada08fc8a70 451 char *query = (char*)malloc(received_coap_header->options_list_ptr->uri_query_len+1);
mbedAustin 11:cada08fc8a70 452 if (query){
mbedAustin 11:cada08fc8a70 453 msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
mbedAustin 11:cada08fc8a70 454 memset(query, 0, received_coap_header->options_list_ptr->uri_query_len+1);
mbedAustin 11:cada08fc8a70 455 memcpy(query,
mbedAustin 11:cada08fc8a70 456 received_coap_header->options_list_ptr->uri_query_ptr,
mbedAustin 11:cada08fc8a70 457 received_coap_header->options_list_ptr->uri_query_len);
mbedAustin 11:cada08fc8a70 458 memset(query + received_coap_header->options_list_ptr->uri_query_len,'\0',1);//String terminator
mbedAustin 11:cada08fc8a70 459 tr_debug("M2MResource::handle_put_request() - Query %s", query);
mbedAustin 11:cada08fc8a70 460 // if anything was updated, re-initialize the stored notification attributes
mbedAustin 11:cada08fc8a70 461 if (!handle_observation_attribute(query)){
mbedAustin 11:cada08fc8a70 462 tr_debug("M2MResource::handle_put_request() - Invalid query");
mbedAustin 11:cada08fc8a70 463 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
mbedAustin 11:cada08fc8a70 464 }
mbedAustin 11:cada08fc8a70 465 free(query);
mbedAustin 11:cada08fc8a70 466 }
mbedAustin 11:cada08fc8a70 467 }
mbedAustin 11:cada08fc8a70 468 } else {
mbedAustin 11:cada08fc8a70 469 // Operation is not allowed.
mbedAustin 11:cada08fc8a70 470 tr_error("M2MResource::handle_put_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
mbedAustin 11:cada08fc8a70 471 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
mbedAustin 11:cada08fc8a70 472 }
mbedAustin 11:cada08fc8a70 473 } else {
mbedAustin 11:cada08fc8a70 474 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
mbedAustin 11:cada08fc8a70 475 }
mbedAustin 11:cada08fc8a70 476 if(coap_response) {
mbedAustin 11:cada08fc8a70 477 coap_response->msg_code = msg_code;
mbedAustin 11:cada08fc8a70 478 }
mbedAustin 11:cada08fc8a70 479 } else {
mbedAustin 11:cada08fc8a70 480 coap_response = M2MResourceInstance::handle_put_request(nsdl,
mbedAustin 11:cada08fc8a70 481 received_coap_header,
mbedAustin 11:cada08fc8a70 482 observation_handler);
mbedAustin 11:cada08fc8a70 483 }
mbedAustin 11:cada08fc8a70 484 return coap_response;
mbedAustin 11:cada08fc8a70 485 }
mbedAustin 11:cada08fc8a70 486
mbedAustin 11:cada08fc8a70 487 sn_coap_hdr_s* M2MResource::handle_post_request(nsdl_s *nsdl,
mbedAustin 11:cada08fc8a70 488 sn_coap_hdr_s *received_coap_header,
mbedAustin 11:cada08fc8a70 489 M2MObservationHandler */*observation_handler*/)
mbedAustin 11:cada08fc8a70 490 {
mbedAustin 11:cada08fc8a70 491 tr_debug("M2MResource::handle_post_request()");
mbedAustin 11:cada08fc8a70 492 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
mbedAustin 11:cada08fc8a70 493 sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl,
mbedAustin 11:cada08fc8a70 494 received_coap_header,
mbedAustin 11:cada08fc8a70 495 msg_code);
mbedAustin 11:cada08fc8a70 496 // process the POST if we have registered a callback for it
mbedAustin 11:cada08fc8a70 497 if(received_coap_header) {
mbedAustin 11:cada08fc8a70 498 if ((operation() & SN_GRS_POST_ALLOWED) != 0) {
mbedAustin 11:cada08fc8a70 499 void *arguments = NULL;
mbedAustin 11:cada08fc8a70 500 if(received_coap_header->payload_ptr) {
mbedAustin 11:cada08fc8a70 501 if(received_coap_header->payload_ptr) {
mbedAustin 11:cada08fc8a70 502 arguments = (void*)malloc(received_coap_header->payload_len+1);
mbedAustin 11:cada08fc8a70 503 if (arguments){
mbedAustin 11:cada08fc8a70 504 memset(arguments, 0, received_coap_header->payload_len+1);
mbedAustin 11:cada08fc8a70 505 memcpy(arguments,
mbedAustin 11:cada08fc8a70 506 received_coap_header->payload_ptr,
mbedAustin 11:cada08fc8a70 507 received_coap_header->payload_len);
mbedAustin 11:cada08fc8a70 508 }
mbedAustin 11:cada08fc8a70 509 }
mbedAustin 11:cada08fc8a70 510 }
mbedAustin 11:cada08fc8a70 511 tr_debug("M2MResource::handle_post_request - Execute resource function");
mbedAustin 11:cada08fc8a70 512 execute(arguments);
mbedAustin 11:cada08fc8a70 513 free(arguments);
mbedAustin 11:cada08fc8a70 514 } else { // if ((object->operation() & SN_GRS_POST_ALLOWED) != 0)
mbedAustin 11:cada08fc8a70 515 tr_error("M2MResource::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
mbedAustin 11:cada08fc8a70 516 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.05
mbedAustin 11:cada08fc8a70 517 }
mbedAustin 11:cada08fc8a70 518 } else { //if(object && received_coap_header)
mbedAustin 11:cada08fc8a70 519 tr_error("M2MResource::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
mbedAustin 11:cada08fc8a70 520 msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.01
mbedAustin 11:cada08fc8a70 521 }
mbedAustin 11:cada08fc8a70 522 if(coap_response) {
mbedAustin 11:cada08fc8a70 523 coap_response->msg_code = msg_code;
mbedAustin 11:cada08fc8a70 524 }
mbedAustin 11:cada08fc8a70 525 return coap_response;
mbedAustin 11:cada08fc8a70 526 }
mbedAustin 11:cada08fc8a70 527
mbedAustin 11:cada08fc8a70 528 void M2MResource::notification_update()
mbedAustin 11:cada08fc8a70 529 {
mbedAustin 11:cada08fc8a70 530 tr_debug("M2MResource::notification_update()");
mbedAustin 11:cada08fc8a70 531 M2MReportHandler *report_handler = M2MBase::report_handler();
mbedAustin 11:cada08fc8a70 532 if(report_handler && is_observable()) {
mbedAustin 11:cada08fc8a70 533 report_handler->set_notification_trigger();
mbedAustin 11:cada08fc8a70 534 }
mbedAustin 11:cada08fc8a70 535 }