esp_now.h 11 KB

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