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