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