Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

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