Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
BroadcastTime.hxx
Go to the documentation of this file.
1
35#ifndef _OPENLCB_BROADCASTTIME_HXX_
36#define _OPENLCB_BROADCASTTIME_HXX_
37
38#include <functional>
39
42#include "utils/TimeBase.hxx"
43
44namespace openlcb
45{
46
49 , public StateFlowBase
50 , public TimeBase
51{
52public:
55 typedef size_t UpdateSubscribeHandle;
56
62 typedef std::function<void(time_t old, time_t current)> TimeUpdateCallback;
63
66 {
67 }
68
73 void set_time(int hours, int minutes)
74 {
77 send_event(node_, event_id);
78 }
79
84 void set_date(int month, int day)
85 {
88 send_event(node_, event_id);
89 }
90
100
103 void set_date_year_str(const char *date_year);
104
109 void set_rate_quarters(int16_t rate)
110 {
113 send_event(node_, event_id);
114 }
115
121
127
133
140 {
141 AtomicHolder h(this);
142 for (size_t i = 0; i < callbacks_.size(); ++i)
143 {
144 // atempt to garbage collect unused entries
145 if (callbacks_[i] == nullptr)
146 {
147 callbacks_[i] = std::move(callback);
148 return i;
149 }
150 }
151 callbacks_.emplace_back(std::move(callback));
152 return callbacks_.size() - 1;
153 }
154
158 {
159 AtomicHolder h(this);
160 callbacks_[handle] = nullptr;
161 }
162
166 {
167 return node_;
168 }
169
173 {
174 return eventBase_ >> 16;
175 }
176
178 EventId event_base()
179 {
180 return eventBase_;
181 }
182
185 const struct tm *gmtime_recalculate()
186 {
187 gmtime_r(&tm_);
188 return &tm_;
189 }
190
193 const struct tm *gmtime_get()
194 {
195 return &tm_;
196 }
197
200 virtual bool is_server_detected() = 0;
201
204 virtual bool is_server_self() = 0;
205
206protected:
212 : StateFlowBase(node->iface())
213 , node_(node)
214 , eventBase_((uint64_t)clock_id << 16)
215 , writer_()
216 , timer_(this)
217 , callbacks_()
218 , rateRequested_(0)
219 {
220 // use a process-local timezone
222
223 time_t time = 0;
224 ::gmtime_r(&time, &tm_);
225 tm_.tm_isdst = 0;
226 }
227
231 virtual void set_shortcut(uint64_t event)
232 {
233 }
234
240 void service_callbacks(time_t old, time_t current)
241 {
242 AtomicHolder h(this);
243 for (auto n : callbacks_)
244 {
245 if (n)
246 {
247 n(old, current);
248 }
249 }
250 }
251
252 struct tm tm_;
253
255 EventId eventBase_;
258
260 std::vector<TimeUpdateCallback> callbacks_;
261
263
264private:
266 void clear_timezone();
267
269};
270
271} // namespace openlcb
272
273#endif // _OPENLCB_BROADCASTTIME_HXX_
See OSMutexLock in os/OS.hxx.
Definition Atomic.hxx:153
Use this timer class to deliver the timeout notification to a stateflow.
Base class for state machines.
Helper class for implementing fast clocks.
Definition TimeBase.hxx:44
struct tm * gmtime_r(struct tm *result)
Get the time as a standard struct tm.
Definition TimeBase.hxx:108
int year()
Get the year.
Definition TimeBase.hxx:157
time_t time()
Get the time as a value of seconds relative to the system epoch.
Definition TimeBase.hxx:87
Implementation of Broadcast Time Protocol.
StateFlowTimer timer_
timer helper
virtual bool is_server_detected()=0
Has a time server been detected?
const struct tm * gmtime_get()
Get the last calculated reprentation of time.
void clear_timezone()
Reset our process local timezone environment to GMT0.
int16_t rateRequested_
pending clock rate
void set_year(int year)
Set the time in seconds since the system Epoch.
std::function< void(time_t old, time_t current)> TimeUpdateCallback
Callback type used for time update subscribers.
void update_subscribe_remove(UpdateSubscribeHandle handle)
Unregister a callback for when the time synchronization is updated.
void set_date_year_str(const char *date_year)
Set the date and year from a C string.
virtual bool is_server_self()=0
Test if this is a server.
virtual ~BroadcastTime()
Destructor.
std::vector< TimeUpdateCallback > callbacks_
update subscribers
void service_callbacks(time_t old, time_t current)
Service all of the attached update subscribers.
void start()
Start clock.
Node * node()
Accessor method to get the Node reference.
BroadcastTime(Node *node, NodeID clock_id)
Constructor.
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.
size_t UpdateSubscribeHandle
An opaque data element that is returned from update subscriber registration, and allows unregistering...
struct tm tm_
the time we are last set to as a struct tm
void query()
Query the current time.
void set_date(int month, int day)
Set the time in seconds since the system Epoch.
void set_time(int hours, int minutes)
Set the time in seconds since the system Epoch.
virtual void set_shortcut(uint64_t event)
Try the possible set event shortcut.
UpdateSubscribeHandle update_subscribe_add(TimeUpdateCallback callback)
Register a callback for when the time synchronization is updated.
WriteHelper writer_
helper for sending event messages
const struct tm * gmtime_recalculate()
Recalculate the struct tm representation of time.
void stop()
Stop clock.
void set_rate_quarters(int16_t rate)
Set Rate.
EventId eventBase_
48-bit unique identifier for the clock instance
Node * node_
OpenLCB node to export the consumer on.
Base class for NMRAnet nodes conforming to the asynchronous interface.
Definition Node.hxx:52
SimpleEventHandler ignores all non-essential callbacks.
A statically allocated buffer for sending one message to the OpenLCB bus.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
uint64_t NodeID
48-bit NMRAnet Node ID type
void send_event(Node *src_node, uint64_t event_id)
Helper function to send an event report to the bus.
Definition If.cxx:143
static uint64_t time_to_event(uint64_t event_base, int hours, int minutes)
Build an event from hours and minutes.
static uint64_t year_to_event(uint64_t event_base, int year)
Build an event from year.
static uint64_t rate_to_event(uint64_t event_base, int16_t rate)
Build an event from rate.
static uint64_t date_to_event(uint64_t event_base, int month, int day)
Build an event from month and day.
@ EVENT_SET_SUFFIX_MASK
suffix max for setting a property
@ START_EVENT_SUFFIX
start clock event suffix value
@ STOP_EVENT_SUFFIX
stop clock event suffix value
@ QUERY_EVENT_SUFFIX
query event suffix value