BlueSync - BLE Time Sync for Zephyr
High-precision time synchronization for BLE Mesh networks
Loading...
Searching...
No Matches
bluesync_statistic_bsim.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_statistic_bsim.c
17 * Description: File implementing the required method to use babblesim
18 *
19 * Project: BlueSync - BLE Time Sync for Zephyr
20 * Repository: https://github.com/Tobi15/zephyr-bluesync-ble
21 */
22
23
24
25#include <zephyr/kernel.h>
26
27//#include <posix_native_task.h>
28#include "posix_native_task.h"
29//#include <bsim_args_runner.h>
30#include "bsim_args_runner.h"
31
32#include <stdio.h>
33#include <string.h>
34
35#include "bluesync_statistic.h"
36#include "../bluesync_bitfields.h"
37
38#include <zephyr/logging/log.h>
39
40#include "synced_time_logger.h"
41
42LOG_MODULE_REGISTER(bluesync_statistic_bsim, CONFIG_BLUESYNC_LOG_LEVEL);
43
44#define SEPARATION_TOKEN ";"
45
46struct node_stat {
47 struct k_mutex mutex;
48 FILE *file;
49};
50
51static struct node_stat node;
52
60
62 k_mutex_lock(&node.mutex, K_FOREVER);
63 {
64 fprintf(node.file,
66 "\n",
67 packet_status->idx, packet_status->master_timer_ticks, packet_status->client_timer_ticks,
68 packet_status->estimation_master_ticks,
69 packet_status->valid);
70 }
71 k_mutex_unlock(&node.mutex);
72}
73
74
75void statistic_bluesync_status(bluesync_timestamps_t *elem_master, bluesync_timestamps_t *elem_slave, size_t size) {
76
77 uint8_t result_bitfield[NB_BYTES_BITFIELD] = {0};
78
79 // Compute bitfield indicating timestamps available in both set
80 bitwise_and_bitfields(result_bitfield, elem_master, elem_slave, NB_BYTES_BITFIELD);
81
82 for (size_t i = 0; i < size; i++) {
83 struct bluesync_msg_client_statistic stat = {
84 .idx = i,
85 .master_timer_ticks = elem_master->timer_ticks[i],
86 .client_timer_ticks = elem_slave->timer_ticks[i],
87 .estimation_master_ticks = elem_slave->remote_est_ticks[i],
88 .valid = is_bit_set(result_bitfield, i),
89 };
90
92 }
93}
94
95
96static FILE *open_stat(char *filename, uint32_t device_number)
97{
98 FILE *fp;
99 static char path[250];
100 memset(path, 0, sizeof(path));
101
102 snprintf(path, sizeof(path) - 1,
103 "%s%s_%i.csv", CONFIG_BLUESYNC_TEST_BABBLESIM_PATH, filename,
104 device_number);
105
106 fp = fopen(path, "w");
107 __ASSERT(fp != NULL, "Cannot open file");
108
109 return fp;
110}
111
112
113
115{
116 uint32_t device_number = bsim_args_get_global_device_nbr();
117
118 node.file = open_stat("node_status", device_number);
119
120 fprintf(node.file, "timeslot_idx" SEPARATION_TOKEN "rcv_time" SEPARATION_TOKEN
121 "local_time" SEPARATION_TOKEN "estimation_master" SEPARATION_TOKEN "valid"
122 "\n");
123
125}
126
127
133
134/* Automatically init the statistic if CONFIG_BLUESYNC_TEST_BABBLESIM_SUPPORT is enabled*/
136/* Automatically close the opened files by calling hbi_api_gateway_deinit() */
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.
bool is_bit_set(uint8_t *bitfield, size_t bit_index)
Check if a specific bit is set in the bitfield.
static struct node_stat node
static FILE * open_stat(char *filename, uint32_t device_number)
LOG_MODULE_REGISTER(bluesync_statistic_bsim, CONFIG_BLUESYNC_LOG_LEVEL)
void bluesync_statistic_init()
Initialize the BlueSync statistics module.
NATIVE_TASK(bluesync_statistic_init, BOOT, 101)
void bluesync_statistic_deinit()
Deinitialize the BlueSync statistics module.
void bluesync_statistic_packet_status(struct bluesync_msg_client_statistic *packet_status)
void statistic_bluesync_status(bluesync_timestamps_t *elem_master, bluesync_timestamps_t *elem_slave, size_t size)
Analyze and log synchronization statistics based on a series of timestamps.
#define SEPARATION_TOKEN
#define NB_BYTES_BITFIELD
Definition bluesync.h:31
uint64_t timer_ticks[SLOT_NUMBER]
Definition bluesync.h:52
struct k_mutex mutex
void synced_time_logger_deinit()
Deinitialize the timer that store regularly timestamp of the node.
void synced_time_logger_init()
Initialize the timer that store regularly timestamp of the node.