Toyomasa Watarai / simple-mbed-cloud-client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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