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