This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
Ren Boting
Date:
Tue Sep 05 11:56:13 2017 +0900
Revision:
2:b894b3508057
Parent:
0:29983394c6b6
Update all libraries and reform main.cpp

Who changed what in which revision?

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