Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
openlcb::BroadcastTime Class Referenceabstract

Implementation of Broadcast Time Protocol. More...

#include <BroadcastTime.hxx>

Inheritance diagram for openlcb::BroadcastTime:
openlcb::SimpleEventHandler StateFlowBase TimeBase openlcb::EventHandler Executable Atomic Notifiable QMember Destructable openlcb::BroadcastTimeClient openlcb::BroadcastTimeServer

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.
 
Nodenode ()
 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 &registry_entry, EventReport *event, BarrierNotifiable *done)=0
 Called on incoming EventReport messages.
 
virtual void handle_consumer_identified (const EventRegistryEntry &registry_entry, EventReport *event, BarrierNotifiable *done)
 Called on another node sending ConsumerIdentified for this event.
 
virtual void handle_consumer_range_identified (const EventRegistryEntry &registry_entry, EventReport *event, BarrierNotifiable *done)
 Called on another node sending ConsumerRangeIdentified.
 
virtual void handle_producer_identified (const EventRegistryEntry &registry_entry, EventReport *event, BarrierNotifiable *done)
 Called on another node sending ProducerIdentified for this event.
 
virtual void handle_producer_range_identified (const EventRegistryEntry &registry_entry, EventReport *event, BarrierNotifiable *done)
 Called on another node sending ProducerRangeIdentified for this event.
 
virtual void handle_identify_global (const EventRegistryEntry &registry_entry, EventReport *event, BarrierNotifiable *done)=0
 Called on the need of sending out identification messages.
 
virtual void handle_identify_consumer (const EventRegistryEntry &registry_entry, EventReport *event, BarrierNotifiable *done)=0
 Called on another node sending IdentifyConsumer.
 
