sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

Committer:
Yogesh Pande
Date:
Sat Apr 02 00:39:03 2016 +0300
Revision:
4:5d91b0f5038c
Parent:
1:43f5c94c6771
Latest mbed-client-c sources from Github.

Who changed what in which revision?

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