esp_ieee802154.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "esp_ieee802154_types.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @brief Initialize the IEEE 802.15.4 subsystem.
  16. *
  17. * @return
  18. * - ESP_OK on success.
  19. * - ESP_FAIL on failure.
  20. *
  21. */
  22. esp_err_t esp_ieee802154_enable(void);
  23. /**
  24. * @brief Deinitialize the IEEE 802.15.4 subsystem.
  25. *
  26. * @return
  27. * - ESP_OK on success.
  28. * - ESP_FAIL on failure.
  29. */
  30. esp_err_t esp_ieee802154_disable(void);
  31. /**
  32. * @brief Get the operational channel.
  33. *
  34. * @return The channel number (11~26).
  35. *
  36. */
  37. uint8_t esp_ieee802154_get_channel(void);
  38. /**
  39. * @brief Set the operational channel.
  40. *
  41. * @param[in] channel The channel number (11-26).
  42. *
  43. * @return
  44. * - ESP_OK on success.
  45. * - ESP_FAIL on failure.
  46. */
  47. esp_err_t esp_ieee802154_set_channel(uint8_t channel);
  48. /**
  49. * @brief Get the transmit power.
  50. *
  51. * @return The transmit power in dBm.
  52. *
  53. */
  54. int8_t esp_ieee802154_get_txpower(void);
  55. /**
  56. * @brief Set the transmit power.
  57. *
  58. * @param[in] power The transmit power in dBm.
  59. *
  60. * @return
  61. * - ESP_OK on success.
  62. * - ESP_FAIL on failure.
  63. */
  64. esp_err_t esp_ieee802154_set_txpower(int8_t power);
  65. /**
  66. * @brief Get the promiscuous mode.
  67. *
  68. * @return
  69. * - True The promiscuous mode is enabled.
  70. * - False The promiscuous mode is disabled.
  71. *
  72. */
  73. bool esp_ieee802154_get_promiscuous(void);
  74. /**
  75. * @brief Set the promiscuous mode.
  76. *
  77. * @param[in] enable The promiscuous mode to be set.
  78. *
  79. * @return
  80. * - ESP_OK on success.
  81. * - ESP_FAIL on failure.
  82. */
  83. esp_err_t esp_ieee802154_set_promiscuous(bool enable);
  84. /**
  85. * @brief Get the IEEE 802.15.4 Radio state.
  86. *
  87. * @return The IEEE 802.15.4 Radio state, refer to esp_ieee802154_state_t.
  88. *
  89. */
  90. esp_ieee802154_state_t esp_ieee802154_get_state(void);
  91. /**
  92. * @brief Set the IEEE 802.15.4 Radio to sleep state.
  93. *
  94. * @return
  95. * - ESP_OK on success.
  96. * - ESP_FAIL on failure due to invalid state.
  97. *
  98. */
  99. esp_err_t esp_ieee802154_sleep(void);
  100. /**
  101. * @brief Set the IEEE 802.15.4 Radio to receive state.
  102. *
  103. * @return
  104. * - ESP_OK on success
  105. * - ESP_FAIL on failure due to invalid state.
  106. *
  107. * Note: Radio will continue receiving until it receives a valid frame.
  108. * Ref to esp_ieee802154_receive_done().
  109. *
  110. */
  111. esp_err_t esp_ieee802154_receive(void);
  112. /**
  113. * @brief Transmit the given frame.
  114. *
  115. * @param[in] frame The pointer to the frame, the frame format:
  116. * |-----------------------------------------------------------------------|
  117. * | Len | MHR | MAC Payload | FCS |
  118. * |-----------------------------------------------------------------------|
  119. * @param[in] cca Perform CCA before transmission if it's true, otherwise transmit the frame directly.
  120. *
  121. * @return
  122. * - ESP_OK on success.
  123. * - ESP_FAIL on failure due to invalid state.
  124. *
  125. * Note: The transmit result will be reported via esp_ieee802154_transmit_done()
  126. * or esp_ieee802154_transmit_failed().
  127. *
  128. */
  129. esp_err_t esp_ieee802154_transmit(const uint8_t *frame, bool cca);
  130. /**
  131. * @brief Set the time to wait for the ack frame.
  132. *
  133. * @param[in] timeout The time to wait for the ack frame, in symbol unit (16 us).
  134. * Default: 0x006C, Range: 0x0000 - 0xFFFF.
  135. *
  136. * @return
  137. * - ESP_OK on success.
  138. * - ESP_FAIL on failure.
  139. */
  140. esp_err_t esp_ieee802154_set_ack_timeout(uint32_t timeout);
  141. /**
  142. * @brief Get the device PAN ID.
  143. *
  144. * @return The device PAN ID.
  145. *
  146. */
  147. uint16_t esp_ieee802154_get_panid(void);
  148. /**
  149. * @brief Set the device PAN ID.
  150. *
  151. * @param[in] panid The device PAN ID.
  152. *
  153. * @return
  154. * - ESP_OK on success.
  155. * - ESP_FAIL on failure.
  156. */
  157. esp_err_t esp_ieee802154_set_panid(uint16_t panid);
  158. /**
  159. * @brief Get the device short address.
  160. *
  161. * @return The device short address.
  162. *
  163. */
  164. uint16_t esp_ieee802154_get_short_address(void);
  165. /**
  166. * @brief Set the device short address.
  167. *
  168. * @param[in] short_address The device short address.
  169. *
  170. * @return
  171. * - ESP_OK on success.
  172. * - ESP_FAIL on failure.
  173. */
  174. esp_err_t esp_ieee802154_set_short_address(uint16_t short_address);
  175. /**
  176. * @brief Get the device extended address.
  177. *
  178. * @param[out] ext_addr The pointer to the device extended address.
  179. *
  180. * @return
  181. * - ESP_OK on success.
  182. * - ESP_FAIL on failure.
  183. */
  184. esp_err_t esp_ieee802154_get_extended_address(uint8_t *ext_addr);
  185. /**
  186. * @brief Set the device extended address.
  187. *
  188. * @param[in] ext_addr The pointer to the device extended address.
  189. *
  190. * @return
  191. * - ESP_OK on success.
  192. * - ESP_FAIL on failure.
  193. */
  194. esp_err_t esp_ieee802154_set_extended_address(const uint8_t *ext_addr);
  195. /**
  196. * @brief Get the device PAN ID for specific interface.
  197. *
  198. * @param[in] index The interface index.
  199. *
  200. * @return The device PAN ID.
  201. *
  202. */
  203. uint16_t esp_ieee802154_get_multipan_panid(esp_ieee802154_multipan_index_t index);
  204. /**
  205. * @brief Set the device PAN ID for specific interface.
  206. *
  207. * @param[in] index The interface index.
  208. * @param[in] panid The device PAN ID.
  209. *
  210. * @return
  211. * - ESP_OK on success.
  212. * - ESP_FAIL on failure.
  213. */
  214. esp_err_t esp_ieee802154_set_multipan_panid(esp_ieee802154_multipan_index_t index, uint16_t panid);
  215. /**
  216. * @brief Get the device short address for specific interface.
  217. *
  218. * @param[in] index The interface index.
  219. *
  220. * @return The device short address.
  221. *
  222. */
  223. uint16_t esp_ieee802154_get_multipan_short_address(esp_ieee802154_multipan_index_t index);
  224. /**
  225. * @brief Set the device short address for specific interface.
  226. *
  227. * @param[in] index The interface index.
  228. * @param[in] short_address The device short address.
  229. *
  230. * @return
  231. * - ESP_OK on success.
  232. * - ESP_FAIL on failure.
  233. */
  234. esp_err_t esp_ieee802154_set_multipan_short_address(esp_ieee802154_multipan_index_t index, uint16_t short_address);
  235. /**
  236. * @brief Get the device extended address for specific interface.
  237. *
  238. * @param[in] index The interface index.
  239. * @param[out] ext_addr The pointer to the device extended address.
  240. *
  241. * @return
  242. * - ESP_OK on success.
  243. * - ESP_FAIL on failure.
  244. */
  245. esp_err_t esp_ieee802154_get_multipan_extended_address(esp_ieee802154_multipan_index_t index, uint8_t *ext_addr);
  246. /**
  247. * @brief Set the device extended address for specific interface.
  248. *
  249. * @param[in] index The interface index.
  250. * @param[in] ext_addr The pointer to the device extended address.
  251. *
  252. * @return
  253. * - ESP_OK on success.
  254. * - ESP_FAIL on failure.
  255. */
  256. esp_err_t esp_ieee802154_set_multipan_extended_address(esp_ieee802154_multipan_index_t index, const uint8_t *ext_addr);
  257. /**
  258. * @brief Get the device current multipan interface enable mask.
  259. *
  260. * @return Current multipan interface enable mask.
  261. *
  262. */
  263. uint8_t esp_ieee802154_get_multipan_enable(void);
  264. /**
  265. * @brief Enable specific interface for the device.
  266. *
  267. * As an example, call `esp_ieee802154_set_multipan_enable(BIT(ESP_IEEE802154_MULTIPAN_0) | BIT(ESP_IEEE802154_MULTIPAN_1));`
  268. * to enable multipan interface 0 and 1.
  269. *
  270. * @param[in] mask The multipan interface bit mask.
  271. *
  272. * @return
  273. * - ESP_OK on success.
  274. * - ESP_FAIL on failure.
  275. */
  276. esp_err_t esp_ieee802154_set_multipan_enable(uint8_t mask);
  277. /**
  278. * @brief Get the device coordinator.
  279. *
  280. * @return
  281. * - True The coordinator is enabled.
  282. * - False The coordinator is disabled.
  283. *
  284. */
  285. bool esp_ieee802154_get_coordinator(void);
  286. /**
  287. * @brief Set the device coordinator role.
  288. *
  289. * @param[in] enable The coordinator role to be set.
  290. *
  291. * @return
  292. * - ESP_OK on success.
  293. * - ESP_FAIL on failure.
  294. */
  295. esp_err_t esp_ieee802154_set_coordinator(bool enable);
  296. /**
  297. * @brief Get the auto frame pending mode.
  298. *
  299. * @return The auto frame pending mode, refer to esp_ieee802154_pending_mode_t.
  300. *
  301. */
  302. esp_ieee802154_pending_mode_t esp_ieee802154_get_pending_mode(void);
  303. /**
  304. * @brief Set the auto frame pending mode.
  305. *
  306. * @param[in] pending_mode The auto frame pending mode, refer to esp_ieee802154_pending_mode_t.
  307. *
  308. * @return
  309. * - ESP_OK on success.
  310. * - ESP_FAIL on failure.
  311. */
  312. esp_err_t esp_ieee802154_set_pending_mode(esp_ieee802154_pending_mode_t pending_mode);
  313. /**
  314. * @brief Add address to the source matching table.
  315. *
  316. * @param[in] addr The pointer to the address.
  317. * @param[in] is_short Short address or Extended address.
  318. *
  319. * @return
  320. * - ESP_OK on success.
  321. * - ESP_ERR_NO_MEM if the pending table is full.
  322. *
  323. */
  324. esp_err_t esp_ieee802154_add_pending_addr(const uint8_t *addr, bool is_short);
  325. /**
  326. * @brief Remove address from the source matching table.
  327. *
  328. * @param[in] addr The pointer to the address.
  329. * @param[in] is_short Short address or Extended address.
  330. *
  331. * @return
  332. * - ESP_OK on success.
  333. * - ESP_ERR_NOT_FOUND if the address was not found from the source matching table.
  334. *
  335. */
  336. esp_err_t esp_ieee802154_clear_pending_addr(const uint8_t *addr, bool is_short);
  337. /**
  338. * @brief Clear the source matching table to empty.
  339. *
  340. * @param[in] is_short Clear Short address table or Extended address table.
  341. *
  342. * @return
  343. * - ESP_OK on success.
  344. * - ESP_FAIL on failure.
  345. */
  346. esp_err_t esp_ieee802154_reset_pending_table(bool is_short);
  347. /**
  348. * @brief Get the CCA threshold.
  349. *
  350. * @return The CCA threshold in dBm.
  351. *
  352. */
  353. int8_t esp_ieee802154_get_cca_threshold(void);
  354. /**
  355. * @brief Set the CCA threshold.
  356. *
  357. * @param[in] cca_threshold The CCA threshold in dBm.
  358. *
  359. * @return
  360. * - ESP_OK on success.
  361. * - ESP_FAIL on failure.
  362. */
  363. esp_err_t esp_ieee802154_set_cca_threshold(int8_t cca_threshold);
  364. /**
  365. * @brief Get the CCA mode.
  366. *
  367. * @return The CCA mode, refer to esp_ieee802154_cca_mode_t.
  368. *
  369. */
  370. esp_ieee802154_cca_mode_t esp_ieee802154_get_cca_mode(void);
  371. /**
  372. * @brief Set the CCA mode.
  373. *
  374. * @param[in] cca_mode The CCA mode, refer to esp_ieee802154_cca_mode_t.
  375. *
  376. * @return
  377. * - ESP_OK on success.
  378. * - ESP_FAIL on failure.
  379. */
  380. esp_err_t esp_ieee802154_set_cca_mode(esp_ieee802154_cca_mode_t cca_mode);
  381. /**
  382. * @brief Enable rx_on_when_idle mode, radio will receive during idle.
  383. *
  384. * @param[in] enable Enable/Disable rx_on_when_idle mode.
  385. *
  386. * @return
  387. * - ESP_OK on success.
  388. * - ESP_FAIL on failure.
  389. */
  390. esp_err_t esp_ieee802154_set_rx_when_idle(bool enable);
  391. /**
  392. * @brief Get the rx_on_when_idle mode.
  393. *
  394. * @return rx_on_when_idle mode.
  395. *
  396. */
  397. bool esp_ieee802154_get_rx_when_idle(void);
  398. /**
  399. * @brief Perform energy detection.
  400. *
  401. * @param[in] duration The duration of energy detection, in symbol unit (16 us).
  402. * The result will be reported via esp_ieee802154_energy_detect_done().
  403. *
  404. * @return
  405. * - ESP_OK on success.
  406. * - ESP_FAIL on failure due to invalid state.
  407. *
  408. */
  409. esp_err_t esp_ieee802154_energy_detect(uint32_t duration);
  410. /** Below are the events generated by IEEE 802.15.4 subsystem, which are in ISR context **/
  411. /**
  412. * @brief A Frame was received.
  413. *
  414. * @param[in] frame The point to the received frame, frame format:
  415. * |-----------------------------------------------------------------------|
  416. * | Len | MHR | MAC Payload (no FCS) |
  417. * |-----------------------------------------------------------------------|
  418. * @param[in] frame_info More information of the received frame, refer to esp_ieee802154_frame_info_t.
  419. *
  420. */
  421. extern void esp_ieee802154_receive_done(uint8_t *frame, esp_ieee802154_frame_info_t *frame_info);
  422. /**
  423. * @brief The SFD field of the frame was received.
  424. *
  425. */
  426. extern void esp_ieee802154_receive_sfd_done(void);
  427. /**
  428. * @brief The Frame Transmission succeeded.
  429. *
  430. * @param[in] frame The pointer to the transmitted frame.
  431. * @param[in] ack The received ACK frame, it could be NULL if the transmitted frame's AR bit is not set.
  432. * @param[in] ack_frame_info More information of the ACK frame, refer to esp_ieee802154_frame_info_t.
  433. *
  434. * Note: refer to esp_ieee802154_transmit().
  435. *
  436. */
  437. extern void esp_ieee802154_transmit_done(const uint8_t *frame, const uint8_t *ack, esp_ieee802154_frame_info_t *ack_frame_info);
  438. /**
  439. * @brief The Frame Transmission failed.
  440. *
  441. * @param[in] frame The pointer to the frame.
  442. * @param[in] error The transmission failure reason, refer to esp_ieee802154_tx_error_t.
  443. *
  444. * Note: refer to esp_ieee802154_transmit().
  445. *
  446. */
  447. extern void esp_ieee802154_transmit_failed(const uint8_t *frame, esp_ieee802154_tx_error_t error);
  448. /**
  449. * @brief The SFD field of the frame was transmitted.
  450. *
  451. */
  452. extern void esp_ieee802154_transmit_sfd_done(uint8_t *frame);
  453. /**
  454. * @brief The energy detection done.
  455. *
  456. * @param[in] power The detected power level, in dBm.
  457. *
  458. * Note: refer to esp_ieee802154_energy_detect().
  459. *
  460. */
  461. extern void esp_ieee802154_energy_detect_done(int8_t power);
  462. /**
  463. * @brief Set the IEEE 802.15.4 Radio to receive state at a specific time.
  464. *
  465. *
  466. * @param[in] time A specific timestamp for starting receiving.
  467. * @return
  468. * - ESP_OK on success
  469. * - ESP_FAIL on failure due to invalid state.
  470. *
  471. * Note: Radio will start receiving after the timestamp, and continue receiving until it receives a valid frame.
  472. * Ref to esp_ieee802154_receive_done().
  473. *
  474. */
  475. esp_err_t esp_ieee802154_receive_at(uint32_t time);
  476. /**
  477. * @brief Transmit the given frame at a specific time.
  478. *
  479. * @param[in] frame The pointer to the frame. Refer to `esp_ieee802154_transmit()`.
  480. * @param[in] cca Perform CCA before transmission if it's true, otherwise transmit the frame directly.
  481. * @param[in] time A specific timestamp for starting transmission.
  482. *
  483. * @return
  484. * - ESP_OK on success.
  485. * - ESP_FAIL on failure due to invalid state.
  486. *
  487. * Note: The transmit result will be reported via esp_ieee802154_transmit_done()
  488. * or esp_ieee802154_transmit_failed().
  489. *
  490. */
  491. esp_err_t esp_ieee802154_transmit_at(const uint8_t *frame, bool cca, uint32_t time);
  492. /**
  493. * @brief Get the RSSI of the most recent received frame.
  494. *
  495. * @return The value of RSSI.
  496. *
  497. */
  498. int8_t esp_ieee802154_get_recent_rssi(void);
  499. /**
  500. * @brief Get the LQI of the most recent received frame.
  501. *
  502. * @return The value of LQI.
  503. *
  504. */
  505. uint8_t esp_ieee802154_get_recent_lqi(void);
  506. /**
  507. * @brief Set the key and addr for a frame needs to be encrypted by HW.
  508. *
  509. * @param[in] frame A frame needs to be encrypted. Refer to `esp_ieee802154_transmit()`.
  510. * @param[in] key A 16-bytes key for encryption.
  511. * @param[in] addr An 8-bytes addr for HW to generate nonce, in general, is the device extended address.
  512. *
  513. * @return
  514. * - ESP_OK on success.
  515. * - ESP_FAIL on failure.
  516. */
  517. esp_err_t esp_ieee802154_set_transmit_security(uint8_t *frame, uint8_t *key, uint8_t *addr);
  518. /**
  519. * @brief This function will be called when a received frame needs to be acked with Enh-Ack, the upper
  520. * layer should generate the Enh-Ack frame in this callback function.
  521. *
  522. * @param[in] frame The received frame.
  523. * @param[in] frame_info The frame information. Refer to `esp_ieee802154_frame_info_t`.
  524. * @param[out] enhack_frame The Enh-ack frame need to be generated via this function, HW will send it back after AIFS.
  525. *
  526. * @return
  527. * - ESP_OK if Enh-Ack generates done.
  528. * - ESP_FAIL if Enh-Ack generates failed.
  529. *
  530. */
  531. esp_err_t esp_ieee802154_enh_ack_generator(uint8_t *frame, esp_ieee802154_frame_info_t *frame_info, uint8_t* enhack_frame);
  532. #ifdef __cplusplus
  533. }
  534. #endif