mac_main.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**************************************************************************************************
  2. Filename: mac_main.h
  3. Revised: $Date: 2009-08-12 14:34:18 -0700 (Wed, 12 Aug 2009) $
  4. Revision: $Revision: 20549 $
  5. Description: Internal interface file for the MAC.
  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. #ifndef MAC_MAIN_H
  34. #define MAC_MAIN_H
  35. /* ------------------------------------------------------------------------------------------------
  36. * Includes
  37. * ------------------------------------------------------------------------------------------------
  38. */
  39. #include "mac_high_level.h"
  40. /* ------------------------------------------------------------------------------------------------
  41. * Constants
  42. * ------------------------------------------------------------------------------------------------
  43. */
  44. /* State machine states */
  45. enum
  46. {
  47. MAC_IDLE_ST, /* Idle state */
  48. MAC_ASSOCIATING_ST, /* MAC device is performing the association procedure */
  49. MAC_POLLING_ST, /* MAC device is polling coordinator for data */
  50. MAC_SCANNING_ST, /* MAC device or coordinator is performing a scan procedure */
  51. MAC_STARTING_ST /* MAC coordinator is starting a network */
  52. };
  53. /* State machine events */
  54. enum
  55. {
  56. MAC_API_DATA_REQ_EVT,
  57. MAC_API_PURGE_REQ_EVT,
  58. MAC_API_ASSOCIATE_REQ_EVT,
  59. MAC_API_ASSOCIATE_RSP_EVT,
  60. MAC_API_DISASSOCIATE_REQ_EVT,
  61. MAC_API_ORPHAN_RSP_EVT,
  62. MAC_API_POLL_REQ_EVT,
  63. MAC_API_SCAN_REQ_EVT,
  64. MAC_API_START_REQ_EVT,
  65. MAC_API_SYNC_REQ_EVT,
  66. MAC_API_PWR_ON_REQ_EVT,
  67. MAC_RX_ASSOC_REQ_EVT,
  68. MAC_RX_ASSOC_RSP_EVT,
  69. MAC_RX_DISASSOC_EVT,
  70. MAC_RX_DATA_REQ_EVT,
  71. MAC_RX_PAN_CONFLICT_EVT,
  72. MAC_RX_ORPHAN_EVT,
  73. MAC_RX_BEACON_REQ_EVT,
  74. MAC_RX_REALIGN_EVT,
  75. MAC_RX_GTS_REQ_EVT,
  76. MAC_RX_BEACON_EVT,
  77. MAC_RX_DATA_IND_EVT,
  78. MAC_TX_ASSOC_REQ_OK_EVT,
  79. MAC_TX_ASSOC_REQ_FAIL_EVT,
  80. MAC_TX_ASSOC_RSP_EVT,
  81. MAC_TX_DISASSOC_EVT,
  82. MAC_TX_ORPHAN_OK_EVT,
  83. MAC_TX_ORPHAN_FAIL_EVT,
  84. MAC_TX_BEACON_REQ_OK_EVT,
  85. MAC_TX_BEACON_REQ_FAIL_EVT,
  86. MAC_TX_REALIGN_OK_EVT,
  87. MAC_TX_REALIGN_FAIL_EVT,
  88. MAC_TX_DATA_REQ_OK_EVT,
  89. MAC_TX_DATA_REQ_FAIL_EVT,
  90. MAC_TX_DATA_REQ_PEND_EVT,
  91. MAC_TX_CONFLICT_OK_EVT,
  92. MAC_TIM_RESPONSE_WAIT_EVT,
  93. MAC_TIM_FRAME_RESPONSE_EVT,
  94. MAC_TIM_SCAN_EVT,
  95. MAC_TIM_SYNC_EVT,
  96. MAC_TIM_EXP_INDIRECT_EVT,
  97. MAC_INT_SCAN_COMPLETE_EVT,
  98. MAC_INT_ASSOC_FAILED_EVT,
  99. MAC_INT_AUTO_POLL_EVT,
  100. MAC_INT_START_COMPLETE_EVT,
  101. MAC_INT_TX_COMPLETE_EVT,
  102. MAC_INT_BEACON_RESP_TIM_EVT,
  103. MAC_INT_BROADCAST_PEND_EVT,
  104. MAC_NO_ACTION_EVT
  105. };
  106. /* This must be set to the last API event in the enumeration */
  107. #define MAC_API_EVT_MAX MAC_API_PWR_ON_REQ_EVT
  108. /* Enumeration of action function table sets; maximum of 16 */
  109. enum
  110. {
  111. MAC_MGMT_ACTION_1,
  112. MAC_MGMT_ACTION_2,
  113. MAC_SCAN_ACTION,
  114. MAC_DEVICE_ACTION_1,
  115. MAC_DEVICE_ACTION_2,
  116. MAC_COORD_ACTION_1,
  117. MAC_COORD_ACTION_2,
  118. MAC_COORD_ACTION_3,
  119. MAC_BEACON_DEVICE_ACTION,
  120. MAC_ACTION_SET_MAX
  121. };
  122. /* these macros process the action enumeration value */
  123. #define MAC_ACTION_INIT(n) ((n) << 4)
  124. #define MAC_ACTION_SET(n) ((n) >> 4)
  125. #define MAC_ACTION_ID(n) ((n) & 0x0F)
  126. /* Enumeration of action functions; maximum of 16 per set */
  127. enum
  128. {
  129. /* Mgmt set 1 */
  130. MAC_NO_ACTION = MAC_ACTION_INIT(MAC_MGMT_ACTION_1),
  131. MAC_API_BAD_STATE,
  132. MAC_API_PENDING,
  133. MAC_API_DATA_REQ,
  134. MAC_COMM_STATUS_IND,
  135. MAC_DATA_SEND,
  136. MAC_DATA_RX_IND,
  137. /* Mgmt set 2 */
  138. MAC_API_PWR_ON_REQ = MAC_ACTION_INIT(MAC_MGMT_ACTION_2),
  139. MAC_API_DISASSOCIATE_REQ,
  140. MAC_DISASSOC_COMPLETE,
  141. MAC_RX_BEACON,
  142. MAC_RX_DISASSOC,
  143. /* Scan */
  144. MAC_API_SCAN_REQ = MAC_ACTION_INIT(MAC_SCAN_ACTION),
  145. MAC_SCAN_COMPLETE,
  146. MAC_SCAN_FAILED_IN_PROGRESS,
  147. MAC_SCAN_NEXT_CHAN,
  148. MAC_SCAN_RX_BEACON,
  149. MAC_SCAN_START_TIMER,
  150. /* Device set 1 */
  151. MAC_API_ASSOCIATE_REQ = MAC_ACTION_INIT(MAC_DEVICE_ACTION_1),
  152. MAC_API_POLL_REQ,
  153. MAC_ASSOC_DATA_REQ,
  154. MAC_ASSOC_DATA_REQ_COMPLETE,
  155. MAC_ASSOC_DATA_RX_IND,
  156. MAC_ASSOC_FAILED,
  157. MAC_ASSOC_FRAME_RESPONSE_TIMEOUT,
  158. MAC_POLL_DATA_REQ_COMPLETE,
  159. MAC_POLL_DATA_RX_IND,
  160. MAC_POLL_FRAME_RESPONSE_TIMEOUT,
  161. MAC_POLL_RX_ASSOC_RSP,
  162. MAC_RX_ASSOC_RSP,
  163. MAC_START_FRAME_RESPONSE_TIMER,
  164. MAC_START_RESPONSE_TIMER,
  165. /* Device set 2 */
  166. MAC_ASSOC_RX_DISASSOC = MAC_ACTION_INIT(MAC_DEVICE_ACTION_2),
  167. MAC_POLL_RX_DISASSOC,
  168. MAC_SCAN_RX_COORD_REALIGN,
  169. MAC_RX_COORD_REALIGN,
  170. MAC_PAN_CONFLICT_COMPLETE,
  171. /* Coordinator set 1 */
  172. MAC_API_START_REQ = MAC_ACTION_INIT(MAC_COORD_ACTION_1),
  173. MAC_START_CONTINUE,
  174. /* Coordinator set 2 */
  175. MAC_API_ASSOCIATE_RSP = MAC_ACTION_INIT(MAC_COORD_ACTION_2),
  176. MAC_RX_ASSOC_REQ,
  177. MAC_RX_BEACON_REQ,
  178. MAC_RX_DATA_REQ,
  179. MAC_START_COMPLETE,
  180. MAC_INDIRECT_EXPIRE,
  181. /* Coordinator set 3 */
  182. MAC_API_ORPHAN_RSP = MAC_ACTION_INIT(MAC_COORD_ACTION_3),
  183. MAC_API_PURGE_REQ,
  184. MAC_RX_ORPHAN,
  185. MAC_RX_PAN_CONFLICT,
  186. /* Beacon device */
  187. MAC_API_SYNC_REQ = MAC_ACTION_INIT(MAC_BEACON_DEVICE_ACTION),
  188. MAC_AUTO_POLL,
  189. MAC_BEACON_START_FRAME_RSP_TIMER,
  190. MAC_START_BROADCAST_PEND_TIMER
  191. };
  192. /* Special hdr.event mask value used to mark buffer as reserved (do not deallocate) */
  193. #define MAC_BUF_RESERVED 0x80
  194. /* OSAL task events (as uint8) */
  195. #define MAC_RESPONSE_WAIT_TASK_EVT 0x01
  196. #define MAC_FRAME_RESPONSE_TASK_EVT 0x02
  197. #define MAC_SCAN_TASK_EVT 0x04
  198. #define MAC_EXP_INDIRECT_TASK_EVT 0x08
  199. #define MAC_TX_COMPLETE_TASK_EVT 0x10
  200. #define MAC_RX_QUEUE_TASK_EVT 0x20
  201. #define MAC_START_COMPLETE_TASK_EVT 0x40
  202. #define MAC_BROADCAST_PEND_TASK_EVT 0x80
  203. /* State table format */
  204. #define MAC_ACTION_POS 0 /* action position */
  205. #define MAC_NEXT_STATE_POS 1 /* next state position */
  206. #define MAC_NUM_COLS 2 /* number of columns in state tables */
  207. /* Configurable features */
  208. enum
  209. {
  210. MAC_FEAT_DEVICE,
  211. MAC_FEAT_COORD,
  212. MAC_FEAT_SECURITY,
  213. MAC_FEAT_BEACON_COORD,
  214. MAC_FEAT_BEACON_DEVICE,
  215. MAC_FEAT_GTS_CLIENT,
  216. MAC_FEAT_GTS_SERVER,
  217. MAC_FEAT_MAX
  218. };
  219. /* Configurable feature masks */
  220. #define MAC_FEAT_DEVICE_MASK ((uint16) 1 << MAC_FEAT_DEVICE)
  221. #define MAC_FEAT_COORD_MASK ((uint16) 1 << MAC_FEAT_COORD)
  222. #define MAC_FEAT_SECURITY_MASK ((uint16) 1 << MAC_FEAT_SECURITY)
  223. #define MAC_FEAT_BEACON_COORD_MASK ((uint16) 1 << MAC_FEAT_BEACON_COORD)
  224. #define MAC_FEAT_BEACON_DEVICE_MASK ((uint16) 1 << MAC_FEAT_BEACON_DEVICE)
  225. #define MAC_FEAT_GTS_CLIENT_MASK ((uint16) 1 << MAC_FEAT_GTS_CLIENT)
  226. #define MAC_FEAT_GTS_SERVER_MASK ((uint16) 1 << MAC_FEAT_GTS_SERVER)
  227. #define MAC_FEAT_GTS_MASK (MAC_FEAT_GTS_CLIENT_MASK | MAC_FEAT_GTS_SERVER_MASK)
  228. /* RX enable/disable masks */
  229. #define MAC_RX_POLL 0x01 /* polling/associating */
  230. #define MAC_RX_WHEN_IDLE 0x02 /* PIB rxOnWhenIdle */
  231. #define MAC_RX_SCAN 0x04 /* scanning */
  232. #define MAC_RX_BROADCAST_PEND 0x10 /* pending broadcast */
  233. #define MAC_RX_BEACON_DEVICE 0x20 /* beacon tracking */
  234. #define MAC_RX_BEACON_NETWORK 0x40 /* beacon network */
  235. #define MAC_RX_BEACON_SYNC 0x80 /* beacon sync */
  236. /* TX mode masks */
  237. #define MAC_TX_MODE_NON_CSMA 0x01 /* frame tx without CSMA */
  238. #define MAC_TX_MODE_SLOTTED 0x02 /* frame tx in slotted mode */
  239. #define MAC_TX_MODE_INTERNAL 0x08 /* frame is for internal purposes */
  240. #define MAC_TX_MODE_SCAN_RELATED 0x10 /* frame is scan related */
  241. #define MAC_TX_MODE_BROADCAST 0x20 /* broadcast frame in beacon network */
  242. #define MAC_TX_MODE_BEACON 0x40 /* beacon tx in beacon network */
  243. #define MAC_TX_MODE_REQUESTED 0x80 /* indirect frame was requested in beacon network */
  244. /* TX schedule masks */
  245. #define MAC_TX_SCHED_OUTGOING_CAP 0x01 /* outgoing CAP (default) */
  246. #define MAC_TX_SCHED_INCOMING_CAP 0x02 /* incoming CAP */
  247. #define MAC_TX_SCHED_INDIRECT_NO_ACK 0x04 /* frame is indirect and not acked */
  248. #define MAC_TX_SCHED_READY 0x10 /* frame scheduled as ready to tx */
  249. #define MAC_TX_SCHED_SCAN_RELATED 0x20 /* frame is scan related */
  250. #define MAC_TX_SCHED_MARKED 0x40 /* frame is marked for pending address list */
  251. #define MAC_TX_SCHED_INDIRECT 0x80 /* frame is indirect */
  252. #define MAC_TX_SCHED_INACTIVE 0xFF /* inactive portion of the superframe */
  253. /* ------------------------------------------------------------------------------------------------
  254. * Macros
  255. * ------------------------------------------------------------------------------------------------
  256. */
  257. /* API parameter checking macro */
  258. #if defined(MAC_NO_PARAM_CHECK) && MAC_NO_PARAM_CHECK == TRUE
  259. #define MAC_PARAM_STATUS(expr, status)
  260. #else
  261. #define MAC_PARAM_STATUS(expr, status) if ((expr) == FALSE) status = MAC_INVALID_PARAMETER
  262. #endif
  263. /* ------------------------------------------------------------------------------------------------
  264. * Typedefs
  265. * ------------------------------------------------------------------------------------------------
  266. */
  267. /* State table type */
  268. typedef const uint8 (CODE * macStateTbl_t)[MAC_NUM_COLS];
  269. /* Event structure for API events with buffer for command frame */
  270. typedef struct
  271. {
  272. macEventHdr_t hdr;
  273. sData_t mpdu;
  274. macTxIntData_t internal;
  275. union
  276. {
  277. macMlmeAssociateReq_t associateReq;
  278. macMlmeAssociateRsp_t associateRsp;
  279. macMlmeDisassociateReq_t disassociateReq;
  280. macMlmeOrphanRsp_t orphanRsp;
  281. } mac;
  282. } macApiDataEvent_t;
  283. /* MLME purge request type */
  284. typedef struct
  285. {
  286. uint8 msduHandle;
  287. } macMcpsPurgeReq_t;
  288. /* Event structure for API events */
  289. typedef struct
  290. {
  291. macEventHdr_t hdr;
  292. union
  293. {
  294. macMlmeScanReq_t scanReq;
  295. macMlmeStartReq_t startReq;
  296. macMlmeSyncReq_t syncReq;
  297. macMlmePollReq_t pollReq;
  298. macMcpsPurgeReq_t purgeReq;
  299. } mac;
  300. } macApiEvent_t;
  301. /* Union of event structures */
  302. typedef union
  303. {
  304. macEventHdr_t hdr;
  305. macApiEvent_t api;
  306. macApiDataEvent_t apiData;
  307. macMcpsDataReq_t dataReq;
  308. macMcpsDataInd_t dataInd;
  309. macTx_t tx;
  310. macRx_t rx;
  311. macMlmeBeaconNotifyInd_t beaconNotify;
  312. } macEvent_t;
  313. /* Action function type */
  314. typedef void (*macAction_t)(macEvent_t *pEvent);
  315. /* Action function set type */
  316. typedef const macAction_t CODE * macActionSet_t;
  317. /* Type for mac_main data */
  318. typedef struct
  319. {
  320. macEvent_t *pPending; /* pointer to pending buffer */
  321. uint8 *pBuf;
  322. uint16 featureMask; /* mask of initialized features */
  323. uint8 state; /* state machine state */
  324. } macMain_t;
  325. /* Type for module reset functions */
  326. typedef void (*macReset_t)(void);
  327. /* ------------------------------------------------------------------------------------------------
  328. * Global Variables
  329. * ------------------------------------------------------------------------------------------------
  330. */
  331. /* state tables */
  332. extern const uint8 CODE macIdleSt[][MAC_NUM_COLS];
  333. extern const uint8 CODE macAssociatingSt[][MAC_NUM_COLS];
  334. extern const uint8 CODE macPollingSt[][MAC_NUM_COLS];
  335. extern const uint8 CODE macScanningSt[][MAC_NUM_COLS];
  336. extern const uint8 CODE macStartingSt[][MAC_NUM_COLS];
  337. /* state table array */
  338. extern macStateTbl_t macStateTbl[];
  339. /* OSAL task id */
  340. extern uint8 macTaskId;
  341. /* mac_main data */
  342. extern macMain_t macMain;
  343. /* module reset functions */
  344. extern macReset_t macReset[MAC_FEAT_MAX];
  345. /* state machine action function table pointers */
  346. extern macActionSet_t macActionSet[MAC_ACTION_SET_MAX];
  347. /* configurable parameters */
  348. extern const macCfg_t macCfg;
  349. /* ------------------------------------------------------------------------------------------------
  350. * Function Prototypes
  351. * ------------------------------------------------------------------------------------------------
  352. */
  353. MAC_INTERNAL_API void macMainReset(void);
  354. MAC_INTERNAL_API void macSetEvent(uint8 event);
  355. MAC_INTERNAL_API void macCbackForEvent(macEvent_t *pEvent, uint8 status);
  356. MAC_INTERNAL_API bool macStateIdle(void);
  357. MAC_INTERNAL_API bool macStateIdleOrPolling(void);
  358. MAC_INTERNAL_API bool macStateScanning(void);
  359. MAC_INTERNAL_API void macExecute(macEvent_t *pEvent);
  360. MAC_INTERNAL_API void macSendMsg(uint8 event, void *pData);
  361. MAC_INTERNAL_API uint8 macSendDataMsg(uint8 event, bool security, void *pData);
  362. MAC_INTERNAL_API void macMainReserve(uint8 *ptr);
  363. MAC_INTERNAL_API void mac_msg_deallocate(uint8 **msg_ptr);
  364. /**************************************************************************************************
  365. */
  366. #endif /* MAC_MAIN_H */