sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

Committer:
Christopher Haster
Date:
Fri Jan 22 16:31:54 2016 -0600
Revision:
1:43f5c94c6771
Child:
4:5d91b0f5038c
Initial move of mbed-client-c to mercurial

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