HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
level_control.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_ITF_LEVEL_CONTROL_H
18 #define HF_ITF_LEVEL_CONTROL_H
19 
20 #include "hanfun/protocol.h"
21 #include "hanfun/interface.h"
22 
23 namespace HF
24 {
25  namespace Interfaces
26  {
27  namespace LevelControl
28  {
29  class Server;
30  }
31 
47  uint8_t uid);
48 
52  namespace LevelControl
53  {
63  typedef enum _CMD
65  {
66  SET_LEVEL_CMD = 0x01,
69  __LAST_CMD__ = DECREASE_LEVEL_CMD
70  } CMD;
71 
73  typedef enum _Attributes
74  {
75  LEVEL_ATTR = 0x01,
76  __LAST_ATTR__ = LEVEL_ATTR,
77  } Attributes;
78 
79  struct Message
80  {
82  uint8_t level;
83 
84  Message(uint8_t level = 0): level(level) {}
85 
87  static constexpr uint16_t min_size = sizeof(uint8_t);
88 
90  uint16_t size() const
91  {
92  return min_size;
93  }
94 
96  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const
97  {
98  HF_SERIALIZABLE_CHECK(array, offset, min_size);
99 
100  array.write(offset, level);
101 
102  return min_size;
103  }
104 
106  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0)
107  {
108  HF_SERIALIZABLE_CHECK(array, offset, min_size);
109 
110  array.read(offset, level);
111 
112  return min_size;
113  }
114  };
115 
119  struct Level: public HF::Attributes::Attribute<uint8_t>
120  {
121  static constexpr uint8_t ID = LEVEL_ATTR;
122  static constexpr bool WRITABBLE = true;
123 
124  Level(uint8_t level = 0, HF::Interface *owner = nullptr):
125  Attribute<uint8_t>(HF::Interface::LEVEL_CONTROL, ID, owner, level, WRITABBLE)
126  {}
127  };
128 
140 
146  struct Base: public Interface<HF::Interface::LEVEL_CONTROL>
147  {
148  protected:
149 
150  Base() {}
151 
153 
155  {
156  UNUSED(itf);
157  return payload_size_helper<Level>();
158  }
159 
165  void check_and_fix(float &value)
166  {
167  if (value < 0)
168  {
169  value = 0;
170  }
171  else if (value > 100)
172  {
173  value = 100;
174  }
175  }
176 
182  void check_and_fix(int16_t &value)
183  {
184  if (value < 0)
185  {
186  value = 0;
187  }
188  else if (value > 255)
189  {
190  value = 255;
191  }
192  }
193  };
194 
208  class Server: public InterfaceRole<LevelControl::Base, HF::Interface::SERVER_ROLE>
209  {
210  protected:
211 
213  uint8_t _level;
214 
215  public:
216 
218  Server(): _level(0) {}
219 
220  // ======================================================================
221  // API
222  // ======================================================================
223 
229  uint8_t level();
230 
236  void level(uint8_t new_level);
237 
246  void level(float new_level);
247 
248 #ifdef HF_ITF_LEVEL_CONTROL_INCREASE_LEVEL_CMD
249 
254  void increase(uint8_t increment);
255 
262  void increase(float increment);
263 #endif
264 
265 #ifdef HF_ITF_LEVEL_CONTROL_DECREASE_LEVEL_CMD
266 
271  void decrease(uint8_t decrement);
272 
279  void decrease(float decrement);
280 #endif
281 
282  // =============================================================================
283  // Events
284  // =============================================================================
287 
299  virtual void level_change(Protocol::Address &source,
300  uint8_t old_level,
301  uint8_t new_level);
302 
304  // =============================================================================
305 
306  // =============================================================================
307  // Attributes API
308  // =============================================================================
309 
311  {
312  return Interfaces::create_attribute(this, uid);
313  }
314 
317  {
318  UNUSED(pack_id);
319  /* *INDENT-OFF* */
321  /* *INDENT-ON* */
322  }
323 
324  protected:
325 
327  uint16_t offset);
328 
330  uint16_t offset);
331  };
332 
338  class Client: public InterfaceRole<LevelControl::Base, HF::Interface::CLIENT_ROLE>
339  {
340  public:
341 
342  // ======================================================================
343  // Commands
344  // ======================================================================
347 
355  void level(Protocol::Address &addr, uint8_t new_level);
356 
363  void level(uint8_t new_level)
364  {
365  Protocol::Address addr;
366  level(addr, new_level);
367  }
368 
375  void level(Protocol::Address &addr, float new_level);
376 
380  void level(float new_level)
381  {
382  Protocol::Address addr;
383  level(addr, new_level);
384  }
385 
386 #ifdef HF_ITF_LEVEL_CONTROL_INCREASE_LEVEL_CMD
387 
394  void increase_level(Protocol::Address &addr, uint8_t increment);
395 
402  void increase_level(uint8_t increment)
403  {
404  Protocol::Address addr;
405  increase_level(addr, increment);
406  }
407 
414  void increase_level(Protocol::Address &addr, float increment);
415 
422  void increase_level(float increment)
423  {
424  Protocol::Address addr;
425  increase_level(addr, increment);
426  }
427 #endif
428 
429 #ifdef HF_ITF_LEVEL_CONTROL_DECREASE_LEVEL_CMD
430 
437  void decrease_level(Protocol::Address &addr, uint8_t decrement);
438 
445  void decrease_level(uint8_t decrement)
446  {
447  Protocol::Address addr;
448  decrease_level(addr, decrement);
449  }
450 
457  void decrease_level(Protocol::Address &addr, float decrement);
458 
465  void decrease_level(float decrement)
466  {
467  Protocol::Address addr;
468  decrease_level(addr, decrement);
469  }
470 #endif
471  // =============================================================================
473  };
474 
477  } // namespace LevelControl
478 
479  } // namespace Interfaces
480 
481 } // namespace HF
482 
488 // =============================================================================
489 // Stream Helpers
490 // =============================================================================
491 
500 std::ostream &operator<<(std::ostream &stream, const HF::Interfaces::LevelControl::CMD command);
501 
510 std::ostream &operator<<(std::ostream &stream,
512 
515 #endif /* LEVEL_CONTROL_H_ */
virtual void level_change(Protocol::Address &source, uint8_t old_level, uint8_t new_level)
Callback for a SET_ATTR_REQ or SET_LEVEL_CMD message, when the level value is changed.
List of attributes UIDs.
Definition: attributes.h:176
Return all mandatory attributes for the interface.
Definition: attributes.h:842
Helper class to handle the Level attribute for the Level Control interface.
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
Level Control Interface : Client side implementation.
This file contains the definitions for the HAN-FUN protocol messages.
void check_and_fix(float &value)
Make sure level percentage values is in range [0,100].
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
#define HF_SERIALIZABLE_CHECK(__array, __offset, __size)
Helper macro to check if the given __array has enough size so __size bytes can be written/read from t...
This file contains the definitions common to all interfaces.
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.
HF::Interface const * owner() const
Definition: attributes.h:414
This class represents a byte array.
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Level Control interface UID.
Definition: interface.h:80
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
Level Control Interface : Parent.
Level Control Interface : Server side implementation.
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
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::LevelCont...
void level(uint8_t new_level)
Send a SET_LEVEL_CMD to broadcast network address to set the level at new_level.
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.
Common::Result handle_attribute(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
void check_and_fix(int16_t &value)
Make sure level percentage values is in range [0,255].
Helper template class to declare an attribute with the given T type.
Definition: attributes.h:349
void level(Protocol::Address &addr, uint8_t new_level)
Send a SET_LEVEL_CMD to the given address to set the level at new_level.
void level(float new_level)
Send a SET_LEVEL_CMD to broadcast network address to set the level at new_level.
Common interface for all Interfaces.
Definition: interface.h:43
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...
std::ostream & operator<<(std::ostream &stream, const HF::Interfaces::LevelControl::CMD command)
Convert the given command into a string and write it to the given stream.
Result
Commands result codes.
uint8_t level()
Getter for the current level.
uint8_t _level
Current level value.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22