mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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