mbed client on ethernet with LWIP
Dependencies: mbed Socket lwip-eth lwip-sys lwip
Fork of mbed-client-classic-example-lwip by
mbed-client/source/m2mnsdlinterface.cpp@11:cada08fc8a70, 2016-06-09 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Jun 09 17:08:36 2016 +0000
- Revision:
- 11:cada08fc8a70
Commit for public Consumption
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbedAustin | 11:cada08fc8a70 | 1 | /* |
mbedAustin | 11:cada08fc8a70 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
mbedAustin | 11:cada08fc8a70 | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbedAustin | 11:cada08fc8a70 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
mbedAustin | 11:cada08fc8a70 | 5 | * not use this file except in compliance with the License. |
mbedAustin | 11:cada08fc8a70 | 6 | * You may obtain a copy of the License at |
mbedAustin | 11:cada08fc8a70 | 7 | * |
mbedAustin | 11:cada08fc8a70 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbedAustin | 11:cada08fc8a70 | 9 | * |
mbedAustin | 11:cada08fc8a70 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbedAustin | 11:cada08fc8a70 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
mbedAustin | 11:cada08fc8a70 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbedAustin | 11:cada08fc8a70 | 13 | * See the License for the specific language governing permissions and |
mbedAustin | 11:cada08fc8a70 | 14 | * limitations under the License. |
mbedAustin | 11:cada08fc8a70 | 15 | */ |
mbedAustin | 11:cada08fc8a70 | 16 | #include "include/nsdlaccesshelper.h" |
mbedAustin | 11:cada08fc8a70 | 17 | #include "include/m2mnsdlobserver.h" |
mbedAustin | 11:cada08fc8a70 | 18 | #include "mbed-client/m2msecurity.h" |
mbedAustin | 11:cada08fc8a70 | 19 | #include "mbed-client/m2mserver.h" |
mbedAustin | 11:cada08fc8a70 | 20 | #include "mbed-client/m2mobject.h" |
mbedAustin | 11:cada08fc8a70 | 21 | #include "mbed-client/m2mobjectinstance.h" |
mbedAustin | 11:cada08fc8a70 | 22 | #include "mbed-client/m2mresource.h" |
mbedAustin | 11:cada08fc8a70 | 23 | #include "mbed-client/m2mconstants.h" |
mbedAustin | 11:cada08fc8a70 | 24 | #include "include/m2mtlvserializer.h" |
mbedAustin | 11:cada08fc8a70 | 25 | #include "ip6string.h" |
mbedAustin | 11:cada08fc8a70 | 26 | #include "ns_trace.h" |
mbedAustin | 11:cada08fc8a70 | 27 | #include "mbed-client/m2mtimer.h" |
mbedAustin | 11:cada08fc8a70 | 28 | |
mbedAustin | 11:cada08fc8a70 | 29 | M2MNsdlInterface::M2MNsdlInterface(M2MNsdlObserver &observer) |
mbedAustin | 11:cada08fc8a70 | 30 | : _observer(observer), |
mbedAustin | 11:cada08fc8a70 | 31 | _server(NULL), |
mbedAustin | 11:cada08fc8a70 | 32 | _nsdl_exceution_timer(new M2MTimer(*this)), |
mbedAustin | 11:cada08fc8a70 | 33 | _registration_timer(new M2MTimer(*this)), |
mbedAustin | 11:cada08fc8a70 | 34 | _nsdl_handle(NULL), |
mbedAustin | 11:cada08fc8a70 | 35 | _counter_for_nsdl(0), |
mbedAustin | 11:cada08fc8a70 | 36 | _register_id(0), |
mbedAustin | 11:cada08fc8a70 | 37 | _unregister_id(0), |
mbedAustin | 11:cada08fc8a70 | 38 | _update_id(0), |
mbedAustin | 11:cada08fc8a70 | 39 | _bootstrap_id(0) |
mbedAustin | 11:cada08fc8a70 | 40 | { |
mbedAustin | 11:cada08fc8a70 | 41 | tr_debug("M2MNsdlInterface::M2MNsdlInterface()"); |
mbedAustin | 11:cada08fc8a70 | 42 | _endpoint = NULL; |
mbedAustin | 11:cada08fc8a70 | 43 | _resource = NULL; |
mbedAustin | 11:cada08fc8a70 | 44 | __nsdl_interface = this; |
mbedAustin | 11:cada08fc8a70 | 45 | |
mbedAustin | 11:cada08fc8a70 | 46 | _bootstrap_endpoint.device_object = NULL; |
mbedAustin | 11:cada08fc8a70 | 47 | _bootstrap_endpoint.oma_bs_status_cb = NULL; |
mbedAustin | 11:cada08fc8a70 | 48 | |
mbedAustin | 11:cada08fc8a70 | 49 | _bootstrap_device_setup.sn_oma_device_boot_callback = NULL; |
mbedAustin | 11:cada08fc8a70 | 50 | _bootstrap_device_setup.error_code = NO_ERROR; |
mbedAustin | 11:cada08fc8a70 | 51 | |
mbedAustin | 11:cada08fc8a70 | 52 | _sn_nsdl_address.addr_len = 0; |
mbedAustin | 11:cada08fc8a70 | 53 | _sn_nsdl_address.addr_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 54 | _sn_nsdl_address.port = 0; |
mbedAustin | 11:cada08fc8a70 | 55 | |
mbedAustin | 11:cada08fc8a70 | 56 | // This initializes libCoap and libNsdl |
mbedAustin | 11:cada08fc8a70 | 57 | // Parameters are function pointers to used memory allocation |
mbedAustin | 11:cada08fc8a70 | 58 | // and free functions in structure and used functions for sending |
mbedAustin | 11:cada08fc8a70 | 59 | // and receiving purposes. |
mbedAustin | 11:cada08fc8a70 | 60 | _nsdl_handle = sn_nsdl_init(&(__nsdl_c_send_to_server), &(__nsdl_c_received_from_server), |
mbedAustin | 11:cada08fc8a70 | 61 | &(__nsdl_c_memory_alloc), &(__nsdl_c_memory_free)); |
mbedAustin | 11:cada08fc8a70 | 62 | |
mbedAustin | 11:cada08fc8a70 | 63 | initialize(); |
mbedAustin | 11:cada08fc8a70 | 64 | } |
mbedAustin | 11:cada08fc8a70 | 65 | |
mbedAustin | 11:cada08fc8a70 | 66 | M2MNsdlInterface::~M2MNsdlInterface() |
mbedAustin | 11:cada08fc8a70 | 67 | { |
mbedAustin | 11:cada08fc8a70 | 68 | tr_debug("M2MNsdlInterface::~M2MNsdlInterface() - IN"); |
mbedAustin | 11:cada08fc8a70 | 69 | if(_resource) { |
mbedAustin | 11:cada08fc8a70 | 70 | if(_resource->resource_parameters_ptr) { |
mbedAustin | 11:cada08fc8a70 | 71 | memory_free(_resource->resource_parameters_ptr); |
mbedAustin | 11:cada08fc8a70 | 72 | _resource->resource_parameters_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 73 | } |
mbedAustin | 11:cada08fc8a70 | 74 | memory_free(_resource); |
mbedAustin | 11:cada08fc8a70 | 75 | _resource = NULL; |
mbedAustin | 11:cada08fc8a70 | 76 | } |
mbedAustin | 11:cada08fc8a70 | 77 | if(_endpoint) { |
mbedAustin | 11:cada08fc8a70 | 78 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 79 | memory_free(_endpoint->lifetime_ptr); |
mbedAustin | 11:cada08fc8a70 | 80 | _endpoint->lifetime_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 81 | } |
mbedAustin | 11:cada08fc8a70 | 82 | if(_endpoint->location_ptr) { |
mbedAustin | 11:cada08fc8a70 | 83 | memory_free(_endpoint->location_ptr); |
mbedAustin | 11:cada08fc8a70 | 84 | _endpoint->location_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 85 | } |
mbedAustin | 11:cada08fc8a70 | 86 | memory_free(_endpoint); |
mbedAustin | 11:cada08fc8a70 | 87 | _endpoint = NULL; |
mbedAustin | 11:cada08fc8a70 | 88 | |
mbedAustin | 11:cada08fc8a70 | 89 | } |
mbedAustin | 11:cada08fc8a70 | 90 | delete _nsdl_exceution_timer; |
mbedAustin | 11:cada08fc8a70 | 91 | delete _registration_timer; |
mbedAustin | 11:cada08fc8a70 | 92 | _object_list.clear(); |
mbedAustin | 11:cada08fc8a70 | 93 | |
mbedAustin | 11:cada08fc8a70 | 94 | if(_server){ |
mbedAustin | 11:cada08fc8a70 | 95 | delete _server; |
mbedAustin | 11:cada08fc8a70 | 96 | _server = NULL; |
mbedAustin | 11:cada08fc8a70 | 97 | } |
mbedAustin | 11:cada08fc8a70 | 98 | sn_nsdl_destroy(_nsdl_handle); |
mbedAustin | 11:cada08fc8a70 | 99 | _nsdl_handle = NULL; |
mbedAustin | 11:cada08fc8a70 | 100 | __nsdl_interface = NULL; |
mbedAustin | 11:cada08fc8a70 | 101 | tr_debug("M2MNsdlInterface::~M2MNsdlInterface() - OUT"); |
mbedAustin | 11:cada08fc8a70 | 102 | } |
mbedAustin | 11:cada08fc8a70 | 103 | |
mbedAustin | 11:cada08fc8a70 | 104 | bool M2MNsdlInterface::initialize() |
mbedAustin | 11:cada08fc8a70 | 105 | { |
mbedAustin | 11:cada08fc8a70 | 106 | tr_debug("M2MNsdlInterface::initialize()"); |
mbedAustin | 11:cada08fc8a70 | 107 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 108 | |
mbedAustin | 11:cada08fc8a70 | 109 | //Sets the packet retransmission attempts and time interval |
mbedAustin | 11:cada08fc8a70 | 110 | sn_coap_protocol_set_retransmission_parameters(RETRY_COUNT,RETRY_INTERVAL); |
mbedAustin | 11:cada08fc8a70 | 111 | |
mbedAustin | 11:cada08fc8a70 | 112 | _nsdl_exceution_timer->start_timer(ONE_SECOND_TIMER * 1000, |
mbedAustin | 11:cada08fc8a70 | 113 | M2MTimerObserver::NsdlExecution, |
mbedAustin | 11:cada08fc8a70 | 114 | false); |
mbedAustin | 11:cada08fc8a70 | 115 | |
mbedAustin | 11:cada08fc8a70 | 116 | // Allocate the memory for resources |
mbedAustin | 11:cada08fc8a70 | 117 | _resource = (sn_nsdl_resource_info_s*)memory_alloc(sizeof(sn_nsdl_resource_info_s)); |
mbedAustin | 11:cada08fc8a70 | 118 | if(_resource) { |
mbedAustin | 11:cada08fc8a70 | 119 | memset(_resource, 0, sizeof(sn_nsdl_resource_info_s)); |
mbedAustin | 11:cada08fc8a70 | 120 | _resource->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)memory_alloc(sizeof(sn_nsdl_resource_parameters_s)+1); |
mbedAustin | 11:cada08fc8a70 | 121 | if(_resource->resource_parameters_ptr) { |
mbedAustin | 11:cada08fc8a70 | 122 | memset(_resource->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)+1); |
mbedAustin | 11:cada08fc8a70 | 123 | } |
mbedAustin | 11:cada08fc8a70 | 124 | } |
mbedAustin | 11:cada08fc8a70 | 125 | |
mbedAustin | 11:cada08fc8a70 | 126 | //Allocate the memory for endpoint |
mbedAustin | 11:cada08fc8a70 | 127 | _endpoint = (sn_nsdl_ep_parameters_s*)memory_alloc(sizeof(sn_nsdl_ep_parameters_s)+1); |
mbedAustin | 11:cada08fc8a70 | 128 | if(_endpoint) { |
mbedAustin | 11:cada08fc8a70 | 129 | memset(_endpoint, 0, sizeof(sn_nsdl_ep_parameters_s)+1); |
mbedAustin | 11:cada08fc8a70 | 130 | success = true; |
mbedAustin | 11:cada08fc8a70 | 131 | } |
mbedAustin | 11:cada08fc8a70 | 132 | return success; |
mbedAustin | 11:cada08fc8a70 | 133 | } |
mbedAustin | 11:cada08fc8a70 | 134 | |
mbedAustin | 11:cada08fc8a70 | 135 | void M2MNsdlInterface::create_endpoint(const String &name, |
mbedAustin | 11:cada08fc8a70 | 136 | const String &type, |
mbedAustin | 11:cada08fc8a70 | 137 | const int32_t life_time, |
mbedAustin | 11:cada08fc8a70 | 138 | const String &domain, |
mbedAustin | 11:cada08fc8a70 | 139 | const uint8_t mode, |
mbedAustin | 11:cada08fc8a70 | 140 | const String &/*context_address*/) |
mbedAustin | 11:cada08fc8a70 | 141 | { |
mbedAustin | 11:cada08fc8a70 | 142 | tr_debug("M2MNsdlInterface::create_endpoint( name %s type %s lifetime %d, domain %s, mode %d)", |
mbedAustin | 11:cada08fc8a70 | 143 | name.c_str(), type.c_str(), life_time, domain.c_str(), mode); |
mbedAustin | 11:cada08fc8a70 | 144 | if(_endpoint){ |
mbedAustin | 11:cada08fc8a70 | 145 | memset(_endpoint, 0, sizeof(sn_nsdl_ep_parameters_s)+1); |
mbedAustin | 11:cada08fc8a70 | 146 | if(!name.empty()) { |
mbedAustin | 11:cada08fc8a70 | 147 | _endpoint->endpoint_name_ptr = (uint8_t*)name.c_str(); |
mbedAustin | 11:cada08fc8a70 | 148 | _endpoint->endpoint_name_len = name.length(); |
mbedAustin | 11:cada08fc8a70 | 149 | } |
mbedAustin | 11:cada08fc8a70 | 150 | if(!type.empty()) { |
mbedAustin | 11:cada08fc8a70 | 151 | _endpoint->type_ptr = (uint8_t*)type.c_str(); |
mbedAustin | 11:cada08fc8a70 | 152 | _endpoint->type_len = type.length(); |
mbedAustin | 11:cada08fc8a70 | 153 | } |
mbedAustin | 11:cada08fc8a70 | 154 | if(!domain.empty()) { |
mbedAustin | 11:cada08fc8a70 | 155 | _endpoint->domain_name_ptr = (uint8_t*)domain.c_str(); |
mbedAustin | 11:cada08fc8a70 | 156 | _endpoint->domain_name_len = domain.length(); |
mbedAustin | 11:cada08fc8a70 | 157 | } |
mbedAustin | 11:cada08fc8a70 | 158 | _endpoint->binding_and_mode = (sn_nsdl_oma_binding_and_mode_t)mode; |
mbedAustin | 11:cada08fc8a70 | 159 | |
mbedAustin | 11:cada08fc8a70 | 160 | // If lifetime is less than zero then leave the field empty |
mbedAustin | 11:cada08fc8a70 | 161 | if( life_time > 0) { |
mbedAustin | 11:cada08fc8a70 | 162 | char *buffer = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 163 | int size = snprintf(buffer, 20,"%ld",(long int)life_time); |
mbedAustin | 11:cada08fc8a70 | 164 | if( _endpoint->lifetime_ptr == NULL ){ |
mbedAustin | 11:cada08fc8a70 | 165 | _endpoint->lifetime_ptr = (uint8_t*)memory_alloc(size+1); |
mbedAustin | 11:cada08fc8a70 | 166 | } |
mbedAustin | 11:cada08fc8a70 | 167 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 168 | memset(_endpoint->lifetime_ptr, 0, size+1); |
mbedAustin | 11:cada08fc8a70 | 169 | memcpy(_endpoint->lifetime_ptr,buffer,size); |
mbedAustin | 11:cada08fc8a70 | 170 | _endpoint->lifetime_len = size; |
mbedAustin | 11:cada08fc8a70 | 171 | } |
mbedAustin | 11:cada08fc8a70 | 172 | memory_free(buffer); |
mbedAustin | 11:cada08fc8a70 | 173 | } |
mbedAustin | 11:cada08fc8a70 | 174 | } |
mbedAustin | 11:cada08fc8a70 | 175 | } |
mbedAustin | 11:cada08fc8a70 | 176 | |
mbedAustin | 11:cada08fc8a70 | 177 | void M2MNsdlInterface::delete_endpoint() |
mbedAustin | 11:cada08fc8a70 | 178 | { |
mbedAustin | 11:cada08fc8a70 | 179 | tr_debug("M2MNsdlInterface::delete_endpoint()"); |
mbedAustin | 11:cada08fc8a70 | 180 | if(_endpoint) { |
mbedAustin | 11:cada08fc8a70 | 181 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 182 | free(_endpoint->lifetime_ptr); |
mbedAustin | 11:cada08fc8a70 | 183 | _endpoint->lifetime_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 184 | } |
mbedAustin | 11:cada08fc8a70 | 185 | memory_free(_endpoint); |
mbedAustin | 11:cada08fc8a70 | 186 | _endpoint = NULL; |
mbedAustin | 11:cada08fc8a70 | 187 | } |
mbedAustin | 11:cada08fc8a70 | 188 | } |
mbedAustin | 11:cada08fc8a70 | 189 | |
mbedAustin | 11:cada08fc8a70 | 190 | bool M2MNsdlInterface::create_nsdl_list_structure(const M2MObjectList &object_list) |
mbedAustin | 11:cada08fc8a70 | 191 | { |
mbedAustin | 11:cada08fc8a70 | 192 | tr_debug("M2MNsdlInterface::create_nsdl_list_structure()"); |
mbedAustin | 11:cada08fc8a70 | 193 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 194 | if(!object_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 195 | tr_debug("M2MNsdlInterface::create_nsdl_list_structure - Object count is %d", object_list.size()); |
mbedAustin | 11:cada08fc8a70 | 196 | M2MObjectList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 197 | it = object_list.begin(); |
mbedAustin | 11:cada08fc8a70 | 198 | for ( ; it != object_list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 199 | // Create NSDL structure for all Objects inside |
mbedAustin | 11:cada08fc8a70 | 200 | success = create_nsdl_object_structure(*it); |
mbedAustin | 11:cada08fc8a70 | 201 | add_object_to_list(*it); |
mbedAustin | 11:cada08fc8a70 | 202 | } |
mbedAustin | 11:cada08fc8a70 | 203 | } |
mbedAustin | 11:cada08fc8a70 | 204 | return success; |
mbedAustin | 11:cada08fc8a70 | 205 | } |
mbedAustin | 11:cada08fc8a70 | 206 | |
mbedAustin | 11:cada08fc8a70 | 207 | bool M2MNsdlInterface::delete_nsdl_resource(const String &resource_name) |
mbedAustin | 11:cada08fc8a70 | 208 | { |
mbedAustin | 11:cada08fc8a70 | 209 | tr_debug("M2MNsdlInterface::delete_nsdl_resource( %s)", resource_name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 210 | return (sn_nsdl_delete_resource(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 211 | resource_name.length(), |
mbedAustin | 11:cada08fc8a70 | 212 | (uint8_t *)resource_name.c_str()) == 0) ? true : false; |
mbedAustin | 11:cada08fc8a70 | 213 | } |
mbedAustin | 11:cada08fc8a70 | 214 | |
mbedAustin | 11:cada08fc8a70 | 215 | bool M2MNsdlInterface::create_bootstrap_resource(sn_nsdl_addr_s *address) |
mbedAustin | 11:cada08fc8a70 | 216 | { |
mbedAustin | 11:cada08fc8a70 | 217 | tr_debug("M2MNsdlInterface::create_bootstrap_resource()"); |
mbedAustin | 11:cada08fc8a70 | 218 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 219 | _bootstrap_device_setup.error_code = NO_ERROR; |
mbedAustin | 11:cada08fc8a70 | 220 | _bootstrap_device_setup.sn_oma_device_boot_callback = 0; |
mbedAustin | 11:cada08fc8a70 | 221 | |
mbedAustin | 11:cada08fc8a70 | 222 | _bootstrap_endpoint.device_object = &_bootstrap_device_setup; |
mbedAustin | 11:cada08fc8a70 | 223 | _bootstrap_endpoint.oma_bs_status_cb = &__nsdl_c_bootstrap_done; |
mbedAustin | 11:cada08fc8a70 | 224 | |
mbedAustin | 11:cada08fc8a70 | 225 | if(_bootstrap_id == 0) { |
mbedAustin | 11:cada08fc8a70 | 226 | _bootstrap_id = sn_nsdl_oma_bootstrap(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 227 | address, |
mbedAustin | 11:cada08fc8a70 | 228 | _endpoint, |
mbedAustin | 11:cada08fc8a70 | 229 | &_bootstrap_endpoint); |
mbedAustin | 11:cada08fc8a70 | 230 | tr_debug("M2MNsdlInterface::create_bootstrap_resource - _bootstrap_id %d", _bootstrap_id); |
mbedAustin | 11:cada08fc8a70 | 231 | success = _bootstrap_id != 0; |
mbedAustin | 11:cada08fc8a70 | 232 | |
mbedAustin | 11:cada08fc8a70 | 233 | } |
mbedAustin | 11:cada08fc8a70 | 234 | return success; |
mbedAustin | 11:cada08fc8a70 | 235 | } |
mbedAustin | 11:cada08fc8a70 | 236 | |
mbedAustin | 11:cada08fc8a70 | 237 | bool M2MNsdlInterface::send_register_message(uint8_t* address, |
mbedAustin | 11:cada08fc8a70 | 238 | const uint16_t port, |
mbedAustin | 11:cada08fc8a70 | 239 | sn_nsdl_addr_type_e address_type) |
mbedAustin | 11:cada08fc8a70 | 240 | { |
mbedAustin | 11:cada08fc8a70 | 241 | tr_debug("M2MNsdlInterface::send_register_message()"); |
mbedAustin | 11:cada08fc8a70 | 242 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 243 | if(set_NSP_address(_nsdl_handle,address, port, address_type) == 0) { |
mbedAustin | 11:cada08fc8a70 | 244 | if(_register_id == 0) { |
mbedAustin | 11:cada08fc8a70 | 245 | _register_id = sn_nsdl_register_endpoint(_nsdl_handle,_endpoint); |
mbedAustin | 11:cada08fc8a70 | 246 | tr_debug("M2MNsdlInterface::send_register_message - _register_id %d", _register_id); |
mbedAustin | 11:cada08fc8a70 | 247 | success = _register_id != 0; |
mbedAustin | 11:cada08fc8a70 | 248 | } |
mbedAustin | 11:cada08fc8a70 | 249 | } |
mbedAustin | 11:cada08fc8a70 | 250 | return success; |
mbedAustin | 11:cada08fc8a70 | 251 | } |
mbedAustin | 11:cada08fc8a70 | 252 | |
mbedAustin | 11:cada08fc8a70 | 253 | bool M2MNsdlInterface::send_update_registration(const uint32_t lifetime) |
mbedAustin | 11:cada08fc8a70 | 254 | { |
mbedAustin | 11:cada08fc8a70 | 255 | tr_debug("M2MNsdlInterface::send_update_registration( lifetime %d)", lifetime); |
mbedAustin | 11:cada08fc8a70 | 256 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 257 | |
mbedAustin | 11:cada08fc8a70 | 258 | create_nsdl_list_structure(_object_list); |
mbedAustin | 11:cada08fc8a70 | 259 | //If Lifetime value is 0, then don't change the existing lifetime value |
mbedAustin | 11:cada08fc8a70 | 260 | if(lifetime != 0) { |
mbedAustin | 11:cada08fc8a70 | 261 | char *buffer = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 262 | int size = snprintf(buffer, 20,"%ld",(long int)lifetime); |
mbedAustin | 11:cada08fc8a70 | 263 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 264 | memory_free(_endpoint->lifetime_ptr); |
mbedAustin | 11:cada08fc8a70 | 265 | _endpoint->lifetime_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 266 | _endpoint->lifetime_len = 0; |
mbedAustin | 11:cada08fc8a70 | 267 | } |
mbedAustin | 11:cada08fc8a70 | 268 | |
mbedAustin | 11:cada08fc8a70 | 269 | if(_endpoint->lifetime_ptr == NULL){ |
mbedAustin | 11:cada08fc8a70 | 270 | _endpoint->lifetime_ptr = (uint8_t*)memory_alloc(size+1); |
mbedAustin | 11:cada08fc8a70 | 271 | } |
mbedAustin | 11:cada08fc8a70 | 272 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 273 | memset(_endpoint->lifetime_ptr, 0, size+1); |
mbedAustin | 11:cada08fc8a70 | 274 | memcpy(_endpoint->lifetime_ptr,buffer,size); |
mbedAustin | 11:cada08fc8a70 | 275 | _endpoint->lifetime_len = size; |
mbedAustin | 11:cada08fc8a70 | 276 | } |
mbedAustin | 11:cada08fc8a70 | 277 | memory_free(buffer); |
mbedAustin | 11:cada08fc8a70 | 278 | |
mbedAustin | 11:cada08fc8a70 | 279 | _registration_timer->stop_timer(); |
mbedAustin | 11:cada08fc8a70 | 280 | _registration_timer->start_timer(registration_time() * 1000, |
mbedAustin | 11:cada08fc8a70 | 281 | M2MTimerObserver::Registration, |
mbedAustin | 11:cada08fc8a70 | 282 | false); |
mbedAustin | 11:cada08fc8a70 | 283 | if(_nsdl_handle && |
mbedAustin | 11:cada08fc8a70 | 284 | _endpoint && _endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 285 | _update_id = sn_nsdl_update_registration(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 286 | _endpoint->lifetime_ptr, |
mbedAustin | 11:cada08fc8a70 | 287 | _endpoint->lifetime_len); |
mbedAustin | 11:cada08fc8a70 | 288 | tr_debug("M2MNsdlInterface::send_update_registration - New lifetime value _update_id %d", _update_id); |
mbedAustin | 11:cada08fc8a70 | 289 | success = _update_id != 0; |
mbedAustin | 11:cada08fc8a70 | 290 | } |
mbedAustin | 11:cada08fc8a70 | 291 | } else { |
mbedAustin | 11:cada08fc8a70 | 292 | if(_nsdl_handle) { |
mbedAustin | 11:cada08fc8a70 | 293 | _update_id = sn_nsdl_update_registration(_nsdl_handle, NULL, 0); |
mbedAustin | 11:cada08fc8a70 | 294 | tr_debug("M2MNsdlInterface::send_update_registration - regular update- _update_id %d", _update_id); |
mbedAustin | 11:cada08fc8a70 | 295 | success = _update_id != 0; |
mbedAustin | 11:cada08fc8a70 | 296 | } |
mbedAustin | 11:cada08fc8a70 | 297 | } |
mbedAustin | 11:cada08fc8a70 | 298 | return success; |
mbedAustin | 11:cada08fc8a70 | 299 | } |
mbedAustin | 11:cada08fc8a70 | 300 | |
mbedAustin | 11:cada08fc8a70 | 301 | bool M2MNsdlInterface::send_unregister_message() |
mbedAustin | 11:cada08fc8a70 | 302 | { |
mbedAustin | 11:cada08fc8a70 | 303 | tr_debug("M2MNsdlInterface::send_unregister_message"); |
mbedAustin | 11:cada08fc8a70 | 304 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 305 | //Does not clean resources automatically |
mbedAustin | 11:cada08fc8a70 | 306 | if(_unregister_id == 0) { |
mbedAustin | 11:cada08fc8a70 | 307 | _unregister_id = sn_nsdl_unregister_endpoint(_nsdl_handle); |
mbedAustin | 11:cada08fc8a70 | 308 | tr_debug("M2MNsdlInterface::send_unregister_message - _unregister_id %d", _unregister_id); |
mbedAustin | 11:cada08fc8a70 | 309 | success = _unregister_id != 0; |
mbedAustin | 11:cada08fc8a70 | 310 | } |
mbedAustin | 11:cada08fc8a70 | 311 | return success; |
mbedAustin | 11:cada08fc8a70 | 312 | } |
mbedAustin | 11:cada08fc8a70 | 313 | |
mbedAustin | 11:cada08fc8a70 | 314 | void *M2MNsdlInterface::memory_alloc(uint16_t size) |
mbedAustin | 11:cada08fc8a70 | 315 | { |
mbedAustin | 11:cada08fc8a70 | 316 | if(size) |
mbedAustin | 11:cada08fc8a70 | 317 | return malloc(size); |
mbedAustin | 11:cada08fc8a70 | 318 | else |
mbedAustin | 11:cada08fc8a70 | 319 | return 0; |
mbedAustin | 11:cada08fc8a70 | 320 | } |
mbedAustin | 11:cada08fc8a70 | 321 | |
mbedAustin | 11:cada08fc8a70 | 322 | void M2MNsdlInterface::memory_free(void *ptr) |
mbedAustin | 11:cada08fc8a70 | 323 | { |
mbedAustin | 11:cada08fc8a70 | 324 | if(ptr) |
mbedAustin | 11:cada08fc8a70 | 325 | free(ptr); |
mbedAustin | 11:cada08fc8a70 | 326 | } |
mbedAustin | 11:cada08fc8a70 | 327 | |
mbedAustin | 11:cada08fc8a70 | 328 | uint8_t M2MNsdlInterface::send_to_server_callback(struct nsdl_s * /*nsdl_handle*/, |
mbedAustin | 11:cada08fc8a70 | 329 | sn_nsdl_capab_e /*protocol*/, |
mbedAustin | 11:cada08fc8a70 | 330 | uint8_t *data_ptr, |
mbedAustin | 11:cada08fc8a70 | 331 | uint16_t data_len, |
mbedAustin | 11:cada08fc8a70 | 332 | sn_nsdl_addr_s *address) |
mbedAustin | 11:cada08fc8a70 | 333 | { |
mbedAustin | 11:cada08fc8a70 | 334 | tr_debug("M2MNsdlInterface::send_to_server_callback()"); |
mbedAustin | 11:cada08fc8a70 | 335 | _observer.coap_message_ready(data_ptr,data_len,address); |
mbedAustin | 11:cada08fc8a70 | 336 | return 1; |
mbedAustin | 11:cada08fc8a70 | 337 | } |
mbedAustin | 11:cada08fc8a70 | 338 | |
mbedAustin | 11:cada08fc8a70 | 339 | uint8_t M2MNsdlInterface::received_from_server_callback(struct nsdl_s * /*nsdl_handle*/, |
mbedAustin | 11:cada08fc8a70 | 340 | sn_coap_hdr_s *coap_header, |
mbedAustin | 11:cada08fc8a70 | 341 | sn_nsdl_addr_s *address) |
mbedAustin | 11:cada08fc8a70 | 342 | { |
mbedAustin | 11:cada08fc8a70 | 343 | tr_debug("M2MNsdlInterface::received_from_server_callback()"); |
mbedAustin | 11:cada08fc8a70 | 344 | _observer.coap_data_processed(); |
mbedAustin | 11:cada08fc8a70 | 345 | uint8_t value = 0; |
mbedAustin | 11:cada08fc8a70 | 346 | if(coap_header) { |
mbedAustin | 11:cada08fc8a70 | 347 | if(coap_header->msg_id == _register_id) { |
mbedAustin | 11:cada08fc8a70 | 348 | _register_id = 0; |
mbedAustin | 11:cada08fc8a70 | 349 | if(coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CREATED) { |
mbedAustin | 11:cada08fc8a70 | 350 | if(_server) { |
mbedAustin | 11:cada08fc8a70 | 351 | delete _server; |
mbedAustin | 11:cada08fc8a70 | 352 | _server = NULL; |
mbedAustin | 11:cada08fc8a70 | 353 | } |
mbedAustin | 11:cada08fc8a70 | 354 | tr_debug("M2MNsdlInterface::received_from_server_callback - registration callback"); |
mbedAustin | 11:cada08fc8a70 | 355 | _server = new M2MServer(); |
mbedAustin | 11:cada08fc8a70 | 356 | _server->set_resource_value(M2MServer::ShortServerID,1); |
mbedAustin | 11:cada08fc8a70 | 357 | |
mbedAustin | 11:cada08fc8a70 | 358 | _observer.client_registered(_server); |
mbedAustin | 11:cada08fc8a70 | 359 | // If lifetime is less than zero then leave the field empty |
mbedAustin | 11:cada08fc8a70 | 360 | if(coap_header->options_list_ptr) { |
mbedAustin | 11:cada08fc8a70 | 361 | if(coap_header->options_list_ptr->max_age_ptr) { |
mbedAustin | 11:cada08fc8a70 | 362 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 363 | memory_free(_endpoint->lifetime_ptr); |
mbedAustin | 11:cada08fc8a70 | 364 | _endpoint->lifetime_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 365 | _endpoint->lifetime_len = 0; |
mbedAustin | 11:cada08fc8a70 | 366 | } |
mbedAustin | 11:cada08fc8a70 | 367 | uint32_t max_time = 0; |
mbedAustin | 11:cada08fc8a70 | 368 | for(int i=0;i < coap_header->options_list_ptr->max_age_len; i++) { |
mbedAustin | 11:cada08fc8a70 | 369 | max_time += (*(coap_header->options_list_ptr->max_age_ptr + i) & 0xff) << |
mbedAustin | 11:cada08fc8a70 | 370 | 8*(coap_header->options_list_ptr->max_age_len- 1 - i); |
mbedAustin | 11:cada08fc8a70 | 371 | } |
mbedAustin | 11:cada08fc8a70 | 372 | // If lifetime is less than zero then leave the field empty |
mbedAustin | 11:cada08fc8a70 | 373 | if( max_time > 0) { |
mbedAustin | 11:cada08fc8a70 | 374 | char *buffer = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 375 | if(buffer) { |
mbedAustin | 11:cada08fc8a70 | 376 | int size = snprintf(buffer, 20,"%ld",(long int)max_time); |
mbedAustin | 11:cada08fc8a70 | 377 | _endpoint->lifetime_ptr = (uint8_t*)memory_alloc(size+1); |
mbedAustin | 11:cada08fc8a70 | 378 | |
mbedAustin | 11:cada08fc8a70 | 379 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 380 | memset(_endpoint->lifetime_ptr, 0, size+1); |
mbedAustin | 11:cada08fc8a70 | 381 | memcpy(_endpoint->lifetime_ptr,buffer,size); |
mbedAustin | 11:cada08fc8a70 | 382 | _endpoint->lifetime_len = size; |
mbedAustin | 11:cada08fc8a70 | 383 | } |
mbedAustin | 11:cada08fc8a70 | 384 | memory_free(buffer); |
mbedAustin | 11:cada08fc8a70 | 385 | } |
mbedAustin | 11:cada08fc8a70 | 386 | } |
mbedAustin | 11:cada08fc8a70 | 387 | } |
mbedAustin | 11:cada08fc8a70 | 388 | if(coap_header->options_list_ptr->location_path_ptr) { |
mbedAustin | 11:cada08fc8a70 | 389 | _endpoint->location_ptr = (uint8_t*)memory_alloc(coap_header->options_list_ptr->location_path_len+1); |
mbedAustin | 11:cada08fc8a70 | 390 | memset(_endpoint->location_ptr,0,coap_header->options_list_ptr->location_path_len+1); |
mbedAustin | 11:cada08fc8a70 | 391 | memcpy(_endpoint->location_ptr, |
mbedAustin | 11:cada08fc8a70 | 392 | coap_header->options_list_ptr->location_path_ptr, |
mbedAustin | 11:cada08fc8a70 | 393 | coap_header->options_list_ptr->location_path_len); |
mbedAustin | 11:cada08fc8a70 | 394 | _endpoint->location_len = coap_header->options_list_ptr->location_path_len ; |
mbedAustin | 11:cada08fc8a70 | 395 | sn_nsdl_set_endpoint_location(_nsdl_handle,_endpoint->location_ptr,_endpoint->location_len); |
mbedAustin | 11:cada08fc8a70 | 396 | } |
mbedAustin | 11:cada08fc8a70 | 397 | } |
mbedAustin | 11:cada08fc8a70 | 398 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 399 | _registration_timer->stop_timer(); |
mbedAustin | 11:cada08fc8a70 | 400 | _registration_timer->start_timer(registration_time() * 1000, |
mbedAustin | 11:cada08fc8a70 | 401 | M2MTimerObserver::Registration, |
mbedAustin | 11:cada08fc8a70 | 402 | false); |
mbedAustin | 11:cada08fc8a70 | 403 | } |
mbedAustin | 11:cada08fc8a70 | 404 | } else { |
mbedAustin | 11:cada08fc8a70 | 405 | if(_server) { |
mbedAustin | 11:cada08fc8a70 | 406 | delete _server; |
mbedAustin | 11:cada08fc8a70 | 407 | _server = NULL; |
mbedAustin | 11:cada08fc8a70 | 408 | } |
mbedAustin | 11:cada08fc8a70 | 409 | tr_error("M2MNsdlInterface::received_from_server_callback - registration error %d", coap_header->msg_code); |
mbedAustin | 11:cada08fc8a70 | 410 | M2MInterface::Error error = interface_error(coap_header); |
mbedAustin | 11:cada08fc8a70 | 411 | _observer.registration_error(error); |
mbedAustin | 11:cada08fc8a70 | 412 | } |
mbedAustin | 11:cada08fc8a70 | 413 | } else if(coap_header->msg_id == _unregister_id) { |
mbedAustin | 11:cada08fc8a70 | 414 | _unregister_id = 0; |
mbedAustin | 11:cada08fc8a70 | 415 | if(coap_header->msg_code == COAP_MSG_CODE_RESPONSE_DELETED) { |
mbedAustin | 11:cada08fc8a70 | 416 | _registration_timer->stop_timer(); |
mbedAustin | 11:cada08fc8a70 | 417 | if(_server) { |
mbedAustin | 11:cada08fc8a70 | 418 | delete _server; |
mbedAustin | 11:cada08fc8a70 | 419 | _server = NULL; |
mbedAustin | 11:cada08fc8a70 | 420 | } |
mbedAustin | 11:cada08fc8a70 | 421 | tr_debug("M2MNsdlInterface::received_from_server_callback - unregistration callback"); |
mbedAustin | 11:cada08fc8a70 | 422 | _observer.client_unregistered(); |
mbedAustin | 11:cada08fc8a70 | 423 | } else { |
mbedAustin | 11:cada08fc8a70 | 424 | tr_error("M2MNsdlInterface::received_from_server_callback - unregistration error %d", coap_header->msg_code); |
mbedAustin | 11:cada08fc8a70 | 425 | M2MInterface::Error error = interface_error(coap_header); |
mbedAustin | 11:cada08fc8a70 | 426 | _observer.registration_error(error); |
mbedAustin | 11:cada08fc8a70 | 427 | } |
mbedAustin | 11:cada08fc8a70 | 428 | } else if(coap_header->msg_id == _update_id) { |
mbedAustin | 11:cada08fc8a70 | 429 | _update_id = 0; |
mbedAustin | 11:cada08fc8a70 | 430 | |
mbedAustin | 11:cada08fc8a70 | 431 | if(coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CHANGED) { |
mbedAustin | 11:cada08fc8a70 | 432 | tr_debug("M2MNsdlInterface::received_from_server_callback - registration_updated successfully"); |
mbedAustin | 11:cada08fc8a70 | 433 | _observer.registration_updated(*_server); |
mbedAustin | 11:cada08fc8a70 | 434 | } else { |
mbedAustin | 11:cada08fc8a70 | 435 | tr_error("M2MNsdlInterface::received_from_server_callback - registration_updated failed %d", coap_header->msg_code); |
mbedAustin | 11:cada08fc8a70 | 436 | M2MInterface::Error error = interface_error(coap_header); |
mbedAustin | 11:cada08fc8a70 | 437 | // In case, the error for update registration is not allowed implies the client is no longer registered in |
mbedAustin | 11:cada08fc8a70 | 438 | // server hence the error returns should be NotRegistered. |
mbedAustin | 11:cada08fc8a70 | 439 | if(M2MInterface::NotAllowed == error) { |
mbedAustin | 11:cada08fc8a70 | 440 | error = M2MInterface::NotRegistered; |
mbedAustin | 11:cada08fc8a70 | 441 | } |
mbedAustin | 11:cada08fc8a70 | 442 | _observer.registration_error(error); |
mbedAustin | 11:cada08fc8a70 | 443 | } |
mbedAustin | 11:cada08fc8a70 | 444 | }else if(coap_header->msg_id == _bootstrap_id) { |
mbedAustin | 11:cada08fc8a70 | 445 | _bootstrap_id = 0; |
mbedAustin | 11:cada08fc8a70 | 446 | M2MInterface::Error error = interface_error(coap_header); |
mbedAustin | 11:cada08fc8a70 | 447 | if(error != M2MInterface::ErrorNone) { |
mbedAustin | 11:cada08fc8a70 | 448 | _observer.bootstrap_error(); |
mbedAustin | 11:cada08fc8a70 | 449 | } |
mbedAustin | 11:cada08fc8a70 | 450 | } else { |
mbedAustin | 11:cada08fc8a70 | 451 | if(COAP_MSG_CODE_REQUEST_POST == coap_header->msg_code) { |
mbedAustin | 11:cada08fc8a70 | 452 | if(coap_header->uri_path_ptr) { |
mbedAustin | 11:cada08fc8a70 | 453 | String resource_name = coap_to_string(coap_header->uri_path_ptr, |
mbedAustin | 11:cada08fc8a70 | 454 | coap_header->uri_path_len); |
mbedAustin | 11:cada08fc8a70 | 455 | |
mbedAustin | 11:cada08fc8a70 | 456 | sn_coap_hdr_s *coap_response = NULL; |
mbedAustin | 11:cada08fc8a70 | 457 | String object_name; |
mbedAustin | 11:cada08fc8a70 | 458 | int slash_found = resource_name.find_last_of('/'); |
mbedAustin | 11:cada08fc8a70 | 459 | //The POST operation here is only allowed for non-existing object instances |
mbedAustin | 11:cada08fc8a70 | 460 | if(slash_found != -1) { |
mbedAustin | 11:cada08fc8a70 | 461 | object_name = resource_name.substr(0,slash_found); |
mbedAustin | 11:cada08fc8a70 | 462 | if( object_name.find_last_of('/') != -1){ |
mbedAustin | 11:cada08fc8a70 | 463 | coap_response = sn_nsdl_build_response(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 464 | coap_header, |
mbedAustin | 11:cada08fc8a70 | 465 | COAP_MSG_CODE_RESPONSE_NOT_FOUND); |
mbedAustin | 11:cada08fc8a70 | 466 | } else { |
mbedAustin | 11:cada08fc8a70 | 467 | int32_t instance_id = atoi(resource_name.substr(slash_found+1, |
mbedAustin | 11:cada08fc8a70 | 468 | resource_name.size()-object_name.size()).c_str()); |
mbedAustin | 11:cada08fc8a70 | 469 | M2MBase* base = find_resource(object_name); |
mbedAustin | 11:cada08fc8a70 | 470 | if(base && (instance_id >= 0) && (instance_id < 65535)) { |
mbedAustin | 11:cada08fc8a70 | 471 | if(coap_header->payload_ptr) { |
mbedAustin | 11:cada08fc8a70 | 472 | M2MObject* object = (M2MObject*)base; |
mbedAustin | 11:cada08fc8a70 | 473 | M2MObjectInstance *obj_instance = object->create_object_instance(instance_id); |
mbedAustin | 11:cada08fc8a70 | 474 | if(obj_instance) { |
mbedAustin | 11:cada08fc8a70 | 475 | obj_instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 476 | coap_response = obj_instance->handle_post_request(_nsdl_handle,coap_header,this); |
mbedAustin | 11:cada08fc8a70 | 477 | } |
mbedAustin | 11:cada08fc8a70 | 478 | if(coap_response && coap_response->msg_code != COAP_MSG_CODE_RESPONSE_CREATED) { |
mbedAustin | 11:cada08fc8a70 | 479 | //Invalid request so remove created ObjectInstance |
mbedAustin | 11:cada08fc8a70 | 480 | object->remove_object_instance(instance_id); |
mbedAustin | 11:cada08fc8a70 | 481 | } |
mbedAustin | 11:cada08fc8a70 | 482 | } else { |
mbedAustin | 11:cada08fc8a70 | 483 | tr_debug("M2MNsdlInterface::received_from_server_callback - Missing Payload - Cannot create"); |
mbedAustin | 11:cada08fc8a70 | 484 | coap_response = sn_nsdl_build_response(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 485 | coap_header, |
mbedAustin | 11:cada08fc8a70 | 486 | COAP_MSG_CODE_RESPONSE_BAD_REQUEST); |
mbedAustin | 11:cada08fc8a70 | 487 | } |
mbedAustin | 11:cada08fc8a70 | 488 | } else { //if(base) |
mbedAustin | 11:cada08fc8a70 | 489 | tr_debug("M2MNsdlInterface::received_from_server_callback - Missing BASE - Cannot create"); |
mbedAustin | 11:cada08fc8a70 | 490 | coap_response = sn_nsdl_build_response(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 491 | coap_header, |
mbedAustin | 11:cada08fc8a70 | 492 | COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED); |
mbedAustin | 11:cada08fc8a70 | 493 | } |
mbedAustin | 11:cada08fc8a70 | 494 | } |
mbedAustin | 11:cada08fc8a70 | 495 | } else{ // if(slash_found != -1) |
mbedAustin | 11:cada08fc8a70 | 496 | tr_debug("M2MNsdlInterface::received_from_server_callback - slash_found - Cannot create"); |
mbedAustin | 11:cada08fc8a70 | 497 | coap_response = sn_nsdl_build_response(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 498 | coap_header, |
mbedAustin | 11:cada08fc8a70 | 499 | COAP_MSG_CODE_RESPONSE_NOT_FOUND); |
mbedAustin | 11:cada08fc8a70 | 500 | } |
mbedAustin | 11:cada08fc8a70 | 501 | if(coap_response) { |
mbedAustin | 11:cada08fc8a70 | 502 | tr_debug("M2MNsdlInterface::received_from_server_callback - send CoAP response"); |
mbedAustin | 11:cada08fc8a70 | 503 | (sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response) == 0) ? value = 0 : value = 1; |
mbedAustin | 11:cada08fc8a70 | 504 | sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response); |
mbedAustin | 11:cada08fc8a70 | 505 | } |
mbedAustin | 11:cada08fc8a70 | 506 | } |
mbedAustin | 11:cada08fc8a70 | 507 | } |
mbedAustin | 11:cada08fc8a70 | 508 | } |
mbedAustin | 11:cada08fc8a70 | 509 | } |
mbedAustin | 11:cada08fc8a70 | 510 | return value; |
mbedAustin | 11:cada08fc8a70 | 511 | } |
mbedAustin | 11:cada08fc8a70 | 512 | |
mbedAustin | 11:cada08fc8a70 | 513 | uint8_t M2MNsdlInterface::resource_callback(struct nsdl_s */*nsdl_handle*/, |
mbedAustin | 11:cada08fc8a70 | 514 | sn_coap_hdr_s *received_coap_header, |
mbedAustin | 11:cada08fc8a70 | 515 | sn_nsdl_addr_s *address, |
mbedAustin | 11:cada08fc8a70 | 516 | sn_nsdl_capab_e /*nsdl_capab*/) |
mbedAustin | 11:cada08fc8a70 | 517 | { |
mbedAustin | 11:cada08fc8a70 | 518 | tr_debug("M2MNsdlInterface::resource_callback()"); |
mbedAustin | 11:cada08fc8a70 | 519 | _observer.coap_data_processed(); |
mbedAustin | 11:cada08fc8a70 | 520 | uint8_t result = 1; |
mbedAustin | 11:cada08fc8a70 | 521 | sn_coap_hdr_s *coap_response = NULL; |
mbedAustin | 11:cada08fc8a70 | 522 | sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 4.00 |
mbedAustin | 11:cada08fc8a70 | 523 | String resource_name = coap_to_string(received_coap_header->uri_path_ptr, |
mbedAustin | 11:cada08fc8a70 | 524 | received_coap_header->uri_path_len); |
mbedAustin | 11:cada08fc8a70 | 525 | tr_debug("M2MNsdlInterface::resource_callback() - resource_name %s", resource_name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 526 | M2MBase* base = find_resource(resource_name); |
mbedAustin | 11:cada08fc8a70 | 527 | |
mbedAustin | 11:cada08fc8a70 | 528 | if(base) { |
mbedAustin | 11:cada08fc8a70 | 529 | if(COAP_MSG_CODE_REQUEST_GET == received_coap_header->msg_code) { |
mbedAustin | 11:cada08fc8a70 | 530 | coap_response = base->handle_get_request(_nsdl_handle, received_coap_header,this); |
mbedAustin | 11:cada08fc8a70 | 531 | } else if(COAP_MSG_CODE_REQUEST_PUT == received_coap_header->msg_code) { |
mbedAustin | 11:cada08fc8a70 | 532 | coap_response = base->handle_put_request(_nsdl_handle, received_coap_header,this); |
mbedAustin | 11:cada08fc8a70 | 533 | } else if(COAP_MSG_CODE_REQUEST_POST == received_coap_header->msg_code) { |
mbedAustin | 11:cada08fc8a70 | 534 | if(base->base_type() == M2MBase::ResourceInstance) { |
mbedAustin | 11:cada08fc8a70 | 535 | msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; |
mbedAustin | 11:cada08fc8a70 | 536 | } else { |
mbedAustin | 11:cada08fc8a70 | 537 | coap_response = base->handle_post_request(_nsdl_handle, received_coap_header,this); |
mbedAustin | 11:cada08fc8a70 | 538 | } |
mbedAustin | 11:cada08fc8a70 | 539 | } else if(COAP_MSG_CODE_REQUEST_DELETE == received_coap_header->msg_code) { |
mbedAustin | 11:cada08fc8a70 | 540 | // Delete the object instance |
mbedAustin | 11:cada08fc8a70 | 541 | tr_debug("M2MNsdlInterface::resource_callback() - DELETE the object instance"); |
mbedAustin | 11:cada08fc8a70 | 542 | M2MBase::BaseType type = base->base_type(); |
mbedAustin | 11:cada08fc8a70 | 543 | if(M2MBase::ObjectInstance == type) { |
mbedAustin | 11:cada08fc8a70 | 544 | M2MBase* base_object = find_resource(base->name()); |
mbedAustin | 11:cada08fc8a70 | 545 | if(base_object) { |
mbedAustin | 11:cada08fc8a70 | 546 | M2MObject *object = (M2MObject*)base_object; |
mbedAustin | 11:cada08fc8a70 | 547 | int slash_found = resource_name.find_last_of('/'); |
mbedAustin | 11:cada08fc8a70 | 548 | // Object instance validty checks done in upper level, no need for error handling |
mbedAustin | 11:cada08fc8a70 | 549 | if(slash_found != -1) { |
mbedAustin | 11:cada08fc8a70 | 550 | String object_name; |
mbedAustin | 11:cada08fc8a70 | 551 | object_name = resource_name.substr(slash_found + 1, resource_name.length()); |
mbedAustin | 11:cada08fc8a70 | 552 | if (object->remove_object_instance(strtoul( |
mbedAustin | 11:cada08fc8a70 | 553 | object_name.c_str(), NULL, 10))) { |
mbedAustin | 11:cada08fc8a70 | 554 | msg_code = COAP_MSG_CODE_RESPONSE_DELETED; |
mbedAustin | 11:cada08fc8a70 | 555 | } |
mbedAustin | 11:cada08fc8a70 | 556 | } |
mbedAustin | 11:cada08fc8a70 | 557 | } |
mbedAustin | 11:cada08fc8a70 | 558 | } else { |
mbedAustin | 11:cada08fc8a70 | 559 | msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00 |
mbedAustin | 11:cada08fc8a70 | 560 | } |
mbedAustin | 11:cada08fc8a70 | 561 | } |
mbedAustin | 11:cada08fc8a70 | 562 | } else { |
mbedAustin | 11:cada08fc8a70 | 563 | tr_debug("M2MNsdlInterface::resource_callback() - Resource NOT FOUND"); |
mbedAustin | 11:cada08fc8a70 | 564 | msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00 |
mbedAustin | 11:cada08fc8a70 | 565 | } |
mbedAustin | 11:cada08fc8a70 | 566 | if(!coap_response) { |
mbedAustin | 11:cada08fc8a70 | 567 | coap_response = sn_nsdl_build_response(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 568 | received_coap_header, |
mbedAustin | 11:cada08fc8a70 | 569 | msg_code); |
mbedAustin | 11:cada08fc8a70 | 570 | } |
mbedAustin | 11:cada08fc8a70 | 571 | if(coap_response) { |
mbedAustin | 11:cada08fc8a70 | 572 | tr_debug("M2MNsdlInterface::resource_callback() - send CoAP response"); |
mbedAustin | 11:cada08fc8a70 | 573 | (sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response) == 0) ? result = 0 : result = 1; |
mbedAustin | 11:cada08fc8a70 | 574 | if(coap_response->payload_ptr) { |
mbedAustin | 11:cada08fc8a70 | 575 | free(coap_response->payload_ptr); |
mbedAustin | 11:cada08fc8a70 | 576 | coap_response->payload_ptr = NULL; |
mbedAustin | 11:cada08fc8a70 | 577 | } |
mbedAustin | 11:cada08fc8a70 | 578 | sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response); |
mbedAustin | 11:cada08fc8a70 | 579 | } |
mbedAustin | 11:cada08fc8a70 | 580 | return result; |
mbedAustin | 11:cada08fc8a70 | 581 | } |
mbedAustin | 11:cada08fc8a70 | 582 | |
mbedAustin | 11:cada08fc8a70 | 583 | void M2MNsdlInterface::bootstrap_done_callback(sn_nsdl_oma_server_info_t *server_info) |
mbedAustin | 11:cada08fc8a70 | 584 | { |
mbedAustin | 11:cada08fc8a70 | 585 | tr_debug("M2MNsdlInterface::bootstrap_done_callback()"); |
mbedAustin | 11:cada08fc8a70 | 586 | _bootstrap_id = 0; |
mbedAustin | 11:cada08fc8a70 | 587 | M2MSecurity* security = NULL; |
mbedAustin | 11:cada08fc8a70 | 588 | if(server_info && server_info->omalw_address_ptr->addr_ptr) { |
mbedAustin | 11:cada08fc8a70 | 589 | security = new M2MSecurity(M2MSecurity::M2MServer); |
mbedAustin | 11:cada08fc8a70 | 590 | uint16_t port = server_info->omalw_address_ptr->port; |
mbedAustin | 11:cada08fc8a70 | 591 | char *buffer = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 592 | snprintf(buffer, 20,"%d",port); |
mbedAustin | 11:cada08fc8a70 | 593 | String server_uri(COAP); |
mbedAustin | 11:cada08fc8a70 | 594 | String server_address; |
mbedAustin | 11:cada08fc8a70 | 595 | //TODO: currently only supports IPV4 Mapping, fix to support IPV6 as well |
mbedAustin | 11:cada08fc8a70 | 596 | if(SN_NSDL_ADDRESS_TYPE_IPV4 == server_info->omalw_address_ptr->type) { |
mbedAustin | 11:cada08fc8a70 | 597 | int val = 0; |
mbedAustin | 11:cada08fc8a70 | 598 | for(int index = 0; index < server_info->omalw_address_ptr->addr_len; index++) { |
mbedAustin | 11:cada08fc8a70 | 599 | char *server_buffer = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 600 | val = (int)server_info->omalw_address_ptr->addr_ptr[index]; |
mbedAustin | 11:cada08fc8a70 | 601 | snprintf(server_buffer, 20,"%d",val); |
mbedAustin | 11:cada08fc8a70 | 602 | server_address +=String(server_buffer); |
mbedAustin | 11:cada08fc8a70 | 603 | |
mbedAustin | 11:cada08fc8a70 | 604 | memory_free(server_buffer); |
mbedAustin | 11:cada08fc8a70 | 605 | |
mbedAustin | 11:cada08fc8a70 | 606 | if(index < server_info->omalw_address_ptr->addr_len-1) { |
mbedAustin | 11:cada08fc8a70 | 607 | server_address += String("."); |
mbedAustin | 11:cada08fc8a70 | 608 | } |
mbedAustin | 11:cada08fc8a70 | 609 | } |
mbedAustin | 11:cada08fc8a70 | 610 | |
mbedAustin | 11:cada08fc8a70 | 611 | tr_debug("M2MNsdlInterface::bootstrap_done_callback - IPv4 Server address received %s", server_address.c_str()); |
mbedAustin | 11:cada08fc8a70 | 612 | } else if(SN_NSDL_ADDRESS_TYPE_HOSTNAME == server_info->omalw_address_ptr->type) { |
mbedAustin | 11:cada08fc8a70 | 613 | char *hostname = (char*)memory_alloc(server_info->omalw_address_ptr->addr_len); |
mbedAustin | 11:cada08fc8a70 | 614 | if(hostname) { |
mbedAustin | 11:cada08fc8a70 | 615 | memset(hostname, 0, server_info->omalw_address_ptr->addr_len); |
mbedAustin | 11:cada08fc8a70 | 616 | memcpy(hostname, |
mbedAustin | 11:cada08fc8a70 | 617 | server_info->omalw_address_ptr->addr_ptr, |
mbedAustin | 11:cada08fc8a70 | 618 | server_info->omalw_address_ptr->addr_len); |
mbedAustin | 11:cada08fc8a70 | 619 | server_address += String(hostname); |
mbedAustin | 11:cada08fc8a70 | 620 | memory_free(hostname); |
mbedAustin | 11:cada08fc8a70 | 621 | hostname = NULL; |
mbedAustin | 11:cada08fc8a70 | 622 | tr_debug("M2MNsdlInterface::bootstrap_done_callback - Hostname Server address received %s", server_address.c_str()); |
mbedAustin | 11:cada08fc8a70 | 623 | } |
mbedAustin | 11:cada08fc8a70 | 624 | } else if(SN_NSDL_ADDRESS_TYPE_IPV6 == server_info->omalw_address_ptr->type) { |
mbedAustin | 11:cada08fc8a70 | 625 | char ipv6_address[40]; |
mbedAustin | 11:cada08fc8a70 | 626 | ip6tos(server_info->omalw_address_ptr->addr_ptr, ipv6_address); |
mbedAustin | 11:cada08fc8a70 | 627 | server_address += String(ipv6_address); |
mbedAustin | 11:cada08fc8a70 | 628 | tr_debug("M2MNsdlInterface::bootstrap_done_callback - IPv6 Server address received %s", server_address.c_str()); |
mbedAustin | 11:cada08fc8a70 | 629 | } |
mbedAustin | 11:cada08fc8a70 | 630 | |
mbedAustin | 11:cada08fc8a70 | 631 | server_uri += server_address; |
mbedAustin | 11:cada08fc8a70 | 632 | server_uri +=String(":"); |
mbedAustin | 11:cada08fc8a70 | 633 | server_uri += String(buffer); |
mbedAustin | 11:cada08fc8a70 | 634 | |
mbedAustin | 11:cada08fc8a70 | 635 | memory_free(buffer); |
mbedAustin | 11:cada08fc8a70 | 636 | security->set_resource_value(M2MSecurity::M2MServerUri, server_uri); |
mbedAustin | 11:cada08fc8a70 | 637 | security->set_resource_value(M2MSecurity::BootstrapServer, 0); |
mbedAustin | 11:cada08fc8a70 | 638 | |
mbedAustin | 11:cada08fc8a70 | 639 | M2MSecurity::SecurityModeType security_mode = M2MSecurity::SecurityNotSet; |
mbedAustin | 11:cada08fc8a70 | 640 | |
mbedAustin | 11:cada08fc8a70 | 641 | switch(server_info->omalw_server_security) { |
mbedAustin | 11:cada08fc8a70 | 642 | case SEC_NOT_SET: |
mbedAustin | 11:cada08fc8a70 | 643 | security_mode = M2MSecurity::SecurityNotSet; |
mbedAustin | 11:cada08fc8a70 | 644 | break; |
mbedAustin | 11:cada08fc8a70 | 645 | case PSK: |
mbedAustin | 11:cada08fc8a70 | 646 | //Not supported at the moment |
mbedAustin | 11:cada08fc8a70 | 647 | break; |
mbedAustin | 11:cada08fc8a70 | 648 | case RPK: |
mbedAustin | 11:cada08fc8a70 | 649 | //Not supported at the moment |
mbedAustin | 11:cada08fc8a70 | 650 | break; |
mbedAustin | 11:cada08fc8a70 | 651 | case CERTIFICATE: |
mbedAustin | 11:cada08fc8a70 | 652 | security_mode = M2MSecurity::Certificate; |
mbedAustin | 11:cada08fc8a70 | 653 | break; |
mbedAustin | 11:cada08fc8a70 | 654 | case NO_SEC: |
mbedAustin | 11:cada08fc8a70 | 655 | security_mode = M2MSecurity::NoSecurity; |
mbedAustin | 11:cada08fc8a70 | 656 | break; |
mbedAustin | 11:cada08fc8a70 | 657 | } |
mbedAustin | 11:cada08fc8a70 | 658 | security->set_resource_value(M2MSecurity::SecurityMode,security_mode); |
mbedAustin | 11:cada08fc8a70 | 659 | |
mbedAustin | 11:cada08fc8a70 | 660 | //TODO: This is mandatory parameter for LWM2M server, |
mbedAustin | 11:cada08fc8a70 | 661 | // why is it missing from nsdl-c API ? |
mbedAustin | 11:cada08fc8a70 | 662 | security->set_resource_value(M2MSecurity::ShortServerID,1); |
mbedAustin | 11:cada08fc8a70 | 663 | |
mbedAustin | 11:cada08fc8a70 | 664 | // Check certiticates only if the mode is Certificate |
mbedAustin | 11:cada08fc8a70 | 665 | // else it is in NoSecurity Mode, Psk and Rsk are not supported. |
mbedAustin | 11:cada08fc8a70 | 666 | if(M2MSecurity::Certificate == security_mode) { |
mbedAustin | 11:cada08fc8a70 | 667 | omalw_certificate_list_t *certificates = sn_nsdl_get_certificates(_nsdl_handle); |
mbedAustin | 11:cada08fc8a70 | 668 | if(certificates) { |
mbedAustin | 11:cada08fc8a70 | 669 | security->set_resource_value(M2MSecurity::ServerPublicKey,certificates->certificate_ptr[0],certificates->certificate_len[0]); |
mbedAustin | 11:cada08fc8a70 | 670 | security->set_resource_value(M2MSecurity::PublicKey,certificates->certificate_ptr[1],certificates->certificate_len[1]); |
mbedAustin | 11:cada08fc8a70 | 671 | security->set_resource_value(M2MSecurity::Secretkey,certificates->own_private_key_ptr,certificates->own_private_key_len); |
mbedAustin | 11:cada08fc8a70 | 672 | } else { |
mbedAustin | 11:cada08fc8a70 | 673 | // Mode is certificate but certificates are missing so its bootstrap error. |
mbedAustin | 11:cada08fc8a70 | 674 | delete security; |
mbedAustin | 11:cada08fc8a70 | 675 | security = NULL; |
mbedAustin | 11:cada08fc8a70 | 676 | } |
mbedAustin | 11:cada08fc8a70 | 677 | } |
mbedAustin | 11:cada08fc8a70 | 678 | } |
mbedAustin | 11:cada08fc8a70 | 679 | if(security) { |
mbedAustin | 11:cada08fc8a70 | 680 | tr_debug("M2MNsdlInterface::bootstrap_done_callback - bootstrap_done"); |
mbedAustin | 11:cada08fc8a70 | 681 | // Inform that bootstrap is done and LWM2M server object is available. |
mbedAustin | 11:cada08fc8a70 | 682 | _observer.bootstrap_done(security); |
mbedAustin | 11:cada08fc8a70 | 683 | } else { |
mbedAustin | 11:cada08fc8a70 | 684 | tr_error("M2MNsdlInterface::bootstrap_done_callback - bootstrap_error"); |
mbedAustin | 11:cada08fc8a70 | 685 | // Bootstrap error inform to the application. |
mbedAustin | 11:cada08fc8a70 | 686 | _observer.bootstrap_error(); |
mbedAustin | 11:cada08fc8a70 | 687 | } |
mbedAustin | 11:cada08fc8a70 | 688 | } |
mbedAustin | 11:cada08fc8a70 | 689 | |
mbedAustin | 11:cada08fc8a70 | 690 | bool M2MNsdlInterface::process_received_data(uint8_t *data, |
mbedAustin | 11:cada08fc8a70 | 691 | uint16_t data_size, |
mbedAustin | 11:cada08fc8a70 | 692 | sn_nsdl_addr_s *address) |
mbedAustin | 11:cada08fc8a70 | 693 | { |
mbedAustin | 11:cada08fc8a70 | 694 | tr_debug("M2MNsdlInterface::process_received_data( data size %d)", data_size); |
mbedAustin | 11:cada08fc8a70 | 695 | return (0 == sn_nsdl_process_coap(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 696 | data, |
mbedAustin | 11:cada08fc8a70 | 697 | data_size, |
mbedAustin | 11:cada08fc8a70 | 698 | address)) ? true : false; |
mbedAustin | 11:cada08fc8a70 | 699 | } |
mbedAustin | 11:cada08fc8a70 | 700 | |
mbedAustin | 11:cada08fc8a70 | 701 | void M2MNsdlInterface::stop_timers() |
mbedAustin | 11:cada08fc8a70 | 702 | { |
mbedAustin | 11:cada08fc8a70 | 703 | tr_debug("M2MNsdlInterface::stop_timers()"); |
mbedAustin | 11:cada08fc8a70 | 704 | if(_registration_timer) { |
mbedAustin | 11:cada08fc8a70 | 705 | _registration_timer->stop_timer(); |
mbedAustin | 11:cada08fc8a70 | 706 | } |
mbedAustin | 11:cada08fc8a70 | 707 | } |
mbedAustin | 11:cada08fc8a70 | 708 | |
mbedAustin | 11:cada08fc8a70 | 709 | void M2MNsdlInterface::timer_expired(M2MTimerObserver::Type type) |
mbedAustin | 11:cada08fc8a70 | 710 | { |
mbedAustin | 11:cada08fc8a70 | 711 | if(M2MTimerObserver::NsdlExecution == type) { |
mbedAustin | 11:cada08fc8a70 | 712 | sn_nsdl_exec(_nsdl_handle, _counter_for_nsdl); |
mbedAustin | 11:cada08fc8a70 | 713 | _counter_for_nsdl++; |
mbedAustin | 11:cada08fc8a70 | 714 | } else if(M2MTimerObserver::Registration == type) { |
mbedAustin | 11:cada08fc8a70 | 715 | tr_debug("M2MNsdlInterface::timer_expired - M2MTimerObserver::Registration - Send update registration"); |
mbedAustin | 11:cada08fc8a70 | 716 | send_update_registration(); |
mbedAustin | 11:cada08fc8a70 | 717 | } |
mbedAustin | 11:cada08fc8a70 | 718 | } |
mbedAustin | 11:cada08fc8a70 | 719 | |
mbedAustin | 11:cada08fc8a70 | 720 | void M2MNsdlInterface::observation_to_be_sent(M2MBase *object) |
mbedAustin | 11:cada08fc8a70 | 721 | { |
mbedAustin | 11:cada08fc8a70 | 722 | tr_debug("M2MNsdlInterface::observation_to_be_sent()"); |
mbedAustin | 11:cada08fc8a70 | 723 | if(object) { |
mbedAustin | 11:cada08fc8a70 | 724 | M2MBase::BaseType type = object->base_type(); |
mbedAustin | 11:cada08fc8a70 | 725 | if(type == M2MBase::Object) { |
mbedAustin | 11:cada08fc8a70 | 726 | send_object_observation((M2MObject*)object); |
mbedAustin | 11:cada08fc8a70 | 727 | } else if(type == M2MBase::ObjectInstance) { |
mbedAustin | 11:cada08fc8a70 | 728 | send_object_instance_observation((M2MObjectInstance*)object); |
mbedAustin | 11:cada08fc8a70 | 729 | } else if(type == M2MBase::Resource) { |
mbedAustin | 11:cada08fc8a70 | 730 | send_resource_observation((M2MResource*)object); |
mbedAustin | 11:cada08fc8a70 | 731 | } |
mbedAustin | 11:cada08fc8a70 | 732 | } |
mbedAustin | 11:cada08fc8a70 | 733 | } |
mbedAustin | 11:cada08fc8a70 | 734 | |
mbedAustin | 11:cada08fc8a70 | 735 | void M2MNsdlInterface::resource_to_be_deleted(const String &resource_name) |
mbedAustin | 11:cada08fc8a70 | 736 | { |
mbedAustin | 11:cada08fc8a70 | 737 | tr_debug("M2MNsdlInterface::resource_to_be_deleted(resource_name %s)", resource_name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 738 | delete_nsdl_resource(resource_name); |
mbedAustin | 11:cada08fc8a70 | 739 | } |
mbedAustin | 11:cada08fc8a70 | 740 | |
mbedAustin | 11:cada08fc8a70 | 741 | void M2MNsdlInterface::value_updated(M2MBase *base, |
mbedAustin | 11:cada08fc8a70 | 742 | const String &object_name) |
mbedAustin | 11:cada08fc8a70 | 743 | { |
mbedAustin | 11:cada08fc8a70 | 744 | tr_debug("M2MNsdlInterface::value_updated()"); |
mbedAustin | 11:cada08fc8a70 | 745 | if(base) { |
mbedAustin | 11:cada08fc8a70 | 746 | switch(base->base_type()) { |
mbedAustin | 11:cada08fc8a70 | 747 | case M2MBase::Object: |
mbedAustin | 11:cada08fc8a70 | 748 | create_nsdl_object_structure((M2MObject*)base); |
mbedAustin | 11:cada08fc8a70 | 749 | break; |
mbedAustin | 11:cada08fc8a70 | 750 | case M2MBase::ObjectInstance: |
mbedAustin | 11:cada08fc8a70 | 751 | create_nsdl_object_instance_structure((M2MObjectInstance*)base); |
mbedAustin | 11:cada08fc8a70 | 752 | break; |
mbedAustin | 11:cada08fc8a70 | 753 | case M2MBase::Resource: { |
mbedAustin | 11:cada08fc8a70 | 754 | M2MResource* resource = (M2MResource*)base; |
mbedAustin | 11:cada08fc8a70 | 755 | create_nsdl_resource_structure(resource,object_name, |
mbedAustin | 11:cada08fc8a70 | 756 | resource->supports_multiple_instances()); |
mbedAustin | 11:cada08fc8a70 | 757 | } |
mbedAustin | 11:cada08fc8a70 | 758 | break; |
mbedAustin | 11:cada08fc8a70 | 759 | case M2MBase::ResourceInstance: { |
mbedAustin | 11:cada08fc8a70 | 760 | M2MResourceInstance* instance = (M2MResourceInstance*)base; |
mbedAustin | 11:cada08fc8a70 | 761 | create_nsdl_resource(instance,object_name); |
mbedAustin | 11:cada08fc8a70 | 762 | } |
mbedAustin | 11:cada08fc8a70 | 763 | break; |
mbedAustin | 11:cada08fc8a70 | 764 | } |
mbedAustin | 11:cada08fc8a70 | 765 | } |
mbedAustin | 11:cada08fc8a70 | 766 | _observer.value_updated(base); |
mbedAustin | 11:cada08fc8a70 | 767 | } |
mbedAustin | 11:cada08fc8a70 | 768 | |
mbedAustin | 11:cada08fc8a70 | 769 | void M2MNsdlInterface::remove_object(M2MBase *object) |
mbedAustin | 11:cada08fc8a70 | 770 | { |
mbedAustin | 11:cada08fc8a70 | 771 | tr_debug("M2MNsdlInterface::remove_object()"); |
mbedAustin | 11:cada08fc8a70 | 772 | M2MObject* rem_object = (M2MObject*)object; |
mbedAustin | 11:cada08fc8a70 | 773 | if(rem_object && !_object_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 774 | M2MObjectList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 775 | it = _object_list.begin(); |
mbedAustin | 11:cada08fc8a70 | 776 | int index = 0; |
mbedAustin | 11:cada08fc8a70 | 777 | for ( ; it != _object_list.end(); it++, index++ ) { |
mbedAustin | 11:cada08fc8a70 | 778 | if((*it) == rem_object) { |
mbedAustin | 11:cada08fc8a70 | 779 | _object_list.erase(index); |
mbedAustin | 11:cada08fc8a70 | 780 | break; |
mbedAustin | 11:cada08fc8a70 | 781 | } |
mbedAustin | 11:cada08fc8a70 | 782 | } |
mbedAustin | 11:cada08fc8a70 | 783 | } |
mbedAustin | 11:cada08fc8a70 | 784 | if(_object_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 785 | _object_list.clear(); |
mbedAustin | 11:cada08fc8a70 | 786 | } |
mbedAustin | 11:cada08fc8a70 | 787 | } |
mbedAustin | 11:cada08fc8a70 | 788 | |
mbedAustin | 11:cada08fc8a70 | 789 | bool M2MNsdlInterface::create_nsdl_object_structure(M2MObject *object) |
mbedAustin | 11:cada08fc8a70 | 790 | { |
mbedAustin | 11:cada08fc8a70 | 791 | tr_debug("M2MNsdlInterface::create_nsdl_object_structure()"); |
mbedAustin | 11:cada08fc8a70 | 792 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 793 | if(object) { |
mbedAustin | 11:cada08fc8a70 | 794 | //object->set_under_observation(false,this); |
mbedAustin | 11:cada08fc8a70 | 795 | M2MObjectInstanceList instance_list = object->instances(); |
mbedAustin | 11:cada08fc8a70 | 796 | tr_debug("M2MNsdlInterface::create_nsdl_object_structure - Object Instance count %d", instance_list.size()); |
mbedAustin | 11:cada08fc8a70 | 797 | if(!instance_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 798 | M2MObjectInstanceList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 799 | it = instance_list.begin(); |
mbedAustin | 11:cada08fc8a70 | 800 | for ( ; it != instance_list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 801 | // Create NSDL structure for all object instances inside |
mbedAustin | 11:cada08fc8a70 | 802 | success = create_nsdl_object_instance_structure(*it); |
mbedAustin | 11:cada08fc8a70 | 803 | } |
mbedAustin | 11:cada08fc8a70 | 804 | } |
mbedAustin | 11:cada08fc8a70 | 805 | } |
mbedAustin | 11:cada08fc8a70 | 806 | if((object->operation() != M2MBase::NOT_ALLOWED)) { |
mbedAustin | 11:cada08fc8a70 | 807 | success = create_nsdl_resource(object,object->name()); |
mbedAustin | 11:cada08fc8a70 | 808 | } |
mbedAustin | 11:cada08fc8a70 | 809 | return success; |
mbedAustin | 11:cada08fc8a70 | 810 | } |
mbedAustin | 11:cada08fc8a70 | 811 | |
mbedAustin | 11:cada08fc8a70 | 812 | bool M2MNsdlInterface::create_nsdl_object_instance_structure(M2MObjectInstance *object_instance) |
mbedAustin | 11:cada08fc8a70 | 813 | { |
mbedAustin | 11:cada08fc8a70 | 814 | tr_debug("M2MNsdlInterface::create_nsdl_object_instance_structure()"); |
mbedAustin | 11:cada08fc8a70 | 815 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 816 | if( object_instance) { |
mbedAustin | 11:cada08fc8a70 | 817 | |
mbedAustin | 11:cada08fc8a70 | 818 | char *inst_id = (char*)malloc(20); |
mbedAustin | 11:cada08fc8a70 | 819 | snprintf(inst_id, 20,"%d",object_instance->instance_id()); |
mbedAustin | 11:cada08fc8a70 | 820 | |
mbedAustin | 11:cada08fc8a70 | 821 | // Append object instance id to the object name. |
mbedAustin | 11:cada08fc8a70 | 822 | String object_name = object_instance->name(); |
mbedAustin | 11:cada08fc8a70 | 823 | object_name += String("/"); |
mbedAustin | 11:cada08fc8a70 | 824 | object_name += String(inst_id); |
mbedAustin | 11:cada08fc8a70 | 825 | free(inst_id); |
mbedAustin | 11:cada08fc8a70 | 826 | |
mbedAustin | 11:cada08fc8a70 | 827 | |
mbedAustin | 11:cada08fc8a70 | 828 | //object_instance->set_under_observation(false,this); |
mbedAustin | 11:cada08fc8a70 | 829 | |
mbedAustin | 11:cada08fc8a70 | 830 | M2MResourceList res_list = object_instance->resources(); |
mbedAustin | 11:cada08fc8a70 | 831 | tr_debug("M2MNsdlInterface::create_nsdl_object_instance_structure - ResourceBase count %d", res_list.size()); |
mbedAustin | 11:cada08fc8a70 | 832 | if(!res_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 833 | M2MResourceList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 834 | it = res_list.begin(); |
mbedAustin | 11:cada08fc8a70 | 835 | for ( ; it != res_list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 836 | // Create NSDL structure for all resources inside |
mbedAustin | 11:cada08fc8a70 | 837 | success = create_nsdl_resource_structure(*it,object_name, |
mbedAustin | 11:cada08fc8a70 | 838 | (*it)->supports_multiple_instances()); |
mbedAustin | 11:cada08fc8a70 | 839 | } |
mbedAustin | 11:cada08fc8a70 | 840 | } |
mbedAustin | 11:cada08fc8a70 | 841 | if(object_instance->operation() != M2MBase::NOT_ALLOWED) { |
mbedAustin | 11:cada08fc8a70 | 842 | success = create_nsdl_resource(object_instance,object_name); |
mbedAustin | 11:cada08fc8a70 | 843 | } |
mbedAustin | 11:cada08fc8a70 | 844 | } |
mbedAustin | 11:cada08fc8a70 | 845 | return success; |
mbedAustin | 11:cada08fc8a70 | 846 | } |
mbedAustin | 11:cada08fc8a70 | 847 | |
mbedAustin | 11:cada08fc8a70 | 848 | bool M2MNsdlInterface::create_nsdl_resource_structure(M2MResource *res, |
mbedAustin | 11:cada08fc8a70 | 849 | const String &object_name, |
mbedAustin | 11:cada08fc8a70 | 850 | bool multiple_instances) |
mbedAustin | 11:cada08fc8a70 | 851 | { |
mbedAustin | 11:cada08fc8a70 | 852 | tr_debug("M2MNsdlInterface::create_nsdl_resource_structure(object_name %s)", object_name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 853 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 854 | if(res) { |
mbedAustin | 11:cada08fc8a70 | 855 | // Append object name to the resource. |
mbedAustin | 11:cada08fc8a70 | 856 | // Take out the instance Id and append to the |
mbedAustin | 11:cada08fc8a70 | 857 | // resource name like "object/0/+ resource + / + 0" |
mbedAustin | 11:cada08fc8a70 | 858 | String res_name = object_name; |
mbedAustin | 11:cada08fc8a70 | 859 | res_name+= String("/"); |
mbedAustin | 11:cada08fc8a70 | 860 | res_name.append(res->name().c_str(),res->name().length()); |
mbedAustin | 11:cada08fc8a70 | 861 | |
mbedAustin | 11:cada08fc8a70 | 862 | // if there are multiple instances supported |
mbedAustin | 11:cada08fc8a70 | 863 | // then add instance Id into creating resource path |
mbedAustin | 11:cada08fc8a70 | 864 | // else normal /object_id/object_instance/resource_id format. |
mbedAustin | 11:cada08fc8a70 | 865 | if(multiple_instances) { |
mbedAustin | 11:cada08fc8a70 | 866 | M2MResourceInstanceList res_list = res->resource_instances(); |
mbedAustin | 11:cada08fc8a70 | 867 | tr_debug("M2MNsdlInterface::create_nsdl_resource_structure - ResourceInstance count %d", res_list.size()); |
mbedAustin | 11:cada08fc8a70 | 868 | if(!res_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 869 | M2MResourceInstanceList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 870 | it = res_list.begin(); |
mbedAustin | 11:cada08fc8a70 | 871 | for ( ; it != res_list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 872 | String inst_name = res_name; |
mbedAustin | 11:cada08fc8a70 | 873 | // Create NSDL structure for all resources inside |
mbedAustin | 11:cada08fc8a70 | 874 | char *inst_id = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 875 | snprintf(inst_id, 20,"%d",(*it)->instance_id()); |
mbedAustin | 11:cada08fc8a70 | 876 | inst_name+= String("/") ; |
mbedAustin | 11:cada08fc8a70 | 877 | inst_name+= String(inst_id); |
mbedAustin | 11:cada08fc8a70 | 878 | |
mbedAustin | 11:cada08fc8a70 | 879 | memory_free(inst_id); |
mbedAustin | 11:cada08fc8a70 | 880 | |
mbedAustin | 11:cada08fc8a70 | 881 | success = create_nsdl_resource((*it),inst_name); |
mbedAustin | 11:cada08fc8a70 | 882 | } |
mbedAustin | 11:cada08fc8a70 | 883 | // Register the main Resource as well along with ResourceInstances |
mbedAustin | 11:cada08fc8a70 | 884 | success = create_nsdl_resource(res,res_name); |
mbedAustin | 11:cada08fc8a70 | 885 | } |
mbedAustin | 11:cada08fc8a70 | 886 | } else { |
mbedAustin | 11:cada08fc8a70 | 887 | tr_debug("M2MNsdlInterface::create_nsdl_resource_structure - res_name %s", res_name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 888 | success = create_nsdl_resource(res,res_name); |
mbedAustin | 11:cada08fc8a70 | 889 | } |
mbedAustin | 11:cada08fc8a70 | 890 | } |
mbedAustin | 11:cada08fc8a70 | 891 | return success; |
mbedAustin | 11:cada08fc8a70 | 892 | } |
mbedAustin | 11:cada08fc8a70 | 893 | |
mbedAustin | 11:cada08fc8a70 | 894 | bool M2MNsdlInterface::create_nsdl_resource(M2MBase *base, const String &name) |
mbedAustin | 11:cada08fc8a70 | 895 | { |
mbedAustin | 11:cada08fc8a70 | 896 | tr_debug("M2MNsdlInterface::create_nsdl_resource(name %s)", name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 897 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 898 | uint8_t* buffer = 0; |
mbedAustin | 11:cada08fc8a70 | 899 | uint32_t length = 0; |
mbedAustin | 11:cada08fc8a70 | 900 | |
mbedAustin | 11:cada08fc8a70 | 901 | // Create the NSDL Resource Pointer... |
mbedAustin | 11:cada08fc8a70 | 902 | if(base) { |
mbedAustin | 11:cada08fc8a70 | 903 | sn_nsdl_resource_info_s* resource = sn_nsdl_get_resource(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 904 | name.length(), |
mbedAustin | 11:cada08fc8a70 | 905 | (uint8_t*)name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 906 | if(resource) { |
mbedAustin | 11:cada08fc8a70 | 907 | success = true; |
mbedAustin | 11:cada08fc8a70 | 908 | if(resource->mode == SN_GRS_STATIC) { |
mbedAustin | 11:cada08fc8a70 | 909 | if((M2MBase::Resource == base->base_type() || |
mbedAustin | 11:cada08fc8a70 | 910 | M2MBase::ResourceInstance == base->base_type()) && |
mbedAustin | 11:cada08fc8a70 | 911 | M2MBase::Static == base->mode()) { |
mbedAustin | 11:cada08fc8a70 | 912 | M2MResourceInstance *res = (M2MResourceInstance*)base; |
mbedAustin | 11:cada08fc8a70 | 913 | res->get_value(buffer,length); |
mbedAustin | 11:cada08fc8a70 | 914 | if(resource->resource) { |
mbedAustin | 11:cada08fc8a70 | 915 | memory_free(resource->resource); |
mbedAustin | 11:cada08fc8a70 | 916 | } |
mbedAustin | 11:cada08fc8a70 | 917 | resource->resource = buffer; |
mbedAustin | 11:cada08fc8a70 | 918 | resource->resourcelen = length; |
mbedAustin | 11:cada08fc8a70 | 919 | sn_nsdl_update_resource(_nsdl_handle,resource); |
mbedAustin | 11:cada08fc8a70 | 920 | } |
mbedAustin | 11:cada08fc8a70 | 921 | } |
mbedAustin | 11:cada08fc8a70 | 922 | // Update Resource access everytime for existing resource. |
mbedAustin | 11:cada08fc8a70 | 923 | resource->access = (sn_grs_resource_acl_e)base->operation(); |
mbedAustin | 11:cada08fc8a70 | 924 | } else if(_resource) { |
mbedAustin | 11:cada08fc8a70 | 925 | base->set_under_observation(false,this); |
mbedAustin | 11:cada08fc8a70 | 926 | //TODO: implement access control |
mbedAustin | 11:cada08fc8a70 | 927 | // Currently complete access is given |
mbedAustin | 11:cada08fc8a70 | 928 | _resource->access = (sn_grs_resource_acl_e)base->operation(); |
mbedAustin | 11:cada08fc8a70 | 929 | |
mbedAustin | 11:cada08fc8a70 | 930 | if((M2MBase::Resource == base->base_type() || |
mbedAustin | 11:cada08fc8a70 | 931 | M2MBase::ResourceInstance == base->base_type()) && |
mbedAustin | 11:cada08fc8a70 | 932 | M2MBase::Static == base->mode()) { |
mbedAustin | 11:cada08fc8a70 | 933 | M2MResourceInstance *res = (M2MResourceInstance*)base; |
mbedAustin | 11:cada08fc8a70 | 934 | // Static resource is updated |
mbedAustin | 11:cada08fc8a70 | 935 | _resource->mode = SN_GRS_STATIC; |
mbedAustin | 11:cada08fc8a70 | 936 | |
mbedAustin | 11:cada08fc8a70 | 937 | res->get_value(buffer,length); |
mbedAustin | 11:cada08fc8a70 | 938 | _resource->resource = buffer; |
mbedAustin | 11:cada08fc8a70 | 939 | _resource->resourcelen = length; |
mbedAustin | 11:cada08fc8a70 | 940 | } |
mbedAustin | 11:cada08fc8a70 | 941 | |
mbedAustin | 11:cada08fc8a70 | 942 | if(M2MBase::Dynamic == base->mode()){ |
mbedAustin | 11:cada08fc8a70 | 943 | // Dynamic resource is updated |
mbedAustin | 11:cada08fc8a70 | 944 | _resource->mode = SN_GRS_DYNAMIC; |
mbedAustin | 11:cada08fc8a70 | 945 | _resource->sn_grs_dyn_res_callback = __nsdl_c_callback; |
mbedAustin | 11:cada08fc8a70 | 946 | } else { |
mbedAustin | 11:cada08fc8a70 | 947 | _resource->mode = SN_GRS_DIRECTORY; |
mbedAustin | 11:cada08fc8a70 | 948 | } |
mbedAustin | 11:cada08fc8a70 | 949 | |
mbedAustin | 11:cada08fc8a70 | 950 | if( _resource->path != NULL ){ |
mbedAustin | 11:cada08fc8a70 | 951 | memory_free(_resource->path); |
mbedAustin | 11:cada08fc8a70 | 952 | _resource->path = NULL; |
mbedAustin | 11:cada08fc8a70 | 953 | } |
mbedAustin | 11:cada08fc8a70 | 954 | if(name.length() > 0 ){ |
mbedAustin | 11:cada08fc8a70 | 955 | _resource->path = ((uint8_t*)memory_alloc(name.length()+1)); |
mbedAustin | 11:cada08fc8a70 | 956 | if(_resource->path) { |
mbedAustin | 11:cada08fc8a70 | 957 | memset(_resource->path, 0, name.length()+1); |
mbedAustin | 11:cada08fc8a70 | 958 | memcpy(_resource->path, (uint8_t*)name.c_str(), name.length()); |
mbedAustin | 11:cada08fc8a70 | 959 | _resource->pathlen = name.length(); |
mbedAustin | 11:cada08fc8a70 | 960 | } |
mbedAustin | 11:cada08fc8a70 | 961 | } |
mbedAustin | 11:cada08fc8a70 | 962 | |
mbedAustin | 11:cada08fc8a70 | 963 | if(!base->resource_type().empty() && _resource->resource_parameters_ptr) { |
mbedAustin | 11:cada08fc8a70 | 964 | _resource->resource_parameters_ptr->resource_type_ptr = |
mbedAustin | 11:cada08fc8a70 | 965 | ((uint8_t*)memory_alloc(base->resource_type().length()+1)); |
mbedAustin | 11:cada08fc8a70 | 966 | if(_resource->resource_parameters_ptr->resource_type_ptr) { |
mbedAustin | 11:cada08fc8a70 | 967 | memset(_resource->resource_parameters_ptr->resource_type_ptr, |
mbedAustin | 11:cada08fc8a70 | 968 | 0, base->resource_type().length()+1); |
mbedAustin | 11:cada08fc8a70 | 969 | memcpy(_resource->resource_parameters_ptr->resource_type_ptr, |
mbedAustin | 11:cada08fc8a70 | 970 | (uint8_t*)base->resource_type().c_str(), |
mbedAustin | 11:cada08fc8a70 | 971 | base->resource_type().length()); |
mbedAustin | 11:cada08fc8a70 | 972 | _resource->resource_parameters_ptr->resource_type_len = |
mbedAustin | 11:cada08fc8a70 | 973 | base->resource_type().length(); |
mbedAustin | 11:cada08fc8a70 | 974 | } |
mbedAustin | 11:cada08fc8a70 | 975 | } |
mbedAustin | 11:cada08fc8a70 | 976 | if(!base->interface_description().empty() && _resource->resource_parameters_ptr) { |
mbedAustin | 11:cada08fc8a70 | 977 | _resource->resource_parameters_ptr->interface_description_ptr = |
mbedAustin | 11:cada08fc8a70 | 978 | ((uint8_t*)memory_alloc(base->interface_description().length()+1)); |
mbedAustin | 11:cada08fc8a70 | 979 | if(_resource->resource_parameters_ptr->interface_description_ptr) { |
mbedAustin | 11:cada08fc8a70 | 980 | memset(_resource->resource_parameters_ptr->interface_description_ptr, |
mbedAustin | 11:cada08fc8a70 | 981 | 0, base->interface_description().length()+1); |
mbedAustin | 11:cada08fc8a70 | 982 | memcpy(_resource->resource_parameters_ptr->interface_description_ptr, |
mbedAustin | 11:cada08fc8a70 | 983 | (uint8_t*)base->interface_description().c_str(), |
mbedAustin | 11:cada08fc8a70 | 984 | base->interface_description().length()); |
mbedAustin | 11:cada08fc8a70 | 985 | _resource->resource_parameters_ptr->interface_description_len = |
mbedAustin | 11:cada08fc8a70 | 986 | base->interface_description().length(); |
mbedAustin | 11:cada08fc8a70 | 987 | } |
mbedAustin | 11:cada08fc8a70 | 988 | } |
mbedAustin | 11:cada08fc8a70 | 989 | if(_resource->resource_parameters_ptr) { |
mbedAustin | 11:cada08fc8a70 | 990 | _resource->resource_parameters_ptr->coap_content_type = base->coap_content_type(); |
mbedAustin | 11:cada08fc8a70 | 991 | _resource->resource_parameters_ptr->observable = (uint8_t)base->is_observable(); |
mbedAustin | 11:cada08fc8a70 | 992 | } |
mbedAustin | 11:cada08fc8a70 | 993 | |
mbedAustin | 11:cada08fc8a70 | 994 | int8_t result = sn_nsdl_create_resource(_nsdl_handle,_resource); |
mbedAustin | 11:cada08fc8a70 | 995 | tr_debug("M2MNsdlInterface::create_nsdl_resource - Creating in NSDL-C result %d", result); |
mbedAustin | 11:cada08fc8a70 | 996 | |
mbedAustin | 11:cada08fc8a70 | 997 | // Either the resource is created or it already |
mbedAustin | 11:cada08fc8a70 | 998 | // exists , then result is success. |
mbedAustin | 11:cada08fc8a70 | 999 | if (result == 0 || |
mbedAustin | 11:cada08fc8a70 | 1000 | result == -2){ |
mbedAustin | 11:cada08fc8a70 | 1001 | success = true; |
mbedAustin | 11:cada08fc8a70 | 1002 | } |
mbedAustin | 11:cada08fc8a70 | 1003 | |
mbedAustin | 11:cada08fc8a70 | 1004 | if(_resource->path) { |
mbedAustin | 11:cada08fc8a70 | 1005 | memory_free(_resource->path); |
mbedAustin | 11:cada08fc8a70 | 1006 | } |
mbedAustin | 11:cada08fc8a70 | 1007 | if(_resource->resource_parameters_ptr->resource_type_ptr){ |
mbedAustin | 11:cada08fc8a70 | 1008 | memory_free(_resource->resource_parameters_ptr->resource_type_ptr); |
mbedAustin | 11:cada08fc8a70 | 1009 | } |
mbedAustin | 11:cada08fc8a70 | 1010 | if(_resource->resource_parameters_ptr->interface_description_ptr){ |
mbedAustin | 11:cada08fc8a70 | 1011 | memory_free(_resource->resource_parameters_ptr->interface_description_ptr); |
mbedAustin | 11:cada08fc8a70 | 1012 | } |
mbedAustin | 11:cada08fc8a70 | 1013 | |
mbedAustin | 11:cada08fc8a70 | 1014 | //Clear up the filled resource to fill up new resource. |
mbedAustin | 11:cada08fc8a70 | 1015 | clear_resource(_resource); |
mbedAustin | 11:cada08fc8a70 | 1016 | |
mbedAustin | 11:cada08fc8a70 | 1017 | if(success) { |
mbedAustin | 11:cada08fc8a70 | 1018 | base->set_under_observation(false,this); |
mbedAustin | 11:cada08fc8a70 | 1019 | } |
mbedAustin | 11:cada08fc8a70 | 1020 | } |
mbedAustin | 11:cada08fc8a70 | 1021 | } |
mbedAustin | 11:cada08fc8a70 | 1022 | if(buffer) { |
mbedAustin | 11:cada08fc8a70 | 1023 | free(buffer); |
mbedAustin | 11:cada08fc8a70 | 1024 | } |
mbedAustin | 11:cada08fc8a70 | 1025 | return success; |
mbedAustin | 11:cada08fc8a70 | 1026 | } |
mbedAustin | 11:cada08fc8a70 | 1027 | |
mbedAustin | 11:cada08fc8a70 | 1028 | // convenience method to get the URI from its buffer field... |
mbedAustin | 11:cada08fc8a70 | 1029 | String M2MNsdlInterface::coap_to_string(uint8_t *coap_data,int coap_data_length) |
mbedAustin | 11:cada08fc8a70 | 1030 | { |
mbedAustin | 11:cada08fc8a70 | 1031 | String value = ""; |
mbedAustin | 11:cada08fc8a70 | 1032 | if (coap_data != NULL && coap_data_length > 0) { |
mbedAustin | 11:cada08fc8a70 | 1033 | char buf[256+1]; |
mbedAustin | 11:cada08fc8a70 | 1034 | memset(buf,0,256+1); |
mbedAustin | 11:cada08fc8a70 | 1035 | memcpy(buf,(char *)coap_data,coap_data_length); |
mbedAustin | 11:cada08fc8a70 | 1036 | value = String(buf); |
mbedAustin | 11:cada08fc8a70 | 1037 | } |
mbedAustin | 11:cada08fc8a70 | 1038 | return value; |
mbedAustin | 11:cada08fc8a70 | 1039 | } |
mbedAustin | 11:cada08fc8a70 | 1040 | |
mbedAustin | 11:cada08fc8a70 | 1041 | uint64_t M2MNsdlInterface::registration_time() |
mbedAustin | 11:cada08fc8a70 | 1042 | { |
mbedAustin | 11:cada08fc8a70 | 1043 | uint64_t value = 0; |
mbedAustin | 11:cada08fc8a70 | 1044 | if(_endpoint->lifetime_ptr) { |
mbedAustin | 11:cada08fc8a70 | 1045 | value = atol((const char*)_endpoint->lifetime_ptr); |
mbedAustin | 11:cada08fc8a70 | 1046 | } |
mbedAustin | 11:cada08fc8a70 | 1047 | |
mbedAustin | 11:cada08fc8a70 | 1048 | if(value >= OPTIMUM_LIFETIME) { |
mbedAustin | 11:cada08fc8a70 | 1049 | value = value - REDUCE_LIFETIME; |
mbedAustin | 11:cada08fc8a70 | 1050 | } else { |
mbedAustin | 11:cada08fc8a70 | 1051 | value = REDUCTION_FACTOR * value; |
mbedAustin | 11:cada08fc8a70 | 1052 | } |
mbedAustin | 11:cada08fc8a70 | 1053 | tr_debug("M2MNsdlInterface::registration_time - value (in seconds) %ld", value); |
mbedAustin | 11:cada08fc8a70 | 1054 | return value; |
mbedAustin | 11:cada08fc8a70 | 1055 | } |
mbedAustin | 11:cada08fc8a70 | 1056 | |
mbedAustin | 11:cada08fc8a70 | 1057 | M2MBase* M2MNsdlInterface::find_resource(const String &object_name) |
mbedAustin | 11:cada08fc8a70 | 1058 | { |
mbedAustin | 11:cada08fc8a70 | 1059 | M2MBase *object = NULL; |
mbedAustin | 11:cada08fc8a70 | 1060 | if(!_object_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 1061 | M2MObjectList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 1062 | it = _object_list.begin(); |
mbedAustin | 11:cada08fc8a70 | 1063 | for ( ; it != _object_list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 1064 | if((*it)->name() == object_name) { |
mbedAustin | 11:cada08fc8a70 | 1065 | object = (*it); |
mbedAustin | 11:cada08fc8a70 | 1066 | tr_debug("M2MNsdlInterface::find_resource(%s) found", object_name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 1067 | break; |
mbedAustin | 11:cada08fc8a70 | 1068 | } |
mbedAustin | 11:cada08fc8a70 | 1069 | object = find_resource((*it),object_name); |
mbedAustin | 11:cada08fc8a70 | 1070 | if(object != NULL) { |
mbedAustin | 11:cada08fc8a70 | 1071 | tr_debug("M2MNsdlInterface::find_resource(%s) found", object_name.c_str()); |
mbedAustin | 11:cada08fc8a70 | 1072 | break; |
mbedAustin | 11:cada08fc8a70 | 1073 | } |
mbedAustin | 11:cada08fc8a70 | 1074 | } |
mbedAustin | 11:cada08fc8a70 | 1075 | } |
mbedAustin | 11:cada08fc8a70 | 1076 | return object; |
mbedAustin | 11:cada08fc8a70 | 1077 | } |
mbedAustin | 11:cada08fc8a70 | 1078 | |
mbedAustin | 11:cada08fc8a70 | 1079 | M2MBase* M2MNsdlInterface::find_resource(const M2MObject *object, |
mbedAustin | 11:cada08fc8a70 | 1080 | const String &object_instance) |
mbedAustin | 11:cada08fc8a70 | 1081 | { |
mbedAustin | 11:cada08fc8a70 | 1082 | M2MBase *instance = NULL; |
mbedAustin | 11:cada08fc8a70 | 1083 | if(object) { |
mbedAustin | 11:cada08fc8a70 | 1084 | M2MObjectInstanceList list = object->instances(); |
mbedAustin | 11:cada08fc8a70 | 1085 | if(!list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 1086 | M2MObjectInstanceList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 1087 | it = list.begin(); |
mbedAustin | 11:cada08fc8a70 | 1088 | for ( ; it != list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 1089 | char *inst_id = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 1090 | snprintf(inst_id, 20,"%d",(*it)->instance_id()); |
mbedAustin | 11:cada08fc8a70 | 1091 | |
mbedAustin | 11:cada08fc8a70 | 1092 | // Append object instance id to the object name. |
mbedAustin | 11:cada08fc8a70 | 1093 | String name = (*it)->name(); |
mbedAustin | 11:cada08fc8a70 | 1094 | name+= String("/"); |
mbedAustin | 11:cada08fc8a70 | 1095 | name+= String(inst_id); |
mbedAustin | 11:cada08fc8a70 | 1096 | |
mbedAustin | 11:cada08fc8a70 | 1097 | memory_free(inst_id); |
mbedAustin | 11:cada08fc8a70 | 1098 | |
mbedAustin | 11:cada08fc8a70 | 1099 | if(name == object_instance){ |
mbedAustin | 11:cada08fc8a70 | 1100 | instance = (*it); |
mbedAustin | 11:cada08fc8a70 | 1101 | break; |
mbedAustin | 11:cada08fc8a70 | 1102 | } |
mbedAustin | 11:cada08fc8a70 | 1103 | instance = find_resource((*it),object_instance); |
mbedAustin | 11:cada08fc8a70 | 1104 | if(instance != NULL){ |
mbedAustin | 11:cada08fc8a70 | 1105 | break; |
mbedAustin | 11:cada08fc8a70 | 1106 | } |
mbedAustin | 11:cada08fc8a70 | 1107 | } |
mbedAustin | 11:cada08fc8a70 | 1108 | } |
mbedAustin | 11:cada08fc8a70 | 1109 | } |
mbedAustin | 11:cada08fc8a70 | 1110 | return instance; |
mbedAustin | 11:cada08fc8a70 | 1111 | } |
mbedAustin | 11:cada08fc8a70 | 1112 | |
mbedAustin | 11:cada08fc8a70 | 1113 | M2MBase* M2MNsdlInterface::find_resource(const M2MObjectInstance *object_instance, |
mbedAustin | 11:cada08fc8a70 | 1114 | const String &resource_instance) |
mbedAustin | 11:cada08fc8a70 | 1115 | { |
mbedAustin | 11:cada08fc8a70 | 1116 | M2MBase *instance = NULL; |
mbedAustin | 11:cada08fc8a70 | 1117 | if(object_instance) { |
mbedAustin | 11:cada08fc8a70 | 1118 | M2MResourceList list = object_instance->resources(); |
mbedAustin | 11:cada08fc8a70 | 1119 | if(!list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 1120 | M2MResourceList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 1121 | it = list.begin(); |
mbedAustin | 11:cada08fc8a70 | 1122 | for ( ; it != list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 1123 | String name = object_instance->name(); |
mbedAustin | 11:cada08fc8a70 | 1124 | char *obj_inst_id = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 1125 | snprintf(obj_inst_id, 20,"%d",object_instance->instance_id()); |
mbedAustin | 11:cada08fc8a70 | 1126 | |
mbedAustin | 11:cada08fc8a70 | 1127 | // Append object instance id to the object name. |
mbedAustin | 11:cada08fc8a70 | 1128 | name+= String("/"); |
mbedAustin | 11:cada08fc8a70 | 1129 | name+= String(obj_inst_id); |
mbedAustin | 11:cada08fc8a70 | 1130 | |
mbedAustin | 11:cada08fc8a70 | 1131 | memory_free(obj_inst_id); |
mbedAustin | 11:cada08fc8a70 | 1132 | |
mbedAustin | 11:cada08fc8a70 | 1133 | name+= String("/"); |
mbedAustin | 11:cada08fc8a70 | 1134 | name+= (*it)->name(); |
mbedAustin | 11:cada08fc8a70 | 1135 | |
mbedAustin | 11:cada08fc8a70 | 1136 | if(name == resource_instance) { |
mbedAustin | 11:cada08fc8a70 | 1137 | instance = *it; |
mbedAustin | 11:cada08fc8a70 | 1138 | break; |
mbedAustin | 11:cada08fc8a70 | 1139 | } else if((*it)->supports_multiple_instances()) { |
mbedAustin | 11:cada08fc8a70 | 1140 | instance = find_resource((*it),name, resource_instance); |
mbedAustin | 11:cada08fc8a70 | 1141 | if(instance != NULL){ |
mbedAustin | 11:cada08fc8a70 | 1142 | break; |
mbedAustin | 11:cada08fc8a70 | 1143 | } |
mbedAustin | 11:cada08fc8a70 | 1144 | } |
mbedAustin | 11:cada08fc8a70 | 1145 | } |
mbedAustin | 11:cada08fc8a70 | 1146 | } |
mbedAustin | 11:cada08fc8a70 | 1147 | } |
mbedAustin | 11:cada08fc8a70 | 1148 | return instance; |
mbedAustin | 11:cada08fc8a70 | 1149 | } |
mbedAustin | 11:cada08fc8a70 | 1150 | |
mbedAustin | 11:cada08fc8a70 | 1151 | M2MBase* M2MNsdlInterface::find_resource(const M2MResource *resource, |
mbedAustin | 11:cada08fc8a70 | 1152 | const String &object_name, |
mbedAustin | 11:cada08fc8a70 | 1153 | const String &resource_instance) |
mbedAustin | 11:cada08fc8a70 | 1154 | { |
mbedAustin | 11:cada08fc8a70 | 1155 | M2MBase *res = NULL; |
mbedAustin | 11:cada08fc8a70 | 1156 | if(resource) { |
mbedAustin | 11:cada08fc8a70 | 1157 | if(resource->supports_multiple_instances()) { |
mbedAustin | 11:cada08fc8a70 | 1158 | M2MResourceInstanceList list = resource->resource_instances(); |
mbedAustin | 11:cada08fc8a70 | 1159 | if(!list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 1160 | M2MResourceInstanceList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 1161 | it = list.begin(); |
mbedAustin | 11:cada08fc8a70 | 1162 | for ( ; it != list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 1163 | String name = object_name; |
mbedAustin | 11:cada08fc8a70 | 1164 | // if there are multiple instances supported |
mbedAustin | 11:cada08fc8a70 | 1165 | // then add instance Id into creating resource path |
mbedAustin | 11:cada08fc8a70 | 1166 | // else normal /object_id/object_instance/resource_id format. |
mbedAustin | 11:cada08fc8a70 | 1167 | |
mbedAustin | 11:cada08fc8a70 | 1168 | char *inst_id = (char*)memory_alloc(20); |
mbedAustin | 11:cada08fc8a70 | 1169 | snprintf(inst_id, 20,"%d",(*it)->instance_id()); |
mbedAustin | 11:cada08fc8a70 | 1170 | |
mbedAustin | 11:cada08fc8a70 | 1171 | name+= String("/") ; |
mbedAustin | 11:cada08fc8a70 | 1172 | name+= String(inst_id); |
mbedAustin | 11:cada08fc8a70 | 1173 | |
mbedAustin | 11:cada08fc8a70 | 1174 | memory_free(inst_id); |
mbedAustin | 11:cada08fc8a70 | 1175 | |
mbedAustin | 11:cada08fc8a70 | 1176 | if(name == resource_instance){ |
mbedAustin | 11:cada08fc8a70 | 1177 | res = (*it); |
mbedAustin | 11:cada08fc8a70 | 1178 | break; |
mbedAustin | 11:cada08fc8a70 | 1179 | } |
mbedAustin | 11:cada08fc8a70 | 1180 | } |
mbedAustin | 11:cada08fc8a70 | 1181 | } |
mbedAustin | 11:cada08fc8a70 | 1182 | } |
mbedAustin | 11:cada08fc8a70 | 1183 | } |
mbedAustin | 11:cada08fc8a70 | 1184 | return res; |
mbedAustin | 11:cada08fc8a70 | 1185 | } |
mbedAustin | 11:cada08fc8a70 | 1186 | |
mbedAustin | 11:cada08fc8a70 | 1187 | bool M2MNsdlInterface::object_present(M2MObject* object) const |
mbedAustin | 11:cada08fc8a70 | 1188 | { |
mbedAustin | 11:cada08fc8a70 | 1189 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 1190 | if(object && !_object_list.empty()) { |
mbedAustin | 11:cada08fc8a70 | 1191 | M2MObjectList::const_iterator it; |
mbedAustin | 11:cada08fc8a70 | 1192 | it = _object_list.begin(); |
mbedAustin | 11:cada08fc8a70 | 1193 | for ( ; it != _object_list.end(); it++ ) { |
mbedAustin | 11:cada08fc8a70 | 1194 | if((*it) == object) { |
mbedAustin | 11:cada08fc8a70 | 1195 | success = true; |
mbedAustin | 11:cada08fc8a70 | 1196 | break; |
mbedAustin | 11:cada08fc8a70 | 1197 | } |
mbedAustin | 11:cada08fc8a70 | 1198 | } |
mbedAustin | 11:cada08fc8a70 | 1199 | } |
mbedAustin | 11:cada08fc8a70 | 1200 | return success; |
mbedAustin | 11:cada08fc8a70 | 1201 | } |
mbedAustin | 11:cada08fc8a70 | 1202 | |
mbedAustin | 11:cada08fc8a70 | 1203 | bool M2MNsdlInterface::add_object_to_list(M2MObject* object) |
mbedAustin | 11:cada08fc8a70 | 1204 | { |
mbedAustin | 11:cada08fc8a70 | 1205 | bool success = false; |
mbedAustin | 11:cada08fc8a70 | 1206 | if(object && !object_present(object)) { |
mbedAustin | 11:cada08fc8a70 | 1207 | _object_list.push_back(object); |
mbedAustin | 11:cada08fc8a70 | 1208 | success = true; |
mbedAustin | 11:cada08fc8a70 | 1209 | } |
mbedAustin | 11:cada08fc8a70 | 1210 | return success; |
mbedAustin | 11:cada08fc8a70 | 1211 | } |
mbedAustin | 11:cada08fc8a70 | 1212 | |
mbedAustin | 11:cada08fc8a70 | 1213 | void M2MNsdlInterface::clear_resource(sn_nsdl_resource_info_s *&resource) |
mbedAustin | 11:cada08fc8a70 | 1214 | { |
mbedAustin | 11:cada08fc8a70 | 1215 | //Clear up the filled resource to fill up new resource. |
mbedAustin | 11:cada08fc8a70 | 1216 | if(resource && resource->resource_parameters_ptr) { |
mbedAustin | 11:cada08fc8a70 | 1217 | sn_nsdl_resource_parameters_s *temp_resource_parameter = resource->resource_parameters_ptr; |
mbedAustin | 11:cada08fc8a70 | 1218 | memset(resource->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
mbedAustin | 11:cada08fc8a70 | 1219 | memset(resource,0, sizeof(sn_nsdl_resource_info_s)); |
mbedAustin | 11:cada08fc8a70 | 1220 | resource->resource_parameters_ptr = temp_resource_parameter; |
mbedAustin | 11:cada08fc8a70 | 1221 | } |
mbedAustin | 11:cada08fc8a70 | 1222 | } |
mbedAustin | 11:cada08fc8a70 | 1223 | |
mbedAustin | 11:cada08fc8a70 | 1224 | M2MInterface::Error M2MNsdlInterface::interface_error(sn_coap_hdr_s *coap_header) |
mbedAustin | 11:cada08fc8a70 | 1225 | { |
mbedAustin | 11:cada08fc8a70 | 1226 | M2MInterface::Error error = M2MInterface::ErrorNone; |
mbedAustin | 11:cada08fc8a70 | 1227 | if(coap_header) { |
mbedAustin | 11:cada08fc8a70 | 1228 | switch(coap_header->msg_code) { |
mbedAustin | 11:cada08fc8a70 | 1229 | case COAP_MSG_CODE_RESPONSE_BAD_REQUEST: |
mbedAustin | 11:cada08fc8a70 | 1230 | case COAP_MSG_CODE_RESPONSE_BAD_OPTION: |
mbedAustin | 11:cada08fc8a70 | 1231 | case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE: |
mbedAustin | 11:cada08fc8a70 | 1232 | case COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED: |
mbedAustin | 11:cada08fc8a70 | 1233 | case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE: |
mbedAustin | 11:cada08fc8a70 | 1234 | case COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT: |
mbedAustin | 11:cada08fc8a70 | 1235 | error = M2MInterface::InvalidParameters; |
mbedAustin | 11:cada08fc8a70 | 1236 | break; |
mbedAustin | 11:cada08fc8a70 | 1237 | case COAP_MSG_CODE_RESPONSE_UNAUTHORIZED: |
mbedAustin | 11:cada08fc8a70 | 1238 | case COAP_MSG_CODE_RESPONSE_FORBIDDEN: |
mbedAustin | 11:cada08fc8a70 | 1239 | case COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE: |
mbedAustin | 11:cada08fc8a70 | 1240 | case COAP_MSG_CODE_RESPONSE_NOT_FOUND: |
mbedAustin | 11:cada08fc8a70 | 1241 | case COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED: |
mbedAustin | 11:cada08fc8a70 | 1242 | error = M2MInterface::NotAllowed; |
mbedAustin | 11:cada08fc8a70 | 1243 | break; |
mbedAustin | 11:cada08fc8a70 | 1244 | case COAP_MSG_CODE_RESPONSE_CREATED: |
mbedAustin | 11:cada08fc8a70 | 1245 | case COAP_MSG_CODE_RESPONSE_DELETED: |
mbedAustin | 11:cada08fc8a70 | 1246 | case COAP_MSG_CODE_RESPONSE_VALID: |
mbedAustin | 11:cada08fc8a70 | 1247 | case COAP_MSG_CODE_RESPONSE_CHANGED: |
mbedAustin | 11:cada08fc8a70 | 1248 | case COAP_MSG_CODE_RESPONSE_CONTENT: |
mbedAustin | 11:cada08fc8a70 | 1249 | error = M2MInterface::ErrorNone; |
mbedAustin | 11:cada08fc8a70 | 1250 | break; |
mbedAustin | 11:cada08fc8a70 | 1251 | default: |
mbedAustin | 11:cada08fc8a70 | 1252 | error = M2MInterface::UnknownError; |
mbedAustin | 11:cada08fc8a70 | 1253 | break; |
mbedAustin | 11:cada08fc8a70 | 1254 | } |
mbedAustin | 11:cada08fc8a70 | 1255 | if(coap_header->coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED) { |
mbedAustin | 11:cada08fc8a70 | 1256 | error = M2MInterface::NetworkError; |
mbedAustin | 11:cada08fc8a70 | 1257 | } |
mbedAustin | 11:cada08fc8a70 | 1258 | } |
mbedAustin | 11:cada08fc8a70 | 1259 | return error; |
mbedAustin | 11:cada08fc8a70 | 1260 | } |
mbedAustin | 11:cada08fc8a70 | 1261 | |
mbedAustin | 11:cada08fc8a70 | 1262 | void M2MNsdlInterface::send_object_observation(M2MObject *object) |
mbedAustin | 11:cada08fc8a70 | 1263 | { |
mbedAustin | 11:cada08fc8a70 | 1264 | tr_debug("M2MNsdlInterface::send_object_observation"); |
mbedAustin | 11:cada08fc8a70 | 1265 | if(object) { |
mbedAustin | 11:cada08fc8a70 | 1266 | uint8_t *value = 0; |
mbedAustin | 11:cada08fc8a70 | 1267 | uint32_t length = 0; |
mbedAustin | 11:cada08fc8a70 | 1268 | uint8_t *token = 0; |
mbedAustin | 11:cada08fc8a70 | 1269 | uint32_t token_length = 0; |
mbedAustin | 11:cada08fc8a70 | 1270 | uint8_t observation_number[2]; |
mbedAustin | 11:cada08fc8a70 | 1271 | uint8_t observation_number_length = 1; |
mbedAustin | 11:cada08fc8a70 | 1272 | |
mbedAustin | 11:cada08fc8a70 | 1273 | uint16_t number = object->observation_number(); |
mbedAustin | 11:cada08fc8a70 | 1274 | |
mbedAustin | 11:cada08fc8a70 | 1275 | observation_number[0] = ((number>>8) & 0xFF); |
mbedAustin | 11:cada08fc8a70 | 1276 | observation_number[1] = (number & 0xFF); |
mbedAustin | 11:cada08fc8a70 | 1277 | |
mbedAustin | 11:cada08fc8a70 | 1278 | if(number > 0xFF) { |
mbedAustin | 11:cada08fc8a70 | 1279 | observation_number_length = 2; |
mbedAustin | 11:cada08fc8a70 | 1280 | } |
mbedAustin | 11:cada08fc8a70 | 1281 | |
mbedAustin | 11:cada08fc8a70 | 1282 | M2MTLVSerializer *serializer = new M2MTLVSerializer(); |
mbedAustin | 11:cada08fc8a70 | 1283 | if(serializer) { |
mbedAustin | 11:cada08fc8a70 | 1284 | value = serializer->serialize(object->instances(), length); |
mbedAustin | 11:cada08fc8a70 | 1285 | delete serializer; |
mbedAustin | 11:cada08fc8a70 | 1286 | } |
mbedAustin | 11:cada08fc8a70 | 1287 | |
mbedAustin | 11:cada08fc8a70 | 1288 | object->get_observation_token(token,token_length); |
mbedAustin | 11:cada08fc8a70 | 1289 | |
mbedAustin | 11:cada08fc8a70 | 1290 | sn_nsdl_send_observation_notification(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 1291 | token, |
mbedAustin | 11:cada08fc8a70 | 1292 | token_length, |
mbedAustin | 11:cada08fc8a70 | 1293 | value,length, |
mbedAustin | 11:cada08fc8a70 | 1294 | observation_number, |
mbedAustin | 11:cada08fc8a70 | 1295 | observation_number_length, |
mbedAustin | 11:cada08fc8a70 | 1296 | COAP_MSG_TYPE_CONFIRMABLE, |
mbedAustin | 11:cada08fc8a70 | 1297 | object->coap_content_type()); |
mbedAustin | 11:cada08fc8a70 | 1298 | memory_free(value); |
mbedAustin | 11:cada08fc8a70 | 1299 | memory_free(token); |
mbedAustin | 11:cada08fc8a70 | 1300 | } |
mbedAustin | 11:cada08fc8a70 | 1301 | } |
mbedAustin | 11:cada08fc8a70 | 1302 | |
mbedAustin | 11:cada08fc8a70 | 1303 | void M2MNsdlInterface::send_object_instance_observation(M2MObjectInstance *object_instance) |
mbedAustin | 11:cada08fc8a70 | 1304 | { |
mbedAustin | 11:cada08fc8a70 | 1305 | tr_debug("M2MNsdlInterface::send_object_instance_observation"); |
mbedAustin | 11:cada08fc8a70 | 1306 | if(object_instance) { |
mbedAustin | 11:cada08fc8a70 | 1307 | uint8_t *value = 0; |
mbedAustin | 11:cada08fc8a70 | 1308 | uint32_t length = 0; |
mbedAustin | 11:cada08fc8a70 | 1309 | uint8_t *token = 0; |
mbedAustin | 11:cada08fc8a70 | 1310 | uint32_t token_length = 0; |
mbedAustin | 11:cada08fc8a70 | 1311 | uint8_t observation_number[2]; |
mbedAustin | 11:cada08fc8a70 | 1312 | uint8_t observation_number_length = 1; |
mbedAustin | 11:cada08fc8a70 | 1313 | |
mbedAustin | 11:cada08fc8a70 | 1314 | uint16_t number = object_instance->observation_number(); |
mbedAustin | 11:cada08fc8a70 | 1315 | |
mbedAustin | 11:cada08fc8a70 | 1316 | observation_number[0] = ((number>>8) & 0xFF); |
mbedAustin | 11:cada08fc8a70 | 1317 | observation_number[1] = (number & 0xFF); |
mbedAustin | 11:cada08fc8a70 | 1318 | |
mbedAustin | 11:cada08fc8a70 | 1319 | if(number > 0xFF) { |
mbedAustin | 11:cada08fc8a70 | 1320 | observation_number_length = 2; |
mbedAustin | 11:cada08fc8a70 | 1321 | } |
mbedAustin | 11:cada08fc8a70 | 1322 | |
mbedAustin | 11:cada08fc8a70 | 1323 | M2MTLVSerializer *serializer = new M2MTLVSerializer(); |
mbedAustin | 11:cada08fc8a70 | 1324 | if(serializer) { |
mbedAustin | 11:cada08fc8a70 | 1325 | value = serializer->serialize(object_instance->resources(), length); |
mbedAustin | 11:cada08fc8a70 | 1326 | delete serializer; |
mbedAustin | 11:cada08fc8a70 | 1327 | } |
mbedAustin | 11:cada08fc8a70 | 1328 | |
mbedAustin | 11:cada08fc8a70 | 1329 | object_instance->get_observation_token(token,token_length); |
mbedAustin | 11:cada08fc8a70 | 1330 | |
mbedAustin | 11:cada08fc8a70 | 1331 | sn_nsdl_send_observation_notification(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 1332 | token, |
mbedAustin | 11:cada08fc8a70 | 1333 | token_length, |
mbedAustin | 11:cada08fc8a70 | 1334 | value,length, |
mbedAustin | 11:cada08fc8a70 | 1335 | observation_number, |
mbedAustin | 11:cada08fc8a70 | 1336 | observation_number_length, |
mbedAustin | 11:cada08fc8a70 | 1337 | COAP_MSG_TYPE_CONFIRMABLE, |
mbedAustin | 11:cada08fc8a70 | 1338 | object_instance->coap_content_type()); |
mbedAustin | 11:cada08fc8a70 | 1339 | memory_free(value); |
mbedAustin | 11:cada08fc8a70 | 1340 | memory_free(token); |
mbedAustin | 11:cada08fc8a70 | 1341 | } |
mbedAustin | 11:cada08fc8a70 | 1342 | } |
mbedAustin | 11:cada08fc8a70 | 1343 | |
mbedAustin | 11:cada08fc8a70 | 1344 | void M2MNsdlInterface::send_resource_observation(M2MResource *resource) |
mbedAustin | 11:cada08fc8a70 | 1345 | { |
mbedAustin | 11:cada08fc8a70 | 1346 | tr_debug("M2MNsdlInterface::send_resource_observation"); |
mbedAustin | 11:cada08fc8a70 | 1347 | if(resource) { |
mbedAustin | 11:cada08fc8a70 | 1348 | uint8_t *value = 0; |
mbedAustin | 11:cada08fc8a70 | 1349 | uint32_t length = 0; |
mbedAustin | 11:cada08fc8a70 | 1350 | uint8_t *token = 0; |
mbedAustin | 11:cada08fc8a70 | 1351 | uint32_t token_length = 0; |
mbedAustin | 11:cada08fc8a70 | 1352 | uint8_t observation_number[2]; |
mbedAustin | 11:cada08fc8a70 | 1353 | uint8_t observation_number_length = 1; |
mbedAustin | 11:cada08fc8a70 | 1354 | |
mbedAustin | 11:cada08fc8a70 | 1355 | uint16_t number = resource->observation_number(); |
mbedAustin | 11:cada08fc8a70 | 1356 | |
mbedAustin | 11:cada08fc8a70 | 1357 | observation_number[0] = ((number>>8) & 0xFF); |
mbedAustin | 11:cada08fc8a70 | 1358 | observation_number[1] = (number & 0xFF); |
mbedAustin | 11:cada08fc8a70 | 1359 | |
mbedAustin | 11:cada08fc8a70 | 1360 | if(number > 0xFF) { |
mbedAustin | 11:cada08fc8a70 | 1361 | observation_number_length = 2; |
mbedAustin | 11:cada08fc8a70 | 1362 | } |
mbedAustin | 11:cada08fc8a70 | 1363 | |
mbedAustin | 11:cada08fc8a70 | 1364 | resource->get_observation_token(token,token_length); |
mbedAustin | 11:cada08fc8a70 | 1365 | uint8_t content_type = 0; |
mbedAustin | 11:cada08fc8a70 | 1366 | if (resource->resource_instance_count() > 0) { |
mbedAustin | 11:cada08fc8a70 | 1367 | M2MTLVSerializer *serializer = new M2MTLVSerializer(); |
mbedAustin | 11:cada08fc8a70 | 1368 | content_type = COAP_CONTENT_OMA_TLV_TYPE; |
mbedAustin | 11:cada08fc8a70 | 1369 | if(serializer) { |
mbedAustin | 11:cada08fc8a70 | 1370 | value = serializer->serialize(resource, length); |
mbedAustin | 11:cada08fc8a70 | 1371 | delete serializer; |
mbedAustin | 11:cada08fc8a70 | 1372 | } |
mbedAustin | 11:cada08fc8a70 | 1373 | } else { |
mbedAustin | 11:cada08fc8a70 | 1374 | resource->get_value(value,length); |
mbedAustin | 11:cada08fc8a70 | 1375 | } |
mbedAustin | 11:cada08fc8a70 | 1376 | |
mbedAustin | 11:cada08fc8a70 | 1377 | sn_nsdl_send_observation_notification(_nsdl_handle, |
mbedAustin | 11:cada08fc8a70 | 1378 | token, |
mbedAustin | 11:cada08fc8a70 | 1379 | token_length, |
mbedAustin | 11:cada08fc8a70 | 1380 | value,length, |
mbedAustin | 11:cada08fc8a70 | 1381 | observation_number, |
mbedAustin | 11:cada08fc8a70 | 1382 | observation_number_length, |
mbedAustin | 11:cada08fc8a70 | 1383 | COAP_MSG_TYPE_CONFIRMABLE, |
mbedAustin | 11:cada08fc8a70 | 1384 | content_type); |
mbedAustin | 11:cada08fc8a70 | 1385 | memory_free(value); |
mbedAustin | 11:cada08fc8a70 | 1386 | memory_free(token); |
mbedAustin | 11:cada08fc8a70 | 1387 | } |
mbedAustin | 11:cada08fc8a70 | 1388 | } |