HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
units.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 #ifndef HF_UNITS_H
17 #define HF_UNITS_H
18 
19 #include "hanfun/common.h"
20 #include "hanfun/profiles.h"
21 
22 #include "hanfun/device.h"
23 
24 namespace HF
25 {
26  // Forward declaration.
27  namespace Core
28  {
29  struct IService;
30  }
31 
35  namespace Units
36  {
48  struct IUnit: public Profiles::IProfile
49  {
50  virtual ~IUnit() {}
51 
57  virtual uint8_t id() const = 0;
58 
64  virtual IDevice &device() const = 0;
65 
67  virtual Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload,
68  uint16_t offset) = 0;
69 
77  virtual void send(const Protocol::Address &addr, Protocol::Message &message,
78  Transport::Link *link = nullptr) = 0;
79 
86  virtual std::vector<Common::Interface> interfaces() const = 0;
87 
89  virtual void periodic(uint32_t time) = 0;
90  };
91 
95  class AbstractUnit: public IUnit
96  {
98  IDevice &_device;
99 
100  public:
101 
102  virtual ~AbstractUnit()
103  {
104  device().remove(this);
105  }
106 
107  IDevice &device() const
108  {
109  return _device;
110  }
111 
112  void send(const Protocol::Address &addr, Protocol::Message &message,
113  Transport::Link *link);
114 
115  std::vector<Common::Interface> interfaces() const
116  {
117  return std::vector<Common::Interface>();
118  }
119 
120  void periodic(uint32_t time)
121  {
122  UNUSED(time);
123  }
124 
125  protected:
126 
128  uint8_t _id;
129 
136  {
137  device.add(this);
138  }
139 
146  void notify(const HF::Attributes::IAttribute &old_value,
147  const HF::Attributes::IAttribute &new_value) const;
148  };
149 
153  template<typename _Interface, typename _Proxy, typename = void>
154  struct Proxy: public HF::Interfaces::Proxy<_Interface, _Proxy>
155  {
156  Proxy(_Proxy &_proxy): HF::Interfaces::Proxy<_Interface, _Proxy>(_proxy)
157  {}
158 
159  HF::Units::IUnit &unit() const
160  {
162  }
163  };
164 
168  template<typename _Interface, typename _Proxy>
169  struct Proxy<_Interface, _Proxy, EnableIf(IsParent(HF::Core::IService, _Interface))>:
170  public _Interface
171  {
172  typedef _Interface base;
173 
174  Proxy(_Proxy &_proxy): _Interface(_proxy)
175  {}
176  };
177 
181  template<typename Base, typename... ITF>
182  class InterfacesWrapper: public Interfaces::Container<Base, Proxy<ITF, Base>...>
183  {
185 
186  public:
187 
188  InterfacesWrapper(Base &base): Container(base)
189  {}
190  };
191 
195  template<class Profile, typename... ITF>
196  class Unit: public HF::Units::AbstractUnit, public virtual Profile,
197  public InterfacesWrapper<HF::Units::Unit<Profile, ITF...>, ITF...>
198  {
199  uint8_t _id;
200 
201  typedef Unit<Profile, ITF...> Base;
202  typedef InterfacesWrapper<Unit<Profile, ITF...>, ITF...> Wrapper;
203 
204  public:
205 
212  Unit(uint8_t id, IDevice &device):
213  Profile(), HF::Units::AbstractUnit(device),
214  Wrapper(*this), _id(id)
215  {}
216 
218  uint8_t id() const
219  {
220  return _id;
221  }
222 
224  uint16_t uid() const
225  {
226  return Profile::uid();
227  }
228 
231  uint16_t offset)
232  {
233  Common::Result result = Profile::handle(packet, payload, offset);
234 
235  // Message not handled by base profile, then try extra interfaces.
236  if (result == Common::Result::FAIL_ARG)
237  {
238  return Wrapper::handle(packet, payload, offset);
239  }
240 
241  return result;
242  }
243 
246  const HF::Attributes::UIDS &uids) const
247  {
248  auto result = Profile::attributes(itf, pack_id, uids);
249 
250  Wrapper::attributes(result, itf, pack_id, uids);
251 
252  return result;
253  }
254 
256 
258  void send(const Protocol::Address &addr, Protocol::Message &message)
259  {
260  AbstractUnit::send(addr, message, nullptr);
261  }
262 
264  void notify(const HF::Attributes::IAttribute &old_value,
265  const HF::Attributes::IAttribute &new_value) const
266  {
267  AbstractUnit::notify(old_value, new_value);
268  }
269 
276  std::vector<Common::Interface> interfaces() const
277  {
278  return Wrapper::interfaces();
279  }
280 
281  void periodic(uint32_t time)
282  {
283  Profile::periodic(time);
284  Wrapper::periodic(time);
285  }
286  };
287 
290  } // namespace Units
291 
292 } // namespace HF
293 
294 #endif /* HF_UNITS_H */
void periodic(uint32_t time)
Handle periodic processing.
Definition: units.h:281
This class represents a HAN-FUN Concentrator.
Definition: base.h:302
virtual void remove(Units::IUnit *unit)=0
Remove unit from device&#39;s unit list.
void send(const Protocol::Address &addr, Protocol::Message &message)
Send message msg to the network address given by addr.
Definition: units.h:258
This is the parent class for all HAN-FUN units.
Definition: units.h:95
virtual void send(const Protocol::Address &addr, Protocol::Message &message, Transport::Link *link=nullptr)=0
Create and send a new packet with the given message to the given address.
void notify(const HF::Attributes::IAttribute &old_value, const HF::Attributes::IAttribute &new_value) const
Notify the attribute reporting service that the given attribute has changed value.
Helper class to handle optional interfaces in units.
Definition: units.h:182
List of attributes UIDs.
Definition: attributes.h:176
This file contains the common defines for the HAN-FUN library.
Helper class to add optional interfaces to other classes.
Definition: interface.h:445
This class has the same behavior has a list, however the list element access methods where overwritte...
Definition: attributes.h:752
AbstractUnit(IDevice &device)
Constructor.
Definition: units.h:135
virtual void add(Units::IUnit *unit)=0
Add unit to devices unit lists.
virtual uint8_t id() const =0
Get the id number of this unit on the device.
uint16_t uid() const
Return this profile HAN-FUN UID.
Definition: units.h:224
Helper template class to allow interfaces implementation to be added as fields to other classes...
Definition: interface.h:412
std::vector< Common::Interface > interfaces() const
Return the list of interfaces present in the wrapper.
Definition: interface.h:495
virtual Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)=0
Handle incoming messages from the network.
Helper template class to implement units.
Definition: units.h:196
This class represents the interface common to all HAN-FUN devices.
Definition: device.h:99
std::vector< Common::Interface > interfaces() const
Return a vector containing a list of extra interfaces, other than the interfaces specified by the pro...
Definition: units.h:115
This file contains the declarations and definitions for the HAN-FUN Profiles.
This class represents a byte array.
Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Definition: interface.h:463
virtual std::vector< Common::Interface > interfaces() const =0
Return a vector containing a list of extra interfaces, other than the interfaces specified by the pro...
virtual IDevice & device() const =0
Reference to the device this unit belongs to.
Network Address.
Definition: protocol.h:201
Network Message.
Definition: protocol.h:60
Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Definition: units.h:230
uint8_t id() const
Get the id number of this unit on the device.
Definition: units.h:218
Proxy class for interface objects.
Definition: units.h:154
IDevice & device() const
Reference to the device this unit belongs to.
Definition: units.h:107
virtual void periodic(uint32_t time)=0
Handle periodic processing.
Fail - Invalid Argument.
Unit(uint8_t id, IDevice &device)
Constructor.
Definition: units.h:212
HAN-FUN Protocol Packet.
Definition: protocol.h:298
HF::Attributes::List attributes(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: units.h:245
Interface/Service Attribute API.
Definition: attributes.h:44
This is the interface common to all Services.
Definition: core.h:126
This class represents the interface implemented by all HAN-FUN units.
Definition: units.h:48
Top level class representing a HAN-FUN profile.
Definition: profiles.h:240
std::vector< Common::Interface > interfaces() const
Return the list of optional interfaces implemented by this unit.
Definition: units.h:276
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
void send(const Protocol::Address &addr, Protocol::Message &message, Transport::Link *link)
Create and send a new packet with the given message to the given address.
This file contains the declaration of the API for a HAN-FUN device.
void periodic(uint32_t time)
Handle periodic processing.
Definition: units.h:120
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
Result
Commands result codes.
void notify(const HF::Attributes::IAttribute &old_value, const HF::Attributes::IAttribute &new_value) const
Notify the attribute reporting service that the given attribute has changed value.
Definition: units.h:264
uint8_t _id
Unit ID used to identify a given unit in a given HAN-FUN device.
Definition: units.h:128
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22