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