Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Fri Mar 20 20:15:18 2020 +0000
Revision:
2:990c985a69ae
Parent:
0:977e87915078
Update to work with P2Scan runtime

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 /*
vithyat 0:977e87915078 2 * Copyright (c) 2015-2019 ARM Limited. All rights reserved.
vithyat 0:977e87915078 3 * SPDX-License-Identifier: Apache-2.0
vithyat 0:977e87915078 4 * Licensed under the Apache License, Version 2.0 (the License); you may
vithyat 0:977e87915078 5 * not use this file except in compliance with the License.
vithyat 0:977e87915078 6 * You may obtain a copy of the License at
vithyat 0:977e87915078 7 *
vithyat 0:977e87915078 8 * http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 *
vithyat 0:977e87915078 10 * Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
vithyat 0:977e87915078 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 * See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 * limitations under the License.
vithyat 0:977e87915078 15 */
vithyat 0:977e87915078 16
vithyat 0:977e87915078 17 // Needed for PRIu64 on FreeRTOS
vithyat 0:977e87915078 18 #include <stdio.h>
vithyat 0:977e87915078 19 // Note: this macro is needed on armcc to get the the limit macros like UINT16_MAX
vithyat 0:977e87915078 20 #ifndef __STDC_LIMIT_MACROS
vithyat 0:977e87915078 21 #define __STDC_LIMIT_MACROS
vithyat 0:977e87915078 22 #endif
vithyat 0:977e87915078 23
vithyat 0:977e87915078 24 // Note: this macro is needed on armcc to get the the PRI*32 macros
vithyat 0:977e87915078 25 // from inttypes.h in a C++ code.
vithyat 0:977e87915078 26 #ifndef __STDC_FORMAT_MACROS
vithyat 0:977e87915078 27 #define __STDC_FORMAT_MACROS
vithyat 0:977e87915078 28 #endif
vithyat 0:977e87915078 29
vithyat 0:977e87915078 30 #include <assert.h>
vithyat 0:977e87915078 31 #include <inttypes.h>
vithyat 0:977e87915078 32 #include <stdlib.h>
vithyat 0:977e87915078 33 #include "include/nsdlaccesshelper.h"
vithyat 0:977e87915078 34 #include "include/m2mnsdlobserver.h"
vithyat 0:977e87915078 35 #include "include/m2mtlvdeserializer.h"
vithyat 0:977e87915078 36 #include "include/m2mtlvserializer.h"
vithyat 0:977e87915078 37 #include "include/m2mnsdlinterface.h"
vithyat 0:977e87915078 38 #include "include/m2mreporthandler.h"
vithyat 0:977e87915078 39 #include "mbed-client/m2mstring.h"
vithyat 0:977e87915078 40 #include "mbed-client/m2msecurity.h"
vithyat 0:977e87915078 41 #include "mbed-client/m2mserver.h"
vithyat 0:977e87915078 42 #include "mbed-client/m2mobject.h"
vithyat 0:977e87915078 43 #include "mbed-client/m2mendpoint.h"
vithyat 0:977e87915078 44 #include "mbed-client/m2mobjectinstance.h"
vithyat 0:977e87915078 45 #include "mbed-client/m2mresource.h"
vithyat 0:977e87915078 46 #include "mbed-client/m2mblockmessage.h"
vithyat 0:977e87915078 47 #include "mbed-client/m2mconstants.h"
vithyat 0:977e87915078 48 #include "mbed-client/uriqueryparser.h"
vithyat 0:977e87915078 49 #include "mbed-trace/mbed_trace.h"
vithyat 0:977e87915078 50 #include "sn_grs.h"
vithyat 0:977e87915078 51 #include "mbed-client/m2minterfacefactory.h"
vithyat 0:977e87915078 52 #include "mbed-client/m2mdevice.h"
vithyat 0:977e87915078 53 #include "randLIB.h"
vithyat 0:977e87915078 54 #include "common_functions.h"
vithyat 0:977e87915078 55 #include "sn_nsdl_lib.h"
vithyat 0:977e87915078 56 #include "sn_coap_protocol.h"
vithyat 0:977e87915078 57 #include "m2mnotificationhandler.h"
vithyat 0:977e87915078 58 #include "eventOS_event_timer.h"
vithyat 0:977e87915078 59 #include "eventOS_scheduler.h"
vithyat 0:977e87915078 60 #include "ns_hal_init.h"
vithyat 0:977e87915078 61
vithyat 0:977e87915078 62 #define MBED_CLIENT_NSDLINTERFACE_TASKLET_INIT_EVENT 0 // Tasklet init occurs always when generating a tasklet
vithyat 0:977e87915078 63 #define MBED_CLIENT_NSDLINTERFACE_EVENT 30
vithyat 0:977e87915078 64 #define MBED_CLIENT_NSDLINTERFACE_BS_EVENT 31
vithyat 0:977e87915078 65 #define MBED_CLIENT_NSDLINTERFACE_BS_PUT_EVENT 32
vithyat 0:977e87915078 66 #define MBED_CLIENT_NSDLINTERFACE_BS_FINISH_EVENT 33
vithyat 0:977e87915078 67
vithyat 0:977e87915078 68 #ifdef MBED_CONF_MBED_CLIENT_EVENT_LOOP_SIZE
vithyat 0:977e87915078 69 #define MBED_CLIENT_EVENT_LOOP_SIZE MBED_CONF_MBED_CLIENT_EVENT_LOOP_SIZE
vithyat 0:977e87915078 70 #else
vithyat 0:977e87915078 71 #define MBED_CLIENT_EVENT_LOOP_SIZE 1024
vithyat 0:977e87915078 72 #endif
vithyat 0:977e87915078 73
vithyat 0:977e87915078 74 #define BUFFER_SIZE 21
vithyat 0:977e87915078 75 #define TRACE_GROUP "mClt"
vithyat 0:977e87915078 76 #define MAX_QUERY_COUNT 10
vithyat 0:977e87915078 77
vithyat 0:977e87915078 78 const char *MCC_VERSION = "mccv=2.2.1";
vithyat 0:977e87915078 79
vithyat 0:977e87915078 80 int8_t M2MNsdlInterface::_tasklet_id = -1;
vithyat 0:977e87915078 81
vithyat 0:977e87915078 82 extern "C" void nsdlinterface_tasklet_func(arm_event_s *event)
vithyat 0:977e87915078 83 {
vithyat 0:977e87915078 84 // skip the init event as there will be a timer event after
vithyat 0:977e87915078 85 if (event->event_type == MBED_CLIENT_NSDLINTERFACE_EVENT) {
vithyat 0:977e87915078 86 eventOS_scheduler_mutex_wait();
vithyat 0:977e87915078 87 M2MNsdlInterface::nsdl_coap_data_s *coap_data = (M2MNsdlInterface::nsdl_coap_data_s*)event->data_ptr;
vithyat 0:977e87915078 88 M2MNsdlInterface *interface = (M2MNsdlInterface*)sn_nsdl_get_context(coap_data->nsdl_handle);
vithyat 0:977e87915078 89 if (interface) {
vithyat 0:977e87915078 90 interface->resource_callback_handle_event(coap_data->received_coap_header, &coap_data->address);
vithyat 0:977e87915078 91 if (coap_data->received_coap_header->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED &&
vithyat 0:977e87915078 92 coap_data->received_coap_header->payload_ptr) {
vithyat 0:977e87915078 93 coap_data->nsdl_handle->grs->sn_grs_free(coap_data->received_coap_header->payload_ptr);
vithyat 0:977e87915078 94 coap_data->received_coap_header->payload_ptr = 0;
vithyat 0:977e87915078 95 }
vithyat 0:977e87915078 96 }
vithyat 0:977e87915078 97
vithyat 0:977e87915078 98 M2MNsdlInterface::memory_free(coap_data->received_coap_header->payload_ptr);
vithyat 0:977e87915078 99 sn_coap_parser_release_allocated_coap_msg_mem(coap_data->nsdl_handle->grs->coap, coap_data->received_coap_header);
vithyat 0:977e87915078 100 M2MNsdlInterface::memory_free(coap_data->address.addr_ptr);
vithyat 0:977e87915078 101 M2MNsdlInterface::memory_free(coap_data);
vithyat 0:977e87915078 102 eventOS_scheduler_mutex_release();
vithyat 0:977e87915078 103
vithyat 0:977e87915078 104 } else if (event->event_type == MBED_CLIENT_NSDLINTERFACE_BS_EVENT) {
vithyat 0:977e87915078 105 M2MNsdlInterface::nsdl_coap_data_s *coap_data = (M2MNsdlInterface::nsdl_coap_data_s*)event->data_ptr;
vithyat 0:977e87915078 106 M2MNsdlInterface *interface = (M2MNsdlInterface*)sn_nsdl_get_context(coap_data->nsdl_handle);
vithyat 0:977e87915078 107
vithyat 0:977e87915078 108 sn_coap_hdr_s *coap_response = sn_nsdl_build_response(coap_data->nsdl_handle,
vithyat 0:977e87915078 109 coap_data->received_coap_header,
vithyat 0:977e87915078 110 coap_data->received_coap_header->msg_code);
vithyat 0:977e87915078 111 if (coap_response) {
vithyat 0:977e87915078 112 coap_response->msg_type = coap_data->received_coap_header->msg_type;
vithyat 0:977e87915078 113
vithyat 0:977e87915078 114 if (sn_nsdl_send_coap_message(coap_data->nsdl_handle, &coap_data->address, coap_response) == 0) {
vithyat 0:977e87915078 115 interface->store_bs_finished_response_id(coap_response->msg_id);
vithyat 0:977e87915078 116 } else {
vithyat 0:977e87915078 117 tr_error("Failed to send final response for BS finished");
vithyat 0:977e87915078 118 }
vithyat 0:977e87915078 119
vithyat 0:977e87915078 120 sn_coap_parser_release_allocated_coap_msg_mem(coap_data->nsdl_handle->grs->coap, coap_response);
vithyat 0:977e87915078 121
vithyat 0:977e87915078 122 } else {
vithyat 0:977e87915078 123 tr_error("Failed to create final response message for BS finished");
vithyat 0:977e87915078 124 }
vithyat 0:977e87915078 125
vithyat 0:977e87915078 126 // Release the memory
vithyat 0:977e87915078 127 M2MNsdlInterface::memory_free(coap_data->received_coap_header->payload_ptr);
vithyat 0:977e87915078 128 sn_coap_parser_release_allocated_coap_msg_mem(coap_data->nsdl_handle->grs->coap, coap_data->received_coap_header);
vithyat 0:977e87915078 129 M2MNsdlInterface::memory_free(coap_data->address.addr_ptr);
vithyat 0:977e87915078 130 M2MNsdlInterface::memory_free(coap_data);
vithyat 0:977e87915078 131 } else if (event->event_type == MBED_CLIENT_NSDLINTERFACE_BS_PUT_EVENT) {
vithyat 0:977e87915078 132 M2MNsdlInterface::nsdl_coap_data_s *coap_data = (M2MNsdlInterface::nsdl_coap_data_s*)event->data_ptr;
vithyat 0:977e87915078 133 M2MNsdlInterface *interface = (M2MNsdlInterface*)sn_nsdl_get_context(coap_data->nsdl_handle);
vithyat 0:977e87915078 134 interface->handle_bootstrap_put_message(coap_data->received_coap_header, &coap_data->address);
vithyat 0:977e87915078 135
vithyat 0:977e87915078 136 M2MNsdlInterface::memory_free(coap_data->received_coap_header->payload_ptr);
vithyat 0:977e87915078 137 sn_coap_parser_release_allocated_coap_msg_mem(coap_data->nsdl_handle->grs->coap, coap_data->received_coap_header);
vithyat 0:977e87915078 138 M2MNsdlInterface::memory_free(coap_data->address.addr_ptr);
vithyat 0:977e87915078 139 M2MNsdlInterface::memory_free(coap_data);
vithyat 0:977e87915078 140 } else if (event->event_type == MBED_CLIENT_NSDLINTERFACE_BS_FINISH_EVENT) {
vithyat 0:977e87915078 141 nsdl_s *nsdl_handle = (nsdl_s*)event->data_ptr;
vithyat 0:977e87915078 142 M2MNsdlInterface *interface = (M2MNsdlInterface*)sn_nsdl_get_context(nsdl_handle);
vithyat 0:977e87915078 143 interface->handle_bootstrap_finish_ack(event->event_data);
vithyat 0:977e87915078 144 }
vithyat 0:977e87915078 145 }
vithyat 0:977e87915078 146
vithyat 0:977e87915078 147 M2MNsdlInterface::M2MNsdlInterface(M2MNsdlObserver &observer, M2MConnectionHandler &connection_handler)
vithyat 0:977e87915078 148 : _observer(observer),
vithyat 0:977e87915078 149 _endpoint(NULL),
vithyat 0:977e87915078 150 _nsdl_handle(NULL),
vithyat 0:977e87915078 151 _security(NULL),
vithyat 0:977e87915078 152 _server(NULL),
vithyat 0:977e87915078 153 _nsdl_execution_timer(*this),
vithyat 0:977e87915078 154 _registration_timer(*this),
vithyat 0:977e87915078 155 _connection_handler(connection_handler),
vithyat 0:977e87915078 156 _counter_for_nsdl(0),
vithyat 0:977e87915078 157 _next_coap_ping_send_time(0),
vithyat 0:977e87915078 158 _server_address(NULL),
vithyat 0:977e87915078 159 _custom_uri_query_params(NULL),
vithyat 0:977e87915078 160 _notification_handler(new M2MNotificationHandler()),
vithyat 0:977e87915078 161 _bootstrap_id(0),
vithyat 0:977e87915078 162 _binding_mode(M2MInterface::NOT_SET),
vithyat 0:977e87915078 163 _identity_accepted(false),
vithyat 0:977e87915078 164 _nsdl_execution_timer_running(false),
vithyat 0:977e87915078 165 _notification_send_ongoing(false),
vithyat 0:977e87915078 166 _registered(false),
vithyat 0:977e87915078 167 _bootstrap_finish_ack_received(false),
vithyat 0:977e87915078 168 _download_retry_timer(*this),
vithyat 0:977e87915078 169 _download_retry_time(0)
vithyat 0:977e87915078 170 {
vithyat 0:977e87915078 171 tr_debug("M2MNsdlInterface::M2MNsdlInterface()");
vithyat 0:977e87915078 172
vithyat 0:977e87915078 173 _event.data.data_ptr = NULL;
vithyat 0:977e87915078 174 _event.data.event_data = 0;
vithyat 0:977e87915078 175 _event.data.event_id = 0;
vithyat 0:977e87915078 176 _event.data.sender = 0;
vithyat 0:977e87915078 177 _event.data.event_type = 0;
vithyat 0:977e87915078 178 _event.data.priority = ARM_LIB_MED_PRIORITY_EVENT;
vithyat 0:977e87915078 179
vithyat 0:977e87915078 180 _server = new M2MServer();
vithyat 0:977e87915078 181
vithyat 0:977e87915078 182 // This initializes libCoap and libNsdl
vithyat 0:977e87915078 183 // Parameters are function pointers to used memory allocation
vithyat 0:977e87915078 184 // and free functions in structure and used functions for sending
vithyat 0:977e87915078 185 // and receiving purposes.
vithyat 0:977e87915078 186 _nsdl_handle = sn_nsdl_init(&(__nsdl_c_send_to_server), &(__nsdl_c_received_from_server),
vithyat 0:977e87915078 187 &(__nsdl_c_memory_alloc), &(__nsdl_c_memory_free), &(__nsdl_c_auto_obs_token));
vithyat 0:977e87915078 188
vithyat 0:977e87915078 189 sn_nsdl_set_context(_nsdl_handle, this);
vithyat 0:977e87915078 190
vithyat 0:977e87915078 191 ns_hal_init(NULL, MBED_CLIENT_EVENT_LOOP_SIZE, NULL, NULL);
vithyat 0:977e87915078 192 eventOS_scheduler_mutex_wait();
vithyat 0:977e87915078 193 if (M2MNsdlInterface::_tasklet_id < 0) {
vithyat 0:977e87915078 194 M2MNsdlInterface::_tasklet_id = eventOS_event_handler_create(nsdlinterface_tasklet_func, MBED_CLIENT_NSDLINTERFACE_TASKLET_INIT_EVENT);
vithyat 0:977e87915078 195 assert(M2MNsdlInterface::_tasklet_id >= 0);
vithyat 0:977e87915078 196 }
vithyat 0:977e87915078 197 eventOS_scheduler_mutex_release();
vithyat 0:977e87915078 198
vithyat 0:977e87915078 199 _event.data.receiver = M2MNsdlInterface::_tasklet_id;
vithyat 0:977e87915078 200
vithyat 0:977e87915078 201 // Randomize the initial auto obs token. Range is in 1 - 1023
vithyat 0:977e87915078 202 _auto_obs_token = randLIB_get_random_in_range(AUTO_OBS_TOKEN_MIN, AUTO_OBS_TOKEN_MAX);
vithyat 0:977e87915078 203
vithyat 0:977e87915078 204 initialize();
vithyat 0:977e87915078 205 }
vithyat 0:977e87915078 206
vithyat 0:977e87915078 207 M2MNsdlInterface::~M2MNsdlInterface()
vithyat 0:977e87915078 208 {
vithyat 0:977e87915078 209 tr_debug("M2MNsdlInterface::~M2MNsdlInterface() - IN");
vithyat 0:977e87915078 210 if (_endpoint) {
vithyat 0:977e87915078 211 memory_free(_endpoint->endpoint_name_ptr);
vithyat 0:977e87915078 212 memory_free(_endpoint->domain_name_ptr);
vithyat 0:977e87915078 213 memory_free(_endpoint->type_ptr);
vithyat 0:977e87915078 214 memory_free(_endpoint->lifetime_ptr);
vithyat 0:977e87915078 215 memory_free(_endpoint);
vithyat 0:977e87915078 216 }
vithyat 0:977e87915078 217
vithyat 0:977e87915078 218 delete _notification_handler;
vithyat 0:977e87915078 219 _base_list.clear();
vithyat 0:977e87915078 220 _security = NULL;
vithyat 0:977e87915078 221 delete _server;
vithyat 0:977e87915078 222 sn_nsdl_destroy(_nsdl_handle);
vithyat 0:977e87915078 223 _nsdl_handle = NULL;
vithyat 0:977e87915078 224 memory_free(_server_address);
vithyat 0:977e87915078 225 free_request_context_list(NULL, false);
vithyat 0:977e87915078 226 free_response_list();
vithyat 0:977e87915078 227 memory_free(_custom_uri_query_params);
vithyat 0:977e87915078 228 tr_debug("M2MNsdlInterface::~M2MNsdlInterface() - OUT");
vithyat 0:977e87915078 229 }
vithyat 0:977e87915078 230
vithyat 0:977e87915078 231 bool M2MNsdlInterface::initialize()
vithyat 0:977e87915078 232 {
vithyat 0:977e87915078 233 tr_debug("M2MNsdlInterface::initialize()");
vithyat 0:977e87915078 234 bool success = false;
vithyat 0:977e87915078 235
vithyat 0:977e87915078 236 // Sets the packet retransmission attempts and time interval
vithyat 0:977e87915078 237 sn_nsdl_set_retransmission_parameters(_nsdl_handle,
vithyat 0:977e87915078 238 MBED_CLIENT_RECONNECTION_COUNT,
vithyat 0:977e87915078 239 MBED_CLIENT_RECONNECTION_INTERVAL);
vithyat 0:977e87915078 240
vithyat 0:977e87915078 241 sn_nsdl_set_retransmission_buffer(_nsdl_handle, MBED_CLIENT_SN_COAP_RESENDING_QUEUE_SIZE_MSGS, 0);
vithyat 0:977e87915078 242
vithyat 0:977e87915078 243 sn_nsdl_handle_block2_response_internally(_nsdl_handle, false);
vithyat 0:977e87915078 244
vithyat 0:977e87915078 245 // Allocate the memory for endpoint
vithyat 0:977e87915078 246 _endpoint = (sn_nsdl_ep_parameters_s*)memory_alloc(sizeof(sn_nsdl_ep_parameters_s));
vithyat 0:977e87915078 247 if (_endpoint) {
vithyat 0:977e87915078 248 memset(_endpoint, 0, sizeof(sn_nsdl_ep_parameters_s));
vithyat 0:977e87915078 249 success = true;
vithyat 0:977e87915078 250 }
vithyat 0:977e87915078 251
vithyat 0:977e87915078 252 M2MResource* update_trigger = _server->get_resource(M2MServer::RegistrationUpdate);
vithyat 0:977e87915078 253 if (update_trigger) {
vithyat 0:977e87915078 254 update_trigger->set_execute_function(execute_callback(this,
vithyat 0:977e87915078 255 &M2MNsdlInterface::update_trigger_callback));
vithyat 0:977e87915078 256 }
vithyat 0:977e87915078 257
vithyat 0:977e87915078 258 add_object_to_list(_server);
vithyat 0:977e87915078 259 create_nsdl_object_structure(_server);
vithyat 0:977e87915078 260 ns_list_init(&_request_context_list);
vithyat 0:977e87915078 261 ns_list_init(&_response_list);
vithyat 0:977e87915078 262
vithyat 0:977e87915078 263 return success;
vithyat 0:977e87915078 264 }
vithyat 0:977e87915078 265
vithyat 0:977e87915078 266 void M2MNsdlInterface::create_endpoint(const String &name,
vithyat 0:977e87915078 267 const String &type,
vithyat 0:977e87915078 268 const int32_t life_time,
vithyat 0:977e87915078 269 const String &domain,
vithyat 0:977e87915078 270 const uint8_t mode,
vithyat 0:977e87915078 271 const String &/*context_address*/)
vithyat 0:977e87915078 272 {
vithyat 0:977e87915078 273 tr_info("M2MNsdlInterface::create_endpoint( name %s type %s lifetime %" PRId32 ", domain %s, mode %d)",
vithyat 0:977e87915078 274 name.c_str(), type.c_str(), life_time, domain.c_str(), mode);
vithyat 0:977e87915078 275 _endpoint_name = name;
vithyat 0:977e87915078 276 _binding_mode = mode;
vithyat 0:977e87915078 277
vithyat 0:977e87915078 278 if (_endpoint){
vithyat 0:977e87915078 279 memset(_endpoint, 0, sizeof(sn_nsdl_ep_parameters_s));
vithyat 0:977e87915078 280 if (!_endpoint_name.empty()) {
vithyat 0:977e87915078 281 memory_free(_endpoint->endpoint_name_ptr);
vithyat 0:977e87915078 282 _endpoint->endpoint_name_ptr = alloc_string_copy((uint8_t*)_endpoint_name.c_str(), _endpoint_name.length());
vithyat 0:977e87915078 283 _endpoint->endpoint_name_len = _endpoint_name.length();
vithyat 0:977e87915078 284 }
vithyat 0:977e87915078 285 if (!type.empty()) {
vithyat 0:977e87915078 286 _endpoint->type_ptr = alloc_string_copy((uint8_t*)type.c_str(), type.length());
vithyat 0:977e87915078 287 _endpoint->type_len = type.length();
vithyat 0:977e87915078 288 }
vithyat 0:977e87915078 289 if (!domain.empty()) {
vithyat 0:977e87915078 290 _endpoint->domain_name_ptr = alloc_string_copy((uint8_t*)domain.c_str(), domain.length());
vithyat 0:977e87915078 291 _endpoint->domain_name_len = domain.length();
vithyat 0:977e87915078 292 }
vithyat 0:977e87915078 293
vithyat 0:977e87915078 294 // nsdl binding mode is only 3 least significant bits
vithyat 0:977e87915078 295 _endpoint->binding_and_mode = (sn_nsdl_oma_binding_and_mode_t)((uint8_t)mode & 0x07);
vithyat 0:977e87915078 296
vithyat 0:977e87915078 297 // If lifetime is less than zero then leave the field empty
vithyat 0:977e87915078 298 if (life_time > 0) {
vithyat 0:977e87915078 299 set_endpoint_lifetime_buffer(life_time);
vithyat 0:977e87915078 300 }
vithyat 0:977e87915078 301 }
vithyat 0:977e87915078 302 }
vithyat 0:977e87915078 303
vithyat 0:977e87915078 304 void M2MNsdlInterface::update_endpoint(const String &name)
vithyat 0:977e87915078 305 {
vithyat 0:977e87915078 306 _endpoint_name = name;
vithyat 0:977e87915078 307 if (_endpoint){
vithyat 0:977e87915078 308 if (!_endpoint_name.empty()) {
vithyat 0:977e87915078 309 memory_free(_endpoint->endpoint_name_ptr);
vithyat 0:977e87915078 310 _endpoint->endpoint_name_ptr = alloc_string_copy((uint8_t*)_endpoint_name.c_str(), _endpoint_name.length());
vithyat 0:977e87915078 311 _endpoint->endpoint_name_len = _endpoint_name.length();
vithyat 0:977e87915078 312 }
vithyat 0:977e87915078 313 }
vithyat 0:977e87915078 314 }
vithyat 0:977e87915078 315
vithyat 0:977e87915078 316 void M2MNsdlInterface::update_domain(const String &domain)
vithyat 0:977e87915078 317 {
vithyat 0:977e87915078 318 if (_endpoint){
vithyat 0:977e87915078 319 memory_free(_endpoint->domain_name_ptr);
vithyat 0:977e87915078 320 if (!domain.empty()) {
vithyat 0:977e87915078 321 _endpoint->domain_name_ptr = alloc_string_copy((uint8_t*)domain.c_str(), domain.length());
vithyat 0:977e87915078 322 _endpoint->domain_name_len = domain.length();
vithyat 0:977e87915078 323 } else {
vithyat 0:977e87915078 324 _endpoint->domain_name_ptr = NULL;
vithyat 0:977e87915078 325 _endpoint->domain_name_len = 0;
vithyat 0:977e87915078 326 }
vithyat 0:977e87915078 327 }
vithyat 0:977e87915078 328 }
vithyat 0:977e87915078 329
vithyat 0:977e87915078 330 void M2MNsdlInterface::set_endpoint_lifetime_buffer(int lifetime)
vithyat 0:977e87915078 331 {
vithyat 0:977e87915078 332 tr_info("M2MNsdlInterface::set_endpoint_lifetime_buffer - %d", lifetime);
vithyat 0:977e87915078 333 if (lifetime < MINIMUM_REGISTRATION_TIME) {
vithyat 0:977e87915078 334 return;
vithyat 0:977e87915078 335 }
vithyat 0:977e87915078 336
vithyat 0:977e87915078 337 _server->set_resource_value(M2MServer::Lifetime, lifetime);
vithyat 0:977e87915078 338
vithyat 0:977e87915078 339 if (_endpoint && _endpoint->lifetime_ptr) {
vithyat 0:977e87915078 340 memory_free(_endpoint->lifetime_ptr);
vithyat 0:977e87915078 341 _endpoint->lifetime_ptr = NULL;
vithyat 0:977e87915078 342 _endpoint->lifetime_len = 0;
vithyat 0:977e87915078 343 }
vithyat 0:977e87915078 344
vithyat 0:977e87915078 345 char buffer[20+1];
vithyat 0:977e87915078 346 uint32_t size = m2m::itoa_c(lifetime, buffer);
vithyat 0:977e87915078 347 if (_endpoint && size <= sizeof(buffer)) {
vithyat 0:977e87915078 348 _endpoint->lifetime_len = 0;
vithyat 0:977e87915078 349 _endpoint->lifetime_ptr = alloc_string_copy((uint8_t*)buffer, size);
vithyat 0:977e87915078 350 if (_endpoint->lifetime_ptr) {
vithyat 0:977e87915078 351 _endpoint->lifetime_len = size;
vithyat 0:977e87915078 352 }
vithyat 0:977e87915078 353 }
vithyat 0:977e87915078 354
vithyat 0:977e87915078 355 set_retransmission_parameters();
vithyat 0:977e87915078 356 }
vithyat 0:977e87915078 357
vithyat 0:977e87915078 358
vithyat 0:977e87915078 359 void M2MNsdlInterface::delete_endpoint()
vithyat 0:977e87915078 360 {
vithyat 0:977e87915078 361 tr_debug("M2MNsdlInterface::delete_endpoint()");
vithyat 0:977e87915078 362 if (_endpoint) {
vithyat 0:977e87915078 363 free(_endpoint->lifetime_ptr);
vithyat 0:977e87915078 364 memory_free(_endpoint);
vithyat 0:977e87915078 365 _endpoint = NULL;
vithyat 0:977e87915078 366 }
vithyat 0:977e87915078 367 }
vithyat 0:977e87915078 368
vithyat 0:977e87915078 369 bool M2MNsdlInterface::create_nsdl_list_structure(const M2MBaseList &list)
vithyat 0:977e87915078 370 {
vithyat 0:977e87915078 371 tr_debug("M2MNsdlInterface::create_nsdl_list_structure()");
vithyat 0:977e87915078 372 bool success = false;
vithyat 0:977e87915078 373 if (!list.empty()) {
vithyat 0:977e87915078 374 tr_debug("M2MNsdlInterface::create_nsdl_list_structure - Object count is %d", list.size());
vithyat 0:977e87915078 375 M2MBaseList::const_iterator it;
vithyat 0:977e87915078 376 it = list.begin();
vithyat 0:977e87915078 377 for ( ; it != list.end(); it++ ) {
vithyat 0:977e87915078 378 // Create NSDL structure for all Objects inside
vithyat 0:977e87915078 379 success = create_nsdl_structure(*it);
vithyat 0:977e87915078 380 if (!success) {
vithyat 0:977e87915078 381 tr_debug("M2MNsdlInterface::create_nsdl_list_structure - fail to create resource");
vithyat 0:977e87915078 382 break;
vithyat 0:977e87915078 383 }
vithyat 0:977e87915078 384
vithyat 0:977e87915078 385 tr_debug("M2MNsdlInterface::create_nsdl_list_structure - create %d", success);
vithyat 0:977e87915078 386 add_object_to_list(*it);
vithyat 0:977e87915078 387 }
vithyat 0:977e87915078 388 }
vithyat 0:977e87915078 389
vithyat 0:977e87915078 390 return success;
vithyat 0:977e87915078 391 }
vithyat 0:977e87915078 392
vithyat 0:977e87915078 393 bool M2MNsdlInterface::remove_nsdl_resource(M2MBase *base)
vithyat 0:977e87915078 394 {
vithyat 0:977e87915078 395 sn_nsdl_dynamic_resource_parameters_s* resource = base->get_nsdl_resource();
vithyat 0:977e87915078 396 return sn_nsdl_pop_resource(_nsdl_handle, resource);
vithyat 0:977e87915078 397 }
vithyat 0:977e87915078 398
vithyat 0:977e87915078 399 bool M2MNsdlInterface::create_bootstrap_resource(sn_nsdl_addr_s *address)
vithyat 0:977e87915078 400 {
vithyat 0:977e87915078 401 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 402 tr_debug("M2MNsdlInterface::create_bootstrap_resource()");
vithyat 0:977e87915078 403 _identity_accepted = false;
vithyat 0:977e87915078 404 _bootstrap_finish_ack_received = false;
vithyat 0:977e87915078 405 bool success = false;
vithyat 0:977e87915078 406 tr_debug("M2MNsdlInterface::create_bootstrap_resource() - endpoint name: %.*s", _endpoint->endpoint_name_len,
vithyat 0:977e87915078 407 _endpoint->endpoint_name_ptr);
vithyat 0:977e87915078 408
vithyat 0:977e87915078 409 if (_bootstrap_id == 0) {
vithyat 0:977e87915078 410 // Take copy of the address, uri_query_parameters() will modify the source buffer
vithyat 0:977e87915078 411 bool msg_sent = false;
vithyat 0:977e87915078 412 if (_server_address) {
vithyat 0:977e87915078 413 char *address_copy = M2MBase::alloc_string_copy(_server_address);
vithyat 0:977e87915078 414 if (address_copy) {
vithyat 0:977e87915078 415 char* query = parse_uri_query_parameters(_server_address);
vithyat 0:977e87915078 416 if (query != NULL) {
vithyat 0:977e87915078 417 size_t query_len = 1 + strlen(query) + 1 + strlen(MCC_VERSION) + 1;
vithyat 0:977e87915078 418 if (query_len <= MAX_URI_QUERY_LEN) {
vithyat 0:977e87915078 419 char query_params[MAX_URI_QUERY_LEN];
vithyat 0:977e87915078 420 strcpy(query_params, "&");
vithyat 0:977e87915078 421 strcat(query_params, query);
vithyat 0:977e87915078 422 strcat(query_params, "&");
vithyat 0:977e87915078 423 strcat(query_params, MCC_VERSION);
vithyat 0:977e87915078 424 msg_sent = true;
vithyat 0:977e87915078 425 sn_nsdl_clear_coap_resending_queue(_nsdl_handle);
vithyat 0:977e87915078 426 _bootstrap_id = sn_nsdl_oma_bootstrap(_nsdl_handle,
vithyat 0:977e87915078 427 address,
vithyat 0:977e87915078 428 _endpoint,
vithyat 0:977e87915078 429 query_params);
vithyat 0:977e87915078 430 free(_server_address);
vithyat 0:977e87915078 431 _server_address = M2MBase::alloc_string_copy(address_copy);
vithyat 0:977e87915078 432 } else {
vithyat 0:977e87915078 433 tr_error("M2MNsdlInterface::create_bootstrap_resource() - max uri param length reached (%lu)",
vithyat 0:977e87915078 434 (unsigned long)query_len);
vithyat 0:977e87915078 435 }
vithyat 0:977e87915078 436 }
vithyat 0:977e87915078 437 free(address_copy);
vithyat 0:977e87915078 438 }
vithyat 0:977e87915078 439 }
vithyat 0:977e87915078 440 if (!msg_sent) {
vithyat 0:977e87915078 441 sn_nsdl_clear_coap_resending_queue(_nsdl_handle);
vithyat 0:977e87915078 442 _bootstrap_id = sn_nsdl_oma_bootstrap(_nsdl_handle,
vithyat 0:977e87915078 443 address,
vithyat 0:977e87915078 444 _endpoint,
vithyat 0:977e87915078 445 NULL);
vithyat 0:977e87915078 446 }
vithyat 0:977e87915078 447 success = _bootstrap_id != 0;
vithyat 0:977e87915078 448 tr_debug("M2MNsdlInterface::create_bootstrap_resource - _bootstrap_id %d", _bootstrap_id);
vithyat 0:977e87915078 449 }
vithyat 0:977e87915078 450 return success;
vithyat 0:977e87915078 451 #else
vithyat 0:977e87915078 452 (void)address;
vithyat 0:977e87915078 453 return false;
vithyat 0:977e87915078 454 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 455 }
vithyat 0:977e87915078 456
vithyat 0:977e87915078 457 void M2MNsdlInterface::set_server_address(uint8_t* address,
vithyat 0:977e87915078 458 uint8_t address_length,
vithyat 0:977e87915078 459 const uint16_t port,
vithyat 0:977e87915078 460 sn_nsdl_addr_type_e address_type)
vithyat 0:977e87915078 461 {
vithyat 0:977e87915078 462 tr_debug("M2MNsdlInterface::set_server_address()");
vithyat 0:977e87915078 463 set_NSP_address(_nsdl_handle, address, address_length, port, address_type);
vithyat 0:977e87915078 464 }
vithyat 0:977e87915078 465
vithyat 0:977e87915078 466 bool M2MNsdlInterface::send_register_message()
vithyat 0:977e87915078 467 {
vithyat 0:977e87915078 468 tr_info("M2MNsdlInterface::send_register_message()");
vithyat 0:977e87915078 469 bool success = false;
vithyat 0:977e87915078 470 if (_server_address) {
vithyat 0:977e87915078 471 success = parse_and_send_uri_query_parameters();
vithyat 0:977e87915078 472 }
vithyat 0:977e87915078 473 // If URI parsing fails or there is no parameters, try again without parameters
vithyat 0:977e87915078 474 if (!success) {
vithyat 0:977e87915078 475 // Clear the observation tokens
vithyat 0:977e87915078 476 send_next_notification(true);
vithyat 0:977e87915078 477
vithyat 0:977e87915078 478 success = sn_nsdl_register_endpoint(_nsdl_handle,_endpoint, NULL) != 0;
vithyat 0:977e87915078 479 }
vithyat 0:977e87915078 480 return success;
vithyat 0:977e87915078 481 }
vithyat 0:977e87915078 482
vithyat 0:977e87915078 483 void M2MNsdlInterface::send_request(DownloadType type,
vithyat 0:977e87915078 484 const char *uri,
vithyat 0:977e87915078 485 const sn_coap_msg_code_e msg_code,
vithyat 0:977e87915078 486 const size_t offset,
vithyat 0:977e87915078 487 const bool async,
vithyat 0:977e87915078 488 uint32_t token,
vithyat 0:977e87915078 489 const uint16_t payload_len,
vithyat 0:977e87915078 490 uint8_t *payload_ptr,
vithyat 0:977e87915078 491 request_data_cb data_cb,
vithyat 0:977e87915078 492 request_error_cb error_cb,
vithyat 0:977e87915078 493 void *context)
vithyat 0:977e87915078 494 {
vithyat 0:977e87915078 495 assert(uri != NULL);
vithyat 0:977e87915078 496 int32_t message_id = 0;
vithyat 0:977e87915078 497 request_context_s *data_request = NULL;
vithyat 0:977e87915078 498
vithyat 0:977e87915078 499 if (msg_code == COAP_MSG_CODE_REQUEST_GET && !_registered) {
vithyat 0:977e87915078 500 tr_error("M2MNsdlInterface::send_request - client not registered!");
vithyat 0:977e87915078 501 error_cb(ERROR_NOT_REGISTERED, context);
vithyat 0:977e87915078 502 return;
vithyat 0:977e87915078 503 }
vithyat 0:977e87915078 504
vithyat 0:977e87915078 505 // Check the duplicate items
vithyat 0:977e87915078 506 request_context_s *data = (request_context_s *)ns_list_get_first(&_request_context_list);
vithyat 0:977e87915078 507 while (data) {
vithyat 0:977e87915078 508 if ((strcmp(uri, data->uri_path) == 0) && (offset == data->received_size)) {
vithyat 0:977e87915078 509 tr_debug("M2MNsdlInterface::send_request - item already exists");
vithyat 0:977e87915078 510 // Remove queued message from the resend queue before resuming file download.
vithyat 0:977e87915078 511 // Otherwise there will be duplicate block transfer with a just different message id's.
vithyat 0:977e87915078 512 sn_nsdl_remove_msg_from_retransmission(_nsdl_handle,
vithyat 0:977e87915078 513 (uint8_t*)&data->msg_token,
vithyat 0:977e87915078 514 sizeof(data->msg_token));
vithyat 0:977e87915078 515 data_request = data;
vithyat 0:977e87915078 516 break;
vithyat 0:977e87915078 517 }
vithyat 0:977e87915078 518 data = (request_context_s *)ns_list_get_next(&_request_context_list, data);
vithyat 0:977e87915078 519 }
vithyat 0:977e87915078 520
vithyat 0:977e87915078 521 if (data_request == NULL) {
vithyat 0:977e87915078 522 data_request = (struct request_context_s*)memory_alloc(sizeof(struct request_context_s));
vithyat 0:977e87915078 523 if (data_request == NULL) {
vithyat 0:977e87915078 524 error_cb(FAILED_TO_ALLOCATE_MEMORY, context);
vithyat 0:977e87915078 525 return;
vithyat 0:977e87915078 526 }
vithyat 0:977e87915078 527
vithyat 0:977e87915078 528 data_request->resend = false;
vithyat 0:977e87915078 529 data_request->context = context;
vithyat 0:977e87915078 530 data_request->async_req = async;
vithyat 0:977e87915078 531 data_request->received_size = offset;
vithyat 0:977e87915078 532 data_request->download_type = type;
vithyat 0:977e87915078 533 data_request->uri_path = (char*)alloc_string_copy((uint8_t*)uri, strlen(uri));
vithyat 0:977e87915078 534 if (data_request->uri_path == NULL) {
vithyat 0:977e87915078 535 memory_free(data_request);
vithyat 0:977e87915078 536 error_cb(FAILED_TO_ALLOCATE_MEMORY, context);
vithyat 0:977e87915078 537 return;
vithyat 0:977e87915078 538 }
vithyat 0:977e87915078 539
vithyat 0:977e87915078 540 data_request->on_request_data_cb = data_cb;
vithyat 0:977e87915078 541 data_request->on_request_error_cb = error_cb;
vithyat 0:977e87915078 542
vithyat 0:977e87915078 543 if (!token) {
vithyat 0:977e87915078 544 randLIB_get_n_bytes_random(&token, sizeof(token));
vithyat 0:977e87915078 545
vithyat 0:977e87915078 546 if (!token) {
vithyat 0:977e87915078 547 token++;
vithyat 0:977e87915078 548 }
vithyat 0:977e87915078 549 }
vithyat 0:977e87915078 550
vithyat 0:977e87915078 551 data_request->msg_token = token;
vithyat 0:977e87915078 552 data_request->msg_code = msg_code;
vithyat 0:977e87915078 553
vithyat 0:977e87915078 554 ns_list_add_to_end(&_request_context_list, data_request);
vithyat 0:977e87915078 555
vithyat 0:977e87915078 556 }
vithyat 0:977e87915078 557
vithyat 0:977e87915078 558 message_id = sn_nsdl_send_request(_nsdl_handle,
vithyat 0:977e87915078 559 data_request->msg_code,
vithyat 0:977e87915078 560 data_request->uri_path,
vithyat 0:977e87915078 561 data_request->msg_token,
vithyat 0:977e87915078 562 data_request->received_size,
vithyat 0:977e87915078 563 payload_len,
vithyat 0:977e87915078 564 payload_ptr,
vithyat 0:977e87915078 565 data_request->download_type);
vithyat 0:977e87915078 566
vithyat 0:977e87915078 567 if (message_id == -4) {
vithyat 0:977e87915078 568 data_request->resend = true;
vithyat 0:977e87915078 569 } else if (message_id <= 0) {
vithyat 0:977e87915078 570 ns_list_remove(&_request_context_list, data_request);
vithyat 0:977e87915078 571 memory_free(data_request->uri_path);
vithyat 0:977e87915078 572 memory_free(data_request);
vithyat 0:977e87915078 573 error_cb(FAILED_TO_ALLOCATE_MEMORY, context);
vithyat 0:977e87915078 574 }
vithyat 0:977e87915078 575 }
vithyat 0:977e87915078 576
vithyat 0:977e87915078 577 bool M2MNsdlInterface::send_update_registration(const uint32_t lifetime)
vithyat 0:977e87915078 578 {
vithyat 0:977e87915078 579 tr_info("M2MNsdlInterface::send_update_registration( lifetime %" PRIu32 ")", lifetime);
vithyat 0:977e87915078 580 bool success = false;
vithyat 0:977e87915078 581 int32_t ret = 0;
vithyat 0:977e87915078 582
vithyat 0:977e87915078 583 _registration_timer.stop_timer();
vithyat 0:977e87915078 584 bool lifetime_changed = true;
vithyat 0:977e87915078 585
vithyat 0:977e87915078 586 // If new resources have been created after registration those must be created and published to the server.
vithyat 0:977e87915078 587 create_nsdl_list_structure(_base_list);
vithyat 0:977e87915078 588
vithyat 0:977e87915078 589 // Check if resource(1/0/1) value has been updated and update it into _endpoint struct
vithyat 0:977e87915078 590 if (lifetime == 0) {
vithyat 0:977e87915078 591 lifetime_changed = lifetime_value_changed();
vithyat 0:977e87915078 592 if (lifetime_changed) {
vithyat 0:977e87915078 593 set_endpoint_lifetime_buffer(_server->resource_value_int(M2MServer::Lifetime));;
vithyat 0:977e87915078 594 }
vithyat 0:977e87915078 595 } else {
vithyat 0:977e87915078 596 set_endpoint_lifetime_buffer(lifetime);
vithyat 0:977e87915078 597 }
vithyat 0:977e87915078 598
vithyat 0:977e87915078 599 if (_nsdl_handle) {
vithyat 0:977e87915078 600 if (!lifetime_changed) {
vithyat 0:977e87915078 601 tr_debug("M2MNsdlInterface::send_update_registration - regular update");
vithyat 0:977e87915078 602 ret = sn_nsdl_update_registration(_nsdl_handle, NULL, 0);
vithyat 0:977e87915078 603 } else {
vithyat 0:977e87915078 604 if (_endpoint && _endpoint->lifetime_ptr) {
vithyat 0:977e87915078 605 tr_debug("M2MNsdlInterface::send_update_registration - new lifetime value");
vithyat 0:977e87915078 606 ret = sn_nsdl_update_registration(_nsdl_handle,
vithyat 0:977e87915078 607 _endpoint->lifetime_ptr,
vithyat 0:977e87915078 608 _endpoint->lifetime_len);
vithyat 0:977e87915078 609 }
vithyat 0:977e87915078 610 }
vithyat 0:977e87915078 611 }
vithyat 0:977e87915078 612
vithyat 0:977e87915078 613 if (ret >= 0) {
vithyat 0:977e87915078 614 success = true;
vithyat 0:977e87915078 615 }
vithyat 0:977e87915078 616
vithyat 0:977e87915078 617 _registration_timer.start_timer(registration_time() * 1000,
vithyat 0:977e87915078 618 M2MTimerObserver::Registration,
vithyat 0:977e87915078 619 false);
vithyat 0:977e87915078 620
vithyat 0:977e87915078 621 return success;
vithyat 0:977e87915078 622 }
vithyat 0:977e87915078 623
vithyat 0:977e87915078 624 bool M2MNsdlInterface::send_unregister_message()
vithyat 0:977e87915078 625 {
vithyat 0:977e87915078 626 tr_info("M2MNsdlInterface::send_unregister_message");
vithyat 0:977e87915078 627 if (is_unregister_ongoing()) {
vithyat 0:977e87915078 628 tr_debug("M2MNsdlInterface::send_unregister_message - unregistration already in progress");
vithyat 0:977e87915078 629 return true;
vithyat 0:977e87915078 630 }
vithyat 0:977e87915078 631
vithyat 0:977e87915078 632 bool success = false;
vithyat 0:977e87915078 633 int32_t ret = 0;
vithyat 0:977e87915078 634
vithyat 0:977e87915078 635 ret = sn_nsdl_unregister_endpoint(_nsdl_handle);
vithyat 0:977e87915078 636 if (ret == -4) {
vithyat 0:977e87915078 637 tr_warn("Failed to send registration update. Clearing queue and retrying.");
vithyat 0:977e87915078 638 sn_nsdl_clear_coap_resending_queue(_nsdl_handle);
vithyat 0:977e87915078 639 ret = sn_nsdl_unregister_endpoint(_nsdl_handle);
vithyat 0:977e87915078 640 }
vithyat 0:977e87915078 641 if (ret >= 0) {
vithyat 0:977e87915078 642 success = true;
vithyat 0:977e87915078 643 }
vithyat 0:977e87915078 644 return success;
vithyat 0:977e87915078 645 }
vithyat 0:977e87915078 646
vithyat 0:977e87915078 647 // XXX: move these to common place, no need to copy these wrappers to multiple places:
vithyat 0:977e87915078 648 void *M2MNsdlInterface::memory_alloc(uint32_t size)
vithyat 0:977e87915078 649 {
vithyat 0:977e87915078 650 if(size)
vithyat 0:977e87915078 651 return malloc(size);
vithyat 0:977e87915078 652 else
vithyat 0:977e87915078 653 return 0;
vithyat 0:977e87915078 654 }
vithyat 0:977e87915078 655
vithyat 0:977e87915078 656 void M2MNsdlInterface::memory_free(void *ptr)
vithyat 0:977e87915078 657 {
vithyat 0:977e87915078 658 free(ptr);
vithyat 0:977e87915078 659 }
vithyat 0:977e87915078 660
vithyat 0:977e87915078 661 uint8_t* M2MNsdlInterface::alloc_string_copy(const uint8_t* source, uint16_t size)
vithyat 0:977e87915078 662 {
vithyat 0:977e87915078 663 assert(source != NULL);
vithyat 0:977e87915078 664
vithyat 0:977e87915078 665 uint8_t* result = (uint8_t*)memory_alloc(size + 1);
vithyat 0:977e87915078 666 if (result) {
vithyat 0:977e87915078 667 memcpy(result, source, size);
vithyat 0:977e87915078 668 result[size] = '\0';
vithyat 0:977e87915078 669 }
vithyat 0:977e87915078 670 return result;
vithyat 0:977e87915078 671 }
vithyat 0:977e87915078 672
vithyat 0:977e87915078 673 uint8_t M2MNsdlInterface::send_to_server_callback(struct nsdl_s * /*nsdl_handle*/,
vithyat 0:977e87915078 674 sn_nsdl_capab_e /*protocol*/,
vithyat 0:977e87915078 675 uint8_t *data_ptr,
vithyat 0:977e87915078 676 uint16_t data_len,
vithyat 0:977e87915078 677 sn_nsdl_addr_s *address)
vithyat 0:977e87915078 678 {
vithyat 0:977e87915078 679 tr_debug("M2MNsdlInterface::send_to_server_callback(data size %d)", data_len);
vithyat 0:977e87915078 680 _observer.coap_message_ready(data_ptr,data_len,address);
vithyat 0:977e87915078 681 return 1;
vithyat 0:977e87915078 682 }
vithyat 0:977e87915078 683
vithyat 0:977e87915078 684 uint8_t M2MNsdlInterface::received_from_server_callback(struct nsdl_s *nsdl_handle,
vithyat 0:977e87915078 685 sn_coap_hdr_s *coap_header,
vithyat 0:977e87915078 686 sn_nsdl_addr_s *address)
vithyat 0:977e87915078 687 {
vithyat 0:977e87915078 688 tr_debug("M2MNsdlInterface::received_from_server_callback");
vithyat 0:977e87915078 689 _observer.coap_data_processed();
vithyat 0:977e87915078 690 uint8_t value = 0;
vithyat 0:977e87915078 691 request_context_s request_context;
vithyat 0:977e87915078 692 if(nsdl_handle && coap_header) {
vithyat 0:977e87915078 693 bool is_bootstrap_msg = nsdl_handle->is_bs_server;
vithyat 0:977e87915078 694
vithyat 0:977e87915078 695 if (coap_header->token_ptr &&
vithyat 0:977e87915078 696 coap_header->token_len == sizeof(nsdl_handle->register_token) &&
vithyat 0:977e87915078 697 memcmp(coap_header->token_ptr, &nsdl_handle->register_token, sizeof(nsdl_handle->register_token)) == 0) {
vithyat 0:977e87915078 698
vithyat 0:977e87915078 699 handle_register_response(coap_header);
vithyat 0:977e87915078 700
vithyat 0:977e87915078 701 } else if (coap_header->token_ptr &&
vithyat 0:977e87915078 702 coap_header->token_len == sizeof(nsdl_handle->unregister_token) &&
vithyat 0:977e87915078 703 memcmp(coap_header->token_ptr,
vithyat 0:977e87915078 704 &nsdl_handle->unregister_token,
vithyat 0:977e87915078 705 sizeof(nsdl_handle->unregister_token)) == 0) {
vithyat 0:977e87915078 706
vithyat 0:977e87915078 707 handle_unregister_response(coap_header);
vithyat 0:977e87915078 708
vithyat 0:977e87915078 709 } else if (coap_header->token_ptr &&
vithyat 0:977e87915078 710 coap_header->token_len == sizeof(nsdl_handle->update_register_token) &&
vithyat 0:977e87915078 711 memcmp(coap_header->token_ptr,
vithyat 0:977e87915078 712 &nsdl_handle->update_register_token,
vithyat 0:977e87915078 713 sizeof(nsdl_handle->update_register_token)) == 0) {
vithyat 0:977e87915078 714
vithyat 0:977e87915078 715 handle_register_update_response(coap_header);
vithyat 0:977e87915078 716
vithyat 0:977e87915078 717 } else if (coap_header->token_ptr && is_response_to_request(coap_header, request_context)) {
vithyat 0:977e87915078 718
vithyat 0:977e87915078 719 handle_request_response(coap_header, &request_context);
vithyat 0:977e87915078 720
vithyat 0:977e87915078 721 }
vithyat 0:977e87915078 722 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 723 else if (coap_header->token_ptr &&
vithyat 0:977e87915078 724 coap_header->token_len == sizeof(nsdl_handle->bootstrap_token) &&
vithyat 0:977e87915078 725 memcmp(coap_header->token_ptr, &nsdl_handle->bootstrap_token, sizeof(nsdl_handle->bootstrap_token)) == 0) {
vithyat 0:977e87915078 726
vithyat 0:977e87915078 727 handle_bootstrap_response(coap_header);
vithyat 0:977e87915078 728
vithyat 0:977e87915078 729 }
vithyat 0:977e87915078 730 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 731 else {
vithyat 0:977e87915078 732
vithyat 0:977e87915078 733 sn_coap_hdr_s *coap_response = NULL;
vithyat 0:977e87915078 734 bool execute_value_updated = false;
vithyat 0:977e87915078 735 M2MObjectInstance *obj_instance = NULL;
vithyat 0:977e87915078 736
vithyat 0:977e87915078 737 if (COAP_MSG_CODE_REQUEST_PUT == coap_header->msg_code) {
vithyat 0:977e87915078 738 if (is_bootstrap_msg) {
vithyat 0:977e87915078 739 send_empty_ack(coap_header, address);
vithyat 0:977e87915078 740 nsdl_coap_data_s *nsdl_coap_data = create_coap_event_data(coap_header,
vithyat 0:977e87915078 741 address,
vithyat 0:977e87915078 742 nsdl_handle,
vithyat 0:977e87915078 743 coap_header->msg_code);
vithyat 0:977e87915078 744 if (nsdl_coap_data) {
vithyat 0:977e87915078 745 _event.data.event_type = MBED_CLIENT_NSDLINTERFACE_BS_PUT_EVENT;
vithyat 0:977e87915078 746 _event.data.data_ptr = (void*)nsdl_coap_data;
vithyat 0:977e87915078 747 eventOS_event_send_user_allocated(&_event);
vithyat 0:977e87915078 748 return 2; // freeing will be performed in MBED_CLIENT_NSDLINTERFACE_BS_PUT_EVENT event
vithyat 0:977e87915078 749 } else {
vithyat 0:977e87915078 750 tr_error("M2MNsdlInterface::received_from_server_callback() - BS PUT failed to allocate nsdl_coap_data_s!");
vithyat 0:977e87915078 751 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 752 coap_header,
vithyat 0:977e87915078 753 COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE);
vithyat 0:977e87915078 754 }
vithyat 0:977e87915078 755
vithyat 0:977e87915078 756 } else {
vithyat 0:977e87915078 757 tr_debug("M2MNsdlInterface::received_from_server_callback - Method not allowed (PUT).");
vithyat 0:977e87915078 758 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 759 coap_header,
vithyat 0:977e87915078 760 COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED);
vithyat 0:977e87915078 761 }
vithyat 0:977e87915078 762 }
vithyat 0:977e87915078 763 else if (COAP_MSG_CODE_REQUEST_DELETE == coap_header->msg_code) {
vithyat 0:977e87915078 764 if (is_bootstrap_msg) {
vithyat 0:977e87915078 765 handle_bootstrap_delete(coap_header, address);
vithyat 0:977e87915078 766 } else {
vithyat 0:977e87915078 767 tr_debug("M2MNsdlInterface::received_from_server_callback - Method not allowed (DELETE).");
vithyat 0:977e87915078 768 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 769 coap_header,
vithyat 0:977e87915078 770 COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED);
vithyat 0:977e87915078 771 }
vithyat 0:977e87915078 772 } else if (COAP_MSG_CODE_REQUEST_POST == coap_header->msg_code) {
vithyat 0:977e87915078 773
vithyat 0:977e87915078 774 execute_value_updated = handle_post_response(coap_header,
vithyat 0:977e87915078 775 address,
vithyat 0:977e87915078 776 coap_response,
vithyat 0:977e87915078 777 obj_instance,
vithyat 0:977e87915078 778 is_bootstrap_msg);
vithyat 0:977e87915078 779
vithyat 0:977e87915078 780 } else if (COAP_STATUS_BUILDER_BLOCK_SENDING_DONE == coap_header->coap_status &&
vithyat 0:977e87915078 781 (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT ||
vithyat 0:977e87915078 782 coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CHANGED)) {
vithyat 0:977e87915078 783
vithyat 0:977e87915078 784 coap_response_s *resp = find_response(coap_header->msg_id);
vithyat 0:977e87915078 785 if (resp) {
vithyat 0:977e87915078 786 M2MBase *base = find_resource(resp->uri_path);
vithyat 0:977e87915078 787 if (base) {
vithyat 0:977e87915078 788 if (resp->type == M2MBase::BLOCK_SUBSCRIBE) {
vithyat 0:977e87915078 789 sn_coap_msg_code_e code;
vithyat 0:977e87915078 790 // This case coap response is not needed.
vithyat 0:977e87915078 791 // coap_header have the payload length and the observation number which is needed in following call.
vithyat 0:977e87915078 792 base->handle_observation(nsdl_handle, *coap_header, *coap_header, this, code);
vithyat 0:977e87915078 793 base->start_observation(*coap_header, this);
vithyat 0:977e87915078 794 } else {
vithyat 0:977e87915078 795 handle_message_delivered(base, resp->type);
vithyat 0:977e87915078 796 }
vithyat 0:977e87915078 797
vithyat 0:977e87915078 798 remove_item_from_response_list(NULL, coap_header->msg_id);
vithyat 0:977e87915078 799 }
vithyat 0:977e87915078 800 }
vithyat 0:977e87915078 801
vithyat 0:977e87915078 802 // Retransmission done
vithyat 0:977e87915078 803 } else if (COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED == coap_header->coap_status ||
vithyat 0:977e87915078 804 COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED == coap_header->coap_status) {
vithyat 0:977e87915078 805
vithyat 0:977e87915078 806 tr_info("M2MNsdlInterface::received_from_server_callback - message sending failed, id %d", coap_header->msg_id);
vithyat 0:977e87915078 807 _observer.registration_error(M2MInterface::NetworkError, true);
vithyat 0:977e87915078 808
vithyat 0:977e87915078 809 // Handle Server-side expections during registration flow
vithyat 0:977e87915078 810 // Client might receive error from server due to temporary connection/operability reasons,
vithyat 0:977e87915078 811 // server might not recover the flow in this case, so it is better for Client to restart registration.
vithyat 0:977e87915078 812 } else if (COAP_MSG_CODE_EMPTY == coap_header->msg_code) {
vithyat 0:977e87915078 813
vithyat 0:977e87915078 814 handle_empty_ack(coap_header, is_bootstrap_msg);
vithyat 0:977e87915078 815
vithyat 0:977e87915078 816 } else if (nsdl_handle->register_token &&
vithyat 0:977e87915078 817 ((coap_header->msg_code == COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR) ||
vithyat 0:977e87915078 818 (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_BAD_GATEWAY) ||
vithyat 0:977e87915078 819 (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE) ||
vithyat 0:977e87915078 820 (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT))) {
vithyat 0:977e87915078 821
vithyat 0:977e87915078 822 tr_error("M2MNsdlInterface::received_from_server_callback - registration error %d", coap_header->msg_code);
vithyat 0:977e87915078 823 tr_error("M2MNsdlInterface::received_from_server_callback - unexpected error received from server");
vithyat 0:977e87915078 824 // Try to do clean register again
vithyat 0:977e87915078 825 _observer.registration_error(M2MInterface::NetworkError, true);
vithyat 0:977e87915078 826
vithyat 0:977e87915078 827 } else {
vithyat 0:977e87915078 828 // Add warn for any message that gets this far. We might be missing some handling in above.
vithyat 0:977e87915078 829 tr_warn("M2MNsdlInterface::received_from_server_callback - msg was ignored %d", coap_header->msg_code);
vithyat 0:977e87915078 830 }
vithyat 0:977e87915078 831
vithyat 0:977e87915078 832 // Send response to server
vithyat 0:977e87915078 833 if (coap_response) {
vithyat 0:977e87915078 834 tr_debug("M2MNsdlInterface::received_from_server_callback - send CoAP response");
vithyat 0:977e87915078 835 (sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response) == 0) ? value = 0 : value = 1;
vithyat 0:977e87915078 836 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 837 }
vithyat 0:977e87915078 838
vithyat 0:977e87915078 839 // Tell to application that value has been updated
vithyat 0:977e87915078 840 if (execute_value_updated) {
vithyat 0:977e87915078 841 value_updated(obj_instance);
vithyat 0:977e87915078 842 }
vithyat 0:977e87915078 843 }
vithyat 0:977e87915078 844 }
vithyat 0:977e87915078 845 return value;
vithyat 0:977e87915078 846 }
vithyat 0:977e87915078 847 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 848 M2MBase::Operation M2MNsdlInterface::operation_for_message_code(sn_coap_msg_code_e code)
vithyat 0:977e87915078 849 {
vithyat 0:977e87915078 850 M2MBase::Operation ret_val;
vithyat 0:977e87915078 851 switch (code) {
vithyat 0:977e87915078 852 case COAP_MSG_CODE_REQUEST_POST: {
vithyat 0:977e87915078 853 ret_val = M2MBase::POST_ALLOWED;
vithyat 0:977e87915078 854 break;
vithyat 0:977e87915078 855 }
vithyat 0:977e87915078 856 case COAP_MSG_CODE_REQUEST_GET: {
vithyat 0:977e87915078 857 ret_val = M2MBase::GET_ALLOWED;
vithyat 0:977e87915078 858 break;
vithyat 0:977e87915078 859 }
vithyat 0:977e87915078 860 case COAP_MSG_CODE_REQUEST_PUT: {
vithyat 0:977e87915078 861 ret_val = M2MBase::PUT_ALLOWED;
vithyat 0:977e87915078 862 break;
vithyat 0:977e87915078 863 }
vithyat 0:977e87915078 864 default:
vithyat 0:977e87915078 865 ret_val = M2MBase::NOT_ALLOWED;
vithyat 0:977e87915078 866 break;
vithyat 0:977e87915078 867 }
vithyat 0:977e87915078 868 return ret_val;
vithyat 0:977e87915078 869 }
vithyat 0:977e87915078 870 #endif // ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 871
vithyat 0:977e87915078 872 uint8_t M2MNsdlInterface::resource_callback(struct nsdl_s *nsdl_handle,
vithyat 0:977e87915078 873 sn_coap_hdr_s *received_coap_header,
vithyat 0:977e87915078 874 sn_nsdl_addr_s *address,
vithyat 0:977e87915078 875 sn_nsdl_capab_e /*nsdl_capab*/)
vithyat 0:977e87915078 876 {
vithyat 0:977e87915078 877 bool async_response = false;
vithyat 0:977e87915078 878 tr_debug("M2MNsdlInterface::resource_callback()");
vithyat 0:977e87915078 879
vithyat 0:977e87915078 880 assert(received_coap_header);
vithyat 0:977e87915078 881 _observer.coap_data_processed();
vithyat 0:977e87915078 882
vithyat 0:977e87915078 883 String resource_name = coap_to_string(received_coap_header->uri_path_ptr,
vithyat 0:977e87915078 884 received_coap_header->uri_path_len);
vithyat 0:977e87915078 885
vithyat 0:977e87915078 886 M2MBase *base = find_resource(resource_name);
vithyat 0:977e87915078 887
vithyat 0:977e87915078 888 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 889 if (base) {
vithyat 0:977e87915078 890 if (base->is_async_coap_request_callback_set()) {
vithyat 0:977e87915078 891 async_response = true;
vithyat 0:977e87915078 892 if (!handle_delayed_response_store(resource_name.c_str(),
vithyat 0:977e87915078 893 received_coap_header,
vithyat 0:977e87915078 894 address,
vithyat 0:977e87915078 895 M2MBase::DELAYED_RESPONSE)) {
vithyat 0:977e87915078 896 return 0;
vithyat 0:977e87915078 897 }
vithyat 0:977e87915078 898 }
vithyat 0:977e87915078 899 }
vithyat 0:977e87915078 900 #endif // ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 901
vithyat 0:977e87915078 902 // Use piggypacked response for any other types than POST
vithyat 0:977e87915078 903 if (received_coap_header->msg_code != COAP_MSG_CODE_REQUEST_POST) {
vithyat 0:977e87915078 904 // If there is a async callback set for this resource than skip this and
vithyat 0:977e87915078 905 // send empty ACK below, application will be responsible to send final response.
vithyat 0:977e87915078 906 if (!async_response) {
vithyat 0:977e87915078 907 uint8_t status = resource_callback_handle_event(received_coap_header, address);
vithyat 0:977e87915078 908 if (received_coap_header->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
vithyat 0:977e87915078 909 memory_free(received_coap_header->payload_ptr);
vithyat 0:977e87915078 910 }
vithyat 0:977e87915078 911 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, received_coap_header);
vithyat 0:977e87915078 912 return status;
vithyat 0:977e87915078 913 }
vithyat 0:977e87915078 914 }
vithyat 0:977e87915078 915
vithyat 0:977e87915078 916 // Only handle this in case of delayed response for POST and not for
vithyat 0:977e87915078 917 // implementation behind ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 918 if (base && !async_response) {
vithyat 0:977e87915078 919 M2MResource *res = NULL;
vithyat 0:977e87915078 920 if (M2MBase::Resource == base->base_type()) {
vithyat 0:977e87915078 921 res = static_cast<M2MResource *> (base);
vithyat 0:977e87915078 922 }
vithyat 0:977e87915078 923
vithyat 0:977e87915078 924 #ifndef DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 925 if (res && res->delayed_response()) {
vithyat 0:977e87915078 926 if (!handle_delayed_response_store(resource_name.c_str(),
vithyat 0:977e87915078 927 received_coap_header,
vithyat 0:977e87915078 928 address,
vithyat 0:977e87915078 929 M2MBase::DELAYED_POST_RESPONSE)) {
vithyat 0:977e87915078 930 return 0;
vithyat 0:977e87915078 931 }
vithyat 0:977e87915078 932 }
vithyat 0:977e87915078 933 #endif // DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 934
vithyat 0:977e87915078 935 }
vithyat 0:977e87915078 936
vithyat 0:977e87915078 937 send_empty_ack(received_coap_header, address);
vithyat 0:977e87915078 938 nsdl_coap_data_s *nsdl_coap_data = create_coap_event_data(received_coap_header,
vithyat 0:977e87915078 939 address,
vithyat 0:977e87915078 940 nsdl_handle,
vithyat 0:977e87915078 941 received_coap_header->msg_code);
vithyat 0:977e87915078 942 if (nsdl_coap_data) {
vithyat 0:977e87915078 943 _event.data.event_type = MBED_CLIENT_NSDLINTERFACE_EVENT;
vithyat 0:977e87915078 944 _event.data.data_ptr = (void*)nsdl_coap_data;
vithyat 0:977e87915078 945 eventOS_event_send_user_allocated(&_event);
vithyat 0:977e87915078 946 } else {
vithyat 0:977e87915078 947 tr_error("M2MNsdlInterface::resource_callback() - failed to allocate nsdl_coap_data_s!");
vithyat 0:977e87915078 948 }
vithyat 0:977e87915078 949 return 0;
vithyat 0:977e87915078 950 }
vithyat 0:977e87915078 951
vithyat 0:977e87915078 952 uint8_t M2MNsdlInterface::resource_callback_handle_event(sn_coap_hdr_s *received_coap_header,
vithyat 0:977e87915078 953 sn_nsdl_addr_s *address)
vithyat 0:977e87915078 954 {
vithyat 0:977e87915078 955 tr_debug("M2MNsdlInterface::resource_callback_handle_event");
vithyat 0:977e87915078 956 uint8_t result = 1;
vithyat 0:977e87915078 957 uint8_t *payload = NULL;
vithyat 0:977e87915078 958 bool free_payload = true;
vithyat 0:977e87915078 959 sn_coap_hdr_s *coap_response = NULL;
vithyat 0:977e87915078 960 sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 4.00
vithyat 0:977e87915078 961 String resource_name = coap_to_string(received_coap_header->uri_path_ptr,
vithyat 0:977e87915078 962 received_coap_header->uri_path_len);
vithyat 0:977e87915078 963
vithyat 0:977e87915078 964 bool execute_value_updated = false;
vithyat 0:977e87915078 965 M2MBase* base = find_resource(resource_name);
vithyat 0:977e87915078 966 bool subscribed = false;
vithyat 0:977e87915078 967 if (base) {
vithyat 0:977e87915078 968 if (COAP_MSG_CODE_REQUEST_GET == received_coap_header->msg_code) {
vithyat 0:977e87915078 969 coap_response = base->handle_get_request(_nsdl_handle, received_coap_header,this);
vithyat 0:977e87915078 970
vithyat 0:977e87915078 971 if (coap_response &&
vithyat 0:977e87915078 972 coap_response->options_list_ptr &&
vithyat 0:977e87915078 973 coap_response->options_list_ptr->observe != STOP_OBSERVATION &&
vithyat 0:977e87915078 974 coap_response->options_list_ptr->observe != -1 &&
vithyat 0:977e87915078 975 coap_response->token_ptr) {
vithyat 0:977e87915078 976 if (M2MBase::is_blockwise_needed(_nsdl_handle, coap_response->payload_len)) {
vithyat 0:977e87915078 977 store_to_response_list(resource_name.c_str(),
vithyat 0:977e87915078 978 received_coap_header->msg_id,
vithyat 0:977e87915078 979 M2MBase::BLOCK_SUBSCRIBE);
vithyat 0:977e87915078 980 }
vithyat 0:977e87915078 981 subscribed = true;
vithyat 0:977e87915078 982 }
vithyat 0:977e87915078 983 } else if (COAP_MSG_CODE_REQUEST_PUT == received_coap_header->msg_code) {
vithyat 0:977e87915078 984 coap_response = base->handle_put_request(_nsdl_handle, received_coap_header, this, execute_value_updated);
vithyat 0:977e87915078 985 } else if (COAP_MSG_CODE_REQUEST_POST == received_coap_header->msg_code) {
vithyat 0:977e87915078 986 if (base->base_type() == M2MBase::ResourceInstance) {
vithyat 0:977e87915078 987 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 988 } else {
vithyat 0:977e87915078 989 coap_response = base->handle_post_request(_nsdl_handle,
vithyat 0:977e87915078 990 received_coap_header,
vithyat 0:977e87915078 991 this,
vithyat 0:977e87915078 992 execute_value_updated,
vithyat 0:977e87915078 993 address);
vithyat 0:977e87915078 994
vithyat 0:977e87915078 995 #ifndef DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 996 if (base->base_type() == M2MBase::Resource) {
vithyat 0:977e87915078 997 M2MResource *res = (M2MResource*) base;
vithyat 0:977e87915078 998 if (res->delayed_response()) {
vithyat 0:977e87915078 999 tr_debug("M2MNsdlInterface::resource_callback_handle_event - final response sent by application");
vithyat 0:977e87915078 1000 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 1001 return 0;
vithyat 0:977e87915078 1002 }
vithyat 0:977e87915078 1003 }
vithyat 0:977e87915078 1004 #endif // DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 1005 // Separate response used for POST
vithyat 0:977e87915078 1006 if (coap_response) {
vithyat 0:977e87915078 1007 coap_response->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
vithyat 0:977e87915078 1008 }
vithyat 0:977e87915078 1009 }
vithyat 0:977e87915078 1010 } else if (COAP_MSG_CODE_REQUEST_DELETE == received_coap_header->msg_code) {
vithyat 0:977e87915078 1011 // Delete the object instance
vithyat 0:977e87915078 1012 M2MBase::BaseType type = base->base_type();
vithyat 0:977e87915078 1013 if(M2MBase::ObjectInstance == type) {
vithyat 0:977e87915078 1014 M2MBase* base_object = find_resource(base->uri_path());
vithyat 0:977e87915078 1015 if(base_object) {
vithyat 0:977e87915078 1016 M2MObject &object = ((M2MObjectInstance*)base_object)->get_parent_object();
vithyat 0:977e87915078 1017 int slash_found = resource_name.find_last_of('/');
vithyat 0:977e87915078 1018 // Object instance validty checks done in upper level, no need for error handling
vithyat 0:977e87915078 1019 if (slash_found != -1) {
vithyat 0:977e87915078 1020 const String object_name = resource_name.substr(slash_found + 1, resource_name.length());
vithyat 0:977e87915078 1021 if (object.remove_object_instance(strtoul(
vithyat 0:977e87915078 1022 object_name.c_str(),
vithyat 0:977e87915078 1023 NULL,
vithyat 0:977e87915078 1024 10))) {
vithyat 0:977e87915078 1025 msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
vithyat 0:977e87915078 1026 }
vithyat 0:977e87915078 1027 }
vithyat 0:977e87915078 1028 }
vithyat 0:977e87915078 1029 } else {
vithyat 0:977e87915078 1030 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
vithyat 0:977e87915078 1031 }
vithyat 0:977e87915078 1032 }
vithyat 0:977e87915078 1033 } else {
vithyat 0:977e87915078 1034 tr_error("M2MNsdlInterface::resource_callback_handle_event() - Resource NOT FOUND");
vithyat 0:977e87915078 1035 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; // 4.00
vithyat 0:977e87915078 1036 }
vithyat 0:977e87915078 1037
vithyat 0:977e87915078 1038 if (!coap_response) {
vithyat 0:977e87915078 1039 coap_response = sn_nsdl_build_response(_nsdl_handle, received_coap_header, msg_code);
vithyat 0:977e87915078 1040 }
vithyat 0:977e87915078 1041
vithyat 0:977e87915078 1042 // This copy will be passed to resource instance
vithyat 0:977e87915078 1043 if (received_coap_header->payload_len > 0 && received_coap_header->payload_ptr) {
vithyat 0:977e87915078 1044 payload = (uint8_t *) memory_alloc(received_coap_header->payload_len);
vithyat 0:977e87915078 1045 if (payload) {
vithyat 0:977e87915078 1046 assert(received_coap_header->payload_ptr);
vithyat 0:977e87915078 1047 memcpy(payload, received_coap_header->payload_ptr, received_coap_header->payload_len);
vithyat 0:977e87915078 1048 } else {
vithyat 0:977e87915078 1049 if (coap_response) {
vithyat 0:977e87915078 1050 coap_response->msg_code = COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE;
vithyat 0:977e87915078 1051 }
vithyat 0:977e87915078 1052 }
vithyat 0:977e87915078 1053 }
vithyat 0:977e87915078 1054
vithyat 0:977e87915078 1055 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 1056 bool async_request_callback_called = false;
vithyat 0:977e87915078 1057 #endif
vithyat 0:977e87915078 1058
vithyat 0:977e87915078 1059 if (coap_response &&
vithyat 0:977e87915078 1060 coap_response->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING &&
vithyat 0:977e87915078 1061 coap_response->msg_code != COAP_MSG_CODE_EMPTY) {
vithyat 0:977e87915078 1062 bool send_response = true;
vithyat 0:977e87915078 1063
vithyat 0:977e87915078 1064 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 1065 if (base) {
vithyat 0:977e87915078 1066 if (base->is_async_coap_request_callback_set()) {
vithyat 0:977e87915078 1067 // In case of error or callback not found go to default response flow
vithyat 0:977e87915078 1068 if (coap_response->msg_code < COAP_MSG_CODE_RESPONSE_BAD_REQUEST) {
vithyat 0:977e87915078 1069 M2MBase::Operation operation = operation_for_message_code(received_coap_header->msg_code);
vithyat 0:977e87915078 1070 tr_debug("M2MNsdlInterface::resource_callback_handle_event - final response sent by application for "
vithyat 0:977e87915078 1071 "operation 0x%x", operation);
vithyat 0:977e87915078 1072 base->call_async_coap_request_callback(received_coap_header,
vithyat 0:977e87915078 1073 operation,
vithyat 0:977e87915078 1074 async_request_callback_called);
vithyat 0:977e87915078 1075
vithyat 0:977e87915078 1076 // Response sent by the application
vithyat 0:977e87915078 1077 if (async_request_callback_called) {
vithyat 0:977e87915078 1078 send_response = false;
vithyat 0:977e87915078 1079 result = 0;
vithyat 0:977e87915078 1080 } else {
vithyat 0:977e87915078 1081 tr_error("M2MNsdlInterface::resource_callback_handle_event - async callback not called!");
vithyat 0:977e87915078 1082 }
vithyat 0:977e87915078 1083 } else {
vithyat 0:977e87915078 1084 remove_item_from_response_list(base->uri_path(), UNDEFINED_MSG_ID);
vithyat 0:977e87915078 1085 }
vithyat 0:977e87915078 1086 }
vithyat 0:977e87915078 1087 }
vithyat 0:977e87915078 1088 #endif //ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 1089
vithyat 0:977e87915078 1090 // Send CoAP response only for methods which are not handled by application
vithyat 0:977e87915078 1091 if (send_response) {
vithyat 0:977e87915078 1092 (sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response) == 0) ? result = 0 : result = 1;
vithyat 0:977e87915078 1093 }
vithyat 0:977e87915078 1094
vithyat 0:977e87915078 1095 free(coap_response->payload_ptr);
vithyat 0:977e87915078 1096 coap_response->payload_ptr = NULL;
vithyat 0:977e87915078 1097
vithyat 0:977e87915078 1098 // See if there any pending notification to be sent after resource is subscribed.
vithyat 0:977e87915078 1099 if (subscribed) {
vithyat 0:977e87915078 1100 _notification_handler->send_notification(this);
vithyat 0:977e87915078 1101 }
vithyat 0:977e87915078 1102 }
vithyat 0:977e87915078 1103
vithyat 0:977e87915078 1104 // If the external blockwise storing is enabled call value updated only when all blocks have been received
vithyat 0:977e87915078 1105 if (execute_value_updated &&
vithyat 0:977e87915078 1106 coap_response &&
vithyat 0:977e87915078 1107 coap_response->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING &&
vithyat 0:977e87915078 1108 coap_response->msg_code < COAP_MSG_CODE_RESPONSE_BAD_REQUEST) {
vithyat 0:977e87915078 1109 if ((COAP_MSG_CODE_REQUEST_PUT == received_coap_header->msg_code) &&
vithyat 0:977e87915078 1110 (base->base_type() == M2MBase::Resource ||
vithyat 0:977e87915078 1111 base->base_type() == M2MBase::ResourceInstance)) {
vithyat 0:977e87915078 1112 M2MResourceBase* res = (M2MResourceBase*)base;
vithyat 0:977e87915078 1113
vithyat 0:977e87915078 1114 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 1115 if (!async_request_callback_called) {
vithyat 0:977e87915078 1116 #endif
vithyat 0:977e87915078 1117 // Clear the old resource value since the data is now passed to application
vithyat 0:977e87915078 1118 if (res->block_message() && res->block_message()->is_block_message()) {
vithyat 0:977e87915078 1119 res->clear_value();
vithyat 0:977e87915078 1120 } else {
vithyat 0:977e87915078 1121 // Ownership of payload moved to resource, skip the freeing.
vithyat 0:977e87915078 1122 free_payload = false;
vithyat 0:977e87915078 1123 res->set_value_raw(payload, received_coap_header->payload_len);
vithyat 0:977e87915078 1124 }
vithyat 0:977e87915078 1125 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 1126 }
vithyat 0:977e87915078 1127 #endif
vithyat 0:977e87915078 1128 }
vithyat 0:977e87915078 1129 if (coap_response->msg_code != COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE) {
vithyat 0:977e87915078 1130 value_updated(base);
vithyat 0:977e87915078 1131 }
vithyat 0:977e87915078 1132 }
vithyat 0:977e87915078 1133
vithyat 0:977e87915078 1134 if (free_payload) {
vithyat 0:977e87915078 1135 free(payload);
vithyat 0:977e87915078 1136 }
vithyat 0:977e87915078 1137
vithyat 0:977e87915078 1138 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 1139
vithyat 0:977e87915078 1140 return result;
vithyat 0:977e87915078 1141 }
vithyat 0:977e87915078 1142
vithyat 0:977e87915078 1143 bool M2MNsdlInterface::process_received_data(uint8_t *data,
vithyat 0:977e87915078 1144 uint16_t data_size,
vithyat 0:977e87915078 1145 sn_nsdl_addr_s *address)
vithyat 0:977e87915078 1146 {
vithyat 0:977e87915078 1147 tr_debug("M2MNsdlInterface::process_received_data(data size %d)", data_size);
vithyat 0:977e87915078 1148 return (0 == sn_nsdl_process_coap(_nsdl_handle,
vithyat 0:977e87915078 1149 data,
vithyat 0:977e87915078 1150 data_size,
vithyat 0:977e87915078 1151 address)) ? true : false;
vithyat 0:977e87915078 1152 }
vithyat 0:977e87915078 1153
vithyat 0:977e87915078 1154 void M2MNsdlInterface::stop_timers()
vithyat 0:977e87915078 1155 {
vithyat 0:977e87915078 1156 tr_debug("M2MNsdlInterface::stop_timers()");
vithyat 0:977e87915078 1157 _registration_timer.stop_timer();
vithyat 0:977e87915078 1158 _nsdl_execution_timer.stop_timer();
vithyat 0:977e87915078 1159 _nsdl_execution_timer_running = false;
vithyat 0:977e87915078 1160 _bootstrap_id = 0;
vithyat 0:977e87915078 1161 _nsdl_handle->update_register_token = 0;
vithyat 0:977e87915078 1162 _nsdl_handle->unregister_token = 0;
vithyat 0:977e87915078 1163 _download_retry_timer.stop_timer();
vithyat 0:977e87915078 1164 }
vithyat 0:977e87915078 1165
vithyat 0:977e87915078 1166 void M2MNsdlInterface::timer_expired(M2MTimerObserver::Type type)
vithyat 0:977e87915078 1167 {
vithyat 0:977e87915078 1168 if(M2MTimerObserver::NsdlExecution == type) {
vithyat 0:977e87915078 1169 sn_nsdl_exec(_nsdl_handle, _counter_for_nsdl);
vithyat 0:977e87915078 1170 _counter_for_nsdl++;
vithyat 0:977e87915078 1171 send_coap_ping();
vithyat 0:977e87915078 1172 } else if((M2MTimerObserver::Registration) == type &&
vithyat 0:977e87915078 1173 (is_unregister_ongoing() == false) &&
vithyat 0:977e87915078 1174 (is_update_register_ongoing() == false)) {
vithyat 0:977e87915078 1175 tr_debug("M2MNsdlInterface::timer_expired - Send update registration");
vithyat 0:977e87915078 1176 if (!send_update_registration()) {
vithyat 0:977e87915078 1177 // Most likely case would be memory allocation failure
vithyat 0:977e87915078 1178 _observer.registration_error(M2MInterface::MemoryFail, false);
vithyat 0:977e87915078 1179 }
vithyat 0:977e87915078 1180 } else if (M2MTimerObserver::RetryTimer == type) {
vithyat 0:977e87915078 1181 send_pending_request();
vithyat 0:977e87915078 1182 }
vithyat 0:977e87915078 1183 }
vithyat 0:977e87915078 1184
vithyat 0:977e87915078 1185 bool M2MNsdlInterface::observation_to_be_sent(M2MBase *object,
vithyat 0:977e87915078 1186 uint16_t obs_number,
vithyat 0:977e87915078 1187 const m2m::Vector<uint16_t> &changed_instance_ids,
vithyat 0:977e87915078 1188 bool send_object)
vithyat 0:977e87915078 1189 {
vithyat 0:977e87915078 1190 claim_mutex();
vithyat 0:977e87915078 1191
vithyat 0:977e87915078 1192 if (object && _nsdl_execution_timer_running && _registered) {
vithyat 0:977e87915078 1193 tr_debug("M2MNsdlInterface::observation_to_be_sent() uri %s", object->uri_path());
vithyat 0:977e87915078 1194
vithyat 0:977e87915078 1195 if (!_notification_send_ongoing) {
vithyat 0:977e87915078 1196 _notification_send_ongoing = true;
vithyat 0:977e87915078 1197 object->report_handler()->set_notification_in_queue(false);
vithyat 0:977e87915078 1198 M2MBase::BaseType type = object->base_type();
vithyat 0:977e87915078 1199
vithyat 0:977e87915078 1200 if (type == M2MBase::Object) {
vithyat 0:977e87915078 1201 send_object_observation(static_cast<M2MObject*> (object),
vithyat 0:977e87915078 1202 obs_number,
vithyat 0:977e87915078 1203 changed_instance_ids,
vithyat 0:977e87915078 1204 send_object);
vithyat 0:977e87915078 1205 } else if (type == M2MBase::ObjectInstance) {
vithyat 0:977e87915078 1206 send_object_instance_observation(static_cast<M2MObjectInstance*> (object), obs_number);
vithyat 0:977e87915078 1207 } else if (type == M2MBase::Resource) {
vithyat 0:977e87915078 1208 send_resource_observation(static_cast<M2MResource*> (object), obs_number);
vithyat 0:977e87915078 1209 }
vithyat 0:977e87915078 1210
vithyat 0:977e87915078 1211 release_mutex();
vithyat 0:977e87915078 1212 return true;
vithyat 0:977e87915078 1213 } else {
vithyat 0:977e87915078 1214 tr_info("M2MNsdlInterface::observation_to_be_sent() - send already in progress");
vithyat 0:977e87915078 1215 }
vithyat 0:977e87915078 1216 } else {
vithyat 0:977e87915078 1217 tr_info("M2MNsdlInterface::observation_to_be_sent() - object NULL, in reconnection mode or not registered");
vithyat 0:977e87915078 1218 }
vithyat 0:977e87915078 1219
vithyat 0:977e87915078 1220 release_mutex();
vithyat 0:977e87915078 1221
vithyat 0:977e87915078 1222 return false;
vithyat 0:977e87915078 1223 }
vithyat 0:977e87915078 1224
vithyat 0:977e87915078 1225 #ifndef DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 1226 void M2MNsdlInterface::send_delayed_response(M2MBase *base)
vithyat 0:977e87915078 1227 {
vithyat 0:977e87915078 1228 claim_mutex();
vithyat 0:977e87915078 1229 tr_debug("M2MNsdlInterface::send_delayed_response()");
vithyat 0:977e87915078 1230 M2MResource *resource = NULL;
vithyat 0:977e87915078 1231 if(base) {
vithyat 0:977e87915078 1232 if(M2MBase::Resource == base->base_type()) {
vithyat 0:977e87915078 1233 resource = static_cast<M2MResource *> (base);
vithyat 0:977e87915078 1234 }
vithyat 0:977e87915078 1235 if(resource) {
vithyat 0:977e87915078 1236 coap_response_s* resp = find_delayed_response(resource->uri_path(), M2MBase::DELAYED_POST_RESPONSE);
vithyat 0:977e87915078 1237 // If there is no response it means that this API is called
vithyat 0:977e87915078 1238 // before actual POST request has received the device
vithyat 0:977e87915078 1239 if (resp) {
vithyat 0:977e87915078 1240 sn_coap_hdr_s coap_response;
vithyat 0:977e87915078 1241
vithyat 0:977e87915078 1242 memset(&coap_response,0,sizeof(sn_coap_hdr_s));
vithyat 0:977e87915078 1243
vithyat 0:977e87915078 1244 coap_response.msg_type = COAP_MSG_TYPE_CONFIRMABLE;
vithyat 0:977e87915078 1245 coap_response.msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
vithyat 0:977e87915078 1246 resource->get_delayed_token(coap_response.token_ptr,coap_response.token_len);
vithyat 0:977e87915078 1247
vithyat 0:977e87915078 1248 uint32_t length = 0;
vithyat 0:977e87915078 1249 resource->get_value(coap_response.payload_ptr, length);
vithyat 0:977e87915078 1250 coap_response.payload_len = length;
vithyat 0:977e87915078 1251
vithyat 0:977e87915078 1252 if (sn_nsdl_send_coap_message(_nsdl_handle, &_nsdl_handle->server_address, &coap_response) >= 0) {
vithyat 0:977e87915078 1253 // Update msgid, this will be used to track server response
vithyat 0:977e87915078 1254 resp->msg_id = coap_response.msg_id;
vithyat 0:977e87915078 1255 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_SENT, M2MBase::DELAYED_POST_RESPONSE);
vithyat 0:977e87915078 1256 } else {
vithyat 0:977e87915078 1257 // Failed to create a message
vithyat 0:977e87915078 1258 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_SEND_FAILED, M2MBase::DELAYED_POST_RESPONSE);
vithyat 0:977e87915078 1259 // Remove stored response from the list
vithyat 0:977e87915078 1260 remove_item_from_response_list(resource->uri_path(), UNDEFINED_MSG_ID);
vithyat 0:977e87915078 1261 }
vithyat 0:977e87915078 1262
vithyat 0:977e87915078 1263 free(coap_response.payload_ptr);
vithyat 0:977e87915078 1264 free(coap_response.token_ptr);
vithyat 0:977e87915078 1265 } else {
vithyat 0:977e87915078 1266 tr_error("M2MNsdlInterface::send_delayed_response() - request not in list!");
vithyat 0:977e87915078 1267 }
vithyat 0:977e87915078 1268 }
vithyat 0:977e87915078 1269 }
vithyat 0:977e87915078 1270 release_mutex();
vithyat 0:977e87915078 1271 }
vithyat 0:977e87915078 1272 #endif //DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 1273
vithyat 0:977e87915078 1274 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 1275 void M2MNsdlInterface::send_asynchronous_response(M2MBase *base,
vithyat 0:977e87915078 1276 const uint8_t *payload,
vithyat 0:977e87915078 1277 size_t payload_len,
vithyat 0:977e87915078 1278 const uint8_t *token,
vithyat 0:977e87915078 1279 const uint8_t token_len,
vithyat 0:977e87915078 1280 coap_response_code_e code)
vithyat 0:977e87915078 1281 {
vithyat 0:977e87915078 1282 claim_mutex();
vithyat 0:977e87915078 1283 tr_debug("M2MNsdlInterface::send_asynchronous_response() %s", base->uri_path());
vithyat 0:977e87915078 1284 if (base) {
vithyat 0:977e87915078 1285 coap_response_s* resp = find_delayed_response(base->uri_path(), M2MBase::DELAYED_RESPONSE);
vithyat 0:977e87915078 1286 // If there is no response it means that this API is called
vithyat 0:977e87915078 1287 // before actual GET/PUT/POST request has been received in the device.
vithyat 0:977e87915078 1288 if (resp) {
vithyat 0:977e87915078 1289 sn_coap_hdr_s *coap_response = (sn_coap_hdr_s *) memory_alloc(sizeof(sn_coap_hdr_s));
vithyat 0:977e87915078 1290 bool msg_sent = false;
vithyat 0:977e87915078 1291 if (coap_response) {
vithyat 0:977e87915078 1292 memset(coap_response, 0, sizeof(sn_coap_hdr_s));
vithyat 0:977e87915078 1293
vithyat 0:977e87915078 1294 coap_response->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
vithyat 0:977e87915078 1295 coap_response->msg_code = (sn_coap_msg_code_e) code;
vithyat 0:977e87915078 1296 coap_response->token_ptr = (uint8_t*)token;
vithyat 0:977e87915078 1297 coap_response->token_len = token_len;
vithyat 0:977e87915078 1298 coap_response->payload_ptr = (uint8_t*)payload;
vithyat 0:977e87915078 1299 coap_response->payload_len = payload_len;
vithyat 0:977e87915078 1300
vithyat 0:977e87915078 1301 if (sn_nsdl_send_coap_message(_nsdl_handle, &_nsdl_handle->server_address, coap_response) >= 0) {
vithyat 0:977e87915078 1302 // Update msgid, this will be used to track server response
vithyat 0:977e87915078 1303 resp->msg_id = coap_response->msg_id;
vithyat 0:977e87915078 1304 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_SENT, M2MBase::DELAYED_RESPONSE);
vithyat 0:977e87915078 1305 msg_sent = true;
vithyat 0:977e87915078 1306 if (M2MBase::is_blockwise_needed(_nsdl_handle, payload_len)) {
vithyat 0:977e87915078 1307 resp->blockwise_used = true;
vithyat 0:977e87915078 1308 }
vithyat 0:977e87915078 1309 }
vithyat 0:977e87915078 1310
vithyat 0:977e87915078 1311 coap_response->token_ptr = NULL;
vithyat 0:977e87915078 1312 coap_response->payload_ptr = NULL;
vithyat 0:977e87915078 1313 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 1314 }
vithyat 0:977e87915078 1315
vithyat 0:977e87915078 1316 if (!msg_sent) {
vithyat 0:977e87915078 1317 // Failed to create a message
vithyat 0:977e87915078 1318 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_SEND_FAILED, M2MBase::DELAYED_RESPONSE);
vithyat 0:977e87915078 1319 // Remove stored response from the list
vithyat 0:977e87915078 1320 remove_item_from_response_list(base->uri_path(), UNDEFINED_MSG_ID);
vithyat 0:977e87915078 1321 }
vithyat 0:977e87915078 1322
vithyat 0:977e87915078 1323 } else {
vithyat 0:977e87915078 1324 tr_error("M2MNsdlInterface::send_delayed_response() - request not in list!");
vithyat 0:977e87915078 1325 }
vithyat 0:977e87915078 1326
vithyat 0:977e87915078 1327 }
vithyat 0:977e87915078 1328 release_mutex();
vithyat 0:977e87915078 1329 }
vithyat 0:977e87915078 1330 #endif //ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 1331
vithyat 0:977e87915078 1332 void M2MNsdlInterface::resource_to_be_deleted(M2MBase *base)
vithyat 0:977e87915078 1333 {
vithyat 0:977e87915078 1334 tr_debug("M2MNsdlInterface::resource_to_be_deleted() %p", base);
vithyat 0:977e87915078 1335 claim_mutex();
vithyat 0:977e87915078 1336 remove_nsdl_resource(base);
vithyat 0:977e87915078 1337 #if !defined(DISABLE_DELAYED_RESPONSE) || defined(ENABLE_ASYNC_REST_RESPONSE)
vithyat 0:977e87915078 1338 remove_items_from_response_list_for_uri(base->uri_path());
vithyat 0:977e87915078 1339 #endif
vithyat 0:977e87915078 1340 // Since the M2MObject's are stored in _base_list, they need to be removed from there also.
vithyat 0:977e87915078 1341 if (base && base->base_type() == M2MBase::Object) {
vithyat 0:977e87915078 1342 remove_object(base);
vithyat 0:977e87915078 1343 }
vithyat 0:977e87915078 1344
vithyat 0:977e87915078 1345 release_mutex();
vithyat 0:977e87915078 1346 }
vithyat 0:977e87915078 1347
vithyat 0:977e87915078 1348 void M2MNsdlInterface::value_updated(M2MBase *base)
vithyat 0:977e87915078 1349 {
vithyat 0:977e87915078 1350 tr_debug("M2MNsdlInterface::value_updated()");
vithyat 0:977e87915078 1351 String name;
vithyat 0:977e87915078 1352 if(base) {
vithyat 0:977e87915078 1353 switch(base->base_type()) {
vithyat 0:977e87915078 1354 case M2MBase::Object:
vithyat 0:977e87915078 1355 create_nsdl_object_structure(static_cast<M2MObject*> (base));
vithyat 0:977e87915078 1356 name = base->name();
vithyat 0:977e87915078 1357 break;
vithyat 0:977e87915078 1358 case M2MBase::ObjectInstance:
vithyat 0:977e87915078 1359 create_nsdl_object_instance_structure(static_cast<M2MObjectInstance*> (base));
vithyat 0:977e87915078 1360 name = static_cast<M2MObjectInstance*> (base)->get_parent_object().name();
vithyat 0:977e87915078 1361 break;
vithyat 0:977e87915078 1362 case M2MBase::Resource: {
vithyat 0:977e87915078 1363 M2MResource* resource = static_cast<M2MResource*> (base);
vithyat 0:977e87915078 1364 create_nsdl_resource_structure(resource, resource->supports_multiple_instances());
vithyat 0:977e87915078 1365 name = base->name();
vithyat 0:977e87915078 1366 break;
vithyat 0:977e87915078 1367 }
vithyat 0:977e87915078 1368 case M2MBase::ResourceInstance: {
vithyat 0:977e87915078 1369 M2MResourceInstance* instance = static_cast<M2MResourceInstance*> (base);
vithyat 0:977e87915078 1370 create_nsdl_resource(instance);
vithyat 0:977e87915078 1371 name = static_cast<M2MResourceInstance*> (base)->get_parent_resource().name();
vithyat 0:977e87915078 1372 break;
vithyat 0:977e87915078 1373 }
vithyat 0:977e87915078 1374 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 1375 case M2MBase::ObjectDirectory:
vithyat 0:977e87915078 1376 tr_error("M2MNsdlInterface::value_updated() - unsupported ObjectDirectory base type!");
vithyat 0:977e87915078 1377 return;
vithyat 0:977e87915078 1378 #endif
vithyat 0:977e87915078 1379 }
vithyat 0:977e87915078 1380 }
vithyat 0:977e87915078 1381
vithyat 0:977e87915078 1382 if (base && base->is_value_updated_function_set()) {
vithyat 0:977e87915078 1383 base->execute_value_updated(name);
vithyat 0:977e87915078 1384 }
vithyat 0:977e87915078 1385 else {
vithyat 0:977e87915078 1386 _observer.value_updated(base);
vithyat 0:977e87915078 1387 }
vithyat 0:977e87915078 1388 }
vithyat 0:977e87915078 1389
vithyat 0:977e87915078 1390 void M2MNsdlInterface::remove_object(M2MBase *object)
vithyat 0:977e87915078 1391 {
vithyat 0:977e87915078 1392 claim_mutex();
vithyat 0:977e87915078 1393 tr_debug("M2MNsdlInterface::remove_object() %p", object);
vithyat 0:977e87915078 1394 M2MObject* rem_object = static_cast<M2MObject*> (object);
vithyat 0:977e87915078 1395 if(rem_object && !_base_list.empty()) {
vithyat 0:977e87915078 1396 M2MBaseList::const_iterator it;
vithyat 0:977e87915078 1397 it = _base_list.begin();
vithyat 0:977e87915078 1398 int index = 0;
vithyat 0:977e87915078 1399 for ( ; it != _base_list.end(); it++, index++ ) {
vithyat 0:977e87915078 1400 if((*it)->base_type() == M2MBase::Object && (*it) == rem_object) {
vithyat 0:977e87915078 1401 _base_list.erase(index);
vithyat 0:977e87915078 1402 break;
vithyat 0:977e87915078 1403 }
vithyat 0:977e87915078 1404 }
vithyat 0:977e87915078 1405 }
vithyat 0:977e87915078 1406 release_mutex();
vithyat 0:977e87915078 1407 }
vithyat 0:977e87915078 1408
vithyat 0:977e87915078 1409 bool M2MNsdlInterface::create_nsdl_structure(M2MBase *base)
vithyat 0:977e87915078 1410 {
vithyat 0:977e87915078 1411 tr_debug("M2MNsdlInterface::create_nsdl_structure()");
vithyat 0:977e87915078 1412 bool success = false;
vithyat 0:977e87915078 1413 if(base) {
vithyat 0:977e87915078 1414 switch (base->base_type()) {
vithyat 0:977e87915078 1415 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 1416 case M2MBase::ObjectDirectory:
vithyat 0:977e87915078 1417 success = create_nsdl_endpoint_structure((M2MEndpoint*)base);
vithyat 0:977e87915078 1418 break;
vithyat 0:977e87915078 1419 #endif
vithyat 0:977e87915078 1420 case M2MBase::Object:
vithyat 0:977e87915078 1421 success = create_nsdl_object_structure((M2MObject*)base);
vithyat 0:977e87915078 1422 break;
vithyat 0:977e87915078 1423 default:
vithyat 0:977e87915078 1424 break;
vithyat 0:977e87915078 1425 }
vithyat 0:977e87915078 1426 }
vithyat 0:977e87915078 1427 return success;
vithyat 0:977e87915078 1428 }
vithyat 0:977e87915078 1429
vithyat 0:977e87915078 1430 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 1431 bool M2MNsdlInterface::create_nsdl_endpoint_structure(M2MEndpoint *endpoint)
vithyat 0:977e87915078 1432 {
vithyat 0:977e87915078 1433 tr_debug("M2MNsdlInterface::create_nsdl_endpoint_structure()");
vithyat 0:977e87915078 1434 bool success = false;
vithyat 0:977e87915078 1435 if(endpoint) {
vithyat 0:977e87915078 1436 success = true;
vithyat 0:977e87915078 1437 if (endpoint->get_changed()) {
vithyat 0:977e87915078 1438 const M2MObjectList &object_list = endpoint->objects();
vithyat 0:977e87915078 1439 tr_debug("M2MNsdlInterface::create_nsdl_endpoint_structure - Object count %d", object_list.size());
vithyat 0:977e87915078 1440 if(!endpoint->is_deleted() && !object_list.empty()) {
vithyat 0:977e87915078 1441 M2MObjectList::const_iterator it;
vithyat 0:977e87915078 1442 it = object_list.begin();
vithyat 0:977e87915078 1443 for ( ; it != object_list.end(); it++ ) {
vithyat 0:977e87915078 1444 // Create NSDL structure for all object instances inside
vithyat 0:977e87915078 1445 success = create_nsdl_object_structure(*it);
vithyat 0:977e87915078 1446 }
vithyat 0:977e87915078 1447 }
vithyat 0:977e87915078 1448 if (!create_nsdl_resource(endpoint)) {
vithyat 0:977e87915078 1449 success = false;
vithyat 0:977e87915078 1450 }
vithyat 0:977e87915078 1451 endpoint->clear_changed();
vithyat 0:977e87915078 1452 }
vithyat 0:977e87915078 1453 }
vithyat 0:977e87915078 1454 return success;
vithyat 0:977e87915078 1455 }
vithyat 0:977e87915078 1456 #endif
vithyat 0:977e87915078 1457
vithyat 0:977e87915078 1458 bool M2MNsdlInterface::create_nsdl_object_structure(M2MObject *object)
vithyat 0:977e87915078 1459 {
vithyat 0:977e87915078 1460 bool success = false;
vithyat 0:977e87915078 1461 if (object) {
vithyat 0:977e87915078 1462 const M2MObjectInstanceList &instance_list = object->instances();
vithyat 0:977e87915078 1463 if (!instance_list.empty()) {
vithyat 0:977e87915078 1464 M2MObjectInstanceList::const_iterator it;
vithyat 0:977e87915078 1465 it = instance_list.begin();
vithyat 0:977e87915078 1466 for ( ; it != instance_list.end(); it++ ) {
vithyat 0:977e87915078 1467 // Create NSDL structure for all object instances inside
vithyat 0:977e87915078 1468 success = create_nsdl_object_instance_structure(*it);
vithyat 0:977e87915078 1469 if (!success) {
vithyat 0:977e87915078 1470 tr_error("M2MNsdlInterface::create_nsdl_object_structure - fail to create resource");
vithyat 0:977e87915078 1471 return false;
vithyat 0:977e87915078 1472 }
vithyat 0:977e87915078 1473 }
vithyat 0:977e87915078 1474 }
vithyat 0:977e87915078 1475 }
vithyat 0:977e87915078 1476
vithyat 0:977e87915078 1477 // If marked as NOT_ALLOWED then there is no need to
vithyat 0:977e87915078 1478 // create nsdl resource at all since it will not be published to mds
vithyat 0:977e87915078 1479 if (object && object->operation() != M2MBase::NOT_ALLOWED) {
vithyat 0:977e87915078 1480 success = create_nsdl_resource(object);
vithyat 0:977e87915078 1481 } else {
vithyat 0:977e87915078 1482 success = true;
vithyat 0:977e87915078 1483 }
vithyat 0:977e87915078 1484
vithyat 0:977e87915078 1485 return success;
vithyat 0:977e87915078 1486 }
vithyat 0:977e87915078 1487
vithyat 0:977e87915078 1488 bool M2MNsdlInterface::create_nsdl_object_instance_structure(M2MObjectInstance *object_instance)
vithyat 0:977e87915078 1489 {
vithyat 0:977e87915078 1490 bool success = false;
vithyat 0:977e87915078 1491
vithyat 0:977e87915078 1492 if (object_instance) {
vithyat 0:977e87915078 1493 const M2MResourceList &res_list = object_instance->resources();
vithyat 0:977e87915078 1494 if (!res_list.empty()) {
vithyat 0:977e87915078 1495 M2MResourceList::const_iterator it;
vithyat 0:977e87915078 1496 it = res_list.begin();
vithyat 0:977e87915078 1497 for ( ; it != res_list.end(); it++ ) {
vithyat 0:977e87915078 1498 // Create NSDL structure for all resources inside
vithyat 0:977e87915078 1499 success = create_nsdl_resource_structure(*it, (*it)->supports_multiple_instances());
vithyat 0:977e87915078 1500 if (!success) {
vithyat 0:977e87915078 1501 tr_error("M2MNsdlInterface::create_nsdl_object_instance_structure - fail to create resource");
vithyat 0:977e87915078 1502 return false;
vithyat 0:977e87915078 1503 }
vithyat 0:977e87915078 1504 }
vithyat 0:977e87915078 1505 }
vithyat 0:977e87915078 1506
vithyat 0:977e87915078 1507 // If marked as NOT_ALLOWED then there is no need to
vithyat 0:977e87915078 1508 // create nsdl resource at all since it will not be published to mds
vithyat 0:977e87915078 1509 if (object_instance->operation() != M2MBase::NOT_ALLOWED) {
vithyat 0:977e87915078 1510 success = create_nsdl_resource(object_instance);
vithyat 0:977e87915078 1511 } else {
vithyat 0:977e87915078 1512 success = true;
vithyat 0:977e87915078 1513 }
vithyat 0:977e87915078 1514 }
vithyat 0:977e87915078 1515
vithyat 0:977e87915078 1516 return success;
vithyat 0:977e87915078 1517 }
vithyat 0:977e87915078 1518
vithyat 0:977e87915078 1519 bool M2MNsdlInterface::create_nsdl_resource_structure(M2MResource *res,
vithyat 0:977e87915078 1520 bool multiple_instances)
vithyat 0:977e87915078 1521 {
vithyat 0:977e87915078 1522 bool success = false;
vithyat 0:977e87915078 1523 if (res) {
vithyat 0:977e87915078 1524 // if there are multiple instances supported
vithyat 0:977e87915078 1525 if (multiple_instances) {
vithyat 0:977e87915078 1526 const M2MResourceInstanceList &res_list = res->resource_instances();
vithyat 0:977e87915078 1527 if (!res_list.empty()) {
vithyat 0:977e87915078 1528 M2MResourceInstanceList::const_iterator it;
vithyat 0:977e87915078 1529 it = res_list.begin();
vithyat 0:977e87915078 1530 for ( ; it != res_list.end(); it++ ) {
vithyat 0:977e87915078 1531 success = create_nsdl_resource((*it));
vithyat 0:977e87915078 1532 if (!success) {
vithyat 0:977e87915078 1533 tr_error("M2MNsdlInterface::create_nsdl_resource_structure - instance creation failed");
vithyat 0:977e87915078 1534 return false;
vithyat 0:977e87915078 1535 }
vithyat 0:977e87915078 1536 }
vithyat 0:977e87915078 1537 // Register the main Resource as well along with ResourceInstances
vithyat 0:977e87915078 1538 success = create_nsdl_resource(res);
vithyat 0:977e87915078 1539 }
vithyat 0:977e87915078 1540 } else {
vithyat 0:977e87915078 1541 success = create_nsdl_resource(res);
vithyat 0:977e87915078 1542 }
vithyat 0:977e87915078 1543 }
vithyat 0:977e87915078 1544 return success;
vithyat 0:977e87915078 1545 }
vithyat 0:977e87915078 1546
vithyat 0:977e87915078 1547 bool M2MNsdlInterface::create_nsdl_resource(M2MBase *base)
vithyat 0:977e87915078 1548 {
vithyat 0:977e87915078 1549 claim_mutex();
vithyat 0:977e87915078 1550 bool success = false;
vithyat 0:977e87915078 1551
vithyat 0:977e87915078 1552 if (base) {
vithyat 0:977e87915078 1553 int8_t result = 0;
vithyat 0:977e87915078 1554 sn_nsdl_dynamic_resource_parameters_s* nsdl_resource = base->get_nsdl_resource();
vithyat 0:977e87915078 1555
vithyat 0:977e87915078 1556 // needed on deletion
vithyat 0:977e87915078 1557 if (base->observation_handler() == NULL) {
vithyat 0:977e87915078 1558 base->set_observation_handler(this);
vithyat 0:977e87915078 1559 }
vithyat 0:977e87915078 1560
vithyat 0:977e87915078 1561 result = sn_nsdl_put_resource(_nsdl_handle, nsdl_resource);
vithyat 0:977e87915078 1562
vithyat 0:977e87915078 1563 // Put under observation if auto-obs feature is set.
vithyat 0:977e87915078 1564 if (nsdl_resource &&
vithyat 0:977e87915078 1565 nsdl_resource->auto_observable &&
vithyat 0:977e87915078 1566 result != SN_GRS_RESOURCE_ALREADY_EXISTS) {
vithyat 0:977e87915078 1567 base->set_under_observation(true, base->observation_handler());
vithyat 0:977e87915078 1568
vithyat 0:977e87915078 1569 // Increment auto-obs token to be unique in every object
vithyat 0:977e87915078 1570 _auto_obs_token++;
vithyat 0:977e87915078 1571 if (_auto_obs_token > AUTO_OBS_TOKEN_MAX) {
vithyat 0:977e87915078 1572 _auto_obs_token = 1;
vithyat 0:977e87915078 1573 }
vithyat 0:977e87915078 1574
vithyat 0:977e87915078 1575 // Store token in big-endian byte order
vithyat 0:977e87915078 1576 uint8_t token[sizeof(uint16_t)];
vithyat 0:977e87915078 1577 common_write_16_bit(_auto_obs_token, token);
vithyat 0:977e87915078 1578 base->set_observation_token(token, sizeof(uint16_t));
vithyat 0:977e87915078 1579
vithyat 0:977e87915078 1580 switch (base->base_type()) {
vithyat 0:977e87915078 1581 case M2MBase::Object:
vithyat 0:977e87915078 1582 base->add_observation_level(M2MBase::O_Attribute);
vithyat 0:977e87915078 1583 break;
vithyat 0:977e87915078 1584
vithyat 0:977e87915078 1585 case M2MBase::ObjectInstance:
vithyat 0:977e87915078 1586 base->add_observation_level(M2MBase::OI_Attribute);
vithyat 0:977e87915078 1587 break;
vithyat 0:977e87915078 1588
vithyat 0:977e87915078 1589 case M2MBase::Resource:
vithyat 0:977e87915078 1590 case M2MBase::ResourceInstance:
vithyat 0:977e87915078 1591 base->add_observation_level(M2MBase::R_Attribute);
vithyat 0:977e87915078 1592 break;
vithyat 0:977e87915078 1593 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 1594 case M2MBase::ObjectDirectory:
vithyat 0:977e87915078 1595 break;
vithyat 0:977e87915078 1596 #endif
vithyat 0:977e87915078 1597 }
vithyat 0:977e87915078 1598 }
vithyat 0:977e87915078 1599 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 1600 else if (base->base_type() == M2MBase::ObjectDirectory) {
vithyat 0:977e87915078 1601 M2MEndpoint *endpoint = (M2MEndpoint*) base;
vithyat 0:977e87915078 1602 if (endpoint->is_deleted()) {
vithyat 0:977e87915078 1603 sn_nsdl_dynamic_resource_parameters_s *nsdl_resource = endpoint->get_nsdl_resource();
vithyat 0:977e87915078 1604 nsdl_resource->registered = SN_NDSL_RESOURCE_DELETE;
vithyat 0:977e87915078 1605 }
vithyat 0:977e87915078 1606 }
vithyat 0:977e87915078 1607 #endif
vithyat 0:977e87915078 1608
vithyat 0:977e87915078 1609 // Either the resource is created or it already
vithyat 0:977e87915078 1610 // exists , then result is success.
vithyat 0:977e87915078 1611 if (result == 0 ||
vithyat 0:977e87915078 1612 result == SN_GRS_RESOURCE_ALREADY_EXISTS){
vithyat 0:977e87915078 1613 success = true;
vithyat 0:977e87915078 1614 }
vithyat 0:977e87915078 1615 }
vithyat 0:977e87915078 1616
vithyat 0:977e87915078 1617 release_mutex();
vithyat 0:977e87915078 1618 return success;
vithyat 0:977e87915078 1619 }
vithyat 0:977e87915078 1620
vithyat 0:977e87915078 1621 // convenience method to get the URI from its buffer field...
vithyat 0:977e87915078 1622 String M2MNsdlInterface::coap_to_string(const uint8_t *coap_data, int coap_data_length)
vithyat 0:977e87915078 1623 {
vithyat 0:977e87915078 1624 String value;
vithyat 0:977e87915078 1625 if (coap_data != NULL && coap_data_length > 0) {
vithyat 0:977e87915078 1626 value.append_raw((char *)coap_data,coap_data_length);
vithyat 0:977e87915078 1627 }
vithyat 0:977e87915078 1628 return value;
vithyat 0:977e87915078 1629 }
vithyat 0:977e87915078 1630
vithyat 0:977e87915078 1631 uint64_t M2MNsdlInterface::registration_time() const
vithyat 0:977e87915078 1632 {
vithyat 0:977e87915078 1633 uint64_t value = 0;
vithyat 0:977e87915078 1634 if(_endpoint) {
vithyat 0:977e87915078 1635 value = _server->resource_value_int(M2MServer::Lifetime);
vithyat 0:977e87915078 1636 }
vithyat 0:977e87915078 1637 if(value < MINIMUM_REGISTRATION_TIME) {
vithyat 0:977e87915078 1638 tr_warn("M2MNsdlInterface::registration_time - stored value in resource (in seconds) %" PRIu64, value);
vithyat 0:977e87915078 1639 value = MINIMUM_REGISTRATION_TIME;
vithyat 0:977e87915078 1640 }
vithyat 0:977e87915078 1641
vithyat 0:977e87915078 1642 if(value >= OPTIMUM_LIFETIME) {
vithyat 0:977e87915078 1643 value = value - REDUCE_LIFETIME;
vithyat 0:977e87915078 1644 } else {
vithyat 0:977e87915078 1645 value = REDUCTION_FACTOR * value;
vithyat 0:977e87915078 1646 }
vithyat 0:977e87915078 1647 tr_debug("M2MNsdlInterface::registration_time - value (in seconds) %" PRIu64, value);
vithyat 0:977e87915078 1648 return value;
vithyat 0:977e87915078 1649 }
vithyat 0:977e87915078 1650
vithyat 0:977e87915078 1651 M2MBase* M2MNsdlInterface::find_resource(const String &object_name) const
vithyat 0:977e87915078 1652 {
vithyat 0:977e87915078 1653 tr_debug("M2MNsdlInterface::find_resource(object level) - from %p name (%s) ", this, object_name.c_str());
vithyat 0:977e87915078 1654 M2MObject *current = NULL;
vithyat 0:977e87915078 1655 M2MBase *found = NULL;
vithyat 0:977e87915078 1656 if(!_base_list.empty()) {
vithyat 0:977e87915078 1657 M2MBaseList::const_iterator it;
vithyat 0:977e87915078 1658 it = _base_list.begin();
vithyat 0:977e87915078 1659 for ( ; it != _base_list.end(); it++ ) {
vithyat 0:977e87915078 1660 if ((*it)->base_type() == M2MBase::Object) {
vithyat 0:977e87915078 1661 current = (M2MObject*)*it;
vithyat 0:977e87915078 1662 tr_debug("M2MNsdlInterface::find_resource(object level) - path (%s)",
vithyat 0:977e87915078 1663 (char*)current->uri_path());
vithyat 0:977e87915078 1664 if (strcmp((char*)current->uri_path(), object_name.c_str()) == 0) {
vithyat 0:977e87915078 1665 found = current;
vithyat 0:977e87915078 1666 tr_debug("M2MNsdlInterface::find_resource(%s) found", object_name.c_str());
vithyat 0:977e87915078 1667 break;
vithyat 0:977e87915078 1668 }
vithyat 0:977e87915078 1669
vithyat 0:977e87915078 1670 found = find_resource(current, object_name);
vithyat 0:977e87915078 1671 if(found != NULL) {
vithyat 0:977e87915078 1672 break;
vithyat 0:977e87915078 1673 }
vithyat 0:977e87915078 1674 }
vithyat 0:977e87915078 1675 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 1676 else if ((*it)->base_type() == M2MBase::ObjectDirectory) {
vithyat 0:977e87915078 1677 M2MEndpoint *ep = (M2MEndpoint*)*it;
vithyat 0:977e87915078 1678 if(!strcmp((char*)(*it)->uri_path(), object_name.c_str())) {
vithyat 0:977e87915078 1679 found = NULL;
vithyat 0:977e87915078 1680 break;
vithyat 0:977e87915078 1681 } else {
vithyat 0:977e87915078 1682 found = find_resource(ep, object_name);
vithyat 0:977e87915078 1683 }
vithyat 0:977e87915078 1684 if(found != NULL) {
vithyat 0:977e87915078 1685 break;
vithyat 0:977e87915078 1686 }
vithyat 0:977e87915078 1687 }
vithyat 0:977e87915078 1688 #endif
vithyat 0:977e87915078 1689 }
vithyat 0:977e87915078 1690 }
vithyat 0:977e87915078 1691 return found;
vithyat 0:977e87915078 1692 }
vithyat 0:977e87915078 1693
vithyat 0:977e87915078 1694 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 1695 M2MBase* M2MNsdlInterface::find_resource(const M2MEndpoint *endpoint,
vithyat 0:977e87915078 1696 const String &object_name) const
vithyat 0:977e87915078 1697 {
vithyat 0:977e87915078 1698 tr_debug("M2MNsdlInterface::find_resource(endpoint level) - name (%s)", object_name.c_str());
vithyat 0:977e87915078 1699 M2MBase *object = NULL;
vithyat 0:977e87915078 1700 if(endpoint) {
vithyat 0:977e87915078 1701 const M2MObjectList &list = endpoint->objects();
vithyat 0:977e87915078 1702 if(!list.empty()) {
vithyat 0:977e87915078 1703 M2MObjectList::const_iterator it;
vithyat 0:977e87915078 1704 it = list.begin();
vithyat 0:977e87915078 1705 for ( ; it != list.end(); it++ ) {
vithyat 0:977e87915078 1706 if (!strcmp((char*)(*it)->uri_path(), object_name.c_str())) {
vithyat 0:977e87915078 1707 tr_debug("M2MNsdlInterface::find_resource(endpoint level) - object %p object name (%s)",
vithyat 0:977e87915078 1708 object, object_name.c_str());
vithyat 0:977e87915078 1709 object = (*it);
vithyat 0:977e87915078 1710 break;
vithyat 0:977e87915078 1711 }
vithyat 0:977e87915078 1712
vithyat 0:977e87915078 1713 object = find_resource((*it),object_name);
vithyat 0:977e87915078 1714 if(object != NULL){
vithyat 0:977e87915078 1715 break;
vithyat 0:977e87915078 1716 }
vithyat 0:977e87915078 1717 }
vithyat 0:977e87915078 1718 }
vithyat 0:977e87915078 1719 }
vithyat 0:977e87915078 1720 return object;
vithyat 0:977e87915078 1721 }
vithyat 0:977e87915078 1722 #endif
vithyat 0:977e87915078 1723
vithyat 0:977e87915078 1724 M2MBase* M2MNsdlInterface::find_resource(const M2MObject *object,
vithyat 0:977e87915078 1725 const String &object_instance) const
vithyat 0:977e87915078 1726 {
vithyat 0:977e87915078 1727 M2MBase *instance = NULL;
vithyat 0:977e87915078 1728 if(object) {
vithyat 0:977e87915078 1729 const M2MObjectInstanceList &list = object->instances();
vithyat 0:977e87915078 1730 if(!list.empty()) {
vithyat 0:977e87915078 1731 M2MObjectInstanceList::const_iterator it;
vithyat 0:977e87915078 1732 it = list.begin();
vithyat 0:977e87915078 1733 for ( ; it != list.end(); it++ ) {
vithyat 0:977e87915078 1734 if(!strcmp((char*)(*it)->uri_path(), object_instance.c_str())){
vithyat 0:977e87915078 1735 instance = (*it);
vithyat 0:977e87915078 1736 tr_debug("M2MNsdlInterface::find_resource(object instance level) - found (%s)",
vithyat 0:977e87915078 1737 (char*)(*it)->uri_path());
vithyat 0:977e87915078 1738 break;
vithyat 0:977e87915078 1739 }
vithyat 0:977e87915078 1740
vithyat 0:977e87915078 1741 instance = find_resource((*it),object_instance);
vithyat 0:977e87915078 1742 if(instance != NULL){
vithyat 0:977e87915078 1743 break;
vithyat 0:977e87915078 1744 }
vithyat 0:977e87915078 1745 }
vithyat 0:977e87915078 1746 }
vithyat 0:977e87915078 1747 }
vithyat 0:977e87915078 1748 return instance;
vithyat 0:977e87915078 1749 }
vithyat 0:977e87915078 1750
vithyat 0:977e87915078 1751 M2MBase* M2MNsdlInterface::find_resource(const M2MObjectInstance *object_instance,
vithyat 0:977e87915078 1752 const String &resource_instance) const
vithyat 0:977e87915078 1753 {
vithyat 0:977e87915078 1754 M2MBase *instance = NULL;
vithyat 0:977e87915078 1755 if(object_instance) {
vithyat 0:977e87915078 1756 const M2MResourceList &list = object_instance->resources();
vithyat 0:977e87915078 1757 if(!list.empty()) {
vithyat 0:977e87915078 1758 M2MResourceList::const_iterator it;
vithyat 0:977e87915078 1759 it = list.begin();
vithyat 0:977e87915078 1760 for ( ; it != list.end(); it++ ) {
vithyat 0:977e87915078 1761 if(!strcmp((char*)(*it)->uri_path(), resource_instance.c_str())) {
vithyat 0:977e87915078 1762 instance = *it;
vithyat 0:977e87915078 1763 break;
vithyat 0:977e87915078 1764 }
vithyat 0:977e87915078 1765 else if((*it)->supports_multiple_instances()) {
vithyat 0:977e87915078 1766 instance = find_resource((*it), (*it)->uri_path(),
vithyat 0:977e87915078 1767 resource_instance);
vithyat 0:977e87915078 1768 if(instance != NULL){
vithyat 0:977e87915078 1769 break;
vithyat 0:977e87915078 1770 }
vithyat 0:977e87915078 1771 }
vithyat 0:977e87915078 1772 }
vithyat 0:977e87915078 1773 }
vithyat 0:977e87915078 1774 }
vithyat 0:977e87915078 1775 return instance;
vithyat 0:977e87915078 1776 }
vithyat 0:977e87915078 1777
vithyat 0:977e87915078 1778 M2MBase* M2MNsdlInterface::find_resource(const M2MResource *resource,
vithyat 0:977e87915078 1779 const String &object_name,
vithyat 0:977e87915078 1780 const String &resource_instance) const
vithyat 0:977e87915078 1781 {
vithyat 0:977e87915078 1782 M2MBase *res = NULL;
vithyat 0:977e87915078 1783 if(resource) {
vithyat 0:977e87915078 1784 if(resource->supports_multiple_instances()) {
vithyat 0:977e87915078 1785 const M2MResourceInstanceList &list = resource->resource_instances();
vithyat 0:977e87915078 1786 if(!list.empty()) {
vithyat 0:977e87915078 1787 M2MResourceInstanceList::const_iterator it;
vithyat 0:977e87915078 1788 it = list.begin();
vithyat 0:977e87915078 1789 for ( ; it != list.end(); it++ ) {
vithyat 0:977e87915078 1790 if(!strcmp((char*)(*it)->uri_path(), resource_instance.c_str())){
vithyat 0:977e87915078 1791 res = (*it);
vithyat 0:977e87915078 1792 break;
vithyat 0:977e87915078 1793 }
vithyat 0:977e87915078 1794 }
vithyat 0:977e87915078 1795 }
vithyat 0:977e87915078 1796 }
vithyat 0:977e87915078 1797 }
vithyat 0:977e87915078 1798 return res;
vithyat 0:977e87915078 1799 }
vithyat 0:977e87915078 1800
vithyat 0:977e87915078 1801 bool M2MNsdlInterface::object_present(M2MBase* base) const
vithyat 0:977e87915078 1802 {
vithyat 0:977e87915078 1803 bool success = false;
vithyat 0:977e87915078 1804 if(base && !_base_list.empty()) {
vithyat 0:977e87915078 1805 M2MBaseList::const_iterator it;
vithyat 0:977e87915078 1806 it = _base_list.begin();
vithyat 0:977e87915078 1807 for ( ; it != _base_list.end(); it++ ) {
vithyat 0:977e87915078 1808 if((*it) == base) {
vithyat 0:977e87915078 1809 success = true;
vithyat 0:977e87915078 1810 break;
vithyat 0:977e87915078 1811 }
vithyat 0:977e87915078 1812 }
vithyat 0:977e87915078 1813 }
vithyat 0:977e87915078 1814 return success;
vithyat 0:977e87915078 1815 }
vithyat 0:977e87915078 1816
vithyat 0:977e87915078 1817 int M2MNsdlInterface::object_index(M2MBase* base) const
vithyat 0:977e87915078 1818 {
vithyat 0:977e87915078 1819 int found_index = -1;
vithyat 0:977e87915078 1820 int index;
vithyat 0:977e87915078 1821 if(base && !_base_list.empty()) {
vithyat 0:977e87915078 1822 M2MBaseList::const_iterator it;
vithyat 0:977e87915078 1823
vithyat 0:977e87915078 1824 for (it = _base_list.begin(), index = 0; it != _base_list.end(); it++, index++) {
vithyat 0:977e87915078 1825 if((*it) == base) {
vithyat 0:977e87915078 1826 found_index = index;
vithyat 0:977e87915078 1827 break;
vithyat 0:977e87915078 1828 }
vithyat 0:977e87915078 1829 }
vithyat 0:977e87915078 1830 }
vithyat 0:977e87915078 1831 return found_index;
vithyat 0:977e87915078 1832 }
vithyat 0:977e87915078 1833
vithyat 0:977e87915078 1834
vithyat 0:977e87915078 1835 bool M2MNsdlInterface::add_object_to_list(M2MBase* object)
vithyat 0:977e87915078 1836 {
vithyat 0:977e87915078 1837 tr_debug("M2MNsdlInterface::add_object_to_list this=%p object=%p", this, object);
vithyat 0:977e87915078 1838 bool success = false;
vithyat 0:977e87915078 1839 if(object && !object_present(object)) {
vithyat 0:977e87915078 1840 _base_list.push_back(object);
vithyat 0:977e87915078 1841 success = true;
vithyat 0:977e87915078 1842 }
vithyat 0:977e87915078 1843 return success;
vithyat 0:977e87915078 1844 }
vithyat 0:977e87915078 1845
vithyat 0:977e87915078 1846 bool M2MNsdlInterface::remove_object_from_list(M2MBase* object)
vithyat 0:977e87915078 1847 {
vithyat 0:977e87915078 1848 tr_debug("M2MNsdlInterface::remove_object_from_list this=%p object=%p", this, object);
vithyat 0:977e87915078 1849 bool success = false;
vithyat 0:977e87915078 1850 int index;
vithyat 0:977e87915078 1851 if(object && (-1 != (index = object_index(object)))) {
vithyat 0:977e87915078 1852 tr_debug(" object found at index %d", index);
vithyat 0:977e87915078 1853 _base_list.erase(index);
vithyat 0:977e87915078 1854 success = true;
vithyat 0:977e87915078 1855 }
vithyat 0:977e87915078 1856 return success;
vithyat 0:977e87915078 1857 }
vithyat 0:977e87915078 1858
vithyat 0:977e87915078 1859 M2MInterface::Error M2MNsdlInterface::interface_error(const sn_coap_hdr_s &coap_header)
vithyat 0:977e87915078 1860 {
vithyat 0:977e87915078 1861 M2MInterface::Error error;
vithyat 0:977e87915078 1862 switch(coap_header.msg_code) {
vithyat 0:977e87915078 1863 case COAP_MSG_CODE_RESPONSE_BAD_REQUEST:
vithyat 0:977e87915078 1864 case COAP_MSG_CODE_RESPONSE_BAD_OPTION:
vithyat 0:977e87915078 1865 case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE:
vithyat 0:977e87915078 1866 case COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED:
vithyat 0:977e87915078 1867 case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE:
vithyat 0:977e87915078 1868 case COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT:
vithyat 0:977e87915078 1869 error = M2MInterface::InvalidParameters;
vithyat 0:977e87915078 1870 break;
vithyat 0:977e87915078 1871 case COAP_MSG_CODE_RESPONSE_UNAUTHORIZED:
vithyat 0:977e87915078 1872 case COAP_MSG_CODE_RESPONSE_FORBIDDEN:
vithyat 0:977e87915078 1873 case COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE:
vithyat 0:977e87915078 1874 case COAP_MSG_CODE_RESPONSE_NOT_FOUND:
vithyat 0:977e87915078 1875 case COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED:
vithyat 0:977e87915078 1876 error = M2MInterface::NotAllowed;
vithyat 0:977e87915078 1877 break;
vithyat 0:977e87915078 1878 case COAP_MSG_CODE_RESPONSE_CREATED:
vithyat 0:977e87915078 1879 case COAP_MSG_CODE_RESPONSE_DELETED:
vithyat 0:977e87915078 1880 case COAP_MSG_CODE_RESPONSE_VALID:
vithyat 0:977e87915078 1881 case COAP_MSG_CODE_RESPONSE_CHANGED:
vithyat 0:977e87915078 1882 case COAP_MSG_CODE_RESPONSE_CONTENT:
vithyat 0:977e87915078 1883 error = M2MInterface::ErrorNone;
vithyat 0:977e87915078 1884 break;
vithyat 0:977e87915078 1885 default:
vithyat 0:977e87915078 1886 error = M2MInterface::UnknownError;
vithyat 0:977e87915078 1887 break;
vithyat 0:977e87915078 1888 }
vithyat 0:977e87915078 1889 if(coap_header.coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED ||
vithyat 0:977e87915078 1890 coap_header.coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED) {
vithyat 0:977e87915078 1891 error = M2MInterface::NetworkError;
vithyat 0:977e87915078 1892 }
vithyat 0:977e87915078 1893 return error;
vithyat 0:977e87915078 1894 }
vithyat 0:977e87915078 1895
vithyat 0:977e87915078 1896 const char *M2MNsdlInterface::coap_error(const sn_coap_hdr_s &coap_header)
vithyat 0:977e87915078 1897 {
vithyat 0:977e87915078 1898 if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_BAD_REQUEST) {
vithyat 0:977e87915078 1899 return COAP_ERROR_REASON_1;
vithyat 0:977e87915078 1900 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_BAD_OPTION) {
vithyat 0:977e87915078 1901 return COAP_ERROR_REASON_2;
vithyat 0:977e87915078 1902 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE) {
vithyat 0:977e87915078 1903 return COAP_ERROR_REASON_3;
vithyat 0:977e87915078 1904 }else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED) {
vithyat 0:977e87915078 1905 return COAP_ERROR_REASON_4;
vithyat 0:977e87915078 1906 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE) {
vithyat 0:977e87915078 1907 return COAP_ERROR_REASON_5;
vithyat 0:977e87915078 1908 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT) {
vithyat 0:977e87915078 1909 return COAP_ERROR_REASON_6;
vithyat 0:977e87915078 1910 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_UNAUTHORIZED) {
vithyat 0:977e87915078 1911 return COAP_ERROR_REASON_7;
vithyat 0:977e87915078 1912 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_FORBIDDEN) {
vithyat 0:977e87915078 1913 return COAP_ERROR_REASON_8;
vithyat 0:977e87915078 1914 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE) {
vithyat 0:977e87915078 1915 return COAP_ERROR_REASON_9;
vithyat 0:977e87915078 1916 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_NOT_FOUND) {
vithyat 0:977e87915078 1917 return COAP_ERROR_REASON_10;
vithyat 0:977e87915078 1918 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED) {
vithyat 0:977e87915078 1919 return COAP_ERROR_REASON_11;
vithyat 0:977e87915078 1920 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE) {
vithyat 0:977e87915078 1921 return COAP_ERROR_REASON_13;
vithyat 0:977e87915078 1922 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR) {
vithyat 0:977e87915078 1923 return COAP_ERROR_REASON_14;
vithyat 0:977e87915078 1924 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_BAD_GATEWAY) {
vithyat 0:977e87915078 1925 return COAP_ERROR_REASON_15;
vithyat 0:977e87915078 1926 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT) {
vithyat 0:977e87915078 1927 return COAP_ERROR_REASON_16;
vithyat 0:977e87915078 1928 } else if (coap_header.msg_code == COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED) {
vithyat 0:977e87915078 1929 return COAP_ERROR_REASON_17;
vithyat 0:977e87915078 1930 } else if (coap_header.coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED ||
vithyat 0:977e87915078 1931 coap_header.coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED) {
vithyat 0:977e87915078 1932 return COAP_ERROR_REASON_12;
vithyat 0:977e87915078 1933 }
vithyat 0:977e87915078 1934 return COAP_NO_ERROR;
vithyat 0:977e87915078 1935 }
vithyat 0:977e87915078 1936
vithyat 0:977e87915078 1937 void M2MNsdlInterface::send_object_observation(M2MObject *object,
vithyat 0:977e87915078 1938 uint16_t obs_number,
vithyat 0:977e87915078 1939 const m2m::Vector<uint16_t> &changed_instance_ids,
vithyat 0:977e87915078 1940 bool send_object)
vithyat 0:977e87915078 1941 {
vithyat 0:977e87915078 1942 tr_info("M2MNsdlInterface::send_object_observation");
vithyat 0:977e87915078 1943 if(object) {
vithyat 0:977e87915078 1944 uint8_t *value = 0;
vithyat 0:977e87915078 1945 uint32_t length = 0;
vithyat 0:977e87915078 1946 uint8_t token[MAX_TOKEN_SIZE];
vithyat 0:977e87915078 1947 uint8_t token_length = 0;
vithyat 0:977e87915078 1948
vithyat 0:977e87915078 1949 // Send whole object structure
vithyat 0:977e87915078 1950 if (send_object) {
vithyat 0:977e87915078 1951 value = M2MTLVSerializer::serialize(object->instances(), length);
vithyat 0:977e87915078 1952 }
vithyat 0:977e87915078 1953 // Send only changed object instances
vithyat 0:977e87915078 1954 else {
vithyat 0:977e87915078 1955 M2MObjectInstanceList list;
vithyat 0:977e87915078 1956 Vector<uint16_t>::const_iterator it;
vithyat 0:977e87915078 1957 it = changed_instance_ids.begin();
vithyat 0:977e87915078 1958 for (; it != changed_instance_ids.end(); it++){
vithyat 0:977e87915078 1959 M2MObjectInstance* obj_instance = object->object_instance(*it);
vithyat 0:977e87915078 1960 if (obj_instance){
vithyat 0:977e87915078 1961 list.push_back(obj_instance);
vithyat 0:977e87915078 1962 }
vithyat 0:977e87915078 1963 }
vithyat 0:977e87915078 1964 if (!list.empty()) {
vithyat 0:977e87915078 1965 value = M2MTLVSerializer::serialize(list, length);
vithyat 0:977e87915078 1966 list.clear();
vithyat 0:977e87915078 1967 }
vithyat 0:977e87915078 1968 }
vithyat 0:977e87915078 1969
vithyat 0:977e87915078 1970 object->get_observation_token((uint8_t*)&token,token_length);
vithyat 0:977e87915078 1971
vithyat 0:977e87915078 1972 object->report_handler()->set_blockwise_notify(M2MBase::is_blockwise_needed(_nsdl_handle, length));
vithyat 0:977e87915078 1973
vithyat 0:977e87915078 1974 int32_t msgid = sn_nsdl_send_observation_notification(_nsdl_handle, token, token_length, value, length,
vithyat 0:977e87915078 1975 sn_coap_observe_e(obs_number), COAP_MSG_TYPE_CONFIRMABLE,
vithyat 0:977e87915078 1976 sn_coap_content_format_e(object->coap_content_type()), -1);
vithyat 0:977e87915078 1977 execute_notification_delivery_status_cb(object, msgid);
vithyat 0:977e87915078 1978
vithyat 0:977e87915078 1979 memory_free(value);
vithyat 0:977e87915078 1980 }
vithyat 0:977e87915078 1981 }
vithyat 0:977e87915078 1982
vithyat 0:977e87915078 1983 void M2MNsdlInterface::send_object_instance_observation(M2MObjectInstance *object_instance,
vithyat 0:977e87915078 1984 uint16_t obs_number)
vithyat 0:977e87915078 1985 {
vithyat 0:977e87915078 1986 tr_info("M2MNsdlInterface::send_object_instance_observation");
vithyat 0:977e87915078 1987 if(object_instance) {
vithyat 0:977e87915078 1988 uint8_t *value = 0;
vithyat 0:977e87915078 1989 uint32_t length = 0;
vithyat 0:977e87915078 1990 uint8_t token[MAX_TOKEN_SIZE];
vithyat 0:977e87915078 1991 uint8_t token_length = 0;
vithyat 0:977e87915078 1992
vithyat 0:977e87915078 1993 value = M2MTLVSerializer::serialize(object_instance->resources(), length);
vithyat 0:977e87915078 1994
vithyat 0:977e87915078 1995 object_instance->get_observation_token((uint8_t*)&token,token_length);
vithyat 0:977e87915078 1996
vithyat 0:977e87915078 1997 object_instance->report_handler()->set_blockwise_notify(M2MBase::is_blockwise_needed(_nsdl_handle, length));
vithyat 0:977e87915078 1998
vithyat 0:977e87915078 1999 int32_t msgid = sn_nsdl_send_observation_notification(_nsdl_handle, token, token_length, value, length,
vithyat 0:977e87915078 2000 sn_coap_observe_e(obs_number), COAP_MSG_TYPE_CONFIRMABLE,
vithyat 0:977e87915078 2001 sn_coap_content_format_e(object_instance->coap_content_type()), -1);
vithyat 0:977e87915078 2002
vithyat 0:977e87915078 2003 execute_notification_delivery_status_cb(object_instance, msgid);
vithyat 0:977e87915078 2004
vithyat 0:977e87915078 2005
vithyat 0:977e87915078 2006 memory_free(value);
vithyat 0:977e87915078 2007 }
vithyat 0:977e87915078 2008 }
vithyat 0:977e87915078 2009
vithyat 0:977e87915078 2010 void M2MNsdlInterface::send_resource_observation(M2MResource *resource,
vithyat 0:977e87915078 2011 uint16_t obs_number)
vithyat 0:977e87915078 2012 {
vithyat 0:977e87915078 2013 if(resource) {
vithyat 0:977e87915078 2014 tr_info("M2MNsdlInterface::send_resource_observation - uri %s", resource->uri_path());
vithyat 0:977e87915078 2015 uint8_t *value = 0;
vithyat 0:977e87915078 2016 uint32_t length = 0;
vithyat 0:977e87915078 2017 uint8_t token[MAX_TOKEN_SIZE];
vithyat 0:977e87915078 2018 uint8_t token_length = 0;
vithyat 0:977e87915078 2019
vithyat 0:977e87915078 2020 resource->get_observation_token((uint8_t*)token,token_length);
vithyat 0:977e87915078 2021 uint16_t content_type = resource->coap_content_type();
vithyat 0:977e87915078 2022 if (M2MResourceBase::OPAQUE == resource->resource_instance_type()) {
vithyat 0:977e87915078 2023 content_type = COAP_CONTENT_OMA_OPAQUE_TYPE;
vithyat 0:977e87915078 2024 }
vithyat 0:977e87915078 2025
vithyat 0:977e87915078 2026 if (resource->resource_instance_count() > 0 || content_type == COAP_CONTENT_OMA_TLV_TYPE) {
vithyat 0:977e87915078 2027 value = M2MTLVSerializer::serialize(resource, length);
vithyat 0:977e87915078 2028 } else {
vithyat 0:977e87915078 2029 resource->get_value(value,length);
vithyat 0:977e87915078 2030 }
vithyat 0:977e87915078 2031
vithyat 0:977e87915078 2032 resource->report_handler()->set_blockwise_notify(M2MBase::is_blockwise_needed(_nsdl_handle, length));
vithyat 0:977e87915078 2033
vithyat 0:977e87915078 2034 int32_t msgid = sn_nsdl_send_observation_notification(_nsdl_handle, token, token_length, value, length,
vithyat 0:977e87915078 2035 sn_coap_observe_e(obs_number),
vithyat 0:977e87915078 2036 COAP_MSG_TYPE_CONFIRMABLE,
vithyat 0:977e87915078 2037 sn_coap_content_format_e(content_type), -1);
vithyat 0:977e87915078 2038 execute_notification_delivery_status_cb(resource, msgid);
vithyat 0:977e87915078 2039
vithyat 0:977e87915078 2040 memory_free(value);
vithyat 0:977e87915078 2041 }
vithyat 0:977e87915078 2042 }
vithyat 0:977e87915078 2043 nsdl_s * M2MNsdlInterface::get_nsdl_handle() const
vithyat 0:977e87915078 2044 {
vithyat 0:977e87915078 2045 return _nsdl_handle;
vithyat 0:977e87915078 2046 }
vithyat 0:977e87915078 2047
vithyat 0:977e87915078 2048 void M2MNsdlInterface::handle_bootstrap_put_message(sn_coap_hdr_s *coap_header,
vithyat 0:977e87915078 2049 sn_nsdl_addr_s *address) {
vithyat 0:977e87915078 2050 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 2051 tr_info("M2MNsdlInterface::handle_bootstrap_put_message");
vithyat 0:977e87915078 2052 uint8_t response_code = COAP_MSG_CODE_RESPONSE_CHANGED;
vithyat 0:977e87915078 2053 sn_coap_hdr_s *coap_response = NULL;
vithyat 0:977e87915078 2054 bool success = false;
vithyat 0:977e87915078 2055 uint16_t content_type = 0;
vithyat 0:977e87915078 2056 char buffer[MAX_ALLOWED_ERROR_STRING_LENGTH];
vithyat 0:977e87915078 2057 buffer[0] = '\0';
vithyat 0:977e87915078 2058 M2MNsdlInterface::ObjectType object_type = M2MNsdlInterface::SECURITY;
vithyat 0:977e87915078 2059
vithyat 0:977e87915078 2060 if (!_security) {
vithyat 0:977e87915078 2061 _security = M2MSecurity::get_instance();
vithyat 0:977e87915078 2062 }
vithyat 0:977e87915078 2063
vithyat 0:977e87915078 2064 String resource_name = coap_to_string(coap_header->uri_path_ptr,
vithyat 0:977e87915078 2065 coap_header->uri_path_len);
vithyat 0:977e87915078 2066 tr_debug("M2MNsdlInterface::handle_bootstrap_put_message - object path %s", resource_name.c_str());
vithyat 0:977e87915078 2067
vithyat 0:977e87915078 2068 // Security object
vithyat 0:977e87915078 2069 if (resource_name.compare(0,1,"0") == 0) {
vithyat 0:977e87915078 2070 object_type = M2MNsdlInterface::SECURITY;
vithyat 0:977e87915078 2071 success = true;
vithyat 0:977e87915078 2072 }
vithyat 0:977e87915078 2073 // Server object
vithyat 0:977e87915078 2074 else if (resource_name.compare(0,1,"1") == 0) {
vithyat 0:977e87915078 2075 object_type = M2MNsdlInterface::SERVER;
vithyat 0:977e87915078 2076 success = true;
vithyat 0:977e87915078 2077 }
vithyat 0:977e87915078 2078 // Device object
vithyat 0:977e87915078 2079 else if (resource_name.compare(0,1,"3") == 0) {
vithyat 0:977e87915078 2080 M2MDevice* dev = M2MInterfaceFactory::create_device();
vithyat 0:977e87915078 2081 // Not mandatory resource, that's why it must be created first
vithyat 0:977e87915078 2082 M2MResource *res = dev->create_resource(M2MDevice::CurrentTime, 0);
vithyat 0:977e87915078 2083 if (res) {
vithyat 0:977e87915078 2084 res->set_auto_observable(true);
vithyat 0:977e87915078 2085 }
vithyat 0:977e87915078 2086 object_type = M2MNsdlInterface::DEVICE;
vithyat 0:977e87915078 2087 success = true;
vithyat 0:977e87915078 2088 }
vithyat 0:977e87915078 2089
vithyat 0:977e87915078 2090 if (success) {
vithyat 0:977e87915078 2091 if(coap_header->content_format != COAP_CT_NONE) {
vithyat 0:977e87915078 2092 content_type = coap_header->content_format;
vithyat 0:977e87915078 2093 }
vithyat 0:977e87915078 2094
vithyat 0:977e87915078 2095 if (content_type != COAP_CONTENT_OMA_TLV_TYPE &&
vithyat 0:977e87915078 2096 content_type != COAP_CONTENT_OMA_TLV_TYPE_OLD) {
vithyat 0:977e87915078 2097 tr_error("M2MNsdlInterface::handle_bootstrap_put_message - content_type %d", content_type);
vithyat 0:977e87915078 2098 success = false;
vithyat 0:977e87915078 2099 }
vithyat 0:977e87915078 2100 // Parse TLV message and check is the object valid
vithyat 0:977e87915078 2101 if (success) {
vithyat 0:977e87915078 2102 change_operation_mode(_security, M2MBase::PUT_ALLOWED);
vithyat 0:977e87915078 2103 success = parse_bootstrap_message(coap_header, object_type);
vithyat 0:977e87915078 2104 if (success && object_type == M2MNsdlInterface::SECURITY) {
vithyat 0:977e87915078 2105 success = validate_security_object();
vithyat 0:977e87915078 2106 if (!success) {
vithyat 0:977e87915078 2107 const char *desc = "Invalid security object";
vithyat 0:977e87915078 2108 if (strlen(ERROR_REASON_22) + strlen(desc) <= MAX_ALLOWED_ERROR_STRING_LENGTH) {
vithyat 0:977e87915078 2109 snprintf(buffer, sizeof(buffer), ERROR_REASON_22, desc);
vithyat 0:977e87915078 2110 }
vithyat 0:977e87915078 2111 response_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 2112 }
vithyat 0:977e87915078 2113 }
vithyat 0:977e87915078 2114 // Set operation back to default ones
vithyat 0:977e87915078 2115 if (_security) {
vithyat 0:977e87915078 2116 change_operation_mode(_security, M2MBase::NOT_ALLOWED);
vithyat 0:977e87915078 2117 }
vithyat 0:977e87915078 2118 }
vithyat 0:977e87915078 2119 }
vithyat 0:977e87915078 2120
vithyat 0:977e87915078 2121 if (!success) {
vithyat 0:977e87915078 2122 response_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 2123 }
vithyat 0:977e87915078 2124
vithyat 0:977e87915078 2125 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 2126 coap_header,
vithyat 0:977e87915078 2127 response_code);
vithyat 0:977e87915078 2128
vithyat 0:977e87915078 2129 if (coap_response) {
vithyat 0:977e87915078 2130 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
vithyat 0:977e87915078 2131 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 2132 }
vithyat 0:977e87915078 2133
vithyat 0:977e87915078 2134 if (!success) {
vithyat 0:977e87915078 2135 // Do not overwrite ERROR_REASON_22
vithyat 0:977e87915078 2136 if (strlen(buffer) == 0) {
vithyat 0:977e87915078 2137 if (strlen(ERROR_REASON_20) + resource_name.size() <= MAX_ALLOWED_ERROR_STRING_LENGTH) {
vithyat 0:977e87915078 2138 snprintf(buffer, sizeof(buffer), ERROR_REASON_20, resource_name.c_str());
vithyat 0:977e87915078 2139 }
vithyat 0:977e87915078 2140 }
vithyat 0:977e87915078 2141 handle_bootstrap_error(buffer, true);
vithyat 0:977e87915078 2142 }
vithyat 0:977e87915078 2143 #else
vithyat 0:977e87915078 2144 (void) coap_header;
vithyat 0:977e87915078 2145 (void) address;
vithyat 0:977e87915078 2146 #endif
vithyat 0:977e87915078 2147 }
vithyat 0:977e87915078 2148
vithyat 0:977e87915078 2149 bool M2MNsdlInterface::parse_bootstrap_message(sn_coap_hdr_s *coap_header,
vithyat 0:977e87915078 2150 M2MNsdlInterface::ObjectType lwm2m_object_type)
vithyat 0:977e87915078 2151 {
vithyat 0:977e87915078 2152 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 2153 tr_info("M2MNsdlInterface::parse_bootstrap_message");
vithyat 0:977e87915078 2154 bool ret = false;
vithyat 0:977e87915078 2155 bool is_obj_instance = false;
vithyat 0:977e87915078 2156 uint16_t instance_id = 0;
vithyat 0:977e87915078 2157 if (_security) {
vithyat 0:977e87915078 2158 ret = is_obj_instance = M2MTLVDeserializer::is_object_instance(coap_header->payload_ptr);
vithyat 0:977e87915078 2159 if (!is_obj_instance) {
vithyat 0:977e87915078 2160 ret = M2MTLVDeserializer::is_resource(coap_header->payload_ptr);
vithyat 0:977e87915078 2161 }
vithyat 0:977e87915078 2162 if (ret) {
vithyat 0:977e87915078 2163 M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
vithyat 0:977e87915078 2164 if (is_obj_instance) {
vithyat 0:977e87915078 2165 M2MObject* dev_object = static_cast<M2MObject*> (M2MInterfaceFactory::create_device());
vithyat 0:977e87915078 2166
vithyat 0:977e87915078 2167 switch (lwm2m_object_type) {
vithyat 0:977e87915078 2168 case M2MNsdlInterface::SECURITY:
vithyat 0:977e87915078 2169 instance_id = M2MTLVDeserializer::instance_id(coap_header->payload_ptr);
vithyat 0:977e87915078 2170 if (_security->object_instance(instance_id) == NULL) {
vithyat 0:977e87915078 2171 tr_debug("M2MNsdlInterface::parse_bootstrap_message - create instance %d", instance_id);
vithyat 0:977e87915078 2172 _security->create_object_instance(M2MSecurity::M2MServer);
vithyat 0:977e87915078 2173 change_operation_mode(_security, M2MBase::PUT_ALLOWED);
vithyat 0:977e87915078 2174 }
vithyat 0:977e87915078 2175
vithyat 0:977e87915078 2176 error = M2MTLVDeserializer::deserialise_object_instances(coap_header->payload_ptr,
vithyat 0:977e87915078 2177 coap_header->payload_len,
vithyat 0:977e87915078 2178 *_security,
vithyat 0:977e87915078 2179 M2MTLVDeserializer::Put);
vithyat 0:977e87915078 2180 break;
vithyat 0:977e87915078 2181 case M2MNsdlInterface::SERVER:
vithyat 0:977e87915078 2182 error = M2MTLVDeserializer::deserialise_object_instances(coap_header->payload_ptr,
vithyat 0:977e87915078 2183 coap_header->payload_len,
vithyat 0:977e87915078 2184 *_server,
vithyat 0:977e87915078 2185 M2MTLVDeserializer::Put);
vithyat 0:977e87915078 2186 break;
vithyat 0:977e87915078 2187 case M2MNsdlInterface::DEVICE:
vithyat 0:977e87915078 2188 error = M2MTLVDeserializer::deserialise_object_instances(coap_header->payload_ptr,
vithyat 0:977e87915078 2189 coap_header->payload_len,
vithyat 0:977e87915078 2190 *dev_object,
vithyat 0:977e87915078 2191 M2MTLVDeserializer::Put);
vithyat 0:977e87915078 2192 break;
vithyat 0:977e87915078 2193 default:
vithyat 0:977e87915078 2194 break;
vithyat 0:977e87915078 2195 }
vithyat 0:977e87915078 2196 }
vithyat 0:977e87915078 2197 else {
vithyat 0:977e87915078 2198 instance_id = M2MTLVDeserializer::instance_id(coap_header->payload_ptr);
vithyat 0:977e87915078 2199 M2MObjectInstance* instance = NULL;
vithyat 0:977e87915078 2200 switch (lwm2m_object_type) {
vithyat 0:977e87915078 2201 case M2MNsdlInterface::SECURITY:
vithyat 0:977e87915078 2202 instance = _security->object_instance(instance_id);
vithyat 0:977e87915078 2203 if (instance) {
vithyat 0:977e87915078 2204 error = M2MTLVDeserializer::deserialize_resources(coap_header->payload_ptr,
vithyat 0:977e87915078 2205 coap_header->payload_len,
vithyat 0:977e87915078 2206 *instance,
vithyat 0:977e87915078 2207 M2MTLVDeserializer::Put);
vithyat 0:977e87915078 2208 } else {
vithyat 0:977e87915078 2209 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 2210 }
vithyat 0:977e87915078 2211
vithyat 0:977e87915078 2212 break;
vithyat 0:977e87915078 2213 case M2MNsdlInterface::SERVER:
vithyat 0:977e87915078 2214 instance = _server->object_instance(instance_id);
vithyat 0:977e87915078 2215 if (instance) {
vithyat 0:977e87915078 2216 error = M2MTLVDeserializer::deserialize_resources(coap_header->payload_ptr,
vithyat 0:977e87915078 2217 coap_header->payload_len,
vithyat 0:977e87915078 2218 *instance,
vithyat 0:977e87915078 2219 M2MTLVDeserializer::Post);
vithyat 0:977e87915078 2220 } else {
vithyat 0:977e87915078 2221 error = M2MTLVDeserializer::NotValid;
vithyat 0:977e87915078 2222 }
vithyat 0:977e87915078 2223
vithyat 0:977e87915078 2224 break;
vithyat 0:977e87915078 2225 case M2MNsdlInterface::DEVICE:
vithyat 0:977e87915078 2226 default:
vithyat 0:977e87915078 2227 break;
vithyat 0:977e87915078 2228 }
vithyat 0:977e87915078 2229 }
vithyat 0:977e87915078 2230
vithyat 0:977e87915078 2231 if (error != M2MTLVDeserializer::None) {
vithyat 0:977e87915078 2232 tr_error("M2MNsdlInterface::parse_bootstrap_message - error %d", error);
vithyat 0:977e87915078 2233 ret = false;
vithyat 0:977e87915078 2234 }
vithyat 0:977e87915078 2235 }
vithyat 0:977e87915078 2236 } else {
vithyat 0:977e87915078 2237 tr_error("M2MNsdlInterface::parse_bootstrap_message -- no security object!");
vithyat 0:977e87915078 2238 }
vithyat 0:977e87915078 2239 return ret;
vithyat 0:977e87915078 2240 #else
vithyat 0:977e87915078 2241 (void) coap_header;
vithyat 0:977e87915078 2242 (void) is_security_object;
vithyat 0:977e87915078 2243 return false;
vithyat 0:977e87915078 2244 #endif
vithyat 0:977e87915078 2245 }
vithyat 0:977e87915078 2246
vithyat 0:977e87915078 2247 void M2MNsdlInterface::handle_bootstrap_finished(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address)
vithyat 0:977e87915078 2248 {
vithyat 0:977e87915078 2249 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 2250 char buffer[MAX_ALLOWED_ERROR_STRING_LENGTH];
vithyat 0:977e87915078 2251
vithyat 0:977e87915078 2252 String object_name = coap_to_string(coap_header->uri_path_ptr,
vithyat 0:977e87915078 2253 coap_header->uri_path_len);
vithyat 0:977e87915078 2254
vithyat 0:977e87915078 2255 int32_t m2m_id = -1;
vithyat 0:977e87915078 2256 // Security object can be null in case messages are coming in wrong order, for example
vithyat 0:977e87915078 2257 // BS POST is received before BS PUT.
vithyat 0:977e87915078 2258 if (_security) {
vithyat 0:977e87915078 2259 m2m_id = _security->get_security_instance_id(M2MSecurity::M2MServer);
vithyat 0:977e87915078 2260 }
vithyat 0:977e87915078 2261
vithyat 0:977e87915078 2262 tr_info("M2MNsdlInterface::handle_bootstrap_finished - path: %s, m2mid: %" PRId32, object_name.c_str(), m2m_id);
vithyat 0:977e87915078 2263
vithyat 0:977e87915078 2264 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
vithyat 0:977e87915078 2265 // In EST mode we must receive iep in uri-query
vithyat 0:977e87915078 2266 bool est_iep_ok = false;
vithyat 0:977e87915078 2267 if (m2m_id >= 0 &&
vithyat 0:977e87915078 2268 _security->resource_value_int(M2MSecurity::SecurityMode, m2m_id) == M2MSecurity::EST) {
vithyat 0:977e87915078 2269 if (coap_header->options_list_ptr && coap_header->options_list_ptr->uri_query_ptr) {
vithyat 0:977e87915078 2270 String uri_query = coap_to_string(coap_header->options_list_ptr->uri_query_ptr,
vithyat 0:977e87915078 2271 coap_header->options_list_ptr->uri_query_len);
vithyat 0:977e87915078 2272 tr_info("M2MNsdlInterface::handle_bootstrap_finished - query: %s", uri_query.c_str());
vithyat 0:977e87915078 2273 const char *iep_ptr = NULL;
vithyat 0:977e87915078 2274 const int iep_len = parse_query_parameter_value_from_query(uri_query.c_str(), QUERY_PARAM_IEP, &iep_ptr);
vithyat 0:977e87915078 2275 if (iep_ptr && iep_len > 0) {
vithyat 0:977e87915078 2276 est_iep_ok = true;
vithyat 0:977e87915078 2277 _internal_endpoint_name.clear();
vithyat 0:977e87915078 2278 _internal_endpoint_name.append_raw(iep_ptr, iep_len);
vithyat 0:977e87915078 2279 tr_info("M2MNsdlInterface::handle_bootstrap_finished - iep: %s", _internal_endpoint_name.c_str());
vithyat 0:977e87915078 2280 }
vithyat 0:977e87915078 2281 }
vithyat 0:977e87915078 2282 }
vithyat 0:977e87915078 2283 #endif
vithyat 0:977e87915078 2284
vithyat 0:977e87915078 2285 sn_coap_hdr_s *coap_response = NULL;
vithyat 0:977e87915078 2286 uint8_t msg_code = COAP_MSG_CODE_RESPONSE_CHANGED;
vithyat 0:977e87915078 2287 // Accept only '/bs' path and check that needed data is in security object
vithyat 0:977e87915078 2288 if (object_name.size() != 2 ||
vithyat 0:977e87915078 2289 object_name.compare(0, 2, BOOTSTRAP_URI) != 0) {
vithyat 0:977e87915078 2290 if (strlen(ERROR_REASON_22) + object_name.size() <= MAX_ALLOWED_ERROR_STRING_LENGTH) {
vithyat 0:977e87915078 2291 snprintf(buffer, sizeof(buffer), ERROR_REASON_22, object_name.c_str());
vithyat 0:977e87915078 2292 }
vithyat 0:977e87915078 2293 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 2294 }
vithyat 0:977e87915078 2295 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
vithyat 0:977e87915078 2296 else if (!est_iep_ok &&
vithyat 0:977e87915078 2297 m2m_id >= 0 &&
vithyat 0:977e87915078 2298 _security->resource_value_int(M2MSecurity::SecurityMode, m2m_id) == M2MSecurity::EST) {
vithyat 0:977e87915078 2299 tr_error("M2MNsdlInterface::handle_bootstrap_finished - EST mode but missing iep parameter!");
vithyat 0:977e87915078 2300 snprintf(buffer, sizeof(buffer), ERROR_REASON_26);
vithyat 0:977e87915078 2301 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 2302 }
vithyat 0:977e87915078 2303 #endif
vithyat 0:977e87915078 2304 else {
vithyat 0:977e87915078 2305 // Add short server id to server object
vithyat 0:977e87915078 2306 if (m2m_id == -1) {
vithyat 0:977e87915078 2307 snprintf(buffer,sizeof(buffer), ERROR_REASON_4);
vithyat 0:977e87915078 2308 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 2309 }
vithyat 0:977e87915078 2310 else {
vithyat 0:977e87915078 2311 _server->set_resource_value(M2MServer::ShortServerID,
vithyat 0:977e87915078 2312 _security->resource_value_int(M2MSecurity::ShortServerID, m2m_id));
vithyat 0:977e87915078 2313 }
vithyat 0:977e87915078 2314 }
vithyat 0:977e87915078 2315
vithyat 0:977e87915078 2316 // In ok case send response as a separate response
vithyat 0:977e87915078 2317 if (msg_code == COAP_MSG_CODE_RESPONSE_CHANGED) {
vithyat 0:977e87915078 2318 send_empty_ack(coap_header, address);
vithyat 0:977e87915078 2319 // In error case use piggybacked response
vithyat 0:977e87915078 2320 } else {
vithyat 0:977e87915078 2321 coap_response = sn_nsdl_build_response(_nsdl_handle, coap_header, msg_code);
vithyat 0:977e87915078 2322 if (coap_response) {
vithyat 0:977e87915078 2323 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
vithyat 0:977e87915078 2324 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 2325 }
vithyat 0:977e87915078 2326
vithyat 0:977e87915078 2327 handle_bootstrap_error(buffer, true);
vithyat 0:977e87915078 2328 }
vithyat 0:977e87915078 2329
vithyat 0:977e87915078 2330 // Send a event which is responsible of sending the final response
vithyat 0:977e87915078 2331 if (COAP_MSG_CODE_RESPONSE_CHANGED == msg_code) {
vithyat 0:977e87915078 2332 bool success = false;
vithyat 0:977e87915078 2333 sn_coap_hdr_s *coap_message = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 2334 coap_header,
vithyat 0:977e87915078 2335 (sn_coap_msg_code_e)msg_code);
vithyat 0:977e87915078 2336 if (coap_message) {
vithyat 0:977e87915078 2337 // Switch back to original ep name
vithyat 0:977e87915078 2338 memory_free(_endpoint->endpoint_name_ptr);
vithyat 0:977e87915078 2339 _endpoint->endpoint_name_ptr = alloc_string_copy((uint8_t*)_endpoint_name.c_str(), _endpoint_name.length());
vithyat 0:977e87915078 2340 if (_endpoint->endpoint_name_ptr) {
vithyat 0:977e87915078 2341 _endpoint->endpoint_name_len = _endpoint_name.length();
vithyat 0:977e87915078 2342 nsdl_coap_data_s *nsdl_coap_data = create_coap_event_data(coap_message,
vithyat 0:977e87915078 2343 address,
vithyat 0:977e87915078 2344 _nsdl_handle,
vithyat 0:977e87915078 2345 (sn_coap_msg_code_e)msg_code);
vithyat 0:977e87915078 2346 if (nsdl_coap_data) {
vithyat 0:977e87915078 2347 success = true;
vithyat 0:977e87915078 2348 _event.data.event_type = MBED_CLIENT_NSDLINTERFACE_BS_EVENT;
vithyat 0:977e87915078 2349 _event.data.data_ptr = (void*)nsdl_coap_data;
vithyat 0:977e87915078 2350 eventOS_event_send_user_allocated(&_event);
vithyat 0:977e87915078 2351 }
vithyat 0:977e87915078 2352 }
vithyat 0:977e87915078 2353 }
vithyat 0:977e87915078 2354
vithyat 0:977e87915078 2355 if (!success) {
vithyat 0:977e87915078 2356 const char *desc = "memory allocation failed";
vithyat 0:977e87915078 2357 if (strlen(ERROR_REASON_22) + strlen(desc) <= MAX_ALLOWED_ERROR_STRING_LENGTH) {
vithyat 0:977e87915078 2358 snprintf(buffer, sizeof(buffer), ERROR_REASON_22, desc);
vithyat 0:977e87915078 2359 }
vithyat 0:977e87915078 2360
vithyat 0:977e87915078 2361 handle_bootstrap_error(buffer, true);
vithyat 0:977e87915078 2362 }
vithyat 0:977e87915078 2363 }
vithyat 0:977e87915078 2364 #else
vithyat 0:977e87915078 2365 (void) coap_header;
vithyat 0:977e87915078 2366 (void) address;
vithyat 0:977e87915078 2367 #endif
vithyat 0:977e87915078 2368 }
vithyat 0:977e87915078 2369
vithyat 0:977e87915078 2370 void M2MNsdlInterface::handle_bootstrap_delete(sn_coap_hdr_s *coap_header,sn_nsdl_addr_s *address)
vithyat 0:977e87915078 2371 {
vithyat 0:977e87915078 2372
vithyat 0:977e87915078 2373 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 2374 char buffer[MAX_ALLOWED_ERROR_STRING_LENGTH];
vithyat 0:977e87915078 2375 memset(buffer,0,sizeof(buffer));
vithyat 0:977e87915078 2376 sn_coap_hdr_s *coap_response = NULL;
vithyat 0:977e87915078 2377 uint8_t msg_code = COAP_MSG_CODE_RESPONSE_DELETED;
vithyat 0:977e87915078 2378 String object_name = coap_to_string(coap_header->uri_path_ptr,
vithyat 0:977e87915078 2379 coap_header->uri_path_len);
vithyat 0:977e87915078 2380 tr_info("M2MNsdlInterface::handle_bootstrap_delete - obj %s", object_name.c_str());
vithyat 0:977e87915078 2381 if(!_identity_accepted) {
vithyat 0:977e87915078 2382 tr_warn("M2MNsdlInterface::handle_bootstrap_delete - Message received out-of-order - IGNORE");
vithyat 0:977e87915078 2383 return;
vithyat 0:977e87915078 2384 }
vithyat 0:977e87915078 2385 // Only following paths are accepted, 0, 0/0
vithyat 0:977e87915078 2386 else if (object_name.size() == 2 || object_name.size() > 3) {
vithyat 0:977e87915078 2387 if (strlen(ERROR_REASON_21) + object_name.size() <= MAX_ALLOWED_ERROR_STRING_LENGTH) {
vithyat 0:977e87915078 2388 snprintf(buffer, sizeof(buffer), ERROR_REASON_21,object_name.c_str());
vithyat 0:977e87915078 2389 }
vithyat 0:977e87915078 2390 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 2391 }
vithyat 0:977e87915078 2392 else if ((object_name.size() == 1 && object_name.compare(0,1,"0") != 0) ||
vithyat 0:977e87915078 2393 (object_name.size() == 3 && object_name.compare(0,3,"0/0") != 0)) {
vithyat 0:977e87915078 2394 if (strlen(ERROR_REASON_21) + object_name.size() <= MAX_ALLOWED_ERROR_STRING_LENGTH) {
vithyat 0:977e87915078 2395 snprintf(buffer, sizeof(buffer), ERROR_REASON_21, object_name.c_str());
vithyat 0:977e87915078 2396 }
vithyat 0:977e87915078 2397 msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
vithyat 0:977e87915078 2398 }
vithyat 0:977e87915078 2399
vithyat 0:977e87915078 2400 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 2401 coap_header,
vithyat 0:977e87915078 2402 msg_code);
vithyat 0:977e87915078 2403
vithyat 0:977e87915078 2404 if(coap_response) {
vithyat 0:977e87915078 2405 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
vithyat 0:977e87915078 2406 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 2407 if(_security) {
vithyat 0:977e87915078 2408 _security->clear_resources();
vithyat 0:977e87915078 2409 }
vithyat 0:977e87915078 2410 }
vithyat 0:977e87915078 2411 if (!coap_response || COAP_MSG_CODE_RESPONSE_DELETED != msg_code) {
vithyat 0:977e87915078 2412 handle_bootstrap_error(buffer, true);
vithyat 0:977e87915078 2413 }
vithyat 0:977e87915078 2414 #else
vithyat 0:977e87915078 2415 (void) coap_header;
vithyat 0:977e87915078 2416 (void) address;
vithyat 0:977e87915078 2417 #endif
vithyat 0:977e87915078 2418 }
vithyat 0:977e87915078 2419
vithyat 0:977e87915078 2420 bool M2MNsdlInterface::validate_security_object()
vithyat 0:977e87915078 2421 {
vithyat 0:977e87915078 2422 bool valid = false;
vithyat 0:977e87915078 2423 #ifndef M2M_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 2424 const M2MObjectInstanceList &instances = _security->instances();
vithyat 0:977e87915078 2425 M2MObjectInstanceList::const_iterator it;
vithyat 0:977e87915078 2426 it = instances.begin();
vithyat 0:977e87915078 2427 uint16_t instance_id = 0;
vithyat 0:977e87915078 2428 for ( ; it != instances.end(); it++ ) {
vithyat 0:977e87915078 2429 valid = true;
vithyat 0:977e87915078 2430 instance_id = (*it)->instance_id();
vithyat 0:977e87915078 2431 tr_debug("M2MNsdlInterface::validate_security_object - instance %d", instance_id);
vithyat 0:977e87915078 2432 String address = _security->resource_value_string(M2MSecurity::M2MServerUri, instance_id);
vithyat 0:977e87915078 2433 uint32_t sec_mode = _security->resource_value_int(M2MSecurity::SecurityMode, instance_id);
vithyat 0:977e87915078 2434 uint32_t is_bs_server = _security->resource_value_int(M2MSecurity::BootstrapServer, instance_id);
vithyat 0:977e87915078 2435
vithyat 0:977e87915078 2436 uint32_t chain_size = 0;
vithyat 0:977e87915078 2437 uint32_t server_key_size = 0;
vithyat 0:977e87915078 2438 uint32_t pkey_size = 0;
vithyat 0:977e87915078 2439
vithyat 0:977e87915078 2440 size_t buffer_size = MAX_CERTIFICATE_SIZE;
vithyat 0:977e87915078 2441 uint8_t certificate[MAX_CERTIFICATE_SIZE];
vithyat 0:977e87915078 2442 uint8_t *certificate_ptr = (uint8_t *)&certificate;
vithyat 0:977e87915078 2443
vithyat 0:977e87915078 2444 // Read through callback if set
vithyat 0:977e87915078 2445 M2MResource *res = _security->get_resource(M2MSecurity::OpenCertificateChain, instance_id);
vithyat 0:977e87915078 2446 if (res) {
vithyat 0:977e87915078 2447 M2MBase::lwm2m_parameters_s *param = res->get_lwm2m_parameters();
vithyat 0:977e87915078 2448 if (param->read_write_callback_set) {
vithyat 0:977e87915078 2449 // Read the chain size
vithyat 0:977e87915078 2450 if (_security->resource_value_buffer(M2MSecurity::OpenCertificateChain, certificate_ptr, instance_id, &buffer_size) == 0) {
vithyat 0:977e87915078 2451 // Only set size if no error when reading
vithyat 0:977e87915078 2452 chain_size = buffer_size;
vithyat 0:977e87915078 2453 }
vithyat 0:977e87915078 2454 _security->resource_value_buffer(M2MSecurity::CloseCertificateChain, certificate_ptr, instance_id, &buffer_size);
vithyat 0:977e87915078 2455 } else {
vithyat 0:977e87915078 2456 // Read directly from the resource
vithyat 0:977e87915078 2457 if (_security->resource_value_buffer(M2MSecurity::PublicKey, certificate_ptr, instance_id, &buffer_size) == 0) {
vithyat 0:977e87915078 2458 // Only set size if no error when reading
vithyat 0:977e87915078 2459 chain_size = buffer_size;
vithyat 0:977e87915078 2460 }
vithyat 0:977e87915078 2461 }
vithyat 0:977e87915078 2462 }
vithyat 0:977e87915078 2463
vithyat 0:977e87915078 2464 buffer_size = MAX_CERTIFICATE_SIZE;
vithyat 0:977e87915078 2465
vithyat 0:977e87915078 2466 if (_security->resource_value_buffer(M2MSecurity::ServerPublicKey, certificate_ptr, instance_id, &buffer_size) == 0) {
vithyat 0:977e87915078 2467 // Only set size if no error when reading
vithyat 0:977e87915078 2468 server_key_size = buffer_size;
vithyat 0:977e87915078 2469 }
vithyat 0:977e87915078 2470
vithyat 0:977e87915078 2471 buffer_size = MAX_CERTIFICATE_SIZE;
vithyat 0:977e87915078 2472 if (_security->resource_value_buffer(M2MSecurity::Secretkey, certificate_ptr, instance_id, &buffer_size) == 0) {
vithyat 0:977e87915078 2473 // Only set size if no error when reading
vithyat 0:977e87915078 2474 pkey_size = buffer_size;
vithyat 0:977e87915078 2475 }
vithyat 0:977e87915078 2476
vithyat 0:977e87915078 2477 tr_info("M2MNsdlInterface::validate_security_object - Server URI /0/0: %s", address.c_str());
vithyat 0:977e87915078 2478 tr_info("M2MNsdlInterface::validate_security_object - is bs server /0/1: %" PRIu32, is_bs_server);
vithyat 0:977e87915078 2479 tr_info("M2MNsdlInterface::validate_security_object - Security Mode /0/2: %" PRIu32, sec_mode);
vithyat 0:977e87915078 2480 tr_info("M2MNsdlInterface::validate_security_object - Public chain size /0/3: %" PRIu32, chain_size);
vithyat 0:977e87915078 2481 tr_info("M2MNsdlInterface::validate_security_object - Server Public key size /0/4: %" PRIu32, server_key_size);
vithyat 0:977e87915078 2482 tr_info("M2MNsdlInterface::validate_security_object - Secret key size /0/5: %" PRIu32, pkey_size);
vithyat 0:977e87915078 2483 if (address.empty()) {
vithyat 0:977e87915078 2484 return false;
vithyat 0:977e87915078 2485 }
vithyat 0:977e87915078 2486
vithyat 0:977e87915078 2487 switch (sec_mode) {
vithyat 0:977e87915078 2488 case M2MSecurity::Certificate:
vithyat 0:977e87915078 2489 // Server public key and client private and public keys should be populated
vithyat 0:977e87915078 2490 if (!chain_size || !server_key_size || !pkey_size) {
vithyat 0:977e87915078 2491 return false;
vithyat 0:977e87915078 2492 }
vithyat 0:977e87915078 2493 break;
vithyat 0:977e87915078 2494 #ifndef MBED_CLIENT_DISABLE_EST_FEATURE
vithyat 0:977e87915078 2495 case M2MSecurity::EST:
vithyat 0:977e87915078 2496 // Only server public key should be populated for lwm2m, client keys will be generated
vithyat 0:977e87915078 2497 if (!is_bs_server && (!server_key_size || chain_size || pkey_size)) {
vithyat 0:977e87915078 2498 return false;
vithyat 0:977e87915078 2499 }
vithyat 0:977e87915078 2500 break;
vithyat 0:977e87915078 2501 #endif
vithyat 0:977e87915078 2502 case M2MSecurity::NoSecurity:
vithyat 0:977e87915078 2503 // Nothing to check for no security
vithyat 0:977e87915078 2504 break;
vithyat 0:977e87915078 2505 default:
vithyat 0:977e87915078 2506 // Security mode not supported
vithyat 0:977e87915078 2507 return false;
vithyat 0:977e87915078 2508 }
vithyat 0:977e87915078 2509 }
vithyat 0:977e87915078 2510 #endif
vithyat 0:977e87915078 2511 return valid;
vithyat 0:977e87915078 2512 }
vithyat 0:977e87915078 2513
vithyat 0:977e87915078 2514
vithyat 0:977e87915078 2515 void M2MNsdlInterface::handle_bootstrap_error(const char *reason, bool wait)
vithyat 0:977e87915078 2516 {
vithyat 0:977e87915078 2517 tr_error("M2MNsdlInterface::handle_bootstrap_error(%s)",reason);
vithyat 0:977e87915078 2518 _identity_accepted = false;
vithyat 0:977e87915078 2519
vithyat 0:977e87915078 2520 if (wait) {
vithyat 0:977e87915078 2521 _observer.bootstrap_error_wait(reason);
vithyat 0:977e87915078 2522 } else {
vithyat 0:977e87915078 2523 _observer.bootstrap_error(reason);
vithyat 0:977e87915078 2524 }
vithyat 0:977e87915078 2525 }
vithyat 0:977e87915078 2526
vithyat 0:977e87915078 2527 const String& M2MNsdlInterface::endpoint_name() const
vithyat 0:977e87915078 2528 {
vithyat 0:977e87915078 2529 return _endpoint_name;
vithyat 0:977e87915078 2530 }
vithyat 0:977e87915078 2531
vithyat 0:977e87915078 2532 const String M2MNsdlInterface::internal_endpoint_name() const
vithyat 0:977e87915078 2533 {
vithyat 0:977e87915078 2534 String iep;
vithyat 0:977e87915078 2535 if (_internal_endpoint_name.length() > 0) {
vithyat 0:977e87915078 2536 iep = _internal_endpoint_name;
vithyat 0:977e87915078 2537 }
vithyat 0:977e87915078 2538 else if (_nsdl_handle->ep_information_ptr->location_ptr) {
vithyat 0:977e87915078 2539 // If internal_endpoint_name not set yet, parse it from location path
vithyat 0:977e87915078 2540 String temp((const char*)_nsdl_handle->ep_information_ptr->location_ptr,
vithyat 0:977e87915078 2541 _nsdl_handle->ep_information_ptr->location_len);
vithyat 0:977e87915078 2542 // Get last part of the location path.
vithyat 0:977e87915078 2543 // In mbed Cloud environment full path is /rd/accountid/internal_endpoint
vithyat 0:977e87915078 2544 int location = temp.find_last_of('/') + 1;
vithyat 0:977e87915078 2545 iep.append_raw((const char*)_nsdl_handle->ep_information_ptr->location_ptr + location,
vithyat 0:977e87915078 2546 _nsdl_handle->ep_information_ptr->location_len - location);
vithyat 0:977e87915078 2547 }
vithyat 0:977e87915078 2548 return iep;
vithyat 0:977e87915078 2549 }
vithyat 0:977e87915078 2550
vithyat 0:977e87915078 2551 void M2MNsdlInterface::change_operation_mode(M2MObject *object, M2MBase::Operation operation)
vithyat 0:977e87915078 2552 {
vithyat 0:977e87915078 2553 const M2MObjectInstanceList &instances = object->instances();
vithyat 0:977e87915078 2554 M2MObjectInstanceList::const_iterator inst = instances.begin();
vithyat 0:977e87915078 2555 for (; inst != instances.end(); inst++ ) {
vithyat 0:977e87915078 2556 (*inst)->set_operation(operation);
vithyat 0:977e87915078 2557 const M2MResourceList &list = (*inst)->resources();
vithyat 0:977e87915078 2558 if(!list.empty()) {
vithyat 0:977e87915078 2559 M2MResourceList::const_iterator it;
vithyat 0:977e87915078 2560 it = list.begin();
vithyat 0:977e87915078 2561 for ( ; it != list.end(); it++ ) {
vithyat 0:977e87915078 2562 (*it)->set_operation(operation);
vithyat 0:977e87915078 2563 }
vithyat 0:977e87915078 2564 }
vithyat 0:977e87915078 2565 }
vithyat 0:977e87915078 2566 }
vithyat 0:977e87915078 2567
vithyat 0:977e87915078 2568 void M2MNsdlInterface::set_server_address(const char* server_address)
vithyat 0:977e87915078 2569 {
vithyat 0:977e87915078 2570 free(_server_address);
vithyat 0:977e87915078 2571 _server_address = M2MBase::alloc_string_copy(server_address);
vithyat 0:977e87915078 2572 }
vithyat 0:977e87915078 2573
vithyat 0:977e87915078 2574 M2MTimer &M2MNsdlInterface::get_nsdl_execution_timer()
vithyat 0:977e87915078 2575 {
vithyat 0:977e87915078 2576 return _nsdl_execution_timer;
vithyat 0:977e87915078 2577 }
vithyat 0:977e87915078 2578
vithyat 0:977e87915078 2579 bool M2MNsdlInterface::is_unregister_ongoing() const
vithyat 0:977e87915078 2580 {
vithyat 0:977e87915078 2581 return _nsdl_handle->unregister_token == 0 ? false : true;
vithyat 0:977e87915078 2582 }
vithyat 0:977e87915078 2583
vithyat 0:977e87915078 2584 bool M2MNsdlInterface::parse_and_send_uri_query_parameters()
vithyat 0:977e87915078 2585 {
vithyat 0:977e87915078 2586 bool msg_sent = false;
vithyat 0:977e87915078 2587 char *address_copy = M2MBase::alloc_string_copy(_server_address);
vithyat 0:977e87915078 2588 if (address_copy) {
vithyat 0:977e87915078 2589 const char* query = parse_uri_query_parameters(_server_address);
vithyat 0:977e87915078 2590 if (query != NULL) {
vithyat 0:977e87915078 2591 size_t query_len = 1 + strlen(query) + 1 + strlen(MCC_VERSION) + 1;
vithyat 0:977e87915078 2592 if (_custom_uri_query_params) {
vithyat 0:977e87915078 2593 query_len += 1 + strlen(_custom_uri_query_params);
vithyat 0:977e87915078 2594 }
vithyat 0:977e87915078 2595
vithyat 0:977e87915078 2596 if (query_len <= MAX_URI_QUERY_LEN) {
vithyat 0:977e87915078 2597 char query_params[MAX_URI_QUERY_LEN];
vithyat 0:977e87915078 2598 strcpy(query_params, "&");
vithyat 0:977e87915078 2599 strcat(query_params, query);
vithyat 0:977e87915078 2600 strcat(query_params, "&");
vithyat 0:977e87915078 2601 strcat(query_params, MCC_VERSION);
vithyat 0:977e87915078 2602 if (_custom_uri_query_params) {
vithyat 0:977e87915078 2603 strcat(query_params, "&");
vithyat 0:977e87915078 2604 strcat(query_params, _custom_uri_query_params);
vithyat 0:977e87915078 2605 }
vithyat 0:977e87915078 2606
vithyat 0:977e87915078 2607 tr_debug("M2MNsdlInterface::parse_and_send_uri_query_parameters - uri params: %s", query_params);
vithyat 0:977e87915078 2608 msg_sent = sn_nsdl_register_endpoint(_nsdl_handle,_endpoint,query_params) != 0;
vithyat 0:977e87915078 2609 } else {
vithyat 0:977e87915078 2610 tr_error("M2MNsdlInterface::parse_and_send_uri_query_parameters - max uri param length reached (%lu)",
vithyat 0:977e87915078 2611 (unsigned long)query_len);
vithyat 0:977e87915078 2612 }
vithyat 0:977e87915078 2613 }
vithyat 0:977e87915078 2614 free(address_copy);
vithyat 0:977e87915078 2615 }
vithyat 0:977e87915078 2616 return msg_sent;
vithyat 0:977e87915078 2617 }
vithyat 0:977e87915078 2618
vithyat 0:977e87915078 2619 void M2MNsdlInterface::claim_mutex()
vithyat 0:977e87915078 2620 {
vithyat 0:977e87915078 2621 _connection_handler.claim_mutex();
vithyat 0:977e87915078 2622 }
vithyat 0:977e87915078 2623
vithyat 0:977e87915078 2624 void M2MNsdlInterface::release_mutex()
vithyat 0:977e87915078 2625 {
vithyat 0:977e87915078 2626 _connection_handler.release_mutex();
vithyat 0:977e87915078 2627 }
vithyat 0:977e87915078 2628
vithyat 0:977e87915078 2629 void M2MNsdlInterface::start_nsdl_execution_timer()
vithyat 0:977e87915078 2630 {
vithyat 0:977e87915078 2631 tr_debug("M2MNsdlInterface::start_nsdl_execution_timer");
vithyat 0:977e87915078 2632 _nsdl_execution_timer_running = true;
vithyat 0:977e87915078 2633 _nsdl_execution_timer.stop_timer();
vithyat 0:977e87915078 2634 _nsdl_execution_timer.start_timer(ONE_SECOND_TIMER * 1000,
vithyat 0:977e87915078 2635 M2MTimerObserver::NsdlExecution,
vithyat 0:977e87915078 2636 false);
vithyat 0:977e87915078 2637 }
vithyat 0:977e87915078 2638
vithyat 0:977e87915078 2639 M2MSecurity* M2MNsdlInterface::get_security_object()
vithyat 0:977e87915078 2640 {
vithyat 0:977e87915078 2641 return _security;
vithyat 0:977e87915078 2642 }
vithyat 0:977e87915078 2643
vithyat 0:977e87915078 2644 void M2MNsdlInterface::update_trigger_callback(void */*argument*/)
vithyat 0:977e87915078 2645 {
vithyat 0:977e87915078 2646 if (!send_update_registration()) {
vithyat 0:977e87915078 2647 // Most likely case would be memory allocation failure
vithyat 0:977e87915078 2648 _observer.registration_error(M2MInterface::MemoryFail, false);
vithyat 0:977e87915078 2649 }
vithyat 0:977e87915078 2650 }
vithyat 0:977e87915078 2651
vithyat 0:977e87915078 2652 bool M2MNsdlInterface::lifetime_value_changed() const
vithyat 0:977e87915078 2653 {
vithyat 0:977e87915078 2654 uint64_t value = 0;
vithyat 0:977e87915078 2655 if (_endpoint && _endpoint->lifetime_ptr) {
vithyat 0:977e87915078 2656 value = atol((const char*)_endpoint->lifetime_ptr);
vithyat 0:977e87915078 2657 }
vithyat 0:977e87915078 2658 if (_server->resource_value_int(M2MServer::Lifetime) != value) {
vithyat 0:977e87915078 2659 return true;
vithyat 0:977e87915078 2660 }
vithyat 0:977e87915078 2661 return false;
vithyat 0:977e87915078 2662 }
vithyat 0:977e87915078 2663
vithyat 0:977e87915078 2664 void M2MNsdlInterface::execute_notification_delivery_status_cb(M2MBase* object, int32_t msgid)
vithyat 0:977e87915078 2665 {
vithyat 0:977e87915078 2666 if (msgid > 0) {
vithyat 0:977e87915078 2667 object->send_notification_delivery_status(*object, NOTIFICATION_STATUS_SENT);
vithyat 0:977e87915078 2668 object->send_message_delivery_status(*object,
vithyat 0:977e87915078 2669 M2MBase::MESSAGE_STATUS_SENT,
vithyat 0:977e87915078 2670 M2MBase::NOTIFICATION);
vithyat 0:977e87915078 2671 store_to_response_list(object->uri_path(), msgid, M2MBase::NOTIFICATION);
vithyat 0:977e87915078 2672 } else {
vithyat 0:977e87915078 2673 object->send_notification_delivery_status(*object, NOTIFICATION_STATUS_BUILD_ERROR);
vithyat 0:977e87915078 2674 object->send_message_delivery_status(*object,
vithyat 0:977e87915078 2675 M2MBase::MESSAGE_STATUS_BUILD_ERROR,
vithyat 0:977e87915078 2676 M2MBase::NOTIFICATION);
vithyat 0:977e87915078 2677 _notification_send_ongoing = false;
vithyat 0:977e87915078 2678 }
vithyat 0:977e87915078 2679 }
vithyat 0:977e87915078 2680
vithyat 0:977e87915078 2681 uint8_t M2MNsdlInterface::find_auto_obs_token(const char *path, uint8_t *token) const
vithyat 0:977e87915078 2682 {
vithyat 0:977e87915078 2683 uint8_t token_len = 0;
vithyat 0:977e87915078 2684 const String name(path);
vithyat 0:977e87915078 2685 M2MBase *object = find_resource(name);
vithyat 0:977e87915078 2686 if (object) {
vithyat 0:977e87915078 2687 object->get_observation_token(token, token_len);
vithyat 0:977e87915078 2688 }
vithyat 0:977e87915078 2689 return token_len;
vithyat 0:977e87915078 2690 }
vithyat 0:977e87915078 2691
vithyat 0:977e87915078 2692 bool M2MNsdlInterface::is_response_to_request(const sn_coap_hdr_s *coap_header, request_context_s &get_data)
vithyat 0:977e87915078 2693 {
vithyat 0:977e87915078 2694 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 2695 request_context_s *data = (request_context_s *)ns_list_get_first(&_request_context_list);
vithyat 0:977e87915078 2696 while (data) {
vithyat 0:977e87915078 2697 if (memcmp(coap_header->token_ptr, &data->msg_token, sizeof(data->msg_token)) == 0) {
vithyat 0:977e87915078 2698 get_data = *data;
vithyat 0:977e87915078 2699 return true;
vithyat 0:977e87915078 2700 }
vithyat 0:977e87915078 2701 data = (request_context_s *)ns_list_get_next(&_request_context_list, data);
vithyat 0:977e87915078 2702 }
vithyat 0:977e87915078 2703
vithyat 0:977e87915078 2704 return false;
vithyat 0:977e87915078 2705 }
vithyat 0:977e87915078 2706
vithyat 0:977e87915078 2707 void M2MNsdlInterface::free_request_context_list(const sn_coap_hdr_s *coap_header, bool call_error_cb, request_error_t error_code)
vithyat 0:977e87915078 2708 {
vithyat 0:977e87915078 2709 // Clean up whole list
vithyat 0:977e87915078 2710 if (coap_header == NULL) {
vithyat 0:977e87915078 2711 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 2712 while (!ns_list_is_empty(&_request_context_list)) {
vithyat 0:977e87915078 2713 request_context_s* data = (request_context_s*)ns_list_get_first(&_request_context_list);
vithyat 0:977e87915078 2714 if (call_error_cb) {
vithyat 0:977e87915078 2715 data->on_request_error_cb(error_code, data->context);
vithyat 0:977e87915078 2716 }
vithyat 0:977e87915078 2717 ns_list_remove(&_request_context_list, data);
vithyat 0:977e87915078 2718 memory_free(data->uri_path);
vithyat 0:977e87915078 2719 memory_free(data);
vithyat 0:977e87915078 2720 }
vithyat 0:977e87915078 2721
vithyat 0:977e87915078 2722 // Clean just one item from the list
vithyat 0:977e87915078 2723 } else {
vithyat 0:977e87915078 2724 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 2725 request_context_s *data = (request_context_s *)ns_list_get_first(&_request_context_list);
vithyat 0:977e87915078 2726 while (data) {
vithyat 0:977e87915078 2727 if (memcmp(coap_header->token_ptr, &data->msg_token, sizeof(data->msg_token)) == 0) {
vithyat 0:977e87915078 2728 if (call_error_cb) {
vithyat 0:977e87915078 2729 data->on_request_error_cb(error_code, data->context);
vithyat 0:977e87915078 2730 }
vithyat 0:977e87915078 2731 ns_list_remove(&_request_context_list, data);
vithyat 0:977e87915078 2732 memory_free(data->uri_path);
vithyat 0:977e87915078 2733 memory_free(data);
vithyat 0:977e87915078 2734 return;
vithyat 0:977e87915078 2735 }
vithyat 0:977e87915078 2736 data = (request_context_s *)ns_list_get_next(&_request_context_list, data);
vithyat 0:977e87915078 2737 }
vithyat 0:977e87915078 2738 }
vithyat 0:977e87915078 2739 }
vithyat 0:977e87915078 2740
vithyat 0:977e87915078 2741 void M2MNsdlInterface::set_request_context_to_be_resend(uint8_t *token, uint8_t token_len)
vithyat 0:977e87915078 2742 {
vithyat 0:977e87915078 2743 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 2744 request_context_s *data = (request_context_s *)ns_list_get_first(&_request_context_list);
vithyat 0:977e87915078 2745 while (data) {
vithyat 0:977e87915078 2746 if (token && token_len) {
vithyat 0:977e87915078 2747 if (token_len == sizeof(data->msg_token) &&
vithyat 0:977e87915078 2748 memcmp((uint8_t*)&data->msg_token, token, token_len) == 0) {
vithyat 0:977e87915078 2749 data->resend = true;
vithyat 0:977e87915078 2750 }
vithyat 0:977e87915078 2751 } else {
vithyat 0:977e87915078 2752 data->resend = true;
vithyat 0:977e87915078 2753 }
vithyat 0:977e87915078 2754 data = (request_context_s *)ns_list_get_next(&_request_context_list, data);
vithyat 0:977e87915078 2755 }
vithyat 0:977e87915078 2756 }
vithyat 0:977e87915078 2757
vithyat 0:977e87915078 2758 char* M2MNsdlInterface::parse_uri_query_parameters(char* uri)
vithyat 0:977e87915078 2759 {
vithyat 0:977e87915078 2760 char* query = strchr((char*)uri, '?');
vithyat 0:977e87915078 2761 if (query != NULL) {
vithyat 0:977e87915078 2762 query++;
vithyat 0:977e87915078 2763 if (*query == '\0') {
vithyat 0:977e87915078 2764 return NULL;
vithyat 0:977e87915078 2765 } else {
vithyat 0:977e87915078 2766 return query;
vithyat 0:977e87915078 2767 }
vithyat 0:977e87915078 2768 } else {
vithyat 0:977e87915078 2769 return NULL;
vithyat 0:977e87915078 2770 }
vithyat 0:977e87915078 2771 }
vithyat 0:977e87915078 2772
vithyat 0:977e87915078 2773 bool M2MNsdlInterface::set_uri_query_parameters(const char *uri_query_params)
vithyat 0:977e87915078 2774 {
vithyat 0:977e87915078 2775 tr_debug("M2MNsdlInterface::set_uri_query_parameters");
vithyat 0:977e87915078 2776 size_t query_len = uri_query_params == NULL ? 0:strlen(uri_query_params);
vithyat 0:977e87915078 2777 size_t current_len = _custom_uri_query_params == NULL ? 0:strlen(_custom_uri_query_params);
vithyat 0:977e87915078 2778 size_t new_size = query_len + current_len;
vithyat 0:977e87915078 2779
vithyat 0:977e87915078 2780 if (query_len == 0 ||
vithyat 0:977e87915078 2781 query_len > MAX_ALLOWED_STRING_LENGTH ||
vithyat 0:977e87915078 2782 new_size > MAX_ALLOWED_STRING_LENGTH) {
vithyat 0:977e87915078 2783 tr_error("M2MNsdlInterface::set_uri_query_parameters - invalid params!");
vithyat 0:977e87915078 2784 return false;
vithyat 0:977e87915078 2785 }
vithyat 0:977e87915078 2786
vithyat 0:977e87915078 2787 // Append into existing string
vithyat 0:977e87915078 2788 if (_custom_uri_query_params) {
vithyat 0:977e87915078 2789 // Reserve space for "&" and null marks
vithyat 0:977e87915078 2790 _custom_uri_query_params = (char*)realloc(_custom_uri_query_params, 1 + new_size + 1);
vithyat 0:977e87915078 2791 if (_custom_uri_query_params == NULL) {
vithyat 0:977e87915078 2792 return false;
vithyat 0:977e87915078 2793 }
vithyat 0:977e87915078 2794
vithyat 0:977e87915078 2795 memcpy(_custom_uri_query_params + current_len, "&", 1);
vithyat 0:977e87915078 2796 memcpy(_custom_uri_query_params + current_len + 1, uri_query_params, query_len);
vithyat 0:977e87915078 2797 _custom_uri_query_params[1 + new_size] = '\0';
vithyat 0:977e87915078 2798 } else {
vithyat 0:977e87915078 2799 _custom_uri_query_params = (char*)alloc_string_copy((uint8_t*)uri_query_params, query_len + 1);
vithyat 0:977e87915078 2800 if (_custom_uri_query_params == NULL) {
vithyat 0:977e87915078 2801 return false;
vithyat 0:977e87915078 2802 }
vithyat 0:977e87915078 2803 }
vithyat 0:977e87915078 2804
vithyat 0:977e87915078 2805 tr_info("M2MNsdlInterface::set_uri_query_parameters - query %s", _custom_uri_query_params);
vithyat 0:977e87915078 2806 return true;
vithyat 0:977e87915078 2807 }
vithyat 0:977e87915078 2808
vithyat 0:977e87915078 2809 void M2MNsdlInterface::clear_sent_blockwise_messages()
vithyat 0:977e87915078 2810 {
vithyat 0:977e87915078 2811 sn_nsdl_clear_coap_sent_blockwise_messages(_nsdl_handle);
vithyat 0:977e87915078 2812 }
vithyat 0:977e87915078 2813
vithyat 0:977e87915078 2814 void M2MNsdlInterface::clear_received_blockwise_messages()
vithyat 0:977e87915078 2815 {
vithyat 0:977e87915078 2816 sn_nsdl_clear_coap_received_blockwise_messages(_nsdl_handle);
vithyat 0:977e87915078 2817 }
vithyat 0:977e87915078 2818
vithyat 0:977e87915078 2819 void M2MNsdlInterface::send_coap_ping()
vithyat 0:977e87915078 2820 {
vithyat 0:977e87915078 2821 if (_binding_mode == M2MInterface::TCP && _registered &&
vithyat 0:977e87915078 2822 _counter_for_nsdl == _next_coap_ping_send_time &&
vithyat 0:977e87915078 2823 !coap_ping_in_process()) {
vithyat 0:977e87915078 2824
vithyat 0:977e87915078 2825 tr_info("M2MNsdlInterface::send_coap_ping()");
vithyat 0:977e87915078 2826
vithyat 0:977e87915078 2827 // Build the CoAP here as the CoAP builder would add the message to re-sending queue.
vithyat 0:977e87915078 2828 // Store the id to prevent multiple simultanous ping messages, may happen if ping interval is shorter than total retransmission time.
vithyat 0:977e87915078 2829 int32_t message_id = sn_nsdl_send_coap_ping(_nsdl_handle);
vithyat 0:977e87915078 2830 if (message_id > 0) {
vithyat 0:977e87915078 2831 store_to_response_list(NULL, message_id, M2MBase::PING);
vithyat 0:977e87915078 2832 } else {
vithyat 0:977e87915078 2833 tr_error("M2MNsdlInterface::send_coap_ping() - failed to create ping message!");
vithyat 0:977e87915078 2834 }
vithyat 0:977e87915078 2835 }
vithyat 0:977e87915078 2836 }
vithyat 0:977e87915078 2837
vithyat 0:977e87915078 2838 void M2MNsdlInterface::calculate_new_coap_ping_send_time()
vithyat 0:977e87915078 2839 {
vithyat 0:977e87915078 2840 if (_binding_mode != M2MInterface::TCP) {
vithyat 0:977e87915078 2841 return;
vithyat 0:977e87915078 2842 }
vithyat 0:977e87915078 2843
vithyat 0:977e87915078 2844 _next_coap_ping_send_time = _counter_for_nsdl + MBED_CLIENT_TCP_KEEPALIVE_INTERVAL;
vithyat 0:977e87915078 2845 }
vithyat 0:977e87915078 2846
vithyat 0:977e87915078 2847 void M2MNsdlInterface::send_next_notification(bool clear_token)
vithyat 0:977e87915078 2848 {
vithyat 0:977e87915078 2849 tr_debug("M2MNsdlInterface::send_next_notification");
vithyat 0:977e87915078 2850 claim_mutex();
vithyat 0:977e87915078 2851 if (!_base_list.empty()) {
vithyat 0:977e87915078 2852 M2MBaseList::const_iterator base_iterator;
vithyat 0:977e87915078 2853 base_iterator = _base_list.begin();
vithyat 0:977e87915078 2854 for ( ; base_iterator != _base_list.end(); base_iterator++ ) {
vithyat 0:977e87915078 2855 if ((*base_iterator)->base_type() == M2MBase::Object) {
vithyat 0:977e87915078 2856 if (send_next_notification_for_object(*(M2MObject*)*base_iterator, clear_token)) {
vithyat 0:977e87915078 2857 release_mutex();
vithyat 0:977e87915078 2858 return;
vithyat 0:977e87915078 2859 }
vithyat 0:977e87915078 2860 }
vithyat 0:977e87915078 2861 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 2862 else if ((*base_iterator)->base_type() == M2MBase::ObjectDirectory) {
vithyat 0:977e87915078 2863 M2MEndpoint* endpoint = static_cast<M2MEndpoint*> (*base_iterator);
vithyat 0:977e87915078 2864 const M2MObjectList& object_list = endpoint->objects();
vithyat 0:977e87915078 2865 if (!object_list.empty()) {
vithyat 0:977e87915078 2866 M2MObjectList::const_iterator object_iterator;
vithyat 0:977e87915078 2867 object_iterator = object_list.begin();
vithyat 0:977e87915078 2868 // Object level
vithyat 0:977e87915078 2869 for ( ; object_iterator != object_list.end(); object_iterator++ ) {
vithyat 0:977e87915078 2870 if (send_next_notification_for_object(**object_iterator, clear_token)) {
vithyat 0:977e87915078 2871 release_mutex();
vithyat 0:977e87915078 2872 return;
vithyat 0:977e87915078 2873 }
vithyat 0:977e87915078 2874 }
vithyat 0:977e87915078 2875 }
vithyat 0:977e87915078 2876 }
vithyat 0:977e87915078 2877 #endif
vithyat 0:977e87915078 2878 }
vithyat 0:977e87915078 2879 }
vithyat 0:977e87915078 2880
vithyat 0:977e87915078 2881 _notification_send_ongoing = false;
vithyat 0:977e87915078 2882 release_mutex();
vithyat 0:977e87915078 2883 tr_debug("M2MNsdlInterface::send_next_notification - nothing to send");
vithyat 0:977e87915078 2884 }
vithyat 0:977e87915078 2885
vithyat 0:977e87915078 2886 bool M2MNsdlInterface::send_next_notification_for_object(M2MObject& object, bool clear_token) {
vithyat 0:977e87915078 2887 const M2MObjectInstanceList &object_instance_list = object.instances();
vithyat 0:977e87915078 2888 M2MReportHandler* reporter = object.report_handler();
vithyat 0:977e87915078 2889 if (reporter) {
vithyat 0:977e87915078 2890 if (clear_token && !object.get_nsdl_resource()->auto_observable) {
vithyat 0:977e87915078 2891 reporter->set_observation_token(NULL, 0);
vithyat 0:977e87915078 2892 } else if (reporter->is_under_observation() &&
vithyat 0:977e87915078 2893 (reporter->notification_in_queue() || reporter->notification_send_in_progress())) {
vithyat 0:977e87915078 2894 reporter->schedule_report(true);
vithyat 0:977e87915078 2895 return true;
vithyat 0:977e87915078 2896 }
vithyat 0:977e87915078 2897 }
vithyat 0:977e87915078 2898
vithyat 0:977e87915078 2899 // Object instance level
vithyat 0:977e87915078 2900 if (!object_instance_list.empty()) {
vithyat 0:977e87915078 2901 M2MObjectInstanceList::const_iterator object_instance_iterator;
vithyat 0:977e87915078 2902 object_instance_iterator = object_instance_list.begin();
vithyat 0:977e87915078 2903 for ( ; object_instance_iterator != object_instance_list.end(); object_instance_iterator++ ) {
vithyat 0:977e87915078 2904 reporter = (*object_instance_iterator)->report_handler();
vithyat 0:977e87915078 2905 if (reporter) {
vithyat 0:977e87915078 2906 if (clear_token && !(*object_instance_iterator)->get_nsdl_resource()->auto_observable) {
vithyat 0:977e87915078 2907 reporter->set_observation_token(NULL, 0);
vithyat 0:977e87915078 2908 } else if (reporter->is_under_observation() &&
vithyat 0:977e87915078 2909 (reporter->notification_in_queue() || reporter->notification_send_in_progress())) {
vithyat 0:977e87915078 2910 reporter->schedule_report(true);
vithyat 0:977e87915078 2911 return true;
vithyat 0:977e87915078 2912 }
vithyat 0:977e87915078 2913 }
vithyat 0:977e87915078 2914
vithyat 0:977e87915078 2915 // Resource level
vithyat 0:977e87915078 2916 const M2MResourceList &resource_list = (*object_instance_iterator)->resources();
vithyat 0:977e87915078 2917 if (!resource_list.empty()) {
vithyat 0:977e87915078 2918 M2MResourceList::const_iterator resource_iterator;
vithyat 0:977e87915078 2919 resource_iterator = resource_list.begin();
vithyat 0:977e87915078 2920 for ( ; resource_iterator != resource_list.end(); resource_iterator++) {
vithyat 0:977e87915078 2921 reporter = (*resource_iterator)->report_handler();
vithyat 0:977e87915078 2922 if (reporter) {
vithyat 0:977e87915078 2923 // Auto obs token can't be cleared
vithyat 0:977e87915078 2924 if (clear_token && !(*resource_iterator)->get_nsdl_resource()->auto_observable) {
vithyat 0:977e87915078 2925 reporter->set_observation_token(NULL, 0);
vithyat 0:977e87915078 2926 } else if (reporter->is_under_observation() &&
vithyat 0:977e87915078 2927 (reporter->notification_in_queue() || reporter->notification_send_in_progress())) {
vithyat 0:977e87915078 2928 reporter->schedule_report(true);
vithyat 0:977e87915078 2929 return true;
vithyat 0:977e87915078 2930 }
vithyat 0:977e87915078 2931 }
vithyat 0:977e87915078 2932 }
vithyat 0:977e87915078 2933 }
vithyat 0:977e87915078 2934 }
vithyat 0:977e87915078 2935 }
vithyat 0:977e87915078 2936
vithyat 0:977e87915078 2937 return false;
vithyat 0:977e87915078 2938 }
vithyat 0:977e87915078 2939
vithyat 0:977e87915078 2940 void M2MNsdlInterface::send_empty_ack(const sn_coap_hdr_s *header, sn_nsdl_addr_s *address)
vithyat 0:977e87915078 2941 {
vithyat 0:977e87915078 2942 tr_debug("M2MNsdlInterface::send_empty_ack()");
vithyat 0:977e87915078 2943 sn_coap_hdr_s *empty_coap_ack = (sn_coap_hdr_s *) memory_alloc(sizeof(sn_coap_hdr_s));
vithyat 0:977e87915078 2944 if (empty_coap_ack) {
vithyat 0:977e87915078 2945 memset(empty_coap_ack, 0, sizeof(sn_coap_hdr_s));
vithyat 0:977e87915078 2946 empty_coap_ack->msg_code = COAP_MSG_CODE_EMPTY;
vithyat 0:977e87915078 2947 empty_coap_ack->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
vithyat 0:977e87915078 2948 empty_coap_ack->msg_id = header->msg_id;
vithyat 0:977e87915078 2949 sn_nsdl_send_coap_message(_nsdl_handle, address, empty_coap_ack);
vithyat 0:977e87915078 2950 memory_free(empty_coap_ack);
vithyat 0:977e87915078 2951 }
vithyat 0:977e87915078 2952 }
vithyat 0:977e87915078 2953
vithyat 0:977e87915078 2954 void M2MNsdlInterface::store_bs_finished_response_id(uint16_t msg_id)
vithyat 0:977e87915078 2955 {
vithyat 0:977e87915078 2956 tr_debug("M2MNsdlInterface::store_bs_finished_response_id - id %d", msg_id);
vithyat 0:977e87915078 2957 _bootstrap_id = msg_id;
vithyat 0:977e87915078 2958 }
vithyat 0:977e87915078 2959
vithyat 0:977e87915078 2960 struct M2MNsdlInterface::nsdl_coap_data_s* M2MNsdlInterface::create_coap_event_data(
vithyat 0:977e87915078 2961 sn_coap_hdr_s *received_coap_header,
vithyat 0:977e87915078 2962 sn_nsdl_addr_s *address,
vithyat 0:977e87915078 2963 struct nsdl_s *nsdl_handle,
vithyat 0:977e87915078 2964 uint8_t coap_msg_code)
vithyat 0:977e87915078 2965 {
vithyat 0:977e87915078 2966 nsdl_coap_data_s *nsdl_coap_data = (nsdl_coap_data_s*)memory_alloc(sizeof(nsdl_coap_data_s));
vithyat 0:977e87915078 2967
vithyat 0:977e87915078 2968 if (nsdl_coap_data) {
vithyat 0:977e87915078 2969 nsdl_coap_data->nsdl_handle = nsdl_handle;
vithyat 0:977e87915078 2970 nsdl_coap_data->address.addr_len = address->addr_len;
vithyat 0:977e87915078 2971 nsdl_coap_data->address.type = address->type;
vithyat 0:977e87915078 2972 nsdl_coap_data->address.port = address->port;
vithyat 0:977e87915078 2973
vithyat 0:977e87915078 2974 // Needs to copy all the dynamic data since it resides on stack and this wil turn into an event based call.
vithyat 0:977e87915078 2975 nsdl_coap_data->address.addr_ptr = (uint8_t*) memory_alloc(address->addr_len);
vithyat 0:977e87915078 2976
vithyat 0:977e87915078 2977 if (nsdl_coap_data->address.addr_ptr) {
vithyat 0:977e87915078 2978 memcpy(nsdl_coap_data->address.addr_ptr, address->addr_ptr, address->addr_len);
vithyat 0:977e87915078 2979 nsdl_coap_data->received_coap_header = received_coap_header;
vithyat 0:977e87915078 2980 nsdl_coap_data->received_coap_header->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
vithyat 0:977e87915078 2981 nsdl_coap_data->received_coap_header->msg_code = (sn_coap_msg_code_e)coap_msg_code;
vithyat 0:977e87915078 2982
vithyat 0:977e87915078 2983 // Copy payload
vithyat 0:977e87915078 2984 if ((received_coap_header->payload_len > 0) &&
vithyat 0:977e87915078 2985 (received_coap_header->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED)) {
vithyat 0:977e87915078 2986 assert(received_coap_header->payload_ptr);
vithyat 0:977e87915078 2987
vithyat 0:977e87915078 2988 uint8_t *temp_ptr = (uint8_t*) memory_alloc(received_coap_header->payload_len);
vithyat 0:977e87915078 2989 if (temp_ptr) {
vithyat 0:977e87915078 2990 memcpy(temp_ptr, received_coap_header->payload_ptr, received_coap_header->payload_len);
vithyat 0:977e87915078 2991 nsdl_coap_data->received_coap_header->payload_ptr = temp_ptr;
vithyat 0:977e87915078 2992 nsdl_coap_data->received_coap_header->payload_len = received_coap_header->payload_len;
vithyat 0:977e87915078 2993 } else {
vithyat 0:977e87915078 2994 memory_free(nsdl_coap_data->received_coap_header->payload_ptr);
vithyat 0:977e87915078 2995 sn_coap_parser_release_allocated_coap_msg_mem(nsdl_handle->grs->coap, nsdl_coap_data->received_coap_header);
vithyat 0:977e87915078 2996 memory_free(nsdl_coap_data->address.addr_ptr);
vithyat 0:977e87915078 2997 memory_free(nsdl_coap_data);
vithyat 0:977e87915078 2998 return NULL;
vithyat 0:977e87915078 2999 }
vithyat 0:977e87915078 3000 }
vithyat 0:977e87915078 3001 } else {
vithyat 0:977e87915078 3002 memory_free(nsdl_coap_data);
vithyat 0:977e87915078 3003 return NULL;
vithyat 0:977e87915078 3004 }
vithyat 0:977e87915078 3005 } else {
vithyat 0:977e87915078 3006 return NULL;
vithyat 0:977e87915078 3007 }
vithyat 0:977e87915078 3008
vithyat 0:977e87915078 3009 return nsdl_coap_data;
vithyat 0:977e87915078 3010 }
vithyat 0:977e87915078 3011
vithyat 0:977e87915078 3012 void M2MNsdlInterface::set_registration_status(bool registered)
vithyat 0:977e87915078 3013 {
vithyat 0:977e87915078 3014 _registered = registered;
vithyat 0:977e87915078 3015
vithyat 0:977e87915078 3016 // Unblock CoAP ping sending by removing ping request from the list.
vithyat 0:977e87915078 3017 if (!registered) {
vithyat 0:977e87915078 3018 remove_ping_from_response_list();
vithyat 0:977e87915078 3019 }
vithyat 0:977e87915078 3020 }
vithyat 0:977e87915078 3021
vithyat 0:977e87915078 3022 void M2MNsdlInterface::handle_register_response(const sn_coap_hdr_s *coap_header)
vithyat 0:977e87915078 3023 {
vithyat 0:977e87915078 3024 if (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CREATED) {
vithyat 0:977e87915078 3025 tr_info("M2MNsdlInterface::handle_register_response - registered");
vithyat 0:977e87915078 3026 // If lifetime is less than zero then leave the field empty
vithyat 0:977e87915078 3027 if (coap_header->options_list_ptr) {
vithyat 0:977e87915078 3028 uint32_t max_time = coap_header->options_list_ptr->max_age;
vithyat 0:977e87915078 3029
vithyat 0:977e87915078 3030 // If a sufficiently-large Max-Age option is present, we interpret it as registration lifetime;
vithyat 0:977e87915078 3031 // mbed server (mDS) reports lifetime this way as a non-standard extension. Other servers
vithyat 0:977e87915078 3032 // would likely not include an explicit Max-Age option, in which case we'd see the default 60 seconds.
vithyat 0:977e87915078 3033 if (max_time >= MINIMUM_REGISTRATION_TIME) {
vithyat 0:977e87915078 3034 set_endpoint_lifetime_buffer(max_time);
vithyat 0:977e87915078 3035 }
vithyat 0:977e87915078 3036 if (coap_header->options_list_ptr->location_path_ptr) {
vithyat 0:977e87915078 3037 sn_nsdl_set_endpoint_location(_nsdl_handle,
vithyat 0:977e87915078 3038 coap_header->options_list_ptr->location_path_ptr,
vithyat 0:977e87915078 3039 coap_header->options_list_ptr->location_path_len);
vithyat 0:977e87915078 3040 }
vithyat 0:977e87915078 3041
vithyat 0:977e87915078 3042 }
vithyat 0:977e87915078 3043 if (_endpoint->lifetime_ptr) {
vithyat 0:977e87915078 3044 _registration_timer.stop_timer();
vithyat 0:977e87915078 3045 _registration_timer.start_timer(registration_time() * 1000,
vithyat 0:977e87915078 3046 M2MTimerObserver::Registration,
vithyat 0:977e87915078 3047 false);
vithyat 0:977e87915078 3048 }
vithyat 0:977e87915078 3049
vithyat 0:977e87915078 3050 _observer.client_registered(_server);
vithyat 0:977e87915078 3051
vithyat 0:977e87915078 3052 _notification_send_ongoing = false;
vithyat 0:977e87915078 3053
vithyat 0:977e87915078 3054 // Check if there are any pending download requests
vithyat 0:977e87915078 3055 send_pending_request();
vithyat 0:977e87915078 3056
vithyat 0:977e87915078 3057 } else {
vithyat 0:977e87915078 3058 tr_error("M2MNsdlInterface::handle_register_response - registration error %d", coap_header->msg_code);
vithyat 0:977e87915078 3059 if (coap_header->coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED ||
vithyat 0:977e87915078 3060 coap_header->coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED) {
vithyat 0:977e87915078 3061 tr_error("M2MNsdlInterface::handle_register_response - message sending failed !!!!");
vithyat 0:977e87915078 3062 }
vithyat 0:977e87915078 3063
vithyat 0:977e87915078 3064 if (COAP_MSG_CODE_RESPONSE_BAD_REQUEST == coap_header->msg_code ||
vithyat 0:977e87915078 3065 COAP_MSG_CODE_RESPONSE_FORBIDDEN == coap_header->msg_code) {
vithyat 0:977e87915078 3066 _observer.registration_error(M2MInterface::InvalidParameters, false);
vithyat 0:977e87915078 3067 } else {
vithyat 0:977e87915078 3068 // Try to do clean register again
vithyat 0:977e87915078 3069 _observer.registration_error(M2MInterface::NetworkError, true, true);
vithyat 0:977e87915078 3070 }
vithyat 0:977e87915078 3071 }
vithyat 0:977e87915078 3072 }
vithyat 0:977e87915078 3073
vithyat 0:977e87915078 3074 void M2MNsdlInterface::handle_unregister_response(const sn_coap_hdr_s *coap_header)
vithyat 0:977e87915078 3075 {
vithyat 0:977e87915078 3076 tr_info("M2MNsdlInterface::handle_unregister_response - unregistered");
vithyat 0:977e87915078 3077
vithyat 0:977e87915078 3078 // Clear out the ongoing requests and call error callback with status ERROR_NOT_REGISTERED
vithyat 0:977e87915078 3079 free_request_context_list(NULL, true, ERROR_NOT_REGISTERED);
vithyat 0:977e87915078 3080
vithyat 0:977e87915078 3081 if(coap_header->msg_code == COAP_MSG_CODE_RESPONSE_DELETED) {
vithyat 0:977e87915078 3082 _registration_timer.stop_timer();
vithyat 0:977e87915078 3083 _observer.client_unregistered();
vithyat 0:977e87915078 3084 } else {
vithyat 0:977e87915078 3085 tr_error("M2MNsdlInterface::handle_unregister_response - unregistration error %d", coap_header->msg_code);
vithyat 0:977e87915078 3086 M2MInterface::Error error = M2MInterface::UnregistrationFailed;
vithyat 0:977e87915078 3087 if (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_NOT_FOUND) {
vithyat 0:977e87915078 3088 _observer.registration_error(error, false);
vithyat 0:977e87915078 3089 } else {
vithyat 0:977e87915078 3090 _observer.registration_error(error, true);
vithyat 0:977e87915078 3091 }
vithyat 0:977e87915078 3092 }
vithyat 0:977e87915078 3093 }
vithyat 0:977e87915078 3094
vithyat 0:977e87915078 3095 void M2MNsdlInterface::handle_register_update_response(const sn_coap_hdr_s *coap_header)
vithyat 0:977e87915078 3096 {
vithyat 0:977e87915078 3097 if (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_CHANGED) {
vithyat 0:977e87915078 3098 tr_info("M2MNsdlInterface::handle_register_update_response - registration_updated");
vithyat 0:977e87915078 3099 _observer.registration_updated(*_server);
vithyat 0:977e87915078 3100
vithyat 0:977e87915078 3101 _notification_send_ongoing = false;
vithyat 0:977e87915078 3102 // Check if there are any pending notifications in queue
vithyat 0:977e87915078 3103 _notification_handler->send_notification(this);
vithyat 0:977e87915078 3104
vithyat 0:977e87915078 3105 // Check if there are any pending download requests
vithyat 0:977e87915078 3106 send_pending_request();
vithyat 0:977e87915078 3107
vithyat 0:977e87915078 3108 } else {
vithyat 0:977e87915078 3109 tr_error("M2MNsdlInterface::handle_register_update_response - registration_updated failed %d, %d", coap_header->msg_code, coap_header->coap_status);
vithyat 0:977e87915078 3110 _nsdl_handle->update_register_token = 0;
vithyat 0:977e87915078 3111 _registration_timer.stop_timer();
vithyat 0:977e87915078 3112
vithyat 0:977e87915078 3113 if (coap_header->coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED ||
vithyat 0:977e87915078 3114 coap_header->coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED) {
vithyat 0:977e87915078 3115 // Inform interfaceimpl to do a reconnection and registration update
vithyat 0:977e87915078 3116 // till we get CoAP level response for the request
vithyat 0:977e87915078 3117 _observer.registration_error(M2MInterface::NetworkError, true);
vithyat 0:977e87915078 3118 } else {
vithyat 0:977e87915078 3119 // Do a full registration
vithyat 0:977e87915078 3120 bool msg_sent = false;
vithyat 0:977e87915078 3121 if (_server_address) {
vithyat 0:977e87915078 3122 msg_sent = parse_and_send_uri_query_parameters();
vithyat 0:977e87915078 3123 }
vithyat 0:977e87915078 3124 if (!msg_sent) {
vithyat 0:977e87915078 3125 sn_nsdl_register_endpoint(_nsdl_handle, _endpoint, NULL);
vithyat 0:977e87915078 3126 }
vithyat 0:977e87915078 3127 }
vithyat 0:977e87915078 3128 }
vithyat 0:977e87915078 3129 }
vithyat 0:977e87915078 3130
vithyat 0:977e87915078 3131 void M2MNsdlInterface::handle_request_response(const sn_coap_hdr_s *coap_header,
vithyat 0:977e87915078 3132 request_context_s *request_context)
vithyat 0:977e87915078 3133 {
vithyat 0:977e87915078 3134 tr_info("M2MNsdlInterface::handle_request_response");
vithyat 0:977e87915078 3135 size_t total_size = 0;
vithyat 0:977e87915078 3136
vithyat 0:977e87915078 3137 if (coap_header->options_list_ptr) {
vithyat 0:977e87915078 3138 if (coap_header->options_list_ptr->use_size2) {
vithyat 0:977e87915078 3139 total_size = coap_header->options_list_ptr->size2;
vithyat 0:977e87915078 3140 }
vithyat 0:977e87915078 3141 } else {
vithyat 0:977e87915078 3142 total_size = coap_header->payload_len;
vithyat 0:977e87915078 3143 }
vithyat 0:977e87915078 3144
vithyat 0:977e87915078 3145 if (coap_header->msg_code >= COAP_MSG_CODE_RESPONSE_CREATED &&
vithyat 0:977e87915078 3146 coap_header->msg_code <= COAP_MSG_CODE_RESPONSE_CONTENT) {
vithyat 0:977e87915078 3147
vithyat 0:977e87915078 3148 // Reset retry timer for next GET request
vithyat 0:977e87915078 3149 _download_retry_time = 0;
vithyat 0:977e87915078 3150
vithyat 0:977e87915078 3151 // Take copy of uri_path in case of sync mode
vithyat 0:977e87915078 3152 // Pointer is freed already by "free_request_context_list" and then used again in send_request() call
vithyat 0:977e87915078 3153 char *temp = NULL;
vithyat 0:977e87915078 3154 if (!request_context->async_req) {
vithyat 0:977e87915078 3155 temp = (char*)alloc_string_copy((uint8_t*)request_context->uri_path, strlen(request_context->uri_path));
vithyat 0:977e87915078 3156 if (temp == NULL) {
vithyat 0:977e87915078 3157 free_request_context_list(coap_header, true, FAILED_TO_ALLOCATE_MEMORY);
vithyat 0:977e87915078 3158 return;
vithyat 0:977e87915078 3159 }
vithyat 0:977e87915078 3160 }
vithyat 0:977e87915078 3161
vithyat 0:977e87915078 3162 // TODO: clean this up, could we keep request_context in the list a bit longer
vithyat 0:977e87915078 3163 // or pass the existing one to send_request rather than copying?
vithyat 0:977e87915078 3164 size_t rcv_size = request_context->received_size + coap_header->payload_len;
vithyat 0:977e87915078 3165 request_data_cb data_cb = request_context->on_request_data_cb;
vithyat 0:977e87915078 3166 request_error_cb error_cb = request_context->on_request_error_cb;
vithyat 0:977e87915078 3167 void *ctx = request_context->context;
vithyat 0:977e87915078 3168 bool async = request_context->async_req;
vithyat 0:977e87915078 3169 sn_coap_msg_code_e msg_code = request_context->msg_code;
vithyat 0:977e87915078 3170 uint32_t token = request_context->msg_token;
vithyat 0:977e87915078 3171 DownloadType download_type = request_context->download_type;
vithyat 0:977e87915078 3172
vithyat 0:977e87915078 3173 // Remove the request before calling the "on_request_data_cb" callback
vithyat 0:977e87915078 3174 free_request_context_list(coap_header, false);
vithyat 0:977e87915078 3175
vithyat 0:977e87915078 3176 bool last_block = true;
vithyat 0:977e87915078 3177 if (coap_header->options_list_ptr &&
vithyat 0:977e87915078 3178 coap_header->options_list_ptr->block2 != -1 &&
vithyat 0:977e87915078 3179 coap_header->options_list_ptr->block2 & 0x08) {
vithyat 0:977e87915078 3180 // Not last block if block2 is set (blockwised transfer) and more bit is set
vithyat 0:977e87915078 3181 last_block = false;
vithyat 0:977e87915078 3182 }
vithyat 0:977e87915078 3183
vithyat 0:977e87915078 3184 data_cb(coap_header->payload_ptr,
vithyat 0:977e87915078 3185 coap_header->payload_len,
vithyat 0:977e87915078 3186 total_size,
vithyat 0:977e87915078 3187 last_block,
vithyat 0:977e87915078 3188 ctx);
vithyat 0:977e87915078 3189
vithyat 0:977e87915078 3190 // In sync mode, call next request automatically until all blocks have been received
vithyat 0:977e87915078 3191 if (!async) {
vithyat 0:977e87915078 3192 if (!last_block) {
vithyat 0:977e87915078 3193 // Note that payload will be empty here as it should have already been sent
vithyat 0:977e87915078 3194 // when the initial request was sent!
vithyat 0:977e87915078 3195 send_request(download_type, temp, msg_code, rcv_size, async, token, 0, NULL, data_cb, error_cb, ctx);
vithyat 0:977e87915078 3196 } else {
vithyat 0:977e87915078 3197 tr_info("M2MNsdlInterface::handle_request_response - all blocks received");
vithyat 0:977e87915078 3198 }
vithyat 0:977e87915078 3199
vithyat 0:977e87915078 3200 memory_free(temp);
vithyat 0:977e87915078 3201 }
vithyat 0:977e87915078 3202
vithyat 0:977e87915078 3203 } else {
vithyat 0:977e87915078 3204 // Retransmission completed
vithyat 0:977e87915078 3205 if (coap_header->coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED ||
vithyat 0:977e87915078 3206 coap_header->coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED) {
vithyat 0:977e87915078 3207 _observer.registration_error(M2MInterface::NetworkError, true);
vithyat 0:977e87915078 3208
vithyat 0:977e87915078 3209 // Start retry logic, only for file download operation
vithyat 0:977e87915078 3210 } else if (coap_header->msg_code == COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE &&
vithyat 0:977e87915078 3211 request_context->msg_code == COAP_MSG_CODE_REQUEST_GET) {
vithyat 0:977e87915078 3212 bool retry = true;
vithyat 0:977e87915078 3213
vithyat 0:977e87915078 3214 if (!_download_retry_time) {
vithyat 0:977e87915078 3215 // Range is from 1 sec to 10 sec
vithyat 0:977e87915078 3216 _download_retry_time = randLIB_get_random_in_range(1, 10);
vithyat 0:977e87915078 3217 } else {
vithyat 0:977e87915078 3218 _download_retry_time *= RECONNECT_INCREMENT_FACTOR;
vithyat 0:977e87915078 3219 if (_download_retry_time > MAX_RECONNECT_TIMEOUT) {
vithyat 0:977e87915078 3220 tr_error("M2MNsdlInterface::handle_request_response - file download failed, retry completed");
vithyat 0:977e87915078 3221 retry = false;
vithyat 0:977e87915078 3222 failed_to_send_request(request_context, coap_header);
vithyat 0:977e87915078 3223 }
vithyat 0:977e87915078 3224 }
vithyat 0:977e87915078 3225
vithyat 0:977e87915078 3226 if (retry) {
vithyat 0:977e87915078 3227 tr_info("M2MNsdlInterface::handle_request_response - continue file download after %" PRIu64, _download_retry_time);
vithyat 0:977e87915078 3228 set_request_context_to_be_resend(coap_header->token_ptr, coap_header->token_len);
vithyat 0:977e87915078 3229 _download_retry_timer.start_timer(_download_retry_time * 1000, M2MTimerObserver::RetryTimer);
vithyat 0:977e87915078 3230 }
vithyat 0:977e87915078 3231
vithyat 0:977e87915078 3232 // Message sending has failed, inform application
vithyat 0:977e87915078 3233 } else {
vithyat 0:977e87915078 3234 failed_to_send_request(request_context, coap_header);
vithyat 0:977e87915078 3235 }
vithyat 0:977e87915078 3236 }
vithyat 0:977e87915078 3237 }
vithyat 0:977e87915078 3238
vithyat 0:977e87915078 3239 void M2MNsdlInterface::handle_bootstrap_response(const sn_coap_hdr_s *coap_header)
vithyat 0:977e87915078 3240 {
vithyat 0:977e87915078 3241 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 3242 tr_info("M2MNsdlInterface::handle_bootstrap_response");
vithyat 0:977e87915078 3243 _bootstrap_id = 0;
vithyat 0:977e87915078 3244 M2MInterface::Error error_code = interface_error(*coap_header);
vithyat 0:977e87915078 3245 if (error_code != M2MInterface::ErrorNone) {
vithyat 0:977e87915078 3246
vithyat 0:977e87915078 3247 #ifdef DISABLE_ERROR_DESCRIPTION
vithyat 0:977e87915078 3248 // this ifdef is saving +800B on ARMCC as it gets rid of the COAP_ERROR_* -strings from binary
vithyat 0:977e87915078 3249 const char *buffer = "";
vithyat 0:977e87915078 3250 #else
vithyat 0:977e87915078 3251 char buffer[MAX_ALLOWED_ERROR_STRING_LENGTH];
vithyat 0:977e87915078 3252 const char* error = coap_error(*coap_header);
vithyat 0:977e87915078 3253 snprintf(buffer, sizeof(buffer), "%s:%.*s", error, coap_header->payload_len, coap_header->payload_ptr);
vithyat 0:977e87915078 3254 #endif
vithyat 0:977e87915078 3255 handle_bootstrap_error(buffer, false);
vithyat 0:977e87915078 3256 } else {
vithyat 0:977e87915078 3257 _identity_accepted = true;
vithyat 0:977e87915078 3258 }
vithyat 0:977e87915078 3259 #else
vithyat 0:977e87915078 3260 (void)coap_header;
vithyat 0:977e87915078 3261 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
vithyat 0:977e87915078 3262 }
vithyat 0:977e87915078 3263
vithyat 0:977e87915078 3264 bool M2MNsdlInterface::handle_post_response(sn_coap_hdr_s* coap_header,
vithyat 0:977e87915078 3265 sn_nsdl_addr_s* address,
vithyat 0:977e87915078 3266 sn_coap_hdr_s *&coap_response,
vithyat 0:977e87915078 3267 M2MObjectInstance *&obj_instance,
vithyat 0:977e87915078 3268 bool is_bootstrap_msg)
vithyat 0:977e87915078 3269 {
vithyat 0:977e87915078 3270 bool execute_value_updated = false;
vithyat 0:977e87915078 3271
vithyat 0:977e87915078 3272 if (is_bootstrap_msg) {
vithyat 0:977e87915078 3273 handle_bootstrap_finished(coap_header, address);
vithyat 0:977e87915078 3274 } else if (coap_header->uri_path_ptr) {
vithyat 0:977e87915078 3275
vithyat 0:977e87915078 3276 String resource_name = coap_to_string(coap_header->uri_path_ptr,
vithyat 0:977e87915078 3277 coap_header->uri_path_len);
vithyat 0:977e87915078 3278
vithyat 0:977e87915078 3279 String object_name;
vithyat 0:977e87915078 3280 int slash_found = resource_name.find_last_of('/');
vithyat 0:977e87915078 3281 //The POST operation here is only allowed for non-existing object instances
vithyat 0:977e87915078 3282 if (slash_found != -1) {
vithyat 0:977e87915078 3283 object_name = resource_name.substr(0,slash_found);
vithyat 0:977e87915078 3284 if (object_name.find_last_of('/') != -1){
vithyat 0:977e87915078 3285 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 3286 coap_header,
vithyat 0:977e87915078 3287 COAP_MSG_CODE_RESPONSE_NOT_FOUND);
vithyat 0:977e87915078 3288 } else {
vithyat 0:977e87915078 3289 int32_t instance_id = atoi(resource_name.substr(slash_found+1,
vithyat 0:977e87915078 3290 resource_name.size()-object_name.size()).c_str());
vithyat 0:977e87915078 3291 M2MBase* base = find_resource(object_name);
vithyat 0:977e87915078 3292 if(base) {
vithyat 0:977e87915078 3293 if((instance_id >= 0) && (instance_id < UINT16_MAX)) {
vithyat 0:977e87915078 3294 if(coap_header->payload_ptr) {
vithyat 0:977e87915078 3295 M2MObject* object = static_cast<M2MObject*> (base);
vithyat 0:977e87915078 3296 obj_instance = object->create_object_instance(instance_id);
vithyat 0:977e87915078 3297 if(obj_instance) {
vithyat 0:977e87915078 3298 obj_instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
vithyat 0:977e87915078 3299 coap_response = obj_instance->handle_post_request(_nsdl_handle,
vithyat 0:977e87915078 3300 coap_header,
vithyat 0:977e87915078 3301 this,
vithyat 0:977e87915078 3302 execute_value_updated);
vithyat 0:977e87915078 3303 }
vithyat 0:977e87915078 3304 if (coap_response && coap_response->msg_code != COAP_MSG_CODE_RESPONSE_CREATED) {
vithyat 0:977e87915078 3305 //Invalid request so remove created ObjectInstance
vithyat 0:977e87915078 3306 object->remove_object_instance(instance_id);
vithyat 0:977e87915078 3307 } else {
vithyat 0:977e87915078 3308 tr_debug("M2MNsdlInterface::handle_post_response - Send Update registration for Create");
vithyat 0:977e87915078 3309 if (!send_update_registration()) {
vithyat 0:977e87915078 3310 // Most likely case would be memory allocation failure
vithyat 0:977e87915078 3311 _observer.registration_error(M2MInterface::MemoryFail, false);
vithyat 0:977e87915078 3312 }
vithyat 0:977e87915078 3313 }
vithyat 0:977e87915078 3314 } else {
vithyat 0:977e87915078 3315 tr_error("M2MNsdlInterface::handle_post_response - Missing Payload - Cannot create");
vithyat 0:977e87915078 3316 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 3317 coap_header,
vithyat 0:977e87915078 3318 COAP_MSG_CODE_RESPONSE_BAD_REQUEST);
vithyat 0:977e87915078 3319 }
vithyat 0:977e87915078 3320 } else { // instance id out of range
vithyat 0:977e87915078 3321 tr_error("M2MNsdlInterface::handle_post_response - instance id out of range - Cannot create");
vithyat 0:977e87915078 3322 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 3323 coap_header,
vithyat 0:977e87915078 3324 COAP_MSG_CODE_RESPONSE_BAD_REQUEST);
vithyat 0:977e87915078 3325 }
vithyat 0:977e87915078 3326 } else { // if(base)
vithyat 0:977e87915078 3327 tr_error("M2MNsdlInterface::handle_post_response - Missing BASE - Cannot create");
vithyat 0:977e87915078 3328 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 3329 coap_header,
vithyat 0:977e87915078 3330 COAP_MSG_CODE_RESPONSE_NOT_FOUND);
vithyat 0:977e87915078 3331 }
vithyat 0:977e87915078 3332 }
vithyat 0:977e87915078 3333 } else { // if(slash_found != -1)
vithyat 0:977e87915078 3334 tr_error("M2MNsdlInterface::handle_post_response - slash_found - Cannot create");
vithyat 0:977e87915078 3335 coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 3336 coap_header,
vithyat 0:977e87915078 3337 COAP_MSG_CODE_RESPONSE_NOT_FOUND);
vithyat 0:977e87915078 3338 }
vithyat 0:977e87915078 3339
vithyat 0:977e87915078 3340 }
vithyat 0:977e87915078 3341 return execute_value_updated;
vithyat 0:977e87915078 3342 }
vithyat 0:977e87915078 3343
vithyat 0:977e87915078 3344 void M2MNsdlInterface::handle_empty_ack(const sn_coap_hdr_s *coap_header, bool is_bootstrap_msg)
vithyat 0:977e87915078 3345 {
vithyat 0:977e87915078 3346 // Handle reset message
vithyat 0:977e87915078 3347 if (COAP_MSG_TYPE_RESET == coap_header->msg_type) {
vithyat 0:977e87915078 3348 coap_response_s *resp = find_response(coap_header->msg_id);
vithyat 0:977e87915078 3349 if (resp) {
vithyat 0:977e87915078 3350 if (resp->type == M2MBase::PING) {
vithyat 0:977e87915078 3351 remove_item_from_response_list(resp->uri_path, coap_header->msg_id);
vithyat 0:977e87915078 3352 } else {
vithyat 0:977e87915078 3353 M2MBase *base = find_resource(resp->uri_path);
vithyat 0:977e87915078 3354 if (base) {
vithyat 0:977e87915078 3355 if (resp->type == M2MBase::NOTIFICATION) {
vithyat 0:977e87915078 3356 M2MBase::BaseType type = base->base_type();
vithyat 0:977e87915078 3357 switch (type) {
vithyat 0:977e87915078 3358 case M2MBase::Object:
vithyat 0:977e87915078 3359 base->remove_observation_level(M2MBase::O_Attribute);
vithyat 0:977e87915078 3360 break;
vithyat 0:977e87915078 3361 case M2MBase::Resource:
vithyat 0:977e87915078 3362 base->remove_observation_level(M2MBase::R_Attribute);
vithyat 0:977e87915078 3363 break;
vithyat 0:977e87915078 3364 case M2MBase::ObjectInstance:
vithyat 0:977e87915078 3365 base->remove_observation_level(M2MBase::OI_Attribute);
vithyat 0:977e87915078 3366 break;
vithyat 0:977e87915078 3367 default:
vithyat 0:977e87915078 3368 break;
vithyat 0:977e87915078 3369 }
vithyat 0:977e87915078 3370 base->set_under_observation(false, this);
vithyat 0:977e87915078 3371 _notification_send_ongoing = false;
vithyat 0:977e87915078 3372 base->send_notification_delivery_status(*base, NOTIFICATION_STATUS_UNSUBSCRIBED);
vithyat 0:977e87915078 3373 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_UNSUBSCRIBED, M2MBase::NOTIFICATION);
vithyat 0:977e87915078 3374 _notification_handler->send_notification(this);
vithyat 0:977e87915078 3375 } else if (resp->type == M2MBase::DELAYED_POST_RESPONSE) {
vithyat 0:977e87915078 3376 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_REJECTED, M2MBase::DELAYED_POST_RESPONSE);
vithyat 0:977e87915078 3377 }
vithyat 0:977e87915078 3378 #ifdef ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 3379 else if (resp->type == M2MBase::DELAYED_RESPONSE) {
vithyat 0:977e87915078 3380 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_REJECTED, M2MBase::DELAYED_RESPONSE);
vithyat 0:977e87915078 3381 }
vithyat 0:977e87915078 3382 #endif // ENABLE_ASYNC_REST_RESPONSE
vithyat 0:977e87915078 3383 remove_item_from_response_list(resp->uri_path, coap_header->msg_id);
vithyat 0:977e87915078 3384 }
vithyat 0:977e87915078 3385 }
vithyat 0:977e87915078 3386 }
vithyat 0:977e87915078 3387 } else if (is_bootstrap_msg) {
vithyat 0:977e87915078 3388 if (!_bootstrap_finish_ack_received) {
vithyat 0:977e87915078 3389 // The _bootstrap_finish_ack_received flag is used to avoid sending the finish event
vithyat 0:977e87915078 3390 // twice incase we get the same ack before the event loop has handled the event.
vithyat 0:977e87915078 3391 _observer.bootstrap_wait();
vithyat 0:977e87915078 3392 if (coap_header->coap_status == COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED ||
vithyat 0:977e87915078 3393 coap_header->coap_status == COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED) {
vithyat 0:977e87915078 3394 handle_bootstrap_error(ERROR_REASON_28, false);
vithyat 0:977e87915078 3395 } else {
vithyat 0:977e87915078 3396 tr_debug("M2MNsdlInterface::handle_empty_ack - sending finish event - status %d", coap_header->coap_status);
vithyat 0:977e87915078 3397 _event.data.event_type = MBED_CLIENT_NSDLINTERFACE_BS_FINISH_EVENT;
vithyat 0:977e87915078 3398 _event.data.event_data = coap_header->msg_id;
vithyat 0:977e87915078 3399 _event.data.data_ptr = _nsdl_handle;
vithyat 0:977e87915078 3400 _bootstrap_finish_ack_received = true;
vithyat 0:977e87915078 3401 eventOS_event_send_user_allocated(&_event);
vithyat 0:977e87915078 3402 }
vithyat 0:977e87915078 3403 }
vithyat 0:977e87915078 3404 else {
vithyat 0:977e87915078 3405 tr_debug("M2MNsdlInterface::handle_empty_ack - finish event already in progress");
vithyat 0:977e87915078 3406 }
vithyat 0:977e87915078 3407 } else {
vithyat 0:977e87915078 3408 coap_response_s *data = find_response(coap_header->msg_id);
vithyat 0:977e87915078 3409 if (data) {
vithyat 0:977e87915078 3410 M2MBase *base = find_resource(data->uri_path);
vithyat 0:977e87915078 3411 if (base) {
vithyat 0:977e87915078 3412 bool report = true;
vithyat 0:977e87915078 3413 if (data->type == M2MBase::NOTIFICATION) {
vithyat 0:977e87915078 3414 if (base->report_handler()->blockwise_notify()) {
vithyat 0:977e87915078 3415 report = false;
vithyat 0:977e87915078 3416 }
vithyat 0:977e87915078 3417 }
vithyat 0:977e87915078 3418
vithyat 0:977e87915078 3419 if (report) {
vithyat 0:977e87915078 3420 if (!data->blockwise_used) {
vithyat 0:977e87915078 3421 handle_message_delivered(base, data->type);
vithyat 0:977e87915078 3422 remove_item_from_response_list(NULL, coap_header->msg_id);
vithyat 0:977e87915078 3423 }
vithyat 0:977e87915078 3424 }
vithyat 0:977e87915078 3425 }
vithyat 0:977e87915078 3426 }
vithyat 0:977e87915078 3427 }
vithyat 0:977e87915078 3428 }
vithyat 0:977e87915078 3429
vithyat 0:977e87915078 3430 void M2MNsdlInterface::handle_bootstrap_finish_ack(uint16_t msg_id)
vithyat 0:977e87915078 3431 {
vithyat 0:977e87915078 3432 // EMPTY ACK for BS finished
vithyat 0:977e87915078 3433 tr_debug("M2MNsdlInterface::handle_bootstrap_finish_ack - id: %d", msg_id);
vithyat 0:977e87915078 3434 if (_bootstrap_id == msg_id) {
vithyat 0:977e87915078 3435 _observer.bootstrap_finish();
vithyat 0:977e87915078 3436 _bootstrap_id = 0;
vithyat 0:977e87915078 3437 } else {
vithyat 0:977e87915078 3438 tr_error("M2MNsdlInterface::handle_empty_ack - empty ACK id does not match to BS finished response id!");
vithyat 0:977e87915078 3439 char buffer[MAX_ALLOWED_ERROR_STRING_LENGTH];
vithyat 0:977e87915078 3440 const char *desc = "message id does not match";
vithyat 0:977e87915078 3441 snprintf(buffer, sizeof(buffer), ERROR_REASON_22, desc);
vithyat 0:977e87915078 3442 handle_bootstrap_error(buffer, false);
vithyat 0:977e87915078 3443 }
vithyat 0:977e87915078 3444 }
vithyat 0:977e87915078 3445
vithyat 0:977e87915078 3446 void M2MNsdlInterface::handle_message_delivered(M2MBase *base, const M2MBase::MessageType type)
vithyat 0:977e87915078 3447 {
vithyat 0:977e87915078 3448 if (M2MBase::NOTIFICATION == type) {
vithyat 0:977e87915078 3449 base->report_handler()->set_notification_send_in_progress(false);
vithyat 0:977e87915078 3450 _notification_send_ongoing = false;
vithyat 0:977e87915078 3451 base->send_notification_delivery_status(*base, NOTIFICATION_STATUS_DELIVERED);
vithyat 0:977e87915078 3452
vithyat 0:977e87915078 3453 _notification_handler->send_notification(this);
vithyat 0:977e87915078 3454
vithyat 0:977e87915078 3455 // Supported only in Resource level
vithyat 0:977e87915078 3456 // TODO! remove below code once old API is removed
vithyat 0:977e87915078 3457 if (M2MBase::Resource == base->base_type()) {
vithyat 0:977e87915078 3458 M2MResource *resource = static_cast<M2MResource *> (base);
vithyat 0:977e87915078 3459 resource->notification_sent();
vithyat 0:977e87915078 3460 }
vithyat 0:977e87915078 3461 }
vithyat 0:977e87915078 3462
vithyat 0:977e87915078 3463 base->send_message_delivery_status(*base, M2MBase::MESSAGE_STATUS_DELIVERED, type);
vithyat 0:977e87915078 3464 }
vithyat 0:977e87915078 3465
vithyat 0:977e87915078 3466 void M2MNsdlInterface::set_retransmission_parameters()
vithyat 0:977e87915078 3467 {
vithyat 0:977e87915078 3468 // in UDP mode, reconnection attempts must be scaled down so that last attempt does not slip
vithyat 0:977e87915078 3469 // past the client lifetime.
vithyat 0:977e87915078 3470 uint64_t lifetime = registration_time();
vithyat 0:977e87915078 3471 uint32_t resend_count = MBED_CLIENT_RECONNECTION_COUNT;
vithyat 0:977e87915078 3472
vithyat 0:977e87915078 3473 uint32_t reconnection_total_time = total_retransmission_time(resend_count);
vithyat 0:977e87915078 3474 tr_debug("M2MNsdlInterface::set_retransmission_parameters() - total resend time %" PRIu32, reconnection_total_time);
vithyat 0:977e87915078 3475
vithyat 0:977e87915078 3476 while (resend_count > 1 && reconnection_total_time > lifetime) {
vithyat 0:977e87915078 3477 reconnection_total_time = total_retransmission_time(--resend_count);
vithyat 0:977e87915078 3478 }
vithyat 0:977e87915078 3479
vithyat 0:977e87915078 3480 tr_info("M2MNsdlInterface::set_retransmission_parameters() - setting max resend count to %" PRIu32 " with total time: %" PRIu32, resend_count, reconnection_total_time);
vithyat 0:977e87915078 3481 sn_nsdl_set_retransmission_parameters(_nsdl_handle, resend_count, MBED_CLIENT_RECONNECTION_INTERVAL);
vithyat 0:977e87915078 3482 }
vithyat 0:977e87915078 3483
vithyat 0:977e87915078 3484 uint32_t M2MNsdlInterface::total_retransmission_time(uint32_t resend_count)
vithyat 0:977e87915078 3485 {
vithyat 0:977e87915078 3486 uint32_t reconnection_total_time = 1;
vithyat 0:977e87915078 3487
vithyat 0:977e87915078 3488 for (uint32_t i = 0; i <= resend_count; i++) {
vithyat 0:977e87915078 3489 reconnection_total_time *= 2;
vithyat 0:977e87915078 3490 }
vithyat 0:977e87915078 3491
vithyat 0:977e87915078 3492 reconnection_total_time--;
vithyat 0:977e87915078 3493 reconnection_total_time *= MBED_CLIENT_RECONNECTION_INTERVAL;
vithyat 0:977e87915078 3494
vithyat 0:977e87915078 3495 // We need to take into account that CoAP specification mentions that each retransmission
vithyat 0:977e87915078 3496 // has to have a random multiplying factor between 1 - 1.5 , max of which can be 1.5
vithyat 0:977e87915078 3497 reconnection_total_time *= RESPONSE_RANDOM_FACTOR;
vithyat 0:977e87915078 3498
vithyat 0:977e87915078 3499 return reconnection_total_time;
vithyat 0:977e87915078 3500 }
vithyat 0:977e87915078 3501
vithyat 0:977e87915078 3502 bool M2MNsdlInterface::is_update_register_ongoing() const
vithyat 0:977e87915078 3503 {
vithyat 0:977e87915078 3504 return _nsdl_handle->update_register_token == 0 ? false : true;
vithyat 0:977e87915078 3505 }
vithyat 0:977e87915078 3506
vithyat 0:977e87915078 3507 uint8_t M2MNsdlInterface::get_resend_count()
vithyat 0:977e87915078 3508 {
vithyat 0:977e87915078 3509 return sn_nsdl_get_retransmission_count(_nsdl_handle);
vithyat 0:977e87915078 3510 }
vithyat 0:977e87915078 3511
vithyat 0:977e87915078 3512 void M2MNsdlInterface::send_pending_request()
vithyat 0:977e87915078 3513 {
vithyat 0:977e87915078 3514 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 3515 request_context_s *data = (request_context_s *)ns_list_get_first(&_request_context_list);
vithyat 0:977e87915078 3516 while (data) {
vithyat 0:977e87915078 3517 if (data->resend && data->msg_code == COAP_MSG_CODE_REQUEST_GET) {
vithyat 0:977e87915078 3518 send_request(data->download_type,
vithyat 0:977e87915078 3519 data->uri_path,
vithyat 0:977e87915078 3520 data->msg_code,
vithyat 0:977e87915078 3521 data->received_size,
vithyat 0:977e87915078 3522 data->async_req,
vithyat 0:977e87915078 3523 data->msg_token,
vithyat 0:977e87915078 3524 0,
vithyat 0:977e87915078 3525 NULL,
vithyat 0:977e87915078 3526 data->on_request_data_cb,
vithyat 0:977e87915078 3527 data->on_request_error_cb,
vithyat 0:977e87915078 3528 data->context);
vithyat 0:977e87915078 3529 }
vithyat 0:977e87915078 3530
vithyat 0:977e87915078 3531 data = (request_context_s *)ns_list_get_next(&_request_context_list, data);
vithyat 0:977e87915078 3532 }
vithyat 0:977e87915078 3533 }
vithyat 0:977e87915078 3534
vithyat 0:977e87915078 3535 void M2MNsdlInterface::free_response_list()
vithyat 0:977e87915078 3536 {
vithyat 0:977e87915078 3537 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 3538 while (!ns_list_is_empty(&_response_list)) {
vithyat 0:977e87915078 3539 coap_response_s* data = (coap_response_s*)ns_list_get_first(&_response_list);
vithyat 0:977e87915078 3540 ns_list_remove(&_response_list, data);
vithyat 0:977e87915078 3541 memory_free(data->uri_path);
vithyat 0:977e87915078 3542 memory_free(data);
vithyat 0:977e87915078 3543 }
vithyat 0:977e87915078 3544 }
vithyat 0:977e87915078 3545
vithyat 0:977e87915078 3546 void M2MNsdlInterface::remove_item_from_response_list(const char* uri_path, const int32_t msg_id)
vithyat 0:977e87915078 3547 {
vithyat 0:977e87915078 3548 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 3549 coap_response_s *data = (coap_response_s *)ns_list_get_first(&_response_list);
vithyat 0:977e87915078 3550 while (data) {
vithyat 0:977e87915078 3551 if (data->msg_id == msg_id) {
vithyat 0:977e87915078 3552 bool remove = true;
vithyat 0:977e87915078 3553 if (uri_path) {
vithyat 0:977e87915078 3554 remove = false;
vithyat 0:977e87915078 3555 if ((strcmp(uri_path, data->uri_path) == 0)) {
vithyat 0:977e87915078 3556 remove = true;
vithyat 0:977e87915078 3557 }
vithyat 0:977e87915078 3558 }
vithyat 0:977e87915078 3559 if (remove) {
vithyat 0:977e87915078 3560 ns_list_remove(&_response_list, data);
vithyat 0:977e87915078 3561 memory_free(data->uri_path);
vithyat 0:977e87915078 3562 memory_free(data);
vithyat 0:977e87915078 3563 return;
vithyat 0:977e87915078 3564 }
vithyat 0:977e87915078 3565 }
vithyat 0:977e87915078 3566 data = (coap_response_s *)ns_list_get_next(&_response_list, data);
vithyat 0:977e87915078 3567 }
vithyat 0:977e87915078 3568 }
vithyat 0:977e87915078 3569
vithyat 0:977e87915078 3570 #if !defined(DISABLE_DELAYED_RESPONSE) || defined(ENABLE_ASYNC_REST_RESPONSE)
vithyat 0:977e87915078 3571 void M2MNsdlInterface::remove_items_from_response_list_for_uri(const char* uri_path)
vithyat 0:977e87915078 3572 {
vithyat 0:977e87915078 3573 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 3574 coap_response_s *data = (coap_response_s *)ns_list_get_first(&_response_list);
vithyat 0:977e87915078 3575 while (data) {
vithyat 0:977e87915078 3576 bool remove = false;
vithyat 0:977e87915078 3577 if (uri_path) {
vithyat 0:977e87915078 3578 if ((strcmp(uri_path, data->uri_path) == 0)) {
vithyat 0:977e87915078 3579 remove = true;
vithyat 0:977e87915078 3580 }
vithyat 0:977e87915078 3581 }
vithyat 0:977e87915078 3582 coap_response_s *next = (coap_response_s *)ns_list_get_next(&_response_list, data);
vithyat 0:977e87915078 3583 if (remove) {
vithyat 0:977e87915078 3584 ns_list_remove(&_response_list, data);
vithyat 0:977e87915078 3585 memory_free(data->uri_path);
vithyat 0:977e87915078 3586 memory_free(data);
vithyat 0:977e87915078 3587 }
vithyat 0:977e87915078 3588 data = next;
vithyat 0:977e87915078 3589 }
vithyat 0:977e87915078 3590 }
vithyat 0:977e87915078 3591 #endif
vithyat 0:977e87915078 3592
vithyat 0:977e87915078 3593 void M2MNsdlInterface::store_to_response_list(const char *uri, int32_t msg_id, M2MBase::MessageType type)
vithyat 0:977e87915078 3594 {
vithyat 0:977e87915078 3595 coap_response_s *resp = (struct coap_response_s*)memory_alloc(sizeof(struct coap_response_s));
vithyat 0:977e87915078 3596 if (resp) {
vithyat 0:977e87915078 3597 resp->uri_path = NULL;
vithyat 0:977e87915078 3598 if (uri) {
vithyat 0:977e87915078 3599 resp->uri_path = M2MBase::alloc_string_copy(uri);
vithyat 0:977e87915078 3600 if (resp->uri_path == NULL) {
vithyat 0:977e87915078 3601 tr_error("M2MNsdlInterface::store_to_response_list - failed to allocate uri_path!");
vithyat 0:977e87915078 3602 memory_free(resp);
vithyat 0:977e87915078 3603 return;
vithyat 0:977e87915078 3604 }
vithyat 0:977e87915078 3605 }
vithyat 0:977e87915078 3606
vithyat 0:977e87915078 3607 resp->msg_id = msg_id;
vithyat 0:977e87915078 3608 resp->type = type;
vithyat 0:977e87915078 3609 resp->blockwise_used = false;
vithyat 0:977e87915078 3610 ns_list_add_to_end(&_response_list, resp);
vithyat 0:977e87915078 3611 } else {
vithyat 0:977e87915078 3612 tr_error("M2MNsdlInterface::store_to_response_list - failed to allocate coap_response_s!");
vithyat 0:977e87915078 3613 }
vithyat 0:977e87915078 3614 }
vithyat 0:977e87915078 3615
vithyat 0:977e87915078 3616 struct M2MNsdlInterface::coap_response_s* M2MNsdlInterface::find_response(int32_t msg_id)
vithyat 0:977e87915078 3617 {
vithyat 0:977e87915078 3618 coap_response_s *data = (coap_response_s *)ns_list_get_first(&_response_list);
vithyat 0:977e87915078 3619 while (data) {
vithyat 0:977e87915078 3620 if (data->msg_id == msg_id) {
vithyat 0:977e87915078 3621 return data;
vithyat 0:977e87915078 3622 }
vithyat 0:977e87915078 3623 data = (coap_response_s *)ns_list_get_next(&_response_list, data);
vithyat 0:977e87915078 3624 }
vithyat 0:977e87915078 3625
vithyat 0:977e87915078 3626 return NULL;
vithyat 0:977e87915078 3627 }
vithyat 0:977e87915078 3628
vithyat 0:977e87915078 3629 #if !defined(DISABLE_DELAYED_RESPONSE) || defined(ENABLE_ASYNC_REST_RESPONSE)
vithyat 0:977e87915078 3630 struct M2MNsdlInterface::coap_response_s* M2MNsdlInterface::find_delayed_response(const char* uri_path,
vithyat 0:977e87915078 3631 const M2MBase::MessageType type,
vithyat 0:977e87915078 3632 int32_t message_id)
vithyat 0:977e87915078 3633 {
vithyat 0:977e87915078 3634 coap_response_s *data = (coap_response_s *)ns_list_get_first(&_response_list);
vithyat 0:977e87915078 3635 while (data) {
vithyat 0:977e87915078 3636 if (data->uri_path &&
vithyat 0:977e87915078 3637 strcmp(data->uri_path, uri_path) == 0 &&
vithyat 0:977e87915078 3638 data->type == type &&
vithyat 0:977e87915078 3639 ((message_id == UNDEFINED_MSG_ID)) || (data->msg_id == message_id)) {
vithyat 0:977e87915078 3640 return data;
vithyat 0:977e87915078 3641 }
vithyat 0:977e87915078 3642 data = (coap_response_s *)ns_list_get_next(&_response_list, data);
vithyat 0:977e87915078 3643 }
vithyat 0:977e87915078 3644
vithyat 0:977e87915078 3645 return NULL;
vithyat 0:977e87915078 3646 }
vithyat 0:977e87915078 3647 #endif // DISABLE_DELAYED_RESPONSE
vithyat 0:977e87915078 3648
vithyat 0:977e87915078 3649 void M2MNsdlInterface::failed_to_send_request(request_context_s *request, const sn_coap_hdr_s *coap_header)
vithyat 0:977e87915078 3650 {
vithyat 0:977e87915078 3651 sn_nsdl_remove_msg_from_retransmission(_nsdl_handle,
vithyat 0:977e87915078 3652 (uint8_t*)&request->msg_token,
vithyat 0:977e87915078 3653 sizeof(request->msg_token));
vithyat 0:977e87915078 3654 free_request_context_list(coap_header, true, FAILED_TO_SEND_MSG);
vithyat 0:977e87915078 3655 }
vithyat 0:977e87915078 3656
vithyat 0:977e87915078 3657 bool M2MNsdlInterface::coap_ping_in_process() const
vithyat 0:977e87915078 3658 {
vithyat 0:977e87915078 3659 const coap_response_s *data = (coap_response_s *)ns_list_get_first(&_response_list);
vithyat 0:977e87915078 3660 while (data) {
vithyat 0:977e87915078 3661 if (data->type == M2MBase::PING) {
vithyat 0:977e87915078 3662 tr_info("M2MNsdlInterface::coap_ping_in_process() - already in process");
vithyat 0:977e87915078 3663 return true;
vithyat 0:977e87915078 3664 }
vithyat 0:977e87915078 3665 data = (coap_response_s *)ns_list_get_next(&_response_list, data);
vithyat 0:977e87915078 3666 }
vithyat 0:977e87915078 3667
vithyat 0:977e87915078 3668 return false;
vithyat 0:977e87915078 3669 }
vithyat 0:977e87915078 3670
vithyat 0:977e87915078 3671 void M2MNsdlInterface::remove_ping_from_response_list()
vithyat 0:977e87915078 3672 {
vithyat 0:977e87915078 3673 // ns_list_foreach() replacement since it does not compile with IAR 7.x versions.
vithyat 0:977e87915078 3674 coap_response_s *data = (coap_response_s *)ns_list_get_first(&_response_list);
vithyat 0:977e87915078 3675 while (data) {
vithyat 0:977e87915078 3676 if (data->type == M2MBase::PING) {
vithyat 0:977e87915078 3677 ns_list_remove(&_response_list, data);
vithyat 0:977e87915078 3678 memory_free(data->uri_path);
vithyat 0:977e87915078 3679 memory_free(data);
vithyat 0:977e87915078 3680 return;
vithyat 0:977e87915078 3681 }
vithyat 0:977e87915078 3682 data = (coap_response_s *)ns_list_get_next(&_response_list, data);
vithyat 0:977e87915078 3683 }
vithyat 0:977e87915078 3684 }
vithyat 0:977e87915078 3685
vithyat 0:977e87915078 3686 #if !defined(DISABLE_DELAYED_RESPONSE) || defined(ENABLE_ASYNC_REST_RESPONSE)
vithyat 0:977e87915078 3687 bool M2MNsdlInterface::handle_delayed_response_store(const char* uri_path,
vithyat 0:977e87915078 3688 sn_coap_hdr_s* received_coap,
vithyat 0:977e87915078 3689 sn_nsdl_addr_s *address,
vithyat 0:977e87915078 3690 const M2MBase::MessageType message_type)
vithyat 0:977e87915078 3691 {
vithyat 0:977e87915078 3692 coap_response_s *resp = NULL;
vithyat 0:977e87915078 3693 // When running client in Edge, it can store more than one request per resource
vithyat 0:977e87915078 3694 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 3695 resp = find_delayed_response(uri_path, message_type, received_coap->msg_id);
vithyat 0:977e87915078 3696 #else
vithyat 0:977e87915078 3697 resp = find_delayed_response(uri_path, message_type);
vithyat 0:977e87915078 3698 #endif
vithyat 0:977e87915078 3699 bool success = true;
vithyat 0:977e87915078 3700 // Only one request can be in process at a time
vithyat 0:977e87915078 3701 if (resp) {
vithyat 0:977e87915078 3702 sn_coap_hdr_s *coap_response = sn_nsdl_build_response(_nsdl_handle,
vithyat 0:977e87915078 3703 received_coap,
vithyat 0:977e87915078 3704 COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED);
vithyat 0:977e87915078 3705 if (coap_response) {
vithyat 0:977e87915078 3706 sn_nsdl_send_coap_message(_nsdl_handle, address, coap_response);
vithyat 0:977e87915078 3707 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, coap_response);
vithyat 0:977e87915078 3708 }
vithyat 0:977e87915078 3709
vithyat 0:977e87915078 3710 sn_nsdl_release_allocated_coap_msg_mem(_nsdl_handle, received_coap);
vithyat 0:977e87915078 3711 success = false;
vithyat 0:977e87915078 3712 } else {
vithyat 0:977e87915078 3713 // When running client in Edge, it can store more than one request per resource
vithyat 0:977e87915078 3714 #ifdef MBED_CLOUD_CLIENT_EDGE_EXTENSION
vithyat 0:977e87915078 3715 store_to_response_list(uri_path, received_coap->msg_id, message_type);
vithyat 0:977e87915078 3716 #else
vithyat 0:977e87915078 3717 store_to_response_list(uri_path, UNDEFINED_MSG_ID, message_type);
vithyat 0:977e87915078 3718 #endif
vithyat 0:977e87915078 3719 }
vithyat 0:977e87915078 3720
vithyat 0:977e87915078 3721 return success;
vithyat 0:977e87915078 3722 }
vithyat 0:977e87915078 3723 #endif