Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 08:06:37 2018 +0000
Revision:
2:bf2124b482f9
Parent:
0:276e7a263c35
Update library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:276e7a263c35 1 /*
MACRUM 0:276e7a263c35 2 * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
MACRUM 0:276e7a263c35 3 * SPDX-License-Identifier: Apache-2.0
MACRUM 0:276e7a263c35 4 * Licensed under the Apache License, Version 2.0 (the License); you may
MACRUM 0:276e7a263c35 5 * not use this file except in compliance with the License.
MACRUM 0:276e7a263c35 6 * You may obtain a copy of the License at
MACRUM 0:276e7a263c35 7 *
MACRUM 0:276e7a263c35 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:276e7a263c35 9 *
MACRUM 0:276e7a263c35 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 0:276e7a263c35 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
MACRUM 0:276e7a263c35 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:276e7a263c35 13 * See the License for the specific language governing permissions and
MACRUM 0:276e7a263c35 14 * limitations under the License.
MACRUM 0:276e7a263c35 15 */
MACRUM 0:276e7a263c35 16 /**
MACRUM 0:276e7a263c35 17 * \file sn_nsdl.c
MACRUM 0:276e7a263c35 18 *
MACRUM 0:276e7a263c35 19 * \brief Nano service device library
MACRUM 0:276e7a263c35 20 *
MACRUM 0:276e7a263c35 21 */
MACRUM 0:276e7a263c35 22
MACRUM 0:276e7a263c35 23 // Needed for PRIu64 on FreeRTOS
MACRUM 0:276e7a263c35 24 #include <stdio.h>
MACRUM 0:276e7a263c35 25 // Note: this macro is needed on armcc to get the the limit macros like UINT16_MAX
MACRUM 0:276e7a263c35 26 #ifndef __STDC_LIMIT_MACROS
MACRUM 0:276e7a263c35 27 #define __STDC_LIMIT_MACROS
MACRUM 0:276e7a263c35 28 #endif
MACRUM 0:276e7a263c35 29
MACRUM 0:276e7a263c35 30 // Note: this macro is needed on armcc to get the the PRI*32 macros
MACRUM 0:276e7a263c35 31 // from inttypes.h in a C++ code.
MACRUM 0:276e7a263c35 32 #ifndef __STDC_FORMAT_MACROS
MACRUM 0:276e7a263c35 33 #define __STDC_FORMAT_MACROS
MACRUM 0:276e7a263c35 34 #endif
MACRUM 0:276e7a263c35 35
MACRUM 0:276e7a263c35 36 #include <string.h>
MACRUM 0:276e7a263c35 37
MACRUM 0:276e7a263c35 38 #include "ns_types.h"
MACRUM 0:276e7a263c35 39 #include "sn_nsdl.h"
MACRUM 0:276e7a263c35 40 #include "sn_coap_header.h"
MACRUM 0:276e7a263c35 41 #include "sn_coap_protocol.h"
MACRUM 0:276e7a263c35 42 #include "source/include/sn_coap_protocol_internal.h"
MACRUM 0:276e7a263c35 43 #include "sn_nsdl_lib.h"
MACRUM 0:276e7a263c35 44 #include "sn_grs.h"
MACRUM 0:276e7a263c35 45 #include "mbed-trace/mbed_trace.h"
MACRUM 0:276e7a263c35 46 #include "common_functions.h"
MACRUM 0:276e7a263c35 47 #include <stdlib.h>
MACRUM 0:276e7a263c35 48
MACRUM 0:276e7a263c35 49 /* Defines */
MACRUM 0:276e7a263c35 50 #define TRACE_GROUP "mClt"
MACRUM 0:276e7a263c35 51 #define RESOURCE_DIR_LEN 2
MACRUM 0:276e7a263c35 52 #define EP_NAME_PARAMETERS_LEN 3
MACRUM 0:276e7a263c35 53 #define ET_PARAMETER_LEN 3
MACRUM 0:276e7a263c35 54 #define LT_PARAMETER_LEN 3
MACRUM 0:276e7a263c35 55 #define DOMAIN_PARAMETER_LEN 2
MACRUM 0:276e7a263c35 56 #define RT_PARAMETER_LEN 3
MACRUM 0:276e7a263c35 57 #define IF_PARAMETER_LEN 3
MACRUM 0:276e7a263c35 58 #define NAME_PARAMETER_LEN 5
MACRUM 0:276e7a263c35 59 #define OBS_PARAMETER_LEN 3
MACRUM 0:276e7a263c35 60 #define AOBS_PARAMETER_LEN 5
MACRUM 0:276e7a263c35 61 #define COAP_CON_PARAMETER_LEN 3
MACRUM 0:276e7a263c35 62 #define BS_EP_PARAMETER_LEN 3
MACRUM 0:276e7a263c35 63 #define BS_QUEUE_MODE_PARAMATER_LEN 2
MACRUM 0:276e7a263c35 64
MACRUM 0:276e7a263c35 65 #define SN_NSDL_EP_REGISTER_MESSAGE 1
MACRUM 0:276e7a263c35 66 #define SN_NSDL_EP_UPDATE_MESSAGE 2
MACRUM 0:276e7a263c35 67
MACRUM 0:276e7a263c35 68 #define SN_NSDL_MSG_UNDEFINED 0
MACRUM 0:276e7a263c35 69 #define SN_NSDL_MSG_REGISTER 1
MACRUM 0:276e7a263c35 70 #define SN_NSDL_MSG_UNREGISTER 2
MACRUM 0:276e7a263c35 71 #define SN_NSDL_MSG_UPDATE 3
MACRUM 0:276e7a263c35 72 #define SN_NSDL_MSG_BOOTSTRAP 4
MACRUM 0:276e7a263c35 73
MACRUM 0:276e7a263c35 74 #ifdef YOTTA_CFG_DISABLE_OBS_FEATURE
MACRUM 0:276e7a263c35 75 #define COAP_DISABLE_OBS_FEATURE YOTTA_CFG_DISABLE_OBS_FEATURE
MACRUM 0:276e7a263c35 76 #elif defined MBED_CONF_MBED_CLIENT_COAP_DISABLE_OBS_FEATURE
MACRUM 0:276e7a263c35 77 #define COAP_DISABLE_OBS_FEATURE MBED_CONF_MBED_CLIENT_COAP_DISABLE_OBS_FEATURE
MACRUM 0:276e7a263c35 78 #endif
MACRUM 0:276e7a263c35 79
MACRUM 0:276e7a263c35 80 #ifdef YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 81 #define MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE YOTTA_CFG_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 82 #elif defined MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 83 #define MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE MBED_CONF_MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 84 #endif
MACRUM 0:276e7a263c35 85
MACRUM 0:276e7a263c35 86 /* Constants */
MACRUM 0:276e7a263c35 87 static uint8_t ep_name_parameter_string[] = {'e', 'p', '='}; /* Endpoint name. A unique name for the registering node in a domain. */
MACRUM 0:276e7a263c35 88 static uint8_t resource_path_ptr[] = {'r', 'd'}; /* For resource directory */
MACRUM 0:276e7a263c35 89 static uint8_t resource_type_parameter[] = {'r', 't', '='}; /* Resource type. Only once for registration */
MACRUM 0:276e7a263c35 90 #ifndef COAP_DISABLE_OBS_FEATURE
MACRUM 0:276e7a263c35 91 static uint8_t obs_parameter[] = {'o', 'b', 's'}; /* Observable */
MACRUM 0:276e7a263c35 92 static uint8_t aobs_parameter[] = {'a', 'o', 'b', 's', '='}; /* Auto observable */
MACRUM 0:276e7a263c35 93 #endif
MACRUM 0:276e7a263c35 94 static uint8_t if_description_parameter[] = {'i', 'f', '='}; /* Interface description. Only once */
MACRUM 0:276e7a263c35 95 static uint8_t ep_lifetime_parameter[] = {'l', 't', '='}; /* Lifetime. Number of seconds that this registration will be valid for. Must be updated within this time, or will be removed. */
MACRUM 0:276e7a263c35 96 static uint8_t ep_domain_parameter[] = {'d', '='}; /* Domain name. If this parameter is missing, a default domain is assumed. */
MACRUM 0:276e7a263c35 97 static uint8_t coap_con_type_parameter[] = {'c', 't', '='}; /* CoAP content type */
MACRUM 0:276e7a263c35 98
MACRUM 0:276e7a263c35 99 #ifdef RESOURCE_ATTRIBUTES_LIST
MACRUM 0:276e7a263c35 100 static uint8_t name_parameter[] = {'n', 'a', 'm', 'e', '='};
MACRUM 0:276e7a263c35 101 #endif
MACRUM 0:276e7a263c35 102
MACRUM 0:276e7a263c35 103 /* * OMA BS parameters * */
MACRUM 0:276e7a263c35 104 static uint8_t bs_uri[] = {'b', 's'};
MACRUM 0:276e7a263c35 105 static uint8_t bs_ep_name[] = {'e', 'p', '='};
MACRUM 0:276e7a263c35 106 static uint8_t et_parameter[] = {'e', 't', '='}; /* Endpoint type */
MACRUM 0:276e7a263c35 107 static uint8_t bs_queue_mode[] = {'b', '='};
MACRUM 0:276e7a263c35 108
MACRUM 0:276e7a263c35 109 /* Function prototypes */
MACRUM 0:276e7a263c35 110 static int32_t sn_nsdl_internal_coap_send(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description);
MACRUM 0:276e7a263c35 111 static void sn_nsdl_resolve_nsp_address(struct nsdl_s *handle);
MACRUM 0:276e7a263c35 112 int8_t sn_nsdl_build_registration_body(struct nsdl_s *handle, sn_coap_hdr_s *message_ptr, uint8_t updating_registeration);
MACRUM 0:276e7a263c35 113 static uint16_t sn_nsdl_calculate_registration_body_size(struct nsdl_s *handle, uint8_t updating_registeration, int8_t *error);
MACRUM 0:276e7a263c35 114 static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type, const char *uri_query);
MACRUM 0:276e7a263c35 115 static int8_t sn_nsdl_fill_uri_query_options(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type, const char *uri_query);
MACRUM 0:276e7a263c35 116 static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr);
MACRUM 0:276e7a263c35 117 static int8_t sn_nsdl_resolve_ep_information(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr);
MACRUM 0:276e7a263c35 118 static uint8_t sn_nsdl_itoa_len(uint32_t value);
MACRUM 0:276e7a263c35 119 static uint8_t *sn_nsdl_itoa(uint8_t *ptr, uint32_t value);
MACRUM 0:276e7a263c35 120 static int8_t set_endpoint_info(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *endpoint_info_ptr);
MACRUM 0:276e7a263c35 121 static bool validateParameters(sn_nsdl_ep_parameters_s *parameter_ptr);
MACRUM 0:276e7a263c35 122 static bool validate(uint8_t* ptr, uint32_t len, char illegalChar);
MACRUM 0:276e7a263c35 123 static bool sn_nsdl_check_uint_overflow(uint16_t resource_size, uint16_t param_a, uint16_t param_b);
MACRUM 0:276e7a263c35 124 static void remove_previous_block_data(struct nsdl_s *handle, sn_nsdl_addr_s *src_ptr, const uint32_t block_number);
MACRUM 0:276e7a263c35 125 static bool update_last_block_data(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, bool block1);
MACRUM 0:276e7a263c35 126 static void sn_nsdl_print_coap_data(sn_coap_hdr_s *coap_header_ptr, bool outgoing);
MACRUM 0:276e7a263c35 127 #if defined(FEA_TRACE_SUPPORT) || MBED_CONF_MBED_TRACE_ENABLE || YOTTA_CFG_MBED_TRACE || (defined(YOTTA_CFG) && !defined(NDEBUG))
MACRUM 0:276e7a263c35 128 static const char* sn_nsdl_coap_status_description(sn_coap_status_e status);
MACRUM 0:276e7a263c35 129 static const char* sn_nsdl_coap_message_code_desc(int msg_code);
MACRUM 0:276e7a263c35 130 static const char* sn_nsdl_coap_message_type_desc(int msg_type);
MACRUM 0:276e7a263c35 131 #endif
MACRUM 0:276e7a263c35 132
MACRUM 0:276e7a263c35 133 int8_t sn_nsdl_destroy(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 134 {
MACRUM 0:276e7a263c35 135 if (handle == NULL) {
MACRUM 0:276e7a263c35 136 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 137 }
MACRUM 0:276e7a263c35 138
MACRUM 0:276e7a263c35 139 if (handle->ep_information_ptr) {
MACRUM 0:276e7a263c35 140 handle->sn_nsdl_free(handle->ep_information_ptr->endpoint_name_ptr);
MACRUM 0:276e7a263c35 141 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
MACRUM 0:276e7a263c35 142 handle->sn_nsdl_free(handle->ep_information_ptr->location_ptr);
MACRUM 0:276e7a263c35 143 handle->sn_nsdl_free(handle->ep_information_ptr->type_ptr);
MACRUM 0:276e7a263c35 144 handle->sn_nsdl_free(handle->ep_information_ptr->lifetime_ptr);
MACRUM 0:276e7a263c35 145 handle->sn_nsdl_free(handle->ep_information_ptr);
MACRUM 0:276e7a263c35 146 }
MACRUM 0:276e7a263c35 147
MACRUM 0:276e7a263c35 148 if (handle->nsp_address_ptr) {
MACRUM 0:276e7a263c35 149 if (handle->nsp_address_ptr->omalw_address_ptr) {
MACRUM 0:276e7a263c35 150 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
MACRUM 0:276e7a263c35 151 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr);
MACRUM 0:276e7a263c35 152 }
MACRUM 0:276e7a263c35 153
MACRUM 0:276e7a263c35 154 handle->sn_nsdl_free(handle->nsp_address_ptr);
MACRUM 0:276e7a263c35 155 }
MACRUM 0:276e7a263c35 156
MACRUM 0:276e7a263c35 157 handle->sn_nsdl_free(handle->oma_bs_address_ptr);
MACRUM 0:276e7a263c35 158
MACRUM 0:276e7a263c35 159 /* Destroy also libCoap and grs part of libNsdl */
MACRUM 0:276e7a263c35 160 sn_coap_protocol_destroy(handle->grs->coap);
MACRUM 0:276e7a263c35 161 sn_grs_destroy(handle->grs);
MACRUM 0:276e7a263c35 162 handle->sn_nsdl_free(handle);
MACRUM 0:276e7a263c35 163
MACRUM 0:276e7a263c35 164 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 165 }
MACRUM 0:276e7a263c35 166
MACRUM 0:276e7a263c35 167 struct nsdl_s *sn_nsdl_init(uint8_t (*sn_nsdl_tx_cb)(struct nsdl_s *, sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *),
MACRUM 0:276e7a263c35 168 uint8_t (*sn_nsdl_rx_cb)(struct nsdl_s *, sn_coap_hdr_s *, sn_nsdl_addr_s *),
MACRUM 0:276e7a263c35 169 void *(*sn_nsdl_alloc)(uint16_t), void (*sn_nsdl_free)(void *),
MACRUM 0:276e7a263c35 170 uint8_t (*sn_nsdl_auto_obs_token_cb)(struct nsdl_s *, const char *, uint8_t *))
MACRUM 0:276e7a263c35 171 {
MACRUM 0:276e7a263c35 172 /* Check pointers and define function pointers */
MACRUM 0:276e7a263c35 173 if (!sn_nsdl_alloc || !sn_nsdl_free || !sn_nsdl_tx_cb || !sn_nsdl_rx_cb) {
MACRUM 0:276e7a263c35 174 return NULL;
MACRUM 0:276e7a263c35 175 }
MACRUM 0:276e7a263c35 176
MACRUM 0:276e7a263c35 177 struct nsdl_s *handle = NULL;
MACRUM 0:276e7a263c35 178
MACRUM 0:276e7a263c35 179 handle = sn_nsdl_alloc(sizeof(struct nsdl_s));
MACRUM 0:276e7a263c35 180
MACRUM 0:276e7a263c35 181 if (handle == NULL) {
MACRUM 0:276e7a263c35 182 return NULL;
MACRUM 0:276e7a263c35 183 }
MACRUM 0:276e7a263c35 184
MACRUM 0:276e7a263c35 185 memset(handle, 0, sizeof(struct nsdl_s));
MACRUM 0:276e7a263c35 186
MACRUM 0:276e7a263c35 187 /* Define function pointers */
MACRUM 0:276e7a263c35 188 handle->sn_nsdl_alloc = sn_nsdl_alloc;
MACRUM 0:276e7a263c35 189 handle->sn_nsdl_free = sn_nsdl_free;
MACRUM 0:276e7a263c35 190
MACRUM 0:276e7a263c35 191 handle->sn_nsdl_tx_callback = sn_nsdl_tx_cb;
MACRUM 0:276e7a263c35 192 handle->sn_nsdl_rx_callback = sn_nsdl_rx_cb;
MACRUM 0:276e7a263c35 193 handle->sn_nsdl_auto_obs_token_callback = sn_nsdl_auto_obs_token_cb;
MACRUM 0:276e7a263c35 194
MACRUM 0:276e7a263c35 195 /* Initialize ep parameters struct */
MACRUM 0:276e7a263c35 196 if (!handle->ep_information_ptr) {
MACRUM 0:276e7a263c35 197 handle->ep_information_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
MACRUM 0:276e7a263c35 198 if (!handle->ep_information_ptr) {
MACRUM 0:276e7a263c35 199 sn_nsdl_free(handle);
MACRUM 0:276e7a263c35 200 return NULL;
MACRUM 0:276e7a263c35 201 }
MACRUM 0:276e7a263c35 202 memset(handle->ep_information_ptr, 0, sizeof(sn_nsdl_ep_parameters_s));
MACRUM 0:276e7a263c35 203 }
MACRUM 0:276e7a263c35 204
MACRUM 0:276e7a263c35 205 handle->grs = sn_grs_init(sn_nsdl_tx_cb, &sn_nsdl_local_rx_function, sn_nsdl_alloc, sn_nsdl_free);
MACRUM 0:276e7a263c35 206
MACRUM 0:276e7a263c35 207 /* Initialize GRS */
MACRUM 0:276e7a263c35 208 if (handle->grs == NULL) {
MACRUM 0:276e7a263c35 209 handle->sn_nsdl_free(handle->ep_information_ptr);
MACRUM 0:276e7a263c35 210 handle->ep_information_ptr = 0;
MACRUM 0:276e7a263c35 211 sn_nsdl_free(handle);
MACRUM 0:276e7a263c35 212 return NULL;
MACRUM 0:276e7a263c35 213 }
MACRUM 0:276e7a263c35 214
MACRUM 0:276e7a263c35 215 sn_nsdl_resolve_nsp_address(handle);
MACRUM 0:276e7a263c35 216
MACRUM 0:276e7a263c35 217 handle->sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_NOT_REGISTERED;
MACRUM 0:276e7a263c35 218 handle->context = NULL;
MACRUM 0:276e7a263c35 219
MACRUM 0:276e7a263c35 220 return handle;
MACRUM 0:276e7a263c35 221 }
MACRUM 0:276e7a263c35 222
MACRUM 0:276e7a263c35 223 uint16_t sn_nsdl_register_endpoint(struct nsdl_s *handle,
MACRUM 0:276e7a263c35 224 sn_nsdl_ep_parameters_s *endpoint_info_ptr,
MACRUM 0:276e7a263c35 225 const char *uri_query_parameters)
MACRUM 0:276e7a263c35 226 {
MACRUM 0:276e7a263c35 227 /* Local variables */
MACRUM 0:276e7a263c35 228 sn_coap_hdr_s *register_message_ptr;
MACRUM 0:276e7a263c35 229 uint16_t message_id = 0;
MACRUM 0:276e7a263c35 230
MACRUM 0:276e7a263c35 231 if (endpoint_info_ptr == NULL || handle == NULL) {
MACRUM 0:276e7a263c35 232 return 0;
MACRUM 0:276e7a263c35 233 }
MACRUM 0:276e7a263c35 234
MACRUM 0:276e7a263c35 235 /*** Build endpoint register message ***/
MACRUM 0:276e7a263c35 236
MACRUM 0:276e7a263c35 237 /* Allocate memory for header struct */
MACRUM 0:276e7a263c35 238 register_message_ptr = sn_coap_parser_alloc_message(handle->grs->coap);
MACRUM 0:276e7a263c35 239 if (register_message_ptr == NULL) {
MACRUM 0:276e7a263c35 240 return 0;
MACRUM 0:276e7a263c35 241 }
MACRUM 0:276e7a263c35 242
MACRUM 0:276e7a263c35 243 /* Fill message fields -> confirmable post to specified NSP path */
MACRUM 0:276e7a263c35 244 register_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
MACRUM 0:276e7a263c35 245 register_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST;
MACRUM 0:276e7a263c35 246
MACRUM 0:276e7a263c35 247 /* Allocate memory for the extended options list */
MACRUM 0:276e7a263c35 248 if (sn_coap_parser_alloc_options(handle->grs->coap, register_message_ptr) == NULL) {
MACRUM 0:276e7a263c35 249 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 250 register_message_ptr = 0;
MACRUM 0:276e7a263c35 251 return 0;
MACRUM 0:276e7a263c35 252 }
MACRUM 0:276e7a263c35 253
MACRUM 0:276e7a263c35 254 register_message_ptr->uri_path_len = sizeof(resource_path_ptr);
MACRUM 0:276e7a263c35 255 register_message_ptr->uri_path_ptr = resource_path_ptr;
MACRUM 0:276e7a263c35 256
MACRUM 0:276e7a263c35 257 /* Fill Uri-query options */
MACRUM 0:276e7a263c35 258 if( SN_NSDL_FAILURE == sn_nsdl_fill_uri_query_options(handle, endpoint_info_ptr,
MACRUM 0:276e7a263c35 259 register_message_ptr, SN_NSDL_EP_REGISTER_MESSAGE,
MACRUM 0:276e7a263c35 260 uri_query_parameters) ){
MACRUM 0:276e7a263c35 261 register_message_ptr->uri_path_ptr = NULL;
MACRUM 0:276e7a263c35 262 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 263 return 0;
MACRUM 0:276e7a263c35 264 }
MACRUM 0:276e7a263c35 265
MACRUM 0:276e7a263c35 266 if (endpoint_info_ptr->ds_register_mode == REGISTER_WITH_RESOURCES) {
MACRUM 0:276e7a263c35 267 /* Built body for message */
MACRUM 0:276e7a263c35 268 if (sn_nsdl_build_registration_body(handle, register_message_ptr, 0) == SN_NSDL_FAILURE) {
MACRUM 0:276e7a263c35 269 register_message_ptr->uri_path_ptr = NULL;
MACRUM 0:276e7a263c35 270 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
MACRUM 0:276e7a263c35 271 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 272 return 0;
MACRUM 0:276e7a263c35 273 }
MACRUM 0:276e7a263c35 274 }
MACRUM 0:276e7a263c35 275
MACRUM 0:276e7a263c35 276 /* Clean (possible) existing and save new endpoint info to handle */
MACRUM 0:276e7a263c35 277 if (set_endpoint_info(handle, endpoint_info_ptr) == -1) {
MACRUM 0:276e7a263c35 278
MACRUM 0:276e7a263c35 279 handle->sn_nsdl_free(register_message_ptr->payload_ptr);
MACRUM 0:276e7a263c35 280 register_message_ptr->payload_ptr = NULL;
MACRUM 0:276e7a263c35 281
MACRUM 0:276e7a263c35 282 register_message_ptr->uri_path_ptr = NULL;
MACRUM 0:276e7a263c35 283 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
MACRUM 0:276e7a263c35 284
MACRUM 0:276e7a263c35 285 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 286
MACRUM 0:276e7a263c35 287 return 0;
MACRUM 0:276e7a263c35 288 }
MACRUM 0:276e7a263c35 289
MACRUM 0:276e7a263c35 290 /* Build and send coap message to NSP */
MACRUM 0:276e7a263c35 291 message_id = sn_nsdl_internal_coap_send(handle, register_message_ptr, handle->nsp_address_ptr->omalw_address_ptr, SN_NSDL_MSG_REGISTER);
MACRUM 0:276e7a263c35 292
MACRUM 0:276e7a263c35 293 handle->sn_nsdl_free(register_message_ptr->payload_ptr);
MACRUM 0:276e7a263c35 294 register_message_ptr->payload_ptr = NULL;
MACRUM 0:276e7a263c35 295
MACRUM 0:276e7a263c35 296 register_message_ptr->uri_path_ptr = NULL;
MACRUM 0:276e7a263c35 297 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
MACRUM 0:276e7a263c35 298
MACRUM 0:276e7a263c35 299 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 300
MACRUM 0:276e7a263c35 301 return message_id;
MACRUM 0:276e7a263c35 302 }
MACRUM 0:276e7a263c35 303
MACRUM 0:276e7a263c35 304 int32_t sn_nsdl_unregister_endpoint(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 305 {
MACRUM 0:276e7a263c35 306 /* Local variables */
MACRUM 0:276e7a263c35 307 sn_coap_hdr_s *unregister_message_ptr;
MACRUM 0:276e7a263c35 308 uint8_t *temp_ptr = 0;
MACRUM 0:276e7a263c35 309 int32_t message_id = 0;
MACRUM 0:276e7a263c35 310
MACRUM 0:276e7a263c35 311 /* Check parameters */
MACRUM 0:276e7a263c35 312 if (handle == NULL) {
MACRUM 0:276e7a263c35 313 return 0;
MACRUM 0:276e7a263c35 314 }
MACRUM 0:276e7a263c35 315
MACRUM 0:276e7a263c35 316 /* Check that EP have been registered */
MACRUM 0:276e7a263c35 317 if (sn_nsdl_is_ep_registered(handle)) {
MACRUM 0:276e7a263c35 318
MACRUM 0:276e7a263c35 319 /* Memory allocation for unregister message */
MACRUM 0:276e7a263c35 320 unregister_message_ptr = sn_coap_parser_alloc_message(handle->grs->coap);
MACRUM 0:276e7a263c35 321 if (!unregister_message_ptr) {
MACRUM 0:276e7a263c35 322 return 0;
MACRUM 0:276e7a263c35 323 }
MACRUM 0:276e7a263c35 324
MACRUM 0:276e7a263c35 325 /* Fill unregister message */
MACRUM 0:276e7a263c35 326 unregister_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
MACRUM 0:276e7a263c35 327 unregister_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_DELETE;
MACRUM 0:276e7a263c35 328
MACRUM 0:276e7a263c35 329 if(handle->ep_information_ptr->location_ptr) {
MACRUM 0:276e7a263c35 330 unregister_message_ptr->uri_path_len = handle->ep_information_ptr->location_len;
MACRUM 0:276e7a263c35 331 unregister_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(unregister_message_ptr->uri_path_len);
MACRUM 0:276e7a263c35 332 if (!unregister_message_ptr->uri_path_ptr) {
MACRUM 0:276e7a263c35 333 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, unregister_message_ptr);
MACRUM 0:276e7a263c35 334 return 0;
MACRUM 0:276e7a263c35 335 }
MACRUM 0:276e7a263c35 336
MACRUM 0:276e7a263c35 337 temp_ptr = unregister_message_ptr->uri_path_ptr;
MACRUM 0:276e7a263c35 338
MACRUM 0:276e7a263c35 339 memcpy(temp_ptr , handle->ep_information_ptr->location_ptr, handle->ep_information_ptr->location_len);
MACRUM 0:276e7a263c35 340 } else {
MACRUM 0:276e7a263c35 341 unregister_message_ptr->uri_path_len = (RESOURCE_DIR_LEN + 1 + handle->ep_information_ptr->domain_name_len + 1 + handle->ep_information_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 342 unregister_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(unregister_message_ptr->uri_path_len);
MACRUM 0:276e7a263c35 343 if (!unregister_message_ptr->uri_path_ptr) {
MACRUM 0:276e7a263c35 344 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, unregister_message_ptr);
MACRUM 0:276e7a263c35 345 return 0;
MACRUM 0:276e7a263c35 346 }
MACRUM 0:276e7a263c35 347
MACRUM 0:276e7a263c35 348 temp_ptr = unregister_message_ptr->uri_path_ptr;
MACRUM 0:276e7a263c35 349
MACRUM 0:276e7a263c35 350 memcpy(temp_ptr, resource_path_ptr, RESOURCE_DIR_LEN);
MACRUM 0:276e7a263c35 351 temp_ptr += RESOURCE_DIR_LEN;
MACRUM 0:276e7a263c35 352
MACRUM 0:276e7a263c35 353 *temp_ptr++ = '/';
MACRUM 0:276e7a263c35 354
MACRUM 0:276e7a263c35 355 memcpy(temp_ptr , handle->ep_information_ptr->domain_name_ptr, handle->ep_information_ptr->domain_name_len);
MACRUM 0:276e7a263c35 356 temp_ptr += handle->ep_information_ptr->domain_name_len;
MACRUM 0:276e7a263c35 357
MACRUM 0:276e7a263c35 358 *temp_ptr++ = '/';
MACRUM 0:276e7a263c35 359
MACRUM 0:276e7a263c35 360 memcpy(temp_ptr , handle->ep_information_ptr->endpoint_name_ptr, handle->ep_information_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 361 }
MACRUM 0:276e7a263c35 362
MACRUM 0:276e7a263c35 363 /* Send message */
MACRUM 0:276e7a263c35 364 message_id = sn_nsdl_internal_coap_send(handle, unregister_message_ptr, handle->nsp_address_ptr->omalw_address_ptr, SN_NSDL_MSG_UNREGISTER);
MACRUM 0:276e7a263c35 365
MACRUM 0:276e7a263c35 366 /* Free memory */
MACRUM 0:276e7a263c35 367 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, unregister_message_ptr);
MACRUM 0:276e7a263c35 368
MACRUM 0:276e7a263c35 369 }
MACRUM 0:276e7a263c35 370
MACRUM 0:276e7a263c35 371 return message_id;
MACRUM 0:276e7a263c35 372 }
MACRUM 0:276e7a263c35 373
MACRUM 0:276e7a263c35 374 int32_t sn_nsdl_update_registration(struct nsdl_s *handle, uint8_t *lt_ptr, uint8_t lt_len)
MACRUM 0:276e7a263c35 375 {
MACRUM 0:276e7a263c35 376 /* Local variables */
MACRUM 0:276e7a263c35 377 sn_coap_hdr_s *register_message_ptr;
MACRUM 0:276e7a263c35 378 uint8_t *temp_ptr;
MACRUM 0:276e7a263c35 379 sn_nsdl_ep_parameters_s temp_parameters;
MACRUM 0:276e7a263c35 380 int32_t message_id = 0;
MACRUM 0:276e7a263c35 381
MACRUM 0:276e7a263c35 382 /* Check parameters */
MACRUM 0:276e7a263c35 383 if (handle == NULL) {
MACRUM 0:276e7a263c35 384 return 0;
MACRUM 0:276e7a263c35 385 }
MACRUM 0:276e7a263c35 386
MACRUM 0:276e7a263c35 387 if (!sn_nsdl_is_ep_registered(handle)){
MACRUM 0:276e7a263c35 388 return 0;
MACRUM 0:276e7a263c35 389 }
MACRUM 0:276e7a263c35 390
MACRUM 0:276e7a263c35 391 memset(&temp_parameters, 0, sizeof(sn_nsdl_ep_parameters_s));
MACRUM 0:276e7a263c35 392
MACRUM 0:276e7a263c35 393 temp_parameters.lifetime_len = lt_len;
MACRUM 0:276e7a263c35 394 temp_parameters.lifetime_ptr = lt_ptr;
MACRUM 0:276e7a263c35 395
MACRUM 0:276e7a263c35 396 /*** Build endpoint register update message ***/
MACRUM 0:276e7a263c35 397
MACRUM 0:276e7a263c35 398 /* Allocate memory for header struct */
MACRUM 0:276e7a263c35 399 register_message_ptr = sn_coap_parser_alloc_message(handle->grs->coap);
MACRUM 0:276e7a263c35 400 if (register_message_ptr == NULL) {
MACRUM 0:276e7a263c35 401 return 0;
MACRUM 0:276e7a263c35 402 }
MACRUM 0:276e7a263c35 403
MACRUM 0:276e7a263c35 404 /* Fill message fields -> confirmable post to specified NSP path */
MACRUM 0:276e7a263c35 405 register_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
MACRUM 0:276e7a263c35 406 register_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST;
MACRUM 0:276e7a263c35 407
MACRUM 0:276e7a263c35 408 if(handle->ep_information_ptr->location_ptr) {
MACRUM 0:276e7a263c35 409 register_message_ptr->uri_path_len = handle->ep_information_ptr->location_len; /* = Only location set by Device Server*/
MACRUM 0:276e7a263c35 410
MACRUM 0:276e7a263c35 411 register_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(register_message_ptr->uri_path_len);
MACRUM 0:276e7a263c35 412 if (!register_message_ptr->uri_path_ptr) {
MACRUM 0:276e7a263c35 413 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 414 return 0;
MACRUM 0:276e7a263c35 415 }
MACRUM 0:276e7a263c35 416
MACRUM 0:276e7a263c35 417 temp_ptr = register_message_ptr->uri_path_ptr;
MACRUM 0:276e7a263c35 418
MACRUM 0:276e7a263c35 419 /* location */
MACRUM 0:276e7a263c35 420 memcpy(temp_ptr, handle->ep_information_ptr->location_ptr, handle->ep_information_ptr->location_len);
MACRUM 0:276e7a263c35 421 } else {
MACRUM 0:276e7a263c35 422 register_message_ptr->uri_path_len = sizeof(resource_path_ptr) + handle->ep_information_ptr->domain_name_len + handle->ep_information_ptr->endpoint_name_len + 2; /* = rd/domain/endpoint */
MACRUM 0:276e7a263c35 423
MACRUM 0:276e7a263c35 424 register_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(register_message_ptr->uri_path_len);
MACRUM 0:276e7a263c35 425 if (!register_message_ptr->uri_path_ptr) {
MACRUM 0:276e7a263c35 426 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 427 return 0;
MACRUM 0:276e7a263c35 428 }
MACRUM 0:276e7a263c35 429
MACRUM 0:276e7a263c35 430 temp_ptr = register_message_ptr->uri_path_ptr;
MACRUM 0:276e7a263c35 431
MACRUM 0:276e7a263c35 432 /* rd/ */
MACRUM 0:276e7a263c35 433 memcpy(temp_ptr, resource_path_ptr, sizeof(resource_path_ptr));
MACRUM 0:276e7a263c35 434 temp_ptr += sizeof(resource_path_ptr);
MACRUM 0:276e7a263c35 435 *temp_ptr++ = '/';
MACRUM 0:276e7a263c35 436
MACRUM 0:276e7a263c35 437 /* rd/DOMAIN/ */
MACRUM 0:276e7a263c35 438 memcpy(temp_ptr, handle->ep_information_ptr->domain_name_ptr, handle->ep_information_ptr->domain_name_len);
MACRUM 0:276e7a263c35 439 temp_ptr += handle->ep_information_ptr->domain_name_len;
MACRUM 0:276e7a263c35 440 *temp_ptr++ = '/';
MACRUM 0:276e7a263c35 441
MACRUM 0:276e7a263c35 442 /* rd/domain/ENDPOINT */
MACRUM 0:276e7a263c35 443 memcpy(temp_ptr, handle->ep_information_ptr->endpoint_name_ptr, handle->ep_information_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 444 }
MACRUM 0:276e7a263c35 445
MACRUM 0:276e7a263c35 446 /* Allocate memory for the extended options list */
MACRUM 0:276e7a263c35 447 if (sn_coap_parser_alloc_options(handle->grs->coap, register_message_ptr) == NULL) {
MACRUM 0:276e7a263c35 448 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 449 return 0;
MACRUM 0:276e7a263c35 450 }
MACRUM 0:276e7a263c35 451
MACRUM 0:276e7a263c35 452 /* Fill Uri-query options */
MACRUM 0:276e7a263c35 453 sn_nsdl_fill_uri_query_options(handle, &temp_parameters, register_message_ptr, SN_NSDL_EP_UPDATE_MESSAGE, NULL);
MACRUM 0:276e7a263c35 454
MACRUM 0:276e7a263c35 455 /* Build payload */
MACRUM 0:276e7a263c35 456 if (handle->ep_information_ptr->ds_register_mode == REGISTER_WITH_RESOURCES) {
MACRUM 0:276e7a263c35 457
MACRUM 0:276e7a263c35 458 if (sn_nsdl_build_registration_body(handle, register_message_ptr, 1) == SN_NSDL_FAILURE) {
MACRUM 0:276e7a263c35 459 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 460 return 0;
MACRUM 0:276e7a263c35 461 }
MACRUM 0:276e7a263c35 462 }
MACRUM 0:276e7a263c35 463
MACRUM 0:276e7a263c35 464 /* Build and send coap message to NSP */
MACRUM 0:276e7a263c35 465 message_id = sn_nsdl_internal_coap_send(handle, register_message_ptr, handle->nsp_address_ptr->omalw_address_ptr, SN_NSDL_MSG_UPDATE);
MACRUM 0:276e7a263c35 466
MACRUM 0:276e7a263c35 467 handle->sn_nsdl_free(register_message_ptr->payload_ptr);
MACRUM 0:276e7a263c35 468
MACRUM 0:276e7a263c35 469 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
MACRUM 0:276e7a263c35 470
MACRUM 0:276e7a263c35 471 return message_id;
MACRUM 0:276e7a263c35 472 }
MACRUM 0:276e7a263c35 473
MACRUM 0:276e7a263c35 474 int8_t sn_nsdl_set_endpoint_location(struct nsdl_s *handle, uint8_t *location_ptr, uint8_t location_len)
MACRUM 0:276e7a263c35 475 {
MACRUM 0:276e7a263c35 476 if(!handle || !location_ptr || (location_len == 0)) {
MACRUM 0:276e7a263c35 477 return -1;
MACRUM 0:276e7a263c35 478 }
MACRUM 0:276e7a263c35 479
MACRUM 0:276e7a263c35 480 handle->sn_nsdl_free(handle->ep_information_ptr->location_ptr);
MACRUM 0:276e7a263c35 481 handle->ep_information_ptr->location_ptr = handle->sn_nsdl_alloc(location_len);
MACRUM 0:276e7a263c35 482 memcpy(handle->ep_information_ptr->location_ptr, location_ptr, location_len);
MACRUM 0:276e7a263c35 483 handle->ep_information_ptr->location_len = location_len;
MACRUM 0:276e7a263c35 484
MACRUM 0:276e7a263c35 485 return 0;
MACRUM 0:276e7a263c35 486 }
MACRUM 0:276e7a263c35 487
MACRUM 0:276e7a263c35 488 void sn_nsdl_nsp_lost(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 489 {
MACRUM 0:276e7a263c35 490 /* Check parameters */
MACRUM 0:276e7a263c35 491 if (handle == NULL) {
MACRUM 0:276e7a263c35 492 return;
MACRUM 0:276e7a263c35 493 }
MACRUM 0:276e7a263c35 494
MACRUM 0:276e7a263c35 495 handle->sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_NOT_REGISTERED;
MACRUM 0:276e7a263c35 496 }
MACRUM 0:276e7a263c35 497
MACRUM 0:276e7a263c35 498 int8_t sn_nsdl_is_ep_registered(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 499 {
MACRUM 0:276e7a263c35 500 /* Check parameters */
MACRUM 0:276e7a263c35 501 if (handle == NULL) {
MACRUM 0:276e7a263c35 502 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 503 }
MACRUM 0:276e7a263c35 504
MACRUM 0:276e7a263c35 505 return handle->sn_nsdl_endpoint_registered;
MACRUM 0:276e7a263c35 506 }
MACRUM 0:276e7a263c35 507
MACRUM 0:276e7a263c35 508 int32_t sn_nsdl_send_observation_notification(struct nsdl_s *handle, uint8_t *token_ptr, uint8_t token_len,
MACRUM 0:276e7a263c35 509 uint8_t *payload_ptr, uint16_t payload_len, sn_coap_observe_e observe, sn_coap_msg_type_e message_type,
MACRUM 0:276e7a263c35 510 sn_coap_content_format_e content_format,
MACRUM 0:276e7a263c35 511 const int32_t message_id)
MACRUM 0:276e7a263c35 512 {
MACRUM 0:276e7a263c35 513 sn_coap_hdr_s *notification_message_ptr;
MACRUM 0:276e7a263c35 514 int32_t return_msg_id = 0;
MACRUM 0:276e7a263c35 515
MACRUM 0:276e7a263c35 516 /* Check parameters */
MACRUM 0:276e7a263c35 517 if (handle == NULL || handle->grs == NULL) {
MACRUM 0:276e7a263c35 518 return 0;
MACRUM 0:276e7a263c35 519 }
MACRUM 0:276e7a263c35 520
MACRUM 0:276e7a263c35 521 /* Allocate and initialize memory for header struct */
MACRUM 0:276e7a263c35 522 notification_message_ptr = sn_coap_parser_alloc_message(handle->grs->coap);
MACRUM 0:276e7a263c35 523 if (notification_message_ptr == NULL) {
MACRUM 0:276e7a263c35 524 return 0;
MACRUM 0:276e7a263c35 525 }
MACRUM 0:276e7a263c35 526
MACRUM 0:276e7a263c35 527 if (sn_coap_parser_alloc_options(handle->grs->coap, notification_message_ptr) == NULL) {
MACRUM 0:276e7a263c35 528 handle->sn_nsdl_free(notification_message_ptr);
MACRUM 0:276e7a263c35 529 return 0;
MACRUM 0:276e7a263c35 530 }
MACRUM 0:276e7a263c35 531
MACRUM 0:276e7a263c35 532 /* Fill header */
MACRUM 0:276e7a263c35 533 notification_message_ptr->msg_type = message_type;
MACRUM 0:276e7a263c35 534 notification_message_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
MACRUM 0:276e7a263c35 535
MACRUM 0:276e7a263c35 536 /* Fill token */
MACRUM 0:276e7a263c35 537 notification_message_ptr->token_len = token_len;
MACRUM 0:276e7a263c35 538 notification_message_ptr->token_ptr = token_ptr;
MACRUM 0:276e7a263c35 539
MACRUM 0:276e7a263c35 540 /* Fill payload */
MACRUM 0:276e7a263c35 541 notification_message_ptr->payload_len = payload_len;
MACRUM 0:276e7a263c35 542 notification_message_ptr->payload_ptr = payload_ptr;
MACRUM 0:276e7a263c35 543
MACRUM 0:276e7a263c35 544 /* Fill observe */
MACRUM 0:276e7a263c35 545 notification_message_ptr->options_list_ptr->observe = observe;
MACRUM 0:276e7a263c35 546
MACRUM 0:276e7a263c35 547 /* Fill content format */
MACRUM 0:276e7a263c35 548 notification_message_ptr->content_format = content_format;
MACRUM 0:276e7a263c35 549
MACRUM 0:276e7a263c35 550 if (message_id != -1) {
MACRUM 0:276e7a263c35 551 notification_message_ptr->msg_id = message_id;
MACRUM 0:276e7a263c35 552 }
MACRUM 0:276e7a263c35 553
MACRUM 0:276e7a263c35 554 /* Send message */
MACRUM 0:276e7a263c35 555 return_msg_id = sn_nsdl_send_coap_message(handle, handle->nsp_address_ptr->omalw_address_ptr, notification_message_ptr);
MACRUM 0:276e7a263c35 556 if (return_msg_id >= SN_NSDL_SUCCESS) {
MACRUM 0:276e7a263c35 557 return_msg_id = notification_message_ptr->msg_id;
MACRUM 0:276e7a263c35 558 }
MACRUM 0:276e7a263c35 559
MACRUM 0:276e7a263c35 560 /* Free memory */
MACRUM 0:276e7a263c35 561 notification_message_ptr->payload_ptr = NULL;
MACRUM 0:276e7a263c35 562 notification_message_ptr->token_ptr = NULL;
MACRUM 0:276e7a263c35 563
MACRUM 0:276e7a263c35 564 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, notification_message_ptr);
MACRUM 0:276e7a263c35 565
MACRUM 0:276e7a263c35 566 return return_msg_id;
MACRUM 0:276e7a263c35 567 }
MACRUM 0:276e7a263c35 568
MACRUM 0:276e7a263c35 569 /* * * * * * * * * * */
MACRUM 0:276e7a263c35 570 /* ~ OMA functions ~ */
MACRUM 0:276e7a263c35 571 /* * * * * * * * * * */
MACRUM 0:276e7a263c35 572
MACRUM 0:276e7a263c35 573 uint16_t sn_nsdl_oma_bootstrap(struct nsdl_s *handle, sn_nsdl_addr_s *bootstrap_address_ptr,
MACRUM 0:276e7a263c35 574 sn_nsdl_ep_parameters_s *endpoint_info_ptr,
MACRUM 0:276e7a263c35 575 sn_nsdl_bs_ep_info_t *bootstrap_endpoint_info_ptr,
MACRUM 0:276e7a263c35 576 const char *uri_query_parameters)
MACRUM 0:276e7a263c35 577 {
MACRUM 0:276e7a263c35 578 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 579 /* Local variables */
MACRUM 0:276e7a263c35 580 sn_coap_hdr_s bootstrap_coap_header;
MACRUM 0:276e7a263c35 581 uint8_t *uri_query_tmp_ptr;
MACRUM 0:276e7a263c35 582 uint16_t message_id = 0;
MACRUM 0:276e7a263c35 583
MACRUM 0:276e7a263c35 584 /* Check parameters */
MACRUM 0:276e7a263c35 585 if (!bootstrap_address_ptr || !bootstrap_endpoint_info_ptr || !endpoint_info_ptr || !handle) {
MACRUM 0:276e7a263c35 586 return 0;
MACRUM 0:276e7a263c35 587 }
MACRUM 0:276e7a263c35 588
MACRUM 0:276e7a263c35 589 handle->sn_nsdl_oma_bs_done_cb = bootstrap_endpoint_info_ptr->oma_bs_status_cb;
MACRUM 0:276e7a263c35 590 handle->sn_nsdl_oma_bs_done_cb_handle = bootstrap_endpoint_info_ptr->oma_bs_status_cb_handle;
MACRUM 0:276e7a263c35 591
MACRUM 0:276e7a263c35 592 /* XXX FIX -- Init CoAP header struct */
MACRUM 0:276e7a263c35 593 sn_coap_parser_init_message(&bootstrap_coap_header);
MACRUM 0:276e7a263c35 594
MACRUM 0:276e7a263c35 595 if (!sn_coap_parser_alloc_options(handle->grs->coap, &bootstrap_coap_header)) {
MACRUM 0:276e7a263c35 596 return 0;
MACRUM 0:276e7a263c35 597 }
MACRUM 0:276e7a263c35 598
MACRUM 0:276e7a263c35 599 /* Build bootstrap start message */
MACRUM 0:276e7a263c35 600 bootstrap_coap_header.msg_code = COAP_MSG_CODE_REQUEST_POST;
MACRUM 0:276e7a263c35 601 bootstrap_coap_header.msg_type = COAP_MSG_TYPE_CONFIRMABLE;
MACRUM 0:276e7a263c35 602
MACRUM 0:276e7a263c35 603 bootstrap_coap_header.uri_path_ptr = bs_uri;
MACRUM 0:276e7a263c35 604 bootstrap_coap_header.uri_path_len = sizeof(bs_uri);
MACRUM 0:276e7a263c35 605
MACRUM 0:276e7a263c35 606 size_t query_len = endpoint_info_ptr->endpoint_name_len + BS_EP_PARAMETER_LEN;
MACRUM 0:276e7a263c35 607 size_t optional_params_len = 0;
MACRUM 0:276e7a263c35 608 if (uri_query_parameters) {
MACRUM 0:276e7a263c35 609 optional_params_len = strlen(uri_query_parameters);
MACRUM 0:276e7a263c35 610 }
MACRUM 0:276e7a263c35 611
MACRUM 0:276e7a263c35 612 query_len += optional_params_len;
MACRUM 0:276e7a263c35 613
MACRUM 0:276e7a263c35 614 if (query_len > MAX_URI_QUERY_LEN) {
MACRUM 0:276e7a263c35 615 handle->sn_nsdl_free(bootstrap_coap_header.options_list_ptr);
MACRUM 0:276e7a263c35 616 tr_error("sn_nsdl_oma_bootstrap - max param length reached (%d)", query_len);
MACRUM 0:276e7a263c35 617 return 0;
MACRUM 0:276e7a263c35 618 }
MACRUM 0:276e7a263c35 619
MACRUM 0:276e7a263c35 620 uri_query_tmp_ptr = handle->sn_nsdl_alloc(query_len);
MACRUM 0:276e7a263c35 621 if (!uri_query_tmp_ptr) {
MACRUM 0:276e7a263c35 622 handle->sn_nsdl_free(bootstrap_coap_header.options_list_ptr);
MACRUM 0:276e7a263c35 623 return 0;
MACRUM 0:276e7a263c35 624 }
MACRUM 0:276e7a263c35 625
MACRUM 0:276e7a263c35 626 memcpy(uri_query_tmp_ptr, bs_ep_name, BS_EP_PARAMETER_LEN);
MACRUM 0:276e7a263c35 627 memcpy((uri_query_tmp_ptr + BS_EP_PARAMETER_LEN),
MACRUM 0:276e7a263c35 628 endpoint_info_ptr->endpoint_name_ptr,
MACRUM 0:276e7a263c35 629 endpoint_info_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 630
MACRUM 0:276e7a263c35 631 if (optional_params_len > 0) {
MACRUM 0:276e7a263c35 632 memcpy(uri_query_tmp_ptr + endpoint_info_ptr->endpoint_name_len + BS_EP_PARAMETER_LEN,
MACRUM 0:276e7a263c35 633 uri_query_parameters,
MACRUM 0:276e7a263c35 634 optional_params_len);
MACRUM 0:276e7a263c35 635 }
MACRUM 0:276e7a263c35 636
MACRUM 0:276e7a263c35 637 bootstrap_coap_header.options_list_ptr->uri_query_len = query_len;
MACRUM 0:276e7a263c35 638 bootstrap_coap_header.options_list_ptr->uri_query_ptr = uri_query_tmp_ptr;
MACRUM 0:276e7a263c35 639
MACRUM 0:276e7a263c35 640 /* Save bootstrap server address */
MACRUM 0:276e7a263c35 641 handle->oma_bs_address_len = bootstrap_address_ptr->addr_len; /* Length.. */
MACRUM 0:276e7a263c35 642 handle->oma_bs_address_ptr = handle->sn_nsdl_alloc(handle->oma_bs_address_len); /* Address.. */
MACRUM 0:276e7a263c35 643 if (!handle->oma_bs_address_ptr) {
MACRUM 0:276e7a263c35 644 handle->sn_nsdl_free(bootstrap_coap_header.options_list_ptr);
MACRUM 0:276e7a263c35 645 handle->sn_nsdl_free(uri_query_tmp_ptr);
MACRUM 0:276e7a263c35 646 return 0;
MACRUM 0:276e7a263c35 647 }
MACRUM 0:276e7a263c35 648 memcpy(handle->oma_bs_address_ptr, bootstrap_address_ptr->addr_ptr, handle->oma_bs_address_len);
MACRUM 0:276e7a263c35 649 handle->oma_bs_port = bootstrap_address_ptr->port; /* And port */
MACRUM 0:276e7a263c35 650
MACRUM 0:276e7a263c35 651 /* Send message */
MACRUM 0:276e7a263c35 652 message_id = sn_nsdl_internal_coap_send(handle, &bootstrap_coap_header, bootstrap_address_ptr, SN_NSDL_MSG_BOOTSTRAP);
MACRUM 0:276e7a263c35 653
MACRUM 0:276e7a263c35 654 /* Free allocated memory */
MACRUM 0:276e7a263c35 655 handle->sn_nsdl_free(uri_query_tmp_ptr);
MACRUM 0:276e7a263c35 656 handle->sn_nsdl_free(bootstrap_coap_header.options_list_ptr);
MACRUM 0:276e7a263c35 657
MACRUM 0:276e7a263c35 658 return message_id;
MACRUM 0:276e7a263c35 659 #else
MACRUM 0:276e7a263c35 660 return 0;
MACRUM 0:276e7a263c35 661 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 662
MACRUM 0:276e7a263c35 663 }
MACRUM 0:276e7a263c35 664
MACRUM 0:276e7a263c35 665 char *sn_nsdl_get_version(void)
MACRUM 0:276e7a263c35 666 {
MACRUM 0:276e7a263c35 667 #if defined(YOTTA_MBED_CLIENT_C_VERSION_STRING)
MACRUM 0:276e7a263c35 668 return YOTTA_MBED_CLIENT_C_VERSION_STRING;
MACRUM 0:276e7a263c35 669 #elif defined(VERSION)
MACRUM 0:276e7a263c35 670 return VERSION;
MACRUM 0:276e7a263c35 671 #else
MACRUM 0:276e7a263c35 672 return "0.0.0";
MACRUM 0:276e7a263c35 673 #endif
MACRUM 0:276e7a263c35 674 }
MACRUM 0:276e7a263c35 675
MACRUM 0:276e7a263c35 676 int8_t sn_nsdl_process_coap(struct nsdl_s *handle, uint8_t *packet_ptr, uint16_t packet_len, sn_nsdl_addr_s *src_ptr)
MACRUM 0:276e7a263c35 677 {
MACRUM 0:276e7a263c35 678 sn_coap_hdr_s *coap_packet_ptr = NULL;
MACRUM 0:276e7a263c35 679 sn_coap_hdr_s *coap_response_ptr = NULL;
MACRUM 0:276e7a263c35 680 sn_nsdl_dynamic_resource_parameters_s *resource = NULL;
MACRUM 0:276e7a263c35 681 /* Check parameters */
MACRUM 0:276e7a263c35 682 if (handle == NULL) {
MACRUM 0:276e7a263c35 683 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 684 }
MACRUM 0:276e7a263c35 685
MACRUM 0:276e7a263c35 686 /* Parse CoAP packet */
MACRUM 0:276e7a263c35 687 coap_packet_ptr = sn_coap_protocol_parse(handle->grs->coap, src_ptr, packet_len, packet_ptr, (void *)handle);
MACRUM 0:276e7a263c35 688
MACRUM 0:276e7a263c35 689 /* Check if parsing was successfull */
MACRUM 0:276e7a263c35 690 if (coap_packet_ptr == (sn_coap_hdr_s *)NULL) {
MACRUM 0:276e7a263c35 691 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 692 }
MACRUM 0:276e7a263c35 693
MACRUM 0:276e7a263c35 694 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
MACRUM 0:276e7a263c35 695 // Pass block to application if external_memory_block is set
MACRUM 0:276e7a263c35 696 if((coap_packet_ptr->options_list_ptr &&
MACRUM 0:276e7a263c35 697 coap_packet_ptr->options_list_ptr->block1 != -1) &&
MACRUM 0:276e7a263c35 698 (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING ||
MACRUM 0:276e7a263c35 699 coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED)) {
MACRUM 0:276e7a263c35 700 // Block 1 handling
MACRUM 0:276e7a263c35 701 /* Get resource */
MACRUM 0:276e7a263c35 702 char* path = handle->sn_nsdl_alloc(coap_packet_ptr->uri_path_len + 1);
MACRUM 0:276e7a263c35 703 if (!path) {
MACRUM 0:276e7a263c35 704 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 705 }
MACRUM 0:276e7a263c35 706
MACRUM 0:276e7a263c35 707 memcpy(path,
MACRUM 0:276e7a263c35 708 coap_packet_ptr->uri_path_ptr,
MACRUM 0:276e7a263c35 709 coap_packet_ptr->uri_path_len);
MACRUM 0:276e7a263c35 710 path[coap_packet_ptr->uri_path_len] = '\0';
MACRUM 0:276e7a263c35 711
MACRUM 0:276e7a263c35 712
MACRUM 0:276e7a263c35 713 resource = sn_nsdl_get_resource(handle, path);
MACRUM 0:276e7a263c35 714 handle->sn_nsdl_free(path);
MACRUM 0:276e7a263c35 715
MACRUM 0:276e7a263c35 716 if (coap_packet_ptr->options_list_ptr) {
MACRUM 0:276e7a263c35 717 if(resource &&
MACRUM 0:276e7a263c35 718 resource->static_resource_parameters->external_memory_block &&
MACRUM 0:276e7a263c35 719 coap_packet_ptr->options_list_ptr->block1) {
MACRUM 0:276e7a263c35 720
MACRUM 0:276e7a263c35 721 uint32_t block_number = coap_packet_ptr->options_list_ptr->block1 >> 4;
MACRUM 0:276e7a263c35 722 if (block_number) {
MACRUM 0:276e7a263c35 723 remove_previous_block_data(handle, src_ptr, block_number);
MACRUM 0:276e7a263c35 724 }
MACRUM 0:276e7a263c35 725
MACRUM 0:276e7a263c35 726 // Whole message received --> pass only the last block data to application
MACRUM 0:276e7a263c35 727 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
MACRUM 0:276e7a263c35 728 // Get the block size
MACRUM 0:276e7a263c35 729 uint8_t temp = (coap_packet_ptr->options_list_ptr->block1 & 0x07);
MACRUM 0:276e7a263c35 730 uint16_t block_size = 1u << (temp + 4);
MACRUM 0:276e7a263c35 731
MACRUM 0:276e7a263c35 732 uint32_t new_payload_len = coap_packet_ptr->payload_len - block_size;
MACRUM 0:276e7a263c35 733 uint8_t *temp_ptr = handle->grs->coap->sn_coap_protocol_malloc(new_payload_len);
MACRUM 0:276e7a263c35 734 if (temp_ptr) {
MACRUM 0:276e7a263c35 735 // Skip the second last block data since it's still stored in mbed-coap list!
MACRUM 0:276e7a263c35 736 memcpy(temp_ptr, coap_packet_ptr->payload_ptr + block_size, new_payload_len);
MACRUM 0:276e7a263c35 737 handle->grs->coap->sn_coap_protocol_free(coap_packet_ptr->payload_ptr);
MACRUM 0:276e7a263c35 738 coap_packet_ptr->payload_ptr = NULL;
MACRUM 0:276e7a263c35 739
MACRUM 0:276e7a263c35 740 coap_packet_ptr->payload_ptr = handle->grs->coap->sn_coap_protocol_malloc(new_payload_len);
MACRUM 0:276e7a263c35 741 if (coap_packet_ptr->payload_ptr) {
MACRUM 0:276e7a263c35 742 memcpy(coap_packet_ptr->payload_ptr, temp_ptr, new_payload_len);
MACRUM 0:276e7a263c35 743 coap_packet_ptr->payload_len = new_payload_len;
MACRUM 0:276e7a263c35 744 }
MACRUM 0:276e7a263c35 745
MACRUM 0:276e7a263c35 746 handle->grs->coap->sn_coap_protocol_free(temp_ptr);
MACRUM 0:276e7a263c35 747 }
MACRUM 0:276e7a263c35 748 }
MACRUM 0:276e7a263c35 749 } else {
MACRUM 0:276e7a263c35 750 resource = NULL;
MACRUM 0:276e7a263c35 751 }
MACRUM 0:276e7a263c35 752 } else {
MACRUM 0:276e7a263c35 753 resource = NULL;
MACRUM 0:276e7a263c35 754 }
MACRUM 0:276e7a263c35 755 }
MACRUM 0:276e7a263c35 756 #endif
MACRUM 0:276e7a263c35 757
MACRUM 0:276e7a263c35 758 sn_nsdl_print_coap_data(coap_packet_ptr, false);
MACRUM 0:276e7a263c35 759
MACRUM 0:276e7a263c35 760 // Handling of GET responses
MACRUM 0:276e7a263c35 761 if (coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_CONTENT) {
MACRUM 0:276e7a263c35 762 bool data_updated = false;
MACRUM 0:276e7a263c35 763 if (coap_packet_ptr->options_list_ptr && coap_packet_ptr->options_list_ptr->block2 != -1) {
MACRUM 0:276e7a263c35 764 uint32_t block_number = coap_packet_ptr->options_list_ptr->block2 >> 4;
MACRUM 0:276e7a263c35 765 if (block_number) {
MACRUM 0:276e7a263c35 766 remove_previous_block_data(handle, src_ptr, block_number);
MACRUM 0:276e7a263c35 767 }
MACRUM 0:276e7a263c35 768
MACRUM 0:276e7a263c35 769 // Modify payload to have only last received block data
MACRUM 0:276e7a263c35 770 data_updated = update_last_block_data(handle, coap_packet_ptr, false);
MACRUM 0:276e7a263c35 771 }
MACRUM 0:276e7a263c35 772
MACRUM 0:276e7a263c35 773 handle->sn_nsdl_rx_callback(handle, coap_packet_ptr, src_ptr);
MACRUM 0:276e7a263c35 774 if (data_updated) {
MACRUM 0:276e7a263c35 775 handle->grs->coap->sn_coap_protocol_free(coap_packet_ptr->payload_ptr);
MACRUM 0:276e7a263c35 776 }
MACRUM 0:276e7a263c35 777
MACRUM 0:276e7a263c35 778 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
MACRUM 0:276e7a263c35 779 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 780 }
MACRUM 0:276e7a263c35 781
MACRUM 0:276e7a263c35 782 /* Check, if coap itself sends response, or block receiving is ongoing... */
MACRUM 0:276e7a263c35 783 if (coap_packet_ptr->coap_status != COAP_STATUS_OK &&
MACRUM 0:276e7a263c35 784 coap_packet_ptr->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED &&
MACRUM 0:276e7a263c35 785 coap_packet_ptr &&
MACRUM 0:276e7a263c35 786 !resource) {
MACRUM 0:276e7a263c35 787 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
MACRUM 0:276e7a263c35 788 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 789 }
MACRUM 0:276e7a263c35 790
MACRUM 0:276e7a263c35 791 /* If proxy options added, return not supported */
MACRUM 0:276e7a263c35 792 if (coap_packet_ptr->options_list_ptr) {
MACRUM 0:276e7a263c35 793 if (coap_packet_ptr->options_list_ptr->proxy_uri_len) {
MACRUM 0:276e7a263c35 794 coap_response_ptr = sn_coap_build_response(handle->grs->coap, coap_packet_ptr, COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED);
MACRUM 0:276e7a263c35 795 if (coap_response_ptr) {
MACRUM 0:276e7a263c35 796 sn_nsdl_send_coap_message(handle, src_ptr, coap_response_ptr);
MACRUM 0:276e7a263c35 797 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_response_ptr);
MACRUM 0:276e7a263c35 798 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
MACRUM 0:276e7a263c35 799 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 800 } else {
MACRUM 0:276e7a263c35 801 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
MACRUM 0:276e7a263c35 802 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 803 }
MACRUM 0:276e7a263c35 804 }
MACRUM 0:276e7a263c35 805 }
MACRUM 0:276e7a263c35 806
MACRUM 0:276e7a263c35 807 /* * * * * * * * * * * * * * * * * * * * * * * * * * */
MACRUM 0:276e7a263c35 808 /* If message is response message, call RX callback */
MACRUM 0:276e7a263c35 809 /* * * * * * * * * * * * * * * * * * * * * * * * * * */
MACRUM 0:276e7a263c35 810
MACRUM 0:276e7a263c35 811 if (((coap_packet_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) ||
MACRUM 0:276e7a263c35 812 (coap_packet_ptr->msg_type >= COAP_MSG_TYPE_ACKNOWLEDGEMENT))) {
MACRUM 0:276e7a263c35 813 int8_t retval = sn_nsdl_local_rx_function(handle, coap_packet_ptr, src_ptr);
MACRUM 0:276e7a263c35 814 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED &&
MACRUM 0:276e7a263c35 815 coap_packet_ptr->payload_ptr) {
MACRUM 0:276e7a263c35 816 handle->sn_nsdl_free(coap_packet_ptr->payload_ptr);
MACRUM 0:276e7a263c35 817 coap_packet_ptr->payload_ptr = 0;
MACRUM 0:276e7a263c35 818 }
MACRUM 0:276e7a263c35 819 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
MACRUM 0:276e7a263c35 820 return retval;
MACRUM 0:276e7a263c35 821 }
MACRUM 0:276e7a263c35 822 #ifndef MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 823 /* * If OMA bootstrap message... * */
MACRUM 0:276e7a263c35 824 bool bootstrap_msg = src_ptr && (handle->oma_bs_address_len == src_ptr->addr_len) &&
MACRUM 0:276e7a263c35 825 (handle->oma_bs_port == src_ptr->port) &&
MACRUM 0:276e7a263c35 826 !memcmp(handle->oma_bs_address_ptr, src_ptr->addr_ptr, handle->oma_bs_address_len);
MACRUM 0:276e7a263c35 827
MACRUM 0:276e7a263c35 828 // Pass bootstrap data to application
MACRUM 0:276e7a263c35 829 if (bootstrap_msg) {
MACRUM 0:276e7a263c35 830 handle->sn_nsdl_rx_callback(handle, coap_packet_ptr,src_ptr);
MACRUM 0:276e7a263c35 831 if (coap_packet_ptr &&
MACRUM 0:276e7a263c35 832 coap_packet_ptr->options_list_ptr &&
MACRUM 0:276e7a263c35 833 coap_packet_ptr->coap_status != COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED &&
MACRUM 0:276e7a263c35 834 coap_packet_ptr->options_list_ptr->block1 != -1) {
MACRUM 0:276e7a263c35 835 handle->sn_nsdl_free(coap_packet_ptr->payload_ptr);
MACRUM 0:276e7a263c35 836 coap_packet_ptr->payload_ptr = NULL;
MACRUM 0:276e7a263c35 837 }
MACRUM 0:276e7a263c35 838 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
MACRUM 0:276e7a263c35 839 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 840 }
MACRUM 0:276e7a263c35 841 #endif //MBED_CLIENT_DISABLE_BOOTSTRAP_FEATURE
MACRUM 0:276e7a263c35 842
MACRUM 0:276e7a263c35 843 /* * * * * * * * * * * * * * * */
MACRUM 0:276e7a263c35 844 /* Other messages are for GRS */
MACRUM 0:276e7a263c35 845 /* * * * * * * * * * * * * * * */
MACRUM 0:276e7a263c35 846 return sn_grs_process_coap(handle, coap_packet_ptr, src_ptr);
MACRUM 0:276e7a263c35 847 }
MACRUM 0:276e7a263c35 848
MACRUM 0:276e7a263c35 849 int8_t sn_nsdl_exec(struct nsdl_s *handle, uint32_t time)
MACRUM 0:276e7a263c35 850 {
MACRUM 0:276e7a263c35 851 if(!handle || !handle->grs){
MACRUM 0:276e7a263c35 852 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 853 }
MACRUM 0:276e7a263c35 854 /* Call CoAP execution function */
MACRUM 0:276e7a263c35 855 return sn_coap_protocol_exec(handle->grs->coap, time);
MACRUM 0:276e7a263c35 856 }
MACRUM 0:276e7a263c35 857
MACRUM 0:276e7a263c35 858 sn_nsdl_dynamic_resource_parameters_s *sn_nsdl_get_resource(struct nsdl_s *handle, const char *path_ptr)
MACRUM 0:276e7a263c35 859 {
MACRUM 0:276e7a263c35 860 /* Check parameters */
MACRUM 0:276e7a263c35 861 if (handle == NULL) {
MACRUM 0:276e7a263c35 862 return NULL;
MACRUM 0:276e7a263c35 863 }
MACRUM 0:276e7a263c35 864
MACRUM 0:276e7a263c35 865 return sn_grs_search_resource(handle->grs, path_ptr, SN_GRS_SEARCH_METHOD);
MACRUM 0:276e7a263c35 866 }
MACRUM 0:276e7a263c35 867
MACRUM 0:276e7a263c35 868
MACRUM 0:276e7a263c35 869 /**
MACRUM 0:276e7a263c35 870 * \fn static int32_t sn_nsdl_internal_coap_send(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description)
MACRUM 0:276e7a263c35 871 *
MACRUM 0:276e7a263c35 872 *
MACRUM 0:276e7a263c35 873 * \brief To send NSDL messages. Stores message id?s and message description to catch response from NSP server
MACRUM 0:276e7a263c35 874 * \param *handle Pointer to nsdl-library handle
MACRUM 0:276e7a263c35 875 * \param *coap_header_ptr Pointer to the CoAP message header to be sent
MACRUM 0:276e7a263c35 876 * \param *dst_addr_ptr Pointer to the address structure that contains destination address information
MACRUM 0:276e7a263c35 877 * \param message_description Message description to be stored to list for waiting response
MACRUM 0:276e7a263c35 878 *
MACRUM 0:276e7a263c35 879 * \return message id, <=0 if failed
MACRUM 0:276e7a263c35 880 */
MACRUM 0:276e7a263c35 881 static int32_t sn_nsdl_internal_coap_send(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description)
MACRUM 0:276e7a263c35 882 {
MACRUM 0:276e7a263c35 883
MACRUM 0:276e7a263c35 884 tr_debug("sn_nsdl_internal_coap_send");
MACRUM 0:276e7a263c35 885 uint8_t *coap_message_ptr = NULL;
MACRUM 0:276e7a263c35 886 int32_t coap_message_len = 0;
MACRUM 0:276e7a263c35 887 uint16_t coap_header_len = 0;
MACRUM 0:276e7a263c35 888
MACRUM 0:276e7a263c35 889 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE /* If Message blockwising is not used at all, this part of code will not be compiled */
MACRUM 0:276e7a263c35 890 int8_t ret_val = prepare_blockwise_message(handle->grs->coap, coap_header_ptr);
MACRUM 0:276e7a263c35 891 if( 0 != ret_val ) {
MACRUM 0:276e7a263c35 892 return 0;
MACRUM 0:276e7a263c35 893 }
MACRUM 0:276e7a263c35 894 #endif
MACRUM 0:276e7a263c35 895
MACRUM 0:276e7a263c35 896 coap_message_len = sn_coap_builder_calc_needed_packet_data_size_2(coap_header_ptr, handle->grs->coap->sn_coap_block_data_size);
MACRUM 0:276e7a263c35 897 tr_debug("sn_nsdl_internal_coap_send - msg len after calc: %" PRId32 "", coap_message_len);
MACRUM 0:276e7a263c35 898 if (coap_message_len == 0) {
MACRUM 0:276e7a263c35 899 return 0;
MACRUM 0:276e7a263c35 900 }
MACRUM 0:276e7a263c35 901
MACRUM 0:276e7a263c35 902 coap_message_ptr = handle->sn_nsdl_alloc(coap_message_len);
MACRUM 0:276e7a263c35 903 if (!coap_message_ptr) {
MACRUM 0:276e7a263c35 904 return 0;
MACRUM 0:276e7a263c35 905 }
MACRUM 0:276e7a263c35 906
MACRUM 0:276e7a263c35 907 coap_header_len = coap_header_ptr->payload_len;
MACRUM 0:276e7a263c35 908
MACRUM 0:276e7a263c35 909 /* Build message */
MACRUM 0:276e7a263c35 910 int16_t ret = sn_coap_protocol_build(handle->grs->coap, dst_addr_ptr, coap_message_ptr, coap_header_ptr, (void *)handle);
MACRUM 0:276e7a263c35 911 if (ret < 0) {
MACRUM 0:276e7a263c35 912 handle->sn_nsdl_free(coap_message_ptr);
MACRUM 0:276e7a263c35 913 return ret;
MACRUM 0:276e7a263c35 914 }
MACRUM 0:276e7a263c35 915
MACRUM 0:276e7a263c35 916 /* If message type is confirmable, save it to list to wait for reply */
MACRUM 0:276e7a263c35 917 if (coap_header_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
MACRUM 0:276e7a263c35 918 if (message_description == SN_NSDL_MSG_REGISTER) {
MACRUM 0:276e7a263c35 919 handle->register_msg_id = coap_header_ptr->msg_id;
MACRUM 0:276e7a263c35 920 handle->register_msg_len = coap_header_len;
MACRUM 0:276e7a263c35 921 }
MACRUM 0:276e7a263c35 922 else if (message_description == SN_NSDL_MSG_UNREGISTER) {
MACRUM 0:276e7a263c35 923 handle->unregister_msg_id = coap_header_ptr->msg_id;
MACRUM 0:276e7a263c35 924 }
MACRUM 0:276e7a263c35 925 else if (message_description == SN_NSDL_MSG_UPDATE) {
MACRUM 0:276e7a263c35 926 handle->update_register_msg_id = coap_header_ptr->msg_id;
MACRUM 0:276e7a263c35 927 handle->update_register_msg_len = coap_header_len;
MACRUM 0:276e7a263c35 928 }
MACRUM 0:276e7a263c35 929 else if (message_description == SN_NSDL_MSG_BOOTSTRAP) {
MACRUM 0:276e7a263c35 930 handle->bootstrap_msg_id = coap_header_ptr->msg_id;
MACRUM 0:276e7a263c35 931 }
MACRUM 0:276e7a263c35 932 }
MACRUM 0:276e7a263c35 933 sn_nsdl_print_coap_data(coap_header_ptr, true);
MACRUM 0:276e7a263c35 934 handle->sn_nsdl_tx_callback(handle, SN_NSDL_PROTOCOL_COAP, coap_message_ptr, coap_message_len, dst_addr_ptr);
MACRUM 0:276e7a263c35 935 handle->sn_nsdl_free(coap_message_ptr);
MACRUM 0:276e7a263c35 936
MACRUM 0:276e7a263c35 937 return coap_header_ptr->msg_id;
MACRUM 0:276e7a263c35 938 }
MACRUM 0:276e7a263c35 939
MACRUM 0:276e7a263c35 940 /**
MACRUM 0:276e7a263c35 941 * \fn static void sn_nsdl_resolve_nsp_address(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 942 *
MACRUM 0:276e7a263c35 943 * \brief Resolves NSP server address.
MACRUM 0:276e7a263c35 944 *
MACRUM 0:276e7a263c35 945 * \param *handle Pointer to nsdl-library handle
MACRUM 0:276e7a263c35 946 * \note Application must set NSP address with set_nsp_address
MACRUM 0:276e7a263c35 947 */
MACRUM 0:276e7a263c35 948 static void sn_nsdl_resolve_nsp_address(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 949 {
MACRUM 0:276e7a263c35 950 /* Local variables */
MACRUM 0:276e7a263c35 951 if (!handle->nsp_address_ptr) {
MACRUM 0:276e7a263c35 952 //allocate only if previously not allocated
MACRUM 0:276e7a263c35 953 handle->nsp_address_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_oma_server_info_t));
MACRUM 0:276e7a263c35 954 }
MACRUM 0:276e7a263c35 955
MACRUM 0:276e7a263c35 956 if (handle->nsp_address_ptr) {
MACRUM 0:276e7a263c35 957 handle->nsp_address_ptr->omalw_server_security = SEC_NOT_SET;
MACRUM 0:276e7a263c35 958 handle->nsp_address_ptr->omalw_address_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_addr_s));
MACRUM 0:276e7a263c35 959 if (handle->nsp_address_ptr->omalw_address_ptr) {
MACRUM 0:276e7a263c35 960 memset(handle->nsp_address_ptr->omalw_address_ptr, 0, sizeof(sn_nsdl_addr_s));
MACRUM 0:276e7a263c35 961 handle->nsp_address_ptr->omalw_address_ptr->type = SN_NSDL_ADDRESS_TYPE_NONE;
MACRUM 0:276e7a263c35 962 }
MACRUM 0:276e7a263c35 963 }
MACRUM 0:276e7a263c35 964 }
MACRUM 0:276e7a263c35 965
MACRUM 0:276e7a263c35 966 #ifdef RESOURCE_ATTRIBUTES_LIST
MACRUM 0:276e7a263c35 967 static char *sn_nsdl_build_resource_attribute_str(char *dst, const sn_nsdl_attribute_item_s *attribute, const char *name, const size_t name_len)
MACRUM 0:276e7a263c35 968 {
MACRUM 0:276e7a263c35 969 if (attribute != NULL && name != NULL && name_len > 0 && attribute->value) {
MACRUM 0:276e7a263c35 970 size_t attribute_len = strlen(attribute->value);
MACRUM 0:276e7a263c35 971 *dst++ = ';';
MACRUM 0:276e7a263c35 972 memcpy(dst, name, name_len);
MACRUM 0:276e7a263c35 973 dst += name_len;
MACRUM 0:276e7a263c35 974 *dst++ = '"';
MACRUM 0:276e7a263c35 975 memcpy(dst,
MACRUM 0:276e7a263c35 976 attribute->value,
MACRUM 0:276e7a263c35 977 attribute_len);
MACRUM 0:276e7a263c35 978 dst += attribute_len;
MACRUM 0:276e7a263c35 979 *dst++ = '"';
MACRUM 0:276e7a263c35 980 }
MACRUM 0:276e7a263c35 981 return dst;
MACRUM 0:276e7a263c35 982 }
MACRUM 0:276e7a263c35 983 #endif
MACRUM 0:276e7a263c35 984
MACRUM 0:276e7a263c35 985 /**
MACRUM 0:276e7a263c35 986 * \fn int8_t sn_nsdl_build_registration_body(struct nsdl_s *handle, sn_coap_hdr_s *message_ptr, uint8_t updating_registeration)
MACRUM 0:276e7a263c35 987 *
MACRUM 0:276e7a263c35 988 * \brief To build GRS resources to registration message payload
MACRUM 0:276e7a263c35 989 * \param *handle Pointer to nsdl-library handle
MACRUM 0:276e7a263c35 990 * \param *message_ptr Pointer to CoAP message header
MACRUM 0:276e7a263c35 991 *
MACRUM 0:276e7a263c35 992 * \return SN_NSDL_SUCCESS = 0, Failed = -1
MACRUM 0:276e7a263c35 993 */
MACRUM 0:276e7a263c35 994 int8_t sn_nsdl_build_registration_body(struct nsdl_s *handle, sn_coap_hdr_s *message_ptr, uint8_t updating_registeration)
MACRUM 0:276e7a263c35 995 {
MACRUM 0:276e7a263c35 996 tr_debug("sn_nsdl_build_registration_body");
MACRUM 0:276e7a263c35 997 /* Local variables */
MACRUM 0:276e7a263c35 998 uint8_t *temp_ptr;
MACRUM 0:276e7a263c35 999 sn_nsdl_dynamic_resource_parameters_s *resource_temp_ptr;
MACRUM 0:276e7a263c35 1000
MACRUM 0:276e7a263c35 1001 /* Calculate needed memory and allocate */
MACRUM 0:276e7a263c35 1002 int8_t error = 0;
MACRUM 0:276e7a263c35 1003 uint16_t msg_len = sn_nsdl_calculate_registration_body_size(handle, updating_registeration, &error);
MACRUM 0:276e7a263c35 1004 if (SN_NSDL_FAILURE == error) {
MACRUM 0:276e7a263c35 1005 return error;
MACRUM 0:276e7a263c35 1006 }
MACRUM 0:276e7a263c35 1007
MACRUM 0:276e7a263c35 1008 if (!msg_len) {
MACRUM 0:276e7a263c35 1009 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 1010 } else {
MACRUM 0:276e7a263c35 1011 message_ptr->payload_len = msg_len;
MACRUM 0:276e7a263c35 1012 }
MACRUM 0:276e7a263c35 1013 tr_debug("sn_nsdl_build_registration_body - body size: [%d]", message_ptr->payload_len);
MACRUM 0:276e7a263c35 1014 message_ptr->payload_ptr = handle->sn_nsdl_alloc(message_ptr->payload_len);
MACRUM 0:276e7a263c35 1015 if (!message_ptr->payload_ptr) {
MACRUM 0:276e7a263c35 1016 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1017 }
MACRUM 0:276e7a263c35 1018
MACRUM 0:276e7a263c35 1019 /* Build message */
MACRUM 0:276e7a263c35 1020 temp_ptr = message_ptr->payload_ptr;
MACRUM 0:276e7a263c35 1021
MACRUM 0:276e7a263c35 1022 resource_temp_ptr = sn_grs_get_first_resource(handle->grs);
MACRUM 0:276e7a263c35 1023
MACRUM 0:276e7a263c35 1024 /* Loop trough all resources */
MACRUM 0:276e7a263c35 1025 while (resource_temp_ptr) {
MACRUM 0:276e7a263c35 1026 /* if resource needs to be registered */
MACRUM 0:276e7a263c35 1027 if (resource_temp_ptr->publish_uri) {
MACRUM 0:276e7a263c35 1028 if (updating_registeration && resource_temp_ptr->registered == SN_NDSL_RESOURCE_REGISTERED) {
MACRUM 0:276e7a263c35 1029 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
MACRUM 0:276e7a263c35 1030 continue;
MACRUM 0:276e7a263c35 1031 } else {
MACRUM 0:276e7a263c35 1032 resource_temp_ptr->registered = SN_NDSL_RESOURCE_REGISTERED;
MACRUM 0:276e7a263c35 1033 }
MACRUM 0:276e7a263c35 1034
MACRUM 0:276e7a263c35 1035 /* If not first resource, add '.' to separator */
MACRUM 0:276e7a263c35 1036 if (temp_ptr != message_ptr->payload_ptr) {
MACRUM 0:276e7a263c35 1037 *temp_ptr++ = ',';
MACRUM 0:276e7a263c35 1038 }
MACRUM 0:276e7a263c35 1039
MACRUM 0:276e7a263c35 1040 *temp_ptr++ = '<';
MACRUM 0:276e7a263c35 1041 *temp_ptr++ = '/';
MACRUM 0:276e7a263c35 1042 size_t path_len = 0;
MACRUM 0:276e7a263c35 1043 if (resource_temp_ptr->static_resource_parameters->path) {
MACRUM 0:276e7a263c35 1044 path_len = strlen(resource_temp_ptr->static_resource_parameters->path);
MACRUM 0:276e7a263c35 1045 }
MACRUM 0:276e7a263c35 1046 memcpy(temp_ptr,
MACRUM 0:276e7a263c35 1047 resource_temp_ptr->static_resource_parameters->path,
MACRUM 0:276e7a263c35 1048 path_len);
MACRUM 0:276e7a263c35 1049 temp_ptr += path_len;
MACRUM 0:276e7a263c35 1050 *temp_ptr++ = '>';
MACRUM 0:276e7a263c35 1051
MACRUM 0:276e7a263c35 1052 /* Resource attributes */
MACRUM 0:276e7a263c35 1053 #ifndef RESOURCE_ATTRIBUTES_LIST
MACRUM 0:276e7a263c35 1054 #ifndef DISABLE_RESOURCE_TYPE
MACRUM 0:276e7a263c35 1055 size_t resource_type_len = 0;
MACRUM 0:276e7a263c35 1056 if (resource_temp_ptr->static_resource_parameters->resource_type_ptr) {
MACRUM 0:276e7a263c35 1057 resource_type_len = strlen(resource_temp_ptr->static_resource_parameters->resource_type_ptr);
MACRUM 0:276e7a263c35 1058 }
MACRUM 0:276e7a263c35 1059 if (resource_type_len) {
MACRUM 0:276e7a263c35 1060 *temp_ptr++ = ';';
MACRUM 0:276e7a263c35 1061 memcpy(temp_ptr, resource_type_parameter, RT_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1062 temp_ptr += RT_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1063 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1064 memcpy(temp_ptr,
MACRUM 0:276e7a263c35 1065 resource_temp_ptr->static_resource_parameters->resource_type_ptr,
MACRUM 0:276e7a263c35 1066 resource_type_len);
MACRUM 0:276e7a263c35 1067 temp_ptr += resource_type_len;
MACRUM 0:276e7a263c35 1068 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1069 }
MACRUM 0:276e7a263c35 1070 #endif
MACRUM 0:276e7a263c35 1071 #ifndef DISABLE_INTERFACE_DESCRIPTION
MACRUM 0:276e7a263c35 1072 size_t interface_description_len = 0;
MACRUM 0:276e7a263c35 1073 if (resource_temp_ptr->static_resource_parameters->interface_description_ptr) {
MACRUM 0:276e7a263c35 1074 interface_description_len = strlen(resource_temp_ptr->static_resource_parameters->interface_description_ptr);
MACRUM 0:276e7a263c35 1075 }
MACRUM 0:276e7a263c35 1076
MACRUM 0:276e7a263c35 1077 if (interface_description_len) {
MACRUM 0:276e7a263c35 1078 *temp_ptr++ = ';';
MACRUM 0:276e7a263c35 1079 memcpy(temp_ptr, if_description_parameter, IF_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1080 temp_ptr += IF_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1081 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1082 memcpy(temp_ptr,
MACRUM 0:276e7a263c35 1083 resource_temp_ptr->static_resource_parameters->interface_description_ptr,
MACRUM 0:276e7a263c35 1084 interface_description_len);
MACRUM 0:276e7a263c35 1085 temp_ptr += interface_description_len;
MACRUM 0:276e7a263c35 1086 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1087 }
MACRUM 0:276e7a263c35 1088 #endif
MACRUM 0:276e7a263c35 1089 #else
MACRUM 0:276e7a263c35 1090 size_t attribute_len = 0;
MACRUM 0:276e7a263c35 1091 if (resource_temp_ptr->static_resource_parameters->attributes_ptr) {
MACRUM 0:276e7a263c35 1092 sn_nsdl_attribute_item_s *attribute = resource_temp_ptr->static_resource_parameters->attributes_ptr;
MACRUM 0:276e7a263c35 1093 while (attribute->attribute_name != ATTR_END) {
MACRUM 0:276e7a263c35 1094 switch (attribute->attribute_name) {
MACRUM 0:276e7a263c35 1095 case ATTR_RESOURCE_TYPE:
MACRUM 0:276e7a263c35 1096 temp_ptr = sn_nsdl_build_resource_attribute_str(temp_ptr, attribute, resource_type_parameter, RT_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1097 break;
MACRUM 0:276e7a263c35 1098 case ATTR_INTERFACE_DESCRIPTION:
MACRUM 0:276e7a263c35 1099 temp_ptr = sn_nsdl_build_resource_attribute_str(temp_ptr, attribute, if_description_parameter, IF_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1100 break;
MACRUM 0:276e7a263c35 1101 case ATTR_ENDPOINT_NAME:
MACRUM 0:276e7a263c35 1102 temp_ptr = sn_nsdl_build_resource_attribute_str(temp_ptr, attribute, name_parameter, NAME_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1103 break;
MACRUM 0:276e7a263c35 1104 default:
MACRUM 0:276e7a263c35 1105 break;
MACRUM 0:276e7a263c35 1106 }
MACRUM 0:276e7a263c35 1107 attribute++;
MACRUM 0:276e7a263c35 1108 }
MACRUM 0:276e7a263c35 1109 }
MACRUM 0:276e7a263c35 1110 #endif
MACRUM 0:276e7a263c35 1111 if (resource_temp_ptr->coap_content_type != 0) {
MACRUM 0:276e7a263c35 1112 *temp_ptr++ = ';';
MACRUM 0:276e7a263c35 1113 memcpy(temp_ptr, coap_con_type_parameter, COAP_CON_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1114 temp_ptr += COAP_CON_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1115 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1116 temp_ptr = sn_nsdl_itoa(temp_ptr,
MACRUM 0:276e7a263c35 1117 resource_temp_ptr->coap_content_type);
MACRUM 0:276e7a263c35 1118 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1119 }
MACRUM 0:276e7a263c35 1120
MACRUM 0:276e7a263c35 1121 /* ;aobs / ;obs */
MACRUM 0:276e7a263c35 1122 // This needs to be re-visited and may be need an API for maganging obs value for different server implementation
MACRUM 0:276e7a263c35 1123 #ifndef COAP_DISABLE_OBS_FEATURE
MACRUM 0:276e7a263c35 1124 if (resource_temp_ptr->auto_observable) {
MACRUM 0:276e7a263c35 1125 uint8_t token[MAX_TOKEN_SIZE] = {0};
MACRUM 0:276e7a263c35 1126 uint8_t len = handle->sn_nsdl_auto_obs_token_callback(handle,
MACRUM 0:276e7a263c35 1127 resource_temp_ptr->static_resource_parameters->path,
MACRUM 0:276e7a263c35 1128 (uint8_t*)token);
MACRUM 0:276e7a263c35 1129 if (len > 0) {
MACRUM 0:276e7a263c35 1130 *temp_ptr++ = ';';
MACRUM 0:276e7a263c35 1131 memcpy(temp_ptr, aobs_parameter, AOBS_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1132 temp_ptr += AOBS_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1133 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1134 uint16_t temp = common_read_16_bit((uint8_t*)token);
MACRUM 0:276e7a263c35 1135 temp_ptr = sn_nsdl_itoa(temp_ptr, temp);
MACRUM 0:276e7a263c35 1136 *temp_ptr++ = '"';
MACRUM 0:276e7a263c35 1137 } else {
MACRUM 0:276e7a263c35 1138 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1139 }
MACRUM 0:276e7a263c35 1140 }
MACRUM 0:276e7a263c35 1141 else if (resource_temp_ptr->observable) {
MACRUM 0:276e7a263c35 1142 *temp_ptr++ = ';';
MACRUM 0:276e7a263c35 1143 memcpy(temp_ptr, obs_parameter, OBS_PARAMETER_LEN);
MACRUM 0:276e7a263c35 1144 temp_ptr += OBS_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1145 }
MACRUM 0:276e7a263c35 1146 #endif
MACRUM 0:276e7a263c35 1147 }
MACRUM 0:276e7a263c35 1148 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
MACRUM 0:276e7a263c35 1149
MACRUM 0:276e7a263c35 1150 }
MACRUM 0:276e7a263c35 1151 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 1152 }
MACRUM 0:276e7a263c35 1153
MACRUM 0:276e7a263c35 1154 /**
MACRUM 0:276e7a263c35 1155 * \fn static uint16_t sn_nsdl_calculate_registration_body_size(struct nsdl_s *handle, uint8_t updating_registeration, int8_t *error)
MACRUM 0:276e7a263c35 1156 *
MACRUM 0:276e7a263c35 1157 *
MACRUM 0:276e7a263c35 1158 * \brief Calculates registration message payload size
MACRUM 0:276e7a263c35 1159 * \param *handle Pointer to nsdl-library handle
MACRUM 0:276e7a263c35 1160 * \param *updating_registeration Pointer to list of GRS resources
MACRUM 0:276e7a263c35 1161 * \param *error Error code, SN_NSDL_SUCCESS or SN_NSDL_FAILURE
MACRUM 0:276e7a263c35 1162 *
MACRUM 0:276e7a263c35 1163 * \return Needed payload size
MACRUM 0:276e7a263c35 1164 */
MACRUM 0:276e7a263c35 1165 static uint16_t sn_nsdl_calculate_registration_body_size(struct nsdl_s *handle, uint8_t updating_registeration, int8_t *error)
MACRUM 0:276e7a263c35 1166 {
MACRUM 0:276e7a263c35 1167 tr_debug("sn_nsdl_calculate_registration_body_size");
MACRUM 0:276e7a263c35 1168 /* Local variables */
MACRUM 0:276e7a263c35 1169 uint16_t return_value = 0;
MACRUM 0:276e7a263c35 1170 *error = SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 1171 const sn_nsdl_dynamic_resource_parameters_s *resource_temp_ptr;
MACRUM 0:276e7a263c35 1172
MACRUM 0:276e7a263c35 1173 /* check pointer */
MACRUM 0:276e7a263c35 1174 resource_temp_ptr = sn_grs_get_first_resource(handle->grs);
MACRUM 0:276e7a263c35 1175
MACRUM 0:276e7a263c35 1176 while (resource_temp_ptr) {
MACRUM 0:276e7a263c35 1177 if (resource_temp_ptr->publish_uri) {
MACRUM 0:276e7a263c35 1178 if (updating_registeration && resource_temp_ptr->registered == SN_NDSL_RESOURCE_REGISTERED) {
MACRUM 0:276e7a263c35 1179 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
MACRUM 0:276e7a263c35 1180 continue;
MACRUM 0:276e7a263c35 1181 }
MACRUM 0:276e7a263c35 1182 /* If not first resource, then '.' will be added */
MACRUM 0:276e7a263c35 1183 if (return_value) {
MACRUM 0:276e7a263c35 1184 if (sn_nsdl_check_uint_overflow(return_value, 1, 0)) {
MACRUM 0:276e7a263c35 1185 return_value++;
MACRUM 0:276e7a263c35 1186 } else {
MACRUM 0:276e7a263c35 1187 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1188 break;
MACRUM 0:276e7a263c35 1189 }
MACRUM 0:276e7a263c35 1190 }
MACRUM 0:276e7a263c35 1191
MACRUM 0:276e7a263c35 1192 /* Count length for the resource path </path> */
MACRUM 0:276e7a263c35 1193 size_t path_len = 0;
MACRUM 0:276e7a263c35 1194 if (resource_temp_ptr->static_resource_parameters->path) {
MACRUM 0:276e7a263c35 1195 path_len = strlen(resource_temp_ptr->static_resource_parameters->path);
MACRUM 0:276e7a263c35 1196 }
MACRUM 0:276e7a263c35 1197
MACRUM 0:276e7a263c35 1198 if (sn_nsdl_check_uint_overflow(return_value, 3, path_len)) {
MACRUM 0:276e7a263c35 1199 return_value += (3 + path_len);
MACRUM 0:276e7a263c35 1200 } else {
MACRUM 0:276e7a263c35 1201 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1202 break;
MACRUM 0:276e7a263c35 1203 }
MACRUM 0:276e7a263c35 1204
MACRUM 0:276e7a263c35 1205 /* Count lengths of the attributes */
MACRUM 0:276e7a263c35 1206 #ifndef RESOURCE_ATTRIBUTES_LIST
MACRUM 0:276e7a263c35 1207 #ifndef DISABLE_RESOURCE_TYPE
MACRUM 0:276e7a263c35 1208 /* Resource type parameter */
MACRUM 0:276e7a263c35 1209 size_t resource_type_len = 0;
MACRUM 0:276e7a263c35 1210 if (resource_temp_ptr->static_resource_parameters->resource_type_ptr) {
MACRUM 0:276e7a263c35 1211 resource_type_len = strlen(resource_temp_ptr->static_resource_parameters->resource_type_ptr);
MACRUM 0:276e7a263c35 1212 }
MACRUM 0:276e7a263c35 1213
MACRUM 0:276e7a263c35 1214 if (resource_type_len) {
MACRUM 0:276e7a263c35 1215 /* ;rt="restype" */
MACRUM 0:276e7a263c35 1216 if (sn_nsdl_check_uint_overflow(return_value,
MACRUM 0:276e7a263c35 1217 6,
MACRUM 0:276e7a263c35 1218 resource_type_len)) {
MACRUM 0:276e7a263c35 1219 return_value += (6 + resource_type_len);
MACRUM 0:276e7a263c35 1220 } else {
MACRUM 0:276e7a263c35 1221 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1222 break;
MACRUM 0:276e7a263c35 1223 }
MACRUM 0:276e7a263c35 1224 }
MACRUM 0:276e7a263c35 1225 #endif
MACRUM 0:276e7a263c35 1226
MACRUM 0:276e7a263c35 1227 #ifndef DISABLE_INTERFACE_DESCRIPTION
MACRUM 0:276e7a263c35 1228 /* Interface description parameter */
MACRUM 0:276e7a263c35 1229 size_t interface_description_len = 0;
MACRUM 0:276e7a263c35 1230 if (resource_temp_ptr->static_resource_parameters->interface_description_ptr) {
MACRUM 0:276e7a263c35 1231 interface_description_len = strlen(resource_temp_ptr->static_resource_parameters->interface_description_ptr);
MACRUM 0:276e7a263c35 1232 }
MACRUM 0:276e7a263c35 1233 if (interface_description_len) {
MACRUM 0:276e7a263c35 1234 /* ;if="iftype" */
MACRUM 0:276e7a263c35 1235 if (sn_nsdl_check_uint_overflow(return_value,
MACRUM 0:276e7a263c35 1236 6,
MACRUM 0:276e7a263c35 1237 interface_description_len)) {
MACRUM 0:276e7a263c35 1238 return_value += (6 + interface_description_len);
MACRUM 0:276e7a263c35 1239 } else {
MACRUM 0:276e7a263c35 1240 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1241 break;
MACRUM 0:276e7a263c35 1242 }
MACRUM 0:276e7a263c35 1243 }
MACRUM 0:276e7a263c35 1244 #endif
MACRUM 0:276e7a263c35 1245 #else
MACRUM 0:276e7a263c35 1246 /* All attributes */
MACRUM 0:276e7a263c35 1247 if (resource_temp_ptr->static_resource_parameters->attributes_ptr) {
MACRUM 0:276e7a263c35 1248 size_t attribute_len = 0;
MACRUM 0:276e7a263c35 1249 size_t attribute_desc_len = 0;
MACRUM 0:276e7a263c35 1250 uint8_t success = 1;
MACRUM 0:276e7a263c35 1251 sn_nsdl_attribute_item_s *item = resource_temp_ptr->static_resource_parameters->attributes_ptr;
MACRUM 0:276e7a263c35 1252 while (item->attribute_name != ATTR_END) {
MACRUM 0:276e7a263c35 1253 switch(item->attribute_name) {
MACRUM 0:276e7a263c35 1254 case ATTR_RESOURCE_TYPE:
MACRUM 0:276e7a263c35 1255 /* ;rt="restype" */
MACRUM 0:276e7a263c35 1256 attribute_desc_len = 6;
MACRUM 0:276e7a263c35 1257 attribute_len = strlen(item->value);
MACRUM 0:276e7a263c35 1258 break;
MACRUM 0:276e7a263c35 1259 case ATTR_INTERFACE_DESCRIPTION:
MACRUM 0:276e7a263c35 1260 /* ;if="iftype" */
MACRUM 0:276e7a263c35 1261 attribute_desc_len = 6;
MACRUM 0:276e7a263c35 1262 attribute_len = strlen(item->value);
MACRUM 0:276e7a263c35 1263 break;
MACRUM 0:276e7a263c35 1264 case ATTR_ENDPOINT_NAME:
MACRUM 0:276e7a263c35 1265 /* ;name="name" */
MACRUM 0:276e7a263c35 1266 attribute_desc_len = 8;
MACRUM 0:276e7a263c35 1267 attribute_len = strlen(item->value);
MACRUM 0:276e7a263c35 1268 break;
MACRUM 0:276e7a263c35 1269 default:
MACRUM 0:276e7a263c35 1270 break;
MACRUM 0:276e7a263c35 1271 }
MACRUM 0:276e7a263c35 1272 if (sn_nsdl_check_uint_overflow(return_value,
MACRUM 0:276e7a263c35 1273 attribute_desc_len,
MACRUM 0:276e7a263c35 1274 attribute_len)) {
MACRUM 0:276e7a263c35 1275 return_value += (attribute_desc_len + attribute_len);
MACRUM 0:276e7a263c35 1276 } else {
MACRUM 0:276e7a263c35 1277 success = 0;
MACRUM 0:276e7a263c35 1278 break;
MACRUM 0:276e7a263c35 1279 }
MACRUM 0:276e7a263c35 1280 item++;
MACRUM 0:276e7a263c35 1281 }
MACRUM 0:276e7a263c35 1282 if (!success) {
MACRUM 0:276e7a263c35 1283 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1284 break;
MACRUM 0:276e7a263c35 1285 }
MACRUM 0:276e7a263c35 1286 }
MACRUM 0:276e7a263c35 1287 #endif
MACRUM 0:276e7a263c35 1288 if (resource_temp_ptr->coap_content_type != 0) {
MACRUM 0:276e7a263c35 1289 /* ;if="content" */
MACRUM 0:276e7a263c35 1290 uint8_t len = sn_nsdl_itoa_len(resource_temp_ptr->coap_content_type);
MACRUM 0:276e7a263c35 1291 if (sn_nsdl_check_uint_overflow(return_value, 6, len)) {
MACRUM 0:276e7a263c35 1292 return_value += (6 + len);
MACRUM 0:276e7a263c35 1293 } else {
MACRUM 0:276e7a263c35 1294 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1295 break;
MACRUM 0:276e7a263c35 1296 }
MACRUM 0:276e7a263c35 1297 }
MACRUM 0:276e7a263c35 1298 #ifndef COAP_DISABLE_OBS_FEATURE
MACRUM 0:276e7a263c35 1299 // Auto obs will take higher priority
MACRUM 0:276e7a263c35 1300 // This needs to be re-visited and may be need an API for maganging obs value for different server implementation
MACRUM 0:276e7a263c35 1301 if (resource_temp_ptr->auto_observable) {
MACRUM 0:276e7a263c35 1302 /* ;aobs="" */
MACRUM 0:276e7a263c35 1303 uint8_t token[MAX_TOKEN_SIZE] = {0};
MACRUM 0:276e7a263c35 1304 uint8_t len = handle->sn_nsdl_auto_obs_token_callback(handle,
MACRUM 0:276e7a263c35 1305 resource_temp_ptr->static_resource_parameters->path,
MACRUM 0:276e7a263c35 1306 (uint8_t*)token);
MACRUM 0:276e7a263c35 1307
MACRUM 0:276e7a263c35 1308 if (len > 0) {
MACRUM 0:276e7a263c35 1309 uint16_t temp = common_read_16_bit((uint8_t*)token);
MACRUM 0:276e7a263c35 1310 uint8_t token_len = sn_nsdl_itoa_len(temp);
MACRUM 0:276e7a263c35 1311 if (sn_nsdl_check_uint_overflow(return_value, 8, token_len)) {
MACRUM 0:276e7a263c35 1312 return_value += (8 + token_len);
MACRUM 0:276e7a263c35 1313 } else {
MACRUM 0:276e7a263c35 1314 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1315 break;
MACRUM 0:276e7a263c35 1316 }
MACRUM 0:276e7a263c35 1317 } else {
MACRUM 0:276e7a263c35 1318 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1319 break;
MACRUM 0:276e7a263c35 1320 }
MACRUM 0:276e7a263c35 1321 }
MACRUM 0:276e7a263c35 1322 else if (resource_temp_ptr->observable) {
MACRUM 0:276e7a263c35 1323 if (sn_nsdl_check_uint_overflow(return_value, 4, 0)) {
MACRUM 0:276e7a263c35 1324 return_value += 4;
MACRUM 0:276e7a263c35 1325 } else {
MACRUM 0:276e7a263c35 1326 *error = SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1327 break;
MACRUM 0:276e7a263c35 1328 }
MACRUM 0:276e7a263c35 1329 }
MACRUM 0:276e7a263c35 1330 #endif
MACRUM 0:276e7a263c35 1331 }
MACRUM 0:276e7a263c35 1332 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
MACRUM 0:276e7a263c35 1333 }
MACRUM 0:276e7a263c35 1334 return return_value;
MACRUM 0:276e7a263c35 1335 }
MACRUM 0:276e7a263c35 1336
MACRUM 0:276e7a263c35 1337 /**
MACRUM 0:276e7a263c35 1338 * \fn static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type)
MACRUM 0:276e7a263c35 1339 *
MACRUM 0:276e7a263c35 1340 *
MACRUM 0:276e7a263c35 1341 * \brief Calculates needed uri query option length
MACRUM 0:276e7a263c35 1342 *
MACRUM 0:276e7a263c35 1343 * \param *endpoint_info_ptr Pointer to endpoint info structure
MACRUM 0:276e7a263c35 1344 * \param msg_type Message type
MACRUM 0:276e7a263c35 1345 *
MACRUM 0:276e7a263c35 1346 * \return number of parameters in uri query
MACRUM 0:276e7a263c35 1347 */
MACRUM 0:276e7a263c35 1348 static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr,
MACRUM 0:276e7a263c35 1349 uint8_t msg_type,
MACRUM 0:276e7a263c35 1350 const char *uri_query)
MACRUM 0:276e7a263c35 1351 {
MACRUM 0:276e7a263c35 1352 uint16_t return_value = 0;
MACRUM 0:276e7a263c35 1353 uint8_t number_of_parameters = 0;
MACRUM 0:276e7a263c35 1354
MACRUM 0:276e7a263c35 1355
MACRUM 0:276e7a263c35 1356 if ((endpoint_info_ptr->endpoint_name_len != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE) && endpoint_info_ptr->endpoint_name_ptr != 0) {
MACRUM 0:276e7a263c35 1357 return_value += endpoint_info_ptr->endpoint_name_len;
MACRUM 0:276e7a263c35 1358 return_value += EP_NAME_PARAMETERS_LEN; //ep=
MACRUM 0:276e7a263c35 1359 number_of_parameters++;
MACRUM 0:276e7a263c35 1360 }
MACRUM 0:276e7a263c35 1361
MACRUM 0:276e7a263c35 1362 if ((endpoint_info_ptr->type_len != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE) && (endpoint_info_ptr->type_ptr != 0)) {
MACRUM 0:276e7a263c35 1363 return_value += endpoint_info_ptr->type_len;
MACRUM 0:276e7a263c35 1364 return_value += ET_PARAMETER_LEN; //et=
MACRUM 0:276e7a263c35 1365 number_of_parameters++;
MACRUM 0:276e7a263c35 1366 }
MACRUM 0:276e7a263c35 1367
MACRUM 0:276e7a263c35 1368 if ((endpoint_info_ptr->lifetime_len != 0) && (endpoint_info_ptr->lifetime_ptr != 0)) {
MACRUM 0:276e7a263c35 1369 return_value += endpoint_info_ptr->lifetime_len;
MACRUM 0:276e7a263c35 1370 return_value += LT_PARAMETER_LEN; //lt=
MACRUM 0:276e7a263c35 1371 number_of_parameters++;
MACRUM 0:276e7a263c35 1372 }
MACRUM 0:276e7a263c35 1373
MACRUM 0:276e7a263c35 1374 if ((endpoint_info_ptr->domain_name_len != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE) && (endpoint_info_ptr->domain_name_ptr != 0)) {
MACRUM 0:276e7a263c35 1375 return_value += endpoint_info_ptr->domain_name_len;
MACRUM 0:276e7a263c35 1376 return_value += DOMAIN_PARAMETER_LEN; //d=
MACRUM 0:276e7a263c35 1377 number_of_parameters++;
MACRUM 0:276e7a263c35 1378 }
MACRUM 0:276e7a263c35 1379
MACRUM 0:276e7a263c35 1380 if (((endpoint_info_ptr->binding_and_mode & 0x04) || (endpoint_info_ptr->binding_and_mode & 0x01)) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
MACRUM 0:276e7a263c35 1381 return_value += BS_QUEUE_MODE_PARAMATER_LEN;
MACRUM 0:276e7a263c35 1382
MACRUM 0:276e7a263c35 1383 if (endpoint_info_ptr->binding_and_mode & 0x01) {
MACRUM 0:276e7a263c35 1384 return_value++;
MACRUM 0:276e7a263c35 1385 }
MACRUM 0:276e7a263c35 1386 if (endpoint_info_ptr->binding_and_mode & 0x04) {
MACRUM 0:276e7a263c35 1387 return_value++;
MACRUM 0:276e7a263c35 1388 }
MACRUM 0:276e7a263c35 1389 if ((endpoint_info_ptr->binding_and_mode & 0x02) && ((endpoint_info_ptr->binding_and_mode & 0x04) || (endpoint_info_ptr->binding_and_mode & 0x01))) {
MACRUM 0:276e7a263c35 1390 return_value++;
MACRUM 0:276e7a263c35 1391 }
MACRUM 0:276e7a263c35 1392
MACRUM 0:276e7a263c35 1393 number_of_parameters++;
MACRUM 0:276e7a263c35 1394 }
MACRUM 0:276e7a263c35 1395
MACRUM 0:276e7a263c35 1396 if (number_of_parameters != 0) {
MACRUM 0:276e7a263c35 1397 return_value += (number_of_parameters - 1);
MACRUM 0:276e7a263c35 1398 }
MACRUM 0:276e7a263c35 1399
MACRUM 0:276e7a263c35 1400 if (uri_query) {
MACRUM 0:276e7a263c35 1401 return_value += strlen(uri_query);
MACRUM 0:276e7a263c35 1402 }
MACRUM 0:276e7a263c35 1403
MACRUM 0:276e7a263c35 1404 if (return_value > MAX_URI_QUERY_LEN) {
MACRUM 0:276e7a263c35 1405 tr_error("sn_nsdl_calculate_uri_query_option_len - max param length reached (%d)", return_value);
MACRUM 0:276e7a263c35 1406 return_value = 0;
MACRUM 0:276e7a263c35 1407 }
MACRUM 0:276e7a263c35 1408
MACRUM 0:276e7a263c35 1409 return return_value;
MACRUM 0:276e7a263c35 1410 }
MACRUM 0:276e7a263c35 1411
MACRUM 0:276e7a263c35 1412 /**
MACRUM 0:276e7a263c35 1413 * \fn static int8_t sn_nsdl_fill_uri_query_options(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type)
MACRUM 0:276e7a263c35 1414 *
MACRUM 0:276e7a263c35 1415 *
MACRUM 0:276e7a263c35 1416 * \brief Fills uri-query options to message header struct
MACRUM 0:276e7a263c35 1417 * \param *handle Pointer to nsdl-library handle
MACRUM 0:276e7a263c35 1418 * \param *parameter_ptr Pointer to endpoint parameters struct
MACRUM 0:276e7a263c35 1419 * \param *source_msg_ptr Pointer to CoAP header struct
MACRUM 0:276e7a263c35 1420 * \param msg_type Message type
MACRUM 0:276e7a263c35 1421 *
MACRUM 0:276e7a263c35 1422 * \return SN_NSDL_SUCCESS = 0, Failed = -1
MACRUM 0:276e7a263c35 1423 */
MACRUM 0:276e7a263c35 1424 static int8_t sn_nsdl_fill_uri_query_options(struct nsdl_s *handle,
MACRUM 0:276e7a263c35 1425 sn_nsdl_ep_parameters_s *parameter_ptr,
MACRUM 0:276e7a263c35 1426 sn_coap_hdr_s *source_msg_ptr,
MACRUM 0:276e7a263c35 1427 uint8_t msg_type,
MACRUM 0:276e7a263c35 1428 const char *uri_query)
MACRUM 0:276e7a263c35 1429 {
MACRUM 0:276e7a263c35 1430 uint8_t *temp_ptr = NULL;
MACRUM 0:276e7a263c35 1431 if( !validateParameters(parameter_ptr) ){
MACRUM 0:276e7a263c35 1432 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1433 }
MACRUM 0:276e7a263c35 1434
MACRUM 0:276e7a263c35 1435 size_t query_len = sn_nsdl_calculate_uri_query_option_len(parameter_ptr, msg_type, uri_query);
MACRUM 0:276e7a263c35 1436 if (query_len == 0) {
MACRUM 0:276e7a263c35 1437 return 0;
MACRUM 0:276e7a263c35 1438 }
MACRUM 0:276e7a263c35 1439
MACRUM 0:276e7a263c35 1440 source_msg_ptr->options_list_ptr->uri_query_len = query_len;
MACRUM 0:276e7a263c35 1441 source_msg_ptr->options_list_ptr->uri_query_ptr = handle->sn_nsdl_alloc(query_len);
MACRUM 0:276e7a263c35 1442
MACRUM 0:276e7a263c35 1443 if (source_msg_ptr->options_list_ptr->uri_query_ptr == NULL) {
MACRUM 0:276e7a263c35 1444 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1445 }
MACRUM 0:276e7a263c35 1446 memset(source_msg_ptr->options_list_ptr->uri_query_ptr, 0, source_msg_ptr->options_list_ptr->uri_query_len);
MACRUM 0:276e7a263c35 1447
MACRUM 0:276e7a263c35 1448 temp_ptr = source_msg_ptr->options_list_ptr->uri_query_ptr;
MACRUM 0:276e7a263c35 1449
MACRUM 0:276e7a263c35 1450 /******************************************************/
MACRUM 0:276e7a263c35 1451 /* If endpoint name is configured, fill needed fields */
MACRUM 0:276e7a263c35 1452 /******************************************************/
MACRUM 0:276e7a263c35 1453
MACRUM 0:276e7a263c35 1454 if ((parameter_ptr->endpoint_name_len != 0) &&
MACRUM 0:276e7a263c35 1455 (parameter_ptr->endpoint_name_ptr != 0) &&
MACRUM 0:276e7a263c35 1456 (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
MACRUM 0:276e7a263c35 1457 /* fill endpoint name, first ?ep=, then endpoint name */
MACRUM 0:276e7a263c35 1458 memcpy(temp_ptr, ep_name_parameter_string, sizeof(ep_name_parameter_string));
MACRUM 0:276e7a263c35 1459 temp_ptr += EP_NAME_PARAMETERS_LEN;
MACRUM 0:276e7a263c35 1460 memcpy(temp_ptr, parameter_ptr->endpoint_name_ptr, parameter_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 1461 temp_ptr += parameter_ptr->endpoint_name_len;
MACRUM 0:276e7a263c35 1462 }
MACRUM 0:276e7a263c35 1463
MACRUM 0:276e7a263c35 1464 /******************************************************/
MACRUM 0:276e7a263c35 1465 /* If endpoint type is configured, fill needed fields */
MACRUM 0:276e7a263c35 1466 /******************************************************/
MACRUM 0:276e7a263c35 1467
MACRUM 0:276e7a263c35 1468 if ((parameter_ptr->type_len != 0) &&
MACRUM 0:276e7a263c35 1469 (parameter_ptr->type_ptr != 0) &&
MACRUM 0:276e7a263c35 1470 (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
MACRUM 0:276e7a263c35 1471 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
MACRUM 0:276e7a263c35 1472 *temp_ptr++ = '&';
MACRUM 0:276e7a263c35 1473 }
MACRUM 0:276e7a263c35 1474
MACRUM 0:276e7a263c35 1475 memcpy(temp_ptr, et_parameter, sizeof(et_parameter));
MACRUM 0:276e7a263c35 1476 temp_ptr += ET_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1477 memcpy(temp_ptr, parameter_ptr->type_ptr, parameter_ptr->type_len);
MACRUM 0:276e7a263c35 1478 temp_ptr += parameter_ptr->type_len;
MACRUM 0:276e7a263c35 1479 }
MACRUM 0:276e7a263c35 1480
MACRUM 0:276e7a263c35 1481
MACRUM 0:276e7a263c35 1482 /******************************************************/
MACRUM 0:276e7a263c35 1483 /* If lifetime is configured, fill needed fields */
MACRUM 0:276e7a263c35 1484 /******************************************************/
MACRUM 0:276e7a263c35 1485
MACRUM 0:276e7a263c35 1486 if ((parameter_ptr->lifetime_len != 0) && (parameter_ptr->lifetime_ptr != 0)) {
MACRUM 0:276e7a263c35 1487 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
MACRUM 0:276e7a263c35 1488 *temp_ptr++ = '&';
MACRUM 0:276e7a263c35 1489 }
MACRUM 0:276e7a263c35 1490
MACRUM 0:276e7a263c35 1491 memcpy(temp_ptr, ep_lifetime_parameter, sizeof(ep_lifetime_parameter));
MACRUM 0:276e7a263c35 1492 temp_ptr += LT_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1493 memcpy(temp_ptr, parameter_ptr->lifetime_ptr, parameter_ptr->lifetime_len);
MACRUM 0:276e7a263c35 1494 temp_ptr += parameter_ptr->lifetime_len;
MACRUM 0:276e7a263c35 1495 }
MACRUM 0:276e7a263c35 1496
MACRUM 0:276e7a263c35 1497 /******************************************************/
MACRUM 0:276e7a263c35 1498 /* If domain is configured, fill needed fields */
MACRUM 0:276e7a263c35 1499 /******************************************************/
MACRUM 0:276e7a263c35 1500
MACRUM 0:276e7a263c35 1501 if ((parameter_ptr->domain_name_len != 0) &&
MACRUM 0:276e7a263c35 1502 (parameter_ptr->domain_name_ptr != 0) &&
MACRUM 0:276e7a263c35 1503 (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
MACRUM 0:276e7a263c35 1504 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
MACRUM 0:276e7a263c35 1505 *temp_ptr++ = '&';
MACRUM 0:276e7a263c35 1506 }
MACRUM 0:276e7a263c35 1507
MACRUM 0:276e7a263c35 1508 memcpy(temp_ptr, ep_domain_parameter, sizeof(ep_domain_parameter));
MACRUM 0:276e7a263c35 1509 temp_ptr += DOMAIN_PARAMETER_LEN;
MACRUM 0:276e7a263c35 1510 memcpy(temp_ptr, parameter_ptr->domain_name_ptr, parameter_ptr->domain_name_len);
MACRUM 0:276e7a263c35 1511 temp_ptr += parameter_ptr->domain_name_len;
MACRUM 0:276e7a263c35 1512 }
MACRUM 0:276e7a263c35 1513
MACRUM 0:276e7a263c35 1514 /******************************************************/
MACRUM 0:276e7a263c35 1515 /* If queue-mode is configured, fill needed fields */
MACRUM 0:276e7a263c35 1516 /******************************************************/
MACRUM 0:276e7a263c35 1517
MACRUM 0:276e7a263c35 1518 if (((parameter_ptr->binding_and_mode & 0x01) ||
MACRUM 0:276e7a263c35 1519 (parameter_ptr->binding_and_mode & 0x04)) &&
MACRUM 0:276e7a263c35 1520 (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
MACRUM 0:276e7a263c35 1521 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
MACRUM 0:276e7a263c35 1522 *temp_ptr++ = '&';
MACRUM 0:276e7a263c35 1523 }
MACRUM 0:276e7a263c35 1524
MACRUM 0:276e7a263c35 1525 memcpy(temp_ptr, bs_queue_mode, sizeof(bs_queue_mode));
MACRUM 0:276e7a263c35 1526 temp_ptr += BS_QUEUE_MODE_PARAMATER_LEN;
MACRUM 0:276e7a263c35 1527
MACRUM 0:276e7a263c35 1528 if (parameter_ptr->binding_and_mode & 0x01) {
MACRUM 0:276e7a263c35 1529 *temp_ptr++ = 'U';
MACRUM 0:276e7a263c35 1530 if (parameter_ptr->binding_and_mode & 0x02) {
MACRUM 0:276e7a263c35 1531 *temp_ptr++ = 'Q';
MACRUM 0:276e7a263c35 1532 }
MACRUM 0:276e7a263c35 1533 }
MACRUM 0:276e7a263c35 1534
MACRUM 0:276e7a263c35 1535 if (parameter_ptr->binding_and_mode & 0x04) {
MACRUM 0:276e7a263c35 1536 *temp_ptr++ = 'S';
MACRUM 0:276e7a263c35 1537 if ((parameter_ptr->binding_and_mode & 0x02) && !(parameter_ptr->binding_and_mode & 0x01)) {
MACRUM 0:276e7a263c35 1538 *temp_ptr++ = 'Q';
MACRUM 0:276e7a263c35 1539 }
MACRUM 0:276e7a263c35 1540 }
MACRUM 0:276e7a263c35 1541 }
MACRUM 0:276e7a263c35 1542
MACRUM 0:276e7a263c35 1543 if (uri_query) {
MACRUM 0:276e7a263c35 1544 memcpy(temp_ptr, uri_query, strlen(uri_query));
MACRUM 0:276e7a263c35 1545 }
MACRUM 0:276e7a263c35 1546
MACRUM 0:276e7a263c35 1547 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 1548 }
MACRUM 0:276e7a263c35 1549
MACRUM 0:276e7a263c35 1550 static bool validateParameters(sn_nsdl_ep_parameters_s *parameter_ptr)
MACRUM 0:276e7a263c35 1551 {
MACRUM 0:276e7a263c35 1552 if( !validate( parameter_ptr->domain_name_ptr, parameter_ptr->domain_name_len, '&' ) ){
MACRUM 0:276e7a263c35 1553 return false;
MACRUM 0:276e7a263c35 1554 }
MACRUM 0:276e7a263c35 1555
MACRUM 0:276e7a263c35 1556 if( !validate( parameter_ptr->endpoint_name_ptr, parameter_ptr->endpoint_name_len, '&' ) ){
MACRUM 0:276e7a263c35 1557 return false;
MACRUM 0:276e7a263c35 1558 }
MACRUM 0:276e7a263c35 1559
MACRUM 0:276e7a263c35 1560 if( !validate( parameter_ptr->lifetime_ptr, parameter_ptr->lifetime_len, '&' ) ){
MACRUM 0:276e7a263c35 1561 return false;
MACRUM 0:276e7a263c35 1562 }
MACRUM 0:276e7a263c35 1563
MACRUM 0:276e7a263c35 1564 if( !validate( parameter_ptr->type_ptr, parameter_ptr->type_len, '&' ) ){
MACRUM 0:276e7a263c35 1565 return false;
MACRUM 0:276e7a263c35 1566 }
MACRUM 0:276e7a263c35 1567 return true;
MACRUM 0:276e7a263c35 1568 }
MACRUM 0:276e7a263c35 1569
MACRUM 0:276e7a263c35 1570 static bool validate(uint8_t* ptr, uint32_t len, char illegalChar)
MACRUM 0:276e7a263c35 1571 {
MACRUM 0:276e7a263c35 1572 if( ptr ){
MACRUM 0:276e7a263c35 1573 for( uint32_t i=0; i < len; i++ ){
MACRUM 0:276e7a263c35 1574 if( ptr[i] == illegalChar ){
MACRUM 0:276e7a263c35 1575 return false;
MACRUM 0:276e7a263c35 1576 }
MACRUM 0:276e7a263c35 1577 }
MACRUM 0:276e7a263c35 1578 }
MACRUM 0:276e7a263c35 1579 return true;
MACRUM 0:276e7a263c35 1580 }
MACRUM 0:276e7a263c35 1581
MACRUM 0:276e7a263c35 1582 /**
MACRUM 0:276e7a263c35 1583 * \fn static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
MACRUM 0:276e7a263c35 1584 *
MACRUM 0:276e7a263c35 1585 * \brief If received message is reply for the message that NSDL has been sent, it is processed here. Else, packet will be sent to application.
MACRUM 0:276e7a263c35 1586 * \param *handle Pointer to nsdl-library handle
MACRUM 0:276e7a263c35 1587 * \param *coap_packet_ptr Pointer to received CoAP packet
MACRUM 0:276e7a263c35 1588 * \param *address_ptr Pointer to source address struct
MACRUM 0:276e7a263c35 1589 *
MACRUM 0:276e7a263c35 1590 * \return SN_NSDL_SUCCESS = 0, Failed = -1
MACRUM 0:276e7a263c35 1591 */
MACRUM 0:276e7a263c35 1592 static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
MACRUM 0:276e7a263c35 1593 {
MACRUM 0:276e7a263c35 1594 if ((coap_packet_ptr == 0) || (address_ptr == 0)) {
MACRUM 0:276e7a263c35 1595 return -1;
MACRUM 0:276e7a263c35 1596 }
MACRUM 0:276e7a263c35 1597
MACRUM 0:276e7a263c35 1598 bool is_reg_msg = false;
MACRUM 0:276e7a263c35 1599 bool is_update_reg_msg = false;
MACRUM 0:276e7a263c35 1600 bool is_unreg_msg = false;
MACRUM 0:276e7a263c35 1601 if (coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_CREATED) {
MACRUM 0:276e7a263c35 1602 if (handle->grs->coap->sn_coap_block_data_size > 0) {
MACRUM 0:276e7a263c35 1603 handle->register_msg_id += handle->register_msg_len / handle->grs->coap->sn_coap_block_data_size;
MACRUM 0:276e7a263c35 1604 }
MACRUM 0:276e7a263c35 1605 if (coap_packet_ptr->msg_id == handle->register_msg_id) {
MACRUM 0:276e7a263c35 1606 handle->sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_IS_REGISTERED;
MACRUM 0:276e7a263c35 1607 is_reg_msg = true;
MACRUM 0:276e7a263c35 1608 sn_grs_mark_resources_as_registered(handle);
MACRUM 0:276e7a263c35 1609 if (sn_nsdl_resolve_ep_information(handle, coap_packet_ptr) != SN_NSDL_SUCCESS) {
MACRUM 0:276e7a263c35 1610 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1611 }
MACRUM 0:276e7a263c35 1612 }
MACRUM 0:276e7a263c35 1613 }
MACRUM 0:276e7a263c35 1614
MACRUM 0:276e7a263c35 1615 else if (coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_CHANGED) {
MACRUM 0:276e7a263c35 1616 if (handle->grs->coap->sn_coap_block_data_size > 0) {
MACRUM 0:276e7a263c35 1617 handle->update_register_msg_id += handle->update_register_msg_len / handle->grs->coap->sn_coap_block_data_size;
MACRUM 0:276e7a263c35 1618 }
MACRUM 0:276e7a263c35 1619 if (coap_packet_ptr->msg_id == handle->update_register_msg_id) {
MACRUM 0:276e7a263c35 1620 is_update_reg_msg = true;
MACRUM 0:276e7a263c35 1621 }
MACRUM 0:276e7a263c35 1622 }
MACRUM 0:276e7a263c35 1623
MACRUM 0:276e7a263c35 1624 if (coap_packet_ptr->msg_id == handle->unregister_msg_id) {
MACRUM 0:276e7a263c35 1625 is_unreg_msg = true;
MACRUM 0:276e7a263c35 1626 if (coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_DELETED) {
MACRUM 0:276e7a263c35 1627
MACRUM 0:276e7a263c35 1628 handle->sn_nsdl_free(handle->ep_information_ptr->endpoint_name_ptr);
MACRUM 0:276e7a263c35 1629 handle->ep_information_ptr->endpoint_name_ptr = 0;
MACRUM 0:276e7a263c35 1630 handle->ep_information_ptr->endpoint_name_len = 0;
MACRUM 0:276e7a263c35 1631
MACRUM 0:276e7a263c35 1632 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
MACRUM 0:276e7a263c35 1633 handle->ep_information_ptr->domain_name_ptr = 0;
MACRUM 0:276e7a263c35 1634 handle->ep_information_ptr->domain_name_len = 0;
MACRUM 0:276e7a263c35 1635 }
MACRUM 0:276e7a263c35 1636 }
MACRUM 0:276e7a263c35 1637
MACRUM 0:276e7a263c35 1638 /* No messages to wait for, or message was not response to our request */
MACRUM 0:276e7a263c35 1639 int ret = handle->sn_nsdl_rx_callback(handle, coap_packet_ptr, address_ptr);
MACRUM 0:276e7a263c35 1640 if (is_reg_msg) {
MACRUM 0:276e7a263c35 1641 handle->register_msg_id = 0;
MACRUM 0:276e7a263c35 1642 handle->register_msg_len = 0;
MACRUM 0:276e7a263c35 1643 }
MACRUM 0:276e7a263c35 1644 else if (is_unreg_msg) {
MACRUM 0:276e7a263c35 1645 handle->unregister_msg_id = 0;
MACRUM 0:276e7a263c35 1646 }
MACRUM 0:276e7a263c35 1647 else if (is_update_reg_msg) {
MACRUM 0:276e7a263c35 1648 handle->update_register_msg_id = 0;
MACRUM 0:276e7a263c35 1649 handle->update_register_msg_len = 0;
MACRUM 0:276e7a263c35 1650 }
MACRUM 0:276e7a263c35 1651 else if (coap_packet_ptr->msg_id == handle->bootstrap_msg_id) {
MACRUM 0:276e7a263c35 1652 handle->bootstrap_msg_id = 0;
MACRUM 0:276e7a263c35 1653 }
MACRUM 0:276e7a263c35 1654 return ret;
MACRUM 0:276e7a263c35 1655 }
MACRUM 0:276e7a263c35 1656
MACRUM 0:276e7a263c35 1657 /**
MACRUM 0:276e7a263c35 1658 * \fn static int8_t sn_nsdl_resolve_ep_information(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr)
MACRUM 0:276e7a263c35 1659 *
MACRUM 0:276e7a263c35 1660 *
MACRUM 0:276e7a263c35 1661 * \brief Resolves endpoint information from received CoAP message
MACRUM 0:276e7a263c35 1662 * \param *handle Pointer to nsdl-library handle
MACRUM 0:276e7a263c35 1663 * \param *coap_packet_ptr Pointer to received CoAP message
MACRUM 0:276e7a263c35 1664 *
MACRUM 0:276e7a263c35 1665 * \return SN_NSDL_SUCCESS = 0, Failed = -1
MACRUM 0:276e7a263c35 1666 */
MACRUM 0:276e7a263c35 1667 static int8_t sn_nsdl_resolve_ep_information(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr)
MACRUM 0:276e7a263c35 1668 {
MACRUM 0:276e7a263c35 1669 uint8_t *temp_ptr;
MACRUM 0:276e7a263c35 1670 uint8_t parameter_count = 0;
MACRUM 0:276e7a263c35 1671 uint16_t parameter_len = 0;
MACRUM 0:276e7a263c35 1672
MACRUM 0:276e7a263c35 1673 if (!coap_packet_ptr || !coap_packet_ptr->options_list_ptr ||
MACRUM 0:276e7a263c35 1674 !coap_packet_ptr->options_list_ptr->location_path_ptr) {
MACRUM 0:276e7a263c35 1675 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1676 }
MACRUM 0:276e7a263c35 1677
MACRUM 0:276e7a263c35 1678 temp_ptr = coap_packet_ptr->options_list_ptr->location_path_ptr;
MACRUM 0:276e7a263c35 1679
MACRUM 0:276e7a263c35 1680 while (temp_ptr <= (coap_packet_ptr->options_list_ptr->location_path_ptr + coap_packet_ptr->options_list_ptr->location_path_len)) {
MACRUM 0:276e7a263c35 1681
MACRUM 0:276e7a263c35 1682 if ((temp_ptr == (coap_packet_ptr->options_list_ptr->location_path_ptr + coap_packet_ptr->options_list_ptr->location_path_len)) || (*temp_ptr == '/')) {
MACRUM 0:276e7a263c35 1683
MACRUM 0:276e7a263c35 1684 parameter_count++;
MACRUM 0:276e7a263c35 1685 if (parameter_count == 2) {
MACRUM 0:276e7a263c35 1686 if (!handle->ep_information_ptr->domain_name_ptr) {
MACRUM 0:276e7a263c35 1687 handle->ep_information_ptr->domain_name_len = parameter_len - 1;
MACRUM 0:276e7a263c35 1688 handle->ep_information_ptr->domain_name_ptr = handle->sn_nsdl_alloc(handle->ep_information_ptr->domain_name_len);
MACRUM 0:276e7a263c35 1689 if (!handle->ep_information_ptr->domain_name_ptr) {
MACRUM 0:276e7a263c35 1690 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1691 }
MACRUM 0:276e7a263c35 1692 memcpy(handle->ep_information_ptr->domain_name_ptr, temp_ptr - handle->ep_information_ptr->domain_name_len, handle->ep_information_ptr->domain_name_len);
MACRUM 0:276e7a263c35 1693 }
MACRUM 0:276e7a263c35 1694
MACRUM 0:276e7a263c35 1695 }
MACRUM 0:276e7a263c35 1696 if (parameter_count == 3) {
MACRUM 0:276e7a263c35 1697 if (!handle->ep_information_ptr->endpoint_name_ptr) {
MACRUM 0:276e7a263c35 1698 handle->ep_information_ptr->endpoint_name_len = parameter_len - 1;
MACRUM 0:276e7a263c35 1699 handle->ep_information_ptr->endpoint_name_ptr = handle->sn_nsdl_alloc(handle->ep_information_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 1700 if (!handle->ep_information_ptr->endpoint_name_ptr) {
MACRUM 0:276e7a263c35 1701 if (handle->ep_information_ptr->domain_name_ptr) {
MACRUM 0:276e7a263c35 1702 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
MACRUM 0:276e7a263c35 1703 handle->ep_information_ptr->domain_name_ptr = NULL;
MACRUM 0:276e7a263c35 1704 handle->ep_information_ptr->domain_name_len = 0;
MACRUM 0:276e7a263c35 1705 }
MACRUM 0:276e7a263c35 1706
MACRUM 0:276e7a263c35 1707 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1708
MACRUM 0:276e7a263c35 1709 }
MACRUM 0:276e7a263c35 1710 memcpy(handle->ep_information_ptr->endpoint_name_ptr, temp_ptr - handle->ep_information_ptr->endpoint_name_len, handle->ep_information_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 1711 }
MACRUM 0:276e7a263c35 1712 }
MACRUM 0:276e7a263c35 1713 parameter_len = 0;
MACRUM 0:276e7a263c35 1714 }
MACRUM 0:276e7a263c35 1715 parameter_len++;
MACRUM 0:276e7a263c35 1716 temp_ptr++;
MACRUM 0:276e7a263c35 1717 }
MACRUM 0:276e7a263c35 1718
MACRUM 0:276e7a263c35 1719
MACRUM 0:276e7a263c35 1720 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 1721 }
MACRUM 0:276e7a263c35 1722
MACRUM 0:276e7a263c35 1723 extern int8_t set_NSP_address(struct nsdl_s *handle, uint8_t *NSP_address, uint8_t address_length, uint16_t port, sn_nsdl_addr_type_e address_type)
MACRUM 0:276e7a263c35 1724 {
MACRUM 0:276e7a263c35 1725 /* Check parameters and source pointers */
MACRUM 0:276e7a263c35 1726 if (!handle || !handle->nsp_address_ptr || !handle->nsp_address_ptr->omalw_address_ptr || !NSP_address) {
MACRUM 0:276e7a263c35 1727 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1728 }
MACRUM 0:276e7a263c35 1729
MACRUM 0:276e7a263c35 1730 handle->nsp_address_ptr->omalw_address_ptr->type = address_type;
MACRUM 0:276e7a263c35 1731 handle->nsp_address_ptr->omalw_server_security = SEC_NOT_SET;
MACRUM 0:276e7a263c35 1732
MACRUM 0:276e7a263c35 1733 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
MACRUM 0:276e7a263c35 1734
MACRUM 0:276e7a263c35 1735 handle->nsp_address_ptr->omalw_address_ptr->addr_len = address_length;
MACRUM 0:276e7a263c35 1736
MACRUM 0:276e7a263c35 1737 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = handle->sn_nsdl_alloc(handle->nsp_address_ptr->omalw_address_ptr->addr_len);
MACRUM 0:276e7a263c35 1738 if (!handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
MACRUM 0:276e7a263c35 1739 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1740 }
MACRUM 0:276e7a263c35 1741
MACRUM 0:276e7a263c35 1742 memcpy(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr, NSP_address, handle->nsp_address_ptr->omalw_address_ptr->addr_len);
MACRUM 0:276e7a263c35 1743 handle->nsp_address_ptr->omalw_address_ptr->port = port;
MACRUM 0:276e7a263c35 1744
MACRUM 0:276e7a263c35 1745 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 1746 }
MACRUM 0:276e7a263c35 1747
MACRUM 0:276e7a263c35 1748
MACRUM 0:276e7a263c35 1749 static uint8_t sn_nsdl_itoa_len(uint32_t value)
MACRUM 0:276e7a263c35 1750 {
MACRUM 0:276e7a263c35 1751 uint8_t i = 0;
MACRUM 0:276e7a263c35 1752
MACRUM 0:276e7a263c35 1753 do {
MACRUM 0:276e7a263c35 1754 i++;
MACRUM 0:276e7a263c35 1755 } while ((value /= 10) > 0);
MACRUM 0:276e7a263c35 1756
MACRUM 0:276e7a263c35 1757 return i;
MACRUM 0:276e7a263c35 1758 }
MACRUM 0:276e7a263c35 1759
MACRUM 0:276e7a263c35 1760 static uint8_t *sn_nsdl_itoa(uint8_t *ptr, uint32_t value)
MACRUM 0:276e7a263c35 1761 {
MACRUM 0:276e7a263c35 1762
MACRUM 0:276e7a263c35 1763 uint8_t start = 0;
MACRUM 0:276e7a263c35 1764 uint8_t end = 0;
MACRUM 0:276e7a263c35 1765 uint8_t i;
MACRUM 0:276e7a263c35 1766
MACRUM 0:276e7a263c35 1767 i = 0;
MACRUM 0:276e7a263c35 1768
MACRUM 0:276e7a263c35 1769 /* ITOA */
MACRUM 0:276e7a263c35 1770 do {
MACRUM 0:276e7a263c35 1771 ptr[i++] = (value % 10) + '0';
MACRUM 0:276e7a263c35 1772 } while ((value /= 10) > 0);
MACRUM 0:276e7a263c35 1773
MACRUM 0:276e7a263c35 1774 end = i - 1;
MACRUM 0:276e7a263c35 1775
MACRUM 0:276e7a263c35 1776 /* reverse (part of ITOA) */
MACRUM 0:276e7a263c35 1777 while (start < end) {
MACRUM 0:276e7a263c35 1778 uint8_t chr;
MACRUM 0:276e7a263c35 1779
MACRUM 0:276e7a263c35 1780 chr = ptr[start];
MACRUM 0:276e7a263c35 1781 ptr[start] = ptr[end];
MACRUM 0:276e7a263c35 1782 ptr[end] = chr;
MACRUM 0:276e7a263c35 1783
MACRUM 0:276e7a263c35 1784 start++;
MACRUM 0:276e7a263c35 1785 end--;
MACRUM 0:276e7a263c35 1786
MACRUM 0:276e7a263c35 1787 }
MACRUM 0:276e7a263c35 1788 return (ptr + i);
MACRUM 0:276e7a263c35 1789 }
MACRUM 0:276e7a263c35 1790
MACRUM 0:276e7a263c35 1791 static int8_t set_endpoint_info(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *endpoint_info_ptr)
MACRUM 0:276e7a263c35 1792 {
MACRUM 0:276e7a263c35 1793 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
MACRUM 0:276e7a263c35 1794 handle->ep_information_ptr->domain_name_ptr = 0;
MACRUM 0:276e7a263c35 1795 handle->ep_information_ptr->domain_name_len = 0;
MACRUM 0:276e7a263c35 1796
MACRUM 0:276e7a263c35 1797 handle->sn_nsdl_free(handle->ep_information_ptr->endpoint_name_ptr);
MACRUM 0:276e7a263c35 1798 handle->ep_information_ptr->endpoint_name_ptr = 0;
MACRUM 0:276e7a263c35 1799 handle->ep_information_ptr->endpoint_name_len = 0;
MACRUM 0:276e7a263c35 1800
MACRUM 0:276e7a263c35 1801 if (endpoint_info_ptr->domain_name_ptr && endpoint_info_ptr->domain_name_len) {
MACRUM 0:276e7a263c35 1802 handle->ep_information_ptr->domain_name_ptr = handle->sn_nsdl_alloc(endpoint_info_ptr->domain_name_len);
MACRUM 0:276e7a263c35 1803
MACRUM 0:276e7a263c35 1804 if (!handle->ep_information_ptr->domain_name_ptr) {
MACRUM 0:276e7a263c35 1805 return -1;
MACRUM 0:276e7a263c35 1806 }
MACRUM 0:276e7a263c35 1807
MACRUM 0:276e7a263c35 1808 memcpy(handle->ep_information_ptr->domain_name_ptr, endpoint_info_ptr->domain_name_ptr, endpoint_info_ptr->domain_name_len);
MACRUM 0:276e7a263c35 1809 handle->ep_information_ptr->domain_name_len = endpoint_info_ptr->domain_name_len;
MACRUM 0:276e7a263c35 1810 }
MACRUM 0:276e7a263c35 1811
MACRUM 0:276e7a263c35 1812 if (endpoint_info_ptr->endpoint_name_ptr && endpoint_info_ptr->endpoint_name_len) {
MACRUM 0:276e7a263c35 1813 handle->ep_information_ptr->endpoint_name_ptr = handle->sn_nsdl_alloc(endpoint_info_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 1814
MACRUM 0:276e7a263c35 1815 if (!handle->ep_information_ptr->endpoint_name_ptr) {
MACRUM 0:276e7a263c35 1816 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
MACRUM 0:276e7a263c35 1817 handle->ep_information_ptr->domain_name_ptr = 0;
MACRUM 0:276e7a263c35 1818 handle->ep_information_ptr->domain_name_len = 0;
MACRUM 0:276e7a263c35 1819 return -1;
MACRUM 0:276e7a263c35 1820 }
MACRUM 0:276e7a263c35 1821
MACRUM 0:276e7a263c35 1822 memcpy(handle->ep_information_ptr->endpoint_name_ptr, endpoint_info_ptr->endpoint_name_ptr, endpoint_info_ptr->endpoint_name_len);
MACRUM 0:276e7a263c35 1823 handle->ep_information_ptr->endpoint_name_len = endpoint_info_ptr->endpoint_name_len;
MACRUM 0:276e7a263c35 1824 }
MACRUM 0:276e7a263c35 1825
MACRUM 0:276e7a263c35 1826 handle->ep_information_ptr->binding_and_mode = endpoint_info_ptr->binding_and_mode;
MACRUM 0:276e7a263c35 1827 handle->ep_information_ptr->ds_register_mode = endpoint_info_ptr->ds_register_mode;
MACRUM 0:276e7a263c35 1828
MACRUM 0:276e7a263c35 1829 return 0;
MACRUM 0:276e7a263c35 1830 }
MACRUM 0:276e7a263c35 1831
MACRUM 0:276e7a263c35 1832 /* Wrapper */
MACRUM 0:276e7a263c35 1833 sn_grs_resource_list_s *sn_nsdl_list_resource(struct nsdl_s *handle, const char *path)
MACRUM 0:276e7a263c35 1834 {
MACRUM 0:276e7a263c35 1835 /* Check parameters */
MACRUM 0:276e7a263c35 1836 if (handle == NULL) {
MACRUM 0:276e7a263c35 1837 return NULL;
MACRUM 0:276e7a263c35 1838 }
MACRUM 0:276e7a263c35 1839
MACRUM 0:276e7a263c35 1840 return sn_grs_list_resource(handle->grs, path);
MACRUM 0:276e7a263c35 1841 }
MACRUM 0:276e7a263c35 1842
MACRUM 0:276e7a263c35 1843 void sn_nsdl_free_resource_list(struct nsdl_s *handle, sn_grs_resource_list_s *list)
MACRUM 0:276e7a263c35 1844 {
MACRUM 0:276e7a263c35 1845 /* Check parameters */
MACRUM 0:276e7a263c35 1846 if (handle == NULL) {
MACRUM 0:276e7a263c35 1847 return;
MACRUM 0:276e7a263c35 1848 }
MACRUM 0:276e7a263c35 1849
MACRUM 0:276e7a263c35 1850 sn_grs_free_resource_list(handle->grs, list);
MACRUM 0:276e7a263c35 1851 }
MACRUM 0:276e7a263c35 1852
MACRUM 0:276e7a263c35 1853 extern int8_t sn_nsdl_send_coap_message(struct nsdl_s *handle, sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr)
MACRUM 0:276e7a263c35 1854 {
MACRUM 0:276e7a263c35 1855 /* Check parameters */
MACRUM 0:276e7a263c35 1856 if (handle == NULL) {
MACRUM 0:276e7a263c35 1857 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1858 }
MACRUM 0:276e7a263c35 1859
MACRUM 0:276e7a263c35 1860 int8_t ret = sn_grs_send_coap_message(handle, address_ptr, coap_hdr_ptr);
MACRUM 0:276e7a263c35 1861
MACRUM 0:276e7a263c35 1862 sn_nsdl_print_coap_data(coap_hdr_ptr, true);
MACRUM 0:276e7a263c35 1863
MACRUM 0:276e7a263c35 1864 return ret;
MACRUM 0:276e7a263c35 1865 }
MACRUM 0:276e7a263c35 1866
MACRUM 0:276e7a263c35 1867 extern int8_t sn_nsdl_handle_block2_response_internally(struct nsdl_s *handle, uint8_t build_response)
MACRUM 0:276e7a263c35 1868 {
MACRUM 0:276e7a263c35 1869 /* Check parameters */
MACRUM 0:276e7a263c35 1870 if (handle == NULL) {
MACRUM 0:276e7a263c35 1871 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1872 }
MACRUM 0:276e7a263c35 1873
MACRUM 0:276e7a263c35 1874 return sn_coap_protocol_handle_block2_response_internally(handle->grs->coap, build_response);
MACRUM 0:276e7a263c35 1875 }
MACRUM 0:276e7a263c35 1876
MACRUM 0:276e7a263c35 1877 extern int8_t sn_nsdl_clear_coap_sent_blockwise_messages(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 1878 {
MACRUM 0:276e7a263c35 1879 /* Check parameters */
MACRUM 0:276e7a263c35 1880 if (handle == NULL) {
MACRUM 0:276e7a263c35 1881 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1882 }
MACRUM 0:276e7a263c35 1883
MACRUM 0:276e7a263c35 1884 // Enable function once new CoAP API is released to mbed-os
MACRUM 0:276e7a263c35 1885 //sn_coap_protocol_clear_sent_blockwise_messages(handle->grs->coap);
MACRUM 0:276e7a263c35 1886
MACRUM 0:276e7a263c35 1887 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
MACRUM 0:276e7a263c35 1888 // Workaround until CoAP API is released
MACRUM 0:276e7a263c35 1889 /* Loop all stored Blockwise messages in Linked list */
MACRUM 0:276e7a263c35 1890 ns_list_foreach_safe(coap_blockwise_msg_s, removed_blocwise_msg_ptr, &handle->grs->coap->linked_list_blockwise_sent_msgs) {
MACRUM 0:276e7a263c35 1891 if (removed_blocwise_msg_ptr->coap == handle->grs->coap) {
MACRUM 0:276e7a263c35 1892 if (removed_blocwise_msg_ptr->coap_msg_ptr){
MACRUM 0:276e7a263c35 1893 if (removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr) {
MACRUM 0:276e7a263c35 1894 handle->grs->coap->sn_coap_protocol_free(removed_blocwise_msg_ptr->coap_msg_ptr->payload_ptr);
MACRUM 0:276e7a263c35 1895 }
MACRUM 0:276e7a263c35 1896 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap,
MACRUM 0:276e7a263c35 1897 removed_blocwise_msg_ptr->coap_msg_ptr);
MACRUM 0:276e7a263c35 1898
MACRUM 0:276e7a263c35 1899 }
MACRUM 0:276e7a263c35 1900
MACRUM 0:276e7a263c35 1901 ns_list_remove(&handle->grs->coap->linked_list_blockwise_sent_msgs, removed_blocwise_msg_ptr);
MACRUM 0:276e7a263c35 1902 handle->grs->coap->sn_coap_protocol_free(removed_blocwise_msg_ptr);
MACRUM 0:276e7a263c35 1903 }
MACRUM 0:276e7a263c35 1904 }
MACRUM 0:276e7a263c35 1905 #endif
MACRUM 0:276e7a263c35 1906 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 1907 }
MACRUM 0:276e7a263c35 1908
MACRUM 0:276e7a263c35 1909 extern int32_t sn_nsdl_send_get_data_request(struct nsdl_s *handle,
MACRUM 0:276e7a263c35 1910 const char *uri_path,
MACRUM 0:276e7a263c35 1911 const uint32_t token,
MACRUM 0:276e7a263c35 1912 const size_t offset)
MACRUM 0:276e7a263c35 1913 {
MACRUM 0:276e7a263c35 1914 sn_coap_hdr_s req_message;
MACRUM 0:276e7a263c35 1915 int32_t message_id;
MACRUM 0:276e7a263c35 1916
MACRUM 0:276e7a263c35 1917 if (handle == NULL || uri_path == NULL || handle->grs->coap->sn_coap_block_data_size == 0) {
MACRUM 0:276e7a263c35 1918 return 0;
MACRUM 0:276e7a263c35 1919 }
MACRUM 0:276e7a263c35 1920
MACRUM 0:276e7a263c35 1921 memset(&req_message, 0, sizeof(sn_coap_hdr_s));
MACRUM 0:276e7a263c35 1922
MACRUM 0:276e7a263c35 1923 // Fill message fields
MACRUM 0:276e7a263c35 1924 req_message.msg_type = COAP_MSG_TYPE_CONFIRMABLE;
MACRUM 0:276e7a263c35 1925 req_message.msg_code = COAP_MSG_CODE_REQUEST_GET;
MACRUM 0:276e7a263c35 1926 req_message.uri_path_len = (uint16_t)strlen(uri_path);
MACRUM 0:276e7a263c35 1927 req_message.uri_path_ptr = (uint8_t*)uri_path;
MACRUM 0:276e7a263c35 1928 req_message.token_ptr = (uint8_t*)&token;
MACRUM 0:276e7a263c35 1929 req_message.token_len = sizeof(token);
MACRUM 0:276e7a263c35 1930
MACRUM 0:276e7a263c35 1931 // Skip block options if feature is not enabled
MACRUM 0:276e7a263c35 1932 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
MACRUM 0:276e7a263c35 1933 if (sn_coap_parser_alloc_options(handle->grs->coap, &req_message) == NULL) {
MACRUM 0:276e7a263c35 1934 handle->grs->coap->sn_coap_protocol_free(req_message.options_list_ptr);
MACRUM 0:276e7a263c35 1935 return 0;
MACRUM 0:276e7a263c35 1936 }
MACRUM 0:276e7a263c35 1937
MACRUM 0:276e7a263c35 1938 // Add block number
MACRUM 0:276e7a263c35 1939 req_message.options_list_ptr->block2 = 0;
MACRUM 0:276e7a263c35 1940 if (offset > 0) {
MACRUM 0:276e7a263c35 1941 req_message.options_list_ptr->block2 = ((offset / handle->grs->coap->sn_coap_block_data_size) << 4);
MACRUM 0:276e7a263c35 1942 }
MACRUM 0:276e7a263c35 1943 // Add block size
MACRUM 0:276e7a263c35 1944 req_message.options_list_ptr->block2 |= sn_coap_convert_block_size(handle->grs->coap->sn_coap_block_data_size);
MACRUM 0:276e7a263c35 1945 #endif
MACRUM 0:276e7a263c35 1946
MACRUM 0:276e7a263c35 1947 // Build and send coap message
MACRUM 0:276e7a263c35 1948 message_id = sn_nsdl_internal_coap_send(handle, &req_message, handle->nsp_address_ptr->omalw_address_ptr, SN_NSDL_MSG_UNDEFINED);
MACRUM 0:276e7a263c35 1949 handle->grs->coap->sn_coap_protocol_free(req_message.options_list_ptr);
MACRUM 0:276e7a263c35 1950
MACRUM 0:276e7a263c35 1951 return message_id;
MACRUM 0:276e7a263c35 1952 }
MACRUM 0:276e7a263c35 1953
MACRUM 0:276e7a263c35 1954 extern int8_t sn_nsdl_put_resource(struct nsdl_s *handle, sn_nsdl_dynamic_resource_parameters_s *res)
MACRUM 0:276e7a263c35 1955 {
MACRUM 0:276e7a263c35 1956 if (!handle) {
MACRUM 0:276e7a263c35 1957 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1958 }
MACRUM 0:276e7a263c35 1959
MACRUM 0:276e7a263c35 1960 return sn_grs_put_resource(handle->grs, res);
MACRUM 0:276e7a263c35 1961 }
MACRUM 0:276e7a263c35 1962
MACRUM 0:276e7a263c35 1963 extern int8_t sn_nsdl_pop_resource(struct nsdl_s *handle, sn_nsdl_dynamic_resource_parameters_s *res)
MACRUM 0:276e7a263c35 1964 {
MACRUM 0:276e7a263c35 1965 if (!handle) {
MACRUM 0:276e7a263c35 1966 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1967 }
MACRUM 0:276e7a263c35 1968
MACRUM 0:276e7a263c35 1969 return sn_grs_pop_resource(handle->grs, res);
MACRUM 0:276e7a263c35 1970 }
MACRUM 0:276e7a263c35 1971
MACRUM 0:276e7a263c35 1972 extern int8_t sn_nsdl_delete_resource(struct nsdl_s *handle, const char *path)
MACRUM 0:276e7a263c35 1973 {
MACRUM 0:276e7a263c35 1974 /* Check parameters */
MACRUM 0:276e7a263c35 1975 if (handle == NULL) {
MACRUM 0:276e7a263c35 1976 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 1977 }
MACRUM 0:276e7a263c35 1978
MACRUM 0:276e7a263c35 1979 return sn_grs_delete_resource(handle->grs, path);
MACRUM 0:276e7a263c35 1980 }
MACRUM 0:276e7a263c35 1981 extern const sn_nsdl_dynamic_resource_parameters_s *sn_nsdl_get_first_resource(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 1982 {
MACRUM 0:276e7a263c35 1983 /* Check parameters */
MACRUM 0:276e7a263c35 1984 if (handle == NULL) {
MACRUM 0:276e7a263c35 1985 return NULL;
MACRUM 0:276e7a263c35 1986 }
MACRUM 0:276e7a263c35 1987
MACRUM 0:276e7a263c35 1988 return sn_grs_get_first_resource(handle->grs);
MACRUM 0:276e7a263c35 1989 }
MACRUM 0:276e7a263c35 1990 extern const sn_nsdl_dynamic_resource_parameters_s *sn_nsdl_get_next_resource(struct nsdl_s *handle, const sn_nsdl_dynamic_resource_parameters_s *resource)
MACRUM 0:276e7a263c35 1991 {
MACRUM 0:276e7a263c35 1992 /* Check parameters */
MACRUM 0:276e7a263c35 1993 if (handle == NULL) {
MACRUM 0:276e7a263c35 1994 return NULL;
MACRUM 0:276e7a263c35 1995 }
MACRUM 0:276e7a263c35 1996
MACRUM 0:276e7a263c35 1997 return sn_grs_get_next_resource(handle->grs, resource);
MACRUM 0:276e7a263c35 1998 }
MACRUM 0:276e7a263c35 1999
MACRUM 0:276e7a263c35 2000 extern sn_coap_hdr_s *sn_nsdl_build_response(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code)
MACRUM 0:276e7a263c35 2001 {
MACRUM 0:276e7a263c35 2002 if (handle == NULL) {
MACRUM 0:276e7a263c35 2003 return NULL;
MACRUM 0:276e7a263c35 2004 }
MACRUM 0:276e7a263c35 2005
MACRUM 0:276e7a263c35 2006 return sn_coap_build_response(handle->grs->coap, coap_packet_ptr, msg_code);
MACRUM 0:276e7a263c35 2007 }
MACRUM 0:276e7a263c35 2008
MACRUM 0:276e7a263c35 2009 extern sn_coap_options_list_s *sn_nsdl_alloc_options_list(struct nsdl_s *handle, sn_coap_hdr_s *coap_msg_ptr)
MACRUM 0:276e7a263c35 2010 {
MACRUM 0:276e7a263c35 2011 if (handle == NULL || coap_msg_ptr == NULL) {
MACRUM 0:276e7a263c35 2012 return NULL;
MACRUM 0:276e7a263c35 2013 }
MACRUM 0:276e7a263c35 2014 return sn_coap_parser_alloc_options(handle->grs->coap, coap_msg_ptr);
MACRUM 0:276e7a263c35 2015 }
MACRUM 0:276e7a263c35 2016
MACRUM 0:276e7a263c35 2017 extern void sn_nsdl_release_allocated_coap_msg_mem(struct nsdl_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr)
MACRUM 0:276e7a263c35 2018 {
MACRUM 0:276e7a263c35 2019 if (handle == NULL) {
MACRUM 0:276e7a263c35 2020 return;
MACRUM 0:276e7a263c35 2021 }
MACRUM 0:276e7a263c35 2022
MACRUM 0:276e7a263c35 2023 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, freed_coap_msg_ptr);
MACRUM 0:276e7a263c35 2024 }
MACRUM 0:276e7a263c35 2025
MACRUM 0:276e7a263c35 2026 extern int8_t sn_nsdl_set_retransmission_parameters(struct nsdl_s *handle,
MACRUM 0:276e7a263c35 2027 uint8_t resending_count, uint8_t resending_interval)
MACRUM 0:276e7a263c35 2028 {
MACRUM 0:276e7a263c35 2029 if (handle == NULL) {
MACRUM 0:276e7a263c35 2030 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 2031 }
MACRUM 0:276e7a263c35 2032 return sn_coap_protocol_set_retransmission_parameters(handle->grs->coap,
MACRUM 0:276e7a263c35 2033 resending_count,resending_interval);
MACRUM 0:276e7a263c35 2034 }
MACRUM 0:276e7a263c35 2035
MACRUM 0:276e7a263c35 2036 extern int8_t sn_nsdl_set_retransmission_buffer(struct nsdl_s *handle,
MACRUM 0:276e7a263c35 2037 uint8_t buffer_size_messages, uint16_t buffer_size_bytes)
MACRUM 0:276e7a263c35 2038 {
MACRUM 0:276e7a263c35 2039 if (handle == NULL) {
MACRUM 0:276e7a263c35 2040 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 2041 }
MACRUM 0:276e7a263c35 2042 return sn_coap_protocol_set_retransmission_buffer(handle->grs->coap,
MACRUM 0:276e7a263c35 2043 buffer_size_messages, buffer_size_bytes);
MACRUM 0:276e7a263c35 2044 }
MACRUM 0:276e7a263c35 2045
MACRUM 0:276e7a263c35 2046 extern int8_t sn_nsdl_set_block_size(struct nsdl_s *handle, uint16_t block_size)
MACRUM 0:276e7a263c35 2047 {
MACRUM 0:276e7a263c35 2048 if (handle == NULL) {
MACRUM 0:276e7a263c35 2049 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 2050 }
MACRUM 0:276e7a263c35 2051 return sn_coap_protocol_set_block_size(handle->grs->coap, block_size);
MACRUM 0:276e7a263c35 2052 }
MACRUM 0:276e7a263c35 2053
MACRUM 0:276e7a263c35 2054 extern int8_t sn_nsdl_set_duplicate_buffer_size(struct nsdl_s *handle, uint8_t message_count)
MACRUM 0:276e7a263c35 2055 {
MACRUM 0:276e7a263c35 2056 if (handle == NULL) {
MACRUM 0:276e7a263c35 2057 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 2058 }
MACRUM 0:276e7a263c35 2059 return sn_coap_protocol_set_duplicate_buffer_size(handle->grs->coap, message_count);
MACRUM 0:276e7a263c35 2060 }
MACRUM 0:276e7a263c35 2061
MACRUM 0:276e7a263c35 2062 bool sn_nsdl_check_uint_overflow(uint16_t resource_size, uint16_t param_a, uint16_t param_b)
MACRUM 0:276e7a263c35 2063 {
MACRUM 0:276e7a263c35 2064 uint16_t first_check = param_a + param_b;
MACRUM 0:276e7a263c35 2065 if (first_check < param_b) {
MACRUM 0:276e7a263c35 2066 return false;
MACRUM 0:276e7a263c35 2067 } else {
MACRUM 0:276e7a263c35 2068 uint16_t total = resource_size + first_check;
MACRUM 0:276e7a263c35 2069 if (total < first_check) {
MACRUM 0:276e7a263c35 2070 return false;
MACRUM 0:276e7a263c35 2071 } else {
MACRUM 0:276e7a263c35 2072 return true;
MACRUM 0:276e7a263c35 2073 }
MACRUM 0:276e7a263c35 2074 }
MACRUM 0:276e7a263c35 2075 }
MACRUM 0:276e7a263c35 2076
MACRUM 0:276e7a263c35 2077 extern int8_t sn_nsdl_set_context(struct nsdl_s * const handle, void * const context)
MACRUM 0:276e7a263c35 2078 {
MACRUM 0:276e7a263c35 2079 if (handle == NULL) {
MACRUM 0:276e7a263c35 2080 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 2081 }
MACRUM 0:276e7a263c35 2082 handle->context = context;
MACRUM 0:276e7a263c35 2083 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 2084 }
MACRUM 0:276e7a263c35 2085
MACRUM 0:276e7a263c35 2086 extern void *sn_nsdl_get_context(const struct nsdl_s * const handle)
MACRUM 0:276e7a263c35 2087 {
MACRUM 0:276e7a263c35 2088 if (handle == NULL) {
MACRUM 0:276e7a263c35 2089 return NULL;
MACRUM 0:276e7a263c35 2090 }
MACRUM 0:276e7a263c35 2091 return handle->context;
MACRUM 0:276e7a263c35 2092 }
MACRUM 0:276e7a263c35 2093
MACRUM 0:276e7a263c35 2094
MACRUM 0:276e7a263c35 2095 int8_t sn_nsdl_clear_coap_resending_queue(struct nsdl_s *handle)
MACRUM 0:276e7a263c35 2096 {
MACRUM 0:276e7a263c35 2097 if (handle == NULL || handle->grs == NULL) {
MACRUM 0:276e7a263c35 2098 tr_err("sn_nsdl_clear_coap_resending_queue failed.");
MACRUM 0:276e7a263c35 2099 return SN_NSDL_FAILURE;
MACRUM 0:276e7a263c35 2100 }
MACRUM 0:276e7a263c35 2101 sn_coap_protocol_clear_retransmission_buffer(handle->grs->coap);
MACRUM 0:276e7a263c35 2102 return SN_NSDL_SUCCESS;
MACRUM 0:276e7a263c35 2103 }
MACRUM 0:276e7a263c35 2104
MACRUM 0:276e7a263c35 2105 #ifdef RESOURCE_ATTRIBUTES_LIST
MACRUM 0:276e7a263c35 2106 static void sn_nsdl_free_attribute_value(sn_nsdl_attribute_item_s *attribute)
MACRUM 0:276e7a263c35 2107 {
MACRUM 0:276e7a263c35 2108 switch (attribute->attribute_name) {
MACRUM 0:276e7a263c35 2109 case ATTR_RESOURCE_TYPE:
MACRUM 0:276e7a263c35 2110 case ATTR_INTERFACE_DESCRIPTION:
MACRUM 0:276e7a263c35 2111 case ATTR_ENDPOINT_NAME:
MACRUM 0:276e7a263c35 2112 free(attribute->value);
MACRUM 0:276e7a263c35 2113 attribute->value = NULL;
MACRUM 0:276e7a263c35 2114 break;
MACRUM 0:276e7a263c35 2115 case ATTR_NOP:
MACRUM 0:276e7a263c35 2116 case ATTR_END:
MACRUM 0:276e7a263c35 2117 default:
MACRUM 0:276e7a263c35 2118 break;
MACRUM 0:276e7a263c35 2119 }
MACRUM 0:276e7a263c35 2120 }
MACRUM 0:276e7a263c35 2121
MACRUM 0:276e7a263c35 2122 void sn_nsdl_free_resource_attributes_list(sn_nsdl_static_resource_parameters_s *params)
MACRUM 0:276e7a263c35 2123 {
MACRUM 0:276e7a263c35 2124 if (params == NULL || params->free_on_delete == false) {
MACRUM 0:276e7a263c35 2125 return;
MACRUM 0:276e7a263c35 2126 }
MACRUM 0:276e7a263c35 2127 sn_nsdl_attribute_item_s *item = params->attributes_ptr;
MACRUM 0:276e7a263c35 2128 if (item) {
MACRUM 0:276e7a263c35 2129 while (item->attribute_name != ATTR_END) {
MACRUM 0:276e7a263c35 2130 sn_nsdl_free_attribute_value(item);
MACRUM 0:276e7a263c35 2131 item++;
MACRUM 0:276e7a263c35 2132 }
MACRUM 0:276e7a263c35 2133 free(params->attributes_ptr);
MACRUM 0:276e7a263c35 2134 params->attributes_ptr = NULL;
MACRUM 0:276e7a263c35 2135 }
MACRUM 0:276e7a263c35 2136 }
MACRUM 0:276e7a263c35 2137
MACRUM 0:276e7a263c35 2138 bool sn_nsdl_set_resource_attribute(sn_nsdl_static_resource_parameters_s *params, const sn_nsdl_attribute_item_s *attribute)
MACRUM 0:276e7a263c35 2139 {
MACRUM 0:276e7a263c35 2140 if (params == NULL || params->free_on_delete == false) {
MACRUM 0:276e7a263c35 2141 return false;
MACRUM 0:276e7a263c35 2142 }
MACRUM 0:276e7a263c35 2143 unsigned int item_count = 0;
MACRUM 0:276e7a263c35 2144 sn_nsdl_attribute_item_s *item = params->attributes_ptr;
MACRUM 0:276e7a263c35 2145 // Count the number of attributes for reallocation, update in place though
MACRUM 0:276e7a263c35 2146 // if the attribute already existed
MACRUM 0:276e7a263c35 2147 while (item != NULL) {
MACRUM 0:276e7a263c35 2148 item_count++;
MACRUM 0:276e7a263c35 2149 if (item->attribute_name == ATTR_END) {
MACRUM 0:276e7a263c35 2150 break;
MACRUM 0:276e7a263c35 2151 }
MACRUM 0:276e7a263c35 2152 // Check if attribute already exists or if there is NOP we can overwrite
MACRUM 0:276e7a263c35 2153 if (item->attribute_name == attribute->attribute_name || item->attribute_name == ATTR_NOP) {
MACRUM 0:276e7a263c35 2154 // Found attribute or NOP, overwrite it
MACRUM 0:276e7a263c35 2155 sn_nsdl_free_attribute_value(item);
MACRUM 0:276e7a263c35 2156 item->attribute_name = attribute->attribute_name;
MACRUM 0:276e7a263c35 2157 item->value = attribute->value;
MACRUM 0:276e7a263c35 2158 return true;
MACRUM 0:276e7a263c35 2159 }
MACRUM 0:276e7a263c35 2160 item++;
MACRUM 0:276e7a263c35 2161 }
MACRUM 0:276e7a263c35 2162 // Attribute did not yet exist (ptr was null or ATTR_END was first one)
MACRUM 0:276e7a263c35 2163 if (item_count > 0) {
MACRUM 0:276e7a263c35 2164 // List already had some attributes, so reallocate
MACRUM 0:276e7a263c35 2165 size_t new_size = (item_count + 1) * sizeof(sn_nsdl_attribute_item_s);
MACRUM 0:276e7a263c35 2166 item = params->attributes_ptr;
MACRUM 0:276e7a263c35 2167 params->attributes_ptr = realloc(item, new_size);
MACRUM 0:276e7a263c35 2168 if (params->attributes_ptr == NULL) {
MACRUM 0:276e7a263c35 2169 // realloc failed, put back original pointer and return false
MACRUM 0:276e7a263c35 2170 params->attributes_ptr = item;
MACRUM 0:276e7a263c35 2171 return false;
MACRUM 0:276e7a263c35 2172 }
MACRUM 0:276e7a263c35 2173 // And move item ptr to ATTR_END to update that and last attribute
MACRUM 0:276e7a263c35 2174 item = &(params->attributes_ptr[item_count - 1]);
MACRUM 0:276e7a263c35 2175 }
MACRUM 0:276e7a263c35 2176 else {
MACRUM 0:276e7a263c35 2177 // No attributes, so allocate first time (1 struct for attribute and 1 struct for ATTR_END)
MACRUM 0:276e7a263c35 2178 params->attributes_ptr = (char*)malloc(2 * sizeof(sn_nsdl_attribute_item_s));
MACRUM 0:276e7a263c35 2179 if (params->attributes_ptr == NULL) {
MACRUM 0:276e7a263c35 2180 return false;
MACRUM 0:276e7a263c35 2181 }
MACRUM 0:276e7a263c35 2182 item = params->attributes_ptr;
MACRUM 0:276e7a263c35 2183 }
MACRUM 0:276e7a263c35 2184 item->attribute_name = attribute->attribute_name;
MACRUM 0:276e7a263c35 2185 item->value = attribute->value;
MACRUM 0:276e7a263c35 2186 item++;
MACRUM 0:276e7a263c35 2187 item->attribute_name = ATTR_END;
MACRUM 0:276e7a263c35 2188 item->value = NULL;
MACRUM 0:276e7a263c35 2189 return true;
MACRUM 0:276e7a263c35 2190 }
MACRUM 0:276e7a263c35 2191
MACRUM 0:276e7a263c35 2192 const char *sn_nsdl_get_resource_attribute(const sn_nsdl_static_resource_parameters_s *params, sn_nsdl_resource_attribute_t attribute_name)
MACRUM 0:276e7a263c35 2193 {
MACRUM 0:276e7a263c35 2194 char *value = NULL;
MACRUM 0:276e7a263c35 2195 if (params != NULL) {
MACRUM 0:276e7a263c35 2196 sn_nsdl_attribute_item_s *item = params->attributes_ptr;
MACRUM 0:276e7a263c35 2197 while (item != NULL && item->attribute_name != ATTR_END) {
MACRUM 0:276e7a263c35 2198 if (item->attribute_name == attribute_name) {
MACRUM 0:276e7a263c35 2199 value = item->value;
MACRUM 0:276e7a263c35 2200 break;
MACRUM 0:276e7a263c35 2201 }
MACRUM 0:276e7a263c35 2202 item++;
MACRUM 0:276e7a263c35 2203 }
MACRUM 0:276e7a263c35 2204 }
MACRUM 0:276e7a263c35 2205 return value;
MACRUM 0:276e7a263c35 2206 }
MACRUM 0:276e7a263c35 2207
MACRUM 0:276e7a263c35 2208 bool sn_nsdl_remove_resource_attribute(sn_nsdl_static_resource_parameters_s *params, sn_nsdl_resource_attribute_t attribute_name)
MACRUM 0:276e7a263c35 2209 {
MACRUM 0:276e7a263c35 2210 if (params == NULL || params->free_on_delete == false) {
MACRUM 0:276e7a263c35 2211 return false;
MACRUM 0:276e7a263c35 2212 }
MACRUM 0:276e7a263c35 2213
MACRUM 0:276e7a263c35 2214 bool found = false;
MACRUM 0:276e7a263c35 2215 sn_nsdl_attribute_item_s *item = params->attributes_ptr;
MACRUM 0:276e7a263c35 2216 while (item != NULL) {
MACRUM 0:276e7a263c35 2217 if (item->attribute_name == ATTR_END) {
MACRUM 0:276e7a263c35 2218 break;
MACRUM 0:276e7a263c35 2219 }
MACRUM 0:276e7a263c35 2220 // Remove if attribute name matches
MACRUM 0:276e7a263c35 2221 if (item->attribute_name == attribute_name) {
MACRUM 0:276e7a263c35 2222 // Currently only pointer values, need to free and set as NOP
MACRUM 0:276e7a263c35 2223 sn_nsdl_free_attribute_value(item);
MACRUM 0:276e7a263c35 2224 item->attribute_name = ATTR_NOP;
MACRUM 0:276e7a263c35 2225 found = true;
MACRUM 0:276e7a263c35 2226 break;
MACRUM 0:276e7a263c35 2227 }
MACRUM 0:276e7a263c35 2228 item++;
MACRUM 0:276e7a263c35 2229 }
MACRUM 0:276e7a263c35 2230
MACRUM 0:276e7a263c35 2231 return found;
MACRUM 0:276e7a263c35 2232
MACRUM 0:276e7a263c35 2233 }
MACRUM 0:276e7a263c35 2234
MACRUM 0:276e7a263c35 2235 #endif
MACRUM 0:276e7a263c35 2236
MACRUM 0:276e7a263c35 2237
MACRUM 0:276e7a263c35 2238 void sn_nsdl_print_coap_data(sn_coap_hdr_s *coap_header_ptr, bool outgoing)
MACRUM 0:276e7a263c35 2239 {
MACRUM 0:276e7a263c35 2240 #if defined(FEA_TRACE_SUPPORT) || MBED_CONF_MBED_TRACE_ENABLE || YOTTA_CFG_MBED_TRACE || (defined(YOTTA_CFG) && !defined(NDEBUG))
MACRUM 0:276e7a263c35 2241 if (!coap_header_ptr) {
MACRUM 0:276e7a263c35 2242 return;
MACRUM 0:276e7a263c35 2243 }
MACRUM 0:276e7a263c35 2244
MACRUM 0:276e7a263c35 2245 if (outgoing) {
MACRUM 0:276e7a263c35 2246 tr_info("======== Outgoing CoAP package ========");
MACRUM 0:276e7a263c35 2247 } else {
MACRUM 0:276e7a263c35 2248 tr_info("======== Incoming CoAP package ========");
MACRUM 0:276e7a263c35 2249 }
MACRUM 0:276e7a263c35 2250
MACRUM 0:276e7a263c35 2251 if (coap_header_ptr->uri_path_len > 0 && coap_header_ptr->uri_path_ptr) {
MACRUM 0:276e7a263c35 2252 tr_info("Uri-Path:\t\t%.*s", coap_header_ptr->uri_path_len, coap_header_ptr->uri_path_ptr);
MACRUM 0:276e7a263c35 2253 }
MACRUM 0:276e7a263c35 2254 tr_info("Status:\t\t%s", sn_nsdl_coap_status_description(coap_header_ptr->coap_status));
MACRUM 0:276e7a263c35 2255 tr_info("Code:\t\t%s", sn_nsdl_coap_message_code_desc(coap_header_ptr->msg_code));
MACRUM 0:276e7a263c35 2256 tr_info("Type:\t\t%s", sn_nsdl_coap_message_type_desc(coap_header_ptr->msg_type));
MACRUM 0:276e7a263c35 2257 tr_info("Id:\t\t%d", coap_header_ptr->msg_id);
MACRUM 0:276e7a263c35 2258 if (coap_header_ptr->token_ptr && coap_header_ptr->token_len > 0) {
MACRUM 0:276e7a263c35 2259 tr_info("Token:\t\t%s", tr_array(coap_header_ptr->token_ptr, coap_header_ptr->token_len));
MACRUM 0:276e7a263c35 2260 }
MACRUM 0:276e7a263c35 2261 if (coap_header_ptr->content_format != -1) {
MACRUM 0:276e7a263c35 2262 tr_info("Content-type:\t%d", coap_header_ptr->content_format);
MACRUM 0:276e7a263c35 2263 }
MACRUM 0:276e7a263c35 2264 tr_info("Payload len:\t%d", coap_header_ptr->payload_len);
MACRUM 0:276e7a263c35 2265 #ifdef MBED_CLIENT_PRINT_COAP_PAYLOAD
MACRUM 0:276e7a263c35 2266 if (coap_header_ptr->payload_ptr && coap_header_ptr->payload_len > 0) {
MACRUM 0:276e7a263c35 2267 int i = 0;
MACRUM 0:276e7a263c35 2268 int row_len = 40;
MACRUM 0:276e7a263c35 2269 int max_length = 2048;
MACRUM 0:276e7a263c35 2270 while (i < coap_header_ptr->payload_len && i < max_length) {
MACRUM 0:276e7a263c35 2271 if (i + row_len > coap_header_ptr->payload_len) {
MACRUM 0:276e7a263c35 2272 row_len = coap_header_ptr->payload_len - i;
MACRUM 0:276e7a263c35 2273 }
MACRUM 0:276e7a263c35 2274 tr_info("Payload:\t\t%s", tr_array( coap_header_ptr->payload_ptr + i, row_len));
MACRUM 0:276e7a263c35 2275 i += row_len;
MACRUM 0:276e7a263c35 2276 }
MACRUM 0:276e7a263c35 2277 if (i >= max_length)
MACRUM 0:276e7a263c35 2278 tr_info("Payload:\t\t.....");
MACRUM 0:276e7a263c35 2279 }
MACRUM 0:276e7a263c35 2280 #endif
MACRUM 0:276e7a263c35 2281
MACRUM 0:276e7a263c35 2282 if (coap_header_ptr->options_list_ptr) {
MACRUM 0:276e7a263c35 2283 if (coap_header_ptr->options_list_ptr->etag_ptr && coap_header_ptr->options_list_ptr->etag_len > 0) {
MACRUM 0:276e7a263c35 2284 tr_info("E-tag:\t%.*s", coap_header_ptr->options_list_ptr->etag_len, coap_header_ptr->options_list_ptr->etag_ptr);
MACRUM 0:276e7a263c35 2285 }
MACRUM 0:276e7a263c35 2286 if (coap_header_ptr->options_list_ptr->proxy_uri_ptr && coap_header_ptr->options_list_ptr->proxy_uri_len > 0) {
MACRUM 0:276e7a263c35 2287 tr_info("Proxy uri:\t%.*s", coap_header_ptr->options_list_ptr->proxy_uri_len, coap_header_ptr->options_list_ptr->proxy_uri_ptr);
MACRUM 0:276e7a263c35 2288 }
MACRUM 0:276e7a263c35 2289
MACRUM 0:276e7a263c35 2290 if (coap_header_ptr->options_list_ptr->uri_host_ptr && coap_header_ptr->options_list_ptr->uri_host_len > 0) {
MACRUM 0:276e7a263c35 2291 tr_info("Uri host:\t%.*s", coap_header_ptr->options_list_ptr->uri_host_len, coap_header_ptr->options_list_ptr->uri_host_ptr);
MACRUM 0:276e7a263c35 2292 }
MACRUM 0:276e7a263c35 2293
MACRUM 0:276e7a263c35 2294 if (coap_header_ptr->options_list_ptr->location_path_ptr && coap_header_ptr->options_list_ptr->location_path_len > 0) {
MACRUM 0:276e7a263c35 2295 tr_info("Location path:\t%.*s", coap_header_ptr->options_list_ptr->location_path_len, coap_header_ptr->options_list_ptr->location_path_ptr);
MACRUM 0:276e7a263c35 2296 }
MACRUM 0:276e7a263c35 2297
MACRUM 0:276e7a263c35 2298 if (coap_header_ptr->options_list_ptr->location_query_ptr && coap_header_ptr->options_list_ptr->location_query_len > 0) {
MACRUM 0:276e7a263c35 2299 tr_info("Location query:\t%.*s", coap_header_ptr->options_list_ptr->location_query_len, coap_header_ptr->options_list_ptr->location_query_ptr);
MACRUM 0:276e7a263c35 2300 }
MACRUM 0:276e7a263c35 2301
MACRUM 0:276e7a263c35 2302 if (coap_header_ptr->options_list_ptr->uri_query_ptr && coap_header_ptr->options_list_ptr->uri_query_len > 0) {
MACRUM 0:276e7a263c35 2303 tr_info("Uri query:\t%.*s", coap_header_ptr->options_list_ptr->uri_query_len, coap_header_ptr->options_list_ptr->uri_query_ptr);
MACRUM 0:276e7a263c35 2304 }
MACRUM 0:276e7a263c35 2305
MACRUM 0:276e7a263c35 2306 tr_info("Max-age:\t\t%" PRIu32"", coap_header_ptr->options_list_ptr->max_age);
MACRUM 0:276e7a263c35 2307 if (coap_header_ptr->options_list_ptr->use_size1) {
MACRUM 0:276e7a263c35 2308 tr_info("Size 1:\t\t%" PRIu32"", coap_header_ptr->options_list_ptr->size1);
MACRUM 0:276e7a263c35 2309 }
MACRUM 0:276e7a263c35 2310 if (coap_header_ptr->options_list_ptr->use_size2) {
MACRUM 0:276e7a263c35 2311 tr_info("Size 2:\t\t%" PRIu32"", coap_header_ptr->options_list_ptr->size2);
MACRUM 0:276e7a263c35 2312 }
MACRUM 0:276e7a263c35 2313 if (coap_header_ptr->options_list_ptr->accept != -1) {
MACRUM 0:276e7a263c35 2314 tr_info("Accept:\t\t%d", coap_header_ptr->options_list_ptr->accept);
MACRUM 0:276e7a263c35 2315 }
MACRUM 0:276e7a263c35 2316 if (coap_header_ptr->options_list_ptr->uri_port != -1) {
MACRUM 0:276e7a263c35 2317 tr_info("Uri port:\t%" PRId32"", coap_header_ptr->options_list_ptr->uri_port);
MACRUM 0:276e7a263c35 2318 }
MACRUM 0:276e7a263c35 2319 if (coap_header_ptr->options_list_ptr->observe != -1) {
MACRUM 0:276e7a263c35 2320 tr_info("Observe:\t\t%" PRId32"", coap_header_ptr->options_list_ptr->observe);
MACRUM 0:276e7a263c35 2321 }
MACRUM 0:276e7a263c35 2322 if (coap_header_ptr->options_list_ptr->block1 != -1) {
MACRUM 0:276e7a263c35 2323 tr_info("Block1 number:\t%" PRId32"", coap_header_ptr->options_list_ptr->block1 >> 4);
MACRUM 0:276e7a263c35 2324 uint8_t temp = (coap_header_ptr->options_list_ptr->block1 & 0x07);
MACRUM 0:276e7a263c35 2325 uint16_t block_size = 1u << (temp + 4);
MACRUM 0:276e7a263c35 2326 tr_info("Block1 size:\t%d", block_size);
MACRUM 0:276e7a263c35 2327 tr_info("Block1 more:\t%d", (coap_header_ptr->options_list_ptr->block1) & 0x08 ? true : false);
MACRUM 0:276e7a263c35 2328 }
MACRUM 0:276e7a263c35 2329 if (coap_header_ptr->options_list_ptr->block2 != -1) {
MACRUM 0:276e7a263c35 2330 tr_info("Block2 number:\t%" PRId32"", coap_header_ptr->options_list_ptr->block2 >> 4);
MACRUM 0:276e7a263c35 2331 uint8_t temp = (coap_header_ptr->options_list_ptr->block2 & 0x07);
MACRUM 0:276e7a263c35 2332 uint16_t block_size = 1u << (temp + 4);
MACRUM 0:276e7a263c35 2333 tr_info("Block2 size:\t%d", block_size);
MACRUM 0:276e7a263c35 2334 tr_info("Block2 more:\t%d", (coap_header_ptr->options_list_ptr->block2) & 0x08 ? true : false);
MACRUM 0:276e7a263c35 2335 }
MACRUM 0:276e7a263c35 2336 }
MACRUM 0:276e7a263c35 2337 tr_info("======== End of CoAP package ========");
MACRUM 0:276e7a263c35 2338 #else
MACRUM 0:276e7a263c35 2339 (void) coap_header_ptr;
MACRUM 0:276e7a263c35 2340 (void) outgoing;
MACRUM 0:276e7a263c35 2341 #endif
MACRUM 0:276e7a263c35 2342 }
MACRUM 0:276e7a263c35 2343
MACRUM 0:276e7a263c35 2344 #if defined(FEA_TRACE_SUPPORT) || MBED_CONF_MBED_TRACE_ENABLE || YOTTA_CFG_MBED_TRACE || (defined(YOTTA_CFG) && !defined(NDEBUG))
MACRUM 0:276e7a263c35 2345 const char *sn_nsdl_coap_status_description(sn_coap_status_e status)
MACRUM 0:276e7a263c35 2346 {
MACRUM 0:276e7a263c35 2347 switch(status) {
MACRUM 0:276e7a263c35 2348 case COAP_STATUS_OK:
MACRUM 0:276e7a263c35 2349 return "COAP_STATUS_OK";
MACRUM 0:276e7a263c35 2350 case COAP_STATUS_PARSER_ERROR_IN_HEADER:
MACRUM 0:276e7a263c35 2351 return "COAP_STATUS_PARSER_ERROR_IN_HEADER";
MACRUM 0:276e7a263c35 2352 case COAP_STATUS_PARSER_DUPLICATED_MSG:
MACRUM 0:276e7a263c35 2353 return "COAP_STATUS_PARSER_DUPLICATED_MSG";
MACRUM 0:276e7a263c35 2354 case COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING:
MACRUM 0:276e7a263c35 2355 return "COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVING";
MACRUM 0:276e7a263c35 2356 case COAP_STATUS_PARSER_BLOCKWISE_ACK:
MACRUM 0:276e7a263c35 2357 return "COAP_STATUS_PARSER_BLOCKWISE_ACK";
MACRUM 0:276e7a263c35 2358 case COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED:
MACRUM 0:276e7a263c35 2359 return "COAP_STATUS_PARSER_BLOCKWISE_MSG_REJECTED";
MACRUM 0:276e7a263c35 2360 case COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED:
MACRUM 0:276e7a263c35 2361 return "COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED";
MACRUM 0:276e7a263c35 2362 case COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED:
MACRUM 0:276e7a263c35 2363 return "COAP_STATUS_BUILDER_MESSAGE_SENDING_FAILED";
MACRUM 0:276e7a263c35 2364 default:
MACRUM 0:276e7a263c35 2365 return "";
MACRUM 0:276e7a263c35 2366 }
MACRUM 0:276e7a263c35 2367 }
MACRUM 0:276e7a263c35 2368
MACRUM 0:276e7a263c35 2369 const char *sn_nsdl_coap_message_code_desc(int msg_code)
MACRUM 0:276e7a263c35 2370 {
MACRUM 0:276e7a263c35 2371 switch(msg_code) {
MACRUM 0:276e7a263c35 2372 case COAP_MSG_CODE_EMPTY:
MACRUM 0:276e7a263c35 2373 return "COAP_MSG_CODE_EMPTY";
MACRUM 0:276e7a263c35 2374 case COAP_MSG_CODE_REQUEST_GET:
MACRUM 0:276e7a263c35 2375 return "COAP_MSG_CODE_REQUEST_GET";
MACRUM 0:276e7a263c35 2376 case COAP_MSG_CODE_REQUEST_POST:
MACRUM 0:276e7a263c35 2377 return "COAP_MSG_CODE_REQUEST_POST";
MACRUM 0:276e7a263c35 2378 case COAP_MSG_CODE_REQUEST_PUT:
MACRUM 0:276e7a263c35 2379 return "COAP_MSG_CODE_REQUEST_PUT";
MACRUM 0:276e7a263c35 2380 case COAP_MSG_CODE_REQUEST_DELETE:
MACRUM 0:276e7a263c35 2381 return "COAP_MSG_CODE_REQUEST_DELETE";
MACRUM 0:276e7a263c35 2382 case COAP_MSG_CODE_RESPONSE_CREATED:
MACRUM 0:276e7a263c35 2383 return "COAP_MSG_CODE_RESPONSE_CREATED";
MACRUM 0:276e7a263c35 2384 case COAP_MSG_CODE_RESPONSE_DELETED:
MACRUM 0:276e7a263c35 2385 return "COAP_MSG_CODE_RESPONSE_DELETED";
MACRUM 0:276e7a263c35 2386 case COAP_MSG_CODE_RESPONSE_VALID:
MACRUM 0:276e7a263c35 2387 return "COAP_MSG_CODE_RESPONSE_VALID";
MACRUM 0:276e7a263c35 2388 case COAP_MSG_CODE_RESPONSE_CHANGED:
MACRUM 0:276e7a263c35 2389 return "COAP_MSG_CODE_RESPONSE_CHANGED";
MACRUM 0:276e7a263c35 2390 case COAP_MSG_CODE_RESPONSE_CONTENT:
MACRUM 0:276e7a263c35 2391 return "COAP_MSG_CODE_RESPONSE_CONTENT";
MACRUM 0:276e7a263c35 2392 case COAP_MSG_CODE_RESPONSE_CONTINUE:
MACRUM 0:276e7a263c35 2393 return "COAP_MSG_CODE_RESPONSE_CONTINUE";
MACRUM 0:276e7a263c35 2394 case COAP_MSG_CODE_RESPONSE_BAD_REQUEST:
MACRUM 0:276e7a263c35 2395 return "COAP_MSG_CODE_RESPONSE_BAD_REQUEST";
MACRUM 0:276e7a263c35 2396 case COAP_MSG_CODE_RESPONSE_UNAUTHORIZED:
MACRUM 0:276e7a263c35 2397 return "COAP_MSG_CODE_RESPONSE_UNAUTHORIZED";
MACRUM 0:276e7a263c35 2398 case COAP_MSG_CODE_RESPONSE_BAD_OPTION:
MACRUM 0:276e7a263c35 2399 return "COAP_MSG_CODE_RESPONSE_BAD_OPTION";
MACRUM 0:276e7a263c35 2400 case COAP_MSG_CODE_RESPONSE_FORBIDDEN:
MACRUM 0:276e7a263c35 2401 return "COAP_MSG_CODE_RESPONSE_FORBIDDEN";
MACRUM 0:276e7a263c35 2402 case COAP_MSG_CODE_RESPONSE_NOT_FOUND:
MACRUM 0:276e7a263c35 2403 return "COAP_MSG_CODE_RESPONSE_NOT_FOUND";
MACRUM 0:276e7a263c35 2404 case COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED:
MACRUM 0:276e7a263c35 2405 return "COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED";
MACRUM 0:276e7a263c35 2406 case COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE:
MACRUM 0:276e7a263c35 2407 return "COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE";
MACRUM 0:276e7a263c35 2408 case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE:
MACRUM 0:276e7a263c35 2409 return "COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_INCOMPLETE";
MACRUM 0:276e7a263c35 2410 case COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED:
MACRUM 0:276e7a263c35 2411 return "COAP_MSG_CODE_RESPONSE_PRECONDITION_FAILED";
MACRUM 0:276e7a263c35 2412 case COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE:
MACRUM 0:276e7a263c35 2413 return "COAP_MSG_CODE_RESPONSE_REQUEST_ENTITY_TOO_LARGE";
MACRUM 0:276e7a263c35 2414 case COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT:
MACRUM 0:276e7a263c35 2415 return "COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT";
MACRUM 0:276e7a263c35 2416 case COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR:
MACRUM 0:276e7a263c35 2417 return "COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR";
MACRUM 0:276e7a263c35 2418 case COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED:
MACRUM 0:276e7a263c35 2419 return "COAP_MSG_CODE_RESPONSE_NOT_IMPLEMENTED";
MACRUM 0:276e7a263c35 2420 case COAP_MSG_CODE_RESPONSE_BAD_GATEWAY:
MACRUM 0:276e7a263c35 2421 return "COAP_MSG_CODE_RESPONSE_BAD_GATEWAY";
MACRUM 0:276e7a263c35 2422 case COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE:
MACRUM 0:276e7a263c35 2423 return "COAP_MSG_CODE_RESPONSE_SERVICE_UNAVAILABLE";
MACRUM 0:276e7a263c35 2424 case COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT:
MACRUM 0:276e7a263c35 2425 return "COAP_MSG_CODE_RESPONSE_GATEWAY_TIMEOUT";
MACRUM 0:276e7a263c35 2426 case COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED:
MACRUM 0:276e7a263c35 2427 return "COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED";
MACRUM 0:276e7a263c35 2428 default:
MACRUM 0:276e7a263c35 2429 return "";
MACRUM 0:276e7a263c35 2430 }
MACRUM 0:276e7a263c35 2431 }
MACRUM 0:276e7a263c35 2432
MACRUM 0:276e7a263c35 2433 const char *sn_nsdl_coap_message_type_desc(int msg_type)
MACRUM 0:276e7a263c35 2434 {
MACRUM 0:276e7a263c35 2435 switch(msg_type) {
MACRUM 0:276e7a263c35 2436 case COAP_MSG_TYPE_CONFIRMABLE:
MACRUM 0:276e7a263c35 2437 return "COAP_MSG_TYPE_CONFIRMABLE";
MACRUM 0:276e7a263c35 2438 case COAP_MSG_TYPE_NON_CONFIRMABLE:
MACRUM 0:276e7a263c35 2439 return "COAP_MSG_TYPE_NON_CONFIRMABLE";
MACRUM 0:276e7a263c35 2440 case COAP_MSG_TYPE_ACKNOWLEDGEMENT:
MACRUM 0:276e7a263c35 2441 return "COAP_MSG_TYPE_ACKNOWLEDGEMENT";
MACRUM 0:276e7a263c35 2442 case COAP_MSG_TYPE_RESET:
MACRUM 0:276e7a263c35 2443 return "COAP_MSG_TYPE_RESET";
MACRUM 0:276e7a263c35 2444 default:
MACRUM 0:276e7a263c35 2445 return "";
MACRUM 0:276e7a263c35 2446 }
MACRUM 0:276e7a263c35 2447 }
MACRUM 0:276e7a263c35 2448 #endif
MACRUM 0:276e7a263c35 2449
MACRUM 0:276e7a263c35 2450 void remove_previous_block_data(struct nsdl_s *handle, sn_nsdl_addr_s *src_ptr, const uint32_t block_number)
MACRUM 0:276e7a263c35 2451 {
MACRUM 0:276e7a263c35 2452 #if SN_COAP_MAX_BLOCKWISE_PAYLOAD_SIZE
MACRUM 0:276e7a263c35 2453 ns_list_foreach(coap_blockwise_payload_s, stored_payload_info_ptr, &handle->grs->coap->linked_list_blockwise_received_payloads) {
MACRUM 0:276e7a263c35 2454 uint32_t stored_number = stored_payload_info_ptr->block_number;
MACRUM 0:276e7a263c35 2455 // Remove the previous block data
MACRUM 0:276e7a263c35 2456 if (block_number - 1 == stored_number) {
MACRUM 0:276e7a263c35 2457 sn_coap_protocol_block_remove(handle->grs->coap,
MACRUM 0:276e7a263c35 2458 src_ptr,
MACRUM 0:276e7a263c35 2459 stored_payload_info_ptr->payload_len,
MACRUM 0:276e7a263c35 2460 stored_payload_info_ptr->payload_ptr);
MACRUM 0:276e7a263c35 2461 break;
MACRUM 0:276e7a263c35 2462 }
MACRUM 0:276e7a263c35 2463 }
MACRUM 0:276e7a263c35 2464 #endif
MACRUM 0:276e7a263c35 2465 }
MACRUM 0:276e7a263c35 2466
MACRUM 0:276e7a263c35 2467 bool update_last_block_data(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, bool block1)
MACRUM 0:276e7a263c35 2468 {
MACRUM 0:276e7a263c35 2469 bool data_updated = false;
MACRUM 0:276e7a263c35 2470 // Whole message received --> pass only the last block data to application
MACRUM 0:276e7a263c35 2471 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
MACRUM 0:276e7a263c35 2472 // Get the block size
MACRUM 0:276e7a263c35 2473 uint8_t temp = 0;
MACRUM 0:276e7a263c35 2474 if (block1) {
MACRUM 0:276e7a263c35 2475 temp = (coap_packet_ptr->options_list_ptr->block1 & 0x07);
MACRUM 0:276e7a263c35 2476 } else {
MACRUM 0:276e7a263c35 2477 temp = (coap_packet_ptr->options_list_ptr->block2 & 0x07);
MACRUM 0:276e7a263c35 2478 }
MACRUM 0:276e7a263c35 2479 uint16_t block_size = 1u << (temp + 4);
MACRUM 0:276e7a263c35 2480
MACRUM 0:276e7a263c35 2481 uint32_t new_payload_len = coap_packet_ptr->payload_len - block_size;
MACRUM 0:276e7a263c35 2482 uint8_t *temp_ptr = handle->grs->coap->sn_coap_protocol_malloc(new_payload_len);
MACRUM 0:276e7a263c35 2483 if (temp_ptr) {
MACRUM 0:276e7a263c35 2484 // Skip the second last block data since it's still stored in mbed-coap list!
MACRUM 0:276e7a263c35 2485 memcpy(temp_ptr, coap_packet_ptr->payload_ptr + block_size, new_payload_len);
MACRUM 0:276e7a263c35 2486 handle->grs->coap->sn_coap_protocol_free(coap_packet_ptr->payload_ptr);
MACRUM 0:276e7a263c35 2487 coap_packet_ptr->payload_ptr = temp_ptr;
MACRUM 0:276e7a263c35 2488 coap_packet_ptr->payload_len = new_payload_len;
MACRUM 0:276e7a263c35 2489 data_updated = true;
MACRUM 0:276e7a263c35 2490 }
MACRUM 0:276e7a263c35 2491 }
MACRUM 0:276e7a263c35 2492
MACRUM 0:276e7a263c35 2493 return data_updated;
MACRUM 0:276e7a263c35 2494 }