esp_now.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef __ESP_NOW_H__
  14. #define __ESP_NOW_H__
  15. #include <stdbool.h>
  16. #include "esp_err.h"
  17. #include "esp_wifi_types.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /** \defgroup WiFi_APIs WiFi Related APIs
  22. * @brief WiFi APIs
  23. */
  24. /** @addtogroup WiFi_APIs
  25. * @{
  26. */
  27. /** \defgroup ESPNOW_APIs ESPNOW APIs
  28. * @brief ESP32 ESPNOW APIs
  29. *
  30. */
  31. /** @addtogroup ESPNOW_APIs
  32. * @{
  33. */
  34. #define ESP_ERR_ESPNOW_BASE (ESP_ERR_WIFI_BASE + 100) /*!< ESPNOW error number base. */
  35. #define ESP_ERR_ESPNOW_NOT_INIT (ESP_ERR_ESPNOW_BASE + 1) /*!< ESPNOW is not initialized. */
  36. #define ESP_ERR_ESPNOW_ARG (ESP_ERR_ESPNOW_BASE + 2) /*!< Invalid argument */
  37. #define ESP_ERR_ESPNOW_NO_MEM (ESP_ERR_ESPNOW_BASE + 3) /*!< Out of memory */
  38. #define ESP_ERR_ESPNOW_FULL (ESP_ERR_ESPNOW_BASE + 4) /*!< ESPNOW peer list is full */
  39. #define ESP_ERR_ESPNOW_NOT_FOUND (ESP_ERR_ESPNOW_BASE + 5) /*!< ESPNOW peer is not found */
  40. #define ESP_ERR_ESPNOW_INTERNAL (ESP_ERR_ESPNOW_BASE + 6) /*!< Internal error */
  41. #define ESP_ERR_ESPNOW_EXIST (ESP_ERR_ESPNOW_BASE + 7) /*!< ESPNOW peer has existed */
  42. #define ESP_ERR_ESPNOW_IF (ESP_ERR_ESPNOW_BASE + 8) /*!< Interface error */
  43. #define ESP_NOW_ETH_ALEN 6 /*!< Length of ESPNOW peer MAC address */
  44. #define ESP_NOW_KEY_LEN 16 /*!< Length of ESPNOW peer local master key */
  45. #define ESP_NOW_MAX_TOTAL_PEER_NUM 20 /*!< Maximum number of ESPNOW total peers */
  46. #define ESP_NOW_MAX_ENCRYPT_PEER_NUM 6 /*!< Maximum number of ESPNOW encrypted peers */
  47. #define ESP_NOW_MAX_DATA_LEN 250 /*!< Maximum length of ESPNOW data which is sent very time */
  48. /**
  49. * @brief Status of sending ESPNOW data .
  50. */
  51. typedef enum {
  52. ESP_NOW_SEND_SUCCESS = 0, /**< Send ESPNOW data successfully */
  53. ESP_NOW_SEND_FAIL, /**< Send ESPNOW data fail */
  54. } esp_now_send_status_t;
  55. /**
  56. * @brief ESPNOW peer information parameters.
  57. */
  58. typedef struct esp_now_peer_info {
  59. uint8_t peer_addr[ESP_NOW_ETH_ALEN]; /**< ESPNOW peer MAC address that is also the MAC address of station or softap */
  60. uint8_t lmk[ESP_NOW_KEY_LEN]; /**< ESPNOW peer local master key that is used to encrypt data */
  61. uint8_t channel; /**< Wi-Fi channel that peer uses to send/receive ESPNOW data. If the value is 0,
  62. use the current channel which station or softap is on. Otherwise, it must be
  63. set as the channel that station or softap is on. */
  64. wifi_interface_t ifidx; /**< Wi-Fi interface that peer uses to send/receive ESPNOW data */
  65. bool encrypt; /**< ESPNOW data that this peer sends/receives is encrypted or not */
  66. void *priv; /**< ESPNOW peer private data */
  67. } esp_now_peer_info_t;
  68. /**
  69. * @brief Number of ESPNOW peers which exist currently.
  70. */
  71. typedef struct esp_now_peer_num {
  72. int total_num; /**< Total number of ESPNOW peers, maximum value is ESP_NOW_MAX_TOTAL_PEER_NUM */
  73. int encrypt_num; /**< Number of encrypted ESPNOW peers, maximum value is ESP_NOW_MAX_ENCRYPT_PEER_NUM */
  74. } esp_now_peer_num_t;
  75. /**
  76. * @brief Callback function of receiving ESPNOW data
  77. * @param mac_addr peer MAC address
  78. * @param data received data
  79. * @param data_len length of received data
  80. */
  81. typedef void (*esp_now_recv_cb_t)(const uint8_t *mac_addr, const uint8_t *data, int data_len);
  82. /**
  83. * @brief Callback function of sending ESPNOW data
  84. * @param mac_addr peer MAC address
  85. * @param status status of sending ESPNOW data (succeed or fail)
  86. */
  87. typedef void (*esp_now_send_cb_t)(const uint8_t *mac_addr, esp_now_send_status_t status);
  88. /**
  89. * @brief Initialize ESPNOW function
  90. *
  91. * @return
  92. * - ESP_OK : succeed
  93. * - ESP_ERR_ESPNOW_INTERNAL : Internal error
  94. */
  95. esp_err_t esp_now_init(void);
  96. /**
  97. * @brief De-initialize ESPNOW function
  98. *
  99. * @return
  100. * - ESP_OK : succeed
  101. */
  102. esp_err_t esp_now_deinit(void);
  103. /**
  104. * @brief Get the version of ESPNOW
  105. *
  106. * @param version ESPNOW version
  107. *
  108. * @return
  109. * - ESP_OK : succeed
  110. * - ESP_ERR_ESPNOW_ARG : invalid argument
  111. */
  112. esp_err_t esp_now_get_version(uint32_t *version);
  113. /**
  114. * @brief Register callback function of receiving ESPNOW data
  115. *
  116. * @param cb callback function of receiving ESPNOW data
  117. *
  118. * @return
  119. * - ESP_OK : succeed
  120. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  121. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  122. */
  123. esp_err_t esp_now_register_recv_cb(esp_now_recv_cb_t cb);
  124. /**
  125. * @brief Unregister callback function of receiving ESPNOW data
  126. *
  127. * @return
  128. * - ESP_OK : succeed
  129. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  130. */
  131. esp_err_t esp_now_unregister_recv_cb(void);
  132. /**
  133. * @brief Register callback function of sending ESPNOW data
  134. *
  135. * @param cb callback function of sending ESPNOW data
  136. *
  137. * @return
  138. * - ESP_OK : succeed
  139. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  140. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  141. */
  142. esp_err_t esp_now_register_send_cb(esp_now_send_cb_t cb);
  143. /**
  144. * @brief Unregister callback function of sending ESPNOW data
  145. *
  146. * @return
  147. * - ESP_OK : succeed
  148. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  149. */
  150. esp_err_t esp_now_unregister_send_cb(void);
  151. /**
  152. * @brief Send ESPNOW data
  153. *
  154. * @attention 1. If peer_addr is not NULL, send data to the peer whose MAC address matches peer_addr
  155. * @attention 2. If peer_addr is NULL, send data to all of the peers that are added to the peer list
  156. * @attention 3. The maximum length of data must be less than ESP_NOW_MAX_DATA_LEN
  157. * @attention 4. The buffer pointed to by data argument does not need to be valid after esp_now_send returns
  158. *
  159. * @param peer_addr peer MAC address
  160. * @param data data to send
  161. * @param len length of data
  162. *
  163. * @return
  164. * - ESP_OK : succeed
  165. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  166. * - ESP_ERR_ESPNOW_ARG : invalid argument
  167. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  168. * - ESP_ERR_ESPNOW_NO_MEM : out of memory
  169. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  170. * - ESP_ERR_ESPNOW_IF : current WiFi interface doesn't match that of peer
  171. */
  172. esp_err_t esp_now_send(const uint8_t *peer_addr, const uint8_t *data, size_t len);
  173. /**
  174. * @brief Add a peer to peer list
  175. *
  176. * @param peer peer information
  177. *
  178. * @return
  179. * - ESP_OK : succeed
  180. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  181. * - ESP_ERR_ESPNOW_ARG : invalid argument
  182. * - ESP_ERR_ESPNOW_FULL : peer list is full
  183. * - ESP_ERR_ESPNOW_NO_MEM : out of memory
  184. * - ESP_ERR_ESPNOW_EXIST : peer has existed
  185. */
  186. esp_err_t esp_now_add_peer(const esp_now_peer_info_t *peer);
  187. /**
  188. * @brief Delete a peer from peer list
  189. *
  190. * @param peer_addr peer MAC address
  191. *
  192. * @return
  193. * - ESP_OK : succeed
  194. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  195. * - ESP_ERR_ESPNOW_ARG : invalid argument
  196. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  197. */
  198. esp_err_t esp_now_del_peer(const uint8_t *peer_addr);
  199. /**
  200. * @brief Modify a peer
  201. *
  202. * @param peer peer information
  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_FULL : peer list is full
  209. */
  210. esp_err_t esp_now_mod_peer(const esp_now_peer_info_t *peer);
  211. /**
  212. * @brief Get a peer whose MAC address matches peer_addr from peer list
  213. *
  214. * @param peer_addr peer MAC address
  215. * @param peer peer information
  216. *
  217. * @return
  218. * - ESP_OK : succeed
  219. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  220. * - ESP_ERR_ESPNOW_ARG : invalid argument
  221. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  222. */
  223. esp_err_t esp_now_get_peer(const uint8_t *peer_addr, esp_now_peer_info_t *peer);
  224. /**
  225. * @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.
  226. *
  227. * @param from_head fetch from head of list or not
  228. * @param peer peer information
  229. *
  230. * @return
  231. * - ESP_OK : succeed
  232. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  233. * - ESP_ERR_ESPNOW_ARG : invalid argument
  234. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  235. */
  236. esp_err_t esp_now_fetch_peer(bool from_head, esp_now_peer_info_t *peer);
  237. /**
  238. * @brief Peer exists or not
  239. *
  240. * @param peer_addr peer MAC address
  241. *
  242. * @return
  243. * - true : peer exists
  244. * - false : peer not exists
  245. */
  246. bool esp_now_is_peer_exist(const uint8_t *peer_addr);
  247. /**
  248. * @brief Get the number of peers
  249. *
  250. * @param num number of peers
  251. *
  252. * @return
  253. * - ESP_OK : succeed
  254. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  255. * - ESP_ERR_ESPNOW_ARG : invalid argument
  256. */
  257. esp_err_t esp_now_get_peer_num(esp_now_peer_num_t *num);
  258. /**
  259. * @brief Set the primary master key
  260. *
  261. * @param pmk primary master key
  262. *
  263. * @attention 1. primary master key is used to encrypt local master key
  264. *
  265. * @return
  266. * - ESP_OK : succeed
  267. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  268. * - ESP_ERR_ESPNOW_ARG : invalid argument
  269. */
  270. esp_err_t esp_now_set_pmk(const uint8_t *pmk);
  271. /**
  272. * @brief Set esp_now wake window for sta_disconnected power management
  273. *
  274. * @param window how much microsecond would the chip keep waked each interval, vary from 0 to 65535
  275. *
  276. * @attention 1. Only when ESP_WIFI_STA_DISCONNECTED_PM_ENABLE is enabled, this configuration could work
  277. * @attention 2. This configuration only work for station mode and disconnected status
  278. * @attention 3. If more than one module has configured its wake_window, chip would choose the largest one to stay waked
  279. * @attention 4. If the gap between interval and window is smaller than 5ms, the chip would keep waked all the time
  280. * @attention 5. If never configured wake_window, the chip would keep waked at disconnected once it uses esp_now
  281. *
  282. * @return
  283. * - ESP_OK : succeed
  284. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  285. */
  286. esp_err_t esp_now_set_wake_window(uint16_t window);
  287. /**
  288. * @}
  289. */
  290. /**
  291. * @}
  292. */
  293. #ifdef __cplusplus
  294. }
  295. #endif
  296. #endif /* __ESP_NOW_H__ */