HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
on_off.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_ITF_ON_OFF_H
18 #define HF_ITF_ON_OFF_H
19 
20 #include "hanfun/protocol.h"
21 #include "hanfun/interface.h"
22 
23 namespace HF
24 {
25  namespace Interfaces
26  {
27  // Forward declaration.
28  namespace OnOff
29  {
30  class Server;
31  }
32 
48 
52  namespace OnOff
53  {
61  typedef enum _CMD
63  {
64  ON_CMD = 0x01,
65  OFF_CMD = 0x02,
66  TOGGLE_CMD = 0x03,
67  __LAST_CMD__ = TOGGLE_CMD,
68  } CMD;
69 
71  typedef enum _Attributes
72  {
73  STATE_ATTR = 0x01,
74  __LAST_ATTR__ = STATE_ATTR,
75  } Attributes;
76 
80  struct State: public HF::Attributes::Attribute<bool>
81  {
82  static constexpr uint8_t ID = STATE_ATTR;
83  static constexpr bool WRITABBLE = false;
84 
85  State(bool state = false, HF::Interface *owner = nullptr):
86  Attribute<bool>(HF::Interface::ON_OFF, ID, owner, state, WRITABBLE)
87  {}
88  };
89 
101 
107  struct Base: public Interface<HF::Interface::ON_OFF>
108  {
109  protected:
110 
111  Base() {}
112  };
113 
119  class Server: public InterfaceRole<OnOff::Base, HF::Interface::SERVER_ROLE>
120  {
121  protected:
122 
129  bool _state;
130 
131  public:
132 
134  Server(): _state(false) {}
135 
137  virtual ~Server() {}
138 
139  // ======================================================================
140  // Events
141  // ======================================================================
144 
150  virtual void on(Protocol::Address &source);
151 
157  virtual void off(Protocol::Address &source);
158 
164  virtual void toggle(Protocol::Address &source);
165 
167 
168  // =============================================================================
169  // Get/Set API.
170  // =============================================================================
171 
177  void state(bool state);
178 
187  bool state();
188 
189  // =============================================================================
190  // Attribute API.
191  // =============================================================================
192 
194  {
195  return Interfaces::create_attribute(this, uid);
196  }
197 
200  {
201  UNUSED(pack_id);
202  /* *INDENT-OFF* */
204  /* *INDENT-ON* */
205  }
206 
207  protected:
208 
210  uint16_t offset);
211  };
212 
218  struct Client: public InterfaceRole<OnOff::Base, HF::Interface::CLIENT_ROLE>
219  {
220  // ======================================================================
221  // Commands
222  // ======================================================================
225 
231  void on(Protocol::Address &addr);
232 
236  void on()
237  {
238  Protocol::Address addr;
239  on(addr);
240  }
241 
247  void off(Protocol::Address &addr);
248 
252  void off()
253  {
254  Protocol::Address addr;
255  off(addr);
256  }
257 
263  void toggle(Protocol::Address &addr);
264 
268  void toggle()
269  {
270  Protocol::Address addr;
271  toggle(addr);
272  }
273 
275  // =============================================================================
276  };
277 
280  } // namespace OnOff
281 
282  } // namespace Interfaces
283 
284 } // namespace HF
285 
291 // =============================================================================
292 // Stream Helpers
293 // =============================================================================
294 
303 std::ostream &operator<<(std::ostream &stream, const HF::Interfaces::OnOff::CMD command);
304 
313 std::ostream &operator<<(std::ostream &stream, const HF::Interfaces::OnOff::Attributes attribute);
314 
317 #endif /* HF_ITF_ON_OFF_H */
virtual void toggle(Protocol::Address &source)
Callback that is called when a TOGGLE_CMD message is received.
Attributes
Attributes.
Definition: on_off.h:71
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: on_off.h:198
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::OnOff::Se...
On-Off Interface : Client side implementation.
Definition: on_off.h:218
virtual ~Server()
Destructor.
Definition: on_off.h:137
static constexpr uint8_t ID
Attribute UID.
Definition: on_off.h:82
On Command ID.
Definition: on_off.h:64
List of attributes UIDs.
Definition: attributes.h:176
void toggle()
Send a OnOff::TOGGLE_CMD message to the broadcast address.
Definition: on_off.h:268
Return all mandatory attributes for the interface.
Definition: attributes.h:842
State attribute UID.
Definition: on_off.h:73
virtual void on(Protocol::Address &source)
Callback that is called when a ON_CMD message is received.
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
On-Off Interface : Parent.
Definition: on_off.h:107
static constexpr bool WRITABBLE
Attribute Read/Write.
Definition: on_off.h:83
This file contains the definitions for the HAN-FUN protocol messages.
Server()
Constructor.
Definition: on_off.h:134
This file contains the definitions common to all interfaces.
bool state()
Getter : get the current state of the interface :
On-Off Interface : Server side implementation.
Definition: on_off.h:119
Helper class to handle the State attribute for the On-Off interface.
Definition: on_off.h:80
void off()
Send a OnOff::OFF_CMD message to the broadcast address.
Definition: on_off.h:252
HF::Interface const * owner() const
Definition: attributes.h:414
This class represents a byte array.
Toggle Command ID.
Definition: on_off.h:66
std::ostream & operator<<(std::ostream &stream, const HF::Interfaces::OnOff::CMD command)
Convert the given command into a string and write it to the given stream.
Network Address.
Definition: protocol.h:201
uint16_t uid() const
This method returns the interface UID.
Definition: interface.h:374
virtual void off(Protocol::Address &source)
Callback that is called when a OFF_CMD message is received.
HAN-FUN Protocol Packet.
Definition: protocol.h:298
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.
Interface/Service Attribute API.
Definition: attributes.h:44
Helper class template for implementing a given interface role.
Definition: interface.h:394
bool _state
Current server state.
Definition: on_off.h:129
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
void on()
Send a OnOff::ON_CMD message to the broadcast address.
Definition: on_off.h:236
Off Command ID.
Definition: on_off.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, bool data, bool writable=false)
Attribute template constructor.
Definition: attributes.h:360
CMD
Command IDs.
Definition: on_off.h:62
Common interface for all Interfaces.
Definition: interface.h:43
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
Definition: on_off.h:193
Result
Commands result codes.
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22