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