Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of OmniWheels by
thread_resolution_server.c
00001 /* 00002 * Copyright (c) 2014-2017, Arm Limited and affiliates. 00003 * SPDX-License-Identifier: BSD-3-Clause 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the copyright holder nor the 00014 * names of its contributors may be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 00031 #include "nsconfig.h" 00032 #ifdef HAVE_THREAD_NEIGHBOR_DISCOVERY 00033 #include <string.h> 00034 #include <ns_types.h> 00035 #include <ns_list.h> 00036 #include <ns_trace.h> 00037 #include "nsdynmemLIB.h" 00038 #include "common_functions.h" 00039 #include "6LoWPAN/Thread/thread_resolution_server.h" 00040 00041 #define TRACE_GROUP "TRsS" 00042 00043 #include "coap_service_api.h" 00044 00045 #include "net_interface.h" 00046 #include "socket_api.h" 00047 #include "thread_config.h" 00048 #include "thread_tmfcop_lib.h" 00049 #include "thread_management_if.h" 00050 #include "thread_commissioning_if.h" 00051 00052 00053 typedef struct thread_management_server { 00054 int8_t interface_id; 00055 int8_t coap_service_id; 00056 thread_resolution_server_addr_query_cb *query_cb_ptr; 00057 ns_list_link_t link; 00058 } thread_resolution_server_t; 00059 00060 static NS_LIST_DEFINE(instance_list, thread_resolution_server_t, link); 00061 00062 static thread_resolution_server_t *thread_resolution_server_find(int8_t interface_id) 00063 { 00064 ns_list_foreach(thread_resolution_server_t, cur_ptr, &instance_list) { 00065 if (cur_ptr->interface_id == interface_id) { 00066 return cur_ptr; 00067 } 00068 } 00069 return NULL; 00070 } 00071 00072 static thread_resolution_server_t *thread_management_find_by_service(int8_t service_id) 00073 { 00074 ns_list_foreach(thread_resolution_server_t, cur_ptr, &instance_list) { 00075 if (cur_ptr->coap_service_id == service_id) { 00076 return cur_ptr; 00077 } 00078 } 00079 return NULL; 00080 } 00081 00082 /** 00083 * Address query 00084 * uri = tn/d/aq 00085 */ 00086 static int thread_resolution_server_query_cb(int8_t service_id, uint8_t source_address[16], uint16_t source_port, sn_coap_hdr_s *request_ptr) 00087 { 00088 thread_resolution_server_t *this = thread_management_find_by_service(service_id); 00089 uint8_t payload[2 + 16 + 2 + 2 + 2 + 8 + 2 + 4]; 00090 uint8_t *target_ip_ptr; 00091 uint8_t mlEID[8]; 00092 bool proxy; 00093 uint16_t rloc16; 00094 uint32_t last_transaction_time = 0; 00095 uint8_t *ptr; 00096 (void)source_port; 00097 00098 /* Address queries should be a multicast, non-confirmed POST. Basic CoAP 00099 * protocol dictates that we should respond in some way if it's not multicast, 00100 * but we currently can't distinguish. (RFC 7390 says "A server may choose 00101 * not to respond to an IP multicast request if there's nothing useful to 00102 * respond back", but that doesn't extend to unicast). So we just drop in 00103 * all cases of non-resolution. People shouldn't(TM) be sending unicast 00104 * queries anyway... 00105 * 00106 * Note that even when we have an answer, we don't actually send a CoAP 00107 * response to the POST, but we instead trigger a new unicast confirmed 00108 * POST to the sender. 00109 */ 00110 00111 if (!this || !this->query_cb_ptr) { 00112 /* XXX "Internal Server Error" response? */ 00113 return -1; 00114 } 00115 00116 if (16 > thread_tmfcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_TARGET_EID, &target_ip_ptr)) { 00117 /* XXX "Bad Request" response? */ 00118 return -1; 00119 } 00120 00121 tr_debug("Thread address query %s", trace_ipv6(target_ip_ptr)); 00122 00123 int ret = this->query_cb_ptr(this->interface_id, target_ip_ptr, &rloc16, &proxy, &last_transaction_time, mlEID); 00124 if (ret < 0) { 00125 /* XXX "Forbidden" response? */ 00126 return -1; 00127 } 00128 00129 tr_debug("Thread query locator %x", rloc16); 00130 00131 ptr = payload; 00132 ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_TARGET_EID, 16, target_ip_ptr); 00133 ptr = thread_tmfcop_tlv_data_write_uint16(ptr, TMFCOP_TLV_RLOC16, rloc16); 00134 ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_ML_EID, 8, mlEID); 00135 if (proxy) { 00136 tr_debug(" proxy: last transaction %" PRIu32, last_transaction_time); 00137 ptr = thread_tmfcop_tlv_data_write_uint32(ptr, TMFCOP_TLV_LAST_TRANSACTION_TIME, last_transaction_time); 00138 } 00139 00140 // XXX "Accepted" response? 00141 00142 /* We don't require a response, so we don't specify a callback. Library 00143 * should retry itself until it gets an ACK. 00144 */ 00145 coap_service_request_send(this->coap_service_id, COAP_REQUEST_OPTIONS_ADDRESS_SHORT, 00146 source_address, THREAD_MANAGEMENT_PORT, 00147 COAP_MSG_TYPE_CONFIRMABLE, COAP_MSG_CODE_REQUEST_POST, 00148 THREAD_URI_ADDRESS_NOTIFICATION, COAP_CT_OCTET_STREAM, 00149 payload, ptr - payload, NULL); 00150 00151 return -1;// 0 is expecting response to the message as this is different behaviour need to handle it 00152 } 00153 00154 /** 00155 * Public interface functions 00156 */ 00157 int thread_resolution_server_init(int8_t interface_id, thread_resolution_server_addr_query_cb *query_cb) 00158 { 00159 thread_resolution_server_t *this = thread_resolution_server_find(interface_id); 00160 if (this) { 00161 return -1; 00162 } 00163 00164 this = ns_dyn_mem_alloc(sizeof(thread_resolution_server_t)); 00165 if (!this) { 00166 return -2; 00167 } 00168 00169 this->interface_id = interface_id; 00170 this->coap_service_id = coap_service_initialize(this->interface_id, THREAD_MANAGEMENT_PORT, COAP_SERVICE_OPTIONS_NONE, NULL, NULL); 00171 if (this->coap_service_id < 0) { 00172 tr_warn("Thread resolution init failed"); 00173 ns_dyn_mem_free(this); 00174 return -3; 00175 } 00176 this->query_cb_ptr = query_cb; 00177 ns_list_add_to_start(&instance_list, this); 00178 00179 coap_service_register_uri(this->coap_service_id, THREAD_URI_ADDRESS_QUERY_REQUEST, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_resolution_server_query_cb); 00180 00181 return 0; 00182 } 00183 00184 void thread_resolution_server_delete(int8_t interface_id) 00185 { 00186 thread_resolution_server_t *this = thread_resolution_server_find(interface_id); 00187 if (!this) { 00188 return; 00189 } 00190 00191 coap_service_unregister_uri(this->coap_service_id, THREAD_URI_ADDRESS_QUERY_REQUEST); 00192 00193 coap_service_delete(this->coap_service_id); 00194 ns_list_remove(&instance_list, this); 00195 ns_dyn_mem_free(this); 00196 } 00197 00198 #endif //HAVE_THREAD_NEIGHBOR_DISCOVERY 00199
Generated on Fri Jul 22 2022 04:54:03 by
 1.7.2
 1.7.2 
    