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