esp_mesh_internal.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef __ESP_MESH_INTERNAL_H__
  7. #define __ESP_MESH_INTERNAL_H__
  8. #include "esp_err.h"
  9. #include "esp_mesh.h"
  10. #include "esp_wifi.h"
  11. #include "esp_wifi_types.h"
  12. #include "esp_private/wifi.h"
  13. #include "esp_wifi_crypto_types.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /*******************************************************
  18. * Constants
  19. *******************************************************/
  20. /*******************************************************
  21. * Structures
  22. *******************************************************/
  23. /**
  24. * @brief Mesh attempts
  25. */
  26. typedef struct {
  27. int scan; /**< minimum scan times before being a root, default:10 */
  28. int vote; /**< max vote times in self-healing, default:1000 */
  29. int fail; /**< parent selection fail times, if the scan times reach this value,
  30. device will disconnect with associated children and join self-healing. default:60 */
  31. int monitor_ie; /**< acceptable times of parent networking IE change before update its own networking IE. default:3 */
  32. } mesh_attempts_t;
  33. /**
  34. * @brief Mesh switch parent
  35. */
  36. typedef struct {
  37. int duration_ms; /**< parent weak RSSI monitor duration, if the RSSI continues to be weak during this duration_ms,
  38. device will search for a new parent. */
  39. int cnx_rssi; /**< RSSI threshold for keeping a good connection with parent.
  40. If set a value greater than -120 dBm, a timer will be armed to monitor parent RSSI at a period time of duration_ms. */
  41. int select_rssi; /**< RSSI threshold for parent selection. It should be a value greater than switch_rssi. */
  42. int switch_rssi; /**< Disassociate with current parent and switch to a new parent when the RSSI is greater than this set threshold. */
  43. int backoff_rssi; /**< RSSI threshold for connecting to the root */
  44. } mesh_switch_parent_t;
  45. /**
  46. * @brief Mesh RSSI threshold
  47. */
  48. typedef struct {
  49. int high; /**< high RSSI threshold, used to determine whether the new parent and the current parent are in the same RSSI range */
  50. int medium; /**< medium RSSI threshold, used to determine whether the new parent and the current parent are in the same RSSI range */
  51. int low; /**< low RSSI threshold. If the parent's RSSI is lower than low for a period time of duration_ms,
  52. then the mesh node will post MESH_WEAK_RSSI event.
  53. Also used to determine whether the new parent and the current parent are in the same RSSI range */
  54. } mesh_rssi_threshold_t;
  55. /**
  56. * @brief Mesh networking IE
  57. */
  58. typedef struct {
  59. /**< mesh networking IE head */
  60. uint8_t eid; /**< element ID, vendor specific, 221 */
  61. uint8_t len; /**< element length, the length after this member */
  62. uint8_t oui[3]; /**< organization identifier, 0x18fe34 */
  63. uint8_t type; /**< ESP defined IE type, include Assoc IE, SSID IE, Ext Assoc IE, Roots IE, etc. */
  64. uint8_t encrypted : 1; /**< whether mesh networking IE is encrypted */
  65. uint8_t version : 7; /**< mesh networking IE version, equal to 2 if mesh PS is enabled, equal to 1 otherwise */
  66. /**< content */
  67. uint8_t mesh_type; /**< mesh device type, include idle, root, node, etc, refer to mesh_type_t */
  68. uint8_t mesh_id[6]; /**< mesh ID, only the same mesh id can form a unified mesh network */
  69. uint8_t layer_cap; /**< layer_cap = max_layer - layer, indicates the number of remaining available layers of the mesh network */
  70. uint8_t layer; /**< the current layer of this node */
  71. uint8_t assoc_cap; /**< the maximum connections of this mesh AP */
  72. uint8_t assoc; /**< current connections of this mesh AP */
  73. uint8_t leaf_cap; /**< the maximum number of leaves in the mesh network */
  74. uint8_t leaf_assoc; /**< the number of current connected leaves */
  75. uint16_t root_cap; /**< the capacity of the root, equal to the total child numbers plus 1, root node updates root_cap and self_cap */
  76. uint16_t self_cap; /**< the capacity of myself, total child numbers plus 1, all nodes update this member */
  77. uint16_t layer2_cap; /**< the capacity of layer2 node, total child numbers plus 1, layer2 node updates layer2_cap and self_cap, root sets this to 0 */
  78. uint16_t scan_ap_num; /**< the number of mesh APs around */
  79. int8_t rssi; /**< RSSI of the connected parent, default value is -120, root node will not update this */
  80. int8_t router_rssi; /**< RSSI of the router, default value is -120 */
  81. uint8_t flag; /**< flag of networking, indicates the status of the network, refer to MESH_ASSOC_FLAG_XXX */
  82. /**< vote related */
  83. uint8_t rc_addr[6]; /**< the address of the root candidate, i.e. the voted addesss before connection, root node will update this with self address */
  84. int8_t rc_rssi; /**< the router RSSI of the root candidate */
  85. uint8_t vote_addr[6]; /**< the voted address after connection */
  86. int8_t vote_rssi; /**< the router RSSI of the voted address */
  87. uint8_t vote_ttl; /**< vote ttl, indicate the voting is from myself or from other nodes */
  88. uint16_t votes; /**< the number of all voting nodes */
  89. uint16_t my_votes; /**< the number of nodes that voted for me */
  90. uint8_t reason; /**< the reason why the voting happens, root initiated or child initiated, refer to mesh_vote_reason_t */
  91. uint8_t child[6]; /**< child address, not used currently */
  92. uint8_t toDS; /**< state represents whether the root is able to access external IP network */
  93. } __attribute__((packed)) mesh_assoc_t;
  94. /**
  95. * @brief Mesh chain layer
  96. */
  97. typedef struct {
  98. uint16_t layer_cap; /**< max layer of the network */
  99. uint16_t layer; /**< current layer of this node */
  100. } mesh_chain_layer_t;
  101. /**
  102. * @brief Mesh chain assoc
  103. */
  104. typedef struct {
  105. mesh_assoc_t tree; /**< tree top, mesh_assoc IE */
  106. mesh_chain_layer_t chain; /**< chain top, mesh_assoc IE */
  107. } __attribute__((packed)) mesh_chain_assoc_t;
  108. /* mesh max connections */
  109. #define MESH_MAX_CONNECTIONS (10)
  110. /**
  111. * @brief Mesh power save duties
  112. */
  113. typedef struct {
  114. uint8_t device; /**< device power save duty*/
  115. uint8_t parent; /**< parent power save duty*/
  116. struct {
  117. bool used; /**< whether the child is joined */
  118. uint8_t duty; /**< power save duty of the child */
  119. uint8_t mac[6]; /**< mac address of the child */
  120. } child[MESH_MAX_CONNECTIONS]; /**< child */
  121. } esp_mesh_ps_duties_t;
  122. /*******************************************************
  123. * Function Definitions
  124. *******************************************************/
  125. /**
  126. * @brief Set mesh softAP beacon interval
  127. *
  128. * @param[in] interval_ms beacon interval (msecs) (100 msecs ~ 60000 msecs)
  129. *
  130. * @return
  131. * - ESP_OK
  132. * - ESP_FAIL
  133. * - ESP_ERR_INVALID_ARG
  134. */
  135. esp_err_t esp_mesh_set_beacon_interval(int interval_ms);
  136. /**
  137. * @brief Get mesh softAP beacon interval
  138. *
  139. * @param[out] interval_ms beacon interval (msecs)
  140. *
  141. * @return
  142. * - ESP_OK
  143. */
  144. esp_err_t esp_mesh_get_beacon_interval(int *interval_ms);
  145. /**
  146. * @brief Set attempts for mesh self-organized networking
  147. *
  148. * @param[in] attempts
  149. *
  150. * @return
  151. * - ESP_OK
  152. * - ESP_FAIL
  153. */
  154. esp_err_t esp_mesh_set_attempts(mesh_attempts_t *attempts);
  155. /**
  156. * @brief Get attempts for mesh self-organized networking
  157. *
  158. * @param[out] attempts
  159. *
  160. * @return
  161. * - ESP_OK
  162. * - ESP_ERR_MESH_ARGUMENT
  163. */
  164. esp_err_t esp_mesh_get_attempts(mesh_attempts_t *attempts);
  165. /**
  166. * @brief Set parameters for parent switch
  167. *
  168. * @param[in] paras parameters for parent switch
  169. *
  170. * @return
  171. * - ESP_OK
  172. * - ESP_ERR_MESH_ARGUMENT
  173. */
  174. esp_err_t esp_mesh_set_switch_parent_paras(mesh_switch_parent_t *paras);
  175. /**
  176. * @brief Get parameters for parent switch
  177. *
  178. * @param[out] paras parameters for parent switch
  179. *
  180. * @return
  181. * - ESP_OK
  182. * - ESP_ERR_MESH_ARGUMENT
  183. */
  184. esp_err_t esp_mesh_get_switch_parent_paras(mesh_switch_parent_t *paras);
  185. /**
  186. * @brief Set RSSI threshold of current parent
  187. * - The default high RSSI threshold value is -78 dBm.
  188. * - The default medium RSSI threshold value is -82 dBm.
  189. * - The default low RSSI threshold value is -85 dBm.
  190. *
  191. * @param[in] threshold RSSI threshold
  192. *
  193. * @return
  194. * - ESP_OK
  195. * - ESP_ERR_MESH_ARGUMENT
  196. */
  197. esp_err_t esp_mesh_set_rssi_threshold(const mesh_rssi_threshold_t *threshold);
  198. /**
  199. * @brief Get RSSI threshold of current parent
  200. *
  201. * @param[out] threshold RSSI threshold
  202. *
  203. * @return
  204. * - ESP_OK
  205. * - ESP_ERR_MESH_ARGUMENT
  206. */
  207. esp_err_t esp_mesh_get_rssi_threshold(mesh_rssi_threshold_t *threshold);
  208. /**
  209. * @brief Enable the minimum rate to 6 Mbps
  210. *
  211. * @attention This API shall be called before Wi-Fi is started.
  212. *
  213. * @param[in] is_6m enable or not
  214. *
  215. * @return
  216. * - ESP_OK
  217. */
  218. esp_err_t esp_mesh_set_6m_rate(bool is_6m);
  219. /**
  220. * @brief Print the number of txQ waiting
  221. *
  222. * @return
  223. * - ESP_OK
  224. * - ESP_FAIL
  225. */
  226. esp_err_t esp_mesh_print_txQ_waiting(void);
  227. /**
  228. * @brief Print the number of rxQ waiting
  229. *
  230. * @return
  231. * - ESP_OK
  232. * - ESP_FAIL
  233. */
  234. esp_err_t esp_mesh_print_rxQ_waiting(void);
  235. /**
  236. * @brief Set passive scan time
  237. *
  238. * @param[in] time_ms passive scan time (msecs)
  239. *
  240. * @return
  241. * - ESP_OK
  242. * - ESP_FAIL
  243. * - ESP_ERR_ARGUMENT
  244. */
  245. esp_err_t esp_mesh_set_passive_scan_time(int time_ms);
  246. /**
  247. * @brief Get passive scan time
  248. *
  249. * @return interval_ms passive scan time (msecs)
  250. */
  251. int esp_mesh_get_passive_scan_time(void);
  252. /**
  253. * @brief Set announce interval
  254. * - The default short interval is 500 milliseconds.
  255. * - The default long interval is 3000 milliseconds.
  256. *
  257. * @param[in] short_ms shall be greater than the default value
  258. * @param[in] long_ms shall be greater than the default value
  259. *
  260. * @return
  261. * - ESP_OK
  262. */
  263. esp_err_t esp_mesh_set_announce_interval(int short_ms, int long_ms);
  264. /**
  265. * @brief Get announce interval
  266. *
  267. * @param[out] short_ms short interval
  268. * @param[out] long_ms long interval
  269. *
  270. * @return
  271. * - ESP_OK
  272. */
  273. esp_err_t esp_mesh_get_announce_interval(int *short_ms, int *long_ms);
  274. /**
  275. * @brief Get the running duties of device, parent and children
  276. *
  277. * @param[out] ps_duties ps duties
  278. *
  279. * @return
  280. * - ESP_OK
  281. */
  282. esp_err_t esp_mesh_ps_get_duties(esp_mesh_ps_duties_t* ps_duties);
  283. /**
  284. * @brief Enable mesh print scan result
  285. *
  286. * @param[in] enable enable or not
  287. *
  288. * @return
  289. * - ESP_OK
  290. */
  291. esp_err_t esp_mesh_print_scan_result(bool enable);
  292. #ifdef __cplusplus
  293. }
  294. #endif
  295. #endif /* __ESP_MESH_INTERNAL_H__ */