HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
core.h
Go to the documentation of this file.
1 // =============================================================================
16 // =============================================================================
17 #ifndef HF_CORE_H
18 #define HF_CORE_H
19 
20 #include "hanfun/common.h"
21 #include "hanfun/interface.h"
22 #include "hanfun/protocol.h"
23 
24 #include "hanfun/units.h"
25 
26 // =============================================================================
27 // API
28 // =============================================================================
29 
30 namespace HF
31 {
48  namespace Core
49  {
50  // Forward declaration.
51  namespace DeviceInformation
52  {
53  struct Server;
54  }
55 
67  struct Unit0: public Units::AbstractUnit
68  {
69  virtual ~Unit0() {}
70 
77  {}
78 
79  uint8_t id() const
80  {
81  return 0;
82  }
83 
84  uint16_t uid() const
85  {
86  return 0;
87  }
88 
90 
92  const HF::Attributes::UIDS &uids) const
93  {
94  UNUSED(itf);
95  UNUSED(pack_id);
96  UNUSED(uids);
97  return HF::Attributes::List();
98  }
99 
100  protected:
101 
102  static void remove_mandatory(std::vector<Common::Interface> &itfs)
103  {
104  static std::array<uint16_t, 3> const mandatory =
105  {
106  {
110  }
111  };
112 
113  itfs.erase(std::remove_if(itfs.begin(), itfs.end(), [](const Common::Interface &itf)
114  {
115  return std::any_of(mandatory.begin(), mandatory.end(), [&itf](uint16_t uid)
116  {
117  return itf.id == uid;
118  });
119  }), itfs.end());
120  }
121  };
122 
126  struct IService: virtual public HF::Interface
127  {
128  virtual ~IService() {}
129 
131  virtual Unit0 &unit() const = 0;
132  };
133 
137  struct AbstractService: virtual public IService,
138  virtual public HF::Interfaces::AbstractInterface
139  {
140  virtual ~AbstractService() {}
141 
142  Unit0 &unit() const
143  {
144  return _unit;
145  }
146 
148  {
149  return AbstractInterface::attribute(uid);
150  }
151 
153  {
154  return AbstractInterface::attributes(pack_id);
155  }
156 
157  protected:
158 
161 
168  {}
169 
171  void send(const Protocol::Address &addr, Protocol::Message &message)
172  {
173  unit().send(addr, message, nullptr);
174  }
175 
177  void send(const Protocol::Address &addr, Protocol::Message &message,
178  Transport::Link *link)
179  {
180  unit().send(addr, message, link);
181  }
182  };
183 
187  template<Interface::UID _uid, typename Parent = AbstractService>
188  struct Service: public Parent
189  {
191  uint16_t uid() const
192  {
193  return _uid;
194  }
195 
196  protected:
197 
203  Service(Unit0 &unit):
204  Parent(unit)
205  {}
206 
215  bool check_uid(uint16_t uid) const
216  {
217  return Service::uid() == uid;
218  }
219  };
220 
224  template<class Parent, Interface::Role _role>
225  struct ServiceRole: public Parent
226  {
227  static_assert(std::is_base_of<HF::Core::AbstractService, Parent>::value,
228  "Parent must be of type HF::Core::AbstractService");
229 
232  {
233  return _role;
234  }
235 
236  protected:
237 
244  Parent(unit)
245  {}
246  };
247 
250  } // namespace Core
251 
263  template<typename Base, typename... ITF>
264  class Unit0: public Base, public Units::InterfacesWrapper<Base, ITF...>
265  {
266  static_assert(std::is_base_of<HF::Core::Unit0, Base>::value,
267  "Base must be of type HF::Core::Unit0");
268 
269  typedef typename Units::InterfacesWrapper<Base, ITF...> InterfacesWrapper;
270  typedef typename std::tuple<ITF...> interfaces_t;
271 
272  public:
273 
275  enum Inferface: uint8_t
276  {
277  DEV_INFO = 0,
280 #if HF_TIME_SUPPORT
281  TIME,
282 #endif
283 #if HF_BATCH_PROGRAM_SUPPORT
284  BATCH_PROGRAM,
285 #endif
286 #if HF_EVENT_SCHEDULING_SUPPORT
287  EVENT_SCH,
288 #endif
289 #if HF_WEEKLY_SCHEDULING_SUPPORT
290  WEEKLY_SCH,
291 #endif
292 #if HF_GROUP_SUPPORT
293  GROUP_TABLE,
294  GROUP_MGT,
295 #endif
297  };
298 
299  typedef typename std::tuple_element<DEV_INFO, interfaces_t>::type DeviceInfo;
300 
301  typedef typename std::tuple_element<DEV_MGT, interfaces_t>::type DeviceMgt;
302 
303  typedef typename std::tuple_element<ATTR_RPT, interfaces_t>::type AttrReporting;
304 
305  static_assert(std::is_base_of<HF::Core::DeviceInformation::Server, DeviceInfo>::value,
306  "DeviceInfo must be of type HF::Core::DeviceInformation::Server");
307 
308  static_assert(std::is_base_of<HF::Core::AttributeReporting::IServer, AttrReporting>::value,
309  "AttrReporting must be of type HF::Core::AttributeReporting::Server");
310 
311 #if HF_GROUP_SUPPORT
312  typedef typename std::tuple_element<GROUP_TABLE, interfaces_t>::type GroupTable;
313 
314  static_assert(std::is_base_of<HF::Core::GroupTable::IServer, GroupTable>::value,
315  "GroupTable must be of type HF::Core::GroupTable::IServer");
316 #endif
317 
318 #if HF_TIME_SUPPORT
319  typedef typename std::tuple_element<TIME, interfaces_t>::type Time;
320 
321  static_assert(std::is_base_of<HF::Core::Time::Server, Time>::value,
322  "Time must be of type HF::Core::Time::Server");
323 #endif
324 
325 #if HF_BATCH_PROGRAM_SUPPORT
326  typedef typename std::tuple_element<BATCH_PROGRAM, interfaces_t>::type BatchProgram;
327 
328  static_assert(std::is_base_of<HF::Core::BatchProgramManagement::IServer, BatchProgram>::value,
329  "BatchProgram must be of type HF::Core::BatchProgramManagement::IServer");
330 #endif
331 
332 #if HF_EVENT_SCHEDULING_SUPPORT
333  typedef typename std::tuple_element<EVENT_SCH, interfaces_t>::type EventScheduling;
334 
335  static_assert(std::is_base_of<HF::Core::Scheduling::Event::IServer, EventScheduling>::value,
336  "EventSch must be of type HF::Core::Scheduling::Event::IServer");
337 #endif
338 
339 #if HF_WEEKLY_SCHEDULING_SUPPORT
340  typedef typename std::tuple_element<WEEKLY_SCH, interfaces_t>::type WeeklyScheduling;
341 
342  static_assert(std::is_base_of<HF::Core::Scheduling::Weekly::IServer, WeeklyScheduling>::value,
343  "WeeklyScheduling must be of type HF::Core::Scheduling::Weekly::IServer");
344 #endif
345 
351  Unit0(HF::IDevice &device):
352  Base(device), InterfacesWrapper(*(static_cast<Base *>(this)))
353  {}
354 
355  // =============================================================================
356 
362  DeviceInfo *device_info() const
363  {
364  return get<DeviceInfo, DEV_INFO>();
365  }
366 
372  DeviceInfo *device_info()
373  {
374  return get<DeviceInfo, DEV_INFO>();
375  }
376 
382  DeviceMgt *device_management() const
383  {
384  return get<DeviceMgt, DEV_MGT>();
385  }
386 
392  DeviceMgt *device_management()
393  {
394  return get<DeviceMgt, DEV_MGT>();
395  }
396 
402  AttrReporting *attribute_reporting() const
403  {
404  return get<AttrReporting, ATTR_RPT>();
405  }
406 
412  AttrReporting *attribute_reporting()
413  {
414  return get<AttrReporting, ATTR_RPT>();
415  }
416 
417 #if HF_GROUP_SUPPORT
418 
423  GroupTable *group_table()
424  {
425  return get<GroupTable, GROUP_TABLE>();
426  }
427 
433  GroupTable *group_table() const
434  {
435  return get<GroupTable, GROUP_TABLE>();
436  }
437 #endif
438 
439 #if HF_TIME_SUPPORT
440 
445  Time *time()
446  {
447  return get<Time, TIME>();
448  }
449 
455  Time *time() const
456  {
457  return get<Time, TIME>();
458  }
459 #endif
460 
461 #if HF_BATCH_PROGRAM_SUPPORT
462 
467  BatchProgram *batch_program()
468  {
469  return get<BatchProgram, BATCH_PROGRAM>();
470  }
471 
477  BatchProgram *batch_program() const
478  {
479  return get<BatchProgram, BATCH_PROGRAM>();
480  }
481 #endif
482 
483 #if HF_EVENT_SCHEDULING_SUPPORT
484 
489  EventScheduling *event_scheduling()
490  {
491  return get<EventScheduling, EVENT_SCH>();
492  }
493 
499  EventScheduling *event_scheduling() const
500  {
501  return get<EventScheduling, EVENT_SCH>();
502  }
503 #endif
504 
505 #if HF_WEEKLY_SCHEDULING_SUPPORT
506 
511  WeeklyScheduling *weekly_scheduling()
512  {
513  return get<WeeklyScheduling, WEEKLY_SCH>();
514  }
515 
521  WeeklyScheduling *weekly_scheduling() const
522  {
523  return get<WeeklyScheduling, WEEKLY_SCH>();
524  }
525 #endif
526 
527  // =============================================================================
528  // IUnit API
529  // =============================================================================
530 
533  uint16_t offset)
534  {
535 #if HF_GROUP_SUPPORT
537  InterfacesWrapper::for_each([&result, &packet, &payload, offset](HF::Interface &itf)
538  {
539  if (result != Common::Result::OK) // Message already handled, skip.
540  {
541  result = itf.handle(packet, payload, offset);
542  }
543  });
544 
545  return result;
546 
547 #else
548  return InterfacesWrapper::handle(packet, payload, offset);
549 
550 #endif
551  }
552 
555  const HF::Attributes::UIDS &uids) const
556  {
557  HF::Attributes::List result;
558  InterfacesWrapper::attributes(result, itf, pack_id, uids);
559  return result;
560  }
561 
562  void periodic(uint32_t time)
563  {
564  Base::periodic(time);
565  InterfacesWrapper::periodic(time);
566  }
567 
568  std::vector<Common::Interface> interfaces() const
569  {
570  auto result = InterfacesWrapper::interfaces();
571 
572  Base::remove_mandatory(result);
573  return result;
574  }
575 
576  // =============================================================================
577 
578  protected:
579 
580  template<typename T, uint8_t N>
581  T *get() const
582  {
583  return const_cast<T *>(InterfacesWrapper::template get<N>());
584  }
585  };
586 
589 } // namespace HF
590 
591 #endif /* HF_CORE_H */
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: core.h:554
This class represents a HAN-FUN Concentrator.
Definition: base.h:302
Top-level parent class for all implemented interfaces.
Definition: interface.h:197
ServiceRole(Unit0 &unit)
Constructor.
Definition: core.h:243
This represent the special unit with ID/UID = 0.
Definition: core.h:67
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
Definition: core.h:147
This is the parent class for all HAN-FUN units.
Definition: units.h:95
DeviceInfo * device_info() const
Device Information service.
Definition: core.h:362
Service(Unit0 &unit)
Constructor.
Definition: core.h:203
virtual Unit0 & unit() const =0
The device this unit is associated with.
Inferface
Core Services indexs.
Definition: core.h:275
AbstractService(Unit0 &unit)
Constructor.
Definition: core.h:167
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.
Definition: core.h:177
Helper class to handle optional interfaces in units.
Definition: units.h:182
List of attributes UIDs.
Definition: attributes.h:176
Attribute Reporting interface UID.
Definition: interface.h:65
Return all mandatory attributes for the interface.
Definition: attributes.h:842
This file contains the common defines for the HAN-FUN library.
AttrReporting * attribute_reporting() const
Attribute Reporting service.
Definition: core.h:402
Helper template class used to define custom Unit 0 for devices.
Definition: core.h:264
This class has the same behavior has a list, however the list element access methods where overwritte...
Definition: attributes.h:752
This file contains the definitions for the HAN-FUN protocol messages.
AbstractUnit(IDevice &device)
Constructor.
Definition: units.h:135
This is the parent class for all services implementations.
Definition: core.h:137
uint8_t id() const
Get the id number of this unit on the device.
Definition: core.h:79
std::vector< Common::Interface > interfaces() const
Return the list of interfaces present in the wrapper.
Definition: interface.h:495
Attribute Reporting service index.
Definition: core.h:279
void for_each(std::function< void(HF::Interface &)> func) const
Call the given function for all the interfaces.
Definition: interface.h:540
This class represents the interface common to all HAN-FUN devices.
Definition: device.h:99
uint16_t uid() const
Return this profile HAN-FUN UID.
Definition: core.h:84
Role
Interface roles.
Definition: interface.h:48
This file contains the definitions common to all interfaces.
DeviceInfo * device_info()
Device Information service.
Definition: core.h:372
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: core.h:152
DeviceMgt * device_management() const
Device Management service.
Definition: core.h:382
Common::Result handle(HF::Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Definition: core.h:532
Device Management interface UID.
Definition: interface.h:60
void periodic(uint32_t time)
Handle periodic processing.
Fail - Unknown reason.
AttrReporting * attribute_reporting()
Attribute Reporting service.
Definition: core.h:412
Unit0(HF::IDevice &device)
Constructor.
Definition: core.h:351
uint16_t uid() const
This method returns the interface UID.
Definition: core.h:191
Unit0 & unit() const
The device this unit is associated with.
Definition: core.h:142
Unit0 & _unit
Reference to the unit the service belongs to.
Definition: core.h:160
This class represents a byte array.
void send(const Protocol::Address &addr, Protocol::Message &message)
Send message msg to the network address given by addr.
Definition: core.h:171
Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Definition: interface.h:463
Device Information service index.
Definition: core.h:277
Device Management service index.
Definition: core.h:278
Network Address.
Definition: protocol.h:201
Network Message.
Definition: protocol.h:60
This file contains the definitions for the HAN-FUN unit implementation.
bool check_uid(uint16_t uid) const
Check if the given uid value matches the service&#39;s UID value.
Definition: core.h:215
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: core.h:91
IDevice & device() const
Reference to the device this unit belongs to.
Definition: units.h:107
Interface::Role role() const
Return the Interface::Role this interface implements.
Definition: core.h:231
HAN-FUN Protocol Packet.
Definition: protocol.h:298
Interface/Service Attribute API.
Definition: attributes.h:44
DeviceMgt * device_management()
Device Management service.
Definition: core.h:392
This is the interface common to all Services.
Definition: core.h:126
Class template for all interfaces role implementations.
Definition: core.h:225
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
void periodic(uint32_t time)
Handle periodic processing.
Definition: core.h:562
Time
This represents the type of time that is associated with a time measurement.
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.
virtual Common::Result handle(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)=0
Handle incoming messages from the network.
Device information interface UID.
Definition: interface.h:64
Common interface for all Interfaces.
Definition: interface.h:43
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
Class template for all core services implementations.
Definition: core.h:188
virtual uint16_t uid() const =0
This method returns the interface UID.
Result
Commands result codes.
Protocol::Message * mandatory()
Create a message that can be used to retrieve the mandatory attributes on a remote device...
Bind Management service index.
Definition: core.h:296
Unit0(IDevice &device)
Constructor.
Definition: core.h:76
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22