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