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