reactor-c
C Runtime for Lingua Franca
|
#include "low_level_platform.h"
Go to the source code of this file.
Data Structures | |
struct | socket_stat_t |
Macros | |
#define | LF_CLOCK_SYNC_OFF 1 |
#define | LF_CLOCK_SYNC_INIT 2 |
#define | LF_CLOCK_SYNC_ON 3 |
#define | LF_CLOCK_SYNC LF_CLOCK_SYNC_INIT |
#define | _LF_CLOCK_SYNC_EXCHANGES_PER_INTERVAL 10 |
#define | _LF_CLOCK_SYNC_ATTENUATION 10 |
#define | _LF_CLOCK_SYNC_COLLECT_STATS true |
#define | CLOCK_SYNC_GUARD_BAND USEC(100) |
Typedefs | |
typedef struct socket_stat_t | socket_stat_t |
Functions | |
void | update_socket_stat (struct socket_stat_t *socket_stat, long long network_delay, long long clock_synchronization_error) |
struct lf_stat_ll | calculate_socket_stat (struct socket_stat_t *socket_stat) |
void | reset_socket_stat (struct socket_stat_t *socket_stat) |
uint16_t | setup_clock_synchronization_with_rti (void) |
void | synchronize_initial_physical_clock_with_rti (int *rti_socket_TCP) |
int | handle_T1_clock_sync_message (unsigned char *buffer, int socket, instant_t t2) |
void | handle_T4_clock_sync_message (unsigned char *buffer, int socket, instant_t r4) |
void * | listen_to_rti_UDP_thread (void *args) |
int | create_clock_sync_thread (lf_thread_t *thread_id) |
void | clock_sync_add_offset (instant_t *t) |
Add the current clock synchronization offset to a specified timestamp. | |
void | clock_sync_subtract_offset (instant_t *t) |
Subtract the clock synchronization offset from a timestamp. | |
void | clock_sync_set_constant_bias (interval_t offset) |
Copyright (c) 2020-21, The University of California at Berkeley.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Utility functions for clock synchronization.
#define _LF_CLOCK_SYNC_ATTENUATION 10 |
Runtime clock offset updates will be divided by this number.
#define _LF_CLOCK_SYNC_COLLECT_STATS true |
By default, collect statistics on clock synchronization.
#define _LF_CLOCK_SYNC_EXCHANGES_PER_INTERVAL 10 |
Number of required clock sync T4 messages per synchronization interval. The offset to the clock will not be adjusted until this number of T4 clock synchronization messages have been received.
#define CLOCK_SYNC_GUARD_BAND USEC(100) |
Define a guard band to filter clock synchronization messages based on discrepancies in the network delay.
#define LF_CLOCK_SYNC LF_CLOCK_SYNC_INIT |
#define LF_CLOCK_SYNC_INIT 2 |
#define LF_CLOCK_SYNC_OFF 1 |
#define LF_CLOCK_SYNC_ON 3 |
typedef struct socket_stat_t socket_stat_t |
Statistics for a given socket. The RTI initiates a clock synchronization action by sending its current physical time T1 to a federate. The federate records the local time T2 that it receives T1. It sends a reply at local time T3, which the RTI receives at its time T4. The RTI sends back T4. The round trip delay on the socket is therefore estimated as: (T4 - T1) - (T3 - T2).
struct lf_stat_ll calculate_socket_stat | ( | struct socket_stat_t * | socket_stat | ) |
Calculate statistics of the socket. The releavent information is returned as a lf_stat_ll struct.
socket_stat | The socket_stat_t struct that keeps track of stats for a given connection |
void clock_sync_add_offset | ( | instant_t * | t | ) |
Add the current clock synchronization offset to a specified timestamp.
t | Pointer to the timestamp to which to add the offset. |
void clock_sync_set_constant_bias | ( | interval_t | offset | ) |
Set a fixed offset to the physical clock. After calling this, the value returned by lf_time_physical(void) and get_elpased_physical_time(void) will have this specified offset added to what it would have returned before the call.
void clock_sync_subtract_offset | ( | instant_t * | t | ) |
Subtract the clock synchronization offset from a timestamp.
t | The timestamp from which to subtract the current clock sync offset. |
int create_clock_sync_thread | ( | lf_thread_t * | thread_id | ) |
Create the thread responsible for handling clock synchronization with the RTI if (runtime) clock synchronization is on. Otherwise, do nothing an return 0.
int handle_T1_clock_sync_message | ( | unsigned char * | buffer, |
int | socket, | ||
instant_t | t2 ) |
Handle a clock synchroninzation message T1 coming from the RTI. T1 is the first message in a PTP exchange. This replies to the RTI with a T3 message. It also measures the time it takes between when the method is called and the reply has been sent.
buffer | The buffer containing the message, including the message type. |
socket | The socket (either _lf_rti_socket_TCP or _lf_rti_socket_UDP). |
t2 | The physical time at which the T1 message was received. |
void handle_T4_clock_sync_message | ( | unsigned char * | buffer, |
int | socket, | ||
instant_t | r4 ) |
Handle a clock synchronization message T4 coming from the RTI. If the socket is _lf_rti_socket_TCP, then assume we are in the initial clock synchronization phase and set the clock offset based on the estimated clock synchronization error. Otherwise, if the socket is _lf_rti_socket_UDP, then this looks also for a subsequent "coded probe" message on the socket. If the delay between the T4 and the coded probe message is not as expected, then reject this clock synchronization round. If it is not rejected, then make an adjustment to the clock offset based on the estimated error. This function does not acquire the socket_mutex lock. The caller should acquire it unless it is sure there is only one thread running.
buffer | The buffer containing the message, including the message type. |
socket | The socket (either _lf_rti_socket_TCP or _lf_rti_socket_UDP). |
r4 | The physical time at which this T4 message was received. |
void * listen_to_rti_UDP_thread | ( | void * | args | ) |
Thread that listens for UDP inputs from the RTI.
void reset_socket_stat | ( | struct socket_stat_t * | socket_stat | ) |
Reset statistics on the socket.
socket_stat | The socket_stat_t struct that keeps track of stats for a given connection |
uint16_t setup_clock_synchronization_with_rti | ( | void | ) |
Setup necessary functionalities to synchronize clock with the RTI.
void synchronize_initial_physical_clock_with_rti | ( | int * | rti_socket_TCP | ) |
Synchronize the initial physical clock with the RTI. A call to this function is inserted into the startup sequence by the code generator if initial clock synchronization is required.
This is a blocking function that expects to read a MSG_TYPE_CLOCK_SYNC_T1 from the RTI TCP socket. It will then follow the PTP protocol to synchronize the local physical clock with the RTI. Failing to complete this protocol is treated as a catastrophic error that causes the federate to exit.
rti_socket_TCP | Pointer to the RTI's socket |
void update_socket_stat | ( | struct socket_stat_t * | socket_stat, |
long long | network_delay, | ||
long long | clock_synchronization_error ) |
Update statistics on the socket based on the newly calculated network delay and clock synchronization error
socket_stat | The socket_stat_t struct that keeps track of stats for a given connection |
network_round_trip_delay | The newly calculated round trip delay to the remote federate/RTI |
clock_synchronization_error | The newly calculated clock synchronization error relative to the remote federate/RTI |