BlueSync - BLE Time Sync for Zephyr
High-precision time synchronization for BLE Mesh networks
Loading...
Searching...
No Matches
bluesync_bitfields.c
Go to the documentation of this file.
1/*
2 * Copyright 2025 Tobias Moullet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * File: bluesync_bitfields.c
17 * Description: Bitfield file to keep in touch which timeslot is received
18 *
19 * Project: BlueSync - BLE Time Sync for Zephyr
20 * Repository: https://github.com/Tobi15/zephyr-bluesync-ble
21 */
22
23#include <zephyr/kernel.h>
24#include "stdio.h"
25#include "bluesync_bitfields.h"
26
27
28//********************BITFIELD********************************* */
29void print_bitfield_as_binary(uint8_t *bitfield, size_t size) {
30 printk("0x");
31 for (size_t i = size; i-- > 0;) {
32 printk("%02X", bitfield[i]);
33 }
34 printk("\n");
35}
36
37void set_bit(uint8_t *bitfield, size_t bit_index) {
38 bitfield[bit_index / 8] |= BIT(bit_index % 8);
39}
40
41bool is_bit_set(uint8_t *bitfield, size_t bit_index) {
42 return bitfield[bit_index / 8] & BIT(bit_index % 8);
43}
44
45void bitwise_and_bitfields(uint8_t *result, const bluesync_timestamps_t *rcv, const bluesync_timestamps_t *local, size_t num_bytes) {
46 for (size_t i = 0; i < num_bytes; i++) {
47 result[i] = rcv->bitfield[i] & local->bitfield[i];
48 }
49}
50
51// Count the number of 1 bits in a byte
52static inline uint8_t count_bits_in_byte(uint8_t byte) {
53 // Using GCC built-in function if available (more efficient)
54 #if defined(__GNUC__)
55 return __builtin_popcount(byte);
56 #else
57 // Fallback method: Count bits manually
58 uint8_t count = 0;
59 while (byte) {
60 count += byte & 1;
61 byte >>= 1;
62 }
63 return count;
64 #endif
65}
66
67// Count the total number of 1 bits in the bitfield
68size_t count_set_bits(uint8_t *bitfield, size_t num_bytes) {
69 size_t total_count = 0;
70 for (size_t i = 0; i < num_bytes; i++) {
71 total_count += count_bits_in_byte(bitfield[i]);
72 }
73 return total_count;
74}
void print_bitfield_as_binary(uint8_t *bitfield, size_t size)
void bitwise_and_bitfields(uint8_t *result, const bluesync_timestamps_t *rcv, const bluesync_timestamps_t *local, size_t num_bytes)
Perform a bitwise AND between two bitfields and store the result.
size_t count_set_bits(uint8_t *bitfield, size_t num_bytes)
Count the number of bits set to 1 in a bitfield.
void set_bit(uint8_t *bitfield, size_t bit_index)
Set a specific bit in the bitfield.
bool is_bit_set(uint8_t *bitfield, size_t bit_index)
Check if a specific bit is set in the bitfield.
static uint8_t count_bits_in_byte(uint8_t byte)
static struct local_time local
Definition local_time.c:53
uint8_t bitfield[NB_BYTES_BITFIELD]
Definition bluesync.h:51