Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

371 righe
13 KiB

  1. /*******************************************************************************
  2. WINC3400 OTA Upgrade API Interface
  3. File Name:
  4. m2m_ota.h
  5. Summary:
  6. WINC3400 OTA Upgrade API Interface
  7. Description:
  8. WINC3400 OTA Upgrade API Interface
  9. *******************************************************************************/
  10. //DOM-IGNORE-BEGIN
  11. /*******************************************************************************
  12. * Copyright (C) 2021 Microchip Technology Inc. and its subsidiaries.
  13. *
  14. * Subject to your compliance with these terms, you may use Microchip software
  15. * and any derivatives exclusively with Microchip products. It is your
  16. * responsibility to comply with third party license terms applicable to your
  17. * use of third party software (including open source software) that may
  18. * accompany Microchip software.
  19. *
  20. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
  21. * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
  22. * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
  23. * PARTICULAR PURPOSE.
  24. *
  25. * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
  26. * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
  27. * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
  28. * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
  29. * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
  30. * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  31. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  32. *******************************************************************************/
  33. // DOM-IGNORE-END
  34. /**@defgroup OTAAPI OTA
  35. @brief
  36. The WINC supports OTA (Over-The-Air) updates. Using the APIs described in this module,
  37. it is possible to request an ATWINC15x0 to update its firmware, or safely rollback to
  38. the previous firmware version.\n There are also APIs to download files and store them in
  39. the WINC's Flash (supported by ATWINC1510 only), which can be used for Host MCU OTA
  40. updates or accessing information stored remotely.
  41. @{
  42. @defgroup OTACALLBACKS Callbacks
  43. @brief
  44. Lists the different callbacks that can be used during OTA updates.\n
  45. Callbacks of type @ref tpfOtaNotifCb and @ref tpfOtaUpdateCb should be passed
  46. onto @ref m2m_ota_init at system initialization. Other callbacks are provided
  47. to handle the various steps of Host File Download.
  48. @defgroup OTADEFINE Defines
  49. @brief
  50. Specifies the macros and defines used by the OTA APIs.
  51. @defgroup OTATYPEDEF Enumerations and Typedefs
  52. @brief
  53. Specifies the enums and Data Structures used by the OTA APIs.
  54. @defgroup OTAFUNCTIONS Functions
  55. @brief
  56. Lists the full set of available APIs to manage OTA updates and Host File Downloads.
  57. @}
  58. */
  59. #ifndef __M2M_OTA_H__
  60. #define __M2M_OTA_H__
  61. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  62. INCLUDES
  63. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  64. #include "nm_common.h"
  65. #include "m2m_types.h"
  66. #include "nmdrv.h"
  67. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  68. MACROS
  69. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  70. /*!
  71. @typedef void (*tpfOtaUpdateCb)(uint8_t u8OtaUpdateStatusType, uint8_t u8OtaUpdateStatus);
  72. @brief
  73. A callback to get OTA status update, the callback provides the status type and its status.\n
  74. The OTA callback provides the download status, the switch to the downloaded firmware status
  75. and roll-back status.
  76. @param[in] u8OtaUpdateStatusType
  77. Possible values are listed in @ref tenuOtaUpdateStatusType.
  78. @param[in] u8OtaUpdateStatus
  79. Possible values are listed as enumerated by @ref tenuOtaUpdateStatus.
  80. @note
  81. Executes other callbacks passed to the OTA module.
  82. @see
  83. tenuOtaUpdateStatusType
  84. tenuOtaUpdateStatus
  85. */
  86. typedef void (*tpfOtaUpdateCb)(uint8_t u8OtaUpdateStatusType, uint8_t u8OtaUpdateStatus);
  87. /**@}*/ // OTACALLBACKS
  88. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  89. FUNCTION PROTOTYPES
  90. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  91. #ifdef __cplusplus
  92. extern "C" {
  93. #endif
  94. /*!
  95. @ingroup OTAFUNCTIONS
  96. @fn \
  97. int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb);
  98. @brief
  99. Synchronous initialization function for the OTA layer by registering the update callback.\n
  100. The notification callback is not supported at the current version. Calling this API is a
  101. MUST for all the OTA API's.
  102. @param[in] pfOtaUpdateCb
  103. OTA Update callback function.
  104. @return
  105. The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  106. */
  107. int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb);
  108. /*!
  109. @ingroup OTAFUNCTIONS
  110. @fn \
  111. int8_t m2m_ota_start_update(unsigned char * pcDownloadUrl);
  112. @brief
  113. Request OTA start update using the download URL. The OTA module will download the OTA image, ensure integrity of the image
  114. and update the validity of the image in the control structure. On completion, a callback of type @ref tpfOtaUpdateCb is called
  115. (callback previously provided via @ref m2m_ota_init). Switching to the updated image additionally requires completion of
  116. @ref m2m_ota_switch_firmware, followed by a WINC reset.
  117. @param[in] pcDownloadUrl
  118. The download firmware URL, according to the application server.
  119. @warning
  120. Calling this API does not guarantee OTA WINC image update, it depends on the connection with the
  121. download server and the validity of the image.\n
  122. Calling this API invalidates any previous valid rollback image, irrespective of the result, but when
  123. the OTA succeeds, the current image will become the rollback image after @ref m2m_ota_switch_firmware.
  124. @pre
  125. @ref m2m_ota_init is a prerequisite and must have been called before using @ref m2m_ota_start_update.\n
  126. Switching to the newly downloaded image requires calling @ref m2m_ota_switch_firmware API.
  127. @sa
  128. @ref m2m_ota_init
  129. @ref m2m_ota_switch_firmware
  130. @ref tpfOtaUpdateCb
  131. @return
  132. The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  133. Note that successful operation in this context means the OTA update request has reached the firmware OTA module.
  134. It does not indicate whether or not the image update succeeded.
  135. @section OTAExample Example
  136. This example shows how an OTA image update and switch is carried out.
  137. It demonstrates use of the following OTA APIs:
  138. - @ref m2m_ota_init
  139. - @ref tpfOtaUpdateCb
  140. - @ref m2m_ota_start_update
  141. - @ref m2m_ota_switch_firmware
  142. - @ref m2m_ota_rollback
  143. It also makes use of @ref m2m_wifi_check_ota_rb in order to inform OTA decisions.
  144. @code
  145. static void OtaUpdateCb(uint8_t u8OtaUpdateStatusType, uint8_t u8OtaUpdateStatus)
  146. {
  147. int8_t s8tmp;
  148. tstrM2mRev strtmp;
  149. M2M_INFO("%d %d\n", u8OtaUpdateStatusType, u8OtaUpdateStatus);
  150. switch(u8OtaUpdateStatusType)
  151. {
  152. case DL_STATUS:
  153. if(u8OtaUpdateStatus == OTA_STATUS_SUCCESS)
  154. {
  155. M2M_INFO("OTA download succeeded\n");
  156. s8tmp = m2m_wifi_check_ota_rb();
  157. if(s8tmp == M2M_ERR_FW_VER_MISMATCH)
  158. {
  159. // In this case the application SHOULD update the host driver before calling
  160. // @ref m2m_ota_switch_firmware(). Switching firmware image and resetting without updating host
  161. // driver would lead to severely limited functionality (i.e. OTA rollback only).
  162. }
  163. else if(s8tmp == M2M_SUCCESS)
  164. {
  165. // In this case the application MAY WANT TO update the host driver before calling
  166. // @ref m2m_ota_switch_firmware(). Switching firmware image and resetting without
  167. // updating host driver may lead to suboptimal functionality.
  168. }
  169. else
  170. {
  171. M2M_INFO("Cannot recognize downloaded image\n");
  172. // In this case the application MUST NOT update the host driver if such an update would change the
  173. // driver HIF Major field. Firmware switch @ref using m2m_ota_switch_firmware() is blocked.
  174. break;
  175. }
  176. // Switch to the upgraded firmware
  177. M2M_INFO("Now switching active partition...\n");
  178. s8tmp = m2m_ota_switch_firmware();
  179. }
  180. break;
  181. case SW_STATUS:
  182. case RB_STATUS:
  183. if(u8OtaUpdateStatus == OTA_STATUS_SUCCESS)
  184. {
  185. M2M_INFO("Switch/Rollback succeeded\n");
  186. // Start the host SW upgrade if required, then system reset is required (Reinitialize the driver)
  187. M2M_INFO("Now resetting the system...\n");
  188. system_reset();
  189. }
  190. break;
  191. }
  192. }
  193. static void wifi_event_cb(uint8_t u8WiFiEvent, void *pvMsg)
  194. {
  195. // ...
  196. case M2M_WIFI_REQ_DHCP_CONF:
  197. {
  198. // After successful connection, start the OTA upgrade
  199. m2m_ota_start_update(OTA_URL);
  200. }
  201. break;
  202. default:
  203. break;
  204. // ...
  205. }
  206. int main(void)
  207. {
  208. tstrWifiInitParam param;
  209. int8_t s8Ret = M2M_SUCCESS;
  210. bool rollback_required = FALSE;
  211. // System init, etc should be here...
  212. memset((uint8_t *)&param, 0, sizeof(param));
  213. param.pfAppWifiCb = wifi_event_cb;
  214. // Initialize the WINC Driver
  215. s8Ret = m2m_wifi_init(&param);
  216. if(s8Ret == M2M_ERR_FW_VER_MISMATCH)
  217. {
  218. M2M_ERR("Firmware version mismatch\n");
  219. s8Ret = m2m_wifi_check_ota_rb();
  220. if(s8Ret == M2M_SUCCESS)
  221. {
  222. // In this case the image in the inactive partition has compatible HIF. We will switch/rollback to it
  223. // after initializing the OTA module.
  224. rollback_required = TRUE;
  225. }
  226. }
  227. if(M2M_SUCCESS != s8Ret)
  228. {
  229. M2M_ERR("Driver Init Failed <%d>\n", s8Ret);
  230. while(1);
  231. }
  232. // Initialize the OTA module
  233. m2m_ota_init(OtaUpdateCb, NULL);
  234. if(rollback_required)
  235. {
  236. // We need to call either @ref m2m_ota_rollback() or @ref m2m_ota_switch_firmware() (functionally equivalent).
  237. m2m_ota_rollback();
  238. }
  239. else
  240. {
  241. // Connect to AP that provides connection to the OTA server
  242. m2m_wifi_default_connect();
  243. }
  244. while(1)
  245. {
  246. // Handle the app state machine plus the WINC event handler
  247. while(m2m_wifi_handle_events(NULL) != M2M_SUCCESS) {
  248. }
  249. }
  250. }
  251. @endcode
  252. */
  253. int8_t m2m_ota_start_update(unsigned char *pcDownloadUrl);
  254. /*!
  255. @ingroup OTAFUNCTIONS
  256. @fn \
  257. int8_t m2m_ota_rollback(void);
  258. @brief
  259. Request OTA Roll-back to the old (inactive) WINC image, the WINC firmware will check the validity of the inactive image
  260. and activate it if valid. On completion, a callback of type @ref tpfOtaUpdateCb is called (application must previously have
  261. provided the callback via @ref m2m_ota_init). If the callback indicates successful activation, the newly-activated image
  262. will start running after next system reset.
  263. @warning
  264. If rollback requires a host driver update in order to maintain HIF compatibility (HIF
  265. major value change), then it is recommended to update the host driver prior to calling this API.\n
  266. In the event of system reset with incompatible driver/firmware, compatibility can be
  267. recovered by calling @ref m2m_ota_rollback or @ref m2m_ota_switch_firmware. See @ref OTAExample.
  268. @sa
  269. m2m_ota_init
  270. m2m_ota_start_update
  271. @return
  272. The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  273. */
  274. int8_t m2m_ota_rollback(void);
  275. /*!
  276. @ingroup OTAFUNCTIONS
  277. @fn \
  278. int8_t m2m_ota_abort(void);
  279. @brief
  280. Request the WINC to abort an OTA in progress.\n
  281. If no download is in progress, the API will respond with failure.
  282. @sa
  283. m2m_ota_init
  284. m2m_ota_start_update
  285. @return
  286. The function returns @ref M2M_SUCCESS for a successful operation and a negative value otherwise.
  287. */
  288. int8_t m2m_ota_abort(void);
  289. /*!
  290. @ingroup OTAFUNCTIONS
  291. @fn \
  292. int8_t m2m_ota_switch_firmware(void);
  293. @brief
  294. Request switch to the updated WINC image. The WINC firmware will check the validity of the
  295. inactive image and activate it if valid. On completion, a callback of type @ref tpfOtaUpdateCb
  296. is called (application must previously have provided the callback via @ref m2m_ota_init).
  297. If the callback indicates successful activation, the newly-activated image will start running
  298. after next system reset.
  299. @warning
  300. If switch requires a host driver update in order to maintain HIF compatibility (HIF
  301. major value change), then it is recommended to update the host driver prior to calling this API.\n
  302. In the event of system reset with incompatible driver/firmware, compatibility can be
  303. recovered by calling @ref m2m_ota_rollback or @ref m2m_ota_switch_firmware. See @ref OTAExample.
  304. @sa
  305. m2m_ota_init
  306. m2m_ota_start_update
  307. @return
  308. The function returns @ref M2M_SUCCESS for successful operations and a negative value otherwise.
  309. */
  310. int8_t m2m_ota_switch_firmware(void);
  311. #ifdef __cplusplus
  312. }
  313. #endif
  314. #endif /* __M2M_OTA_H__ */