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