reactor-c
C Runtime for Lingua Franca
Loading...
Searching...
No Matches
clock-sync.h
Go to the documentation of this file.
1
33#ifndef CLOCK_SYNC_H
34#define CLOCK_SYNC_H
35
36#include "low_level_platform.h"
37
38// Clock synchronization defaults to performing clock synchronization only at initialization.
39#define LF_CLOCK_SYNC_OFF 1
40#define LF_CLOCK_SYNC_INIT 2
41#define LF_CLOCK_SYNC_ON 3
42
43#ifndef LF_CLOCK_SYNC
44#define LF_CLOCK_SYNC LF_CLOCK_SYNC_INIT
45#endif
46
52#ifndef _LF_CLOCK_SYNC_EXCHANGES_PER_INTERVAL
53#define _LF_CLOCK_SYNC_EXCHANGES_PER_INTERVAL 10
54#endif
55
57#ifndef _LF_CLOCK_SYNC_ATTENUATION
58#define _LF_CLOCK_SYNC_ATTENUATION 10
59#endif
60
62#ifndef _LF_CLOCK_SYNC_COLLECT_STATS
63#define _LF_CLOCK_SYNC_COLLECT_STATS true
64#endif
65
72#define CLOCK_SYNC_GUARD_BAND USEC(100)
73
83typedef struct socket_stat_t {
84 instant_t remote_physical_clock_snapshot_T1; // T1 in PTP. The first snapshot of the physical
85 // clock of the remote device (the RTI).
86 instant_t local_physical_clock_snapshot_T2; // T2 in PTP. The first snapshot of the physical
87 // clock of the local device (the federate).
88 interval_t local_delay; // T3 - T2. Estimated delay between a consecutive
89 // receive and send on the socket for one byte.
90 int received_T4_messages_in_current_sync_window; // Checked against _LF_CLOCK_SYNC_EXCHANGES_PER_INTERVAL
91 // Must be reset to 0 every time it reaches the threshold.
92 interval_t history; // A history of clock synchronization data. For AVG
93 // strategy, this is a running partially computed average.
94
95
98
99 interval_t network_stat_round_trip_delay_max; // Maximum estimated delay between the local socket and the
100 // remote socket.
101 int network_stat_sample_index; // Current index of network_stat_samples
102
103 interval_t clock_synchronization_error_bound; // A bound on the differences between this federate's clock and
104 // the remote clock.
105 // Note: The following array should come last because g++ will not allow
106 // designated initialization (e.g., .network_stat_sample_index = 0) out of
107 // order and we do not want to (and cannot) initialize this array statically
109 // period
111
112#ifdef _LF_CLOCK_SYNC_COLLECT_STATS
116struct lf_stat_ll;
117
127void update_socket_stat(struct socket_stat_t* socket_stat, long long network_delay,
128 long long clock_synchronization_error);
129
138#endif // _LF_CLOCK_SYNC_COLLECT_STATS
139
144void reset_socket_stat(struct socket_stat_t* socket_stat);
145
152
169
181int handle_T1_clock_sync_message(unsigned char* buffer, int socket, instant_t t2);
182
199void handle_T4_clock_sync_message(unsigned char* buffer, int socket, instant_t r4);
200
204void* listen_to_rti_UDP_thread(void* args);
205
214
220
226
234
235#endif // CLOCK_SYNC_H
struct lf_stat_ll calculate_socket_stat(struct socket_stat_t *socket_stat)
void handle_T4_clock_sync_message(unsigned char *buffer, int socket, instant_t r4)
void synchronize_initial_physical_clock_with_rti(int *rti_socket_TCP)
void clock_sync_set_constant_bias(interval_t offset)
void update_socket_stat(struct socket_stat_t *socket_stat, long long network_delay, long long clock_synchronization_error)
void clock_sync_add_offset(instant_t *t)
Add the current clock synchronization offset to a specified timestamp.
Definition clock.c:16
void * listen_to_rti_UDP_thread(void *args)
struct socket_stat_t socket_stat_t
uint16_t setup_clock_synchronization_with_rti(void)
#define _LF_CLOCK_SYNC_EXCHANGES_PER_INTERVAL
Definition clock-sync.h:53
int create_clock_sync_thread(lf_thread_t *thread_id)
int handle_T1_clock_sync_message(unsigned char *buffer, int socket, instant_t t2)
void reset_socket_stat(struct socket_stat_t *socket_stat)
void clock_sync_subtract_offset(instant_t *t)
Subtract the clock synchronization offset from a timestamp.
Definition clock.c:17
void * lf_thread_t
Definition lf_arduino_support.h:120
Platform API support for the C target of Lingua Franca.
Definition util.h:45
Definition clock-sync.h:83
interval_t network_stat_round_trip_delay_max
Definition clock-sync.h:99
interval_t network_stat_samples[_LF_CLOCK_SYNC_EXCHANGES_PER_INTERVAL]
Definition clock-sync.h:108
instant_t remote_physical_clock_snapshot_T1
Definition clock-sync.h:84
int network_stat_sample_index
Definition clock-sync.h:101
interval_t local_delay
Definition clock-sync.h:88
interval_t clock_synchronization_error_bound
Definition clock-sync.h:103
interval_t history
Definition clock-sync.h:92
int received_T4_messages_in_current_sync_window
Definition clock-sync.h:90
instant_t local_physical_clock_snapshot_T2
Definition clock-sync.h:86
int64_t instant_t
Definition tag.h:66
int64_t interval_t
Definition tag.h:71
char buffer[BUFFER_SIZE]
Definition trace_util.c:42