BlueSync - BLE Time Sync for Zephyr
High-precision time synchronization for BLE Mesh networks
Loading...
Searching...
No Matches
bluesync.c File Reference
#include <zephyr/kernel.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/mesh.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#include <math.h>
#include <bluesync/bluesync.h>
#include "bluesync.h"
#include "bs_state_machine.h"
#include "bluesync_bitfields.h"
#include "local_time.h"
#include <zephyr/logging/log.h>
Include dependency graph for bluesync.c:

Go to the source code of this file.

Macros

#define BT_UNIT_MS_TO_TICKS(_ms)   ((_ms) * 8 / 5)
 
#define BLUESYNC_BT_MIN_ADV_INT   BT_UNIT_MS_TO_TICKS(30)
 
#define BLUESYNC_BT_MAX_ADV_INT   BT_UNIT_MS_TO_TICKS(60)
 
#define BLUESYNC_BT_SCAN_INT   BT_UNIT_MS_TO_TICKS(60)
 
#define BLUESYNC_BT_SCAN_WIND_SIZE   BT_UNIT_MS_TO_TICKS(60)
 

Functions

 LOG_MODULE_REGISTER (bluesync, CONFIG_BLUESYNC_LOG_LEVEL)
 
 K_THREAD_STACK_DEFINE (bluesync_thread_stack, CONFIG_BLUESYNC_THREAD_STACK_SIZE)
 
 K_MSGQ_DEFINE (bluesync_rx_msgq, sizeof(struct bluesync_msg_client), 17, 1)
 
 K_SEM_DEFINE (bluesync_end_sync_sem, 0, 1)
 
 K_SEM_DEFINE (bluesync_start_new_sync_sem, 0, 1)
 
 K_SEM_DEFINE (bluesync_role_assign_sem, 0, 1)
 
static void reset_bluesync_timestamps (bluesync_timestamps_t *elem)
 
static void add_bluesync_timestamps (bluesync_timestamps_t *elem, uint8_t pos, int64_t ticks)
 
static void bluesync_reset_param ()
 
static bluesync_status_t calculate_lr_from_history (double *slope, double *offset, size_t min_nb_timestamp)
 
static void bluesync_decode_msg (struct bluesync_msg_client *msg, struct net_buf_simple *buf)
 
static void bluesync_store_current_burst ()
 
static bluesync_status_t end_sync_timeslot_process ()
 
void drift_estimation_handler (struct k_timer *timer_id)
 
static void bluesync_scan_packet_process ()
 
static bluesync_status_t bluesync_encode_msg (struct bt_data *bt_pkt, uint8_t current_round_id, uint8_t current_timeslot_idx, int64_t time_ticks)
 
static void bluesync_send_adv ()
 
void scan_cb (const bt_addr_le_t *addr, int8_t rssi, struct net_buf_simple *buf)
 
static void bt_scan_cb (const bt_addr_le_t *addr, int8_t rssi, uint8_t adv_type, struct net_buf_simple *buf)
 
static void bluesync_scan_start ()
 
static void bluesync_scan_stop ()
 
static void bluesync_adv_process ()
 
void adv_sent_cb (struct bt_le_ext_adv *adv, struct bt_le_ext_adv_sent_info *info)
 
static void bluesync_init_adv ()
 
void bs_scan_wait_for_sync_handler (void)
 
void bs_sync_handler ()
 
void bs_update_handler (void)
 
void bs_adv_handler (void)
 
void bs_stop_handler (void)
 
void bluesync_thread_fnt (void *arg1, void *arg2, void *arg3)
 
void bluesync_init ()
 Initializes the BlueSync time synchronization module.
 
void bluesync_set_role (bluesync_role_t role)
 Sets the operational role of the BlueSync node.
 
void bluesync_start_net_sync ()
 Starts a BlueSync synchronization round as the time authority.
 
