HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
batch_program_management.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_CORE_BATCH_PROGRAM_MANAGEMENT_H
18 #define HF_CORE_BATCH_PROGRAM_MANAGEMENT_H
19 
20 #include "hanfun/protocol.h"
21 #include "hanfun/core.h"
22 
23 #include <string>
24 #include <map>
25 #include <forward_list>
26 
27 namespace HF
28 {
29  namespace Core
30  {
31  // Forward declaration.
32  namespace BatchProgramManagement
33  {
34  class IServer;
35  }
36 
52  uint8_t uid);
53 
57  namespace BatchProgramManagement
58  {
66  typedef enum _CMD
68  {
74  __LAST_CMD__ = GET_PROGRAM_ACTIONS_CMD
75  } CMD;
76 
78  typedef enum _Attributes
79  {
82  __LAST_ATTR__ = NUMBER_OF_ENTRIES_ATTR
83  } Attributes;
84 
85  // =============================================================================
86  // Basic memory struct
87  // =============================================================================
88 
89  struct Action: public Protocol::Message
90  {
101  Action(uint8_t _uid, HF::Protocol::Message::Type _msg_type,
102  uint8_t _itf_type, uint16_t _itf_uid, uint8_t _itf_member,
103  Common::ByteArray &_payload)
104  {
105  reference = _uid;
106  type = _msg_type;
107  itf.role = _itf_type;
108  itf.id = _itf_uid;
109  itf.member = _itf_member;
110  payload = _payload;
111  }
112 
118  Action(void) = default;
119 
120  // =================================================================
121  // Serializable API
122  // =================================================================
123 
125  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
127  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
128 
129  };
130 
131  struct Entry
132  {
133  uint8_t pid;
134  std::string name;
135  std::vector<Action> actions;
136 
143  Entry(uint8_t _pid, std::string _name):
144  pid(_pid), name(_name)
145  {}
146 
154  Entry(uint8_t _pid, std::string _name, std::vector<Action> &_actions):
155  pid(_pid), name(_name), actions(_actions)
156  {}
157 
163  Entry(void) = default;
164 
165  static constexpr uint16_t START_PID = 0x00;
166  static constexpr uint16_t MAX_PID = 0xFE;
167  static constexpr uint16_t AVAILABLE_PID = 0xFF;
168 
169  // =================================================================
170  // Serializable API
171  // =================================================================
173  static constexpr uint16_t min_size = sizeof(uint8_t) // Program ID
174  + sizeof(uint8_t) // Name Length
175  + sizeof(uint8_t); // Number of actions
176 
178  uint16_t size() const;
179 
181  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
182 
184  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
185  };
186 
187  typedef Common::Pointer<Entry> EntryPtr;
188 
189  // =============================================================================
190  // Messages
191  // =============================================================================
192 
193  typedef Entry DefineProgram;
194 
195  struct DefineProgramResponse: public Protocol::Response
196  {
197  uint8_t pid;
198 
205  DefineProgramResponse(Common::Result _code = Common::Result::OK,
206  uint8_t _pid = 0):
207  Protocol::Response(_code), pid(_pid)
208  {}
209 
210  // =================================================================
211  // Serializable API
212  // =================================================================
213 
215  static constexpr uint16_t min_size = Protocol::Response::min_size; // Response Code
216 
218  uint16_t size() const;
219 
221  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
222 
224  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
225  };
226 
227  struct InvokeProgram
228  {
229  uint8_t pid;
230 
236  InvokeProgram(uint8_t _pid = 0):
237  pid(_pid)
238  {}
239 
240  // =================================================================
241  // Serializable API
242  // =================================================================
243 
245  static constexpr uint16_t min_size = sizeof(uint8_t); // Program ID
246 
248  uint16_t size() const;
249 
251  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
252 
254  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
255  };
256 
257  typedef DefineProgramResponse InvokeProgramResponse;
258 
259  typedef InvokeProgram DeleteProgram;
260  typedef InvokeProgramResponse DeleteProgramResponse;
261 
262  typedef Protocol::Response DeleteAllProgramsResponse;
263 
264  typedef InvokeProgram GetProgramActions;
265 
266  struct GetProgramActionsResponse: public Protocol::Response
267  {
268  Entry program;
269 
276  GetProgramActionsResponse(Common::Result _code,
277  Entry &_program):
278  Protocol::Response(_code), program(_program)
279  {}
280 
281  GetProgramActionsResponse(Common::Result _code = Common::Result::OK):
282  Protocol::Response(_code)
283  {}
284 
285  // =================================================================
286  // Serializable API
287  // =================================================================
288 
290  uint16_t size() const;
291 
293  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
294 
296  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
297  };
298 
299  // =============================================================================
300  // Attribute Helper classes
301  // =============================================================================
302 
308  {
309  static constexpr uint8_t ID = MAXIMUM_NUMBER_OF_ENTRIES_ATTR;
310  static constexpr bool WRITABLE = false;
311 
312  MaximumNumberOfEntries(uint8_t value = 0, HF::Interface *owner = nullptr):
313  Attribute<uint8_t>(HF::Interface::BATCH_PROGRAM_MANAGEMENT, ID, owner,
314  value, WRITABLE)
315  {}
316  };
317 
322  {
323  static constexpr uint8_t ID = NUMBER_OF_ENTRIES_ATTR;
324  static constexpr bool WRITABLE = false;
325 
326  NumberOfEntries(uint8_t value = 0, HF::Interface *owner = nullptr):
327  Attribute<uint8_t>(HF::Interface::BATCH_PROGRAM_MANAGEMENT, ID, owner,
328  value, WRITABLE)
329  {}
330  };
331 
343 
347  struct IEntries: public Common::IEntries<Entry>
348  {
350 
361  virtual Common::Result save(const uint8_t pid, const std::string &name,
362  std::vector<Action> &actions) = 0;
363 
365 
375  virtual Common::Result destroy(const uint8_t pid) = 0;
376 
380  virtual void clear(void) = 0;
381 
390  virtual EntryPtr find(const uint8_t pid) const = 0;
391 
400  virtual EntryPtr find(const std::string &name) const = 0;
401 
408  virtual uint8_t next_pid() const = 0;
409  };
410 
414  struct Entries: public IEntries
415  {
416  typedef std::map<uint8_t, Entry> Container;
417  typedef Container::iterator iterator;
418  typedef Container::const_iterator const_iterator;
419  typedef Container::value_type value_type;
420 
421  virtual ~Entries() {}
422 
423  uint16_t size() const;
424 
425  Common::Result save(const Entry &entry);
426 
427  Common::Result save(const uint8_t pid, const std::string &name,
428  std::vector<Action> &actions);
429 
430  Common::Result destroy(const uint8_t pid);
431 
438  Common::Result destroy(const Entry &entry);
439 
440  void clear(void);
441 
442  EntryPtr find(const uint8_t pid) const;
443 
444  EntryPtr find(const std::string &name) const;
445 
446  uint8_t next_pid() const;
447 
453  iterator begin()
454  {
455  return db.begin();
456  }
457 
463  iterator end()
464  {
465  return db.end();
466  }
467 
473  const_iterator begin() const
474  {
475  return db.cbegin();
476  }
477 
483  const_iterator end() const
484  {
485  return db.cend();
486  }
487 
488  protected:
489 
491  Container db;
492  };
493 
499  struct Base: public Service<HF::Interface::BATCH_PROGRAM_MANAGEMENT>
500  {
501  protected:
502 
505  };
506 
512  class IServer: public ServiceRole<BatchProgramManagement::Base,
513  HF::Interface::SERVER_ROLE>
514  {
515  protected:
516 
518 
519  public:
520 
522  IServer(Unit0 &unit): ServiceRole<BatchProgramManagement::Base,
524  _maximum_number_of_entries(std::numeric_limits<uint8_t>::max())
525  {}
526 
528  virtual ~IServer() {}
529 
530  // ======================================================================
531 
532  // ======================================================================
533  // Events
534  // ======================================================================
537 
550  virtual Common::Result define_program(const Protocol::Packet &packet,
551  DefineProgram &msg);
552 
563  virtual Common::Result invoke_program(const Protocol::Packet &packet,
564  InvokeProgram &msg);
575  virtual Common::Result delete_program(const Protocol::Packet &packet,
576  DeleteProgram &msg);
577 
586  virtual Common::Result delete_all_programs(const Protocol::Packet &packet);
587 
599  GetProgramActions &msg);
600 
602  // ======================================================================
603 
604 
605  // =============================================================================
606  // API
607  // =============================================================================
608 
615  virtual IEntries &entries() const = 0;
616 
625  EntryPtr entry(const uint8_t pid) const
626  {
627  return entries().find(pid);
628  }
629 
638  EntryPtr entry(const std::string &name) const
639  {
640  return entries().find(name);
641  }
642 
646  uint8_t next_pid() const
647  {
648  return entries().next_pid();
649  }
650 
651  // =============================================================================
652  // Get/Set API.
653  // =============================================================================
654 
660  uint8_t maximum_number_of_entries() const;
661 
667  void maximum_number_of_entries(uint8_t __value);
668 
674  uint8_t number_of_entries() const;
675 
676  // =============================================================================
677  // Attribute API.
678  // =============================================================================
679 
681 
682  HF::Attributes::UIDS attributes(uint8_t pack_id =
684 
685  protected:
686 
688  uint16_t offset);
689 
690  };
691 
696  template<typename _Entries = Entries>
697  class Server: public IServer
698  {
699  public:
700 
701  Server(Unit0 &unit): IServer(unit) {}
702 
703  virtual ~Server() {}
704 
705  _Entries &entries() const
706  {
707  return const_cast<_Entries &>(_entries);
708  }
709 
710  protected:
711 
712  _Entries _entries;
713  };
714 
717 
723  struct Client: public ServiceRole<BatchProgramManagement::Base,
724  HF::Interface::CLIENT_ROLE>
725  {
728 
729  virtual ~Client() {}
730 
731  // ======================================================================
732  // Commands
733  // ======================================================================
736 
749  Common::Result define_program(const Protocol::Address &addr, const uint8_t pid,
750  const std::string name, std::vector<Action> &actions)
751  {
752  DefineProgram request(pid, name, actions);
753 
754  return define_program(addr, request);
755  }
756 
767  Common::Result define_program(const Protocol::Address &addr, Entry &program);
768 
776  void invoke_program(const Protocol::Address &addr, uint8_t pid);
777 
785  void delete_program(const Protocol::Address &addr, uint8_t pid);
786 
793  void delete_all_programs(const Protocol::Address &addr);
794 
795 #ifdef HF_CORE_BATCH_PROGRAM_MANAGEMENT_GET_PROGRAM_ACTIONS_CMD
796 
803  void get_program_actions(const Protocol::Address &addr, uint8_t pid);
804 #endif
805 
807  // =============================================================================
808 
809  // ======================================================================
810  // Events
811  // ======================================================================
814 
821  virtual void defined(DefineProgramResponse &response) = 0;
822 
829  virtual void deleted(DeleteProgramResponse &response) = 0;
830 
837  virtual void deleted(DeleteAllProgramsResponse &response) = 0;
838 
845  virtual void invoked(InvokeProgramResponse &response) = 0;
846 
853  virtual void got_actions(GetProgramActionsResponse &response) = 0;
854 
856  // ======================================================================
857 
858  protected:
859 
860  uint16_t payload_size(Protocol::Message::Interface &itf) const;
861 
863  uint16_t offset);
864  };
865 
868  } // namespace BatchProgramManagement
869 
870  } // namespace Core
871 
872 } // namespace HF
873 
879 // =============================================================================
880 // Stream Helpers
881 // =============================================================================
882 
891 std::ostream &operator<<(std::ostream &stream,
893 
902 std::ostream &operator<<(std::ostream &stream,
906 #endif /* HF_CORE_BATCH_PROGRAM_MANAGEMENT__H */
virtual void got_actions(GetProgramActionsResponse &response)=0
This method is called when a response to a get info message is received.
const_iterator begin() const
Get a constant iterator to the start of the entries in this container.
bool request(Message::Type type, bool response=false)
Check if message type is a request.
virtual EntryPtr find(const uint8_t pid) const =0
Find the program with the given pid.
bool response(Message::Type type)
Check if message is a response.
virtual Common::Result invoke_program(const Protocol::Packet &packet, InvokeProgram &msg)
Callback that is called when a BatchProgramManagement::INVOKE_PROGRAM_CMD, is received.
Type
Message types.
Definition: protocol.h:65
This represent the special unit with ID/UID = 0.
Definition: core.h:67
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
virtual void deleted(DeleteProgramResponse &response)=0
This method is called when a response to a delete program is received.
List of attributes UIDs.
Definition: attributes.h:176
virtual IEntries & entries() const =0
Get a reference to the current object implementing the persistence API, for the device information...
_Entries & entries() const
Get a reference to the current object implementing the persistence API, for the device information...
Return all mandatory attributes for the interface.
Definition: attributes.h:842
Container db
Actual container for the entries.
This file contains the forward declarations of the core services and interfaces implementing classes...
STL namespace.
uint8_t _maximum_number_of_entries
Maximum Number Of Entries.
Common::Result destroy(const uint8_t pid)
Remove the program with the given pid ftom.
uint16_t id
Identifier of the interface.
void delete_all_programs(const Protocol::Address &addr)
Send a HAN-FUN message containing a BatchProgramManagement::DELETE_ALL_PROGRAMS_CMD, to the given network address.
Scheduling::Entry< Interval > Entry
Specific part for the Event Scheduler of the HF::Scheduling::Entry.
This file contains the definitions for the HAN-FUN protocol messages.
Helper template to declare a Batch Program server with custom entries backend.
uint8_t maximum_number_of_entries() const
Get the Maximum Number Of Entries for the Batch Program Management server.
virtual uint8_t next_pid() const =0
Return next available PID for the program.
Common::Result define_program(const Protocol::Address &addr, const uint8_t pid, const std::string name, std::vector< Action > &actions)
Send a HAN-FUN message containing a BatchProgramManagement::DEFINE_PROGRAM_CMD, to the given network ...
HF::Attributes::IAttribute * attribute(uint8_t uid)
Return a pointer to the interface attribute with the given uid.
Batch Program Management - Persistent Storage API.
virtual Common::Result save(const uint8_t pid, const std::string &name, std::vector< Action > &actions)=0
Store the given entry to persistent storage.
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...
EntryPtr entry(const std::string &name) const
Get the program entry given by name.
uint8_t next_pid() const
Return next available PID for the program.
static constexpr bool WRITABLE
Attribute Read/Write.
Batch Program Management Interfaces::Interface : Parent.
EntryPtr find(const uint8_t pid) const
Find the program with the given pid.
Batch Program Management Interfaces::Interface : Server side implementation.
virtual Common::Result destroy(const uint8_t pid)=0
Remove the program with the given pid ftom.
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Core::BatchProgramMan...
virtual void defined(DefineProgramResponse &response)=0
This method is called when a response to a create program is received.
static constexpr bool WRITABLE
Attribute Read/Write.
iterator end()
Get an iterator to the end of the entries in this container.
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.
uint16_t uid() const
This method returns the interface UID.
Definition: core.h:191
uint16_t role
Interface role : Server or Client.
HF::Interface const * owner() const
Definition: attributes.h:414
Helper class to handle the Maximum Number Of Entries attribute for the Batch Program Management inter...
Unit0 & unit() const
The device this unit is associated with.
Definition: core.h:142
This class represents a byte array.
void clear(void)
Erase all the DB entries.
void invoke_program(const Protocol::Address &addr, uint8_t pid)
Send a HAN-FUN message containing a BatchProgramManagement::INVOKE_PROGRAM_CMD, to the given network ...
Network Address.
Definition: protocol.h:201
uint8_t member
Interface destination member.
Definition: protocol.h:99
Interface Address.
Definition: protocol.h:93
Network Message.
Definition: protocol.h:60
const_iterator end() const
Get a constant iterator to the start of the entries in this container.
virtual void invoked(InvokeProgramResponse &response)=0
This method is called when a response to a invoke program message is received.
Batch Program Management Interfaces::Interface : Client side implementation.
HAN-FUN Protocol Packet.
Definition: protocol.h:298
iterator begin()
Get an iterator to the start of the entries in this container.
Interface/Service Attribute API.
Definition: attributes.h:44
EntryPtr entry(const uint8_t pid) const
Get the program entry given by pid.
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
Helper class to handle the Number Of Entries attribute for the Batch Program Management interface...
Parent class for the response messages.
Definition: protocol.h:368
Class template for all interfaces role implementations.
Definition: core.h:225
Simple raw pointer wrapper.
void delete_program(const Protocol::Address &addr, uint8_t pid)
Send a HAN-FUN message containing a BatchProgramManagement::DELETE_PROGRAM_CMD, to the given network ...
Helper template class to declare an attribute with the given T type.
Definition: attributes.h:349
virtual Common::Result delete_program(const Protocol::Packet &packet, DeleteProgram &msg)
Callback that is called when a BatchProgramManagement::DELETE_PROGRAM_CMD, is received.
Attribute(const uint16_t interface, const uint8_t uid, const HF::Interface *__owner, uint8_t data, bool writable=false)
Attribute template constructor.
Definition: attributes.h:360
static constexpr uint16_t min_size
Minimum number of bytes required by this message.
Definition: protocol.h:375
virtual Common::Result get_program_actions(const Protocol::Packet &packet, GetProgramActions &msg)
Callback that is called when a BatchProgramManagement::GET_PROGRAM_ACTIONS_CMD, is received...
uint8_t next_pid() const
Return next available PID for the program.
Common interface for all Interfaces.
Definition: interface.h:43
Default implementation of the persistence API.
uint16_t size() const
Return the number of entries in the container.
virtual Common::Result delete_all_programs(const Protocol::Packet &packet)
Callback that is called when a BatchProgramManagement::DELETE_ALL_PROGRAMS_CMD, is received...
virtual void clear(void)=0
Erase all the DB entries.
Class template for all core services implementations.
Definition: core.h:188
std::ostream & operator<<(std::ostream &stream, const HF::Core::BatchProgramManagement::CMD command)
Convert the given command into a string and write it to the given stream.
Batch Program Management interface UID.
Definition: interface.h:66
Result
Commands result codes.
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.
uint8_t number_of_entries() const
Get the Number Of Entries for the Batch Program Management server.
virtual Common::Result define_program(const Protocol::Packet &packet, DefineProgram &msg)
Callback that is called when a BatchProgramManagement::DEFINE_PROGRAM_CMD, is received.
Basic API for persistent storage implementations.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22