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:
edamame22
Date:
Thu Apr 13 04:48:11 2017 +0000
Revision:
0:29983394c6b6
Initial commit

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