sandbox / mbed-client-c

Fork of mbed-client-c by Christopher Haster

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christopher Haster 1:43f5c94c6771 1 /*
Christopher Haster 1:43f5c94c6771 2 * Copyright (c) 2011-2015 ARM Limited. All rights reserved.
Christopher Haster 1:43f5c94c6771 3 * SPDX-License-Identifier: Apache-2.0
Christopher Haster 1:43f5c94c6771 4 * Licensed under the Apache License, Version 2.0 (the License); you may
Christopher Haster 1:43f5c94c6771 5 * not use this file except in compliance with the License.
Christopher Haster 1:43f5c94c6771 6 * You may obtain a copy of the License at
Christopher Haster 1:43f5c94c6771 7 *
Christopher Haster 1:43f5c94c6771 8 * http://www.apache.org/licenses/LICENSE-2.0
Christopher Haster 1:43f5c94c6771 9 *
Christopher Haster 1:43f5c94c6771 10 * Unless required by applicable law or agreed to in writing, software
Christopher Haster 1:43f5c94c6771 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
Christopher Haster 1:43f5c94c6771 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Christopher Haster 1:43f5c94c6771 13 * See the License for the specific language governing permissions and
Christopher Haster 1:43f5c94c6771 14 * limitations under the License.
Christopher Haster 1:43f5c94c6771 15 */
Christopher Haster 1:43f5c94c6771 16 /**
Christopher Haster 1:43f5c94c6771 17 * \file sn_nsdl.c
Christopher Haster 1:43f5c94c6771 18 *
Christopher Haster 1:43f5c94c6771 19 * \brief Nano service device library
Christopher Haster 1:43f5c94c6771 20 *
Christopher Haster 1:43f5c94c6771 21 */
Christopher Haster 1:43f5c94c6771 22
Christopher Haster 1:43f5c94c6771 23 #include <string.h>
Christopher Haster 1:43f5c94c6771 24
Christopher Haster 1:43f5c94c6771 25 #include "ns_types.h"
Christopher Haster 1:43f5c94c6771 26 #include "sn_nsdl.h"
Christopher Haster 1:43f5c94c6771 27 #include "sn_coap_header.h"
Christopher Haster 1:43f5c94c6771 28 #include "sn_coap_protocol.h"
Christopher Haster 1:43f5c94c6771 29 #include "sn_nsdl_lib.h"
Christopher Haster 1:43f5c94c6771 30 #include "sn_grs.h"
Christopher Haster 1:43f5c94c6771 31
Christopher Haster 1:43f5c94c6771 32 /* Defines */
Christopher Haster 1:43f5c94c6771 33 #define RESOURCE_DIR_LEN 2
Christopher Haster 1:43f5c94c6771 34 #define EP_NAME_PARAMETERS_LEN 3
Christopher Haster 1:43f5c94c6771 35 #define ET_PARAMETER_LEN 3
Christopher Haster 1:43f5c94c6771 36 #define LT_PARAMETER_LEN 3
Christopher Haster 1:43f5c94c6771 37 #define DOMAIN_PARAMETER_LEN 2
Christopher Haster 1:43f5c94c6771 38 #define RT_PARAMETER_LEN 3
Christopher Haster 1:43f5c94c6771 39 #define IF_PARAMETER_LEN 3
Christopher Haster 1:43f5c94c6771 40 #define OBS_PARAMETER_LEN 3
Christopher Haster 1:43f5c94c6771 41 #define AOBS_PARAMETER_LEN 8
Christopher Haster 1:43f5c94c6771 42 #define COAP_CON_PARAMETER_LEN 3
Christopher Haster 1:43f5c94c6771 43 #define BS_EP_PARAMETER_LEN 3
Christopher Haster 1:43f5c94c6771 44 #define BS_QUEUE_MODE_PARAMATER_LEN 2
Christopher Haster 1:43f5c94c6771 45
Christopher Haster 1:43f5c94c6771 46 #define SN_NSDL_EP_REGISTER_MESSAGE 1
Christopher Haster 1:43f5c94c6771 47 #define SN_NSDL_EP_UPDATE_MESSAGE 2
Christopher Haster 1:43f5c94c6771 48
Christopher Haster 1:43f5c94c6771 49 #define SN_NSDL_MSG_UNDEFINED 0
Christopher Haster 1:43f5c94c6771 50 #define SN_NSDL_MSG_REGISTER 1
Christopher Haster 1:43f5c94c6771 51 #define SN_NSDL_MSG_UNREGISTER 2
Christopher Haster 1:43f5c94c6771 52 #define SN_NSDL_MSG_UPDATE 3
Christopher Haster 1:43f5c94c6771 53
Christopher Haster 1:43f5c94c6771 54 /* Constants */
Christopher Haster 1:43f5c94c6771 55 static uint8_t ep_name_parameter_string[] = {'e', 'p', '='}; /* Endpoint name. A unique name for the registering node in a domain. */
Christopher Haster 1:43f5c94c6771 56 static uint8_t resource_path_ptr[] = {'r', 'd'}; /* For resource directory */
Christopher Haster 1:43f5c94c6771 57 static uint8_t resource_type_parameter[] = {'r', 't', '='}; /* Resource type. Only once for registration */
Christopher Haster 1:43f5c94c6771 58 static uint8_t obs_parameter[] = {'o', 'b', 's'}; /* Observable */
Christopher Haster 1:43f5c94c6771 59 //static uint8_t aobs_parameter[] = {'a','o','b','s',';','i','d','='}; /* Auto-observable - TBD */
Christopher Haster 1:43f5c94c6771 60 static uint8_t if_description_parameter[] = {'i', 'f', '='}; /* Interface description. Only once */
Christopher Haster 1:43f5c94c6771 61 static uint8_t ep_lifetime_parameter[] = {'l', 't', '='}; /* Lifetime. Number of seconds that this registration will be valid for. Must be updated within this time, or will be removed. */
Christopher Haster 1:43f5c94c6771 62 static uint8_t ep_domain_parameter[] = {'d', '='}; /* Domain name. If this parameter is missing, a default domain is assumed. */
Christopher Haster 1:43f5c94c6771 63 static uint8_t coap_con_type_parameter[] = {'c', 't', '='}; /* CoAP content type */
Christopher Haster 1:43f5c94c6771 64 /* * OMA BS parameters * */
Christopher Haster 1:43f5c94c6771 65 static uint8_t bs_uri[] = {'b', 's'};
Christopher Haster 1:43f5c94c6771 66 static uint8_t bs_ep_name[] = {'e', 'p', '='};
Christopher Haster 1:43f5c94c6771 67 static uint8_t et_parameter[] = {'e', 't', '='}; /* Endpoint type */
Christopher Haster 1:43f5c94c6771 68 static uint8_t bs_queue_mode[] = {'b', '='};
Christopher Haster 1:43f5c94c6771 69
Christopher Haster 1:43f5c94c6771 70 /* Function prototypes */
Christopher Haster 1:43f5c94c6771 71 static uint16_t sn_nsdl_internal_coap_send(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description);
Christopher Haster 1:43f5c94c6771 72 static void sn_nsdl_resolve_nsp_address(struct nsdl_s *handle);
Christopher Haster 1:43f5c94c6771 73 int8_t sn_nsdl_build_registration_body(struct nsdl_s *handle, sn_coap_hdr_s *message_ptr, uint8_t updating_registeration);
Christopher Haster 1:43f5c94c6771 74 static uint16_t sn_nsdl_calculate_registration_body_size(struct nsdl_s *handle, uint8_t updating_registeration);
Christopher Haster 1:43f5c94c6771 75 static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type);
Christopher Haster 1:43f5c94c6771 76 static int8_t sn_nsdl_fill_uri_query_options(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type);
Christopher Haster 1:43f5c94c6771 77 static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr);
Christopher Haster 1:43f5c94c6771 78 static int8_t sn_nsdl_resolve_ep_information(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 79 static uint8_t sn_nsdl_itoa_len(uint8_t value);
Christopher Haster 1:43f5c94c6771 80 static uint8_t *sn_nsdl_itoa(uint8_t *ptr, uint8_t value);
Christopher Haster 1:43f5c94c6771 81 static int32_t sn_nsdl_atoi(uint8_t *ptr, uint8_t len);
Christopher Haster 1:43f5c94c6771 82 static uint32_t sn_nsdl_ahextoi(uint8_t *ptr, uint8_t len);
Christopher Haster 1:43f5c94c6771 83 static int8_t sn_nsdl_resolve_lwm2m_address(struct nsdl_s *handle, uint8_t *uri, uint16_t uri_len);
Christopher Haster 1:43f5c94c6771 84 static int8_t sn_nsdl_process_oma_tlv(struct nsdl_s *handle, uint8_t *data_ptr, uint16_t data_len);
Christopher Haster 1:43f5c94c6771 85 static void sn_nsdl_check_oma_bs_status(struct nsdl_s *handle);
Christopher Haster 1:43f5c94c6771 86 static int8_t sn_nsdl_create_oma_device_object_base(struct nsdl_s *handle, sn_nsdl_oma_device_t *oma_device_setup_ptr, sn_nsdl_oma_binding_and_mode_t binding_and_mode);
Christopher Haster 1:43f5c94c6771 87 static int8_t set_endpoint_info(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *endpoint_info_ptr);
Christopher Haster 1:43f5c94c6771 88 static bool validateParameters(sn_nsdl_ep_parameters_s *parameter_ptr);
Christopher Haster 1:43f5c94c6771 89 static bool validate(uint8_t* ptr, uint32_t len, char illegalChar);
Christopher Haster 1:43f5c94c6771 90
Christopher Haster 1:43f5c94c6771 91 int8_t sn_nsdl_destroy(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 92 {
Christopher Haster 1:43f5c94c6771 93 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 94 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 95 }
Christopher Haster 1:43f5c94c6771 96
Christopher Haster 1:43f5c94c6771 97 if (handle->ep_information_ptr) {
Christopher Haster 1:43f5c94c6771 98 if (handle->ep_information_ptr->endpoint_name_ptr) {
Christopher Haster 1:43f5c94c6771 99 handle->sn_nsdl_free(handle->ep_information_ptr->endpoint_name_ptr);
Christopher Haster 1:43f5c94c6771 100 handle->ep_information_ptr->endpoint_name_ptr = 0;
Christopher Haster 1:43f5c94c6771 101 }
Christopher Haster 1:43f5c94c6771 102 if (handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 103 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
Christopher Haster 1:43f5c94c6771 104 handle->ep_information_ptr->domain_name_ptr = 0;
Christopher Haster 1:43f5c94c6771 105 handle->ep_information_ptr->domain_name_len = 0;
Christopher Haster 1:43f5c94c6771 106 }
Christopher Haster 1:43f5c94c6771 107 if (handle->ep_information_ptr->location_ptr) {
Christopher Haster 1:43f5c94c6771 108 handle->sn_nsdl_free(handle->ep_information_ptr->location_ptr);
Christopher Haster 1:43f5c94c6771 109 handle->ep_information_ptr->location_ptr = 0;
Christopher Haster 1:43f5c94c6771 110 handle->ep_information_ptr->location_len = 0;
Christopher Haster 1:43f5c94c6771 111 }
Christopher Haster 1:43f5c94c6771 112 if (handle->ep_information_ptr->type_ptr) {
Christopher Haster 1:43f5c94c6771 113 handle->sn_nsdl_free(handle->ep_information_ptr->type_ptr);
Christopher Haster 1:43f5c94c6771 114 handle->ep_information_ptr->type_ptr = 0;
Christopher Haster 1:43f5c94c6771 115 }
Christopher Haster 1:43f5c94c6771 116
Christopher Haster 1:43f5c94c6771 117 if (handle->ep_information_ptr->lifetime_ptr)
Christopher Haster 1:43f5c94c6771 118
Christopher Haster 1:43f5c94c6771 119 {
Christopher Haster 1:43f5c94c6771 120 handle->sn_nsdl_free(handle->ep_information_ptr->lifetime_ptr);
Christopher Haster 1:43f5c94c6771 121 handle->ep_information_ptr->lifetime_ptr = 0;
Christopher Haster 1:43f5c94c6771 122 }
Christopher Haster 1:43f5c94c6771 123
Christopher Haster 1:43f5c94c6771 124 handle->sn_nsdl_free(handle->ep_information_ptr);
Christopher Haster 1:43f5c94c6771 125 handle->ep_information_ptr = 0;
Christopher Haster 1:43f5c94c6771 126 }
Christopher Haster 1:43f5c94c6771 127
Christopher Haster 1:43f5c94c6771 128 if (handle->nsp_address_ptr) {
Christopher Haster 1:43f5c94c6771 129 if (handle->nsp_address_ptr->omalw_address_ptr) {
Christopher Haster 1:43f5c94c6771 130 if (handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 131 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 132 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = 0;
Christopher Haster 1:43f5c94c6771 133 }
Christopher Haster 1:43f5c94c6771 134 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr);
Christopher Haster 1:43f5c94c6771 135 }
Christopher Haster 1:43f5c94c6771 136
Christopher Haster 1:43f5c94c6771 137 handle->sn_nsdl_free(handle->nsp_address_ptr);
Christopher Haster 1:43f5c94c6771 138 handle->nsp_address_ptr = 0;
Christopher Haster 1:43f5c94c6771 139 }
Christopher Haster 1:43f5c94c6771 140
Christopher Haster 1:43f5c94c6771 141 if (handle->oma_bs_address_ptr) {
Christopher Haster 1:43f5c94c6771 142 handle->sn_nsdl_free(handle->oma_bs_address_ptr);
Christopher Haster 1:43f5c94c6771 143 }
Christopher Haster 1:43f5c94c6771 144
Christopher Haster 1:43f5c94c6771 145 /* Destroy also libCoap and grs part of libNsdl */
Christopher Haster 1:43f5c94c6771 146 sn_coap_protocol_destroy(handle->grs->coap);
Christopher Haster 1:43f5c94c6771 147 sn_grs_destroy(handle->grs);
Christopher Haster 1:43f5c94c6771 148 handle->sn_nsdl_free(handle);
Christopher Haster 1:43f5c94c6771 149
Christopher Haster 1:43f5c94c6771 150 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 151 }
Christopher Haster 1:43f5c94c6771 152
Christopher Haster 1:43f5c94c6771 153 struct nsdl_s *sn_nsdl_init(uint8_t (*sn_nsdl_tx_cb)(struct nsdl_s *, sn_nsdl_capab_e , uint8_t *, uint16_t, sn_nsdl_addr_s *),
Christopher Haster 1:43f5c94c6771 154 uint8_t (*sn_nsdl_rx_cb)(struct nsdl_s *, sn_coap_hdr_s *, sn_nsdl_addr_s *),
Christopher Haster 1:43f5c94c6771 155 void *(*sn_nsdl_alloc)(uint16_t), void (*sn_nsdl_free)(void *))
Christopher Haster 1:43f5c94c6771 156 {
Christopher Haster 1:43f5c94c6771 157 /* Check pointers and define function pointers */
Christopher Haster 1:43f5c94c6771 158 if (!sn_nsdl_alloc || !sn_nsdl_free || !sn_nsdl_tx_cb || !sn_nsdl_rx_cb) {
Christopher Haster 1:43f5c94c6771 159 return NULL;
Christopher Haster 1:43f5c94c6771 160 }
Christopher Haster 1:43f5c94c6771 161
Christopher Haster 1:43f5c94c6771 162 struct nsdl_s *handle = NULL;
Christopher Haster 1:43f5c94c6771 163
Christopher Haster 1:43f5c94c6771 164 handle = sn_nsdl_alloc(sizeof(struct nsdl_s));
Christopher Haster 1:43f5c94c6771 165
Christopher Haster 1:43f5c94c6771 166 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 167 return NULL;
Christopher Haster 1:43f5c94c6771 168 }
Christopher Haster 1:43f5c94c6771 169
Christopher Haster 1:43f5c94c6771 170 memset(handle, 0, sizeof(struct nsdl_s));
Christopher Haster 1:43f5c94c6771 171
Christopher Haster 1:43f5c94c6771 172 /* Define function pointers */
Christopher Haster 1:43f5c94c6771 173 handle->sn_nsdl_alloc = sn_nsdl_alloc;
Christopher Haster 1:43f5c94c6771 174 handle->sn_nsdl_free = sn_nsdl_free;
Christopher Haster 1:43f5c94c6771 175
Christopher Haster 1:43f5c94c6771 176 handle->sn_nsdl_tx_callback = sn_nsdl_tx_cb;
Christopher Haster 1:43f5c94c6771 177 handle->sn_nsdl_rx_callback = sn_nsdl_rx_cb;
Christopher Haster 1:43f5c94c6771 178
Christopher Haster 1:43f5c94c6771 179 /* Initialize ep parameters struct */
Christopher Haster 1:43f5c94c6771 180 if (!handle->ep_information_ptr) {
Christopher Haster 1:43f5c94c6771 181 handle->ep_information_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_ep_parameters_s));
Christopher Haster 1:43f5c94c6771 182 if (!handle->ep_information_ptr) {
Christopher Haster 1:43f5c94c6771 183 sn_nsdl_free(handle);
Christopher Haster 1:43f5c94c6771 184 return NULL;
Christopher Haster 1:43f5c94c6771 185 }
Christopher Haster 1:43f5c94c6771 186 memset(handle->ep_information_ptr, 0, sizeof(sn_nsdl_ep_parameters_s));
Christopher Haster 1:43f5c94c6771 187 }
Christopher Haster 1:43f5c94c6771 188
Christopher Haster 1:43f5c94c6771 189 handle->grs = sn_grs_init(sn_nsdl_tx_cb, &sn_nsdl_local_rx_function, sn_nsdl_alloc, sn_nsdl_free);
Christopher Haster 1:43f5c94c6771 190
Christopher Haster 1:43f5c94c6771 191 /* Initialize GRS */
Christopher Haster 1:43f5c94c6771 192 if (handle->grs == NULL) {
Christopher Haster 1:43f5c94c6771 193 handle->sn_nsdl_free(handle->ep_information_ptr);
Christopher Haster 1:43f5c94c6771 194 handle->ep_information_ptr = 0;
Christopher Haster 1:43f5c94c6771 195 sn_nsdl_free(handle);
Christopher Haster 1:43f5c94c6771 196 return NULL;
Christopher Haster 1:43f5c94c6771 197 }
Christopher Haster 1:43f5c94c6771 198
Christopher Haster 1:43f5c94c6771 199 sn_nsdl_resolve_nsp_address(handle);
Christopher Haster 1:43f5c94c6771 200
Christopher Haster 1:43f5c94c6771 201 handle->sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_NOT_REGISTERED;
Christopher Haster 1:43f5c94c6771 202
Christopher Haster 1:43f5c94c6771 203 return handle;
Christopher Haster 1:43f5c94c6771 204 }
Christopher Haster 1:43f5c94c6771 205
Christopher Haster 1:43f5c94c6771 206 uint16_t sn_nsdl_register_endpoint(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *endpoint_info_ptr)
Christopher Haster 1:43f5c94c6771 207 {
Christopher Haster 1:43f5c94c6771 208 /* Local variables */
Christopher Haster 1:43f5c94c6771 209 sn_coap_hdr_s *register_message_ptr;
Christopher Haster 1:43f5c94c6771 210 uint16_t message_id = 0;
Christopher Haster 1:43f5c94c6771 211
Christopher Haster 1:43f5c94c6771 212 if (endpoint_info_ptr == NULL || handle == NULL) {
Christopher Haster 1:43f5c94c6771 213 return 0;
Christopher Haster 1:43f5c94c6771 214 }
Christopher Haster 1:43f5c94c6771 215
Christopher Haster 1:43f5c94c6771 216 /*** Build endpoint register message ***/
Christopher Haster 1:43f5c94c6771 217
Christopher Haster 1:43f5c94c6771 218 /* Allocate memory for header struct */
Christopher Haster 1:43f5c94c6771 219 register_message_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 220 if (register_message_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 221 return 0;
Christopher Haster 1:43f5c94c6771 222 }
Christopher Haster 1:43f5c94c6771 223
Christopher Haster 1:43f5c94c6771 224 memset(register_message_ptr, 0, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 225
Christopher Haster 1:43f5c94c6771 226 /* Fill message fields -> confirmable post to specified NSP path */
Christopher Haster 1:43f5c94c6771 227 register_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
Christopher Haster 1:43f5c94c6771 228 register_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST;
Christopher Haster 1:43f5c94c6771 229
Christopher Haster 1:43f5c94c6771 230 /* Allocate memory for the extended options list */
Christopher Haster 1:43f5c94c6771 231 register_message_ptr->options_list_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 232 if (register_message_ptr->options_list_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 233 handle->sn_nsdl_free(register_message_ptr);
Christopher Haster 1:43f5c94c6771 234 register_message_ptr = 0;
Christopher Haster 1:43f5c94c6771 235 return 0;
Christopher Haster 1:43f5c94c6771 236 }
Christopher Haster 1:43f5c94c6771 237
Christopher Haster 1:43f5c94c6771 238 memset(register_message_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 239
Christopher Haster 1:43f5c94c6771 240 register_message_ptr->uri_path_len = sizeof(resource_path_ptr);
Christopher Haster 1:43f5c94c6771 241 register_message_ptr->uri_path_ptr = resource_path_ptr;
Christopher Haster 1:43f5c94c6771 242
Christopher Haster 1:43f5c94c6771 243 /* Fill Uri-query options */
Christopher Haster 1:43f5c94c6771 244 if( SN_NSDL_FAILURE == sn_nsdl_fill_uri_query_options(handle, endpoint_info_ptr,
Christopher Haster 1:43f5c94c6771 245 register_message_ptr, SN_NSDL_EP_REGISTER_MESSAGE) ){
Christopher Haster 1:43f5c94c6771 246 register_message_ptr->uri_path_ptr = NULL;
Christopher Haster 1:43f5c94c6771 247 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 248 return 0;
Christopher Haster 1:43f5c94c6771 249 }
Christopher Haster 1:43f5c94c6771 250
Christopher Haster 1:43f5c94c6771 251 if (endpoint_info_ptr->ds_register_mode == REGISTER_WITH_RESOURCES) {
Christopher Haster 1:43f5c94c6771 252 /* Built body for message */
Christopher Haster 1:43f5c94c6771 253 if (sn_nsdl_build_registration_body(handle, register_message_ptr, 0) == SN_NSDL_FAILURE) {
Christopher Haster 1:43f5c94c6771 254 register_message_ptr->uri_path_ptr = NULL;
Christopher Haster 1:43f5c94c6771 255 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
Christopher Haster 1:43f5c94c6771 256 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 257 return 0;
Christopher Haster 1:43f5c94c6771 258 }
Christopher Haster 1:43f5c94c6771 259 }
Christopher Haster 1:43f5c94c6771 260
Christopher Haster 1:43f5c94c6771 261 /* Clean (possible) existing and save new endpoint info to handle */
Christopher Haster 1:43f5c94c6771 262 if (set_endpoint_info(handle, endpoint_info_ptr) == -1) {
Christopher Haster 1:43f5c94c6771 263 if (register_message_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 264 handle->sn_nsdl_free(register_message_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 265 register_message_ptr->payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 266 }
Christopher Haster 1:43f5c94c6771 267
Christopher Haster 1:43f5c94c6771 268 register_message_ptr->uri_path_ptr = NULL;
Christopher Haster 1:43f5c94c6771 269 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
Christopher Haster 1:43f5c94c6771 270
Christopher Haster 1:43f5c94c6771 271 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 272
Christopher Haster 1:43f5c94c6771 273 return 0;
Christopher Haster 1:43f5c94c6771 274 }
Christopher Haster 1:43f5c94c6771 275
Christopher Haster 1:43f5c94c6771 276 /* Build and send coap message to NSP */
Christopher Haster 1:43f5c94c6771 277 message_id = sn_nsdl_internal_coap_send(handle, register_message_ptr, handle->nsp_address_ptr->omalw_address_ptr, SN_NSDL_MSG_REGISTER);
Christopher Haster 1:43f5c94c6771 278
Christopher Haster 1:43f5c94c6771 279 if (register_message_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 280 handle->sn_nsdl_free(register_message_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 281 register_message_ptr->payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 282 }
Christopher Haster 1:43f5c94c6771 283
Christopher Haster 1:43f5c94c6771 284 register_message_ptr->uri_path_ptr = NULL;
Christopher Haster 1:43f5c94c6771 285 register_message_ptr->options_list_ptr->uri_host_ptr = NULL;
Christopher Haster 1:43f5c94c6771 286
Christopher Haster 1:43f5c94c6771 287 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 288
Christopher Haster 1:43f5c94c6771 289 return message_id;
Christopher Haster 1:43f5c94c6771 290 }
Christopher Haster 1:43f5c94c6771 291
Christopher Haster 1:43f5c94c6771 292 uint16_t sn_nsdl_unregister_endpoint(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 293 {
Christopher Haster 1:43f5c94c6771 294 /* Local variables */
Christopher Haster 1:43f5c94c6771 295 sn_coap_hdr_s *unregister_message_ptr;
Christopher Haster 1:43f5c94c6771 296 uint8_t *temp_ptr = 0;
Christopher Haster 1:43f5c94c6771 297 uint16_t message_id = 0;
Christopher Haster 1:43f5c94c6771 298
Christopher Haster 1:43f5c94c6771 299 /* Check parameters */
Christopher Haster 1:43f5c94c6771 300 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 301 return 0;
Christopher Haster 1:43f5c94c6771 302 }
Christopher Haster 1:43f5c94c6771 303
Christopher Haster 1:43f5c94c6771 304 /* Check that EP have been registered */
Christopher Haster 1:43f5c94c6771 305 if (sn_nsdl_is_ep_registered(handle)) {
Christopher Haster 1:43f5c94c6771 306
Christopher Haster 1:43f5c94c6771 307 /* Memory allocation for unregister message */
Christopher Haster 1:43f5c94c6771 308 unregister_message_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 309 if (!unregister_message_ptr) {
Christopher Haster 1:43f5c94c6771 310 return 0;
Christopher Haster 1:43f5c94c6771 311 }
Christopher Haster 1:43f5c94c6771 312
Christopher Haster 1:43f5c94c6771 313 memset(unregister_message_ptr, 0, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 314
Christopher Haster 1:43f5c94c6771 315 /* Fill unregister message */
Christopher Haster 1:43f5c94c6771 316 unregister_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
Christopher Haster 1:43f5c94c6771 317 unregister_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_DELETE;
Christopher Haster 1:43f5c94c6771 318
Christopher Haster 1:43f5c94c6771 319 if(handle->ep_information_ptr->location_ptr) {
Christopher Haster 1:43f5c94c6771 320 unregister_message_ptr->uri_path_len = handle->ep_information_ptr->location_len;
Christopher Haster 1:43f5c94c6771 321 unregister_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(unregister_message_ptr->uri_path_len);
Christopher Haster 1:43f5c94c6771 322 if (!unregister_message_ptr->uri_path_ptr) {
Christopher Haster 1:43f5c94c6771 323 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, unregister_message_ptr);
Christopher Haster 1:43f5c94c6771 324 return 0;
Christopher Haster 1:43f5c94c6771 325 }
Christopher Haster 1:43f5c94c6771 326
Christopher Haster 1:43f5c94c6771 327 temp_ptr = unregister_message_ptr->uri_path_ptr;
Christopher Haster 1:43f5c94c6771 328
Christopher Haster 1:43f5c94c6771 329 memcpy(temp_ptr , handle->ep_information_ptr->location_ptr, handle->ep_information_ptr->location_len);
Christopher Haster 1:43f5c94c6771 330 } else {
Christopher Haster 1:43f5c94c6771 331 unregister_message_ptr->uri_path_len = (RESOURCE_DIR_LEN + 1 + handle->ep_information_ptr->domain_name_len + 1 + handle->ep_information_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 332 unregister_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(unregister_message_ptr->uri_path_len);
Christopher Haster 1:43f5c94c6771 333 if (!unregister_message_ptr->uri_path_ptr) {
Christopher Haster 1:43f5c94c6771 334 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, unregister_message_ptr);
Christopher Haster 1:43f5c94c6771 335 return 0;
Christopher Haster 1:43f5c94c6771 336 }
Christopher Haster 1:43f5c94c6771 337
Christopher Haster 1:43f5c94c6771 338 temp_ptr = unregister_message_ptr->uri_path_ptr;
Christopher Haster 1:43f5c94c6771 339
Christopher Haster 1:43f5c94c6771 340 memcpy(temp_ptr, resource_path_ptr, RESOURCE_DIR_LEN);
Christopher Haster 1:43f5c94c6771 341 temp_ptr += RESOURCE_DIR_LEN;
Christopher Haster 1:43f5c94c6771 342
Christopher Haster 1:43f5c94c6771 343 *temp_ptr++ = '/';
Christopher Haster 1:43f5c94c6771 344
Christopher Haster 1:43f5c94c6771 345 memcpy(temp_ptr , handle->ep_information_ptr->domain_name_ptr, handle->ep_information_ptr->domain_name_len);
Christopher Haster 1:43f5c94c6771 346 temp_ptr += handle->ep_information_ptr->domain_name_len;
Christopher Haster 1:43f5c94c6771 347
Christopher Haster 1:43f5c94c6771 348 *temp_ptr++ = '/';
Christopher Haster 1:43f5c94c6771 349
Christopher Haster 1:43f5c94c6771 350 memcpy(temp_ptr , handle->ep_information_ptr->endpoint_name_ptr, handle->ep_information_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 351 }
Christopher Haster 1:43f5c94c6771 352
Christopher Haster 1:43f5c94c6771 353 /* Send message */
Christopher Haster 1:43f5c94c6771 354 message_id = sn_nsdl_internal_coap_send(handle, unregister_message_ptr, handle->nsp_address_ptr->omalw_address_ptr, SN_NSDL_MSG_UNREGISTER);
Christopher Haster 1:43f5c94c6771 355
Christopher Haster 1:43f5c94c6771 356 /* Free memory */
Christopher Haster 1:43f5c94c6771 357 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, unregister_message_ptr);
Christopher Haster 1:43f5c94c6771 358
Christopher Haster 1:43f5c94c6771 359 }
Christopher Haster 1:43f5c94c6771 360
Christopher Haster 1:43f5c94c6771 361 return message_id;
Christopher Haster 1:43f5c94c6771 362 }
Christopher Haster 1:43f5c94c6771 363
Christopher Haster 1:43f5c94c6771 364 uint16_t sn_nsdl_update_registration(struct nsdl_s *handle, uint8_t *lt_ptr, uint8_t lt_len)
Christopher Haster 1:43f5c94c6771 365 {
Christopher Haster 1:43f5c94c6771 366 /* Local variables */
Christopher Haster 1:43f5c94c6771 367 sn_coap_hdr_s *register_message_ptr;
Christopher Haster 1:43f5c94c6771 368 uint8_t *temp_ptr;
Christopher Haster 1:43f5c94c6771 369 sn_nsdl_ep_parameters_s temp_parameters;
Christopher Haster 1:43f5c94c6771 370 uint16_t message_id = 0;
Christopher Haster 1:43f5c94c6771 371
Christopher Haster 1:43f5c94c6771 372 /* Check parameters */
Christopher Haster 1:43f5c94c6771 373 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 374 return 0;
Christopher Haster 1:43f5c94c6771 375 }
Christopher Haster 1:43f5c94c6771 376
Christopher Haster 1:43f5c94c6771 377 if (!sn_nsdl_is_ep_registered(handle)){
Christopher Haster 1:43f5c94c6771 378 return 0;
Christopher Haster 1:43f5c94c6771 379 }
Christopher Haster 1:43f5c94c6771 380
Christopher Haster 1:43f5c94c6771 381 memset(&temp_parameters, 0, sizeof(sn_nsdl_ep_parameters_s));
Christopher Haster 1:43f5c94c6771 382
Christopher Haster 1:43f5c94c6771 383 temp_parameters.lifetime_len = lt_len;
Christopher Haster 1:43f5c94c6771 384 temp_parameters.lifetime_ptr = lt_ptr;
Christopher Haster 1:43f5c94c6771 385
Christopher Haster 1:43f5c94c6771 386 /*** Build endpoint register update message ***/
Christopher Haster 1:43f5c94c6771 387
Christopher Haster 1:43f5c94c6771 388 /* Allocate memory for header struct */
Christopher Haster 1:43f5c94c6771 389 register_message_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 390 if (register_message_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 391 return 0;
Christopher Haster 1:43f5c94c6771 392 }
Christopher Haster 1:43f5c94c6771 393
Christopher Haster 1:43f5c94c6771 394 memset(register_message_ptr, 0, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 395
Christopher Haster 1:43f5c94c6771 396 /* Fill message fields -> confirmable post to specified NSP path */
Christopher Haster 1:43f5c94c6771 397 register_message_ptr->msg_type = COAP_MSG_TYPE_CONFIRMABLE;
Christopher Haster 1:43f5c94c6771 398 register_message_ptr->msg_code = COAP_MSG_CODE_REQUEST_POST;
Christopher Haster 1:43f5c94c6771 399
Christopher Haster 1:43f5c94c6771 400 if(handle->ep_information_ptr->location_ptr) {
Christopher Haster 1:43f5c94c6771 401 register_message_ptr->uri_path_len = handle->ep_information_ptr->location_len; /* = Only location set by Device Server*/
Christopher Haster 1:43f5c94c6771 402
Christopher Haster 1:43f5c94c6771 403 register_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(register_message_ptr->uri_path_len);
Christopher Haster 1:43f5c94c6771 404 if (!register_message_ptr->uri_path_ptr) {
Christopher Haster 1:43f5c94c6771 405 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 406 return 0;
Christopher Haster 1:43f5c94c6771 407 }
Christopher Haster 1:43f5c94c6771 408
Christopher Haster 1:43f5c94c6771 409 temp_ptr = register_message_ptr->uri_path_ptr;
Christopher Haster 1:43f5c94c6771 410
Christopher Haster 1:43f5c94c6771 411 /* location */
Christopher Haster 1:43f5c94c6771 412 memcpy(temp_ptr, handle->ep_information_ptr->location_ptr, handle->ep_information_ptr->location_len);
Christopher Haster 1:43f5c94c6771 413 } else {
Christopher Haster 1:43f5c94c6771 414 register_message_ptr->uri_path_len = sizeof(resource_path_ptr) + handle->ep_information_ptr->domain_name_len + handle->ep_information_ptr->endpoint_name_len + 2; /* = rd/domain/endpoint */
Christopher Haster 1:43f5c94c6771 415
Christopher Haster 1:43f5c94c6771 416 register_message_ptr->uri_path_ptr = handle->sn_nsdl_alloc(register_message_ptr->uri_path_len);
Christopher Haster 1:43f5c94c6771 417 if (!register_message_ptr->uri_path_ptr) {
Christopher Haster 1:43f5c94c6771 418 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 419 return 0;
Christopher Haster 1:43f5c94c6771 420 }
Christopher Haster 1:43f5c94c6771 421
Christopher Haster 1:43f5c94c6771 422 temp_ptr = register_message_ptr->uri_path_ptr;
Christopher Haster 1:43f5c94c6771 423
Christopher Haster 1:43f5c94c6771 424 /* rd/ */
Christopher Haster 1:43f5c94c6771 425 memcpy(temp_ptr, resource_path_ptr, sizeof(resource_path_ptr));
Christopher Haster 1:43f5c94c6771 426 temp_ptr += sizeof(resource_path_ptr);
Christopher Haster 1:43f5c94c6771 427 *temp_ptr++ = '/';
Christopher Haster 1:43f5c94c6771 428
Christopher Haster 1:43f5c94c6771 429 /* rd/DOMAIN/ */
Christopher Haster 1:43f5c94c6771 430 memcpy(temp_ptr, handle->ep_information_ptr->domain_name_ptr, handle->ep_information_ptr->domain_name_len);
Christopher Haster 1:43f5c94c6771 431 temp_ptr += handle->ep_information_ptr->domain_name_len;
Christopher Haster 1:43f5c94c6771 432 *temp_ptr++ = '/';
Christopher Haster 1:43f5c94c6771 433
Christopher Haster 1:43f5c94c6771 434 /* rd/domain/ENDPOINT */
Christopher Haster 1:43f5c94c6771 435 memcpy(temp_ptr, handle->ep_information_ptr->endpoint_name_ptr, handle->ep_information_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 436 }
Christopher Haster 1:43f5c94c6771 437
Christopher Haster 1:43f5c94c6771 438 /* Allocate memory for the extended options list */
Christopher Haster 1:43f5c94c6771 439 register_message_ptr->options_list_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 440 if (register_message_ptr->options_list_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 441 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 442 return 0;
Christopher Haster 1:43f5c94c6771 443 }
Christopher Haster 1:43f5c94c6771 444
Christopher Haster 1:43f5c94c6771 445 memset(register_message_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 446
Christopher Haster 1:43f5c94c6771 447 /* Fill Uri-query options */
Christopher Haster 1:43f5c94c6771 448 sn_nsdl_fill_uri_query_options(handle, &temp_parameters, register_message_ptr, SN_NSDL_EP_UPDATE_MESSAGE);
Christopher Haster 1:43f5c94c6771 449
Christopher Haster 1:43f5c94c6771 450 /* Build payload */
Christopher Haster 1:43f5c94c6771 451 if (handle->ep_information_ptr->ds_register_mode == REGISTER_WITH_RESOURCES) {
Christopher Haster 1:43f5c94c6771 452
Christopher Haster 1:43f5c94c6771 453 if (sn_nsdl_build_registration_body(handle, register_message_ptr, 1) == SN_NSDL_FAILURE) {
Christopher Haster 1:43f5c94c6771 454 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 455 return 0;
Christopher Haster 1:43f5c94c6771 456 }
Christopher Haster 1:43f5c94c6771 457 }
Christopher Haster 1:43f5c94c6771 458
Christopher Haster 1:43f5c94c6771 459 /* Build and send coap message to NSP */
Christopher Haster 1:43f5c94c6771 460 message_id = sn_nsdl_internal_coap_send(handle, register_message_ptr, handle->nsp_address_ptr->omalw_address_ptr, SN_NSDL_MSG_UPDATE);
Christopher Haster 1:43f5c94c6771 461
Christopher Haster 1:43f5c94c6771 462 if (register_message_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 463 handle->sn_nsdl_free(register_message_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 464 }
Christopher Haster 1:43f5c94c6771 465 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, register_message_ptr);
Christopher Haster 1:43f5c94c6771 466
Christopher Haster 1:43f5c94c6771 467 return message_id;
Christopher Haster 1:43f5c94c6771 468 }
Christopher Haster 1:43f5c94c6771 469
Christopher Haster 1:43f5c94c6771 470 int8_t sn_nsdl_set_endpoint_location(struct nsdl_s *handle, uint8_t *location_ptr, uint8_t location_len)
Christopher Haster 1:43f5c94c6771 471 {
Christopher Haster 1:43f5c94c6771 472 if(!handle || !location_ptr || (location_len == 0)) {
Christopher Haster 1:43f5c94c6771 473 return -1;
Christopher Haster 1:43f5c94c6771 474 }
Christopher Haster 1:43f5c94c6771 475 handle->ep_information_ptr->location_ptr = handle->sn_nsdl_alloc(location_len);
Christopher Haster 1:43f5c94c6771 476 memcpy(handle->ep_information_ptr->location_ptr, location_ptr, location_len);
Christopher Haster 1:43f5c94c6771 477 handle->ep_information_ptr->location_len = location_len;
Christopher Haster 1:43f5c94c6771 478
Christopher Haster 1:43f5c94c6771 479 return 0;
Christopher Haster 1:43f5c94c6771 480 }
Christopher Haster 1:43f5c94c6771 481
Christopher Haster 1:43f5c94c6771 482 void sn_nsdl_nsp_lost(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 483 {
Christopher Haster 1:43f5c94c6771 484 /* Check parameters */
Christopher Haster 1:43f5c94c6771 485 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 486 return;
Christopher Haster 1:43f5c94c6771 487 }
Christopher Haster 1:43f5c94c6771 488
Christopher Haster 1:43f5c94c6771 489 handle->sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_NOT_REGISTERED;
Christopher Haster 1:43f5c94c6771 490 }
Christopher Haster 1:43f5c94c6771 491
Christopher Haster 1:43f5c94c6771 492 int8_t sn_nsdl_is_ep_registered(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 493 {
Christopher Haster 1:43f5c94c6771 494 /* Check parameters */
Christopher Haster 1:43f5c94c6771 495 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 496 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 497 }
Christopher Haster 1:43f5c94c6771 498
Christopher Haster 1:43f5c94c6771 499 return handle->sn_nsdl_endpoint_registered;
Christopher Haster 1:43f5c94c6771 500 }
Christopher Haster 1:43f5c94c6771 501
Christopher Haster 1:43f5c94c6771 502 uint16_t sn_nsdl_send_observation_notification(struct nsdl_s *handle, uint8_t *token_ptr, uint8_t token_len,
Christopher Haster 1:43f5c94c6771 503 uint8_t *payload_ptr, uint16_t payload_len,
Christopher Haster 1:43f5c94c6771 504 uint8_t *observe_ptr, uint8_t observe_len,
Christopher Haster 1:43f5c94c6771 505 sn_coap_msg_type_e message_type, uint8_t content_type)
Christopher Haster 1:43f5c94c6771 506 {
Christopher Haster 1:43f5c94c6771 507 sn_coap_hdr_s *notification_message_ptr;
Christopher Haster 1:43f5c94c6771 508 uint16_t return_msg_id = 0;
Christopher Haster 1:43f5c94c6771 509
Christopher Haster 1:43f5c94c6771 510 /* Check parameters */
Christopher Haster 1:43f5c94c6771 511 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 512 return 0;
Christopher Haster 1:43f5c94c6771 513 }
Christopher Haster 1:43f5c94c6771 514
Christopher Haster 1:43f5c94c6771 515 /* Allocate and initialize memory for header struct */
Christopher Haster 1:43f5c94c6771 516 notification_message_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 517 if (notification_message_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 518 return 0;
Christopher Haster 1:43f5c94c6771 519 }
Christopher Haster 1:43f5c94c6771 520
Christopher Haster 1:43f5c94c6771 521 memset(notification_message_ptr, 0, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 522
Christopher Haster 1:43f5c94c6771 523 notification_message_ptr->options_list_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 524 if (notification_message_ptr->options_list_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 525 handle->sn_nsdl_free(notification_message_ptr);
Christopher Haster 1:43f5c94c6771 526 return 0;
Christopher Haster 1:43f5c94c6771 527 }
Christopher Haster 1:43f5c94c6771 528
Christopher Haster 1:43f5c94c6771 529 memset(notification_message_ptr->options_list_ptr , 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 530
Christopher Haster 1:43f5c94c6771 531 /* Fill header */
Christopher Haster 1:43f5c94c6771 532 notification_message_ptr->msg_type = message_type;
Christopher Haster 1:43f5c94c6771 533 notification_message_ptr->msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
Christopher Haster 1:43f5c94c6771 534
Christopher Haster 1:43f5c94c6771 535 /* Fill token */
Christopher Haster 1:43f5c94c6771 536 notification_message_ptr->token_len = token_len;
Christopher Haster 1:43f5c94c6771 537 notification_message_ptr->token_ptr = token_ptr;
Christopher Haster 1:43f5c94c6771 538
Christopher Haster 1:43f5c94c6771 539 /* Fill payload */
Christopher Haster 1:43f5c94c6771 540 notification_message_ptr->payload_len = payload_len;
Christopher Haster 1:43f5c94c6771 541 notification_message_ptr->payload_ptr = payload_ptr;
Christopher Haster 1:43f5c94c6771 542
Christopher Haster 1:43f5c94c6771 543 /* Fill observe */
Christopher Haster 1:43f5c94c6771 544 notification_message_ptr->options_list_ptr->observe_len = observe_len;
Christopher Haster 1:43f5c94c6771 545 notification_message_ptr->options_list_ptr->observe_ptr = observe_ptr;
Christopher Haster 1:43f5c94c6771 546
Christopher Haster 1:43f5c94c6771 547 /* Fill content type */
Christopher Haster 1:43f5c94c6771 548 if (content_type) {
Christopher Haster 1:43f5c94c6771 549 notification_message_ptr->content_type_len = 1;
Christopher Haster 1:43f5c94c6771 550 notification_message_ptr->content_type_ptr = &content_type;
Christopher Haster 1:43f5c94c6771 551 }
Christopher Haster 1:43f5c94c6771 552
Christopher Haster 1:43f5c94c6771 553 /* Send message */
Christopher Haster 1:43f5c94c6771 554 if (sn_nsdl_send_coap_message(handle, handle->nsp_address_ptr->omalw_address_ptr, notification_message_ptr) == SN_NSDL_FAILURE) {
Christopher Haster 1:43f5c94c6771 555 return_msg_id = 0;
Christopher Haster 1:43f5c94c6771 556 } else {
Christopher Haster 1:43f5c94c6771 557 return_msg_id = notification_message_ptr->msg_id;
Christopher Haster 1:43f5c94c6771 558 }
Christopher Haster 1:43f5c94c6771 559
Christopher Haster 1:43f5c94c6771 560 /* Free memory */
Christopher Haster 1:43f5c94c6771 561
Christopher Haster 1:43f5c94c6771 562 notification_message_ptr->payload_ptr = NULL;
Christopher Haster 1:43f5c94c6771 563 notification_message_ptr->options_list_ptr->observe_ptr = NULL;
Christopher Haster 1:43f5c94c6771 564 notification_message_ptr->token_ptr = NULL;
Christopher Haster 1:43f5c94c6771 565 notification_message_ptr->content_type_ptr = NULL;
Christopher Haster 1:43f5c94c6771 566
Christopher Haster 1:43f5c94c6771 567 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, notification_message_ptr);
Christopher Haster 1:43f5c94c6771 568
Christopher Haster 1:43f5c94c6771 569 return return_msg_id;
Christopher Haster 1:43f5c94c6771 570 }
Christopher Haster 1:43f5c94c6771 571
Christopher Haster 1:43f5c94c6771 572 /* * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 573 /* ~ OMA functions ~ */
Christopher Haster 1:43f5c94c6771 574 /* * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 575
Christopher Haster 1:43f5c94c6771 576 uint16_t sn_nsdl_oma_bootstrap(struct nsdl_s *handle, sn_nsdl_addr_s *bootstrap_address_ptr, sn_nsdl_ep_parameters_s *endpoint_info_ptr, sn_nsdl_bs_ep_info_t *bootstrap_endpoint_info_ptr)
Christopher Haster 1:43f5c94c6771 577 {
Christopher Haster 1:43f5c94c6771 578
Christopher Haster 1:43f5c94c6771 579 /* Local variables */
Christopher Haster 1:43f5c94c6771 580 sn_coap_hdr_s bootstrap_coap_header;
Christopher Haster 1:43f5c94c6771 581 uint8_t *uri_query_tmp_ptr;
Christopher Haster 1:43f5c94c6771 582 uint16_t message_id = 0;
Christopher Haster 1:43f5c94c6771 583
Christopher Haster 1:43f5c94c6771 584 /* Check parameters */
Christopher Haster 1:43f5c94c6771 585 if (!bootstrap_address_ptr || !bootstrap_endpoint_info_ptr || !endpoint_info_ptr || !handle) {
Christopher Haster 1:43f5c94c6771 586 return 0;
Christopher Haster 1:43f5c94c6771 587 }
Christopher Haster 1:43f5c94c6771 588
Christopher Haster 1:43f5c94c6771 589 /* Create device object */
Christopher Haster 1:43f5c94c6771 590 if (sn_nsdl_create_oma_device_object_base(handle, bootstrap_endpoint_info_ptr->device_object, endpoint_info_ptr->binding_and_mode) < 0) {
Christopher Haster 1:43f5c94c6771 591 return 0;
Christopher Haster 1:43f5c94c6771 592 }
Christopher Haster 1:43f5c94c6771 593
Christopher Haster 1:43f5c94c6771 594 handle->sn_nsdl_oma_bs_done_cb = bootstrap_endpoint_info_ptr->oma_bs_status_cb;
Christopher Haster 1:43f5c94c6771 595
Christopher Haster 1:43f5c94c6771 596 /* Init CoAP header struct */
Christopher Haster 1:43f5c94c6771 597 memset(&bootstrap_coap_header, 0, sizeof(sn_coap_hdr_s));
Christopher Haster 1:43f5c94c6771 598
Christopher Haster 1:43f5c94c6771 599 bootstrap_coap_header.options_list_ptr = handle->sn_nsdl_alloc(sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 600 if (!bootstrap_coap_header.options_list_ptr) {
Christopher Haster 1:43f5c94c6771 601 return 0;
Christopher Haster 1:43f5c94c6771 602 }
Christopher Haster 1:43f5c94c6771 603
Christopher Haster 1:43f5c94c6771 604 memset(bootstrap_coap_header.options_list_ptr, 0, sizeof(sn_coap_options_list_s));
Christopher Haster 1:43f5c94c6771 605
Christopher Haster 1:43f5c94c6771 606 /* Build bootstrap start message */
Christopher Haster 1:43f5c94c6771 607 bootstrap_coap_header.msg_code = COAP_MSG_CODE_REQUEST_POST;
Christopher Haster 1:43f5c94c6771 608 bootstrap_coap_header.msg_type = COAP_MSG_TYPE_CONFIRMABLE;
Christopher Haster 1:43f5c94c6771 609
Christopher Haster 1:43f5c94c6771 610 bootstrap_coap_header.uri_path_ptr = bs_uri;
Christopher Haster 1:43f5c94c6771 611 bootstrap_coap_header.uri_path_len = sizeof(bs_uri);
Christopher Haster 1:43f5c94c6771 612
Christopher Haster 1:43f5c94c6771 613 uri_query_tmp_ptr = handle->sn_nsdl_alloc(endpoint_info_ptr->endpoint_name_len + BS_EP_PARAMETER_LEN);
Christopher Haster 1:43f5c94c6771 614 if (!uri_query_tmp_ptr) {
Christopher Haster 1:43f5c94c6771 615 handle->sn_nsdl_free(bootstrap_coap_header.options_list_ptr);
Christopher Haster 1:43f5c94c6771 616 return 0;
Christopher Haster 1:43f5c94c6771 617 }
Christopher Haster 1:43f5c94c6771 618
Christopher Haster 1:43f5c94c6771 619 memcpy(uri_query_tmp_ptr, bs_ep_name, BS_EP_PARAMETER_LEN);
Christopher Haster 1:43f5c94c6771 620 memcpy((uri_query_tmp_ptr + BS_EP_PARAMETER_LEN), endpoint_info_ptr->endpoint_name_ptr, endpoint_info_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 621
Christopher Haster 1:43f5c94c6771 622 bootstrap_coap_header.options_list_ptr->uri_query_len = endpoint_info_ptr->endpoint_name_len + BS_EP_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 623 bootstrap_coap_header.options_list_ptr->uri_query_ptr = uri_query_tmp_ptr;
Christopher Haster 1:43f5c94c6771 624
Christopher Haster 1:43f5c94c6771 625 /* Save bootstrap server address */
Christopher Haster 1:43f5c94c6771 626 handle->oma_bs_address_len = bootstrap_address_ptr->addr_len; /* Length.. */
Christopher Haster 1:43f5c94c6771 627 handle->oma_bs_address_ptr = handle->sn_nsdl_alloc(handle->oma_bs_address_len); /* Address.. */
Christopher Haster 1:43f5c94c6771 628 if (!handle->oma_bs_address_ptr) {
Christopher Haster 1:43f5c94c6771 629 handle->sn_nsdl_free(bootstrap_coap_header.options_list_ptr);
Christopher Haster 1:43f5c94c6771 630 handle->sn_nsdl_free(uri_query_tmp_ptr);
Christopher Haster 1:43f5c94c6771 631 return 0;
Christopher Haster 1:43f5c94c6771 632 }
Christopher Haster 1:43f5c94c6771 633 memcpy(handle->oma_bs_address_ptr, bootstrap_address_ptr->addr_ptr, handle->oma_bs_address_len);
Christopher Haster 1:43f5c94c6771 634 handle->oma_bs_port = bootstrap_address_ptr->port; /* And port */
Christopher Haster 1:43f5c94c6771 635
Christopher Haster 1:43f5c94c6771 636 /* Send message */
Christopher Haster 1:43f5c94c6771 637 message_id = sn_nsdl_internal_coap_send(handle, &bootstrap_coap_header, bootstrap_address_ptr, SN_NSDL_MSG_UNDEFINED);
Christopher Haster 1:43f5c94c6771 638
Christopher Haster 1:43f5c94c6771 639 /* Free allocated memory */
Christopher Haster 1:43f5c94c6771 640 handle->sn_nsdl_free(uri_query_tmp_ptr);
Christopher Haster 1:43f5c94c6771 641 handle->sn_nsdl_free(bootstrap_coap_header.options_list_ptr);
Christopher Haster 1:43f5c94c6771 642
Christopher Haster 1:43f5c94c6771 643 return message_id;
Christopher Haster 1:43f5c94c6771 644 }
Christopher Haster 1:43f5c94c6771 645
Christopher Haster 1:43f5c94c6771 646 omalw_certificate_list_t *sn_nsdl_get_certificates(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 647 {
Christopher Haster 1:43f5c94c6771 648 sn_nsdl_resource_info_s *resource_ptr = 0;;
Christopher Haster 1:43f5c94c6771 649 omalw_certificate_list_t *certi_list_ptr = 0;
Christopher Haster 1:43f5c94c6771 650
Christopher Haster 1:43f5c94c6771 651 /* Check parameters */
Christopher Haster 1:43f5c94c6771 652 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 653 return NULL;
Christopher Haster 1:43f5c94c6771 654 }
Christopher Haster 1:43f5c94c6771 655
Christopher Haster 1:43f5c94c6771 656 certi_list_ptr = handle->sn_nsdl_alloc(sizeof(omalw_certificate_list_t));
Christopher Haster 1:43f5c94c6771 657
Christopher Haster 1:43f5c94c6771 658 if (!certi_list_ptr) {
Christopher Haster 1:43f5c94c6771 659 return NULL;
Christopher Haster 1:43f5c94c6771 660 }
Christopher Haster 1:43f5c94c6771 661
Christopher Haster 1:43f5c94c6771 662 /* Get private key resource */
Christopher Haster 1:43f5c94c6771 663 resource_ptr = sn_nsdl_get_resource(handle, 5, (void *)"0/0/5");
Christopher Haster 1:43f5c94c6771 664 if (!resource_ptr) {
Christopher Haster 1:43f5c94c6771 665 handle->sn_nsdl_free(certi_list_ptr);
Christopher Haster 1:43f5c94c6771 666 return NULL;
Christopher Haster 1:43f5c94c6771 667 }
Christopher Haster 1:43f5c94c6771 668 certi_list_ptr->own_private_key_ptr = resource_ptr->resource;
Christopher Haster 1:43f5c94c6771 669 certi_list_ptr->own_private_key_len = resource_ptr->resourcelen;
Christopher Haster 1:43f5c94c6771 670
Christopher Haster 1:43f5c94c6771 671 /* Get client certificate resource */
Christopher Haster 1:43f5c94c6771 672 resource_ptr = sn_nsdl_get_resource(handle, 5, (void *)"0/0/4");
Christopher Haster 1:43f5c94c6771 673 if (!resource_ptr) {
Christopher Haster 1:43f5c94c6771 674 handle->sn_nsdl_free(certi_list_ptr);
Christopher Haster 1:43f5c94c6771 675 return NULL;
Christopher Haster 1:43f5c94c6771 676 }
Christopher Haster 1:43f5c94c6771 677 certi_list_ptr->certificate_ptr[0] = resource_ptr->resource;
Christopher Haster 1:43f5c94c6771 678 certi_list_ptr->certificate_len[0] = resource_ptr->resourcelen;
Christopher Haster 1:43f5c94c6771 679
Christopher Haster 1:43f5c94c6771 680 /* Get root certificate resource */
Christopher Haster 1:43f5c94c6771 681 resource_ptr = sn_nsdl_get_resource(handle, 5, (void *)"0/0/3");
Christopher Haster 1:43f5c94c6771 682 if (!resource_ptr) {
Christopher Haster 1:43f5c94c6771 683 handle->sn_nsdl_free(certi_list_ptr);
Christopher Haster 1:43f5c94c6771 684 return NULL;
Christopher Haster 1:43f5c94c6771 685 }
Christopher Haster 1:43f5c94c6771 686 certi_list_ptr->certificate_ptr[1] = resource_ptr->resource;
Christopher Haster 1:43f5c94c6771 687 certi_list_ptr->certificate_len[1] = resource_ptr->resourcelen;
Christopher Haster 1:43f5c94c6771 688
Christopher Haster 1:43f5c94c6771 689 /* return filled list */
Christopher Haster 1:43f5c94c6771 690 return certi_list_ptr;
Christopher Haster 1:43f5c94c6771 691
Christopher Haster 1:43f5c94c6771 692 }
Christopher Haster 1:43f5c94c6771 693
Christopher Haster 1:43f5c94c6771 694 int8_t sn_nsdl_update_certificates(struct nsdl_s *handle, omalw_certificate_list_t *certificate_ptr, uint8_t certificate_chain)
Christopher Haster 1:43f5c94c6771 695 {
Christopher Haster 1:43f5c94c6771 696 (void)certificate_chain;
Christopher Haster 1:43f5c94c6771 697
Christopher Haster 1:43f5c94c6771 698 /* Check pointers */
Christopher Haster 1:43f5c94c6771 699 if (!certificate_ptr || !handle) {
Christopher Haster 1:43f5c94c6771 700 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 701 }
Christopher Haster 1:43f5c94c6771 702
Christopher Haster 1:43f5c94c6771 703 sn_nsdl_resource_info_s *resource_ptr = 0;;
Christopher Haster 1:43f5c94c6771 704
Christopher Haster 1:43f5c94c6771 705 /* Get private key resource */
Christopher Haster 1:43f5c94c6771 706 resource_ptr = sn_nsdl_get_resource(handle, 5, (void *)"0/0/5");
Christopher Haster 1:43f5c94c6771 707 if (!resource_ptr) {
Christopher Haster 1:43f5c94c6771 708 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 709 }
Christopher Haster 1:43f5c94c6771 710 handle->sn_nsdl_free(resource_ptr->resource);
Christopher Haster 1:43f5c94c6771 711 resource_ptr->resource = certificate_ptr->own_private_key_ptr;
Christopher Haster 1:43f5c94c6771 712 resource_ptr->resourcelen = certificate_ptr->own_private_key_len;
Christopher Haster 1:43f5c94c6771 713
Christopher Haster 1:43f5c94c6771 714 /* Get client certificate resource */
Christopher Haster 1:43f5c94c6771 715 resource_ptr = sn_nsdl_get_resource(handle, 5, (void *)"0/0/4");
Christopher Haster 1:43f5c94c6771 716 if (!resource_ptr) {
Christopher Haster 1:43f5c94c6771 717 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 718 }
Christopher Haster 1:43f5c94c6771 719 handle->sn_nsdl_free(resource_ptr->resource);
Christopher Haster 1:43f5c94c6771 720 resource_ptr->resource = certificate_ptr->certificate_ptr[0];
Christopher Haster 1:43f5c94c6771 721 resource_ptr->resourcelen = certificate_ptr->certificate_len[0];
Christopher Haster 1:43f5c94c6771 722
Christopher Haster 1:43f5c94c6771 723 /* Get root certificate resource */
Christopher Haster 1:43f5c94c6771 724 resource_ptr = sn_nsdl_get_resource(handle, 5, (void *)"0/0/3");
Christopher Haster 1:43f5c94c6771 725 if (!resource_ptr) {
Christopher Haster 1:43f5c94c6771 726 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 727 }
Christopher Haster 1:43f5c94c6771 728 handle->sn_nsdl_free(resource_ptr->resource);
Christopher Haster 1:43f5c94c6771 729 resource_ptr->resource = certificate_ptr->certificate_ptr[1];
Christopher Haster 1:43f5c94c6771 730 resource_ptr->resourcelen = certificate_ptr->certificate_len[1];
Christopher Haster 1:43f5c94c6771 731
Christopher Haster 1:43f5c94c6771 732 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 733 }
Christopher Haster 1:43f5c94c6771 734
Christopher Haster 1:43f5c94c6771 735 int8_t sn_nsdl_create_oma_device_object(struct nsdl_s *handle, sn_nsdl_oma_device_t *device_object_ptr)
Christopher Haster 1:43f5c94c6771 736 {
Christopher Haster 1:43f5c94c6771 737 sn_nsdl_resource_info_s *resource_temp = 0;
Christopher Haster 1:43f5c94c6771 738 uint8_t path[8] = "3/0/11/0";
Christopher Haster 1:43f5c94c6771 739
Christopher Haster 1:43f5c94c6771 740 if (!device_object_ptr || !handle) {
Christopher Haster 1:43f5c94c6771 741 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 742 }
Christopher Haster 1:43f5c94c6771 743
Christopher Haster 1:43f5c94c6771 744 /* * Error code * */
Christopher Haster 1:43f5c94c6771 745
Christopher Haster 1:43f5c94c6771 746 /* Get first error message */
Christopher Haster 1:43f5c94c6771 747 resource_temp = sn_grs_search_resource(handle->grs, 8, path, SN_GRS_SEARCH_METHOD);
Christopher Haster 1:43f5c94c6771 748
Christopher Haster 1:43f5c94c6771 749 while (resource_temp) {
Christopher Haster 1:43f5c94c6771 750 if (resource_temp->resource) {
Christopher Haster 1:43f5c94c6771 751 /* If no error code set */
Christopher Haster 1:43f5c94c6771 752 if (*resource_temp->resource == 0) {
Christopher Haster 1:43f5c94c6771 753 /* Set error code */
Christopher Haster 1:43f5c94c6771 754 *resource_temp->resource = (uint8_t)device_object_ptr->error_code;
Christopher Haster 1:43f5c94c6771 755 resource_temp->resourcelen = 1;
Christopher Haster 1:43f5c94c6771 756
Christopher Haster 1:43f5c94c6771 757 sn_nsdl_update_resource(handle, resource_temp);
Christopher Haster 1:43f5c94c6771 758 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 759 }
Christopher Haster 1:43f5c94c6771 760 break;
Christopher Haster 1:43f5c94c6771 761 }
Christopher Haster 1:43f5c94c6771 762
Christopher Haster 1:43f5c94c6771 763 if (path[7] == '9') {
Christopher Haster 1:43f5c94c6771 764 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 765 }
Christopher Haster 1:43f5c94c6771 766
Christopher Haster 1:43f5c94c6771 767 path[7]++;
Christopher Haster 1:43f5c94c6771 768 resource_temp = sn_grs_search_resource(handle->grs, 8, path, SN_GRS_SEARCH_METHOD);
Christopher Haster 1:43f5c94c6771 769 }
Christopher Haster 1:43f5c94c6771 770
Christopher Haster 1:43f5c94c6771 771 /* Create new resource for this error */
Christopher Haster 1:43f5c94c6771 772 resource_temp = handle->sn_nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
Christopher Haster 1:43f5c94c6771 773 if (!resource_temp) {
Christopher Haster 1:43f5c94c6771 774 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 775 }
Christopher Haster 1:43f5c94c6771 776
Christopher Haster 1:43f5c94c6771 777 memset(resource_temp, 0, sizeof(sn_nsdl_resource_info_s));
Christopher Haster 1:43f5c94c6771 778
Christopher Haster 1:43f5c94c6771 779 resource_temp->access = SN_GRS_GET_ALLOWED;
Christopher Haster 1:43f5c94c6771 780 resource_temp->mode = SN_GRS_DYNAMIC;
Christopher Haster 1:43f5c94c6771 781
Christopher Haster 1:43f5c94c6771 782 resource_temp->path = path;
Christopher Haster 1:43f5c94c6771 783 resource_temp->pathlen = 8;
Christopher Haster 1:43f5c94c6771 784
Christopher Haster 1:43f5c94c6771 785 resource_temp->resource = handle->sn_nsdl_alloc(1);
Christopher Haster 1:43f5c94c6771 786 if (!resource_temp->resource) {
Christopher Haster 1:43f5c94c6771 787 handle->sn_nsdl_free(resource_temp);
Christopher Haster 1:43f5c94c6771 788 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 789 }
Christopher Haster 1:43f5c94c6771 790
Christopher Haster 1:43f5c94c6771 791 *resource_temp->resource = (uint8_t)device_object_ptr->error_code;
Christopher Haster 1:43f5c94c6771 792 resource_temp->resourcelen = 1;
Christopher Haster 1:43f5c94c6771 793
Christopher Haster 1:43f5c94c6771 794 resource_temp->resource_parameters_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
Christopher Haster 1:43f5c94c6771 795
Christopher Haster 1:43f5c94c6771 796 if (!resource_temp->resource_parameters_ptr) {
Christopher Haster 1:43f5c94c6771 797 handle->sn_nsdl_free(resource_temp->resource);
Christopher Haster 1:43f5c94c6771 798 handle->sn_nsdl_free(resource_temp);
Christopher Haster 1:43f5c94c6771 799
Christopher Haster 1:43f5c94c6771 800 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 801 }
Christopher Haster 1:43f5c94c6771 802
Christopher Haster 1:43f5c94c6771 803 memset(resource_temp->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
Christopher Haster 1:43f5c94c6771 804
Christopher Haster 1:43f5c94c6771 805 sn_nsdl_create_resource(handle, resource_temp);
Christopher Haster 1:43f5c94c6771 806
Christopher Haster 1:43f5c94c6771 807 handle->sn_nsdl_free(resource_temp->resource);
Christopher Haster 1:43f5c94c6771 808 handle->sn_nsdl_free(resource_temp->resource_parameters_ptr);
Christopher Haster 1:43f5c94c6771 809 handle->sn_nsdl_free(resource_temp);
Christopher Haster 1:43f5c94c6771 810
Christopher Haster 1:43f5c94c6771 811 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 812 }
Christopher Haster 1:43f5c94c6771 813
Christopher Haster 1:43f5c94c6771 814 char *sn_nsdl_get_version(void)
Christopher Haster 1:43f5c94c6771 815 {
Christopher Haster 1:43f5c94c6771 816 #if defined(YOTTA_MBED_CLIENT_C_VERSION_STRING)
Christopher Haster 1:43f5c94c6771 817 return YOTTA_MBED_CLIENT_C_VERSION_STRING;
Christopher Haster 1:43f5c94c6771 818 #elif defined(VERSION)
Christopher Haster 1:43f5c94c6771 819 return VERSION;
Christopher Haster 1:43f5c94c6771 820 #else
Christopher Haster 1:43f5c94c6771 821 return "0.0.0";
Christopher Haster 1:43f5c94c6771 822 #endif
Christopher Haster 1:43f5c94c6771 823 }
Christopher Haster 1:43f5c94c6771 824
Christopher Haster 1:43f5c94c6771 825
Christopher Haster 1:43f5c94c6771 826 int8_t sn_nsdl_process_coap(struct nsdl_s *handle, uint8_t *packet_ptr, uint16_t packet_len, sn_nsdl_addr_s *src_ptr)
Christopher Haster 1:43f5c94c6771 827 {
Christopher Haster 1:43f5c94c6771 828 sn_coap_hdr_s *coap_packet_ptr = NULL;
Christopher Haster 1:43f5c94c6771 829 sn_coap_hdr_s *coap_response_ptr = NULL;
Christopher Haster 1:43f5c94c6771 830
Christopher Haster 1:43f5c94c6771 831 /* Check parameters */
Christopher Haster 1:43f5c94c6771 832 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 833 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 834 }
Christopher Haster 1:43f5c94c6771 835
Christopher Haster 1:43f5c94c6771 836 /* Parse CoAP packet */
Christopher Haster 1:43f5c94c6771 837 coap_packet_ptr = sn_coap_protocol_parse(handle->grs->coap, src_ptr, packet_len, packet_ptr, (void *)handle);
Christopher Haster 1:43f5c94c6771 838
Christopher Haster 1:43f5c94c6771 839 /* Check if parsing was successfull */
Christopher Haster 1:43f5c94c6771 840 if (coap_packet_ptr == (sn_coap_hdr_s *)NULL) {
Christopher Haster 1:43f5c94c6771 841 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 842 }
Christopher Haster 1:43f5c94c6771 843
Christopher Haster 1:43f5c94c6771 844 /* Check, if coap itself sends response, or block receiving is ongoing... */
Christopher Haster 1:43f5c94c6771 845 if (coap_packet_ptr->coap_status != COAP_STATUS_OK && coap_packet_ptr->coap_status != COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED) {
Christopher Haster 1:43f5c94c6771 846 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 847 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 848 }
Christopher Haster 1:43f5c94c6771 849
Christopher Haster 1:43f5c94c6771 850 /* If proxy options added, return not supported */
Christopher Haster 1:43f5c94c6771 851 if (coap_packet_ptr->options_list_ptr) {
Christopher Haster 1:43f5c94c6771 852 if (coap_packet_ptr->options_list_ptr->proxy_uri_len) {
Christopher Haster 1:43f5c94c6771 853 coap_response_ptr = sn_coap_build_response(handle->grs->coap, coap_packet_ptr, COAP_MSG_CODE_RESPONSE_PROXYING_NOT_SUPPORTED);
Christopher Haster 1:43f5c94c6771 854 if (coap_response_ptr) {
Christopher Haster 1:43f5c94c6771 855 sn_nsdl_send_coap_message(handle, src_ptr, coap_response_ptr);
Christopher Haster 1:43f5c94c6771 856 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_response_ptr);
Christopher Haster 1:43f5c94c6771 857 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 858 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 859 } else {
Christopher Haster 1:43f5c94c6771 860 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 861 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 862 }
Christopher Haster 1:43f5c94c6771 863 }
Christopher Haster 1:43f5c94c6771 864 }
Christopher Haster 1:43f5c94c6771 865
Christopher Haster 1:43f5c94c6771 866 /* * * * * * * * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 867 /* If message is response message, call RX callback */
Christopher Haster 1:43f5c94c6771 868 /* * * * * * * * * * * * * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 869
Christopher Haster 1:43f5c94c6771 870 if ((coap_packet_ptr->msg_code > COAP_MSG_CODE_REQUEST_DELETE) || (coap_packet_ptr->msg_type == COAP_MSG_TYPE_ACKNOWLEDGEMENT)) {
Christopher Haster 1:43f5c94c6771 871 int8_t retval = sn_nsdl_local_rx_function(handle, coap_packet_ptr, src_ptr);
Christopher Haster 1:43f5c94c6771 872 if (coap_packet_ptr->coap_status == COAP_STATUS_PARSER_BLOCKWISE_MSG_RECEIVED && coap_packet_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 873 handle->sn_nsdl_free(coap_packet_ptr->payload_ptr);
Christopher Haster 1:43f5c94c6771 874 coap_packet_ptr->payload_ptr = 0;
Christopher Haster 1:43f5c94c6771 875 }
Christopher Haster 1:43f5c94c6771 876 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 877 return retval;
Christopher Haster 1:43f5c94c6771 878 }
Christopher Haster 1:43f5c94c6771 879
Christopher Haster 1:43f5c94c6771 880 /* * If OMA bootstrap message... * */
Christopher Haster 1:43f5c94c6771 881 if (src_ptr && (handle->oma_bs_address_len == src_ptr->addr_len) && (handle->oma_bs_port == src_ptr->port) && !memcmp(handle->oma_bs_address_ptr, src_ptr->addr_ptr, handle->oma_bs_address_len)) {
Christopher Haster 1:43f5c94c6771 882 /* TLV message. Parse message and check status of the OMA bootstrap */
Christopher Haster 1:43f5c94c6771 883 /* process. If ok, call cb function and return. Otherwise send error */
Christopher Haster 1:43f5c94c6771 884 /* and return failure. */
Christopher Haster 1:43f5c94c6771 885
Christopher Haster 1:43f5c94c6771 886 if (coap_packet_ptr->content_type_len == 1) { //todo check message type
Christopher Haster 1:43f5c94c6771 887 if (*coap_packet_ptr->content_type_ptr == 99) {
Christopher Haster 1:43f5c94c6771 888 /* TLV parsing failed. Send response to get non-tlv messages */
Christopher Haster 1:43f5c94c6771 889 if (sn_nsdl_process_oma_tlv(handle, coap_packet_ptr->payload_ptr, coap_packet_ptr->payload_len) == SN_NSDL_FAILURE) {
Christopher Haster 1:43f5c94c6771 890 coap_response_ptr = sn_coap_build_response(handle->grs->coap, coap_packet_ptr, COAP_MSG_CODE_RESPONSE_NOT_ACCEPTABLE);
Christopher Haster 1:43f5c94c6771 891 if (coap_response_ptr) {
Christopher Haster 1:43f5c94c6771 892 sn_nsdl_send_coap_message(handle, src_ptr, coap_response_ptr);
Christopher Haster 1:43f5c94c6771 893 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_response_ptr);
Christopher Haster 1:43f5c94c6771 894 } else {
Christopher Haster 1:43f5c94c6771 895 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 896 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 897 }
Christopher Haster 1:43f5c94c6771 898 }
Christopher Haster 1:43f5c94c6771 899 /* Success TLV parsing */
Christopher Haster 1:43f5c94c6771 900 else {
Christopher Haster 1:43f5c94c6771 901 coap_response_ptr = sn_coap_build_response(handle->grs->coap, coap_packet_ptr, COAP_MSG_CODE_RESPONSE_CREATED);
Christopher Haster 1:43f5c94c6771 902 if (coap_response_ptr) {
Christopher Haster 1:43f5c94c6771 903 sn_nsdl_send_coap_message(handle, src_ptr, coap_response_ptr);
Christopher Haster 1:43f5c94c6771 904 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_response_ptr);
Christopher Haster 1:43f5c94c6771 905
Christopher Haster 1:43f5c94c6771 906 } else {
Christopher Haster 1:43f5c94c6771 907 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 908 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 909 }
Christopher Haster 1:43f5c94c6771 910 sn_nsdl_check_oma_bs_status(handle);
Christopher Haster 1:43f5c94c6771 911 }
Christopher Haster 1:43f5c94c6771 912
Christopher Haster 1:43f5c94c6771 913 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 914 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 915 }
Christopher Haster 1:43f5c94c6771 916
Christopher Haster 1:43f5c94c6771 917 /* Non - TLV message */
Christopher Haster 1:43f5c94c6771 918 else if (*coap_packet_ptr->content_type_ptr == 97) {
Christopher Haster 1:43f5c94c6771 919 sn_grs_process_coap(handle, coap_packet_ptr, src_ptr);
Christopher Haster 1:43f5c94c6771 920
Christopher Haster 1:43f5c94c6771 921 /* Todo: move this copying to sn_nsdl_check_oma_bs_status(), also from TLV parser */
Christopher Haster 1:43f5c94c6771 922 /* Security mode */
Christopher Haster 1:43f5c94c6771 923 if (*(coap_packet_ptr->uri_path_ptr + (coap_packet_ptr->uri_path_len - 1)) == '2') {
Christopher Haster 1:43f5c94c6771 924 handle->nsp_address_ptr->omalw_server_security = (omalw_server_security_t)sn_nsdl_atoi(coap_packet_ptr->payload_ptr, coap_packet_ptr->payload_len);
Christopher Haster 1:43f5c94c6771 925 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 926 }
Christopher Haster 1:43f5c94c6771 927
Christopher Haster 1:43f5c94c6771 928 /* NSP address */
Christopher Haster 1:43f5c94c6771 929 else if (*(coap_packet_ptr->uri_path_ptr + (coap_packet_ptr->uri_path_len - 1)) == '0') {
Christopher Haster 1:43f5c94c6771 930 sn_nsdl_resolve_lwm2m_address(handle, coap_packet_ptr->payload_ptr, coap_packet_ptr->payload_len);
Christopher Haster 1:43f5c94c6771 931 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 932 }
Christopher Haster 1:43f5c94c6771 933
Christopher Haster 1:43f5c94c6771 934 sn_nsdl_check_oma_bs_status(handle);
Christopher Haster 1:43f5c94c6771 935 } else {
Christopher Haster 1:43f5c94c6771 936 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 937 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 938 }
Christopher Haster 1:43f5c94c6771 939 } else {
Christopher Haster 1:43f5c94c6771 940 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, coap_packet_ptr);
Christopher Haster 1:43f5c94c6771 941 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 942 }
Christopher Haster 1:43f5c94c6771 943
Christopher Haster 1:43f5c94c6771 944
Christopher Haster 1:43f5c94c6771 945 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 946 }
Christopher Haster 1:43f5c94c6771 947
Christopher Haster 1:43f5c94c6771 948
Christopher Haster 1:43f5c94c6771 949 /* * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 950 /* Other messages are for GRS */
Christopher Haster 1:43f5c94c6771 951 /* * * * * * * * * * * * * * * */
Christopher Haster 1:43f5c94c6771 952
Christopher Haster 1:43f5c94c6771 953 return sn_grs_process_coap(handle, coap_packet_ptr, src_ptr);
Christopher Haster 1:43f5c94c6771 954 }
Christopher Haster 1:43f5c94c6771 955
Christopher Haster 1:43f5c94c6771 956 int8_t sn_nsdl_exec(struct nsdl_s *handle, uint32_t time)
Christopher Haster 1:43f5c94c6771 957 {
Christopher Haster 1:43f5c94c6771 958 if(!handle || !handle->grs){
Christopher Haster 1:43f5c94c6771 959 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 960 }
Christopher Haster 1:43f5c94c6771 961 /* Call CoAP execution function */
Christopher Haster 1:43f5c94c6771 962 return sn_coap_protocol_exec(handle->grs->coap, time);
Christopher Haster 1:43f5c94c6771 963 }
Christopher Haster 1:43f5c94c6771 964
Christopher Haster 1:43f5c94c6771 965 sn_nsdl_resource_info_s *sn_nsdl_get_resource(struct nsdl_s *handle, uint16_t pathlen, uint8_t *path_ptr)
Christopher Haster 1:43f5c94c6771 966 {
Christopher Haster 1:43f5c94c6771 967 /* Check parameters */
Christopher Haster 1:43f5c94c6771 968 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 969 return NULL;
Christopher Haster 1:43f5c94c6771 970 }
Christopher Haster 1:43f5c94c6771 971
Christopher Haster 1:43f5c94c6771 972 return sn_grs_search_resource(handle->grs, pathlen, path_ptr, SN_GRS_SEARCH_METHOD);
Christopher Haster 1:43f5c94c6771 973 }
Christopher Haster 1:43f5c94c6771 974
Christopher Haster 1:43f5c94c6771 975
Christopher Haster 1:43f5c94c6771 976 /**
Christopher Haster 1:43f5c94c6771 977 * \fn static uint16_t sn_nsdl_internal_coap_send(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description)
Christopher Haster 1:43f5c94c6771 978 *
Christopher Haster 1:43f5c94c6771 979 *
Christopher Haster 1:43f5c94c6771 980 * \brief To send NSDL messages. Stores message id?s and message description to catch response from NSP server
Christopher Haster 1:43f5c94c6771 981 * \param *handle Pointer to nsdl-library handle
Christopher Haster 1:43f5c94c6771 982 * \param *coap_header_ptr Pointer to the CoAP message header to be sent
Christopher Haster 1:43f5c94c6771 983 * \param *dst_addr_ptr Pointer to the address structure that contains destination address information
Christopher Haster 1:43f5c94c6771 984 * \param message_description Message description to be stored to list for waiting response
Christopher Haster 1:43f5c94c6771 985 *
Christopher Haster 1:43f5c94c6771 986 * \return message id, 0 if failed
Christopher Haster 1:43f5c94c6771 987 */
Christopher Haster 1:43f5c94c6771 988 static uint16_t sn_nsdl_internal_coap_send(struct nsdl_s *handle, sn_coap_hdr_s *coap_header_ptr, sn_nsdl_addr_s *dst_addr_ptr, uint8_t message_description)
Christopher Haster 1:43f5c94c6771 989 {
Christopher Haster 1:43f5c94c6771 990 uint8_t *coap_message_ptr = NULL;
Christopher Haster 1:43f5c94c6771 991 uint16_t coap_message_len = 0;
Christopher Haster 1:43f5c94c6771 992
Christopher Haster 1:43f5c94c6771 993 coap_message_len = sn_coap_builder_calc_needed_packet_data_size(coap_header_ptr);
Christopher Haster 1:43f5c94c6771 994
Christopher Haster 1:43f5c94c6771 995 if (coap_message_len == 0) {
Christopher Haster 1:43f5c94c6771 996 return 0;
Christopher Haster 1:43f5c94c6771 997 }
Christopher Haster 1:43f5c94c6771 998
Christopher Haster 1:43f5c94c6771 999 coap_message_ptr = handle->sn_nsdl_alloc(coap_message_len);
Christopher Haster 1:43f5c94c6771 1000 if (!coap_message_ptr) {
Christopher Haster 1:43f5c94c6771 1001 return 0;
Christopher Haster 1:43f5c94c6771 1002 }
Christopher Haster 1:43f5c94c6771 1003
Christopher Haster 1:43f5c94c6771 1004 /* Build message */
Christopher Haster 1:43f5c94c6771 1005 if (sn_coap_protocol_build(handle->grs->coap, dst_addr_ptr, coap_message_ptr, coap_header_ptr, (void *)handle) < 0) {
Christopher Haster 1:43f5c94c6771 1006 handle->sn_nsdl_free(coap_message_ptr);
Christopher Haster 1:43f5c94c6771 1007 return 0;
Christopher Haster 1:43f5c94c6771 1008 }
Christopher Haster 1:43f5c94c6771 1009
Christopher Haster 1:43f5c94c6771 1010 /* If mesage type is confirmable, save it to list to wait for reply */
Christopher Haster 1:43f5c94c6771 1011 if (coap_header_ptr->msg_type == COAP_MSG_TYPE_CONFIRMABLE) {
Christopher Haster 1:43f5c94c6771 1012 if (message_description == SN_NSDL_MSG_REGISTER) {
Christopher Haster 1:43f5c94c6771 1013 handle->register_msg_id = coap_header_ptr->msg_id;
Christopher Haster 1:43f5c94c6771 1014 }
Christopher Haster 1:43f5c94c6771 1015 if (message_description == SN_NSDL_MSG_UNREGISTER) {
Christopher Haster 1:43f5c94c6771 1016 handle->unregister_msg_id = coap_header_ptr->msg_id;
Christopher Haster 1:43f5c94c6771 1017 }
Christopher Haster 1:43f5c94c6771 1018
Christopher Haster 1:43f5c94c6771 1019 }
Christopher Haster 1:43f5c94c6771 1020
Christopher Haster 1:43f5c94c6771 1021 handle->sn_nsdl_tx_callback(handle, SN_NSDL_PROTOCOL_COAP, coap_message_ptr, coap_message_len, dst_addr_ptr);
Christopher Haster 1:43f5c94c6771 1022 handle->sn_nsdl_free(coap_message_ptr);
Christopher Haster 1:43f5c94c6771 1023
Christopher Haster 1:43f5c94c6771 1024 return coap_header_ptr->msg_id;
Christopher Haster 1:43f5c94c6771 1025 }
Christopher Haster 1:43f5c94c6771 1026
Christopher Haster 1:43f5c94c6771 1027 /**
Christopher Haster 1:43f5c94c6771 1028 * \fn static void sn_nsdl_resolve_nsp_address(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 1029 *
Christopher Haster 1:43f5c94c6771 1030 * \brief Resolves NSP server address.
Christopher Haster 1:43f5c94c6771 1031 *
Christopher Haster 1:43f5c94c6771 1032 * \param *handle Pointer to nsdl-library handle
Christopher Haster 1:43f5c94c6771 1033 * \note Application must set NSP address with set_nsp_address
Christopher Haster 1:43f5c94c6771 1034 */
Christopher Haster 1:43f5c94c6771 1035 static void sn_nsdl_resolve_nsp_address(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 1036 {
Christopher Haster 1:43f5c94c6771 1037 /* Local variables */
Christopher Haster 1:43f5c94c6771 1038 if (!handle->nsp_address_ptr) {
Christopher Haster 1:43f5c94c6771 1039 //allocate only if previously not allocated
Christopher Haster 1:43f5c94c6771 1040 handle->nsp_address_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_oma_server_info_t));
Christopher Haster 1:43f5c94c6771 1041 }
Christopher Haster 1:43f5c94c6771 1042
Christopher Haster 1:43f5c94c6771 1043 if (handle->nsp_address_ptr) {
Christopher Haster 1:43f5c94c6771 1044 handle->nsp_address_ptr->omalw_server_security = SEC_NOT_SET;
Christopher Haster 1:43f5c94c6771 1045 handle->nsp_address_ptr->omalw_address_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_addr_s));
Christopher Haster 1:43f5c94c6771 1046 if (handle->nsp_address_ptr->omalw_address_ptr) {
Christopher Haster 1:43f5c94c6771 1047 memset(handle->nsp_address_ptr->omalw_address_ptr, 0, sizeof(sn_nsdl_addr_s));
Christopher Haster 1:43f5c94c6771 1048 handle->nsp_address_ptr->omalw_address_ptr->type = SN_NSDL_ADDRESS_TYPE_NONE;
Christopher Haster 1:43f5c94c6771 1049 }
Christopher Haster 1:43f5c94c6771 1050 }
Christopher Haster 1:43f5c94c6771 1051 }
Christopher Haster 1:43f5c94c6771 1052
Christopher Haster 1:43f5c94c6771 1053 static int8_t sn_nsdl_create_oma_device_object_base(struct nsdl_s *handle, sn_nsdl_oma_device_t *oma_device_setup_ptr, sn_nsdl_oma_binding_and_mode_t binding_and_mode)
Christopher Haster 1:43f5c94c6771 1054 {
Christopher Haster 1:43f5c94c6771 1055 sn_nsdl_resource_info_s new_resource;
Christopher Haster 1:43f5c94c6771 1056 uint8_t object_path[8] = "3/0/11/0";
Christopher Haster 1:43f5c94c6771 1057 uint8_t resource_temp[3];
Christopher Haster 1:43f5c94c6771 1058 uint8_t x = 0;
Christopher Haster 1:43f5c94c6771 1059
Christopher Haster 1:43f5c94c6771 1060 if (!oma_device_setup_ptr) {
Christopher Haster 1:43f5c94c6771 1061 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1062 }
Christopher Haster 1:43f5c94c6771 1063
Christopher Haster 1:43f5c94c6771 1064 /* * Create resources. * */
Christopher Haster 1:43f5c94c6771 1065
Christopher Haster 1:43f5c94c6771 1066 /* These resources can be created multiple times. */
Christopher Haster 1:43f5c94c6771 1067 memset(&new_resource, 0, sizeof(sn_nsdl_resource_info_s));
Christopher Haster 1:43f5c94c6771 1068 new_resource.resource_parameters_ptr = handle->sn_nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
Christopher Haster 1:43f5c94c6771 1069 if (!new_resource.resource_parameters_ptr) {
Christopher Haster 1:43f5c94c6771 1070 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1071 }
Christopher Haster 1:43f5c94c6771 1072
Christopher Haster 1:43f5c94c6771 1073 memset(new_resource.resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
Christopher Haster 1:43f5c94c6771 1074
Christopher Haster 1:43f5c94c6771 1075 /* Create error - resource */
Christopher Haster 1:43f5c94c6771 1076 new_resource.mode = SN_GRS_STATIC;
Christopher Haster 1:43f5c94c6771 1077 new_resource.access = SN_GRS_GET_ALLOWED;
Christopher Haster 1:43f5c94c6771 1078
Christopher Haster 1:43f5c94c6771 1079 new_resource.path = object_path;
Christopher Haster 1:43f5c94c6771 1080 new_resource.pathlen = 8;
Christopher Haster 1:43f5c94c6771 1081
Christopher Haster 1:43f5c94c6771 1082 sn_nsdl_itoa(resource_temp, (uint8_t)oma_device_setup_ptr->error_code);
Christopher Haster 1:43f5c94c6771 1083
Christopher Haster 1:43f5c94c6771 1084 new_resource.resource = resource_temp;
Christopher Haster 1:43f5c94c6771 1085 new_resource.resourcelen = 1;
Christopher Haster 1:43f5c94c6771 1086
Christopher Haster 1:43f5c94c6771 1087 if (sn_nsdl_create_resource(handle, &new_resource) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 1088 handle->sn_nsdl_free(new_resource.resource_parameters_ptr);
Christopher Haster 1:43f5c94c6771 1089 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1090 }
Christopher Haster 1:43f5c94c6771 1091
Christopher Haster 1:43f5c94c6771 1092 /* These resources can be only once, during OMA bootstrap.. */
Christopher Haster 1:43f5c94c6771 1093 /* Create supported binding and modes */
Christopher Haster 1:43f5c94c6771 1094 object_path[5] = '6';
Christopher Haster 1:43f5c94c6771 1095 new_resource.path = object_path;
Christopher Haster 1:43f5c94c6771 1096 new_resource.pathlen = 6;
Christopher Haster 1:43f5c94c6771 1097
Christopher Haster 1:43f5c94c6771 1098 if (binding_and_mode & 0x01) {
Christopher Haster 1:43f5c94c6771 1099 resource_temp[x] = 'U';
Christopher Haster 1:43f5c94c6771 1100 x++;
Christopher Haster 1:43f5c94c6771 1101 if (binding_and_mode & 0x02) {
Christopher Haster 1:43f5c94c6771 1102 resource_temp[x] = 'Q';
Christopher Haster 1:43f5c94c6771 1103 x++;
Christopher Haster 1:43f5c94c6771 1104 }
Christopher Haster 1:43f5c94c6771 1105 }
Christopher Haster 1:43f5c94c6771 1106 if (binding_and_mode & 0x04) {
Christopher Haster 1:43f5c94c6771 1107 resource_temp[x] = 'S';
Christopher Haster 1:43f5c94c6771 1108 x++;
Christopher Haster 1:43f5c94c6771 1109 if ((binding_and_mode & 0x02) && !(binding_and_mode & 0x01)) {
Christopher Haster 1:43f5c94c6771 1110 resource_temp[x] = 'Q';
Christopher Haster 1:43f5c94c6771 1111 x++;
Christopher Haster 1:43f5c94c6771 1112 }
Christopher Haster 1:43f5c94c6771 1113 }
Christopher Haster 1:43f5c94c6771 1114
Christopher Haster 1:43f5c94c6771 1115 new_resource.resourcelen = x;
Christopher Haster 1:43f5c94c6771 1116
Christopher Haster 1:43f5c94c6771 1117 if (new_resource.resourcelen) {
Christopher Haster 1:43f5c94c6771 1118 new_resource.resource = resource_temp;
Christopher Haster 1:43f5c94c6771 1119 } else {
Christopher Haster 1:43f5c94c6771 1120 new_resource.resource = 0;
Christopher Haster 1:43f5c94c6771 1121 }
Christopher Haster 1:43f5c94c6771 1122
Christopher Haster 1:43f5c94c6771 1123
Christopher Haster 1:43f5c94c6771 1124 if (sn_nsdl_create_resource(handle, &new_resource) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 1125 handle->sn_nsdl_free(new_resource.resource_parameters_ptr);
Christopher Haster 1:43f5c94c6771 1126 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1127 }
Christopher Haster 1:43f5c94c6771 1128
Christopher Haster 1:43f5c94c6771 1129
Christopher Haster 1:43f5c94c6771 1130 /* Create dynamic reboot object */
Christopher Haster 1:43f5c94c6771 1131 new_resource.mode = SN_GRS_DYNAMIC;
Christopher Haster 1:43f5c94c6771 1132
Christopher Haster 1:43f5c94c6771 1133 new_resource.access = SN_GRS_POST_ALLOWED;
Christopher Haster 1:43f5c94c6771 1134
Christopher Haster 1:43f5c94c6771 1135 object_path[4] = '4';
Christopher Haster 1:43f5c94c6771 1136
Christopher Haster 1:43f5c94c6771 1137 new_resource.path = object_path;
Christopher Haster 1:43f5c94c6771 1138 new_resource.pathlen = 5;
Christopher Haster 1:43f5c94c6771 1139
Christopher Haster 1:43f5c94c6771 1140 new_resource.resourcelen = 0;
Christopher Haster 1:43f5c94c6771 1141 new_resource.resource = 0;
Christopher Haster 1:43f5c94c6771 1142
Christopher Haster 1:43f5c94c6771 1143 new_resource.sn_grs_dyn_res_callback = oma_device_setup_ptr->sn_oma_device_boot_callback;
Christopher Haster 1:43f5c94c6771 1144
Christopher Haster 1:43f5c94c6771 1145 if (sn_nsdl_create_resource(handle, &new_resource) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 1146 handle->sn_nsdl_free(new_resource.resource_parameters_ptr);
Christopher Haster 1:43f5c94c6771 1147 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1148 }
Christopher Haster 1:43f5c94c6771 1149
Christopher Haster 1:43f5c94c6771 1150 handle->sn_nsdl_free(new_resource.resource_parameters_ptr);
Christopher Haster 1:43f5c94c6771 1151 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 1152 }
Christopher Haster 1:43f5c94c6771 1153
Christopher Haster 1:43f5c94c6771 1154 /**
Christopher Haster 1:43f5c94c6771 1155 * \fn int8_t sn_nsdl_build_registration_body(struct nsdl_s *handle, sn_coap_hdr_s *message_ptr, uint8_t updating_registeration)
Christopher Haster 1:43f5c94c6771 1156 *
Christopher Haster 1:43f5c94c6771 1157 * \brief To build GRS resources to registration message payload
Christopher Haster 1:43f5c94c6771 1158 * \param *handle Pointer to nsdl-library handle
Christopher Haster 1:43f5c94c6771 1159 * \param *message_ptr Pointer to CoAP message header
Christopher Haster 1:43f5c94c6771 1160 *
Christopher Haster 1:43f5c94c6771 1161 * \return SN_NSDL_SUCCESS = 0, Failed = -1
Christopher Haster 1:43f5c94c6771 1162 */
Christopher Haster 1:43f5c94c6771 1163 int8_t sn_nsdl_build_registration_body(struct nsdl_s *handle, sn_coap_hdr_s *message_ptr, uint8_t updating_registeration)
Christopher Haster 1:43f5c94c6771 1164 {
Christopher Haster 1:43f5c94c6771 1165 /* Local variables */
Christopher Haster 1:43f5c94c6771 1166 uint8_t *temp_ptr;
Christopher Haster 1:43f5c94c6771 1167 const sn_nsdl_resource_info_s *resource_temp_ptr;
Christopher Haster 1:43f5c94c6771 1168
Christopher Haster 1:43f5c94c6771 1169
Christopher Haster 1:43f5c94c6771 1170 /* Calculate needed memory and allocate */
Christopher Haster 1:43f5c94c6771 1171 message_ptr->payload_len = sn_nsdl_calculate_registration_body_size(handle, updating_registeration);
Christopher Haster 1:43f5c94c6771 1172
Christopher Haster 1:43f5c94c6771 1173 /* If no resources to be registered, return SN_NSDL_SUCCESS */
Christopher Haster 1:43f5c94c6771 1174 if (!message_ptr->payload_len) {
Christopher Haster 1:43f5c94c6771 1175 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 1176 }
Christopher Haster 1:43f5c94c6771 1177
Christopher Haster 1:43f5c94c6771 1178 message_ptr->payload_ptr = handle->sn_nsdl_alloc(message_ptr->payload_len);
Christopher Haster 1:43f5c94c6771 1179 if (!message_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 1180 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1181 }
Christopher Haster 1:43f5c94c6771 1182
Christopher Haster 1:43f5c94c6771 1183 /* Build message */
Christopher Haster 1:43f5c94c6771 1184 temp_ptr = message_ptr->payload_ptr;
Christopher Haster 1:43f5c94c6771 1185
Christopher Haster 1:43f5c94c6771 1186 resource_temp_ptr = sn_grs_get_first_resource(handle->grs);
Christopher Haster 1:43f5c94c6771 1187
Christopher Haster 1:43f5c94c6771 1188 /* Loop trough all resources */
Christopher Haster 1:43f5c94c6771 1189 while (resource_temp_ptr) {
Christopher Haster 1:43f5c94c6771 1190 /* if resource needs to be registered */
Christopher Haster 1:43f5c94c6771 1191 if (resource_temp_ptr->resource_parameters_ptr) {
Christopher Haster 1:43f5c94c6771 1192 if (updating_registeration && resource_temp_ptr->resource_parameters_ptr->registered == SN_NDSL_RESOURCE_REGISTERED) {
Christopher Haster 1:43f5c94c6771 1193 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
Christopher Haster 1:43f5c94c6771 1194 continue;
Christopher Haster 1:43f5c94c6771 1195 } else {
Christopher Haster 1:43f5c94c6771 1196 resource_temp_ptr->resource_parameters_ptr->registered = SN_NDSL_RESOURCE_REGISTERED;
Christopher Haster 1:43f5c94c6771 1197 }
Christopher Haster 1:43f5c94c6771 1198
Christopher Haster 1:43f5c94c6771 1199 /* If not first resource, add '.' to separator */
Christopher Haster 1:43f5c94c6771 1200 if (temp_ptr != message_ptr->payload_ptr) {
Christopher Haster 1:43f5c94c6771 1201 *temp_ptr++ = ',';
Christopher Haster 1:43f5c94c6771 1202 }
Christopher Haster 1:43f5c94c6771 1203
Christopher Haster 1:43f5c94c6771 1204 *temp_ptr++ = '<';
Christopher Haster 1:43f5c94c6771 1205 *temp_ptr++ = '/';
Christopher Haster 1:43f5c94c6771 1206 memcpy(temp_ptr, resource_temp_ptr->path, resource_temp_ptr->pathlen);
Christopher Haster 1:43f5c94c6771 1207 temp_ptr += resource_temp_ptr->pathlen;
Christopher Haster 1:43f5c94c6771 1208 *temp_ptr++ = '>';
Christopher Haster 1:43f5c94c6771 1209
Christopher Haster 1:43f5c94c6771 1210 /* Resource attributes */
Christopher Haster 1:43f5c94c6771 1211 if (resource_temp_ptr->resource_parameters_ptr->resource_type_len) {
Christopher Haster 1:43f5c94c6771 1212 *temp_ptr++ = ';';
Christopher Haster 1:43f5c94c6771 1213 memcpy(temp_ptr, resource_type_parameter, RT_PARAMETER_LEN);
Christopher Haster 1:43f5c94c6771 1214 temp_ptr += RT_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1215 *temp_ptr++ = '"';
Christopher Haster 1:43f5c94c6771 1216 memcpy(temp_ptr, resource_temp_ptr->resource_parameters_ptr->resource_type_ptr, resource_temp_ptr->resource_parameters_ptr->resource_type_len);
Christopher Haster 1:43f5c94c6771 1217 temp_ptr += resource_temp_ptr->resource_parameters_ptr->resource_type_len;
Christopher Haster 1:43f5c94c6771 1218 *temp_ptr++ = '"';
Christopher Haster 1:43f5c94c6771 1219 }
Christopher Haster 1:43f5c94c6771 1220
Christopher Haster 1:43f5c94c6771 1221 if (resource_temp_ptr->resource_parameters_ptr->interface_description_len) {
Christopher Haster 1:43f5c94c6771 1222 *temp_ptr++ = ';';
Christopher Haster 1:43f5c94c6771 1223 memcpy(temp_ptr, if_description_parameter, IF_PARAMETER_LEN);
Christopher Haster 1:43f5c94c6771 1224 temp_ptr += IF_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1225 *temp_ptr++ = '"';
Christopher Haster 1:43f5c94c6771 1226 memcpy(temp_ptr, resource_temp_ptr->resource_parameters_ptr->interface_description_ptr, resource_temp_ptr->resource_parameters_ptr->interface_description_len);
Christopher Haster 1:43f5c94c6771 1227 temp_ptr += resource_temp_ptr->resource_parameters_ptr->interface_description_len;
Christopher Haster 1:43f5c94c6771 1228 *temp_ptr++ = '"';
Christopher Haster 1:43f5c94c6771 1229 }
Christopher Haster 1:43f5c94c6771 1230
Christopher Haster 1:43f5c94c6771 1231 if (resource_temp_ptr->resource_parameters_ptr->coap_content_type != 0) {
Christopher Haster 1:43f5c94c6771 1232 *temp_ptr++ = ';';
Christopher Haster 1:43f5c94c6771 1233 memcpy(temp_ptr, coap_con_type_parameter, COAP_CON_PARAMETER_LEN);
Christopher Haster 1:43f5c94c6771 1234 temp_ptr += COAP_CON_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1235 *temp_ptr++ = '"';
Christopher Haster 1:43f5c94c6771 1236 temp_ptr = sn_nsdl_itoa(temp_ptr, resource_temp_ptr->resource_parameters_ptr->coap_content_type);
Christopher Haster 1:43f5c94c6771 1237 *temp_ptr++ = '"';
Christopher Haster 1:43f5c94c6771 1238 }
Christopher Haster 1:43f5c94c6771 1239
Christopher Haster 1:43f5c94c6771 1240 /* ;obs */
Christopher Haster 1:43f5c94c6771 1241 if (resource_temp_ptr->resource_parameters_ptr->observable) {
Christopher Haster 1:43f5c94c6771 1242 *temp_ptr++ = ';';
Christopher Haster 1:43f5c94c6771 1243 memcpy(temp_ptr, obs_parameter, OBS_PARAMETER_LEN);
Christopher Haster 1:43f5c94c6771 1244 temp_ptr += OBS_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1245 }
Christopher Haster 1:43f5c94c6771 1246
Christopher Haster 1:43f5c94c6771 1247 /* ;aobs;id= */
Christopher Haster 1:43f5c94c6771 1248 /* todo: aosb not supported ATM */
Christopher Haster 1:43f5c94c6771 1249 /*
Christopher Haster 1:43f5c94c6771 1250 if((resource_temp_ptr->resource_parameters_ptr->auto_obs_len > 0 && resource_temp_ptr->resource_parameters_ptr->auto_obs_len <= 8) &&
Christopher Haster 1:43f5c94c6771 1251 resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr)
Christopher Haster 1:43f5c94c6771 1252 {
Christopher Haster 1:43f5c94c6771 1253 uint8_t i = 0;
Christopher Haster 1:43f5c94c6771 1254
Christopher Haster 1:43f5c94c6771 1255 *temp_ptr++ = ';';
Christopher Haster 1:43f5c94c6771 1256 memcpy(temp_ptr, aobs_parameter, AOBS_PARAMETER_LEN);
Christopher Haster 1:43f5c94c6771 1257 temp_ptr += AOBS_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1258
Christopher Haster 1:43f5c94c6771 1259 while(i < resource_temp_ptr->resource_parameters_ptr->auto_obs_len)
Christopher Haster 1:43f5c94c6771 1260 {
Christopher Haster 1:43f5c94c6771 1261 temp_ptr = sn_nsdl_itoa(temp_ptr, *(resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr + i));
Christopher Haster 1:43f5c94c6771 1262 i++;
Christopher Haster 1:43f5c94c6771 1263 }
Christopher Haster 1:43f5c94c6771 1264 }
Christopher Haster 1:43f5c94c6771 1265 */
Christopher Haster 1:43f5c94c6771 1266
Christopher Haster 1:43f5c94c6771 1267 }
Christopher Haster 1:43f5c94c6771 1268
Christopher Haster 1:43f5c94c6771 1269 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
Christopher Haster 1:43f5c94c6771 1270 }
Christopher Haster 1:43f5c94c6771 1271
Christopher Haster 1:43f5c94c6771 1272 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 1273 }
Christopher Haster 1:43f5c94c6771 1274
Christopher Haster 1:43f5c94c6771 1275 /**
Christopher Haster 1:43f5c94c6771 1276 * \fn static uint16_t sn_nsdl_calculate_registration_body_size(struct nsdl_s *handle, uint8_t updating_registeration)
Christopher Haster 1:43f5c94c6771 1277 *
Christopher Haster 1:43f5c94c6771 1278 *
Christopher Haster 1:43f5c94c6771 1279 * \brief Calculates registration message payload size
Christopher Haster 1:43f5c94c6771 1280 * \param *handle Pointer to nsdl-library handle
Christopher Haster 1:43f5c94c6771 1281 * \param *grs_resources_list_ptr Pointer to list of GRS resources
Christopher Haster 1:43f5c94c6771 1282 *
Christopher Haster 1:43f5c94c6771 1283 * \return Needed payload size
Christopher Haster 1:43f5c94c6771 1284 */
Christopher Haster 1:43f5c94c6771 1285 static uint16_t sn_nsdl_calculate_registration_body_size(struct nsdl_s *handle, uint8_t updating_registeration)
Christopher Haster 1:43f5c94c6771 1286 {
Christopher Haster 1:43f5c94c6771 1287 /* Local variables */
Christopher Haster 1:43f5c94c6771 1288 uint16_t return_value = 0;
Christopher Haster 1:43f5c94c6771 1289 const sn_nsdl_resource_info_s *resource_temp_ptr;
Christopher Haster 1:43f5c94c6771 1290
Christopher Haster 1:43f5c94c6771 1291 /* check pointer */
Christopher Haster 1:43f5c94c6771 1292
Christopher Haster 1:43f5c94c6771 1293 resource_temp_ptr = sn_grs_get_first_resource(handle->grs);
Christopher Haster 1:43f5c94c6771 1294
Christopher Haster 1:43f5c94c6771 1295 while (resource_temp_ptr) {
Christopher Haster 1:43f5c94c6771 1296 if (resource_temp_ptr->resource_parameters_ptr) {
Christopher Haster 1:43f5c94c6771 1297 if (updating_registeration && resource_temp_ptr->resource_parameters_ptr->registered == SN_NDSL_RESOURCE_REGISTERED) {
Christopher Haster 1:43f5c94c6771 1298 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
Christopher Haster 1:43f5c94c6771 1299 continue;
Christopher Haster 1:43f5c94c6771 1300 }
Christopher Haster 1:43f5c94c6771 1301
Christopher Haster 1:43f5c94c6771 1302 /* If not first resource, then '.' will be added */
Christopher Haster 1:43f5c94c6771 1303 if (return_value) {
Christopher Haster 1:43f5c94c6771 1304 return_value++;
Christopher Haster 1:43f5c94c6771 1305 }
Christopher Haster 1:43f5c94c6771 1306
Christopher Haster 1:43f5c94c6771 1307 /* Count length for the resource path </path> */
Christopher Haster 1:43f5c94c6771 1308 return_value += (3 + resource_temp_ptr->pathlen);
Christopher Haster 1:43f5c94c6771 1309
Christopher Haster 1:43f5c94c6771 1310 /* Count lengths of the attributes */
Christopher Haster 1:43f5c94c6771 1311
Christopher Haster 1:43f5c94c6771 1312 /* Resource type parameter */
Christopher Haster 1:43f5c94c6771 1313 if (resource_temp_ptr->resource_parameters_ptr->resource_type_len) {
Christopher Haster 1:43f5c94c6771 1314 /* ;rt="restype" */
Christopher Haster 1:43f5c94c6771 1315 return_value += (6 + resource_temp_ptr->resource_parameters_ptr->resource_type_len);
Christopher Haster 1:43f5c94c6771 1316 }
Christopher Haster 1:43f5c94c6771 1317
Christopher Haster 1:43f5c94c6771 1318 /* Interface description parameter */
Christopher Haster 1:43f5c94c6771 1319 if (resource_temp_ptr->resource_parameters_ptr->interface_description_len) {
Christopher Haster 1:43f5c94c6771 1320 /* ;if="iftype" */
Christopher Haster 1:43f5c94c6771 1321 return_value += (6 + resource_temp_ptr->resource_parameters_ptr->interface_description_len);
Christopher Haster 1:43f5c94c6771 1322 }
Christopher Haster 1:43f5c94c6771 1323
Christopher Haster 1:43f5c94c6771 1324 if (resource_temp_ptr->resource_parameters_ptr->coap_content_type != 0) {
Christopher Haster 1:43f5c94c6771 1325 /* ;if="content" */
Christopher Haster 1:43f5c94c6771 1326 return_value += 6; // all but not content
Christopher Haster 1:43f5c94c6771 1327 return_value += sn_nsdl_itoa_len(resource_temp_ptr->resource_parameters_ptr->coap_content_type);
Christopher Haster 1:43f5c94c6771 1328 }
Christopher Haster 1:43f5c94c6771 1329
Christopher Haster 1:43f5c94c6771 1330 if (resource_temp_ptr->resource_parameters_ptr->observable) {
Christopher Haster 1:43f5c94c6771 1331 /* ;obs */
Christopher Haster 1:43f5c94c6771 1332 return_value += 4;
Christopher Haster 1:43f5c94c6771 1333 }
Christopher Haster 1:43f5c94c6771 1334 /*todo: aobs not supported ATM */
Christopher Haster 1:43f5c94c6771 1335 /*
Christopher Haster 1:43f5c94c6771 1336 if((resource_temp_ptr->resource_parameters_ptr->auto_obs_len > 0 && resource_temp_ptr->resource_parameters_ptr->auto_obs_len <= 8) &&
Christopher Haster 1:43f5c94c6771 1337 resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr)
Christopher Haster 1:43f5c94c6771 1338 {
Christopher Haster 1:43f5c94c6771 1339 uint8_t i = resource_temp_ptr->resource_parameters_ptr->auto_obs_len;
Christopher Haster 1:43f5c94c6771 1340 // ;aobs;id=
Christopher Haster 1:43f5c94c6771 1341 return_value += 9;
Christopher Haster 1:43f5c94c6771 1342 while(i--)
Christopher Haster 1:43f5c94c6771 1343 {
Christopher Haster 1:43f5c94c6771 1344 return_value += sn_nsdl_itoa_len(*(resource_temp_ptr->resource_parameters_ptr->auto_obs_ptr + i));
Christopher Haster 1:43f5c94c6771 1345 }
Christopher Haster 1:43f5c94c6771 1346 }
Christopher Haster 1:43f5c94c6771 1347 */
Christopher Haster 1:43f5c94c6771 1348
Christopher Haster 1:43f5c94c6771 1349 }
Christopher Haster 1:43f5c94c6771 1350
Christopher Haster 1:43f5c94c6771 1351 resource_temp_ptr = sn_grs_get_next_resource(handle->grs, resource_temp_ptr);
Christopher Haster 1:43f5c94c6771 1352
Christopher Haster 1:43f5c94c6771 1353 }
Christopher Haster 1:43f5c94c6771 1354
Christopher Haster 1:43f5c94c6771 1355 return return_value;
Christopher Haster 1:43f5c94c6771 1356 }
Christopher Haster 1:43f5c94c6771 1357
Christopher Haster 1:43f5c94c6771 1358 /**
Christopher Haster 1:43f5c94c6771 1359 * \fn static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type)
Christopher Haster 1:43f5c94c6771 1360 *
Christopher Haster 1:43f5c94c6771 1361 *
Christopher Haster 1:43f5c94c6771 1362 * \brief Calculates needed uri query option length
Christopher Haster 1:43f5c94c6771 1363 *
Christopher Haster 1:43f5c94c6771 1364 * \param *endpoint_info_ptr Pointer to endpoint info structure
Christopher Haster 1:43f5c94c6771 1365 * \param msg_type Message type
Christopher Haster 1:43f5c94c6771 1366 *
Christopher Haster 1:43f5c94c6771 1367 * \return number of parameters in uri query
Christopher Haster 1:43f5c94c6771 1368 */
Christopher Haster 1:43f5c94c6771 1369 static uint8_t sn_nsdl_calculate_uri_query_option_len(sn_nsdl_ep_parameters_s *endpoint_info_ptr, uint8_t msg_type)
Christopher Haster 1:43f5c94c6771 1370 {
Christopher Haster 1:43f5c94c6771 1371 uint8_t return_value = 0;
Christopher Haster 1:43f5c94c6771 1372 uint8_t number_of_parameters = 0;
Christopher Haster 1:43f5c94c6771 1373
Christopher Haster 1:43f5c94c6771 1374
Christopher Haster 1:43f5c94c6771 1375 if ((endpoint_info_ptr->endpoint_name_len != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE) && endpoint_info_ptr->endpoint_name_ptr != 0) {
Christopher Haster 1:43f5c94c6771 1376 return_value += endpoint_info_ptr->endpoint_name_len;
Christopher Haster 1:43f5c94c6771 1377 return_value += EP_NAME_PARAMETERS_LEN; //ep=
Christopher Haster 1:43f5c94c6771 1378 number_of_parameters++;
Christopher Haster 1:43f5c94c6771 1379 }
Christopher Haster 1:43f5c94c6771 1380
Christopher Haster 1:43f5c94c6771 1381 if ((endpoint_info_ptr->type_len != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE) && (endpoint_info_ptr->type_ptr != 0)) {
Christopher Haster 1:43f5c94c6771 1382 return_value += endpoint_info_ptr->type_len;
Christopher Haster 1:43f5c94c6771 1383 return_value += ET_PARAMETER_LEN; //et=
Christopher Haster 1:43f5c94c6771 1384 number_of_parameters++;
Christopher Haster 1:43f5c94c6771 1385 }
Christopher Haster 1:43f5c94c6771 1386
Christopher Haster 1:43f5c94c6771 1387 if ((endpoint_info_ptr->lifetime_len != 0) && (endpoint_info_ptr->lifetime_ptr != 0)) {
Christopher Haster 1:43f5c94c6771 1388 return_value += endpoint_info_ptr->lifetime_len;
Christopher Haster 1:43f5c94c6771 1389 return_value += LT_PARAMETER_LEN; //lt=
Christopher Haster 1:43f5c94c6771 1390 number_of_parameters++;
Christopher Haster 1:43f5c94c6771 1391 }
Christopher Haster 1:43f5c94c6771 1392
Christopher Haster 1:43f5c94c6771 1393 if ((endpoint_info_ptr->domain_name_len != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE) && (endpoint_info_ptr->domain_name_ptr != 0)) {
Christopher Haster 1:43f5c94c6771 1394 return_value += endpoint_info_ptr->domain_name_len;
Christopher Haster 1:43f5c94c6771 1395 return_value += DOMAIN_PARAMETER_LEN; //d=
Christopher Haster 1:43f5c94c6771 1396 number_of_parameters++;
Christopher Haster 1:43f5c94c6771 1397 }
Christopher Haster 1:43f5c94c6771 1398
Christopher Haster 1:43f5c94c6771 1399 if (((endpoint_info_ptr->binding_and_mode & 0x04) || (endpoint_info_ptr->binding_and_mode & 0x01)) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
Christopher Haster 1:43f5c94c6771 1400 return_value += BS_QUEUE_MODE_PARAMATER_LEN;
Christopher Haster 1:43f5c94c6771 1401
Christopher Haster 1:43f5c94c6771 1402 if (endpoint_info_ptr->binding_and_mode & 0x01) {
Christopher Haster 1:43f5c94c6771 1403 return_value++;
Christopher Haster 1:43f5c94c6771 1404 }
Christopher Haster 1:43f5c94c6771 1405 if (endpoint_info_ptr->binding_and_mode & 0x04) {
Christopher Haster 1:43f5c94c6771 1406 return_value++;
Christopher Haster 1:43f5c94c6771 1407 }
Christopher Haster 1:43f5c94c6771 1408 if ((endpoint_info_ptr->binding_and_mode & 0x02) && ((endpoint_info_ptr->binding_and_mode & 0x04) || (endpoint_info_ptr->binding_and_mode & 0x01))) {
Christopher Haster 1:43f5c94c6771 1409 return_value++;
Christopher Haster 1:43f5c94c6771 1410 }
Christopher Haster 1:43f5c94c6771 1411
Christopher Haster 1:43f5c94c6771 1412 number_of_parameters++;
Christopher Haster 1:43f5c94c6771 1413 }
Christopher Haster 1:43f5c94c6771 1414
Christopher Haster 1:43f5c94c6771 1415 if (number_of_parameters != 0) {
Christopher Haster 1:43f5c94c6771 1416 return_value += (number_of_parameters - 1);
Christopher Haster 1:43f5c94c6771 1417 }
Christopher Haster 1:43f5c94c6771 1418
Christopher Haster 1:43f5c94c6771 1419 return return_value;
Christopher Haster 1:43f5c94c6771 1420 }
Christopher Haster 1:43f5c94c6771 1421
Christopher Haster 1:43f5c94c6771 1422 /**
Christopher Haster 1:43f5c94c6771 1423 * \fn static int8_t sn_nsdl_fill_uri_query_options(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type)
Christopher Haster 1:43f5c94c6771 1424 *
Christopher Haster 1:43f5c94c6771 1425 *
Christopher Haster 1:43f5c94c6771 1426 * \brief Fills uri-query options to message header struct
Christopher Haster 1:43f5c94c6771 1427 * \param *handle Pointer to nsdl-library handle
Christopher Haster 1:43f5c94c6771 1428 * \param *parameter_ptr Pointer to endpoint parameters struct
Christopher Haster 1:43f5c94c6771 1429 * \param *source_msg_ptr Pointer to CoAP header struct
Christopher Haster 1:43f5c94c6771 1430 * \param msg_type Message type
Christopher Haster 1:43f5c94c6771 1431 *
Christopher Haster 1:43f5c94c6771 1432 * \return SN_NSDL_SUCCESS = 0, Failed = -1
Christopher Haster 1:43f5c94c6771 1433 */
Christopher Haster 1:43f5c94c6771 1434 static int8_t sn_nsdl_fill_uri_query_options(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *parameter_ptr, sn_coap_hdr_s *source_msg_ptr, uint8_t msg_type)
Christopher Haster 1:43f5c94c6771 1435 {
Christopher Haster 1:43f5c94c6771 1436 uint8_t *temp_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1437 if( !validateParameters(parameter_ptr) ){
Christopher Haster 1:43f5c94c6771 1438 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1439 }
Christopher Haster 1:43f5c94c6771 1440 source_msg_ptr->options_list_ptr->uri_query_len = sn_nsdl_calculate_uri_query_option_len(parameter_ptr, msg_type);
Christopher Haster 1:43f5c94c6771 1441 if (source_msg_ptr->options_list_ptr->uri_query_len == 0) {
Christopher Haster 1:43f5c94c6771 1442 return 0;
Christopher Haster 1:43f5c94c6771 1443 }
Christopher Haster 1:43f5c94c6771 1444
Christopher Haster 1:43f5c94c6771 1445 source_msg_ptr->options_list_ptr->uri_query_ptr = handle->sn_nsdl_alloc(source_msg_ptr->options_list_ptr->uri_query_len);
Christopher Haster 1:43f5c94c6771 1446
Christopher Haster 1:43f5c94c6771 1447 if (source_msg_ptr->options_list_ptr->uri_query_ptr == NULL) {
Christopher Haster 1:43f5c94c6771 1448 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1449 }
Christopher Haster 1:43f5c94c6771 1450 memset(source_msg_ptr->options_list_ptr->uri_query_ptr,0,source_msg_ptr->options_list_ptr->uri_query_len);
Christopher Haster 1:43f5c94c6771 1451
Christopher Haster 1:43f5c94c6771 1452 temp_ptr = source_msg_ptr->options_list_ptr->uri_query_ptr;
Christopher Haster 1:43f5c94c6771 1453
Christopher Haster 1:43f5c94c6771 1454 /******************************************************/
Christopher Haster 1:43f5c94c6771 1455 /* If endpoint name is configured, fill needed fields */
Christopher Haster 1:43f5c94c6771 1456 /******************************************************/
Christopher Haster 1:43f5c94c6771 1457
Christopher Haster 1:43f5c94c6771 1458 if ((parameter_ptr->endpoint_name_len != 0) && (parameter_ptr->endpoint_name_ptr != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
Christopher Haster 1:43f5c94c6771 1459 /* fill endpoint name, first ?ep=, then endpoint name */
Christopher Haster 1:43f5c94c6771 1460 memcpy(temp_ptr, ep_name_parameter_string, sizeof(ep_name_parameter_string));
Christopher Haster 1:43f5c94c6771 1461 temp_ptr += EP_NAME_PARAMETERS_LEN;
Christopher Haster 1:43f5c94c6771 1462 memcpy(temp_ptr, parameter_ptr->endpoint_name_ptr, parameter_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 1463 temp_ptr += parameter_ptr->endpoint_name_len;
Christopher Haster 1:43f5c94c6771 1464 }
Christopher Haster 1:43f5c94c6771 1465
Christopher Haster 1:43f5c94c6771 1466 /******************************************************/
Christopher Haster 1:43f5c94c6771 1467 /* If endpoint type is configured, fill needed fields */
Christopher Haster 1:43f5c94c6771 1468 /******************************************************/
Christopher Haster 1:43f5c94c6771 1469
Christopher Haster 1:43f5c94c6771 1470 if ((parameter_ptr->type_len != 0) && (parameter_ptr->type_ptr != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
Christopher Haster 1:43f5c94c6771 1471 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
Christopher Haster 1:43f5c94c6771 1472 *temp_ptr++ = '&';
Christopher Haster 1:43f5c94c6771 1473 }
Christopher Haster 1:43f5c94c6771 1474
Christopher Haster 1:43f5c94c6771 1475 memcpy(temp_ptr, et_parameter, sizeof(et_parameter));
Christopher Haster 1:43f5c94c6771 1476 temp_ptr += ET_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1477 memcpy(temp_ptr, parameter_ptr->type_ptr, parameter_ptr->type_len);
Christopher Haster 1:43f5c94c6771 1478 temp_ptr += parameter_ptr->type_len;
Christopher Haster 1:43f5c94c6771 1479 }
Christopher Haster 1:43f5c94c6771 1480
Christopher Haster 1:43f5c94c6771 1481
Christopher Haster 1:43f5c94c6771 1482 /******************************************************/
Christopher Haster 1:43f5c94c6771 1483 /* If lifetime is configured, fill needed fields */
Christopher Haster 1:43f5c94c6771 1484 /******************************************************/
Christopher Haster 1:43f5c94c6771 1485
Christopher Haster 1:43f5c94c6771 1486 if ((parameter_ptr->lifetime_len != 0) && (parameter_ptr->lifetime_ptr != 0)) {
Christopher Haster 1:43f5c94c6771 1487 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
Christopher Haster 1:43f5c94c6771 1488 *temp_ptr++ = '&';
Christopher Haster 1:43f5c94c6771 1489 }
Christopher Haster 1:43f5c94c6771 1490
Christopher Haster 1:43f5c94c6771 1491 memcpy(temp_ptr, ep_lifetime_parameter, sizeof(ep_lifetime_parameter));
Christopher Haster 1:43f5c94c6771 1492 temp_ptr += LT_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1493 memcpy(temp_ptr, parameter_ptr->lifetime_ptr, parameter_ptr->lifetime_len);
Christopher Haster 1:43f5c94c6771 1494 temp_ptr += parameter_ptr->lifetime_len;
Christopher Haster 1:43f5c94c6771 1495 }
Christopher Haster 1:43f5c94c6771 1496
Christopher Haster 1:43f5c94c6771 1497 /******************************************************/
Christopher Haster 1:43f5c94c6771 1498 /* If domain is configured, fill needed fields */
Christopher Haster 1:43f5c94c6771 1499 /******************************************************/
Christopher Haster 1:43f5c94c6771 1500
Christopher Haster 1:43f5c94c6771 1501 if ((parameter_ptr->domain_name_len != 0) && (parameter_ptr->domain_name_ptr != 0) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
Christopher Haster 1:43f5c94c6771 1502 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
Christopher Haster 1:43f5c94c6771 1503 *temp_ptr++ = '&';
Christopher Haster 1:43f5c94c6771 1504 }
Christopher Haster 1:43f5c94c6771 1505
Christopher Haster 1:43f5c94c6771 1506 memcpy(temp_ptr, ep_domain_parameter, sizeof(ep_domain_parameter));
Christopher Haster 1:43f5c94c6771 1507 temp_ptr += DOMAIN_PARAMETER_LEN;
Christopher Haster 1:43f5c94c6771 1508 memcpy(temp_ptr, parameter_ptr->domain_name_ptr, parameter_ptr->domain_name_len);
Christopher Haster 1:43f5c94c6771 1509 temp_ptr += parameter_ptr->domain_name_len;
Christopher Haster 1:43f5c94c6771 1510 }
Christopher Haster 1:43f5c94c6771 1511
Christopher Haster 1:43f5c94c6771 1512 /******************************************************/
Christopher Haster 1:43f5c94c6771 1513 /* If queue-mode is configured, fill needed fields */
Christopher Haster 1:43f5c94c6771 1514 /******************************************************/
Christopher Haster 1:43f5c94c6771 1515
Christopher Haster 1:43f5c94c6771 1516 if (((parameter_ptr->binding_and_mode & 0x01) || (parameter_ptr->binding_and_mode & 0x04)) && (msg_type == SN_NSDL_EP_REGISTER_MESSAGE)) {
Christopher Haster 1:43f5c94c6771 1517 if (temp_ptr != source_msg_ptr->options_list_ptr->uri_query_ptr) {
Christopher Haster 1:43f5c94c6771 1518 *temp_ptr++ = '&';
Christopher Haster 1:43f5c94c6771 1519 }
Christopher Haster 1:43f5c94c6771 1520
Christopher Haster 1:43f5c94c6771 1521 memcpy(temp_ptr, bs_queue_mode, sizeof(bs_queue_mode));
Christopher Haster 1:43f5c94c6771 1522 temp_ptr += BS_QUEUE_MODE_PARAMATER_LEN;
Christopher Haster 1:43f5c94c6771 1523
Christopher Haster 1:43f5c94c6771 1524 if (parameter_ptr->binding_and_mode & 0x01) {
Christopher Haster 1:43f5c94c6771 1525 *temp_ptr++ = 'U';
Christopher Haster 1:43f5c94c6771 1526 if (parameter_ptr->binding_and_mode & 0x02) {
Christopher Haster 1:43f5c94c6771 1527 *temp_ptr++ = 'Q';
Christopher Haster 1:43f5c94c6771 1528 }
Christopher Haster 1:43f5c94c6771 1529 }
Christopher Haster 1:43f5c94c6771 1530
Christopher Haster 1:43f5c94c6771 1531 if (parameter_ptr->binding_and_mode & 0x04) {
Christopher Haster 1:43f5c94c6771 1532 *temp_ptr++ = 'S';
Christopher Haster 1:43f5c94c6771 1533 if ((parameter_ptr->binding_and_mode & 0x02) && !(parameter_ptr->binding_and_mode & 0x01)) {
Christopher Haster 1:43f5c94c6771 1534 *temp_ptr++ = 'Q';
Christopher Haster 1:43f5c94c6771 1535 }
Christopher Haster 1:43f5c94c6771 1536 }
Christopher Haster 1:43f5c94c6771 1537 }
Christopher Haster 1:43f5c94c6771 1538
Christopher Haster 1:43f5c94c6771 1539 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 1540 }
Christopher Haster 1:43f5c94c6771 1541
Christopher Haster 1:43f5c94c6771 1542 static bool validateParameters(sn_nsdl_ep_parameters_s *parameter_ptr)
Christopher Haster 1:43f5c94c6771 1543 {
Christopher Haster 1:43f5c94c6771 1544 if( !validate( parameter_ptr->domain_name_ptr, parameter_ptr->domain_name_len, '&' ) ){
Christopher Haster 1:43f5c94c6771 1545 return false;
Christopher Haster 1:43f5c94c6771 1546 }
Christopher Haster 1:43f5c94c6771 1547
Christopher Haster 1:43f5c94c6771 1548 if( !validate( parameter_ptr->endpoint_name_ptr, parameter_ptr->endpoint_name_len, '&' ) ){
Christopher Haster 1:43f5c94c6771 1549 return false;
Christopher Haster 1:43f5c94c6771 1550 }
Christopher Haster 1:43f5c94c6771 1551
Christopher Haster 1:43f5c94c6771 1552 if( !validate( parameter_ptr->lifetime_ptr, parameter_ptr->lifetime_len, '&' ) ){
Christopher Haster 1:43f5c94c6771 1553 return false;
Christopher Haster 1:43f5c94c6771 1554 }
Christopher Haster 1:43f5c94c6771 1555
Christopher Haster 1:43f5c94c6771 1556 if( !validate( parameter_ptr->type_ptr, parameter_ptr->type_len, '&' ) ){
Christopher Haster 1:43f5c94c6771 1557 return false;
Christopher Haster 1:43f5c94c6771 1558 }
Christopher Haster 1:43f5c94c6771 1559 return true;
Christopher Haster 1:43f5c94c6771 1560 }
Christopher Haster 1:43f5c94c6771 1561
Christopher Haster 1:43f5c94c6771 1562 static bool validate(uint8_t* ptr, uint32_t len, char illegalChar)
Christopher Haster 1:43f5c94c6771 1563 {
Christopher Haster 1:43f5c94c6771 1564 if( ptr ){
Christopher Haster 1:43f5c94c6771 1565 for( int i=0; i < len; i++ ){
Christopher Haster 1:43f5c94c6771 1566 if( ptr[i] == illegalChar ){
Christopher Haster 1:43f5c94c6771 1567 return false;
Christopher Haster 1:43f5c94c6771 1568 }
Christopher Haster 1:43f5c94c6771 1569 }
Christopher Haster 1:43f5c94c6771 1570 }
Christopher Haster 1:43f5c94c6771 1571 return true;
Christopher Haster 1:43f5c94c6771 1572 }
Christopher Haster 1:43f5c94c6771 1573
Christopher Haster 1:43f5c94c6771 1574 /**
Christopher Haster 1:43f5c94c6771 1575 * \fn static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
Christopher Haster 1:43f5c94c6771 1576 *
Christopher Haster 1:43f5c94c6771 1577 * \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.
Christopher Haster 1:43f5c94c6771 1578 * \param *handle Pointer to nsdl-library handle
Christopher Haster 1:43f5c94c6771 1579 * \param *coap_packet_ptr Pointer to received CoAP packet
Christopher Haster 1:43f5c94c6771 1580 * \param *address_ptr Pointer to source address struct
Christopher Haster 1:43f5c94c6771 1581 *
Christopher Haster 1:43f5c94c6771 1582 * \return SN_NSDL_SUCCESS = 0, Failed = -1
Christopher Haster 1:43f5c94c6771 1583 */
Christopher Haster 1:43f5c94c6771 1584 static int8_t sn_nsdl_local_rx_function(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, sn_nsdl_addr_s *address_ptr)
Christopher Haster 1:43f5c94c6771 1585 {
Christopher Haster 1:43f5c94c6771 1586 if ((coap_packet_ptr == 0) || (address_ptr == 0)) {
Christopher Haster 1:43f5c94c6771 1587 return -1;
Christopher Haster 1:43f5c94c6771 1588 }
Christopher Haster 1:43f5c94c6771 1589
Christopher Haster 1:43f5c94c6771 1590 if (coap_packet_ptr->msg_id == handle->register_msg_id) {
Christopher Haster 1:43f5c94c6771 1591 if (coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_CREATED) {
Christopher Haster 1:43f5c94c6771 1592 handle->sn_nsdl_endpoint_registered = SN_NSDL_ENDPOINT_IS_REGISTERED;
Christopher Haster 1:43f5c94c6771 1593 sn_grs_mark_resources_as_registered(handle);
Christopher Haster 1:43f5c94c6771 1594 if (sn_nsdl_resolve_ep_information(handle, coap_packet_ptr) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 1595 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1596 }
Christopher Haster 1:43f5c94c6771 1597
Christopher Haster 1:43f5c94c6771 1598 handle->register_msg_id = 0;
Christopher Haster 1:43f5c94c6771 1599 }
Christopher Haster 1:43f5c94c6771 1600 }
Christopher Haster 1:43f5c94c6771 1601
Christopher Haster 1:43f5c94c6771 1602 if (coap_packet_ptr->msg_id == handle->unregister_msg_id) {
Christopher Haster 1:43f5c94c6771 1603 if (coap_packet_ptr->msg_code == COAP_MSG_CODE_RESPONSE_DELETED) {
Christopher Haster 1:43f5c94c6771 1604 if (handle->ep_information_ptr->endpoint_name_ptr) {
Christopher Haster 1:43f5c94c6771 1605 handle->sn_nsdl_free(handle->ep_information_ptr->endpoint_name_ptr);
Christopher Haster 1:43f5c94c6771 1606 handle->ep_information_ptr->endpoint_name_ptr = 0;
Christopher Haster 1:43f5c94c6771 1607 handle->ep_information_ptr->endpoint_name_len = 0;
Christopher Haster 1:43f5c94c6771 1608 }
Christopher Haster 1:43f5c94c6771 1609 if (handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 1610 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
Christopher Haster 1:43f5c94c6771 1611 handle->ep_information_ptr->domain_name_ptr = 0;
Christopher Haster 1:43f5c94c6771 1612 handle->ep_information_ptr->domain_name_len = 0;
Christopher Haster 1:43f5c94c6771 1613 }
Christopher Haster 1:43f5c94c6771 1614
Christopher Haster 1:43f5c94c6771 1615 handle->unregister_msg_id = 0;
Christopher Haster 1:43f5c94c6771 1616 }
Christopher Haster 1:43f5c94c6771 1617 }
Christopher Haster 1:43f5c94c6771 1618
Christopher Haster 1:43f5c94c6771 1619 /* No messages to wait for, or message was not response to our request */
Christopher Haster 1:43f5c94c6771 1620 return handle->sn_nsdl_rx_callback(handle, coap_packet_ptr, address_ptr);
Christopher Haster 1:43f5c94c6771 1621 }
Christopher Haster 1:43f5c94c6771 1622
Christopher Haster 1:43f5c94c6771 1623 /**
Christopher Haster 1:43f5c94c6771 1624 * \fn static int8_t sn_nsdl_resolve_ep_information(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr)
Christopher Haster 1:43f5c94c6771 1625 *
Christopher Haster 1:43f5c94c6771 1626 *
Christopher Haster 1:43f5c94c6771 1627 * \brief Resolves endpoint information from received CoAP message
Christopher Haster 1:43f5c94c6771 1628 * \param *handle Pointer to nsdl-library handle
Christopher Haster 1:43f5c94c6771 1629 * \param *coap_packet_ptr Pointer to received CoAP message
Christopher Haster 1:43f5c94c6771 1630 *
Christopher Haster 1:43f5c94c6771 1631 * \return SN_NSDL_SUCCESS = 0, Failed = -1
Christopher Haster 1:43f5c94c6771 1632 */
Christopher Haster 1:43f5c94c6771 1633 static int8_t sn_nsdl_resolve_ep_information(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr)
Christopher Haster 1:43f5c94c6771 1634 {
Christopher Haster 1:43f5c94c6771 1635 uint8_t *temp_ptr;
Christopher Haster 1:43f5c94c6771 1636 uint8_t parameter_count = 0;
Christopher Haster 1:43f5c94c6771 1637 uint16_t parameter_len = 0;
Christopher Haster 1:43f5c94c6771 1638
Christopher Haster 1:43f5c94c6771 1639 if (!coap_packet_ptr || !coap_packet_ptr->options_list_ptr ||
Christopher Haster 1:43f5c94c6771 1640 !coap_packet_ptr->options_list_ptr->location_path_ptr) {
Christopher Haster 1:43f5c94c6771 1641 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1642 }
Christopher Haster 1:43f5c94c6771 1643
Christopher Haster 1:43f5c94c6771 1644 temp_ptr = coap_packet_ptr->options_list_ptr->location_path_ptr;
Christopher Haster 1:43f5c94c6771 1645
Christopher Haster 1:43f5c94c6771 1646 while (temp_ptr <= (coap_packet_ptr->options_list_ptr->location_path_ptr + coap_packet_ptr->options_list_ptr->location_path_len)) {
Christopher Haster 1:43f5c94c6771 1647
Christopher Haster 1:43f5c94c6771 1648 if ((temp_ptr == (coap_packet_ptr->options_list_ptr->location_path_ptr + coap_packet_ptr->options_list_ptr->location_path_len)) || (*temp_ptr == '/')) {
Christopher Haster 1:43f5c94c6771 1649
Christopher Haster 1:43f5c94c6771 1650 parameter_count++;
Christopher Haster 1:43f5c94c6771 1651 if (parameter_count == 2) {
Christopher Haster 1:43f5c94c6771 1652 if (!handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 1653 handle->ep_information_ptr->domain_name_len = parameter_len - 1;
Christopher Haster 1:43f5c94c6771 1654 handle->ep_information_ptr->domain_name_ptr = handle->sn_nsdl_alloc(handle->ep_information_ptr->domain_name_len);
Christopher Haster 1:43f5c94c6771 1655 if (!handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 1656 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1657 }
Christopher Haster 1:43f5c94c6771 1658 memcpy(handle->ep_information_ptr->domain_name_ptr, temp_ptr - handle->ep_information_ptr->domain_name_len, handle->ep_information_ptr->domain_name_len);
Christopher Haster 1:43f5c94c6771 1659 }
Christopher Haster 1:43f5c94c6771 1660
Christopher Haster 1:43f5c94c6771 1661 }
Christopher Haster 1:43f5c94c6771 1662 if (parameter_count == 3) {
Christopher Haster 1:43f5c94c6771 1663 if (!handle->ep_information_ptr->endpoint_name_ptr) {
Christopher Haster 1:43f5c94c6771 1664 handle->ep_information_ptr->endpoint_name_len = parameter_len - 1;
Christopher Haster 1:43f5c94c6771 1665 handle->ep_information_ptr->endpoint_name_ptr = handle->sn_nsdl_alloc(handle->ep_information_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 1666 if (!handle->ep_information_ptr->endpoint_name_ptr) {
Christopher Haster 1:43f5c94c6771 1667 if (handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 1668 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
Christopher Haster 1:43f5c94c6771 1669 handle->ep_information_ptr->domain_name_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1670 handle->ep_information_ptr->domain_name_len = 0;
Christopher Haster 1:43f5c94c6771 1671 }
Christopher Haster 1:43f5c94c6771 1672
Christopher Haster 1:43f5c94c6771 1673 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1674
Christopher Haster 1:43f5c94c6771 1675 }
Christopher Haster 1:43f5c94c6771 1676 memcpy(handle->ep_information_ptr->endpoint_name_ptr, temp_ptr - handle->ep_information_ptr->endpoint_name_len, handle->ep_information_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 1677 }
Christopher Haster 1:43f5c94c6771 1678 }
Christopher Haster 1:43f5c94c6771 1679 parameter_len = 0;
Christopher Haster 1:43f5c94c6771 1680 }
Christopher Haster 1:43f5c94c6771 1681 parameter_len++;
Christopher Haster 1:43f5c94c6771 1682 temp_ptr++;
Christopher Haster 1:43f5c94c6771 1683 }
Christopher Haster 1:43f5c94c6771 1684
Christopher Haster 1:43f5c94c6771 1685
Christopher Haster 1:43f5c94c6771 1686 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 1687 }
Christopher Haster 1:43f5c94c6771 1688
Christopher Haster 1:43f5c94c6771 1689 int8_t set_NSP_address(struct nsdl_s *handle, uint8_t *NSP_address, uint16_t port, sn_nsdl_addr_type_e address_type)
Christopher Haster 1:43f5c94c6771 1690 {
Christopher Haster 1:43f5c94c6771 1691
Christopher Haster 1:43f5c94c6771 1692 /* Check parameters and source pointers */
Christopher Haster 1:43f5c94c6771 1693 if (!handle || !handle->nsp_address_ptr || !handle->nsp_address_ptr->omalw_address_ptr || !NSP_address) {
Christopher Haster 1:43f5c94c6771 1694 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1695 }
Christopher Haster 1:43f5c94c6771 1696
Christopher Haster 1:43f5c94c6771 1697 handle->nsp_address_ptr->omalw_address_ptr->type = address_type;
Christopher Haster 1:43f5c94c6771 1698 handle->nsp_address_ptr->omalw_server_security = SEC_NOT_SET;
Christopher Haster 1:43f5c94c6771 1699
Christopher Haster 1:43f5c94c6771 1700 if (address_type == SN_NSDL_ADDRESS_TYPE_IPV4) {
Christopher Haster 1:43f5c94c6771 1701 if (handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 1702 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1703 }
Christopher Haster 1:43f5c94c6771 1704
Christopher Haster 1:43f5c94c6771 1705 handle->nsp_address_ptr->omalw_address_ptr->addr_len = 4;
Christopher Haster 1:43f5c94c6771 1706
Christopher Haster 1:43f5c94c6771 1707 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = handle->sn_nsdl_alloc(handle->nsp_address_ptr->omalw_address_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1708 if (!handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 1709 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1710 }
Christopher Haster 1:43f5c94c6771 1711
Christopher Haster 1:43f5c94c6771 1712 memcpy(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr, NSP_address, handle->nsp_address_ptr->omalw_address_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1713 handle->nsp_address_ptr->omalw_address_ptr->port = port;
Christopher Haster 1:43f5c94c6771 1714 }
Christopher Haster 1:43f5c94c6771 1715
Christopher Haster 1:43f5c94c6771 1716 else if (address_type == SN_NSDL_ADDRESS_TYPE_IPV6) {
Christopher Haster 1:43f5c94c6771 1717 if (handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 1718 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1719 }
Christopher Haster 1:43f5c94c6771 1720
Christopher Haster 1:43f5c94c6771 1721 handle->nsp_address_ptr->omalw_address_ptr->addr_len = 16;
Christopher Haster 1:43f5c94c6771 1722
Christopher Haster 1:43f5c94c6771 1723 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = handle->sn_nsdl_alloc(handle->nsp_address_ptr->omalw_address_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1724 if (!handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 1725 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1726 }
Christopher Haster 1:43f5c94c6771 1727
Christopher Haster 1:43f5c94c6771 1728 memcpy(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr, NSP_address, handle->nsp_address_ptr->omalw_address_ptr->addr_len);
Christopher Haster 1:43f5c94c6771 1729 handle->nsp_address_ptr->omalw_address_ptr->port = port;
Christopher Haster 1:43f5c94c6771 1730 }
Christopher Haster 1:43f5c94c6771 1731 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 1732 }
Christopher Haster 1:43f5c94c6771 1733
Christopher Haster 1:43f5c94c6771 1734
Christopher Haster 1:43f5c94c6771 1735 static uint8_t sn_nsdl_itoa_len(uint8_t value)
Christopher Haster 1:43f5c94c6771 1736 {
Christopher Haster 1:43f5c94c6771 1737 uint8_t i = 0;
Christopher Haster 1:43f5c94c6771 1738
Christopher Haster 1:43f5c94c6771 1739 do {
Christopher Haster 1:43f5c94c6771 1740 i++;
Christopher Haster 1:43f5c94c6771 1741 } while ((value /= 10) > 0);
Christopher Haster 1:43f5c94c6771 1742
Christopher Haster 1:43f5c94c6771 1743 return i;
Christopher Haster 1:43f5c94c6771 1744 }
Christopher Haster 1:43f5c94c6771 1745
Christopher Haster 1:43f5c94c6771 1746 static uint8_t *sn_nsdl_itoa(uint8_t *ptr, uint8_t value)
Christopher Haster 1:43f5c94c6771 1747 {
Christopher Haster 1:43f5c94c6771 1748
Christopher Haster 1:43f5c94c6771 1749 uint8_t start = 0;
Christopher Haster 1:43f5c94c6771 1750 uint8_t end = 0;
Christopher Haster 1:43f5c94c6771 1751 uint8_t i;
Christopher Haster 1:43f5c94c6771 1752
Christopher Haster 1:43f5c94c6771 1753 i = 0;
Christopher Haster 1:43f5c94c6771 1754
Christopher Haster 1:43f5c94c6771 1755 /* ITOA */
Christopher Haster 1:43f5c94c6771 1756 do {
Christopher Haster 1:43f5c94c6771 1757 ptr[i++] = (value % 10) + '0';
Christopher Haster 1:43f5c94c6771 1758 } while ((value /= 10) > 0);
Christopher Haster 1:43f5c94c6771 1759
Christopher Haster 1:43f5c94c6771 1760 end = i - 1;
Christopher Haster 1:43f5c94c6771 1761
Christopher Haster 1:43f5c94c6771 1762 /* reverse (part of ITOA) */
Christopher Haster 1:43f5c94c6771 1763 while (start < end) {
Christopher Haster 1:43f5c94c6771 1764 uint8_t chr;
Christopher Haster 1:43f5c94c6771 1765
Christopher Haster 1:43f5c94c6771 1766 chr = ptr[start];
Christopher Haster 1:43f5c94c6771 1767 ptr[start] = ptr[end];
Christopher Haster 1:43f5c94c6771 1768 ptr[end] = chr;
Christopher Haster 1:43f5c94c6771 1769
Christopher Haster 1:43f5c94c6771 1770 start++;
Christopher Haster 1:43f5c94c6771 1771 end--;
Christopher Haster 1:43f5c94c6771 1772
Christopher Haster 1:43f5c94c6771 1773 }
Christopher Haster 1:43f5c94c6771 1774 return (ptr + i);
Christopher Haster 1:43f5c94c6771 1775 }
Christopher Haster 1:43f5c94c6771 1776
Christopher Haster 1:43f5c94c6771 1777 static int32_t sn_nsdl_atoi(uint8_t *ptr, uint8_t len)
Christopher Haster 1:43f5c94c6771 1778 {
Christopher Haster 1:43f5c94c6771 1779
Christopher Haster 1:43f5c94c6771 1780 int32_t result = 0;
Christopher Haster 1:43f5c94c6771 1781
Christopher Haster 1:43f5c94c6771 1782 while (len--) {
Christopher Haster 1:43f5c94c6771 1783
Christopher Haster 1:43f5c94c6771 1784 if (result) {
Christopher Haster 1:43f5c94c6771 1785 result *= 10;
Christopher Haster 1:43f5c94c6771 1786 }
Christopher Haster 1:43f5c94c6771 1787
Christopher Haster 1:43f5c94c6771 1788 if (*ptr >= '0' && *ptr <= '9') {
Christopher Haster 1:43f5c94c6771 1789 result += *ptr - '0';
Christopher Haster 1:43f5c94c6771 1790 } else{
Christopher Haster 1:43f5c94c6771 1791 return -1;
Christopher Haster 1:43f5c94c6771 1792 }
Christopher Haster 1:43f5c94c6771 1793
Christopher Haster 1:43f5c94c6771 1794 ptr++;
Christopher Haster 1:43f5c94c6771 1795
Christopher Haster 1:43f5c94c6771 1796 }
Christopher Haster 1:43f5c94c6771 1797 return result;
Christopher Haster 1:43f5c94c6771 1798
Christopher Haster 1:43f5c94c6771 1799 }
Christopher Haster 1:43f5c94c6771 1800
Christopher Haster 1:43f5c94c6771 1801 static uint32_t sn_nsdl_ahextoi(uint8_t *ptr, uint8_t len)
Christopher Haster 1:43f5c94c6771 1802 {
Christopher Haster 1:43f5c94c6771 1803
Christopher Haster 1:43f5c94c6771 1804 uint32_t result = 0;
Christopher Haster 1:43f5c94c6771 1805
Christopher Haster 1:43f5c94c6771 1806 while (len--) {
Christopher Haster 1:43f5c94c6771 1807
Christopher Haster 1:43f5c94c6771 1808 if (result) {
Christopher Haster 1:43f5c94c6771 1809 result *= 16;
Christopher Haster 1:43f5c94c6771 1810 }
Christopher Haster 1:43f5c94c6771 1811
Christopher Haster 1:43f5c94c6771 1812 if (*ptr >= '0' && *ptr <= '9') {
Christopher Haster 1:43f5c94c6771 1813 result += *ptr - '0';
Christopher Haster 1:43f5c94c6771 1814 } else if (*ptr >= 'a' && *ptr <= 'f') {
Christopher Haster 1:43f5c94c6771 1815 result += *ptr - 87;
Christopher Haster 1:43f5c94c6771 1816 } else if (*ptr >= 'A' && *ptr <= 'F') {
Christopher Haster 1:43f5c94c6771 1817 result += *ptr - 55;
Christopher Haster 1:43f5c94c6771 1818 }
Christopher Haster 1:43f5c94c6771 1819
Christopher Haster 1:43f5c94c6771 1820 ptr++;
Christopher Haster 1:43f5c94c6771 1821
Christopher Haster 1:43f5c94c6771 1822 }
Christopher Haster 1:43f5c94c6771 1823 return result;
Christopher Haster 1:43f5c94c6771 1824
Christopher Haster 1:43f5c94c6771 1825 }
Christopher Haster 1:43f5c94c6771 1826
Christopher Haster 1:43f5c94c6771 1827 static int8_t sn_nsdl_resolve_lwm2m_address(struct nsdl_s *handle, uint8_t *uri, uint16_t uri_len)
Christopher Haster 1:43f5c94c6771 1828 {
Christopher Haster 1:43f5c94c6771 1829 if( uri_len < 2 ){
Christopher Haster 1:43f5c94c6771 1830 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1831 }
Christopher Haster 1:43f5c94c6771 1832 uint8_t *temp_ptr = uri+2;
Christopher Haster 1:43f5c94c6771 1833 uint16_t i = 0;
Christopher Haster 1:43f5c94c6771 1834 uint8_t char_cnt = 0;
Christopher Haster 1:43f5c94c6771 1835
Christopher Haster 1:43f5c94c6771 1836 /* jump over coap// */
Christopher Haster 1:43f5c94c6771 1837 while ((*(temp_ptr - 2) != '/') || (*(temp_ptr - 1) != '/')) {
Christopher Haster 1:43f5c94c6771 1838 temp_ptr++;
Christopher Haster 1:43f5c94c6771 1839 if (temp_ptr - uri >= uri_len) {
Christopher Haster 1:43f5c94c6771 1840 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1841 }
Christopher Haster 1:43f5c94c6771 1842 }
Christopher Haster 1:43f5c94c6771 1843
Christopher Haster 1:43f5c94c6771 1844 /* Resolve address type */
Christopher Haster 1:43f5c94c6771 1845 /* Count semicolons */
Christopher Haster 1:43f5c94c6771 1846
Christopher Haster 1:43f5c94c6771 1847 int8_t endPos = -1;
Christopher Haster 1:43f5c94c6771 1848
Christopher Haster 1:43f5c94c6771 1849 while (i < (uri_len - (temp_ptr - uri))) {
Christopher Haster 1:43f5c94c6771 1850 if (*(temp_ptr + i) == ':') {
Christopher Haster 1:43f5c94c6771 1851 char_cnt++;
Christopher Haster 1:43f5c94c6771 1852 }else if(*(temp_ptr + i) == ']'){
Christopher Haster 1:43f5c94c6771 1853 endPos = i;
Christopher Haster 1:43f5c94c6771 1854 }
Christopher Haster 1:43f5c94c6771 1855 i++;
Christopher Haster 1:43f5c94c6771 1856 }
Christopher Haster 1:43f5c94c6771 1857
Christopher Haster 1:43f5c94c6771 1858 uint8_t *temp_pos = temp_ptr; //store starting point in case of IPv4 parsing fails
Christopher Haster 1:43f5c94c6771 1859
Christopher Haster 1:43f5c94c6771 1860 /* IPv6 */
Christopher Haster 1:43f5c94c6771 1861 if (char_cnt > 2) {
Christopher Haster 1:43f5c94c6771 1862 i = 0;
Christopher Haster 1:43f5c94c6771 1863
Christopher Haster 1:43f5c94c6771 1864 if( handle->nsp_address_ptr->omalw_address_ptr->addr_ptr ){
Christopher Haster 1:43f5c94c6771 1865 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1866 }
Christopher Haster 1:43f5c94c6771 1867
Christopher Haster 1:43f5c94c6771 1868 handle->nsp_address_ptr->omalw_address_ptr->type = SN_NSDL_ADDRESS_TYPE_IPV6;
Christopher Haster 1:43f5c94c6771 1869 handle->nsp_address_ptr->omalw_address_ptr->addr_len = 16;
Christopher Haster 1:43f5c94c6771 1870 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = handle->sn_nsdl_alloc(16);
Christopher Haster 1:43f5c94c6771 1871 if (!handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 1872 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1873 }
Christopher Haster 1:43f5c94c6771 1874
Christopher Haster 1:43f5c94c6771 1875 memset(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr, 0, 16);
Christopher Haster 1:43f5c94c6771 1876 if (*temp_ptr == '[' && endPos > 0 && (temp_ptr - uri) + endPos < uri_len && *(temp_ptr + endPos + 1) == ':') {
Christopher Haster 1:43f5c94c6771 1877 temp_ptr++;
Christopher Haster 1:43f5c94c6771 1878 endPos--;
Christopher Haster 1:43f5c94c6771 1879 }else{
Christopher Haster 1:43f5c94c6771 1880 /* return failure, because port is mandatory */
Christopher Haster 1:43f5c94c6771 1881 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1882 }
Christopher Haster 1:43f5c94c6771 1883
Christopher Haster 1:43f5c94c6771 1884 int8_t loopbackPos = -1;
Christopher Haster 1:43f5c94c6771 1885 if( char_cnt != 8 ){
Christopher Haster 1:43f5c94c6771 1886 i = 0;
Christopher Haster 1:43f5c94c6771 1887 char_cnt -= 1;
Christopher Haster 1:43f5c94c6771 1888 while( i+1 < endPos ){
Christopher Haster 1:43f5c94c6771 1889 if(*(temp_ptr + i) == ':' && *(temp_ptr + i+1) == ':') {
Christopher Haster 1:43f5c94c6771 1890 loopbackPos = i;
Christopher Haster 1:43f5c94c6771 1891 break;
Christopher Haster 1:43f5c94c6771 1892 }
Christopher Haster 1:43f5c94c6771 1893 i++;
Christopher Haster 1:43f5c94c6771 1894 }
Christopher Haster 1:43f5c94c6771 1895 }
Christopher Haster 1:43f5c94c6771 1896 i = 0;
Christopher Haster 1:43f5c94c6771 1897
Christopher Haster 1:43f5c94c6771 1898 uint8_t numberOfZeros = 8 - char_cnt;
Christopher Haster 1:43f5c94c6771 1899 if(loopbackPos == 0){
Christopher Haster 1:43f5c94c6771 1900 numberOfZeros++;
Christopher Haster 1:43f5c94c6771 1901 }
Christopher Haster 1:43f5c94c6771 1902
Christopher Haster 1:43f5c94c6771 1903 if(loopbackPos == endPos-2){
Christopher Haster 1:43f5c94c6771 1904 numberOfZeros++;
Christopher Haster 1:43f5c94c6771 1905 }
Christopher Haster 1:43f5c94c6771 1906
Christopher Haster 1:43f5c94c6771 1907 /* Resolve address */
Christopher Haster 1:43f5c94c6771 1908 int8_t pos = loopbackPos == 0?0:-1;
Christopher Haster 1:43f5c94c6771 1909 while (i < 16 && ((temp_ptr - uri) + char_cnt) < uri_len) {
Christopher Haster 1:43f5c94c6771 1910 char_cnt = 0;
Christopher Haster 1:43f5c94c6771 1911 if( pos == loopbackPos ){
Christopher Haster 1:43f5c94c6771 1912 for( int k=0; k < numberOfZeros; k++ ){
Christopher Haster 1:43f5c94c6771 1913 i+=2;
Christopher Haster 1:43f5c94c6771 1914 }
Christopher Haster 1:43f5c94c6771 1915 pos+=2;
Christopher Haster 1:43f5c94c6771 1916 temp_ptr += 2;
Christopher Haster 1:43f5c94c6771 1917 if( numberOfZeros == 8 ){
Christopher Haster 1:43f5c94c6771 1918 temp_ptr++;
Christopher Haster 1:43f5c94c6771 1919 }
Christopher Haster 1:43f5c94c6771 1920 continue;
Christopher Haster 1:43f5c94c6771 1921 }
Christopher Haster 1:43f5c94c6771 1922 while (*(temp_ptr + char_cnt) != ':' && *(temp_ptr + char_cnt) != ']') {
Christopher Haster 1:43f5c94c6771 1923 char_cnt++;
Christopher Haster 1:43f5c94c6771 1924 pos++;
Christopher Haster 1:43f5c94c6771 1925 }
Christopher Haster 1:43f5c94c6771 1926 pos++;
Christopher Haster 1:43f5c94c6771 1927
Christopher Haster 1:43f5c94c6771 1928 if (char_cnt <= 2) {
Christopher Haster 1:43f5c94c6771 1929 i++;
Christopher Haster 1:43f5c94c6771 1930 }
Christopher Haster 1:43f5c94c6771 1931
Christopher Haster 1:43f5c94c6771 1932 while (char_cnt) {
Christopher Haster 1:43f5c94c6771 1933 if (char_cnt % 2) {
Christopher Haster 1:43f5c94c6771 1934 *(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr + i) = (uint8_t)sn_nsdl_ahextoi(temp_ptr, 1);
Christopher Haster 1:43f5c94c6771 1935 temp_ptr++;
Christopher Haster 1:43f5c94c6771 1936 char_cnt --;
Christopher Haster 1:43f5c94c6771 1937 } else {
Christopher Haster 1:43f5c94c6771 1938 *(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr + i) = (uint8_t)sn_nsdl_ahextoi(temp_ptr, 2);
Christopher Haster 1:43f5c94c6771 1939 temp_ptr += 2;
Christopher Haster 1:43f5c94c6771 1940 char_cnt -= 2;
Christopher Haster 1:43f5c94c6771 1941 }
Christopher Haster 1:43f5c94c6771 1942 i++;
Christopher Haster 1:43f5c94c6771 1943 }
Christopher Haster 1:43f5c94c6771 1944 temp_ptr++;
Christopher Haster 1:43f5c94c6771 1945 }
Christopher Haster 1:43f5c94c6771 1946
Christopher Haster 1:43f5c94c6771 1947 temp_ptr++;
Christopher Haster 1:43f5c94c6771 1948 uint16_t handled = (temp_ptr - uri);
Christopher Haster 1:43f5c94c6771 1949 if( handled < uri_len ){
Christopher Haster 1:43f5c94c6771 1950 if( *(temp_ptr + (uri_len - (temp_ptr - uri) -1)) == '/' ){
Christopher Haster 1:43f5c94c6771 1951 handle->nsp_address_ptr->omalw_address_ptr->port = sn_nsdl_atoi(temp_ptr, uri_len - (temp_ptr - uri) - 1);
Christopher Haster 1:43f5c94c6771 1952 }else{
Christopher Haster 1:43f5c94c6771 1953 handle->nsp_address_ptr->omalw_address_ptr->port = sn_nsdl_atoi(temp_ptr, uri_len - (temp_ptr - uri));
Christopher Haster 1:43f5c94c6771 1954 }
Christopher Haster 1:43f5c94c6771 1955 }
Christopher Haster 1:43f5c94c6771 1956 }
Christopher Haster 1:43f5c94c6771 1957 /* IPv4 or Hostname */
Christopher Haster 1:43f5c94c6771 1958 else if (char_cnt == 1) {
Christopher Haster 1:43f5c94c6771 1959 char_cnt = 0;
Christopher Haster 1:43f5c94c6771 1960 i = 0;
Christopher Haster 1:43f5c94c6771 1961
Christopher Haster 1:43f5c94c6771 1962 if( handle->nsp_address_ptr->omalw_address_ptr->addr_ptr ){
Christopher Haster 1:43f5c94c6771 1963 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1964 }
Christopher Haster 1:43f5c94c6771 1965
Christopher Haster 1:43f5c94c6771 1966 /* Check address type */
Christopher Haster 1:43f5c94c6771 1967 while (i < (uri_len - (temp_ptr - uri))) {
Christopher Haster 1:43f5c94c6771 1968 if (*(temp_ptr + i) == '.') {
Christopher Haster 1:43f5c94c6771 1969 char_cnt++;
Christopher Haster 1:43f5c94c6771 1970 }
Christopher Haster 1:43f5c94c6771 1971 i++;
Christopher Haster 1:43f5c94c6771 1972 }
Christopher Haster 1:43f5c94c6771 1973
Christopher Haster 1:43f5c94c6771 1974 bool parseOk = true;
Christopher Haster 1:43f5c94c6771 1975
Christopher Haster 1:43f5c94c6771 1976 /* Try IPv4 first */
Christopher Haster 1:43f5c94c6771 1977 if (char_cnt == 3) {
Christopher Haster 1:43f5c94c6771 1978 i = 0;
Christopher Haster 1:43f5c94c6771 1979 char_cnt = 0;
Christopher Haster 1:43f5c94c6771 1980
Christopher Haster 1:43f5c94c6771 1981 handle->nsp_address_ptr->omalw_address_ptr->type = SN_NSDL_ADDRESS_TYPE_IPV4;
Christopher Haster 1:43f5c94c6771 1982 handle->nsp_address_ptr->omalw_address_ptr->addr_len = 4;
Christopher Haster 1:43f5c94c6771 1983 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = handle->sn_nsdl_alloc(4);
Christopher Haster 1:43f5c94c6771 1984 if (!handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 1985 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 1986 }
Christopher Haster 1:43f5c94c6771 1987
Christopher Haster 1:43f5c94c6771 1988 while (parseOk && ((temp_ptr - uri) < uri_len) && *(temp_ptr - 1) != ':') {
Christopher Haster 1:43f5c94c6771 1989 i++;
Christopher Haster 1:43f5c94c6771 1990
Christopher Haster 1:43f5c94c6771 1991 if (*(temp_ptr + i) == ':' || *(temp_ptr + i) == '.') {
Christopher Haster 1:43f5c94c6771 1992 int8_t value = (int8_t)sn_nsdl_atoi(temp_ptr, i);
Christopher Haster 1:43f5c94c6771 1993 if( value == -1 ){
Christopher Haster 1:43f5c94c6771 1994 parseOk = false;
Christopher Haster 1:43f5c94c6771 1995 char_cnt = 3;
Christopher Haster 1:43f5c94c6771 1996 handle->sn_nsdl_free(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr);
Christopher Haster 1:43f5c94c6771 1997 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = NULL;
Christopher Haster 1:43f5c94c6771 1998 break;
Christopher Haster 1:43f5c94c6771 1999 }
Christopher Haster 1:43f5c94c6771 2000 *(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr + char_cnt) = value;
Christopher Haster 1:43f5c94c6771 2001 temp_ptr = temp_ptr + i + 1;
Christopher Haster 1:43f5c94c6771 2002 char_cnt++;
Christopher Haster 1:43f5c94c6771 2003 i = 0;
Christopher Haster 1:43f5c94c6771 2004 }
Christopher Haster 1:43f5c94c6771 2005 }
Christopher Haster 1:43f5c94c6771 2006 if(parseOk) {
Christopher Haster 1:43f5c94c6771 2007 if( *(temp_ptr + (uri_len - (temp_ptr - uri) -1)) == '/' ){
Christopher Haster 1:43f5c94c6771 2008 handle->nsp_address_ptr->omalw_address_ptr->port = sn_nsdl_atoi(temp_ptr, uri_len - (temp_ptr - uri) - 1);
Christopher Haster 1:43f5c94c6771 2009 }else{
Christopher Haster 1:43f5c94c6771 2010 handle->nsp_address_ptr->omalw_address_ptr->port = sn_nsdl_atoi(temp_ptr, uri_len - (temp_ptr - uri));
Christopher Haster 1:43f5c94c6771 2011 }
Christopher Haster 1:43f5c94c6771 2012 }
Christopher Haster 1:43f5c94c6771 2013 }else{
Christopher Haster 1:43f5c94c6771 2014 parseOk = false;
Christopher Haster 1:43f5c94c6771 2015 }
Christopher Haster 1:43f5c94c6771 2016
Christopher Haster 1:43f5c94c6771 2017 /* Then try Hostname */
Christopher Haster 1:43f5c94c6771 2018 if(!parseOk) {
Christopher Haster 1:43f5c94c6771 2019 i = 0;
Christopher Haster 1:43f5c94c6771 2020 temp_ptr = temp_pos;
Christopher Haster 1:43f5c94c6771 2021
Christopher Haster 1:43f5c94c6771 2022 handle->nsp_address_ptr->omalw_address_ptr->type = SN_NSDL_ADDRESS_TYPE_HOSTNAME;
Christopher Haster 1:43f5c94c6771 2023
Christopher Haster 1:43f5c94c6771 2024 /* Resolve address length */
Christopher Haster 1:43f5c94c6771 2025 if (uri_len > 0xff) {
Christopher Haster 1:43f5c94c6771 2026 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2027 }
Christopher Haster 1:43f5c94c6771 2028
Christopher Haster 1:43f5c94c6771 2029 while (((temp_ptr - uri) + i < uri_len) && *(temp_ptr + i) != ':') {
Christopher Haster 1:43f5c94c6771 2030 i++;
Christopher Haster 1:43f5c94c6771 2031 }
Christopher Haster 1:43f5c94c6771 2032
Christopher Haster 1:43f5c94c6771 2033 handle->nsp_address_ptr->omalw_address_ptr->addr_len = i;
Christopher Haster 1:43f5c94c6771 2034
Christopher Haster 1:43f5c94c6771 2035 /* Copy address */
Christopher Haster 1:43f5c94c6771 2036 handle->nsp_address_ptr->omalw_address_ptr->addr_ptr = handle->sn_nsdl_alloc(i);
Christopher Haster 1:43f5c94c6771 2037 if (!handle->nsp_address_ptr->omalw_address_ptr->addr_ptr) {
Christopher Haster 1:43f5c94c6771 2038 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2039 }
Christopher Haster 1:43f5c94c6771 2040
Christopher Haster 1:43f5c94c6771 2041 memcpy(handle->nsp_address_ptr->omalw_address_ptr->addr_ptr, temp_ptr, i);
Christopher Haster 1:43f5c94c6771 2042
Christopher Haster 1:43f5c94c6771 2043 temp_ptr += i + 1;
Christopher Haster 1:43f5c94c6771 2044
Christopher Haster 1:43f5c94c6771 2045 /* Set port */
Christopher Haster 1:43f5c94c6771 2046 if( *(temp_ptr + (uri_len - (temp_ptr - uri) - 1)) == '/' ){
Christopher Haster 1:43f5c94c6771 2047 handle->nsp_address_ptr->omalw_address_ptr->port = sn_nsdl_atoi(temp_ptr, uri_len - (temp_ptr - uri) - 1);
Christopher Haster 1:43f5c94c6771 2048 }else{
Christopher Haster 1:43f5c94c6771 2049 handle->nsp_address_ptr->omalw_address_ptr->port = sn_nsdl_atoi(temp_ptr, uri_len - (temp_ptr - uri));
Christopher Haster 1:43f5c94c6771 2050 }
Christopher Haster 1:43f5c94c6771 2051 }
Christopher Haster 1:43f5c94c6771 2052 } else {
Christopher Haster 1:43f5c94c6771 2053 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2054 }
Christopher Haster 1:43f5c94c6771 2055
Christopher Haster 1:43f5c94c6771 2056 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 2057 }
Christopher Haster 1:43f5c94c6771 2058
Christopher Haster 1:43f5c94c6771 2059
Christopher Haster 1:43f5c94c6771 2060 int8_t sn_nsdl_process_oma_tlv(struct nsdl_s *handle, uint8_t *data_ptr, uint16_t data_len)
Christopher Haster 1:43f5c94c6771 2061 {
Christopher Haster 1:43f5c94c6771 2062 uint8_t *temp_ptr = data_ptr;
Christopher Haster 1:43f5c94c6771 2063 uint8_t type = 0;
Christopher Haster 1:43f5c94c6771 2064 uint16_t identifier = 0;
Christopher Haster 1:43f5c94c6771 2065 uint32_t length = 0;
Christopher Haster 1:43f5c94c6771 2066 uint8_t path_temp[5] = "0/0/x";
Christopher Haster 1:43f5c94c6771 2067
Christopher Haster 1:43f5c94c6771 2068 sn_nsdl_resource_info_s resource_temp = {
Christopher Haster 1:43f5c94c6771 2069 .resource_parameters_ptr = 0,
Christopher Haster 1:43f5c94c6771 2070 .mode = SN_GRS_STATIC,
Christopher Haster 1:43f5c94c6771 2071 .pathlen = 5,
Christopher Haster 1:43f5c94c6771 2072 .path = path_temp,
Christopher Haster 1:43f5c94c6771 2073 .resourcelen = 0,
Christopher Haster 1:43f5c94c6771 2074 .resource = 0,
Christopher Haster 1:43f5c94c6771 2075 .access = (sn_grs_resource_acl_e) 0x0f, /* All allowed */
Christopher Haster 1:43f5c94c6771 2076 .sn_grs_dyn_res_callback = 0
Christopher Haster 1:43f5c94c6771 2077 };
Christopher Haster 1:43f5c94c6771 2078
Christopher Haster 1:43f5c94c6771 2079 while ((temp_ptr - data_ptr) < data_len) {
Christopher Haster 1:43f5c94c6771 2080 /* Save type for future use */
Christopher Haster 1:43f5c94c6771 2081 type = *temp_ptr++;
Christopher Haster 1:43f5c94c6771 2082
Christopher Haster 1:43f5c94c6771 2083 /* * Bit 5: Indicates the Length of the Identifier. * */
Christopher Haster 1:43f5c94c6771 2084 if (type & 0x20) {
Christopher Haster 1:43f5c94c6771 2085 /* 1=The Identifier field of this TLV is 16 bits long */
Christopher Haster 1:43f5c94c6771 2086 identifier = (uint8_t)(*temp_ptr++) << 8;
Christopher Haster 1:43f5c94c6771 2087 identifier += (uint8_t) * temp_ptr++;
Christopher Haster 1:43f5c94c6771 2088 } else {
Christopher Haster 1:43f5c94c6771 2089 /* 0=The Identifier field of this TLV is 8 bits long */
Christopher Haster 1:43f5c94c6771 2090 identifier = (uint8_t) * temp_ptr++;
Christopher Haster 1:43f5c94c6771 2091 }
Christopher Haster 1:43f5c94c6771 2092
Christopher Haster 1:43f5c94c6771 2093 /* * Bit 4-3: Indicates the type of Length. * */
Christopher Haster 1:43f5c94c6771 2094 if ((type & 0x18) == 0) {
Christopher Haster 1:43f5c94c6771 2095 /* 00 = No length field, the value immediately follows the Identifier field in is of the length indicated by Bits 2-0 of this field */
Christopher Haster 1:43f5c94c6771 2096 length = (type & 0x07);
Christopher Haster 1:43f5c94c6771 2097 } else if ((type & 0x18) == 0x08) {
Christopher Haster 1:43f5c94c6771 2098 /* 01 = The Length field is 8-bits and Bits 2-0 MUST be ignored */
Christopher Haster 1:43f5c94c6771 2099 length = *temp_ptr++;
Christopher Haster 1:43f5c94c6771 2100 } else if ((type & 0x18) == 0x10) {
Christopher Haster 1:43f5c94c6771 2101 /* 10 = The Length field is 16-bits and Bits 2-0 MUST be ignored */
Christopher Haster 1:43f5c94c6771 2102 length = (uint8_t)(*temp_ptr++) << 8;
Christopher Haster 1:43f5c94c6771 2103 length += (uint8_t) * temp_ptr++;
Christopher Haster 1:43f5c94c6771 2104 } else if ((type & 0x18) == 0x18) {
Christopher Haster 1:43f5c94c6771 2105 /* 11 = The Length field is 24-bits and Bits 2-0 MUST be ignored */
Christopher Haster 1:43f5c94c6771 2106 length = (uint8_t)(*temp_ptr++);
Christopher Haster 1:43f5c94c6771 2107 length = length << 16;
Christopher Haster 1:43f5c94c6771 2108 length += (uint8_t)(*temp_ptr++) << 8;
Christopher Haster 1:43f5c94c6771 2109 length += (uint8_t) * temp_ptr++;
Christopher Haster 1:43f5c94c6771 2110 }
Christopher Haster 1:43f5c94c6771 2111
Christopher Haster 1:43f5c94c6771 2112 /* * Bits 7-6: Indicates the type of Identifier. * */
Christopher Haster 1:43f5c94c6771 2113 if ((type & 0xC0) == 0x00) {
Christopher Haster 1:43f5c94c6771 2114 /* 00 = Object Instance in which case the Value contains one or more Resource TLVs */
Christopher Haster 1:43f5c94c6771 2115 /* Not implemented, return failure */
Christopher Haster 1:43f5c94c6771 2116 } else if ((type & 0xC0) == 0xC0) {
Christopher Haster 1:43f5c94c6771 2117 /* 11 = Resource with Value */
Christopher Haster 1:43f5c94c6771 2118 switch (identifier) {
Christopher Haster 1:43f5c94c6771 2119 case 0:
Christopher Haster 1:43f5c94c6771 2120 /* Resolve LWM2M Server URI */
Christopher Haster 1:43f5c94c6771 2121 sn_nsdl_resolve_lwm2m_address(handle, temp_ptr, length);
Christopher Haster 1:43f5c94c6771 2122 path_temp[4] = '0';
Christopher Haster 1:43f5c94c6771 2123 resource_temp.resource = temp_ptr;
Christopher Haster 1:43f5c94c6771 2124 resource_temp.resourcelen = length;
Christopher Haster 1:43f5c94c6771 2125 if (sn_nsdl_create_resource(handle, &resource_temp) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 2126 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2127 }
Christopher Haster 1:43f5c94c6771 2128 break;
Christopher Haster 1:43f5c94c6771 2129 case 2:
Christopher Haster 1:43f5c94c6771 2130 /* Resolve security Mode */
Christopher Haster 1:43f5c94c6771 2131 handle->nsp_address_ptr->omalw_server_security = (omalw_server_security_t)sn_nsdl_atoi(temp_ptr, length);
Christopher Haster 1:43f5c94c6771 2132 path_temp[4] = '2';
Christopher Haster 1:43f5c94c6771 2133 resource_temp.resource = temp_ptr;
Christopher Haster 1:43f5c94c6771 2134 resource_temp.resourcelen = length;
Christopher Haster 1:43f5c94c6771 2135 if (sn_nsdl_create_resource(handle, &resource_temp) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 2136 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2137 }
Christopher Haster 1:43f5c94c6771 2138
Christopher Haster 1:43f5c94c6771 2139 break;
Christopher Haster 1:43f5c94c6771 2140 case 3:
Christopher Haster 1:43f5c94c6771 2141 /* Public Key or Identity */
Christopher Haster 1:43f5c94c6771 2142 path_temp[4] = '3';
Christopher Haster 1:43f5c94c6771 2143 resource_temp.resource = temp_ptr;
Christopher Haster 1:43f5c94c6771 2144 resource_temp.resourcelen = length;
Christopher Haster 1:43f5c94c6771 2145 if (sn_nsdl_create_resource(handle, &resource_temp) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 2146 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2147 }
Christopher Haster 1:43f5c94c6771 2148 break;
Christopher Haster 1:43f5c94c6771 2149 case 4:
Christopher Haster 1:43f5c94c6771 2150 /* Server Public Key or Identity */
Christopher Haster 1:43f5c94c6771 2151 ;
Christopher Haster 1:43f5c94c6771 2152 path_temp[4] = '4';
Christopher Haster 1:43f5c94c6771 2153 resource_temp.resource = temp_ptr;
Christopher Haster 1:43f5c94c6771 2154 resource_temp.resourcelen = length;
Christopher Haster 1:43f5c94c6771 2155 if (sn_nsdl_create_resource(handle, &resource_temp) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 2156 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2157 }
Christopher Haster 1:43f5c94c6771 2158
Christopher Haster 1:43f5c94c6771 2159 break;
Christopher Haster 1:43f5c94c6771 2160 case 5:
Christopher Haster 1:43f5c94c6771 2161 /* Secret Key */
Christopher Haster 1:43f5c94c6771 2162 path_temp[4] = '5';
Christopher Haster 1:43f5c94c6771 2163 resource_temp.resource = temp_ptr;
Christopher Haster 1:43f5c94c6771 2164 resource_temp.resourcelen = length;
Christopher Haster 1:43f5c94c6771 2165 if (sn_nsdl_create_resource(handle, &resource_temp) != SN_NSDL_SUCCESS) {
Christopher Haster 1:43f5c94c6771 2166 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2167 }
Christopher Haster 1:43f5c94c6771 2168 break;
Christopher Haster 1:43f5c94c6771 2169 default:
Christopher Haster 1:43f5c94c6771 2170 break;
Christopher Haster 1:43f5c94c6771 2171 }
Christopher Haster 1:43f5c94c6771 2172
Christopher Haster 1:43f5c94c6771 2173 /* Move pointer to next TLV message */
Christopher Haster 1:43f5c94c6771 2174 temp_ptr += length;
Christopher Haster 1:43f5c94c6771 2175 }
Christopher Haster 1:43f5c94c6771 2176 }
Christopher Haster 1:43f5c94c6771 2177
Christopher Haster 1:43f5c94c6771 2178 return SN_NSDL_SUCCESS;
Christopher Haster 1:43f5c94c6771 2179 }
Christopher Haster 1:43f5c94c6771 2180
Christopher Haster 1:43f5c94c6771 2181 static void sn_nsdl_check_oma_bs_status(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 2182 {
Christopher Haster 1:43f5c94c6771 2183 /* Check OMA BS status */
Christopher Haster 1:43f5c94c6771 2184 if ((handle->nsp_address_ptr->omalw_server_security == PSK) && (handle->nsp_address_ptr->omalw_address_ptr->type != SN_NSDL_ADDRESS_TYPE_NONE)) {
Christopher Haster 1:43f5c94c6771 2185 /* call cb that oma bootstrap is done */
Christopher Haster 1:43f5c94c6771 2186 if(handle->sn_nsdl_oma_bs_done_cb != 0){
Christopher Haster 1:43f5c94c6771 2187 handle->sn_nsdl_oma_bs_done_cb(handle->nsp_address_ptr);
Christopher Haster 1:43f5c94c6771 2188 }
Christopher Haster 1:43f5c94c6771 2189 } else if ((handle->nsp_address_ptr->omalw_server_security == CERTIFICATE) && (handle->nsp_address_ptr->omalw_address_ptr->type != SN_NSDL_ADDRESS_TYPE_NONE) &&
Christopher Haster 1:43f5c94c6771 2190 ((sn_nsdl_get_resource(handle, 5, (void *)"0/0/5") != 0) &&
Christopher Haster 1:43f5c94c6771 2191 (sn_nsdl_get_resource(handle, 5, (void *)"0/0/4") != 0) &&
Christopher Haster 1:43f5c94c6771 2192 (sn_nsdl_get_resource(handle, 5, (void *)"0/0/3") != 0))) {
Christopher Haster 1:43f5c94c6771 2193 if( handle->sn_nsdl_oma_bs_done_cb ){
Christopher Haster 1:43f5c94c6771 2194 handle->sn_nsdl_oma_bs_done_cb(handle->nsp_address_ptr);
Christopher Haster 1:43f5c94c6771 2195 }
Christopher Haster 1:43f5c94c6771 2196 }
Christopher Haster 1:43f5c94c6771 2197 }
Christopher Haster 1:43f5c94c6771 2198
Christopher Haster 1:43f5c94c6771 2199 static int8_t set_endpoint_info(struct nsdl_s *handle, sn_nsdl_ep_parameters_s *endpoint_info_ptr)
Christopher Haster 1:43f5c94c6771 2200 {
Christopher Haster 1:43f5c94c6771 2201 if (handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 2202 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
Christopher Haster 1:43f5c94c6771 2203 handle->ep_information_ptr->domain_name_ptr = 0;
Christopher Haster 1:43f5c94c6771 2204 handle->ep_information_ptr->domain_name_len = 0;
Christopher Haster 1:43f5c94c6771 2205 }
Christopher Haster 1:43f5c94c6771 2206
Christopher Haster 1:43f5c94c6771 2207 if (handle->ep_information_ptr->endpoint_name_ptr) {
Christopher Haster 1:43f5c94c6771 2208 handle->sn_nsdl_free(handle->ep_information_ptr->endpoint_name_ptr);
Christopher Haster 1:43f5c94c6771 2209 handle->ep_information_ptr->endpoint_name_ptr = 0;
Christopher Haster 1:43f5c94c6771 2210 handle->ep_information_ptr->endpoint_name_len = 0;
Christopher Haster 1:43f5c94c6771 2211 }
Christopher Haster 1:43f5c94c6771 2212
Christopher Haster 1:43f5c94c6771 2213 if (endpoint_info_ptr->domain_name_ptr && endpoint_info_ptr->domain_name_len) {
Christopher Haster 1:43f5c94c6771 2214 handle->ep_information_ptr->domain_name_ptr = handle->sn_nsdl_alloc(endpoint_info_ptr->domain_name_len);
Christopher Haster 1:43f5c94c6771 2215
Christopher Haster 1:43f5c94c6771 2216 if (!handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 2217 return -1;
Christopher Haster 1:43f5c94c6771 2218 }
Christopher Haster 1:43f5c94c6771 2219
Christopher Haster 1:43f5c94c6771 2220 memcpy(handle->ep_information_ptr->domain_name_ptr, endpoint_info_ptr->domain_name_ptr, endpoint_info_ptr->domain_name_len);
Christopher Haster 1:43f5c94c6771 2221 handle->ep_information_ptr->domain_name_len = endpoint_info_ptr->domain_name_len;
Christopher Haster 1:43f5c94c6771 2222 }
Christopher Haster 1:43f5c94c6771 2223
Christopher Haster 1:43f5c94c6771 2224 if (endpoint_info_ptr->endpoint_name_ptr && endpoint_info_ptr->endpoint_name_len) {
Christopher Haster 1:43f5c94c6771 2225 handle->ep_information_ptr->endpoint_name_ptr = handle->sn_nsdl_alloc(endpoint_info_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 2226
Christopher Haster 1:43f5c94c6771 2227 if (!handle->ep_information_ptr->endpoint_name_ptr) {
Christopher Haster 1:43f5c94c6771 2228 if (handle->ep_information_ptr->domain_name_ptr) {
Christopher Haster 1:43f5c94c6771 2229 handle->sn_nsdl_free(handle->ep_information_ptr->domain_name_ptr);
Christopher Haster 1:43f5c94c6771 2230 handle->ep_information_ptr->domain_name_ptr = 0;
Christopher Haster 1:43f5c94c6771 2231 handle->ep_information_ptr->domain_name_len = 0;
Christopher Haster 1:43f5c94c6771 2232 }
Christopher Haster 1:43f5c94c6771 2233 return -1;
Christopher Haster 1:43f5c94c6771 2234 }
Christopher Haster 1:43f5c94c6771 2235
Christopher Haster 1:43f5c94c6771 2236 memcpy(handle->ep_information_ptr->endpoint_name_ptr, endpoint_info_ptr->endpoint_name_ptr, endpoint_info_ptr->endpoint_name_len);
Christopher Haster 1:43f5c94c6771 2237 handle->ep_information_ptr->endpoint_name_len = endpoint_info_ptr->endpoint_name_len;
Christopher Haster 1:43f5c94c6771 2238 }
Christopher Haster 1:43f5c94c6771 2239
Christopher Haster 1:43f5c94c6771 2240 handle->ep_information_ptr->binding_and_mode = endpoint_info_ptr->binding_and_mode;
Christopher Haster 1:43f5c94c6771 2241 handle->ep_information_ptr->ds_register_mode = endpoint_info_ptr->ds_register_mode;
Christopher Haster 1:43f5c94c6771 2242
Christopher Haster 1:43f5c94c6771 2243 handle->ep_information_ptr->location_ptr = 0;
Christopher Haster 1:43f5c94c6771 2244 handle->ep_information_ptr->location_len = 0;
Christopher Haster 1:43f5c94c6771 2245
Christopher Haster 1:43f5c94c6771 2246 return 0;
Christopher Haster 1:43f5c94c6771 2247 }
Christopher Haster 1:43f5c94c6771 2248
Christopher Haster 1:43f5c94c6771 2249 /* Wrapper */
Christopher Haster 1:43f5c94c6771 2250 sn_grs_resource_list_s *sn_nsdl_list_resource(struct nsdl_s *handle, uint16_t pathlen, uint8_t *path)
Christopher Haster 1:43f5c94c6771 2251 {
Christopher Haster 1:43f5c94c6771 2252 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2253 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2254 return NULL;
Christopher Haster 1:43f5c94c6771 2255 }
Christopher Haster 1:43f5c94c6771 2256
Christopher Haster 1:43f5c94c6771 2257 return sn_grs_list_resource(handle->grs, pathlen, path);
Christopher Haster 1:43f5c94c6771 2258 }
Christopher Haster 1:43f5c94c6771 2259
Christopher Haster 1:43f5c94c6771 2260 void sn_nsdl_free_resource_list(struct nsdl_s *handle, sn_grs_resource_list_s *list)
Christopher Haster 1:43f5c94c6771 2261 {
Christopher Haster 1:43f5c94c6771 2262 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2263 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2264 return;
Christopher Haster 1:43f5c94c6771 2265 }
Christopher Haster 1:43f5c94c6771 2266
Christopher Haster 1:43f5c94c6771 2267 sn_grs_free_resource_list(handle->grs, list);
Christopher Haster 1:43f5c94c6771 2268 }
Christopher Haster 1:43f5c94c6771 2269
Christopher Haster 1:43f5c94c6771 2270 extern int8_t sn_nsdl_update_resource(struct nsdl_s *handle, sn_nsdl_resource_info_s *res)
Christopher Haster 1:43f5c94c6771 2271 {
Christopher Haster 1:43f5c94c6771 2272 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2273 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2274 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2275 }
Christopher Haster 1:43f5c94c6771 2276
Christopher Haster 1:43f5c94c6771 2277 return sn_grs_update_resource(handle->grs, res);
Christopher Haster 1:43f5c94c6771 2278 }
Christopher Haster 1:43f5c94c6771 2279
Christopher Haster 1:43f5c94c6771 2280 extern int8_t sn_nsdl_send_coap_message(struct nsdl_s *handle, sn_nsdl_addr_s *address_ptr, sn_coap_hdr_s *coap_hdr_ptr)
Christopher Haster 1:43f5c94c6771 2281 {
Christopher Haster 1:43f5c94c6771 2282 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2283 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2284 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2285 }
Christopher Haster 1:43f5c94c6771 2286
Christopher Haster 1:43f5c94c6771 2287 return sn_grs_send_coap_message(handle, address_ptr, coap_hdr_ptr);
Christopher Haster 1:43f5c94c6771 2288 }
Christopher Haster 1:43f5c94c6771 2289
Christopher Haster 1:43f5c94c6771 2290 extern int8_t sn_nsdl_create_resource(struct nsdl_s *handle, sn_nsdl_resource_info_s *res)
Christopher Haster 1:43f5c94c6771 2291 {
Christopher Haster 1:43f5c94c6771 2292 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2293 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2294 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2295 }
Christopher Haster 1:43f5c94c6771 2296
Christopher Haster 1:43f5c94c6771 2297 return sn_grs_create_resource(handle->grs, res);
Christopher Haster 1:43f5c94c6771 2298 }
Christopher Haster 1:43f5c94c6771 2299
Christopher Haster 1:43f5c94c6771 2300 extern int8_t sn_nsdl_delete_resource(struct nsdl_s *handle, uint16_t pathlen, uint8_t *path)
Christopher Haster 1:43f5c94c6771 2301 {
Christopher Haster 1:43f5c94c6771 2302 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2303 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2304 return SN_NSDL_FAILURE;
Christopher Haster 1:43f5c94c6771 2305 }
Christopher Haster 1:43f5c94c6771 2306
Christopher Haster 1:43f5c94c6771 2307 return sn_grs_delete_resource(handle->grs, pathlen, path);
Christopher Haster 1:43f5c94c6771 2308 }
Christopher Haster 1:43f5c94c6771 2309 extern const sn_nsdl_resource_info_s *sn_nsdl_get_first_resource(struct nsdl_s *handle)
Christopher Haster 1:43f5c94c6771 2310 {
Christopher Haster 1:43f5c94c6771 2311 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2312 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2313 return NULL;
Christopher Haster 1:43f5c94c6771 2314 }
Christopher Haster 1:43f5c94c6771 2315
Christopher Haster 1:43f5c94c6771 2316 return sn_grs_get_first_resource(handle->grs);
Christopher Haster 1:43f5c94c6771 2317 }
Christopher Haster 1:43f5c94c6771 2318 extern const sn_nsdl_resource_info_s *sn_nsdl_get_next_resource(struct nsdl_s *handle, const sn_nsdl_resource_info_s *resource)
Christopher Haster 1:43f5c94c6771 2319 {
Christopher Haster 1:43f5c94c6771 2320 /* Check parameters */
Christopher Haster 1:43f5c94c6771 2321 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2322 return NULL;
Christopher Haster 1:43f5c94c6771 2323 }
Christopher Haster 1:43f5c94c6771 2324
Christopher Haster 1:43f5c94c6771 2325 return sn_grs_get_next_resource(handle->grs, resource);
Christopher Haster 1:43f5c94c6771 2326 }
Christopher Haster 1:43f5c94c6771 2327
Christopher Haster 1:43f5c94c6771 2328 extern sn_coap_hdr_s *sn_nsdl_build_response(struct nsdl_s *handle, sn_coap_hdr_s *coap_packet_ptr, uint8_t msg_code)
Christopher Haster 1:43f5c94c6771 2329 {
Christopher Haster 1:43f5c94c6771 2330 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2331 return NULL;
Christopher Haster 1:43f5c94c6771 2332 }
Christopher Haster 1:43f5c94c6771 2333
Christopher Haster 1:43f5c94c6771 2334 return sn_coap_build_response(handle->grs->coap, coap_packet_ptr, msg_code);
Christopher Haster 1:43f5c94c6771 2335 }
Christopher Haster 1:43f5c94c6771 2336
Christopher Haster 1:43f5c94c6771 2337 extern void sn_nsdl_release_allocated_coap_msg_mem(struct nsdl_s *handle, sn_coap_hdr_s *freed_coap_msg_ptr)
Christopher Haster 1:43f5c94c6771 2338 {
Christopher Haster 1:43f5c94c6771 2339 if (handle == NULL) {
Christopher Haster 1:43f5c94c6771 2340 return;
Christopher Haster 1:43f5c94c6771 2341 }
Christopher Haster 1:43f5c94c6771 2342
Christopher Haster 1:43f5c94c6771 2343 sn_coap_parser_release_allocated_coap_msg_mem(handle->grs->coap, freed_coap_msg_ptr);
Christopher Haster 1:43f5c94c6771 2344 }
Christopher Haster 1:43f5c94c6771 2345