Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

UserRevisionLine numberNew 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
nexpaq 0:6c56fb4bc5f0 17 // Note: this macro is needed on armcc to get the the PRI*32 macros
nexpaq 0:6c56fb4bc5f0 18 // from inttypes.h in a C++ code.
nexpaq 0:6c56fb4bc5f0 19 #ifndef __STDC_FORMAT_MACROS
nexpaq 0:6c56fb4bc5f0 20 #define __STDC_FORMAT_MACROS
nexpaq 0:6c56fb4bc5f0 21 #endif
nexpaq 0:6c56fb4bc5f0 22
nexpaq 0:6c56fb4bc5f0 23 // Note: this macro is needed on armcc to get the the limit macros like UINT16_MAX
nexpaq 0:6c56fb4bc5f0 24 #ifndef __STDC_LIMIT_MACROS
nexpaq 0:6c56fb4bc5f0 25 #define __STDC_LIMIT_MACROS
nexpaq 0:6c56fb4bc5f0 26 #endif
nexpaq 0:6c56fb4bc5f0 27
nexpaq 0:6c56fb4bc5f0 28
nexpaq 0:6c56fb4bc5f0 29 #include "include/nsdlaccesshelper.h"
nexpaq 0:6c56fb4bc5f0 30 #include "include/m2mnsdlobserver.h"
nexpaq 0:6c56fb4bc5f0 31 #include "include/m2mtlvdeserializer.h"
nexpaq 0:6c56fb4bc5f0 32 #include "include/m2mtlvserializer.h"
nexpaq 0:6c56fb4bc5f0 33 #include "include/m2mnsdlinterface.h"
nexpaq 0:6c56fb4bc5f0 34 #include "mbed-client/m2mstring.h"
nexpaq 0:6c56fb4bc5f0 35 #include "mbed-client/m2msecurity.h"
nexpaq 0:6c56fb4bc5f0 36 #include "mbed-client/m2mserver.h"
nexpaq 0:6c56fb4bc5f0 37 #include "mbed-client/m2mobject.h"
nexpaq 0:6c56fb4bc5f0 38 #include "mbed-client/m2mobjectinstance.h"
nexpaq 0:6c56fb4bc5f0 39 #include "mbed-client/m2mresource.h"
nexpaq 0:6c56fb4bc5f0 40 #include "mbed-client/m2mconstants.h"
nexpaq 0:6c56fb4bc5f0 41 #include "mbed-trace/mbed_trace.h"
nexpaq 0:6c56fb4bc5f0 42 #include "mbed-client/m2mtimer.h"
nexpaq 0:6c56fb4bc5f0 43 #include "source/libNsdl/src/include/sn_grs.h"
nexpaq 0:6c56fb4bc5f0 44
nexpaq 0:6c56fb4bc5f0 45 #include <assert.h>
nexpaq 0:6c56fb4bc5f0 46 #include <inttypes.h>
nexpaq 0:6c56fb4bc5f0 47
nexpaq 0:6c56fb4bc5f0 48 #define BUFFER_SIZE 21
nexpaq 0:6c56fb4bc5f0 49 #define TRACE_GROUP "mClt"
nexpaq 0:6c56fb4bc5f0 50
nexpaq 0:6c56fb4bc5f0 51 M2MNsdlInterface::M2MNsdlInterface(M2MNsdlObserver &observer)
nexpaq 0:6c56fb4bc5f0 52 : _observer(observer),
nexpaq 0:6c56fb4bc5f0 53 _server(NULL),
nexpaq 0:6c56fb4bc5f0 54 _security(NULL),
nexpaq 0:6c56fb4bc5f0 55 _nsdl_exceution_timer(new M2MTimer(*this)),
nexpaq 0:6c56fb4bc5f0 56 _registration_timer(new M2MTimer(*this)),
nexpaq 0:6c56fb4bc5f0 57 _endpoint(NULL),
nexpaq 0:6c56fb4bc5f0 58 _resource(NULL),
nexpaq 0:6c56fb4bc5f0 59 _nsdl_handle(NULL),
nexpaq 0:6c56fb4bc5f0 60 _counter_for_nsdl(0),
nexpaq 0:6c56fb4bc5f0 61 _bootstrap_id(0),
nexpaq 0:6c56fb4bc5f0 62 _register_ongoing(false),
nexpaq 0:6c56fb4bc5f0 63 _unregister_ongoing(false),
nexpaq 0:6c56fb4bc5f0 64 _update_register_ongoing(false)
nexpaq 0:6c56fb4bc5f0 65 {
nexpaq 0:6c56fb4bc5f0 66 tr_debug("M2MNsdlInterface::M2MNsdlInterface()");
nexpaq 0:6c56fb4bc5f0 67 __nsdl_interface_list.push_back(this);
nexpaq 0:6c56fb4bc5f0 68 _sn_nsdl_address.addr_len = 0;
nexpaq 0:6c56fb4bc5f0 69 _sn_nsdl_address.addr_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 70 _sn_nsdl_address.port = 0;
nexpaq 0:6c56fb4bc5f0 71
nexpaq 0:6c56fb4bc5f0 72 // This initializes libCoap and libNsdl
nexpaq 0:6c56fb4bc5f0 73 // Parameters are function pointers to used memory allocation
nexpaq 0:6c56fb4bc5f0 74 // and free functions in structure and used functions for sending
nexpaq 0:6c56fb4bc5f0 75 // and receiving purposes.
nexpaq 0:6c56fb4bc5f0 76 _nsdl_handle = sn_nsdl_init(&(__nsdl_c_send_to_server), &(__nsdl_c_received_from_server),
nexpaq 0:6c56fb4bc5f0 77 &(__nsdl_c_memory_alloc), &(__nsdl_c_memory_free));
nexpaq 0:6c56fb4bc5f0 78
nexpaq 0:6c56fb4bc5f0 79
nexpaq 0:6c56fb4bc5f0 80 _server = new M2MServer();
nexpaq 0:6c56fb4bc5f0 81 initialize();
nexpaq 0:6c56fb4bc5f0 82 }
nexpaq 0:6c56fb4bc5f0 83
nexpaq 0:6c56fb4bc5f0 84 M2MNsdlInterface::~M2MNsdlInterface()
nexpaq 0:6c56fb4bc5f0 85 {
nexpaq 0:6c56fb4bc5f0 86 tr_debug("M2MNsdlInterface::~M2MNsdlInterface() - IN");
nexpaq 0:6c56fb4bc5f0 87 if(_resource) {
nexpaq 0:6c56fb4bc5f0 88 memory_free(_resource->resource_parameters_ptr);
nexpaq 0:6c56fb4bc5f0 89 memory_free(_resource);
nexpaq 0:6c56fb4bc5f0 90 }
nexpaq 0:6c56fb4bc5f0 91 if(_endpoint) {
nexpaq 0:6c56fb4bc5f0 92 memory_free(_endpoint->endpoint_name_ptr);
nexpaq 0:6c56fb4bc5f0 93 memory_free(_endpoint->lifetime_ptr);
nexpaq 0:6c56fb4bc5f0 94 memory_free(_endpoint->location_ptr);
nexpaq 0:6c56fb4bc5f0 95 memory_free(_endpoint);
nexpaq 0:6c56fb4bc5f0 96 }
nexpaq 0:6c56fb4bc5f0 97 delete _nsdl_exceution_timer;
nexpaq 0:6c56fb4bc5f0 98 delete _registration_timer;
nexpaq 0:6c56fb4bc5f0 99 _object_list.clear();
nexpaq 0:6c56fb4bc5f0 100 delete _server;
nexpaq 0:6c56fb4bc5f0 101 _security = NULL;
nexpaq 0:6c56fb4bc5f0 102
nexpaq 0:6c56fb4bc5f0 103 sn_nsdl_destroy(_nsdl_handle);
nexpaq 0:6c56fb4bc5f0 104 _nsdl_handle = NULL;
nexpaq 0:6c56fb4bc5f0 105
nexpaq 0:6c56fb4bc5f0 106 M2MNsdlInterfaceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 107 it = __nsdl_interface_list.begin();
nexpaq 0:6c56fb4bc5f0 108 int index = 0;
nexpaq 0:6c56fb4bc5f0 109 for (; it!=__nsdl_interface_list.end(); it++) {
nexpaq 0:6c56fb4bc5f0 110 if ((*it) == this) {
nexpaq 0:6c56fb4bc5f0 111 __nsdl_interface_list.erase(index);
nexpaq 0:6c56fb4bc5f0 112 break;
nexpaq 0:6c56fb4bc5f0 113 }
nexpaq 0:6c56fb4bc5f0 114 index++;
nexpaq 0:6c56fb4bc5f0 115 }
nexpaq 0:6c56fb4bc5f0 116 tr_debug("M2MNsdlInterface::~M2MNsdlInterface() - OUT");
nexpaq 0:6c56fb4bc5f0 117 }
nexpaq 0:6c56fb4bc5f0 118
nexpaq 0:6c56fb4bc5f0 119 bool M2MNsdlInterface::initialize()
nexpaq 0:6c56fb4bc5f0 120 {
nexpaq 0:6c56fb4bc5f0 121 tr_debug("M2MNsdlInterface::initialize()");
nexpaq 0:6c56fb4bc5f0 122 bool success = false;
nexpaq 0:6c56fb4bc5f0 123
nexpaq 0:6c56fb4bc5f0 124 //Sets the packet retransmission attempts and time interval
nexpaq 0:6c56fb4bc5f0 125 sn_nsdl_set_retransmission_parameters(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 126 MBED_CLIENT_RECONNECTION_COUNT,
nexpaq 0:6c56fb4bc5f0 127 MBED_CLIENT_RECONNECTION_INTERVAL);
nexpaq 0:6c56fb4bc5f0 128
nexpaq 0:6c56fb4bc5f0 129 // We want to parse and handle bootstrap messages
nexpaq 0:6c56fb4bc5f0 130 if (_nsdl_handle) {
nexpaq 0:6c56fb4bc5f0 131 _nsdl_handle->handle_bootstrap_msg = false;
nexpaq 0:6c56fb4bc5f0 132 }
nexpaq 0:6c56fb4bc5f0 133
nexpaq 0:6c56fb4bc5f0 134 // Allocate the memory for resources
nexpaq 0:6c56fb4bc5f0 135 _resource = (sn_nsdl_resource_info_s*)memory_alloc(sizeof(sn_nsdl_resource_info_s));
nexpaq 0:6c56fb4bc5f0 136 if(_resource) {
nexpaq 0:6c56fb4bc5f0 137 memset(_resource, 0, sizeof(sn_nsdl_resource_info_s));
nexpaq 0:6c56fb4bc5f0 138 _resource->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)memory_alloc(sizeof(sn_nsdl_resource_parameters_s));
nexpaq 0:6c56fb4bc5f0 139 if(_resource->resource_parameters_ptr) {
nexpaq 0:6c56fb4bc5f0 140 memset(_resource->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
nexpaq 0:6c56fb4bc5f0 141 }
nexpaq 0:6c56fb4bc5f0 142 }
nexpaq 0:6c56fb4bc5f0 143
nexpaq 0:6c56fb4bc5f0 144 //Allocate the memory for endpoint
nexpaq 0:6c56fb4bc5f0 145 _endpoint = (sn_nsdl_ep_parameters_s*)memory_alloc(sizeof(sn_nsdl_ep_parameters_s));
nexpaq 0:6c56fb4bc5f0 146 if(_endpoint) {
nexpaq 0:6c56fb4bc5f0 147 memset(_endpoint, 0, sizeof(sn_nsdl_ep_parameters_s));
nexpaq 0:6c56fb4bc5f0 148 success = true;
nexpaq 0:6c56fb4bc5f0 149 }
nexpaq 0:6c56fb4bc5f0 150 return success;
nexpaq 0:6c56fb4bc5f0 151 }
nexpaq 0:6c56fb4bc5f0 152
nexpaq 0:6c56fb4bc5f0 153 void M2MNsdlInterface::create_endpoint(const String &name,
nexpaq 0:6c56fb4bc5f0 154 const String &type,
nexpaq 0:6c56fb4bc5f0 155 const int32_t life_time,
nexpaq 0:6c56fb4bc5f0 156 const String &domain,
nexpaq 0:6c56fb4bc5f0 157 const uint8_t mode,
nexpaq 0:6c56fb4bc5f0 158 const String &/*context_address*/)
nexpaq 0:6c56fb4bc5f0 159 {
nexpaq 0:6c56fb4bc5f0 160 tr_debug("M2MNsdlInterface::create_endpoint( name %s type %s lifetime %" PRId32 ", domain %s, mode %d)",
nexpaq 0:6c56fb4bc5f0 161 name.c_str(), type.c_str(), life_time, domain.c_str(), mode);
nexpaq 0:6c56fb4bc5f0 162 _endpoint_name = name;
nexpaq 0:6c56fb4bc5f0 163 if(_endpoint){
nexpaq 0:6c56fb4bc5f0 164 memset(_endpoint, 0, sizeof(sn_nsdl_ep_parameters_s));
nexpaq 0:6c56fb4bc5f0 165 if(!_endpoint_name.empty()) {
nexpaq 0:6c56fb4bc5f0 166 memory_free(_endpoint->endpoint_name_ptr);
nexpaq 0:6c56fb4bc5f0 167 _endpoint->endpoint_name_ptr = alloc_string_copy((uint8_t*)_endpoint_name.c_str(), _endpoint_name.length());
nexpaq 0:6c56fb4bc5f0 168 _endpoint->endpoint_name_len = _endpoint_name.length();
nexpaq 0:6c56fb4bc5f0 169 }
nexpaq 0:6c56fb4bc5f0 170 if(!type.empty()) {
nexpaq 0:6c56fb4bc5f0 171 _endpoint->type_ptr = (uint8_t*)type.c_str();
nexpaq 0:6c56fb4bc5f0 172 _endpoint->type_len = type.length();
nexpaq 0:6c56fb4bc5f0 173 }
nexpaq 0:6c56fb4bc5f0 174 if(!domain.empty()) {
nexpaq 0:6c56fb4bc5f0 175 _endpoint->domain_name_ptr = (uint8_t*)domain.c_str();
nexpaq 0:6c56fb4bc5f0 176 _endpoint->domain_name_len = domain.length();
nexpaq 0:6c56fb4bc5f0 177 }
nexpaq 0:6c56fb4bc5f0 178 _endpoint->binding_and_mode = (sn_nsdl_oma_binding_and_mode_t)mode;
nexpaq 0:6c56fb4bc5f0 179
nexpaq 0:6c56fb4bc5f0 180 // If lifetime is less than zero then leave the field empty
nexpaq 0:6c56fb4bc5f0 181 if( life_time > 0) {
nexpaq 0:6c56fb4bc5f0 182 set_endpoint_lifetime_buffer(life_time);
nexpaq 0:6c56fb4bc5f0 183 }
nexpaq 0:6c56fb4bc5f0 184 }
nexpaq 0:6c56fb4bc5f0 185 }
nexpaq 0:6c56fb4bc5f0 186
nexpaq 0:6c56fb4bc5f0 187 void M2MNsdlInterface::set_endpoint_lifetime_buffer(int lifetime)
nexpaq 0:6c56fb4bc5f0 188 {
nexpaq 0:6c56fb4bc5f0 189 // max len of "-9223372036854775808" plus zero termination
nexpaq 0:6c56fb4bc5f0 190 char buffer[20+1];
nexpaq 0:6c56fb4bc5f0 191
nexpaq 0:6c56fb4bc5f0 192 uint32_t size = m2m::itoa_c(lifetime, buffer);
nexpaq 0:6c56fb4bc5f0 193
nexpaq 0:6c56fb4bc5f0 194 if (size <= sizeof(buffer)) {
nexpaq 0:6c56fb4bc5f0 195 _endpoint->lifetime_ptr = alloc_string_copy((uint8_t*)buffer, size);
nexpaq 0:6c56fb4bc5f0 196 if(_endpoint->lifetime_ptr) {
nexpaq 0:6c56fb4bc5f0 197 _endpoint->lifetime_len = size;
nexpaq 0:6c56fb4bc5f0 198 } else {
nexpaq 0:6c56fb4bc5f0 199 _endpoint->lifetime_len = 0;
nexpaq 0:6c56fb4bc5f0 200 }
nexpaq 0:6c56fb4bc5f0 201 }
nexpaq 0:6c56fb4bc5f0 202 }
nexpaq 0:6c56fb4bc5f0 203
nexpaq 0:6c56fb4bc5f0 204
nexpaq 0:6c56fb4bc5f0 205 void M2MNsdlInterface::delete_endpoint()
nexpaq 0:6c56fb4bc5f0 206 {
nexpaq 0:6c56fb4bc5f0 207 tr_debug("M2MNsdlInterface::delete_endpoint()");
nexpaq 0:6c56fb4bc5f0 208 if(_endpoint) {
nexpaq 0:6c56fb4bc5f0 209 free(_endpoint->lifetime_ptr);
nexpaq 0:6c56fb4bc5f0 210
nexpaq 0:6c56fb4bc5f0 211 memory_free(_endpoint);
nexpaq 0:6c56fb4bc5f0 212 _endpoint = NULL;
nexpaq 0:6c56fb4bc5f0 213 }
nexpaq 0:6c56fb4bc5f0 214 }
nexpaq 0:6c56fb4bc5f0 215
nexpaq 0:6c56fb4bc5f0 216 bool M2MNsdlInterface::create_nsdl_list_structure(const M2MObjectList &object_list)
nexpaq 0:6c56fb4bc5f0 217 {
nexpaq 0:6c56fb4bc5f0 218 tr_debug("M2MNsdlInterface::create_nsdl_list_structure()");
nexpaq 0:6c56fb4bc5f0 219 bool success = false;
nexpaq 0:6c56fb4bc5f0 220 if(!object_list.empty()) {
nexpaq 0:6c56fb4bc5f0 221 tr_debug("M2MNsdlInterface::create_nsdl_list_structure - Object count is %d", object_list.size());
nexpaq 0:6c56fb4bc5f0 222 M2MObjectList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 223 it = object_list.begin();
nexpaq 0:6c56fb4bc5f0 224 for ( ; it != object_list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 225 // Create NSDL structure for all Objects inside
nexpaq 0:6c56fb4bc5f0 226 success = create_nsdl_object_structure(*it);
nexpaq 0:6c56fb4bc5f0 227 add_object_to_list(*it);
nexpaq 0:6c56fb4bc5f0 228 }
nexpaq 0:6c56fb4bc5f0 229 }
nexpaq 0:6c56fb4bc5f0 230 return success;
nexpaq 0:6c56fb4bc5f0 231 }
nexpaq 0:6c56fb4bc5f0 232
nexpaq 0:6c56fb4bc5f0 233 bool M2MNsdlInterface::delete_nsdl_resource(const String &resource_name)
nexpaq 0:6c56fb4bc5f0 234 {
nexpaq 0:6c56fb4bc5f0 235 tr_debug("M2MNsdlInterface::delete_nsdl_resource( %s)", resource_name.c_str());
nexpaq 0:6c56fb4bc5f0 236 return (sn_nsdl_delete_resource(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 237 resource_name.length(),
nexpaq 0:6c56fb4bc5f0 238 (uint8_t *)resource_name.c_str()) == 0) ? true : false;
nexpaq 0:6c56fb4bc5f0 239 }
nexpaq 0:6c56fb4bc5f0 240
nexpaq 0:6c56fb4bc5f0 241
nexpaq 0:6c56fb4bc5f0 242 bool M2MNsdlInterface::create_bootstrap_resource(sn_nsdl_addr_s *address, const String &bootstrap_endpoint_name)
nexpaq 0:6c56fb4bc5f0 243 {
nexpaq 0:6c56fb4bc5f0 244 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 245 tr_debug("M2MNsdlInterface::create_bootstrap_resource()");
nexpaq 0:6c56fb4bc5f0 246 bool success = false;
nexpaq 0:6c56fb4bc5f0 247 sn_nsdl_bs_ep_info_t bootstrap_endpoint;
nexpaq 0:6c56fb4bc5f0 248 tr_debug("M2MNsdlInterface::create_bootstrap_resource() - endpoint name: %s", bootstrap_endpoint_name.c_str());
nexpaq 0:6c56fb4bc5f0 249 if (_endpoint->endpoint_name_ptr) {
nexpaq 0:6c56fb4bc5f0 250 memory_free(_endpoint->endpoint_name_ptr);
nexpaq 0:6c56fb4bc5f0 251 }
nexpaq 0:6c56fb4bc5f0 252 //_endpoint->endpoint_name_ptr = (uint8_t*)bootstrap_endpoint_name.c_str();
nexpaq 0:6c56fb4bc5f0 253 _endpoint->endpoint_name_ptr = alloc_string_copy((uint8_t*)bootstrap_endpoint_name.c_str(), bootstrap_endpoint_name.length());
nexpaq 0:6c56fb4bc5f0 254 _endpoint->endpoint_name_len = bootstrap_endpoint_name.length();
nexpaq 0:6c56fb4bc5f0 255 if(_bootstrap_id == 0) {
nexpaq 0:6c56fb4bc5f0 256 _bootstrap_id = sn_nsdl_oma_bootstrap(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 257 address,
nexpaq 0:6c56fb4bc5f0 258 _endpoint,
nexpaq 0:6c56fb4bc5f0 259 &bootstrap_endpoint);
nexpaq 0:6c56fb4bc5f0 260 tr_debug("M2MNsdlInterface::create_bootstrap_resource - _bootstrap_id %d", _bootstrap_id);
nexpaq 0:6c56fb4bc5f0 261 success = _bootstrap_id != 0;
nexpaq 0:6c56fb4bc5f0 262
nexpaq 0:6c56fb4bc5f0 263 }
nexpaq 0:6c56fb4bc5f0 264 return success;
nexpaq 0:6c56fb4bc5f0 265 #else
nexpaq 0:6c56fb4bc5f0 266 (void)address;
nexpaq 0:6c56fb4bc5f0 267 (void)bootstrap_endpoint_name;
nexpaq 0:6c56fb4bc5f0 268 return false;
nexpaq 0:6c56fb4bc5f0 269 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 270 }
nexpaq 0:6c56fb4bc5f0 271
nexpaq 0:6c56fb4bc5f0 272 bool M2MNsdlInterface::send_register_message(uint8_t* address,
nexpaq 0:6c56fb4bc5f0 273 const uint16_t port,
nexpaq 0:6c56fb4bc5f0 274 sn_nsdl_addr_type_e address_type)
nexpaq 0:6c56fb4bc5f0 275 {
nexpaq 0:6c56fb4bc5f0 276 tr_debug("M2MNsdlInterface::send_register_message()");
nexpaq 0:6c56fb4bc5f0 277 _nsdl_exceution_timer->stop_timer();
nexpaq 0:6c56fb4bc5f0 278 _nsdl_exceution_timer->start_timer(ONE_SECOND_TIMER * 1000,
nexpaq 0:6c56fb4bc5f0 279 M2MTimerObserver::NsdlExecution,
nexpaq 0:6c56fb4bc5f0 280 false);
nexpaq 0:6c56fb4bc5f0 281 bool success = false;
nexpaq 0:6c56fb4bc5f0 282 if(set_NSP_address(_nsdl_handle,address, port, address_type) == 0) {
nexpaq 0:6c56fb4bc5f0 283 if(!_register_ongoing) {
nexpaq 0:6c56fb4bc5f0 284 _register_ongoing = true;
nexpaq 0:6c56fb4bc5f0 285 success = sn_nsdl_register_endpoint(_nsdl_handle,_endpoint) != 0;
nexpaq 0:6c56fb4bc5f0 286 }
nexpaq 0:6c56fb4bc5f0 287 }
nexpaq 0:6c56fb4bc5f0 288 return success;
nexpaq 0:6c56fb4bc5f0 289 }
nexpaq 0:6c56fb4bc5f0 290
nexpaq 0:6c56fb4bc5f0 291 bool M2MNsdlInterface::send_update_registration(const uint32_t lifetime)
nexpaq 0:6c56fb4bc5f0 292 {
nexpaq 0:6c56fb4bc5f0 293 if (_update_register_ongoing) {
nexpaq 0:6c56fb4bc5f0 294 tr_debug("M2MNsdlInterface::send_update_registration - update already in progress");
nexpaq 0:6c56fb4bc5f0 295 return true;
nexpaq 0:6c56fb4bc5f0 296 }
nexpaq 0:6c56fb4bc5f0 297 tr_debug("M2MNsdlInterface::send_update_registration( lifetime %" PRIu32 ")", lifetime);
nexpaq 0:6c56fb4bc5f0 298 bool success = false;
nexpaq 0:6c56fb4bc5f0 299
nexpaq 0:6c56fb4bc5f0 300 create_nsdl_list_structure(_object_list);
nexpaq 0:6c56fb4bc5f0 301 //If Lifetime value is 0, then don't change the existing lifetime value
nexpaq 0:6c56fb4bc5f0 302 if(lifetime != 0) {
nexpaq 0:6c56fb4bc5f0 303 if(_endpoint->lifetime_ptr) {
nexpaq 0:6c56fb4bc5f0 304 memory_free(_endpoint->lifetime_ptr);
nexpaq 0:6c56fb4bc5f0 305 _endpoint->lifetime_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 306 _endpoint->lifetime_len = 0;
nexpaq 0:6c56fb4bc5f0 307 }
nexpaq 0:6c56fb4bc5f0 308 set_endpoint_lifetime_buffer(lifetime);
nexpaq 0:6c56fb4bc5f0 309
nexpaq 0:6c56fb4bc5f0 310 _registration_timer->stop_timer();
nexpaq 0:6c56fb4bc5f0 311 _registration_timer->start_timer(registration_time() * 1000,
nexpaq 0:6c56fb4bc5f0 312 M2MTimerObserver::Registration,
nexpaq 0:6c56fb4bc5f0 313 false);
nexpaq 0:6c56fb4bc5f0 314 if(_nsdl_handle &&
nexpaq 0:6c56fb4bc5f0 315 _endpoint && _endpoint->lifetime_ptr) {
nexpaq 0:6c56fb4bc5f0 316 tr_debug("M2MNsdlInterface::send_update_registration - new lifetime value");
nexpaq 0:6c56fb4bc5f0 317 _update_register_ongoing = true;
nexpaq 0:6c56fb4bc5f0 318 success = sn_nsdl_update_registration(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 319 _endpoint->lifetime_ptr,
nexpaq 0:6c56fb4bc5f0 320 _endpoint->lifetime_len) != 0;
nexpaq 0:6c56fb4bc5f0 321 }
nexpaq 0:6c56fb4bc5f0 322 } else {
nexpaq 0:6c56fb4bc5f0 323 if(_nsdl_handle) {
nexpaq 0:6c56fb4bc5f0 324 tr_debug("M2MNsdlInterface::send_update_registration - regular update");
nexpaq 0:6c56fb4bc5f0 325 _update_register_ongoing = true;
nexpaq 0:6c56fb4bc5f0 326 success = sn_nsdl_update_registration(_nsdl_handle, NULL, 0) != 0;
nexpaq 0:6c56fb4bc5f0 327 }
nexpaq 0:6c56fb4bc5f0 328 }
nexpaq 0:6c56fb4bc5f0 329 return success;
nexpaq 0:6c56fb4bc5f0 330 }
nexpaq 0:6c56fb4bc5f0 331
nexpaq 0:6c56fb4bc5f0 332 bool M2MNsdlInterface::send_unregister_message()
nexpaq 0:6c56fb4bc5f0 333 {
nexpaq 0:6c56fb4bc5f0 334 tr_debug("M2MNsdlInterface::send_unregister_message");
nexpaq 0:6c56fb4bc5f0 335 if (_unregister_ongoing) {
nexpaq 0:6c56fb4bc5f0 336 tr_debug("M2MNsdlInterface::send_unregister_message - unregistration already in progress");
nexpaq 0:6c56fb4bc5f0 337 return true;
nexpaq 0:6c56fb4bc5f0 338 }
nexpaq 0:6c56fb4bc5f0 339
nexpaq 0:6c56fb4bc5f0 340 bool success = false;
nexpaq 0:6c56fb4bc5f0 341 _unregister_ongoing = true;
nexpaq 0:6c56fb4bc5f0 342 success = sn_nsdl_unregister_endpoint(_nsdl_handle) != 0;
nexpaq 0:6c56fb4bc5f0 343 return success;
nexpaq 0:6c56fb4bc5f0 344 }
nexpaq 0:6c56fb4bc5f0 345
nexpaq 0:6c56fb4bc5f0 346 // XXX: move these to common place, no need to copy these wrappers to multiple places:
nexpaq 0:6c56fb4bc5f0 347 void *M2MNsdlInterface::memory_alloc(uint16_t size)
nexpaq 0:6c56fb4bc5f0 348 {
nexpaq 0:6c56fb4bc5f0 349 if(size)
nexpaq 0:6c56fb4bc5f0 350 return malloc(size);
nexpaq 0:6c56fb4bc5f0 351 else
nexpaq 0:6c56fb4bc5f0 352 return 0;
nexpaq 0:6c56fb4bc5f0 353 }
nexpaq 0:6c56fb4bc5f0 354
nexpaq 0:6c56fb4bc5f0 355 void M2MNsdlInterface::memory_free(void *ptr)
nexpaq 0:6c56fb4bc5f0 356 {
nexpaq 0:6c56fb4bc5f0 357 if(ptr)
nexpaq 0:6c56fb4bc5f0 358 free(ptr);
nexpaq 0:6c56fb4bc5f0 359 }
nexpaq 0:6c56fb4bc5f0 360
nexpaq 0:6c56fb4bc5f0 361 uint8_t* M2MNsdlInterface::alloc_string_copy(const uint8_t* source, uint16_t size)
nexpaq 0:6c56fb4bc5f0 362 {
nexpaq 0:6c56fb4bc5f0 363 assert(source != NULL);
nexpaq 0:6c56fb4bc5f0 364
nexpaq 0:6c56fb4bc5f0 365 uint8_t* result = (uint8_t*)memory_alloc(size + 1);
nexpaq 0:6c56fb4bc5f0 366 if (result) {
nexpaq 0:6c56fb4bc5f0 367 memcpy(result, source, size);
nexpaq 0:6c56fb4bc5f0 368 result[size] = '\0';
nexpaq 0:6c56fb4bc5f0 369 }
nexpaq 0:6c56fb4bc5f0 370 return result;
nexpaq 0:6c56fb4bc5f0 371 }
nexpaq 0:6c56fb4bc5f0 372
nexpaq 0:6c56fb4bc5f0 373 uint8_t M2MNsdlInterface::send_to_server_callback(struct nsdl_s * /*nsdl_handle*/,
nexpaq 0:6c56fb4bc5f0 374 sn_nsdl_capab_e /*protocol*/,
nexpaq 0:6c56fb4bc5f0 375 uint8_t *data_ptr,
nexpaq 0:6c56fb4bc5f0 376 uint16_t data_len,
nexpaq 0:6c56fb4bc5f0 377 sn_nsdl_addr_s *address)
nexpaq 0:6c56fb4bc5f0 378 {
nexpaq 0:6c56fb4bc5f0 379 tr_debug("M2MNsdlInterface::send_to_server_callback()");
nexpaq 0:6c56fb4bc5f0 380 _observer.coap_message_ready(data_ptr,data_len,address);
nexpaq 0:6c56fb4bc5f0 381 return 1;
nexpaq 0:6c56fb4bc5f0 382 }
nexpaq 0:6c56fb4bc5f0 383
nexpaq 0:6c56fb4bc5f0 384 uint8_t M2MNsdlInterface::received_from_server_callback(struct nsdl_s * nsdl_handle,
nexpaq 0:6c56fb4bc5f0 385 sn_coap_hdr_s *coap_header,
nexpaq 0:6c56fb4bc5f0 386 sn_nsdl_addr_s *address)
nexpaq 0:6c56fb4bc5f0 387 {
nexpaq 0:6c56fb4bc5f0 388 tr_debug("M2MNsdlInterface::received_from_server_callback - msg id:%" PRIu16, coap_header->msg_id);
nexpaq 0:6c56fb4bc5f0 389 tr_debug("M2MNsdlInterface::received_from_server_callback - registration id:%" PRIu16, nsdl_handle->register_msg_id);
nexpaq 0:6c56fb4bc5f0 390 tr_debug("M2MNsdlInterface::received_from_server_callback - unregistration id:%" PRIu16, nsdl_handle->unregister_msg_id);
nexpaq 0:6c56fb4bc5f0 391 tr_debug("M2MNsdlInterface::received_from_server_callback - update registration id:%" PRIu16, nsdl_handle->update_register_msg_id);
nexpaq 0:6c56fb4bc5f0 392 _observer.coap_data_processed();
nexpaq 0:6c56fb4bc5f0 393 uint8_t value = 0;
nexpaq 0:6c56fb4bc5f0 394 if(nsdl_handle && coap_header) {
nexpaq 0:6c56fb4bc5f0 395 bool is_bootstrap_msg = address && (nsdl_handle->oma_bs_address_len == address->addr_len) &&
nexpaq 0:6c56fb4bc5f0 396 (nsdl_handle->oma_bs_port == address->port) &&
nexpaq 0:6c56fb4bc5f0 397 !memcmp(nsdl_handle->oma_bs_address_ptr, address->addr_ptr, nsdl_handle->oma_bs_address_len);
nexpaq 0:6c56fb4bc5f0 398 if(coap_header->msg_id == nsdl_handle->register_msg_id) {
nexpaq 0:6c56fb4bc5f0 399 _register_ongoing = false;
nexpaq 0:6c56fb4bc5f0 400 if(coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CREATED) {
nexpaq 0:6c56fb4bc5f0 401 tr_debug("M2MNsdlInterface::received_from_server_callback - registration callback");
nexpaq 0:6c56fb4bc5f0 402 _observer.client_registered(_server);
nexpaq 0:6c56fb4bc5f0 403 // If lifetime is less than zero then leave the field empty
nexpaq 0:6c56fb4bc5f0 404 if(coap_header->options_list_ptr) {
nexpaq 0:6c56fb4bc5f0 405 if(coap_header->options_list_ptr->max_age_ptr) {
nexpaq 0:6c56fb4bc5f0 406 memory_free(_endpoint->lifetime_ptr);
nexpaq 0:6c56fb4bc5f0 407 _endpoint->lifetime_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 408 _endpoint->lifetime_len = 0;
nexpaq 0:6c56fb4bc5f0 409
nexpaq 0:6c56fb4bc5f0 410 uint32_t max_time = 0;
nexpaq 0:6c56fb4bc5f0 411 for(int i=0;i < coap_header->options_list_ptr->max_age_len; i++) {
nexpaq 0:6c56fb4bc5f0 412 max_time += (*(coap_header->options_list_ptr->max_age_ptr + i) & 0xff) <<
nexpaq 0:6c56fb4bc5f0 413 8*(coap_header->options_list_ptr->max_age_len- 1 - i);
nexpaq 0:6c56fb4bc5f0 414 }
nexpaq 0:6c56fb4bc5f0 415 // If lifetime is less than zero then leave the field empty
nexpaq 0:6c56fb4bc5f0 416 if( max_time > 0) {
nexpaq 0:6c56fb4bc5f0 417 set_endpoint_lifetime_buffer(max_time);
nexpaq 0:6c56fb4bc5f0 418 }
nexpaq 0:6c56fb4bc5f0 419 }
nexpaq 0:6c56fb4bc5f0 420 if(coap_header->options_list_ptr->location_path_ptr) {
nexpaq 0:6c56fb4bc5f0 421
nexpaq 0:6c56fb4bc5f0 422 memory_free(_endpoint->location_ptr);
nexpaq 0:6c56fb4bc5f0 423
nexpaq 0:6c56fb4bc5f0 424 _endpoint->location_ptr = alloc_string_copy(coap_header->options_list_ptr->location_path_ptr, coap_header->options_list_ptr->location_path_len);
nexpaq 0:6c56fb4bc5f0 425 if (_endpoint->location_ptr != NULL) {
nexpaq 0:6c56fb4bc5f0 426 _endpoint->location_len = coap_header->options_list_ptr->location_path_len;
nexpaq 0:6c56fb4bc5f0 427 }
nexpaq 0:6c56fb4bc5f0 428 sn_nsdl_set_endpoint_location(_nsdl_handle,_endpoint->location_ptr,_endpoint->location_len);
nexpaq 0:6c56fb4bc5f0 429 }
nexpaq 0:6c56fb4bc5f0 430 }
nexpaq 0:6c56fb4bc5f0 431 if(_endpoint->lifetime_ptr) {
nexpaq 0:6c56fb4bc5f0 432 _registration_timer->stop_timer();
nexpaq 0:6c56fb4bc5f0 433 _registration_timer->start_timer(registration_time() * 1000,
nexpaq 0:6c56fb4bc5f0 434 M2MTimerObserver::Registration,
nexpaq 0:6c56fb4bc5f0 435 false);
nexpaq 0:6c56fb4bc5f0 436 }
nexpaq 0:6c56fb4bc5f0 437 } else {
nexpaq 0:6c56fb4bc5f0 438 tr_error("M2MNsdlInterface::received_from_server_callback - registration error %d", coap_header->msg_code);
nexpaq 0:6c56fb4bc5f0 439 // Try to do clean register again
nexpaq 0:6c56fb4bc5f0 440 if(COAP_MSG_CODE_RESPONSE_BAD_REQUEST == coap_header->msg_code ||
nexpaq 0:6c56fb4bc5f0 441 COAP_MSG_CODE_RESPONSE_FORBIDDEN == coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 442 _observer.registration_error(M2MInterface::InvalidParameters, false);
nexpaq 0:6c56fb4bc5f0 443 } else {
nexpaq 0:6c56fb4bc5f0 444 _observer.registration_error(M2MInterface::NetworkError, true);
nexpaq 0:6c56fb4bc5f0 445 }
nexpaq 0:6c56fb4bc5f0 446
nexpaq 0:6c56fb4bc5f0 447 }
nexpaq 0:6c56fb4bc5f0 448 } else if(coap_header->msg_id == nsdl_handle->unregister_msg_id) {
nexpaq 0:6c56fb4bc5f0 449 _unregister_ongoing = false;
nexpaq 0:6c56fb4bc5f0 450 tr_debug("M2MNsdlInterface::received_from_server_callback - unregistration callback");
nexpaq 0:6c56fb4bc5f0 451 if(coap_header->msg_code == COAP_MSG_CODE_RESPONSE_DELETED) {
nexpaq 0:6c56fb4bc5f0 452 _registration_timer->stop_timer();
nexpaq 0:6c56fb4bc5f0 453 _observer.client_unregistered();
nexpaq 0:6c56fb4bc5f0 454 } else {
nexpaq 0:6c56fb4bc5f0 455 tr_error("M2MNsdlInterface::received_from_server_callback - unregistration error %d", coap_header->msg_code);
nexpaq 0:6c56fb4bc5f0 456 M2MInterface::Error error = interface_error(coap_header);
nexpaq 0:6c56fb4bc5f0 457 _observer.registration_error(error);
nexpaq 0:6c56fb4bc5f0 458 }
nexpaq 0:6c56fb4bc5f0 459 } else if(coap_header->msg_id == nsdl_handle->update_register_msg_id) {
nexpaq 0:6c56fb4bc5f0 460 _update_register_ongoing = false;
nexpaq 0:6c56fb4bc5f0 461 if(coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CHANGED) {
nexpaq 0:6c56fb4bc5f0 462 tr_debug("M2MNsdlInterface::received_from_server_callback - registration_updated successfully");
nexpaq 0:6c56fb4bc5f0 463 _observer.registration_updated(*_server);
nexpaq 0:6c56fb4bc5f0 464 } else {
nexpaq 0:6c56fb4bc5f0 465 tr_error("M2MNsdlInterface::received_from_server_callback - registration_updated failed %d", coap_header->msg_code);
nexpaq 0:6c56fb4bc5f0 466 _registration_timer->stop_timer();
nexpaq 0:6c56fb4bc5f0 467 _register_ongoing = true;
nexpaq 0:6c56fb4bc5f0 468 sn_nsdl_register_endpoint(_nsdl_handle,_endpoint);
nexpaq 0:6c56fb4bc5f0 469 }
nexpaq 0:6c56fb4bc5f0 470 }
nexpaq 0:6c56fb4bc5f0 471 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 472 else if(coap_header->msg_id == nsdl_handle->bootstrap_msg_id) {
nexpaq 0:6c56fb4bc5f0 473 tr_debug("M2MNsdlInterface::received_from_server_callback - bootstrap");
nexpaq 0:6c56fb4bc5f0 474 _bootstrap_id = 0;
nexpaq 0:6c56fb4bc5f0 475 M2MInterface::Error error = interface_error(coap_header);
nexpaq 0:6c56fb4bc5f0 476 if(error != M2MInterface::ErrorNone) {
nexpaq 0:6c56fb4bc5f0 477 handle_bootstrap_error();
nexpaq 0:6c56fb4bc5f0 478 }
nexpaq 0:6c56fb4bc5f0 479 }
nexpaq 0:6c56fb4bc5f0 480 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 481 else {
nexpaq 0:6c56fb4bc5f0 482 if(COAP_MSG_CODE_REQUEST_PUT == coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 483 if (is_bootstrap_msg) {
nexpaq 0:6c56fb4bc5f0 484 handle_bootstrap_put_message(coap_header, address);
nexpaq 0:6c56fb4bc5f0 485 }
nexpaq 0:6c56fb4bc5f0 486 }
nexpaq 0:6c56fb4bc5f0 487 else if(COAP_MSG_CODE_REQUEST_DELETE == coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 488 if (is_bootstrap_msg) {
nexpaq 0:6c56fb4bc5f0 489 handle_bootstrap_delete(coap_header, address);
nexpaq 0:6c56fb4bc5f0 490 }
nexpaq 0:6c56fb4bc5f0 491 }
nexpaq 0:6c56fb4bc5f0 492 else if(COAP_MSG_CODE_REQUEST_POST == coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 493 if(is_bootstrap_msg) {
nexpaq 0:6c56fb4bc5f0 494 handle_bootstrap_finished(coap_header, address);
nexpaq 0:6c56fb4bc5f0 495 }
nexpaq 0:6c56fb4bc5f0 496 else if(coap_header->uri_path_ptr) {
nexpaq 0:6c56fb4bc5f0 497 sn_coap_hdr_s *coap_response = NULL;
nexpaq 0:6c56fb4bc5f0 498 bool execute_value_updated = false;
nexpaq 0:6c56fb4bc5f0 499 M2MObjectInstance *obj_instance = NULL;
nexpaq 0:6c56fb4bc5f0 500 String resource_name = coap_to_string(coap_header->uri_path_ptr,
nexpaq 0:6c56fb4bc5f0 501 coap_header->uri_path_len);
nexpaq 0:6c56fb4bc5f0 502
nexpaq 0:6c56fb4bc5f0 503 String object_name;
nexpaq 0:6c56fb4bc5f0 504 int slash_found = resource_name.find_last_of('/');
nexpaq 0:6c56fb4bc5f0 505 //The POST operation here is only allowed for non-existing object instances
nexpaq 0:6c56fb4bc5f0 506 if(slash_found != -1) {
nexpaq 0:6c56fb4bc5f0 507 object_name = resource_name.substr(0,slash_found);
nexpaq 0:6c56fb4bc5f0 508 if( object_name.find_last_of('/') != -1){
nexpaq 0:6c56fb4bc5f0 509 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 510 coap_header,
nexpaq 0:6c56fb4bc5f0 511 COAP_MSG_CODE_RESPONSE_NOT_FOUND);
nexpaq 0:6c56fb4bc5f0 512 } else {
nexpaq 0:6c56fb4bc5f0 513 int32_t instance_id = atoi(resource_name.substr(slash_found+1,
nexpaq 0:6c56fb4bc5f0 514 resource_name.size()-object_name.size()).c_str());
nexpaq 0:6c56fb4bc5f0 515 M2MBase* base = find_resource(object_name);
nexpaq 0:6c56fb4bc5f0 516 if(base && (instance_id >= 0) && (instance_id < UINT16_MAX)) {
nexpaq 0:6c56fb4bc5f0 517 if(coap_header->payload_ptr) {
nexpaq 0:6c56fb4bc5f0 518 M2MObject* object = static_cast<M2MObject*> (base);
nexpaq 0:6c56fb4bc5f0 519 obj_instance = object->create_object_instance(instance_id);
nexpaq 0:6c56fb4bc5f0 520 if(obj_instance) {
nexpaq 0:6c56fb4bc5f0 521 obj_instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
nexpaq 0:6c56fb4bc5f0 522 coap_response = obj_instance->handle_post_request(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 523 coap_header,
nexpaq 0:6c56fb4bc5f0 524 this,
nexpaq 0:6c56fb4bc5f0 525 execute_value_updated);
nexpaq 0:6c56fb4bc5f0 526 }
nexpaq 0:6c56fb4bc5f0 527 if(coap_response && coap_response->msg_code != COAP_MSG_CODE_RESPONSE_CREATED) {
nexpaq 0:6c56fb4bc5f0 528 //Invalid request so remove created ObjectInstance
nexpaq 0:6c56fb4bc5f0 529 object->remove_object_instance(instance_id);
nexpaq 0:6c56fb4bc5f0 530 } else {
nexpaq 0:6c56fb4bc5f0 531 tr_debug("M2MNsdlInterface::received_from_server_callback - Send Update registration for Create");
nexpaq 0:6c56fb4bc5f0 532 send_update_registration();
nexpaq 0:6c56fb4bc5f0 533 }
nexpaq 0:6c56fb4bc5f0 534 } else {
nexpaq 0:6c56fb4bc5f0 535 tr_debug("M2MNsdlInterface::received_from_server_callback - Missing Payload - Cannot create");
nexpaq 0:6c56fb4bc5f0 536 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 537 coap_header,
nexpaq 0:6c56fb4bc5f0 538 COAP_MSG_CODE_RESPONSE_BAD_REQUEST);
nexpaq 0:6c56fb4bc5f0 539 }
nexpaq 0:6c56fb4bc5f0 540 } else { //if(base)
nexpaq 0:6c56fb4bc5f0 541 tr_debug("M2MNsdlInterface::received_from_server_callback - Missing BASE - Cannot create");
nexpaq 0:6c56fb4bc5f0 542 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 543 coap_header,
nexpaq 0:6c56fb4bc5f0 544 COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 545 }
nexpaq 0:6c56fb4bc5f0 546 }
nexpaq 0:6c56fb4bc5f0 547 } else{ // if(slash_found != -1)
nexpaq 0:6c56fb4bc5f0 548 tr_debug("M2MNsdlInterface::received_from_server_callback - slash_found - Cannot create");
nexpaq 0:6c56fb4bc5f0 549 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 550 coap_header,
nexpaq 0:6c56fb4bc5f0 551 COAP_MSG_CODE_RESPONSE_NOT_FOUND);
nexpaq 0:6c56fb4bc5f0 552 }
nexpaq 0:6c56fb4bc5f0 553 if(coap_response) {
nexpaq 0:6c56fb4bc5f0 554 tr_debug("M2MNsdlInterface::received_from_server_callback - send CoAP response");
nexpaq 0:6c56fb4bc5f0 555 (sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response) == 0) ? value = 0 : value = 1;
nexpaq 0:6c56fb4bc5f0 556 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
nexpaq 0:6c56fb4bc5f0 557 }
nexpaq 0:6c56fb4bc5f0 558 if (execute_value_updated) {
nexpaq 0:6c56fb4bc5f0 559 value_updated(obj_instance, obj_instance->name());
nexpaq 0:6c56fb4bc5f0 560 }
nexpaq 0:6c56fb4bc5f0 561 }
nexpaq 0:6c56fb4bc5f0 562 }
nexpaq 0:6c56fb4bc5f0 563 }
nexpaq 0:6c56fb4bc5f0 564 }
nexpaq 0:6c56fb4bc5f0 565 return value;
nexpaq 0:6c56fb4bc5f0 566 }
nexpaq 0:6c56fb4bc5f0 567
nexpaq 0:6c56fb4bc5f0 568 uint8_t M2MNsdlInterface::resource_callback(struct nsdl_s */*nsdl_handle*/,
nexpaq 0:6c56fb4bc5f0 569 sn_coap_hdr_s *received_coap_header,
nexpaq 0:6c56fb4bc5f0 570 sn_nsdl_addr_s *address,
nexpaq 0:6c56fb4bc5f0 571 sn_nsdl_capab_e /*nsdl_capab*/)
nexpaq 0:6c56fb4bc5f0 572 {
nexpaq 0:6c56fb4bc5f0 573 tr_debug("M2MNsdlInterface::resource_callback()");
nexpaq 0:6c56fb4bc5f0 574 _observer.coap_data_processed();
nexpaq 0:6c56fb4bc5f0 575 uint8_t result = 1;
nexpaq 0:6c56fb4bc5f0 576 sn_coap_hdr_s *coap_response = NULL;
nexpaq 0:6c56fb4bc5f0 577 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 4.00
nexpaq 0:6c56fb4bc5f0 578 String resource_name = coap_to_string(received_coap_header->uri_path_ptr,
nexpaq 0:6c56fb4bc5f0 579 received_coap_header->uri_path_len);
nexpaq 0:6c56fb4bc5f0 580 tr_debug("M2MNsdlInterface::resource_callback() - resource_name %s", resource_name.c_str());
nexpaq 0:6c56fb4bc5f0 581 bool execute_value_updated = false;
nexpaq 0:6c56fb4bc5f0 582 M2MBase* base = find_resource(resource_name);
nexpaq 0:6c56fb4bc5f0 583 if(base) {
nexpaq 0:6c56fb4bc5f0 584 base->set_uri_path(resource_name);
nexpaq 0:6c56fb4bc5f0 585 if(COAP_MSG_CODE_REQUEST_GET == received_coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 586 coap_response = base->handle_get_request(_nsdl_handle, received_coap_header,this);
nexpaq 0:6c56fb4bc5f0 587 } else if(COAP_MSG_CODE_REQUEST_PUT == received_coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 588 coap_response = base->handle_put_request(_nsdl_handle, received_coap_header, this, execute_value_updated);
nexpaq 0:6c56fb4bc5f0 589 } else if(COAP_MSG_CODE_REQUEST_POST == received_coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 590 if(base->base_type() == M2MBase::ResourceInstance) {
nexpaq 0:6c56fb4bc5f0 591 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
nexpaq 0:6c56fb4bc5f0 592 } else {
nexpaq 0:6c56fb4bc5f0 593 coap_response = base->handle_post_request(_nsdl_handle, received_coap_header,this, execute_value_updated);
nexpaq 0:6c56fb4bc5f0 594 }
nexpaq 0:6c56fb4bc5f0 595 } else if(COAP_MSG_CODE_REQUEST_DELETE == received_coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 596 // Delete the object instance
nexpaq 0:6c56fb4bc5f0 597 tr_debug("M2MNsdlInterface::resource_callback() - DELETE the object instance");
nexpaq 0:6c56fb4bc5f0 598 M2MBase::BaseType type = base->base_type();
nexpaq 0:6c56fb4bc5f0 599 if(M2MBase::ObjectInstance == type) {
nexpaq 0:6c56fb4bc5f0 600 M2MBase* base_object = find_resource(base->name());
nexpaq 0:6c56fb4bc5f0 601 if(base_object) {
nexpaq 0:6c56fb4bc5f0 602 M2MObject *object = static_cast<M2MObject*> (base_object);
nexpaq 0:6c56fb4bc5f0 603 int slash_found = resource_name.find_last_of('/');
nexpaq 0:6c56fb4bc5f0 604 // Object instance validty checks done in upper level, no need for error handling
nexpaq 0:6c56fb4bc5f0 605 if(slash_found != -1) {
nexpaq 0:6c56fb4bc5f0 606 String object_name;
nexpaq 0:6c56fb4bc5f0 607 object_name = resource_name.substr(slash_found + 1, resource_name.length());
nexpaq 0:6c56fb4bc5f0 608 if (object->remove_object_instance(strtoul(
nexpaq 0:6c56fb4bc5f0 609 object_name.c_str(),
nexpaq 0:6c56fb4bc5f0 610 NULL,
nexpaq 0:6c56fb4bc5f0 611 10))) {
nexpaq 0:6c56fb4bc5f0 612 msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
nexpaq 0:6c56fb4bc5f0 613 }
nexpaq 0:6c56fb4bc5f0 614 }
nexpaq 0:6c56fb4bc5f0 615 }
nexpaq 0:6c56fb4bc5f0 616 } else {
nexpaq 0:6c56fb4bc5f0 617 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
nexpaq 0:6c56fb4bc5f0 618 }
nexpaq 0:6c56fb4bc5f0 619 } else if(COAP_MSG_TYPE_RESET == received_coap_header->msg_type) {
nexpaq 0:6c56fb4bc5f0 620 // Cancel ongoing observation
nexpaq 0:6c56fb4bc5f0 621 tr_error("M2MNsdlInterface::resource_callback() - RESET message");
nexpaq 0:6c56fb4bc5f0 622 M2MBase::BaseType type = base->base_type();
nexpaq 0:6c56fb4bc5f0 623 switch (type) {
nexpaq 0:6c56fb4bc5f0 624 case M2MBase::Object:
nexpaq 0:6c56fb4bc5f0 625 base->remove_observation_level(M2MBase::O_Attribute);
nexpaq 0:6c56fb4bc5f0 626 break;
nexpaq 0:6c56fb4bc5f0 627 case M2MBase::Resource:
nexpaq 0:6c56fb4bc5f0 628 base->remove_observation_level(M2MBase::R_Attribute);
nexpaq 0:6c56fb4bc5f0 629 break;
nexpaq 0:6c56fb4bc5f0 630 case M2MBase::ObjectInstance:
nexpaq 0:6c56fb4bc5f0 631 base->remove_observation_level(M2MBase::OI_Attribute);
nexpaq 0:6c56fb4bc5f0 632 break;
nexpaq 0:6c56fb4bc5f0 633 default:
nexpaq 0:6c56fb4bc5f0 634 break;
nexpaq 0:6c56fb4bc5f0 635 }
nexpaq 0:6c56fb4bc5f0 636 base->set_under_observation(false, this);
nexpaq 0:6c56fb4bc5f0 637 }
nexpaq 0:6c56fb4bc5f0 638 } else {
nexpaq 0:6c56fb4bc5f0 639 tr_debug("M2MNsdlInterface::resource_callback() - Resource NOT FOUND");
nexpaq 0:6c56fb4bc5f0 640 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
nexpaq 0:6c56fb4bc5f0 641 }
nexpaq 0:6c56fb4bc5f0 642 if(!coap_response) {
nexpaq 0:6c56fb4bc5f0 643 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 644 received_coap_header,
nexpaq 0:6c56fb4bc5f0 645 msg_code);
nexpaq 0:6c56fb4bc5f0 646 }
nexpaq 0:6c56fb4bc5f0 647
nexpaq 0:6c56fb4bc5f0 648 if(coap_response) {
nexpaq 0:6c56fb4bc5f0 649 tr_debug("M2MNsdlInterface::resource_callback() - send CoAP response");
nexpaq 0:6c56fb4bc5f0 650 (sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response) == 0) ? result = 0 : result = 1;
nexpaq 0:6c56fb4bc5f0 651 if(coap_response->payload_ptr) {
nexpaq 0:6c56fb4bc5f0 652 free(coap_response->payload_ptr);
nexpaq 0:6c56fb4bc5f0 653 coap_response->payload_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 654 }
nexpaq 0:6c56fb4bc5f0 655 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
nexpaq 0:6c56fb4bc5f0 656 }
nexpaq 0:6c56fb4bc5f0 657 if (execute_value_updated) {
nexpaq 0:6c56fb4bc5f0 658 value_updated(base,base->uri_path());
nexpaq 0:6c56fb4bc5f0 659 }
nexpaq 0:6c56fb4bc5f0 660
nexpaq 0:6c56fb4bc5f0 661 return result;
nexpaq 0:6c56fb4bc5f0 662 }
nexpaq 0:6c56fb4bc5f0 663
nexpaq 0:6c56fb4bc5f0 664 bool M2MNsdlInterface::process_received_data(uint8_t *data,
nexpaq 0:6c56fb4bc5f0 665 uint16_t data_size,
nexpaq 0:6c56fb4bc5f0 666 sn_nsdl_addr_s *address)
nexpaq 0:6c56fb4bc5f0 667 {
nexpaq 0:6c56fb4bc5f0 668 tr_debug("M2MNsdlInterface::process_received_data( data size %d)", data_size);
nexpaq 0:6c56fb4bc5f0 669 return (0 == sn_nsdl_process_coap(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 670 data,
nexpaq 0:6c56fb4bc5f0 671 data_size,
nexpaq 0:6c56fb4bc5f0 672 address)) ? true : false;
nexpaq 0:6c56fb4bc5f0 673 }
nexpaq 0:6c56fb4bc5f0 674
nexpaq 0:6c56fb4bc5f0 675 void M2MNsdlInterface::stop_timers()
nexpaq 0:6c56fb4bc5f0 676 {
nexpaq 0:6c56fb4bc5f0 677 tr_debug("M2MNsdlInterface::stop_timers()");
nexpaq 0:6c56fb4bc5f0 678 if(_registration_timer) {
nexpaq 0:6c56fb4bc5f0 679 _registration_timer->stop_timer();
nexpaq 0:6c56fb4bc5f0 680 }
nexpaq 0:6c56fb4bc5f0 681 if (_nsdl_exceution_timer) {
nexpaq 0:6c56fb4bc5f0 682 _nsdl_exceution_timer->stop_timer();
nexpaq 0:6c56fb4bc5f0 683 }
nexpaq 0:6c56fb4bc5f0 684 _bootstrap_id = 0;
nexpaq 0:6c56fb4bc5f0 685 _register_ongoing = false;
nexpaq 0:6c56fb4bc5f0 686 _unregister_ongoing = false;
nexpaq 0:6c56fb4bc5f0 687 _update_register_ongoing = false;
nexpaq 0:6c56fb4bc5f0 688 }
nexpaq 0:6c56fb4bc5f0 689
nexpaq 0:6c56fb4bc5f0 690 void M2MNsdlInterface::timer_expired(M2MTimerObserver::Type type)
nexpaq 0:6c56fb4bc5f0 691 {
nexpaq 0:6c56fb4bc5f0 692 if(M2MTimerObserver::NsdlExecution == type) {
nexpaq 0:6c56fb4bc5f0 693 sn_nsdl_exec(_nsdl_handle, _counter_for_nsdl);
nexpaq 0:6c56fb4bc5f0 694 _counter_for_nsdl++;
nexpaq 0:6c56fb4bc5f0 695 } else if(M2MTimerObserver::Registration == type) {
nexpaq 0:6c56fb4bc5f0 696 tr_debug("M2MNsdlInterface::timer_expired - M2MTimerObserver::Registration - Send update registration");
nexpaq 0:6c56fb4bc5f0 697 send_update_registration();
nexpaq 0:6c56fb4bc5f0 698 }
nexpaq 0:6c56fb4bc5f0 699 }
nexpaq 0:6c56fb4bc5f0 700
nexpaq 0:6c56fb4bc5f0 701 void M2MNsdlInterface::observation_to_be_sent(M2MBase *object,
nexpaq 0:6c56fb4bc5f0 702 uint16_t obs_number,
nexpaq 0:6c56fb4bc5f0 703 m2m::Vector<uint16_t> changed_instance_ids,
nexpaq 0:6c56fb4bc5f0 704 bool send_object)
nexpaq 0:6c56fb4bc5f0 705 {
nexpaq 0:6c56fb4bc5f0 706 __mutex_claim();
nexpaq 0:6c56fb4bc5f0 707 tr_debug("M2MNsdlInterface::observation_to_be_sent(), %s", object->uri_path().c_str());
nexpaq 0:6c56fb4bc5f0 708 if(object) {
nexpaq 0:6c56fb4bc5f0 709 M2MBase::BaseType type = object->base_type();
nexpaq 0:6c56fb4bc5f0 710 if(type == M2MBase::Object) {
nexpaq 0:6c56fb4bc5f0 711 send_object_observation(static_cast<M2MObject*> (object),
nexpaq 0:6c56fb4bc5f0 712 obs_number,
nexpaq 0:6c56fb4bc5f0 713 changed_instance_ids,
nexpaq 0:6c56fb4bc5f0 714 send_object);
nexpaq 0:6c56fb4bc5f0 715 } else if(type == M2MBase::ObjectInstance) {
nexpaq 0:6c56fb4bc5f0 716 send_object_instance_observation(static_cast<M2MObjectInstance*> (object), obs_number);
nexpaq 0:6c56fb4bc5f0 717 } else if(type == M2MBase::Resource) {
nexpaq 0:6c56fb4bc5f0 718 send_resource_observation(static_cast<M2MResource*> (object), obs_number);
nexpaq 0:6c56fb4bc5f0 719 }
nexpaq 0:6c56fb4bc5f0 720 }
nexpaq 0:6c56fb4bc5f0 721 __mutex_release();
nexpaq 0:6c56fb4bc5f0 722 }
nexpaq 0:6c56fb4bc5f0 723
nexpaq 0:6c56fb4bc5f0 724 void M2MNsdlInterface::send_delayed_response(M2MBase *base)
nexpaq 0:6c56fb4bc5f0 725 {
nexpaq 0:6c56fb4bc5f0 726 __mutex_claim();
nexpaq 0:6c56fb4bc5f0 727 tr_debug("M2MNsdlInterface::send_delayed_response()");
nexpaq 0:6c56fb4bc5f0 728 M2MResource *resource = NULL;
nexpaq 0:6c56fb4bc5f0 729 if(base) {
nexpaq 0:6c56fb4bc5f0 730 if(M2MBase::Resource == base->base_type()) {
nexpaq 0:6c56fb4bc5f0 731 resource = static_cast<M2MResource *> (base);
nexpaq 0:6c56fb4bc5f0 732 }
nexpaq 0:6c56fb4bc5f0 733 if(resource) {
nexpaq 0:6c56fb4bc5f0 734 sn_coap_hdr_s * coap_response = static_cast<sn_coap_hdr_s *>(malloc(sizeof(sn_coap_hdr_s)));
nexpaq 0:6c56fb4bc5f0 735 if(coap_response) {
nexpaq 0:6c56fb4bc5f0 736 memset(coap_response,0,sizeof(sn_coap_hdr_s));
nexpaq 0:6c56fb4bc5f0 737
nexpaq 0:6c56fb4bc5f0 738 coap_response->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
nexpaq 0:6c56fb4bc5f0 739 coap_response->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
nexpaq 0:6c56fb4bc5f0 740 resource->get_delayed_token(coap_response->token_ptr,coap_response->token_len);
nexpaq 0:6c56fb4bc5f0 741
nexpaq 0:6c56fb4bc5f0 742 uint32_t length = 0;
nexpaq 0:6c56fb4bc5f0 743 resource->get_value(coap_response->payload_ptr, length);
nexpaq 0:6c56fb4bc5f0 744 coap_response->payload_len = length;
nexpaq 0:6c56fb4bc5f0 745
nexpaq 0:6c56fb4bc5f0 746 sn_nsdl_send_coap_message(_nsdl_handle, _nsdl_handle->nsp_address_ptr->omalw_address_ptr, coap_response);
nexpaq 0:6c56fb4bc5f0 747
nexpaq 0:6c56fb4bc5f0 748 if(coap_response->payload_ptr) {
nexpaq 0:6c56fb4bc5f0 749 free(coap_response->payload_ptr);
nexpaq 0:6c56fb4bc5f0 750 coap_response->payload_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 751 }
nexpaq 0:6c56fb4bc5f0 752 if(coap_response->token_ptr) {
nexpaq 0:6c56fb4bc5f0 753 free(coap_response->token_ptr);
nexpaq 0:6c56fb4bc5f0 754 coap_response->token_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 755 }
nexpaq 0:6c56fb4bc5f0 756 free(coap_response);
nexpaq 0:6c56fb4bc5f0 757 }
nexpaq 0:6c56fb4bc5f0 758 }
nexpaq 0:6c56fb4bc5f0 759 }
nexpaq 0:6c56fb4bc5f0 760 __mutex_release();
nexpaq 0:6c56fb4bc5f0 761 }
nexpaq 0:6c56fb4bc5f0 762
nexpaq 0:6c56fb4bc5f0 763 void M2MNsdlInterface::resource_to_be_deleted(const String &resource_name)
nexpaq 0:6c56fb4bc5f0 764 {
nexpaq 0:6c56fb4bc5f0 765 __mutex_claim();
nexpaq 0:6c56fb4bc5f0 766 tr_debug("M2MNsdlInterface::resource_to_be_deleted(resource_name %s)", resource_name.c_str());
nexpaq 0:6c56fb4bc5f0 767 delete_nsdl_resource(resource_name);
nexpaq 0:6c56fb4bc5f0 768 __mutex_release();
nexpaq 0:6c56fb4bc5f0 769 }
nexpaq 0:6c56fb4bc5f0 770
nexpaq 0:6c56fb4bc5f0 771 void M2MNsdlInterface::value_updated(M2MBase *base,
nexpaq 0:6c56fb4bc5f0 772 const String &object_name)
nexpaq 0:6c56fb4bc5f0 773 {
nexpaq 0:6c56fb4bc5f0 774 tr_debug("M2MNsdlInterface::value_updated()");
nexpaq 0:6c56fb4bc5f0 775 if(base) {
nexpaq 0:6c56fb4bc5f0 776 switch(base->base_type()) {
nexpaq 0:6c56fb4bc5f0 777 case M2MBase::Object:
nexpaq 0:6c56fb4bc5f0 778 create_nsdl_object_structure(static_cast<M2MObject*> (base));
nexpaq 0:6c56fb4bc5f0 779 break;
nexpaq 0:6c56fb4bc5f0 780 case M2MBase::ObjectInstance:
nexpaq 0:6c56fb4bc5f0 781 create_nsdl_object_instance_structure(static_cast<M2MObjectInstance*> (base));
nexpaq 0:6c56fb4bc5f0 782 break;
nexpaq 0:6c56fb4bc5f0 783 case M2MBase::Resource: {
nexpaq 0:6c56fb4bc5f0 784 M2MResource* resource = static_cast<M2MResource*> (base);
nexpaq 0:6c56fb4bc5f0 785 create_nsdl_resource_structure(resource,object_name,
nexpaq 0:6c56fb4bc5f0 786 resource->supports_multiple_instances());
nexpaq 0:6c56fb4bc5f0 787 }
nexpaq 0:6c56fb4bc5f0 788 break;
nexpaq 0:6c56fb4bc5f0 789 case M2MBase::ResourceInstance: {
nexpaq 0:6c56fb4bc5f0 790 M2MResourceInstance* instance = static_cast<M2MResourceInstance*> (base);
nexpaq 0:6c56fb4bc5f0 791 create_nsdl_resource(instance,object_name);
nexpaq 0:6c56fb4bc5f0 792 }
nexpaq 0:6c56fb4bc5f0 793 break;
nexpaq 0:6c56fb4bc5f0 794 }
nexpaq 0:6c56fb4bc5f0 795 }
nexpaq 0:6c56fb4bc5f0 796
nexpaq 0:6c56fb4bc5f0 797 if (base->is_value_updated_function_set()) {
nexpaq 0:6c56fb4bc5f0 798 base->execute_value_updated(base->name());
nexpaq 0:6c56fb4bc5f0 799 }
nexpaq 0:6c56fb4bc5f0 800 else {
nexpaq 0:6c56fb4bc5f0 801 _observer.value_updated(base);
nexpaq 0:6c56fb4bc5f0 802 }
nexpaq 0:6c56fb4bc5f0 803 }
nexpaq 0:6c56fb4bc5f0 804
nexpaq 0:6c56fb4bc5f0 805 void M2MNsdlInterface::remove_object(M2MBase *object)
nexpaq 0:6c56fb4bc5f0 806 {
nexpaq 0:6c56fb4bc5f0 807 __mutex_claim();
nexpaq 0:6c56fb4bc5f0 808 tr_debug("M2MNsdlInterface::remove_object()");
nexpaq 0:6c56fb4bc5f0 809 M2MObject* rem_object = static_cast<M2MObject*> (object);
nexpaq 0:6c56fb4bc5f0 810 if(rem_object && !_object_list.empty()) {
nexpaq 0:6c56fb4bc5f0 811 M2MObjectList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 812 it = _object_list.begin();
nexpaq 0:6c56fb4bc5f0 813 int index = 0;
nexpaq 0:6c56fb4bc5f0 814 for ( ; it != _object_list.end(); it++, index++ ) {
nexpaq 0:6c56fb4bc5f0 815 if((*it) == rem_object) {
nexpaq 0:6c56fb4bc5f0 816 _object_list.erase(index);
nexpaq 0:6c56fb4bc5f0 817 break;
nexpaq 0:6c56fb4bc5f0 818 }
nexpaq 0:6c56fb4bc5f0 819 }
nexpaq 0:6c56fb4bc5f0 820 }
nexpaq 0:6c56fb4bc5f0 821 if(_object_list.empty()) {
nexpaq 0:6c56fb4bc5f0 822 _object_list.clear();
nexpaq 0:6c56fb4bc5f0 823 }
nexpaq 0:6c56fb4bc5f0 824 __mutex_release();
nexpaq 0:6c56fb4bc5f0 825 }
nexpaq 0:6c56fb4bc5f0 826
nexpaq 0:6c56fb4bc5f0 827 bool M2MNsdlInterface::create_nsdl_object_structure(M2MObject *object)
nexpaq 0:6c56fb4bc5f0 828 {
nexpaq 0:6c56fb4bc5f0 829 tr_debug("M2MNsdlInterface::create_nsdl_object_structure()");
nexpaq 0:6c56fb4bc5f0 830 bool success = false;
nexpaq 0:6c56fb4bc5f0 831 if(object) {
nexpaq 0:6c56fb4bc5f0 832 M2MObjectInstanceList instance_list = object->instances();
nexpaq 0:6c56fb4bc5f0 833 tr_debug("M2MNsdlInterface::create_nsdl_object_structure - Object Instance count %d", instance_list.size());
nexpaq 0:6c56fb4bc5f0 834 if(!instance_list.empty()) {
nexpaq 0:6c56fb4bc5f0 835 M2MObjectInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 836 it = instance_list.begin();
nexpaq 0:6c56fb4bc5f0 837 for ( ; it != instance_list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 838 // Create NSDL structure for all object instances inside
nexpaq 0:6c56fb4bc5f0 839 success = create_nsdl_object_instance_structure(*it);
nexpaq 0:6c56fb4bc5f0 840 }
nexpaq 0:6c56fb4bc5f0 841 }
nexpaq 0:6c56fb4bc5f0 842 }
nexpaq 0:6c56fb4bc5f0 843 if((object->operation() != M2MBase::NOT_ALLOWED)) {
nexpaq 0:6c56fb4bc5f0 844 success = create_nsdl_resource(object,object->name(),object->register_uri());
nexpaq 0:6c56fb4bc5f0 845 }
nexpaq 0:6c56fb4bc5f0 846 return success;
nexpaq 0:6c56fb4bc5f0 847 }
nexpaq 0:6c56fb4bc5f0 848
nexpaq 0:6c56fb4bc5f0 849 bool M2MNsdlInterface::create_nsdl_object_instance_structure(M2MObjectInstance *object_instance)
nexpaq 0:6c56fb4bc5f0 850 {
nexpaq 0:6c56fb4bc5f0 851 tr_debug("M2MNsdlInterface::create_nsdl_object_instance_structure()");
nexpaq 0:6c56fb4bc5f0 852 bool success = false;
nexpaq 0:6c56fb4bc5f0 853 if( object_instance) {
nexpaq 0:6c56fb4bc5f0 854
nexpaq 0:6c56fb4bc5f0 855 // Append object instance id to the object name.
nexpaq 0:6c56fb4bc5f0 856 String object_name = object_instance->name();
nexpaq 0:6c56fb4bc5f0 857 object_name.push_back('/');
nexpaq 0:6c56fb4bc5f0 858 object_name.append_int(object_instance->instance_id());
nexpaq 0:6c56fb4bc5f0 859
nexpaq 0:6c56fb4bc5f0 860 M2MResourceList res_list = object_instance->resources();
nexpaq 0:6c56fb4bc5f0 861 tr_debug("M2MNsdlInterface::create_nsdl_object_instance_structure - ResourceBase count %d", res_list.size());
nexpaq 0:6c56fb4bc5f0 862 if(!res_list.empty()) {
nexpaq 0:6c56fb4bc5f0 863 M2MResourceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 864 it = res_list.begin();
nexpaq 0:6c56fb4bc5f0 865 for ( ; it != res_list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 866 // Create NSDL structure for all resources inside
nexpaq 0:6c56fb4bc5f0 867 success = create_nsdl_resource_structure(*it,object_name,
nexpaq 0:6c56fb4bc5f0 868 (*it)->supports_multiple_instances());
nexpaq 0:6c56fb4bc5f0 869 }
nexpaq 0:6c56fb4bc5f0 870 }
nexpaq 0:6c56fb4bc5f0 871 if(object_instance->operation() != M2MBase::NOT_ALLOWED) {
nexpaq 0:6c56fb4bc5f0 872 success = create_nsdl_resource(object_instance,object_name,object_instance->register_uri());
nexpaq 0:6c56fb4bc5f0 873 }
nexpaq 0:6c56fb4bc5f0 874 }
nexpaq 0:6c56fb4bc5f0 875 return success;
nexpaq 0:6c56fb4bc5f0 876 }
nexpaq 0:6c56fb4bc5f0 877
nexpaq 0:6c56fb4bc5f0 878 bool M2MNsdlInterface::create_nsdl_resource_structure(M2MResource *res,
nexpaq 0:6c56fb4bc5f0 879 const String &object_name,
nexpaq 0:6c56fb4bc5f0 880 bool multiple_instances)
nexpaq 0:6c56fb4bc5f0 881 {
nexpaq 0:6c56fb4bc5f0 882 tr_debug("M2MNsdlInterface::create_nsdl_resource_structure(object_name %s)", object_name.c_str());
nexpaq 0:6c56fb4bc5f0 883 bool success = false;
nexpaq 0:6c56fb4bc5f0 884 if(res) {
nexpaq 0:6c56fb4bc5f0 885 // Append object name to the resource.
nexpaq 0:6c56fb4bc5f0 886 // Take out the instance Id and append to the
nexpaq 0:6c56fb4bc5f0 887 // resource name like "object/0/+ resource + / + 0"
nexpaq 0:6c56fb4bc5f0 888 String res_name = object_name;
nexpaq 0:6c56fb4bc5f0 889 if (strcmp(res_name.c_str(), res->uri_path().c_str()) != 0) {
nexpaq 0:6c56fb4bc5f0 890 res_name.push_back('/');
nexpaq 0:6c56fb4bc5f0 891 res_name.append(res->name().c_str(),res->name().length());
nexpaq 0:6c56fb4bc5f0 892 }
nexpaq 0:6c56fb4bc5f0 893
nexpaq 0:6c56fb4bc5f0 894 // if there are multiple instances supported
nexpaq 0:6c56fb4bc5f0 895 // then add instance Id into creating resource path
nexpaq 0:6c56fb4bc5f0 896 // else normal /object_id/object_instance/resource_id format.
nexpaq 0:6c56fb4bc5f0 897 if(multiple_instances) {
nexpaq 0:6c56fb4bc5f0 898 M2MResourceInstanceList res_list = res->resource_instances();
nexpaq 0:6c56fb4bc5f0 899 tr_debug("M2MNsdlInterface::create_nsdl_resource_structure - ResourceInstance count %d", res_list.size());
nexpaq 0:6c56fb4bc5f0 900 if(!res_list.empty()) {
nexpaq 0:6c56fb4bc5f0 901 M2MResourceInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 902 it = res_list.begin();
nexpaq 0:6c56fb4bc5f0 903 for ( ; it != res_list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 904 String inst_name = res_name;
nexpaq 0:6c56fb4bc5f0 905 // Create NSDL structure for all resources inside
nexpaq 0:6c56fb4bc5f0 906 inst_name.push_back('/');
nexpaq 0:6c56fb4bc5f0 907 inst_name.append_int((*it)->instance_id());
nexpaq 0:6c56fb4bc5f0 908
nexpaq 0:6c56fb4bc5f0 909 success = create_nsdl_resource((*it),inst_name,(*it)->register_uri());
nexpaq 0:6c56fb4bc5f0 910 }
nexpaq 0:6c56fb4bc5f0 911 // Register the main Resource as well along with ResourceInstances
nexpaq 0:6c56fb4bc5f0 912 success = create_nsdl_resource(res,res_name,res->register_uri());
nexpaq 0:6c56fb4bc5f0 913 }
nexpaq 0:6c56fb4bc5f0 914 } else {
nexpaq 0:6c56fb4bc5f0 915 tr_debug("M2MNsdlInterface::create_nsdl_resource_structure - res_name %s", res_name.c_str());
nexpaq 0:6c56fb4bc5f0 916 success = create_nsdl_resource(res,res_name,res->register_uri());
nexpaq 0:6c56fb4bc5f0 917 }
nexpaq 0:6c56fb4bc5f0 918 }
nexpaq 0:6c56fb4bc5f0 919 return success;
nexpaq 0:6c56fb4bc5f0 920 }
nexpaq 0:6c56fb4bc5f0 921
nexpaq 0:6c56fb4bc5f0 922 bool M2MNsdlInterface::create_nsdl_resource(M2MBase *base, const String &name, bool publish_uri)
nexpaq 0:6c56fb4bc5f0 923 {
nexpaq 0:6c56fb4bc5f0 924 __mutex_claim();
nexpaq 0:6c56fb4bc5f0 925 tr_debug("M2MNsdlInterface::create_nsdl_resource(name %s)", name.c_str());
nexpaq 0:6c56fb4bc5f0 926 bool success = false;
nexpaq 0:6c56fb4bc5f0 927 uint8_t* buffer = 0;
nexpaq 0:6c56fb4bc5f0 928 uint32_t length = 0;
nexpaq 0:6c56fb4bc5f0 929
nexpaq 0:6c56fb4bc5f0 930 // Create the NSDL Resource Pointer...
nexpaq 0:6c56fb4bc5f0 931 if(base) {
nexpaq 0:6c56fb4bc5f0 932 sn_nsdl_resource_info_s* resource = sn_nsdl_get_resource(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 933 name.length(),
nexpaq 0:6c56fb4bc5f0 934 (uint8_t*)name.c_str());
nexpaq 0:6c56fb4bc5f0 935
nexpaq 0:6c56fb4bc5f0 936 if(resource) {
nexpaq 0:6c56fb4bc5f0 937 bool changed = false;
nexpaq 0:6c56fb4bc5f0 938 success = true;
nexpaq 0:6c56fb4bc5f0 939 if(resource->mode == SN_GRS_STATIC) {
nexpaq 0:6c56fb4bc5f0 940 if((M2MBase::Resource == base->base_type() ||
nexpaq 0:6c56fb4bc5f0 941 M2MBase::ResourceInstance == base->base_type()) &&
nexpaq 0:6c56fb4bc5f0 942 M2MBase::Static == base->mode()) {
nexpaq 0:6c56fb4bc5f0 943 M2MResourceInstance *res = (M2MResourceInstance*) base;
nexpaq 0:6c56fb4bc5f0 944 res->get_value(buffer,length);
nexpaq 0:6c56fb4bc5f0 945 if(resource->resource) {
nexpaq 0:6c56fb4bc5f0 946 memory_free(resource->resource);
nexpaq 0:6c56fb4bc5f0 947 }
nexpaq 0:6c56fb4bc5f0 948 resource->resource = buffer;
nexpaq 0:6c56fb4bc5f0 949 resource->resourcelen = length;
nexpaq 0:6c56fb4bc5f0 950 resource->publish_uri = publish_uri;
nexpaq 0:6c56fb4bc5f0 951 }
nexpaq 0:6c56fb4bc5f0 952 }
nexpaq 0:6c56fb4bc5f0 953 // Check if the access level for the resource has changed.
nexpaq 0:6c56fb4bc5f0 954 if(resource->access != (sn_grs_resource_acl_e)base->operation()) {
nexpaq 0:6c56fb4bc5f0 955 changed = true;
nexpaq 0:6c56fb4bc5f0 956 resource->access = (sn_grs_resource_acl_e)base->operation();
nexpaq 0:6c56fb4bc5f0 957 }
nexpaq 0:6c56fb4bc5f0 958 if(resource->resource_parameters_ptr) {
nexpaq 0:6c56fb4bc5f0 959 // Check if the observation parameter for the resource has changed.
nexpaq 0:6c56fb4bc5f0 960 if(resource->resource_parameters_ptr->observable != (uint8_t)base->is_observable()) {
nexpaq 0:6c56fb4bc5f0 961 changed = true;
nexpaq 0:6c56fb4bc5f0 962 resource->resource_parameters_ptr->observable = (uint8_t)base->is_observable();
nexpaq 0:6c56fb4bc5f0 963 }
nexpaq 0:6c56fb4bc5f0 964 }
nexpaq 0:6c56fb4bc5f0 965 if(changed && resource->resource_parameters_ptr) {
nexpaq 0:6c56fb4bc5f0 966 resource->resource_parameters_ptr->registered = SN_NDSL_RESOURCE_NOT_REGISTERED;
nexpaq 0:6c56fb4bc5f0 967 }
nexpaq 0:6c56fb4bc5f0 968 } else if(_resource) {
nexpaq 0:6c56fb4bc5f0 969 base->set_under_observation(false,this);
nexpaq 0:6c56fb4bc5f0 970 //TODO: implement access control
nexpaq 0:6c56fb4bc5f0 971 // Currently complete access is given
nexpaq 0:6c56fb4bc5f0 972 _resource->access = (sn_grs_resource_acl_e)base->operation();
nexpaq 0:6c56fb4bc5f0 973
nexpaq 0:6c56fb4bc5f0 974 if((M2MBase::Resource == base->base_type() ||
nexpaq 0:6c56fb4bc5f0 975 M2MBase::ResourceInstance == base->base_type()) &&
nexpaq 0:6c56fb4bc5f0 976 M2MBase::Static == base->mode()) {
nexpaq 0:6c56fb4bc5f0 977 M2MResourceInstance *res = (M2MResourceInstance*)base;
nexpaq 0:6c56fb4bc5f0 978 // Static resource is updated
nexpaq 0:6c56fb4bc5f0 979 _resource->mode = SN_GRS_STATIC;
nexpaq 0:6c56fb4bc5f0 980
nexpaq 0:6c56fb4bc5f0 981 res->get_value(buffer,length);
nexpaq 0:6c56fb4bc5f0 982 _resource->resource = buffer;
nexpaq 0:6c56fb4bc5f0 983 _resource->resourcelen = length;
nexpaq 0:6c56fb4bc5f0 984 }
nexpaq 0:6c56fb4bc5f0 985
nexpaq 0:6c56fb4bc5f0 986 if(M2MBase::Dynamic == base->mode()){
nexpaq 0:6c56fb4bc5f0 987 // Dynamic resource is updated
nexpaq 0:6c56fb4bc5f0 988 _resource->mode = SN_GRS_DYNAMIC;
nexpaq 0:6c56fb4bc5f0 989 _resource->sn_grs_dyn_res_callback = __nsdl_c_callback;
nexpaq 0:6c56fb4bc5f0 990 }
nexpaq 0:6c56fb4bc5f0 991
nexpaq 0:6c56fb4bc5f0 992 if( _resource->path != NULL ){
nexpaq 0:6c56fb4bc5f0 993 memory_free(_resource->path);
nexpaq 0:6c56fb4bc5f0 994 _resource->path = NULL;
nexpaq 0:6c56fb4bc5f0 995 }
nexpaq 0:6c56fb4bc5f0 996 if(name.length() > 0 ){
nexpaq 0:6c56fb4bc5f0 997 _resource->path = alloc_string_copy((uint8_t*)name.c_str(), name.length());
nexpaq 0:6c56fb4bc5f0 998 if(_resource->path) {
nexpaq 0:6c56fb4bc5f0 999 _resource->pathlen = name.length();
nexpaq 0:6c56fb4bc5f0 1000 }
nexpaq 0:6c56fb4bc5f0 1001 }
nexpaq 0:6c56fb4bc5f0 1002 if(!base->resource_type().empty() && _resource->resource_parameters_ptr) {
nexpaq 0:6c56fb4bc5f0 1003 _resource->resource_parameters_ptr->resource_type_ptr =
nexpaq 0:6c56fb4bc5f0 1004 alloc_string_copy((uint8_t*)base->resource_type().c_str(),
nexpaq 0:6c56fb4bc5f0 1005 base->resource_type().length());
nexpaq 0:6c56fb4bc5f0 1006 if(_resource->resource_parameters_ptr->resource_type_ptr) {
nexpaq 0:6c56fb4bc5f0 1007 _resource->resource_parameters_ptr->resource_type_len =
nexpaq 0:6c56fb4bc5f0 1008 base->resource_type().length();
nexpaq 0:6c56fb4bc5f0 1009 }
nexpaq 0:6c56fb4bc5f0 1010 }
nexpaq 0:6c56fb4bc5f0 1011 if(!base->interface_description().empty() && _resource->resource_parameters_ptr) {
nexpaq 0:6c56fb4bc5f0 1012 _resource->resource_parameters_ptr->interface_description_ptr =
nexpaq 0:6c56fb4bc5f0 1013 alloc_string_copy((uint8_t*)base->interface_description().c_str(),
nexpaq 0:6c56fb4bc5f0 1014 base->interface_description().length());
nexpaq 0:6c56fb4bc5f0 1015 if(_resource->resource_parameters_ptr->interface_description_ptr) {
nexpaq 0:6c56fb4bc5f0 1016 _resource->resource_parameters_ptr->interface_description_len =
nexpaq 0:6c56fb4bc5f0 1017 base->interface_description().length();
nexpaq 0:6c56fb4bc5f0 1018 }
nexpaq 0:6c56fb4bc5f0 1019 }
nexpaq 0:6c56fb4bc5f0 1020 if(_resource->resource_parameters_ptr) {
nexpaq 0:6c56fb4bc5f0 1021 _resource->resource_parameters_ptr->coap_content_type = base->coap_content_type();
nexpaq 0:6c56fb4bc5f0 1022 _resource->resource_parameters_ptr->observable = (uint8_t)base->is_observable();
nexpaq 0:6c56fb4bc5f0 1023 }
nexpaq 0:6c56fb4bc5f0 1024 _resource->publish_uri = publish_uri;
nexpaq 0:6c56fb4bc5f0 1025 int8_t result = sn_nsdl_create_resource(_nsdl_handle,_resource);
nexpaq 0:6c56fb4bc5f0 1026 tr_debug("M2MNsdlInterface::create_nsdl_resource - Creating in NSDL-C result %d", result);
nexpaq 0:6c56fb4bc5f0 1027
nexpaq 0:6c56fb4bc5f0 1028 // Either the resource is created or it already
nexpaq 0:6c56fb4bc5f0 1029 // exists , then result is success.
nexpaq 0:6c56fb4bc5f0 1030 if (result == 0 ||
nexpaq 0:6c56fb4bc5f0 1031 result == -2){
nexpaq 0:6c56fb4bc5f0 1032 success = true;
nexpaq 0:6c56fb4bc5f0 1033 }
nexpaq 0:6c56fb4bc5f0 1034
nexpaq 0:6c56fb4bc5f0 1035 if(_resource->path) {
nexpaq 0:6c56fb4bc5f0 1036 memory_free(_resource->path);
nexpaq 0:6c56fb4bc5f0 1037 }
nexpaq 0:6c56fb4bc5f0 1038 if(_resource->resource_parameters_ptr->resource_type_ptr){
nexpaq 0:6c56fb4bc5f0 1039 memory_free(_resource->resource_parameters_ptr->resource_type_ptr);
nexpaq 0:6c56fb4bc5f0 1040 }
nexpaq 0:6c56fb4bc5f0 1041 if(_resource->resource_parameters_ptr->interface_description_ptr){
nexpaq 0:6c56fb4bc5f0 1042 memory_free(_resource->resource_parameters_ptr->interface_description_ptr);
nexpaq 0:6c56fb4bc5f0 1043 }
nexpaq 0:6c56fb4bc5f0 1044 if (_resource->resource) {
nexpaq 0:6c56fb4bc5f0 1045 memory_free(_resource->resource);
nexpaq 0:6c56fb4bc5f0 1046 }
nexpaq 0:6c56fb4bc5f0 1047
nexpaq 0:6c56fb4bc5f0 1048 //Clear up the filled resource to fill up new resource.
nexpaq 0:6c56fb4bc5f0 1049 clear_resource(_resource);
nexpaq 0:6c56fb4bc5f0 1050
nexpaq 0:6c56fb4bc5f0 1051 if(success) {
nexpaq 0:6c56fb4bc5f0 1052 base->set_under_observation(false,this);
nexpaq 0:6c56fb4bc5f0 1053 }
nexpaq 0:6c56fb4bc5f0 1054 }
nexpaq 0:6c56fb4bc5f0 1055 }
nexpaq 0:6c56fb4bc5f0 1056 __mutex_release();
nexpaq 0:6c56fb4bc5f0 1057 return success;
nexpaq 0:6c56fb4bc5f0 1058 }
nexpaq 0:6c56fb4bc5f0 1059
nexpaq 0:6c56fb4bc5f0 1060 // convenience method to get the URI from its buffer field...
nexpaq 0:6c56fb4bc5f0 1061 String M2MNsdlInterface::coap_to_string(uint8_t *coap_data,int coap_data_length)
nexpaq 0:6c56fb4bc5f0 1062 {
nexpaq 0:6c56fb4bc5f0 1063 String value = "";
nexpaq 0:6c56fb4bc5f0 1064 if (coap_data != NULL && coap_data_length > 0) {
nexpaq 0:6c56fb4bc5f0 1065 value.append_raw((char *)coap_data,coap_data_length);
nexpaq 0:6c56fb4bc5f0 1066 }
nexpaq 0:6c56fb4bc5f0 1067 return value;
nexpaq 0:6c56fb4bc5f0 1068 }
nexpaq 0:6c56fb4bc5f0 1069
nexpaq 0:6c56fb4bc5f0 1070 uint64_t M2MNsdlInterface::registration_time()
nexpaq 0:6c56fb4bc5f0 1071 {
nexpaq 0:6c56fb4bc5f0 1072 uint64_t value = 0;
nexpaq 0:6c56fb4bc5f0 1073 if(_endpoint->lifetime_ptr) {
nexpaq 0:6c56fb4bc5f0 1074 value = atol((const char*)_endpoint->lifetime_ptr);
nexpaq 0:6c56fb4bc5f0 1075 }
nexpaq 0:6c56fb4bc5f0 1076
nexpaq 0:6c56fb4bc5f0 1077 if(value >= OPTIMUM_LIFETIME) {
nexpaq 0:6c56fb4bc5f0 1078 value = value - REDUCE_LIFETIME;
nexpaq 0:6c56fb4bc5f0 1079 } else {
nexpaq 0:6c56fb4bc5f0 1080 value = REDUCTION_FACTOR * value;
nexpaq 0:6c56fb4bc5f0 1081 }
nexpaq 0:6c56fb4bc5f0 1082 tr_debug("M2MNsdlInterface::registration_time - value (in seconds) %ld", value);
nexpaq 0:6c56fb4bc5f0 1083 return value;
nexpaq 0:6c56fb4bc5f0 1084 }
nexpaq 0:6c56fb4bc5f0 1085
nexpaq 0:6c56fb4bc5f0 1086 M2MBase* M2MNsdlInterface::find_resource(const String &object_name)
nexpaq 0:6c56fb4bc5f0 1087 {
nexpaq 0:6c56fb4bc5f0 1088 M2MBase *object = NULL;
nexpaq 0:6c56fb4bc5f0 1089 if(!_object_list.empty()) {
nexpaq 0:6c56fb4bc5f0 1090 M2MObjectList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1091 it = _object_list.begin();
nexpaq 0:6c56fb4bc5f0 1092 for ( ; it != _object_list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 1093 if((*it)->name() == object_name) {
nexpaq 0:6c56fb4bc5f0 1094 object = (*it);
nexpaq 0:6c56fb4bc5f0 1095 tr_debug("M2MNsdlInterface::find_resource(%s) found", object_name.c_str());
nexpaq 0:6c56fb4bc5f0 1096 break;
nexpaq 0:6c56fb4bc5f0 1097 }
nexpaq 0:6c56fb4bc5f0 1098 object = find_resource((*it),object_name);
nexpaq 0:6c56fb4bc5f0 1099 if(object != NULL) {
nexpaq 0:6c56fb4bc5f0 1100 tr_debug("M2MNsdlInterface::find_resource(%s) found", object_name.c_str());
nexpaq 0:6c56fb4bc5f0 1101 break;
nexpaq 0:6c56fb4bc5f0 1102 }
nexpaq 0:6c56fb4bc5f0 1103 }
nexpaq 0:6c56fb4bc5f0 1104 }
nexpaq 0:6c56fb4bc5f0 1105 return object;
nexpaq 0:6c56fb4bc5f0 1106 }
nexpaq 0:6c56fb4bc5f0 1107
nexpaq 0:6c56fb4bc5f0 1108 M2MBase* M2MNsdlInterface::find_resource(const M2MObject *object,
nexpaq 0:6c56fb4bc5f0 1109 const String &object_instance)
nexpaq 0:6c56fb4bc5f0 1110 {
nexpaq 0:6c56fb4bc5f0 1111 M2MBase *instance = NULL;
nexpaq 0:6c56fb4bc5f0 1112 if(object) {
nexpaq 0:6c56fb4bc5f0 1113 M2MObjectInstanceList list = object->instances();
nexpaq 0:6c56fb4bc5f0 1114 if(!list.empty()) {
nexpaq 0:6c56fb4bc5f0 1115 M2MObjectInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1116 it = list.begin();
nexpaq 0:6c56fb4bc5f0 1117 for ( ; it != list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 1118 // Append object instance id to the object name.
nexpaq 0:6c56fb4bc5f0 1119 String name = (*it)->name();
nexpaq 0:6c56fb4bc5f0 1120 name.push_back('/');
nexpaq 0:6c56fb4bc5f0 1121 name.append_int((*it)->instance_id());
nexpaq 0:6c56fb4bc5f0 1122
nexpaq 0:6c56fb4bc5f0 1123 if(name == object_instance){
nexpaq 0:6c56fb4bc5f0 1124 instance = (*it);
nexpaq 0:6c56fb4bc5f0 1125 break;
nexpaq 0:6c56fb4bc5f0 1126 }
nexpaq 0:6c56fb4bc5f0 1127 instance = find_resource((*it),object_instance);
nexpaq 0:6c56fb4bc5f0 1128 if(instance != NULL){
nexpaq 0:6c56fb4bc5f0 1129 break;
nexpaq 0:6c56fb4bc5f0 1130 }
nexpaq 0:6c56fb4bc5f0 1131 }
nexpaq 0:6c56fb4bc5f0 1132 }
nexpaq 0:6c56fb4bc5f0 1133 }
nexpaq 0:6c56fb4bc5f0 1134 return instance;
nexpaq 0:6c56fb4bc5f0 1135 }
nexpaq 0:6c56fb4bc5f0 1136
nexpaq 0:6c56fb4bc5f0 1137 M2MBase* M2MNsdlInterface::find_resource(const M2MObjectInstance *object_instance,
nexpaq 0:6c56fb4bc5f0 1138 const String &resource_instance)
nexpaq 0:6c56fb4bc5f0 1139 {
nexpaq 0:6c56fb4bc5f0 1140 M2MBase *instance = NULL;
nexpaq 0:6c56fb4bc5f0 1141 if(object_instance) {
nexpaq 0:6c56fb4bc5f0 1142 M2MResourceList list = object_instance->resources();
nexpaq 0:6c56fb4bc5f0 1143 if(!list.empty()) {
nexpaq 0:6c56fb4bc5f0 1144 M2MResourceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1145 it = list.begin();
nexpaq 0:6c56fb4bc5f0 1146 for ( ; it != list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 1147 String name = object_instance->name();
nexpaq 0:6c56fb4bc5f0 1148 // Append object instance id to the object name.
nexpaq 0:6c56fb4bc5f0 1149 name.push_back('/');
nexpaq 0:6c56fb4bc5f0 1150 name.append_int(object_instance->instance_id());
nexpaq 0:6c56fb4bc5f0 1151
nexpaq 0:6c56fb4bc5f0 1152 name.push_back('/');
nexpaq 0:6c56fb4bc5f0 1153 name+= (*it)->name();
nexpaq 0:6c56fb4bc5f0 1154
nexpaq 0:6c56fb4bc5f0 1155 if(name == resource_instance) {
nexpaq 0:6c56fb4bc5f0 1156 instance = *it;
nexpaq 0:6c56fb4bc5f0 1157 break;
nexpaq 0:6c56fb4bc5f0 1158 } else if((*it)->supports_multiple_instances()) {
nexpaq 0:6c56fb4bc5f0 1159 instance = find_resource((*it),name, resource_instance);
nexpaq 0:6c56fb4bc5f0 1160 if(instance != NULL){
nexpaq 0:6c56fb4bc5f0 1161 break;
nexpaq 0:6c56fb4bc5f0 1162 }
nexpaq 0:6c56fb4bc5f0 1163 }
nexpaq 0:6c56fb4bc5f0 1164 }
nexpaq 0:6c56fb4bc5f0 1165 }
nexpaq 0:6c56fb4bc5f0 1166 }
nexpaq 0:6c56fb4bc5f0 1167 return instance;
nexpaq 0:6c56fb4bc5f0 1168 }
nexpaq 0:6c56fb4bc5f0 1169
nexpaq 0:6c56fb4bc5f0 1170 M2MBase* M2MNsdlInterface::find_resource(const M2MResource *resource,
nexpaq 0:6c56fb4bc5f0 1171 const String &object_name,
nexpaq 0:6c56fb4bc5f0 1172 const String &resource_instance)
nexpaq 0:6c56fb4bc5f0 1173 {
nexpaq 0:6c56fb4bc5f0 1174 M2MBase *res = NULL;
nexpaq 0:6c56fb4bc5f0 1175 if(resource) {
nexpaq 0:6c56fb4bc5f0 1176 if(resource->supports_multiple_instances()) {
nexpaq 0:6c56fb4bc5f0 1177 M2MResourceInstanceList list = resource->resource_instances();
nexpaq 0:6c56fb4bc5f0 1178 if(!list.empty()) {
nexpaq 0:6c56fb4bc5f0 1179 M2MResourceInstanceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1180 it = list.begin();
nexpaq 0:6c56fb4bc5f0 1181 for ( ; it != list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 1182 String name = object_name;
nexpaq 0:6c56fb4bc5f0 1183 // if there are multiple instances supported
nexpaq 0:6c56fb4bc5f0 1184 // then add instance Id into creating resource path
nexpaq 0:6c56fb4bc5f0 1185 // else normal /object_id/object_instance/resource_id format.
nexpaq 0:6c56fb4bc5f0 1186
nexpaq 0:6c56fb4bc5f0 1187 name.push_back('/');
nexpaq 0:6c56fb4bc5f0 1188 name.append_int((*it)->instance_id());
nexpaq 0:6c56fb4bc5f0 1189
nexpaq 0:6c56fb4bc5f0 1190 if(name == resource_instance){
nexpaq 0:6c56fb4bc5f0 1191 res = (*it);
nexpaq 0:6c56fb4bc5f0 1192 break;
nexpaq 0:6c56fb4bc5f0 1193 }
nexpaq 0:6c56fb4bc5f0 1194 }
nexpaq 0:6c56fb4bc5f0 1195 }
nexpaq 0:6c56fb4bc5f0 1196 }
nexpaq 0:6c56fb4bc5f0 1197 }
nexpaq 0:6c56fb4bc5f0 1198 return res;
nexpaq 0:6c56fb4bc5f0 1199 }
nexpaq 0:6c56fb4bc5f0 1200
nexpaq 0:6c56fb4bc5f0 1201 bool M2MNsdlInterface::object_present(M2MObject* object) const
nexpaq 0:6c56fb4bc5f0 1202 {
nexpaq 0:6c56fb4bc5f0 1203 bool success = false;
nexpaq 0:6c56fb4bc5f0 1204 if(object && !_object_list.empty()) {
nexpaq 0:6c56fb4bc5f0 1205 M2MObjectList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1206 it = _object_list.begin();
nexpaq 0:6c56fb4bc5f0 1207 for ( ; it != _object_list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 1208 if((*it) == object) {
nexpaq 0:6c56fb4bc5f0 1209 success = true;
nexpaq 0:6c56fb4bc5f0 1210 break;
nexpaq 0:6c56fb4bc5f0 1211 }
nexpaq 0:6c56fb4bc5f0 1212 }
nexpaq 0:6c56fb4bc5f0 1213 }
nexpaq 0:6c56fb4bc5f0 1214 return success;
nexpaq 0:6c56fb4bc5f0 1215 }
nexpaq 0:6c56fb4bc5f0 1216
nexpaq 0:6c56fb4bc5f0 1217 bool M2MNsdlInterface::add_object_to_list(M2MObject* object)
nexpaq 0:6c56fb4bc5f0 1218 {
nexpaq 0:6c56fb4bc5f0 1219 bool success = false;
nexpaq 0:6c56fb4bc5f0 1220 if(object && !object_present(object)) {
nexpaq 0:6c56fb4bc5f0 1221 _object_list.push_back(object);
nexpaq 0:6c56fb4bc5f0 1222 success = true;
nexpaq 0:6c56fb4bc5f0 1223 }
nexpaq 0:6c56fb4bc5f0 1224 return success;
nexpaq 0:6c56fb4bc5f0 1225 }
nexpaq 0:6c56fb4bc5f0 1226
nexpaq 0:6c56fb4bc5f0 1227 void M2MNsdlInterface::clear_resource(sn_nsdl_resource_info_s *&resource)
nexpaq 0:6c56fb4bc5f0 1228 {
nexpaq 0:6c56fb4bc5f0 1229 //Clear up the filled resource to fill up new resource.
nexpaq 0:6c56fb4bc5f0 1230 if(resource && resource->resource_parameters_ptr) {
nexpaq 0:6c56fb4bc5f0 1231 sn_nsdl_resource_parameters_s *temp_resource_parameter = resource->resource_parameters_ptr;
nexpaq 0:6c56fb4bc5f0 1232 memset(resource->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
nexpaq 0:6c56fb4bc5f0 1233 memset(resource,0, sizeof(sn_nsdl_resource_info_s));
nexpaq 0:6c56fb4bc5f0 1234 resource->resource_parameters_ptr = temp_resource_parameter;
nexpaq 0:6c56fb4bc5f0 1235 }
nexpaq 0:6c56fb4bc5f0 1236 }
nexpaq 0:6c56fb4bc5f0 1237
nexpaq 0:6c56fb4bc5f0 1238 M2MInterface::Error M2MNsdlInterface::interface_error(sn_coap_hdr_s *coap_header)
nexpaq 0:6c56fb4bc5f0 1239 {
nexpaq 0:6c56fb4bc5f0 1240 M2MInterface::Error error = M2MInterface::ErrorNone;
nexpaq 0:6c56fb4bc5f0 1241 if(coap_header) {
nexpaq 0:6c56fb4bc5f0 1242 switch(coap_header->msg_code) {
nexpaq 0:6c56fb4bc5f0 1243 case COAP_MSG_CODE_RESPONSE_BAD_REQUEST:
nexpaq 0:6c56fb4bc5f0 1244 case COAP_MSG_CODE_RESPONSE_BAD_OPTION:
nexpaq 0:6c56fb4bc5f0 1245 case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE:
nexpaq 0:6c56fb4bc5f0 1246 case COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED:
nexpaq 0:6c56fb4bc5f0 1247 case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE:
nexpaq 0:6c56fb4bc5f0 1248 case COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT:
nexpaq 0:6c56fb4bc5f0 1249 error = M2MInterface::InvalidParameters;
nexpaq 0:6c56fb4bc5f0 1250 break;
nexpaq 0:6c56fb4bc5f0 1251 case COAP_MSG_CODE_RESPONSE_UNAUTHORIZED:
nexpaq 0:6c56fb4bc5f0 1252 case COAP_MSG_CODE_RESPONSE_FORBIDDEN:
nexpaq 0:6c56fb4bc5f0 1253 case COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE:
nexpaq 0:6c56fb4bc5f0 1254 case COAP_MSG_CODE_RESPONSE_NOT_FOUND:
nexpaq 0:6c56fb4bc5f0 1255 case COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED:
nexpaq 0:6c56fb4bc5f0 1256 error = M2MInterface::NotAllowed;
nexpaq 0:6c56fb4bc5f0 1257 break;
nexpaq 0:6c56fb4bc5f0 1258 case COAP_MSG_CODE_RESPONSE_CREATED:
nexpaq 0:6c56fb4bc5f0 1259 case COAP_MSG_CODE_RESPONSE_DELETED:
nexpaq 0:6c56fb4bc5f0 1260 case COAP_MSG_CODE_RESPONSE_VALID:
nexpaq 0:6c56fb4bc5f0 1261 case COAP_MSG_CODE_RESPONSE_CHANGED:
nexpaq 0:6c56fb4bc5f0 1262 case COAP_MSG_CODE_RESPONSE_CONTENT:
nexpaq 0:6c56fb4bc5f0 1263 error = M2MInterface::ErrorNone;
nexpaq 0:6c56fb4bc5f0 1264 break;
nexpaq 0:6c56fb4bc5f0 1265 default:
nexpaq 0:6c56fb4bc5f0 1266 error = M2MInterface::UnknownError;
nexpaq 0:6c56fb4bc5f0 1267 break;
nexpaq 0:6c56fb4bc5f0 1268 }
nexpaq 0:6c56fb4bc5f0 1269 if(coap_header->coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED) {
nexpaq 0:6c56fb4bc5f0 1270 error = M2MInterface::NetworkError;
nexpaq 0:6c56fb4bc5f0 1271 }
nexpaq 0:6c56fb4bc5f0 1272 }
nexpaq 0:6c56fb4bc5f0 1273 return error;
nexpaq 0:6c56fb4bc5f0 1274 }
nexpaq 0:6c56fb4bc5f0 1275
nexpaq 0:6c56fb4bc5f0 1276 void M2MNsdlInterface::send_object_observation(M2MObject *object,
nexpaq 0:6c56fb4bc5f0 1277 uint16_t obs_number,
nexpaq 0:6c56fb4bc5f0 1278 m2m::Vector<uint16_t> changed_instance_ids,
nexpaq 0:6c56fb4bc5f0 1279 bool send_object)
nexpaq 0:6c56fb4bc5f0 1280 {
nexpaq 0:6c56fb4bc5f0 1281 tr_debug("M2MNsdlInterface::send_object_observation");
nexpaq 0:6c56fb4bc5f0 1282 if(object) {
nexpaq 0:6c56fb4bc5f0 1283 uint8_t *value = 0;
nexpaq 0:6c56fb4bc5f0 1284 uint32_t length = 0;
nexpaq 0:6c56fb4bc5f0 1285 uint8_t *token = 0;
nexpaq 0:6c56fb4bc5f0 1286 uint32_t token_length = 0;
nexpaq 0:6c56fb4bc5f0 1287
nexpaq 0:6c56fb4bc5f0 1288 M2MTLVSerializer serializer;
nexpaq 0:6c56fb4bc5f0 1289 // Send whole object structure
nexpaq 0:6c56fb4bc5f0 1290 if (send_object) {
nexpaq 0:6c56fb4bc5f0 1291 value = serializer.serialize(object->instances(), length);
nexpaq 0:6c56fb4bc5f0 1292 }
nexpaq 0:6c56fb4bc5f0 1293 // Send only changed object instances
nexpaq 0:6c56fb4bc5f0 1294 else {
nexpaq 0:6c56fb4bc5f0 1295 M2MObjectInstanceList list;
nexpaq 0:6c56fb4bc5f0 1296 Vector<uint16_t>::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1297 it = changed_instance_ids.begin();
nexpaq 0:6c56fb4bc5f0 1298 for (; it != changed_instance_ids.end(); it++){
nexpaq 0:6c56fb4bc5f0 1299 M2MObjectInstance* obj_instance = object->object_instance(*it);
nexpaq 0:6c56fb4bc5f0 1300 if (obj_instance){
nexpaq 0:6c56fb4bc5f0 1301 list.push_back(obj_instance);
nexpaq 0:6c56fb4bc5f0 1302 }
nexpaq 0:6c56fb4bc5f0 1303 }
nexpaq 0:6c56fb4bc5f0 1304 if (!list.empty()) {
nexpaq 0:6c56fb4bc5f0 1305 value = serializer.serialize(list, length);
nexpaq 0:6c56fb4bc5f0 1306 list.clear();
nexpaq 0:6c56fb4bc5f0 1307 }
nexpaq 0:6c56fb4bc5f0 1308 }
nexpaq 0:6c56fb4bc5f0 1309
nexpaq 0:6c56fb4bc5f0 1310 object->get_observation_token(token,token_length);
nexpaq 0:6c56fb4bc5f0 1311
nexpaq 0:6c56fb4bc5f0 1312 send_notification(token,
nexpaq 0:6c56fb4bc5f0 1313 token_length,
nexpaq 0:6c56fb4bc5f0 1314 value,
nexpaq 0:6c56fb4bc5f0 1315 length,
nexpaq 0:6c56fb4bc5f0 1316 obs_number,
nexpaq 0:6c56fb4bc5f0 1317 object->max_age(),
nexpaq 0:6c56fb4bc5f0 1318 object->coap_content_type(),
nexpaq 0:6c56fb4bc5f0 1319 object->uri_path());
nexpaq 0:6c56fb4bc5f0 1320
nexpaq 0:6c56fb4bc5f0 1321 memory_free(value);
nexpaq 0:6c56fb4bc5f0 1322 memory_free(token);
nexpaq 0:6c56fb4bc5f0 1323 }
nexpaq 0:6c56fb4bc5f0 1324 }
nexpaq 0:6c56fb4bc5f0 1325
nexpaq 0:6c56fb4bc5f0 1326 void M2MNsdlInterface::send_object_instance_observation(M2MObjectInstance *object_instance,
nexpaq 0:6c56fb4bc5f0 1327 uint16_t obs_number)
nexpaq 0:6c56fb4bc5f0 1328 {
nexpaq 0:6c56fb4bc5f0 1329 tr_debug("M2MNsdlInterface::send_object_instance_observation");
nexpaq 0:6c56fb4bc5f0 1330 if(object_instance) {
nexpaq 0:6c56fb4bc5f0 1331 uint8_t *value = 0;
nexpaq 0:6c56fb4bc5f0 1332 uint32_t length = 0;
nexpaq 0:6c56fb4bc5f0 1333 uint8_t *token = 0;
nexpaq 0:6c56fb4bc5f0 1334 uint32_t token_length = 0;
nexpaq 0:6c56fb4bc5f0 1335
nexpaq 0:6c56fb4bc5f0 1336 M2MTLVSerializer serializer;
nexpaq 0:6c56fb4bc5f0 1337 value = serializer.serialize(object_instance->resources(), length);
nexpaq 0:6c56fb4bc5f0 1338
nexpaq 0:6c56fb4bc5f0 1339 object_instance->get_observation_token(token,token_length);
nexpaq 0:6c56fb4bc5f0 1340
nexpaq 0:6c56fb4bc5f0 1341 send_notification(token,
nexpaq 0:6c56fb4bc5f0 1342 token_length,
nexpaq 0:6c56fb4bc5f0 1343 value,
nexpaq 0:6c56fb4bc5f0 1344 length,
nexpaq 0:6c56fb4bc5f0 1345 obs_number,
nexpaq 0:6c56fb4bc5f0 1346 object_instance->max_age(),
nexpaq 0:6c56fb4bc5f0 1347 object_instance->coap_content_type(),
nexpaq 0:6c56fb4bc5f0 1348 object_instance->uri_path());
nexpaq 0:6c56fb4bc5f0 1349
nexpaq 0:6c56fb4bc5f0 1350 memory_free(value);
nexpaq 0:6c56fb4bc5f0 1351 memory_free(token);
nexpaq 0:6c56fb4bc5f0 1352 }
nexpaq 0:6c56fb4bc5f0 1353 }
nexpaq 0:6c56fb4bc5f0 1354
nexpaq 0:6c56fb4bc5f0 1355 void M2MNsdlInterface::send_resource_observation(M2MResource *resource,
nexpaq 0:6c56fb4bc5f0 1356 uint16_t obs_number)
nexpaq 0:6c56fb4bc5f0 1357 {
nexpaq 0:6c56fb4bc5f0 1358 tr_debug("M2MNsdlInterface::send_resource_observation");
nexpaq 0:6c56fb4bc5f0 1359 if(resource) {
nexpaq 0:6c56fb4bc5f0 1360 uint8_t *value = 0;
nexpaq 0:6c56fb4bc5f0 1361 uint32_t length = 0;
nexpaq 0:6c56fb4bc5f0 1362 uint8_t *token = 0;
nexpaq 0:6c56fb4bc5f0 1363 uint32_t token_length = 0;
nexpaq 0:6c56fb4bc5f0 1364
nexpaq 0:6c56fb4bc5f0 1365 resource->get_observation_token(token,token_length);
nexpaq 0:6c56fb4bc5f0 1366 uint8_t content_type = 0;
nexpaq 0:6c56fb4bc5f0 1367 if(M2MResourceInstance::OPAQUE == resource->resource_instance_type()) {
nexpaq 0:6c56fb4bc5f0 1368 content_type = COAP_CONTENT_OMA_OPAQUE_TYPE;
nexpaq 0:6c56fb4bc5f0 1369 }
nexpaq 0:6c56fb4bc5f0 1370 if (resource->resource_instance_count() > 0) {
nexpaq 0:6c56fb4bc5f0 1371 content_type = COAP_CONTENT_OMA_TLV_TYPE;
nexpaq 0:6c56fb4bc5f0 1372 M2MTLVSerializer serializer;
nexpaq 0:6c56fb4bc5f0 1373 value = serializer.serialize(resource, length);
nexpaq 0:6c56fb4bc5f0 1374 } else {
nexpaq 0:6c56fb4bc5f0 1375 resource->get_value(value,length);
nexpaq 0:6c56fb4bc5f0 1376 }
nexpaq 0:6c56fb4bc5f0 1377 send_notification(token,
nexpaq 0:6c56fb4bc5f0 1378 token_length,
nexpaq 0:6c56fb4bc5f0 1379 value,
nexpaq 0:6c56fb4bc5f0 1380 length,
nexpaq 0:6c56fb4bc5f0 1381 obs_number,
nexpaq 0:6c56fb4bc5f0 1382 resource->max_age(),
nexpaq 0:6c56fb4bc5f0 1383 content_type,
nexpaq 0:6c56fb4bc5f0 1384 resource->uri_path());
nexpaq 0:6c56fb4bc5f0 1385
nexpaq 0:6c56fb4bc5f0 1386 memory_free(value);
nexpaq 0:6c56fb4bc5f0 1387 memory_free(token);
nexpaq 0:6c56fb4bc5f0 1388 }
nexpaq 0:6c56fb4bc5f0 1389 }
nexpaq 0:6c56fb4bc5f0 1390
nexpaq 0:6c56fb4bc5f0 1391 void M2MNsdlInterface::build_observation_number(uint8_t *obs_number,
nexpaq 0:6c56fb4bc5f0 1392 uint8_t *obs_len,
nexpaq 0:6c56fb4bc5f0 1393 uint16_t number)
nexpaq 0:6c56fb4bc5f0 1394 {
nexpaq 0:6c56fb4bc5f0 1395 if(number > 0xFF) {
nexpaq 0:6c56fb4bc5f0 1396 *obs_len = 2;
nexpaq 0:6c56fb4bc5f0 1397 *(obs_number) = (number >> 8) & 0x00FF;
nexpaq 0:6c56fb4bc5f0 1398 obs_number[1] = number & 0x00FF;
nexpaq 0:6c56fb4bc5f0 1399 } else {
nexpaq 0:6c56fb4bc5f0 1400 *obs_len = 1;
nexpaq 0:6c56fb4bc5f0 1401 *(obs_number) = number & 0x00FF;
nexpaq 0:6c56fb4bc5f0 1402 }
nexpaq 0:6c56fb4bc5f0 1403 }
nexpaq 0:6c56fb4bc5f0 1404
nexpaq 0:6c56fb4bc5f0 1405 void M2MNsdlInterface::send_notification(uint8_t *token,
nexpaq 0:6c56fb4bc5f0 1406 uint8_t token_length,
nexpaq 0:6c56fb4bc5f0 1407 uint8_t *value,
nexpaq 0:6c56fb4bc5f0 1408 uint32_t value_length,
nexpaq 0:6c56fb4bc5f0 1409 uint16_t observation,
nexpaq 0:6c56fb4bc5f0 1410 uint32_t max_age,
nexpaq 0:6c56fb4bc5f0 1411 uint8_t coap_content_type,
nexpaq 0:6c56fb4bc5f0 1412 const String &uri_path)
nexpaq 0:6c56fb4bc5f0 1413
nexpaq 0:6c56fb4bc5f0 1414 {
nexpaq 0:6c56fb4bc5f0 1415 tr_debug("M2MNsdlInterface::send_notification");
nexpaq 0:6c56fb4bc5f0 1416 sn_coap_hdr_s *notification_message_ptr;
nexpaq 0:6c56fb4bc5f0 1417
nexpaq 0:6c56fb4bc5f0 1418 /* Allocate and initialize memory for header struct */
nexpaq 0:6c56fb4bc5f0 1419 notification_message_ptr = static_cast<sn_coap_hdr_s *>(memory_alloc(sizeof(sn_coap_hdr_s)));
nexpaq 0:6c56fb4bc5f0 1420 if (notification_message_ptr) {
nexpaq 0:6c56fb4bc5f0 1421 memset(notification_message_ptr, 0, sizeof(sn_coap_hdr_s));
nexpaq 0:6c56fb4bc5f0 1422
nexpaq 0:6c56fb4bc5f0 1423 notification_message_ptr->options_list_ptr = (sn_coap_options_list_s *)memory_alloc(sizeof(sn_coap_options_list_s));
nexpaq 0:6c56fb4bc5f0 1424 if (notification_message_ptr->options_list_ptr) {
nexpaq 0:6c56fb4bc5f0 1425
nexpaq 0:6c56fb4bc5f0 1426 memset(notification_message_ptr->options_list_ptr , 0, sizeof(sn_coap_options_list_s));
nexpaq 0:6c56fb4bc5f0 1427
nexpaq 0:6c56fb4bc5f0 1428 /* Fill header */
nexpaq 0:6c56fb4bc5f0 1429 notification_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
nexpaq 0:6c56fb4bc5f0 1430 notification_message_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
nexpaq 0:6c56fb4bc5f0 1431
nexpaq 0:6c56fb4bc5f0 1432 /* Fill token */
nexpaq 0:6c56fb4bc5f0 1433 notification_message_ptr->token_len = token_length;
nexpaq 0:6c56fb4bc5f0 1434 notification_message_ptr->token_ptr = token;
nexpaq 0:6c56fb4bc5f0 1435
nexpaq 0:6c56fb4bc5f0 1436 /* Fill payload */
nexpaq 0:6c56fb4bc5f0 1437 notification_message_ptr->payload_len = value_length;
nexpaq 0:6c56fb4bc5f0 1438 notification_message_ptr->payload_ptr = value;
nexpaq 0:6c56fb4bc5f0 1439
nexpaq 0:6c56fb4bc5f0 1440 /* Fill uri path */
nexpaq 0:6c56fb4bc5f0 1441 notification_message_ptr->uri_path_len = uri_path.size();
nexpaq 0:6c56fb4bc5f0 1442 notification_message_ptr->uri_path_ptr = (uint8_t *)uri_path.c_str();
nexpaq 0:6c56fb4bc5f0 1443
nexpaq 0:6c56fb4bc5f0 1444 /* Fill observe */
nexpaq 0:6c56fb4bc5f0 1445 uint8_t observation_number[2];
nexpaq 0:6c56fb4bc5f0 1446 uint8_t observation_number_length = 0;
nexpaq 0:6c56fb4bc5f0 1447
nexpaq 0:6c56fb4bc5f0 1448 build_observation_number(observation_number,
nexpaq 0:6c56fb4bc5f0 1449 &observation_number_length,
nexpaq 0:6c56fb4bc5f0 1450 observation);
nexpaq 0:6c56fb4bc5f0 1451 notification_message_ptr->options_list_ptr->observe_len = observation_number_length;
nexpaq 0:6c56fb4bc5f0 1452 notification_message_ptr->options_list_ptr->observe_ptr = observation_number;
nexpaq 0:6c56fb4bc5f0 1453
nexpaq 0:6c56fb4bc5f0 1454 notification_message_ptr->options_list_ptr->max_age_ptr =
nexpaq 0:6c56fb4bc5f0 1455 m2m::String::convert_integer_to_array(max_age,
nexpaq 0:6c56fb4bc5f0 1456 notification_message_ptr->options_list_ptr->max_age_len);
nexpaq 0:6c56fb4bc5f0 1457
nexpaq 0:6c56fb4bc5f0 1458 notification_message_ptr->content_type_ptr =
nexpaq 0:6c56fb4bc5f0 1459 m2m::String::convert_integer_to_array(coap_content_type,
nexpaq 0:6c56fb4bc5f0 1460 notification_message_ptr->content_type_len);
nexpaq 0:6c56fb4bc5f0 1461
nexpaq 0:6c56fb4bc5f0 1462 /* Send message */
nexpaq 0:6c56fb4bc5f0 1463 sn_nsdl_send_coap_message(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 1464 _nsdl_handle->nsp_address_ptr->omalw_address_ptr,
nexpaq 0:6c56fb4bc5f0 1465 notification_message_ptr);
nexpaq 0:6c56fb4bc5f0 1466
nexpaq 0:6c56fb4bc5f0 1467 /* Free memory */
nexpaq 0:6c56fb4bc5f0 1468 notification_message_ptr->uri_path_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 1469 notification_message_ptr->payload_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 1470 notification_message_ptr->options_list_ptr->observe_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 1471 notification_message_ptr->token_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 1472 if (notification_message_ptr->content_type_ptr) {
nexpaq 0:6c56fb4bc5f0 1473 free(notification_message_ptr->content_type_ptr);
nexpaq 0:6c56fb4bc5f0 1474 }
nexpaq 0:6c56fb4bc5f0 1475 notification_message_ptr->content_type_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 1476 if (notification_message_ptr->options_list_ptr->max_age_ptr) {
nexpaq 0:6c56fb4bc5f0 1477 free(notification_message_ptr->options_list_ptr->max_age_ptr);
nexpaq 0:6c56fb4bc5f0 1478 }
nexpaq 0:6c56fb4bc5f0 1479 notification_message_ptr->options_list_ptr->max_age_ptr = NULL;
nexpaq 0:6c56fb4bc5f0 1480 }
nexpaq 0:6c56fb4bc5f0 1481 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, notification_message_ptr);
nexpaq 0:6c56fb4bc5f0 1482 }
nexpaq 0:6c56fb4bc5f0 1483 }
nexpaq 0:6c56fb4bc5f0 1484
nexpaq 0:6c56fb4bc5f0 1485 nsdl_s * M2MNsdlInterface::get_nsdl_handle()
nexpaq 0:6c56fb4bc5f0 1486 {
nexpaq 0:6c56fb4bc5f0 1487 return _nsdl_handle;
nexpaq 0:6c56fb4bc5f0 1488 }
nexpaq 0:6c56fb4bc5f0 1489
nexpaq 0:6c56fb4bc5f0 1490 void M2MNsdlInterface::handle_bootstrap_put_message(sn_coap_hdr_s *coap_header,
nexpaq 0:6c56fb4bc5f0 1491 sn_nsdl_addr_s *address) {
nexpaq 0:6c56fb4bc5f0 1492 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 1493 tr_debug("M2MNsdlInterface::handle_bootstrap_message");
nexpaq 0:6c56fb4bc5f0 1494 uint8_t response_code = COAP_MSG_CODE_RESPONSE_CHANGED;
nexpaq 0:6c56fb4bc5f0 1495 sn_coap_hdr_s *coap_response = NULL;
nexpaq 0:6c56fb4bc5f0 1496 bool success = false;
nexpaq 0:6c56fb4bc5f0 1497 bool security_object = false;
nexpaq 0:6c56fb4bc5f0 1498 uint16_t content_type = 0;
nexpaq 0:6c56fb4bc5f0 1499
nexpaq 0:6c56fb4bc5f0 1500 if (!_security) {
nexpaq 0:6c56fb4bc5f0 1501 _security = new M2MSecurity(M2MSecurity::M2MServer);
nexpaq 0:6c56fb4bc5f0 1502 }
nexpaq 0:6c56fb4bc5f0 1503
nexpaq 0:6c56fb4bc5f0 1504 String resource_name = coap_to_string(coap_header->uri_path_ptr,
nexpaq 0:6c56fb4bc5f0 1505 coap_header->uri_path_len);
nexpaq 0:6c56fb4bc5f0 1506 tr_debug("M2MNsdlInterface::handle_bootstrap_message - uri %s", resource_name.c_str());
nexpaq 0:6c56fb4bc5f0 1507
nexpaq 0:6c56fb4bc5f0 1508 // Check incoming object
nexpaq 0:6c56fb4bc5f0 1509 if (resource_name.compare(0,1,"0") == 0) {
nexpaq 0:6c56fb4bc5f0 1510 security_object = true;
nexpaq 0:6c56fb4bc5f0 1511 if(_security) {
nexpaq 0:6c56fb4bc5f0 1512 success = true;
nexpaq 0:6c56fb4bc5f0 1513 // Not mandatory resource that's why it must be created first
nexpaq 0:6c56fb4bc5f0 1514 _security->create_resource(M2MSecurity::ShortServerID, 1);
nexpaq 0:6c56fb4bc5f0 1515 // Change operation mode
nexpaq 0:6c56fb4bc5f0 1516 M2MResourceList list = _security->object_instance()->resources();
nexpaq 0:6c56fb4bc5f0 1517 if(!list.empty()) {
nexpaq 0:6c56fb4bc5f0 1518 M2MResourceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1519 it = list.begin();
nexpaq 0:6c56fb4bc5f0 1520 for ( ; it != list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 1521 (*it)->set_operation(M2MBase::PUT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 1522 }
nexpaq 0:6c56fb4bc5f0 1523 }
nexpaq 0:6c56fb4bc5f0 1524 }
nexpaq 0:6c56fb4bc5f0 1525 }
nexpaq 0:6c56fb4bc5f0 1526 else if (resource_name.compare(0,1,"1") == 0) {
nexpaq 0:6c56fb4bc5f0 1527 success = true;
nexpaq 0:6c56fb4bc5f0 1528 }
nexpaq 0:6c56fb4bc5f0 1529
nexpaq 0:6c56fb4bc5f0 1530 if (success) {
nexpaq 0:6c56fb4bc5f0 1531 // Send delayed response if token is part of the message
nexpaq 0:6c56fb4bc5f0 1532 if (coap_header->token_ptr) {
nexpaq 0:6c56fb4bc5f0 1533 tr_debug("M2MNsdlInterface::handle_bootstrap_message - send delayed response");
nexpaq 0:6c56fb4bc5f0 1534 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 1535 coap_header,
nexpaq 0:6c56fb4bc5f0 1536 COAP_MSG_CODE_EMPTY);
nexpaq 0:6c56fb4bc5f0 1537 if (coap_response) {
nexpaq 0:6c56fb4bc5f0 1538 coap_response->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
nexpaq 0:6c56fb4bc5f0 1539 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
nexpaq 0:6c56fb4bc5f0 1540 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
nexpaq 0:6c56fb4bc5f0 1541 }
nexpaq 0:6c56fb4bc5f0 1542 }
nexpaq 0:6c56fb4bc5f0 1543
nexpaq 0:6c56fb4bc5f0 1544 if(coap_header->content_type_ptr) {
nexpaq 0:6c56fb4bc5f0 1545 content_type = String::convert_array_to_integer(coap_header->content_type_ptr,
nexpaq 0:6c56fb4bc5f0 1546 coap_header->content_type_len);
nexpaq 0:6c56fb4bc5f0 1547 }
nexpaq 0:6c56fb4bc5f0 1548
nexpaq 0:6c56fb4bc5f0 1549 tr_debug("M2MNsdlInterface::handle_bootstrap_message - content_type %d", content_type);
nexpaq 0:6c56fb4bc5f0 1550 if (content_type != COAP_CONTENT_OMA_TLV_TYPE) {
nexpaq 0:6c56fb4bc5f0 1551 success = false;
nexpaq 0:6c56fb4bc5f0 1552 }
nexpaq 0:6c56fb4bc5f0 1553 if (success) {
nexpaq 0:6c56fb4bc5f0 1554 success = parse_bootstrap_message(coap_header, security_object);
nexpaq 0:6c56fb4bc5f0 1555 // Set operation back to default ones
nexpaq 0:6c56fb4bc5f0 1556 if (_security) {
nexpaq 0:6c56fb4bc5f0 1557 M2MResourceList list = _security->object_instance()->resources();
nexpaq 0:6c56fb4bc5f0 1558 if(!list.empty()) {
nexpaq 0:6c56fb4bc5f0 1559 M2MResourceList::const_iterator it;
nexpaq 0:6c56fb4bc5f0 1560 it = list.begin();
nexpaq 0:6c56fb4bc5f0 1561 for ( ; it != list.end(); it++ ) {
nexpaq 0:6c56fb4bc5f0 1562 (*it)->set_operation(M2MBase::NOT_ALLOWED);
nexpaq 0:6c56fb4bc5f0 1563 }
nexpaq 0:6c56fb4bc5f0 1564 }
nexpaq 0:6c56fb4bc5f0 1565 }
nexpaq 0:6c56fb4bc5f0 1566 }
nexpaq 0:6c56fb4bc5f0 1567 }
nexpaq 0:6c56fb4bc5f0 1568
nexpaq 0:6c56fb4bc5f0 1569 if (!success) {
nexpaq 0:6c56fb4bc5f0 1570 response_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
nexpaq 0:6c56fb4bc5f0 1571 handle_bootstrap_error();
nexpaq 0:6c56fb4bc5f0 1572 }
nexpaq 0:6c56fb4bc5f0 1573 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 1574 coap_header,
nexpaq 0:6c56fb4bc5f0 1575 response_code);
nexpaq 0:6c56fb4bc5f0 1576 if (coap_response) {
nexpaq 0:6c56fb4bc5f0 1577 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
nexpaq 0:6c56fb4bc5f0 1578 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
nexpaq 0:6c56fb4bc5f0 1579 }
nexpaq 0:6c56fb4bc5f0 1580 #else
nexpaq 0:6c56fb4bc5f0 1581 (void) coap_header;
nexpaq 0:6c56fb4bc5f0 1582 (void) address;
nexpaq 0:6c56fb4bc5f0 1583 #endif
nexpaq 0:6c56fb4bc5f0 1584 }
nexpaq 0:6c56fb4bc5f0 1585
nexpaq 0:6c56fb4bc5f0 1586 bool M2MNsdlInterface::parse_bootstrap_message(sn_coap_hdr_s *coap_header, bool is_security_object)
nexpaq 0:6c56fb4bc5f0 1587 {
nexpaq 0:6c56fb4bc5f0 1588 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 1589 tr_debug("M2MNsdlInterface::parse_bootstrap_put_message");
nexpaq 0:6c56fb4bc5f0 1590 M2MTLVDeserializer *deserializer = new M2MTLVDeserializer();
nexpaq 0:6c56fb4bc5f0 1591 bool ret = false;
nexpaq 0:6c56fb4bc5f0 1592 bool is_obj_instance = false;
nexpaq 0:6c56fb4bc5f0 1593 uint16_t instance_id = 0;
nexpaq 0:6c56fb4bc5f0 1594 if (deserializer && _security) {
nexpaq 0:6c56fb4bc5f0 1595 ret = is_obj_instance = deserializer->is_object_instance(coap_header->payload_ptr);
nexpaq 0:6c56fb4bc5f0 1596 if (!is_obj_instance) {
nexpaq 0:6c56fb4bc5f0 1597 ret = deserializer->is_resource(coap_header->payload_ptr);
nexpaq 0:6c56fb4bc5f0 1598 }
nexpaq 0:6c56fb4bc5f0 1599
nexpaq 0:6c56fb4bc5f0 1600 if (ret) {
nexpaq 0:6c56fb4bc5f0 1601 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
nexpaq 0:6c56fb4bc5f0 1602 if (is_obj_instance) {
nexpaq 0:6c56fb4bc5f0 1603 if (is_security_object) {
nexpaq 0:6c56fb4bc5f0 1604 error = deserializer->deserialise_object_instances(coap_header->payload_ptr,
nexpaq 0:6c56fb4bc5f0 1605 coap_header->payload_len,
nexpaq 0:6c56fb4bc5f0 1606 *_security,
nexpaq 0:6c56fb4bc5f0 1607 M2MTLVDeserializer::Put);
nexpaq 0:6c56fb4bc5f0 1608 }
nexpaq 0:6c56fb4bc5f0 1609 else {
nexpaq 0:6c56fb4bc5f0 1610 error = deserializer->deserialise_object_instances(coap_header->payload_ptr,
nexpaq 0:6c56fb4bc5f0 1611 coap_header->payload_len,
nexpaq 0:6c56fb4bc5f0 1612 *_server,
nexpaq 0:6c56fb4bc5f0 1613 M2MTLVDeserializer::Put);
nexpaq 0:6c56fb4bc5f0 1614 }
nexpaq 0:6c56fb4bc5f0 1615 }
nexpaq 0:6c56fb4bc5f0 1616 else {
nexpaq 0:6c56fb4bc5f0 1617 if (is_security_object) {
nexpaq 0:6c56fb4bc5f0 1618 instance_id = deserializer->instance_id(coap_header->payload_ptr);
nexpaq 0:6c56fb4bc5f0 1619 error = deserializer->deserialize_resources(coap_header->payload_ptr,
nexpaq 0:6c56fb4bc5f0 1620 coap_header->payload_len,
nexpaq 0:6c56fb4bc5f0 1621 *_security->object_instance(instance_id),
nexpaq 0:6c56fb4bc5f0 1622 M2MTLVDeserializer::Put);
nexpaq 0:6c56fb4bc5f0 1623 }
nexpaq 0:6c56fb4bc5f0 1624 else {
nexpaq 0:6c56fb4bc5f0 1625 instance_id = deserializer->instance_id(coap_header->payload_ptr);
nexpaq 0:6c56fb4bc5f0 1626 error = deserializer->deserialize_resources(coap_header->payload_ptr,
nexpaq 0:6c56fb4bc5f0 1627 coap_header->payload_len,
nexpaq 0:6c56fb4bc5f0 1628 *_server->object_instance(instance_id),
nexpaq 0:6c56fb4bc5f0 1629 M2MTLVDeserializer::Post);
nexpaq 0:6c56fb4bc5f0 1630 }
nexpaq 0:6c56fb4bc5f0 1631 }
nexpaq 0:6c56fb4bc5f0 1632
nexpaq 0:6c56fb4bc5f0 1633 if (error != M2MTLVDeserializer::None) {
nexpaq 0:6c56fb4bc5f0 1634 tr_error("M2MNsdlInterface::parse_bootstrap_put_message - error %d", error);
nexpaq 0:6c56fb4bc5f0 1635 ret = false;
nexpaq 0:6c56fb4bc5f0 1636 }
nexpaq 0:6c56fb4bc5f0 1637 }
nexpaq 0:6c56fb4bc5f0 1638 }
nexpaq 0:6c56fb4bc5f0 1639 delete deserializer;
nexpaq 0:6c56fb4bc5f0 1640 return ret;
nexpaq 0:6c56fb4bc5f0 1641 #else
nexpaq 0:6c56fb4bc5f0 1642 (void) coap_header;
nexpaq 0:6c56fb4bc5f0 1643 (void) is_security_object;
nexpaq 0:6c56fb4bc5f0 1644 return false;
nexpaq 0:6c56fb4bc5f0 1645 #endif
nexpaq 0:6c56fb4bc5f0 1646 }
nexpaq 0:6c56fb4bc5f0 1647
nexpaq 0:6c56fb4bc5f0 1648 void M2MNsdlInterface::handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address)
nexpaq 0:6c56fb4bc5f0 1649 {
nexpaq 0:6c56fb4bc5f0 1650 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 1651 String object_name = coap_to_string(coap_header->uri_path_ptr,
nexpaq 0:6c56fb4bc5f0 1652 coap_header->uri_path_len);
nexpaq 0:6c56fb4bc5f0 1653 tr_debug("M2MNsdlInterface::handle_bootstrap_finished - path: %s", object_name.c_str());
nexpaq 0:6c56fb4bc5f0 1654 sn_coap_hdr_s *coap_response = NULL;
nexpaq 0:6c56fb4bc5f0 1655 uint8_t msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
nexpaq 0:6c56fb4bc5f0 1656
nexpaq 0:6c56fb4bc5f0 1657 // Accept only '/bs' path and check that needed data is in security object
nexpaq 0:6c56fb4bc5f0 1658 if (object_name.size() != 2 ||
nexpaq 0:6c56fb4bc5f0 1659 object_name.compare(0,2,BOOTSTRAP_URI) != 0 ||
nexpaq 0:6c56fb4bc5f0 1660 !validate_security_object()) {
nexpaq 0:6c56fb4bc5f0 1661 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
nexpaq 0:6c56fb4bc5f0 1662 } else {
nexpaq 0:6c56fb4bc5f0 1663 // Add short server id to server object
nexpaq 0:6c56fb4bc5f0 1664 _server->set_resource_value(M2MServer::ShortServerID,
nexpaq 0:6c56fb4bc5f0 1665 _security->resource_value_int(M2MSecurity::ShortServerID));
nexpaq 0:6c56fb4bc5f0 1666 }
nexpaq 0:6c56fb4bc5f0 1667
nexpaq 0:6c56fb4bc5f0 1668 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 1669 coap_header,
nexpaq 0:6c56fb4bc5f0 1670 msg_code);
nexpaq 0:6c56fb4bc5f0 1671 if(coap_response) {
nexpaq 0:6c56fb4bc5f0 1672 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
nexpaq 0:6c56fb4bc5f0 1673 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
nexpaq 0:6c56fb4bc5f0 1674 }
nexpaq 0:6c56fb4bc5f0 1675 if (COAP_MSG_CODE_RESPONSE_CHANGED == msg_code) {
nexpaq 0:6c56fb4bc5f0 1676 // Switch back to original ep name
nexpaq 0:6c56fb4bc5f0 1677 if (_endpoint->endpoint_name_ptr) {
nexpaq 0:6c56fb4bc5f0 1678 memory_free(_endpoint->endpoint_name_ptr);
nexpaq 0:6c56fb4bc5f0 1679 }
nexpaq 0:6c56fb4bc5f0 1680 _endpoint->endpoint_name_ptr = alloc_string_copy((uint8_t*)_endpoint_name.c_str(), _endpoint_name.length());
nexpaq 0:6c56fb4bc5f0 1681 _endpoint->endpoint_name_len = _endpoint_name.length();
nexpaq 0:6c56fb4bc5f0 1682 _observer.bootstrap_done(_security);
nexpaq 0:6c56fb4bc5f0 1683 } else {
nexpaq 0:6c56fb4bc5f0 1684 handle_bootstrap_error();
nexpaq 0:6c56fb4bc5f0 1685 }
nexpaq 0:6c56fb4bc5f0 1686 #else
nexpaq 0:6c56fb4bc5f0 1687 (void) coap_header;
nexpaq 0:6c56fb4bc5f0 1688 (void) address;
nexpaq 0:6c56fb4bc5f0 1689 #endif
nexpaq 0:6c56fb4bc5f0 1690 }
nexpaq 0:6c56fb4bc5f0 1691
nexpaq 0:6c56fb4bc5f0 1692 void M2MNsdlInterface::handle_bootstrap_delete(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address)
nexpaq 0:6c56fb4bc5f0 1693 {
nexpaq 0:6c56fb4bc5f0 1694 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 1695 sn_coap_hdr_s *coap_response = NULL;
nexpaq 0:6c56fb4bc5f0 1696 uint8_t msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
nexpaq 0:6c56fb4bc5f0 1697 String object_name = coap_to_string(coap_header->uri_path_ptr,
nexpaq 0:6c56fb4bc5f0 1698 coap_header->uri_path_len);
nexpaq 0:6c56fb4bc5f0 1699 tr_debug("M2MNsdlInterface::handle_bootstrap_delete - obj %s", object_name.c_str());
nexpaq 0:6c56fb4bc5f0 1700
nexpaq 0:6c56fb4bc5f0 1701 // Only following paths are accepted, 0, 0/0
nexpaq 0:6c56fb4bc5f0 1702 if (object_name.size() == 2 || object_name.size() > 3) {
nexpaq 0:6c56fb4bc5f0 1703 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
nexpaq 0:6c56fb4bc5f0 1704 }
nexpaq 0:6c56fb4bc5f0 1705 else if ((object_name.size() == 1 && object_name.compare(0,1,"0") != 0) ||
nexpaq 0:6c56fb4bc5f0 1706 (object_name.size() == 3 && object_name.compare(0,3,"0/0") != 0)) {
nexpaq 0:6c56fb4bc5f0 1707 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
nexpaq 0:6c56fb4bc5f0 1708 }
nexpaq 0:6c56fb4bc5f0 1709
nexpaq 0:6c56fb4bc5f0 1710 coap_response = sn_nsdl_build_response(_nsdl_handle,
nexpaq 0:6c56fb4bc5f0 1711 coap_header,
nexpaq 0:6c56fb4bc5f0 1712 msg_code);
nexpaq 0:6c56fb4bc5f0 1713
nexpaq 0:6c56fb4bc5f0 1714 if(coap_response) {
nexpaq 0:6c56fb4bc5f0 1715 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
nexpaq 0:6c56fb4bc5f0 1716 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
nexpaq 0:6c56fb4bc5f0 1717 if(_security) {
nexpaq 0:6c56fb4bc5f0 1718 _security->clear_resources();
nexpaq 0:6c56fb4bc5f0 1719 }
nexpaq 0:6c56fb4bc5f0 1720 } else {
nexpaq 0:6c56fb4bc5f0 1721 handle_bootstrap_error();
nexpaq 0:6c56fb4bc5f0 1722 }
nexpaq 0:6c56fb4bc5f0 1723 #else
nexpaq 0:6c56fb4bc5f0 1724 (void) coap_header;
nexpaq 0:6c56fb4bc5f0 1725 (void) address;
nexpaq 0:6c56fb4bc5f0 1726 #endif
nexpaq 0:6c56fb4bc5f0 1727 }
nexpaq 0:6c56fb4bc5f0 1728
nexpaq 0:6c56fb4bc5f0 1729 bool M2MNsdlInterface::validate_security_object()
nexpaq 0:6c56fb4bc5f0 1730 {
nexpaq 0:6c56fb4bc5f0 1731 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
nexpaq 0:6c56fb4bc5f0 1732 tr_debug("M2MNsdlInterface::validate_security_object");
nexpaq 0:6c56fb4bc5f0 1733 if (_security) {
nexpaq 0:6c56fb4bc5f0 1734 String address = _security->resource_value_string(M2MSecurity::M2MServerUri);
nexpaq 0:6c56fb4bc5f0 1735 uint32_t sec_mode = _security->resource_value_int(M2MSecurity::SecurityMode);
nexpaq 0:6c56fb4bc5f0 1736 bool is_bs_server = _security->resource_value_int(M2MSecurity::BootstrapServer);
nexpaq 0:6c56fb4bc5f0 1737 uint32_t public_key_size = _security->get_resource(M2MSecurity::PublicKey)->value_length();
nexpaq 0:6c56fb4bc5f0 1738 uint32_t server_key_size = _security->get_resource(M2MSecurity::ServerPublicKey)->value_length();
nexpaq 0:6c56fb4bc5f0 1739 uint32_t pkey_size = _security->get_resource(M2MSecurity::Secretkey)->value_length();
nexpaq 0:6c56fb4bc5f0 1740 tr_debug("M2MNsdlInterface::validate_security_object - Server URI /0/0: %s", address.c_str());
nexpaq 0:6c56fb4bc5f0 1741 tr_debug("M2MNsdlInterface::validate_security_object - is bs server /0/1: %d", is_bs_server);
nexpaq 0:6c56fb4bc5f0 1742 tr_debug("M2MNsdlInterface::validate_security_object - Security Mode /0/2: %d", sec_mode);
nexpaq 0:6c56fb4bc5f0 1743 tr_debug("M2MNsdlInterface::validate_security_object - Public key size /0/3: %d", public_key_size);
nexpaq 0:6c56fb4bc5f0 1744 tr_debug("M2MNsdlInterface::validate_security_object - Server Public key size /0/4: %d", server_key_size);
nexpaq 0:6c56fb4bc5f0 1745 tr_debug("M2MNsdlInterface::validate_security_object - Secret key size /0/5: %d", pkey_size);
nexpaq 0:6c56fb4bc5f0 1746 // Only NoSec and Certificate modes are supported
nexpaq 0:6c56fb4bc5f0 1747 if (!address.empty() && !is_bs_server) {
nexpaq 0:6c56fb4bc5f0 1748 if (M2MSecurity::Certificate == sec_mode) {
nexpaq 0:6c56fb4bc5f0 1749 if (!public_key_size || !server_key_size || !pkey_size) {
nexpaq 0:6c56fb4bc5f0 1750 return false;
nexpaq 0:6c56fb4bc5f0 1751 } else {
nexpaq 0:6c56fb4bc5f0 1752 return true;
nexpaq 0:6c56fb4bc5f0 1753 }
nexpaq 0:6c56fb4bc5f0 1754 } else if (M2MSecurity::NoSecurity == sec_mode){
nexpaq 0:6c56fb4bc5f0 1755 return true;
nexpaq 0:6c56fb4bc5f0 1756 } else {
nexpaq 0:6c56fb4bc5f0 1757 return false;
nexpaq 0:6c56fb4bc5f0 1758 }
nexpaq 0:6c56fb4bc5f0 1759 } else {
nexpaq 0:6c56fb4bc5f0 1760 return false;
nexpaq 0:6c56fb4bc5f0 1761 }
nexpaq 0:6c56fb4bc5f0 1762 }
nexpaq 0:6c56fb4bc5f0 1763 return false;
nexpaq 0:6c56fb4bc5f0 1764 #else
nexpaq 0:6c56fb4bc5f0 1765 return false;
nexpaq 0:6c56fb4bc5f0 1766 #endif
nexpaq 0:6c56fb4bc5f0 1767 }
nexpaq 0:6c56fb4bc5f0 1768
nexpaq 0:6c56fb4bc5f0 1769 void M2MNsdlInterface::handle_bootstrap_error()
nexpaq 0:6c56fb4bc5f0 1770 {
nexpaq 0:6c56fb4bc5f0 1771 tr_debug("M2MNsdlInterface::handle_bootstrap_error()");
nexpaq 0:6c56fb4bc5f0 1772 if (_security) {
nexpaq 0:6c56fb4bc5f0 1773 delete _security;
nexpaq 0:6c56fb4bc5f0 1774 _security = NULL;
nexpaq 0:6c56fb4bc5f0 1775 }
nexpaq 0:6c56fb4bc5f0 1776 _observer.bootstrap_error();
nexpaq 0:6c56fb4bc5f0 1777 }
nexpaq 0:6c56fb4bc5f0 1778
nexpaq 0:6c56fb4bc5f0 1779 const String& M2MNsdlInterface::endpoint_name() const
nexpaq 0:6c56fb4bc5f0 1780 {
nexpaq 0:6c56fb4bc5f0 1781 return _endpoint_name;
nexpaq 0:6c56fb4bc5f0 1782 }