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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16
leothedragon 0:8f0bb79ddd48 17 /**
leothedragon 0:8f0bb79ddd48 18 *
leothedragon 0:8f0bb79ddd48 19 * \file sn_grs.c
leothedragon 0:8f0bb79ddd48 20 *
leothedragon 0:8f0bb79ddd48 21 * \brief General resource server.
leothedragon 0:8f0bb79ddd48 22 *
leothedragon 0:8f0bb79ddd48 23 */
leothedragon 0:8f0bb79ddd48 24 #include <string.h>
leothedragon 0:8f0bb79ddd48 25 #include <stdlib.h>
leothedragon 0:8f0bb79ddd48 26 #include "ns_list.h"
leothedragon 0:8f0bb79ddd48 27 #include "ns_types.h"
leothedragon 0:8f0bb79ddd48 28 #include "sn_nsdl.h"
leothedragon 0:8f0bb79ddd48 29 #include "sn_coap_header.h"
leothedragon 0:8f0bb79ddd48 30 #include "sn_coap_protocol.h"
leothedragon 0:8f0bb79ddd48 31 #include "source/include/sn_coap_protocol_internal.h"
leothedragon 0:8f0bb79ddd48 32 #include "sn_nsdl_lib.h"
leothedragon 0:8f0bb79ddd48 33 #include "sn_grs.h"
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 /* Defines */
leothedragon 0:8f0bb79ddd48 36 #define WELLKNOWN_PATH_LEN 16
leothedragon 0:8f0bb79ddd48 37 #define WELLKNOWN_PATH (".well-known/core")
leothedragon 0:8f0bb79ddd48 38
leothedragon 0:8f0bb79ddd48 39 /* Local static function prototypes */
leothedragon 0:8f0bb79ddd48 40 static int8_t sn_grs_resource_info_free(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *resource_ptr);
leothedragon 0:8f0bb79ddd48 41 static char *sn_grs_convert_uri(uint16_t *uri_len, const char *uri_ptr);
leothedragon 0:8f0bb79ddd48 42 static int8_t sn_grs_core_request(struct nsdl_s *handle, sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 43 static uint8_t coap_tx_callback(uint8_t *, uint16_t, sn_nsdl_addr_s *, void *);
leothedragon 0:8f0bb79ddd48 44 static int8_t coap_rx_callback(sn_coap_hdr_s *coap_ptr, sn_nsdl_addr_s *address_ptr, void *param);
leothedragon 0:8f0bb79ddd48 45
leothedragon 0:8f0bb79ddd48 46 /* Extern function prototypes */
leothedragon 0:8f0bb79ddd48 47 extern int8_t sn_nsdl_build_registration_body(struct nsdl_s *handle, sn_coap_hdr_s *message_ptr, uint8_t updating_registeration);
leothedragon 0:8f0bb79ddd48 48
leothedragon 0:8f0bb79ddd48 49 /**
leothedragon 0:8f0bb79ddd48 50 * \fn int8_t sn_grs_destroy(void)
leothedragon 0:8f0bb79ddd48 51 * \brief This function may be used to flush GRS related stuff when a program exits.
leothedragon 0:8f0bb79ddd48 52 * @return always 0.
leothedragon 0:8f0bb79ddd48 53 */
leothedragon 0:8f0bb79ddd48 54 extern int8_t sn_grs_destroy(struct grs_s *handle)
leothedragon 0:8f0bb79ddd48 55 {
leothedragon 0:8f0bb79ddd48 56 if( handle == NULL ){
leothedragon 0:8f0bb79ddd48 57 return 0;
leothedragon 0:8f0bb79ddd48 58 }
leothedragon 0:8f0bb79ddd48 59 ns_list_foreach_safe(sn_nsdl_dynamic_resource_parameters_s, tmp, &handle->resource_root_list) {
leothedragon 0:8f0bb79ddd48 60 ns_list_remove(&handle->resource_root_list, tmp);
leothedragon 0:8f0bb79ddd48 61 --handle->resource_root_count;
leothedragon 0:8f0bb79ddd48 62 sn_grs_resource_info_free(handle, tmp);
leothedragon 0:8f0bb79ddd48 63 }
leothedragon 0:8f0bb79ddd48 64 handle->sn_grs_free(handle);
leothedragon 0:8f0bb79ddd48 65
leothedragon 0:8f0bb79ddd48 66 return 0;
leothedragon 0:8f0bb79ddd48 67 }
leothedragon 0:8f0bb79ddd48 68
leothedragon 0:8f0bb79ddd48 69 static uint8_t coap_tx_callback(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param)
leothedragon 0:8f0bb79ddd48 70 {
leothedragon 0:8f0bb79ddd48 71 struct nsdl_s *handle = (struct nsdl_s *)param;
leothedragon 0:8f0bb79ddd48 72
leothedragon 0:8f0bb79ddd48 73 if (handle == NULL) {
leothedragon 0:8f0bb79ddd48 74 return 0;
leothedragon 0:8f0bb79ddd48 75 }
leothedragon 0:8f0bb79ddd48 76
leothedragon 0:8f0bb79ddd48 77 return handle->grs->sn_grs_tx_callback(handle, SN_NSDL_PROTOCOL_COAP, data_ptr, data_len, address_ptr);
leothedragon 0:8f0bb79ddd48 78 }
leothedragon 0:8f0bb79ddd48 79
leothedragon 0:8f0bb79ddd48 80 static int8_t coap_rx_callback(sn_coap_hdr_s *coap_ptr, sn_nsdl_addr_s *address_ptr, void *param)
leothedragon 0:8f0bb79ddd48 81 {
leothedragon 0:8f0bb79ddd48 82 struct nsdl_s *handle = (struct nsdl_s *)param;
leothedragon 0:8f0bb79ddd48 83
leothedragon 0:8f0bb79ddd48 84 if (handle == NULL) {
leothedragon 0:8f0bb79ddd48 85 return 0;
leothedragon 0:8f0bb79ddd48 86 }
leothedragon 0:8f0bb79ddd48 87
leothedragon 0:8f0bb79ddd48 88 return handle->sn_nsdl_rx_callback(handle, coap_ptr, address_ptr);
leothedragon 0:8f0bb79ddd48 89 }
leothedragon 0:8f0bb79ddd48 90
leothedragon 0:8f0bb79ddd48 91 /**
leothedragon 0:8f0bb79ddd48 92 * \fn int8_t sn_grs_init (uint8_t (*sn_grs_tx_callback_ptr)(sn_nsdl_capab_e , uint8_t *, uint16_t,
leothedragon 0:8f0bb79ddd48 93 * sn_nsdl_addr_s *), int8_t (*sn_grs_rx_callback_ptr)(sn_coap_hdr_s *, sn_nsdl_addr_s *), sn_nsdl_mem_s *sn_memory)
leothedragon 0:8f0bb79ddd48 94 *
leothedragon 0:8f0bb79ddd48 95 * \brief GRS library initialize function.
leothedragon 0:8f0bb79ddd48 96 *
leothedragon 0:8f0bb79ddd48 97 * This function initializes GRS and CoAP libraries.
leothedragon 0:8f0bb79ddd48 98 *
leothedragon 0:8f0bb79ddd48 99 * \param sn_grs_tx_callback A function pointer to a transmit callback function.
leothedragon 0:8f0bb79ddd48 100 * \param *sn_grs_rx_callback_ptr A function pointer to a receiving callback function. If received packet is not for GRS, it will be passed to
leothedragon 0:8f0bb79ddd48 101 * upper level (NSDL) to be proceed.
leothedragon 0:8f0bb79ddd48 102 * \param sn_memory A pointer to a structure containing the platform specific functions for memory allocation and free.
leothedragon 0:8f0bb79ddd48 103 *
leothedragon 0:8f0bb79ddd48 104 * \return success = 0, failure = -1
leothedragon 0:8f0bb79ddd48 105 *
leothedragon 0:8f0bb79ddd48 106 */
leothedragon 0:8f0bb79ddd48 107 extern struct grs_s *sn_grs_init(uint8_t (*sn_grs_tx_callback_ptr)(struct nsdl_s *, sn_nsdl_capab_e , uint8_t *, uint16_t,
leothedragon 0:8f0bb79ddd48 108 sn_nsdl_addr_s *), int8_t (*sn_grs_rx_callback_ptr)(struct nsdl_s *, sn_coap_hdr_s *, sn_nsdl_addr_s *),
leothedragon 0:8f0bb79ddd48 109 void *(*sn_grs_alloc)(uint16_t), void (*sn_grs_free)(void *))
leothedragon 0:8f0bb79ddd48 110 {
leothedragon 0:8f0bb79ddd48 111
leothedragon 0:8f0bb79ddd48 112 struct grs_s *handle_ptr = NULL;
leothedragon 0:8f0bb79ddd48 113
leothedragon 0:8f0bb79ddd48 114 /* Check parameters */
leothedragon 0:8f0bb79ddd48 115 if (sn_grs_alloc == NULL || sn_grs_free == NULL ||
leothedragon 0:8f0bb79ddd48 116 sn_grs_tx_callback_ptr == NULL || sn_grs_rx_callback_ptr == NULL) {
leothedragon 0:8f0bb79ddd48 117 return NULL;
leothedragon 0:8f0bb79ddd48 118 }
leothedragon 0:8f0bb79ddd48 119
leothedragon 0:8f0bb79ddd48 120 handle_ptr = sn_grs_alloc(sizeof(struct grs_s));
leothedragon 0:8f0bb79ddd48 121
leothedragon 0:8f0bb79ddd48 122 if (handle_ptr == NULL) {
leothedragon 0:8f0bb79ddd48 123 return NULL;
leothedragon 0:8f0bb79ddd48 124 }
leothedragon 0:8f0bb79ddd48 125
leothedragon 0:8f0bb79ddd48 126 memset(handle_ptr, 0, sizeof(struct grs_s));
leothedragon 0:8f0bb79ddd48 127
leothedragon 0:8f0bb79ddd48 128 /* Allocation and free - function pointers */
leothedragon 0:8f0bb79ddd48 129 handle_ptr->sn_grs_alloc = sn_grs_alloc;
leothedragon 0:8f0bb79ddd48 130 handle_ptr->sn_grs_free = sn_grs_free;
leothedragon 0:8f0bb79ddd48 131
leothedragon 0:8f0bb79ddd48 132 /* TX callback function pointer */
leothedragon 0:8f0bb79ddd48 133 handle_ptr->sn_grs_tx_callback = sn_grs_tx_callback_ptr;
leothedragon 0:8f0bb79ddd48 134 handle_ptr->sn_grs_rx_callback = sn_grs_rx_callback_ptr;
leothedragon 0:8f0bb79ddd48 135
leothedragon 0:8f0bb79ddd48 136 /* Initialize CoAP protocol library */
leothedragon 0:8f0bb79ddd48 137 handle_ptr->coap = sn_coap_protocol_init(sn_grs_alloc, sn_grs_free, coap_tx_callback, coap_rx_callback);
leothedragon 0:8f0bb79ddd48 138
leothedragon 0:8f0bb79ddd48 139 return handle_ptr;
leothedragon 0:8f0bb79ddd48 140 }
leothedragon 0:8f0bb79ddd48 141
leothedragon 0:8f0bb79ddd48 142 extern sn_nsdl_dynamic_resource_parameters_s *sn_grs_get_first_resource(struct grs_s *handle)
leothedragon 0:8f0bb79ddd48 143 {
leothedragon 0:8f0bb79ddd48 144 if( !handle ){
leothedragon 0:8f0bb79ddd48 145 return NULL;
leothedragon 0:8f0bb79ddd48 146 }
leothedragon 0:8f0bb79ddd48 147 return ns_list_get_first(&handle->resource_root_list);
leothedragon 0:8f0bb79ddd48 148 }
leothedragon 0:8f0bb79ddd48 149
leothedragon 0:8f0bb79ddd48 150 extern sn_nsdl_dynamic_resource_parameters_s *sn_grs_get_next_resource(struct grs_s *handle,
leothedragon 0:8f0bb79ddd48 151 const sn_nsdl_dynamic_resource_parameters_s *sn_grs_current_resource)
leothedragon 0:8f0bb79ddd48 152 {
leothedragon 0:8f0bb79ddd48 153 if( !handle || !sn_grs_current_resource ){
leothedragon 0:8f0bb79ddd48 154 return NULL;
leothedragon 0:8f0bb79ddd48 155 }
leothedragon 0:8f0bb79ddd48 156 return ns_list_get_next(&handle->resource_root_list, sn_grs_current_resource);
leothedragon 0:8f0bb79ddd48 157 }
leothedragon 0:8f0bb79ddd48 158
leothedragon 0:8f0bb79ddd48 159 extern int8_t sn_grs_delete_resource(struct grs_s *handle, const char *path)
leothedragon 0:8f0bb79ddd48 160 {
leothedragon 0:8f0bb79ddd48 161 /* Local variables */
leothedragon 0:8f0bb79ddd48 162 sn_nsdl_dynamic_resource_parameters_s *resource_temp = NULL;
leothedragon 0:8f0bb79ddd48 163
leothedragon 0:8f0bb79ddd48 164 /* Search if resource found */
leothedragon 0:8f0bb79ddd48 165 resource_temp = sn_grs_search_resource(handle, path, SN_GRS_SEARCH_METHOD);
leothedragon 0:8f0bb79ddd48 166
leothedragon 0:8f0bb79ddd48 167 /* If not found */
leothedragon 0:8f0bb79ddd48 168 if (resource_temp == NULL) {
leothedragon 0:8f0bb79ddd48 169 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 170 }
leothedragon 0:8f0bb79ddd48 171
leothedragon 0:8f0bb79ddd48 172 /* If found, delete it and delete also subresources, if there is any */
leothedragon 0:8f0bb79ddd48 173 do {
leothedragon 0:8f0bb79ddd48 174 /* Remove from list */
leothedragon 0:8f0bb79ddd48 175 ns_list_remove(&handle->resource_root_list, resource_temp);
leothedragon 0:8f0bb79ddd48 176 --handle->resource_root_count;
leothedragon 0:8f0bb79ddd48 177
leothedragon 0:8f0bb79ddd48 178 /* Free */
leothedragon 0:8f0bb79ddd48 179 sn_grs_resource_info_free(handle, resource_temp);
leothedragon 0:8f0bb79ddd48 180
leothedragon 0:8f0bb79ddd48 181 /* Search for subresources */
leothedragon 0:8f0bb79ddd48 182 resource_temp = sn_grs_search_resource(handle, path, SN_GRS_DELETE_METHOD);
leothedragon 0:8f0bb79ddd48 183 } while (resource_temp != NULL);
leothedragon 0:8f0bb79ddd48 184
leothedragon 0:8f0bb79ddd48 185 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 186 }
leothedragon 0:8f0bb79ddd48 187
leothedragon 0:8f0bb79ddd48 188 int8_t sn_grs_put_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *res)
leothedragon 0:8f0bb79ddd48 189 {
leothedragon 0:8f0bb79ddd48 190 if (!res || !handle) {
leothedragon 0:8f0bb79ddd48 191 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 192 }
leothedragon 0:8f0bb79ddd48 193
leothedragon 0:8f0bb79ddd48 194 /* Check path validity */
leothedragon 0:8f0bb79ddd48 195 if (!res->static_resource_parameters->path || res->static_resource_parameters->path[0] == '\0') {
leothedragon 0:8f0bb79ddd48 196 return SN_GRS_INVALID_PATH;
leothedragon 0:8f0bb79ddd48 197 }
leothedragon 0:8f0bb79ddd48 198
leothedragon 0:8f0bb79ddd48 199 /* Check if resource already exists */
leothedragon 0:8f0bb79ddd48 200 if (sn_grs_search_resource(handle,
leothedragon 0:8f0bb79ddd48 201 res->static_resource_parameters->path, SN_GRS_SEARCH_METHOD) != (sn_nsdl_dynamic_resource_parameters_s *)NULL) {
leothedragon 0:8f0bb79ddd48 202 return SN_GRS_RESOURCE_ALREADY_EXISTS;
leothedragon 0:8f0bb79ddd48 203 }
leothedragon 0:8f0bb79ddd48 204
leothedragon 0:8f0bb79ddd48 205 res->registered = SN_NDSL_RESOURCE_NOT_REGISTERED;
leothedragon 0:8f0bb79ddd48 206
leothedragon 0:8f0bb79ddd48 207 ns_list_add_to_start(&handle->resource_root_list, res);
leothedragon 0:8f0bb79ddd48 208 ++handle->resource_root_count;
leothedragon 0:8f0bb79ddd48 209
leothedragon 0:8f0bb79ddd48 210 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 211 }
leothedragon 0:8f0bb79ddd48 212
leothedragon 0:8f0bb79ddd48 213 int8_t sn_grs_pop_resource(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *res)
leothedragon 0:8f0bb79ddd48 214 {
leothedragon 0:8f0bb79ddd48 215 if (!res || !handle) {
leothedragon 0:8f0bb79ddd48 216 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 217 }
leothedragon 0:8f0bb79ddd48 218
leothedragon 0:8f0bb79ddd48 219 /* Check path validity */
leothedragon 0:8f0bb79ddd48 220 if (!res->static_resource_parameters->path || res->static_resource_parameters->path[0] == '\0') {
leothedragon 0:8f0bb79ddd48 221 return SN_GRS_INVALID_PATH;
leothedragon 0:8f0bb79ddd48 222 }
leothedragon 0:8f0bb79ddd48 223
leothedragon 0:8f0bb79ddd48 224 /* Check if resource exists on list. */
leothedragon 0:8f0bb79ddd48 225 if (sn_grs_search_resource(handle,
leothedragon 0:8f0bb79ddd48 226 res->static_resource_parameters->path, SN_GRS_SEARCH_METHOD) == (sn_nsdl_dynamic_resource_parameters_s *)NULL) {
leothedragon 0:8f0bb79ddd48 227 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 228 }
leothedragon 0:8f0bb79ddd48 229
leothedragon 0:8f0bb79ddd48 230 ns_list_remove(&handle->resource_root_list, res);
leothedragon 0:8f0bb79ddd48 231 --handle->resource_root_count;
leothedragon 0:8f0bb79ddd48 232
leothedragon 0:8f0bb79ddd48 233 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 234 }
leothedragon 0:8f0bb79ddd48 235
leothedragon 0:8f0bb79ddd48 236 /**
leothedragon 0:8f0bb79ddd48 237 * \fn extern int8_t sn_grs_process_coap(uint8_t *packet, uint16_t *packet_len, sn_nsdl_addr_s *src)
leothedragon 0:8f0bb79ddd48 238 *
leothedragon 0:8f0bb79ddd48 239 * \brief To push CoAP packet to GRS library
leothedragon 0:8f0bb79ddd48 240 *
leothedragon 0:8f0bb79ddd48 241 * Used to push an CoAP packet to GRS library for processing.
leothedragon 0:8f0bb79ddd48 242 *
leothedragon 0:8f0bb79ddd48 243 * \param *packet Pointer to a uint8_t array containing the packet (including the CoAP headers).
leothedragon 0:8f0bb79ddd48 244 * After successful execution this array may contain the response packet.
leothedragon 0:8f0bb79ddd48 245 *
leothedragon 0:8f0bb79ddd48 246 * \param *packet_len Pointer to length of the packet. After successful execution this array may contain the length
leothedragon 0:8f0bb79ddd48 247 * of the response packet.
leothedragon 0:8f0bb79ddd48 248 *
leothedragon 0:8f0bb79ddd48 249 * \param *src Pointer to packet source address information. After successful execution this array may contain
leothedragon 0:8f0bb79ddd48 250 * the destination address of the response packet.
leothedragon 0:8f0bb79ddd48 251 *
leothedragon 0:8f0bb79ddd48 252 * \return 0 = success, -1 = failure
leothedragon 0:8f0bb79ddd48 253 */
leothedragon 0:8f0bb79ddd48 254 extern int8_t sn_grs_process_coap(struct nsdl_s *nsdl_handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *src_addr_ptr)
leothedragon 0:8f0bb79ddd48 255 {
leothedragon 0:8f0bb79ddd48 256 if( !coap_packet_ptr || !nsdl_handle){
leothedragon 0:8f0bb79ddd48 257 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 258 }
leothedragon 0:8f0bb79ddd48 259
leothedragon 0:8f0bb79ddd48 260 sn_nsdl_dynamic_resource_parameters_s *resource_temp_ptr = NULL;
leothedragon 0:8f0bb79ddd48 261 sn_coap_msg_code_e status = COAP_MSG_CODE_EMPTY;
leothedragon 0:8f0bb79ddd48 262 sn_coap_hdr_s *response_message_hdr_ptr = NULL;
leothedragon 0:8f0bb79ddd48 263 struct grs_s *handle = nsdl_handle->grs;
leothedragon 0:8f0bb79ddd48 264 bool static_get_request = false;
leothedragon 0:8f0bb79ddd48 265
leothedragon 0:8f0bb79ddd48 266 if (coap_packet_ptr->msg_code <= COAP_MSG_CODE_REQUEST_DELETE) {
leothedragon 0:8f0bb79ddd48 267 /* Check if .well-known/core */
leothedragon 0:8f0bb79ddd48 268 if (coap_packet_ptr->uri_path_len == WELLKNOWN_PATH_LEN && memcmp(coap_packet_ptr->uri_path_ptr, WELLKNOWN_PATH, WELLKNOWN_PATH_LEN) == 0) {
leothedragon 0:8f0bb79ddd48 269 return sn_grs_core_request(nsdl_handle, src_addr_ptr, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 270 }
leothedragon 0:8f0bb79ddd48 271
leothedragon 0:8f0bb79ddd48 272 /* Get resource */
leothedragon 0:8f0bb79ddd48 273 char* path = nsdl_handle->grs->sn_grs_alloc(coap_packet_ptr->uri_path_len + 1);
leothedragon 0:8f0bb79ddd48 274 if (!path) {
leothedragon 0:8f0bb79ddd48 275 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 276 }
leothedragon 0:8f0bb79ddd48 277
leothedragon 0:8f0bb79ddd48 278 memcpy(path,
leothedragon 0:8f0bb79ddd48 279 coap_packet_ptr->uri_path_ptr,
leothedragon 0:8f0bb79ddd48 280 coap_packet_ptr->uri_path_len);
leothedragon 0:8f0bb79ddd48 281 path[coap_packet_ptr->uri_path_len] = '\0';
leothedragon 0:8f0bb79ddd48 282
leothedragon 0:8f0bb79ddd48 283 resource_temp_ptr = sn_grs_search_resource(handle, path, SN_GRS_SEARCH_METHOD);
leothedragon 0:8f0bb79ddd48 284 nsdl_handle->grs->sn_grs_free(path);
leothedragon 0:8f0bb79ddd48 285
leothedragon 0:8f0bb79ddd48 286 /* * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 287 /* If resource exists */
leothedragon 0:8f0bb79ddd48 288 /* * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 289 if (resource_temp_ptr) {
leothedragon 0:8f0bb79ddd48 290 /* If dynamic resource, go to callback */
leothedragon 0:8f0bb79ddd48 291 if (resource_temp_ptr->static_resource_parameters->mode == SN_GRS_DYNAMIC) {
leothedragon 0:8f0bb79ddd48 292 /* Check accesses */
leothedragon 0:8f0bb79ddd48 293 if (((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) && !(resource_temp_ptr->access & SN_GRS_GET_ALLOWED)) ||
leothedragon 0:8f0bb79ddd48 294 ((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) && !(resource_temp_ptr->access & SN_GRS_POST_ALLOWED)) ||
leothedragon 0:8f0bb79ddd48 295 ((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) && !(resource_temp_ptr->access & SN_GRS_PUT_ALLOWED)) ||
leothedragon 0:8f0bb79ddd48 296 ((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) && !(resource_temp_ptr->access & SN_GRS_DELETE_ALLOWED))) {
leothedragon 0:8f0bb79ddd48 297 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:8f0bb79ddd48 298 } else {
leothedragon 0:8f0bb79ddd48 299 /* Do not call null pointer.. */
leothedragon 0:8f0bb79ddd48 300 if (resource_temp_ptr->sn_grs_dyn_res_callback != NULL) {
leothedragon 0:8f0bb79ddd48 301 resource_temp_ptr->sn_grs_dyn_res_callback(nsdl_handle, coap_packet_ptr, src_addr_ptr, SN_NSDL_PROTOCOL_COAP);
leothedragon 0:8f0bb79ddd48 302 } else {
leothedragon 0:8f0bb79ddd48 303 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 304 handle->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 305 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 306 }
leothedragon 0:8f0bb79ddd48 307 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 308 }
leothedragon 0:8f0bb79ddd48 309
leothedragon 0:8f0bb79ddd48 310 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 311 }
leothedragon 0:8f0bb79ddd48 312 } else {
leothedragon 0:8f0bb79ddd48 313 /* Static resource handling */
leothedragon 0:8f0bb79ddd48 314 switch (coap_packet_ptr->msg_code) {
leothedragon 0:8f0bb79ddd48 315 case COAP_MSG_CODE_REQUEST_GET:
leothedragon 0:8f0bb79ddd48 316 if (resource_temp_ptr->access & SN_GRS_GET_ALLOWED) {
leothedragon 0:8f0bb79ddd48 317 status = COAP_MSG_CODE_RESPONSE_CONTENT;
leothedragon 0:8f0bb79ddd48 318 static_get_request = true;
leothedragon 0:8f0bb79ddd48 319 } else {
leothedragon 0:8f0bb79ddd48 320 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:8f0bb79ddd48 321 }
leothedragon 0:8f0bb79ddd48 322 break;
leothedragon 0:8f0bb79ddd48 323
leothedragon 0:8f0bb79ddd48 324 case COAP_MSG_CODE_REQUEST_POST:
leothedragon 0:8f0bb79ddd48 325 case COAP_MSG_CODE_REQUEST_PUT:
leothedragon 0:8f0bb79ddd48 326 case COAP_MSG_CODE_REQUEST_DELETE:
leothedragon 0:8f0bb79ddd48 327 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
leothedragon 0:8f0bb79ddd48 328 break;
leothedragon 0:8f0bb79ddd48 329
leothedragon 0:8f0bb79ddd48 330 default:
leothedragon 0:8f0bb79ddd48 331 status = COAP_MSG_CODE_RESPONSE_FORBIDDEN;
leothedragon 0:8f0bb79ddd48 332 break;
leothedragon 0:8f0bb79ddd48 333 }
leothedragon 0:8f0bb79ddd48 334 }
leothedragon 0:8f0bb79ddd48 335 }
leothedragon 0:8f0bb79ddd48 336
leothedragon 0:8f0bb79ddd48 337 /* * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 338 /* If resource was not found */
leothedragon 0:8f0bb79ddd48 339 /* * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 340
leothedragon 0:8f0bb79ddd48 341 else {
leothedragon 0:8f0bb79ddd48 342 if (coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
leothedragon 0:8f0bb79ddd48 343 handle->sn_grs_rx_callback(nsdl_handle, coap_packet_ptr, src_addr_ptr);
leothedragon 0:8f0bb79ddd48 344
leothedragon 0:8f0bb79ddd48 345 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 346 handle->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 347 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 348 }
leothedragon 0:8f0bb79ddd48 349
leothedragon 0:8f0bb79ddd48 350 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 351 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 352 } else {
leothedragon 0:8f0bb79ddd48 353 status = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
leothedragon 0:8f0bb79ddd48 354 }
leothedragon 0:8f0bb79ddd48 355 }
leothedragon 0:8f0bb79ddd48 356 }
leothedragon 0:8f0bb79ddd48 357
leothedragon 0:8f0bb79ddd48 358 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 359 /* If received packed was other than reset, create response */
leothedragon 0:8f0bb79ddd48 360 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
leothedragon 0:8f0bb79ddd48 361 if (coap_packet_ptr->msg_type != COAP_MSG_TYPE_RESET && coap_packet_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT) {
leothedragon 0:8f0bb79ddd48 362
leothedragon 0:8f0bb79ddd48 363 /* Allocate resopnse message */
leothedragon 0:8f0bb79ddd48 364 response_message_hdr_ptr = sn_coap_parser_alloc_message(handle->coap);
leothedragon 0:8f0bb79ddd48 365 if (!response_message_hdr_ptr) {
leothedragon 0:8f0bb79ddd48 366 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 367 handle->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 368 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 369 }
leothedragon 0:8f0bb79ddd48 370 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 371 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 372 }
leothedragon 0:8f0bb79ddd48 373
leothedragon 0:8f0bb79ddd48 374 /* If status has not been defined, response internal server error */
leothedragon 0:8f0bb79ddd48 375 if (status == COAP_MSG_CODE_EMPTY) {
leothedragon 0:8f0bb79ddd48 376 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
leothedragon 0:8f0bb79ddd48 377 }
leothedragon 0:8f0bb79ddd48 378
leothedragon 0:8f0bb79ddd48 379 /* Fill header */
leothedragon 0:8f0bb79ddd48 380 response_message_hdr_ptr->msg_code = status;
leothedragon 0:8f0bb79ddd48 381
leothedragon 0:8f0bb79ddd48 382 if (coap_packet_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
leothedragon 0:8f0bb79ddd48 383 response_message_hdr_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
leothedragon 0:8f0bb79ddd48 384 } else {
leothedragon 0:8f0bb79ddd48 385 response_message_hdr_ptr->msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE;
leothedragon 0:8f0bb79ddd48 386 }
leothedragon 0:8f0bb79ddd48 387
leothedragon 0:8f0bb79ddd48 388 response_message_hdr_ptr->msg_id = coap_packet_ptr->msg_id;
leothedragon 0:8f0bb79ddd48 389
leothedragon 0:8f0bb79ddd48 390 if (coap_packet_ptr->token_ptr) {
leothedragon 0:8f0bb79ddd48 391 response_message_hdr_ptr->token_len = coap_packet_ptr->token_len;
leothedragon 0:8f0bb79ddd48 392 response_message_hdr_ptr->token_ptr = handle->sn_grs_alloc(response_message_hdr_ptr->token_len);
leothedragon 0:8f0bb79ddd48 393 if (!response_message_hdr_ptr->token_ptr) {
leothedragon 0:8f0bb79ddd48 394 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, response_message_hdr_ptr);
leothedragon 0:8f0bb79ddd48 395
leothedragon 0:8f0bb79ddd48 396 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 397 handle->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 398 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 399 }
leothedragon 0:8f0bb79ddd48 400
leothedragon 0:8f0bb79ddd48 401 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 402 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 403 }
leothedragon 0:8f0bb79ddd48 404 memcpy(response_message_hdr_ptr->token_ptr, coap_packet_ptr->token_ptr, response_message_hdr_ptr->token_len);
leothedragon 0:8f0bb79ddd48 405 }
leothedragon 0:8f0bb79ddd48 406
leothedragon 0:8f0bb79ddd48 407 if (status == COAP_MSG_CODE_RESPONSE_CONTENT) {
leothedragon 0:8f0bb79ddd48 408 /* Add content type if other than default */
leothedragon 0:8f0bb79ddd48 409 if (resource_temp_ptr->static_resource_parameters) {
leothedragon 0:8f0bb79ddd48 410 response_message_hdr_ptr->content_format =
leothedragon 0:8f0bb79ddd48 411 (sn_coap_content_format_e) resource_temp_ptr->coap_content_type;
leothedragon 0:8f0bb79ddd48 412 }
leothedragon 0:8f0bb79ddd48 413
leothedragon 0:8f0bb79ddd48 414 /* Add payload */
leothedragon 0:8f0bb79ddd48 415 if (resource_temp_ptr->resource_len != 0) {
leothedragon 0:8f0bb79ddd48 416 response_message_hdr_ptr->payload_len = resource_temp_ptr->resource_len;
leothedragon 0:8f0bb79ddd48 417 response_message_hdr_ptr->payload_ptr = handle->sn_grs_alloc(response_message_hdr_ptr->payload_len);
leothedragon 0:8f0bb79ddd48 418
leothedragon 0:8f0bb79ddd48 419 if (!response_message_hdr_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 420 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, response_message_hdr_ptr);
leothedragon 0:8f0bb79ddd48 421
leothedragon 0:8f0bb79ddd48 422 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 423 handle->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 424 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 425 }
leothedragon 0:8f0bb79ddd48 426
leothedragon 0:8f0bb79ddd48 427 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 428 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 429 }
leothedragon 0:8f0bb79ddd48 430
leothedragon 0:8f0bb79ddd48 431 memcpy(response_message_hdr_ptr->payload_ptr,
leothedragon 0:8f0bb79ddd48 432 resource_temp_ptr->resource,
leothedragon 0:8f0bb79ddd48 433 response_message_hdr_ptr->payload_len);
leothedragon 0:8f0bb79ddd48 434 }
leothedragon 0:8f0bb79ddd48 435 // Add max-age attribute for static resources.
leothedragon 0:8f0bb79ddd48 436 // Not a mandatory parameter, no need to return in case of memory allocation fails.
leothedragon 0:8f0bb79ddd48 437 if (static_get_request) {
leothedragon 0:8f0bb79ddd48 438 if (sn_coap_parser_alloc_options(handle->coap, response_message_hdr_ptr)) {
leothedragon 0:8f0bb79ddd48 439 response_message_hdr_ptr->options_list_ptr->max_age = 0;
leothedragon 0:8f0bb79ddd48 440 }
leothedragon 0:8f0bb79ddd48 441 }
leothedragon 0:8f0bb79ddd48 442 }
leothedragon 0:8f0bb79ddd48 443 sn_grs_send_coap_message(nsdl_handle, src_addr_ptr, response_message_hdr_ptr);
leothedragon 0:8f0bb79ddd48 444
leothedragon 0:8f0bb79ddd48 445 if (response_message_hdr_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 446 handle->sn_grs_free(response_message_hdr_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 447 response_message_hdr_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 448 }
leothedragon 0:8f0bb79ddd48 449 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, response_message_hdr_ptr);
leothedragon 0:8f0bb79ddd48 450 }
leothedragon 0:8f0bb79ddd48 451
leothedragon 0:8f0bb79ddd48 452 /* Free parsed CoAP message */
leothedragon 0:8f0bb79ddd48 453 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 454 handle->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 455 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 456 }
leothedragon 0:8f0bb79ddd48 457 sn_coap_parser_release_allocated_coap_msg_mem(handle->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 458
leothedragon 0:8f0bb79ddd48 459 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 460 }
leothedragon 0:8f0bb79ddd48 461
leothedragon 0:8f0bb79ddd48 462 extern int8_t sn_grs_send_coap_message(struct nsdl_s *handle, sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr)
leothedragon 0:8f0bb79ddd48 463 {
leothedragon 0:8f0bb79ddd48 464 uint8_t *message_ptr = NULL;
leothedragon 0:8f0bb79ddd48 465 uint16_t message_len = 0;
leothedragon 0:8f0bb79ddd48 466 int16_t ret_val = 0;
leothedragon 0:8f0bb79ddd48 467
leothedragon 0:8f0bb79ddd48 468 if( !handle ){
leothedragon 0:8f0bb79ddd48 469 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 470 }
leothedragon 0:8f0bb79ddd48 471
leothedragon 0:8f0bb79ddd48 472 #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:8f0bb79ddd48 473 ret_val = prepare_blockwise_message(handle->grs->coap, coap_hdr_ptr);
leothedragon 0:8f0bb79ddd48 474 if( 0 != ret_val ) {
leothedragon 0:8f0bb79ddd48 475 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 476 }
leothedragon 0:8f0bb79ddd48 477 #endif
leothedragon 0:8f0bb79ddd48 478
leothedragon 0:8f0bb79ddd48 479 /* Calculate message length */
leothedragon 0:8f0bb79ddd48 480 message_len = sn_coap_builder_calc_needed_packet_data_size_2(coap_hdr_ptr, handle->grs->coap->sn_coap_block_data_size);
leothedragon 0:8f0bb79ddd48 481
leothedragon 0:8f0bb79ddd48 482 /* Allocate memory for message and check was allocating successfully */
leothedragon 0:8f0bb79ddd48 483 message_ptr = handle->grs->sn_grs_alloc(message_len);
leothedragon 0:8f0bb79ddd48 484 if (message_ptr == NULL) {
leothedragon 0:8f0bb79ddd48 485 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 486 }
leothedragon 0:8f0bb79ddd48 487
leothedragon 0:8f0bb79ddd48 488 /* Build CoAP message */
leothedragon 0:8f0bb79ddd48 489 ret_val = sn_coap_protocol_build(handle->grs->coap, address_ptr, message_ptr, coap_hdr_ptr, (void *)handle);
leothedragon 0:8f0bb79ddd48 490 if (ret_val < 0) {
leothedragon 0:8f0bb79ddd48 491 handle->grs->sn_grs_free(message_ptr);
leothedragon 0:8f0bb79ddd48 492 message_ptr = 0;
leothedragon 0:8f0bb79ddd48 493 if (ret_val == -4) {
leothedragon 0:8f0bb79ddd48 494 return SN_NSDL_RESEND_QUEUE_FULL;
leothedragon 0:8f0bb79ddd48 495 } else {
leothedragon 0:8f0bb79ddd48 496 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 497 }
leothedragon 0:8f0bb79ddd48 498 }
leothedragon 0:8f0bb79ddd48 499
leothedragon 0:8f0bb79ddd48 500 /* Call tx callback function to send message */
leothedragon 0:8f0bb79ddd48 501 ret_val = handle->grs->sn_grs_tx_callback(handle, SN_NSDL_PROTOCOL_COAP, message_ptr, message_len, address_ptr);
leothedragon 0:8f0bb79ddd48 502
leothedragon 0:8f0bb79ddd48 503 /* Free allocated memory */
leothedragon 0:8f0bb79ddd48 504 handle->grs->sn_grs_free(message_ptr);
leothedragon 0:8f0bb79ddd48 505 message_ptr = 0;
leothedragon 0:8f0bb79ddd48 506
leothedragon 0:8f0bb79ddd48 507 if (ret_val == 0) {
leothedragon 0:8f0bb79ddd48 508 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 509 } else {
leothedragon 0:8f0bb79ddd48 510 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 511 }
leothedragon 0:8f0bb79ddd48 512 }
leothedragon 0:8f0bb79ddd48 513
leothedragon 0:8f0bb79ddd48 514 static int8_t sn_grs_core_request(struct nsdl_s *handle, sn_nsdl_addr_s *src_addr_ptr, sn_coap_hdr_s *coap_packet_ptr)
leothedragon 0:8f0bb79ddd48 515 {
leothedragon 0:8f0bb79ddd48 516 sn_coap_hdr_s *response_message_hdr_ptr = NULL;
leothedragon 0:8f0bb79ddd48 517 sn_coap_content_format_e wellknown_content_format = COAP_CT_LINK_FORMAT;
leothedragon 0:8f0bb79ddd48 518
leothedragon 0:8f0bb79ddd48 519 /* Allocate response message */
leothedragon 0:8f0bb79ddd48 520 response_message_hdr_ptr = sn_coap_parser_alloc_message(handle->grs->coap);
leothedragon 0:8f0bb79ddd48 521 if (response_message_hdr_ptr == NULL) {
leothedragon 0:8f0bb79ddd48 522 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 523 handle->grs->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 524 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 525 }
leothedragon 0:8f0bb79ddd48 526 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 527 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 528 }
leothedragon 0:8f0bb79ddd48 529
leothedragon 0:8f0bb79ddd48 530 /* Build response */
leothedragon 0:8f0bb79ddd48 531 response_message_hdr_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
leothedragon 0:8f0bb79ddd48 532 response_message_hdr_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
leothedragon 0:8f0bb79ddd48 533 response_message_hdr_ptr->msg_id = coap_packet_ptr->msg_id;
leothedragon 0:8f0bb79ddd48 534 response_message_hdr_ptr->content_format = wellknown_content_format;
leothedragon 0:8f0bb79ddd48 535
leothedragon 0:8f0bb79ddd48 536 sn_nsdl_build_registration_body(handle, response_message_hdr_ptr, 0);
leothedragon 0:8f0bb79ddd48 537
leothedragon 0:8f0bb79ddd48 538 /* Send and free */
leothedragon 0:8f0bb79ddd48 539 sn_grs_send_coap_message(handle, src_addr_ptr, response_message_hdr_ptr);
leothedragon 0:8f0bb79ddd48 540
leothedragon 0:8f0bb79ddd48 541 if (response_message_hdr_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 542 handle->grs->sn_grs_free(response_message_hdr_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 543 response_message_hdr_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 544 }
leothedragon 0:8f0bb79ddd48 545 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, response_message_hdr_ptr);
leothedragon 0:8f0bb79ddd48 546
leothedragon 0:8f0bb79ddd48 547 /* Free parsed CoAP message */
leothedragon 0:8f0bb79ddd48 548 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
leothedragon 0:8f0bb79ddd48 549 handle->grs->sn_grs_free(coap_packet_ptr->payload_ptr);
leothedragon 0:8f0bb79ddd48 550 coap_packet_ptr->payload_ptr = 0;
leothedragon 0:8f0bb79ddd48 551 }
leothedragon 0:8f0bb79ddd48 552 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
leothedragon 0:8f0bb79ddd48 553
leothedragon 0:8f0bb79ddd48 554 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 555 }
leothedragon 0:8f0bb79ddd48 556
leothedragon 0:8f0bb79ddd48 557 /**
leothedragon 0:8f0bb79ddd48 558 * \fn static sn_grs_resource_info_s *sn_grs_search_resource(struct grs_s *handle, const char *path, uint8_t search_method)
leothedragon 0:8f0bb79ddd48 559 *
leothedragon 0:8f0bb79ddd48 560 * \brief Searches given resource from linked list
leothedragon 0:8f0bb79ddd48 561 *
leothedragon 0:8f0bb79ddd48 562 * Search either precise path, or subresources, eg. dr/x -> returns dr/x/1, dr/x/2 etc...
leothedragon 0:8f0bb79ddd48 563 *
leothedragon 0:8f0bb79ddd48 564 * \param pathlen Length of the path to be search
leothedragon 0:8f0bb79ddd48 565 *
leothedragon 0:8f0bb79ddd48 566 * \param *path Pointer to the path string to be search
leothedragon 0:8f0bb79ddd48 567 *
leothedragon 0:8f0bb79ddd48 568 * \param search_method Search method, SEARCH or DELETE
leothedragon 0:8f0bb79ddd48 569 *
leothedragon 0:8f0bb79ddd48 570 * \return Pointer to the resource. If resource not found, return value is NULL
leothedragon 0:8f0bb79ddd48 571 *
leothedragon 0:8f0bb79ddd48 572 */
leothedragon 0:8f0bb79ddd48 573
leothedragon 0:8f0bb79ddd48 574 sn_nsdl_dynamic_resource_parameters_s *sn_grs_search_resource(struct grs_s *handle, const char *path, uint8_t search_method)
leothedragon 0:8f0bb79ddd48 575 {
leothedragon 0:8f0bb79ddd48 576 /* Local variables */
leothedragon 0:8f0bb79ddd48 577 char *path_temp_ptr = NULL;
leothedragon 0:8f0bb79ddd48 578 /* Check parameters */
leothedragon 0:8f0bb79ddd48 579 if (!handle || !path) {
leothedragon 0:8f0bb79ddd48 580 return NULL;
leothedragon 0:8f0bb79ddd48 581 }
leothedragon 0:8f0bb79ddd48 582 uint16_t pathlen = strlen(path);
leothedragon 0:8f0bb79ddd48 583 /* Remove '/' - marks from the end and beginning */
leothedragon 0:8f0bb79ddd48 584 path_temp_ptr = sn_grs_convert_uri(&pathlen, path);
leothedragon 0:8f0bb79ddd48 585
leothedragon 0:8f0bb79ddd48 586 /* Searchs exact path */
leothedragon 0:8f0bb79ddd48 587 if (search_method == SN_GRS_SEARCH_METHOD) {
leothedragon 0:8f0bb79ddd48 588 /* Scan all nodes on list */
leothedragon 0:8f0bb79ddd48 589 ns_list_foreach(sn_nsdl_dynamic_resource_parameters_s, resource_search_temp, &handle->resource_root_list) {
leothedragon 0:8f0bb79ddd48 590 /* If length equals.. */
leothedragon 0:8f0bb79ddd48 591 size_t len = 0;
leothedragon 0:8f0bb79ddd48 592 if(resource_search_temp &&
leothedragon 0:8f0bb79ddd48 593 resource_search_temp->static_resource_parameters &&
leothedragon 0:8f0bb79ddd48 594 resource_search_temp->static_resource_parameters->path) {
leothedragon 0:8f0bb79ddd48 595 len = strlen(resource_search_temp->static_resource_parameters->path);
leothedragon 0:8f0bb79ddd48 596 }
leothedragon 0:8f0bb79ddd48 597
leothedragon 0:8f0bb79ddd48 598 if (len == pathlen) {
leothedragon 0:8f0bb79ddd48 599 /* Compare paths, If same return node pointer*/
leothedragon 0:8f0bb79ddd48 600 if (0 == memcmp(resource_search_temp->static_resource_parameters->path,
leothedragon 0:8f0bb79ddd48 601 path_temp_ptr,
leothedragon 0:8f0bb79ddd48 602 pathlen)) {
leothedragon 0:8f0bb79ddd48 603 return resource_search_temp;
leothedragon 0:8f0bb79ddd48 604 }
leothedragon 0:8f0bb79ddd48 605 }
leothedragon 0:8f0bb79ddd48 606 }
leothedragon 0:8f0bb79ddd48 607 }
leothedragon 0:8f0bb79ddd48 608 /* Search also subresources, eg. dr/x -> returns dr/x/1, dr/x/2 etc... */
leothedragon 0:8f0bb79ddd48 609 else if (search_method == SN_GRS_DELETE_METHOD) {
leothedragon 0:8f0bb79ddd48 610 /* Scan all nodes on list */
leothedragon 0:8f0bb79ddd48 611 ns_list_foreach(sn_nsdl_dynamic_resource_parameters_s, resource_search_temp, &handle->resource_root_list) {
leothedragon 0:8f0bb79ddd48 612 char *temp_path = resource_search_temp->static_resource_parameters->path;
leothedragon 0:8f0bb79ddd48 613 if (strlen(resource_search_temp->static_resource_parameters->path) > pathlen &&
leothedragon 0:8f0bb79ddd48 614 (*(temp_path + (uint8_t)pathlen) == '/') &&
leothedragon 0:8f0bb79ddd48 615 0 == memcmp(resource_search_temp->static_resource_parameters->path,
leothedragon 0:8f0bb79ddd48 616 path_temp_ptr,
leothedragon 0:8f0bb79ddd48 617 pathlen)) {
leothedragon 0:8f0bb79ddd48 618 return resource_search_temp;
leothedragon 0:8f0bb79ddd48 619 }
leothedragon 0:8f0bb79ddd48 620 }
leothedragon 0:8f0bb79ddd48 621 }
leothedragon 0:8f0bb79ddd48 622
leothedragon 0:8f0bb79ddd48 623 /* If there was not nodes we wanted, return NULL */
leothedragon 0:8f0bb79ddd48 624 return NULL;
leothedragon 0:8f0bb79ddd48 625 }
leothedragon 0:8f0bb79ddd48 626
leothedragon 0:8f0bb79ddd48 627 /**
leothedragon 0:8f0bb79ddd48 628 * \fn static uint8_t *sn_grs_convert_uri(uint16_t *uri_len, uint8_t *uri_ptr)
leothedragon 0:8f0bb79ddd48 629 *
leothedragon 0:8f0bb79ddd48 630 * \brief Removes '/' from the beginning and from the end of uri string
leothedragon 0:8f0bb79ddd48 631 *
leothedragon 0:8f0bb79ddd48 632 * \param *uri_len Pointer to the length of the path string
leothedragon 0:8f0bb79ddd48 633 *
leothedragon 0:8f0bb79ddd48 634 * \param *uri_ptr Pointer to the path string
leothedragon 0:8f0bb79ddd48 635 *
leothedragon 0:8f0bb79ddd48 636 * \return start pointer of the uri
leothedragon 0:8f0bb79ddd48 637 *
leothedragon 0:8f0bb79ddd48 638 */
leothedragon 0:8f0bb79ddd48 639
leothedragon 0:8f0bb79ddd48 640 static char *sn_grs_convert_uri(uint16_t *uri_len, const char *uri_ptr)
leothedragon 0:8f0bb79ddd48 641 {
leothedragon 0:8f0bb79ddd48 642 /* Local variables */
leothedragon 0:8f0bb79ddd48 643 char *uri_start_ptr = (char *) uri_ptr;
leothedragon 0:8f0bb79ddd48 644
leothedragon 0:8f0bb79ddd48 645 /* If '/' in the beginning, update uri start pointer and uri len */
leothedragon 0:8f0bb79ddd48 646 if (*uri_ptr == '/') {
leothedragon 0:8f0bb79ddd48 647 uri_start_ptr = (char *) uri_ptr + 1;
leothedragon 0:8f0bb79ddd48 648 *uri_len = *uri_len - 1;
leothedragon 0:8f0bb79ddd48 649 }
leothedragon 0:8f0bb79ddd48 650
leothedragon 0:8f0bb79ddd48 651 /* If '/' at the end, update uri len */
leothedragon 0:8f0bb79ddd48 652 if (*(uri_start_ptr + *uri_len - 1) == '/') {
leothedragon 0:8f0bb79ddd48 653 *uri_len = *uri_len - 1;
leothedragon 0:8f0bb79ddd48 654 }
leothedragon 0:8f0bb79ddd48 655
leothedragon 0:8f0bb79ddd48 656 /* Return start pointer */
leothedragon 0:8f0bb79ddd48 657 return uri_start_ptr;
leothedragon 0:8f0bb79ddd48 658 }
leothedragon 0:8f0bb79ddd48 659
leothedragon 0:8f0bb79ddd48 660 /**
leothedragon 0:8f0bb79ddd48 661 * \fn static int8_t sn_grs_resource_info_free(sn_grs_resource_info_s *resource_ptr)
leothedragon 0:8f0bb79ddd48 662 *
leothedragon 0:8f0bb79ddd48 663 * \brief Frees resource info structure
leothedragon 0:8f0bb79ddd48 664 *
leothedragon 0:8f0bb79ddd48 665 * \param *resource_ptr Pointer to the resource
leothedragon 0:8f0bb79ddd48 666 *
leothedragon 0:8f0bb79ddd48 667 * \return 0 if success, -1 if failed
leothedragon 0:8f0bb79ddd48 668 *
leothedragon 0:8f0bb79ddd48 669 */
leothedragon 0:8f0bb79ddd48 670 static int8_t sn_grs_resource_info_free(struct grs_s *handle, sn_nsdl_dynamic_resource_parameters_s *resource_ptr)
leothedragon 0:8f0bb79ddd48 671 {
leothedragon 0:8f0bb79ddd48 672 if (resource_ptr) {
leothedragon 0:8f0bb79ddd48 673 #ifdef MEMORY_OPTIMIZED_API
leothedragon 0:8f0bb79ddd48 674 if (resource_ptr->free_on_delete) {
leothedragon 0:8f0bb79ddd48 675 handle->sn_grs_free(resource_ptr);
leothedragon 0:8f0bb79ddd48 676 }
leothedragon 0:8f0bb79ddd48 677 return SN_NSDL_FAILURE;
leothedragon 0:8f0bb79ddd48 678 #else
leothedragon 0:8f0bb79ddd48 679 if (resource_ptr->static_resource_parameters &&
leothedragon 0:8f0bb79ddd48 680 resource_ptr->static_resource_parameters->free_on_delete) {
leothedragon 0:8f0bb79ddd48 681 #ifndef RESOURCE_ATTRIBUTES_LIST
leothedragon 0:8f0bb79ddd48 682 #ifndef DISABLE_INTERFACE_DESCRIPTION
leothedragon 0:8f0bb79ddd48 683 if (resource_ptr->static_resource_parameters->interface_description_ptr) {
leothedragon 0:8f0bb79ddd48 684 handle->sn_grs_free(resource_ptr->static_resource_parameters->interface_description_ptr);
leothedragon 0:8f0bb79ddd48 685 resource_ptr->static_resource_parameters->interface_description_ptr = 0;
leothedragon 0:8f0bb79ddd48 686 }
leothedragon 0:8f0bb79ddd48 687 #endif
leothedragon 0:8f0bb79ddd48 688 #ifndef DISABLE_RESOURCE_TYPE
leothedragon 0:8f0bb79ddd48 689 if (resource_ptr->static_resource_parameters->resource_type_ptr) {
leothedragon 0:8f0bb79ddd48 690 handle->sn_grs_free(resource_ptr->static_resource_parameters->resource_type_ptr);
leothedragon 0:8f0bb79ddd48 691 resource_ptr->static_resource_parameters->resource_type_ptr = 0;
leothedragon 0:8f0bb79ddd48 692 }
leothedragon 0:8f0bb79ddd48 693 #endif
leothedragon 0:8f0bb79ddd48 694 #else
leothedragon 0:8f0bb79ddd48 695 sn_nsdl_free_resource_attributes_list(resource_ptr->static_resource_parameters);
leothedragon 0:8f0bb79ddd48 696 #endif
leothedragon 0:8f0bb79ddd48 697 if (resource_ptr->static_resource_parameters->path) {
leothedragon 0:8f0bb79ddd48 698 handle->sn_grs_free(resource_ptr->static_resource_parameters->path);
leothedragon 0:8f0bb79ddd48 699 resource_ptr->static_resource_parameters->path = 0;
leothedragon 0:8f0bb79ddd48 700 }
leothedragon 0:8f0bb79ddd48 701
leothedragon 0:8f0bb79ddd48 702 if (resource_ptr->resource) {
leothedragon 0:8f0bb79ddd48 703 handle->sn_grs_free(resource_ptr->resource);
leothedragon 0:8f0bb79ddd48 704 resource_ptr->resource = 0;
leothedragon 0:8f0bb79ddd48 705 }
leothedragon 0:8f0bb79ddd48 706
leothedragon 0:8f0bb79ddd48 707 handle->sn_grs_free(resource_ptr->static_resource_parameters);
leothedragon 0:8f0bb79ddd48 708 resource_ptr->static_resource_parameters = 0;
leothedragon 0:8f0bb79ddd48 709 }
leothedragon 0:8f0bb79ddd48 710 if (resource_ptr->free_on_delete) {
leothedragon 0:8f0bb79ddd48 711 handle->sn_grs_free(resource_ptr);
leothedragon 0:8f0bb79ddd48 712 }
leothedragon 0:8f0bb79ddd48 713 return SN_NSDL_SUCCESS;
leothedragon 0:8f0bb79ddd48 714 #endif
leothedragon 0:8f0bb79ddd48 715 }
leothedragon 0:8f0bb79ddd48 716 return SN_NSDL_FAILURE; //Dead code?
leothedragon 0:8f0bb79ddd48 717 }
leothedragon 0:8f0bb79ddd48 718
leothedragon 0:8f0bb79ddd48 719 void sn_grs_mark_resources_as_registered(struct nsdl_s *handle)
leothedragon 0:8f0bb79ddd48 720 {
leothedragon 0:8f0bb79ddd48 721 if( !handle ){
leothedragon 0:8f0bb79ddd48 722 return;
leothedragon 0:8f0bb79ddd48 723 }
leothedragon 0:8f0bb79ddd48 724
leothedragon 0:8f0bb79ddd48 725 sn_nsdl_dynamic_resource_parameters_s *temp_resource;
leothedragon 0:8f0bb79ddd48 726
leothedragon 0:8f0bb79ddd48 727 temp_resource = sn_grs_get_first_resource(handle->grs);
leothedragon 0:8f0bb79ddd48 728
leothedragon 0:8f0bb79ddd48 729 while (temp_resource) {
leothedragon 0:8f0bb79ddd48 730 if (temp_resource->registered == SN_NDSL_RESOURCE_REGISTERING) {
leothedragon 0:8f0bb79ddd48 731 temp_resource->registered = SN_NDSL_RESOURCE_REGISTERED;
leothedragon 0:8f0bb79ddd48 732 }
leothedragon 0:8f0bb79ddd48 733 temp_resource = sn_grs_get_next_resource(handle->grs, temp_resource);
leothedragon 0:8f0bb79ddd48 734 }
leothedragon 0:8f0bb79ddd48 735 }