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.
 
 
 
 

190 regels
6.3 KiB

  1. /*******************************************************************************
  2. WINC3400 IoT OTA Interface.
  3. File Name:
  4. m2m_ota.c
  5. Summary:
  6. WINC3400 IoT OTA Interface
  7. Description:
  8. WINC3400 IoT OTA 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. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  34. INCLUDES
  35. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  36. #include "nm_common.h"
  37. #include "m2m_types.h"
  38. #include "m2m_ota.h"
  39. #include "m2m_wifi.h"
  40. #include "m2m_hif.h"
  41. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  42. MACROS
  43. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  44. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  45. DATA TYPES
  46. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  47. static tpfOtaUpdateCb gpfOtaUpdateCb = NULL;
  48. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  49. FUNCTION PROTOTYPES
  50. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  51. /**
  52. @fn m2m_ota_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr)
  53. @brief Internal OTA call back function.
  54. @param[in] u8OpCode
  55. HIF Opcode type.
  56. @param[in] u16DataSize
  57. HIF data length.
  58. @param[in] u32Addr
  59. HIF address.
  60. */
  61. static void m2m_ota_cb(uint8_t u8OpCode, uint16_t u16DataSize, uint32_t u32Addr)
  62. {
  63. int8_t s8Ret = M2M_SUCCESS;
  64. if (u8OpCode == M2M_OTA_RESP_UPDATE_STATUS)
  65. {
  66. tstrOtaUpdateStatusResp strOtaUpdateStatusResp;
  67. memset((uint8_t*)&strOtaUpdateStatusResp, 0, sizeof(tstrOtaUpdateStatusResp));
  68. s8Ret = hif_receive(u32Addr, (uint8_t*)&strOtaUpdateStatusResp, sizeof(tstrOtaUpdateStatusResp), 0);
  69. if(s8Ret == M2M_SUCCESS)
  70. {
  71. if(gpfOtaUpdateCb)
  72. gpfOtaUpdateCb(strOtaUpdateStatusResp.u8OtaUpdateStatusType, strOtaUpdateStatusResp.u8OtaUpdateStatus);
  73. }
  74. }
  75. else
  76. {
  77. M2M_ERR("Invalid OTA resp %d ?\r\n", u8OpCode);
  78. }
  79. }
  80. /*!
  81. @fn int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb)
  82. @brief Initialize the OTA layer.
  83. @param[in] pfOtaUpdateCb
  84. OTA Update callback function.
  85. @return The function returns @ref M2M_SUCCESS for success and a negative value otherwise.
  86. */
  87. int8_t m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb)
  88. {
  89. int8_t ret = M2M_SUCCESS;
  90. if (pfOtaUpdateCb) {
  91. gpfOtaUpdateCb = pfOtaUpdateCb;
  92. } else {
  93. M2M_ERR("Invalid OTA update callback\r\n");
  94. }
  95. hif_register_cb(M2M_REQ_GROUP_OTA, m2m_ota_cb);
  96. return ret;
  97. }
  98. /*!
  99. @fn int8_t m2m_ota_start_update(unsigned char * pcDownloadUrl)
  100. @brief Request OTA start update using the downloaded URL.
  101. @param[in] pcDownloadUrl
  102. The download firmware URL, you get it from device info.
  103. @return The function returns @ref M2M_SUCCESS for success and a negative value otherwise.
  104. */
  105. int8_t m2m_ota_start_update(unsigned char *pcDownloadUrl)
  106. {
  107. int8_t ret = M2M_SUCCESS;
  108. uint16_t u16DurlSize = strlen((char*)pcDownloadUrl) + 1;
  109. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_START_UPDATE, pcDownloadUrl, u16DurlSize, NULL, 0, 0);
  110. return ret;
  111. }
  112. /*!
  113. @fn int8_t m2m_ota_rollback(void)
  114. @brief Request OTA Rollback image.
  115. @return The function returns @ref M2M_SUCCESS for success and a negative value otherwise.
  116. */
  117. int8_t m2m_ota_rollback(void)
  118. {
  119. int8_t ret = M2M_SUCCESS;
  120. tstrM2mRev strRev;
  121. ret = m2m_ota_get_firmware_version(&strRev);
  122. if(ret == M2M_SUCCESS)
  123. {
  124. if(M2M_GET_HIF_BLOCK(strRev.u16FirmwareHifInfo) == M2M_HIF_BLOCK_VALUE)
  125. {
  126. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_ROLLBACK, NULL, 0, NULL, 0, 0);
  127. }
  128. else
  129. {
  130. ret = M2M_ERR_FAIL;
  131. }
  132. }
  133. return ret;
  134. }
  135. /*!
  136. @fn int8_t m2m_ota_abort(void)
  137. @brief Request OTA Abort.
  138. @return The function returns @ref M2M_SUCCESS for success and a negative value otherwise.
  139. */
  140. int8_t m2m_ota_abort(void)
  141. {
  142. int8_t ret = M2M_SUCCESS;
  143. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_ABORT, NULL, 0, NULL, 0, 0);
  144. return ret;
  145. }
  146. /*!
  147. @fn int8_t m2m_ota_switch_firmware(void)
  148. @brief Switch to the upgraded Firmware.
  149. @return The function returns @ref M2M_SUCCESS for success and a negative value otherwise.
  150. */
  151. int8_t m2m_ota_switch_firmware(void)
  152. {
  153. int8_t ret = M2M_SUCCESS;
  154. tstrM2mRev strRev;
  155. ret = m2m_ota_get_firmware_version(&strRev);
  156. if(ret == M2M_SUCCESS)
  157. {
  158. if(M2M_GET_HIF_BLOCK(strRev.u16FirmwareHifInfo) == M2M_HIF_BLOCK_VALUE)
  159. {
  160. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_SWITCH_FIRMWARE, NULL, 0, NULL, 0, 0);
  161. }
  162. else
  163. {
  164. ret = M2M_ERR_FAIL;
  165. }
  166. }
  167. return ret;
  168. }
  169. //DOM-IGNORE-END