MT_OTA.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**************************************************************************************************
  2. Filename: MT_OTA.c
  3. Revised: $Date: 2010-09-24 08:53:00 -0700 (Fri, 24 Sep 2010) $
  4. Revision: $Revision: 23900 $
  5. Description: MonitorTest functions for the ZCL OTA Upgrade.
  6. Copyright 2004-2007 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. #ifdef MT_OTA_FUNC
  34. /**************************************************************************************************
  35. * INCLUDES
  36. **************************************************************************************************/
  37. #include "ZComDef.h"
  38. #include "OSAL.h"
  39. #include "MT.h"
  40. #include "MT_OTA.h"
  41. #if !defined( WIN32 )
  42. #include "OnBoard.h"
  43. #endif
  44. /**************************************************************************************************
  45. * CONSTANTS
  46. **************************************************************************************************/
  47. /**************************************************************************************************
  48. * GLOBAL VARIABLES
  49. **************************************************************************************************/
  50. uint8 OTA_Task = 0xFF;
  51. /**************************************************************************************************
  52. * LOCAL VARIABLES
  53. **************************************************************************************************/
  54. /**************************************************************************************************
  55. * LOCAL FUNCTIONS
  56. **************************************************************************************************/
  57. /***************************************************************************************************
  58. * @fn MT_OtaRegister
  59. *
  60. * @brief Called to set the task to receive callbacks from the MT OTA.
  61. *
  62. * @param taskId - task identifier
  63. *
  64. * @return void
  65. ***************************************************************************************************/
  66. void MT_OtaRegister(uint8 taskId)
  67. {
  68. OTA_Task = taskId;
  69. }
  70. /***************************************************************************************************
  71. * @fn MT_OtaCommandProcessing
  72. *
  73. * @brief Process all the MT OTA commands that are issued by the OTA Console tool
  74. *
  75. * @param pBuf - pointer to the msg buffer
  76. *
  77. * | LEN | CMD0 | CMD1 | DATA |
  78. * | 1 | 1 | 1 | 0-255 |
  79. *
  80. * @return status
  81. ***************************************************************************************************/
  82. uint8 MT_OtaCommandProcessing(uint8* pBuf)
  83. {
  84. uint8 status = MT_RPC_SUCCESS;
  85. uint8 len;
  86. OTA_MtMsg_t *pMsg;
  87. uint8 cmd = pBuf[MT_RPC_POS_CMD1];
  88. if (cmd == MT_OTA_FILE_READ_RSP || cmd == MT_OTA_NEXT_IMG_RSP)
  89. {
  90. // Forward the message to the task
  91. if (OTA_Task != 0xff)
  92. {
  93. len = pBuf[MT_RPC_POS_LEN];
  94. pMsg = (OTA_MtMsg_t*) osal_msg_allocate(len + sizeof(OTA_MtMsg_t));
  95. if (pMsg)
  96. {
  97. pMsg->hdr.event = MT_SYS_OTA_MSG;
  98. pMsg->cmd = cmd;
  99. osal_memcpy(pMsg->data, &pBuf[MT_RPC_POS_DAT0], len);
  100. osal_msg_send(OTA_Task, (uint8*) pMsg);
  101. }
  102. }
  103. }
  104. else
  105. {
  106. status = MT_RPC_ERR_COMMAND_ID;
  107. }
  108. return status;
  109. }
  110. /***************************************************************************************************
  111. * @fn MT_OtaFileReadReq
  112. *
  113. * @brief Requests a block of a file be read from the remote.
  114. *
  115. * @param pAddr - The addres of the device requsting the data
  116. * @param pFileId - Teh id of the image to read from
  117. * @param len - Amount of data to read (must be smaller than the max MT message payload len)
  118. * @param offset - The offset into the image to start reading from
  119. *
  120. * @return status
  121. ***************************************************************************************************/
  122. uint8 MT_OtaFileReadReq(afAddrType_t *pAddr, zclOTA_FileID_t *pFileId, uint8 len, uint32 offset)
  123. {
  124. uint8 msgLen;
  125. uint8 *pBuf;
  126. uint8 *p;
  127. // Check if the requested length is longer than the RX receive buffer
  128. if (len + MT_OTA_FILE_READ_RSP_LEN + SPI_0DATA_MSG_LEN > MT_UART_RX_BUFF_MAX)
  129. return 0;
  130. // Get length
  131. msgLen = MT_OTA_FILE_READ_REQ_LEN;
  132. // Allocate a buffer
  133. if ((p = pBuf = MT_TransportAlloc(0, msgLen)) != NULL)
  134. {
  135. /* build header */
  136. *p++ = msgLen;
  137. *p++ = (uint8) MT_RPC_CMD_AREQ | (uint8) MT_RPC_SYS_OTA;
  138. *p++ = MT_OTA_FILE_READ_REQ;
  139. // Add the file ID
  140. p = OTA_FileIdToStream(pFileId, p);
  141. // Add the device address
  142. p = OTA_AfAddrToStream(pAddr, p);
  143. // File ofset to read from
  144. *p++ = BREAK_UINT32(offset, 0);
  145. *p++ = BREAK_UINT32(offset, 1);
  146. *p++ = BREAK_UINT32(offset, 2);
  147. *p++ = BREAK_UINT32(offset, 3);
  148. *p = len;
  149. // Send command to server
  150. MT_TransportSend(pBuf);
  151. return ZSuccess;
  152. }
  153. return ZMemError;
  154. }
  155. /***************************************************************************************************
  156. * @fn MT_OtaGetImage
  157. *
  158. * @brief Requests the next OTA image for a given device.
  159. *
  160. * @param pAddr - Address of the device requesting the image
  161. * @param pFileId - The file ID of the image currently on the device
  162. * @param hwVer - The hardware version of the device (optional)
  163. * @param ieee - The IEEE address of the device (optional)
  164. * @param options - The get image options
  165. *
  166. * @return Status
  167. *
  168. ***************************************************************************************************/
  169. uint8 MT_OtaGetImage(afAddrType_t *pAddr, zclOTA_FileID_t *pFileId, uint16 hwVer,
  170. uint8 *ieee, uint8 options)
  171. {
  172. uint8 msgLen;
  173. uint8 *pBuf;
  174. uint8 *p;
  175. // Get length
  176. msgLen = MT_OTA_GET_IMG_MSG_LEN;
  177. // Allocate a buffer
  178. if ((p = pBuf = MT_TransportAlloc(0, msgLen)) != NULL)
  179. {
  180. // build header
  181. *p++ = msgLen;
  182. *p++ = (uint8) MT_RPC_CMD_AREQ | (uint8) MT_RPC_SYS_OTA;
  183. *p++ = MT_OTA_NEXT_IMG_REQ;
  184. // Add the file ID
  185. p = OTA_FileIdToStream(pFileId, p);
  186. // Add the device address
  187. p = OTA_AfAddrToStream(pAddr, p);
  188. // Add the options
  189. *p++ = options;
  190. // Add the hardware ID (optional)
  191. *p++ = LO_UINT16(hwVer);
  192. *p = HI_UINT16(hwVer);
  193. if (ieee)
  194. osal_memcpy(p, ieee, Z_EXTADDR_LEN);
  195. // Send command to server
  196. MT_TransportSend(pBuf);
  197. return ZSuccess;
  198. }
  199. return ZMemError;
  200. }
  201. /***************************************************************************************************
  202. * @fn MT_OtaGetImage
  203. *
  204. * @brief Requests the next OTA image for a given device.
  205. *
  206. * @param shortAddr - Short Address of the device the status relates to
  207. * @param type - The type of status being reported
  208. * @param status - The status value
  209. * @param optional - Optional type specific additional information
  210. *
  211. * @return status
  212. *
  213. ***************************************************************************************************/
  214. uint8 MT_OtaSendStatus(uint16 shortAddr, uint8 type, uint8 status, uint8 optional)
  215. {
  216. uint8 msgLen;
  217. uint8 *pBuf;
  218. uint8 *p;
  219. // Get length
  220. msgLen = 7;
  221. // Allocate a buffer
  222. if ((p = pBuf = MT_TransportAlloc(0, msgLen)) != NULL)
  223. {
  224. // build header
  225. *p++ = msgLen;
  226. *p++ = (uint8) MT_RPC_CMD_AREQ | (uint8) MT_RPC_SYS_OTA;
  227. *p++ = MT_OTA_STATUS_IND;
  228. // Add message parameters
  229. *p++ = LO_UINT16(_NIB.nwkPanId);
  230. *p++ = HI_UINT16(_NIB.nwkPanId);
  231. *p++ = LO_UINT16(shortAddr);
  232. *p++ = HI_UINT16(shortAddr);
  233. *p++ = type;
  234. *p++ = status;
  235. *p = optional;
  236. // Send command to server
  237. MT_TransportSend(pBuf);
  238. return ZSuccess;
  239. }
  240. return ZMemError;
  241. }
  242. #endif // MT_OTA_FUNC