Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
BroadcastTimeDefs.hxx
Go to the documentation of this file.
1
36#ifndef _OPENLCB_BROADCASTTIMEDEFS_HXX_
37#define _OPENLCB_BROADCASTTIMEDEFS_HXX_
38
39#include <time.h>
40
41#include "openlcb/Defs.hxx"
42
43namespace openlcb
44{
45
48{
50 static constexpr NodeID DEFAULT_FAST_CLOCK_ID = 0x010100000100ULL;
51
53 static constexpr NodeID DEFAULT_REALTIME_CLOCK_ID = 0x010100000101ULL;
54
56 static constexpr NodeID ALTERNATE_CLOCK_1_ID = 0x010100000102ULL;
57
59 static constexpr NodeID ALTERNATE_CLOCK_2_ID = 0x010100000103ULL;
60
78
79 enum
80 {
81 EVENT_ID_MASK = 0xFFFFFFFFFFFF0000,
82 EVENT_SUFFIX_MASK = 0x000000000000FFFF,
83 EVENT_TYPE_MASK = 0x000000000000F000,
84 EVENT_HOURS_MASK = 0x0000000000001F00,
85 EVENT_MINUTES_MASK = 0x00000000000000FF,
86 EVENT_MONTH_MASK = 0x0000000000000F00,
87 EVENT_DAY_MASK = 0x00000000000000FF,
88 EVENT_YEAR_MASK = 0x0000000000000FFF,
89 EVENT_RATE_MASK = 0x0000000000000FFF,
90
97
106
108 };
109
110 enum
111 {
112 SUNDAY = 0,
119 };
120
124 static EventType get_event_type(uint16_t suffix)
125 {
126 switch (suffix & EVENT_TYPE_MASK)
127 {
128 case 0x0000:
129 case 0x1000:
130 if (valid_time(suffix))
131 {
132 return REPORT_TIME;
133 }
134 break;
135 case 0x2000:
136 if (valid_date(suffix))
137 {
138 return REPORT_DATE;
139 }
140 break;
141 case 0x3000:
142 return REPORT_YEAR;
143 case 0x4000:
144 return REPORT_RATE;
145 case 0x8000:
146 case 0x9000:
147 if (valid_time(suffix))
148 {
149 return SET_TIME;
150 }
151 break;
152 case 0xA000:
153 if (valid_date(suffix))
154 {
155 return SET_DATE;
156 }
157 break;
158 case 0xB000:
159 return SET_YEAR;
160 case 0xC000:
161 return SET_RATE;
162 case 0xF000:
163 switch (suffix & 0xFFF)
164 {
165 case 0x000:
166 return QUERY;
167 case 0x001:
168 return STOP;
169 case 0x002:
170 return START;
171 case 0x003:
172 return DATE_ROLLOVER;
173 default:
174 break;
175 }
176 default:
177 break;
178 }
179 return UNDEFINED;
180 }
181
185 static bool valid_time(uint16_t suffix)
186 {
187 return (((suffix & EVENT_HOURS_MASK) >> EVENT_HOURS_SHIFT) <= 23 &&
188 ((suffix & EVENT_MINUTES_MASK) >> EVENT_MINUTES_SHIFT) <= 59);
189 }
190
194 static bool valid_date(uint16_t suffix)
195 {
196 return (((suffix & EVENT_MONTH_MASK) >> EVENT_MONTH_SHIFT) >= 1 &&
197 ((suffix & EVENT_MONTH_MASK) >> EVENT_MONTH_SHIFT) <= 12 &&
198 ((suffix & EVENT_DAY_MASK) >> EVENT_DAY_SHIFT) >= 1 &&
199 ((suffix & EVENT_DAY_MASK) >> EVENT_DAY_SHIFT) <= 31);
200 }
201
205 static int event_to_min(uint64_t event)
206 {
207 unsigned min = (event & EVENT_MINUTES_MASK) >> EVENT_MINUTES_SHIFT;
208 if (min <= 59)
209 {
210 return min;
211 }
212 return -1;
213 }
214
218 static int event_to_hour(uint64_t event)
219 {
220 unsigned hour = (event & EVENT_HOURS_MASK) >> EVENT_HOURS_SHIFT;
221 if (hour <= 23)
222 {
223 return hour;
224 }
225 return -1;
226 }
227
231 static int event_to_day(uint64_t event)
232 {
233 unsigned day = (event & EVENT_DAY_MASK) >> EVENT_DAY_SHIFT;
234 if (day >= 1 && day <= 31)
235 {
236 return day;
237 }
238 return -1;
239 }
240
244 static int event_to_month(uint64_t event)
245 {
246 unsigned month = (event & EVENT_MONTH_MASK) >> EVENT_MONTH_SHIFT;
247 if (month >= 1 && month <= 12)
248 {
249 return month;
250 }
251 return -1;
252 }
253
257 static int event_to_year(uint64_t event)
258 {
259 return (event & EVENT_YEAR_MASK) >> EVENT_YEAR_SHIFT;
260 }
261
265 static int16_t event_to_rate(uint64_t event)
266 {
267 union Rate
268 {
269 uint16_t rate_;
270 int16_t srate_;
271 };
272
273 Rate rate;
274
275 rate.rate_ = (event & EVENT_RATE_MASK) >> EVENT_RATE_SHIFT;
276 if (rate.rate_ & 0x0800)
277 {
278 // sign extend negative value
279 rate.rate_ |= 0xF000;
280 }
281
282 return rate.srate_;
283 }
284
290 static uint64_t time_to_event(uint64_t event_base, int hours, int minutes)
291 {
292 HASSERT(minutes >= 0 && minutes <= 59);
293 HASSERT(hours >= 0 && hours <= 23);
294
295 return event_base + TIME_EVENT_BASE_SUFFIX +
296 (hours << EVENT_HOURS_SHIFT) + (minutes << EVENT_MINUTES_SHIFT);
297 }
298
304 static uint64_t date_to_event(uint64_t event_base, int month, int day)
305 {
306 HASSERT(month >= 1 && month <= 12);
307 HASSERT(day >= 1 && day <= 31);
308
309 return event_base + DATE_EVENT_BASE_SUFFIX +
310 (month << EVENT_MONTH_SHIFT) + (day << EVENT_DAY_SHIFT);
311 }
312
317 static uint64_t year_to_event(uint64_t event_base, int year)
318 {
319 HASSERT(year >= 0 && year <= 4095);
320
321 return event_base + YEAR_EVENT_BASE_SUFFIX + (year << EVENT_YEAR_SHIFT);
322 }
323
328 static uint64_t rate_to_event(uint64_t event_base, int16_t rate)
329 {
330 HASSERT(rate >= -2048 && rate < 2048);
331
332 union Rate
333 {
334 uint16_t rate_;
335 int16_t srate_;
336 };
337
338 Rate r;
339 r.srate_ = rate;
340
341 return event_base + RATE_EVENT_BASE_SUFFIX +
342 ((r.rate_ & EVENT_RATE_MASK) << EVENT_RATE_SHIFT);
343 }
344
349 static std::string time_to_string(int hour, int min);
350
354 static std::string rate_quarters_to_string(int16_t rate);
355
360 static std::string date_to_string(int year, int month, int day);
361
367 static bool string_to_time(const std::string &stime, int *hour, int *min);
368
374 static int16_t string_to_rate_quarters(const std::string &srate);
375
386 static bool string_to_date(
387 const std::string &sdate, int *year, int *month, int *day);
388
394 static bool canonicalize_time_string(std::string *stime);
395
401 static bool canonicalize_rate_string(std::string* srate);
402
408 static bool canonicalize_date_string(std::string* sdate);
409};
410
411} // namespace openlcb
412
413#endif // _OPENLCB_BROADCASTTIMEDEFS_HXX_
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138
uint64_t NodeID
48-bit NMRAnet Node ID type
Static constants and helper functions for Broadcast Time Protocol.
static uint64_t time_to_event(uint64_t event_base, int hours, int minutes)
Build an event from hours and minutes.
static std::string time_to_string(int hour, int min)
Convert time in integer hours/minutes to a string ("hh:mm").
static int event_to_day(uint64_t event)
Get the day from the event.
static int event_to_month(uint64_t event)
Get the month from the event.
static uint64_t year_to_event(uint64_t event_base, int year)
Build an event from year.
static int event_to_min(uint64_t event)
Get the minutes from the event.
static std::string rate_quarters_to_string(int16_t rate)
Convert rate in integer rate quarters to a string (float).
static constexpr NodeID ALTERNATE_CLOCK_1_ID
Unique identifier for Alternate Clock 1.
static constexpr NodeID ALTERNATE_CLOCK_2_ID
Unique identifier for Alternate Clock 2.
static bool string_to_time(const std::string &stime, int *hour, int *min)
Convert a string (hh:mm) to hour and minute component integers.
static EventType get_event_type(uint16_t suffix)
Get the EventTuype from the event suffix number.
static int16_t string_to_rate_quarters(const std::string &srate)
Convert a string (float) to rate quarters.
static uint64_t rate_to_event(uint64_t event_base, int16_t rate)
Build an event from rate.
static constexpr NodeID DEFAULT_REALTIME_CLOCK_ID
Unique identifier for the Default Real-Time Clock.
@ TUESDAY
Day of the week value Tuesday.
@ SUNDAY
Day of the week value Sunday.
@ THURSDAY
Day of the week value Thursday.
@ MONDAY
Day of the week value Monday.
@ WEDNESDAY
Day of the week value Wednesday.
@ FRIDAY
Day of the week value Friday.
@ SATURDAY
Day of the week value Saturday.
static bool canonicalize_time_string(std::string *stime)
Verifies that a user-provided string parses as time, and canonicalizes the string format.
static int16_t event_to_rate(uint64_t event)
Get the rate from the event.
static constexpr NodeID DEFAULT_FAST_CLOCK_ID
Unique identifier for the Default Fast Clock.
static uint64_t date_to_event(uint64_t event_base, int month, int day)
Build an event from month and day.
static int event_to_year(uint64_t event)
Get the year from the event.
static std::string date_to_string(int year, int month, int day)
Converts a date to a string "Mmm dd, yyyy".
static bool canonicalize_rate_string(std::string *srate)
Verifies that a user-provided string parses as rate quarters, and canonicalizes the string format.
static bool valid_date(uint16_t suffix)
Validate that this is a supported date event.
static bool valid_time(uint16_t suffix)
Validate that this is a supported time event.
@ DATE_ROLLOVER
date rollover event
static bool canonicalize_date_string(std::string *sdate)
Verifies that a user-provided string parses as date, and canonicalizes the string format.
static int event_to_hour(uint64_t event)
Get the hour from the event.
@ EVENT_SET_SUFFIX_MASK
suffix max for setting a property
@ START_EVENT_SUFFIX
start clock event suffix value
@ DATE_ROLLOVER_EVENT_SUFFIX
rollover the date suffix value
@ STOP_EVENT_SUFFIX
stop clock event suffix value
@ DATE_EVENT_BASE_SUFFIX
date event base suffix
@ YEAR_EVENT_BASE_SUFFIX
year event base suffix
@ RATE_EVENT_BASE_SUFFIX
rate event base suffix
@ TIME_EVENT_BASE_SUFFIX
time event base suffix
@ QUERY_EVENT_SUFFIX
query event suffix value
static bool string_to_date(const std::string &sdate, int *year, int *month, int *day)
Converts a (user-provided) string "Mmm dd, yyyy" to date.