esp_now.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. bool dcm; /**< ESPNOW using dcm rate to send frame*/
  85. } esp_now_rate_config_t;
  86. /**
  87. * @brief Callback function of receiving ESPNOW data
  88. * @param esp_now_info received ESPNOW packet information
  89. * @param data received data
  90. * @param data_len length of received data
  91. * @attention esp_now_info is a local variable,it can only be used in the callback.
  92. */
  93. typedef void (*esp_now_recv_cb_t)(const esp_now_recv_info_t * esp_now_info, const uint8_t *data, int data_len);
  94. /**
  95. * @brief Callback function of sending ESPNOW data
  96. * @param mac_addr peer MAC address
  97. * @param status status of sending ESPNOW data (succeed or fail)
  98. */
  99. typedef void (*esp_now_send_cb_t)(const uint8_t *mac_addr, esp_now_send_status_t status);
  100. /**
  101. * @brief Initialize ESPNOW function
  102. *
  103. * @return
  104. * - ESP_OK : succeed
  105. * - ESP_ERR_ESPNOW_INTERNAL : Internal error
  106. */
  107. esp_err_t esp_now_init(void);
  108. /**
  109. * @brief De-initialize ESPNOW function
  110. *
  111. * @return
  112. * - ESP_OK : succeed
  113. */
  114. esp_err_t esp_now_deinit(void);
  115. /**
  116. * @brief Get the version of ESPNOW
  117. *
  118. * @param version ESPNOW version
  119. *
  120. * @return
  121. * - ESP_OK : succeed
  122. * - ESP_ERR_ESPNOW_ARG : invalid argument
  123. */
  124. esp_err_t esp_now_get_version(uint32_t *version);
  125. /**
  126. * @brief Register callback function of receiving ESPNOW data
  127. *
  128. * @param cb callback function of receiving ESPNOW data
  129. *
  130. * @return
  131. * - ESP_OK : succeed
  132. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  133. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  134. */
  135. esp_err_t esp_now_register_recv_cb(esp_now_recv_cb_t cb);
  136. /**
  137. * @brief Unregister callback function of receiving ESPNOW data
  138. *
  139. * @return
  140. * - ESP_OK : succeed
  141. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  142. */
  143. esp_err_t esp_now_unregister_recv_cb(void);
  144. /**
  145. * @brief Register callback function of sending ESPNOW data
  146. *
  147. * @param cb callback function of sending ESPNOW data
  148. *
  149. * @return
  150. * - ESP_OK : succeed
  151. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  152. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  153. */
  154. esp_err_t esp_now_register_send_cb(esp_now_send_cb_t cb);
  155. /**
  156. * @brief Unregister callback function of sending ESPNOW data
  157. *
  158. * @return
  159. * - ESP_OK : succeed
  160. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  161. */
  162. esp_err_t esp_now_unregister_send_cb(void);
  163. /**
  164. * @brief Send ESPNOW data
  165. *
  166. * @attention 1. If peer_addr is not NULL, send data to the peer whose MAC address matches peer_addr
  167. * @attention 2. If peer_addr is NULL, send data to all of the peers that are added to the peer list
  168. * @attention 3. The maximum length of data must be less than ESP_NOW_MAX_DATA_LEN
  169. * @attention 4. The buffer pointed to by data argument does not need to be valid after esp_now_send returns
  170. *
  171. * @param peer_addr peer MAC address
  172. * @param data data to send
  173. * @param len length of data
  174. *
  175. * @return
  176. * - ESP_OK : succeed
  177. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  178. * - ESP_ERR_ESPNOW_ARG : invalid argument
  179. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  180. * - ESP_ERR_ESPNOW_NO_MEM : out of memory, when this happens, you can delay a while before sending the next data
  181. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  182. * - ESP_ERR_ESPNOW_IF : current WiFi interface doesn't match that of peer
  183. */
  184. esp_err_t esp_now_send(const uint8_t *peer_addr, const uint8_t *data, size_t len);
  185. /**
  186. * @brief Add a peer to peer list
  187. *
  188. * @param peer peer information
  189. *
  190. * @return
  191. * - ESP_OK : succeed
  192. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  193. * - ESP_ERR_ESPNOW_ARG : invalid argument
  194. * - ESP_ERR_ESPNOW_FULL : peer list is full
  195. * - ESP_ERR_ESPNOW_NO_MEM : out of memory
  196. * - ESP_ERR_ESPNOW_EXIST : peer has existed
  197. */
  198. esp_err_t esp_now_add_peer(const esp_now_peer_info_t *peer);
  199. /**
  200. * @brief Delete a peer from peer list
  201. *
  202. * @param peer_addr peer MAC address
  203. *
  204. * @return
  205. * - ESP_OK : succeed
  206. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  207. * - ESP_ERR_ESPNOW_ARG : invalid argument
  208. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  209. */
  210. esp_err_t esp_now_del_peer(const uint8_t *peer_addr);
  211. /**
  212. * @brief Modify a peer
  213. *
  214. * @param peer peer information
  215. *
  216. * @return
  217. * - ESP_OK : succeed
  218. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  219. * - ESP_ERR_ESPNOW_ARG : invalid argument
  220. * - ESP_ERR_ESPNOW_FULL : peer list is full
  221. */
  222. esp_err_t esp_now_mod_peer(const esp_now_peer_info_t *peer);
  223. /**
  224. * @brief Config ESPNOW rate of specified interface
  225. *
  226. * @deprecated please use esp_now_set_peer_rate_config() instead.
  227. *
  228. * @attention 1. This API should be called after esp_wifi_start().
  229. * @attention 2. This API only work when not use Wi-Fi 6 and esp_now_set_peer_rate_config() not called.
  230. *
  231. * @param ifx Interface to be configured.
  232. * @param rate Phy rate to be configured.
  233. *
  234. * @return
  235. * - ESP_OK: succeed
  236. * - others: failed
  237. */
  238. esp_err_t esp_wifi_config_espnow_rate(wifi_interface_t ifx, wifi_phy_rate_t rate)
  239. __attribute__((deprecated("This API can be only used when rate is non-HE rate, \
  240. please use esp_now_set_peer_rate_config if you want full support of the rate.")));
  241. /**
  242. * @brief Set ESPNOW rate config for each peer
  243. *
  244. * @attention 1. This API should be called after esp_wifi_start() and esp_now_init().
  245. *
  246. * @param peer_addr peer MAC address
  247. * @param config rate config to be configured.
  248. *
  249. * @return
  250. * - ESP_OK : succeed
  251. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  252. * - ESP_ERR_ESPNOW_ARG : invalid argument
  253. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  254. */
  255. esp_err_t esp_now_set_peer_rate_config(const uint8_t *peer_addr, esp_now_rate_config_t *config);
  256. /**
  257. * @brief Get a peer whose MAC address matches peer_addr from peer list
  258. *
  259. * @param peer_addr peer MAC address
  260. * @param peer peer information
  261. *
  262. * @return
  263. * - ESP_OK : succeed
  264. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  265. * - ESP_ERR_ESPNOW_ARG : invalid argument
  266. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  267. */
  268. esp_err_t esp_now_get_peer(const uint8_t *peer_addr, esp_now_peer_info_t *peer);
  269. /**
  270. * @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.
  271. *
  272. * @param from_head fetch from head of list or not
  273. * @param peer peer information
  274. *
  275. * @return
  276. * - ESP_OK : succeed
  277. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  278. * - ESP_ERR_ESPNOW_ARG : invalid argument
  279. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  280. */
  281. esp_err_t esp_now_fetch_peer(bool from_head, esp_now_peer_info_t *peer);
  282. /**
  283. * @brief Peer exists or not
  284. *
  285. * @param peer_addr peer MAC address
  286. *
  287. * @return
  288. * - true : peer exists
  289. * - false : peer not exists
  290. */
  291. bool esp_now_is_peer_exist(const uint8_t *peer_addr);
  292. /**
  293. * @brief Get the number of peers
  294. *
  295. * @param num number of peers
  296. *
  297. * @return
  298. * - ESP_OK : succeed
  299. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  300. * - ESP_ERR_ESPNOW_ARG : invalid argument
  301. */
  302. esp_err_t esp_now_get_peer_num(esp_now_peer_num_t *num);
  303. /**
  304. * @brief Set the primary master key
  305. *
  306. * @param pmk primary master key
  307. *
  308. * @attention 1. primary master key is used to encrypt local master key
  309. *
  310. * @return
  311. * - ESP_OK : succeed
  312. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  313. * - ESP_ERR_ESPNOW_ARG : invalid argument
  314. */
  315. esp_err_t esp_now_set_pmk(const uint8_t *pmk);
  316. /**
  317. * @brief Set wake window for esp_now to wake up in interval unit
  318. *
  319. * @param window Milliseconds would the chip keep waked each interval, from 0 to 65535.
  320. *
  321. * @attention 1. This configuration could work at connected status.
  322. * When ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is enabled, this configuration could work at disconnected status.
  323. * @attention 2. Default value is the maximum.
  324. *
  325. * @return
  326. * - ESP_OK : succeed
  327. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  328. */
  329. esp_err_t esp_now_set_wake_window(uint16_t window);
  330. /**
  331. * @}
  332. */
  333. /**
  334. * @}
  335. */
  336. #ifdef __cplusplus
  337. }
  338. #endif
  339. #endif /* __ESP_NOW_H__ */