HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
bind_management.h
Go to the documentation of this file.
1 // =============================================================================
16 // =============================================================================
17 #ifndef HF_BIND_MANAGEMENT_H
18 #define HF_BIND_MANAGEMENT_H
19 
20 #include "hanfun/common.h"
21 #include "hanfun/protocol.h"
22 
23 #include "hanfun/core.h"
24 
25 #include "hanfun/interface.h"
26 
28 
29 // =============================================================================
30 // API
31 // =============================================================================
32 
33 namespace HF
34 {
35  // Forward declaration.
36 
37  namespace Devices
38  {
39  namespace Concentrator
40  {
41  struct IUnit0;
42 
43  } // namespace Concentrator
44 
45  } // namespace Devices
46 
47  // =============================================================================
48  // API
49  // =============================================================================
50 
51  namespace Core
52  {
53  // Forward declaration.
54  namespace BindManagement
55  {
56  struct IServer;
57  } // namespace BindManagement
58 
74  uint8_t uid);
75 
92  namespace BindManagement
93  {
102  typedef enum _CMD
104  {
105  ADD_BIND_CMD = 0x01,
110  __LAST_CMD__ = GET_ENTRIES_CMD
111  } CMD;
112 
114  typedef enum _Attributes
115  {
117  __LAST_ATTR__ = NUMBER_OF_ENTRIES_ATTR
118  } Attributes;
119 
123  struct Entry
124  {
126 
128 
130 
131  Entry()
132  {}
133 
141  source(_source), itf(_itf)
142  {}
143 
152  Protocol::Address _destination):
153  source(_source), destination(_destination), itf(_itf)
154  {}
155 
157  static constexpr uint16_t min_size = Protocol::Address::min_size // Source Address
158  + Protocol::Address::min_size // Destination Address
159  + Common::Interface::min_size; // Interface UID.
160 
162  uint16_t size() const;
163 
165  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
166 
168  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
169  };
170 
172 
176  struct Message: public Entry
177  {};
178 
179  // =============================================================================
180  // API
181  // =============================================================================
182 
186  struct IEntries: public Common::IEntries<Entry>
187  {
188  // =============================================================================
189  // API
190  // =============================================================================
191 
193 
207  virtual Common::Result destroy(uint16_t address, Protocol::Address::Type type =
209 
210  // =============================================================================
211  // Query API
212  // =============================================================================
213 
224  virtual EntryPtr find(const Protocol::Address &source, const Common::Interface &itf,
225  const Protocol::Address &destination) const = 0;
226 
237  virtual bool any_of(Protocol::Address const &source,
238  Common::Interface const &itf) const = 0;
239 
248  virtual void for_each(Protocol::Address const &source, Common::Interface const &itf,
249  std::function<void(const Entry &)> func) const = 0;
250  };
251 
263 
267  class Base: public Service<HF::Interface::BIND_MANAGEMENT>
268  {
269  protected:
270 
277  Service(unit)
278  {}
279  };
280 
284  class Client: public ServiceRole<Base, HF::Interface::CLIENT_ROLE>,
285  protected SessionManagement::Client<Entry>
286  {
288 
289  public:
290 
292 
299 
300  virtual ~Client() {}
301 
309  {
310  return *this;
311  }
312 
313  // ======================================================================
314  // Commands
315  // ======================================================================
318 
326  void add(const Protocol::Address &source, const Protocol::Address &destination,
327  const Common::Interface &itf);
328 
336  void remove(const Protocol::Address &source, const Protocol::Address &destination,
337  const Common::Interface &itf);
338 
339  // ======================================================================
340  // Session Management
341  // ======================================================================
342 
346  void start_session() const
347  {
350  }
351 
358  void get_entries(uint16_t offset, uint8_t count = 0) const
359  {
360  SessionMgr::get_entries<SERVER_ROLE, Interface::DEVICE_MANAGEMENT,
361  GET_ENTRIES_CMD>(offset, count);
362  }
363 
367  void end_session() const
368  {
370  END_SESSION_CMD>();
371  }
372 
374  // ======================================================================
375 
376  // ======================================================================
377  // Events
378  // ======================================================================
381 
388  virtual void response(const CMD cmd, const Protocol::Response &response);
389 
391  // ======================================================================
392 
393  using Service::send;
394 
395  void send(const Protocol::Address &addr, Protocol::Message &message)
396  {
397  Service::send(addr, message);
398  }
399 
400  protected:
401 
402  using ServiceRole::payload_size;
403 
404  uint16_t payload_size(Protocol::Message::Interface &itf) const;
405 
406  Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload,
407  uint16_t offset);
408  };
409 
413  struct IServer: public ServiceRole<Base, HF::Interface::SERVER_ROLE>
414  {
415  virtual ~IServer() {}
416 
417  // =============================================================================
418  // API
419  // =============================================================================
420 
430 
431  // ======================================================================
432  // Commands
433  // ======================================================================
436 
446  virtual Common::Result add(const Protocol::Address &source,
447  const Protocol::Address &destination,
448  const Common::Interface &itf) = 0;
449 
459  virtual Common::Result remove(const Protocol::Address &source,
460  const Protocol::Address &destination,
461  const Common::Interface &itf) = 0;
462 
464  // ======================================================================
465 
472  virtual IEntries &entries() const = 0;
473 
480  virtual SessionManagement::IServer &sessions() = 0;
481 
482  protected:
483 
490  ServiceRole<Base, HF::Interface::SERVER_ROLE>(unit)
491  {}
492  };
493 
497  struct AbstractServer: public IServer
498  {
499  virtual ~AbstractServer() {}
500 
501  // =============================================================================
502  // API.
503  // =============================================================================
504 
505  Common::Result add(const Protocol::Address &source,
506  const Protocol::Address &destination,
507  const Common::Interface &itf);
508 
509  Common::Result remove(const Protocol::Address &source,
510  const Protocol::Address &destination,
511  const Common::Interface &itf);
512 
513  // =============================================================================
514  // Interface Attribute API.
515  // =============================================================================
516 
517  HF::Attributes::IAttribute *attribute(uint8_t uid)
518  {
519  return Core::create_attribute(this, uid);
520  }
521 
522  HF::Attributes::UIDS attributes(uint8_t pack_id =
524  {
525  UNUSED(pack_id);
527  }
528 
529  protected:
530 
537  {}
538 
539  Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload,
540  uint16_t offset);
541  };
542 
546  struct Entries: public IEntries
547  {
548  typedef std::set<Entry> Container;
549  typedef Container::iterator iterator;
550  typedef Container::const_iterator const_iterator;
551  typedef Container::value_type value_type;
552 
553  uint16_t size() const;
554 
555  Common::Result save(const Entry &entry);
556 
563  Common::Result destroy(const Entry &entry);
564 
565  Common::Result destroy(uint16_t address,
567 
568  // =============================================================================
569  // Query API
570  // =============================================================================
571 
572  EntryPtr find(const Protocol::Address &source, const Common::Interface &itf,
573  const Protocol::Address &destination) const;
574 
575  bool any_of(Protocol::Address const &source, Common::Interface const &itf) const;
576 
577  void for_each(Protocol::Address const &source, Common::Interface const &itf,
578  std::function<void(const Entry &)> func) const;
579 
580  iterator begin()
581  {
582  return db.begin();
583  }
584 
585  iterator end()
586  {
587  return db.end();
588  }
589 
590  const_iterator begin() const
591  {
592  return db.begin();
593  }
594 
595  iterator end() const
596  {
597  return db.end();
598  }
599 
609  std::pair<iterator, iterator> find(Protocol::Address const &source,
610  Common::Interface const &itf) const;
611 
622  template<typename Func>
624  {
626 
627  for (iterator itr = db.begin(); itr != db.end();)
628  {
629  if (func(*itr))
630  {
631  auto old = itr++;
632  destroy(*old);
633  result = Common::Result::OK;
634  }
635  else
636  {
637  ++itr;
638  }
639  }
640 
641  return result;
642  }
643 
644  protected:
645 
647  Container db;
648  };
649 
654  template<typename _Entries = BindManagement::Entries>
655  struct Server: public AbstractServer, public SessionManagement::Server<_Entries>
656  {
658  typedef typename SessionMgr::Container Container;
659 
666 
667  virtual ~Server()
668  {}
669 
670  Container &entries() const
671  {
672  return SessionMgr::entries();
673  }
674 
676  {
677  return *this;
678  }
679 
680  using AbstractServer::send;
681 
682  void send(const Protocol::Address &addr, Protocol::Message &message)
683  {
684  AbstractServer::send(addr, message);
685  }
686 
687  protected:
688 
689  _Entries _entries;
690 
691  uint16_t payload_size(Protocol::Message::Interface &itf) const
692  {
693  switch (itf.member)
694  {
695  case START_SESSION_CMD:
697 
698  case GET_ENTRIES_CMD:
700 
701  case END_SESSION_CMD:
703 
704  default:
705  return AbstractService::payload_size(itf);
706  }
707  }
708 
709  Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload,
710  uint16_t offset)
711  {
712  switch (packet.message.itf.member)
713  {
714  case START_SESSION_CMD:
716  payload, offset);
717 
718  case GET_ENTRIES_CMD:
720  payload, offset);
721 
722  case END_SESSION_CMD:
724  payload, offset);
725 
726  default:
727  return AbstractServer::handle_command(packet, payload, offset);
728  }
729  }
730 
731  using SessionMgr::entries;
732  };
733 
734  typedef Server<> DefaultServer;
735 
736  // =============================================================================
737  // Operators
738  // =============================================================================
739 
740  inline bool operator==(const Entry &lhs, const Entry &rhs)
741  {
742  return lhs.source == rhs.source && lhs.destination == rhs.destination &&
743  lhs.itf == rhs.itf;
744  }
745 
746  inline bool operator!=(const Entry &lhs, const Entry &rhs)
747  {
748  return !(lhs == rhs);
749  }
750 
751  inline bool operator<(const Entry &lhs, const Entry &rhs)
752  {
753  return lhs.source < rhs.source || (lhs.source == rhs.source
754  && (lhs.itf < rhs.itf ||
755  (lhs.itf == rhs.itf &&
756  lhs.destination < rhs.destination)));
757  }
758 
759  } // namespace BindManagement
760 
763  } // namespace Core
764 
765 } // namespace HF
766 
772 // =============================================================================
773 // Stream Helpers
774 // =============================================================================
775 
784 std::ostream &operator<<(std::ostream &stream, const HF::Core::BindManagement::CMD command);
785 
794 std::ostream &operator<<(std::ostream &stream,
795  const HF::Core::BindManagement::Attributes attribute);
796 
799 #endif /* HF_BIND_MANAGEMENT_H */
Container & entries() const
Reference to the persistent storage implementation.
HF::Devices::Concentrator::IUnit0 & unit0() const
Return a reference to the unit that this service belongs to.
SessionMgr & sessions()
Reference to the session management API.
Destination address is for single device.
Definition: protocol.h:213
virtual Common::Result add(const Protocol::Address &source, const Protocol::Address &destination, const Common::Interface &itf)=0
Create a new bind entry in the database.
This represent the special unit with ID/UID = 0.
Definition: core.h:67
Base(HF::Core::Unit0 &unit)
Constructor.
Bind Management interface : Client side.
Helper template to inject session management functionality into services requiring it - Server side...
SessionMgr & session()
Reference to the session management API.
Message message
Packet message payload;.
Definition: protocol.h:306
Common::Result destroy(const Entry &entry)
Destroy the given entry in the persistent storage.
Helper template to inject session management functionality into services requiring it - Client side...
List of attributes UIDs.
Definition: attributes.h:176
_Entries _entries
Bind entries database.
void end_session() const
End read entries session.
Default implementation of the bind entries container.
Return all mandatory attributes for the interface.
Definition: attributes.h:842
This file contains the common defines for the HAN-FUN library.
Units::IUnit * unit(uint8_t id) const
Return pointer to the unit with the given id.
This file contains the forward declarations of the core services and interfaces implementing classes...
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: protocol.h:230
virtual IEntries & entries() const =0
Reference to the persistent storage implementation.
Scheduling::Entry< Interval > Entry
Specific part for the Event Scheduler of the HF::Scheduling::Entry.
Container & entries() const
Return the container for the service entries.
void add(const Protocol::Address &source, const Protocol::Address &destination, const Common::Interface &itf)
Send a ADD_BIND command.
void send(Protocol::Packet &packet)
Send given packet into the HAN-FUN network.
This file contains the definitions for the HAN-FUN protocol messages.
Parent class for the Bind Management interface implementation.
void send(const Protocol::Address &addr, Protocol::Message &message)
Send message msg to the network address given by addr.
Protocol::Address source
Source Address.
EntryPtr find(const Protocol::Address &source, const Common::Interface &itf, const Protocol::Address &destination) const
Return the Bind entry for the given parameters.
Parent class for bind management - server role.
virtual void response(const CMD cmd, const Protocol::Response &response)
This method is called when the response to a command is received.
void for_each(Protocol::Address const &source, Common::Interface const &itf, std::function< void(const Entry &)> func) const
Call the given function for all the entries with given source address and for the given interface ifc...
Common::Interface itf
Destination Interface.
Bind Management persistent storage API.
This file contains the definitions common to all interfaces.
End the session for device.
Device Management interface UID.
Definition: interface.h:60
Unit 0 interface API for HAN-FUN Concentrators.
Definition: devices.h:607
IServer(Unit0 &unit)
Constructor.
Unit0 & unit() const
The device this unit is associated with.
Definition: core.h:142
Helper class used to implement custom functionality to the bind management server side...
Template for HAN-FUN concentrator devices.
Definition: devices.h:906
virtual void for_each(Protocol::Address const &source, Common::Interface const &itf, std::function< void(const Entry &)> func) const =0
Call the given function for all the entries with given source address and for the given interface ifc...
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.
This file contains the definitions for the session management functionality.
Entry(Protocol::Address _source, Common::Interface _itf)
Constructor.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Core::BindManagement:...
Network Address.
Definition: protocol.h:201
uint8_t member
Interface destination member.
Definition: protocol.h:99
Container db
Container for the bind entries.
std::ostream & operator<<(std::ostream &stream, const HF::Core::BindManagement::CMD command)
Convert the given command into a string and write it to the given stream.
Interface Address.
Definition: protocol.h:93
Network Message.
Definition: protocol.h:60
Entry(Protocol::Address _source, Common::Interface _itf, Protocol::Address _destination)
Constructor.
Start Session Read Registration Info.
virtual uint16_t payload_size(Protocol::Message &message) const
Return the minimal payload size that should be present for the given message.
void start_session() const
Start read entries session.
Session Management API : Server side.
virtual bool any_of(Protocol::Address const &source, Common::Interface const &itf) const =0
Check if entries for the device with given source address and for the given interface ifc exist...
Fail - Invalid Argument.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
HAN-FUN Protocol Packet.
Definition: protocol.h:298
uint16_t size() const
Number bytes needed to serialize the message.
Bind Management interface : Server side API.
uint16_t pack(Common::ByteArray &array, uint16_t offset=0) const
Write the object on to a ByteArray so it can be sent over the network.
Interface itf
Interface Address.
Definition: protocol.h:129
bool any_of(Protocol::Address const &source, Common::Interface const &itf) const
Check if entries for the device with given source address and for the given interface ifc exist...
Common::Result destroy(Func func)
Destroy the entries in the persistent storage, for which the given predicate function returns true...
virtual SessionManagement::IServer & sessions()=0
Reference to the session management API.
Interface/Service Attribute API.
Definition: attributes.h:44
Client(HF::Core::Unit0 &unit)
Constructor.
virtual EntryPtr find(const Protocol::Address &source, const Common::Interface &itf, const Protocol::Address &destination) const =0
Return the Bind entry for the given parameters.
This represents a bind entry data structure.
Parent class for the response messages.
Definition: protocol.h:368
Common::Result handle_command(CMD cmd, Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset=0)
Handle command request/response messages.
Class template for all interfaces role implementations.
Definition: core.h:225
Simple raw pointer wrapper.
Type
HAN-FUN Network Destination Address Types.
Definition: protocol.h:211
#define UNUSED(x)
Helper macro to remove warning about unused function/method argument.
void request() const
Helper method to create a session management request.
AbstractServer(Unit0 &unit)
Constructor.
Server(Unit0 &unit)
Constructor.
virtual Common::Result destroy(uint16_t address, Protocol::Address::Type type=Protocol::Address::DEVICE)=0
Destroy the entries in the persistent storage, that refer to the the given address.
Common interface for all Interfaces.
Definition: interface.h:43
void get_entries(uint16_t offset, uint8_t count=0) const
Read entries.
End Session Read Registration Info.
Class template for all core services implementations.
Definition: core.h:188
uint16_t payload_size(CMD cmd) const
Get the minimum number of bytes necessary to pack/unpack a message of the given command.
Result
Commands result codes.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Common::Result add(const Protocol::Address &source, const Protocol::Address &destination, const Common::Interface &itf)
Create a new bind entry in the database.
Protocol::Address destination
Destination Address.
uint16_t size() const
Return the number of entries in the container.
HF::Attributes::IAttribute * create_attribute(HF::Core::AttributeReporting::IServer *server, uint8_t uid)
Create an attribute object that can hold the attribute with the given uid.
Add/Remove Bind message.
Start a new session for a device.
Basic API for persistent storage implementations.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22