BlueSync - BLE Time Sync for Zephyr
High-precision time synchronization for BLE Mesh networks
Loading...
Searching...
No Matches
bluesync.h
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.h
17 * Description: Private API for the BlueSync time synchronization module.
18 *
19 * Project: BlueSync - BLE Time Sync for Zephyr
20 * Repository: https://github.com/Tobi15/zephyr-bluesync-ble
21 */
22
23
24#ifndef TIME_SYNC_BLUESYNC_H_
25#define TIME_SYNC_BLUESYNC_H_
26
27#include <zephyr/kernel.h>
28#include <zephyr/bluetooth/bluetooth.h>
29
30#define SLOT_NUMBER CONFIG_BLUESYNC_SLOTS_IN_BURST
31#define NB_BYTES_BITFIELD (SLOT_NUMBER + 7) / 8
32#define BLUESYNC_TIMESTAMP_ARRAY_SIZE SLOT_NUMBER +1
33
34#define BURST_WINDOWS_SIZE CONFIG_BLUESYNC_BURST_WINDOWS_SIZE
35
36#define MY_MANUFACTURER_ID 0x1234
37
48
49
50typedef struct {
51 uint8_t bitfield[NB_BYTES_BITFIELD];
52 uint64_t timer_ticks[SLOT_NUMBER];
53#if defined(CONFIG_BLUESYNC_TEST_BABBLESIM_SUPPORT)
54 uint64_t remote_est_ticks[SLOT_NUMBER];
55#endif
57
59 // curent timeslot index
61
62 //########### RCV ###############
63 // curent rcv burst
65 struct k_mutex rcv_mutex;
66
67 // Ring buffer for received bursts
69 uint8_t rcv_head;
70 uint8_t rcv_count;
71 struct k_mutex rcv_history_mutex;
72
73 //########## LOCAL ###############
74 // curent local burst
76 struct k_mutex local_mutex;
77
78 // Ring buffer for local bursts
80 uint8_t local_head;
81 uint8_t local_count;
82 struct k_mutex local_history_mutex;
83
84 //########## BLE #################
85 struct bt_le_ext_adv *adv;
86 struct bt_le_adv_param adv_param;
87 struct bt_le_scan_param scan_param;
88
89 struct k_work_delayable bluesync_adv_delayed_work;
90
91 struct k_timer drift_estimation_timer;
92 // Worker used to perform slave synchronisation
94
95 // round id with which node is synchronized with
97 uint8_t new_round_id;
98
99 struct k_mutex mutex;
100 struct k_thread bluesync_thread;
101};
102
115
116
127#if defined(CONFIG_BLUESYNC_TEST_BABBLESIM_SUPPORT)
128 uint64_t master_estimation_ticks;
129#endif
130};
131
132#endif /* TIME_SYNC_BLUESYNC_H_ */
bluesync_status_t
Definition bluesync.h:38
@ BLUESYNC_TIMEOUT_STATUS
Definition bluesync.h:43
@ BLUESYNC_BUSY_STATUS
Definition bluesync.h:45
@ BLUESYNC_INVALID_PARAM_STATUS
Definition bluesync.h:44
@ BLUESYNC_BUF_WRONG_SIZE
Definition bluesync.h:46
@ BLUESYNC_NO_ENOUGH_DATA_STATUS
Definition bluesync.h:41
@ BLUESYNC_NO_VALID_DATA_STATUS
Definition bluesync.h:40
@ BLUESYNC_SUCCESS_STATUS
Definition bluesync.h:39
@ BLUESYNC_DENOMINATOR_TOO_SMALL
Definition bluesync.h:42
#define BURST_WINDOWS_SIZE
Definition bluesync.h:34
struct bluesync_msg __packed
#define NB_BYTES_BITFIELD
Definition bluesync.h:31
#define SLOT_NUMBER
Definition bluesync.h:30
Encapsulated BlueSync message received by the client during synchronization.
Definition bluesync.h:124
uint64_t client_timer_ticks
Definition bluesync.h:126
struct bluesync_msg rcv
Definition bluesync.h:125
Message structure used in BlueSync time synchronization exchanges.
Definition bluesync.h:110
uint8_t round_id
Definition bluesync.h:111
uint8_t index_timeslot
Definition bluesync.h:112
uint64_t master_timer_ticks
Definition bluesync.h:113
uint8_t rcv_head
Definition bluesync.h:69
uint8_t current_round_id
Definition bluesync.h:96
struct k_work end_sync_timeslot_worker
Definition bluesync.h:93
struct k_mutex local_history_mutex
Definition bluesync.h:82
bluesync_timestamps_t local
Definition bluesync.h:75
struct k_thread bluesync_thread
Definition bluesync.h:100
struct k_mutex mutex
Definition bluesync.h:99
uint8_t rcv_count
Definition bluesync.h:70
struct bt_le_scan_param scan_param
Definition bluesync.h:87
uint8_t local_head
Definition bluesync.h:80
struct k_timer drift_estimation_timer
Definition bluesync.h:91
struct k_mutex rcv_history_mutex
Definition bluesync.h:71
bluesync_timestamps_t local_history[BURST_WINDOWS_SIZE]
Definition bluesync.h:79
uint8_t local_count
Definition bluesync.h:81
struct bt_le_ext_adv * adv
Definition bluesync.h:85
uint8_t new_round_id
Definition bluesync.h:97
struct k_mutex rcv_mutex
Definition bluesync.h:65
struct k_work_delayable bluesync_adv_delayed_work
Definition bluesync.h:89
bluesync_timestamps_t rcv
Definition bluesync.h:64
struct k_mutex local_mutex
Definition bluesync.h:76
uint8_t timeslot_index
Definition bluesync.h:60
bluesync_timestamps_t rcv_history[BURST_WINDOWS_SIZE]
Definition bluesync.h:68
struct bt_le_adv_param adv_param
Definition bluesync.h:86