void bluesync_start_net_sync_with_unix_epoch_us (uint64_t unix_epoch_us)
 Starts a synchronization round using a known UNIX epoch time.
 

Variables

static struct bluesync_param param
 
static uint8_t bt_packet_buf [sizeof(uint16_t)+sizeof(struct bluesync_msg)] = {0}
 
static struct bt_le_ext_adv_cb bt_callbacks
 
struct bs_sm_handlers handlers
 

Macro Definition Documentation

◆ BLUESYNC_BT_MAX_ADV_INT

#define BLUESYNC_BT_MAX_ADV_INT   BT_UNIT_MS_TO_TICKS(60)

Definition at line 54 of file bluesync.c.

◆ BLUESYNC_BT_MIN_ADV_INT

#define BLUESYNC_BT_MIN_ADV_INT   BT_UNIT_MS_TO_TICKS(30)

Definition at line 53 of file bluesync.c.

◆ BLUESYNC_BT_SCAN_INT

#define BLUESYNC_BT_SCAN_INT   BT_UNIT_MS_TO_TICKS(60)

Definition at line 55 of file bluesync.c.

◆ BLUESYNC_BT_SCAN_WIND_SIZE

#define BLUESYNC_BT_SCAN_WIND_SIZE   BT_UNIT_MS_TO_TICKS(60)

Definition at line 56 of file bluesync.c.

◆ BT_UNIT_MS_TO_TICKS

#define BT_UNIT_MS_TO_TICKS (   _ms)    ((_ms) * 8 / 5)

Definition at line 52 of file bluesync.c.

Function Documentation

◆ add_bluesync_timestamps()

static void add_bluesync_timestamps ( bluesync_timestamps_t elem,
uint8_t  pos,
int64_t  ticks 
)
static

Definition at line 124 of file bluesync.c.

◆ adv_sent_cb()

void adv_sent_cb ( struct bt_le_ext_adv *  adv,
struct bt_le_ext_adv_sent_info *  info 
)

Definition at line 518 of file bluesync.c.

◆ bluesync_adv_process()

static void bluesync_adv_process ( )
static

Definition at line 511 of file bluesync.c.

◆ bluesync_decode_msg()

static void bluesync_decode_msg ( struct bluesync_msg_client msg,
struct net_buf_simple *  buf 
)
static

Definition at line 223 of file bluesync.c.

◆ bluesync_encode_msg()

static bluesync_status_t bluesync_encode_msg ( struct bt_data *  bt_pkt,
uint8_t  current_round_id,
uint8_t  current_timeslot_idx,
int64_t  time_ticks 
)
static

Definition at line 362 of file bluesync.c.

◆ bluesync_init_adv()

static void bluesync_init_adv ( )
static

Definition at line 540 of file bluesync.c.

◆ bluesync_reset_param()

static void bluesync_reset_param ( )
static

Definition at line 139 of file bluesync.c.

◆ bluesync_scan_packet_process()

static void bluesync_scan_packet_process ( )
static

Definition at line 293 of file bluesync.c.

◆ bluesync_scan_start()

static void bluesync_scan_start ( )
static

Definition at line 481 of file bluesync.c.

◆ bluesync_scan_stop()

static void bluesync_scan_stop ( )
static

Definition at line 494 of file bluesync.c.

◆ bluesync_send_adv()

static void bluesync_send_adv ( )
static

Definition at line 394 of file bluesync.c.

◆ bluesync_store_current_burst()

static void bluesync_store_current_burst ( )
static

Definition at line 233 of file bluesync.c.

◆ bluesync_thread_fnt()

void bluesync_thread_fnt ( void *  arg1,
void *  arg2,
void *  arg3 
)

Definition at line 597 of file bluesync.c.

◆ bs_adv_handler()

void bs_adv_handler ( void  )

Definition at line 574 of file bluesync.c.

◆ bs_scan_wait_for_sync_handler()

void bs_scan_wait_for_sync_handler ( void  )

