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