HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
interface.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 #ifndef HF_INTERFACE_H
17 #define HF_INTERFACE_H
18 
19 #include "hanfun/common.h"
20 #include "hanfun/attributes.h"
21 
22 #include "hanfun/protocol.h"
23 
24 namespace HF
25 {
43  struct Interface
44  {
48  typedef enum _Role
49  {
52  } Role;
53 
57  typedef enum _UID
58  {
59  /* Core Services */
60  DEVICE_MANAGEMENT = 0x0001,
61  BIND_MANAGEMENT = 0x0002,
62  GROUP_MANAGEMENT = 0x0003,
63  IDENTIFY = 0x0004,
64  DEVICE_INFORMATION = 0x0005,
67  EVENT_SCHEDULING = 0x0008,
68  WEEKLY_SCHEDULING = 0x0009,
69  GROUP_TABLE = 0x000A,
70  TAMPER_ALERT = 0x0101,
71  TIME = 0x0102,
72  POWER = 0x0110,
73  KEEP_ALIVE = 0x0115,
74  RSSI = 0x0111,
75  SUOTA = 0x0400,
76 
77  /* Functional Interfaces. */
78  ALERT = 0x0100,
79  ON_OFF = 0x0200,
80  LEVEL_CONTROL = 0x0201,
81  COLOUR_CONTROL = 0x0202,
82  SIMPLE_KEYPAD = 0x0203,
83  SIMPLE_POWER_METER = 0x0300,
84  SIMPLE_TEMPERATURE = 0x0301,
85  SIMPLE_HUMIDITY = 0x0302,
86  SIMPLE_THERMOSTAT = 0x0303,
87  SIMPLE_BUTTON = 0x0304,
91 
92  /* Reserved */
93  RESERVED = 0x7F00,
94  MAX_UID = 0x7FFE,
95  ANY_UID = 0x7FFF,
96  } UID;
97 
101  struct Any: public Common::Interface
102  {
104  {}
105  };
106 
108  static constexpr uint8_t MAX_CMD_ID = 0xFF;
109 
110  virtual ~Interface() {}
111 
112  // =============================================================================
113  // API
114  // =============================================================================
115 
127  virtual uint16_t uid() const = 0;
128 
134  virtual Interface::Role role() const = 0;
135 
148  virtual Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload,
149  uint16_t offset) = 0;
150 
156  virtual void periodic(uint32_t time) = 0;
157 
166  virtual HF::Attributes::IAttribute *attribute(uint8_t uid) = 0;
167 
176  uint8_t pack_id = HF::Attributes::Pack::MANDATORY) const = 0;
177  };
178 
184  namespace Interfaces
185  {
197  struct AbstractInterface: virtual public HF::Interface
198  {
199  virtual ~AbstractInterface() {}
200 
202  uint16_t offset);
203 
204  void periodic(uint32_t time)
205  {
206  UNUSED(time);
207  }
208 
210  {
211  UNUSED(uid);
212  return nullptr;
213  }
214 
216  uint8_t pack_id = HF::Attributes::Pack::MANDATORY) const
217  {
218  UNUSED(pack_id);
219  return HF::Attributes::UIDS();
220  }
221 
222  bool operator==(AbstractInterface &other)
223  {
224  return uid() == other.uid();
225  }
226 
227  bool operator!=(AbstractInterface &other)
228  {
229  return !(*this == other);
230  }
231 
232  protected:
233 
240  virtual void send(const Protocol::Address &addr, Protocol::Message &message) = 0;
241 
248  virtual void notify(const HF::Attributes::IAttribute &old_value,
249  const HF::Attributes::IAttribute &new_value) const
250  {
251  UNUSED(old_value);
252  UNUSED(new_value);
253  }
254 
272  uint16_t offset);
273 
287  uint16_t offset);
288 
297  virtual uint16_t payload_size(Protocol::Message &message) const;
298 
307  virtual uint16_t payload_size(Protocol::Message::Interface &itf) const
308  {
309  UNUSED(itf);
310  return 0;
311  }
312 
321  template<typename _Message>
322  uint16_t payload_size_helper() const
323  {
324  _Message message;
325 
326  return message.size();
327  }
328 
338  Common::ByteArray &payload, uint16_t offset);
339 
352  Common::ByteArray &payload, uint16_t offset);
353 
362  virtual bool check_uid(uint16_t uid) const = 0;
363  };
364 
370  template<uint16_t _uid, typename Parent = AbstractInterface>
371  struct Interface: public Parent
372  {
374  uint16_t uid() const
375  {
376  return _uid;
377  }
378 
379  protected:
380 
381  bool check_uid(uint16_t uid) const
382  {
383  return Interface::uid() == uid;
384  }
385  };
386 
393  template<typename Itf, HF::Interface::Role _role>
394  struct InterfaceRole: public Itf
395  {
398  {
399  return _role;
400  }
401  };
402 
411  template<typename _Interface, typename _Proxy>
412  struct Proxy: public _Interface
413  {
414  static_assert(std::is_base_of<Interfaces::AbstractInterface, _Interface>::value,
415  "Interface MUST be of type HF::Interfaces::AbstractInterface !");
416 
417  typedef _Interface base;
418 
419  Proxy(_Proxy &_proxy): proxy(_proxy)
420  {}
421 
423  void send(const Protocol::Address &addr, Protocol::Message &message)
424  {
425  proxy.send(addr, message);
426  }
427 
429  void notify(const HF::Attributes::IAttribute &old_value,
430  const HF::Attributes::IAttribute &new_value) const
431  {
432  proxy.notify(old_value, new_value);
433  }
434 
435  protected:
436 
438  _Proxy &proxy;
439  };
440 
444  template<typename Base, typename... Proxies>
445  class Container
446  {
447  typedef std::tuple<Proxies...> interfaces_t;
448 
450  interfaces_t _interfaces;
451 
452  public:
453 
459  Container(Base &base): _interfaces(Proxies(base) ...)
460  {}
461 
464  uint16_t offset)
465  {
466  HF::Interface *itf = find<0, Proxies...>(packet.message.itf.id);
467 
468  if (itf != nullptr)
469  {
470  return itf->handle(packet, payload, offset);
471  }
472  else
473  {
475  }
476  }
477 
483  void attributes(HF::Attributes::List &attr_list, Common::Interface itf, uint8_t pack_id,
484  const HF::Attributes::UIDS &uids) const
485  {
486  attributes_itf<0, Proxies...>(attr_list, itf, pack_id, uids);
487  }
488 
495  std::vector<Common::Interface> interfaces() const
496  {
497  std::vector<Common::Interface> result;
498  result.reserve(sizeof ... (Proxies));
499 
500  Common::Interface temp;
501  /* *INDENT-OFF* */
502  for_each ([&result, &temp](HF::Interface &itf)
503  {
504  temp.id = itf.uid ();
505  temp.role = itf.role ();
506  result.push_back (temp);
507  });
508  /* *INDENT-ON* */
509 
510  return result;
511  }
512 
513  void periodic(uint32_t time)
514  {
515  /* *INDENT-OFF* */
516  for_each ([time](HF::Interface &itf) { itf.periodic (time);});
517  /* *INDENT-ON* */
518  }
519 
527  template<uint8_t N>
528  const typename std::tuple_element<N, interfaces_t>::type::base *get() const
529  {
530  return &std::get<N>(_interfaces);
531  }
532 
533  protected:
534 
540  void for_each(std::function<void(HF::Interface &)> func) const
541  {
542  for_each<0, Proxies...>(func);
543  }
544 
550  void for_each(std::function<void(HF::Interface &)> func)
551  {
552  for_each<0, Proxies...>(func);
553  }
554 
555  protected:
556 
569  template<uint8_t N, typename Head, typename... Tail>
570  HF::Interface *find(uint16_t itf_uid) const
571  {
572  static_assert(std::is_base_of<HF::Interface, Head>::value,
573  "Head must be of type HF::Interface");
574 
575  const Head &head = std::get<N>(_interfaces);
576 
577  if (head.uid() == itf_uid)
578  {
579  return const_cast<Head *>(&head);
580  }
581  else
582  {
583  return find<N + 1, Tail...>(itf_uid);
584  }
585  }
586 
597  template<uint8_t N>
598  HF::Interface *find(uint16_t itf_uid) const
599  {
600  UNUSED(itf_uid);
601  return nullptr;
602  }
603 
614  template<uint8_t N, typename Head, typename... Tail>
615  void for_each(std::function<void(HF::Interface &)> func) const
616  {
617  const auto &head = std::get<N>(this->_interfaces);
618 
619  func(*(const_cast<HF::Interface *>(static_cast<const HF::Interface *>(&head))));
620 
621  for_each<N + 1, Tail...>(func);
622  }
623 
632  template<uint8_t N>
633  void for_each(std::function<void(HF::Interface &)> func) const
634  {
635  UNUSED(func);
636  }
637 
652  template<uint8_t N, typename Head, typename... Tail>
654  uint8_t pack_id, const HF::Attributes::UIDS &uids) const
655  {
656  const auto &head = std::get<N>(this->_interfaces);
657 
658  if (head.uid() == itf.id)
659  {
660  auto result = HF::Attributes::get(head, pack_id, uids);
661  attrs.merge(result);
662  }
663  else
664  {
665  attributes_itf<N + 1, Tail...>(attrs, itf, pack_id, uids);
666  }
667  }
668 
682  template<uint8_t N>
684  uint8_t pack_id, const HF::Attributes::UIDS &uids) const
685  {
686  UNUSED(attrs);
687  UNUSED(itf);
688  UNUSED(pack_id);
689  UNUSED(uids);
690  }
691  };
692 
695  } // namespace Interfaces
696 
699 } // namespace HF
700 
706 // =============================================================================
707 // Stream Helpers
708 // =============================================================================
709 
718 std::ostream &operator<<(std::ostream &stream, const HF::Interface::Role role);
719 
728 std::ostream &operator<<(std::ostream &stream, const HF::Interface::UID uid);
729 
732 #endif /* HF_INTERFACE_H */
Common::Result check(Protocol::Message &message, Common::ByteArray &payload, uint16_t offset)
Check if message has correct attributes to be processed by the interface.
This class represents a HAN-FUN Concentrator.
Definition: base.h:302
virtual HF::Attributes::IAttribute * attribute(uint8_t uid)=0
Return a pointer to the interface attribute with the given uid.
virtual void periodic(uint32_t time)=0
Handle periodic processing.
List get(const HF::Interface &itf, uint8_t pack_id, const UIDS &uids)
Get a list with the attributes for the given interface, pack id or the uids passed in...
UID
Interfaces Unique Identifiers (UID).
Definition: interface.h:57
Group Management interface UID.
Definition: interface.h:62
Top-level parent class for all implemented interfaces.
Definition: interface.h:197
HF::Interface::Role role() const
Return the Interface::Role this interface implements.
Definition: interface.h:397
HF::Interface * find(uint16_t itf_uid) const
Final template instantiation that finds the wrapped interface with the given UID. ...
Definition: interface.h:598
HF::Interface * find(uint16_t itf_uid) const
Find the interface with the given UID.
Definition: interface.h:570
virtual void notify(const HF::Attributes::IAttribute &old_value, const HF::Attributes::IAttribute &new_value) const
Notify that an attribute value as changed.
Definition: interface.h:248
virtual void send(const Protocol::Address &addr, Protocol::Message &message)=0
Send message msg to the network address given by addr.
virtual Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Message message
Packet message payload;.
Definition: protocol.h:306
void attributes_itf(HF::Attributes::List &attrs, Common::Interface itf, uint8_t pack_id, const HF::Attributes::UIDS &uids) const
Helper function used to provide HF::Units::Unit::attributes functionality.
Definition: interface.h:653
List of attributes UIDs.
Definition: attributes.h:176
Attribute Reporting interface UID.
Definition: interface.h:65
Weekly Scheduling interface UID.
Definition: interface.h:68
This file contains the definitions for the attribute handling API in HAN-FUN.
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
Definition: interface.h:209
Colour Control interface UID.
Definition: interface.h:81
Return all mandatory attributes for the interface.
Definition: attributes.h:842
This file contains the common defines for the HAN-FUN library.
Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
uint16_t payload_size_helper() const
Helper function template to retrieve minimum size required for serializing/deserializing the class gi...
Definition: interface.h:322
Simple Temperature interface UID.
Definition: interface.h:84
Helper class template for parent class implementation of the interfaces.
Definition: interface.h:371
Container(Base &base)
Constructor.
Definition: interface.h:459
_Proxy & proxy
Referent to the class providing the required functionality.
Definition: interface.h:438
Helper class to add optional interfaces to other classes.
Definition: interface.h:445
virtual Common::Result handle_attribute(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
uint16_t id
Identifier of the interface.
This class has the same behavior has a list, however the list element access methods where overwritte...
Definition: attributes.h:752
Simple Keypad interface UID.
Definition: interface.h:82
This file contains the definitions for the HAN-FUN protocol messages.
void for_each(std::function< void(HF::Interface &)> func) const
Helper template function to implement the HF::Units::Unit::for_each functionality.
Definition: interface.h:633
SUOTA interface UID.
Definition: interface.h:75
Helper template class to allow interfaces implementation to be added as fields to other classes...
Definition: interface.h:412
Helper class to match any interface.
Definition: interface.h:101
Alert interface UID.
Definition: interface.h:78
virtual 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: interface.h:307
std::vector< Common::Interface > interfaces() const
Return the list of interfaces present in the wrapper.
Definition: interface.h:495
void for_each(std::function< void(HF::Interface &)> func) const
Call the given function for all the interfaces.
Definition: interface.h:540
Role
Interface roles.
Definition: interface.h:48
Simple Button interface UID.
Definition: interface.h:87
Time interface UID.
Definition: interface.h:71
Device Management interface UID.
Definition: interface.h:60
void notify(const HF::Attributes::IAttribute &old_value, const HF::Attributes::IAttribute &new_value) const
Notify that an attribute value as changed.
Definition: interface.h:429
uint16_t role
Interface role : Server or Client.
Max interface UID value.
Definition: interface.h:94
virtual HF::Attributes::UIDS attributes(uint8_t pack_id=HF::Attributes::Pack::MANDATORY) const =0
Return a vector containing the attribute UIDs, for the given pack ID.
Identify interface UID.
Definition: interface.h:63
void send(const Protocol::Address &addr, Protocol::Message &message)
Send message msg to the network address given by addr.
Definition: interface.h:423
Simple Power Meter interface UID.
Definition: interface.h:83
This class represents a byte array.
static constexpr uint8_t MAX_CMD_ID
Maximum value for command IDs in interfaces.
Definition: interface.h:108
Simple Light Sensor interface UID.
Definition: interface.h:90
Common::Result check_payload_size(Protocol::Message &message, Common::ByteArray &payload, uint16_t offset)
Check if payload data size if sufficient for processing the message.
Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Definition: interface.h:463
Group Table interface UID.
Definition: interface.h:69
Simple Visual Effects interface UID.
Definition: interface.h:88
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
virtual bool check_uid(uint16_t uid) const =0
Check if the given UID matches the interface UID.
Interface Address.
Definition: protocol.h:93
Network Message.
Definition: protocol.h:60
void for_each(std::function< void(HF::Interface &)> func)
Call the given function for all the interfaces.
Definition: interface.h:550
virtual uint16_t payload_size(Protocol::Message &message) const
Return the minimal payload size that should be present for the given message.
std::ostream & operator<<(std::ostream &stream, const HF::Interface::Role role)
Convert the given role into a string and write it to the given stream.
Simple Humidity interface UID.
Definition: interface.h:85
Event Scheduling interface UID.
Definition: interface.h:67
HAN-FUN Protocol Packet.
Definition: protocol.h:298
Interface itf
Interface Address.
Definition: protocol.h:129
Fail - Not Supported.
Interface/Service Attribute API.
Definition: attributes.h:44
Any interface UID value.
Definition: interface.h:95
Simple Air Pressure interface UID.
Definition: interface.h:89
Tamper Alert interface UID.
Definition: interface.h:70
ON-OFF interface UID.
Definition: interface.h:79
virtual Interface::Role role() const =0
Return the Interface::Role this interface implements.
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.
void for_each(std::function< void(HF::Interface &)> func) const
Helper template function to implement the HF::Units::Unit::for_each functionality.
Definition: interface.h:615
Bind Management interface UID.
Definition: interface.h:61
Power interface UID.
Definition: interface.h:72
virtual Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)=0
Handle incoming messages from the network.
RSSI interface UID.
Definition: interface.h:74
Device information interface UID.
Definition: interface.h:64
Common interface for all Interfaces.
Definition: interface.h:43
Proprietary interfaces.
Definition: interface.h:93
Simple Thermostat interface UID.
Definition: interface.h:86
virtual 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: interface.h:215
void attributes(HF::Attributes::List &attr_list, Common::Interface itf, uint8_t pack_id, const HF::Attributes::UIDS &uids) const
Return a list of all the attributes for a given interface, pack id and list of attributes UID&#39;s...
Definition: interface.h:483
void attributes_itf(HF::Attributes::List &attrs, Common::Interface itf, uint8_t pack_id, const HF::Attributes::UIDS &uids) const
Helper function used to provide HF::Units::Unit::attributes functionality.
Definition: interface.h:683
void periodic(uint32_t time)
Handle periodic processing.
Definition: interface.h:204
virtual uint16_t uid() const =0
This method returns the interface UID.
Batch Program Management interface UID.
Definition: interface.h:66
Result
Commands result codes.
Keep Alive interface UID.
Definition: interface.h:73
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22