MT_TASK.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /***************************************************************************************************
  2. Filename: MT_TASK.c
  3. Revised: $Date: 2011-06-01 14:52:32 -0700 (Wed, 01 Jun 2011) $
  4. Revision: $Revision: 26173 $
  5. Description:
  6. MonitorTest Task handling routines
  7. Copyright 2007-2011 Texas Instruments Incorporated. All rights reserved.
  8. IMPORTANT: Your use of this Software is limited to those specific rights
  9. granted under the terms of a software license agreement between the user
  10. who downloaded the software, his/her employer (which must be your employer)
  11. and Texas Instruments Incorporated (the "License"). You may not use this
  12. Software unless you agree to abide by the terms of the License. The License
  13. limits your use, and you acknowledge, that the Software may not be modified,
  14. copied or distributed unless embedded on a Texas Instruments microcontroller
  15. or used solely and exclusively in conjunction with a Texas Instruments radio
  16. frequency transceiver, which is integrated into your product. Other than for
  17. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  18. works of, modify, distribute, perform, display or sell this Software and/or
  19. its documentation for any purpose.
  20. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  21. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  22. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  23. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  24. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  25. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  26. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  27. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  28. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  29. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  30. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  31. Should you have any questions regarding your right to use this Software,
  32. contact Texas Instruments Incorporated at www.TI.com.
  33. ***************************************************************************************************/
  34. /***************************************************************************************************
  35. * INCLUDES
  36. ***************************************************************************************************/
  37. #include "ZComDef.h"
  38. #include "MT_TASK.h"
  39. #include "MT.h"
  40. #include "MT_DEBUG.h"
  41. #include "MT_UART.h"
  42. #include "MT_UTIL.h"
  43. #include "MT_SYS.h"
  44. #if !defined( NONWK )
  45. #include "MT_ZDO.h"
  46. #include "MT_AF.h"
  47. #endif /* NONWK */
  48. #include "mt_x.h"
  49. #include "hal_uart.h"
  50. #include "OSAL_Memory.h"
  51. /***************************************************************************************************
  52. * LOCAL FUNCTIONS
  53. ***************************************************************************************************/
  54. void MT_ProcessIncomingCommand( mtOSALSerialData_t *msg );
  55. /***************************************************************************************************
  56. * GLOBALS
  57. ***************************************************************************************************/
  58. uint8 MT_TaskID;
  59. /***************************************************************************************************
  60. * @fn MT_TaskInit
  61. *
  62. * @brief MonitorTest Task Initialization. This function is put into the
  63. * task table.
  64. *
  65. * @param task_id - task ID of the MT Task
  66. *
  67. * @return void
  68. ***************************************************************************************************/
  69. void MT_TaskInit(uint8 task_id)
  70. {
  71. MT_TaskID = task_id;
  72. /* Initialize the Serial port */
  73. MT_UartInit();
  74. /* Register taskID - Do this after UartInit() because it will reset the taskID */
  75. MT_UartRegisterTaskID(task_id);
  76. osal_set_event(task_id, MT_SECONDARY_INIT_EVENT);
  77. }
  78. /**************************************************************************************************
  79. * @fn MT_ProcessEvent
  80. *
  81. * @brief MonitorTest Task Event Processor. This task is put into the task table.
  82. *
  83. * @param task_id - task ID of the MT Task
  84. * @param events - event(s) for the MT Task
  85. *
  86. * @return Bit mask of the unprocessed MT Task events.
  87. **************************************************************************************************/
  88. UINT16 MT_ProcessEvent(uint8 task_id, uint16 events)
  89. {
  90. if ( events & SYS_EVENT_MSG )
  91. {
  92. uint8 *msg_ptr;
  93. while ( (msg_ptr = osal_msg_receive( task_id )) )
  94. {
  95. MT_ProcessIncomingCommand((mtOSALSerialData_t *)msg_ptr);
  96. }
  97. return (events ^ SYS_EVENT_MSG);
  98. }
  99. if ( events & MT_SECONDARY_INIT_EVENT )
  100. {
  101. MT_Init();
  102. }
  103. #if defined MT_SYS_FUNC
  104. if ( events & (MT_SYS_OSAL_EVENT_MASK))
  105. {
  106. if (events & MT_SYS_OSAL_EVENT_0)
  107. {
  108. MT_SysOsalTimerExpired(0x00);
  109. }
  110. if (events & MT_SYS_OSAL_EVENT_1)
  111. {
  112. MT_SysOsalTimerExpired(0x01);
  113. }
  114. if (events & MT_SYS_OSAL_EVENT_2)
  115. {
  116. MT_SysOsalTimerExpired(0x02);
  117. }
  118. if (events & MT_SYS_OSAL_EVENT_3)
  119. {
  120. MT_SysOsalTimerExpired(0x03);
  121. }
  122. }
  123. #endif
  124. return 0;
  125. }
  126. /***************************************************************************************************
  127. * @fn MT_ProcessIncomingCommand
  128. *
  129. * @brief
  130. *
  131. * Process Event Messages.
  132. *
  133. * @param *msg - pointer to event message
  134. *
  135. * @return
  136. ***************************************************************************************************/
  137. void MT_ProcessIncomingCommand( mtOSALSerialData_t *msg )
  138. {
  139. uint8 deallocate;
  140. uint8 *msg_ptr;
  141. uint8 len;
  142. /* A little setup for AF, CB_FUNC and MT_SYS_APP_RSP_MSG */
  143. msg_ptr = msg->msg;
  144. deallocate = true;
  145. /* Use the first byte of the message as the command ID */
  146. switch ( msg->hdr.event )
  147. {
  148. case CMD_SERIAL_MSG:
  149. MT_ProcessIncoming(msg->msg);
  150. break;
  151. case CMD_DEBUG_MSG:
  152. MT_ProcessDebugMsg( (mtDebugMsg_t *)msg );
  153. break;
  154. case CB_FUNC:
  155. /*
  156. Build SPI message here instead of redundantly calling MT_BuildSPIMsg
  157. because we have copied data already in the allocated message
  158. */
  159. /* msg_ptr is the beginning of the intended SPI message */
  160. len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];
  161. /*
  162. FCS goes to the last byte in the message and is calculated over all
  163. the bytes except FCS and SOP
  164. */
  165. msg_ptr[len-1] = MT_UartCalcFCS(msg_ptr + 1, (uint8)(len-2));
  166. #ifdef MT_UART_DEFAULT_PORT
  167. HalUARTWrite ( MT_UART_DEFAULT_PORT, msg_ptr, len );
  168. #endif
  169. break;
  170. case CMD_DEBUG_STR:
  171. MT_ProcessDebugStr( (mtDebugStr_t *)msg );
  172. break;
  173. #if !defined ( NONWK )
  174. case MT_SYS_APP_RSP_MSG:
  175. len = SPI_0DATA_MSG_LEN + msg_ptr[DATALEN_FIELD];
  176. MTProcessAppRspMsg( msg_ptr, len );
  177. break;
  178. #endif // NONWK
  179. #if defined (MT_UTIL_FUNC)
  180. #if defined ZCL_KEY_ESTABLISH
  181. case ZCL_KEY_ESTABLISH_IND:
  182. MT_UtilKeyEstablishInd((keyEstablishmentInd_t *)msg);
  183. break;
  184. #endif
  185. #endif
  186. #ifdef MT_ZDO_CB_FUNC
  187. case ZDO_STATE_CHANGE:
  188. MT_ZdoStateChangeCB((osal_event_hdr_t *)msg);
  189. break;
  190. #endif
  191. default:
  192. break;
  193. }
  194. if ( deallocate )
  195. {
  196. osal_msg_deallocate( (uint8 *)msg );
  197. }
  198. }
  199. #ifdef MT_TASK
  200. /***************************************************************************************************
  201. * @fn MT_TransportAlloc
  202. *
  203. * @brief Allocate memory for transport msg
  204. *
  205. * @param uint8 cmd0 - The first byte of the MT command id containing the command type and subsystem.
  206. * uint8 len - length
  207. *
  208. * @return pointer the allocated memory or NULL if fail to allocate the memory
  209. ***************************************************************************************************/
  210. uint8 *MT_TransportAlloc(uint8 cmd0, uint8 len)
  211. {
  212. uint8 *p;
  213. (void)cmd0; // Intentionally unreferenced parameter
  214. /* Allocate a buffer of data length + SOP+CMD+FCS (5 bytes) */
  215. p = osal_msg_allocate(len + SPI_0DATA_MSG_LEN);
  216. if (p)
  217. {
  218. p++; /* Save space for SOP_VALUE, msg structure */
  219. return p;
  220. }
  221. else
  222. {
  223. return NULL;
  224. }
  225. }
  226. /***************************************************************************************************
  227. * @fn MT_TransportSend
  228. *
  229. * @brief Fill in SOP and FCS then send out the msg
  230. *
  231. * @param uint8 *pBuf - pointer to the message that contains CMD, length, data and FCS
  232. *
  233. * @return None
  234. ***************************************************************************************************/
  235. void MT_TransportSend(uint8 *pBuf)
  236. {
  237. MT_X_TransportSend(pBuf-1);
  238. }
  239. #endif /* MT_TASK */
  240. /***************************************************************************************************
  241. ***************************************************************************************************/