HAN-FUN API  1.5.3
This project provides the common implementation of ULE Alliance's HAN-FUN application protocol.
suota.h
Go to the documentation of this file.
1 // =============================================================================
15 // =============================================================================
16 
17 #ifndef HF_CORE_SUOTA_H
18 #define HF_CORE_SUOTA_H
19 
20 #include "hanfun/common.h"
21 
22 #include "hanfun/protocol.h"
23 #include "hanfun/core.h"
24 
25 namespace HF
26 {
27  namespace Core
28  {
29  // Forward declaration.
30  namespace SUOTA
31  {
32  class Server;
33  }
34 
50 
54  namespace SUOTA
55  {
63  typedef enum _CMD
65  {
69  __LAST_CMD__ = UPGRADE_COMPLETE_CMD
70  } CMD;
71 
72  // =============================================================================
73  // Helper Classes
74  // =============================================================================
75 
79  typedef enum _NewVersionResponse
80  {
84  BATTERY_TOO_LOW = 0x13,
87 
91  struct Version
92  {
93  std::string sw_version;
94  std::string hw_version;
95  std::string url;
96 
104  Version(const std::string _sw_version = "",
105  const std::string _hw_version = "",
106  const std::string _url = ""):
107  sw_version(_sw_version), hw_version(_hw_version), url(_url)
108  {}
109 
111  static constexpr uint16_t min_size = 2 * sizeof(uint8_t);
112 
114  uint16_t size() const;
115 
117  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const;
118 
120  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0);
121  };
122 
128  {
132  typedef enum _Status
133  {
138  } Status;
139 
148  CheckVersionResponse(Status _status = FAIL_UNKNOWN, const std::string _sw_version = "",
149  const std::string _hw_version = "", const std::string _url = ""):
150  Protocol::Response(_status),
151  Version(_sw_version, _hw_version, _url) {}
152 
154  Protocol::Response(_status), Version() {}
155 
158 
160  uint16_t size() const
161  {
163  }
164 
166  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const
167  {
168  HF_SERIALIZABLE_CHECK(array, offset, size());
169 
170  uint16_t start = offset;
171 
172  offset += Protocol::Response::pack(array, offset);
173  offset += Version::pack(array, offset);
174 
175  return offset - start;
176  }
177 
179  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0)
180  {
181  HF_SERIALIZABLE_CHECK(array, offset, min_size);
182 
183  uint16_t start = offset;
184 
185  uint16_t size = Protocol::Response::unpack(array, offset);
186  HF_ASSERT(size != 0, {return 0;});
187  offset += size;
188  size = Version::unpack(array, offset);
189  HF_ASSERT(size != 0, {return 0;});
190  offset += size;
191 
192  return offset - start;
193  }
194  };
195 
201  {
205  typedef enum _Status
206  {
210  INVALID_IMAGE = 0x13,
214  } Code;
215 
216  Code code;
217  std::string sw_version;
218 
225  UpgradeStatus(Code _code = FAIL_UNKNOWN, std::string _sw_version = ""):
226  code(_code), sw_version(_sw_version) {}
227 
229  static constexpr uint16_t min_size = sizeof(uint8_t);
230 
232  uint16_t size() const
233  {
234  return sizeof(uint8_t) +
235  (sw_version.empty() ? 0 :
237  }
238 
240  uint16_t pack(Common::ByteArray &array, uint16_t offset = 0) const
241  {
242  HF_SERIALIZABLE_CHECK(array, offset, size());
243 
244  uint16_t start = offset;
245 
246  offset += array.write(offset, static_cast<uint8_t>(code));
247 
248  if (!sw_version.empty())
249  {
250  offset +=
252  }
253 
254  return offset - start;
255  }
256 
258  uint16_t unpack(const Common::ByteArray &array, uint16_t offset = 0)
259  {
260  HF_SERIALIZABLE_CHECK(array, offset, min_size);
261 
262  uint16_t start = offset;
263 
264  uint8_t temp;
265  offset += array.read(offset, temp);
266  code = static_cast<Code>(temp);
267 
269 
270  return offset - start;
271  }
272  };
273 
274  // =============================================================================
275  // Attributes API
276  // =============================================================================
277 
289 
295  struct Base: public Service<HF::Interface::SUOTA>
296  {
297  protected:
298 
301  };
302 
308  class Server: public ServiceRole<SUOTA::Base, HF::Interface::SERVER_ROLE>
309  {
310  public:
311 
315 
317  virtual ~Server() {}
318 
319  // ======================================================================
320  // Commands
321  // ======================================================================
324 
332  void new_version_available(const Protocol::Address &addr, const Version &version);
333 
338  void new_version_available(const Version &version)
339  {
340  Protocol::Address addr;
341  new_version_available(addr, version);
342  }
343 
345  // ======================================================================
346 
347  // ======================================================================
348  // Events
349  // ======================================================================
352 
360  virtual void new_version_available(const Protocol::Address &addr,
361  NewVersionResponse result);
362 
376  const Version &version);
377 
385  virtual void upgrade_complete(const Protocol::Address &addr,
386  const UpgradeStatus &status);
387 
389  // ======================================================================
390  protected:
391 
393  uint16_t offset);
394  };
395 
401  struct Client: public ServiceRole<SUOTA::Base, HF::Interface::CLIENT_ROLE>
402  {
403  Client(HF::Core::Unit0 &unit0):
405 
406  virtual ~Client() {}
407 
408  // ======================================================================
409  // Commands
410  // ======================================================================
413 
414 #ifdef HF_CORE_SUOTA_CHECK_VERSION_CMD
415 
421  void check_version(const Protocol::Address &addr, const Version &version);
422 
427  void check_version(const Version &version)
428  {
429  Protocol::Address addr;
430  check_version(addr, version);
431  }
432 #endif
433 
442  void upgrade_complete(const Protocol::Address &addr, UpgradeStatus::Code status,
443  std::string version = "");
444 
452  void upgrade_complete(UpgradeStatus::Code status, std::string version = "")
453  {
454  Protocol::Address addr;
455  upgrade_complete(addr, status, version);
456  }
457 
459  // ======================================================================
460 
461  // ======================================================================
462  // Events
463  // ======================================================================
467 
483  const Version &version);
484 
492  virtual void check_version(const Protocol::Address &addr,
494 
496  // =============================================================================
497 
498  protected:
499 
501  uint16_t offset);
502  };
503 
506  } // namespace SUOTA
507 
508  } // namespace Core
509 
510 } // namespace HF
511 
517 // =============================================================================
518 // Stream Helpers
519 // =============================================================================
520 
529 std::ostream &operator<<(std::ostream &stream, const HF::Core::SUOTA::CMD command);
530 
533 #endif /* HF_CORE_SUOTA_H */
std::string sw_version
Update status.
Definition: suota.h:217
New Version Available command UID.
Definition: suota.h:66
NewVersionResponse
Result codes for a SUOTA::NEW_VERSION_AVAILABLE_CMD.
Definition: suota.h:79
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.
Definition: suota.h:240
uint16_t write(uint16_t offset, uint8_t data)
Write a byte into the array at the given offset.
bool response(Message::Type type)
Check if message is a response.
std::string sw_version
Current/Available software version.
Definition: suota.h:93
This represent the special unit with ID/UID = 0.
Definition: core.h:67
virtual void check_version(const Protocol::Address &addr, const CheckVersionResponse &response)
Callback that is called when a response to a SUOTA::CHECK_VERSION_CMD, is received.
void new_version_available(const Version &version)
Send a HAN-FUN message containing a SUOTA::NEW_VERSION_AVAILABLE_CMD, to the broadcast network addres...
Definition: suota.h:338
virtual void upgrade_complete(const Protocol::Address &addr, const UpgradeStatus &status)
Callback that is called when a SUOTA::UPGRADE_COMPLETE_CMD, is received.
uint16_t size() const
Number bytes needed to serialize the message.
Definition: suota.h:232
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: suota.h:229
uint16_t size() const
Number bytes needed to serialize the message.
Definition: suota.h:160
This file contains the common defines for the HAN-FUN library.
void upgrade_complete(const Protocol::Address &addr, UpgradeStatus::Code status, std::string version="")
Send a HAN-FUN message containing a SUOTA::UPGRADE_COMPLETE_CMD, to the given network address...
#define HF_ASSERT(_expr, _block)
Helper macro to check for correct assumptions.
This file contains the forward declarations of the core services and interfaces implementing classes...
Upgrade initiated.
Definition: suota.h:81
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
void new_version_available(const Protocol::Address &addr, const Version &version)
Send a HAN-FUN message containing a SUOTA::NEW_VERSION_AVAILABLE_CMD, to the given network address...
Server(HF::Core::Unit0 &unit0)
Constructor.
Definition: suota.h:313
uint16_t unpack(const ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Battery too low.
Definition: suota.h:84
uint16_t size() const
Number bytes needed to serialize the message.
This file contains the definitions for the HAN-FUN protocol messages.
SUOTA interface UID.
Definition: interface.h:75
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: suota.h:111
UpgradeStatus(Code _code=FAIL_UNKNOWN, std::string _sw_version="")
Software version after update.
Definition: suota.h:225
Common::Result handle_command(Protocol::Packet &packet, Common::ByteArray &payload, uint16_t offset)
Handle incoming messages from the network.
SUOTA Interfaces::Interface : Client side implementation.
Definition: suota.h:401
Base(HF::Core::Unit0 &unit0)
Constructor.
Definition: suota.h:300
#define HF_SERIALIZABLE_CHECK(__array, __offset, __size)
Helper macro to check if the given __array has enough size so __size bytes can be written/read from t...
Version(const std::string _sw_version="", const std::string _hw_version="", const std::string _url="")
Constructor.
Definition: suota.h:104
Invalid software.
Definition: suota.h:82
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.
Definition: suota.h:166
Fail - Unknown reason.
Check Version command UID.
Definition: suota.h:67
Helper class that represents the information of a particular version.
Definition: suota.h:91
void upgrade_complete(UpgradeStatus::Code status, std::string version="")
Send a HAN-FUN message containing a SUOTA::UPGRADE_COMPLETE_CMD, to the broadcast network address...
Definition: suota.h:452
FAIL_UNKNOWN.
Definition: suota.h:85
uint16_t size() const
Number bytes needed to serialize the message.
std::ostream & operator<<(std::ostream &stream, const HF::Core::SUOTA::CMD command)
Convert the given command into a string and write it to the given stream.
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.
virtual ~Server()
Destructor.
Definition: suota.h:317
Network Address.
Definition: protocol.h:201
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Definition: suota.h:179
Unsupported hardware.
Definition: suota.h:83
uint16_t pack(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.
Code
Possible status in a.
Definition: suota.h:205
SUOTA Interfaces::Interface : Parent.
Definition: suota.h:295
SUOTA Interfaces::Interface : Server side implementation.
Definition: suota.h:308
HAN-FUN Protocol Packet.
Definition: protocol.h:298
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
Definition: suota.h:258
CheckVersionResponse(Status _status=FAIL_UNKNOWN, const std::string _sw_version="", const std::string _hw_version="", const std::string _url="")
Constructor.
Definition: suota.h:148
This class contains the required parameters for a SUOTA::UPGRADE_COMPLETE_CMD command.
Definition: suota.h:200
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/Service Attribute API.
Definition: attributes.h:44
Status
Possible result codes for a SUOTA::CHECK_VERSION_CMD.
Definition: suota.h:132
uint16_t read(uint16_t offset, uint8_t &data) const
Read the byte at offset into data.
uint16_t size() const
Number bytes needed to serialize the message.
Parent class for the response messages.
Definition: protocol.h:368
HF::Attributes::IAttribute * create_attribute(uint8_t uid)
Create an attribute object that can hold the attribute with the given uid. (HF::Interfaces::SUOTA::Se...
Class template for all interfaces role implementations.
Definition: core.h:225
virtual NewVersionResponse new_version_available(const Protocol::Address &addr, const Version &version)
Callback that is called when a SUOTA::NEW_VERSION_AVAILABLE_CMD, is received.
static constexpr uint16_t min_size
Minimum number of bytes required by this message.
Definition: protocol.h:375
This class represents the message for the response of a SUOTA::CHECK_VERSION_CMD. ...
Definition: suota.h:127
Common interface for all Interfaces.
Definition: interface.h:43
std::string url
Update URL.
Definition: suota.h:95
Class template for all core services implementations.
Definition: core.h:188
uint16_t unpack(const Common::ByteArray &array, uint16_t offset=0)
Read a message from a ByteArray.
CMD
Command IDs.
Definition: suota.h:64
std::string hw_version
Current/Minimum required hardware version.
Definition: suota.h:94
Result
Commands result codes.
Upgrade Complete command UID.
Definition: suota.h:68
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.
static constexpr uint16_t min_size
Minimum pack/unpack required data size.
Definition: suota.h:157
virtual CheckVersionResponse check_version(const Protocol::Address &addr, const Version &version)
Callback that is called when a SUOTA::CHECK_VERSION_CMD, is received.
Top-level namespace for the HAN-FUN library.
Definition: attributes.h:22