BlueSync - BLE Time Sync for Zephyr
High-precision time synchronization for BLE Mesh networks
Loading...
Searching...
No Matches
synced_time_logger.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: synced_time_logger.h
17 * Description: file that logs with bable sim a timestamp value every second
18 *
19 * Project: BlueSync - BLE Time Sync for Zephyr
20 * Repository: https://github.com/Tobi15/zephyr-bluesync-ble
21 */
22#include "synced_time_logger.h"
23
24#include <zephyr/kernel.h>
25
26#include "posix_native_task.h"
27#include "bsim_args_runner.h"
28
29#include <stdio.h>
30#include <string.h>
31
32#include <zephyr/logging/log.h>
33
34#include <bluesync/bluesync.h>
35
36
37LOG_MODULE_REGISTER(synced_time_logger, CONFIG_APP_LOG_LEVEL);
38
39#define SEPARATION_TOKEN ";"
40
41#define TICKS_PER_SECOND 32768
42struct k_work my_work;
43static int64_t val_ref = 0;
44
46 int64_t val;
47 uint64_t timestamp;
48};
49
51
52void my_work_handler(struct k_work *work)
53{
54 struct msg_statistic msg = {
56 .val = val_ref++,
57 };
58
60}
61
62void timer_handler(struct k_timer *timer_id)
63{
64 k_work_submit(&my_work);
65}
66
67K_TIMER_DEFINE(aligned_timer, timer_handler, NULL);
68
70{
71 k_work_init(&my_work, my_work_handler);
72
73 uint64_t now_ticks = k_uptime_ticks();
74
75 const uint64_t TICKS_PER_20SEC = TICKS_PER_SECOND * 20;
76
77 // Calculate ticks to wait until the next aligned 1-second boundary
78 uint64_t remainder = now_ticks % TICKS_PER_20SEC;
79 uint64_t ticks_until_next = (remainder == 0) ? 0 : (TICKS_PER_20SEC - remainder);
80
81 LOG_DBG("Now ticks: %llu", now_ticks);
82 LOG_DBG("First timer in %llu ticks (aligned to 1s)", ticks_until_next);
83
84 // Start periodic timer: delay to next aligned second, then every second
85 k_timer_start(&aligned_timer, K_TICKS(ticks_until_next), K_TICKS(TICKS_PER_SECOND));
86}
87
89 struct k_mutex mutex;
90 FILE *file;
91};
92
93static struct ble_node_stat node;
94
95static FILE *open_stat(char *filename, uint32_t device_number)
96{
97 FILE *fp;
98 static char path[250];
99 memset(path, 0, sizeof(path));
100
101 snprintf(path, sizeof(path) - 1,
102 "%s%s_%i.csv",CONFIG_BLUESYNC_TEST_BABBLESIM_PATH,
103 filename, device_number);
104 LOG_DBG("path : %s\n", path);
105 fp = fopen(path, "w");
106 __ASSERT(fp != NULL, "Cannot open file");
107
108 return fp;
109}
110
112{
113 uint32_t device_number = bsim_args_get_global_device_nbr();
114
115 node.file = open_stat("node", device_number);
116
117 fprintf(node.file, "val" SEPARATION_TOKEN "timestamp"
118 "\n");
119
121}
122
124{
125 k_mutex_lock(&node.mutex, K_FOREVER);
126 {
127 fprintf(node.file,
128 "%lld" SEPARATION_TOKEN "%llu"
129 "\n",
130 msg->val, msg->timestamp);
131 }
132 k_mutex_unlock(&node.mutex);
133}
134
136{
137 fclose(node.file);
138}
uint64_t get_current_unix_time_us(void)
Gets the current synchronized UNIX time.
Definition local_time.c:138
Public API for the BlueSync time synchronization module.
struct k_mutex mutex
void my_work_handler(struct k_work *work)
void synced_time_logger_new_msg(struct msg_statistic *msg)
struct k_work my_work
static FILE * open_stat(char *filename, uint32_t device_number)
#define TICKS_PER_SECOND
static struct ble_node_stat node
void timer_handler(struct k_timer *timer_id)
static int64_t val_ref
LOG_MODULE_REGISTER(synced_time_logger, CONFIG_APP_LOG_LEVEL)
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.
void init_sync_overall_timer(void)
K_TIMER_DEFINE(aligned_timer, timer_handler, NULL)
#define SEPARATION_TOKEN