HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
protocol.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 #ifndef HF_PROTOCOL_H
17 #define HF_PROTOCOL_H
18 
19 #include "hanfun/common.h"
20 
21 // =============================================================================
22 // API
23 // =============================================================================
24 
25 namespace HF
26 {
27  // Forward declaration.
28  namespace Transport
29  {
30  struct Link;
31  }
32 
36  namespace Protocol
37  {
44  constexpr uint16_t BROADCAST_ADDR = 0x7FFF;
46 
48  constexpr uint8_t BROADCAST_UNIT = 0xFF;
49 
51  constexpr uint16_t MAX_PAYLOAD = 0x01FF;
52 
53  // =============================================================================
54  // Classes
55  // =============================================================================
56 
60  struct Message
61  {
65  typedef enum _Type
66  {
67  COMMAND_REQ = 0x01,
69  COMMAND_RES = 0x03,
70 
71  GET_ATTR_REQ = 0x04,
72  GET_ATTR_RES = 0x05,
73 
74  SET_ATTR_REQ = 0x06,
76  SET_ATTR_RES = 0x08,
77 
80 
84 
88  } Type;
89 
94  {
96  static const uint16_t min_size = Common::Interface::min_size // Interface UID.
97  + sizeof(uint8_t); // Interface Member.
98 
99  uint8_t member;
100 
108  Interface(uint16_t uid = 0, uint8_t role = 0, uint8_t member = 0):
109  Common::Interface(uid, role), member(member)
110  {}
111 
113  uint16_t size() const;
114 
116  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
117 
119  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
120  };
121 
122  // =============================================================================
123  // API
124  // =============================================================================
125 
126  uint8_t reference;
128 
130 
135 
137  uint16_t length;
138 
145  Message(uint16_t size = 0, Type _type = COMMAND_REQ):
146  reference(0), type(_type), payload(Common::ByteArray(size)), length(0)
147  {
148  assert(size <= MAX_PAYLOAD);
149  }
150 
159  Message(const Message &parent, uint16_t size);
160 
162  static constexpr uint16_t min_size = sizeof(uint8_t) // Application Reference.
163  + sizeof(uint8_t) // Message Type.
164  + Interface::min_size // Interface UID + Member.
165  + sizeof(uint16_t); // Payload Length Value.
166 
168  uint16_t size() const;
169 
171  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
172 
179  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
180 
187  bool isCommand() const;
188 
195  bool isCommandResponse() const;
196  };
197 
201  struct Address
202  {
203  uint16_t mod : 1;
204  uint16_t device : 15;
205 
206  uint8_t unit;
207 
211  typedef enum _Type
212  {
213  DEVICE = 0,
214  GROUP = 1,
215  } Type;
216 
224  Address(uint16_t _dev = BROADCAST_ADDR, uint8_t _unit = BROADCAST_UNIT,
225  Type _mod = DEVICE):
226  mod(_mod), device(_dev), unit(_unit)
227  {}
228 
230  static constexpr uint16_t min_size = sizeof(uint16_t) // Device Address + Flag.
231  + sizeof(uint8_t); // Unit Address.
232 
234  uint16_t size() const;
235 
237  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
238 
240  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
241 
250  {
251  return device == BROADCAST_ADDR && unit == BROADCAST_UNIT;
252  }
253 
263  bool is_local(uint16_t address)
264  {
265  return this->device == address;
266  }
267 
280  int compare(const Address &other) const
281  {
282  auto serialize = [this](const Address &a)
283  {
284  return (((a.mod & 0x01) << 15) | (a.device & BROADCAST_ADDR)) |
285  a.unit;
286  };
287 
288  uint32_t t = serialize(*this);
289  uint32_t o = serialize(other);
290 
291  return t - o;
292  }
293  };
294 
298  struct Packet
299  {
302 
307 
310 
311  Packet(): link(nullptr) {}
312 
319 
327  Packet(Address &dst_addr, Message &message, uint8_t unit = BROADCAST_UNIT):
328  destination(dst_addr), message(message), link(nullptr)
329  {
332  source.unit = unit;
333  }
334 
342  Packet(Address &src_addr, Address &dst_addr, Message &message):
343  source(src_addr), destination(dst_addr), message(message), link(nullptr)
344  {}
345 
347  static constexpr uint16_t header_min_size = Address::min_size // Source Address.
348  + Address::min_size // Destination Address.
349  + sizeof(uint16_t); // Transport Layer header.
350 
352  static constexpr uint16_t min_size = header_min_size // Network header size.
353  + Message::min_size; // Message size.
354 
356  uint16_t size() const;
357 
359  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
360 
362  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
363  };
364 
368  struct Response
369  {
370  // =============================================================================
371  // API
372  // =============================================================================
373 
375  constexpr static uint16_t min_size = sizeof(uint8_t);
376 
377  Common::Result code;
378 
379  Response(Common::Result code = Common::Result::OK): code(code) {}
380 
381  template<typename T>
382  Response(T code = static_cast<T>(Common::Result::OK)):
383  code(static_cast<Common::Result>(code)) {}
384 
386  uint16_t size() const;
387 
389  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
390 
392  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
393  };
394 
397  // =============================================================================
398  // Filter classes
399  // =============================================================================
400 
407  namespace Filters
408  {
422  {
423  protected:
424 
426  struct Entry
427  {
428  uint16_t address;
431 
432  Entry(uint16_t _address, Message::Type _type, Message::Interface _itf):
433  address(_address), type(_type), itf(_itf)
434  {}
435 
436  Entry(const Packet &packet): address(packet.destination.device),
437  type(packet.message.type), itf(packet.message.itf)
438  {}
439  };
440 
442  std::list<Entry> db;
443 
444  public:
445 
457  bool operator()(const HF::Protocol::Packet &packet);
458 
464  uint16_t size() const
465  {
466  return db.size();
467  }
468  };
469 
472  } // namespace Filters
473 
478  // =============================================================================
479  // Operators
480  // =============================================================================
481 
482  inline bool operator==(Address const &lhs, Address const &rhs)
483  {
484  return (lhs.device == rhs.device) && (lhs.unit == rhs.unit) && (lhs.mod == rhs.mod);
485  }
486 
487  inline bool operator!=(Address const &lhs, Address const &rhs)
488  {
489  return !(lhs == rhs);
490  }
491 
492  inline bool operator<(Address const &lhs, Address const &rhs)
493  {
494  return (lhs.device < rhs.device) ||
495  (lhs.device == rhs.device &&
496  (lhs.mod < rhs.mod || (lhs.mod == rhs.mod && lhs.unit < rhs.unit)));
497  }
498 
499  // =============================================================================
500  // Helper Functions
501  // =============================================================================
502 
516  bool request(Message::Type type, bool response = false);
517 
526  bool response(Message::Type type);
527 
539  bool matches(Message::Type lhs, Message::Type rhs);
540 
543  } // namespace Protocol
544 
545 } // namespace HF
546 
552 // =============================================================================
553 // Stream Helpers
554 // =============================================================================
555 
564 std::ostream &operator<<(std::ostream &stream, const HF::Protocol::Message::Type type);
565 
574 std::ostream &operator<<(std::ostream &stream, const HF::Protocol::Message &message);
575 
584 std::ostream &operator<<(std::ostream &stream, const HF::Protocol::Address &address);
585 
594 std::ostream &operator<<(std::ostream &stream, const HF::Protocol::Packet &packet);
595 
598 #endif /* HF_PROTOCOL_H */
constexpr uint16_t MAX_PAYLOAD
HAN-FUN Network - Maximum application payload length.
Definition: protocol.h:51
uint8_t unit
Source Unit.
Definition: protocol.h:206
Set pack attributes request.
Definition: protocol.h:81
uint16_t address
Source device address.
Definition: protocol.h:428
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.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Destination address is for single device.
Definition: protocol.h:213
uint16_t size() const
Number bytes needed to serialize the message.
Interface(uint16_t uid=0, uint8_t role=0, uint8_t member=0)
Constructor.
Definition: protocol.h:108
bool request(Message::Type type, bool response=false)
Check if message type is a request.
This class provides support for generating a response when a response is required from an incoming me...
Definition: protocol.h:421
uint16_t size() const
Number of entries in the filter&#39;s database.
Definition: protocol.h:464
bool response(Message::Type type)
Check if message is a response.
Type
Message types.
Definition: protocol.h:65
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.
Get pack attributes request.
Definition: protocol.h:78
int compare(const Address &other) const
Compare this address with the given address in other.
Definition: protocol.h:280
bool operator()(const HF::Protocol::Packet &packet)
Checks if the given packet, is a retransmission according to the filters database data...
Message message
Packet message payload;.
Definition: protocol.h:306
This file contains the common defines for the HAN-FUN library.
Set attributes request with response required.
Definition: protocol.h:75
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: protocol.h:230
Packet(Address &src_addr, Address &dst_addr, Message &message)
Constructor.
Definition: protocol.h:342
bool isCommand() const
Check if message type is equal to COMMAND_REQ or to COMMAND_RESP_REQ.
constexpr uint16_t BROADCAST_ADDR
HAN-FUN Broadcast - device address.
Definition: protocol.h:45
Address(uint16_t _dev=BROADCAST_ADDR, uint8_t _unit=BROADCAST_UNIT, Type _mod=DEVICE)
Create a new message address.
Definition: protocol.h:224
Transport::Link * link
Link where this packet originated from.
Definition: protocol.h:309
bool is_broadcast()
Checks if this address if for the Protocol::BROADCAST_ADDR and Protocol::BROADCAST_UNIT.
Definition: protocol.h:249
Set pack attributes response.
Definition: protocol.h:83
Set attributes request.
Definition: protocol.h:74
Atomic set pack attributes response.
Definition: protocol.h:87
uint16_t size() const
Number bytes needed to serialize the message.
Common::ByteArray payload
Message payload.
Definition: protocol.h:134
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Type type
Message type.
Definition: protocol.h:127
Message::Interface itf
Interface the message relates to.
Definition: protocol.h:430
Packet(Message &message)
Constructor.
Definition: protocol.h:318
uint16_t length
The payload length value read when unpacking the message.
Definition: protocol.h:137
bool matches(Message::Type lhs, Message::Type rhs)
Check if the given message types are the request and response for each other, for example...
Address destination
Destination Address.
Definition: protocol.h:301
uint16_t role
Interface role : Server or Client.
static constexpr uint16_t header_min_size
Minimum pack/unpack required header data size.
Definition: protocol.h:347
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: protocol.h:352
static const uint16_t min_size
Minimum pack/unpack required data size.
Definition: protocol.h:96
Atomic set pack attributes request with response required.
Definition: protocol.h:86
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.
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.
This class represents a byte array.
bool isCommandResponse() const
Check if message type is equal to COMMAND_RES.
Network Address.
Definition: protocol.h:201
uint8_t member
Interface destination member.
Definition: protocol.h:99
Get pack attributes response.
Definition: protocol.h:79
Interface Address.
Definition: protocol.h:93
Network Message.
Definition: protocol.h:60
Address source
Source Address.
Definition: protocol.h:300
Get attributes response.
Definition: protocol.h:72
Command request with response required.
Definition: protocol.h:68
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: protocol.h:162
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
HAN-FUN Protocol Packet.
Definition: protocol.h:298
Interface itf
Interface Address.
Definition: protocol.h:129
uint16_t mod
Address modifier.
Definition: protocol.h:203
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.
uint8_t reference
Application reference.
Definition: protocol.h:126
Message(uint16_t size=0, Type _type=COMMAND_REQ)
Constructor.
Definition: protocol.h:145
bool is_local(uint16_t address)
Checks if the given device address is equal to the device address present in this Protocol::Address o...
Definition: protocol.h:263
Message::Type type
Response type.
Definition: protocol.h:429
uint16_t size() const
Number bytes needed to serialize the message.
Parent class for the response messages.
Definition: protocol.h:368
uint16_t device
Device Address.
Definition: protocol.h:204
Type
HAN-FUN Network Destination Address Types.
Definition: protocol.h:211
std::list< Entry > db
Filter database.
Definition: protocol.h:442
std::ostream & operator<<(std::ostream &stream, const HF::Protocol::Message::Type type)
Convert the given message type into a string and write it to the given stream.
Destination address is for a group of devices.
Definition: protocol.h:214
uint16_t size() const
Number bytes needed to serialize the message.
Set attributes response.
Definition: protocol.h:76
static constexpr uint16_t min_size
Minimum number of bytes required by this message.
Definition: protocol.h:375
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Atomic set pack attributes request.
Definition: protocol.h:85
Packet(Address &dst_addr, Message &message, uint8_t unit=BROADCAST_UNIT)
Constructor.
Definition: protocol.h:327
Get attributes request.
Definition: protocol.h:71
Set pack attributes request with response required.
Definition: protocol.h:82
uint16_t size() const
Number bytes needed to serialize the message.
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Result
Commands result codes.
constexpr uint8_t BROADCAST_UNIT
HAN-FUN Broadcast - unit address.
Definition: protocol.h:48
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22