version of nsdl to work with lwm2m, updated RD parameters and location option handling

Dependents:   ArchPro_LWM2M_LED_Client Weather_Station_LWM2M mbedEndpointNetwork

Fork of nanoservice_client_1_12 by Zach Shelby

Committer:
zdshelby
Date:
Tue Feb 18 01:10:07 2014 +0000
Revision:
1:14a9b0f4b9d6
- Added libnsdl import

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zdshelby 1:14a9b0f4b9d6 1 /**
zdshelby 1:14a9b0f4b9d6 2 *
zdshelby 1:14a9b0f4b9d6 3 * \file sn_grs.c
zdshelby 1:14a9b0f4b9d6 4 * \brief General resource server for Sensinode NanoService platforms
zdshelby 1:14a9b0f4b9d6 5 *
zdshelby 1:14a9b0f4b9d6 6 *
zdshelby 1:14a9b0f4b9d6 7 *
zdshelby 1:14a9b0f4b9d6 8 */
zdshelby 1:14a9b0f4b9d6 9 #include "nsdl_types.h"
zdshelby 1:14a9b0f4b9d6 10 #include "sn_linked_list.h"
zdshelby 1:14a9b0f4b9d6 11 #include "sn_nsdl.h"
zdshelby 1:14a9b0f4b9d6 12
zdshelby 1:14a9b0f4b9d6 13 #if(SN_NSDL_HAVE_HTTPS_CAPABILITY&&SN_NSDL_HAVE_HTTP_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 14 #include "sn_http.h"
zdshelby 1:14a9b0f4b9d6 15 #endif
zdshelby 1:14a9b0f4b9d6 16
zdshelby 1:14a9b0f4b9d6 17 #if(SN_NSDL_HAVE_COAP_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 18 #include "sn_coap_header.h"
zdshelby 1:14a9b0f4b9d6 19 #include "sn_coap_protocol.h"
zdshelby 1:14a9b0f4b9d6 20 #endif
zdshelby 1:14a9b0f4b9d6 21
zdshelby 1:14a9b0f4b9d6 22 #include "sn_nsdl_lib.h"
zdshelby 1:14a9b0f4b9d6 23 #include "sn_grs.h"
zdshelby 1:14a9b0f4b9d6 24
zdshelby 1:14a9b0f4b9d6 25 #include <stdlib.h>
zdshelby 1:14a9b0f4b9d6 26 #include <string.h> // for memcomp
zdshelby 1:14a9b0f4b9d6 27
zdshelby 1:14a9b0f4b9d6 28 /* Defines */
zdshelby 1:14a9b0f4b9d6 29 #define WELLKNOWN_PATH_LEN 16
zdshelby 1:14a9b0f4b9d6 30 #define WELLKNOWN_PATH (".well-known/core")
zdshelby 1:14a9b0f4b9d6 31
zdshelby 1:14a9b0f4b9d6 32 /* Local static function prototypes */
zdshelby 1:14a9b0f4b9d6 33 static sn_nsdl_resource_info_s * sn_grs_search_resource (uint16_t pathlen, uint8_t *path, uint8_t search_method);
zdshelby 1:14a9b0f4b9d6 34 static int8_t sn_grs_resource_info_free (sn_nsdl_resource_info_s *resource_ptr);
zdshelby 1:14a9b0f4b9d6 35 static uint8_t * sn_grs_convert_uri (uint16_t *uri_len, uint8_t *uri_ptr);
zdshelby 1:14a9b0f4b9d6 36 static int8_t sn_grs_add_resource_to_list (sn_linked_list_t *list_ptr, sn_nsdl_resource_info_s *resource_ptr);
zdshelby 1:14a9b0f4b9d6 37 #ifdef CC8051_PLAT
zdshelby 1:14a9b0f4b9d6 38 void copy_code_nsdl (uint8_t * ptr, prog_uint8_t * code_ptr, uint16_t len);
zdshelby 1:14a9b0f4b9d6 39 #endif
zdshelby 1:14a9b0f4b9d6 40 static uint8_t sn_grs_compare_code (uint8_t * ptr, prog_uint8_t * code_ptr, uint8_t len);
zdshelby 1:14a9b0f4b9d6 41
zdshelby 1:14a9b0f4b9d6 42 /* Extern function prototypes */
zdshelby 1:14a9b0f4b9d6 43 extern int8_t sn_nsdl_build_registration_body (sn_coap_hdr_s *message_ptr, uint8_t updating_registeration);
zdshelby 1:14a9b0f4b9d6 44
zdshelby 1:14a9b0f4b9d6 45
zdshelby 1:14a9b0f4b9d6 46 /* Local global variables */
zdshelby 1:14a9b0f4b9d6 47 static sn_linked_list_t *resource_root_list = NULL;
zdshelby 1:14a9b0f4b9d6 48
zdshelby 1:14a9b0f4b9d6 49
zdshelby 1:14a9b0f4b9d6 50 /* Local global function pointers */
zdshelby 1:14a9b0f4b9d6 51 static void *(*sn_grs_alloc)(uint16_t);
zdshelby 1:14a9b0f4b9d6 52 static void (*sn_grs_free)(void*);
zdshelby 1:14a9b0f4b9d6 53 static uint8_t (*sn_grs_tx_callback)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *);
zdshelby 1:14a9b0f4b9d6 54 static int8_t (*sn_grs_rx_callback)(sn_coap_hdr_s *, sn_nsdl_addr_s *);
zdshelby 1:14a9b0f4b9d6 55
zdshelby 1:14a9b0f4b9d6 56 /**
zdshelby 1:14a9b0f4b9d6 57 * \fn int8_t sn_grs_destroy(void)
zdshelby 1:14a9b0f4b9d6 58 * \brief This function may be used to flush GRS related stuff when a program exits.
zdshelby 1:14a9b0f4b9d6 59 * @return always 0.
zdshelby 1:14a9b0f4b9d6 60 */
zdshelby 1:14a9b0f4b9d6 61 extern int8_t sn_grs_destroy(void)
zdshelby 1:14a9b0f4b9d6 62 {
zdshelby 1:14a9b0f4b9d6 63 if(resource_root_list)
zdshelby 1:14a9b0f4b9d6 64 {
zdshelby 1:14a9b0f4b9d6 65 uint16_t size = sn_linked_list_count_nodes(resource_root_list);
zdshelby 1:14a9b0f4b9d6 66 uint16_t i = 0;
zdshelby 1:14a9b0f4b9d6 67 sn_nsdl_resource_info_s*tmp;
zdshelby 1:14a9b0f4b9d6 68
zdshelby 1:14a9b0f4b9d6 69 for(i=0;i<size;i++)
zdshelby 1:14a9b0f4b9d6 70 {
zdshelby 1:14a9b0f4b9d6 71 tmp = sn_linked_list_get_first_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 72
zdshelby 1:14a9b0f4b9d6 73 if(tmp)
zdshelby 1:14a9b0f4b9d6 74 {
zdshelby 1:14a9b0f4b9d6 75 if(tmp->resource_parameters_ptr->resource_type_ptr)
zdshelby 1:14a9b0f4b9d6 76 {
zdshelby 1:14a9b0f4b9d6 77 sn_grs_free(tmp->resource_parameters_ptr->resource_type_ptr);
zdshelby 1:14a9b0f4b9d6 78 tmp->resource_parameters_ptr->resource_type_ptr = 0;
zdshelby 1:14a9b0f4b9d6 79 }
zdshelby 1:14a9b0f4b9d6 80
zdshelby 1:14a9b0f4b9d6 81 if(tmp->resource_parameters_ptr->interface_description_ptr)
zdshelby 1:14a9b0f4b9d6 82 {
zdshelby 1:14a9b0f4b9d6 83 sn_grs_free(tmp->resource_parameters_ptr->interface_description_ptr);
zdshelby 1:14a9b0f4b9d6 84 tmp->resource_parameters_ptr->interface_description_ptr = 0;
zdshelby 1:14a9b0f4b9d6 85 }
zdshelby 1:14a9b0f4b9d6 86
zdshelby 1:14a9b0f4b9d6 87 if(tmp->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 88 {
zdshelby 1:14a9b0f4b9d6 89 sn_grs_free(tmp->resource_parameters_ptr);
zdshelby 1:14a9b0f4b9d6 90 tmp->resource_parameters_ptr = 0;
zdshelby 1:14a9b0f4b9d6 91 }
zdshelby 1:14a9b0f4b9d6 92 if(tmp->path)
zdshelby 1:14a9b0f4b9d6 93 {
zdshelby 1:14a9b0f4b9d6 94 sn_grs_free(tmp->path);
zdshelby 1:14a9b0f4b9d6 95 tmp->path = 0;
zdshelby 1:14a9b0f4b9d6 96 }
zdshelby 1:14a9b0f4b9d6 97 if(tmp->resource)
zdshelby 1:14a9b0f4b9d6 98 {
zdshelby 1:14a9b0f4b9d6 99 sn_grs_free(tmp->resource);
zdshelby 1:14a9b0f4b9d6 100 tmp->resource = 0;
zdshelby 1:14a9b0f4b9d6 101 }
zdshelby 1:14a9b0f4b9d6 102 sn_linked_list_remove_current_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 103 sn_grs_free(tmp);
zdshelby 1:14a9b0f4b9d6 104 tmp = 0;
zdshelby 1:14a9b0f4b9d6 105 }
zdshelby 1:14a9b0f4b9d6 106 }
zdshelby 1:14a9b0f4b9d6 107
zdshelby 1:14a9b0f4b9d6 108 if(!sn_linked_list_count_nodes(resource_root_list))
zdshelby 1:14a9b0f4b9d6 109 {
zdshelby 1:14a9b0f4b9d6 110 sn_linked_list_free(resource_root_list);
zdshelby 1:14a9b0f4b9d6 111 }
zdshelby 1:14a9b0f4b9d6 112
zdshelby 1:14a9b0f4b9d6 113 }
zdshelby 1:14a9b0f4b9d6 114 return 0;
zdshelby 1:14a9b0f4b9d6 115 }
zdshelby 1:14a9b0f4b9d6 116
zdshelby 1:14a9b0f4b9d6 117
zdshelby 1:14a9b0f4b9d6 118 /**
zdshelby 1:14a9b0f4b9d6 119 * \fn int8_t sn_grs_init (uint8_t (*sn_grs_tx_callback_ptr)(sn_nsdl_capab_e , uint8_t *, uint16_t,
zdshelby 1:14a9b0f4b9d6 120 * 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)
zdshelby 1:14a9b0f4b9d6 121 *
zdshelby 1:14a9b0f4b9d6 122 * \brief GRS library initialize function.
zdshelby 1:14a9b0f4b9d6 123 *
zdshelby 1:14a9b0f4b9d6 124 * This function initializes GRS and CoAP libraries.
zdshelby 1:14a9b0f4b9d6 125 *
zdshelby 1:14a9b0f4b9d6 126 * \param sn_grs_tx_callback A function pointer to a transmit callback function.
zdshelby 1:14a9b0f4b9d6 127 * \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
zdshelby 1:14a9b0f4b9d6 128 * upper level (NSDL) to be proceed.
zdshelby 1:14a9b0f4b9d6 129 * \param sn_memory A pointer to a structure containing the platform specific functions for memory allocation and free.
zdshelby 1:14a9b0f4b9d6 130 *
zdshelby 1:14a9b0f4b9d6 131 * \return success = 0, failure = -1
zdshelby 1:14a9b0f4b9d6 132 *
zdshelby 1:14a9b0f4b9d6 133 */
zdshelby 1:14a9b0f4b9d6 134
zdshelby 1:14a9b0f4b9d6 135 extern int8_t sn_grs_init (uint8_t (*sn_grs_tx_callback_ptr)(sn_nsdl_capab_e , uint8_t *, uint16_t,
zdshelby 1:14a9b0f4b9d6 136 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)
zdshelby 1:14a9b0f4b9d6 137 {
zdshelby 1:14a9b0f4b9d6 138 /* If application tries to init GRS more than once.. */
zdshelby 1:14a9b0f4b9d6 139 if(!resource_root_list)
zdshelby 1:14a9b0f4b9d6 140 {
zdshelby 1:14a9b0f4b9d6 141 /* if sn_memory struct is NULL or , return failure */
zdshelby 1:14a9b0f4b9d6 142 if(!sn_memory)
zdshelby 1:14a9b0f4b9d6 143 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 144
zdshelby 1:14a9b0f4b9d6 145 if (sn_memory->sn_nsdl_alloc == NULL ||
zdshelby 1:14a9b0f4b9d6 146 sn_memory->sn_nsdl_free == NULL ||
zdshelby 1:14a9b0f4b9d6 147 sn_grs_tx_callback_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 148 {
zdshelby 1:14a9b0f4b9d6 149 /* There was a null pointer as a parameter */
zdshelby 1:14a9b0f4b9d6 150 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 151 }
zdshelby 1:14a9b0f4b9d6 152
zdshelby 1:14a9b0f4b9d6 153 /* Alloc and free - function pointers */
zdshelby 1:14a9b0f4b9d6 154 sn_grs_alloc = sn_memory->sn_nsdl_alloc;
zdshelby 1:14a9b0f4b9d6 155 sn_grs_free = sn_memory->sn_nsdl_free;
zdshelby 1:14a9b0f4b9d6 156
zdshelby 1:14a9b0f4b9d6 157 /* TX callback function pointer */
zdshelby 1:14a9b0f4b9d6 158 sn_grs_tx_callback = sn_grs_tx_callback_ptr;
zdshelby 1:14a9b0f4b9d6 159 sn_grs_rx_callback = sn_grs_rx_callback_ptr;
zdshelby 1:14a9b0f4b9d6 160
zdshelby 1:14a9b0f4b9d6 161 /* Initialize linked list */
zdshelby 1:14a9b0f4b9d6 162 sn_linked_list_init(sn_memory->sn_nsdl_alloc, sn_memory->sn_nsdl_free);
zdshelby 1:14a9b0f4b9d6 163
zdshelby 1:14a9b0f4b9d6 164 /* Initialize HTTP protocol library, if implemented to library*/
zdshelby 1:14a9b0f4b9d6 165 #if (SN_NSDL_HAVE_HTTP_CAPABILITY || SN_NSDL_HAVE_HTTPS_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 166 sn_http_init(sn_memory->sn_nsdl_alloc, sn_memory->sn_grs_free);
zdshelby 1:14a9b0f4b9d6 167 #endif
zdshelby 1:14a9b0f4b9d6 168
zdshelby 1:14a9b0f4b9d6 169 /* Initialize CoAP protocol library, if implemented to library */
zdshelby 1:14a9b0f4b9d6 170
zdshelby 1:14a9b0f4b9d6 171 /* Initialize list for resources */
zdshelby 1:14a9b0f4b9d6 172 resource_root_list = sn_linked_list_create();
zdshelby 1:14a9b0f4b9d6 173 if (!resource_root_list)
zdshelby 1:14a9b0f4b9d6 174 {
zdshelby 1:14a9b0f4b9d6 175 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 176 }
zdshelby 1:14a9b0f4b9d6 177
zdshelby 1:14a9b0f4b9d6 178 #if SN_NSDL_HAVE_COAP_CAPABILITY
zdshelby 1:14a9b0f4b9d6 179 sn_coap_builder_and_parser_init(sn_memory->sn_nsdl_alloc, sn_memory->sn_nsdl_free);
zdshelby 1:14a9b0f4b9d6 180
zdshelby 1:14a9b0f4b9d6 181 if(sn_coap_protocol_init(sn_memory->sn_nsdl_alloc, sn_memory->sn_nsdl_free, sn_grs_tx_callback))
zdshelby 1:14a9b0f4b9d6 182 {
zdshelby 1:14a9b0f4b9d6 183 sn_linked_list_free(resource_root_list);
zdshelby 1:14a9b0f4b9d6 184 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 185 }
zdshelby 1:14a9b0f4b9d6 186 #endif
zdshelby 1:14a9b0f4b9d6 187
zdshelby 1:14a9b0f4b9d6 188 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 189 }
zdshelby 1:14a9b0f4b9d6 190
zdshelby 1:14a9b0f4b9d6 191 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 192 }
zdshelby 1:14a9b0f4b9d6 193
zdshelby 1:14a9b0f4b9d6 194 /**
zdshelby 1:14a9b0f4b9d6 195 * \fn extern int8_t sn_grs_exec(uint32_t time)
zdshelby 1:14a9b0f4b9d6 196 *
zdshelby 1:14a9b0f4b9d6 197 * \brief CoAP retransmission function.
zdshelby 1:14a9b0f4b9d6 198 *
zdshelby 1:14a9b0f4b9d6 199 * Used to give execution time for the GRS (CoAP) library for retransmissions. The GRS library
zdshelby 1:14a9b0f4b9d6 200 * will call the exec functions of all enabled protocol modules.
zdshelby 1:14a9b0f4b9d6 201 *
zdshelby 1:14a9b0f4b9d6 202 * \param time Time in seconds.
zdshelby 1:14a9b0f4b9d6 203 *
zdshelby 1:14a9b0f4b9d6 204 * \return 0 = success, -1 = failure
zdshelby 1:14a9b0f4b9d6 205 *
zdshelby 1:14a9b0f4b9d6 206 */
zdshelby 1:14a9b0f4b9d6 207
zdshelby 1:14a9b0f4b9d6 208 extern int8_t sn_grs_exec(uint32_t time)
zdshelby 1:14a9b0f4b9d6 209 {
zdshelby 1:14a9b0f4b9d6 210 #if(SN_NSDL_HAVE_COAP_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 211 /* Call CoAP execution function */
zdshelby 1:14a9b0f4b9d6 212 return sn_coap_protocol_exec(time);
zdshelby 1:14a9b0f4b9d6 213 #endif
zdshelby 1:14a9b0f4b9d6 214 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 215 }
zdshelby 1:14a9b0f4b9d6 216
zdshelby 1:14a9b0f4b9d6 217 /**
zdshelby 1:14a9b0f4b9d6 218 * \fn extern sn_grs_resource_list_s *sn_grs_list_resource(uint16_t pathlen, uint8_t *path)
zdshelby 1:14a9b0f4b9d6 219 *
zdshelby 1:14a9b0f4b9d6 220 * \brief Resource list function
zdshelby 1:14a9b0f4b9d6 221 *
zdshelby 1:14a9b0f4b9d6 222 * \param pathlen Contains the length of the target path (excluding possible trailing '\0').
zdshelby 1:14a9b0f4b9d6 223 * The length value is not examined if the path itself is a NULL pointer.
zdshelby 1:14a9b0f4b9d6 224 *
zdshelby 1:14a9b0f4b9d6 225 * \param *path A pointer to an array containing the path or a NULL pointer.
zdshelby 1:14a9b0f4b9d6 226 *
zdshelby 1:14a9b0f4b9d6 227 * \return !NULL A pointer to a sn_grs_resource_list structure containing the resource listing.\n
zdshelby 1:14a9b0f4b9d6 228 * NULL failure with an unspecified error
zdshelby 1:14a9b0f4b9d6 229 */
zdshelby 1:14a9b0f4b9d6 230
zdshelby 1:14a9b0f4b9d6 231 extern sn_grs_resource_list_s *sn_grs_list_resource(uint16_t pathlen, uint8_t *path)
zdshelby 1:14a9b0f4b9d6 232 {
zdshelby 1:14a9b0f4b9d6 233 /* Local variables */
zdshelby 1:14a9b0f4b9d6 234 uint8_t i = 0;
zdshelby 1:14a9b0f4b9d6 235 sn_grs_resource_list_s *grs_resource_list_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 236 sn_nsdl_resource_info_s *grs_resource_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 237
zdshelby 1:14a9b0f4b9d6 238 /* Allocate memory for the resource list to be filled */
zdshelby 1:14a9b0f4b9d6 239 grs_resource_list_ptr = (sn_grs_resource_list_s *)sn_grs_alloc(sizeof(sn_grs_resource_list_s));
zdshelby 1:14a9b0f4b9d6 240 if(!grs_resource_list_ptr)
zdshelby 1:14a9b0f4b9d6 241 {
zdshelby 1:14a9b0f4b9d6 242 return (sn_grs_resource_list_s *)NULL;
zdshelby 1:14a9b0f4b9d6 243 }
zdshelby 1:14a9b0f4b9d6 244
zdshelby 1:14a9b0f4b9d6 245 /* Count resources to the resource list struct */
zdshelby 1:14a9b0f4b9d6 246 grs_resource_list_ptr->res_count = sn_linked_list_count_nodes(resource_root_list);
zdshelby 1:14a9b0f4b9d6 247
zdshelby 1:14a9b0f4b9d6 248 /**************************************/
zdshelby 1:14a9b0f4b9d6 249 /* Fill resource structs to the table */
zdshelby 1:14a9b0f4b9d6 250 /**************************************/
zdshelby 1:14a9b0f4b9d6 251
zdshelby 1:14a9b0f4b9d6 252 /* If resources in list */
zdshelby 1:14a9b0f4b9d6 253 if(grs_resource_list_ptr->res_count)
zdshelby 1:14a9b0f4b9d6 254 {
zdshelby 1:14a9b0f4b9d6 255 /* Allocate memory for resources */
zdshelby 1:14a9b0f4b9d6 256 grs_resource_list_ptr->res = sn_grs_alloc(grs_resource_list_ptr->res_count*(sizeof(sn_grs_resource_s)));
zdshelby 1:14a9b0f4b9d6 257 if(!grs_resource_list_ptr->res)
zdshelby 1:14a9b0f4b9d6 258 {
zdshelby 1:14a9b0f4b9d6 259 sn_grs_free(grs_resource_list_ptr);
zdshelby 1:14a9b0f4b9d6 260 return (sn_grs_resource_list_s *)NULL;
zdshelby 1:14a9b0f4b9d6 261 }
zdshelby 1:14a9b0f4b9d6 262
zdshelby 1:14a9b0f4b9d6 263 /* Get first resource */
zdshelby 1:14a9b0f4b9d6 264 grs_resource_ptr = sn_linked_list_get_first_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 265
zdshelby 1:14a9b0f4b9d6 266 for(i = 0; i < grs_resource_list_ptr->res_count; i++)
zdshelby 1:14a9b0f4b9d6 267 {
zdshelby 1:14a9b0f4b9d6 268 /* Copy pathlen to resource list */
zdshelby 1:14a9b0f4b9d6 269 grs_resource_list_ptr->res[i].pathlen = grs_resource_ptr->pathlen;
zdshelby 1:14a9b0f4b9d6 270
zdshelby 1:14a9b0f4b9d6 271 /* Allocate memory for path string */
zdshelby 1:14a9b0f4b9d6 272 grs_resource_list_ptr->res[i].path = sn_grs_alloc(grs_resource_list_ptr->res[i].pathlen);
zdshelby 1:14a9b0f4b9d6 273 if(!grs_resource_list_ptr->res[i].path)
zdshelby 1:14a9b0f4b9d6 274 //todo: free struct
zdshelby 1:14a9b0f4b9d6 275 return (sn_grs_resource_list_s *)NULL;
zdshelby 1:14a9b0f4b9d6 276
zdshelby 1:14a9b0f4b9d6 277 /* Move pathstring to resource list */
zdshelby 1:14a9b0f4b9d6 278 memmove(grs_resource_list_ptr->res[i].path,grs_resource_ptr->path, grs_resource_ptr->pathlen);
zdshelby 1:14a9b0f4b9d6 279
zdshelby 1:14a9b0f4b9d6 280 /* move to next node */
zdshelby 1:14a9b0f4b9d6 281 grs_resource_ptr = sn_linked_list_get_next_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 282 }
zdshelby 1:14a9b0f4b9d6 283 }
zdshelby 1:14a9b0f4b9d6 284 return grs_resource_list_ptr;
zdshelby 1:14a9b0f4b9d6 285 }
zdshelby 1:14a9b0f4b9d6 286
zdshelby 1:14a9b0f4b9d6 287
zdshelby 1:14a9b0f4b9d6 288 extern sn_nsdl_resource_info_s *sn_grs_get_first_resource(void)
zdshelby 1:14a9b0f4b9d6 289 {
zdshelby 1:14a9b0f4b9d6 290
zdshelby 1:14a9b0f4b9d6 291 return sn_linked_list_get_first_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 292
zdshelby 1:14a9b0f4b9d6 293 }
zdshelby 1:14a9b0f4b9d6 294
zdshelby 1:14a9b0f4b9d6 295
zdshelby 1:14a9b0f4b9d6 296 extern sn_nsdl_resource_info_s *sn_grs_get_next_resource(void)
zdshelby 1:14a9b0f4b9d6 297 {
zdshelby 1:14a9b0f4b9d6 298
zdshelby 1:14a9b0f4b9d6 299 return sn_linked_list_get_next_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 300
zdshelby 1:14a9b0f4b9d6 301 }
zdshelby 1:14a9b0f4b9d6 302
zdshelby 1:14a9b0f4b9d6 303 /**
zdshelby 1:14a9b0f4b9d6 304 * \fn extern sn_grs_resource_info_s *sn_grs_get_resource(uint16_t pathlen, uint8_t *path)
zdshelby 1:14a9b0f4b9d6 305 *
zdshelby 1:14a9b0f4b9d6 306 * \brief Resource get function.
zdshelby 1:14a9b0f4b9d6 307 *
zdshelby 1:14a9b0f4b9d6 308 * Used to get a resource.
zdshelby 1:14a9b0f4b9d6 309 *
zdshelby 1:14a9b0f4b9d6 310 * \param pathlen Contains the length of the path that is to be returned (excluding possible trailing '\\0').
zdshelby 1:14a9b0f4b9d6 311 *
zdshelby 1:14a9b0f4b9d6 312 * \param *path A pointer to an array containing the path.
zdshelby 1:14a9b0f4b9d6 313 *
zdshelby 1:14a9b0f4b9d6 314 * \return !NULL success, pointer to a sn_grs_resource_info_t that contains the resource information\n
zdshelby 1:14a9b0f4b9d6 315 * NULL failure
zdshelby 1:14a9b0f4b9d6 316 */
zdshelby 1:14a9b0f4b9d6 317
zdshelby 1:14a9b0f4b9d6 318 extern sn_nsdl_resource_info_s *sn_grs_get_resource(uint16_t pathlen, uint8_t *path)
zdshelby 1:14a9b0f4b9d6 319 {
zdshelby 1:14a9b0f4b9d6 320
zdshelby 1:14a9b0f4b9d6 321 /* Search for resource */
zdshelby 1:14a9b0f4b9d6 322 return sn_grs_search_resource(pathlen, path, SN_GRS_SEARCH_METHOD);
zdshelby 1:14a9b0f4b9d6 323
zdshelby 1:14a9b0f4b9d6 324 }
zdshelby 1:14a9b0f4b9d6 325
zdshelby 1:14a9b0f4b9d6 326
zdshelby 1:14a9b0f4b9d6 327
zdshelby 1:14a9b0f4b9d6 328 /**
zdshelby 1:14a9b0f4b9d6 329 * \fn extern int8_t sn_grs_delete_resource(uint16_t pathlen, uint8_t *path_ptr)
zdshelby 1:14a9b0f4b9d6 330 *
zdshelby 1:14a9b0f4b9d6 331 * \brief Resource delete function.
zdshelby 1:14a9b0f4b9d6 332 *
zdshelby 1:14a9b0f4b9d6 333 * Used to delete a resource. If resource has a subresources, these all must also be removed.
zdshelby 1:14a9b0f4b9d6 334 *
zdshelby 1:14a9b0f4b9d6 335 * \param pathlen Contains the length of the path that is to be deleted (excluding possible trailing �\0�).
zdshelby 1:14a9b0f4b9d6 336 *
zdshelby 1:14a9b0f4b9d6 337 * \param *path_ptr A pointer to an array containing the path.
zdshelby 1:14a9b0f4b9d6 338 *
zdshelby 1:14a9b0f4b9d6 339 * \return 0 = success, -1 = failure (No such resource)
zdshelby 1:14a9b0f4b9d6 340 */
zdshelby 1:14a9b0f4b9d6 341
zdshelby 1:14a9b0f4b9d6 342
zdshelby 1:14a9b0f4b9d6 343 extern int8_t sn_grs_delete_resource(uint16_t pathlen, uint8_t *path_ptr)
zdshelby 1:14a9b0f4b9d6 344 {
zdshelby 1:14a9b0f4b9d6 345 /* Local variables */
zdshelby 1:14a9b0f4b9d6 346 sn_nsdl_resource_info_s *resource_temp = NULL;
zdshelby 1:14a9b0f4b9d6 347
zdshelby 1:14a9b0f4b9d6 348 /* Search if resource found */
zdshelby 1:14a9b0f4b9d6 349 resource_temp = sn_grs_search_resource(pathlen, path_ptr, SN_GRS_SEARCH_METHOD);
zdshelby 1:14a9b0f4b9d6 350
zdshelby 1:14a9b0f4b9d6 351 /* If not found */
zdshelby 1:14a9b0f4b9d6 352 if(resource_temp == (sn_nsdl_resource_info_s *)NULL)
zdshelby 1:14a9b0f4b9d6 353 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 354
zdshelby 1:14a9b0f4b9d6 355 /* If found, delete it and delete also subresources, if there is any */
zdshelby 1:14a9b0f4b9d6 356 while (resource_temp != (sn_nsdl_resource_info_s *)NULL)
zdshelby 1:14a9b0f4b9d6 357 {
zdshelby 1:14a9b0f4b9d6 358 /* Remove from list */
zdshelby 1:14a9b0f4b9d6 359 resource_temp = (sn_nsdl_resource_info_s *)sn_linked_list_remove_current_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 360
zdshelby 1:14a9b0f4b9d6 361 /* Free */
zdshelby 1:14a9b0f4b9d6 362 sn_grs_resource_info_free(resource_temp);
zdshelby 1:14a9b0f4b9d6 363
zdshelby 1:14a9b0f4b9d6 364 /* Search for subresources */
zdshelby 1:14a9b0f4b9d6 365 resource_temp = sn_grs_search_resource(pathlen, path_ptr, SN_GRS_DELETE_METHOD);
zdshelby 1:14a9b0f4b9d6 366 }
zdshelby 1:14a9b0f4b9d6 367 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 368 }
zdshelby 1:14a9b0f4b9d6 369
zdshelby 1:14a9b0f4b9d6 370
zdshelby 1:14a9b0f4b9d6 371
zdshelby 1:14a9b0f4b9d6 372 /**
zdshelby 1:14a9b0f4b9d6 373 * \fn extern int8_t sn_grs_update_resource(sn_grs_resource_info_s *res)
zdshelby 1:14a9b0f4b9d6 374 *
zdshelby 1:14a9b0f4b9d6 375 * \brief Resource updating function.
zdshelby 1:14a9b0f4b9d6 376 *
zdshelby 1:14a9b0f4b9d6 377 * Used to update the direct value of a static resource, the callback function pointer of a dynamic resource
zdshelby 1:14a9b0f4b9d6 378 * and access rights of the recource.
zdshelby 1:14a9b0f4b9d6 379 *
zdshelby 1:14a9b0f4b9d6 380 * \param *res Pointer to a structure of type sn_grs_resource_info_t that contains the information
zdshelby 1:14a9b0f4b9d6 381 * about the resource. Only the pathlen and path elements are evaluated along with
zdshelby 1:14a9b0f4b9d6 382 * either resourcelen and resource or the function pointer.
zdshelby 1:14a9b0f4b9d6 383 *
zdshelby 1:14a9b0f4b9d6 384 * \return 0 = success, -1 = failure
zdshelby 1:14a9b0f4b9d6 385 */
zdshelby 1:14a9b0f4b9d6 386
zdshelby 1:14a9b0f4b9d6 387 extern int8_t sn_grs_update_resource(sn_nsdl_resource_info_s *res)
zdshelby 1:14a9b0f4b9d6 388 {
zdshelby 1:14a9b0f4b9d6 389 /* Local variables */
zdshelby 1:14a9b0f4b9d6 390 sn_nsdl_resource_info_s *resource_temp = NULL;
zdshelby 1:14a9b0f4b9d6 391
zdshelby 1:14a9b0f4b9d6 392 /* Search resource */
zdshelby 1:14a9b0f4b9d6 393 resource_temp = sn_grs_search_resource(res->pathlen, res->path, SN_GRS_SEARCH_METHOD);
zdshelby 1:14a9b0f4b9d6 394 if(!resource_temp)
zdshelby 1:14a9b0f4b9d6 395 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 396
zdshelby 1:14a9b0f4b9d6 397 /* If there is payload on resource, free it */
zdshelby 1:14a9b0f4b9d6 398 if(resource_temp->resource != NULL)
zdshelby 1:14a9b0f4b9d6 399 {
zdshelby 1:14a9b0f4b9d6 400 sn_grs_free(resource_temp->resource);
zdshelby 1:14a9b0f4b9d6 401 resource_temp->resource = 0;
zdshelby 1:14a9b0f4b9d6 402 }
zdshelby 1:14a9b0f4b9d6 403 /* Update resource len */
zdshelby 1:14a9b0f4b9d6 404 resource_temp->resourcelen = res->resourcelen;
zdshelby 1:14a9b0f4b9d6 405
zdshelby 1:14a9b0f4b9d6 406 /* If resource len >0, allocate memory and copy payload */
zdshelby 1:14a9b0f4b9d6 407 if(res->resourcelen)
zdshelby 1:14a9b0f4b9d6 408 {
zdshelby 1:14a9b0f4b9d6 409 resource_temp->resource = sn_grs_alloc(res->resourcelen);
zdshelby 1:14a9b0f4b9d6 410 if(resource_temp->resource == NULL)
zdshelby 1:14a9b0f4b9d6 411 {
zdshelby 1:14a9b0f4b9d6 412
zdshelby 1:14a9b0f4b9d6 413 resource_temp->resourcelen = 0;
zdshelby 1:14a9b0f4b9d6 414 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 415
zdshelby 1:14a9b0f4b9d6 416 }
zdshelby 1:14a9b0f4b9d6 417
zdshelby 1:14a9b0f4b9d6 418 memcpy(resource_temp->resource, res->resource, resource_temp->resourcelen);
zdshelby 1:14a9b0f4b9d6 419 }
zdshelby 1:14a9b0f4b9d6 420
zdshelby 1:14a9b0f4b9d6 421 /* Update access rights and callback address */
zdshelby 1:14a9b0f4b9d6 422 resource_temp->access = res->access;
zdshelby 1:14a9b0f4b9d6 423 resource_temp->sn_grs_dyn_res_callback = res->sn_grs_dyn_res_callback;
zdshelby 1:14a9b0f4b9d6 424
zdshelby 1:14a9b0f4b9d6 425 /* TODO: resource_parameters_ptr not copied */
zdshelby 1:14a9b0f4b9d6 426
zdshelby 1:14a9b0f4b9d6 427 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 428 }
zdshelby 1:14a9b0f4b9d6 429
zdshelby 1:14a9b0f4b9d6 430
zdshelby 1:14a9b0f4b9d6 431
zdshelby 1:14a9b0f4b9d6 432 /**
zdshelby 1:14a9b0f4b9d6 433 * \fn extern int8_t sn_grs_create_resource(sn_grs_resource_info_t *res)
zdshelby 1:14a9b0f4b9d6 434 *
zdshelby 1:14a9b0f4b9d6 435 * \brief Resource creating function.
zdshelby 1:14a9b0f4b9d6 436 *
zdshelby 1:14a9b0f4b9d6 437 * Used to create a static or dynamic HTTP(S) or CoAP resource.
zdshelby 1:14a9b0f4b9d6 438 *
zdshelby 1:14a9b0f4b9d6 439 * \param *res Pointer to a structure of type sn_grs_resource_info_t that contains the information
zdshelby 1:14a9b0f4b9d6 440 * about the resource.
zdshelby 1:14a9b0f4b9d6 441 *
zdshelby 1:14a9b0f4b9d6 442 * \return 0 success
zdshelby 1:14a9b0f4b9d6 443 * -1 Failure
zdshelby 1:14a9b0f4b9d6 444 * -2 Resource already exists
zdshelby 1:14a9b0f4b9d6 445 * -3 Invalid path
zdshelby 1:14a9b0f4b9d6 446 * -4 List adding failure
zdshelby 1:14a9b0f4b9d6 447 */
zdshelby 1:14a9b0f4b9d6 448
zdshelby 1:14a9b0f4b9d6 449 extern int8_t sn_grs_create_resource(sn_nsdl_resource_info_s *res)
zdshelby 1:14a9b0f4b9d6 450 {
zdshelby 1:14a9b0f4b9d6 451
zdshelby 1:14a9b0f4b9d6 452 if(!res)
zdshelby 1:14a9b0f4b9d6 453 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 454
zdshelby 1:14a9b0f4b9d6 455 /* Check path validity */
zdshelby 1:14a9b0f4b9d6 456 if(!res->pathlen || !res->path)
zdshelby 1:14a9b0f4b9d6 457 return SN_GRS_INVALID_PATH;
zdshelby 1:14a9b0f4b9d6 458
zdshelby 1:14a9b0f4b9d6 459 /* Check if resource already exists */
zdshelby 1:14a9b0f4b9d6 460 if(sn_grs_search_resource(res->pathlen, res->path, SN_GRS_SEARCH_METHOD) != (sn_nsdl_resource_info_s *)NULL)
zdshelby 1:14a9b0f4b9d6 461 {
zdshelby 1:14a9b0f4b9d6 462 return SN_GRS_RESOURCE_ALREADY_EXISTS;
zdshelby 1:14a9b0f4b9d6 463 }
zdshelby 1:14a9b0f4b9d6 464
zdshelby 1:14a9b0f4b9d6 465 if(res->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 466 {
zdshelby 1:14a9b0f4b9d6 467 res->resource_parameters_ptr->registered = SN_NDSL_RESOURCE_NOT_REGISTERED;
zdshelby 1:14a9b0f4b9d6 468 }
zdshelby 1:14a9b0f4b9d6 469
zdshelby 1:14a9b0f4b9d6 470 /* Create resource */
zdshelby 1:14a9b0f4b9d6 471 if(sn_grs_add_resource_to_list(resource_root_list, res) == SN_NSDL_SUCCESS)
zdshelby 1:14a9b0f4b9d6 472 {
zdshelby 1:14a9b0f4b9d6 473 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 474 }
zdshelby 1:14a9b0f4b9d6 475 return SN_GRS_LIST_ADDING_FAILURE;
zdshelby 1:14a9b0f4b9d6 476 }
zdshelby 1:14a9b0f4b9d6 477
zdshelby 1:14a9b0f4b9d6 478
zdshelby 1:14a9b0f4b9d6 479
zdshelby 1:14a9b0f4b9d6 480 /**
zdshelby 1:14a9b0f4b9d6 481 * \fn extern int8_t sn_grs_process_http(uint8_t *packet, uint16_t *packet_len, sn_grs_addr_t *src)
zdshelby 1:14a9b0f4b9d6 482 *
zdshelby 1:14a9b0f4b9d6 483 * \brief To push HTTP packet to GRS library
zdshelby 1:14a9b0f4b9d6 484 *
zdshelby 1:14a9b0f4b9d6 485 * Used to push an HTTP or unencrypted HTTPS packet to GRS library for processing.
zdshelby 1:14a9b0f4b9d6 486 *
zdshelby 1:14a9b0f4b9d6 487 * \param *packet Pointer to a uint8_t array containing the packet (including the HTTP headers).
zdshelby 1:14a9b0f4b9d6 488 * After SN_NSDL_SUCCESSful execution this array may contain the response packet.
zdshelby 1:14a9b0f4b9d6 489 *
zdshelby 1:14a9b0f4b9d6 490 * \param *packet_len Pointer to length of the packet. After successful execution this array may
zdshelby 1:14a9b0f4b9d6 491 * contain the length of the response packet.
zdshelby 1:14a9b0f4b9d6 492 *
zdshelby 1:14a9b0f4b9d6 493 * \param *src Pointer to packet source address information. After SN_NSDL_SUCCESSful execution
zdshelby 1:14a9b0f4b9d6 494 * this array may contain the destination address of the response packet.
zdshelby 1:14a9b0f4b9d6 495 *
zdshelby 1:14a9b0f4b9d6 496 * \return 1 success, response packet to be sent.
zdshelby 1:14a9b0f4b9d6 497 * 0 success, no response to be sent
zdshelby 1:14a9b0f4b9d6 498 * -1 failure
zdshelby 1:14a9b0f4b9d6 499 */
zdshelby 1:14a9b0f4b9d6 500
zdshelby 1:14a9b0f4b9d6 501 extern int8_t sn_grs_process_http(uint8_t *packet, uint16_t *packet_len, sn_nsdl_addr_s *src)
zdshelby 1:14a9b0f4b9d6 502 {
zdshelby 1:14a9b0f4b9d6 503 #if(SN_NSDL_HAVE_HTTP_CAPABILITY && SN_NSDL_HAVE_HTTPS_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 504 /* Local variables */
zdshelby 1:14a9b0f4b9d6 505 sn_http_hdr_t *http_packet_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 506 int8_t status = 0;
zdshelby 1:14a9b0f4b9d6 507
zdshelby 1:14a9b0f4b9d6 508 /******************************************/
zdshelby 1:14a9b0f4b9d6 509 /* Parse HTTP packet and check if succeed */
zdshelby 1:14a9b0f4b9d6 510 /******************************************/
zdshelby 1:14a9b0f4b9d6 511 http_packet_ptr = sn_http_parse(*packet_len, packet);
zdshelby 1:14a9b0f4b9d6 512 if(!http_packet_ptr)
zdshelby 1:14a9b0f4b9d6 513 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 514
zdshelby 1:14a9b0f4b9d6 515 if(http_packet_ptr->status != SN_HTTP_STATUS_OK)
zdshelby 1:14a9b0f4b9d6 516 return SN_NSDL_FAILURE; // Todo: other SN_NSDL_FAILUREs from HTTP
zdshelby 1:14a9b0f4b9d6 517
zdshelby 1:14a9b0f4b9d6 518 switch (http_packet_ptr->method)
zdshelby 1:14a9b0f4b9d6 519 {
zdshelby 1:14a9b0f4b9d6 520 case (SN_GRS_POST):
zdshelby 1:14a9b0f4b9d6 521 return status;
zdshelby 1:14a9b0f4b9d6 522
zdshelby 1:14a9b0f4b9d6 523 case (SN_GRS_PUT):
zdshelby 1:14a9b0f4b9d6 524 return status;
zdshelby 1:14a9b0f4b9d6 525
zdshelby 1:14a9b0f4b9d6 526 case (SN_GRS_GET):
zdshelby 1:14a9b0f4b9d6 527 return status;
zdshelby 1:14a9b0f4b9d6 528
zdshelby 1:14a9b0f4b9d6 529 case (SN_GRS_DELETE):
zdshelby 1:14a9b0f4b9d6 530 return status;
zdshelby 1:14a9b0f4b9d6 531
zdshelby 1:14a9b0f4b9d6 532 default:
zdshelby 1:14a9b0f4b9d6 533 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 534 }
zdshelby 1:14a9b0f4b9d6 535 #endif
zdshelby 1:14a9b0f4b9d6 536 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 537 }
zdshelby 1:14a9b0f4b9d6 538
zdshelby 1:14a9b0f4b9d6 539
zdshelby 1:14a9b0f4b9d6 540
zdshelby 1:14a9b0f4b9d6 541 /**
zdshelby 1:14a9b0f4b9d6 542 * \fn extern int8_t sn_grs_process_coap(uint8_t *packet, uint16_t *packet_len, sn_nsdl_addr_s *src)
zdshelby 1:14a9b0f4b9d6 543 *
zdshelby 1:14a9b0f4b9d6 544 * \brief To push CoAP packet to GRS library
zdshelby 1:14a9b0f4b9d6 545 *
zdshelby 1:14a9b0f4b9d6 546 * Used to push an CoAP packet to GRS library for processing.
zdshelby 1:14a9b0f4b9d6 547 *
zdshelby 1:14a9b0f4b9d6 548 * \param *packet Pointer to a uint8_t array containing the packet (including the CoAP headers).
zdshelby 1:14a9b0f4b9d6 549 * After successful execution this array may contain the response packet.
zdshelby 1:14a9b0f4b9d6 550 *
zdshelby 1:14a9b0f4b9d6 551 * \param *packet_len Pointer to length of the packet. After successful execution this array may contain the length
zdshelby 1:14a9b0f4b9d6 552 * of the response packet.
zdshelby 1:14a9b0f4b9d6 553 *
zdshelby 1:14a9b0f4b9d6 554 * \param *src Pointer to packet source address information. After successful execution this array may contain
zdshelby 1:14a9b0f4b9d6 555 * the destination address of the response packet.
zdshelby 1:14a9b0f4b9d6 556 *
zdshelby 1:14a9b0f4b9d6 557 * \return 0 = success, -1 = failure
zdshelby 1:14a9b0f4b9d6 558 */
zdshelby 1:14a9b0f4b9d6 559
zdshelby 1:14a9b0f4b9d6 560 extern int8_t sn_grs_process_coap(uint8_t *packet, uint16_t packet_len, sn_nsdl_addr_s *src_addr_ptr)
zdshelby 1:14a9b0f4b9d6 561 {
zdshelby 1:14a9b0f4b9d6 562 sn_coap_hdr_s *coap_packet_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 563 sn_nsdl_resource_info_s *resource_temp_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 564 sn_coap_msg_code_e status = COAP_MSG_CODE_EMPTY;
zdshelby 1:14a9b0f4b9d6 565 sn_coap_hdr_s *response_message_hdr_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 566
zdshelby 1:14a9b0f4b9d6 567
zdshelby 1:14a9b0f4b9d6 568 /* Parse CoAP packet */
zdshelby 1:14a9b0f4b9d6 569 coap_packet_ptr = sn_coap_protocol_parse(src_addr_ptr, packet_len, packet);
zdshelby 1:14a9b0f4b9d6 570
zdshelby 1:14a9b0f4b9d6 571 /* Check if parsing was successfull */
zdshelby 1:14a9b0f4b9d6 572 if(coap_packet_ptr == (sn_coap_hdr_s *)NULL)
zdshelby 1:14a9b0f4b9d6 573 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 574
zdshelby 1:14a9b0f4b9d6 575 /* Check, if coap itself sends response, or block receiving is ongoing... */
zdshelby 1:14a9b0f4b9d6 576 if(coap_packet_ptr->coap_status != COAP_STATUS_OK && coap_packet_ptr->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED)
zdshelby 1:14a9b0f4b9d6 577 {
zdshelby 1:14a9b0f4b9d6 578 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 579 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 580 }
zdshelby 1:14a9b0f4b9d6 581
zdshelby 1:14a9b0f4b9d6 582 /* If proxy options added, return not supported */
zdshelby 1:14a9b0f4b9d6 583 if (coap_packet_ptr->options_list_ptr)
zdshelby 1:14a9b0f4b9d6 584 {
zdshelby 1:14a9b0f4b9d6 585 if(coap_packet_ptr->options_list_ptr->proxy_uri_len)
zdshelby 1:14a9b0f4b9d6 586 {
zdshelby 1:14a9b0f4b9d6 587 status = COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED;
zdshelby 1:14a9b0f4b9d6 588 }
zdshelby 1:14a9b0f4b9d6 589
zdshelby 1:14a9b0f4b9d6 590 }
zdshelby 1:14a9b0f4b9d6 591
zdshelby 1:14a9b0f4b9d6 592 /* * * * * * * * * * * * * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 593 /* If message is response message, call RX callback */
zdshelby 1:14a9b0f4b9d6 594 /* * * * * * * * * * * * * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 595
zdshelby 1:14a9b0f4b9d6 596 if((coap_packet_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE && status == COAP_MSG_CODE_EMPTY) ||
zdshelby 1:14a9b0f4b9d6 597 (coap_packet_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT && status == COAP_MSG_CODE_EMPTY))
zdshelby 1:14a9b0f4b9d6 598 {
zdshelby 1:14a9b0f4b9d6 599 int8_t retval = sn_grs_rx_callback(coap_packet_ptr, src_addr_ptr);
zdshelby 1:14a9b0f4b9d6 600 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 601 {
zdshelby 1:14a9b0f4b9d6 602 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 603 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 604 }
zdshelby 1:14a9b0f4b9d6 605 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 606 return retval;
zdshelby 1:14a9b0f4b9d6 607 }
zdshelby 1:14a9b0f4b9d6 608
zdshelby 1:14a9b0f4b9d6 609 /* * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 610 /* Other messages are for GRS */
zdshelby 1:14a9b0f4b9d6 611 /* * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 612
zdshelby 1:14a9b0f4b9d6 613 else if(coap_packet_ptr->msg_code <= COAP_MSG_CODE_REQUEST_DELETE && status == COAP_MSG_CODE_EMPTY)
zdshelby 1:14a9b0f4b9d6 614 {
zdshelby 1:14a9b0f4b9d6 615 /* Check if .well-known/core */
zdshelby 1:14a9b0f4b9d6 616 if(coap_packet_ptr->uri_path_len == WELLKNOWN_PATH_LEN && sn_grs_compare_code(coap_packet_ptr->uri_path_ptr, (const uint8_t*)WELLKNOWN_PATH, WELLKNOWN_PATH_LEN) == 0)
zdshelby 1:14a9b0f4b9d6 617 {
zdshelby 1:14a9b0f4b9d6 618
zdshelby 1:14a9b0f4b9d6 619 sn_coap_content_format_e wellknown_content_format = COAP_CT_LINK_FORMAT;
zdshelby 1:14a9b0f4b9d6 620
zdshelby 1:14a9b0f4b9d6 621 /* Allocate resopnse message */
zdshelby 1:14a9b0f4b9d6 622 response_message_hdr_ptr = sn_grs_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 623 if(!response_message_hdr_ptr)
zdshelby 1:14a9b0f4b9d6 624 {
zdshelby 1:14a9b0f4b9d6 625 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 626 {
zdshelby 1:14a9b0f4b9d6 627 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 628 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 629 }
zdshelby 1:14a9b0f4b9d6 630 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 631 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 632 }
zdshelby 1:14a9b0f4b9d6 633 memset(response_message_hdr_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 634
zdshelby 1:14a9b0f4b9d6 635 /* Build response */
zdshelby 1:14a9b0f4b9d6 636 response_message_hdr_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
zdshelby 1:14a9b0f4b9d6 637 response_message_hdr_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
zdshelby 1:14a9b0f4b9d6 638 response_message_hdr_ptr->msg_id = coap_packet_ptr->msg_id;
zdshelby 1:14a9b0f4b9d6 639 response_message_hdr_ptr->content_type_len = 1;
zdshelby 1:14a9b0f4b9d6 640 response_message_hdr_ptr->content_type_ptr = malloc(1);
zdshelby 1:14a9b0f4b9d6 641 if(!response_message_hdr_ptr)
zdshelby 1:14a9b0f4b9d6 642 {
zdshelby 1:14a9b0f4b9d6 643 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 644 {
zdshelby 1:14a9b0f4b9d6 645 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 646 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 647 }
zdshelby 1:14a9b0f4b9d6 648 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 649 sn_grs_free(response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 650 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 651 }
zdshelby 1:14a9b0f4b9d6 652
zdshelby 1:14a9b0f4b9d6 653 *response_message_hdr_ptr->content_type_ptr = wellknown_content_format;
zdshelby 1:14a9b0f4b9d6 654
zdshelby 1:14a9b0f4b9d6 655
zdshelby 1:14a9b0f4b9d6 656 sn_nsdl_build_registration_body(response_message_hdr_ptr, 0);
zdshelby 1:14a9b0f4b9d6 657
zdshelby 1:14a9b0f4b9d6 658 /* Send and free */
zdshelby 1:14a9b0f4b9d6 659 sn_grs_send_coap_message(src_addr_ptr, response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 660
zdshelby 1:14a9b0f4b9d6 661 if(response_message_hdr_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 662 {
zdshelby 1:14a9b0f4b9d6 663 sn_grs_free(response_message_hdr_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 664 response_message_hdr_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 665 }
zdshelby 1:14a9b0f4b9d6 666 sn_coap_parser_release_allocated_coap_msg_mem(response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 667
zdshelby 1:14a9b0f4b9d6 668 /* Free parsed CoAP message */
zdshelby 1:14a9b0f4b9d6 669 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 670 {
zdshelby 1:14a9b0f4b9d6 671 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 672 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 673 }
zdshelby 1:14a9b0f4b9d6 674 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 675
zdshelby 1:14a9b0f4b9d6 676 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 677 }
zdshelby 1:14a9b0f4b9d6 678
zdshelby 1:14a9b0f4b9d6 679 /* Get resource */
zdshelby 1:14a9b0f4b9d6 680 resource_temp_ptr = sn_grs_get_resource(coap_packet_ptr->uri_path_len, coap_packet_ptr->uri_path_ptr);
zdshelby 1:14a9b0f4b9d6 681
zdshelby 1:14a9b0f4b9d6 682 /* * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 683 /* If resource exists */
zdshelby 1:14a9b0f4b9d6 684 /* * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 685 if(resource_temp_ptr)
zdshelby 1:14a9b0f4b9d6 686 {
zdshelby 1:14a9b0f4b9d6 687 /* If dynamic resource, go to callback */
zdshelby 1:14a9b0f4b9d6 688 if(resource_temp_ptr->mode == SN_GRS_DYNAMIC)
zdshelby 1:14a9b0f4b9d6 689 {
zdshelby 1:14a9b0f4b9d6 690 /* Check accesses */
zdshelby 1:14a9b0f4b9d6 691 if(((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) && !(resource_temp_ptr->access & SN_GRS_GET_ALLOWED)) ||
zdshelby 1:14a9b0f4b9d6 692 ((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) && !(resource_temp_ptr->access & SN_GRS_POST_ALLOWED)) ||
zdshelby 1:14a9b0f4b9d6 693 ((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) && !(resource_temp_ptr->access & SN_GRS_PUT_ALLOWED)) ||
zdshelby 1:14a9b0f4b9d6 694 ((coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) && !(resource_temp_ptr->access & SN_GRS_DELETE_ALLOWED)))
zdshelby 1:14a9b0f4b9d6 695 {
zdshelby 1:14a9b0f4b9d6 696
zdshelby 1:14a9b0f4b9d6 697 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
zdshelby 1:14a9b0f4b9d6 698 }
zdshelby 1:14a9b0f4b9d6 699 else
zdshelby 1:14a9b0f4b9d6 700 {
zdshelby 1:14a9b0f4b9d6 701 resource_temp_ptr->sn_grs_dyn_res_callback(coap_packet_ptr, src_addr_ptr,0);
zdshelby 1:14a9b0f4b9d6 702 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 703 {
zdshelby 1:14a9b0f4b9d6 704 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 705 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 706 }
zdshelby 1:14a9b0f4b9d6 707 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 708 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 709 }
zdshelby 1:14a9b0f4b9d6 710 }
zdshelby 1:14a9b0f4b9d6 711 else
zdshelby 1:14a9b0f4b9d6 712 {
zdshelby 1:14a9b0f4b9d6 713 /* Static resource handling */
zdshelby 1:14a9b0f4b9d6 714 switch (coap_packet_ptr->msg_code )
zdshelby 1:14a9b0f4b9d6 715 {
zdshelby 1:14a9b0f4b9d6 716 case (COAP_MSG_CODE_REQUEST_GET):
zdshelby 1:14a9b0f4b9d6 717 if(resource_temp_ptr->access & SN_GRS_GET_ALLOWED)
zdshelby 1:14a9b0f4b9d6 718 {
zdshelby 1:14a9b0f4b9d6 719 status = COAP_MSG_CODE_RESPONSE_CONTENT;
zdshelby 1:14a9b0f4b9d6 720 }
zdshelby 1:14a9b0f4b9d6 721 else
zdshelby 1:14a9b0f4b9d6 722 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
zdshelby 1:14a9b0f4b9d6 723 break;
zdshelby 1:14a9b0f4b9d6 724 case (COAP_MSG_CODE_REQUEST_POST):
zdshelby 1:14a9b0f4b9d6 725 if(resource_temp_ptr->access & SN_GRS_POST_ALLOWED)
zdshelby 1:14a9b0f4b9d6 726 {
zdshelby 1:14a9b0f4b9d6 727 resource_temp_ptr->resourcelen = coap_packet_ptr->payload_len;
zdshelby 1:14a9b0f4b9d6 728 sn_grs_free(resource_temp_ptr->resource);
zdshelby 1:14a9b0f4b9d6 729 resource_temp_ptr->resource = 0;
zdshelby 1:14a9b0f4b9d6 730 if(resource_temp_ptr->resourcelen)
zdshelby 1:14a9b0f4b9d6 731 {
zdshelby 1:14a9b0f4b9d6 732 resource_temp_ptr->resource = sn_grs_alloc(resource_temp_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 733 if(!resource_temp_ptr->resource)
zdshelby 1:14a9b0f4b9d6 734 {
zdshelby 1:14a9b0f4b9d6 735 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 736 break;
zdshelby 1:14a9b0f4b9d6 737 }
zdshelby 1:14a9b0f4b9d6 738 memcpy(resource_temp_ptr->resource, coap_packet_ptr->payload_ptr, resource_temp_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 739 }
zdshelby 1:14a9b0f4b9d6 740 if(coap_packet_ptr->content_type_ptr)
zdshelby 1:14a9b0f4b9d6 741 {
zdshelby 1:14a9b0f4b9d6 742 if(resource_temp_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 743 {
zdshelby 1:14a9b0f4b9d6 744 resource_temp_ptr->resource_parameters_ptr->coap_content_type = *coap_packet_ptr->content_type_ptr;
zdshelby 1:14a9b0f4b9d6 745 }
zdshelby 1:14a9b0f4b9d6 746 }
zdshelby 1:14a9b0f4b9d6 747 status = COAP_MSG_CODE_RESPONSE_CHANGED;
zdshelby 1:14a9b0f4b9d6 748 }
zdshelby 1:14a9b0f4b9d6 749 else
zdshelby 1:14a9b0f4b9d6 750 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
zdshelby 1:14a9b0f4b9d6 751 break;
zdshelby 1:14a9b0f4b9d6 752 case (COAP_MSG_CODE_REQUEST_PUT):
zdshelby 1:14a9b0f4b9d6 753 if(resource_temp_ptr->access & SN_GRS_PUT_ALLOWED)
zdshelby 1:14a9b0f4b9d6 754 {
zdshelby 1:14a9b0f4b9d6 755 resource_temp_ptr->resourcelen = coap_packet_ptr->payload_len;
zdshelby 1:14a9b0f4b9d6 756 sn_grs_free(resource_temp_ptr->resource);
zdshelby 1:14a9b0f4b9d6 757 resource_temp_ptr->resource = 0;
zdshelby 1:14a9b0f4b9d6 758 if(resource_temp_ptr->resourcelen)
zdshelby 1:14a9b0f4b9d6 759 {
zdshelby 1:14a9b0f4b9d6 760 resource_temp_ptr->resource = sn_grs_alloc(resource_temp_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 761 if(!resource_temp_ptr->resource)
zdshelby 1:14a9b0f4b9d6 762 {
zdshelby 1:14a9b0f4b9d6 763 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 764 break;
zdshelby 1:14a9b0f4b9d6 765 }
zdshelby 1:14a9b0f4b9d6 766 memcpy(resource_temp_ptr->resource, coap_packet_ptr->payload_ptr, resource_temp_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 767 }
zdshelby 1:14a9b0f4b9d6 768 if(coap_packet_ptr->content_type_ptr)
zdshelby 1:14a9b0f4b9d6 769 {
zdshelby 1:14a9b0f4b9d6 770 if(resource_temp_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 771 {
zdshelby 1:14a9b0f4b9d6 772 resource_temp_ptr->resource_parameters_ptr->coap_content_type = *coap_packet_ptr->content_type_ptr;
zdshelby 1:14a9b0f4b9d6 773 }
zdshelby 1:14a9b0f4b9d6 774 }
zdshelby 1:14a9b0f4b9d6 775 status = COAP_MSG_CODE_RESPONSE_CHANGED;
zdshelby 1:14a9b0f4b9d6 776 }
zdshelby 1:14a9b0f4b9d6 777 else
zdshelby 1:14a9b0f4b9d6 778 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
zdshelby 1:14a9b0f4b9d6 779 break;
zdshelby 1:14a9b0f4b9d6 780
zdshelby 1:14a9b0f4b9d6 781 case (COAP_MSG_CODE_REQUEST_DELETE):
zdshelby 1:14a9b0f4b9d6 782 if(resource_temp_ptr->access & SN_GRS_DELETE_ALLOWED)
zdshelby 1:14a9b0f4b9d6 783 {
zdshelby 1:14a9b0f4b9d6 784 if(sn_grs_delete_resource(coap_packet_ptr->uri_path_len, coap_packet_ptr->uri_path_ptr) == SN_NSDL_SUCCESS)
zdshelby 1:14a9b0f4b9d6 785 status = COAP_MSG_CODE_RESPONSE_DELETED;
zdshelby 1:14a9b0f4b9d6 786 else
zdshelby 1:14a9b0f4b9d6 787 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 788 }
zdshelby 1:14a9b0f4b9d6 789 else
zdshelby 1:14a9b0f4b9d6 790 status = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
zdshelby 1:14a9b0f4b9d6 791 break;
zdshelby 1:14a9b0f4b9d6 792
zdshelby 1:14a9b0f4b9d6 793 default:
zdshelby 1:14a9b0f4b9d6 794 status = COAP_MSG_CODE_RESPONSE_FORBIDDEN;
zdshelby 1:14a9b0f4b9d6 795 break;
zdshelby 1:14a9b0f4b9d6 796 }
zdshelby 1:14a9b0f4b9d6 797 }
zdshelby 1:14a9b0f4b9d6 798 }
zdshelby 1:14a9b0f4b9d6 799
zdshelby 1:14a9b0f4b9d6 800 /* * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 801 /* If resource was not found */
zdshelby 1:14a9b0f4b9d6 802 /* * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 803
zdshelby 1:14a9b0f4b9d6 804 else
zdshelby 1:14a9b0f4b9d6 805 {
zdshelby 1:14a9b0f4b9d6 806 if(coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST ||
zdshelby 1:14a9b0f4b9d6 807 coap_packet_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT)
zdshelby 1:14a9b0f4b9d6 808 {
zdshelby 1:14a9b0f4b9d6 809 resource_temp_ptr = sn_grs_alloc(sizeof(sn_nsdl_resource_info_s));
zdshelby 1:14a9b0f4b9d6 810 if(!resource_temp_ptr)
zdshelby 1:14a9b0f4b9d6 811 {
zdshelby 1:14a9b0f4b9d6 812 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 813 }
zdshelby 1:14a9b0f4b9d6 814 else
zdshelby 1:14a9b0f4b9d6 815 {
zdshelby 1:14a9b0f4b9d6 816 memset(resource_temp_ptr, 0, sizeof(sn_nsdl_resource_info_s));
zdshelby 1:14a9b0f4b9d6 817
zdshelby 1:14a9b0f4b9d6 818 resource_temp_ptr->access = (sn_grs_resource_acl_e)SN_GRS_DEFAULT_ACCESS;
zdshelby 1:14a9b0f4b9d6 819 resource_temp_ptr->mode = SN_GRS_STATIC;
zdshelby 1:14a9b0f4b9d6 820
zdshelby 1:14a9b0f4b9d6 821 resource_temp_ptr->pathlen = coap_packet_ptr->uri_path_len;
zdshelby 1:14a9b0f4b9d6 822 resource_temp_ptr->path = sn_grs_alloc(resource_temp_ptr->pathlen);
zdshelby 1:14a9b0f4b9d6 823 if(!resource_temp_ptr->path)
zdshelby 1:14a9b0f4b9d6 824 {
zdshelby 1:14a9b0f4b9d6 825 sn_grs_free(resource_temp_ptr);
zdshelby 1:14a9b0f4b9d6 826 resource_temp_ptr = 0;
zdshelby 1:14a9b0f4b9d6 827 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 828 }
zdshelby 1:14a9b0f4b9d6 829 else
zdshelby 1:14a9b0f4b9d6 830 {
zdshelby 1:14a9b0f4b9d6 831 memcpy(resource_temp_ptr->path, coap_packet_ptr->uri_path_ptr, resource_temp_ptr->pathlen);
zdshelby 1:14a9b0f4b9d6 832
zdshelby 1:14a9b0f4b9d6 833 resource_temp_ptr->resourcelen = coap_packet_ptr->payload_len;
zdshelby 1:14a9b0f4b9d6 834 resource_temp_ptr->resource = sn_grs_alloc(resource_temp_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 835 if(!resource_temp_ptr->resource)
zdshelby 1:14a9b0f4b9d6 836 {
zdshelby 1:14a9b0f4b9d6 837 sn_grs_free(resource_temp_ptr->path);
zdshelby 1:14a9b0f4b9d6 838 resource_temp_ptr->path = 0;
zdshelby 1:14a9b0f4b9d6 839 sn_grs_free(resource_temp_ptr);
zdshelby 1:14a9b0f4b9d6 840 resource_temp_ptr = 0;
zdshelby 1:14a9b0f4b9d6 841 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 842 }
zdshelby 1:14a9b0f4b9d6 843 else
zdshelby 1:14a9b0f4b9d6 844 {
zdshelby 1:14a9b0f4b9d6 845
zdshelby 1:14a9b0f4b9d6 846 memcpy(resource_temp_ptr->resource, coap_packet_ptr->payload_ptr, resource_temp_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 847
zdshelby 1:14a9b0f4b9d6 848 status = sn_linked_list_add_node(resource_root_list, resource_temp_ptr);
zdshelby 1:14a9b0f4b9d6 849 if(status == SN_LINKED_LIST_ERROR_NO_ERROR)
zdshelby 1:14a9b0f4b9d6 850 {
zdshelby 1:14a9b0f4b9d6 851 if(coap_packet_ptr->content_type_ptr)
zdshelby 1:14a9b0f4b9d6 852 {
zdshelby 1:14a9b0f4b9d6 853 if(resource_temp_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 854 {
zdshelby 1:14a9b0f4b9d6 855 resource_temp_ptr->resource_parameters_ptr->coap_content_type = *coap_packet_ptr->content_type_ptr;
zdshelby 1:14a9b0f4b9d6 856 }
zdshelby 1:14a9b0f4b9d6 857 }
zdshelby 1:14a9b0f4b9d6 858 status = COAP_MSG_CODE_RESPONSE_CREATED;
zdshelby 1:14a9b0f4b9d6 859 }
zdshelby 1:14a9b0f4b9d6 860 else
zdshelby 1:14a9b0f4b9d6 861 {
zdshelby 1:14a9b0f4b9d6 862 sn_grs_free(resource_temp_ptr->path);
zdshelby 1:14a9b0f4b9d6 863 resource_temp_ptr->path = 0;
zdshelby 1:14a9b0f4b9d6 864
zdshelby 1:14a9b0f4b9d6 865 sn_grs_free(resource_temp_ptr->resource);
zdshelby 1:14a9b0f4b9d6 866 resource_temp_ptr->resource = 0;
zdshelby 1:14a9b0f4b9d6 867
zdshelby 1:14a9b0f4b9d6 868 sn_grs_free(resource_temp_ptr);
zdshelby 1:14a9b0f4b9d6 869 resource_temp_ptr = 0;
zdshelby 1:14a9b0f4b9d6 870
zdshelby 1:14a9b0f4b9d6 871 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 872 }
zdshelby 1:14a9b0f4b9d6 873
zdshelby 1:14a9b0f4b9d6 874 }
zdshelby 1:14a9b0f4b9d6 875
zdshelby 1:14a9b0f4b9d6 876 }
zdshelby 1:14a9b0f4b9d6 877
zdshelby 1:14a9b0f4b9d6 878 }
zdshelby 1:14a9b0f4b9d6 879
zdshelby 1:14a9b0f4b9d6 880 }
zdshelby 1:14a9b0f4b9d6 881 else
zdshelby 1:14a9b0f4b9d6 882 status = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
zdshelby 1:14a9b0f4b9d6 883 }
zdshelby 1:14a9b0f4b9d6 884
zdshelby 1:14a9b0f4b9d6 885 }
zdshelby 1:14a9b0f4b9d6 886
zdshelby 1:14a9b0f4b9d6 887
zdshelby 1:14a9b0f4b9d6 888 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 889 /* If received packed was other than reset, create response */
zdshelby 1:14a9b0f4b9d6 890 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 891 if(coap_packet_ptr->msg_type != COAP_MSG_TYPE_RESET && coap_packet_ptr->msg_type != COAP_MSG_TYPE_ACKNOWLEDGEMENT)
zdshelby 1:14a9b0f4b9d6 892 {
zdshelby 1:14a9b0f4b9d6 893
zdshelby 1:14a9b0f4b9d6 894 /* Allocate resopnse message */
zdshelby 1:14a9b0f4b9d6 895 response_message_hdr_ptr = sn_grs_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 896 if(!response_message_hdr_ptr)
zdshelby 1:14a9b0f4b9d6 897 {
zdshelby 1:14a9b0f4b9d6 898 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 899 {
zdshelby 1:14a9b0f4b9d6 900 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 901 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 902 }
zdshelby 1:14a9b0f4b9d6 903 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 904 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 905 }
zdshelby 1:14a9b0f4b9d6 906 memset(response_message_hdr_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 907
zdshelby 1:14a9b0f4b9d6 908 /* If status has not been defined, response internal server error */
zdshelby 1:14a9b0f4b9d6 909 if(status == COAP_MSG_CODE_EMPTY)
zdshelby 1:14a9b0f4b9d6 910 status = COAP_MSG_CODE_RESPONSE_INTERNAL_SERVER_ERROR;
zdshelby 1:14a9b0f4b9d6 911
zdshelby 1:14a9b0f4b9d6 912 /* Fill header */
zdshelby 1:14a9b0f4b9d6 913 response_message_hdr_ptr->msg_code = status;
zdshelby 1:14a9b0f4b9d6 914
zdshelby 1:14a9b0f4b9d6 915 if(coap_packet_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE)
zdshelby 1:14a9b0f4b9d6 916 response_message_hdr_ptr->msg_type = COAP_MSG_TYPE_ACKNOWLEDGEMENT;
zdshelby 1:14a9b0f4b9d6 917 else
zdshelby 1:14a9b0f4b9d6 918 response_message_hdr_ptr->msg_type = COAP_MSG_TYPE_NON_CONFIRMABLE;
zdshelby 1:14a9b0f4b9d6 919
zdshelby 1:14a9b0f4b9d6 920 response_message_hdr_ptr->msg_id = coap_packet_ptr->msg_id;
zdshelby 1:14a9b0f4b9d6 921
zdshelby 1:14a9b0f4b9d6 922 if(coap_packet_ptr->token_ptr)
zdshelby 1:14a9b0f4b9d6 923 {
zdshelby 1:14a9b0f4b9d6 924 response_message_hdr_ptr->token_len = coap_packet_ptr->token_len;
zdshelby 1:14a9b0f4b9d6 925 response_message_hdr_ptr->token_ptr = sn_grs_alloc(response_message_hdr_ptr->token_len);
zdshelby 1:14a9b0f4b9d6 926 if(!response_message_hdr_ptr->token_ptr)
zdshelby 1:14a9b0f4b9d6 927 {
zdshelby 1:14a9b0f4b9d6 928 if(response_message_hdr_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 929 {
zdshelby 1:14a9b0f4b9d6 930 sn_grs_free(response_message_hdr_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 931 response_message_hdr_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 932 }
zdshelby 1:14a9b0f4b9d6 933 sn_coap_parser_release_allocated_coap_msg_mem(response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 934
zdshelby 1:14a9b0f4b9d6 935 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 936 {
zdshelby 1:14a9b0f4b9d6 937 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 938 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 939 }
zdshelby 1:14a9b0f4b9d6 940
zdshelby 1:14a9b0f4b9d6 941 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 942 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 943 }
zdshelby 1:14a9b0f4b9d6 944 memcpy(response_message_hdr_ptr->token_ptr, coap_packet_ptr->token_ptr, response_message_hdr_ptr->token_len);
zdshelby 1:14a9b0f4b9d6 945 }
zdshelby 1:14a9b0f4b9d6 946
zdshelby 1:14a9b0f4b9d6 947 if(status == COAP_MSG_CODE_RESPONSE_CONTENT)
zdshelby 1:14a9b0f4b9d6 948 {
zdshelby 1:14a9b0f4b9d6 949 /* Add content type if other than default */
zdshelby 1:14a9b0f4b9d6 950 if(resource_temp_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 951 {
zdshelby 1:14a9b0f4b9d6 952 if(resource_temp_ptr->resource_parameters_ptr->coap_content_type != 0)
zdshelby 1:14a9b0f4b9d6 953 {
zdshelby 1:14a9b0f4b9d6 954 response_message_hdr_ptr->content_type_len = 1;
zdshelby 1:14a9b0f4b9d6 955 response_message_hdr_ptr->content_type_ptr = sn_grs_alloc(response_message_hdr_ptr->content_type_len);
zdshelby 1:14a9b0f4b9d6 956 if(!response_message_hdr_ptr->content_type_ptr)
zdshelby 1:14a9b0f4b9d6 957 {
zdshelby 1:14a9b0f4b9d6 958 sn_coap_parser_release_allocated_coap_msg_mem(response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 959
zdshelby 1:14a9b0f4b9d6 960 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 961 {
zdshelby 1:14a9b0f4b9d6 962 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 963 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 964 }
zdshelby 1:14a9b0f4b9d6 965
zdshelby 1:14a9b0f4b9d6 966 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 967 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 968 }
zdshelby 1:14a9b0f4b9d6 969 memcpy(response_message_hdr_ptr->content_type_ptr, &resource_temp_ptr->resource_parameters_ptr->coap_content_type, response_message_hdr_ptr->content_type_len);
zdshelby 1:14a9b0f4b9d6 970 }
zdshelby 1:14a9b0f4b9d6 971 }
zdshelby 1:14a9b0f4b9d6 972
zdshelby 1:14a9b0f4b9d6 973 /* Add payload */
zdshelby 1:14a9b0f4b9d6 974 response_message_hdr_ptr->payload_len = resource_temp_ptr->resourcelen;
zdshelby 1:14a9b0f4b9d6 975 response_message_hdr_ptr->payload_ptr = sn_grs_alloc(response_message_hdr_ptr->payload_len);
zdshelby 1:14a9b0f4b9d6 976
zdshelby 1:14a9b0f4b9d6 977 if(!response_message_hdr_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 978 {
zdshelby 1:14a9b0f4b9d6 979 sn_coap_parser_release_allocated_coap_msg_mem(response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 980
zdshelby 1:14a9b0f4b9d6 981 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 982 {
zdshelby 1:14a9b0f4b9d6 983 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 984 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 985 }
zdshelby 1:14a9b0f4b9d6 986
zdshelby 1:14a9b0f4b9d6 987 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 988 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 989 }
zdshelby 1:14a9b0f4b9d6 990
zdshelby 1:14a9b0f4b9d6 991 memcpy(response_message_hdr_ptr->payload_ptr, resource_temp_ptr->resource, response_message_hdr_ptr->payload_len);
zdshelby 1:14a9b0f4b9d6 992 }
zdshelby 1:14a9b0f4b9d6 993
zdshelby 1:14a9b0f4b9d6 994 sn_grs_send_coap_message(src_addr_ptr, response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 995
zdshelby 1:14a9b0f4b9d6 996 if(response_message_hdr_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 997 {
zdshelby 1:14a9b0f4b9d6 998 sn_grs_free(response_message_hdr_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 999 response_message_hdr_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1000 }
zdshelby 1:14a9b0f4b9d6 1001 sn_coap_parser_release_allocated_coap_msg_mem(response_message_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 1002 }
zdshelby 1:14a9b0f4b9d6 1003
zdshelby 1:14a9b0f4b9d6 1004 /* Free parsed CoAP message */
zdshelby 1:14a9b0f4b9d6 1005 if(coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 1006 {
zdshelby 1:14a9b0f4b9d6 1007 sn_grs_free(coap_packet_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 1008 coap_packet_ptr->payload_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1009 }
zdshelby 1:14a9b0f4b9d6 1010 sn_coap_parser_release_allocated_coap_msg_mem(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 1011
zdshelby 1:14a9b0f4b9d6 1012
zdshelby 1:14a9b0f4b9d6 1013 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1014 }
zdshelby 1:14a9b0f4b9d6 1015
zdshelby 1:14a9b0f4b9d6 1016
zdshelby 1:14a9b0f4b9d6 1017
zdshelby 1:14a9b0f4b9d6 1018
zdshelby 1:14a9b0f4b9d6 1019 /**
zdshelby 1:14a9b0f4b9d6 1020 * \fn extern int16_t sn_grs_get_capability(void)
zdshelby 1:14a9b0f4b9d6 1021 *
zdshelby 1:14a9b0f4b9d6 1022 * \brief Capability query function.
zdshelby 1:14a9b0f4b9d6 1023 *
zdshelby 1:14a9b0f4b9d6 1024 * Used to retrieve the list of supported protocols from the GRS module.
zdshelby 1:14a9b0f4b9d6 1025 *
zdshelby 1:14a9b0f4b9d6 1026 * \return >0 success, supported capabilities reported using bitmask with definitions from sn_grs_capab_t\n
zdshelby 1:14a9b0f4b9d6 1027 * 0 success, no supported capabilities\n
zdshelby 1:14a9b0f4b9d6 1028 */
zdshelby 1:14a9b0f4b9d6 1029
zdshelby 1:14a9b0f4b9d6 1030 extern int16_t sn_grs_get_capability(void)
zdshelby 1:14a9b0f4b9d6 1031 {
zdshelby 1:14a9b0f4b9d6 1032 int16_t capabilities = 0;
zdshelby 1:14a9b0f4b9d6 1033 if(SN_NSDL_HAVE_HTTP_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 1034 capabilities |= 0x01;
zdshelby 1:14a9b0f4b9d6 1035
zdshelby 1:14a9b0f4b9d6 1036 if(SN_NSDL_HAVE_HTTPS_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 1037 capabilities |= 0x02;
zdshelby 1:14a9b0f4b9d6 1038
zdshelby 1:14a9b0f4b9d6 1039 if(SN_NSDL_HAVE_COAP_CAPABILITY)
zdshelby 1:14a9b0f4b9d6 1040 capabilities |= 0x04;
zdshelby 1:14a9b0f4b9d6 1041
zdshelby 1:14a9b0f4b9d6 1042 return capabilities;
zdshelby 1:14a9b0f4b9d6 1043 }
zdshelby 1:14a9b0f4b9d6 1044
zdshelby 1:14a9b0f4b9d6 1045
zdshelby 1:14a9b0f4b9d6 1046 /**
zdshelby 1:14a9b0f4b9d6 1047 * \fn extern uint32_t sn_grs_get_version(void)
zdshelby 1:14a9b0f4b9d6 1048 *
zdshelby 1:14a9b0f4b9d6 1049 * \brief Version query function.
zdshelby 1:14a9b0f4b9d6 1050 *
zdshelby 1:14a9b0f4b9d6 1051 * Used to retrieve the version information structure from the GRS library.
zdshelby 1:14a9b0f4b9d6 1052 *
zdshelby 1:14a9b0f4b9d6 1053 * \return !0 MSB 2 bytes major version, LSB 2 bytes minor version.
zdshelby 1:14a9b0f4b9d6 1054 * 0 failure
zdshelby 1:14a9b0f4b9d6 1055 */
zdshelby 1:14a9b0f4b9d6 1056
zdshelby 1:14a9b0f4b9d6 1057 extern uint32_t sn_grs_get_version(void)
zdshelby 1:14a9b0f4b9d6 1058 {
zdshelby 1:14a9b0f4b9d6 1059 return SN_GRS_VERSION;
zdshelby 1:14a9b0f4b9d6 1060 }
zdshelby 1:14a9b0f4b9d6 1061
zdshelby 1:14a9b0f4b9d6 1062 /**
zdshelby 1:14a9b0f4b9d6 1063 * \fn extern int8_t sn_grs_send_coap_message(sn_nsdl_addr_s * address_ptr, sn_coap_hdr_s *coap_hdr_ptr)
zdshelby 1:14a9b0f4b9d6 1064 *
zdshelby 1:14a9b0f4b9d6 1065 * \brief Sends CoAP message
zdshelby 1:14a9b0f4b9d6 1066 *
zdshelby 1:14a9b0f4b9d6 1067 * Sends CoAP message
zdshelby 1:14a9b0f4b9d6 1068 *
zdshelby 1:14a9b0f4b9d6 1069 * \param *coap_hdr_ptr Pointer to CoAP message to be sent
zdshelby 1:14a9b0f4b9d6 1070 *
zdshelby 1:14a9b0f4b9d6 1071 * \param *address_ptr Pointer to source address struct
zdshelby 1:14a9b0f4b9d6 1072 *
zdshelby 1:14a9b0f4b9d6 1073 * \return 0 = success, -1 = failed
zdshelby 1:14a9b0f4b9d6 1074 *
zdshelby 1:14a9b0f4b9d6 1075 */
zdshelby 1:14a9b0f4b9d6 1076 extern int8_t sn_grs_send_coap_message(sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr)
zdshelby 1:14a9b0f4b9d6 1077 {
zdshelby 1:14a9b0f4b9d6 1078 uint8_t *message_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 1079 uint16_t message_len = 0;
zdshelby 1:14a9b0f4b9d6 1080 uint8_t ret_val = 0;
zdshelby 1:14a9b0f4b9d6 1081
zdshelby 1:14a9b0f4b9d6 1082 /* Calculate message length */
zdshelby 1:14a9b0f4b9d6 1083 message_len = sn_coap_builder_calc_needed_packet_data_size(coap_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 1084
zdshelby 1:14a9b0f4b9d6 1085 /* Allocate memory for message and check was allocating successfully */
zdshelby 1:14a9b0f4b9d6 1086 message_ptr = sn_grs_alloc(message_len);
zdshelby 1:14a9b0f4b9d6 1087 if(message_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 1088 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1089
zdshelby 1:14a9b0f4b9d6 1090 /* Build CoAP message */
zdshelby 1:14a9b0f4b9d6 1091 if(sn_coap_protocol_build(address_ptr, message_ptr, coap_hdr_ptr) < 0)
zdshelby 1:14a9b0f4b9d6 1092 {
zdshelby 1:14a9b0f4b9d6 1093 sn_grs_free(message_ptr);
zdshelby 1:14a9b0f4b9d6 1094 message_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1095 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1096 }
zdshelby 1:14a9b0f4b9d6 1097
zdshelby 1:14a9b0f4b9d6 1098 /* Call tx callback function to send message */
zdshelby 1:14a9b0f4b9d6 1099 ret_val = sn_grs_tx_callback(SN_NSDL_PROTOCOL_COAP, message_ptr, message_len, address_ptr);
zdshelby 1:14a9b0f4b9d6 1100
zdshelby 1:14a9b0f4b9d6 1101 /* Free allocated memory */
zdshelby 1:14a9b0f4b9d6 1102 sn_grs_free(message_ptr);
zdshelby 1:14a9b0f4b9d6 1103 message_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1104
zdshelby 1:14a9b0f4b9d6 1105 if(ret_val == 0)
zdshelby 1:14a9b0f4b9d6 1106 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1107 else
zdshelby 1:14a9b0f4b9d6 1108 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1109 }
zdshelby 1:14a9b0f4b9d6 1110
zdshelby 1:14a9b0f4b9d6 1111 /**
zdshelby 1:14a9b0f4b9d6 1112 * \fn static sn_grs_resource_info_s *sn_grs_search_resource(uint16_t pathlen, uint8_t *path, uint8_t search_method)
zdshelby 1:14a9b0f4b9d6 1113 *
zdshelby 1:14a9b0f4b9d6 1114 * \brief Searches given resource from linked list
zdshelby 1:14a9b0f4b9d6 1115 *
zdshelby 1:14a9b0f4b9d6 1116 * Search either precise path, or subresources, eg. dr/x -> returns dr/x/1, dr/x/2 etc...
zdshelby 1:14a9b0f4b9d6 1117 *
zdshelby 1:14a9b0f4b9d6 1118 * \param pathlen Length of the path to be search
zdshelby 1:14a9b0f4b9d6 1119 *
zdshelby 1:14a9b0f4b9d6 1120 * \param *path Pointer to the path string to be search
zdshelby 1:14a9b0f4b9d6 1121 *
zdshelby 1:14a9b0f4b9d6 1122 * \param search_method Search method, SEARCH or DELETE
zdshelby 1:14a9b0f4b9d6 1123 *
zdshelby 1:14a9b0f4b9d6 1124 * \return Pointer to the resource. If resource not found, return value is NULL
zdshelby 1:14a9b0f4b9d6 1125 *
zdshelby 1:14a9b0f4b9d6 1126 */
zdshelby 1:14a9b0f4b9d6 1127
zdshelby 1:14a9b0f4b9d6 1128 static sn_nsdl_resource_info_s *sn_grs_search_resource(uint16_t pathlen, uint8_t *path, uint8_t search_method)
zdshelby 1:14a9b0f4b9d6 1129 {
zdshelby 1:14a9b0f4b9d6 1130 /* Local variables */
zdshelby 1:14a9b0f4b9d6 1131 sn_nsdl_resource_info_s *resource_search_temp = NULL;
zdshelby 1:14a9b0f4b9d6 1132 uint8_t i = 0;
zdshelby 1:14a9b0f4b9d6 1133 uint8_t *path_temp_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 1134
zdshelby 1:14a9b0f4b9d6 1135 /* Check parameters */
zdshelby 1:14a9b0f4b9d6 1136 if(!pathlen || !path)
zdshelby 1:14a9b0f4b9d6 1137 {
zdshelby 1:14a9b0f4b9d6 1138 return (sn_nsdl_resource_info_s *)NULL;;
zdshelby 1:14a9b0f4b9d6 1139 }
zdshelby 1:14a9b0f4b9d6 1140
zdshelby 1:14a9b0f4b9d6 1141 /* Remove '/' - marks from the end and beginning */
zdshelby 1:14a9b0f4b9d6 1142 path_temp_ptr = sn_grs_convert_uri(&pathlen, path);
zdshelby 1:14a9b0f4b9d6 1143
zdshelby 1:14a9b0f4b9d6 1144 resource_search_temp = sn_linked_list_get_first_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 1145
zdshelby 1:14a9b0f4b9d6 1146 /* Searchs exact path */
zdshelby 1:14a9b0f4b9d6 1147 if(search_method == SN_GRS_SEARCH_METHOD)
zdshelby 1:14a9b0f4b9d6 1148 {
zdshelby 1:14a9b0f4b9d6 1149 /* Scan all nodes on list */
zdshelby 1:14a9b0f4b9d6 1150 while(resource_search_temp)
zdshelby 1:14a9b0f4b9d6 1151 {
zdshelby 1:14a9b0f4b9d6 1152 /* If length equals.. */
zdshelby 1:14a9b0f4b9d6 1153 if(resource_search_temp->pathlen == pathlen)
zdshelby 1:14a9b0f4b9d6 1154 {
zdshelby 1:14a9b0f4b9d6 1155 /* Compare paths */
zdshelby 1:14a9b0f4b9d6 1156 i = memcmp(resource_search_temp->path, path_temp_ptr, pathlen);
zdshelby 1:14a9b0f4b9d6 1157
zdshelby 1:14a9b0f4b9d6 1158 /* If same, return node pointer */
zdshelby 1:14a9b0f4b9d6 1159 if(!i)
zdshelby 1:14a9b0f4b9d6 1160 return resource_search_temp;
zdshelby 1:14a9b0f4b9d6 1161 }
zdshelby 1:14a9b0f4b9d6 1162 /* If that was not what we needed, get next node.. */
zdshelby 1:14a9b0f4b9d6 1163 resource_search_temp = sn_linked_list_get_next_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 1164 }
zdshelby 1:14a9b0f4b9d6 1165 }
zdshelby 1:14a9b0f4b9d6 1166
zdshelby 1:14a9b0f4b9d6 1167 /* Search also subresources, eg. dr/x -> returns dr/x/1, dr/x/2 etc... */
zdshelby 1:14a9b0f4b9d6 1168 else if(search_method == SN_GRS_DELETE_METHOD)
zdshelby 1:14a9b0f4b9d6 1169 {
zdshelby 1:14a9b0f4b9d6 1170 /* Scan all nodes on list */
zdshelby 1:14a9b0f4b9d6 1171 while(resource_search_temp)
zdshelby 1:14a9b0f4b9d6 1172 {
zdshelby 1:14a9b0f4b9d6 1173 uint8_t *temp_ptr = resource_search_temp->path;
zdshelby 1:14a9b0f4b9d6 1174
zdshelby 1:14a9b0f4b9d6 1175 i = memcmp(resource_search_temp->path, path_temp_ptr, pathlen);
zdshelby 1:14a9b0f4b9d6 1176
zdshelby 1:14a9b0f4b9d6 1177 /* If found, return pointer */
zdshelby 1:14a9b0f4b9d6 1178 if((*(temp_ptr+(uint8_t)pathlen) == '/') && !i)
zdshelby 1:14a9b0f4b9d6 1179 return resource_search_temp;
zdshelby 1:14a9b0f4b9d6 1180
zdshelby 1:14a9b0f4b9d6 1181 /* else get next node */
zdshelby 1:14a9b0f4b9d6 1182 resource_search_temp = sn_linked_list_get_next_node(resource_root_list);
zdshelby 1:14a9b0f4b9d6 1183 }
zdshelby 1:14a9b0f4b9d6 1184 }
zdshelby 1:14a9b0f4b9d6 1185
zdshelby 1:14a9b0f4b9d6 1186 /* If there was not nodes we wanted, return NULL */
zdshelby 1:14a9b0f4b9d6 1187 return (sn_nsdl_resource_info_s *)NULL;
zdshelby 1:14a9b0f4b9d6 1188 }
zdshelby 1:14a9b0f4b9d6 1189
zdshelby 1:14a9b0f4b9d6 1190
zdshelby 1:14a9b0f4b9d6 1191 /**
zdshelby 1:14a9b0f4b9d6 1192 * \fn static int8_t sn_grs_add_resource_to_list(sn_linked_list_t *list_ptr, sn_grs_resource_info_s *resource_ptr)
zdshelby 1:14a9b0f4b9d6 1193 *
zdshelby 1:14a9b0f4b9d6 1194 * \brief Adds given resource to resource list
zdshelby 1:14a9b0f4b9d6 1195 *
zdshelby 1:14a9b0f4b9d6 1196 * \param *list_ptr Length of the path to be search
zdshelby 1:14a9b0f4b9d6 1197 *
zdshelby 1:14a9b0f4b9d6 1198 * \param *resource_ptr Pointer to the path string to be search
zdshelby 1:14a9b0f4b9d6 1199 *
zdshelby 1:14a9b0f4b9d6 1200 * \return 0 = SN_NSDL_SUCCESS, -1 = SN_NSDL_FAILURE
zdshelby 1:14a9b0f4b9d6 1201 *
zdshelby 1:14a9b0f4b9d6 1202 */
zdshelby 1:14a9b0f4b9d6 1203
zdshelby 1:14a9b0f4b9d6 1204 static int8_t sn_grs_add_resource_to_list(sn_linked_list_t *list_ptr, sn_nsdl_resource_info_s *resource_ptr)
zdshelby 1:14a9b0f4b9d6 1205 {
zdshelby 1:14a9b0f4b9d6 1206 /* Local variables */
zdshelby 1:14a9b0f4b9d6 1207 int8_t status = 0;
zdshelby 1:14a9b0f4b9d6 1208 uint8_t *path_start_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 1209 uint16_t path_len = 0;
zdshelby 1:14a9b0f4b9d6 1210 sn_nsdl_resource_info_s *resource_copy_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 1211
zdshelby 1:14a9b0f4b9d6 1212 /* Allocate memory for the resource info copy */
zdshelby 1:14a9b0f4b9d6 1213 if(!resource_ptr->pathlen)
zdshelby 1:14a9b0f4b9d6 1214 {
zdshelby 1:14a9b0f4b9d6 1215 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1216 }
zdshelby 1:14a9b0f4b9d6 1217 resource_copy_ptr = sn_grs_alloc(sizeof(sn_nsdl_resource_info_s));
zdshelby 1:14a9b0f4b9d6 1218 if(resource_copy_ptr == (sn_nsdl_resource_info_s*)NULL)
zdshelby 1:14a9b0f4b9d6 1219 {
zdshelby 1:14a9b0f4b9d6 1220 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1221 }
zdshelby 1:14a9b0f4b9d6 1222
zdshelby 1:14a9b0f4b9d6 1223 /* Set everything to zero */
zdshelby 1:14a9b0f4b9d6 1224 memset(resource_copy_ptr, 0, sizeof(sn_nsdl_resource_info_s));
zdshelby 1:14a9b0f4b9d6 1225
zdshelby 1:14a9b0f4b9d6 1226 resource_copy_ptr->mode = resource_ptr->mode;
zdshelby 1:14a9b0f4b9d6 1227 resource_copy_ptr->resourcelen = resource_ptr->resourcelen;
zdshelby 1:14a9b0f4b9d6 1228 resource_copy_ptr->sn_grs_dyn_res_callback = resource_ptr->sn_grs_dyn_res_callback;
zdshelby 1:14a9b0f4b9d6 1229 resource_copy_ptr->access = resource_ptr->access;
zdshelby 1:14a9b0f4b9d6 1230
zdshelby 1:14a9b0f4b9d6 1231 /* Remove '/' - chars from the beginning and from the end */
zdshelby 1:14a9b0f4b9d6 1232
zdshelby 1:14a9b0f4b9d6 1233 path_len = resource_ptr->pathlen;
zdshelby 1:14a9b0f4b9d6 1234 path_start_ptr = sn_grs_convert_uri(&path_len, resource_ptr->path);
zdshelby 1:14a9b0f4b9d6 1235
zdshelby 1:14a9b0f4b9d6 1236 /* Allocate memory for the path */
zdshelby 1:14a9b0f4b9d6 1237 resource_copy_ptr->path = sn_grs_alloc(path_len);
zdshelby 1:14a9b0f4b9d6 1238 if(!resource_copy_ptr->path)
zdshelby 1:14a9b0f4b9d6 1239 {
zdshelby 1:14a9b0f4b9d6 1240 sn_grs_resource_info_free(resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1241 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1242 }
zdshelby 1:14a9b0f4b9d6 1243
zdshelby 1:14a9b0f4b9d6 1244 /* Update pathlen */
zdshelby 1:14a9b0f4b9d6 1245 resource_copy_ptr->pathlen = path_len;
zdshelby 1:14a9b0f4b9d6 1246
zdshelby 1:14a9b0f4b9d6 1247 /* Copy path string to the copy */
zdshelby 1:14a9b0f4b9d6 1248 #ifdef CC8051_PLAT
zdshelby 1:14a9b0f4b9d6 1249 copy_code_nsdl(resource_copy_ptr->path, (prog_uint8_t*)path_start_ptr, resource_copy_ptr->pathlen);
zdshelby 1:14a9b0f4b9d6 1250 #else
zdshelby 1:14a9b0f4b9d6 1251 memcpy(resource_copy_ptr->path, path_start_ptr, resource_copy_ptr->pathlen);
zdshelby 1:14a9b0f4b9d6 1252 #endif
zdshelby 1:14a9b0f4b9d6 1253 /* Allocate memory for the resource, and copy it to copy */
zdshelby 1:14a9b0f4b9d6 1254 if(resource_ptr->resource)
zdshelby 1:14a9b0f4b9d6 1255 {
zdshelby 1:14a9b0f4b9d6 1256 resource_copy_ptr->resource = sn_grs_alloc(resource_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 1257 if(!resource_copy_ptr->resource)
zdshelby 1:14a9b0f4b9d6 1258 {
zdshelby 1:14a9b0f4b9d6 1259 sn_grs_resource_info_free(resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1260 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1261 }
zdshelby 1:14a9b0f4b9d6 1262 memcpy(resource_copy_ptr->resource, resource_ptr->resource, resource_ptr->resourcelen);
zdshelby 1:14a9b0f4b9d6 1263 }
zdshelby 1:14a9b0f4b9d6 1264
zdshelby 1:14a9b0f4b9d6 1265
zdshelby 1:14a9b0f4b9d6 1266
zdshelby 1:14a9b0f4b9d6 1267 /* If resource parameters exists, copy them */
zdshelby 1:14a9b0f4b9d6 1268 if(resource_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 1269 {
zdshelby 1:14a9b0f4b9d6 1270 resource_copy_ptr->resource_parameters_ptr = sn_grs_alloc(sizeof(sn_nsdl_resource_parameters_s));
zdshelby 1:14a9b0f4b9d6 1271 if(!resource_copy_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 1272 {
zdshelby 1:14a9b0f4b9d6 1273 sn_grs_resource_info_free(resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1274 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1275 }
zdshelby 1:14a9b0f4b9d6 1276
zdshelby 1:14a9b0f4b9d6 1277 memset(resource_copy_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
zdshelby 1:14a9b0f4b9d6 1278
zdshelby 1:14a9b0f4b9d6 1279
zdshelby 1:14a9b0f4b9d6 1280 resource_copy_ptr->resource_parameters_ptr->resource_type_len = resource_ptr->resource_parameters_ptr->resource_type_len;
zdshelby 1:14a9b0f4b9d6 1281
zdshelby 1:14a9b0f4b9d6 1282 resource_copy_ptr->resource_parameters_ptr->interface_description_len = resource_ptr->resource_parameters_ptr->interface_description_len;
zdshelby 1:14a9b0f4b9d6 1283
zdshelby 1:14a9b0f4b9d6 1284 resource_copy_ptr->resource_parameters_ptr->mime_content_type = resource_ptr->resource_parameters_ptr->mime_content_type;
zdshelby 1:14a9b0f4b9d6 1285
zdshelby 1:14a9b0f4b9d6 1286 resource_copy_ptr->resource_parameters_ptr->observable = resource_ptr->resource_parameters_ptr->observable;
zdshelby 1:14a9b0f4b9d6 1287
zdshelby 1:14a9b0f4b9d6 1288 if(resource_ptr->resource_parameters_ptr->resource_type_ptr)
zdshelby 1:14a9b0f4b9d6 1289 {
zdshelby 1:14a9b0f4b9d6 1290 resource_copy_ptr->resource_parameters_ptr->resource_type_ptr = sn_grs_alloc(resource_ptr->resource_parameters_ptr->resource_type_len);
zdshelby 1:14a9b0f4b9d6 1291 if(!resource_copy_ptr->resource_parameters_ptr->resource_type_ptr)
zdshelby 1:14a9b0f4b9d6 1292 {
zdshelby 1:14a9b0f4b9d6 1293 sn_grs_resource_info_free(resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1294 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1295 }
zdshelby 1:14a9b0f4b9d6 1296 #ifdef CC8051_PLAT
zdshelby 1:14a9b0f4b9d6 1297 copy_code_nsdl(resource_copy_ptr->resource_parameters_ptr->resource_type_ptr,(prog_uint8_t*) resource_ptr->resource_parameters_ptr->resource_type_ptr, resource_ptr->resource_parameters_ptr->resource_type_len);
zdshelby 1:14a9b0f4b9d6 1298 #else
zdshelby 1:14a9b0f4b9d6 1299 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);
zdshelby 1:14a9b0f4b9d6 1300 #endif
zdshelby 1:14a9b0f4b9d6 1301 }
zdshelby 1:14a9b0f4b9d6 1302
zdshelby 1:14a9b0f4b9d6 1303 if(resource_ptr->resource_parameters_ptr->interface_description_ptr)
zdshelby 1:14a9b0f4b9d6 1304 {
zdshelby 1:14a9b0f4b9d6 1305 resource_copy_ptr->resource_parameters_ptr->interface_description_ptr = sn_grs_alloc(resource_ptr->resource_parameters_ptr->interface_description_len);
zdshelby 1:14a9b0f4b9d6 1306 if(!resource_copy_ptr->resource_parameters_ptr->interface_description_ptr)
zdshelby 1:14a9b0f4b9d6 1307 {
zdshelby 1:14a9b0f4b9d6 1308 sn_grs_resource_info_free(resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1309 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1310 }
zdshelby 1:14a9b0f4b9d6 1311 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);
zdshelby 1:14a9b0f4b9d6 1312 }
zdshelby 1:14a9b0f4b9d6 1313
zdshelby 1:14a9b0f4b9d6 1314 /* Copy auto observation parameter */
zdshelby 1:14a9b0f4b9d6 1315 /* todo: aobs not supported ATM - needs fixing */
zdshelby 1:14a9b0f4b9d6 1316 /* if(resource_ptr->resource_parameters_ptr->auto_obs_ptr && resource_ptr->resource_parameters_ptr->auto_obs_len)
zdshelby 1:14a9b0f4b9d6 1317 {
zdshelby 1:14a9b0f4b9d6 1318 resource_copy_ptr->resource_parameters_ptr->auto_obs_ptr = sn_grs_alloc(resource_ptr->resource_parameters_ptr->auto_obs_len);
zdshelby 1:14a9b0f4b9d6 1319 if(!resource_copy_ptr->resource_parameters_ptr->auto_obs_ptr)
zdshelby 1:14a9b0f4b9d6 1320 {
zdshelby 1:14a9b0f4b9d6 1321 sn_grs_resource_info_free(resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1322 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1323 }
zdshelby 1:14a9b0f4b9d6 1324 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);
zdshelby 1:14a9b0f4b9d6 1325 resource_copy_ptr->resource_parameters_ptr->auto_obs_len = resource_ptr->resource_parameters_ptr->auto_obs_len;
zdshelby 1:14a9b0f4b9d6 1326 }
zdshelby 1:14a9b0f4b9d6 1327
zdshelby 1:14a9b0f4b9d6 1328 resource_copy_ptr->resource_parameters_ptr->coap_content_type = resource_ptr->resource_parameters_ptr->coap_content_type;
zdshelby 1:14a9b0f4b9d6 1329 */
zdshelby 1:14a9b0f4b9d6 1330 }
zdshelby 1:14a9b0f4b9d6 1331
zdshelby 1:14a9b0f4b9d6 1332 /* Add copied resource to the linked list */
zdshelby 1:14a9b0f4b9d6 1333 status = sn_linked_list_add_node(list_ptr, resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1334
zdshelby 1:14a9b0f4b9d6 1335 /* Was adding ok? */
zdshelby 1:14a9b0f4b9d6 1336 if(status == SN_LINKED_LIST_ERROR_NO_ERROR)
zdshelby 1:14a9b0f4b9d6 1337 {
zdshelby 1:14a9b0f4b9d6 1338 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1339 }
zdshelby 1:14a9b0f4b9d6 1340 else
zdshelby 1:14a9b0f4b9d6 1341 {
zdshelby 1:14a9b0f4b9d6 1342 sn_grs_resource_info_free(resource_copy_ptr);
zdshelby 1:14a9b0f4b9d6 1343 return SN_NSDL_FAILURE;//DONE?: Free memory
zdshelby 1:14a9b0f4b9d6 1344 }
zdshelby 1:14a9b0f4b9d6 1345 }
zdshelby 1:14a9b0f4b9d6 1346
zdshelby 1:14a9b0f4b9d6 1347
zdshelby 1:14a9b0f4b9d6 1348 /**
zdshelby 1:14a9b0f4b9d6 1349 * \fn static uint8_t *sn_grs_convert_uri(uint16_t *uri_len, uint8_t *uri_ptr)
zdshelby 1:14a9b0f4b9d6 1350 *
zdshelby 1:14a9b0f4b9d6 1351 * \brief Removes '/' from the beginning and from the end of uri string
zdshelby 1:14a9b0f4b9d6 1352 *
zdshelby 1:14a9b0f4b9d6 1353 * \param *uri_len Pointer to the length of the path string
zdshelby 1:14a9b0f4b9d6 1354 *
zdshelby 1:14a9b0f4b9d6 1355 * \param *uri_ptr Pointer to the path string
zdshelby 1:14a9b0f4b9d6 1356 *
zdshelby 1:14a9b0f4b9d6 1357 * \return start pointer of the uri
zdshelby 1:14a9b0f4b9d6 1358 *
zdshelby 1:14a9b0f4b9d6 1359 */
zdshelby 1:14a9b0f4b9d6 1360
zdshelby 1:14a9b0f4b9d6 1361 static uint8_t *sn_grs_convert_uri(uint16_t *uri_len, uint8_t *uri_ptr)
zdshelby 1:14a9b0f4b9d6 1362 {
zdshelby 1:14a9b0f4b9d6 1363 /* Local variables */
zdshelby 1:14a9b0f4b9d6 1364 uint8_t *uri_start_ptr = uri_ptr;
zdshelby 1:14a9b0f4b9d6 1365
zdshelby 1:14a9b0f4b9d6 1366 /* If '/' in the beginning, update uri start pointer and uri len */
zdshelby 1:14a9b0f4b9d6 1367 if(*uri_ptr == '/')
zdshelby 1:14a9b0f4b9d6 1368 {
zdshelby 1:14a9b0f4b9d6 1369 uri_start_ptr = uri_ptr+1;
zdshelby 1:14a9b0f4b9d6 1370 *uri_len = *uri_len-1;
zdshelby 1:14a9b0f4b9d6 1371 }
zdshelby 1:14a9b0f4b9d6 1372
zdshelby 1:14a9b0f4b9d6 1373 /* If '/' at the end, update uri len */
zdshelby 1:14a9b0f4b9d6 1374 if(*(uri_start_ptr+*uri_len-1) == '/')
zdshelby 1:14a9b0f4b9d6 1375 {
zdshelby 1:14a9b0f4b9d6 1376 *uri_len = *uri_len-1;
zdshelby 1:14a9b0f4b9d6 1377 }
zdshelby 1:14a9b0f4b9d6 1378
zdshelby 1:14a9b0f4b9d6 1379 /* Return start pointer */
zdshelby 1:14a9b0f4b9d6 1380 return uri_start_ptr;
zdshelby 1:14a9b0f4b9d6 1381 }
zdshelby 1:14a9b0f4b9d6 1382
zdshelby 1:14a9b0f4b9d6 1383 /**
zdshelby 1:14a9b0f4b9d6 1384 * \fn static int8_t sn_grs_resource_info_free(sn_grs_resource_info_s *resource_ptr)
zdshelby 1:14a9b0f4b9d6 1385 *
zdshelby 1:14a9b0f4b9d6 1386 * \brief Frees resource info structure
zdshelby 1:14a9b0f4b9d6 1387 *
zdshelby 1:14a9b0f4b9d6 1388 * \param *resource_ptr Pointer to the resource
zdshelby 1:14a9b0f4b9d6 1389 *
zdshelby 1:14a9b0f4b9d6 1390 * \return 0 if success, -1 if failed
zdshelby 1:14a9b0f4b9d6 1391 *
zdshelby 1:14a9b0f4b9d6 1392 */
zdshelby 1:14a9b0f4b9d6 1393
zdshelby 1:14a9b0f4b9d6 1394 static int8_t sn_grs_resource_info_free(sn_nsdl_resource_info_s *resource_ptr)
zdshelby 1:14a9b0f4b9d6 1395 {
zdshelby 1:14a9b0f4b9d6 1396 if(resource_ptr)
zdshelby 1:14a9b0f4b9d6 1397 {
zdshelby 1:14a9b0f4b9d6 1398 if(resource_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 1399 {
zdshelby 1:14a9b0f4b9d6 1400 if(resource_ptr->resource_parameters_ptr->interface_description_ptr)
zdshelby 1:14a9b0f4b9d6 1401 {
zdshelby 1:14a9b0f4b9d6 1402 sn_grs_free(resource_ptr->resource_parameters_ptr->interface_description_ptr);
zdshelby 1:14a9b0f4b9d6 1403 resource_ptr->resource_parameters_ptr->interface_description_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1404 }
zdshelby 1:14a9b0f4b9d6 1405
zdshelby 1:14a9b0f4b9d6 1406 if(resource_ptr->resource_parameters_ptr->resource_type_ptr)
zdshelby 1:14a9b0f4b9d6 1407 {
zdshelby 1:14a9b0f4b9d6 1408 sn_grs_free(resource_ptr->resource_parameters_ptr->resource_type_ptr);
zdshelby 1:14a9b0f4b9d6 1409 resource_ptr->resource_parameters_ptr->resource_type_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1410 }
zdshelby 1:14a9b0f4b9d6 1411
zdshelby 1:14a9b0f4b9d6 1412 /* Todo: aobs not supported ATM - needs fixing */
zdshelby 1:14a9b0f4b9d6 1413 /*
zdshelby 1:14a9b0f4b9d6 1414 if(resource_ptr->resource_parameters_ptr->auto_obs_ptr)
zdshelby 1:14a9b0f4b9d6 1415 {
zdshelby 1:14a9b0f4b9d6 1416 sn_grs_free(resource_ptr->resource_parameters_ptr->auto_obs_ptr);
zdshelby 1:14a9b0f4b9d6 1417 resource_ptr->resource_parameters_ptr->auto_obs_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1418 }
zdshelby 1:14a9b0f4b9d6 1419 */
zdshelby 1:14a9b0f4b9d6 1420
zdshelby 1:14a9b0f4b9d6 1421 sn_grs_free(resource_ptr->resource_parameters_ptr);
zdshelby 1:14a9b0f4b9d6 1422 resource_ptr->resource_parameters_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1423 }
zdshelby 1:14a9b0f4b9d6 1424
zdshelby 1:14a9b0f4b9d6 1425 if(resource_ptr->path)
zdshelby 1:14a9b0f4b9d6 1426 {
zdshelby 1:14a9b0f4b9d6 1427 sn_grs_free(resource_ptr->path);
zdshelby 1:14a9b0f4b9d6 1428 resource_ptr->path = 0;
zdshelby 1:14a9b0f4b9d6 1429 }
zdshelby 1:14a9b0f4b9d6 1430 if(resource_ptr->resource)
zdshelby 1:14a9b0f4b9d6 1431 {
zdshelby 1:14a9b0f4b9d6 1432 sn_grs_free(resource_ptr->resource);
zdshelby 1:14a9b0f4b9d6 1433 resource_ptr->resource = 0;
zdshelby 1:14a9b0f4b9d6 1434 }
zdshelby 1:14a9b0f4b9d6 1435 sn_grs_free(resource_ptr);
zdshelby 1:14a9b0f4b9d6 1436 resource_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1437 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1438 }
zdshelby 1:14a9b0f4b9d6 1439 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1440 }
zdshelby 1:14a9b0f4b9d6 1441
zdshelby 1:14a9b0f4b9d6 1442 #ifdef CC8051_PLAT
zdshelby 1:14a9b0f4b9d6 1443 void copy_code_nsdl(uint8_t * ptr, prog_uint8_t * code_ptr, uint16_t len)
zdshelby 1:14a9b0f4b9d6 1444 {
zdshelby 1:14a9b0f4b9d6 1445 uint16_t i;
zdshelby 1:14a9b0f4b9d6 1446 for(i=0; i<len; i++)
zdshelby 1:14a9b0f4b9d6 1447 {
zdshelby 1:14a9b0f4b9d6 1448 ptr[i] = code_ptr[i];
zdshelby 1:14a9b0f4b9d6 1449 }
zdshelby 1:14a9b0f4b9d6 1450 }
zdshelby 1:14a9b0f4b9d6 1451 #endif
zdshelby 1:14a9b0f4b9d6 1452
zdshelby 1:14a9b0f4b9d6 1453 static uint8_t sn_grs_compare_code(uint8_t * ptr, prog_uint8_t * code_ptr, uint8_t len)
zdshelby 1:14a9b0f4b9d6 1454 {
zdshelby 1:14a9b0f4b9d6 1455 uint8_t i=0;
zdshelby 1:14a9b0f4b9d6 1456 while(len)
zdshelby 1:14a9b0f4b9d6 1457 {
zdshelby 1:14a9b0f4b9d6 1458 if(ptr[i] != code_ptr[i])
zdshelby 1:14a9b0f4b9d6 1459 {
zdshelby 1:14a9b0f4b9d6 1460 break;
zdshelby 1:14a9b0f4b9d6 1461 }
zdshelby 1:14a9b0f4b9d6 1462 len--;
zdshelby 1:14a9b0f4b9d6 1463 i++;
zdshelby 1:14a9b0f4b9d6 1464 }
zdshelby 1:14a9b0f4b9d6 1465 return len;
zdshelby 1:14a9b0f4b9d6 1466 }
zdshelby 1:14a9b0f4b9d6 1467
zdshelby 1:14a9b0f4b9d6 1468