Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thread_neighbor_class.c Source File

thread_neighbor_class.c

00001 /*
00002  * Copyright (c) 2018, 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 #include "nsconfig.h"
00030 #include <string.h>
00031 #include "ns_types.h"
00032 #include "ns_trace.h"
00033 #include "nsdynmemLIB.h"
00034 #include "common_functions.h"
00035 #include "NWK_INTERFACE/Include/protocol.h"
00036 #include "6LoWPAN/Thread/thread_common.h"
00037 #include "6LoWPAN/Thread/thread_routing.h"
00038 #include "6LoWPAN/Thread/thread_neighbor_class.h"
00039 
00040 
00041 static thread_neigh_table_entry_t *thread_neighbor_class_table_entry_get(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00042 {
00043     if (!class_ptr->neigh_info_list  || attribute_index >= class_ptr->list_size ) {
00044         return NULL;
00045     }
00046 
00047     thread_neigh_table_entry_t *entry = class_ptr->neigh_info_list  + attribute_index;
00048     return entry;
00049 }
00050 
00051 bool thread_neighbor_class_create(thread_neighbor_class_t *class_ptr, uint8_t neigh_table_size)
00052 {
00053     class_ptr->neigh_info_list  = ns_dyn_mem_alloc(sizeof(thread_neigh_table_entry_t) * neigh_table_size);
00054     if (!class_ptr->neigh_info_list ) {
00055         return false;
00056     }
00057 
00058     class_ptr->list_size  = neigh_table_size;
00059     thread_neigh_table_entry_t *list_ptr = class_ptr->neigh_info_list ;
00060     for (uint8_t i = 0; i < neigh_table_size; i++) {
00061         memset(list_ptr, 0, sizeof(thread_neigh_table_entry_t));
00062         list_ptr++;
00063     }
00064     return true;
00065 }
00066 
00067 void thread_neighbor_class_delete(thread_neighbor_class_t *class_ptr)
00068 {
00069     ns_dyn_mem_free(class_ptr->neigh_info_list );
00070     class_ptr->neigh_info_list  = NULL;
00071     class_ptr->list_size  = 0;
00072 }
00073 
00074 void thread_neighbor_class_add_mleid(struct thread_neighbor_class_s *class_ptr, uint8_t attribute_index, const uint8_t *mleid)
00075 {
00076     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00077     if (!entry) {
00078         return;
00079     }
00080     memcpy(entry->mlEid, mleid, 8);
00081 }
00082 
00083 uint8_t *thread_neighbor_class_get_mleid(struct thread_neighbor_class_s *class_ptr, uint8_t attribute_index)
00084 {
00085     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00086     if (!entry) {
00087         return NULL;
00088     }
00089     return entry->mlEid;
00090 }
00091 
00092 void thread_neighbor_last_communication_time_update(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00093 {
00094     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00095     if (!entry) {
00096         return;
00097     }
00098     entry->last_contact_time = protocol_core_monotonic_time;
00099 }
00100 
00101 void thread_neighbor_class_update_link(thread_neighbor_class_t *class_ptr, uint8_t attribute_index, uint8_t linkmargin, bool new_link)
00102 {
00103     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00104     if (!entry) {
00105         return;
00106     }
00107 
00108     if (new_link) {
00109         entry->link_margin = entry->link_margin + linkmargin - (entry->link_margin >> THREAD_LINK_MARGIN_SCALING);
00110     } else {
00111         entry->link_margin = linkmargin << THREAD_LINK_MARGIN_SCALING;
00112     }
00113 }
00114 
00115 uint16_t thread_neighbor_entry_linkmargin_get(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00116 {
00117     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00118     if (!entry) {
00119         return 0;
00120     }
00121     return entry->link_margin;
00122 }
00123 
00124 uint32_t thread_neighbor_last_communication_time_get(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00125 {
00126     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00127     if (!entry) {
00128         return 0;
00129     }
00130     return entry->last_contact_time;
00131 }
00132 
00133 bool thread_neighbor_class_mleid_compare(thread_neighbor_class_t *class_ptr, uint8_t attribute_index, const uint8_t *mleid)
00134 {
00135     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00136     if (!entry || memcmp(entry->mlEid, mleid, 8)) {
00137         return false;
00138     }
00139     return true;
00140 }
00141 
00142 bool thread_neighbor_class_request_full_data_setup(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00143 {
00144     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00145     if (!entry) {
00146         return false;
00147     }
00148     return entry->request_full_data_set;
00149 
00150 }
00151 
00152 bool thread_neighbor_class_secured_data_request(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00153 {
00154     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00155     if (!entry) {
00156         return false;
00157     }
00158     return entry->secured_data_request;
00159 }
00160 
00161 void thread_neighbor_class_request_full_data_setup_set(thread_neighbor_class_t *class_ptr, uint8_t attribute_index, bool value)
00162 {
00163     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00164     if (entry) {
00165         entry->request_full_data_set = value;
00166     }
00167 }
00168 
00169 void thread_neighbor_class_secured_data_request_set(thread_neighbor_class_t *class_ptr, uint8_t attribute_index, bool value)
00170 {
00171     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00172     if (entry) {
00173         entry->secured_data_request = value;
00174     }
00175 }
00176 
00177 void thread_neighbor_class_mode_parse_to_entry(thread_neighbor_class_t *class_ptr, uint8_t attribute_index, uint8_t mode)
00178 {
00179     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00180     if (entry) {
00181         entry->request_full_data_set = mode & MLE_THREAD_REQ_FULL_DATA_SET;
00182         entry->secured_data_request = mode & MLE_THREAD_SECURED_DATA_REQUEST;
00183     }
00184 }
00185 
00186 uint8_t thread_neighbor_class_mode_write_from_entry(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00187 {
00188     uint8_t mode = 0;
00189     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00190     if (entry) {
00191         if (entry->request_full_data_set) {
00192             mode |= MLE_THREAD_REQ_FULL_DATA_SET;
00193         }
00194 
00195         if (entry->secured_data_request) {
00196             mode |= MLE_THREAD_SECURED_DATA_REQUEST;
00197         }
00198     }
00199     return mode;
00200 }
00201 
00202 
00203 void thread_neighbor_class_entry_remove(thread_neighbor_class_t *class_ptr, uint8_t attribute_index)
00204 {
00205     thread_neigh_table_entry_t *entry = thread_neighbor_class_table_entry_get(class_ptr, attribute_index);
00206     if (entry) {
00207         memset(entry, 0, sizeof(thread_neigh_table_entry_t));
00208     }
00209 }