esp_now.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 + 101) /*!< ESPNOW error number base. */
  35. #define ESP_ERR_ESPNOW_NOT_INIT (ESP_ERR_ESPNOW_BASE) /*!< ESPNOW is not initialized. */
  36. #define ESP_ERR_ESPNOW_ARG (ESP_ERR_ESPNOW_BASE + 1) /*!< Invalid argument */
  37. #define ESP_ERR_ESPNOW_NO_MEM (ESP_ERR_ESPNOW_BASE + 2) /*!< Out of memory */
  38. #define ESP_ERR_ESPNOW_FULL (ESP_ERR_ESPNOW_BASE + 3) /*!< ESPNOW peer list is full */
  39. #define ESP_ERR_ESPNOW_NOT_FOUND (ESP_ERR_ESPNOW_BASE + 4) /*!< ESPNOW peer is not found */
  40. #define ESP_ERR_ESPNOW_INTERNAL (ESP_ERR_ESPNOW_BASE + 5) /*!< Internal error */
  41. #define ESP_ERR_ESPNOW_EXIST (ESP_ERR_ESPNOW_BASE + 6) /*!< ESPNOW peer has existed */
  42. #define ESP_NOW_ETH_ALEN 6 /*!< Length of ESPNOW peer MAC address */
  43. #define ESP_NOW_KEY_LEN 16 /*!< Length of ESPNOW peer local master key */
  44. #define ESP_NOW_MAX_TOTAL_PEER_NUM 20 /*!< Maximum number of ESPNOW total peers */
  45. #define ESP_NOW_MAX_ENCRYPT_PEER_NUM 6 /*!< Maximum number of ESPNOW encrypted peers */
  46. #define ESP_NOW_MAX_DATA_LEN 250 /*!< Maximum length of ESPNOW data which is sent very time */
  47. /**
  48. * @brief Status of sending ESPNOW data .
  49. */
  50. typedef enum {
  51. ESP_NOW_SEND_SUCCESS = 0, /**< Send ESPNOW data successfully */
  52. ESP_NOW_SEND_FAIL, /**< Send ESPNOW data fail */
  53. } esp_now_send_status_t;
  54. /**
  55. * @brief ESPNOW peer information parameters.
  56. */
  57. typedef struct esp_now_peer_info {
  58. uint8_t peer_addr[ESP_NOW_ETH_ALEN]; /**< ESPNOW peer MAC address that is also the MAC address of station or softap */
  59. uint8_t lmk[ESP_NOW_KEY_LEN]; /**< ESPNOW peer local master key that is used to encrypt data */
  60. uint8_t channel; /**< Wi-Fi channel that peer uses to send/receive ESPNOW data. If the value is 0,
  61. use the current channel which station or softap is on. Otherwise, it must be
  62. set as the channel that station or softap is on. */
  63. wifi_interface_t ifidx; /**< Wi-Fi interface that peer uses to send/receive ESPNOW data */
  64. bool encrypt; /**< ESPNOW data that this peer sends/receives is encrypted or not */
  65. void *priv; /**< ESPNOW peer private data */
  66. } esp_now_peer_info_t;
  67. /**
  68. * @brief Number of ESPNOW peers which exist currently.
  69. */
  70. typedef struct esp_now_peer_num {
  71. int total_num; /**< Total number of ESPNOW peers, maximum value is ESP_NOW_MAX_TOTAL_PEER_NUM */
  72. int encrypt_num; /**< Number of encrypted ESPNOW peers, maximum value is ESP_NOW_MAX_ENCRYPT_PEER_NUM */
  73. } esp_now_peer_num_t;
  74. /**
  75. * @brief Callback function of receiving ESPNOW data
  76. * @param mac_addr peer MAC address
  77. * @param data received data
  78. * @param data_len length of received data
  79. */
  80. typedef void (*esp_now_recv_cb_t)(const uint8_t *mac_addr, const uint8_t *data, int data_len);
  81. /**
  82. * @brief Callback function of sending ESPNOW data
  83. * @param mac_addr peer MAC address
  84. * @param status status of sending ESPNOW data (succeed or fail)
  85. */
  86. typedef void (*esp_now_send_cb_t)(const uint8_t *mac_addr, esp_now_send_status_t status);
  87. /**
  88. * @brief Initialize ESPNOW function
  89. *
  90. * @return
  91. * - ESP_OK : succeed
  92. * - ESP_ERR_ESPNOW_INTERNAL : Internal error
  93. */
  94. esp_err_t esp_now_init(void);
  95. /**
  96. * @brief De-initialize ESPNOW function
  97. *
  98. * @return
  99. * - ESP_OK : succeed
  100. */
  101. esp_err_t esp_now_deinit(void);
  102. /**
  103. * @brief Get the version of ESPNOW
  104. *
  105. * @param version ESPNOW version
  106. *
  107. * @return
  108. * - ESP_OK : succeed
  109. * - ESP_ERR_ESPNOW_ARG : invalid argument
  110. */
  111. esp_err_t esp_now_get_version(uint32_t *version);
  112. /**
  113. * @brief Register callback function of receiving ESPNOW data
  114. *
  115. * @param cb callback function of receiving ESPNOW data
  116. *
  117. * @return
  118. * - ESP_OK : succeed
  119. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  120. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  121. */
  122. esp_err_t esp_now_register_recv_cb(esp_now_recv_cb_t cb);
  123. /**
  124. * @brief Unregister callback function of receiving ESPNOW data
  125. *
  126. * @return
  127. * - ESP_OK : succeed
  128. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  129. */
  130. esp_err_t esp_now_unregister_recv_cb(void);
  131. /**
  132. * @brief Register callback function of sending ESPNOW data
  133. *
  134. * @param cb callback function of sending ESPNOW data
  135. *
  136. * @return
  137. * - ESP_OK : succeed
  138. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  139. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  140. */
  141. esp_err_t esp_now_register_send_cb(esp_now_send_cb_t cb);
  142. /**
  143. * @brief Unregister callback function of sending ESPNOW data
  144. *
  145. * @return
  146. * - ESP_OK : succeed
  147. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  148. */
  149. esp_err_t esp_now_unregister_send_cb(void);
  150. /**
  151. * @brief Send ESPNOW data
  152. *
  153. * @attention 1. If peer_addr is not NULL, send data to the peer whose MAC address matches peer_addr
  154. * @attention 2. If peer_addr is NULL, send data to all of the peers that are added to the peer list
  155. * @attention 3. The maximum length of data must be less than ESP_NOW_MAX_DATA_LEN
  156. * @attention 4. The buffer pointed to by data argument does not need to be valid after esp_now_send returns
  157. *
  158. * @param peer_addr peer MAC address
  159. * @param data data to send
  160. * @param len length of data
  161. *
  162. * @return
  163. * - ESP_OK : succeed
  164. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  165. * - ESP_ERR_ESPNOW_ARG : invalid argument
  166. * - ESP_ERR_ESPNOW_INTERNAL : internal error
  167. * - ESP_ERR_ESPNOW_NO_MEM : out of memory
  168. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  169. */
  170. esp_err_t esp_now_send(const uint8_t *peer_addr, const uint8_t *data, size_t len);
  171. /**
  172. * @brief Add a peer to peer list
  173. *
  174. * @param peer peer information
  175. *
  176. * @return
  177. * - ESP_OK : succeed
  178. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  179. * - ESP_ERR_ESPNOW_ARG : invalid argument
  180. * - ESP_ERR_ESPNOW_FULL : peer list is full
  181. * - ESP_ERR_ESPNOW_NO_MEM : out of memory
  182. * - ESP_ERR_ESPNOW_EXIST : peer has existed
  183. */
  184. esp_err_t esp_now_add_peer(const esp_now_peer_info_t *peer);
  185. /**
  186. * @brief Delete a peer from peer list
  187. *
  188. * @param peer_addr peer MAC address
  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_NOT_FOUND : peer is not found
  195. */
  196. esp_err_t esp_now_del_peer(const uint8_t *peer_addr);
  197. /**
  198. * @brief Modify a peer
  199. *
  200. * @param peer peer information
  201. *
  202. * @return
  203. * - ESP_OK : succeed
  204. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  205. * - ESP_ERR_ESPNOW_ARG : invalid argument
  206. * - ESP_ERR_ESPNOW_FULL : peer list is full
  207. */
  208. esp_err_t esp_now_mod_peer(const esp_now_peer_info_t *peer);
  209. /**
  210. * @brief Get a peer whose MAC address matches peer_addr from peer list
  211. *
  212. * @param peer_addr peer MAC address
  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_NOT_FOUND : peer is not found
  220. */
  221. esp_err_t esp_now_get_peer(const uint8_t *peer_addr, esp_now_peer_info_t *peer);
  222. /**
  223. * @brief Fetch a peer from peer list
  224. *
  225. * @param from_head fetch from head of list or not
  226. * @param peer peer information
  227. *
  228. * @return
  229. * - ESP_OK : succeed
  230. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  231. * - ESP_ERR_ESPNOW_ARG : invalid argument
  232. * - ESP_ERR_ESPNOW_NOT_FOUND : peer is not found
  233. */
  234. esp_err_t esp_now_fetch_peer(bool from_head, esp_now_peer_info_t *peer);
  235. /**
  236. * @brief Peer exists or not
  237. *
  238. * @param peer_addr peer MAC address
  239. *
  240. * @return
  241. * - true : peer exists
  242. * - false : peer not exists
  243. */
  244. bool esp_now_is_peer_exist(const uint8_t *peer_addr);
  245. /**
  246. * @brief Get the number of peers
  247. *
  248. * @param num number of peers
  249. *
  250. * @return
  251. * - ESP_OK : succeed
  252. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  253. * - ESP_ERR_ESPNOW_ARG : invalid argument
  254. */
  255. esp_err_t esp_now_get_peer_num(esp_now_peer_num_t *num);
  256. /**
  257. * @brief Set the primary master key
  258. *
  259. * @param pmk primary master key
  260. *
  261. * @attention 1. primary master key is used to encrypt local master key
  262. *
  263. * @return
  264. * - ESP_OK : succeed
  265. * - ESP_ERR_ESPNOW_NOT_INIT : ESPNOW is not initialized
  266. * - ESP_ERR_ESPNOW_ARG : invalid argument
  267. */
  268. esp_err_t esp_now_set_pmk(const uint8_t *pmk);
  269. /**
  270. * @}
  271. */
  272. /**
  273. * @}
  274. */
  275. #ifdef __cplusplus
  276. }
  277. #endif
  278. #endif /* __ESP_NOW_H__ */