Definition at line 550 of file bluesync.c.

◆ bs_stop_handler()

void bs_stop_handler ( void  )

Definition at line 582 of file bluesync.c.

◆ bs_sync_handler()

void bs_sync_handler ( )

Definition at line 557 of file bluesync.c.

◆ bs_update_handler()

void bs_update_handler ( void  )

Definition at line 561 of file bluesync.c.

◆ bt_scan_cb()

static void bt_scan_cb ( const bt_addr_le_t *  addr,
int8_t  rssi,
uint8_t  adv_type,
struct net_buf_simple *  buf 
)
static

Definition at line 473 of file bluesync.c.

◆ calculate_lr_from_history()

static bluesync_status_t calculate_lr_from_history ( double *  slope,
double *  offset,
size_t  min_nb_timestamp 
)
static

Definition at line 154 of file bluesync.c.

◆ drift_estimation_handler()

void drift_estimation_handler ( struct k_timer *  timer_id)

Definition at line 289 of file bluesync.c.

◆ end_sync_timeslot_process()

static bluesync_status_t end_sync_timeslot_process ( )
static

Definition at line 259 of file bluesync.c.

◆ K_MSGQ_DEFINE()

K_MSGQ_DEFINE ( bluesync_rx_msgq  ,
sizeof(struct bluesync_msg_client ,
17  ,
 
)

◆ K_SEM_DEFINE() [1/3]

K_SEM_DEFINE ( bluesync_end_sync_sem  ,
,
 
)

◆ K_SEM_DEFINE() [2/3]

K_SEM_DEFINE ( bluesync_role_assign_sem  ,
,
 
)

◆ K_SEM_DEFINE() [3/3]

K_SEM_DEFINE ( bluesync_start_new_sync_sem  ,
,
 
)

◆ K_THREAD_STACK_DEFINE()

K_THREAD_STACK_DEFINE ( bluesync_thread_stack  ,
CONFIG_BLUESYNC_THREAD_STACK_SIZE   
)

◆ LOG_MODULE_REGISTER()

LOG_MODULE_REGISTER ( bluesync  ,
CONFIG_BLUESYNC_LOG_LEVEL   
)

◆ reset_bluesync_timestamps()

static void reset_bluesync_timestamps ( bluesync_timestamps_t elem)
static

Definition at line 119 of file bluesync.c.

◆ scan_cb()

void scan_cb ( const bt_addr_le_t *  addr,
int8_t  rssi,
struct net_buf_simple *  buf 
)

Definition at line 428 of file bluesync.c.

Variable Documentation

◆ bt_callbacks

struct bt_le_ext_adv_cb bt_callbacks
static
Initial value:
= {
.sent = adv_sent_cb,
}
void adv_sent_cb(struct bt_le_ext_adv *adv, struct bt_le_ext_adv_sent_info *info)
Definition bluesync.c:518

Definition at line 536 of file bluesync.c.

◆ bt_packet_buf

uint8_t bt_packet_buf[sizeof(uint16_t)+sizeof(struct bluesync_msg)] = {0}
static

Definition at line 360 of file bluesync.c.

◆ handlers

struct bs_sm_handlers handlers
Initial value:
={
.bs_scan_wait_for_sync_cb = bs_scan_wait_for_sync_handler,
.bs_sync_cb = bs_sync_handler,
.bs_update_cb = bs_update_handler,
.bs_adv_cb = bs_adv_handler,
.bs_stop_cb = bs_stop_handler,
}
void bs_scan_wait_for_sync_handler(void)
Definition bluesync.c:550
void bs_stop_handler(void)
Definition bluesync.c:582
void bs_update_handler(void)
Definition bluesync.c:561
void bs_adv_handler(void)
Definition bluesync.c:574
void bs_sync_handler()
Definition bluesync.c:557

Definition at line 586 of file bluesync.c.

◆ param

struct bluesync_param param
static

Definition at line 60 of file bluesync.c.