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.
 
 
 
 

396 lines
10 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief NMC1500 IoT OTA Interface.
  6. *
  7. * Copyright (c) 2015 - 2017 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  27. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  29. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  30. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * \asf_license_stop
  39. *
  40. */
  41. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  42. INCLUDES
  43. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  44. #include "common/include/nm_common.h"
  45. #include "driver/include/m2m_types.h"
  46. #include "driver/include/m2m_ota.h"
  47. #include "driver/source/m2m_hif.h"
  48. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  49. MACROS
  50. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  51. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  52. DATA TYPES
  53. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  54. static tpfOtaUpdateCb gpfOtaUpdateCb = NULL;
  55. static tpfOtaNotifCb gpfOtaNotifCb = NULL;
  56. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  57. FUNCTION PROTOTYPES
  58. *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  59. /**
  60. * @fn m2m_wifi_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr, uint8 grp)
  61. * @brief WiFi call back function
  62. * @param [in] u8OpCode
  63. * HIF Opcode type.
  64. * @param [in] u16DataSize
  65. * HIF data length.
  66. * @param [in] u32Addr
  67. * HIF address.
  68. * @param [in] grp
  69. * HIF group type.
  70. * @author
  71. * @date
  72. * @version 1.0
  73. */
  74. static void m2m_ota_cb(uint8 u8OpCode, uint16 u16DataSize, uint32 u32Addr)
  75. {
  76. sint8 ret = M2M_SUCCESS;
  77. if (u8OpCode == M2M_OTA_RESP_NOTIF_UPDATE_INFO) {
  78. tstrOtaUpdateInfo strOtaUpdateInfo;
  79. m2m_memset((uint8 *)&strOtaUpdateInfo, 0, sizeof(tstrOtaUpdateInfo));
  80. ret = hif_receive(u32Addr, (uint8 *)&strOtaUpdateInfo, sizeof(tstrOtaUpdateInfo), 0);
  81. if (ret == M2M_SUCCESS) {
  82. if (gpfOtaNotifCb)
  83. gpfOtaNotifCb(&strOtaUpdateInfo);
  84. }
  85. } else if (u8OpCode == M2M_OTA_RESP_UPDATE_STATUS) {
  86. tstrOtaUpdateStatusResp strOtaUpdateStatusResp;
  87. m2m_memset((uint8 *)&strOtaUpdateStatusResp, 0, sizeof(tstrOtaUpdateStatusResp));
  88. ret = hif_receive(u32Addr, (uint8 *)&strOtaUpdateStatusResp, sizeof(tstrOtaUpdateStatusResp), 0);
  89. if (ret == M2M_SUCCESS) {
  90. if (gpfOtaUpdateCb)
  91. gpfOtaUpdateCb(strOtaUpdateStatusResp.u8OtaUpdateStatusType, strOtaUpdateStatusResp.u8OtaUpdateStatus);
  92. }
  93. } else {
  94. M2M_ERR("Invaild OTA resp %d ?\n", u8OpCode);
  95. }
  96. }
  97. /*!
  98. @fn \
  99. NMI_API sint8 m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb, tpfOtaNotifCb pfOtaNotifCb);
  100. @brief
  101. Initialize the OTA layer.
  102. @param [in] pfOtaUpdateCb
  103. OTA Update callback function
  104. @param [in] pfOtaNotifCb
  105. OTA notify callback function
  106. @return
  107. The function SHALL return 0 for success and a negative value otherwise.
  108. */
  109. NMI_API sint8 m2m_ota_init(tpfOtaUpdateCb pfOtaUpdateCb, tpfOtaNotifCb pfOtaNotifCb)
  110. {
  111. sint8 ret = M2M_SUCCESS;
  112. if (pfOtaUpdateCb) {
  113. gpfOtaUpdateCb = pfOtaUpdateCb;
  114. } else {
  115. M2M_ERR("Invaild Ota update cb\n");
  116. }
  117. if (pfOtaNotifCb) {
  118. gpfOtaNotifCb = pfOtaNotifCb;
  119. } else {
  120. M2M_ERR("Invaild Ota notify cb\n");
  121. }
  122. hif_register_cb(M2M_REQ_GROUP_OTA, m2m_ota_cb);
  123. return ret;
  124. }
  125. /*!
  126. @fn \
  127. NMI_API sint8 m2m_ota_notif_set_url(uint8 * u8Url);
  128. @brief
  129. Set the OTA url
  130. @param [in] u8Url
  131. The url server address
  132. @return
  133. The function SHALL return 0 for success and a negative value otherwise.
  134. */
  135. NMI_API sint8 m2m_ota_notif_set_url(uint8 *u8Url)
  136. {
  137. sint8 ret = M2M_SUCCESS;
  138. uint16 u16UrlSize = m2m_strlen(u8Url) + 1;
  139. /*Todo: we may change it to data pkt but we need to give it higer priority
  140. but the priorty is not implemnted yet in data pkt
  141. */
  142. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_NOTIF_SET_URL, u8Url, u16UrlSize, NULL, 0, 0);
  143. return ret;
  144. }
  145. /*!
  146. @fn \
  147. NMI_API sint8 m2m_ota_notif_check_for_update(void);
  148. @brief
  149. check for ota update
  150. @return
  151. The function SHALL return 0 for success and a negative value otherwise.
  152. */
  153. NMI_API sint8 m2m_ota_notif_check_for_update(void)
  154. {
  155. sint8 ret = M2M_SUCCESS;
  156. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_NOTIF_CHECK_FOR_UPDATE, NULL, 0, NULL, 0, 0);
  157. return ret;
  158. }
  159. /*!
  160. @fn \
  161. NMI_API sint8 m2m_ota_notif_sched(uint32 u32Period);
  162. @brief
  163. Schedule OTA update
  164. @param [in] u32Period
  165. Period in days
  166. @return
  167. The function SHALL return 0 for success and a negative value otherwise.
  168. */
  169. NMI_API sint8 m2m_ota_notif_sched(uint32 u32Period)
  170. {
  171. sint8 ret = M2M_SUCCESS;
  172. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_NOTIF_CHECK_FOR_UPDATE, NULL, 0, NULL, 0, 0);
  173. return ret;
  174. }
  175. /*!
  176. @fn \
  177. NMI_API sint8 m2m_ota_start_update(uint8 * u8DownloadUrl);
  178. @brief
  179. Request OTA start update using the downloaded url
  180. @param [in] u8DownloadUrl
  181. The download firmware url, you get it from device info
  182. @return
  183. The function SHALL return 0 for success and a negative value otherwise.
  184. */
  185. NMI_API sint8 m2m_ota_start_update(uint8 *u8DownloadUrl)
  186. {
  187. sint8 ret = M2M_SUCCESS;
  188. uint16 u16DurlSize = m2m_strlen(u8DownloadUrl) + 1;
  189. /*Todo: we may change it to data pkt but we need to give it higer priority
  190. but the priorty is not implemnted yet in data pkt
  191. */
  192. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_START_FW_UPDATE, u8DownloadUrl, u16DurlSize, NULL, 0, 0);
  193. return ret;
  194. }
  195. /*!
  196. @fn \
  197. NMI_API sint8 m2m_ota_start_update_crt(uint8 * u8DownloadUrl);
  198. @brief
  199. Request OTA start for the Cortus app image.
  200. @param [in] u8DownloadUrl
  201. The cortus application image url.
  202. @return
  203. The function SHALL return 0 for success and a negative value otherwise.
  204. */
  205. NMI_API sint8 m2m_ota_start_update_crt(uint8 *u8DownloadUrl)
  206. {
  207. sint8 ret = M2M_SUCCESS;
  208. uint16 u16DurlSize = m2m_strlen(u8DownloadUrl) + 1;
  209. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_START_CRT_UPDATE, u8DownloadUrl, u16DurlSize, NULL, 0, 0);
  210. return ret;
  211. }
  212. /*!
  213. @fn \
  214. NMI_API sint8 m2m_ota_rollback(void);
  215. @brief
  216. Request OTA Rollback image
  217. @return
  218. The function SHALL return 0 for success and a negative value otherwise.
  219. */
  220. NMI_API sint8 m2m_ota_rollback(void)
  221. {
  222. sint8 ret = M2M_SUCCESS;
  223. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_ROLLBACK_FW, NULL, 0, NULL, 0, 0);
  224. return ret;
  225. }
  226. /*!
  227. @fn \
  228. NMI_API sint8 m2m_ota_rollback_crt(void);
  229. @brief
  230. Request Cortus application OTA Rollback image
  231. @return
  232. The function SHALL return 0 for success and a negative value otherwise.
  233. */
  234. NMI_API sint8 m2m_ota_rollback_crt(void)
  235. {
  236. sint8 ret = M2M_SUCCESS;
  237. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_ROLLBACK_CRT, NULL, 0, NULL, 0, 0);
  238. return ret;
  239. }
  240. /*!
  241. @fn \
  242. NMI_API sint8 m2m_ota_abort(void);
  243. @brief
  244. Request OTA Abort
  245. @return
  246. The function SHALL return 0 for success and a negative value otherwise.
  247. */
  248. NMI_API sint8 m2m_ota_abort(void)
  249. {
  250. sint8 ret = M2M_SUCCESS;
  251. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_ABORT, NULL, 0, NULL, 0, 0);
  252. return ret;
  253. }
  254. /*!
  255. @fn \
  256. NMI_API sint8 m2m_ota_switch_firmware(void);
  257. @brief
  258. Switch to the upgraded Firmware
  259. @return
  260. The function SHALL return 0 for success and a negative value otherwise.
  261. */
  262. NMI_API sint8 m2m_ota_switch_firmware(void)
  263. {
  264. sint8 ret = M2M_SUCCESS;
  265. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_SWITCH_FIRMWARE, NULL, 0, NULL, 0, 0);
  266. return ret;
  267. }
  268. /*!
  269. @fn \
  270. NMI_API sint8 m2m_ota_switch_crt(void);
  271. @brief
  272. Switch to the upgraded cortus application.
  273. @return
  274. The function SHALL return 0 for success and a negative value otherwise.
  275. */
  276. NMI_API sint8 m2m_ota_switch_crt(void)
  277. {
  278. sint8 ret = M2M_SUCCESS;
  279. ret = hif_send(M2M_REQ_GROUP_OTA, M2M_OTA_REQ_SWITCH_CRT_IMG, NULL, 0, NULL, 0, 0);
  280. return ret;
  281. }
  282. /*!
  283. @fn \
  284. NMI_API sint8 m2m_ota_get_firmware_version(tstrM2mRev * pstrRev);
  285. @brief
  286. Get the OTA Firmware version.
  287. @return
  288. The function SHALL return 0 for success and a negative value otherwise.
  289. */
  290. NMI_API sint8 m2m_ota_get_firmware_version(tstrM2mRev *pstrRev)
  291. {
  292. sint8 ret = M2M_SUCCESS;
  293. ret = hif_chip_wake();
  294. if (ret == M2M_SUCCESS) {
  295. ret = nm_get_ota_firmware_info(pstrRev);
  296. hif_chip_sleep();
  297. }
  298. return ret;
  299. }
  300. #if 0
  301. #define M2M_OTA_FILE "../../../m2m_ota.dat"
  302. NMI_API sint8 m2m_ota_test(void)
  303. {
  304. uint32 page = 0;
  305. uint8 buffer[1500];
  306. uint32 u32Sz = 0;
  307. sint8 ret = M2M_SUCCESS;
  308. FILE *fp =NULL;
  309. fp = fopen(M2M_OTA_FILE,"rb");
  310. if(fp)
  311. {
  312. fseek(fp, 0L, SEEK_END);
  313. u32Sz = ftell(fp);
  314. fseek(fp, 0L, SEEK_SET);
  315. while(u32Sz > 0)
  316. {
  317. {
  318. page = (rand()%1400);
  319. if((page<100)||(page>1400)) page = 1400;
  320. }
  321. if(u32Sz>page)
  322. {
  323. u32Sz-=page;
  324. }
  325. else
  326. {
  327. page = u32Sz;
  328. u32Sz = 0;
  329. }
  330. printf("page %d\n", (int)page);
  331. fread(buffer,page,1,fp);
  332. ret = hif_send(M2M_REQ_GROUP_OTA,M2M_OTA_REQ_TEST|M2M_REQ_DATA_PKT,NULL,0,(uint8*)&buffer,page,0);
  333. if(ret != M2M_SUCCESS)
  334. {
  335. M2M_ERR("\n");
  336. }
  337. nm_bsp_sleep(1);
  338. }
  339. }
  340. else
  341. {
  342. M2M_ERR("nO err\n");
  343. }
  344. return ret;
  345. }
  346. #endif