leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

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