Manual NanoService C Client 1.11-RC2 import

Dependents:   Trenton_Doormat_FRDM-KL25Z_ETH

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
zdshelby 1:14a9b0f4b9d6 1 /**
zdshelby 1:14a9b0f4b9d6 2 * \file sn_nsdl.c
zdshelby 1:14a9b0f4b9d6 3 *
zdshelby 1:14a9b0f4b9d6 4 * \brief Nano service device library
zdshelby 1:14a9b0f4b9d6 5 *
zdshelby 1:14a9b0f4b9d6 6 * Application interface to CoAP, GRS and NSP handling.
zdshelby 1:14a9b0f4b9d6 7 *
zdshelby 1:14a9b0f4b9d6 8 */
zdshelby 1:14a9b0f4b9d6 9
zdshelby 1:14a9b0f4b9d6 10 #include <string.h>
zdshelby 1:14a9b0f4b9d6 11
zdshelby 1:14a9b0f4b9d6 12 #include "nsdl_types.h"
zdshelby 1:14a9b0f4b9d6 13 #include "sn_nsdl.h"
zdshelby 1:14a9b0f4b9d6 14 #include "sn_coap_header.h"
zdshelby 1:14a9b0f4b9d6 15 #include "sn_coap_protocol.h"
zdshelby 1:14a9b0f4b9d6 16 #include "sn_nsdl_lib.h"
zdshelby 1:14a9b0f4b9d6 17 #include "sn_grs.h"
zdshelby 1:14a9b0f4b9d6 18 #include "sn_linked_list.h"
zdshelby 1:14a9b0f4b9d6 19
zdshelby 1:14a9b0f4b9d6 20 /* Defines */
zdshelby 1:14a9b0f4b9d6 21 #define RESOURCE_DIR_LEN 2
zdshelby 1:14a9b0f4b9d6 22 #define RESOURCE_DIR_PATH {'r','d'}
zdshelby 1:14a9b0f4b9d6 23
zdshelby 1:14a9b0f4b9d6 24 #define EP_NAME_PARAMETERS_LEN 2
zdshelby 1:14a9b0f4b9d6 25 #define EP_NAME_PARAMETERS {'h','='}
zdshelby 1:14a9b0f4b9d6 26
zdshelby 1:14a9b0f4b9d6 27 #define RT_PARAMETER_LEN 3
zdshelby 1:14a9b0f4b9d6 28 #define RT_PARAMETER {'r','t','='}
zdshelby 1:14a9b0f4b9d6 29
zdshelby 1:14a9b0f4b9d6 30 #define IF_PARAMETER_LEN 3
zdshelby 1:14a9b0f4b9d6 31 #define IF_PARAMETER {'i','f','='}
zdshelby 1:14a9b0f4b9d6 32
zdshelby 1:14a9b0f4b9d6 33 #define CON_PARAMETER_LEN 4
zdshelby 1:14a9b0f4b9d6 34 #define CON_PARAMETER {'c','o','n','='}
zdshelby 1:14a9b0f4b9d6 35
zdshelby 1:14a9b0f4b9d6 36 #define LT_PARAMETER_LEN 3
zdshelby 1:14a9b0f4b9d6 37 #define LT_PARAMETER {'l','t','='}
zdshelby 1:14a9b0f4b9d6 38
zdshelby 1:14a9b0f4b9d6 39 #define OBS_PARAMETER_LEN 3
zdshelby 1:14a9b0f4b9d6 40 #define OBS_PARAMETER {'o','b','s'}
zdshelby 1:14a9b0f4b9d6 41
zdshelby 1:14a9b0f4b9d6 42 #define AOBS_PARAMETER_LEN 8
zdshelby 1:14a9b0f4b9d6 43 #define AOBS_PARAMETER {'a','o','b','s',';','i','d','='}
zdshelby 1:14a9b0f4b9d6 44
zdshelby 1:14a9b0f4b9d6 45 #define COAP_CON_PARAMETER_LEN 3
zdshelby 1:14a9b0f4b9d6 46 #define COAP_CON_PARAMETER {'c','t','='}
zdshelby 1:14a9b0f4b9d6 47
zdshelby 1:14a9b0f4b9d6 48 #define EVENT_PATH_LEN 6
zdshelby 1:14a9b0f4b9d6 49 #define EVENT_PATH {'e','v','e','n','t','/'}
zdshelby 1:14a9b0f4b9d6 50
zdshelby 1:14a9b0f4b9d6 51 #define SN_NSDL_EP_REGISTER_MESSAGE 1
zdshelby 1:14a9b0f4b9d6 52 #define SN_NSDL_EP_UPDATE_MESSAGE 2
zdshelby 1:14a9b0f4b9d6 53
zdshelby 1:14a9b0f4b9d6 54 #define SN_NSDL_MSG_NO_TYPE 0
zdshelby 1:14a9b0f4b9d6 55 #define SN_NSDL_MSG_REGISTER 1
zdshelby 1:14a9b0f4b9d6 56 #define SN_NSDL_MSG_UNREGISTER 2
zdshelby 1:14a9b0f4b9d6 57 #define SN_NSDL_MSG_UPDATE 3
zdshelby 1:14a9b0f4b9d6 58 #define SN_NSDL_MSG_EVENT 4
zdshelby 1:14a9b0f4b9d6 59
zdshelby 1:14a9b0f4b9d6 60 #define SN_NSDL_MAX_MESSAGE_COUNT 1
zdshelby 1:14a9b0f4b9d6 61
zdshelby 1:14a9b0f4b9d6 62 /* Constants */
zdshelby 1:14a9b0f4b9d6 63 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 64 static uint8_t ep_name_parameter_string[] = EP_NAME_PARAMETERS;
zdshelby 1:14a9b0f4b9d6 65
zdshelby 1:14a9b0f4b9d6 66 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 67 static uint8_t resource_path_ptr[] = RESOURCE_DIR_PATH;
zdshelby 1:14a9b0f4b9d6 68
zdshelby 1:14a9b0f4b9d6 69 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 70 static uint8_t resource_type_parameter[] = RT_PARAMETER;
zdshelby 1:14a9b0f4b9d6 71
zdshelby 1:14a9b0f4b9d6 72 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 73 static uint8_t obs_parameter[] = OBS_PARAMETER;
zdshelby 1:14a9b0f4b9d6 74
zdshelby 1:14a9b0f4b9d6 75 //SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 76 //static uint8_t aobs_parameter[] = AOBS_PARAMETER;
zdshelby 1:14a9b0f4b9d6 77
zdshelby 1:14a9b0f4b9d6 78 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 79 static uint8_t if_description_parameter[] = IF_PARAMETER;
zdshelby 1:14a9b0f4b9d6 80
zdshelby 1:14a9b0f4b9d6 81 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 82 static uint8_t ep_lifetime_parameter[] = LT_PARAMETER;
zdshelby 1:14a9b0f4b9d6 83
zdshelby 1:14a9b0f4b9d6 84 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 85 static uint8_t coap_con_type_parameter[] = COAP_CON_PARAMETER;
zdshelby 1:14a9b0f4b9d6 86
zdshelby 1:14a9b0f4b9d6 87 SN_NSDL_CONST_MEMORY_ATTRIBUTE
zdshelby 1:14a9b0f4b9d6 88 static uint8_t event_path_parameter[] = EVENT_PATH;
zdshelby 1:14a9b0f4b9d6 89
zdshelby 1:14a9b0f4b9d6 90 /* Global function pointers */
zdshelby 1:14a9b0f4b9d6 91 static void *(*sn_nsdl_alloc)(uint16_t) = 0;
zdshelby 1:14a9b0f4b9d6 92 static void (*sn_nsdl_free)(void*) = 0;
zdshelby 1:14a9b0f4b9d6 93 static uint8_t (*sn_nsdl_tx_callback)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *) = 0;
zdshelby 1:14a9b0f4b9d6 94 static uint8_t (*sn_nsdl_rx_callback)(sn_coap_hdr_s *, sn_nsdl_addr_s *) = 0;
zdshelby 1:14a9b0f4b9d6 95
zdshelby 1:14a9b0f4b9d6 96 /* Global variables */
zdshelby 1:14a9b0f4b9d6 97 static sn_nsdl_ep_parameters_s *ep_information_ptr = 0; // Endpoint parameters, Name, Domain etc..
zdshelby 1:14a9b0f4b9d6 98 static sn_nsdl_addr_s *nsp_address_ptr = 0; // NSP server address information
zdshelby 1:14a9b0f4b9d6 99 static sn_linked_list_t *message_list_ptr = 0; //
zdshelby 1:14a9b0f4b9d6 100 static uint8_t sn_nsdl_endpoint_registered = 0;
zdshelby 1:14a9b0f4b9d6 101
zdshelby 1:14a9b0f4b9d6 102 /* Function prototypes */
zdshelby 1:14a9b0f4b9d6 103 static int8_t sn_nsdl_internal_coap_send (sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description);
zdshelby 1:14a9b0f4b9d6 104 static void sn_nsdl_resolve_nsp_address (void);
zdshelby 1:14a9b0f4b9d6 105 int8_t sn_nsdl_build_registration_body (sn_coap_hdr_s *message_ptr, uint8_t updating_registeration);
zdshelby 1:14a9b0f4b9d6 106 static uint16_t sn_nsdl_calculate_registration_body_size (uint8_t updating_registeration);
zdshelby 1:14a9b0f4b9d6 107 static uint8_t sn_nsdl_calculate_uri_query_option_len (sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type);
zdshelby 1:14a9b0f4b9d6 108 static int8_t sn_nsdl_fill_uri_query_options (sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type);
zdshelby 1:14a9b0f4b9d6 109 static int8_t sn_nsdl_local_rx_function (sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr);
zdshelby 1:14a9b0f4b9d6 110 static int8_t sn_nsdl_resolve_ep_information (sn_coap_hdr_s *coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 111 static void sn_nsdl_mark_resources_as_registered (void);
zdshelby 1:14a9b0f4b9d6 112 static uint8_t sn_nsdl_itoa_len (uint8_t value);
zdshelby 1:14a9b0f4b9d6 113 static uint8_t *sn_nsdl_itoa (uint8_t *ptr, uint8_t value);
zdshelby 1:14a9b0f4b9d6 114
zdshelby 1:14a9b0f4b9d6 115
zdshelby 1:14a9b0f4b9d6 116 int8_t sn_nsdl_destroy(void)
zdshelby 1:14a9b0f4b9d6 117 {
zdshelby 1:14a9b0f4b9d6 118 if(message_list_ptr)
zdshelby 1:14a9b0f4b9d6 119 {
zdshelby 1:14a9b0f4b9d6 120 uint16_t size = sn_linked_list_count_nodes(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 121 uint16_t i = 0;
zdshelby 1:14a9b0f4b9d6 122 sn_nsdl_sent_messages_s*tmp;
zdshelby 1:14a9b0f4b9d6 123
zdshelby 1:14a9b0f4b9d6 124
zdshelby 1:14a9b0f4b9d6 125 for(i=0;i<size;i++)
zdshelby 1:14a9b0f4b9d6 126 {
zdshelby 1:14a9b0f4b9d6 127 tmp = sn_linked_list_get_first_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 128
zdshelby 1:14a9b0f4b9d6 129 if(tmp)
zdshelby 1:14a9b0f4b9d6 130 {
zdshelby 1:14a9b0f4b9d6 131 sn_linked_list_remove_current_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 132 sn_nsdl_free(tmp);
zdshelby 1:14a9b0f4b9d6 133 tmp = 0;
zdshelby 1:14a9b0f4b9d6 134 }
zdshelby 1:14a9b0f4b9d6 135 }
zdshelby 1:14a9b0f4b9d6 136
zdshelby 1:14a9b0f4b9d6 137 if(!sn_linked_list_count_nodes(message_list_ptr))
zdshelby 1:14a9b0f4b9d6 138 {
zdshelby 1:14a9b0f4b9d6 139 sn_linked_list_free(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 140 message_list_ptr = 0;
zdshelby 1:14a9b0f4b9d6 141 }
zdshelby 1:14a9b0f4b9d6 142 }
zdshelby 1:14a9b0f4b9d6 143
zdshelby 1:14a9b0f4b9d6 144 if(ep_information_ptr)
zdshelby 1:14a9b0f4b9d6 145 {
zdshelby 1:14a9b0f4b9d6 146 if(ep_information_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 147 {
zdshelby 1:14a9b0f4b9d6 148 sn_nsdl_free(ep_information_ptr->endpoint_name_ptr);
zdshelby 1:14a9b0f4b9d6 149 ep_information_ptr->endpoint_name_ptr = 0;
zdshelby 1:14a9b0f4b9d6 150 }
zdshelby 1:14a9b0f4b9d6 151 if(ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 152 {
zdshelby 1:14a9b0f4b9d6 153 sn_nsdl_free(ep_information_ptr->domain_name_ptr);
zdshelby 1:14a9b0f4b9d6 154 ep_information_ptr->domain_name_ptr = 0;
zdshelby 1:14a9b0f4b9d6 155 ep_information_ptr->domain_name_len = 0;
zdshelby 1:14a9b0f4b9d6 156 }
zdshelby 1:14a9b0f4b9d6 157 if(ep_information_ptr->type_ptr)
zdshelby 1:14a9b0f4b9d6 158 {
zdshelby 1:14a9b0f4b9d6 159 sn_nsdl_free(ep_information_ptr->type_ptr);
zdshelby 1:14a9b0f4b9d6 160 ep_information_ptr->type_ptr = 0;
zdshelby 1:14a9b0f4b9d6 161 }
zdshelby 1:14a9b0f4b9d6 162
zdshelby 1:14a9b0f4b9d6 163 if(ep_information_ptr->lifetime_ptr)
zdshelby 1:14a9b0f4b9d6 164
zdshelby 1:14a9b0f4b9d6 165 {
zdshelby 1:14a9b0f4b9d6 166 sn_nsdl_free(ep_information_ptr->lifetime_ptr);
zdshelby 1:14a9b0f4b9d6 167 ep_information_ptr->lifetime_ptr = 0;
zdshelby 1:14a9b0f4b9d6 168 }
zdshelby 1:14a9b0f4b9d6 169
zdshelby 1:14a9b0f4b9d6 170 sn_nsdl_free(ep_information_ptr);
zdshelby 1:14a9b0f4b9d6 171 ep_information_ptr = 0;
zdshelby 1:14a9b0f4b9d6 172 }
zdshelby 1:14a9b0f4b9d6 173
zdshelby 1:14a9b0f4b9d6 174 if(nsp_address_ptr)
zdshelby 1:14a9b0f4b9d6 175 {
zdshelby 1:14a9b0f4b9d6 176 if(nsp_address_ptr->socket_information)
zdshelby 1:14a9b0f4b9d6 177 {
zdshelby 1:14a9b0f4b9d6 178 sn_nsdl_free(nsp_address_ptr->socket_information);
zdshelby 1:14a9b0f4b9d6 179 nsp_address_ptr->socket_information= 0;
zdshelby 1:14a9b0f4b9d6 180 }
zdshelby 1:14a9b0f4b9d6 181
zdshelby 1:14a9b0f4b9d6 182 if(nsp_address_ptr->addr_ptr)
zdshelby 1:14a9b0f4b9d6 183 {
zdshelby 1:14a9b0f4b9d6 184 sn_nsdl_free(nsp_address_ptr->addr_ptr);
zdshelby 1:14a9b0f4b9d6 185 nsp_address_ptr->addr_ptr = 0;
zdshelby 1:14a9b0f4b9d6 186 }
zdshelby 1:14a9b0f4b9d6 187 sn_nsdl_free(nsp_address_ptr);
zdshelby 1:14a9b0f4b9d6 188 nsp_address_ptr = 0;
zdshelby 1:14a9b0f4b9d6 189 }
zdshelby 1:14a9b0f4b9d6 190
zdshelby 1:14a9b0f4b9d6 191 /* Destroy also libCoap and grs part of libNsdl */
zdshelby 1:14a9b0f4b9d6 192 sn_grs_destroy();
zdshelby 1:14a9b0f4b9d6 193 sn_coap_protocol_destroy();
zdshelby 1:14a9b0f4b9d6 194
zdshelby 1:14a9b0f4b9d6 195 return 0;
zdshelby 1:14a9b0f4b9d6 196 }
zdshelby 1:14a9b0f4b9d6 197
zdshelby 1:14a9b0f4b9d6 198 int8_t sn_nsdl_init (uint8_t (*sn_nsdl_tx_cb)(sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *),
zdshelby 1:14a9b0f4b9d6 199 uint8_t (*sn_nsdl_rx_cb)(sn_coap_hdr_s *, sn_nsdl_addr_s *),
zdshelby 1:14a9b0f4b9d6 200 sn_nsdl_mem_s *sn_memory)
zdshelby 1:14a9b0f4b9d6 201 {
zdshelby 1:14a9b0f4b9d6 202 /* Check pointers and define function pointers */
zdshelby 1:14a9b0f4b9d6 203 if(!sn_memory || !sn_memory->sn_nsdl_alloc || !sn_memory->sn_nsdl_free || !sn_nsdl_tx_cb || !sn_nsdl_rx_cb)
zdshelby 1:14a9b0f4b9d6 204 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 205
zdshelby 1:14a9b0f4b9d6 206 /* Define function pointers */
zdshelby 1:14a9b0f4b9d6 207 sn_nsdl_alloc = sn_memory->sn_nsdl_alloc;
zdshelby 1:14a9b0f4b9d6 208 sn_nsdl_free = sn_memory->sn_nsdl_free;
zdshelby 1:14a9b0f4b9d6 209
zdshelby 1:14a9b0f4b9d6 210 sn_nsdl_tx_callback = sn_nsdl_tx_cb;
zdshelby 1:14a9b0f4b9d6 211 sn_nsdl_rx_callback = sn_nsdl_rx_cb;
zdshelby 1:14a9b0f4b9d6 212
zdshelby 1:14a9b0f4b9d6 213 sn_linked_list_init(sn_nsdl_alloc, sn_nsdl_free);
zdshelby 1:14a9b0f4b9d6 214
zdshelby 1:14a9b0f4b9d6 215 message_list_ptr = sn_linked_list_create();
zdshelby 1:14a9b0f4b9d6 216 if(!message_list_ptr)
zdshelby 1:14a9b0f4b9d6 217 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 218
zdshelby 1:14a9b0f4b9d6 219 /* Initialize ep parameters struct */
zdshelby 1:14a9b0f4b9d6 220 if(!ep_information_ptr)
zdshelby 1:14a9b0f4b9d6 221 {
zdshelby 1:14a9b0f4b9d6 222 ep_information_ptr = sn_nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
zdshelby 1:14a9b0f4b9d6 223 if(!ep_information_ptr)
zdshelby 1:14a9b0f4b9d6 224 {
zdshelby 1:14a9b0f4b9d6 225 sn_linked_list_free(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 226 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 227 }
zdshelby 1:14a9b0f4b9d6 228 memset(ep_information_ptr, 0, sizeof(sn_nsdl_ep_parameters_s));
zdshelby 1:14a9b0f4b9d6 229 }
zdshelby 1:14a9b0f4b9d6 230
zdshelby 1:14a9b0f4b9d6 231 /* Initialize GRS */
zdshelby 1:14a9b0f4b9d6 232 if(sn_grs_init(sn_nsdl_tx_cb,&sn_nsdl_local_rx_function, sn_memory))
zdshelby 1:14a9b0f4b9d6 233 {
zdshelby 1:14a9b0f4b9d6 234
zdshelby 1:14a9b0f4b9d6 235 sn_nsdl_free(ep_information_ptr);
zdshelby 1:14a9b0f4b9d6 236 ep_information_ptr = 0;
zdshelby 1:14a9b0f4b9d6 237 sn_linked_list_free(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 238 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 239
zdshelby 1:14a9b0f4b9d6 240 }
zdshelby 1:14a9b0f4b9d6 241
zdshelby 1:14a9b0f4b9d6 242 // todo: Resolve NS server address -> v0.5 = hardcoded address
zdshelby 1:14a9b0f4b9d6 243 sn_nsdl_resolve_nsp_address();
zdshelby 1:14a9b0f4b9d6 244
zdshelby 1:14a9b0f4b9d6 245 sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_NOT_REGISTERED;
zdshelby 1:14a9b0f4b9d6 246
zdshelby 1:14a9b0f4b9d6 247 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 248 }
zdshelby 1:14a9b0f4b9d6 249
zdshelby 1:14a9b0f4b9d6 250 int8_t sn_nsdl_GET_with_QUERY(char * uri, uint16_t urilen, uint8_t*destination, uint16_t port, char *query, uint8_t query_len)
zdshelby 1:14a9b0f4b9d6 251 {
zdshelby 1:14a9b0f4b9d6 252 sn_coap_hdr_s *message_ptr;
zdshelby 1:14a9b0f4b9d6 253 sn_nsdl_addr_s *dst = 0;
zdshelby 1:14a9b0f4b9d6 254
zdshelby 1:14a9b0f4b9d6 255 message_ptr = sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 256 if(message_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 257 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 258
zdshelby 1:14a9b0f4b9d6 259 memset(message_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 260
zdshelby 1:14a9b0f4b9d6 261 /* Fill message fields -> confirmable post to specified NSP path */
zdshelby 1:14a9b0f4b9d6 262 message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
zdshelby 1:14a9b0f4b9d6 263 message_ptr->msg_code = COAP_MSG_CODE_REQUEST_GET;
zdshelby 1:14a9b0f4b9d6 264 /* Allocate memory for the extended options list */
zdshelby 1:14a9b0f4b9d6 265 message_ptr->options_list_ptr = sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 266 if(message_ptr->options_list_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 267 {
zdshelby 1:14a9b0f4b9d6 268 sn_nsdl_free(message_ptr);
zdshelby 1:14a9b0f4b9d6 269 message_ptr = 0;
zdshelby 1:14a9b0f4b9d6 270 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 271 }
zdshelby 1:14a9b0f4b9d6 272
zdshelby 1:14a9b0f4b9d6 273
zdshelby 1:14a9b0f4b9d6 274 memset(message_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 275 message_ptr->options_list_ptr->uri_query_len =query_len;
zdshelby 1:14a9b0f4b9d6 276 message_ptr->options_list_ptr->uri_query_ptr = (uint8_t *)query;
zdshelby 1:14a9b0f4b9d6 277 message_ptr->uri_path_len = urilen;
zdshelby 1:14a9b0f4b9d6 278 message_ptr->uri_path_ptr = (uint8_t *)uri;
zdshelby 1:14a9b0f4b9d6 279
zdshelby 1:14a9b0f4b9d6 280 /* Build and send coap message to NSP */
zdshelby 1:14a9b0f4b9d6 281 /* Local variables */
zdshelby 1:14a9b0f4b9d6 282 if(!dst)
zdshelby 1:14a9b0f4b9d6 283 {
zdshelby 1:14a9b0f4b9d6 284 //allocate only if previously not allocated
zdshelby 1:14a9b0f4b9d6 285 dst = sn_nsdl_alloc(sizeof(sn_nsdl_addr_s));
zdshelby 1:14a9b0f4b9d6 286 }
zdshelby 1:14a9b0f4b9d6 287
zdshelby 1:14a9b0f4b9d6 288 if(dst)
zdshelby 1:14a9b0f4b9d6 289 {
zdshelby 1:14a9b0f4b9d6 290 /* This is only for version 0.5 */
zdshelby 1:14a9b0f4b9d6 291 dst->type = SN_NSDL_ADDRESS_TYPE_IPV6;
zdshelby 1:14a9b0f4b9d6 292 dst->port = port;
zdshelby 1:14a9b0f4b9d6 293 dst->addr_len = 16;
zdshelby 1:14a9b0f4b9d6 294 if(!dst->addr_ptr)
zdshelby 1:14a9b0f4b9d6 295 {
zdshelby 1:14a9b0f4b9d6 296 dst->addr_ptr = sn_nsdl_alloc(dst->addr_len);
zdshelby 1:14a9b0f4b9d6 297 memcpy(dst->addr_ptr, destination, 16);
zdshelby 1:14a9b0f4b9d6 298 }
zdshelby 1:14a9b0f4b9d6 299 }
zdshelby 1:14a9b0f4b9d6 300
zdshelby 1:14a9b0f4b9d6 301 sn_grs_send_coap_message(dst, message_ptr);
zdshelby 1:14a9b0f4b9d6 302
zdshelby 1:14a9b0f4b9d6 303 if(dst->addr_ptr)
zdshelby 1:14a9b0f4b9d6 304 sn_nsdl_free(dst->addr_ptr);
zdshelby 1:14a9b0f4b9d6 305
zdshelby 1:14a9b0f4b9d6 306 if(dst)
zdshelby 1:14a9b0f4b9d6 307 sn_nsdl_free(dst);
zdshelby 1:14a9b0f4b9d6 308 message_ptr->uri_path_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 309 message_ptr->options_list_ptr->uri_host_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 310 message_ptr->options_list_ptr->uri_query_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 311
zdshelby 1:14a9b0f4b9d6 312 sn_coap_parser_release_allocated_coap_msg_mem(message_ptr);
zdshelby 1:14a9b0f4b9d6 313 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 314 }
zdshelby 1:14a9b0f4b9d6 315
zdshelby 1:14a9b0f4b9d6 316 int8_t sn_nsdl_GET(char * uri, uint16_t urilen, uint8_t*destination, uint16_t port)
zdshelby 1:14a9b0f4b9d6 317 {
zdshelby 1:14a9b0f4b9d6 318 sn_coap_hdr_s *message_ptr;
zdshelby 1:14a9b0f4b9d6 319 sn_nsdl_addr_s *dst = 0;
zdshelby 1:14a9b0f4b9d6 320
zdshelby 1:14a9b0f4b9d6 321
zdshelby 1:14a9b0f4b9d6 322 message_ptr = sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 323 if(message_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 324 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 325
zdshelby 1:14a9b0f4b9d6 326 memset(message_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 327
zdshelby 1:14a9b0f4b9d6 328 /* Fill message fields -> confirmable post to specified NSP path */
zdshelby 1:14a9b0f4b9d6 329 message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
zdshelby 1:14a9b0f4b9d6 330 message_ptr->msg_code = COAP_MSG_CODE_REQUEST_GET;
zdshelby 1:14a9b0f4b9d6 331 /* Allocate memory for the extended options list */
zdshelby 1:14a9b0f4b9d6 332 message_ptr->options_list_ptr = sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 333 if(message_ptr->options_list_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 334 {
zdshelby 1:14a9b0f4b9d6 335 sn_nsdl_free(message_ptr);
zdshelby 1:14a9b0f4b9d6 336 message_ptr = 0;
zdshelby 1:14a9b0f4b9d6 337 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 338 }
zdshelby 1:14a9b0f4b9d6 339
zdshelby 1:14a9b0f4b9d6 340
zdshelby 1:14a9b0f4b9d6 341 memset(message_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 342
zdshelby 1:14a9b0f4b9d6 343 message_ptr->uri_path_len = urilen;
zdshelby 1:14a9b0f4b9d6 344 message_ptr->uri_path_ptr = (uint8_t *)uri;
zdshelby 1:14a9b0f4b9d6 345
zdshelby 1:14a9b0f4b9d6 346 /* Build and send coap message to NSP */
zdshelby 1:14a9b0f4b9d6 347 /* Local variables */
zdshelby 1:14a9b0f4b9d6 348 if(!dst)
zdshelby 1:14a9b0f4b9d6 349 {
zdshelby 1:14a9b0f4b9d6 350 //allocate only if previously not allocated
zdshelby 1:14a9b0f4b9d6 351 dst = sn_nsdl_alloc(sizeof(sn_nsdl_addr_s));
zdshelby 1:14a9b0f4b9d6 352 memset(dst, 0, sizeof(sn_nsdl_addr_s));
zdshelby 1:14a9b0f4b9d6 353 }
zdshelby 1:14a9b0f4b9d6 354
zdshelby 1:14a9b0f4b9d6 355 if(dst)
zdshelby 1:14a9b0f4b9d6 356 {
zdshelby 1:14a9b0f4b9d6 357 /* This is only for version 0.5 */
zdshelby 1:14a9b0f4b9d6 358 dst->type = SN_NSDL_ADDRESS_TYPE_IPV6;
zdshelby 1:14a9b0f4b9d6 359 dst->port = port;
zdshelby 1:14a9b0f4b9d6 360 dst->addr_len = 16;
zdshelby 1:14a9b0f4b9d6 361 if(!dst->addr_ptr)
zdshelby 1:14a9b0f4b9d6 362 {
zdshelby 1:14a9b0f4b9d6 363 dst->addr_ptr = sn_nsdl_alloc(dst->addr_len);
zdshelby 1:14a9b0f4b9d6 364 memcpy(dst->addr_ptr, destination, 16);
zdshelby 1:14a9b0f4b9d6 365 }
zdshelby 1:14a9b0f4b9d6 366 }
zdshelby 1:14a9b0f4b9d6 367 sn_grs_send_coap_message(dst, message_ptr);
zdshelby 1:14a9b0f4b9d6 368
zdshelby 1:14a9b0f4b9d6 369 if(dst->addr_ptr)
zdshelby 1:14a9b0f4b9d6 370 sn_nsdl_free(dst->addr_ptr);
zdshelby 1:14a9b0f4b9d6 371 if(dst)
zdshelby 1:14a9b0f4b9d6 372 sn_nsdl_free(dst);
zdshelby 1:14a9b0f4b9d6 373 message_ptr->uri_path_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 374 message_ptr->options_list_ptr->uri_host_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 375
zdshelby 1:14a9b0f4b9d6 376 sn_coap_parser_release_allocated_coap_msg_mem(message_ptr);
zdshelby 1:14a9b0f4b9d6 377 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 378 }
zdshelby 1:14a9b0f4b9d6 379
zdshelby 1:14a9b0f4b9d6 380
zdshelby 1:14a9b0f4b9d6 381
zdshelby 1:14a9b0f4b9d6 382 int8_t sn_nsdl_register_endpoint(sn_nsdl_ep_parameters_s *endpoint_info_ptr)
zdshelby 1:14a9b0f4b9d6 383 {
zdshelby 1:14a9b0f4b9d6 384 /* Local variables */
zdshelby 1:14a9b0f4b9d6 385 sn_coap_hdr_s *register_message_ptr;
zdshelby 1:14a9b0f4b9d6 386 int8_t status = 0;
zdshelby 1:14a9b0f4b9d6 387
zdshelby 1:14a9b0f4b9d6 388 if(!endpoint_info_ptr)
zdshelby 1:14a9b0f4b9d6 389 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 390
zdshelby 1:14a9b0f4b9d6 391 /*** Build endpoint register message ***/
zdshelby 1:14a9b0f4b9d6 392
zdshelby 1:14a9b0f4b9d6 393 /* Allocate memory for header struct */
zdshelby 1:14a9b0f4b9d6 394 register_message_ptr = sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 395 if(register_message_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 396 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 397
zdshelby 1:14a9b0f4b9d6 398 memset(register_message_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 399
zdshelby 1:14a9b0f4b9d6 400 /* Fill message fields -> confirmable post to specified NSP path */
zdshelby 1:14a9b0f4b9d6 401 register_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
zdshelby 1:14a9b0f4b9d6 402 register_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST;
zdshelby 1:14a9b0f4b9d6 403
zdshelby 1:14a9b0f4b9d6 404 /* Allocate memory for the extended options list */
zdshelby 1:14a9b0f4b9d6 405 register_message_ptr->options_list_ptr = sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 406 if(register_message_ptr->options_list_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 407 {
zdshelby 1:14a9b0f4b9d6 408 sn_nsdl_free(register_message_ptr);
zdshelby 1:14a9b0f4b9d6 409 register_message_ptr = 0;
zdshelby 1:14a9b0f4b9d6 410 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 411 }
zdshelby 1:14a9b0f4b9d6 412
zdshelby 1:14a9b0f4b9d6 413 memset(register_message_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 414
zdshelby 1:14a9b0f4b9d6 415 register_message_ptr->uri_path_len = sizeof(resource_path_ptr);
zdshelby 1:14a9b0f4b9d6 416 register_message_ptr->uri_path_ptr = resource_path_ptr;
zdshelby 1:14a9b0f4b9d6 417
zdshelby 1:14a9b0f4b9d6 418 /* If domain name is configured, fill needed fields */
zdshelby 1:14a9b0f4b9d6 419 if(endpoint_info_ptr->domain_name_len)
zdshelby 1:14a9b0f4b9d6 420 {
zdshelby 1:14a9b0f4b9d6 421 register_message_ptr->options_list_ptr->uri_host_len = endpoint_info_ptr->domain_name_len;
zdshelby 1:14a9b0f4b9d6 422 register_message_ptr->options_list_ptr->uri_host_ptr = endpoint_info_ptr->domain_name_ptr;
zdshelby 1:14a9b0f4b9d6 423 }
zdshelby 1:14a9b0f4b9d6 424
zdshelby 1:14a9b0f4b9d6 425 /* Fill Uri-query options */
zdshelby 1:14a9b0f4b9d6 426 sn_nsdl_fill_uri_query_options(endpoint_info_ptr, register_message_ptr, SN_NSDL_EP_REGISTER_MESSAGE);
zdshelby 1:14a9b0f4b9d6 427 #ifndef REG_TEMPLATE
zdshelby 1:14a9b0f4b9d6 428 /* Built body for message */
zdshelby 1:14a9b0f4b9d6 429 status = sn_nsdl_build_registration_body(register_message_ptr, 0);
zdshelby 1:14a9b0f4b9d6 430 if(status == SN_NSDL_FAILURE)
zdshelby 1:14a9b0f4b9d6 431 {
zdshelby 1:14a9b0f4b9d6 432 register_message_ptr->uri_path_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 433 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 434 sn_coap_parser_release_allocated_coap_msg_mem(register_message_ptr);
zdshelby 1:14a9b0f4b9d6 435 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 436 }
zdshelby 1:14a9b0f4b9d6 437 #endif
zdshelby 1:14a9b0f4b9d6 438 /* Build and send coap message to NSP */
zdshelby 1:14a9b0f4b9d6 439 status = sn_nsdl_internal_coap_send(register_message_ptr, nsp_address_ptr, SN_NSDL_MSG_REGISTER);
zdshelby 1:14a9b0f4b9d6 440
zdshelby 1:14a9b0f4b9d6 441 if(register_message_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 442 {
zdshelby 1:14a9b0f4b9d6 443 sn_nsdl_free(register_message_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 444 register_message_ptr->payload_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 445 }
zdshelby 1:14a9b0f4b9d6 446
zdshelby 1:14a9b0f4b9d6 447 register_message_ptr->uri_path_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 448 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 449
zdshelby 1:14a9b0f4b9d6 450 sn_coap_parser_release_allocated_coap_msg_mem(register_message_ptr);
zdshelby 1:14a9b0f4b9d6 451
zdshelby 1:14a9b0f4b9d6 452 if(ep_information_ptr)
zdshelby 1:14a9b0f4b9d6 453 {
zdshelby 1:14a9b0f4b9d6 454 if(ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 455 {
zdshelby 1:14a9b0f4b9d6 456 sn_nsdl_free(ep_information_ptr->domain_name_ptr);
zdshelby 1:14a9b0f4b9d6 457 ep_information_ptr->domain_name_ptr = 0;
zdshelby 1:14a9b0f4b9d6 458 ep_information_ptr->domain_name_len = 0;
zdshelby 1:14a9b0f4b9d6 459 }
zdshelby 1:14a9b0f4b9d6 460
zdshelby 1:14a9b0f4b9d6 461 if(ep_information_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 462 {
zdshelby 1:14a9b0f4b9d6 463 sn_nsdl_free(ep_information_ptr->endpoint_name_ptr);
zdshelby 1:14a9b0f4b9d6 464 ep_information_ptr->endpoint_name_ptr = 0;
zdshelby 1:14a9b0f4b9d6 465 ep_information_ptr->endpoint_name_len = 0;
zdshelby 1:14a9b0f4b9d6 466 }
zdshelby 1:14a9b0f4b9d6 467
zdshelby 1:14a9b0f4b9d6 468 if(endpoint_info_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 469 {
zdshelby 1:14a9b0f4b9d6 470
zdshelby 1:14a9b0f4b9d6 471 if(!ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 472 {
zdshelby 1:14a9b0f4b9d6 473 ep_information_ptr->domain_name_ptr = sn_nsdl_alloc(endpoint_info_ptr->domain_name_len);
zdshelby 1:14a9b0f4b9d6 474 }
zdshelby 1:14a9b0f4b9d6 475 if(!ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 476 {
zdshelby 1:14a9b0f4b9d6 477 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 478 }
zdshelby 1:14a9b0f4b9d6 479
zdshelby 1:14a9b0f4b9d6 480 memcpy(ep_information_ptr->domain_name_ptr, endpoint_info_ptr->domain_name_ptr, endpoint_info_ptr->domain_name_len);
zdshelby 1:14a9b0f4b9d6 481 ep_information_ptr->domain_name_len = endpoint_info_ptr->domain_name_len;
zdshelby 1:14a9b0f4b9d6 482
zdshelby 1:14a9b0f4b9d6 483 }
zdshelby 1:14a9b0f4b9d6 484
zdshelby 1:14a9b0f4b9d6 485 if(endpoint_info_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 486 {
zdshelby 1:14a9b0f4b9d6 487
zdshelby 1:14a9b0f4b9d6 488 if(!ep_information_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 489 {
zdshelby 1:14a9b0f4b9d6 490 ep_information_ptr->endpoint_name_ptr = sn_nsdl_alloc(endpoint_info_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 491 }
zdshelby 1:14a9b0f4b9d6 492 if(!ep_information_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 493 {
zdshelby 1:14a9b0f4b9d6 494 if(ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 495 {
zdshelby 1:14a9b0f4b9d6 496 sn_nsdl_free(ep_information_ptr->domain_name_ptr);
zdshelby 1:14a9b0f4b9d6 497 ep_information_ptr->domain_name_ptr = 0;
zdshelby 1:14a9b0f4b9d6 498 ep_information_ptr->domain_name_len = 0;
zdshelby 1:14a9b0f4b9d6 499 }
zdshelby 1:14a9b0f4b9d6 500 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 501 }
zdshelby 1:14a9b0f4b9d6 502
zdshelby 1:14a9b0f4b9d6 503 memcpy(ep_information_ptr->endpoint_name_ptr, endpoint_info_ptr->endpoint_name_ptr, endpoint_info_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 504 ep_information_ptr->endpoint_name_len = endpoint_info_ptr->endpoint_name_len;
zdshelby 1:14a9b0f4b9d6 505
zdshelby 1:14a9b0f4b9d6 506 }
zdshelby 1:14a9b0f4b9d6 507 }
zdshelby 1:14a9b0f4b9d6 508
zdshelby 1:14a9b0f4b9d6 509 return status;
zdshelby 1:14a9b0f4b9d6 510 }
zdshelby 1:14a9b0f4b9d6 511
zdshelby 1:14a9b0f4b9d6 512 int8_t sn_nsdl_unregister_endpoint(void)
zdshelby 1:14a9b0f4b9d6 513 {
zdshelby 1:14a9b0f4b9d6 514 /* Local variables */
zdshelby 1:14a9b0f4b9d6 515 sn_coap_hdr_s *unregister_message_ptr;
zdshelby 1:14a9b0f4b9d6 516 uint8_t *temp_ptr = 0;
zdshelby 1:14a9b0f4b9d6 517
zdshelby 1:14a9b0f4b9d6 518 /* Check that EP have been registered */
zdshelby 1:14a9b0f4b9d6 519 if(sn_nsdl_is_ep_registered())
zdshelby 1:14a9b0f4b9d6 520 {
zdshelby 1:14a9b0f4b9d6 521
zdshelby 1:14a9b0f4b9d6 522 /* Memory allocation for unregister message */
zdshelby 1:14a9b0f4b9d6 523 unregister_message_ptr = sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 524 if(!unregister_message_ptr)
zdshelby 1:14a9b0f4b9d6 525 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 526
zdshelby 1:14a9b0f4b9d6 527 memset(unregister_message_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 528
zdshelby 1:14a9b0f4b9d6 529 /* Fill unregister message */
zdshelby 1:14a9b0f4b9d6 530 unregister_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
zdshelby 1:14a9b0f4b9d6 531 unregister_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_DELETE;
zdshelby 1:14a9b0f4b9d6 532
zdshelby 1:14a9b0f4b9d6 533 unregister_message_ptr->uri_path_len = (RESOURCE_DIR_LEN + 1 + ep_information_ptr->domain_name_len + 1 + ep_information_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 534 unregister_message_ptr->uri_path_ptr = sn_nsdl_alloc(unregister_message_ptr->uri_path_len);
zdshelby 1:14a9b0f4b9d6 535 if (!unregister_message_ptr->uri_path_ptr)
zdshelby 1:14a9b0f4b9d6 536 {
zdshelby 1:14a9b0f4b9d6 537 sn_coap_parser_release_allocated_coap_msg_mem(unregister_message_ptr);
zdshelby 1:14a9b0f4b9d6 538 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 539 }
zdshelby 1:14a9b0f4b9d6 540
zdshelby 1:14a9b0f4b9d6 541 temp_ptr = unregister_message_ptr->uri_path_ptr;
zdshelby 1:14a9b0f4b9d6 542
zdshelby 1:14a9b0f4b9d6 543 memcpy(temp_ptr,resource_path_ptr, RESOURCE_DIR_LEN);
zdshelby 1:14a9b0f4b9d6 544 temp_ptr += RESOURCE_DIR_LEN;
zdshelby 1:14a9b0f4b9d6 545
zdshelby 1:14a9b0f4b9d6 546 *temp_ptr++ = '/';
zdshelby 1:14a9b0f4b9d6 547
zdshelby 1:14a9b0f4b9d6 548 memcpy(temp_ptr ,ep_information_ptr->domain_name_ptr, ep_information_ptr->domain_name_len);
zdshelby 1:14a9b0f4b9d6 549 temp_ptr += ep_information_ptr->domain_name_len;
zdshelby 1:14a9b0f4b9d6 550
zdshelby 1:14a9b0f4b9d6 551 *temp_ptr++ = '/';
zdshelby 1:14a9b0f4b9d6 552
zdshelby 1:14a9b0f4b9d6 553 memcpy(temp_ptr ,ep_information_ptr->endpoint_name_ptr, ep_information_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 554
zdshelby 1:14a9b0f4b9d6 555 /* Send message */
zdshelby 1:14a9b0f4b9d6 556 sn_nsdl_internal_coap_send(unregister_message_ptr, nsp_address_ptr, SN_NSDL_MSG_UNREGISTER);
zdshelby 1:14a9b0f4b9d6 557
zdshelby 1:14a9b0f4b9d6 558 /* Free memory */
zdshelby 1:14a9b0f4b9d6 559 sn_coap_parser_release_allocated_coap_msg_mem(unregister_message_ptr);
zdshelby 1:14a9b0f4b9d6 560
zdshelby 1:14a9b0f4b9d6 561 }
zdshelby 1:14a9b0f4b9d6 562
zdshelby 1:14a9b0f4b9d6 563 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 564 }
zdshelby 1:14a9b0f4b9d6 565
zdshelby 1:14a9b0f4b9d6 566 int8_t sn_nsdl_update_registration (sn_nsdl_ep_parameters_s *endpoint_info_ptr)
zdshelby 1:14a9b0f4b9d6 567 {
zdshelby 1:14a9b0f4b9d6 568 /* Local variables */
zdshelby 1:14a9b0f4b9d6 569 sn_coap_hdr_s *register_message_ptr;
zdshelby 1:14a9b0f4b9d6 570 uint8_t *temp_ptr;
zdshelby 1:14a9b0f4b9d6 571
zdshelby 1:14a9b0f4b9d6 572 /*** Build endpoint register update message ***/
zdshelby 1:14a9b0f4b9d6 573
zdshelby 1:14a9b0f4b9d6 574 /* Allocate memory for header struct */
zdshelby 1:14a9b0f4b9d6 575 register_message_ptr = sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 576 if(register_message_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 577 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 578
zdshelby 1:14a9b0f4b9d6 579 memset(register_message_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 580
zdshelby 1:14a9b0f4b9d6 581 /* Fill message fields -> confirmable post to specified NSP path */
zdshelby 1:14a9b0f4b9d6 582 register_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
zdshelby 1:14a9b0f4b9d6 583 register_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_PUT;
zdshelby 1:14a9b0f4b9d6 584
zdshelby 1:14a9b0f4b9d6 585 register_message_ptr->uri_path_len = sizeof(resource_path_ptr) + ep_information_ptr->domain_name_len + ep_information_ptr->endpoint_name_len + 2; // = rd/domain/endpoint
zdshelby 1:14a9b0f4b9d6 586
zdshelby 1:14a9b0f4b9d6 587 register_message_ptr->uri_path_ptr = sn_nsdl_alloc(register_message_ptr->uri_path_len);
zdshelby 1:14a9b0f4b9d6 588 if(!register_message_ptr->uri_path_ptr)
zdshelby 1:14a9b0f4b9d6 589 {
zdshelby 1:14a9b0f4b9d6 590 sn_coap_parser_release_allocated_coap_msg_mem(register_message_ptr);
zdshelby 1:14a9b0f4b9d6 591 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 592 }
zdshelby 1:14a9b0f4b9d6 593
zdshelby 1:14a9b0f4b9d6 594 temp_ptr = register_message_ptr->uri_path_ptr;
zdshelby 1:14a9b0f4b9d6 595
zdshelby 1:14a9b0f4b9d6 596 /* rd/ */
zdshelby 1:14a9b0f4b9d6 597 memcpy(temp_ptr, resource_path_ptr, sizeof(resource_path_ptr));
zdshelby 1:14a9b0f4b9d6 598 temp_ptr += sizeof(resource_path_ptr);
zdshelby 1:14a9b0f4b9d6 599 *temp_ptr++ = '/';
zdshelby 1:14a9b0f4b9d6 600
zdshelby 1:14a9b0f4b9d6 601 /* rd/DOMAIN/ */
zdshelby 1:14a9b0f4b9d6 602 memcpy(temp_ptr, ep_information_ptr->domain_name_ptr, ep_information_ptr->domain_name_len);
zdshelby 1:14a9b0f4b9d6 603 temp_ptr += ep_information_ptr->domain_name_len;
zdshelby 1:14a9b0f4b9d6 604 *temp_ptr++ = '/';
zdshelby 1:14a9b0f4b9d6 605
zdshelby 1:14a9b0f4b9d6 606 /* rd/domain/ENDPOINT */
zdshelby 1:14a9b0f4b9d6 607 memcpy(temp_ptr, ep_information_ptr->endpoint_name_ptr, ep_information_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 608
zdshelby 1:14a9b0f4b9d6 609
zdshelby 1:14a9b0f4b9d6 610 /* Allocate memory for the extended options list */
zdshelby 1:14a9b0f4b9d6 611 register_message_ptr->options_list_ptr = sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 612 if(register_message_ptr->options_list_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 613 {
zdshelby 1:14a9b0f4b9d6 614 sn_coap_parser_release_allocated_coap_msg_mem(register_message_ptr);
zdshelby 1:14a9b0f4b9d6 615 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 616 }
zdshelby 1:14a9b0f4b9d6 617
zdshelby 1:14a9b0f4b9d6 618 memset(register_message_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 619
zdshelby 1:14a9b0f4b9d6 620 /* Fill Uri-query options */
zdshelby 1:14a9b0f4b9d6 621 sn_nsdl_fill_uri_query_options(endpoint_info_ptr, register_message_ptr, SN_NSDL_EP_UPDATE_MESSAGE);
zdshelby 1:14a9b0f4b9d6 622
zdshelby 1:14a9b0f4b9d6 623 /* Build and send coap message to NSP */
zdshelby 1:14a9b0f4b9d6 624 sn_nsdl_internal_coap_send(register_message_ptr, nsp_address_ptr, SN_NSDL_MSG_UPDATE);
zdshelby 1:14a9b0f4b9d6 625
zdshelby 1:14a9b0f4b9d6 626 if(register_message_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 627 sn_nsdl_free(register_message_ptr->payload_ptr);
zdshelby 1:14a9b0f4b9d6 628 sn_coap_parser_release_allocated_coap_msg_mem(register_message_ptr);
zdshelby 1:14a9b0f4b9d6 629
zdshelby 1:14a9b0f4b9d6 630 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 631 }
zdshelby 1:14a9b0f4b9d6 632
zdshelby 1:14a9b0f4b9d6 633 int8_t sn_nsdl_send_eventing_message (uint8_t *event_name_ptr, uint16_t event_name_len, uint8_t *message_body_ptr, uint16_t message_body_len)
zdshelby 1:14a9b0f4b9d6 634 {
zdshelby 1:14a9b0f4b9d6 635 sn_coap_hdr_s *eventing_message_ptr;
zdshelby 1:14a9b0f4b9d6 636 int8_t status = 0;
zdshelby 1:14a9b0f4b9d6 637
zdshelby 1:14a9b0f4b9d6 638 /* Allocate and initialize memory for header struct */
zdshelby 1:14a9b0f4b9d6 639 eventing_message_ptr = sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 640 if(eventing_message_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 641 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 642
zdshelby 1:14a9b0f4b9d6 643 memset(eventing_message_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 644
zdshelby 1:14a9b0f4b9d6 645 /* Fill header */
zdshelby 1:14a9b0f4b9d6 646 eventing_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
zdshelby 1:14a9b0f4b9d6 647 eventing_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST;
zdshelby 1:14a9b0f4b9d6 648
zdshelby 1:14a9b0f4b9d6 649 /* Fill uri path option */
zdshelby 1:14a9b0f4b9d6 650 eventing_message_ptr->uri_path_len = sizeof(event_path_parameter) + event_name_len;
zdshelby 1:14a9b0f4b9d6 651 eventing_message_ptr->uri_path_ptr = sn_nsdl_alloc(eventing_message_ptr->uri_path_len);
zdshelby 1:14a9b0f4b9d6 652
zdshelby 1:14a9b0f4b9d6 653 if(!eventing_message_ptr->uri_path_ptr)
zdshelby 1:14a9b0f4b9d6 654 {
zdshelby 1:14a9b0f4b9d6 655 sn_coap_parser_release_allocated_coap_msg_mem(eventing_message_ptr);
zdshelby 1:14a9b0f4b9d6 656 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 657 }
zdshelby 1:14a9b0f4b9d6 658
zdshelby 1:14a9b0f4b9d6 659 memcpy(eventing_message_ptr->uri_path_ptr, event_path_parameter, sizeof(event_path_parameter));
zdshelby 1:14a9b0f4b9d6 660 memcpy(eventing_message_ptr->uri_path_ptr + sizeof(event_path_parameter), event_name_ptr, event_name_len);
zdshelby 1:14a9b0f4b9d6 661
zdshelby 1:14a9b0f4b9d6 662 /* Fill payload */
zdshelby 1:14a9b0f4b9d6 663 eventing_message_ptr->payload_len = message_body_len;
zdshelby 1:14a9b0f4b9d6 664 eventing_message_ptr->payload_ptr = message_body_ptr;
zdshelby 1:14a9b0f4b9d6 665
zdshelby 1:14a9b0f4b9d6 666 /* Send coap message */
zdshelby 1:14a9b0f4b9d6 667 status = sn_nsdl_internal_coap_send(eventing_message_ptr, nsp_address_ptr, SN_NSDL_MSG_EVENT);
zdshelby 1:14a9b0f4b9d6 668
zdshelby 1:14a9b0f4b9d6 669 eventing_message_ptr->payload_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 670
zdshelby 1:14a9b0f4b9d6 671 sn_coap_parser_release_allocated_coap_msg_mem(eventing_message_ptr);
zdshelby 1:14a9b0f4b9d6 672
zdshelby 1:14a9b0f4b9d6 673 return status;
zdshelby 1:14a9b0f4b9d6 674 }
zdshelby 1:14a9b0f4b9d6 675
zdshelby 1:14a9b0f4b9d6 676 void sn_nsdl_nsp_lost(void)
zdshelby 1:14a9b0f4b9d6 677 {
zdshelby 1:14a9b0f4b9d6 678 sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_NOT_REGISTERED;
zdshelby 1:14a9b0f4b9d6 679 return;
zdshelby 1:14a9b0f4b9d6 680 }
zdshelby 1:14a9b0f4b9d6 681
zdshelby 1:14a9b0f4b9d6 682 int8_t sn_nsdl_is_ep_registered(void)
zdshelby 1:14a9b0f4b9d6 683 {
zdshelby 1:14a9b0f4b9d6 684
zdshelby 1:14a9b0f4b9d6 685 return sn_nsdl_endpoint_registered;
zdshelby 1:14a9b0f4b9d6 686 }
zdshelby 1:14a9b0f4b9d6 687
zdshelby 1:14a9b0f4b9d6 688 uint16_t sn_nsdl_send_observation_notification(uint8_t *token_ptr, uint8_t token_len,
zdshelby 1:14a9b0f4b9d6 689 uint8_t *payload_ptr, uint16_t payload_len,
zdshelby 1:14a9b0f4b9d6 690 uint8_t *observe_ptr, uint8_t observe_len,
zdshelby 1:14a9b0f4b9d6 691 sn_coap_msg_type_e message_type, uint8_t content_type)
zdshelby 1:14a9b0f4b9d6 692 {
zdshelby 1:14a9b0f4b9d6 693 sn_coap_hdr_s *notification_message_ptr;
zdshelby 1:14a9b0f4b9d6 694 uint16_t return_msg_id = 0;
zdshelby 1:14a9b0f4b9d6 695
zdshelby 1:14a9b0f4b9d6 696 /* Allocate and initialize memory for header struct */
zdshelby 1:14a9b0f4b9d6 697 notification_message_ptr = sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 698 if(notification_message_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 699 return 0;
zdshelby 1:14a9b0f4b9d6 700
zdshelby 1:14a9b0f4b9d6 701 memset(notification_message_ptr, 0, sizeof(sn_coap_hdr_s));
zdshelby 1:14a9b0f4b9d6 702
zdshelby 1:14a9b0f4b9d6 703 notification_message_ptr->options_list_ptr = sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 704 if(notification_message_ptr->options_list_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 705 {
zdshelby 1:14a9b0f4b9d6 706 sn_nsdl_free(notification_message_ptr);
zdshelby 1:14a9b0f4b9d6 707 return 0;
zdshelby 1:14a9b0f4b9d6 708 }
zdshelby 1:14a9b0f4b9d6 709
zdshelby 1:14a9b0f4b9d6 710 memset(notification_message_ptr->options_list_ptr , 0, sizeof(sn_coap_options_list_s));
zdshelby 1:14a9b0f4b9d6 711
zdshelby 1:14a9b0f4b9d6 712 /* Fill header */
zdshelby 1:14a9b0f4b9d6 713 notification_message_ptr->msg_type = message_type;
zdshelby 1:14a9b0f4b9d6 714 notification_message_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
zdshelby 1:14a9b0f4b9d6 715
zdshelby 1:14a9b0f4b9d6 716 /* Fill token */
zdshelby 1:14a9b0f4b9d6 717 notification_message_ptr->token_len = token_len;
zdshelby 1:14a9b0f4b9d6 718 notification_message_ptr->token_ptr = token_ptr;
zdshelby 1:14a9b0f4b9d6 719
zdshelby 1:14a9b0f4b9d6 720 /* Fill payload */
zdshelby 1:14a9b0f4b9d6 721 notification_message_ptr->payload_len = payload_len;
zdshelby 1:14a9b0f4b9d6 722 notification_message_ptr->payload_ptr = payload_ptr;
zdshelby 1:14a9b0f4b9d6 723
zdshelby 1:14a9b0f4b9d6 724 /* Fill observe */
zdshelby 1:14a9b0f4b9d6 725 notification_message_ptr->options_list_ptr->observe_len = observe_len;
zdshelby 1:14a9b0f4b9d6 726 notification_message_ptr->options_list_ptr->observe_ptr = observe_ptr;
zdshelby 1:14a9b0f4b9d6 727
zdshelby 1:14a9b0f4b9d6 728 /* Fill content type */
zdshelby 1:14a9b0f4b9d6 729 if(content_type)
zdshelby 1:14a9b0f4b9d6 730 {
zdshelby 1:14a9b0f4b9d6 731 notification_message_ptr->content_type_len = 1;
zdshelby 1:14a9b0f4b9d6 732 notification_message_ptr->content_type_ptr = &content_type;
zdshelby 1:14a9b0f4b9d6 733 }
zdshelby 1:14a9b0f4b9d6 734
zdshelby 1:14a9b0f4b9d6 735 /* Send message */
zdshelby 1:14a9b0f4b9d6 736 if(sn_grs_send_coap_message(nsp_address_ptr,notification_message_ptr) == SN_NSDL_FAILURE)
zdshelby 1:14a9b0f4b9d6 737 return_msg_id = 0;
zdshelby 1:14a9b0f4b9d6 738 else
zdshelby 1:14a9b0f4b9d6 739 return_msg_id = notification_message_ptr->msg_id;
zdshelby 1:14a9b0f4b9d6 740
zdshelby 1:14a9b0f4b9d6 741 /* Free memory */
zdshelby 1:14a9b0f4b9d6 742
zdshelby 1:14a9b0f4b9d6 743 notification_message_ptr->payload_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 744 notification_message_ptr->options_list_ptr->observe_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 745 notification_message_ptr->token_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 746 notification_message_ptr->content_type_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 747
zdshelby 1:14a9b0f4b9d6 748 sn_coap_parser_release_allocated_coap_msg_mem(notification_message_ptr);
zdshelby 1:14a9b0f4b9d6 749
zdshelby 1:14a9b0f4b9d6 750 return return_msg_id;
zdshelby 1:14a9b0f4b9d6 751 }
zdshelby 1:14a9b0f4b9d6 752
zdshelby 1:14a9b0f4b9d6 753
zdshelby 1:14a9b0f4b9d6 754 /* * * * * * * * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 755 /* GRS Wrapper */
zdshelby 1:14a9b0f4b9d6 756 /* These are documented in sn_grs.c - file */
zdshelby 1:14a9b0f4b9d6 757 /* * * * * * * * * * * * * * * * * * * * * */
zdshelby 1:14a9b0f4b9d6 758
zdshelby 1:14a9b0f4b9d6 759 int16_t sn_nsdl_get_capability(void)
zdshelby 1:14a9b0f4b9d6 760 {
zdshelby 1:14a9b0f4b9d6 761 return sn_grs_get_capability();
zdshelby 1:14a9b0f4b9d6 762 }
zdshelby 1:14a9b0f4b9d6 763
zdshelby 1:14a9b0f4b9d6 764
zdshelby 1:14a9b0f4b9d6 765 uint32_t sn_nsdl_get_version(void)
zdshelby 1:14a9b0f4b9d6 766 {
zdshelby 1:14a9b0f4b9d6 767 return sn_grs_get_version();
zdshelby 1:14a9b0f4b9d6 768 }
zdshelby 1:14a9b0f4b9d6 769
zdshelby 1:14a9b0f4b9d6 770
zdshelby 1:14a9b0f4b9d6 771 int8_t sn_nsdl_process_http(uint8_t *packet_ptr, uint16_t *packet_len_ptr, sn_nsdl_addr_s *src_ptr)
zdshelby 1:14a9b0f4b9d6 772 {
zdshelby 1:14a9b0f4b9d6 773 return sn_grs_process_http(packet_ptr, packet_len_ptr, src_ptr);
zdshelby 1:14a9b0f4b9d6 774 }
zdshelby 1:14a9b0f4b9d6 775
zdshelby 1:14a9b0f4b9d6 776
zdshelby 1:14a9b0f4b9d6 777 int8_t sn_nsdl_process_coap(uint8_t *packet_ptr, uint16_t packet_len_ptr, sn_nsdl_addr_s *src_ptr)
zdshelby 1:14a9b0f4b9d6 778 {
zdshelby 1:14a9b0f4b9d6 779 return sn_grs_process_coap(packet_ptr, packet_len_ptr, src_ptr);
zdshelby 1:14a9b0f4b9d6 780 }
zdshelby 1:14a9b0f4b9d6 781
zdshelby 1:14a9b0f4b9d6 782 int8_t sn_nsdl_exec(uint32_t time)
zdshelby 1:14a9b0f4b9d6 783 {
zdshelby 1:14a9b0f4b9d6 784 return sn_grs_exec(time);
zdshelby 1:14a9b0f4b9d6 785 }
zdshelby 1:14a9b0f4b9d6 786
zdshelby 1:14a9b0f4b9d6 787 int8_t sn_nsdl_create_resource(sn_nsdl_resource_info_s *res_ptr)
zdshelby 1:14a9b0f4b9d6 788 {
zdshelby 1:14a9b0f4b9d6 789 return sn_grs_create_resource(res_ptr);
zdshelby 1:14a9b0f4b9d6 790 }
zdshelby 1:14a9b0f4b9d6 791
zdshelby 1:14a9b0f4b9d6 792 int8_t sn_nsdl_update_resource(sn_nsdl_resource_info_s *res_ptr)
zdshelby 1:14a9b0f4b9d6 793 {
zdshelby 1:14a9b0f4b9d6 794 return sn_grs_update_resource(res_ptr);
zdshelby 1:14a9b0f4b9d6 795 }
zdshelby 1:14a9b0f4b9d6 796
zdshelby 1:14a9b0f4b9d6 797 int8_t sn_nsdl_delete_resource(uint8_t pathlen, uint8_t *path_ptr)
zdshelby 1:14a9b0f4b9d6 798 {
zdshelby 1:14a9b0f4b9d6 799 return sn_grs_delete_resource(pathlen, path_ptr);
zdshelby 1:14a9b0f4b9d6 800 }
zdshelby 1:14a9b0f4b9d6 801
zdshelby 1:14a9b0f4b9d6 802 sn_nsdl_resource_info_s *sn_nsdl_get_resource(uint16_t pathlen, uint8_t *path_ptr)
zdshelby 1:14a9b0f4b9d6 803 {
zdshelby 1:14a9b0f4b9d6 804 return sn_grs_get_resource(pathlen, path_ptr);
zdshelby 1:14a9b0f4b9d6 805 }
zdshelby 1:14a9b0f4b9d6 806
zdshelby 1:14a9b0f4b9d6 807 sn_grs_resource_list_s *sn_nsdl_list_resource(uint16_t pathlen, uint8_t *path_ptr)
zdshelby 1:14a9b0f4b9d6 808 {
zdshelby 1:14a9b0f4b9d6 809 return sn_grs_list_resource(pathlen, path_ptr);
zdshelby 1:14a9b0f4b9d6 810 }
zdshelby 1:14a9b0f4b9d6 811
zdshelby 1:14a9b0f4b9d6 812 int8_t sn_nsdl_send_coap_message(sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr)
zdshelby 1:14a9b0f4b9d6 813 {
zdshelby 1:14a9b0f4b9d6 814 return sn_grs_send_coap_message(address_ptr, coap_hdr_ptr);
zdshelby 1:14a9b0f4b9d6 815 }
zdshelby 1:14a9b0f4b9d6 816
zdshelby 1:14a9b0f4b9d6 817 /********************/
zdshelby 1:14a9b0f4b9d6 818 /* Static functions */
zdshelby 1:14a9b0f4b9d6 819 /********************/
zdshelby 1:14a9b0f4b9d6 820
zdshelby 1:14a9b0f4b9d6 821
zdshelby 1:14a9b0f4b9d6 822 /**
zdshelby 1:14a9b0f4b9d6 823 * \fn static int8_t sn_nsdl_send_coap_message(sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description)
zdshelby 1:14a9b0f4b9d6 824 *
zdshelby 1:14a9b0f4b9d6 825 *
zdshelby 1:14a9b0f4b9d6 826 * \brief To send NSDL messages. Stores message id?s and message description to catch response from NSP server
zdshelby 1:14a9b0f4b9d6 827 *
zdshelby 1:14a9b0f4b9d6 828 * \param *coap_header_ptr Pointer to the CoAP message header to be sent
zdshelby 1:14a9b0f4b9d6 829 * \param *dst_addr_ptr Pointer to the address structure that contains destination address information
zdshelby 1:14a9b0f4b9d6 830 * \param message_description Message description to be stored to list for waiting response
zdshelby 1:14a9b0f4b9d6 831 *
zdshelby 1:14a9b0f4b9d6 832 * \return SN_NSDL_SUCCESS = 0, Failed = -1
zdshelby 1:14a9b0f4b9d6 833 */
zdshelby 1:14a9b0f4b9d6 834 static int8_t sn_nsdl_internal_coap_send(sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description)
zdshelby 1:14a9b0f4b9d6 835 {
zdshelby 1:14a9b0f4b9d6 836 uint8_t *coap_message_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 837 uint16_t coap_message_len = 0;
zdshelby 1:14a9b0f4b9d6 838 int16_t status = 0;
zdshelby 1:14a9b0f4b9d6 839
zdshelby 1:14a9b0f4b9d6 840 coap_message_len = sn_coap_builder_calc_needed_packet_data_size(coap_header_ptr);
zdshelby 1:14a9b0f4b9d6 841
zdshelby 1:14a9b0f4b9d6 842 if(coap_message_len == 0)
zdshelby 1:14a9b0f4b9d6 843 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 844
zdshelby 1:14a9b0f4b9d6 845 coap_message_ptr = sn_nsdl_alloc(coap_message_len);
zdshelby 1:14a9b0f4b9d6 846 if(!coap_message_ptr)
zdshelby 1:14a9b0f4b9d6 847 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 848
zdshelby 1:14a9b0f4b9d6 849 /* Build message */
zdshelby 1:14a9b0f4b9d6 850 status = sn_coap_protocol_build(dst_addr_ptr,coap_message_ptr, coap_header_ptr);
zdshelby 1:14a9b0f4b9d6 851
zdshelby 1:14a9b0f4b9d6 852 /* If message building failed */
zdshelby 1:14a9b0f4b9d6 853 if(status < 0)
zdshelby 1:14a9b0f4b9d6 854 {
zdshelby 1:14a9b0f4b9d6 855 sn_nsdl_free(coap_message_ptr);
zdshelby 1:14a9b0f4b9d6 856 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 857 }
zdshelby 1:14a9b0f4b9d6 858
zdshelby 1:14a9b0f4b9d6 859 /* If mesage type is confirmable, save it to list to wait for reply */
zdshelby 1:14a9b0f4b9d6 860 if(coap_header_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE)
zdshelby 1:14a9b0f4b9d6 861 {
zdshelby 1:14a9b0f4b9d6 862 sn_nsdl_sent_messages_s *message_ptr = sn_nsdl_alloc(sizeof(sn_nsdl_sent_messages_s));
zdshelby 1:14a9b0f4b9d6 863 if(message_ptr)
zdshelby 1:14a9b0f4b9d6 864 {
zdshelby 1:14a9b0f4b9d6 865 if(sn_linked_list_count_nodes(message_list_ptr) >= SN_NSDL_MAX_MESSAGE_COUNT)
zdshelby 1:14a9b0f4b9d6 866 {
zdshelby 1:14a9b0f4b9d6 867 sn_nsdl_sent_messages_s *message_temp_ptr = sn_linked_list_get_last_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 868 if(message_temp_ptr)
zdshelby 1:14a9b0f4b9d6 869 sn_nsdl_free(message_temp_ptr);
zdshelby 1:14a9b0f4b9d6 870 sn_linked_list_remove_current_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 871 }
zdshelby 1:14a9b0f4b9d6 872
zdshelby 1:14a9b0f4b9d6 873 message_ptr->message_type = message_description;
zdshelby 1:14a9b0f4b9d6 874 message_ptr->msg_id_number = coap_header_ptr->msg_id;
zdshelby 1:14a9b0f4b9d6 875 sn_linked_list_add_node(message_list_ptr, (void*)message_ptr);
zdshelby 1:14a9b0f4b9d6 876
zdshelby 1:14a9b0f4b9d6 877 status = SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 878 }
zdshelby 1:14a9b0f4b9d6 879 else
zdshelby 1:14a9b0f4b9d6 880 {
zdshelby 1:14a9b0f4b9d6 881 status = SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 882 }
zdshelby 1:14a9b0f4b9d6 883
zdshelby 1:14a9b0f4b9d6 884 }
zdshelby 1:14a9b0f4b9d6 885
zdshelby 1:14a9b0f4b9d6 886 sn_nsdl_tx_callback(SN_NSDL_PROTOCOL_COAP, coap_message_ptr, coap_message_len, dst_addr_ptr);
zdshelby 1:14a9b0f4b9d6 887 sn_nsdl_free(coap_message_ptr);
zdshelby 1:14a9b0f4b9d6 888
zdshelby 1:14a9b0f4b9d6 889 return status;
zdshelby 1:14a9b0f4b9d6 890 }
zdshelby 1:14a9b0f4b9d6 891
zdshelby 1:14a9b0f4b9d6 892 /**
zdshelby 1:14a9b0f4b9d6 893 * \fn static void sn_nsdl_resolve_nsp_address(void)
zdshelby 1:14a9b0f4b9d6 894 *
zdshelby 1:14a9b0f4b9d6 895 * \brief Resolves NSP server address.
zdshelby 1:14a9b0f4b9d6 896 *
zdshelby 1:14a9b0f4b9d6 897 * \note Application must set NSP address with set_nsp_address
zdshelby 1:14a9b0f4b9d6 898 */
zdshelby 1:14a9b0f4b9d6 899 static void sn_nsdl_resolve_nsp_address(void)
zdshelby 1:14a9b0f4b9d6 900 {
zdshelby 1:14a9b0f4b9d6 901 /* Local variables */
zdshelby 1:14a9b0f4b9d6 902 if(!nsp_address_ptr)
zdshelby 1:14a9b0f4b9d6 903 {
zdshelby 1:14a9b0f4b9d6 904 //allocate only if previously not allocated
zdshelby 1:14a9b0f4b9d6 905 nsp_address_ptr = sn_nsdl_alloc(sizeof(sn_nsdl_addr_s));
zdshelby 1:14a9b0f4b9d6 906 }
zdshelby 1:14a9b0f4b9d6 907
zdshelby 1:14a9b0f4b9d6 908 if(nsp_address_ptr)
zdshelby 1:14a9b0f4b9d6 909 {
zdshelby 1:14a9b0f4b9d6 910 memset(nsp_address_ptr, 0, sizeof(sn_nsdl_addr_s));
zdshelby 1:14a9b0f4b9d6 911 /* This is only for version 0.5 */
zdshelby 1:14a9b0f4b9d6 912 nsp_address_ptr->type = SN_NSDL_ADDRESS_TYPE_NONE;
zdshelby 1:14a9b0f4b9d6 913 }
zdshelby 1:14a9b0f4b9d6 914
zdshelby 1:14a9b0f4b9d6 915 /* Todo: get NSP address */
zdshelby 1:14a9b0f4b9d6 916 }
zdshelby 1:14a9b0f4b9d6 917
zdshelby 1:14a9b0f4b9d6 918 /**
zdshelby 1:14a9b0f4b9d6 919 * \fn static int8_t sn_nsdl_build_registration_body(sn_coap_hdr_s *message_ptr)
zdshelby 1:14a9b0f4b9d6 920 *
zdshelby 1:14a9b0f4b9d6 921 * \brief To build GRS resources to registration message payload
zdshelby 1:14a9b0f4b9d6 922 *
zdshelby 1:14a9b0f4b9d6 923 * \param *message_ptr Pointer to CoAP message header
zdshelby 1:14a9b0f4b9d6 924 *
zdshelby 1:14a9b0f4b9d6 925 * \return SN_NSDL_SUCCESS = 0, Failed = -1
zdshelby 1:14a9b0f4b9d6 926 */
zdshelby 1:14a9b0f4b9d6 927 int8_t sn_nsdl_build_registration_body(sn_coap_hdr_s *message_ptr, uint8_t updating_registeration)
zdshelby 1:14a9b0f4b9d6 928 {
zdshelby 1:14a9b0f4b9d6 929 /* Local variables */
zdshelby 1:14a9b0f4b9d6 930 uint8_t *temp_ptr;
zdshelby 1:14a9b0f4b9d6 931 sn_nsdl_resource_info_s *resource_temp_ptr;
zdshelby 1:14a9b0f4b9d6 932
zdshelby 1:14a9b0f4b9d6 933
zdshelby 1:14a9b0f4b9d6 934 /* Get list of resources */
zdshelby 1:14a9b0f4b9d6 935
zdshelby 1:14a9b0f4b9d6 936
zdshelby 1:14a9b0f4b9d6 937 /* Calculate needed memory and allocate */
zdshelby 1:14a9b0f4b9d6 938 message_ptr->payload_len = sn_nsdl_calculate_registration_body_size(updating_registeration);
zdshelby 1:14a9b0f4b9d6 939
zdshelby 1:14a9b0f4b9d6 940 /* If no resources to be registered, return SN_NSDL_SUCCESS */
zdshelby 1:14a9b0f4b9d6 941 if(!message_ptr->payload_len)
zdshelby 1:14a9b0f4b9d6 942 {
zdshelby 1:14a9b0f4b9d6 943 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 944 }
zdshelby 1:14a9b0f4b9d6 945
zdshelby 1:14a9b0f4b9d6 946 message_ptr->payload_ptr = sn_nsdl_alloc(message_ptr->payload_len);
zdshelby 1:14a9b0f4b9d6 947 if(!message_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 948 {
zdshelby 1:14a9b0f4b9d6 949 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 950 }
zdshelby 1:14a9b0f4b9d6 951
zdshelby 1:14a9b0f4b9d6 952 /* Build message */
zdshelby 1:14a9b0f4b9d6 953 temp_ptr = message_ptr->payload_ptr;
zdshelby 1:14a9b0f4b9d6 954
zdshelby 1:14a9b0f4b9d6 955 resource_temp_ptr = sn_grs_get_first_resource();
zdshelby 1:14a9b0f4b9d6 956
zdshelby 1:14a9b0f4b9d6 957 /* Loop trough all resources */
zdshelby 1:14a9b0f4b9d6 958 while(resource_temp_ptr)
zdshelby 1:14a9b0f4b9d6 959 {
zdshelby 1:14a9b0f4b9d6 960
zdshelby 1:14a9b0f4b9d6 961 /* if resource needs to be registered */
zdshelby 1:14a9b0f4b9d6 962 if(resource_temp_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 963 {
zdshelby 1:14a9b0f4b9d6 964
zdshelby 1:14a9b0f4b9d6 965 if(updating_registeration && resource_temp_ptr->resource_parameters_ptr->registered == SN_NDSL_RESOURCE_REGISTERED)
zdshelby 1:14a9b0f4b9d6 966 {
zdshelby 1:14a9b0f4b9d6 967 resource_temp_ptr = sn_grs_get_next_resource();
zdshelby 1:14a9b0f4b9d6 968 continue;
zdshelby 1:14a9b0f4b9d6 969 }
zdshelby 1:14a9b0f4b9d6 970 else
zdshelby 1:14a9b0f4b9d6 971 {
zdshelby 1:14a9b0f4b9d6 972 resource_temp_ptr->resource_parameters_ptr->registered = SN_NDSL_RESOURCE_REGISTERING;
zdshelby 1:14a9b0f4b9d6 973 }
zdshelby 1:14a9b0f4b9d6 974
zdshelby 1:14a9b0f4b9d6 975 /* If not first resource, add '.' to separator */
zdshelby 1:14a9b0f4b9d6 976 if(temp_ptr != message_ptr->payload_ptr)
zdshelby 1:14a9b0f4b9d6 977 *temp_ptr++ = ',';
zdshelby 1:14a9b0f4b9d6 978
zdshelby 1:14a9b0f4b9d6 979 *temp_ptr++ = '<';
zdshelby 1:14a9b0f4b9d6 980 *temp_ptr++ = '/';
zdshelby 1:14a9b0f4b9d6 981 memcpy(temp_ptr, resource_temp_ptr->path, resource_temp_ptr->pathlen);
zdshelby 1:14a9b0f4b9d6 982 temp_ptr += resource_temp_ptr->pathlen;
zdshelby 1:14a9b0f4b9d6 983 *temp_ptr++ = '>';
zdshelby 1:14a9b0f4b9d6 984
zdshelby 1:14a9b0f4b9d6 985 /* Resource attributes */
zdshelby 1:14a9b0f4b9d6 986 if(resource_temp_ptr->resource_parameters_ptr->resource_type_len)
zdshelby 1:14a9b0f4b9d6 987 {
zdshelby 1:14a9b0f4b9d6 988 *temp_ptr++ = ';';
zdshelby 1:14a9b0f4b9d6 989 memcpy(temp_ptr, resource_type_parameter, RT_PARAMETER_LEN);
zdshelby 1:14a9b0f4b9d6 990 temp_ptr += RT_PARAMETER_LEN;
zdshelby 1:14a9b0f4b9d6 991 *temp_ptr++ = '"';
zdshelby 1:14a9b0f4b9d6 992 memcpy(temp_ptr, resource_temp_ptr->resource_parameters_ptr->resource_type_ptr, resource_temp_ptr->resource_parameters_ptr->resource_type_len);
zdshelby 1:14a9b0f4b9d6 993 temp_ptr += resource_temp_ptr->resource_parameters_ptr->resource_type_len;
zdshelby 1:14a9b0f4b9d6 994 *temp_ptr++ = '"';
zdshelby 1:14a9b0f4b9d6 995 }
zdshelby 1:14a9b0f4b9d6 996
zdshelby 1:14a9b0f4b9d6 997 if(resource_temp_ptr->resource_parameters_ptr->interface_description_len)
zdshelby 1:14a9b0f4b9d6 998 {
zdshelby 1:14a9b0f4b9d6 999 *temp_ptr++ = ';';
zdshelby 1:14a9b0f4b9d6 1000 memcpy(temp_ptr, if_description_parameter, IF_PARAMETER_LEN);
zdshelby 1:14a9b0f4b9d6 1001 temp_ptr += IF_PARAMETER_LEN;
zdshelby 1:14a9b0f4b9d6 1002 *temp_ptr++ = '"';
zdshelby 1:14a9b0f4b9d6 1003 memcpy(temp_ptr, resource_temp_ptr->resource_parameters_ptr->interface_description_ptr, resource_temp_ptr->resource_parameters_ptr->interface_description_len);
zdshelby 1:14a9b0f4b9d6 1004 temp_ptr += resource_temp_ptr->resource_parameters_ptr->interface_description_len;
zdshelby 1:14a9b0f4b9d6 1005 *temp_ptr++ = '"';
zdshelby 1:14a9b0f4b9d6 1006 }
zdshelby 1:14a9b0f4b9d6 1007
zdshelby 1:14a9b0f4b9d6 1008 if(resource_temp_ptr->resource_parameters_ptr->coap_content_type != 0)
zdshelby 1:14a9b0f4b9d6 1009 {
zdshelby 1:14a9b0f4b9d6 1010 *temp_ptr++ = ';';
zdshelby 1:14a9b0f4b9d6 1011 memcpy(temp_ptr, coap_con_type_parameter, COAP_CON_PARAMETER_LEN);
zdshelby 1:14a9b0f4b9d6 1012 temp_ptr += COAP_CON_PARAMETER_LEN;
zdshelby 1:14a9b0f4b9d6 1013 *temp_ptr++ = '"';
zdshelby 1:14a9b0f4b9d6 1014 temp_ptr = sn_nsdl_itoa(temp_ptr, resource_temp_ptr->resource_parameters_ptr->coap_content_type);
zdshelby 1:14a9b0f4b9d6 1015 *temp_ptr++ = '"';
zdshelby 1:14a9b0f4b9d6 1016 }
zdshelby 1:14a9b0f4b9d6 1017
zdshelby 1:14a9b0f4b9d6 1018 /* ;obs */
zdshelby 1:14a9b0f4b9d6 1019 if(resource_temp_ptr->resource_parameters_ptr->observable)
zdshelby 1:14a9b0f4b9d6 1020 {
zdshelby 1:14a9b0f4b9d6 1021 *temp_ptr++ = ';';
zdshelby 1:14a9b0f4b9d6 1022 memcpy(temp_ptr, obs_parameter, OBS_PARAMETER_LEN);
zdshelby 1:14a9b0f4b9d6 1023 temp_ptr += OBS_PARAMETER_LEN;
zdshelby 1:14a9b0f4b9d6 1024 }
zdshelby 1:14a9b0f4b9d6 1025
zdshelby 1:14a9b0f4b9d6 1026 /* ;aobs;id= */
zdshelby 1:14a9b0f4b9d6 1027 /* todo: aosb not supported ATM - needs fixing */
zdshelby 1:14a9b0f4b9d6 1028 /*
zdshelby 1:14a9b0f4b9d6 1029 if((resource_temp_ptr->resource_parameters_ptr->auto_obs_len > 0 && resource_temp_ptr->resource_parameters_ptr->auto_obs_len <= 8) &&
zdshelby 1:14a9b0f4b9d6 1030 resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr)
zdshelby 1:14a9b0f4b9d6 1031 {
zdshelby 1:14a9b0f4b9d6 1032 uint8_t i = 0;
zdshelby 1:14a9b0f4b9d6 1033
zdshelby 1:14a9b0f4b9d6 1034 *temp_ptr++ = ';';
zdshelby 1:14a9b0f4b9d6 1035 memcpy(temp_ptr, aobs_parameter, AOBS_PARAMETER_LEN);
zdshelby 1:14a9b0f4b9d6 1036 temp_ptr += AOBS_PARAMETER_LEN;
zdshelby 1:14a9b0f4b9d6 1037
zdshelby 1:14a9b0f4b9d6 1038 while(i < resource_temp_ptr->resource_parameters_ptr->auto_obs_len)
zdshelby 1:14a9b0f4b9d6 1039 {
zdshelby 1:14a9b0f4b9d6 1040 temp_ptr = sn_nsdl_itoa(temp_ptr, *(resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr + i));
zdshelby 1:14a9b0f4b9d6 1041 i++;
zdshelby 1:14a9b0f4b9d6 1042 }
zdshelby 1:14a9b0f4b9d6 1043 }
zdshelby 1:14a9b0f4b9d6 1044 */
zdshelby 1:14a9b0f4b9d6 1045
zdshelby 1:14a9b0f4b9d6 1046 }
zdshelby 1:14a9b0f4b9d6 1047
zdshelby 1:14a9b0f4b9d6 1048 resource_temp_ptr = sn_grs_get_next_resource();
zdshelby 1:14a9b0f4b9d6 1049
zdshelby 1:14a9b0f4b9d6 1050 }
zdshelby 1:14a9b0f4b9d6 1051
zdshelby 1:14a9b0f4b9d6 1052 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1053 }
zdshelby 1:14a9b0f4b9d6 1054
zdshelby 1:14a9b0f4b9d6 1055 /**
zdshelby 1:14a9b0f4b9d6 1056 * \fn static uint16_t sn_nsdl_calculate_registration_body_size(sn_grs_resource_list_s *grs_resources_list_ptr)
zdshelby 1:14a9b0f4b9d6 1057 *
zdshelby 1:14a9b0f4b9d6 1058 *
zdshelby 1:14a9b0f4b9d6 1059 * \brief Calculates registration message payload size
zdshelby 1:14a9b0f4b9d6 1060 *
zdshelby 1:14a9b0f4b9d6 1061 * \param *grs_resources_list_ptr Pointer to list of GRS resources
zdshelby 1:14a9b0f4b9d6 1062 *
zdshelby 1:14a9b0f4b9d6 1063 * \return Needed payload size
zdshelby 1:14a9b0f4b9d6 1064 */
zdshelby 1:14a9b0f4b9d6 1065 static uint16_t sn_nsdl_calculate_registration_body_size(uint8_t updating_registeration)
zdshelby 1:14a9b0f4b9d6 1066 {
zdshelby 1:14a9b0f4b9d6 1067 /* Local variables */
zdshelby 1:14a9b0f4b9d6 1068 uint16_t return_value = 0;
zdshelby 1:14a9b0f4b9d6 1069 sn_nsdl_resource_info_s *resource_temp_ptr;
zdshelby 1:14a9b0f4b9d6 1070
zdshelby 1:14a9b0f4b9d6 1071 /* check pointer */
zdshelby 1:14a9b0f4b9d6 1072
zdshelby 1:14a9b0f4b9d6 1073 resource_temp_ptr = sn_grs_get_first_resource();
zdshelby 1:14a9b0f4b9d6 1074
zdshelby 1:14a9b0f4b9d6 1075 while(resource_temp_ptr)
zdshelby 1:14a9b0f4b9d6 1076 {
zdshelby 1:14a9b0f4b9d6 1077
zdshelby 1:14a9b0f4b9d6 1078 if(resource_temp_ptr->resource_parameters_ptr)
zdshelby 1:14a9b0f4b9d6 1079 {
zdshelby 1:14a9b0f4b9d6 1080
zdshelby 1:14a9b0f4b9d6 1081 if(updating_registeration && resource_temp_ptr->resource_parameters_ptr->registered == SN_NDSL_RESOURCE_REGISTERED)
zdshelby 1:14a9b0f4b9d6 1082 {
zdshelby 1:14a9b0f4b9d6 1083 resource_temp_ptr = sn_grs_get_next_resource();
zdshelby 1:14a9b0f4b9d6 1084 continue;
zdshelby 1:14a9b0f4b9d6 1085 }
zdshelby 1:14a9b0f4b9d6 1086
zdshelby 1:14a9b0f4b9d6 1087 /* If not first resource, then '.' will be added */
zdshelby 1:14a9b0f4b9d6 1088 if(return_value)
zdshelby 1:14a9b0f4b9d6 1089 return_value++;
zdshelby 1:14a9b0f4b9d6 1090
zdshelby 1:14a9b0f4b9d6 1091 /* Count length for the resource path </path> */
zdshelby 1:14a9b0f4b9d6 1092 return_value += (3 + resource_temp_ptr->pathlen);
zdshelby 1:14a9b0f4b9d6 1093
zdshelby 1:14a9b0f4b9d6 1094 /* Count lengths of the attributes */
zdshelby 1:14a9b0f4b9d6 1095
zdshelby 1:14a9b0f4b9d6 1096 /* Resource type parameter */
zdshelby 1:14a9b0f4b9d6 1097 if(resource_temp_ptr->resource_parameters_ptr->resource_type_len)
zdshelby 1:14a9b0f4b9d6 1098 {
zdshelby 1:14a9b0f4b9d6 1099 /* ;rt="restype" */
zdshelby 1:14a9b0f4b9d6 1100 return_value += (6 + resource_temp_ptr->resource_parameters_ptr->resource_type_len);
zdshelby 1:14a9b0f4b9d6 1101 }
zdshelby 1:14a9b0f4b9d6 1102
zdshelby 1:14a9b0f4b9d6 1103 /* Interface description parameter */
zdshelby 1:14a9b0f4b9d6 1104 if(resource_temp_ptr->resource_parameters_ptr->interface_description_len)
zdshelby 1:14a9b0f4b9d6 1105 {
zdshelby 1:14a9b0f4b9d6 1106 /* ;if="iftype" */
zdshelby 1:14a9b0f4b9d6 1107 return_value += (6 + resource_temp_ptr->resource_parameters_ptr->interface_description_len);
zdshelby 1:14a9b0f4b9d6 1108 }
zdshelby 1:14a9b0f4b9d6 1109
zdshelby 1:14a9b0f4b9d6 1110 if(resource_temp_ptr->resource_parameters_ptr->coap_content_type != 0)
zdshelby 1:14a9b0f4b9d6 1111 {
zdshelby 1:14a9b0f4b9d6 1112 /* ;if="content" */
zdshelby 1:14a9b0f4b9d6 1113 return_value += 6; // all but not content
zdshelby 1:14a9b0f4b9d6 1114 return_value += sn_nsdl_itoa_len(resource_temp_ptr->resource_parameters_ptr->coap_content_type);
zdshelby 1:14a9b0f4b9d6 1115 }
zdshelby 1:14a9b0f4b9d6 1116
zdshelby 1:14a9b0f4b9d6 1117 if(resource_temp_ptr->resource_parameters_ptr->observable)
zdshelby 1:14a9b0f4b9d6 1118 {
zdshelby 1:14a9b0f4b9d6 1119 /* ;obs */
zdshelby 1:14a9b0f4b9d6 1120 return_value += 4;
zdshelby 1:14a9b0f4b9d6 1121 }
zdshelby 1:14a9b0f4b9d6 1122 /*todo: aobs not supported ATM - needs fixing*/
zdshelby 1:14a9b0f4b9d6 1123 /*
zdshelby 1:14a9b0f4b9d6 1124 if((resource_temp_ptr->resource_parameters_ptr->auto_obs_len > 0 && resource_temp_ptr->resource_parameters_ptr->auto_obs_len <= 8) &&
zdshelby 1:14a9b0f4b9d6 1125 resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr)
zdshelby 1:14a9b0f4b9d6 1126 {
zdshelby 1:14a9b0f4b9d6 1127 uint8_t i = resource_temp_ptr->resource_parameters_ptr->auto_obs_len;
zdshelby 1:14a9b0f4b9d6 1128 // ;aobs;id=
zdshelby 1:14a9b0f4b9d6 1129 return_value += 9;
zdshelby 1:14a9b0f4b9d6 1130 while(i--)
zdshelby 1:14a9b0f4b9d6 1131 {
zdshelby 1:14a9b0f4b9d6 1132 return_value += sn_nsdl_itoa_len(*(resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr + i));
zdshelby 1:14a9b0f4b9d6 1133 }
zdshelby 1:14a9b0f4b9d6 1134 }
zdshelby 1:14a9b0f4b9d6 1135 */
zdshelby 1:14a9b0f4b9d6 1136
zdshelby 1:14a9b0f4b9d6 1137 }
zdshelby 1:14a9b0f4b9d6 1138
zdshelby 1:14a9b0f4b9d6 1139 resource_temp_ptr = sn_grs_get_next_resource();
zdshelby 1:14a9b0f4b9d6 1140
zdshelby 1:14a9b0f4b9d6 1141 }
zdshelby 1:14a9b0f4b9d6 1142
zdshelby 1:14a9b0f4b9d6 1143 return return_value;
zdshelby 1:14a9b0f4b9d6 1144
zdshelby 1:14a9b0f4b9d6 1145 }
zdshelby 1:14a9b0f4b9d6 1146
zdshelby 1:14a9b0f4b9d6 1147 /**
zdshelby 1:14a9b0f4b9d6 1148 * \fn static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type)
zdshelby 1:14a9b0f4b9d6 1149 *
zdshelby 1:14a9b0f4b9d6 1150 *
zdshelby 1:14a9b0f4b9d6 1151 * \brief Calculates needed uri query option length
zdshelby 1:14a9b0f4b9d6 1152 *
zdshelby 1:14a9b0f4b9d6 1153 * \param *endpoint_info_ptr Pointer to endpoint info structure
zdshelby 1:14a9b0f4b9d6 1154 * \param msg_type Message type
zdshelby 1:14a9b0f4b9d6 1155 *
zdshelby 1:14a9b0f4b9d6 1156 * \return SN_NSDL_SUCCESS = 0, Failed = -1
zdshelby 1:14a9b0f4b9d6 1157 */
zdshelby 1:14a9b0f4b9d6 1158 static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type)
zdshelby 1:14a9b0f4b9d6 1159 {
zdshelby 1:14a9b0f4b9d6 1160 uint8_t return_value = 0;
zdshelby 1:14a9b0f4b9d6 1161 uint8_t number_of_parameters = 0;
zdshelby 1:14a9b0f4b9d6 1162
zdshelby 1:14a9b0f4b9d6 1163
zdshelby 1:14a9b0f4b9d6 1164 if((endpoint_info_ptr->endpoint_name_len != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE) && endpoint_info_ptr->endpoint_name_ptr != 0)
zdshelby 1:14a9b0f4b9d6 1165 {
zdshelby 1:14a9b0f4b9d6 1166 return_value += endpoint_info_ptr->endpoint_name_len;
zdshelby 1:14a9b0f4b9d6 1167 return_value += 2; //h=
zdshelby 1:14a9b0f4b9d6 1168 number_of_parameters++;
zdshelby 1:14a9b0f4b9d6 1169 }
zdshelby 1:14a9b0f4b9d6 1170
zdshelby 1:14a9b0f4b9d6 1171 if((endpoint_info_ptr->type_len != 0) && (endpoint_info_ptr->type_ptr != 0))
zdshelby 1:14a9b0f4b9d6 1172 {
zdshelby 1:14a9b0f4b9d6 1173 return_value+=endpoint_info_ptr->type_len;
zdshelby 1:14a9b0f4b9d6 1174 return_value += 3;
zdshelby 1:14a9b0f4b9d6 1175 number_of_parameters++;
zdshelby 1:14a9b0f4b9d6 1176 }
zdshelby 1:14a9b0f4b9d6 1177
zdshelby 1:14a9b0f4b9d6 1178 if((endpoint_info_ptr->lifetime_len != 0) && (endpoint_info_ptr->lifetime_ptr != 0))
zdshelby 1:14a9b0f4b9d6 1179 {
zdshelby 1:14a9b0f4b9d6 1180 return_value+=endpoint_info_ptr->lifetime_len;
zdshelby 1:14a9b0f4b9d6 1181 return_value += 3;
zdshelby 1:14a9b0f4b9d6 1182 number_of_parameters++;
zdshelby 1:14a9b0f4b9d6 1183 }
zdshelby 1:14a9b0f4b9d6 1184
zdshelby 1:14a9b0f4b9d6 1185 if(number_of_parameters != 0)
zdshelby 1:14a9b0f4b9d6 1186 return_value += (number_of_parameters - 1);
zdshelby 1:14a9b0f4b9d6 1187
zdshelby 1:14a9b0f4b9d6 1188 return return_value;
zdshelby 1:14a9b0f4b9d6 1189 }
zdshelby 1:14a9b0f4b9d6 1190
zdshelby 1:14a9b0f4b9d6 1191 /**
zdshelby 1:14a9b0f4b9d6 1192 * \fn static int8_t sn_nsdl_fill_uri_query_options(sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type)
zdshelby 1:14a9b0f4b9d6 1193 *
zdshelby 1:14a9b0f4b9d6 1194 *
zdshelby 1:14a9b0f4b9d6 1195 * \brief Fills uri-query options to message header struct
zdshelby 1:14a9b0f4b9d6 1196 *
zdshelby 1:14a9b0f4b9d6 1197 * \param *parameter_ptr Pointer to endpoint parameters struct
zdshelby 1:14a9b0f4b9d6 1198 * \param *source_msg_ptr Pointer to CoAP header struct
zdshelby 1:14a9b0f4b9d6 1199 * \param msg_type Message type
zdshelby 1:14a9b0f4b9d6 1200 *
zdshelby 1:14a9b0f4b9d6 1201 * \return SN_NSDL_SUCCESS = 0, Failed = -1
zdshelby 1:14a9b0f4b9d6 1202 */
zdshelby 1:14a9b0f4b9d6 1203 static int8_t sn_nsdl_fill_uri_query_options(sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type)
zdshelby 1:14a9b0f4b9d6 1204 {
zdshelby 1:14a9b0f4b9d6 1205 uint8_t *temp_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 1206 source_msg_ptr->options_list_ptr->uri_query_len = sn_nsdl_calculate_uri_query_option_len(parameter_ptr, msg_type);
zdshelby 1:14a9b0f4b9d6 1207
zdshelby 1:14a9b0f4b9d6 1208 if(source_msg_ptr->options_list_ptr->uri_query_len == 0)
zdshelby 1:14a9b0f4b9d6 1209 return 0;
zdshelby 1:14a9b0f4b9d6 1210
zdshelby 1:14a9b0f4b9d6 1211 source_msg_ptr->options_list_ptr->uri_query_ptr = sn_nsdl_alloc(source_msg_ptr->options_list_ptr->uri_query_len);
zdshelby 1:14a9b0f4b9d6 1212
zdshelby 1:14a9b0f4b9d6 1213 if (source_msg_ptr->options_list_ptr->uri_query_ptr == NULL)
zdshelby 1:14a9b0f4b9d6 1214 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1215
zdshelby 1:14a9b0f4b9d6 1216 temp_ptr = source_msg_ptr->options_list_ptr->uri_query_ptr;
zdshelby 1:14a9b0f4b9d6 1217
zdshelby 1:14a9b0f4b9d6 1218 /******************************************************/
zdshelby 1:14a9b0f4b9d6 1219 /* If endpoint name is configured, fill needed fields */
zdshelby 1:14a9b0f4b9d6 1220 /******************************************************/
zdshelby 1:14a9b0f4b9d6 1221
zdshelby 1:14a9b0f4b9d6 1222 if((parameter_ptr->endpoint_name_len != 0) && (parameter_ptr->endpoint_name_ptr != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE))
zdshelby 1:14a9b0f4b9d6 1223 {
zdshelby 1:14a9b0f4b9d6 1224 /* fill endpoint name, first ?h=, then endpoint name */
zdshelby 1:14a9b0f4b9d6 1225 memcpy(temp_ptr, ep_name_parameter_string, sizeof(ep_name_parameter_string));
zdshelby 1:14a9b0f4b9d6 1226 temp_ptr += EP_NAME_PARAMETERS_LEN;
zdshelby 1:14a9b0f4b9d6 1227 memcpy(temp_ptr, parameter_ptr->endpoint_name_ptr, parameter_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 1228 temp_ptr += parameter_ptr->endpoint_name_len;
zdshelby 1:14a9b0f4b9d6 1229 }
zdshelby 1:14a9b0f4b9d6 1230
zdshelby 1:14a9b0f4b9d6 1231 /******************************************************/
zdshelby 1:14a9b0f4b9d6 1232 /* If endpoint type is configured, fill needed fields */
zdshelby 1:14a9b0f4b9d6 1233 /******************************************************/
zdshelby 1:14a9b0f4b9d6 1234
zdshelby 1:14a9b0f4b9d6 1235 if((parameter_ptr->type_len != 0) && (parameter_ptr->type_ptr != 0))
zdshelby 1:14a9b0f4b9d6 1236 {
zdshelby 1:14a9b0f4b9d6 1237 if(temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr)
zdshelby 1:14a9b0f4b9d6 1238 *temp_ptr++ = '&';
zdshelby 1:14a9b0f4b9d6 1239
zdshelby 1:14a9b0f4b9d6 1240 memcpy(temp_ptr, resource_type_parameter, sizeof(resource_type_parameter));
zdshelby 1:14a9b0f4b9d6 1241 temp_ptr += RT_PARAMETER_LEN;
zdshelby 1:14a9b0f4b9d6 1242 memcpy(temp_ptr, parameter_ptr->type_ptr, parameter_ptr->type_len);
zdshelby 1:14a9b0f4b9d6 1243 temp_ptr += parameter_ptr->type_len;
zdshelby 1:14a9b0f4b9d6 1244 }
zdshelby 1:14a9b0f4b9d6 1245
zdshelby 1:14a9b0f4b9d6 1246
zdshelby 1:14a9b0f4b9d6 1247 /******************************************************/
zdshelby 1:14a9b0f4b9d6 1248 /* If lifetime is configured, fill needed fields */
zdshelby 1:14a9b0f4b9d6 1249 /******************************************************/
zdshelby 1:14a9b0f4b9d6 1250
zdshelby 1:14a9b0f4b9d6 1251 if((parameter_ptr->lifetime_len != 0) && (parameter_ptr->lifetime_ptr != 0))
zdshelby 1:14a9b0f4b9d6 1252 {
zdshelby 1:14a9b0f4b9d6 1253 if(temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr)
zdshelby 1:14a9b0f4b9d6 1254 *temp_ptr++ = '&';
zdshelby 1:14a9b0f4b9d6 1255
zdshelby 1:14a9b0f4b9d6 1256 memcpy(temp_ptr, ep_lifetime_parameter, sizeof(ep_lifetime_parameter));
zdshelby 1:14a9b0f4b9d6 1257 temp_ptr += LT_PARAMETER_LEN;
zdshelby 1:14a9b0f4b9d6 1258 memcpy(temp_ptr, parameter_ptr->lifetime_ptr, parameter_ptr->lifetime_len);
zdshelby 1:14a9b0f4b9d6 1259 temp_ptr += parameter_ptr->lifetime_len;
zdshelby 1:14a9b0f4b9d6 1260 }
zdshelby 1:14a9b0f4b9d6 1261
zdshelby 1:14a9b0f4b9d6 1262 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1263 }
zdshelby 1:14a9b0f4b9d6 1264
zdshelby 1:14a9b0f4b9d6 1265 /**
zdshelby 1:14a9b0f4b9d6 1266 * \fn static uint8_t sn_nsdl_local_rx_function(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
zdshelby 1:14a9b0f4b9d6 1267 *
zdshelby 1:14a9b0f4b9d6 1268 * \brief If received message is reply for the message that NSDL has been sent, it is processed here. Else, packet will be sent to application.
zdshelby 1:14a9b0f4b9d6 1269 *
zdshelby 1:14a9b0f4b9d6 1270 * \param *coap_packet_ptr Pointer to received CoAP packet
zdshelby 1:14a9b0f4b9d6 1271 * \param *address_ptr Pointer to source address struct
zdshelby 1:14a9b0f4b9d6 1272 *
zdshelby 1:14a9b0f4b9d6 1273 * \return SN_NSDL_SUCCESS = 0, Failed = -1
zdshelby 1:14a9b0f4b9d6 1274 */
zdshelby 1:14a9b0f4b9d6 1275 static int8_t sn_nsdl_local_rx_function(sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
zdshelby 1:14a9b0f4b9d6 1276 {
zdshelby 1:14a9b0f4b9d6 1277 int8_t status = 0;
zdshelby 1:14a9b0f4b9d6 1278 uint16_t number_of_messages;
zdshelby 1:14a9b0f4b9d6 1279 sn_nsdl_sent_messages_s *sent_message_temp_ptr;
zdshelby 1:14a9b0f4b9d6 1280
zdshelby 1:14a9b0f4b9d6 1281 if((coap_packet_ptr == 0) || (address_ptr == 0))
zdshelby 1:14a9b0f4b9d6 1282 return -1;
zdshelby 1:14a9b0f4b9d6 1283
zdshelby 1:14a9b0f4b9d6 1284 /* If we wait for a response to some message.. */
zdshelby 1:14a9b0f4b9d6 1285 number_of_messages = sn_linked_list_count_nodes(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 1286
zdshelby 1:14a9b0f4b9d6 1287 if(number_of_messages)
zdshelby 1:14a9b0f4b9d6 1288 {
zdshelby 1:14a9b0f4b9d6 1289 while(number_of_messages--)
zdshelby 1:14a9b0f4b9d6 1290 {
zdshelby 1:14a9b0f4b9d6 1291 sent_message_temp_ptr = sn_linked_list_get_last_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 1292
zdshelby 1:14a9b0f4b9d6 1293 if(sent_message_temp_ptr)
zdshelby 1:14a9b0f4b9d6 1294 {
zdshelby 1:14a9b0f4b9d6 1295 if(sent_message_temp_ptr->msg_id_number == coap_packet_ptr->msg_id)
zdshelby 1:14a9b0f4b9d6 1296 {
zdshelby 1:14a9b0f4b9d6 1297 switch(sent_message_temp_ptr->message_type)
zdshelby 1:14a9b0f4b9d6 1298 {
zdshelby 1:14a9b0f4b9d6 1299 case SN_NSDL_MSG_REGISTER:
zdshelby 1:14a9b0f4b9d6 1300 if(coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_CREATED)
zdshelby 1:14a9b0f4b9d6 1301 {
zdshelby 1:14a9b0f4b9d6 1302 sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_IS_REGISTERED;
zdshelby 1:14a9b0f4b9d6 1303 sn_nsdl_mark_resources_as_registered();
zdshelby 1:14a9b0f4b9d6 1304 status = sn_nsdl_resolve_ep_information(coap_packet_ptr);
zdshelby 1:14a9b0f4b9d6 1305 if(status != SN_NSDL_SUCCESS)
zdshelby 1:14a9b0f4b9d6 1306 {
zdshelby 1:14a9b0f4b9d6 1307 /* Node can be removed */
zdshelby 1:14a9b0f4b9d6 1308 sn_nsdl_free(sent_message_temp_ptr);
zdshelby 1:14a9b0f4b9d6 1309 sn_linked_list_remove_current_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 1310 return status;
zdshelby 1:14a9b0f4b9d6 1311 }
zdshelby 1:14a9b0f4b9d6 1312 }
zdshelby 1:14a9b0f4b9d6 1313 break;
zdshelby 1:14a9b0f4b9d6 1314 case SN_NSDL_MSG_UNREGISTER:
zdshelby 1:14a9b0f4b9d6 1315 if(coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_DELETED)
zdshelby 1:14a9b0f4b9d6 1316 {
zdshelby 1:14a9b0f4b9d6 1317 if(ep_information_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 1318 {
zdshelby 1:14a9b0f4b9d6 1319 sn_nsdl_free(ep_information_ptr->endpoint_name_ptr);
zdshelby 1:14a9b0f4b9d6 1320 ep_information_ptr->endpoint_name_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1321 ep_information_ptr->endpoint_name_len = 0;
zdshelby 1:14a9b0f4b9d6 1322 }
zdshelby 1:14a9b0f4b9d6 1323
zdshelby 1:14a9b0f4b9d6 1324 if(ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 1325 {
zdshelby 1:14a9b0f4b9d6 1326 sn_nsdl_free(ep_information_ptr->domain_name_ptr);
zdshelby 1:14a9b0f4b9d6 1327 ep_information_ptr->domain_name_ptr = 0;
zdshelby 1:14a9b0f4b9d6 1328 ep_information_ptr->domain_name_len = 0;
zdshelby 1:14a9b0f4b9d6 1329 }
zdshelby 1:14a9b0f4b9d6 1330
zdshelby 1:14a9b0f4b9d6 1331 }
zdshelby 1:14a9b0f4b9d6 1332 break;
zdshelby 1:14a9b0f4b9d6 1333 case SN_NSDL_MSG_EVENT:
zdshelby 1:14a9b0f4b9d6 1334 case SN_NSDL_MSG_UPDATE:
zdshelby 1:14a9b0f4b9d6 1335 break;
zdshelby 1:14a9b0f4b9d6 1336 }
zdshelby 1:14a9b0f4b9d6 1337 /* Node can be removed */
zdshelby 1:14a9b0f4b9d6 1338 sn_nsdl_free(sent_message_temp_ptr);
zdshelby 1:14a9b0f4b9d6 1339 sn_linked_list_remove_current_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 1340
zdshelby 1:14a9b0f4b9d6 1341 sn_nsdl_rx_callback(coap_packet_ptr, address_ptr);
zdshelby 1:14a9b0f4b9d6 1342 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1343 }
zdshelby 1:14a9b0f4b9d6 1344 }
zdshelby 1:14a9b0f4b9d6 1345 sent_message_temp_ptr = sn_linked_list_get_previous_node(message_list_ptr);
zdshelby 1:14a9b0f4b9d6 1346 }
zdshelby 1:14a9b0f4b9d6 1347 }
zdshelby 1:14a9b0f4b9d6 1348
zdshelby 1:14a9b0f4b9d6 1349 /* No messages to wait for, or message was not response to our request */
zdshelby 1:14a9b0f4b9d6 1350 status = sn_nsdl_rx_callback(coap_packet_ptr, address_ptr);
zdshelby 1:14a9b0f4b9d6 1351
zdshelby 1:14a9b0f4b9d6 1352 return status;
zdshelby 1:14a9b0f4b9d6 1353 }
zdshelby 1:14a9b0f4b9d6 1354
zdshelby 1:14a9b0f4b9d6 1355 void sn_nsdl_mark_resources_as_registered(void)
zdshelby 1:14a9b0f4b9d6 1356 {
zdshelby 1:14a9b0f4b9d6 1357
zdshelby 1:14a9b0f4b9d6 1358 sn_nsdl_resource_info_s *temp_resource;
zdshelby 1:14a9b0f4b9d6 1359
zdshelby 1:14a9b0f4b9d6 1360 temp_resource = sn_grs_get_first_resource();
zdshelby 1:14a9b0f4b9d6 1361
zdshelby 1:14a9b0f4b9d6 1362 while(temp_resource)
zdshelby 1:14a9b0f4b9d6 1363 {
zdshelby 1:14a9b0f4b9d6 1364
zdshelby 1:14a9b0f4b9d6 1365 if(temp_resource->resource_parameters_ptr->registered == SN_NDSL_RESOURCE_REGISTERING)
zdshelby 1:14a9b0f4b9d6 1366 {
zdshelby 1:14a9b0f4b9d6 1367
zdshelby 1:14a9b0f4b9d6 1368 temp_resource->resource_parameters_ptr->registered = SN_NDSL_RESOURCE_REGISTERED;
zdshelby 1:14a9b0f4b9d6 1369
zdshelby 1:14a9b0f4b9d6 1370 }
zdshelby 1:14a9b0f4b9d6 1371
zdshelby 1:14a9b0f4b9d6 1372 temp_resource = sn_grs_get_next_resource();
zdshelby 1:14a9b0f4b9d6 1373
zdshelby 1:14a9b0f4b9d6 1374 }
zdshelby 1:14a9b0f4b9d6 1375
zdshelby 1:14a9b0f4b9d6 1376
zdshelby 1:14a9b0f4b9d6 1377 }
zdshelby 1:14a9b0f4b9d6 1378
zdshelby 1:14a9b0f4b9d6 1379 /**
zdshelby 1:14a9b0f4b9d6 1380 * \fn static int8_t sn_nsdl_resolve_ep_information(sn_coap_hdr_s *coap_packet_ptr)
zdshelby 1:14a9b0f4b9d6 1381 *
zdshelby 1:14a9b0f4b9d6 1382 *
zdshelby 1:14a9b0f4b9d6 1383 * \brief Resolves endpoint information from received CoAP message
zdshelby 1:14a9b0f4b9d6 1384 *
zdshelby 1:14a9b0f4b9d6 1385 * \param *coap_packet_ptr Pointer to received CoAP message
zdshelby 1:14a9b0f4b9d6 1386 *
zdshelby 1:14a9b0f4b9d6 1387 * \return SN_NSDL_SUCCESS = 0, Failed = -1
zdshelby 1:14a9b0f4b9d6 1388 */
zdshelby 1:14a9b0f4b9d6 1389 static int8_t sn_nsdl_resolve_ep_information(sn_coap_hdr_s *coap_packet_ptr)
zdshelby 1:14a9b0f4b9d6 1390 {
zdshelby 1:14a9b0f4b9d6 1391 uint8_t *temp_ptr;
zdshelby 1:14a9b0f4b9d6 1392 uint8_t parameter_count = 0;
zdshelby 1:14a9b0f4b9d6 1393 uint16_t parameter_len = 0;
zdshelby 1:14a9b0f4b9d6 1394
zdshelby 1:14a9b0f4b9d6 1395 if(!coap_packet_ptr)
zdshelby 1:14a9b0f4b9d6 1396 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1397 if(!coap_packet_ptr->options_list_ptr)
zdshelby 1:14a9b0f4b9d6 1398 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1399 if(!coap_packet_ptr->options_list_ptr->location_path_ptr)
zdshelby 1:14a9b0f4b9d6 1400 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1401
zdshelby 1:14a9b0f4b9d6 1402 temp_ptr = coap_packet_ptr->options_list_ptr->location_path_ptr;
zdshelby 1:14a9b0f4b9d6 1403
zdshelby 1:14a9b0f4b9d6 1404 while(temp_ptr <= (coap_packet_ptr->options_list_ptr->location_path_ptr + coap_packet_ptr->options_list_ptr->location_path_len))
zdshelby 1:14a9b0f4b9d6 1405 {
zdshelby 1:14a9b0f4b9d6 1406
zdshelby 1:14a9b0f4b9d6 1407 if((temp_ptr == (coap_packet_ptr->options_list_ptr->location_path_ptr + coap_packet_ptr->options_list_ptr->location_path_len)) || (*temp_ptr == '/'))
zdshelby 1:14a9b0f4b9d6 1408 {
zdshelby 1:14a9b0f4b9d6 1409
zdshelby 1:14a9b0f4b9d6 1410 parameter_count++;
zdshelby 1:14a9b0f4b9d6 1411 if(parameter_count == 2)
zdshelby 1:14a9b0f4b9d6 1412 {
zdshelby 1:14a9b0f4b9d6 1413 if(!ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 1414 {
zdshelby 1:14a9b0f4b9d6 1415 ep_information_ptr->domain_name_len = parameter_len - 1;
zdshelby 1:14a9b0f4b9d6 1416 ep_information_ptr->domain_name_ptr = sn_nsdl_alloc(ep_information_ptr->domain_name_len);
zdshelby 1:14a9b0f4b9d6 1417 if(!ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 1418 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1419 memcpy(ep_information_ptr->domain_name_ptr, temp_ptr - ep_information_ptr->domain_name_len, ep_information_ptr->domain_name_len);
zdshelby 1:14a9b0f4b9d6 1420 }
zdshelby 1:14a9b0f4b9d6 1421
zdshelby 1:14a9b0f4b9d6 1422 }
zdshelby 1:14a9b0f4b9d6 1423 if(parameter_count == 3)
zdshelby 1:14a9b0f4b9d6 1424 {
zdshelby 1:14a9b0f4b9d6 1425 if(!ep_information_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 1426 {
zdshelby 1:14a9b0f4b9d6 1427 ep_information_ptr->endpoint_name_len = parameter_len - 1;
zdshelby 1:14a9b0f4b9d6 1428 ep_information_ptr->endpoint_name_ptr = sn_nsdl_alloc(ep_information_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 1429 if(!ep_information_ptr->endpoint_name_ptr)
zdshelby 1:14a9b0f4b9d6 1430 {
zdshelby 1:14a9b0f4b9d6 1431 if(ep_information_ptr->domain_name_ptr)
zdshelby 1:14a9b0f4b9d6 1432 {
zdshelby 1:14a9b0f4b9d6 1433 sn_nsdl_free(ep_information_ptr->domain_name_ptr);
zdshelby 1:14a9b0f4b9d6 1434 ep_information_ptr->domain_name_ptr = NULL;
zdshelby 1:14a9b0f4b9d6 1435 ep_information_ptr->domain_name_len = 0;
zdshelby 1:14a9b0f4b9d6 1436 }
zdshelby 1:14a9b0f4b9d6 1437
zdshelby 1:14a9b0f4b9d6 1438 return SN_NSDL_FAILURE;
zdshelby 1:14a9b0f4b9d6 1439
zdshelby 1:14a9b0f4b9d6 1440 }
zdshelby 1:14a9b0f4b9d6 1441 memcpy(ep_information_ptr->endpoint_name_ptr, temp_ptr - ep_information_ptr->endpoint_name_len, ep_information_ptr->endpoint_name_len);
zdshelby 1:14a9b0f4b9d6 1442 }
zdshelby 1:14a9b0f4b9d6 1443 }
zdshelby 1:14a9b0f4b9d6 1444 parameter_len = 0;
zdshelby 1:14a9b0f4b9d6 1445 }
zdshelby 1:14a9b0f4b9d6 1446 parameter_len++;
zdshelby 1:14a9b0f4b9d6 1447 temp_ptr++;
zdshelby 1:14a9b0f4b9d6 1448 }
zdshelby 1:14a9b0f4b9d6 1449
zdshelby 1:14a9b0f4b9d6 1450
zdshelby 1:14a9b0f4b9d6 1451 return SN_NSDL_SUCCESS;
zdshelby 1:14a9b0f4b9d6 1452 }
zdshelby 1:14a9b0f4b9d6 1453
zdshelby 1:14a9b0f4b9d6 1454 /**
zdshelby 1:14a9b0f4b9d6 1455 * \fn int8_t set_NSP_address(uint8_t *NSP_address, uint16_t port, sn_nsdl_addr_type_e address_type)
zdshelby 1:14a9b0f4b9d6 1456 * \brief This function is used to set the NSP address given by an application.
zdshelby 1:14a9b0f4b9d6 1457 * \param uint8_t *NSP_address Pointer to NSP address Note! IPv6 address must always be 16 bytes long and IPv4 address must always be 4 bytes long!
zdshelby 1:14a9b0f4b9d6 1458 * \param uint16_t port NSP port
zdshelby 1:14a9b0f4b9d6 1459 * \param sn_nsdl_addr_type_e address_type NSP address type (SN_NSDL_ADDRESS_TYPE_IPV6 or SN_NSDL_ADDRESS_TYPE_IPV4)
zdshelby 1:14a9b0f4b9d6 1460 * \return 0 on success, -1 on false to indicate that NSDL internal address pointer is not allocated (call nsdl_init() first).
zdshelby 1:14a9b0f4b9d6 1461 *
zdshelby 1:14a9b0f4b9d6 1462 */
zdshelby 1:14a9b0f4b9d6 1463 int8_t set_NSP_address(uint8_t *NSP_address, uint16_t port, sn_nsdl_addr_type_e address_type)
zdshelby 1:14a9b0f4b9d6 1464 {
zdshelby 1:14a9b0f4b9d6 1465
zdshelby 1:14a9b0f4b9d6 1466 /* Check parameters and source pointers */
zdshelby 1:14a9b0f4b9d6 1467 if(!nsp_address_ptr || !NSP_address)
zdshelby 1:14a9b0f4b9d6 1468 {
zdshelby 1:14a9b0f4b9d6 1469 return -1;
zdshelby 1:14a9b0f4b9d6 1470 }
zdshelby 1:14a9b0f4b9d6 1471
zdshelby 1:14a9b0f4b9d6 1472 nsp_address_ptr->type = address_type;
zdshelby 1:14a9b0f4b9d6 1473
zdshelby 1:14a9b0f4b9d6 1474 if(address_type == SN_NSDL_ADDRESS_TYPE_IPV4)
zdshelby 1:14a9b0f4b9d6 1475 {
zdshelby 1:14a9b0f4b9d6 1476 if(nsp_address_ptr->addr_ptr)
zdshelby 1:14a9b0f4b9d6 1477 {
zdshelby 1:14a9b0f4b9d6 1478 sn_nsdl_free(nsp_address_ptr->addr_ptr);
zdshelby 1:14a9b0f4b9d6 1479 }
zdshelby 1:14a9b0f4b9d6 1480
zdshelby 1:14a9b0f4b9d6 1481 nsp_address_ptr->addr_len = 4;
zdshelby 1:14a9b0f4b9d6 1482
zdshelby 1:14a9b0f4b9d6 1483 nsp_address_ptr->addr_ptr = sn_nsdl_alloc(nsp_address_ptr->addr_len);
zdshelby 1:14a9b0f4b9d6 1484 if(!nsp_address_ptr->addr_ptr)
zdshelby 1:14a9b0f4b9d6 1485 return -1;
zdshelby 1:14a9b0f4b9d6 1486
zdshelby 1:14a9b0f4b9d6 1487 memcpy(nsp_address_ptr->addr_ptr, NSP_address, nsp_address_ptr->addr_len);
zdshelby 1:14a9b0f4b9d6 1488 nsp_address_ptr->port = port;
zdshelby 1:14a9b0f4b9d6 1489 }
zdshelby 1:14a9b0f4b9d6 1490
zdshelby 1:14a9b0f4b9d6 1491 else if(address_type == SN_NSDL_ADDRESS_TYPE_IPV6)
zdshelby 1:14a9b0f4b9d6 1492 {
zdshelby 1:14a9b0f4b9d6 1493 if(nsp_address_ptr->addr_ptr)
zdshelby 1:14a9b0f4b9d6 1494 {
zdshelby 1:14a9b0f4b9d6 1495 sn_nsdl_free(nsp_address_ptr->addr_ptr);
zdshelby 1:14a9b0f4b9d6 1496 }
zdshelby 1:14a9b0f4b9d6 1497
zdshelby 1:14a9b0f4b9d6 1498 nsp_address_ptr->addr_len = 16;
zdshelby 1:14a9b0f4b9d6 1499
zdshelby 1:14a9b0f4b9d6 1500 nsp_address_ptr->addr_ptr = sn_nsdl_alloc(nsp_address_ptr->addr_len);
zdshelby 1:14a9b0f4b9d6 1501 if(!nsp_address_ptr->addr_ptr)
zdshelby 1:14a9b0f4b9d6 1502 return -1;
zdshelby 1:14a9b0f4b9d6 1503
zdshelby 1:14a9b0f4b9d6 1504 memcpy(nsp_address_ptr->addr_ptr, NSP_address, nsp_address_ptr->addr_len);
zdshelby 1:14a9b0f4b9d6 1505 nsp_address_ptr->port = port;
zdshelby 1:14a9b0f4b9d6 1506 }
zdshelby 1:14a9b0f4b9d6 1507 return 0;
zdshelby 1:14a9b0f4b9d6 1508 }
zdshelby 1:14a9b0f4b9d6 1509
zdshelby 1:14a9b0f4b9d6 1510
zdshelby 1:14a9b0f4b9d6 1511 static uint8_t sn_nsdl_itoa_len(uint8_t value)
zdshelby 1:14a9b0f4b9d6 1512 {
zdshelby 1:14a9b0f4b9d6 1513 uint8_t i = 0;
zdshelby 1:14a9b0f4b9d6 1514
zdshelby 1:14a9b0f4b9d6 1515 do
zdshelby 1:14a9b0f4b9d6 1516 {
zdshelby 1:14a9b0f4b9d6 1517 i++;
zdshelby 1:14a9b0f4b9d6 1518 }while((value /= 10) > 0);
zdshelby 1:14a9b0f4b9d6 1519
zdshelby 1:14a9b0f4b9d6 1520 return i;
zdshelby 1:14a9b0f4b9d6 1521 }
zdshelby 1:14a9b0f4b9d6 1522
zdshelby 1:14a9b0f4b9d6 1523 static uint8_t *sn_nsdl_itoa(uint8_t *ptr, uint8_t value)
zdshelby 1:14a9b0f4b9d6 1524 {
zdshelby 1:14a9b0f4b9d6 1525
zdshelby 1:14a9b0f4b9d6 1526 uint8_t start = 0;
zdshelby 1:14a9b0f4b9d6 1527 uint8_t end = 0;
zdshelby 1:14a9b0f4b9d6 1528 uint8_t i;
zdshelby 1:14a9b0f4b9d6 1529
zdshelby 1:14a9b0f4b9d6 1530 i = 0;
zdshelby 1:14a9b0f4b9d6 1531
zdshelby 1:14a9b0f4b9d6 1532 /* ITOA */
zdshelby 1:14a9b0f4b9d6 1533 do
zdshelby 1:14a9b0f4b9d6 1534 {
zdshelby 1:14a9b0f4b9d6 1535 ptr[i++] = (value % 10) + '0';
zdshelby 1:14a9b0f4b9d6 1536 }while((value /= 10) > 0);
zdshelby 1:14a9b0f4b9d6 1537
zdshelby 1:14a9b0f4b9d6 1538 end = i - 1;
zdshelby 1:14a9b0f4b9d6 1539
zdshelby 1:14a9b0f4b9d6 1540 /* reverse (part of ITOA) */
zdshelby 1:14a9b0f4b9d6 1541 while(start < end)
zdshelby 1:14a9b0f4b9d6 1542 {
zdshelby 1:14a9b0f4b9d6 1543 uint8_t chr;
zdshelby 1:14a9b0f4b9d6 1544
zdshelby 1:14a9b0f4b9d6 1545 chr = ptr[start];
zdshelby 1:14a9b0f4b9d6 1546 ptr[start] = ptr[end];
zdshelby 1:14a9b0f4b9d6 1547 ptr[end] = chr;
zdshelby 1:14a9b0f4b9d6 1548
zdshelby 1:14a9b0f4b9d6 1549 start++;
zdshelby 1:14a9b0f4b9d6 1550 end--;
zdshelby 1:14a9b0f4b9d6 1551
zdshelby 1:14a9b0f4b9d6 1552 }
zdshelby 1:14a9b0f4b9d6 1553 return (ptr + i);
zdshelby 1:14a9b0f4b9d6 1554 }
zdshelby 1:14a9b0f4b9d6 1555