Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

384 строки
12 KiB

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