zmac.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /**************************************************************************************************
  2. Filename: zmac.c
  3. Revised: $Date: 2009-03-31 11:40:58 -0700 (Tue, 31 Mar 2009) $
  4. Revision: $Revision: 19611 $
  5. Description: This file contains the ZStack MAC Porting Layer
  6. Copyright 2005-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. /********************************************************************************************************
  34. * INCLUDES
  35. ********************************************************************************************************/
  36. #include "ZComDef.h"
  37. #include "OSAL.h"
  38. #include "ZMAC.h"
  39. #include "mac_main.h"
  40. #if !defined NONWK
  41. #include "ZGlobals.h"
  42. #endif
  43. /********************************************************************************************************
  44. * MACROS
  45. ********************************************************************************************************/
  46. /********************************************************************************************************
  47. * CONSTANTS
  48. ********************************************************************************************************/
  49. /********************************************************************************************************
  50. * GLOBALS
  51. ********************************************************************************************************/
  52. uint32 _ScanChannels;
  53. extern uint8 aExtendedAddress[];
  54. /********************************************************************************************************
  55. * LOCALS
  56. ********************************************************************************************************/
  57. /* Pointer to scan result buffer */
  58. void *ZMac_ScanBuf = NULL;
  59. /********************************************************************************************************
  60. * LOCAL FUNCTION PROTOTYPES
  61. ********************************************************************************************************/
  62. /********************************************************************************************************
  63. * TYPEDEFS
  64. ********************************************************************************************************/
  65. /********************************************************************************************************
  66. * FUNCTIONS
  67. ********************************************************************************************************/
  68. /********************************************************************************************************
  69. * @fn ZMacInit
  70. *
  71. * @brief Initialize MAC.
  72. *
  73. * @param none.
  74. *
  75. * @return status.
  76. ********************************************************************************************************/
  77. uint8 ZMacInit( void )
  78. {
  79. uint8 stat;
  80. MAC_Init();
  81. MAC_InitDevice();
  82. #if !defined NONWK
  83. if ( ZG_BUILD_RTR_TYPE )
  84. {
  85. MAC_InitCoord();
  86. }
  87. #endif
  88. // If OK, initialize the MAC
  89. stat = ZMacReset( TRUE );
  90. // Turn off interrupts
  91. osal_int_disable( INTS_ALL );
  92. return ( stat );
  93. }
  94. /********************************************************************************************************
  95. * @fn ZMacReset
  96. *
  97. * @brief Reset the MAC.
  98. *
  99. * @param Default to PIB defaults.
  100. *
  101. * @return status.
  102. ********************************************************************************************************/
  103. uint8 ZMacReset( bool SetDefaultPIB )
  104. {
  105. byte stat;
  106. byte value;
  107. stat = MAC_MlmeResetReq( SetDefaultPIB );
  108. // Don't send PAN ID conflict
  109. value = FALSE;
  110. MAC_MlmeSetReq( MAC_ASSOCIATED_PAN_COORD, &value );
  111. MAC_MlmeSetReq( MAC_EXTENDED_ADDRESS, &aExtendedAddress );
  112. if (ZMac_ScanBuf)
  113. {
  114. osal_mem_free(ZMac_ScanBuf);
  115. ZMac_ScanBuf = NULL;
  116. }
  117. return ( stat );
  118. }
  119. /********************************************************************************************************
  120. * @fn ZMacGetReq
  121. *
  122. * @brief Read a MAC PIB attribute.
  123. *
  124. * @param attr - PIB attribute to get
  125. * @param value - pointer to the buffer to store the attribute
  126. *
  127. * @return status
  128. ********************************************************************************************************/
  129. uint8 ZMacGetReq( uint8 attr, uint8 *value )
  130. {
  131. if ( attr == ZMacExtAddr )
  132. {
  133. osal_cpyExtAddr( value, &aExtendedAddress );
  134. return ZMacSuccess;
  135. }
  136. return (ZMacStatus_t) MAC_MlmeGetReq( attr, value );
  137. }
  138. /********************************************************************************************************
  139. * @fn ZMacSetReq
  140. *
  141. * @brief Write a MAC PIB attribute.
  142. *
  143. * @param attr - PIB attribute to Set
  144. * @param value - pointer to the data
  145. *
  146. * @return status
  147. ********************************************************************************************************/
  148. uint8 ZMacSetReq( uint8 attr, byte *value )
  149. {
  150. if ( attr == ZMacExtAddr )
  151. {
  152. osal_cpyExtAddr( aExtendedAddress, value );
  153. }
  154. return (ZMacStatus_t) MAC_MlmeSetReq( attr, value );
  155. }
  156. /********************************************************************************************************
  157. * @fn ZMacAssociateReq
  158. *
  159. * @brief Request an association with a coordinator.
  160. *
  161. * @param structure with info need to associate.
  162. *
  163. * @return status
  164. ********************************************************************************************************/
  165. uint8 ZMacAssociateReq( ZMacAssociateReq_t *pData )
  166. {
  167. /* Right now, set security to zero */
  168. pData->Sec.SecurityLevel = false;
  169. MAC_MlmeAssociateReq ( (macMlmeAssociateReq_t *)pData);
  170. return ( ZMacSuccess );
  171. }
  172. /********************************************************************************************************
  173. * @fn ZMacAssociateRsp
  174. *
  175. * @brief Request to send an association response message.
  176. *
  177. * @param structure with associate response and info needed to send it.
  178. *
  179. * @return status
  180. ********************************************************************************************************/
  181. uint8 ZMacAssociateRsp( ZMacAssociateRsp_t *pData )
  182. {
  183. /* Right now, set security to zero */
  184. pData->Sec.SecurityLevel = false;
  185. MAC_MlmeAssociateRsp( (macMlmeAssociateRsp_t *) pData );
  186. return ( ZMacSuccess );
  187. }
  188. /********************************************************************************************************
  189. * @fn ZMacDisassociateReq
  190. *
  191. * @brief Request to send a disassociate request message.
  192. *
  193. * @param structure with info need send it.
  194. *
  195. * @return status
  196. ********************************************************************************************************/
  197. uint8 ZMacDisassociateReq( ZMacDisassociateReq_t *pData )
  198. {
  199. /* Right now, set security to zero */
  200. pData->Sec.SecurityLevel = false;
  201. MAC_MlmeDisassociateReq( (macMlmeDisassociateReq_t *)pData);
  202. return ( ZMacSuccess );
  203. }
  204. /********************************************************************************************************
  205. * @fn ZMacOrphanRsp
  206. *
  207. * @brief Allows next higher layer to respond to an orphan indication message.
  208. *
  209. * @param structure with info need send it.
  210. *
  211. * @return status
  212. ********************************************************************************************************/
  213. uint8 ZMacOrphanRsp( ZMacOrphanRsp_t *pData )
  214. {
  215. /* Right now, set security to zero */
  216. pData->Sec.SecurityLevel = false;
  217. MAC_MlmeOrphanRsp( (macMlmeOrphanRsp_t *)pData);
  218. return ( ZMacSuccess );
  219. }
  220. /********************************************************************************************************
  221. * @fn ZMacScanReq
  222. *
  223. * @brief This function is called to perform a network scan.
  224. *
  225. * @param param - structure with info need send it.
  226. *
  227. * @return status
  228. ********************************************************************************************************/
  229. uint8 ZMacScanReq( ZMacScanReq_t *pData )
  230. {
  231. _ScanChannels = pData->ScanChannels;
  232. /* scan in progress */
  233. if (ZMac_ScanBuf != NULL)
  234. {
  235. return MAC_SCAN_IN_PROGRESS;
  236. }
  237. if (pData->ScanType != ZMAC_ORPHAN_SCAN)
  238. {
  239. /* Allocate memory depends on the scan type */
  240. if (pData->ScanType == ZMAC_ED_SCAN)
  241. {
  242. if ((ZMac_ScanBuf = osal_mem_alloc(ZMAC_ED_SCAN_MAXCHANNELS)) == NULL)
  243. {
  244. return MAC_NO_RESOURCES;
  245. }
  246. osal_memset(ZMac_ScanBuf, 0, ZMAC_ED_SCAN_MAXCHANNELS);
  247. pData->Result.pEnergyDetect = ((uint8*)ZMac_ScanBuf) + MAC_CHAN_11;
  248. }
  249. else if (pData->MaxResults > 0)
  250. {
  251. if ((ZMac_ScanBuf = pData->Result.pPanDescriptor =
  252. osal_mem_alloc( sizeof( ZMacPanDesc_t ) * pData->MaxResults )) == NULL)
  253. {
  254. return MAC_NO_RESOURCES;
  255. }
  256. }
  257. }
  258. /* Right now, set security to zero */
  259. pData->Sec.SecurityLevel = false;
  260. /* Channel Page */
  261. pData->ChannelPage = 0x00;
  262. MAC_MlmeScanReq ((macMlmeScanReq_t *)pData);
  263. return ZMacSuccess;
  264. }
  265. /********************************************************************************************************
  266. * @fn ZMacStartReq
  267. *
  268. * @brief This function is called to tell the MAC to transmit beacons
  269. * and become a coordinator.
  270. *
  271. * @param structure with info need send it.
  272. *
  273. * @return status
  274. ********************************************************************************************************/
  275. uint8 ZMacStartReq( ZMacStartReq_t *pData )
  276. {
  277. uint8 stat;
  278. // Probably want to keep the receiver on
  279. stat = true;
  280. MAC_MlmeSetReq( MAC_RX_ON_WHEN_IDLE, &stat );
  281. /* Right now, set security to zero */
  282. pData->RealignSec.SecurityLevel = false;
  283. pData->BeaconSec.SecurityLevel = false;
  284. MAC_MlmeStartReq((macMlmeStartReq_t *) pData);
  285. // MAC does not issue mlmeStartConfirm(), so we have to
  286. // mlmeStartConfirm( stat ); This needs to be addressed some how
  287. return ZMacSuccess;
  288. }
  289. /********************************************************************************************************
  290. * @fn ZMacSyncReq
  291. *
  292. * @brief This function is called to request a sync to the current
  293. * networks beacons.
  294. *
  295. * @param LogicalChannel -
  296. * @param TrackBeacon - true/false
  297. *
  298. * @return status
  299. ********************************************************************************************************/
  300. uint8 ZMacSyncReq( ZMacSyncReq_t *pData )
  301. {
  302. MAC_MlmeSyncReq( (macMlmeSyncReq_t *)pData);
  303. return ZMacSuccess;
  304. }
  305. /********************************************************************************************************
  306. * @fn ZMacPollReq
  307. *
  308. * @brief This function is called to request MAC data request poll.
  309. *
  310. * @param coordAddr -
  311. * @param coordPanId -
  312. * @param SecurityEnable - true or false.
  313. *
  314. * @return status
  315. ********************************************************************************************************/
  316. uint8 ZMacPollReq( ZMacPollReq_t *pData )
  317. {
  318. /* Right now, set security to zero */
  319. pData->Sec.SecurityLevel = false;
  320. MAC_MlmePollReq ((macMlmePollReq_t *)pData);
  321. return ( ZMacSuccess );
  322. }
  323. /********************************************************************************************************
  324. * @fn ZMacDataReq
  325. *
  326. * @brief Send a MAC Data Frame packet.
  327. *
  328. * @param structure containing data and where to send it.
  329. *
  330. * @return status
  331. ********************************************************************************************************/
  332. uint8 ZMacDataReq( ZMacDataReq_t *pData )
  333. {
  334. macMcpsDataReq_t *pBuf;
  335. /* Allocate memory */
  336. pBuf = MAC_McpsDataAlloc(pData->msduLength, MAC_SEC_LEVEL_NONE, MAC_KEY_ID_MODE_NONE);
  337. if (pBuf)
  338. {
  339. /* Copy the addresses */
  340. osal_memcpy (&pBuf->mac, pData, sizeof (macDataReq_t));
  341. /* Copy data */
  342. pBuf->msdu.len = pData->msduLength;
  343. osal_memcpy (pBuf->msdu.p, pData->msdu, pData->msduLength);
  344. /* Right now, set security to zero */
  345. pBuf->sec.securityLevel = false;
  346. /* Call Mac Data Request */
  347. MAC_McpsDataReq(pBuf);
  348. return ( ZMacSuccess );
  349. }
  350. return MAC_NO_RESOURCES;
  351. }
  352. /********************************************************************************************************
  353. * @fn ZMacPurgeReq
  354. *
  355. * @brief Purge a MAC Data Frame packet.
  356. *
  357. * @param MSDU data handle.
  358. *
  359. * @return status
  360. ********************************************************************************************************/
  361. uint8 ZMacPurgeReq( byte Handle )
  362. {
  363. MAC_McpsPurgeReq( Handle );
  364. return ZMacSuccess;
  365. }
  366. /********************************************************************************************************
  367. * @fn ZMacSrcMatchEnable
  368. *
  369. * @brief This function is call to enable AUTOPEND and source address matching.
  370. *
  371. * @param addressType - address type that the application uses
  372. * SADDR_MODE_SHORT or SADDR_MODE_EXT.
  373. * numEntries - number of source address table entries to be used
  374. *
  375. * @return status
  376. ********************************************************************************************************/
  377. ZMacStatus_t ZMacSrcMatchEnable (uint8 addrType, uint8 numEntries)
  378. {
  379. return (MAC_SrcMatchEnable(addrType, numEntries));
  380. }
  381. /********************************************************************************************************
  382. * @fn ZMacSrcMatchAddEntry
  383. *
  384. * @brief This function is called to add a short or extended address to source address table.
  385. *
  386. * @param addr - a pointer to sAddr_t which contains addrMode
  387. * and a union of a short 16-bit MAC address or an extended
  388. * 64-bit MAC address to be added to the source address table.
  389. * panID - the device PAN ID. It is only used when the addr is
  390. * using short address
  391. *
  392. * @return status
  393. ********************************************************************************************************/
  394. ZMacStatus_t ZMacSrcMatchAddEntry (zAddrType_t *addr, uint16 panID)
  395. {
  396. return (MAC_SrcMatchAddEntry ((sAddr_t*)addr, panID));
  397. }
  398. /********************************************************************************************************
  399. * @fn ZMacSrcMatchDeleteEntry
  400. *
  401. * @brief This function is called to delete a short or extended address from source address table.
  402. *
  403. * @param addr - a pointer to sAddr_t which contains addrMode
  404. * and a union of a short 16-bit MAC address or an extended
  405. * 64-bit MAC address to be added to the source address table.
  406. * panID - the device PAN ID. It is only used when the addr is
  407. * using short address
  408. *
  409. * @return status
  410. ********************************************************************************************************/
  411. ZMacStatus_t ZMacSrcMatchDeleteEntry (zAddrType_t *addr, uint16 panID)
  412. {
  413. return (MAC_SrcMatchDeleteEntry ((sAddr_t*)addr, panID));
  414. }
  415. /********************************************************************************************************
  416. * @fn ZMacSrcMatchAckAllPending
  417. *
  418. * @brief Enabled/disable acknowledging all packets with pending bit set
  419. * It is normally enabled when adding new entries to
  420. * the source address table fails due to the table is full, or
  421. * disabled when more entries are deleted and the table has
  422. * empty slots.
  423. *
  424. * @param option - true (acknowledging all packets with pending field set)
  425. * false (acknowledging all packets with pending field cleared)
  426. *
  427. * @return status
  428. ********************************************************************************************************/
  429. ZMacStatus_t ZMacSrcMatchAckAllPending (uint8 option)
  430. {
  431. MAC_SrcMatchAckAllPending (option);
  432. return ZMacSuccess;
  433. }
  434. /********************************************************************************************************
  435. * @fn ZMacSrcMatchCheckAllPending
  436. *
  437. * @brief This function is called to check if acknowledging all packets with pending bit set is enabled.
  438. *
  439. * @param none
  440. *
  441. * @return status
  442. ********************************************************************************************************/
  443. ZMacStatus_t ZMacSrcMatchCheckAllPending (void)
  444. {
  445. return (MAC_SrcMatchCheckAllPending ());
  446. }
  447. /********************************************************************************************************
  448. * @fn - ZMACPwrOnReq
  449. *
  450. * @brief - This function requests the MAC to power on the radio hardware
  451. * and wake up. When the power on procedure is complete the MAC
  452. * will send a MAC_PWR_ON_CNF to the application.
  453. *
  454. * @input - None.
  455. *
  456. * @output - None.
  457. *
  458. * @return - None.
  459. ********************************************************************************************************/
  460. void ZMacPwrOnReq ( void )
  461. {
  462. MAC_PwrOnReq();
  463. }
  464. /********************************************************************************************************
  465. * @fn MAC_PwrMode
  466. *
  467. * @brief This function returns the current power mode of the MAC.
  468. *
  469. * input parameters
  470. *
  471. * None.
  472. *
  473. * output parameters
  474. *
  475. * None.
  476. *
  477. * @return The current power mode of the MAC.
  478. ********************************************************************************************************/
  479. uint8 ZMac_PwrMode(void)
  480. {
  481. return (MAC_PwrMode());
  482. }
  483. /********************************************************************************************************
  484. * @fn ZMacSetTransmitPower
  485. *
  486. * @brief Set the transmitter power according to the level setting param.
  487. *
  488. * @param Valid power level setting as defined in ZMAC.h.
  489. *
  490. * @return ZMacSuccess if PHY_TRANSMIT_POWER found or ZMacUnsupportedAttribute.
  491. ********************************************************************************************************/
  492. uint8 ZMacSetTransmitPower( ZMacTransmitPower_t level )
  493. {
  494. return MAC_MlmeSetReq( ZMacPhyTransmitPower, &level );
  495. }
  496. /********************************************************************************************************
  497. * @fn ZMacSendNoData
  498. *
  499. * @brief This function sends an empty msg
  500. *
  501. * @param DstAddr - destination short address
  502. * DstPANId - destination pan id
  503. *
  504. * @return None
  505. ********************************************************************************************************/
  506. void ZMacSendNoData ( uint16 DstAddr, uint16 DstPANId )
  507. {
  508. macMcpsDataReq_t *pBuf;
  509. /* Allocate memory */
  510. pBuf = MAC_McpsDataAlloc(0, MAC_SEC_LEVEL_NONE, MAC_KEY_ID_MODE_NONE);
  511. if (pBuf)
  512. {
  513. /* Fill in src information */
  514. pBuf->mac.srcAddrMode = SADDR_MODE_SHORT;
  515. /* Fill in dst information */
  516. pBuf->mac.dstAddr.addr.shortAddr = DstAddr;
  517. pBuf->mac.dstAddr.addrMode = SADDR_MODE_SHORT;
  518. pBuf->mac.dstPanId = DstPANId;
  519. /* Misc information */
  520. pBuf->mac.msduHandle = 0;
  521. pBuf->mac.txOptions = ZMAC_TXOPTION_ACK | ZMAC_TXOPTION_NO_RETRANS | ZMAC_TXOPTION_NO_CNF;
  522. /* Right now, set security to zero */
  523. pBuf->sec.securityLevel = false;
  524. /* Call Mac Data Request */
  525. MAC_McpsDataReq(pBuf);
  526. }
  527. }
  528. /********************************************************************************************************
  529. * @fn ZMacStateIdle
  530. *
  531. * @brief This function returns true if the MAC state is idle.
  532. *
  533. * @param none
  534. *
  535. * @return TRUE if the MAC state is idle, FALSE otherwise.
  536. ********************************************************************************************************/
  537. uint8 ZMacStateIdle( void )
  538. {
  539. return macStateIdle();
  540. }