esp_now.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __ESP_NOW_H__
  7. #define __ESP_NOW_H__
  8. #include <stdbool.h>
  9. #include "esp_err.h"
  10. #include "esp_wifi_types.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /** \defgroup WiFi_APIs WiFi Related APIs
  15. * @brief WiFi APIs
  16. */
  17. /** @addtogroup WiFi_APIs
  18. * @{
  19. */
  20. /** \defgroup ESPNOW_APIs ESPNOW APIs
  21. * @brief ESP32 ESPNOW APIs
  22. *
  23. */
  24. /** @addtogroup ESPNOW_APIs
  25. * @{
  26. */
  27. #define ESP_ERR_ESPNOW_BASE (ESP_ERR_WIFI_BASE + 100) /*!< ESPNOW error number base. */
  28. #define ESP_ERR_ESPNOW_NOT_INIT (ESP_ERR_ESPNOW_BASE + 1) /*!< ESPNOW is not initialized. */
  29. #define ESP_ERR_ESPNOW_ARG (ESP_ERR_ESPNOW_BASE + 2) /*!< Invalid argument */
  30. #define ESP_ERR_ESPNOW_NO_MEM (ESP_ERR_ESPNOW_BASE + 3) /*!< Out of memory */
  31. #define ESP_ERR_ESPNOW_FULL (ESP_ERR_ESPNOW_BASE + 4) /*!< ESPNOW peer list is full */
  32. #define ESP_ERR_ESPNOW_NOT_FOUND (ESP_ERR_ESPNOW_BASE + 5) /*!< ESPNOW peer is not found */
  33. #define ESP_ERR_ESPNOW_INTERNAL (ESP_ERR_ESPNOW_BASE + 6) /*!< Internal error */
  34. #define ESP_ERR_ESPNOW_EXIST (ESP_ERR_ESPNOW_BASE + 7) /*!< ESPNOW peer has existed */
  35. #define ESP_ERR_ESPNOW_IF (ESP_ERR_ESPNOW_BASE + 8) /*!< Interface error */
  36. #define ESP_NOW_ETH_ALEN 6 /*!< Length of ESPNOW peer MAC address */
  37. #define ESP_NOW_KEY_LEN 16 /*!< Length of ESPNOW peer local master key */
  38. #define ESP_NOW_MAX_TOTAL_PEER_NUM 20 /*!< Maximum number of ESPNOW total peers */
  39. #define ESP_NOW_MAX_ENCRYPT_PEER_NUM 6 /*!< Maximum number of ESPNOW encrypted peers */
  40. #define ESP_NOW_MAX_DATA_LEN 250 /*!< Maximum length of ESPNOW data which is sent very time */
  41. /**
  42. * @brief Status of sending ESPNOW data .
  43. */
  44. typedef enum {
  45. ESP_NOW_SEND_SUCCESS = 0, /**< Send ESPNOW data successfully */
  46. ESP_NOW_SEND_FAIL, /**< Send ESPNOW data fail */
  47. } esp_now_send_status_t;
  48. /**
  49. * @brief ESPNOW peer information parameters.
  50. */
  51. typedef struct esp_now_peer_info {
  52. uint8_t peer_addr[ESP_NOW_ETH_ALEN]; /**< ESPNOW peer MAC address that is also the MAC address of station or softap */
  53. uint8_t lmk[ESP_NOW_KEY_LEN]; /**< ESPNOW peer local master key that is used to encrypt data */
  54. uint8_t channel; /**< Wi-Fi channel that peer uses to send/receive ESPNOW data. If the value is 0,
  55. use the current channel which station or softap is on. Otherwise, it must be
  56. set as the channel that station or softap is on. */
  57. wifi_interface_t ifidx; /**< Wi-Fi interface that peer uses to send/receive ESPNOW data */
  58. bool encrypt; /**< ESPNOW data that this peer sends/receives is encrypted or not */
  59. void *priv; /**< ESPNOW peer private data */
  60. } esp_now_peer_info_t;
  61. /**
  62. * @brief Number of ESPNOW peers which exist currently.
  63. */
  64. typedef struct esp_now_peer_num {
  65. int total_num; /**< Total number of ESPNOW peers, maximum value is ESP_NOW_MAX_TOTAL_PEER_NUM */
  66. int encrypt_num; /**< Number of encrypted ESPNOW peers, maximum value is ESP_NOW_MAX_ENCRYPT_PEER_NUM */
  67. } esp_now_peer_num_t;
  68. /**
  69. * @brief ESPNOW packet information
  70. */
  71. typedef struct esp_now_recv_info {
  72. uint8_t * src_addr; /**< Source address of ESPNOW packet */
  73. uint8_t * des_addr; /**< Destination address of ESPNOW packet */
  74. wifi_pkt_rx_ctrl_t * rx_ctrl; /**< Rx control info of ESPNOW packet */
  75. } esp_now_recv_info_t;
  76. /**
  77. * @brief ESPNOW rate config
  78. *
  79. */
  80. typedef struct esp_now_rate_config {
  81. wifi_phy_mode_t phymode; /**< ESPNOW phymode of specified interface */
  82. wifi_phy_rate_t rate; /**< ESPNOW rate of specified interface*/
  83. bool ersu; /**< ESPNOW using ersu send frame*/
  84. } esp_now_rate_config_t;
  85. /**
  86. * @brief Callback function of receiving ESPNOW data
  87. * @param esp_now_info received ESPNOW packet information
  88. * @param data received data
  89. * @param data_len length of received data
  90. * @attention esp_now_info is a local variable,it can only be used in the callback.
  91. */
  92. typedef void (*esp_now_recv_cb_t)(const esp_now_recv_info_t * esp_now_info, const uint8_t *data, int data_len);
  93. /**
  94. * @brief Callback function of sending ESPNOW data
  95. * @param mac_addr peer MAC address
  96. * @param status status of sending ESPNOW data (succeed or fail)
  97. */
  98. typedef void (*esp_now_send_cb_t)(const uint8_t *mac_addr, esp_now_send_status_t status);
  99. /**
  100. * @brief Initialize ESPNOW function
  101. *
  102. * @return
  103. * - ESP_OK : succeed
  104. * - ESP_ERR_ESPNOW_INTERNAL : Internal error
  105. */
  106. esp_err_t esp_now_init(void);
  107. /**
  108. * @brief De-initialize ESPNOW function
  109. *
  110. * @return
  111. * - ESP_OK : succeed
  112. */
  113. esp_err_t esp_now_deinit(void);
  114. /**
  115. * @brief Get the version of ESPNOW
  116. *
  117. * @param version ESPNOW version
  118. *
  119. * @return
  120. * - ESP_OK : succeed
  121. * - ESP_ERR_ESPNOW_ARG : invalid argument
  122. */
  123. esp_err_t esp_now_get_version(uint32_t *version);
  124. /**
  125. * @brief Register callback function of receiving ESPNOW data
  126. *
  127. * @param cb callback function of receiving ESPNOW data
  128. *
  129. * @return
  130. * - ESP_OK : succeed
  131. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  132. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  133. */
  134. esp_err_t esp_now_register_recv_cb(esp_now_recv_cb_t cb);
  135. /**
  136. * @brief Unregister callback function of receiving ESPNOW data
  137. *
  138. * @return
  139. * - ESP_OK : succeed
  140. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  141. */
  142. esp_err_t esp_now_unregister_recv_cb(void);
  143. /**
  144. * @brief Register callback function of sending ESPNOW data
  145. *
  146. * @param cb callback function of sending ESPNOW data
  147. *
  148. * @return
  149. * - ESP_OK : succeed
  150. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  151. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  152. */
  153. esp_err_t esp_now_register_send_cb(esp_now_send_cb_t cb);
  154. /**
  155. * @brief Unregister callback function of sending ESPNOW data
  156. *
  157. * @return
  158. * - ESP_OK : succeed
  159. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  160. */
  161. esp_err_t esp_now_unregister_send_cb(void);
  162. /**
  163. * @brief Send ESPNOW data
  164. *
  165. * @attention 1. If peer_addr is not NULL, send data to the peer whose MAC address matches peer_addr
  166. * @attention 2. If peer_addr is NULL, send data to all of the peers that are added to the peer list
  167. * @attention 3. The maximum length of data must be less than ESP_NOW_MAX_DATA_LEN
  168. * @attention 4. The buffer pointed to by data argument does not need to be valid after esp_now_send returns
  169. *
  170. * @param peer_addr peer MAC address
  171. * @param data data to send
  172. * @param len length of data
  173. *
  174. * @return
  175. * - ESP_OK : succeed
  176. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  177. * - ESP_ERR_ESPNOW_ARG : invalid argument
  178. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  179. * - ESP_ERR_ESPNOW_NO_MEM : out of memory, when this happens, you can delay a while before sending the next data
  180. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  181. * - ESP_ERR_ESPNOW_IF : current WiFi interface doesn't match that of peer
  182. */
  183. esp_err_t esp_now_send(const uint8_t *peer_addr, const uint8_t *data, size_t len);
  184. /**
  185. * @brief Add a peer to peer list
  186. *
  187. * @param peer peer information
  188. *
  189. * @return
  190. * - ESP_OK : succeed
  191. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  192. * - ESP_ERR_ESPNOW_ARG : invalid argument
  193. * - ESP_ERR_ESPNOW_FULL : peer list is full
  194. * - ESP_ERR_ESPNOW_NO_MEM : out of memory
  195. * - ESP_ERR_ESPNOW_EXIST : peer has existed
  196. */
  197. esp_err_t esp_now_add_peer(const esp_now_peer_info_t *peer);
  198. /**
  199. * @brief Delete a peer from peer list
  200. *
  201. * @param peer_addr peer MAC address
  202. *
  203. * @return
  204. * - ESP_OK : succeed
  205. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  206. * - ESP_ERR_ESPNOW_ARG : invalid argument
  207. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  208. */
  209. esp_err_t esp_now_del_peer(const uint8_t *peer_addr);
  210. /**
  211. * @brief Modify a peer
  212. *
  213. * @param peer peer information
  214. *
  215. * @return
  216. * - ESP_OK : succeed
  217. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  218. * - ESP_ERR_ESPNOW_ARG : invalid argument
  219. * - ESP_ERR_ESPNOW_FULL : peer list is full
  220. */
  221. esp_err_t esp_now_mod_peer(const esp_now_peer_info_t *peer);
  222. /**
  223. * @brief Config ESPNOW rate of specified interface
  224. *
  225. * @deprecated please use esp_now_set_peer_rate_config() instead.
  226. *
  227. * @attention 1. This API should be called after esp_wifi_start().
  228. * @attention 2. This API only work when not use Wi-Fi 6 and esp_now_set_peer_rate_config() not called.
  229. *
  230. * @param ifx Interface to be configured.
  231. * @param rate Phy rate to be configured.
  232. *
  233. * @return
  234. * - ESP_OK: succeed
  235. * - others: failed
  236. */
  237. esp_err_t esp_wifi_config_espnow_rate(wifi_interface_t ifx, wifi_phy_rate_t rate);
  238. /**
  239. * @brief Set ESPNOW rate config for each peer
  240. *
  241. * @attention 1. This API should be called after esp_wifi_start() and esp_now_init().
  242. *
  243. * @param peer_addr peer MAC address
  244. * @param config rate config to be configured.
  245. *
  246. * @return
  247. * - ESP_OK : succeed
  248. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  249. * - ESP_ERR_ESPNOW_ARG : invalid argument
  250. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  251. */
  252. esp_err_t esp_now_set_peer_rate_config(const uint8_t *peer_addr, esp_now_rate_config_t *config);
  253. /**
  254. * @brief Get a peer whose MAC address matches peer_addr from peer list
  255. *
  256. * @param peer_addr peer MAC address
  257. * @param peer peer information
  258. *
  259. * @return
  260. * - ESP_OK : succeed
  261. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  262. * - ESP_ERR_ESPNOW_ARG : invalid argument
  263. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  264. */
  265. esp_err_t esp_now_get_peer(const uint8_t *peer_addr, esp_now_peer_info_t *peer);
  266. /**
  267. * @brief Fetch a peer from peer list. Only return the peer which address is unicast, for the multicast/broadcast address, the function will ignore and try to find the next in the peer list.
  268. *
  269. * @param from_head fetch from head of list or not
  270. * @param peer peer information
  271. *
  272. * @return
  273. * - ESP_OK : succeed
  274. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  275. * - ESP_ERR_ESPNOW_ARG : invalid argument
  276. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  277. */
  278. esp_err_t esp_now_fetch_peer(bool from_head, esp_now_peer_info_t *peer);
  279. /**
  280. * @brief Peer exists or not
  281. *
  282. * @param peer_addr peer MAC address
  283. *
  284. * @return
  285. * - true : peer exists
  286. * - false : peer not exists
  287. */
  288. bool esp_now_is_peer_exist(const uint8_t *peer_addr);
  289. /**
  290. * @brief Get the number of peers
  291. *
  292. * @param num number of peers
  293. *
  294. * @return
  295. * - ESP_OK : succeed
  296. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  297. * - ESP_ERR_ESPNOW_ARG : invalid argument
  298. */
  299. esp_err_t esp_now_get_peer_num(esp_now_peer_num_t *num);
  300. /**
  301. * @brief Set the primary master key
  302. *
  303. * @param pmk primary master key
  304. *
  305. * @attention 1. primary master key is used to encrypt local master key
  306. *
  307. * @return
  308. * - ESP_OK : succeed
  309. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  310. * - ESP_ERR_ESPNOW_ARG : invalid argument
  311. */
  312. esp_err_t esp_now_set_pmk(const uint8_t *pmk);
  313. /**
  314. * @brief Set wake window for esp_now to wake up in interval unit
  315. *
  316. * @param window Milliseconds would the chip keep waked each interval, from 0 to 65535.
  317. *
  318. * @attention 1. This configuration could work at connected status.
  319. * When ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is enabled, this configuration could work at disconnected status.
  320. * @attention 2. Default value is the maximum.
  321. *
  322. * @return
  323. * - ESP_OK : succeed
  324. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  325. */
  326. esp_err_t esp_now_set_wake_window(uint16_t window);
  327. /**
  328. * @}
  329. */
  330. /**
  331. * @}
  332. */
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif /* __ESP_NOW_H__ */