Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers channel_list.c Source File

channel_list.c

00001 /*
00002  * Copyright (c) 2015-2018, 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 
00019 #include "nsconfig.h"
00020 #include "Service_Libs/fhss/channel_list.h"
00021 
00022 #include "common_functions.h"
00023 #include "ns_trace.h"
00024 
00025 #include <stdint.h>
00026 #include <string.h>
00027 
00028 const int CHANNEL_LIST_SIZE_IN_BITS = 8 * 32;
00029 
00030 static bool channel_list_bit_test32(uint32_t word, int_fast8_t bit_number);
00031 static bool channel_list_bit_test(const uint32_t *list, int bit_number);
00032 
00033 // test bit by number
00034 static bool channel_list_bit_test32(uint32_t word, int_fast8_t bit_number)
00035 {
00036     bool bitSet;
00037 
00038     if (word & ((uint32_t) 1 << bit_number)) {
00039         bitSet = true;
00040     } else {
00041         bitSet = false;
00042     }
00043     return bitSet;
00044 }
00045 
00046 static bool channel_list_bit_test(const uint32_t *list, int bit_number)
00047 {
00048     const int_fast8_t word_index = bit_number / 32;
00049     const int_fast8_t bit_index = bit_number % 32;
00050 
00051     return channel_list_bit_test32(list[word_index], bit_index);
00052 }
00053 
00054 static uint8_t channel_list_search(const uint32_t *list, int index)
00055 {
00056     uint8_t channel = 0;
00057     int enabled_channels = 0;
00058     int i, j;
00059 
00060     for (j = 0; j < 8; j++) {
00061         for (i = 0; i < 32; i++) {
00062             if (list[j] & ((uint32_t)1 << i)) {
00063                 enabled_channels++;
00064                 if (enabled_channels == (index + 1)) {
00065                     goto exit;
00066                 }
00067             }
00068             channel++;
00069         }
00070     }
00071 exit:
00072     return channel;
00073 
00074 }
00075 
00076 uint8_t channel_list_get_channel(const uint32_t *list, int current_index)
00077 {
00078     uint8_t found_index;
00079 
00080     if (current_index >= CHANNEL_LIST_SIZE_IN_BITS) {
00081         current_index = 0;
00082     }
00083 
00084     found_index = channel_list_search(list, current_index);
00085 
00086     return found_index;
00087 }
00088 
00089 void channel_list_set_channel(uint32_t *list, int channel, bool active)
00090 {
00091     if (channel >= CHANNEL_LIST_SIZE_IN_BITS) {
00092         return;
00093     }
00094     if (active) {
00095         list[channel / 32] |= (1 << channel % 32);
00096     } else {
00097         list[channel / 32] &= ~(1 << channel % 32);
00098     }
00099     return;
00100 }
00101 
00102 // count the amount of channels enabled in a list
00103 int channel_list_count_channels(const uint32_t *list)
00104 {
00105 
00106     int channel_count = 0;
00107 
00108     for (int index = 0; index < CHANNEL_LIST_SIZE_IN_BITS; index++) {
00109 
00110         if (channel_list_bit_test(list, index)) {
00111             channel_count++;
00112         }
00113     }
00114 
00115     return channel_count;
00116 }