mac_pib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /**************************************************************************************************
  2. Filename: mac_pib.c
  3. Revised: $Date: 2012-02-15 12:54:07 -0800 (Wed, 15 Feb 2012) $
  4. Revision: $Revision: 29306 $
  5. Description: This module contains procedures for the MAC PIB.
  6. Copyright 2006-2012 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. */
  37. #include "hal_mcu.h"
  38. #include "hal_board.h"
  39. #include "mac_api.h"
  40. #include "mac_spec.h"
  41. #include "mac_low_level.h"
  42. #include "mac_radio_defs.h"
  43. #include "mac_main.h"
  44. #include "mac_pib.h"
  45. #include "OSAL.h"
  46. #include <stddef.h>
  47. /* ------------------------------------------------------------------------------------------------
  48. * Constants
  49. * ------------------------------------------------------------------------------------------------
  50. */
  51. /* Attribute index constants, based on attribute ID values from spec */
  52. #define MAC_ATTR_SET1_START 0x40
  53. #define MAC_ATTR_SET1_END 0x5D
  54. #define MAC_ATTR_SET1_OFFSET 0
  55. #define MAC_ATTR_SET2_START 0xE0
  56. #define MAC_ATTR_SET2_END 0xE5
  57. #define MAC_ATTR_SET2_OFFSET (MAC_ATTR_SET1_END - MAC_ATTR_SET1_START + MAC_ATTR_SET1_OFFSET + 1)
  58. /* frame response values */
  59. #define MAC_MAX_FRAME_RESPONSE_MIN 143
  60. #define MAC_MAX_FRAME_RESPONSE_MAX 25776
  61. /* ------------------------------------------------------------------------------------------------
  62. * Typedefs
  63. * ------------------------------------------------------------------------------------------------
  64. */
  65. /* PIB access and min/max table type */
  66. typedef struct
  67. {
  68. uint8 offset;
  69. uint8 len;
  70. uint8 min;
  71. uint8 max;
  72. } macPibTbl_t;
  73. /* ------------------------------------------------------------------------------------------------
  74. * Local Variables
  75. * ------------------------------------------------------------------------------------------------
  76. */
  77. /* PIB default values */
  78. static CODE const macPib_t macPibDefaults =
  79. {
  80. 54, /* ackWaitDuration */
  81. FALSE, /* associationPermit */
  82. TRUE, /* autoRequest */
  83. FALSE, /* battLifeExt */
  84. 6, /* battLifeExtPeriods */
  85. NULL, /* *pMacBeaconPayload */
  86. 0, /* beaconPayloadLength */
  87. MAC_BO_NON_BEACON, /* beaconOrder */
  88. 0, /* beaconTxTime */
  89. 0, /* bsn */
  90. {0, SADDR_MODE_EXT}, /* coordExtendedAddress */
  91. MAC_SHORT_ADDR_NONE, /* coordShortAddress */
  92. 0, /* dsn */
  93. FALSE, /* gtsPermit */
  94. 4, /* maxCsmaBackoffs */
  95. 3, /* minBe */
  96. 0xFFFF, /* panId */
  97. FALSE, /* promiscuousMode */
  98. FALSE, /* rxOnWhenIdle */
  99. MAC_SHORT_ADDR_NONE, /* shortAddress */
  100. MAC_SO_NONE, /* superframeOrder */
  101. 0x01F4, /* transactionPersistenceTime */
  102. FALSE, /* assocciatedPanCoord */
  103. 5, /* maxBe */
  104. 1220, /* maxFrameTotalWaitTime */
  105. 3, /* maxFrameRetries */
  106. 32, /* ResponseWaitTime */
  107. 0, /* syncSymbolOffset */
  108. TRUE, /* timeStampSupported */
  109. FALSE, /* securityEnabled */
  110. /* Proprietary */
  111. #if defined (HAL_PA_LNA)
  112. 19, /* phyTransmitPower for CC2591 */
  113. #elif defined (HAL_PA_LNA_CC2590)
  114. 11, /* phyTransmitPower for CC2590 */
  115. #else
  116. 3, /* phyTransmitPower without frontend */
  117. #endif
  118. MAC_CHAN_11, /* logicalChannel */
  119. {0, SADDR_MODE_EXT}, /* extendedAddress */
  120. 1, /* altBe */
  121. MAC_BO_NON_BEACON, /* deviceBeaconOrder */
  122. };
  123. /* PIB access and min/max table. min/max of 0/0 means not checked; if min/max are
  124. * equal, element is read-only
  125. */
  126. static CODE const macPibTbl_t macPibTbl[] =
  127. {
  128. {offsetof(macPib_t, ackWaitDuration), sizeof(uint8), 54, 54}, /* MAC_ACK_WAIT_DURATION */
  129. {offsetof(macPib_t, associationPermit), sizeof(bool), FALSE, TRUE}, /* MAC_ASSOCIATION_PERMIT */
  130. {offsetof(macPib_t, autoRequest), sizeof(bool), FALSE, TRUE}, /* MAC_AUTO_REQUEST */
  131. {offsetof(macPib_t, battLifeExt), sizeof(bool), FALSE, TRUE}, /* MAC_BATT_LIFE_EXT */
  132. {offsetof(macPib_t, battLifeExtPeriods), sizeof(uint8), 6, 6}, /* MAC_BATT_LIFE_EXT_PERIODS */
  133. {offsetof(macPib_t, pBeaconPayload), sizeof(uint8 *), 0, 0}, /* MAC_BEACON_PAYLOAD */
  134. {offsetof(macPib_t, beaconPayloadLength), sizeof(uint8), 0, 52}, /* MAC_BEACON_PAYLOAD_LENGTH */
  135. {offsetof(macPib_t, beaconOrder), sizeof(uint8), 0, 15}, /* MAC_BEACON_ORDER */
  136. {offsetof(macPib_t, beaconTxTime), sizeof(uint32), 1, 1}, /* MAC_BEACON_TX_TIME */
  137. {offsetof(macPib_t, bsn), sizeof(uint8), 0x00, 0xFF}, /* MAC_BSN */
  138. {offsetof(macPib_t, coordExtendedAddress.addr.extAddr), sizeof(sAddrExt_t), 0, 0}, /* MAC_COORD_EXTENDED_ADDRESS */
  139. {offsetof(macPib_t, coordShortAddress), sizeof(uint16), 0, 0}, /* MAC_COORD_SHORT_ADDRESS */
  140. {offsetof(macPib_t, dsn), sizeof(uint8), 0x00, 0xFF}, /* MAC_DSN */
  141. {offsetof(macPib_t, gtsPermit), sizeof(bool), FALSE, TRUE}, /* MAC_GTS_PERMIT */
  142. /* Range of maxCsmaBackoffs is between 0 and 5 in IEEE 802.15.4.
  143. * Such restriction is removed here to allow 802.15.4 non-compliant experimental
  144. * applications.
  145. * To be compliant with 802.15.4, application or upper layer must not set this
  146. * PIB attribute out of range of 802.15.4.
  147. * The range is still restricted to 254 since value 255 would cause backoff
  148. * counter to overflow, wrap around and loop infinitely. */
  149. {offsetof(macPib_t, maxCsmaBackoffs), sizeof(uint8), 0, 254}, /* MAC_MAX_CSMA_BACKOFFS */
  150. {offsetof(macPib_t, minBe), sizeof(uint8), 0, 8}, /* MAC_MIN_BE */
  151. {offsetof(macPib_t, panId), sizeof(uint16), 0, 0}, /* MAC_PAN_ID */
  152. {offsetof(macPib_t, promiscuousMode), sizeof(bool), FALSE, TRUE}, /* MAC_PROMISCUOUS_MODE */
  153. {offsetof(macPib_t, rxOnWhenIdle), sizeof(bool), FALSE, TRUE}, /* MAC_RX_ON_WHEN_IDLE */
  154. {offsetof(macPib_t, shortAddress), sizeof(uint16), 0, 0}, /* MAC_SHORT_ADDRESS */
  155. {offsetof(macPib_t, superframeOrder), sizeof(uint8), 0, 15}, /* MAC_SUPERFRAME_ORDER */
  156. {offsetof(macPib_t, transactionPersistenceTime), sizeof(uint16), 0, 0}, /* MAC_TRANSACTION_PERSISTENCE_TIME */
  157. {offsetof(macPib_t, associatedPanCoord), sizeof(bool), FALSE, TRUE}, /* MAC_ASSOCIATED_PAN_COORD */
  158. /* Range of maxBe is between 3 and 8 in IEEE 802.15.4.
  159. * Such restriction is removed here to allow 802.15.4 non-compliant experimental
  160. * applications.
  161. * To be compliant with 802.15.4, application or upper layer must not set this
  162. * PIB attribute out of range of 802.15.4. */
  163. {offsetof(macPib_t, maxBe), sizeof(uint8), 0, 8}, /* MAC_MAX_BE */
  164. {offsetof(macPib_t, maxFrameTotalWaitTime), sizeof(uint16), 0x00, 0xFF}, /* MAC_MAX_FRAME_RESPONSE_TIME */
  165. {offsetof(macPib_t, maxFrameRetries), sizeof(uint8), 0, 7}, /* MAC_MAX_FRAME_RETRIES */
  166. {offsetof(macPib_t, responseWaitTime), sizeof(uint8), 2, 64}, /* MAC_RESPONSE_WAIT_TIME */
  167. {offsetof(macPib_t, syncSymbolOffset), sizeof(uint8), 0, 0}, /* MAC_SYNC_SYMBOL_OFFSET */
  168. {offsetof(macPib_t, timeStampSupported), sizeof(bool), FALSE, TRUE}, /* MAC_TIMESTAMP_SUPPORTED */
  169. {offsetof(macPib_t, securityEnabled), sizeof(bool), FALSE, TRUE}, /* MAC_SECURITY_ENABLED */
  170. /* Proprietary PIBs */
  171. {offsetof(macPib_t, phyTransmitPower), sizeof(uint8), 0, 0xFF}, /* MAC_PHY_TRANSMIT_POWER */
  172. {offsetof(macPib_t, logicalChannel), sizeof(uint8), MAC_CHAN_11, MAC_CHAN_28}, /* MAC_LOGICAL_CHANNEL */
  173. {offsetof(macPib_t, extendedAddress.addr.extAddr), sizeof(sAddrExt_t), 0, 0}, /* MAC_EXTENDED_ADDRESS */
  174. {offsetof(macPib_t, altBe), sizeof(uint8), 0, 8}, /* MAC_ALT_BE */
  175. {offsetof(macPib_t, deviceBeaconOrder), sizeof(uint8), 0, 15}, /* MAC_DEVICE_BEACON_ORDER */
  176. {offsetof(macPib_t, phyTransmitPower), sizeof(uint8), 0, 0}, /* MAC_PHY_TRANSMIT_POWER_SIGNED */
  177. };
  178. /* Invalid PIB table index used for error code */
  179. #define MAC_PIB_INVALID ((uint8) (sizeof(macPibTbl) / sizeof(macPibTbl[0])))
  180. /* ------------------------------------------------------------------------------------------------
  181. * Global Variables
  182. * ------------------------------------------------------------------------------------------------
  183. */
  184. /* MAC PIB */
  185. macPib_t macPib;
  186. /**************************************************************************************************
  187. * @fn macPibReset
  188. *
  189. * @brief This function initializes the PIB.
  190. *
  191. * input parameters
  192. *
  193. * @param None.
  194. *
  195. * output parameters
  196. *
  197. * None.
  198. *
  199. * @return None.
  200. **************************************************************************************************
  201. */
  202. MAC_INTERNAL_API void macPibReset(void)
  203. {
  204. /* copy PIB defaults */
  205. macPib = macPibDefaults;
  206. /* initialize random sequence numbers */
  207. macPib.dsn = macRadioRandomByte();
  208. macPib.bsn = macRadioRandomByte();
  209. }
  210. /**************************************************************************************************
  211. * @fn macPibIndex
  212. *
  213. * @brief This function takes an PIB attribute and returns the index in to
  214. * macPibTbl for the attribute.
  215. *
  216. * input parameters
  217. *
  218. * @param pibAttribute - PIB attribute to look up.
  219. *
  220. * output parameters
  221. *
  222. * None.
  223. *
  224. * @return Index in to macPibTbl for the attribute or MAC_PIB_INVALID.
  225. **************************************************************************************************
  226. */
  227. static uint8 macPibIndex(uint8 pibAttribute)
  228. {
  229. if ((pibAttribute >= MAC_ATTR_SET1_START) && (pibAttribute <= MAC_ATTR_SET1_END))
  230. {
  231. return (pibAttribute - MAC_ATTR_SET1_START + MAC_ATTR_SET1_OFFSET);
  232. }
  233. else if ((pibAttribute >= MAC_ATTR_SET2_START) && (pibAttribute <= MAC_ATTR_SET2_END))
  234. {
  235. return (pibAttribute - MAC_ATTR_SET2_START + MAC_ATTR_SET2_OFFSET);
  236. }
  237. else
  238. {
  239. return MAC_PIB_INVALID;
  240. }
  241. }
  242. /**************************************************************************************************
  243. * @fn MAC_MlmeGetReq
  244. *
  245. * @brief This direct execute function retrieves an attribute value
  246. * from the MAC PIB.
  247. *
  248. * input parameters
  249. *
  250. * @param pibAttribute - The attribute identifier.
  251. * @param pValue - pointer to the attribute value.
  252. *
  253. * output parameters
  254. *
  255. * @param pValue - pointer to the attribute value.
  256. *
  257. * @return The status of the request, as follows:
  258. * MAC_SUCCESS Operation successful.
  259. * MAC_UNSUPPORTED_ATTRIBUTE Attribute not found.
  260. *
  261. **************************************************************************************************
  262. */
  263. uint8 MAC_MlmeGetReq(uint8 pibAttribute, void *pValue)
  264. {
  265. uint8 i;
  266. halIntState_t intState;
  267. if ((i = macPibIndex(pibAttribute)) == MAC_PIB_INVALID)
  268. {
  269. return MAC_UNSUPPORTED_ATTRIBUTE;
  270. }
  271. HAL_ENTER_CRITICAL_SECTION(intState);
  272. osal_memcpy(pValue, (uint8 *) &macPib + macPibTbl[i].offset, macPibTbl[i].len);
  273. HAL_EXIT_CRITICAL_SECTION(intState);
  274. return MAC_SUCCESS;
  275. }
  276. /**************************************************************************************************
  277. * @fn MAC_MlmeSetReq
  278. *
  279. * @brief This direct execute function sets an attribute value
  280. * in the MAC PIB.
  281. *
  282. * input parameters
  283. *
  284. * @param pibAttribute - The attribute identifier.
  285. * @param pValue - pointer to the attribute value.
  286. *
  287. * output parameters
  288. *
  289. * None.
  290. *
  291. * @return The status of the request, as follows:
  292. * MAC_SUCCESS Operation successful.
  293. * MAC_UNSUPPORTED_ATTRIBUTE Attribute not found.
  294. *
  295. **************************************************************************************************
  296. */
  297. uint8 MAC_MlmeSetReq(uint8 pibAttribute, void *pValue)
  298. {
  299. uint8 i;
  300. halIntState_t intState;
  301. if (pibAttribute == MAC_BEACON_PAYLOAD)
  302. {
  303. macPib.pBeaconPayload = pValue;
  304. return MAC_SUCCESS;
  305. }
  306. /* look up attribute in PIB table */
  307. if ((i = macPibIndex(pibAttribute)) == MAC_PIB_INVALID)
  308. {
  309. return MAC_UNSUPPORTED_ATTRIBUTE;
  310. }
  311. /* do range check; no range check if min and max are zero */
  312. if ((macPibTbl[i].min != 0) || (macPibTbl[i].max != 0))
  313. {
  314. /* if min == max, this is a read-only attribute */
  315. if (macPibTbl[i].min == macPibTbl[i].max)
  316. {
  317. return MAC_READ_ONLY;
  318. }
  319. /* check for special cases */
  320. if (pibAttribute == MAC_MAX_FRAME_TOTAL_WAIT_TIME)
  321. {
  322. if ((*((uint16 *) pValue) < MAC_MAX_FRAME_RESPONSE_MIN) ||
  323. (*((uint16 *) pValue) > MAC_MAX_FRAME_RESPONSE_MAX))
  324. {
  325. return MAC_INVALID_PARAMETER;
  326. }
  327. }
  328. /* range check for general case */
  329. if ((*((uint8 *) pValue) < macPibTbl[i].min) || (*((uint8 *) pValue) > macPibTbl[i].max))
  330. {
  331. return MAC_INVALID_PARAMETER;
  332. }
  333. }
  334. /* set value in PIB */
  335. HAL_ENTER_CRITICAL_SECTION(intState);
  336. osal_memcpy((uint8 *) &macPib + macPibTbl[i].offset, pValue, macPibTbl[i].len);
  337. HAL_EXIT_CRITICAL_SECTION(intState);
  338. /* handle special cases */
  339. switch (pibAttribute)
  340. {
  341. case MAC_PAN_ID:
  342. /* set pan id in radio */
  343. macRadioSetPanID(macPib.panId);
  344. break;
  345. case MAC_SHORT_ADDRESS:
  346. /* set short address in radio */
  347. macRadioSetShortAddr(macPib.shortAddress);
  348. break;
  349. case MAC_RX_ON_WHEN_IDLE:
  350. /* turn rx on or off */
  351. if (macPib.rxOnWhenIdle)
  352. {
  353. macRxEnable(MAC_RX_WHEN_IDLE);
  354. }
  355. else
  356. {
  357. macRxDisable(MAC_RX_WHEN_IDLE);
  358. }
  359. break;
  360. case MAC_LOGICAL_CHANNEL:
  361. macRadioSetChannel(macPib.logicalChannel);
  362. break;
  363. case MAC_EXTENDED_ADDRESS:
  364. /* set ext address in radio */
  365. macRadioSetIEEEAddr(macPib.extendedAddress.addr.extAddr);
  366. break;
  367. #ifndef MAC_OBSOLETE_PHY_TRANSMIT_POWER
  368. /* Define MAC_OBSOLETE_PHY_TRANSMIT_POWER to save some code */
  369. case MAC_PHY_TRANSMIT_POWER:
  370. /* Legacy transmit power attribute */
  371. #if !defined HAL_MAC_USE_REGISTER_POWER_VALUES && \
  372. !defined HAL_PA_LNA && !defined HAL_PA_LNA_CC2590
  373. /* Legacy transmit power attribute value for CC2530 alone,
  374. * or runtime selection support build means a negative absolute value.
  375. * However, when used as register power values or
  376. * with HAL_PA_LNAxxx definition (without runtime selection)
  377. * the attribute value is not a negative absolute value. */
  378. macPib.phyTransmitPower = (uint8)(-(int8)macPib.phyTransmitPower);
  379. #endif /* !defined HAL_MAC_USE_REGISTER_POWER_VALUES && ... */
  380. /* pass through to next case -- do not break*/
  381. #endif /* MAC_OBSOLETE_PHY_TRANSMIT_POWER */
  382. case MAC_PHY_TRANSMIT_POWER_SIGNED:
  383. (void)macRadioSetTxPower(macPib.phyTransmitPower);
  384. break;
  385. default:
  386. break;
  387. }
  388. return MAC_SUCCESS;
  389. }