mac_mlme_pib.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**
  2. * Copyright (c) 2016 - 2020 Nordic Semiconductor ASA and Luxoft Global Operations Gmbh.
  3. *
  4. * All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form, except as embedded into a Nordic
  14. * Semiconductor ASA integrated circuit in a product or a software update for
  15. * such product, must reproduce the above copyright notice, this list of
  16. * conditions and the following disclaimer in the documentation and/or other
  17. * materials provided with the distribution.
  18. *
  19. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * 4. This software, with or without modification, must only be used with a
  24. * Nordic Semiconductor ASA integrated circuit.
  25. *
  26. * 5. Any software provided in binary form under this license must not be reverse
  27. * engineered, decompiled, modified and/or disassembled.
  28. *
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  31. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  33. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  34. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  35. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  36. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  39. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. */
  42. #ifndef MAC_MLME_PIB_H_INCLUDED
  43. #define MAC_MLME_PIB_H_INCLUDED
  44. #include <stdint.h>
  45. #include "mac_common.h"
  46. #include "phy_plme_pib.h"
  47. #include "mac_task_scheduler.h"
  48. #include "mac_security.h"
  49. #include "sys_debug.h"
  50. /** @file
  51. * The MAC MLME PIB module declares the MAC PHY Information Base routines and
  52. * necessary types/macros according to the MAC specification.
  53. *
  54. * @defgroup mac_pib MAC MLME PIB API
  55. * @ingroup mac_15_4
  56. * @{
  57. * @brief Module to declare MAC MLME PIB API.
  58. * @details The MAC PIB module declares routines and necessary types to deal with the PHY Information Base
  59. * functionality related to MAC. More specifically, MLME PIB Get request aka mlme_get_req(), MLME
  60. * PIB Set request aka mlme_set_req(), MLME PIB confirmation callbacks aka mlme_get_conf_cb_t, and
  61. * mlme_set_conf_cb_t primitives are declared. Two additional primitives not covered by the
  62. * standard are declared. These are mlme_get() and mlme_set() which are synchronous versions of
  63. * mlme_get_req() and mlme_set_req() accordingly. There is one helper informational routine
  64. * mlme_pib_attr_size_calc() to count MLME attribute size in bytes. Refer to the
  65. * mac_pib_param_test application for detailed samples of implementation of these primitives.
  66. * This module also defines the MAC Table API. The tables can be used to deal with MAC attributes.
  67. * A special initialization routine mac_table_init() should be called before using of any other MAC
  68. * table API. The mac_table_reset() routine is used to clean up an existing (initialized) table.
  69. * mac_table_idx_get() searches through a MAC table to find the item with requested idx. The
  70. * mac_table_item_set() routine is needed to substitute a table item with a new value. The
  71. * mac_table_item_remove() routine removes the item with the given index from the table and
  72. * frees all resources associated with the item. mac_table_item_front() and mac_table_item_next()
  73. * return the first and next item from the table. The mac_table_size_get() routine returns the
  74. * number of items in the table, while mac_table_is_empty() checks if the table is empty.
  75. */
  76. /**
  77. * @brief MAC PIB attribute identifiers
  78. *
  79. * In accordance with IEEE Std 802.15.4-2006, section 7.4.2
  80. */
  81. typedef enum
  82. {
  83. MAC_ACK_WAIT_DURATION = 0x40,
  84. MAC_ASSOCIATION_PERMIT,
  85. MAC_AUTO_REQUEST,
  86. MAC_BATT_LIFE_EXT,
  87. MAC_BATT_LIFE_EXT_PERIODS,
  88. MAC_BEACON_PAYLOAD, /* 0x45 */
  89. MAC_BEACON_PAYLOAD_LENGTH,
  90. MAC_BEACON_ORDER, /**< Specification of how often the
  91. coordinator transmits its
  92. beacon. If BO = 15, the
  93. coordinator will not transmit
  94. a periodic beacon.*/
  95. MAC_BEACON_TX_TIME,
  96. MAC_BSN,
  97. MAC_COORD_EXTENDED_ADDRESS, /* 0x4A */
  98. MAC_COORD_SHORT_ADDRESS,
  99. MAC_DSN,
  100. MAC_GTS_PERMIT,
  101. MAC_MAX_CSMA_BACKOFFS,
  102. MAC_MIN_BE,
  103. MAC_PAN_ID, /**< PAN Identifier.*/
  104. /* 0x50 */
  105. MAC_PROMISCUOUS_MODE,
  106. MAC_RX_ON_WHEN_IDLE,
  107. MAC_SHORT_ADDRESS, /**< MAC Short Address.*/
  108. MAC_SUPERFRAME_ORDER,
  109. MAC_TRANSACTION_PERSISTENCE_TIME, /* 0x55 */
  110. MAC_ASSOCIATED_PAN_COORD,
  111. MAC_MAX_BE,
  112. MAC_MAX_FRAME_TOTAL_WAIT_TIME,
  113. MAC_MAX_FRAME_RETRIES,
  114. MAC_RESPONSE_WAIT_TIME, /* 0x5A */
  115. MAC_SYNC_SYMBOL_OFFSET,
  116. MAC_TIMESTAMP_SUPPORTED,
  117. MAC_SECURITY_ENABLED,
  118. MAC_MIN_LIFS_PERIOD, /* 0x5E No attribute id in Table 86.*/
  119. MAC_MIN_SIFS_PERIOD, /* 0x5F No attribute id in Table 86.*/
  120. MAC_EXTENDED_ADDRESS, /**< MAC Extended Address.*/
  121. /* 0x60 Not covered by standard.*/
  122. MAC_IS_PAN_COORD,
  123. #if (CONFIG_SECURE == 1)
  124. MAC_KEY_TABLE = 0x71,
  125. MAC_KEY_TABLE_ENTRIES,
  126. MAC_DEVICE_TABLE,
  127. MAC_DEVICE_TABLE_ENTRIES,
  128. MAC_SECURITY_LEVEL_TABLE, /* 0x75 */
  129. MAC_SECURITY_LEVEL_TABLE_ENTRIES,
  130. MAC_FRAME_COUNTER,
  131. MAC_AUTO_REQUEST_SECURITY_LEVEL,
  132. MAC_AUTO_REQUEST_KEY_ID_MODE,
  133. MAC_AUTO_REQUEST_KEY_SOURCE, /* 0x7A */
  134. MAC_AUTO_REQUEST_KEY_INDEX,
  135. MAC_DEFAULT_KEY_SOURCE,
  136. MAC_PAN_COORD_EXTENDED_ADDRESS,
  137. MAC_PAN_COORD_SHORT_ADDRESS,
  138. /* Items below do not covered by the standard */
  139. // these three IDs are used to make access to the root of security tables
  140. MAC_KEY_TABLE_POINTER,
  141. MAC_DEVICE_TABLE_POINTER,
  142. MAC_SECURITY_LEVEL_TABLE_POINTER,
  143. // these three IDs are stored inside PIB base and
  144. // used to get table item sizes
  145. MAC_KEY_ID_LOOKUP_LIST,
  146. MAC_KEY_DEVICE_LIST,
  147. MAC_KEY_USAGE_LIST,
  148. #endif
  149. } mlme_pib_attr_id_t;
  150. /**
  151. * @brief United PIB attribute identifiers
  152. *
  153. * To unite access to MAC and PHY PIB by one API
  154. */
  155. typedef union
  156. {
  157. mlme_pib_attr_id_t mlme_id; /**< PIB is MAC-based. */
  158. plme_pib_attr_id_t plme_id; /**< PIB is PHY-based. */
  159. } pib_id_t;
  160. /**
  161. * @brief MLME-GET.confirm
  162. *
  163. * @details structure for confirming information about a given PIB attribute.
  164. *
  165. * In accordance with IEEE Std 802.15.4-2006, section 7.1.6.2
  166. */
  167. typedef struct
  168. {
  169. mac_status_t status; /**< Status of operation. */
  170. pib_id_t pib_attribute; /**< PIB Attribute. */
  171. uint8_t pib_attribute_idx; /**< PIB Attribute index. */
  172. /** value size is calculated with 'mlme_pib_attr_size_calc' */
  173. uint8_t * value; /**< Attribute value. */
  174. } mlme_get_conf_t;
  175. /**
  176. * @brief MLME-GET.request
  177. *
  178. * @details structure for requesting information about a given PIB attribute.
  179. *
  180. * In accordance with IEEE Std 802.15.4-2006, section 7.1.6.1
  181. */
  182. typedef struct
  183. {
  184. /** Do not edit this field. */
  185. mac_abstract_req_t service;
  186. /** Confirm to this request. */
  187. mlme_get_conf_t confirm;
  188. pib_id_t pib_attribute; /**< PIB Attribute. */
  189. uint8_t pib_attribute_idx; /**< PIB Attribute index. */
  190. } mlme_get_req_t;
  191. /**
  192. * @brief MLME-SET.confirm
  193. *
  194. * @details structure for reporting the results of an attempt to write a value
  195. * to a PIB attribute.
  196. *
  197. * In accordance with IEEE Std 802.15.4-2006, section 7.1.13.2
  198. */
  199. typedef struct
  200. {
  201. mac_status_t status; /**< Status of operation. */
  202. pib_id_t pib_attribute; /**< PIB Attribute. */
  203. uint8_t pib_attribute_idx; /**< PIB Attribute index. */
  204. } mlme_set_conf_t;
  205. /**
  206. * @brief MLME-SET.request
  207. *
  208. * @details structure for setting a PIB attribute.
  209. *
  210. * In accordance with IEEE Std 802.15.4-2006, section 7.1.13.1
  211. */
  212. typedef struct
  213. {
  214. /** Do not edit this field. */
  215. mac_abstract_req_t service;
  216. /** Confirm to this request. */
  217. mlme_set_conf_t confirm;
  218. pib_id_t pib_attribute; /**< PIB Attribute. */
  219. uint8_t pib_attribute_idx; /**< PIB Attribute index. */
  220. uint8_t * value; /**< Attribute value. The value size is calculated
  221. with mlme_pib_attr_size_calc. */
  222. } mlme_set_req_t;
  223. /**
  224. * @brief Customer's function of confirmation
  225. *
  226. * @details The MLME-GET.confirm primitive is generated by the MLME and issued
  227. * to its next higher layer in response to an MLME-GET.request primitive.
  228. * This primitive returns a status of either SUCCESS, indicating that the request
  229. * to read a PIB attribute was successful, or an error code of UNSUPPORTED_ATTRIBUTE.
  230. * When an error code of UNSUPPORTED_ATTRIBUTE is returned, the PIBAttribute value
  231. * parameter will be set to length zero. The status values are fully described in 7.1.6.1.3.
  232. *
  233. * @param pointer to confirmation primitive
  234. */
  235. typedef void (* mlme_get_conf_cb_t)(mlme_get_conf_t *);
  236. /**
  237. * @brief Customer's function of confirmation
  238. *
  239. * @details The MLME-SET.confirm primitive is generated by the MLME and issued to its
  240. * next higher layer in response to an MLME-SET.request primitive. The MLME-SET.confirm
  241. * primitive returns a status of either SUCCESS, indicating that the requested value was
  242. * written to the indicated PIB attribute, or the appropriate error code.
  243. * The status values are fully described in 7.1.13.1.3.
  244. *
  245. * @param pointer to confirmation primitive
  246. */
  247. typedef void (* mlme_set_conf_cb_t)(mlme_set_conf_t *);
  248. /**
  249. * @brief MLME-GET request
  250. *
  251. * @details Request information about a given PIB attribute.
  252. *
  253. * @param[in] req pointer to request structure.
  254. * @param[in] conf_cb pointer to user callback.
  255. *
  256. * In accordance with IEEE Std 802.15.4-2006, section 7.1.6.
  257. * See \a mlme_get() for more details.
  258. */
  259. void mlme_get_req(mlme_get_req_t * req, mlme_get_conf_cb_t conf_cb);
  260. /**
  261. * @brief MLME-SET request
  262. *
  263. * @details Request to set a PIB attribute.
  264. * After request completion, user callback will be issued with
  265. * valid data stored in structure @ref mlme_set_conf_t.
  266. *
  267. * See \a mlme_set() for more details.
  268. *
  269. * In accordance with IEEE Std 802.15.4-2006, section 7.1.13
  270. *
  271. * @param[in] req MLME_SET request structure.
  272. * @param[in] conf_cb pointer to user callback.
  273. */
  274. void mlme_set_req(mlme_set_req_t * req, mlme_set_conf_cb_t conf_cb);
  275. /**
  276. * @brief Counts MLME attribute size
  277. *
  278. * @details This is an implementation-specific function not covered by the standard.
  279. *
  280. * @param[in] id attribute id.
  281. * @param[in] idx index inside the table in case the attribute is a table.
  282. *
  283. * @return size of attribute in bytes.
  284. */
  285. size_t mlme_pib_attr_size_calc(pib_id_t id, uint8_t idx);
  286. /**
  287. * @brief Gets parameters from PIB directly (without request - confirm approach)
  288. *
  289. * @details Optional. Not covered by a standard.
  290. *
  291. * For non-tabled attributes this function will return value to location
  292. * passed to the last argument.
  293. *
  294. * For tabled attributes this function will return pointer to
  295. * a descriptor structure of corresponding table.
  296. *
  297. * @param[in] id attribute id.
  298. * @param[in] idx index inside the table in case the attribute is a table.
  299. * @param[out] mem either pointer to memory where attribute value is returned
  300. * (for all attributes except MAC_KEY_TABLE, MAC_DEVICE_TABLE,
  301. * MAC_SECURITY_LEVEL_TABLE), or pointer to memory where pointer
  302. * to attribute storage place is returned.
  303. *
  304. * @return status of operation
  305. */
  306. mac_status_t mlme_get(pib_id_t id, uint8_t idx, void * mem);
  307. /**
  308. * @brief Sets parameters to PIB directly (without request - confirm approach)
  309. *
  310. * @details Optional. Not covered by a standard.
  311. *
  312. * This function performs copying or replacement of some attribute value
  313. * into the PIB base memory.
  314. *
  315. * Note, that all security tables are copied into dynamic memory, that
  316. * mlme_set is responsible to allocate. For nested tables copying is done
  317. * in a shallow manner (in Python sense). It means that passed \a mac_key_descr_t
  318. * is copied as-is, without creating copies of internal tables.
  319. * Caller must allocate and prepare all nested tables such as
  320. * #MAC_KEY_DEVICE_LIST, #MAC_KEY_ID_LOOKUP_LIST and #MAC_KEY_USAGE_LIST
  321. * before calling this function.
  322. *
  323. * Passed attribute value will replace the current one, if the item with such
  324. * \a id and \a idx already exists. This function is responsible for
  325. * freeing all items during destruction of existing objects.
  326. *
  327. * @note Nested tables may be expanded and reduced with \a mac_table_item_set()
  328. * and other similar functions.
  329. *
  330. * @param[in] id attribute id.
  331. * @param[in] idx index inside the table in case the attribute is a table.
  332. * @param[out] mem pointer to memory for parameter storing.
  333. *
  334. * @return status of operation
  335. */
  336. mac_status_t mlme_set(pib_id_t id, uint8_t idx, void * mem);
  337. #if (CONFIG_SECURE == 1)
  338. /**
  339. * @brief Initializes a table. This function MUST be called before accessing
  340. * to a newly allocated table.
  341. *
  342. * @param[out] p_table Pointer to a fresh table.
  343. */
  344. void mac_table_init(mac_table_t * p_table);
  345. /**
  346. * @brief Resets a table, freeing all its elements.
  347. *
  348. * @param[in] p_table Pointer to the table to reset.
  349. * @param[in] id One of #MAC_KEY_TABLE, #MAC_DEVICE_TABLE, #MAC_SECURITY_LEVEL_TABLE,
  350. * #MAC_KEY_ID_LOOKUP_LIST, #MAC_KEY_DEVICE_LIST, #MAC_KEY_USAGE_LIST to let
  351. * function know about the size of p_item.
  352. */
  353. void mac_table_reset(mac_table_t * p_table, mlme_pib_attr_id_t id);
  354. /**
  355. * @brief Searches through mac_table_t and finds the item with requested idx.
  356. *
  357. * @param[in] p_table Table to search through.
  358. * @param[in] idx Item idx to match.
  359. *
  360. * @return Pointer to mac_table_item_t with requested idx or NULL if such
  361. * an item cannot be found.
  362. */
  363. mac_table_item_t * mac_table_idx_get(const mac_table_t * p_table, uint8_t idx);
  364. /**
  365. * @brief Sets new value item for mac_table_t.
  366. *
  367. * @param[out] p_table Pointer to the table to add item to.
  368. * @param[in] p_item Pointer to a new item. This item must include appropriate idx
  369. * (less than the maximum table size).
  370. * @param[in] id One of #MAC_KEY_TABLE, #MAC_DEVICE_TABLE, #MAC_SECURITY_LEVEL_TABLE,
  371. * #MAC_KEY_ID_LOOKUP_LIST, #MAC_KEY_DEVICE_LIST, #MAC_KEY_USAGE_LIST to let
  372. * function know about the size of p_item.
  373. * @param[in] idx Item index inside the selected table.
  374. *
  375. * @details This function performs a "deep copy" of passed table item to conform with
  376. * mlme_set behavior. New copy resides in the heap memory. If an item with requested
  377. * idx has been already set earlier, this function frees the old item and pushes
  378. * a new one instead.
  379. *
  380. * @retval #MAC_INVALID_INDEX if idx exceeds allowed maximum number of items in
  381. * the table.
  382. * @retval #MAC_LIMIT_REACHED if there is no enough dynamic memory to put this item
  383. * into the security table.
  384. * @retval #MAC_SUCCESS if insertion has been performed successfully.
  385. */
  386. mac_status_t mac_table_item_set(mac_table_t * p_table,
  387. const mac_table_item_t * p_item,
  388. mlme_pib_attr_id_t id,
  389. uint8_t idx);
  390. /**
  391. * @brief Removes an item from a mac_table_t instance and frees all resources,
  392. * associated with this item.
  393. *
  394. * @param[out] p_table Pointer to the table to remove item from.
  395. * @param[in] id One of #MAC_KEY_TABLE, #MAC_DEVICE_TABLE, #MAC_SECURITY_LEVEL_TABLE,
  396. * #MAC_KEY_ID_LOOKUP_LIST, #MAC_KEY_DEVICE_LIST, #MAC_KEY_USAGE_LIST to let
  397. * function perform down-casting correctly.
  398. * @param[in] idx Item index inside of selected table.
  399. *
  400. * @retval #MAC_INVALID_INDEX if passed index is not found in the table or exceeds
  401. * the allowed maximum.
  402. * @retval #MAC_SUCCESS if no errors happen during removing.
  403. */
  404. mac_status_t mac_table_item_remove(mac_table_t * p_table,
  405. mlme_pib_attr_id_t id,
  406. uint8_t idx);
  407. /**
  408. * @brief Gets first available item from a table.
  409. *
  410. * @details This function might be used along with \a mac_table_item_next to
  411. * search through some table.
  412. *
  413. * @param[in] p_table Pointer to a MAC table.
  414. *
  415. * @return Pointer to the first table item or NULL if the table is empty.
  416. */
  417. mac_table_item_t * mac_table_item_front(const mac_table_t * p_table);
  418. /**
  419. * @brief Returns the next available item in table.
  420. *
  421. * @details MAC tables are stored unsorted in memory, so there is no guarantee that
  422. * index of the next item is always greater or smaller than the current one.
  423. * Items are not stored in chronological order either.
  424. *
  425. * @param[in] p_table Pointer to a table to select item from.
  426. * @param[in] p_current_item Pointer to the current item.
  427. *
  428. * @return Pointer to the next item in table or NULL, if the item is the last one.
  429. */
  430. mac_table_item_t * mac_table_item_next(const mac_table_t * p_table,
  431. const mac_table_item_t * p_current_item);
  432. /**
  433. * @brief Gets number of items used inside mac_table_t.
  434. *
  435. * @param[in] p_table Pointer to interested table.
  436. *
  437. * @return 8-bit integer equal to number of items inside the table that have
  438. * been set at least once.
  439. */
  440. static inline uint8_t mac_table_size_get(const mac_table_t * p_table)
  441. {
  442. ASSERT(p_table != NULL);
  443. return p_table->size;
  444. }
  445. /**
  446. * @brief This function checks if a MAC table is empty.
  447. *
  448. * @param[in] p_table Pointer to a MAC table.
  449. *
  450. * @return true if there are no items inside table, false otherwise.
  451. */
  452. static inline bool mac_table_is_empty(const mac_table_t * p_table)
  453. {
  454. ASSERT(p_table != NULL);
  455. return sys_queue_is_empty(&p_table->queue);
  456. }
  457. #endif
  458. /** @} */
  459. #endif // MAC_MLME_PIB_H_INCLUDED