virtual void handle_identify_producer (const EventRegistryEntry &registry_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.
 
Serviceservice ()
 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
 
Nodenode_
 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< TimeUpdateCallbackcallbacks_
 update subscribers
 
int16_t rateRequested_
 pending clock rate
 
- Protected Attributes inherited from QMember
QMembernext
 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.
 

Detailed Description

Implementation of Broadcast Time Protocol.

Definition at line 48 of file BroadcastTime.hxx.

Member Typedef Documentation

◆ TimeUpdateCallback

typedef std::function<void(time_t old, time_t current)> openlcb::BroadcastTime::TimeUpdateCallback

Callback type used for time update subscribers.

Parameters
oldFast clock's current time according to the pre-update state.
currentFast clock's current time according to the post-update state.

Definition at line 62 of file BroadcastTime.hxx.

◆ 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.

Constructor & Destructor Documentation

◆ ~BroadcastTime()

virtual openlcb::BroadcastTime::~BroadcastTime ( )
inlinevirtual

Destructor.

Definition at line 65 of file BroadcastTime.hxx.

◆ BroadcastTime()

openlcb::BroadcastTime::BroadcastTime ( Node node,
NodeID  clock_id 
)
inlineprotected

Constructor.

Parameters
nodethe virtual node that will be listening for events and responding to Identify messages.
clock_id48-bit unique identifier for the clock instance

Definition at line 211 of file BroadcastTime.hxx.

Member Function Documentation

◆ clear_timezone()

void openlcb::BroadcastTime::clear_timezone ( )
private

Reset our process local timezone environment to GMT0.

Definition at line 47 of file BroadcastTime.cxx.

◆ clock_id()

NodeID openlcb::BroadcastTime::clock_id ( )
inline

Accessor method to get the (48-bit) Clock ID.

Returns
clock ID

Definition at line 172 of file BroadcastTime.hxx.

◆ event_base()

EventId openlcb::BroadcastTime::event_base ( )
inline

Access method to get the (64-bit) Event ID base.

Definition at line 178 of file BroadcastTime.hxx.

◆ gmtime_get()

const struct tm * openlcb::BroadcastTime::gmtime_get ( )
inline

Get the last calculated reprentation of time.

Returns
last calculated time in the form of a struct tm

Definition at line 193 of file BroadcastTime.hxx.

◆ gmtime_recalculate()

const struct tm * openlcb::BroadcastTime::gmtime_recalculate ( )
inline

Recalculate the struct tm representation of time.

Returns
last calculated time in the form of a struct tm

Definition at line 185 of file BroadcastTime.hxx.

◆ is_server_detected()

virtual bool openlcb::BroadcastTime::is_server_detected ( )
pure virtual

Has a time server been detected?

Returns
true if a time server has been detected, else false

Implemented in openlcb::BroadcastTimeClient, and openlcb::BroadcastTimeServer.

◆ is_server_self()

virtual bool openlcb::BroadcastTime::is_server_self ( )
pure virtual

Test if this is a server.

Returns
true if a BroadcastTimeServer, else false

Implemented in openlcb::BroadcastTimeClient, and openlcb::BroadcastTimeServer.

◆ node()

Node * openlcb::BroadcastTime::node ( )
inline

Accessor method to get the Node reference.

Returns
Node reference

Definition at line 165 of file BroadcastTime.hxx.

◆ query()

void openlcb::BroadcastTime::query ( )
inline

Query the current time.

Definition at line 129 of file BroadcastTime.hxx.

◆ service_callbacks()

void openlcb::BroadcastTime::service_callbacks ( time_t  old,
time_t  current 
)
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.

Parameters
oldFast clock's current time according to the pre-update state.
currentFast clock's current time according to the post-update state.

Definition at line 240 of file BroadcastTime.hxx.

◆ set_date()

void openlcb::BroadcastTime::set_date ( int  month,
int  day 
)
inline

Set the time in seconds since the system Epoch.

The new date does not become valid until the update callbacks are called.

Parameters
monthmonth (1 to 12)
dayday of month (1 to 31)

Definition at line 84 of file BroadcastTime.hxx.

◆ set_date_year_str()

void openlcb::BroadcastTime::set_date_year_str ( const char *  date_year)

Set the date and year from a C string.

Parameters
date_yeardate and year format in "Mmm dd, yyyy" format

Definition at line 58 of file BroadcastTime.cxx.

◆ set_rate_quarters()

void openlcb::BroadcastTime::set_rate_quarters ( int16_t  rate)
inline

Set Rate.

The new rate does not become valid until the update callbacks are called.

Parameters
rateclock rate ratio as 12 bit sign extended fixed point rrrrrrrrrr.rr

Definition at line 109 of file BroadcastTime.hxx.

◆ set_shortcut()

virtual void openlcb::BroadcastTime::set_shortcut ( uint64_t  event)
inlineprotectedvirtual

Try the possible set event shortcut.

This is typically a bypass of the OpenLCB loopback.

Parameters
eventevent that we would be "setting"

Reimplemented in openlcb::BroadcastTimeServer.

Definition at line 231 of file BroadcastTime.hxx.

◆ set_time()

void openlcb::BroadcastTime::set_time ( int  hours,
int  minutes 
)
inline

Set the time in seconds since the system Epoch.

The new time does not become valid until the update callbacks are called.

Parameters
hourhour (0 to 23)
minutesminutes (0 to 59)

Definition at line 73 of file BroadcastTime.hxx.

◆ set_year()

void openlcb::BroadcastTime::set_year ( int  year)
inline

Set the time in seconds since the system Epoch.

The new year does not become valid until the update callbacks are called.

Parameters
year(0AD to 4095AD)

Definition at line 94 of file BroadcastTime.hxx.

◆ start()

void openlcb::BroadcastTime::start ( )
inline

Start clock.

Definition at line 117 of file BroadcastTime.hxx.

◆ stop()

void openlcb::BroadcastTime::stop ( )
inline

Stop clock.

Definition at line 123 of file BroadcastTime.hxx.

◆ update_subscribe_add()

UpdateSubscribeHandle openlcb::BroadcastTime::update_subscribe_add ( TimeUpdateCallback  callback)
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.

Parameters
callbackfunction callback to be called.
Returns
handle to entry that can be used in update_unsubscribe

Definition at line 139 of file BroadcastTime.hxx.

◆ update_subscribe_remove()

void openlcb::BroadcastTime::update_subscribe_remove ( UpdateSubscribeHandle  handle)
inline

Unregister a callback for when the time synchronization is updated.

Parameters
handlereturned from corresponding update_subscribe

Definition at line 157 of file BroadcastTime.hxx.

Member Data Documentation

◆ callbacks_

std::vector<TimeUpdateCallback> openlcb::BroadcastTime::callbacks_
protected

update subscribers

Definition at line 260 of file BroadcastTime.hxx.

◆ eventBase_

EventId openlcb::BroadcastTime::eventBase_
protected

48-bit unique identifier for the clock instance

Definition at line 255 of file BroadcastTime.hxx.

◆ node_

Node* openlcb::BroadcastTime::node_
protected

OpenLCB node to export the consumer on.

Definition at line 254 of file BroadcastTime.hxx.

◆ rateRequested_

int16_t openlcb::BroadcastTime::rateRequested_
protected

pending clock rate

Definition at line 262 of file BroadcastTime.hxx.

◆ timer_

StateFlowTimer openlcb::BroadcastTime::timer_
protected

timer helper

Definition at line 257 of file BroadcastTime.hxx.

◆ tm_

struct tm openlcb::BroadcastTime::tm_
protected

the time we are last set to as a struct tm

Definition at line 252 of file BroadcastTime.hxx.

◆ writer_

WriteHelper openlcb::BroadcastTime::writer_
protected

helper for sending event messages

Definition at line 256 of file BroadcastTime.hxx.


The documentation for this class was generated from the following files: