zmac_cb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /**************************************************************************************************
  2. Filename: zmac_cb.c
  3. Revised: $Date: 2009-03-06 14:52:01 -0800 (Fri, 06 Mar 2009) $
  4. Revision: $Revision: 19326 $
  5. Description: This file contains the NWK functions that the ZMAC calls
  6. Copyright 2005-2009 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. /********************************************************************************************************
  34. * INCLUDES
  35. ********************************************************************************************************/
  36. #include "ZComDef.h"
  37. #include "OSAL.h"
  38. #include "ZMAC.h"
  39. #include "MT_MAC.h"
  40. #include "hal_mcu.h"
  41. #if !defined NONWK
  42. #include "nwk.h"
  43. #include "nwk_bufs.h"
  44. #include "ZGlobals.h"
  45. #endif
  46. #if defined( MACSIM )
  47. #include "mac_sim.h"
  48. #endif
  49. #include "mac_main.h"
  50. extern void *ZMac_ScanBuf;
  51. /********************************************************************************************************
  52. * CONSTANTS
  53. ********************************************************************************************************/
  54. #if !defined NONWK
  55. /* Lookup table for size of structures. Must match with the order of MAC callback events */
  56. const uint8 CODE zmacCBSizeTable [] = {
  57. 0,
  58. sizeof(ZMacAssociateInd_t), // MAC_MLME_ASSOCIATE_IND 1 Associate indication
  59. sizeof(ZMacAssociateCnf_t), // MAC_MLME_ASSOCIATE_CNF 2 Associate confirm
  60. 0, // MAC_MLME_DISASSOCIATE_IND 3 Disassociate indication
  61. 0, // MAC_MLME_DISASSOCIATE_CNF 4 Disassociate confirm
  62. sizeof(macMlmeBeaconNotifyInd_t), // MAC_MLME_BEACON_NOTIFY_IND 5 con notify indication
  63. sizeof(ZMacOrphanInd_t), // MAC_MLME_ORPHAN_IND 6 Orphan indication
  64. sizeof(ZMacScanCnf_t), // MAC_MLME_SCAN_CNF 7 Scan confirm
  65. sizeof(ZMacStartCnf_t), // MAC_MLME_START_CNF 8 Start confirm
  66. 0, // MAC_MLME_SYNC_LOSS_IND 9 Sync loss indication
  67. sizeof(ZMacPollCnf_t), // MAC_MLME_POLL_CNF 10 Poll confirm
  68. sizeof(ZMacCommStatusInd_t), // MAC_MLME_COMM_STATUS_IND 11 Comm status indication
  69. sizeof(ZMacDataCnf_t), // MAC_MCPS_DATA_CNF 12 Data confirm
  70. sizeof(macMcpsDataInd_t), // MAC_MCPS_DATA_IND 13 Data indication
  71. 0, // MAC_MCPS_PURGE_CNF 14 Purge confirm
  72. 0, // MAC_PWR_ON_CNF 15 Power on confirm
  73. sizeof(ZMacPollInd_t) // MAC_MLME_POLL_IND 16 Poll indication
  74. };
  75. #endif /* !defined NONWK */
  76. /*********************************************************************
  77. * ZMAC Function Pointers
  78. */
  79. /*
  80. * ZMac Application callback function. This function will be called
  81. * for every MAC message that is received over-the-air or generated
  82. * locally by MAC for the application.
  83. *
  84. * The callback function should return TRUE if it has handled the
  85. * MAC message and no further action should be taken with it. It
  86. * should return FALSE if it has not handled the MAC message and
  87. * normal processing should take place.
  88. *
  89. * NOTE: The processing in this function should be kept to the
  90. * minimum.
  91. */
  92. uint8 (*pZMac_AppCallback)( uint8 *msgPtr ) = (void*)NULL;
  93. /*********************************************************************
  94. * ZMAC Functions
  95. */
  96. /**************************************************************************************************
  97. * @fn MAC_CbackEvent()
  98. *
  99. * @brief convert MAC data confirm and indication to ZMac and send to NWK
  100. *
  101. * @param pData - pointer to macCbackEvent_t
  102. *
  103. * @return none
  104. *************************************************************************************************/
  105. void MAC_CbackEvent(macCbackEvent_t *pData)
  106. #ifndef MT_MAC_CB_FUNC
  107. {
  108. #if !defined NONWK
  109. uint8 event = pData->hdr.event;
  110. uint16 tmp = zmacCBSizeTable[event];
  111. macCbackEvent_t *msgPtr;
  112. /* If the Network layer will handle a new MAC callback, a non-zero value must be entered in the
  113. * corresponding location in the zmacCBSizeTable[] - thus the table acts as "should handle"?
  114. */
  115. if (tmp == 0)
  116. {
  117. return;
  118. }
  119. // MAC_MCPS_DATA_IND is very special - it is the only event where the MAC does not free *pData.
  120. if ( event == MAC_MCPS_DATA_IND )
  121. {
  122. MAC_MlmeGetReq( MAC_SHORT_ADDRESS, &tmp );
  123. if ((tmp == INVALID_NODE_ADDR) || (tmp == NWK_BROADCAST_SHORTADDR_DEVALL) ||
  124. (pData->dataInd.msdu.len == 0))
  125. {
  126. osal_msg_deallocate( (uint8 *)pData );
  127. return;
  128. }
  129. msgPtr = pData;
  130. }
  131. else
  132. {
  133. if (event == MAC_MLME_BEACON_NOTIFY_IND )
  134. {
  135. tmp += sizeof(macPanDesc_t) + pData->beaconNotifyInd.sduLength;
  136. }
  137. else if (event == MAC_MLME_SCAN_CNF)
  138. {
  139. if (pData->scanCnf.scanType == ZMAC_ED_SCAN)
  140. {
  141. tmp += ZMAC_ED_SCAN_MAXCHANNELS;
  142. }
  143. else
  144. {
  145. tmp += sizeof( ZMacPanDesc_t ) * pData->scanCnf.resultListSize;
  146. }
  147. }
  148. if ( !(msgPtr = (macCbackEvent_t *)osal_msg_allocate(tmp)) )
  149. {
  150. // Not enough memory. If data confirm - try again
  151. if ( event == MAC_MCPS_DATA_CNF )
  152. {
  153. halIntState_t intState;
  154. // This is not normally deallocated here because the pZMac_AppCallback()
  155. // application may need it.
  156. HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts.
  157. /* mark MAC data buffer as reserved so that MAC won't deallocate it */
  158. macMainReserve( (uint8 *)pData->dataCnf.pDataReq );
  159. osal_msg_deallocate( (uint8*)pData->dataCnf.pDataReq );
  160. if ( !(msgPtr = (macCbackEvent_t *)osal_msg_allocate(tmp)) )
  161. {
  162. // Still no allocation, something is wrong
  163. HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts.
  164. return;
  165. }
  166. HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts.
  167. pData->dataCnf.pDataReq = NULL;
  168. }
  169. else
  170. {
  171. // This message is dropped
  172. return;
  173. }
  174. }
  175. osal_memcpy(msgPtr, pData, zmacCBSizeTable[event]);
  176. }
  177. if ( event == MAC_MLME_BEACON_NOTIFY_IND )
  178. {
  179. macMlmeBeaconNotifyInd_t *pBeacon = (macMlmeBeaconNotifyInd_t*)msgPtr;
  180. osal_memcpy(pBeacon+1, pBeacon->pPanDesc, sizeof(macPanDesc_t));
  181. pBeacon->pPanDesc = (macPanDesc_t *)(pBeacon+1);
  182. osal_memcpy(pBeacon->pPanDesc+1, pBeacon->pSdu, pBeacon->sduLength);
  183. pBeacon->pSdu = (uint8 *)(pBeacon->pPanDesc+1);
  184. }
  185. else if (event == MAC_MLME_SCAN_CNF)
  186. {
  187. macMlmeScanCnf_t *pScan = (macMlmeScanCnf_t*)msgPtr;
  188. if (ZMac_ScanBuf != NULL)
  189. {
  190. if (pScan->scanType == ZMAC_ED_SCAN)
  191. {
  192. pScan->result.pEnergyDetect = (uint8*) (pScan + 1);
  193. osal_memcpy(pScan->result.pEnergyDetect, ZMac_ScanBuf, ZMAC_ED_SCAN_MAXCHANNELS);
  194. }
  195. else
  196. {
  197. pScan->result.pPanDescriptor = (macPanDesc_t*) (pScan + 1);
  198. osal_memcpy(pScan + 1, ZMac_ScanBuf, sizeof( ZMacPanDesc_t ) * pScan->resultListSize);
  199. }
  200. osal_mem_free(ZMac_ScanBuf);
  201. ZMac_ScanBuf = NULL;
  202. }
  203. }
  204. if ( ( pZMac_AppCallback == NULL ) || ( pZMac_AppCallback( (uint8 *)msgPtr ) == FALSE ) )
  205. {
  206. // Application hasn't already processed this message. Send it to NWK task.
  207. osal_msg_send( NWK_TaskID, (uint8 *)msgPtr );
  208. }
  209. if ( event == MAC_MCPS_DATA_CNF )
  210. {
  211. // If the application needs 'pDataReq' then we cannot free it here.
  212. // The application must free it after using it. Note that 'pDataReq'
  213. // is of macMcpsDataReq_t (and not ZMacDataReq_t) type.
  214. /* mark MAC data buffer as reserved so that MAC won't deallocate it */
  215. macMainReserve( (uint8 *)pData->dataCnf.pDataReq );
  216. osal_msg_deallocate( (uint8*)pData->dataCnf.pDataReq );
  217. }
  218. #endif
  219. }
  220. #else // ifdef MT_MAC_CB_FUNC
  221. {
  222. /* Check if MT has subscribed for this callback If so, pass it as an event to MonitorTest */
  223. switch (pData->hdr.event)
  224. {
  225. case MAC_MLME_ASSOCIATE_IND:
  226. if ( _macCallbackSub & CB_ID_NWK_ASSOCIATE_IND )
  227. nwk_MTCallbackSubNwkAssociateInd ( (ZMacAssociateInd_t *)pData );
  228. break;
  229. case MAC_MLME_ASSOCIATE_CNF:
  230. if ( _macCallbackSub & CB_ID_NWK_ASSOCIATE_CNF )
  231. nwk_MTCallbackSubNwkAssociateCnf ( (ZMacAssociateCnf_t *)pData );
  232. break;
  233. case MAC_MLME_DISASSOCIATE_IND:
  234. if ( _macCallbackSub & CB_ID_NWK_DISASSOCIATE_IND )
  235. nwk_MTCallbackSubNwkDisassociateInd ( (ZMacDisassociateInd_t *)pData );
  236. break;
  237. case MAC_MLME_DISASSOCIATE_CNF:
  238. if ( _macCallbackSub & CB_ID_NWK_DISASSOCIATE_CNF )
  239. nwk_MTCallbackSubNwkDisassociateCnf ( (ZMacDisassociateCnf_t *)pData );
  240. break;
  241. case MAC_MLME_BEACON_NOTIFY_IND:
  242. if ( _macCallbackSub & CB_ID_NWK_BEACON_NOTIFY_IND )
  243. nwk_MTCallbackSubNwkBeaconNotifyInd( (ZMacBeaconNotifyInd_t *)pData );
  244. break;
  245. case MAC_MLME_ORPHAN_IND:
  246. if ( _macCallbackSub & CB_ID_NWK_ORPHAN_IND )
  247. nwk_MTCallbackSubNwkOrphanInd( (ZMacOrphanInd_t *) pData );
  248. break;
  249. case MAC_MLME_SCAN_CNF:
  250. if ( _macCallbackSub & CB_ID_NWK_SCAN_CNF )
  251. {
  252. pData->scanCnf.result.pEnergyDetect = ZMac_ScanBuf;
  253. nwk_MTCallbackSubNwkScanCnf ( (ZMacScanCnf_t *) pData );
  254. }
  255. if (ZMac_ScanBuf != NULL)
  256. {
  257. osal_mem_free(ZMac_ScanBuf);
  258. ZMac_ScanBuf = NULL;
  259. }
  260. break;
  261. case MAC_MLME_START_CNF:
  262. if ( _macCallbackSub & CB_ID_NWK_START_CNF )
  263. nwk_MTCallbackSubNwkStartCnf ( pData->hdr.status );
  264. break;
  265. case MAC_MLME_SYNC_LOSS_IND:
  266. if ( _macCallbackSub & CB_ID_NWK_SYNC_LOSS_IND )
  267. nwk_MTCallbackSubNwkSyncLossInd( (ZMacSyncLossInd_t *) pData );
  268. break;
  269. case MAC_MLME_POLL_CNF:
  270. if ( _macCallbackSub & CB_ID_NWK_POLL_CNF )
  271. nwk_MTCallbackSubNwkPollCnf( pData->hdr.status );
  272. break;
  273. case MAC_MLME_COMM_STATUS_IND:
  274. if ( _macCallbackSub & CB_ID_NWK_COMM_STATUS_IND )
  275. nwk_MTCallbackSubCommStatusInd ( (ZMacCommStatusInd_t *) pData );
  276. break;
  277. case MAC_MCPS_DATA_CNF:
  278. /* mark MAC data buffer as reserved so that MAC won't deallocate it */
  279. macMainReserve( (uint8 *)pData->dataCnf.pDataReq );
  280. osal_msg_deallocate((uint8*)pData->dataCnf.pDataReq);
  281. if ( _macCallbackSub & CB_ID_NWK_DATA_CNF )
  282. nwk_MTCallbackSubNwkDataCnf( (ZMacDataCnf_t *) pData );
  283. break;
  284. case MAC_MCPS_DATA_IND:
  285. {
  286. /*
  287. Data Ind is unconventional: to save an alloc/copy, reuse the MAC
  288. buffer and re-organize the contents into ZMAC format.
  289. */
  290. ZMacDataInd_t *pDataInd = (ZMacDataInd_t *) pData;
  291. uint8 event, status, len, *msdu;
  292. /* Store parameters */
  293. event = pData->hdr.event;
  294. status = pData->hdr.status;
  295. len = pData->dataInd.msdu.len;
  296. msdu = pData->dataInd.msdu.p;
  297. /* Copy header */
  298. osal_memcpy(&pDataInd->SrcAddr, &pData->dataInd.mac, sizeof(ZMacDataInd_t) - sizeof(ZMacEventHdr_t));
  299. /* Security - set to zero for now*/
  300. pDataInd->Sec.SecurityLevel = false;
  301. /* Restore parameters */
  302. pDataInd->hdr.Status = status;
  303. pDataInd->hdr.Event = event;
  304. pDataInd->msduLength = len;
  305. if (len)
  306. pDataInd->msdu = msdu;
  307. else
  308. pDataInd->msdu = NULL;
  309. if ( _macCallbackSub & CB_ID_NWK_DATA_IND )
  310. nwk_MTCallbackSubNwkDataInd ( pDataInd );
  311. /* free buffer */
  312. osal_msg_deallocate( (uint8 *) pData );
  313. }
  314. break;
  315. case MAC_MCPS_PURGE_CNF:
  316. if ( _macCallbackSub & CB_ID_NWK_PURGE_CNF )
  317. nwk_MTCallbackSubNwkPurgeCnf( (ZMacPurgeCnf_t *) pData);
  318. break;
  319. default:
  320. break;
  321. }
  322. }
  323. #endif
  324. /********************************************************************************************************
  325. * @fn MAC_CbackCheckPending
  326. *
  327. * @brief Return number of pending indirect msg
  328. *
  329. * @param None
  330. *
  331. * @return Number of indirect msg holding
  332. ********************************************************************************************************/
  333. uint8 MAC_CbackCheckPending(void)
  334. {
  335. #if !defined (NONWK)
  336. if ( ZSTACK_ROUTER_BUILD )
  337. {
  338. return (nwkDB_ReturnIndirectHoldingCnt());
  339. }
  340. else
  341. {
  342. return (0);
  343. }
  344. #else
  345. return (0);
  346. #endif
  347. }