Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MyOTAFirmwareUpdate.h 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. */
  19. /**
  20. * @file MyOTAFirmwareUpdate.h
  21. *
  22. * @defgroup MyOTAFirmwaregrp MyOTAFirmwareUpdate
  23. * @ingroup internals
  24. * @{
  25. *
  26. * MyOTAFirmwareUpdate-related log messages, format: [!]SYSTEM:[SUB SYSTEM:]MESSAGE
  27. * - [!] Exclamation mark is prepended in case of error or warning
  28. * - SYSTEM:
  29. * - <b>OTA</b> messages emitted by MyOTAFirmwareUpdate
  30. * - SUB SYSTEMS:
  31. * - OTA:<b>FRQ</b> from @ref firmwareOTAUpdateRequest()
  32. * - OTA:<b>FWP</b> from @ref firmwareOTAUpdateProcess()
  33. *
  34. * MyOTAFirmwareUpdate debug log messages:
  35. *
  36. * |E| SYS | SUB | Message | Comment
  37. * |-|-----|-----|-----------------------------|----------------------------------------------------------------------------
  38. * | | OTA | FWP | UPDATE | FW update initiated
  39. * |!| OTA | FWP | UPDO | FW config response received, FW update already ongoing
  40. * |!| OTA | FWP | FLASH INIT FAIL | Failed to initialise flash
  41. * | | OTA | FWP | UPDATE SKIPPED | FW update skipped, no newer version available
  42. * | | OTA | FWP | RECV B=%04X | Received FW block (B)
  43. * |!| OTA | FWP | WRONG FWB | Wrong FW block received
  44. * | | OTA | FWP | FW END | FW received, proceed to CRC verification
  45. * | | OTA | FWP | CRC OK | FW CRC verification OK
  46. * |!| OTA | FWP | CRC FAIL | FW CRC verification failed
  47. * | | OTA | FRQ | FW REQ,T=%04X,V=%04X,B=%04X | Request FW update, FW type (T), version (V), block (B)
  48. * |!| OTA | FRQ | FW UPD FAIL | FW update failed
  49. * | | OTA | CRC | B=%04X,C=%04X,F=%04X | FW CRC verification. FW blocks (B), calculated CRC (C), FW CRC (F)
  50. *
  51. *
  52. * @brief API declaration for MyOTAFirmwareUpdate
  53. */
  54. #ifndef MyOTAFirmwareUpdate_h
  55. #define MyOTAFirmwareUpdate_h
  56. #include "MySensorsCore.h"
  57. #ifdef MCUBOOT_PRESENT
  58. #include "generated_dts_board.h"
  59. #define FIRMWARE_PROTOCOL_31
  60. #endif
  61. #define LOCAL static //!< static
  62. #if MAX_PAYLOAD >= 22
  63. #define FIRMWARE_BLOCK_SIZE (16u) //!< Size of each firmware block
  64. #else
  65. #define FIRMWARE_BLOCK_SIZE (8u) //!< Size of each firmware block
  66. #ifndef FIRMWARE_PROTOCOL_31
  67. #define FIRMWARE_PROTOCOL_31
  68. #endif
  69. #endif
  70. #define FIRMWARE_MAX_REQUESTS (5u) //!< Number of times a firmware block should be requested before giving up
  71. #define MY_OTA_RETRY (5u) //!< Number of times to request a fw block before giving up
  72. #define MY_OTA_RETRY_DELAY (500u) //!< Number of milliseconds before re-requesting a FW block
  73. #ifndef MCUBOOT_PRESENT
  74. #define FIRMWARE_START_OFFSET (10u) //!< Start offset for firmware in flash (DualOptiboot wants to keeps a signature first)
  75. #else
  76. #define FIRMWARE_START_OFFSET (FLASH_AREA_IMAGE_1_OFFSET_0) //!< Use offset from generated_dts_board.h (mcuboot)
  77. #endif
  78. #define MY_OTA_BOOTLOADER_MAJOR_VERSION (3u) //!< Bootloader version major
  79. #ifdef FIRMWARE_PROTOCOL_31
  80. #define MY_OTA_BOOTLOADER_MINOR_VERSION (1u) //!< Bootloader version minor
  81. #else
  82. #define MY_OTA_BOOTLOADER_MINOR_VERSION (0u) //!< Bootloader version minor
  83. #endif
  84. #define MY_OTA_BOOTLOADER_VERSION (MY_OTA_BOOTLOADER_MINOR_VERSION * 256 + MY_OTA_BOOTLOADER_MAJOR_VERSION) //!< Bootloader version
  85. #if defined(MY_DEBUG_VERBOSE_OTA_UPDATE)
  86. #define OTA_DEBUG(x,...) DEBUG_OUTPUT(x, ##__VA_ARGS__) //!< debug
  87. //#define OTA_EXTRA_FLASH_DEBUG //!< Dumps flash after each FW block
  88. #else
  89. #define OTA_DEBUG(x,...) //!< debug NULL
  90. #endif
  91. #if defined(DOXYGEN) && !defined(FIRMWARE_PROTOCOL_31)
  92. /**
  93. * @brief Enabled FOTA 3.1 protocol extensions
  94. *
  95. * Supports smaller FIRMWARE_BLOCK_SIZE, RLE and NVM for nRF5 with mcuboot. The
  96. * extension is enabled per default when mcuboot is present or full FIRMWARE_BLOCK_SIZE
  97. * exeeds MAX_PAYLOAD.
  98. */
  99. #define FIRMWARE_PROTOCOL_31
  100. #endif
  101. /**
  102. * @brief FW config structure, stored in eeprom
  103. */
  104. typedef struct {
  105. uint16_t type; //!< Type of config
  106. uint16_t version; //!< Version of config
  107. uint16_t blocks; //!< Number of blocks
  108. uint16_t crc; //!< CRC of block data
  109. } __attribute__((packed)) nodeFirmwareConfig_t;
  110. /**
  111. * @brief FW config request structure
  112. */
  113. typedef struct {
  114. uint16_t type; //!< Type of config
  115. uint16_t version; //!< Version of config
  116. uint16_t blocks; //!< Number of blocks
  117. uint16_t crc; //!< CRC of block data
  118. uint16_t BLVersion; //!< Bootloader version
  119. #ifdef FIRMWARE_PROTOCOL_31
  120. uint8_t blockSize; //!< Blocksize, when protocol version >= 3.1 is reported. Otherwhise the blocksize is 16
  121. uint8_t img_commited; //!< mcuboot image_ok attribute commited firmware=0x01(mcuboot)|0x02(DualOptiboot), when protocol version >= 3.1 is reported
  122. uint16_t img_revision; //!< mcuboot revision attribute, when protocol version >= 3.1 is reported
  123. uint32_t img_build_num; //!< mcuboot build_num attribute, when protocol version >= 3.1 is reported
  124. #endif
  125. } __attribute__((packed)) requestFirmwareConfig_t;
  126. /**
  127. * @brief FW block request structure
  128. */
  129. typedef struct {
  130. uint16_t type; //!< Type of config
  131. uint16_t version; //!< Version of config
  132. uint16_t block; //!< Block index
  133. } __attribute__((packed)) requestFirmwareBlock_t;
  134. /**
  135. * @brief FW block reply structure
  136. */
  137. typedef struct {
  138. uint16_t type; //!< Type of config
  139. uint16_t version; //!< Version of config
  140. uint16_t block; //!< Block index
  141. uint8_t data[FIRMWARE_BLOCK_SIZE]; //!< Block data
  142. } __attribute__((packed)) replyFirmwareBlock_t;
  143. /**
  144. * @brief FW block reply structure (RLE)
  145. */
  146. typedef struct {
  147. uint16_t type; //!< Type of config
  148. uint16_t version; //!< Version of config
  149. uint16_t block; //!< Block index
  150. uint16_t number_of_blocks; //!< Number of blocks to fill with data
  151. uint8_t data; //!< Block data
  152. } __attribute__((packed)) replyFirmwareBlockRLE_t;
  153. /**
  154. * @brief Read firmware settings from EEPROM
  155. *
  156. * Current firmware settings (type, version, crc, blocks) are read into _fc
  157. */
  158. LOCAL void readFirmwareSettings(void);
  159. /**
  160. * @brief Handle OTA FW update requests
  161. */
  162. LOCAL void firmwareOTAUpdateRequest(void);
  163. /**
  164. * @brief Handle OTA FW update responses
  165. *
  166. * This function handles incoming OTA FW packets and stores them to external flash (Sensebender)
  167. */
  168. LOCAL bool firmwareOTAUpdateProcess(void);
  169. /**
  170. * @brief Validate uploaded FW CRC
  171. *
  172. * This function verifies if uploaded FW CRC is valid
  173. */
  174. LOCAL bool transportIsValidFirmware(void);
  175. /**
  176. * @brief Present bootloader/FW information upon startup
  177. */
  178. LOCAL void presentBootloaderInformation(void);
  179. #endif
  180. /** @}*/