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