Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pan_blacklist.c Source File

pan_blacklist.c

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #include "nsconfig.h"
00019 #include <string.h>
00020 #include <ns_types.h>
00021 #include "ns_trace.h"
00022 #include "nsdynmemLIB.h"
00023 #include "ns_list.h"
00024 #include "pan_blacklist_api.h"
00025 
00026 #define TRACE_GROUP "pbl"
00027 static pan_blaclist_entry_t *pan_blacklist_entry_find(pan_blaclist_cache_s *list_ptr, uint16_t pan_id);
00028 static pan_blaclist_entry_t *pan_blacklist_entry_allocate(pan_blaclist_cache_s *list_ptr);
00029 static pan_coordinator_blacklist_entry_t *pan_cordinator_blacklist_entry_find(pan_coordinator_blaclist_cache_s *list_ptr, uint8_t *compare_data);
00030 static pan_coordinator_blacklist_entry_t *pan_coordinator_blacklist_entry_allocate(pan_coordinator_blaclist_cache_s *list_ptr);
00031 
00032 void pan_blacklist_cache_init(pan_blaclist_cache_s *blacklist_cache)
00033 {
00034     ns_list_init(&blacklist_cache->head);
00035 }
00036 
00037 void pan_coordinator_blacklist_cache_init(pan_coordinator_blaclist_cache_s *blacklist_cache)
00038 {
00039     ns_list_init(&blacklist_cache->head);
00040 }
00041 
00042 void pan_blacklist_pan_set(pan_blaclist_cache_s *list_ptr, uint16_t panid, uint16_t timeout)
00043 {
00044     pan_blaclist_entry_t *entry = pan_blacklist_entry_find(list_ptr, panid);
00045     if (!entry) {
00046         //Allocate entry
00047         entry = pan_blacklist_entry_allocate(list_ptr);
00048     }
00049 
00050     if(entry) {
00051         entry->pan_id = panid;
00052         entry->timeout_in_seconds = timeout;
00053     }
00054 }
00055 
00056 void pan_cordinator_blacklist_pan_set(pan_coordinator_blaclist_cache_s *list_ptr, uint8_t *cordinator_data, uint16_t timeout)
00057 {
00058     pan_coordinator_blacklist_entry_t *entry = pan_cordinator_blacklist_entry_find(list_ptr, cordinator_data);
00059     if (!entry) {
00060         //Allocate entry
00061         entry = pan_coordinator_blacklist_entry_allocate(list_ptr);
00062     }
00063 
00064     if(entry) {
00065         memcpy(entry->coordinator_pan_address, cordinator_data, 10);
00066         entry->timeout_in_seconds = timeout;
00067     }
00068 }
00069 
00070 void pan_blacklist_time_update(pan_blaclist_cache_s *list_ptr, uint16_t time_update_in_seconds)
00071 {
00072     ns_list_foreach_safe(pan_blaclist_entry_t, cur_ptr, &list_ptr->head) {
00073         if (cur_ptr->timeout_in_seconds > time_update_in_seconds) {
00074             cur_ptr->timeout_in_seconds -= time_update_in_seconds;
00075         } else {
00076             ns_list_remove(&list_ptr->head, cur_ptr);
00077             ns_dyn_mem_free(cur_ptr);
00078         }
00079     }
00080 }
00081 
00082 void pan_coordinator_blacklist_time_update(pan_coordinator_blaclist_cache_s *list_ptr, uint16_t time_update_in_seconds)
00083 {
00084     ns_list_foreach_safe(pan_coordinator_blacklist_entry_t, cur_ptr, &list_ptr->head) {
00085         if (cur_ptr->timeout_in_seconds > time_update_in_seconds) {
00086             cur_ptr->timeout_in_seconds -= time_update_in_seconds;
00087         } else {
00088             ns_list_remove(&list_ptr->head, cur_ptr);
00089             ns_dyn_mem_free(cur_ptr);
00090         }
00091     }
00092 }
00093 
00094 void pan_coordinator_blacklist_free(pan_coordinator_blaclist_cache_s *list_ptr)
00095 {
00096     ns_list_foreach_safe(pan_coordinator_blacklist_entry_t, cur_ptr, &list_ptr->head) {
00097         ns_list_remove(&list_ptr->head, cur_ptr);
00098         ns_dyn_mem_free(cur_ptr);
00099     }
00100 }
00101 
00102 bool pan_blacklist_filter(pan_blaclist_cache_s *list_ptr, uint16_t panid)
00103 {
00104     if (pan_blacklist_entry_find(list_ptr, panid) ) {
00105         return true;
00106     }
00107     return false;
00108 }
00109 
00110 bool pan_cordinator_blacklist_filter(pan_coordinator_blaclist_cache_s *list_ptr, uint8_t *compare_data)
00111 {
00112     if (pan_cordinator_blacklist_entry_find(list_ptr, compare_data) ) {
00113         return true;
00114     }
00115     return false;
00116 }
00117 
00118 
00119 static pan_blaclist_entry_t *pan_blacklist_entry_find(pan_blaclist_cache_s *list_ptr, uint16_t pan_id)
00120 {
00121     ns_list_foreach(pan_blaclist_entry_t, cur_ptr, &list_ptr->head) {
00122         if (cur_ptr->pan_id == pan_id) {
00123             return cur_ptr;
00124         }
00125     }
00126     return NULL;
00127 }
00128 
00129 static pan_blaclist_entry_t *pan_blacklist_entry_allocate(pan_blaclist_cache_s *list_ptr)
00130 {
00131     pan_blaclist_entry_t *entry = ns_dyn_mem_alloc(sizeof(pan_blaclist_entry_t));
00132     if (entry) {
00133         //Add to list
00134         ns_list_add_to_end(&list_ptr->head, entry);
00135     }
00136     return entry;
00137 }
00138 
00139 static pan_coordinator_blacklist_entry_t *pan_cordinator_blacklist_entry_find(pan_coordinator_blaclist_cache_s *list_ptr, uint8_t *compare_data)
00140 {
00141     ns_list_foreach(pan_coordinator_blacklist_entry_t, cur_ptr, &list_ptr->head) {
00142         if (memcmp(cur_ptr->coordinator_pan_address, compare_data, 10) == 0) {
00143             return cur_ptr;
00144         }
00145     }
00146     return NULL;
00147 }
00148 
00149 static pan_coordinator_blacklist_entry_t *pan_coordinator_blacklist_entry_allocate(pan_coordinator_blaclist_cache_s *list_ptr)
00150 {
00151     pan_coordinator_blacklist_entry_t *entry = ns_dyn_mem_alloc(sizeof(pan_coordinator_blacklist_entry_t));
00152     if (entry) {
00153         //Add to list
00154         ns_list_add_to_end(&list_ptr->head, entry);
00155     }
00156     return entry;
00157 }
00158 
00159 
00160