leo hendrickson
/
S
simple-mbed-cloud-client/mbed-cloud-client/mbed-client/source/m2mobjectinstance.cpp@0:25fa8795676b, 2021-04-18 (annotated)
- Committer:
- leothedragon
- Date:
- Sun Apr 18 15:20:23 2021 +0000
- Revision:
- 0:25fa8795676b
DS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
leothedragon | 0:25fa8795676b | 1 | /* |
leothedragon | 0:25fa8795676b | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
leothedragon | 0:25fa8795676b | 3 | * SPDX-License-Identifier: Apache-2.0 |
leothedragon | 0:25fa8795676b | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
leothedragon | 0:25fa8795676b | 5 | * not use this file except in compliance with the License. |
leothedragon | 0:25fa8795676b | 6 | * You may obtain a copy of the License at |
leothedragon | 0:25fa8795676b | 7 | * |
leothedragon | 0:25fa8795676b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
leothedragon | 0:25fa8795676b | 9 | * |
leothedragon | 0:25fa8795676b | 10 | * Unless required by applicable law or agreed to in writing, software |
leothedragon | 0:25fa8795676b | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
leothedragon | 0:25fa8795676b | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
leothedragon | 0:25fa8795676b | 13 | * See the License for the specific language governing permissions and |
leothedragon | 0:25fa8795676b | 14 | * limitations under the License. |
leothedragon | 0:25fa8795676b | 15 | */ |
leothedragon | 0:25fa8795676b | 16 | #include "mbed-client/m2mobjectinstance.h" |
leothedragon | 0:25fa8795676b | 17 | #include "mbed-client/m2mobject.h" |
leothedragon | 0:25fa8795676b | 18 | #include "mbed-client/m2mconstants.h" |
leothedragon | 0:25fa8795676b | 19 | #include "mbed-client/m2mresource.h" |
leothedragon | 0:25fa8795676b | 20 | #include "mbed-client/m2mresource.h" |
leothedragon | 0:25fa8795676b | 21 | #include "mbed-client/m2mobservationhandler.h" |
leothedragon | 0:25fa8795676b | 22 | #include "mbed-client/m2mstring.h" |
leothedragon | 0:25fa8795676b | 23 | #include "mbed-client/m2mstringbuffer.h" |
leothedragon | 0:25fa8795676b | 24 | #include "include/m2mtlvserializer.h" |
leothedragon | 0:25fa8795676b | 25 | #include "include/m2mtlvdeserializer.h" |
leothedragon | 0:25fa8795676b | 26 | #include "include/m2mreporthandler.h" |
leothedragon | 0:25fa8795676b | 27 | #include "mbed-trace/mbed_trace.h" |
leothedragon | 0:25fa8795676b | 28 | #include "include/m2mcallbackstorage.h" |
leothedragon | 0:25fa8795676b | 29 | #include <stdlib.h> |
leothedragon | 0:25fa8795676b | 30 | |
leothedragon | 0:25fa8795676b | 31 | #define BUFFER_SIZE 10 |
leothedragon | 0:25fa8795676b | 32 | #define TRACE_GROUP "mClt" |
leothedragon | 0:25fa8795676b | 33 | |
leothedragon | 0:25fa8795676b | 34 | M2MObjectInstance::M2MObjectInstance(M2MObject& parent, |
leothedragon | 0:25fa8795676b | 35 | const String &resource_type, |
leothedragon | 0:25fa8795676b | 36 | char *path, |
leothedragon | 0:25fa8795676b | 37 | bool external_blockwise_store) |
leothedragon | 0:25fa8795676b | 38 | : M2MBase("", |
leothedragon | 0:25fa8795676b | 39 | M2MBase::Dynamic, |
leothedragon | 0:25fa8795676b | 40 | #ifndef DISABLE_RESOURCE_TYPE |
leothedragon | 0:25fa8795676b | 41 | resource_type, |
leothedragon | 0:25fa8795676b | 42 | #endif |
leothedragon | 0:25fa8795676b | 43 | path, |
leothedragon | 0:25fa8795676b | 44 | external_blockwise_store, |
leothedragon | 0:25fa8795676b | 45 | false), |
leothedragon | 0:25fa8795676b | 46 | _parent(parent) |
leothedragon | 0:25fa8795676b | 47 | { |
leothedragon | 0:25fa8795676b | 48 | M2MBase::set_base_type(M2MBase::ObjectInstance); |
leothedragon | 0:25fa8795676b | 49 | M2MBase::set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD); |
leothedragon | 0:25fa8795676b | 50 | M2MBase::set_operation(M2MBase::GET_ALLOWED); |
leothedragon | 0:25fa8795676b | 51 | } |
leothedragon | 0:25fa8795676b | 52 | |
leothedragon | 0:25fa8795676b | 53 | M2MObjectInstance::M2MObjectInstance(M2MObject& parent, const lwm2m_parameters_s* static_res) |
leothedragon | 0:25fa8795676b | 54 | : M2MBase(static_res), _parent(parent) |
leothedragon | 0:25fa8795676b | 55 | { |
leothedragon | 0:25fa8795676b | 56 | M2MBase::set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD); |
leothedragon | 0:25fa8795676b | 57 | M2MBase::set_operation(M2MBase::GET_ALLOWED); |
leothedragon | 0:25fa8795676b | 58 | } |
leothedragon | 0:25fa8795676b | 59 | |
leothedragon | 0:25fa8795676b | 60 | M2MObjectInstance::~M2MObjectInstance() |
leothedragon | 0:25fa8795676b | 61 | { |
leothedragon | 0:25fa8795676b | 62 | if(!_resource_list.empty()) { |
leothedragon | 0:25fa8795676b | 63 | M2MResource* res = NULL; |
leothedragon | 0:25fa8795676b | 64 | M2MResourceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 65 | it = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 66 | for (; it!=_resource_list.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 67 | //Free allocated memory for resources. |
leothedragon | 0:25fa8795676b | 68 | res = *it; |
leothedragon | 0:25fa8795676b | 69 | delete res; |
leothedragon | 0:25fa8795676b | 70 | } |
leothedragon | 0:25fa8795676b | 71 | _resource_list.clear(); |
leothedragon | 0:25fa8795676b | 72 | } |
leothedragon | 0:25fa8795676b | 73 | |
leothedragon | 0:25fa8795676b | 74 | free_resources(); |
leothedragon | 0:25fa8795676b | 75 | } |
leothedragon | 0:25fa8795676b | 76 | |
leothedragon | 0:25fa8795676b | 77 | // TBD, ResourceType to the base class struct?? TODO! |
leothedragon | 0:25fa8795676b | 78 | M2MResource* M2MObjectInstance::create_static_resource(const lwm2m_parameters_s* static_res, |
leothedragon | 0:25fa8795676b | 79 | M2MResourceInstance::ResourceType type) |
leothedragon | 0:25fa8795676b | 80 | { |
leothedragon | 0:25fa8795676b | 81 | tr_debug("M2MObjectInstance::create_static_resource(lwm2m_parameters_s resource_name %s)", static_res->identifier.name); |
leothedragon | 0:25fa8795676b | 82 | M2MResource *res = NULL; |
leothedragon | 0:25fa8795676b | 83 | if (validate_string_length(static_res->identifier.name, 1, MAX_ALLOWED_STRING_LENGTH) == false) { |
leothedragon | 0:25fa8795676b | 84 | return res; |
leothedragon | 0:25fa8795676b | 85 | } |
leothedragon | 0:25fa8795676b | 86 | if(!resource(static_res->identifier.name)) { |
leothedragon | 0:25fa8795676b | 87 | res = new M2MResource(*this, static_res, convert_resource_type(type)); |
leothedragon | 0:25fa8795676b | 88 | if(res) { |
leothedragon | 0:25fa8795676b | 89 | res->add_observation_level(observation_level()); |
leothedragon | 0:25fa8795676b | 90 | //if (multiple_instance) { |
leothedragon | 0:25fa8795676b | 91 | //res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD); |
leothedragon | 0:25fa8795676b | 92 | //} |
leothedragon | 0:25fa8795676b | 93 | _resource_list.push_back(res); |
leothedragon | 0:25fa8795676b | 94 | set_changed(); |
leothedragon | 0:25fa8795676b | 95 | } |
leothedragon | 0:25fa8795676b | 96 | } |
leothedragon | 0:25fa8795676b | 97 | return res; |
leothedragon | 0:25fa8795676b | 98 | } |
leothedragon | 0:25fa8795676b | 99 | |
leothedragon | 0:25fa8795676b | 100 | M2MResource* M2MObjectInstance::create_static_resource(const String &resource_name, |
leothedragon | 0:25fa8795676b | 101 | const String &resource_type, |
leothedragon | 0:25fa8795676b | 102 | M2MResourceInstance::ResourceType type, |
leothedragon | 0:25fa8795676b | 103 | const uint8_t *value, |
leothedragon | 0:25fa8795676b | 104 | const uint8_t value_length, |
leothedragon | 0:25fa8795676b | 105 | bool multiple_instance, |
leothedragon | 0:25fa8795676b | 106 | bool external_blockwise_store) |
leothedragon | 0:25fa8795676b | 107 | { |
leothedragon | 0:25fa8795676b | 108 | tr_debug("M2MObjectInstance::create_static_resource(resource_name %s)",resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 109 | M2MResource *res = NULL; |
leothedragon | 0:25fa8795676b | 110 | if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) { |
leothedragon | 0:25fa8795676b | 111 | return res; |
leothedragon | 0:25fa8795676b | 112 | } |
leothedragon | 0:25fa8795676b | 113 | if(!resource(resource_name)) { |
leothedragon | 0:25fa8795676b | 114 | char *path = create_path(*this, resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 115 | |
leothedragon | 0:25fa8795676b | 116 | if (path) { |
leothedragon | 0:25fa8795676b | 117 | res = new M2MResource(*this, resource_name, M2MBase::Static, resource_type, convert_resource_type(type), |
leothedragon | 0:25fa8795676b | 118 | value, value_length, path, |
leothedragon | 0:25fa8795676b | 119 | multiple_instance, external_blockwise_store); |
leothedragon | 0:25fa8795676b | 120 | if(res) { |
leothedragon | 0:25fa8795676b | 121 | res->add_observation_level(observation_level()); |
leothedragon | 0:25fa8795676b | 122 | if (multiple_instance) { |
leothedragon | 0:25fa8795676b | 123 | res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD); |
leothedragon | 0:25fa8795676b | 124 | } |
leothedragon | 0:25fa8795676b | 125 | _resource_list.push_back(res); |
leothedragon | 0:25fa8795676b | 126 | set_changed(); |
leothedragon | 0:25fa8795676b | 127 | } |
leothedragon | 0:25fa8795676b | 128 | } |
leothedragon | 0:25fa8795676b | 129 | } |
leothedragon | 0:25fa8795676b | 130 | return res; |
leothedragon | 0:25fa8795676b | 131 | } |
leothedragon | 0:25fa8795676b | 132 | |
leothedragon | 0:25fa8795676b | 133 | M2MResource* M2MObjectInstance::create_dynamic_resource(const lwm2m_parameters_s* static_res, |
leothedragon | 0:25fa8795676b | 134 | M2MResourceInstance::ResourceType type, |
leothedragon | 0:25fa8795676b | 135 | bool observable) |
leothedragon | 0:25fa8795676b | 136 | { |
leothedragon | 0:25fa8795676b | 137 | tr_debug("M2MObjectInstance::create_dynamic_resource(resource_name %s)", static_res->identifier.name); |
leothedragon | 0:25fa8795676b | 138 | M2MResource *res = NULL; |
leothedragon | 0:25fa8795676b | 139 | |
leothedragon | 0:25fa8795676b | 140 | if (validate_string_length(static_res->identifier.name, 1, MAX_ALLOWED_STRING_LENGTH) == false) { |
leothedragon | 0:25fa8795676b | 141 | return res; |
leothedragon | 0:25fa8795676b | 142 | } |
leothedragon | 0:25fa8795676b | 143 | if(!resource(static_res->identifier.name)) { |
leothedragon | 0:25fa8795676b | 144 | res = new M2MResource(*this, static_res, convert_resource_type(type)); |
leothedragon | 0:25fa8795676b | 145 | if(res) { |
leothedragon | 0:25fa8795676b | 146 | //if (multiple_instance) { // TODO! |
leothedragon | 0:25fa8795676b | 147 | // res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE); |
leothedragon | 0:25fa8795676b | 148 | //} |
leothedragon | 0:25fa8795676b | 149 | res->add_observation_level(observation_level()); |
leothedragon | 0:25fa8795676b | 150 | _resource_list.push_back(res); |
leothedragon | 0:25fa8795676b | 151 | set_changed(); |
leothedragon | 0:25fa8795676b | 152 | } |
leothedragon | 0:25fa8795676b | 153 | } |
leothedragon | 0:25fa8795676b | 154 | return res; |
leothedragon | 0:25fa8795676b | 155 | } |
leothedragon | 0:25fa8795676b | 156 | |
leothedragon | 0:25fa8795676b | 157 | M2MResource* M2MObjectInstance::create_dynamic_resource(const String &resource_name, |
leothedragon | 0:25fa8795676b | 158 | const String &resource_type, |
leothedragon | 0:25fa8795676b | 159 | M2MResourceInstance::ResourceType type, |
leothedragon | 0:25fa8795676b | 160 | bool observable, |
leothedragon | 0:25fa8795676b | 161 | bool multiple_instance, |
leothedragon | 0:25fa8795676b | 162 | bool external_blockwise_store) |
leothedragon | 0:25fa8795676b | 163 | { |
leothedragon | 0:25fa8795676b | 164 | tr_debug("M2MObjectInstance::create_dynamic_resource(resource_name %s)",resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 165 | M2MResource *res = NULL; |
leothedragon | 0:25fa8795676b | 166 | if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) { |
leothedragon | 0:25fa8795676b | 167 | return res; |
leothedragon | 0:25fa8795676b | 168 | } |
leothedragon | 0:25fa8795676b | 169 | if(!resource(resource_name)) { |
leothedragon | 0:25fa8795676b | 170 | char *path = create_path(*this, resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 171 | if (path) { |
leothedragon | 0:25fa8795676b | 172 | res = new M2MResource(*this, resource_name, M2MBase::Dynamic, resource_type, convert_resource_type(type), |
leothedragon | 0:25fa8795676b | 173 | observable, path, |
leothedragon | 0:25fa8795676b | 174 | multiple_instance, external_blockwise_store); |
leothedragon | 0:25fa8795676b | 175 | if(res) { |
leothedragon | 0:25fa8795676b | 176 | if (multiple_instance) { |
leothedragon | 0:25fa8795676b | 177 | res->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE_OLD); |
leothedragon | 0:25fa8795676b | 178 | } |
leothedragon | 0:25fa8795676b | 179 | res->add_observation_level(observation_level()); |
leothedragon | 0:25fa8795676b | 180 | _resource_list.push_back(res); |
leothedragon | 0:25fa8795676b | 181 | set_changed(); |
leothedragon | 0:25fa8795676b | 182 | } |
leothedragon | 0:25fa8795676b | 183 | } |
leothedragon | 0:25fa8795676b | 184 | } |
leothedragon | 0:25fa8795676b | 185 | return res; |
leothedragon | 0:25fa8795676b | 186 | } |
leothedragon | 0:25fa8795676b | 187 | |
leothedragon | 0:25fa8795676b | 188 | M2MResourceInstance* M2MObjectInstance::create_static_resource_instance(const String &resource_name, |
leothedragon | 0:25fa8795676b | 189 | const String &resource_type, |
leothedragon | 0:25fa8795676b | 190 | M2MResourceInstance::ResourceType type, |
leothedragon | 0:25fa8795676b | 191 | const uint8_t *value, |
leothedragon | 0:25fa8795676b | 192 | const uint8_t value_length, |
leothedragon | 0:25fa8795676b | 193 | uint16_t instance_id, |
leothedragon | 0:25fa8795676b | 194 | bool external_blockwise_store) |
leothedragon | 0:25fa8795676b | 195 | { |
leothedragon | 0:25fa8795676b | 196 | tr_debug("M2MObjectInstance::create_static_resource_instance(resource_name %s)",resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 197 | M2MResourceInstance *instance = NULL; |
leothedragon | 0:25fa8795676b | 198 | if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) { |
leothedragon | 0:25fa8795676b | 199 | |
leothedragon | 0:25fa8795676b | 200 | return instance; |
leothedragon | 0:25fa8795676b | 201 | } |
leothedragon | 0:25fa8795676b | 202 | M2MResource *res = resource(resource_name); |
leothedragon | 0:25fa8795676b | 203 | if(!res) { |
leothedragon | 0:25fa8795676b | 204 | char *path = create_path(*this, resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 205 | if (path) { |
leothedragon | 0:25fa8795676b | 206 | res = new M2MResource(*this, resource_name, M2MBase::Static, resource_type, convert_resource_type(type), |
leothedragon | 0:25fa8795676b | 207 | value, value_length, path, |
leothedragon | 0:25fa8795676b | 208 | true, external_blockwise_store); |
leothedragon | 0:25fa8795676b | 209 | _resource_list.push_back(res); |
leothedragon | 0:25fa8795676b | 210 | set_changed(); |
leothedragon | 0:25fa8795676b | 211 | res->set_operation(M2MBase::GET_ALLOWED); |
leothedragon | 0:25fa8795676b | 212 | res->set_observable(false); |
leothedragon | 0:25fa8795676b | 213 | res->set_register_uri(false); |
leothedragon | 0:25fa8795676b | 214 | } |
leothedragon | 0:25fa8795676b | 215 | } |
leothedragon | 0:25fa8795676b | 216 | if(res && res->supports_multiple_instances()&& (res->resource_instance(instance_id) == NULL)) { |
leothedragon | 0:25fa8795676b | 217 | char *path = M2MBase::create_path(*res, instance_id); |
leothedragon | 0:25fa8795676b | 218 | if (path) { |
leothedragon | 0:25fa8795676b | 219 | instance = new M2MResourceInstance(*res, "", M2MBase::Static, resource_type, convert_resource_type(type), |
leothedragon | 0:25fa8795676b | 220 | value, value_length, |
leothedragon | 0:25fa8795676b | 221 | path, external_blockwise_store,true); |
leothedragon | 0:25fa8795676b | 222 | if(instance) { |
leothedragon | 0:25fa8795676b | 223 | instance->set_operation(M2MBase::GET_ALLOWED); |
leothedragon | 0:25fa8795676b | 224 | instance->set_instance_id(instance_id); |
leothedragon | 0:25fa8795676b | 225 | res->add_resource_instance(instance); |
leothedragon | 0:25fa8795676b | 226 | } |
leothedragon | 0:25fa8795676b | 227 | } |
leothedragon | 0:25fa8795676b | 228 | } |
leothedragon | 0:25fa8795676b | 229 | return instance; |
leothedragon | 0:25fa8795676b | 230 | } |
leothedragon | 0:25fa8795676b | 231 | |
leothedragon | 0:25fa8795676b | 232 | M2MResourceInstance* M2MObjectInstance::create_dynamic_resource_instance(const String &resource_name, |
leothedragon | 0:25fa8795676b | 233 | const String &resource_type, |
leothedragon | 0:25fa8795676b | 234 | M2MResourceInstance::ResourceType type, |
leothedragon | 0:25fa8795676b | 235 | bool observable, |
leothedragon | 0:25fa8795676b | 236 | uint16_t instance_id, |
leothedragon | 0:25fa8795676b | 237 | bool external_blockwise_store) |
leothedragon | 0:25fa8795676b | 238 | { |
leothedragon | 0:25fa8795676b | 239 | tr_debug("M2MObjectInstance::create_dynamic_resource_instance(resource_name %s)",resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 240 | M2MResourceInstance *instance = NULL; |
leothedragon | 0:25fa8795676b | 241 | if (validate_string_length(resource_name, 1, MAX_ALLOWED_STRING_LENGTH) == false) { |
leothedragon | 0:25fa8795676b | 242 | return instance; |
leothedragon | 0:25fa8795676b | 243 | } |
leothedragon | 0:25fa8795676b | 244 | M2MResource *res = resource(resource_name); |
leothedragon | 0:25fa8795676b | 245 | if(!res) { |
leothedragon | 0:25fa8795676b | 246 | char *path = create_path(*this, resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 247 | if (path) { |
leothedragon | 0:25fa8795676b | 248 | res = new M2MResource(*this, resource_name, M2MBase::Dynamic, resource_type, convert_resource_type(type), |
leothedragon | 0:25fa8795676b | 249 | false, path, true, external_blockwise_store); |
leothedragon | 0:25fa8795676b | 250 | _resource_list.push_back(res); |
leothedragon | 0:25fa8795676b | 251 | res->set_register_uri(false); |
leothedragon | 0:25fa8795676b | 252 | res->set_operation(M2MBase::GET_ALLOWED); |
leothedragon | 0:25fa8795676b | 253 | } |
leothedragon | 0:25fa8795676b | 254 | } |
leothedragon | 0:25fa8795676b | 255 | if (res && res->supports_multiple_instances() && (res->resource_instance(instance_id) == NULL)) { |
leothedragon | 0:25fa8795676b | 256 | char *path = create_path(*res, instance_id); |
leothedragon | 0:25fa8795676b | 257 | if (path) { |
leothedragon | 0:25fa8795676b | 258 | instance = new M2MResourceInstance(*res, "", M2MBase::Dynamic, resource_type, convert_resource_type(type), |
leothedragon | 0:25fa8795676b | 259 | path, external_blockwise_store,true); |
leothedragon | 0:25fa8795676b | 260 | if(instance) { |
leothedragon | 0:25fa8795676b | 261 | instance->set_operation(M2MBase::GET_ALLOWED); |
leothedragon | 0:25fa8795676b | 262 | instance->set_observable(observable); |
leothedragon | 0:25fa8795676b | 263 | instance->set_instance_id(instance_id); |
leothedragon | 0:25fa8795676b | 264 | res->add_resource_instance(instance); |
leothedragon | 0:25fa8795676b | 265 | set_changed(); |
leothedragon | 0:25fa8795676b | 266 | } |
leothedragon | 0:25fa8795676b | 267 | } |
leothedragon | 0:25fa8795676b | 268 | } |
leothedragon | 0:25fa8795676b | 269 | return instance; |
leothedragon | 0:25fa8795676b | 270 | } |
leothedragon | 0:25fa8795676b | 271 | |
leothedragon | 0:25fa8795676b | 272 | bool M2MObjectInstance::remove_resource(const String &resource_name) |
leothedragon | 0:25fa8795676b | 273 | { |
leothedragon | 0:25fa8795676b | 274 | return remove_resource(resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 275 | } |
leothedragon | 0:25fa8795676b | 276 | |
leothedragon | 0:25fa8795676b | 277 | bool M2MObjectInstance::remove_resource(const char *resource_name) |
leothedragon | 0:25fa8795676b | 278 | { |
leothedragon | 0:25fa8795676b | 279 | tr_debug("M2MObjectInstance::remove_resource(resource_name %s)", resource_name); |
leothedragon | 0:25fa8795676b | 280 | |
leothedragon | 0:25fa8795676b | 281 | bool success = false; |
leothedragon | 0:25fa8795676b | 282 | if(!_resource_list.empty()) { |
leothedragon | 0:25fa8795676b | 283 | M2MResource* res = NULL; |
leothedragon | 0:25fa8795676b | 284 | M2MResourceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 285 | it = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 286 | int pos = 0; |
leothedragon | 0:25fa8795676b | 287 | for ( ; it != _resource_list.end(); it++, pos++ ) { |
leothedragon | 0:25fa8795676b | 288 | if(strcmp((*it)->name(), resource_name) == 0) { |
leothedragon | 0:25fa8795676b | 289 | // Resource found and deleted. |
leothedragon | 0:25fa8795676b | 290 | res = *it; |
leothedragon | 0:25fa8795676b | 291 | delete res; |
leothedragon | 0:25fa8795676b | 292 | _resource_list.erase(pos); |
leothedragon | 0:25fa8795676b | 293 | set_changed(); |
leothedragon | 0:25fa8795676b | 294 | success = true; |
leothedragon | 0:25fa8795676b | 295 | break; |
leothedragon | 0:25fa8795676b | 296 | } |
leothedragon | 0:25fa8795676b | 297 | } |
leothedragon | 0:25fa8795676b | 298 | } |
leothedragon | 0:25fa8795676b | 299 | return success; |
leothedragon | 0:25fa8795676b | 300 | } |
leothedragon | 0:25fa8795676b | 301 | |
leothedragon | 0:25fa8795676b | 302 | bool M2MObjectInstance::remove_resource_instance(const String &resource_name, |
leothedragon | 0:25fa8795676b | 303 | uint16_t inst_id) |
leothedragon | 0:25fa8795676b | 304 | { |
leothedragon | 0:25fa8795676b | 305 | tr_debug("M2MObjectInstance::remove_resource_instance(resource_name %s inst_id %d)", |
leothedragon | 0:25fa8795676b | 306 | resource_name.c_str(), inst_id); |
leothedragon | 0:25fa8795676b | 307 | bool success = false; |
leothedragon | 0:25fa8795676b | 308 | M2MResource *res = resource(resource_name); |
leothedragon | 0:25fa8795676b | 309 | if(res) { |
leothedragon | 0:25fa8795676b | 310 | const M2MResourceInstanceList &list = res->resource_instances(); |
leothedragon | 0:25fa8795676b | 311 | M2MResourceInstanceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 312 | it = list.begin(); |
leothedragon | 0:25fa8795676b | 313 | for ( ; it != list.end(); it++) { |
leothedragon | 0:25fa8795676b | 314 | if((*it)->instance_id() == inst_id) { |
leothedragon | 0:25fa8795676b | 315 | success = res->remove_resource_instance(inst_id); |
leothedragon | 0:25fa8795676b | 316 | if(res->resource_instance_count() == 0) { |
leothedragon | 0:25fa8795676b | 317 | M2MResourceList::const_iterator itr; |
leothedragon | 0:25fa8795676b | 318 | itr = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 319 | int pos = 0; |
leothedragon | 0:25fa8795676b | 320 | for ( ; itr != _resource_list.end(); itr++, pos++ ) { |
leothedragon | 0:25fa8795676b | 321 | if(strcmp((*itr)->name(),resource_name.c_str()) == 0) { |
leothedragon | 0:25fa8795676b | 322 | delete res; |
leothedragon | 0:25fa8795676b | 323 | _resource_list.erase(pos); |
leothedragon | 0:25fa8795676b | 324 | set_changed(); |
leothedragon | 0:25fa8795676b | 325 | break; |
leothedragon | 0:25fa8795676b | 326 | } |
leothedragon | 0:25fa8795676b | 327 | } |
leothedragon | 0:25fa8795676b | 328 | } |
leothedragon | 0:25fa8795676b | 329 | break; |
leothedragon | 0:25fa8795676b | 330 | } |
leothedragon | 0:25fa8795676b | 331 | } |
leothedragon | 0:25fa8795676b | 332 | } |
leothedragon | 0:25fa8795676b | 333 | return success; |
leothedragon | 0:25fa8795676b | 334 | } |
leothedragon | 0:25fa8795676b | 335 | |
leothedragon | 0:25fa8795676b | 336 | M2MResource* M2MObjectInstance::resource(const String &resource_name) const |
leothedragon | 0:25fa8795676b | 337 | { |
leothedragon | 0:25fa8795676b | 338 | return resource(resource_name.c_str()); |
leothedragon | 0:25fa8795676b | 339 | } |
leothedragon | 0:25fa8795676b | 340 | |
leothedragon | 0:25fa8795676b | 341 | M2MResource* M2MObjectInstance::resource(const char *resource_name) const |
leothedragon | 0:25fa8795676b | 342 | { |
leothedragon | 0:25fa8795676b | 343 | M2MResource *res = NULL; |
leothedragon | 0:25fa8795676b | 344 | if(!_resource_list.empty()) { |
leothedragon | 0:25fa8795676b | 345 | M2MResourceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 346 | it = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 347 | for (; it!=_resource_list.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 348 | if(strcmp((*it)->name(), resource_name) == 0) { |
leothedragon | 0:25fa8795676b | 349 | res = *it; |
leothedragon | 0:25fa8795676b | 350 | break; |
leothedragon | 0:25fa8795676b | 351 | } |
leothedragon | 0:25fa8795676b | 352 | } |
leothedragon | 0:25fa8795676b | 353 | } |
leothedragon | 0:25fa8795676b | 354 | return res; |
leothedragon | 0:25fa8795676b | 355 | } |
leothedragon | 0:25fa8795676b | 356 | |
leothedragon | 0:25fa8795676b | 357 | const M2MResourceList& M2MObjectInstance::resources() const |
leothedragon | 0:25fa8795676b | 358 | { |
leothedragon | 0:25fa8795676b | 359 | return _resource_list; |
leothedragon | 0:25fa8795676b | 360 | } |
leothedragon | 0:25fa8795676b | 361 | |
leothedragon | 0:25fa8795676b | 362 | uint16_t M2MObjectInstance::resource_count() const |
leothedragon | 0:25fa8795676b | 363 | { |
leothedragon | 0:25fa8795676b | 364 | uint16_t count = 0; |
leothedragon | 0:25fa8795676b | 365 | if(!_resource_list.empty()) { |
leothedragon | 0:25fa8795676b | 366 | M2MResourceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 367 | it = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 368 | for ( ; it != _resource_list.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 369 | if((*it)->supports_multiple_instances()) { |
leothedragon | 0:25fa8795676b | 370 | count += (*it)->resource_instance_count(); |
leothedragon | 0:25fa8795676b | 371 | } else { |
leothedragon | 0:25fa8795676b | 372 | count++; |
leothedragon | 0:25fa8795676b | 373 | } |
leothedragon | 0:25fa8795676b | 374 | } |
leothedragon | 0:25fa8795676b | 375 | } |
leothedragon | 0:25fa8795676b | 376 | return count; |
leothedragon | 0:25fa8795676b | 377 | } |
leothedragon | 0:25fa8795676b | 378 | |
leothedragon | 0:25fa8795676b | 379 | uint16_t M2MObjectInstance::resource_count(const String& resource) const |
leothedragon | 0:25fa8795676b | 380 | { |
leothedragon | 0:25fa8795676b | 381 | |
leothedragon | 0:25fa8795676b | 382 | return resource_count(resource.c_str()); |
leothedragon | 0:25fa8795676b | 383 | } |
leothedragon | 0:25fa8795676b | 384 | |
leothedragon | 0:25fa8795676b | 385 | uint16_t M2MObjectInstance::resource_count(const char *resource) const |
leothedragon | 0:25fa8795676b | 386 | { |
leothedragon | 0:25fa8795676b | 387 | uint16_t count = 0; |
leothedragon | 0:25fa8795676b | 388 | if(!_resource_list.empty()) { |
leothedragon | 0:25fa8795676b | 389 | M2MResourceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 390 | it = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 391 | for ( ; it != _resource_list.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 392 | if(strcmp((*it)->name(), resource) == 0) { |
leothedragon | 0:25fa8795676b | 393 | if((*it)->supports_multiple_instances()) { |
leothedragon | 0:25fa8795676b | 394 | count += (*it)->resource_instance_count(); |
leothedragon | 0:25fa8795676b | 395 | } else { |
leothedragon | 0:25fa8795676b | 396 | count++; |
leothedragon | 0:25fa8795676b | 397 | } |
leothedragon | 0:25fa8795676b | 398 | } |
leothedragon | 0:25fa8795676b | 399 | } |
leothedragon | 0:25fa8795676b | 400 | } |
leothedragon | 0:25fa8795676b | 401 | return count; |
leothedragon | 0:25fa8795676b | 402 | } |
leothedragon | 0:25fa8795676b | 403 | |
leothedragon | 0:25fa8795676b | 404 | M2MObservationHandler* M2MObjectInstance::observation_handler() const |
leothedragon | 0:25fa8795676b | 405 | { |
leothedragon | 0:25fa8795676b | 406 | // XXX: need to check the flag too |
leothedragon | 0:25fa8795676b | 407 | return _parent.observation_handler(); |
leothedragon | 0:25fa8795676b | 408 | } |
leothedragon | 0:25fa8795676b | 409 | |
leothedragon | 0:25fa8795676b | 410 | void M2MObjectInstance::set_observation_handler(M2MObservationHandler *handler) |
leothedragon | 0:25fa8795676b | 411 | { |
leothedragon | 0:25fa8795676b | 412 | // XXX: need to set the flag too |
leothedragon | 0:25fa8795676b | 413 | _parent.set_observation_handler(handler); |
leothedragon | 0:25fa8795676b | 414 | } |
leothedragon | 0:25fa8795676b | 415 | |
leothedragon | 0:25fa8795676b | 416 | void M2MObjectInstance::add_observation_level(M2MBase::Observation observation_level) |
leothedragon | 0:25fa8795676b | 417 | { |
leothedragon | 0:25fa8795676b | 418 | M2MBase::add_observation_level(observation_level); |
leothedragon | 0:25fa8795676b | 419 | if(!_resource_list.empty()) { |
leothedragon | 0:25fa8795676b | 420 | M2MResourceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 421 | it = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 422 | for ( ; it != _resource_list.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 423 | (*it)->add_observation_level(observation_level); |
leothedragon | 0:25fa8795676b | 424 | } |
leothedragon | 0:25fa8795676b | 425 | } |
leothedragon | 0:25fa8795676b | 426 | } |
leothedragon | 0:25fa8795676b | 427 | |
leothedragon | 0:25fa8795676b | 428 | void M2MObjectInstance::remove_observation_level(M2MBase::Observation observation_level) |
leothedragon | 0:25fa8795676b | 429 | { |
leothedragon | 0:25fa8795676b | 430 | M2MBase::remove_observation_level(observation_level); |
leothedragon | 0:25fa8795676b | 431 | if(!_resource_list.empty()) { |
leothedragon | 0:25fa8795676b | 432 | M2MResourceList::const_iterator it; |
leothedragon | 0:25fa8795676b | 433 | it = _resource_list.begin(); |
leothedragon | 0:25fa8795676b | 434 | for ( ; it != _resource_list.end(); it++ ) { |
leothedragon | 0:25fa8795676b | 435 | (*it)->remove_observation_level(observation_level); |
leothedragon | 0:25fa8795676b | 436 | } |
leothedragon | 0:25fa8795676b | 437 | } |
leothedragon | 0:25fa8795676b | 438 | } |
leothedragon | 0:25fa8795676b | 439 | |
leothedragon | 0:25fa8795676b | 440 | sn_coap_hdr_s* M2MObjectInstance::handle_get_request(nsdl_s *nsdl, |
leothedragon | 0:25fa8795676b | 441 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:25fa8795676b | 442 | M2MObservationHandler *observation_handler) |
leothedragon | 0:25fa8795676b | 443 | { |
leothedragon | 0:25fa8795676b | 444 | tr_info("M2MObjectInstance::handle_get_request()"); |
leothedragon | 0:25fa8795676b | 445 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CONTENT; |
leothedragon | 0:25fa8795676b | 446 | sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:25fa8795676b | 447 | received_coap_header, |
leothedragon | 0:25fa8795676b | 448 | msg_code); |
leothedragon | 0:25fa8795676b | 449 | uint8_t * data = NULL; |
leothedragon | 0:25fa8795676b | 450 | uint32_t data_length = 0; |
leothedragon | 0:25fa8795676b | 451 | |
leothedragon | 0:25fa8795676b | 452 | if (received_coap_header) { |
leothedragon | 0:25fa8795676b | 453 | // process the GET if we have registered a callback for it |
leothedragon | 0:25fa8795676b | 454 | if ((operation() & M2MBase::GET_ALLOWED) != 0) { |
leothedragon | 0:25fa8795676b | 455 | if (coap_response) { |
leothedragon | 0:25fa8795676b | 456 | bool content_type_present = false; |
leothedragon | 0:25fa8795676b | 457 | bool is_content_type_supported = true; |
leothedragon | 0:25fa8795676b | 458 | |
leothedragon | 0:25fa8795676b | 459 | if (received_coap_header->options_list_ptr && |
leothedragon | 0:25fa8795676b | 460 | received_coap_header->options_list_ptr->accept != COAP_CT_NONE) { |
leothedragon | 0:25fa8795676b | 461 | content_type_present = true; |
leothedragon | 0:25fa8795676b | 462 | coap_response->content_format = received_coap_header->options_list_ptr->accept; |
leothedragon | 0:25fa8795676b | 463 | |
leothedragon | 0:25fa8795676b | 464 | } |
leothedragon | 0:25fa8795676b | 465 | |
leothedragon | 0:25fa8795676b | 466 | // Check if preferred content type is supported |
leothedragon | 0:25fa8795676b | 467 | if (content_type_present) { |
leothedragon | 0:25fa8795676b | 468 | if (coap_response->content_format != COAP_CONTENT_OMA_TLV_TYPE_OLD && |
leothedragon | 0:25fa8795676b | 469 | coap_response->content_format != COAP_CONTENT_OMA_TLV_TYPE) { |
leothedragon | 0:25fa8795676b | 470 | is_content_type_supported = false; |
leothedragon | 0:25fa8795676b | 471 | } |
leothedragon | 0:25fa8795676b | 472 | } |
leothedragon | 0:25fa8795676b | 473 | |
leothedragon | 0:25fa8795676b | 474 | if (is_content_type_supported) { |
leothedragon | 0:25fa8795676b | 475 | if (!content_type_present && |
leothedragon | 0:25fa8795676b | 476 | (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE || |
leothedragon | 0:25fa8795676b | 477 | M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) { |
leothedragon | 0:25fa8795676b | 478 | coap_response->content_format = sn_coap_content_format_e(M2MBase::coap_content_type()); |
leothedragon | 0:25fa8795676b | 479 | } |
leothedragon | 0:25fa8795676b | 480 | |
leothedragon | 0:25fa8795676b | 481 | // fill in the CoAP response payload |
leothedragon | 0:25fa8795676b | 482 | if (COAP_CONTENT_OMA_TLV_TYPE == coap_response->content_format || |
leothedragon | 0:25fa8795676b | 483 | COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_response->content_format) { |
leothedragon | 0:25fa8795676b | 484 | set_coap_content_type(coap_response->content_format); |
leothedragon | 0:25fa8795676b | 485 | data = M2MTLVSerializer::serialize(_resource_list, data_length); |
leothedragon | 0:25fa8795676b | 486 | } |
leothedragon | 0:25fa8795676b | 487 | |
leothedragon | 0:25fa8795676b | 488 | coap_response->payload_len = data_length; |
leothedragon | 0:25fa8795676b | 489 | coap_response->payload_ptr = data; |
leothedragon | 0:25fa8795676b | 490 | |
leothedragon | 0:25fa8795676b | 491 | if (data) { |
leothedragon | 0:25fa8795676b | 492 | coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response); |
leothedragon | 0:25fa8795676b | 493 | if (coap_response->options_list_ptr) { |
leothedragon | 0:25fa8795676b | 494 | coap_response->options_list_ptr->max_age = max_age(); |
leothedragon | 0:25fa8795676b | 495 | } |
leothedragon | 0:25fa8795676b | 496 | |
leothedragon | 0:25fa8795676b | 497 | if (received_coap_header->options_list_ptr) { |
leothedragon | 0:25fa8795676b | 498 | if(received_coap_header->options_list_ptr->observe != -1) { |
leothedragon | 0:25fa8795676b | 499 | handle_observation(nsdl, *received_coap_header, *coap_response, observation_handler, msg_code); |
leothedragon | 0:25fa8795676b | 500 | } |
leothedragon | 0:25fa8795676b | 501 | } |
leothedragon | 0:25fa8795676b | 502 | } else { |
leothedragon | 0:25fa8795676b | 503 | msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; // Content format not supported |
leothedragon | 0:25fa8795676b | 504 | } |
leothedragon | 0:25fa8795676b | 505 | } else { |
leothedragon | 0:25fa8795676b | 506 | tr_error("M2MObjectInstance::handle_get_request() - Content-type: %d not supported", coap_response->content_format); |
leothedragon | 0:25fa8795676b | 507 | msg_code = COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE; |
leothedragon | 0:25fa8795676b | 508 | } |
leothedragon | 0:25fa8795676b | 509 | } |
leothedragon | 0:25fa8795676b | 510 | } else { |
leothedragon | 0:25fa8795676b | 511 | tr_error("M2MObjectInstance::handle_get_request - Return COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED"); |
leothedragon | 0:25fa8795676b | 512 | // Operation is not allowed. |
leothedragon | 0:25fa8795676b | 513 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 514 | } |
leothedragon | 0:25fa8795676b | 515 | } else { |
leothedragon | 0:25fa8795676b | 516 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 517 | } |
leothedragon | 0:25fa8795676b | 518 | if(coap_response) { |
leothedragon | 0:25fa8795676b | 519 | coap_response->msg_code = msg_code; |
leothedragon | 0:25fa8795676b | 520 | } |
leothedragon | 0:25fa8795676b | 521 | return coap_response; |
leothedragon | 0:25fa8795676b | 522 | } |
leothedragon | 0:25fa8795676b | 523 | |
leothedragon | 0:25fa8795676b | 524 | sn_coap_hdr_s* M2MObjectInstance::handle_put_request(nsdl_s *nsdl, |
leothedragon | 0:25fa8795676b | 525 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:25fa8795676b | 526 | M2MObservationHandler *observation_handler, |
leothedragon | 0:25fa8795676b | 527 | bool &/*execute_value_updated*/) |
leothedragon | 0:25fa8795676b | 528 | { |
leothedragon | 0:25fa8795676b | 529 | tr_info("M2MObjectInstance::handle_put_request()"); |
leothedragon | 0:25fa8795676b | 530 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04 |
leothedragon | 0:25fa8795676b | 531 | sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:25fa8795676b | 532 | received_coap_header, |
leothedragon | 0:25fa8795676b | 533 | msg_code);; |
leothedragon | 0:25fa8795676b | 534 | if(received_coap_header) { |
leothedragon | 0:25fa8795676b | 535 | uint16_t coap_content_type = 0; |
leothedragon | 0:25fa8795676b | 536 | bool content_type_present = false; |
leothedragon | 0:25fa8795676b | 537 | |
leothedragon | 0:25fa8795676b | 538 | if(received_coap_header->content_format != COAP_CT_NONE) { |
leothedragon | 0:25fa8795676b | 539 | content_type_present = true; |
leothedragon | 0:25fa8795676b | 540 | set_coap_content_type(received_coap_header->content_format); |
leothedragon | 0:25fa8795676b | 541 | if(coap_response) { |
leothedragon | 0:25fa8795676b | 542 | coap_content_type = received_coap_header->content_format; |
leothedragon | 0:25fa8795676b | 543 | } |
leothedragon | 0:25fa8795676b | 544 | } |
leothedragon | 0:25fa8795676b | 545 | if(received_coap_header->options_list_ptr && |
leothedragon | 0:25fa8795676b | 546 | received_coap_header->options_list_ptr->uri_query_ptr) { |
leothedragon | 0:25fa8795676b | 547 | char *query = (char*)alloc_string_copy(received_coap_header->options_list_ptr->uri_query_ptr, |
leothedragon | 0:25fa8795676b | 548 | received_coap_header->options_list_ptr->uri_query_len); |
leothedragon | 0:25fa8795676b | 549 | if (query){ |
leothedragon | 0:25fa8795676b | 550 | tr_info("M2MObjectInstance::handle_put_request() - query %s", query); |
leothedragon | 0:25fa8795676b | 551 | // if anything was updated, re-initialize the stored notification attributes |
leothedragon | 0:25fa8795676b | 552 | if (!handle_observation_attribute(query)){ |
leothedragon | 0:25fa8795676b | 553 | tr_debug("M2MObjectInstance::handle_put_request() - Invalid query"); |
leothedragon | 0:25fa8795676b | 554 | msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00 |
leothedragon | 0:25fa8795676b | 555 | } else { |
leothedragon | 0:25fa8795676b | 556 | msg_code =COAP_MSG_CODE_RESPONSE_CHANGED; |
leothedragon | 0:25fa8795676b | 557 | } |
leothedragon | 0:25fa8795676b | 558 | free(query); |
leothedragon | 0:25fa8795676b | 559 | } |
leothedragon | 0:25fa8795676b | 560 | } else if ((operation() & M2MBase::PUT_ALLOWED) != 0) { |
leothedragon | 0:25fa8795676b | 561 | if(!content_type_present && |
leothedragon | 0:25fa8795676b | 562 | (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE || |
leothedragon | 0:25fa8795676b | 563 | M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) { |
leothedragon | 0:25fa8795676b | 564 | coap_content_type = M2MBase::coap_content_type(); |
leothedragon | 0:25fa8795676b | 565 | } |
leothedragon | 0:25fa8795676b | 566 | |
leothedragon | 0:25fa8795676b | 567 | tr_debug("M2MObjectInstance::handle_put_request() - Request Content-type: %d", coap_content_type); |
leothedragon | 0:25fa8795676b | 568 | |
leothedragon | 0:25fa8795676b | 569 | if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type || |
leothedragon | 0:25fa8795676b | 570 | COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_content_type ) { |
leothedragon | 0:25fa8795676b | 571 | set_coap_content_type(coap_content_type); |
leothedragon | 0:25fa8795676b | 572 | M2MTLVDeserializer::Error error = M2MTLVDeserializer::None; |
leothedragon | 0:25fa8795676b | 573 | if(received_coap_header->payload_ptr) { |
leothedragon | 0:25fa8795676b | 574 | error = M2MTLVDeserializer::deserialize_resources( |
leothedragon | 0:25fa8795676b | 575 | received_coap_header->payload_ptr, |
leothedragon | 0:25fa8795676b | 576 | received_coap_header->payload_len, *this, |
leothedragon | 0:25fa8795676b | 577 | M2MTLVDeserializer::Put); |
leothedragon | 0:25fa8795676b | 578 | switch(error) { |
leothedragon | 0:25fa8795676b | 579 | case M2MTLVDeserializer::None: |
leothedragon | 0:25fa8795676b | 580 | if(observation_handler) { |
leothedragon | 0:25fa8795676b | 581 | observation_handler->value_updated(this); |
leothedragon | 0:25fa8795676b | 582 | } |
leothedragon | 0:25fa8795676b | 583 | msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; |
leothedragon | 0:25fa8795676b | 584 | break; |
leothedragon | 0:25fa8795676b | 585 | case M2MTLVDeserializer::NotFound: |
leothedragon | 0:25fa8795676b | 586 | msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND; |
leothedragon | 0:25fa8795676b | 587 | break; |
leothedragon | 0:25fa8795676b | 588 | case M2MTLVDeserializer::NotAllowed: |
leothedragon | 0:25fa8795676b | 589 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 590 | break; |
leothedragon | 0:25fa8795676b | 591 | case M2MTLVDeserializer::NotValid: |
leothedragon | 0:25fa8795676b | 592 | msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; |
leothedragon | 0:25fa8795676b | 593 | break; |
leothedragon | 0:25fa8795676b | 594 | case M2MTLVDeserializer::OutOfMemory: |
leothedragon | 0:25fa8795676b | 595 | msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE; |
leothedragon | 0:25fa8795676b | 596 | break; |
leothedragon | 0:25fa8795676b | 597 | } |
leothedragon | 0:25fa8795676b | 598 | } |
leothedragon | 0:25fa8795676b | 599 | } else { |
leothedragon | 0:25fa8795676b | 600 | msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; |
leothedragon | 0:25fa8795676b | 601 | } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) |
leothedragon | 0:25fa8795676b | 602 | } else { |
leothedragon | 0:25fa8795676b | 603 | // Operation is not allowed. |
leothedragon | 0:25fa8795676b | 604 | tr_error("M2MObjectInstance::handle_put_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED"); |
leothedragon | 0:25fa8795676b | 605 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 606 | } |
leothedragon | 0:25fa8795676b | 607 | } else { |
leothedragon | 0:25fa8795676b | 608 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 609 | } |
leothedragon | 0:25fa8795676b | 610 | if(coap_response) { |
leothedragon | 0:25fa8795676b | 611 | coap_response->msg_code = msg_code; |
leothedragon | 0:25fa8795676b | 612 | } |
leothedragon | 0:25fa8795676b | 613 | return coap_response; |
leothedragon | 0:25fa8795676b | 614 | } |
leothedragon | 0:25fa8795676b | 615 | |
leothedragon | 0:25fa8795676b | 616 | sn_coap_hdr_s* M2MObjectInstance::handle_post_request(nsdl_s *nsdl, |
leothedragon | 0:25fa8795676b | 617 | sn_coap_hdr_s *received_coap_header, |
leothedragon | 0:25fa8795676b | 618 | M2MObservationHandler *observation_handler, |
leothedragon | 0:25fa8795676b | 619 | bool &execute_value_updated, |
leothedragon | 0:25fa8795676b | 620 | sn_nsdl_addr_s *) |
leothedragon | 0:25fa8795676b | 621 | { |
leothedragon | 0:25fa8795676b | 622 | tr_info("M2MObjectInstance::handle_post_request()"); |
leothedragon | 0:25fa8795676b | 623 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04 |
leothedragon | 0:25fa8795676b | 624 | sn_coap_hdr_s * coap_response = sn_nsdl_build_response(nsdl, |
leothedragon | 0:25fa8795676b | 625 | received_coap_header, |
leothedragon | 0:25fa8795676b | 626 | msg_code); |
leothedragon | 0:25fa8795676b | 627 | if(received_coap_header) { |
leothedragon | 0:25fa8795676b | 628 | if ((operation() & M2MBase::POST_ALLOWED) != 0) { |
leothedragon | 0:25fa8795676b | 629 | uint16_t coap_content_type = 0; |
leothedragon | 0:25fa8795676b | 630 | bool content_type_present = false; |
leothedragon | 0:25fa8795676b | 631 | if(received_coap_header->content_format != COAP_CT_NONE) { |
leothedragon | 0:25fa8795676b | 632 | set_coap_content_type(received_coap_header->content_format); |
leothedragon | 0:25fa8795676b | 633 | content_type_present = true; |
leothedragon | 0:25fa8795676b | 634 | if(coap_response) { |
leothedragon | 0:25fa8795676b | 635 | coap_content_type = received_coap_header->content_format; |
leothedragon | 0:25fa8795676b | 636 | } |
leothedragon | 0:25fa8795676b | 637 | } |
leothedragon | 0:25fa8795676b | 638 | if(!content_type_present && |
leothedragon | 0:25fa8795676b | 639 | (M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE || |
leothedragon | 0:25fa8795676b | 640 | M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE_OLD)) { |
leothedragon | 0:25fa8795676b | 641 | coap_content_type = M2MBase::coap_content_type(); |
leothedragon | 0:25fa8795676b | 642 | } |
leothedragon | 0:25fa8795676b | 643 | |
leothedragon | 0:25fa8795676b | 644 | tr_debug("M2MObjectInstance::handle_post_request() - Request Content-type: %d", coap_content_type); |
leothedragon | 0:25fa8795676b | 645 | |
leothedragon | 0:25fa8795676b | 646 | if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type || |
leothedragon | 0:25fa8795676b | 647 | COAP_CONTENT_OMA_TLV_TYPE_OLD == coap_content_type) { |
leothedragon | 0:25fa8795676b | 648 | set_coap_content_type(coap_content_type); |
leothedragon | 0:25fa8795676b | 649 | M2MTLVDeserializer::Error error = M2MTLVDeserializer::None; |
leothedragon | 0:25fa8795676b | 650 | error = M2MTLVDeserializer::deserialize_resources( |
leothedragon | 0:25fa8795676b | 651 | received_coap_header->payload_ptr, |
leothedragon | 0:25fa8795676b | 652 | received_coap_header->payload_len, *this, |
leothedragon | 0:25fa8795676b | 653 | M2MTLVDeserializer::Post); |
leothedragon | 0:25fa8795676b | 654 | |
leothedragon | 0:25fa8795676b | 655 | uint16_t instance_id = M2MTLVDeserializer::instance_id(received_coap_header->payload_ptr); |
leothedragon | 0:25fa8795676b | 656 | switch(error) { |
leothedragon | 0:25fa8795676b | 657 | case M2MTLVDeserializer::None: |
leothedragon | 0:25fa8795676b | 658 | if(observation_handler) { |
leothedragon | 0:25fa8795676b | 659 | execute_value_updated = true; |
leothedragon | 0:25fa8795676b | 660 | } |
leothedragon | 0:25fa8795676b | 661 | coap_response->options_list_ptr = sn_nsdl_alloc_options_list(nsdl, coap_response); |
leothedragon | 0:25fa8795676b | 662 | |
leothedragon | 0:25fa8795676b | 663 | if (coap_response->options_list_ptr) { |
leothedragon | 0:25fa8795676b | 664 | |
leothedragon | 0:25fa8795676b | 665 | StringBuffer<MAX_PATH_SIZE_3> obj_name; |
leothedragon | 0:25fa8795676b | 666 | if(!build_path(obj_name, _parent.name(), M2MBase::instance_id(), instance_id)) { |
leothedragon | 0:25fa8795676b | 667 | msg_code = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR; |
leothedragon | 0:25fa8795676b | 668 | break; |
leothedragon | 0:25fa8795676b | 669 | } |
leothedragon | 0:25fa8795676b | 670 | |
leothedragon | 0:25fa8795676b | 671 | coap_response->options_list_ptr->location_path_len = obj_name.get_size(); |
leothedragon | 0:25fa8795676b | 672 | coap_response->options_list_ptr->location_path_ptr = |
leothedragon | 0:25fa8795676b | 673 | alloc_string_copy((uint8_t*)obj_name.c_str(), |
leothedragon | 0:25fa8795676b | 674 | coap_response->options_list_ptr->location_path_len); |
leothedragon | 0:25fa8795676b | 675 | // todo: handle allocation error |
leothedragon | 0:25fa8795676b | 676 | } |
leothedragon | 0:25fa8795676b | 677 | msg_code = COAP_MSG_CODE_RESPONSE_CREATED; |
leothedragon | 0:25fa8795676b | 678 | break; |
leothedragon | 0:25fa8795676b | 679 | case M2MTLVDeserializer::NotAllowed: |
leothedragon | 0:25fa8795676b | 680 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 681 | break; |
leothedragon | 0:25fa8795676b | 682 | case M2MTLVDeserializer::NotValid: |
leothedragon | 0:25fa8795676b | 683 | msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; |
leothedragon | 0:25fa8795676b | 684 | break; |
leothedragon | 0:25fa8795676b | 685 | case M2MTLVDeserializer::NotFound: |
leothedragon | 0:25fa8795676b | 686 | msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND; |
leothedragon | 0:25fa8795676b | 687 | break; |
leothedragon | 0:25fa8795676b | 688 | case M2MTLVDeserializer::OutOfMemory: |
leothedragon | 0:25fa8795676b | 689 | msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE; |
leothedragon | 0:25fa8795676b | 690 | break; |
leothedragon | 0:25fa8795676b | 691 | default: |
leothedragon | 0:25fa8795676b | 692 | break; |
leothedragon | 0:25fa8795676b | 693 | } |
leothedragon | 0:25fa8795676b | 694 | } else { |
leothedragon | 0:25fa8795676b | 695 | msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; |
leothedragon | 0:25fa8795676b | 696 | } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) |
leothedragon | 0:25fa8795676b | 697 | } else { |
leothedragon | 0:25fa8795676b | 698 | // Operation is not allowed. |
leothedragon | 0:25fa8795676b | 699 | tr_error("M2MObjectInstance::handle_post_request() - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED"); |
leothedragon | 0:25fa8795676b | 700 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 701 | } |
leothedragon | 0:25fa8795676b | 702 | } else { |
leothedragon | 0:25fa8795676b | 703 | msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; |
leothedragon | 0:25fa8795676b | 704 | } |
leothedragon | 0:25fa8795676b | 705 | if(coap_response) { |
leothedragon | 0:25fa8795676b | 706 | coap_response->msg_code = msg_code; |
leothedragon | 0:25fa8795676b | 707 | } |
leothedragon | 0:25fa8795676b | 708 | return coap_response; |
leothedragon | 0:25fa8795676b | 709 | } |
leothedragon | 0:25fa8795676b | 710 | |
leothedragon | 0:25fa8795676b | 711 | void M2MObjectInstance::notification_update(M2MBase::Observation observation_level) |
leothedragon | 0:25fa8795676b | 712 | { |
leothedragon | 0:25fa8795676b | 713 | tr_debug("M2MObjectInstance::notification_update() - level(%d)", observation_level); |
leothedragon | 0:25fa8795676b | 714 | if((M2MBase::O_Attribute & observation_level) == M2MBase::O_Attribute) { |
leothedragon | 0:25fa8795676b | 715 | tr_debug("M2MObjectInstance::notification_update() - object callback"); |
leothedragon | 0:25fa8795676b | 716 | _parent.notification_update(instance_id()); |
leothedragon | 0:25fa8795676b | 717 | } |
leothedragon | 0:25fa8795676b | 718 | if((M2MBase::OI_Attribute & observation_level) == M2MBase::OI_Attribute) { |
leothedragon | 0:25fa8795676b | 719 | tr_debug("M2MObjectInstance::notification_update() - object instance callback"); |
leothedragon | 0:25fa8795676b | 720 | M2MReportHandler *report_handler = M2MBase::report_handler(); |
leothedragon | 0:25fa8795676b | 721 | if(report_handler && is_under_observation()) { |
leothedragon | 0:25fa8795676b | 722 | report_handler->set_notification_trigger(); |
leothedragon | 0:25fa8795676b | 723 | } |
leothedragon | 0:25fa8795676b | 724 | |
leothedragon | 0:25fa8795676b | 725 | } |
leothedragon | 0:25fa8795676b | 726 | } |
leothedragon | 0:25fa8795676b | 727 | |
leothedragon | 0:25fa8795676b | 728 | M2MBase *M2MObjectInstance::get_parent() const |
leothedragon | 0:25fa8795676b | 729 | { |
leothedragon | 0:25fa8795676b | 730 | return (M2MBase *) &get_parent_object(); |
leothedragon | 0:25fa8795676b | 731 | } |
leothedragon | 0:25fa8795676b | 732 | |
leothedragon | 0:25fa8795676b | 733 | M2MBase::DataType M2MObjectInstance::convert_resource_type(M2MResourceInstance::ResourceType type) |
leothedragon | 0:25fa8795676b | 734 | { |
leothedragon | 0:25fa8795676b | 735 | M2MBase::DataType data_type = M2MBase::OBJLINK; |
leothedragon | 0:25fa8795676b | 736 | switch(type) { |
leothedragon | 0:25fa8795676b | 737 | case M2MResourceInstance::STRING: |
leothedragon | 0:25fa8795676b | 738 | data_type = M2MBase::STRING; |
leothedragon | 0:25fa8795676b | 739 | break; |
leothedragon | 0:25fa8795676b | 740 | case M2MResourceInstance::INTEGER: |
leothedragon | 0:25fa8795676b | 741 | data_type = M2MBase::INTEGER; |
leothedragon | 0:25fa8795676b | 742 | break; |
leothedragon | 0:25fa8795676b | 743 | case M2MResourceInstance::FLOAT: |
leothedragon | 0:25fa8795676b | 744 | data_type = M2MBase::FLOAT; |
leothedragon | 0:25fa8795676b | 745 | break; |
leothedragon | 0:25fa8795676b | 746 | case M2MResourceInstance::OPAQUE: |
leothedragon | 0:25fa8795676b | 747 | data_type = M2MBase::OPAQUE; |
leothedragon | 0:25fa8795676b | 748 | break; |
leothedragon | 0:25fa8795676b | 749 | case M2MResourceInstance::BOOLEAN: |
leothedragon | 0:25fa8795676b | 750 | data_type = M2MBase::BOOLEAN; |
leothedragon | 0:25fa8795676b | 751 | break; |
leothedragon | 0:25fa8795676b | 752 | case M2MResourceInstance::TIME: |
leothedragon | 0:25fa8795676b | 753 | data_type = M2MBase::TIME; |
leothedragon | 0:25fa8795676b | 754 | break; |
leothedragon | 0:25fa8795676b | 755 | case M2MResourceInstance::OBJLINK: |
leothedragon | 0:25fa8795676b | 756 | data_type = M2MBase::OBJLINK; |
leothedragon | 0:25fa8795676b | 757 | break; |
leothedragon | 0:25fa8795676b | 758 | } |
leothedragon | 0:25fa8795676b | 759 | return data_type; |
leothedragon | 0:25fa8795676b | 760 | } |