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