HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
alert.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 #ifndef HF_ITF_ALERT_H
17 #define HF_ITF_ALERT_H
18 
19 #include "hanfun/common.h"
20 
21 #include "hanfun/protocol.h"
22 #include "hanfun/interface.h"
23 
24 // =============================================================================
25 // API
26 // =============================================================================
27 
28 namespace HF
29 {
30  namespace Interfaces
31  {
32  // Forward declaration.
33  namespace Alert
34  {
35  class Server;
36 
37  } // namespace Alert
38 
54  uint8_t uid);
55 
59  namespace Alert
60  {
68  typedef enum _CMD
70  {
71  STATUS_CMD = 0x01,
72  __LAST_CMD__ = STATUS_CMD
73  } CMD;
74 
76  typedef enum _Attributes
77  {
78  STATE_ATTR = 0x01,
79  ENABLE_ATTR = 0x02,
80  __LAST_ATTR__ = ENABLE_ATTR,
81  } Attributes;
82 
86  struct Message
87  {
89  static constexpr uint16_t min_size = sizeof(uint16_t) // Profile UID.
90  + sizeof(uint32_t); // State.
91 
92  uint16_t type;
93  uint32_t state;
94 
101  Message(uint16_t type = 0, uint32_t state = 0);
102 
104  uint16_t size() const;
105 
107  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
108 
110  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
111  };
112 
116  struct State: public HF::Attributes::Attribute<uint32_t>
117  {
118  static constexpr uint8_t ID = STATE_ATTR;
119  static constexpr bool WRITABBLE = false;
120 
121  State(uint32_t data = 0, const HF::Interface *owner = nullptr):
122  Attribute<uint32_t>(HF::Interface::ALERT, ID, owner, data, WRITABBLE)
123  {}
124  };
125 
129  struct Enable: public HF::Attributes::Attribute<uint32_t>
130  {
131  static constexpr uint8_t ID = ENABLE_ATTR;
132  static constexpr bool WRITABBLE = true;
133 
134  Enable(uint32_t data = 0, const HF::Interface *owner = nullptr):
135  Attribute<uint32_t>(HF::Interface::ALERT, ID, owner, data, WRITABBLE)
136  {}
137  };
138 
150 
156  struct Base: public Interface<HF::Interface::ALERT>
157  {
158  protected:
159 
160  Base() {}
161 
163 
165  {
166  UNUSED(itf);
167  return payload_size_helper<Message>();
168  }
169  };
170 
176  class Server: public InterfaceRole<Alert::Base, HF::Interface::SERVER_ROLE>
177  {
178  protected:
179 
180  uint32_t _state;
181  uint32_t _enabled;
182 
183  public:
184 
186  Server();
187 
189  virtual ~Server();
190 
191  // ======================================================================
192  // State API
193  // ======================================================================
194 
200  uint32_t state();
201 
207  void set_state(uint32_t value);
208 
219  bool state(uint8_t index, bool value);
220 
230  bool state(uint8_t index);
231 
235  void clear();
236 
237  // ======================================================================
238  // Enable API
239  // ======================================================================
240 
246  void enable(uint8_t index);
247 
251  void enableAll();
252 
261  bool enabled(uint8_t index);
262 
268  uint32_t enabled();
269 
275  void set_enabled(uint32_t value);
276 
282  void disable(uint8_t index);
283 
292  bool disabled(uint8_t index);
293 
297  void disableAll();
298 
299  // =============================================================================
300  // Attributes API
301  // =============================================================================
302 
304  {
305  return Interfaces::create_attribute(this, uid);
306  }
307 
308  // ======================================================================
309  // Commands
310  // ======================================================================
313 
321  void status(Protocol::Address &addr, uint16_t unit_type);
322 
329  void status(uint16_t unit_type)
330  {
331  Protocol::Address addr;
332  status(addr, unit_type);
333  }
334 
336  // ======================================================================
337 
340  {
341  UNUSED(pack_id);
342  /* *INDENT-OFF* */
344  /* *INDENT-ON* */
345  }
346 
347  protected:
348 
358  Alert::Message *create_status(uint16_t unit_type);
359 
360  };
361 
367  struct Client: public InterfaceRole<Alert::Base, HF::Interface::CLIENT_ROLE>
368  {
369  // ======================================================================
370  // Events
371  // ======================================================================
374 
382  virtual void status(Protocol::Address &source, Message &message)
383  {
384  UNUSED(source);
385  UNUSED(message);
386  }
387 
389  // ======================================================================
390 
391  protected:
392 
394  uint16_t offset);
395  };
396 
399  } // namespace Alert
400 
401  } // namespace Interfaces
402 
403 } // namespace HF
404 
410 // =============================================================================
411 // Stream Helpers
412 // =============================================================================
413 
422 std::ostream &operator<<(std::ostream &stream, const HF::Interfaces::Alert::CMD command);
423 
432 std::ostream &operator<<(std::ostream &stream, const HF::Interfaces::Alert::Attributes attribute);
433 
436 #endif /* HF_ITF_ALERT_H */
Message(uint16_t type=0, uint32_t state=0)
Constructor.
void set_enabled(uint32_t value)
Set the bitmask of the enable state of the alerts.
uint32_t enabled()
Return the bitmask of the enable state of the alerts.
static constexpr bool WRITABBLE
Attribute Read/Write.
Definition: alert.h:132
void status(uint16_t unit_type)
Send a HAN-FUN message containing a Alert::STATUS_CMD, to the broadcast network address.
Definition: alert.h:329
std::ostream & operator<<(std::ostream &stream, const HF::Interfaces::Alert::CMD command)
Convert the given command into a string and write it to the given stream.
CMD
Command IDs.
Definition: alert.h:69
Helper class to handle the Enabled attribute for the Alert interface.
Definition: alert.h:129
List of attributes UIDs.
Definition: attributes.h:176
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
uint16_t size() const
Number bytes needed to serialize the message.
Alert Interface : Parent.
Definition: alert.h:156
uint32_t _enabled
Alert&#39;s enable state.
Definition: alert.h:181
Return all mandatory attributes for the interface.
Definition: attributes.h:842
This file contains the common defines for the HAN-FUN library.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
static constexpr uint8_t ID
Attribute UID.
Definition: alert.h:118
uint16_t type
Unit Type that generated the message.
Definition: alert.h:92
virtual void status(Protocol::Address &source, Message &message)
Callback function called when a status update message is received from an Alert server.
Definition: alert.h:382
This file contains the definitions for the HAN-FUN protocol messages.
uint16_t payload_size(Protocol::Message::Interface &itf) const
Return the minimal payload size that a message should hold when addressed at the given interface...
Definition: alert.h:164
Payload for the Status command.
Definition: alert.h:86
uint32_t state
Current state of the server.
Definition: alert.h:93
Alert Status Command ID.
Definition: alert.h:71
Alert Interface : Client side implementation.
Definition: alert.h:367
Helper class to handle the State attribute for the Alert interface.
Definition: alert.h:116
This file contains the definitions common to all interfaces.
virtual ~Server()
Destructor.
uint32_t state()
Return the current state for all the alerts as a bitmask.
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
HF::Interface const * owner() const
Definition: attributes.h:414
void set_state(uint32_t value)
Set the state bitmask to the given value.
void enable(uint8_t index)
Enable alert at index.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: alert.h:89
This class represents a byte array.
void disable(uint8_t index)
Disable the alert at index.
uint32_t _state
Alert&#39;s state.
Definition: alert.h:180
void disableAll()
Disable all alerts.
Network Address.
Definition: protocol.h:201
uint16_t uid() const
This method returns the interface UID.
Definition: interface.h:374
Interface Address.
Definition: protocol.h:93
static constexpr uint8_t ID
Attribute UID.
Definition: alert.h:131
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
Definition: alert.h:303
State attribute UID.
Definition: alert.h:78
HAN-FUN Protocol Packet.
Definition: protocol.h:298
Alert Interface : Server side implementation.
Definition: alert.h:176
Alert::Message * create_status(uint16_t unit_type)
Create a HAN-FUN message containing a Alert::STATUS_CMD, to be sent to the network.
bool disabled(uint8_t index)
Check if alert at index is disabled.
HF::Attributes::IAttribute * create_attribute(HF::Interfaces::Alert::Server *server, uint8_t uid)
Create an attribute object that can hold the attribute with the given uid.
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::Alert::Se...
Interface/Service Attribute API.
Definition: attributes.h:44
void status(Protocol::Address &addr, uint16_t unit_type)
Send a HAN-FUN message containing a Alert::STATUS_CMD, to the given network address.
Helper class template for implementing a given interface role.
Definition: interface.h:394
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
Helper template class to declare an attribute with the given T type.
Definition: attributes.h:349
void enableAll()
Enable all alerts.
void clear()
Clear all the alerts.
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
Attributes
Attributes.
Definition: alert.h:76
Common interface for all Interfaces.
Definition: interface.h:43
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.
Definition: alert.h:338
Enable attribute UID.
Definition: alert.h:79
Result
Commands result codes.
static constexpr bool WRITABBLE
Attribute Read/Write.
Definition: alert.h:119
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22