hello

Dependents:   nespresso_demo nespresso_endpoint EnvoyNespressoEndpointColorDetectorV2

Fork of nsdl by Robert Taylor

Committer:
bjblazkowicz
Date:
Mon Jul 14 12:33:59 2014 +0000
Revision:
2:05e4cd1a1542
Parent:
0:f6e4e1bbb3fe
WIP - registration is broken in this commit.

Who changed what in which revision?

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