|
Open Model Railroad Network (OpenMRN)
|
Implementation of Broadcast Time Protocol. More...
#include <BroadcastTime.hxx>
Public Types | |
| typedef size_t | UpdateSubscribeHandle |
| An opaque data element that is returned from update subscriber registration, and allows unregistering a subscriber. | |
| typedef std::function< void(time_t old, time_t current)> | TimeUpdateCallback |
| Callback type used for time update subscribers. | |
Public Types inherited from openlcb::EventHandler | |
| using | EventReport = openlcb::EventReport |
| using | EventRegistryEntry = openlcb::EventRegistryEntry |
| using | EventId = openlcb::EventId |
Public Member Functions | |
| virtual | ~BroadcastTime () |
| Destructor. | |
| void | set_time (int hours, int minutes) |
| Set the time in seconds since the system Epoch. | |
| void | set_date (int month, int day) |
| Set the time in seconds since the system Epoch. | |
| void | set_year (int year) |
| Set the time in seconds since the system Epoch. | |
| void | set_date_year_str (const char *date_year) |
| Set the date and year from a C string. | |
| void | set_rate_quarters (int16_t rate) |
| Set Rate. | |
| void | start () |
| Start clock. | |
| void | stop () |
| Stop clock. | |
| void | query () |
| Query the current time. | |
| UpdateSubscribeHandle | update_subscribe_add (TimeUpdateCallback callback) |
| Register a callback for when the time synchronization is updated. | |
| void | update_subscribe_remove (UpdateSubscribeHandle handle) |
| Unregister a callback for when the time synchronization is updated. | |
| Node * | node () |
| Accessor method to get the Node reference. | |
| NodeID | clock_id () |
| Accessor method to get the (48-bit) Clock ID. | |
| EventId | event_base () |
| Access method to get the (64-bit) Event ID base. | |
| const struct tm * | gmtime_recalculate () |
| Recalculate the struct tm representation of time. | |
| const struct tm * | gmtime_get () |
| Get the last calculated reprentation of time. | |
| virtual bool | is_server_detected ()=0 |
| Has a time server been detected? | |
| virtual bool | is_server_self ()=0 |
| Test if this is a server. | |
Public Member Functions inherited from openlcb::SimpleEventHandler | |
| IGNOREFN (handle_event_report) | |
| IGNOREFN (handle_consumer_identified) | |
| IGNOREFN (handle_consumer_range_identified) | |
| IGNOREFN (handle_producer_identified) | |
| IGNOREFN (handle_producer_range_identified) | |
| IGNOREFN (handle_identify_consumer) | |
| IGNOREFN (handle_identify_producer) | |
Public Member Functions inherited from openlcb::EventHandler | |
| virtual void | handle_event_report (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0 |
| Called on incoming EventReport messages. | |
| virtual void | handle_consumer_identified (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done) |
| Called on another node sending ConsumerIdentified for this event. | |
| virtual void | handle_consumer_range_identified (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done) |
| Called on another node sending ConsumerRangeIdentified. | |
| virtual void | handle_producer_identified (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done) |
| Called on another node sending ProducerIdentified for this event. | |
| virtual void | handle_producer_range_identified (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done) |
| Called on another node sending ProducerRangeIdentified for this event. | |
| virtual void | handle_identify_global (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0 |
| Called on the need of sending out identification messages. | |
| virtual void | handle_identify_consumer (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0 |
| Called on another node sending IdentifyConsumer. | |
| virtual void | handle_identify_producer (const EventRegistryEntry ®istry_entry, EventReport *event, BarrierNotifiable *done)=0 |
| Called on another node sending IdentifyProducer. | |
Public Member Functions inherited from StateFlowBase | |
| void | run () override |
| Callback from the executor. | |
| void | notify () override |
| Wakeup call arrived. | |
| Service * | service () |
| Return a pointer to the service I am bound to. | |
Public Member Functions inherited from Executable | |
| void | test_deletion () |
Public Member Functions inherited from QMember | |
| void | init () |
| Initiailize a QMember, in place of a public placement construction. | |
Public Member Functions inherited from TimeBase | |
| void | sync (const TimeBase &other) |
| Synchronizes this time with a different clock. | |
| std::pair< time_t, int16_t > | time_and_rate_quarters () |
| Get the time as a value of seconds relative to the system epoch. | |
| time_t | compare_realtime (time_t t1, time_t t2) |
| Get the difference in time scaled to real time. | |
| time_t | time () |
| Get the time as a value of seconds relative to the system epoch. | |
| struct tm * | gmtime_r (struct tm *result) |
| Get the time as a standard struct tm. | |
| int | date (int *month, int *day) |
| Get the date (month/day). | |
| int | day_of_week () |
| Get the day of the week. | |
| int | day_of_year () |
| Get the day of the year. | |
| int | year () |
| Get the year. | |
| int16_t | get_rate_quarters () |
| Report the clock rate as a 12-bit fixed point number (-512.00 to 511.75). | |
| bool | is_running () |
| Test of the clock is running. | |
| bool | is_started () |
| Test of the clock is started (rate could still be 0). | |
| bool | fast_sec_to_real_nsec_period (int16_t rate, time_t fast_sec, long long *real_nsec) |
| Convert fast clock period to a period in real nsec. | |
| bool | real_nsec_to_fast_sec_period (int16_t rate, long long real_nsec, time_t *fast_sec) |
| Convert period in real nsec to a fast clock period. | |
| bool | real_nsec_until_fast_time_abs (time_t fast_sec, long long *real_nsec) |
| Convert a fast time to absolute nsec until it will occur. | |
| bool | fast_sec_to_real_nsec_period_abs (time_t fast_sec, long long *real_nsec) |
| Convert fast clock absolute (negative or positive) period to a positive (absolute) period in real nsec. | |
Protected Member Functions | |
| BroadcastTime (Node *node, NodeID clock_id) | |
| Constructor. | |
| virtual void | set_shortcut (uint64_t event) |
| Try the possible set event shortcut. | |
| void | service_callbacks (time_t old, time_t current) |
| Service all of the attached update subscribers. | |
Protected Member Functions inherited from StateFlowBase | |
| StateFlowBase (Service *service) | |
| Constructor. | |
| ~StateFlowBase () | |
| Destructor. | |
| void | reset_flow (Callback c) |
| Resets the flow to the specified state. | |
| bool | is_state (Callback c) |
| bool | is_terminated () |
| void | start_flow (Callback c) |
| Resets the flow to the specified state and starts it. | |
| Action | again () |
| Call the current state again via call_immediately. | |
| Action | exit () |
| Terminate current StateFlow activity. | |
| Action | delete_this () |
| Terminates the flow and deletes *this. | |
| Action | set_terminated () |
| Sets the flow to terminated state. | |
| Action | call_immediately (Callback c) |
| Imediately call the next state upon return. | |
| Action | wait () |
| Wait for an asynchronous call. | |
| Action | wait_and_call (Callback c) |
| Wait for resource to become available before proceeding to next state. | |
| template<class T > | |
| Action | allocate_and_call (FlowInterface< Buffer< T > > *target_flow, Callback c, Pool *pool=nullptr) |
| Allocates a buffer from a pool and proceed to the next state when allocation is successful. | |
| Action | allocate_and_call (Callback c, QAsync *queue) |
| Allocates an entry from an asynchronous queue, and transitions to a state once the allocation is complete. | |
| template<class T > | |
| Buffer< T > * | full_allocation_result (FlowInterface< Buffer< T > > *target_flow) |
| Takes the result of the asynchronous allocation without resetting the object. | |
| template<class T > | |
| T * | full_allocation_result (TypedQAsync< T > *queue) |
| Takes the result of the asynchronous allocation without resetting the object. | |
| template<class T > | |
| void | cast_allocation_result (T **member) |
| Takes the result of the asynchronous allocation without resetting the object. | |
| template<class T > | |
| Buffer< T > * | get_allocation_result (FlowInterface< Buffer< T > > *target_flow) |
| Takes the result of the asynchronous allocation. | |
| Action | yield_and_call (Callback c) |
| Place the current flow to the back of the executor, and transition to a new state after we get the CPU again. | |
| Action | yield () |
| Place the current flow to the back of the executor, and re-try the current state after we get the CPU again. | |
| Action | sleep_and_call (::Timer *timer, long long timeout_nsec, Callback c) |
| Suspends execution of this control flow for a specified time. | |
| template<class T , typename... Args> | |
| Action | invoke_subflow_and_wait (FlowInterface< Buffer< T > > *target_flow, Callback c, Args &&... args) |
| Calls a helper flow to perform some actions. | |
| Action | read_repeated (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Blocks until size bytes are read and then invokes the next state. | |
| Action | read_single (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Attempts to read at most size_t bytes, and blocks the caller until at least one byte is read. | |
| Action | read_nonblocking (StateFlowSelectHelper *helper, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Attempts to read at most size bytes, and then invokes the next state, even if only zero bytes are available right now. | |
| Action | read_repeated_with_timeout (StateFlowTimedSelectHelper *helper, long long timeout_nsec, int fd, void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Blocks until size bytes are read, or a timeout expires. | |
| Action | internal_try_read () |
| Implementation state that gets repeatedly called upon every wakeup and tries to make progress on reading. | |
| Action | write_repeated (StateFlowSelectHelper *helper, int fd, const void *buf, size_t size, Callback c, unsigned priority=Selectable::MAX_PRIO) |
| Writes some data into a file descriptor, repeating the operation as necessary until all bytes are written. | |
| Action | internal_try_write () |
| Implementation state that gets repeatedly called upon every wakeup and tries to make progress on writing. | |
Protected Member Functions inherited from QMember | |
| QMember () | |
| Constructor. | |
| ~QMember () | |
| Destructor. | |
Protected Member Functions inherited from Atomic | |
| void | lock () |
| void | unlock () |
Protected Attributes | |
| struct tm | tm_ |
| the time we are last set to as a struct tm | |
| Node * | node_ |
| OpenLCB node to export the consumer on. | |
| EventId | eventBase_ |
| 48-bit unique identifier for the clock instance | |
| WriteHelper | writer_ |
| helper for sending event messages | |
| StateFlowTimer | timer_ |
| timer helper | |
| std::vector< TimeUpdateCallback > | callbacks_ |
| update subscribers | |
| int16_t | rateRequested_ |
| pending clock rate | |
Protected Attributes inherited from QMember | |
| QMember * | next |
| pointer to the next member in the queue | |
Protected Attributes inherited from TimeBase | |
| long long | timestamp_ {OSTime::get_monotonic()} |
| OS time at the last time update. | |
| time_t | seconds_ {0} |
| Clock time at the last time update. | |
| int16_t | rate_ {0} |
| effective clock rate | |
| uint16_t | started_: 1 |
| true if clock is started | |
Private Member Functions | |
| void | clear_timezone () |
| Reset our process local timezone environment to GMT0. | |
| DISALLOW_COPY_AND_ASSIGN (BroadcastTime) | |
Additional Inherited Members | |
Static Public Member Functions inherited from StateFlowBase | |
| template<class T , typename... Args> | |
| static void | invoke_subflow_and_ignore_result (FlowInterface< Buffer< T > > *target_flow, Args &&... args) |
| Calls a helper flow to perform some actions. | |
Protected Types inherited from StateFlowBase | |
| typedef Action(StateFlowBase::* | Callback) () |
| State Flow callback prototype. | |
Implementation of Broadcast Time Protocol.
Definition at line 48 of file BroadcastTime.hxx.
| typedef std::function<void(time_t old, time_t current)> openlcb::BroadcastTime::TimeUpdateCallback |
Callback type used for time update subscribers.
| old | Fast clock's current time according to the pre-update state. |
| current | Fast clock's current time according to the post-update state. |
Definition at line 62 of file BroadcastTime.hxx.
| typedef size_t openlcb::BroadcastTime::UpdateSubscribeHandle |
An opaque data element that is returned from update subscriber registration, and allows unregistering a subscriber.
Definition at line 55 of file BroadcastTime.hxx.
|
inlinevirtual |
Destructor.
Definition at line 65 of file BroadcastTime.hxx.
Constructor.
| node | the virtual node that will be listening for events and responding to Identify messages. |
| clock_id | 48-bit unique identifier for the clock instance |
Definition at line 211 of file BroadcastTime.hxx.
|
private |
Reset our process local timezone environment to GMT0.
Definition at line 47 of file BroadcastTime.cxx.
|
inline |
Accessor method to get the (48-bit) Clock ID.
Definition at line 172 of file BroadcastTime.hxx.
|
inline |
Access method to get the (64-bit) Event ID base.
Definition at line 178 of file BroadcastTime.hxx.
|
inline |
Get the last calculated reprentation of time.
Definition at line 193 of file BroadcastTime.hxx.
|
inline |
Recalculate the struct tm representation of time.
Definition at line 185 of file BroadcastTime.hxx.
|
pure virtual |
Has a time server been detected?
Implemented in openlcb::BroadcastTimeClient, and openlcb::BroadcastTimeServer.
|
pure virtual |
Test if this is a server.
Implemented in openlcb::BroadcastTimeClient, and openlcb::BroadcastTimeServer.
|
inline |
Accessor method to get the Node reference.
Definition at line 165 of file BroadcastTime.hxx.
|
inline |
Query the current time.
Definition at line 129 of file BroadcastTime.hxx.
|
inlineprotected |
Service all of the attached update subscribers.
These are called when there are jumps in time or if the clock is stopped or started.
| old | Fast clock's current time according to the pre-update state. |
| current | Fast clock's current time according to the post-update state. |
Definition at line 240 of file BroadcastTime.hxx.
|
inline |
Set the time in seconds since the system Epoch.
The new date does not become valid until the update callbacks are called.
| month | month (1 to 12) |
| day | day of month (1 to 31) |
Definition at line 84 of file BroadcastTime.hxx.
| void openlcb::BroadcastTime::set_date_year_str | ( | const char * | date_year | ) |
Set the date and year from a C string.
| date_year | date and year format in "Mmm dd, yyyy" format |
Definition at line 58 of file BroadcastTime.cxx.
|
inline |
Set Rate.
The new rate does not become valid until the update callbacks are called.
| rate | clock rate ratio as 12 bit sign extended fixed point rrrrrrrrrr.rr |
Definition at line 109 of file BroadcastTime.hxx.
|
inlineprotectedvirtual |
Try the possible set event shortcut.
This is typically a bypass of the OpenLCB loopback.
| event | event that we would be "setting" |
Reimplemented in openlcb::BroadcastTimeServer.
Definition at line 231 of file BroadcastTime.hxx.
|
inline |
Set the time in seconds since the system Epoch.
The new time does not become valid until the update callbacks are called.
| hour | hour (0 to 23) |
| minutes | minutes (0 to 59) |
Definition at line 73 of file BroadcastTime.hxx.
|
inline |
Set the time in seconds since the system Epoch.
The new year does not become valid until the update callbacks are called.
| year | (0AD to 4095AD) |
Definition at line 94 of file BroadcastTime.hxx.
|
inline |
Start clock.
Definition at line 117 of file BroadcastTime.hxx.
|
inline |
Stop clock.
Definition at line 123 of file BroadcastTime.hxx.
|
inline |
Register a callback for when the time synchronization is updated.
The context of the caller will be from a state flow on the Node Interface executor.
| callback | function callback to be called. |
Definition at line 139 of file BroadcastTime.hxx.
|
inline |
Unregister a callback for when the time synchronization is updated.
| handle | returned from corresponding update_subscribe |
Definition at line 157 of file BroadcastTime.hxx.
|
protected |
update subscribers
Definition at line 260 of file BroadcastTime.hxx.
|
protected |
48-bit unique identifier for the clock instance
Definition at line 255 of file BroadcastTime.hxx.
|
protected |
OpenLCB node to export the consumer on.
Definition at line 254 of file BroadcastTime.hxx.
|
protected |
pending clock rate
Definition at line 262 of file BroadcastTime.hxx.
|
protected |
timer helper
Definition at line 257 of file BroadcastTime.hxx.
|
protected |
the time we are last set to as a struct tm
Definition at line 252 of file BroadcastTime.hxx.
|
protected |
helper for sending event messages
Definition at line 256 of file BroadcastTime.hxx.