EL4121 Embedded System / mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

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 
00095 
00096 bool pan_blacklist_filter(pan_blaclist_cache_s *list_ptr, uint16_t panid)
00097 {
00098     if (pan_blacklist_entry_find(list_ptr, panid) ) {
00099         return true;
00100     }
00101     return false;
00102 }
00103 
00104 bool pan_cordinator_blacklist_filter(pan_coordinator_blaclist_cache_s *list_ptr, uint8_t *compare_data)
00105 {
00106     if (pan_cordinator_blacklist_entry_find(list_ptr, compare_data) ) {
00107         return true;
00108     }
00109     return false;
00110 }
00111 
00112 
00113 static pan_blaclist_entry_t *pan_blacklist_entry_find(pan_blaclist_cache_s *list_ptr, uint16_t pan_id)
00114 {
00115     ns_list_foreach(pan_blaclist_entry_t, cur_ptr, &list_ptr->head) {
00116         if (cur_ptr->pan_id == pan_id) {
00117             return cur_ptr;
00118         }
00119     }
00120     return NULL;
00121 }
00122 
00123 static pan_blaclist_entry_t *pan_blacklist_entry_allocate(pan_blaclist_cache_s *list_ptr)
00124 {
00125     pan_blaclist_entry_t *entry = ns_dyn_mem_alloc(sizeof(pan_blaclist_entry_t));
00126     if (entry) {
00127         //Add to list
00128         ns_list_add_to_end(&list_ptr->head, entry);
00129     }
00130     return entry;
00131 }
00132 
00133 static pan_coordinator_blacklist_entry_t *pan_cordinator_blacklist_entry_find(pan_coordinator_blaclist_cache_s *list_ptr, uint8_t *compare_data)
00134 {
00135     ns_list_foreach(pan_coordinator_blacklist_entry_t, cur_ptr, &list_ptr->head) {
00136         if (memcmp(cur_ptr->coordinator_pan_address, compare_data, 10) == 0) {
00137             return cur_ptr;
00138         }
00139     }
00140     return NULL;
00141 }
00142 
00143 static pan_coordinator_blacklist_entry_t *pan_coordinator_blacklist_entry_allocate(pan_coordinator_blaclist_cache_s *list_ptr)
00144 {
00145     pan_coordinator_blacklist_entry_t *entry = ns_dyn_mem_alloc(sizeof(pan_coordinator_blacklist_entry_t));
00146     if (entry) {
00147         //Add to list
00148         ns_list_add_to_end(&list_ptr->head, entry);
00149     }
00150     return entry;
00151 }
00152 
00153 
00154