HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
time.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_CORE_TIME_H
18 #define HF_CORE_TIME_H
19 
20 #include "hanfun/protocol.h"
21 #include "hanfun/core.h"
22 
23 namespace HF
24 {
25  namespace Core
26  {
27  // Forward declaration.
28  namespace Time
29  {
30  class Server;
31  }
32 
48 
52  namespace Time
53  {
62  typedef enum _Attributes
64  {
65  TIME_ATTR = 1,
66  __LAST_ATTR__ = TIME_ATTR
67  } Attributes;
68 
70  static constexpr uint16_t YEARS_BASE = 2000;
71 
73  static constexpr uint8_t YEARS_MIN = 0;
74 
76  static constexpr uint8_t YEARS_MAX = 136;
77 
79  static constexpr uint8_t MONTHS_MIN = 1;
80 
82  static constexpr uint8_t MONTHS_MAX = 12;
83 
85  static constexpr uint8_t DAYS_MIN = 1;
86 
88  static constexpr uint8_t DAYS_MAX = 31;
89 
91  static constexpr uint8_t HOURS_MIN = 0;
92 
94  static constexpr uint8_t HOURS_MAX = 23;
95 
97  static constexpr uint8_t MINUTES_MIN = 0;
98 
100  static constexpr uint8_t MINUTES_MAX = 59;
101 
103  static constexpr uint8_t SECONDS_MIN = 0;
104 
106  static constexpr uint8_t SECONDS_MAX = 59;
107 
108  struct Date;
109 
118  uint32_t convert(const Date &date);
119 
128  Date convert(uint32_t time);
129 
130  // =============================================================================
131  // Attribute Helper classes
132  // =============================================================================
133 
137  struct Value: public HF::Attributes::Attribute<uint32_t>
138  {
139  static constexpr uint32_t INVALID = 0xFFFFFFFF;
140  static constexpr uint32_t MIN = 0x00000000;
141  static constexpr uint32_t MAX = INVALID - 1;
142 
143  static constexpr uint8_t ID = TIME_ATTR;
144  static constexpr bool WRITABLE = true;
145 
146  Value(uint32_t value = 0, HF::Interface *owner = nullptr):
147  Attribute<uint32_t>(HF::Interface::TIME, ID, owner, value, WRITABLE)
148  {}
149  };
150 
159  Date convert(const Value &value);
160 
164  struct Date
165  {
166  uint8_t year;
167  uint8_t month;
168  uint8_t day;
169 
170  uint8_t hour;
171  uint8_t minute;
172  uint8_t second;
173 
174  Date(uint8_t _year = 0, uint8_t _month = 0, uint8_t _day = 0,
175  uint8_t _hour = 0, uint8_t _minute = 0, uint8_t _second = 0):
176  year(_year), month(_month), day(_day), hour(_hour),
177  minute(_minute), second(_second)
178  {}
179 
180  Date(uint32_t _time)
181  {
182  *this = convert(_time);
183  }
184  };
185 
186  static Date const Origin = Date(Value::MIN);
187 
188  static Date const End = Date(Value::MAX);
189 
201 
207  struct Base: public Service<HF::Interface::TIME>
208  {
209  protected:
210 
213  };
214 
220  class Server: public ServiceRole<Time::Base, HF::Interface::SERVER_ROLE>
221  {
222  protected:
223 
224  uint32_t _time;
225 
226  public:
227 
231  _time(Value::INVALID) {}
232 
234  virtual ~Server() {}
235 
236  // =============================================================================
237  // Get/Set API.
238  // =============================================================================
239 
245  virtual uint32_t time() const;
246 
252  void time(uint32_t __value);
253 
254  // =============================================================================
255  // Attribute API.
256  // =============================================================================
257 
259 
260  HF::Attributes::UIDS attributes(uint8_t pack_id =
262 
263  // =============================================================================
264  // API
265  // =============================================================================
266 
272  void tick(uint32_t count = 1)
273  {
275  return;});
276  _time += count;
277  }
278  };
279 
285  struct Client: public ServiceRole<Time::Base, HF::Interface::CLIENT_ROLE>
286  {
288 
289  virtual ~Client() {}
290  };
291 
294  } // namespace Time
295 
296  } // namespace Core
297 
298 } // namespace HF
299 
305 inline bool operator==(const HF::Core::Time::Date &lhs, const HF::Core::Time::Date &rhs)
306 {
307  return (memcmp(&lhs, &rhs, sizeof(HF::Core::Time::Date)) == 0);
308 }
309 
310 inline bool operator!=(const HF::Core::Time::Date &lhs, const HF::Core::Time::Date &rhs)
311 {
312  return !(lhs == rhs);
313 }
314 
315 inline bool operator<(const HF::Core::Time::Date &lhs, const HF::Core::Time::Date &rhs)
316 {
317  return memcmp(&lhs, &rhs, sizeof(HF::Core::Time::Date)) < 0;
318 }
319 
320 inline bool operator<=(const HF::Core::Time::Date &lhs, const HF::Core::Time::Date &rhs)
321 {
322  return memcmp(&lhs, &rhs, sizeof(HF::Core::Time::Date)) <= 0;
323 }
324 
325 inline bool operator>(const HF::Core::Time::Date &lhs, const HF::Core::Time::Date &rhs)
326 {
327  return memcmp(&lhs, &rhs, sizeof(HF::Core::Time::Date)) > 0;
328 }
329 
330 inline bool operator>=(const HF::Core::Time::Date &lhs, const HF::Core::Time::Date &rhs)
331 {
332  return memcmp(&lhs, &rhs, sizeof(HF::Core::Time::Date)) >= 0;
333 }
334 
335 // =============================================================================
336 // Stream Helpers
337 // =============================================================================
338 
339 
348 std::ostream &operator<<(std::ostream &stream,
349  const HF::Core::Time::Attributes attribute);
350 
359 std::ostream &operator<<(std::ostream &stream,
360  const HF::Core::Time::Date &date);
363 #endif /* HF_CORE_TIME_H */
static constexpr uint32_t INVALID
Invalid value for time.
Definition: time.h:139
uint8_t second
Seconds : 0 - 59.
Definition: time.h:172
static constexpr uint8_t ID
Attribute UID.
Definition: time.h:143
This represent the special unit with ID/UID = 0.
Definition: core.h:67
List of attributes UIDs.
Definition: attributes.h:176
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::Time::Ser...
Base(Unit0 &unit)
Constructor.
Definition: time.h:212
Helper class to parse the time value into a human readable string.
Definition: time.h:164
Return all mandatory attributes for the interface.
Definition: attributes.h:842
#define HF_ASSERT(_expr, _block)
Helper macro to check for correct assumptions.
This file contains the forward declarations of the core services and interfaces implementing classes...
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
This file contains the definitions for the HAN-FUN protocol messages.
uint32_t convert(const Date &date)
Convert the date given by date to the corresponding time value.
static constexpr bool WRITABLE
Attribute Read/Write.
Definition: time.h:144
static constexpr uint32_t MAX
Maximum value for time.
Definition: time.h:141
Time interface UID.
Definition: interface.h:71
uint32_t _time
Time value.
Definition: time.h:224
uint8_t day
Day : 1 - 31.
Definition: time.h:168
Attributes
Attributes.
Definition: time.h:63
uint16_t uid() const
This method returns the interface UID.
Definition: core.h:191
void tick(uint32_t count=1)
Increment time by the amount given by count seconds.
Definition: time.h:272
HF::Interface const * owner() const
Definition: attributes.h:414
Unit0 & unit() const
The device this unit is associated with.
Definition: core.h:142
std::ostream & operator<<(std::ostream &stream, const HF::Core::Time::Attributes attribute)
Convert the given attribute into a string and write it to the given stream.
Time Service : Client side implementation.
Definition: time.h:285
uint8_t year
Year : 20(00) - 2(255)
Definition: time.h:166
virtual uint32_t time() const
Get the Time for the Time server.
Time Service : Parent.
Definition: time.h:207
static constexpr uint32_t MIN
Minimum value for time.
Definition: time.h:140
Time Service : Server side implementation.
Definition: time.h:220
Interface/Service Attribute API.
Definition: attributes.h:44
Class template for all interfaces role implementations.
Definition: core.h:225
Time
This represents the type of time that is associated with a time measurement.
Time attribute UID.
Definition: time.h:65
Helper template class to declare an attribute with the given T type.
Definition: attributes.h:349
Attribute(const uint16_t interface, const uint8_t uid, const HF::Interface *__owner, uint32_t data, bool writable=false)
Attribute template constructor.
Definition: attributes.h:360
Common interface for all Interfaces.
Definition: interface.h:43
Server(Unit0 &unit)
Constructor.
Definition: time.h:229
uint8_t month
Month: 1 - 12.
Definition: time.h:167
Class template for all core services implementations.
Definition: core.h:188
uint8_t minute
Minutes : 0 - 59.
Definition: time.h:171
HF::Attributes::UIDS attributes(uint8_t pack_id=HF::Attributes::Pack::MANDATORY) const
Return a vector containing the attribute UIDs, for the given pack ID.
virtual ~Server()
Destructor.
Definition: time.h:234
uint8_t hour
Hours : 0 - 23.
Definition: time.h:170
Helper class to handle the Time attribute for the Time service.
Definition: time.h:137
HF::Attributes::IAttribute * create_attribute(HF::Core::AttributeReporting::IServer *server, uint8_t uid)
Create an attribute object that can hold the attribute with the given uid.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22