esp_mesh.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. // Copyright 2017-2018 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. /*
  14. * Software Stack demonstrated:
  15. * |------------------------------------------------------------------------------|
  16. * | | |
  17. * | | Application |
  18. * | |-----------------------------------------------------------------|
  19. * | | | Protocols: | | | | |
  20. * | | Mesh Stack | HTTP, DNS, | | | Other | |
  21. * | RTOS: | (Networking, | DHCP, ... | | | Components | |
  22. * | (freeRTOS) | self-healing, |------------| | | | |
  23. * | | flow control, | Network Stack: | | | |
  24. * | | ...) | (LwIP) | | | |
  25. * | |-----------------------------------| |---------------| |
  26. * | | | |
  27. * | | Wi-Fi Driver | |
  28. * | |--------------------------------------------------| |
  29. * | | |
  30. * | | Platform HAL |
  31. * |------------------------------------------------------------------------------|
  32. *
  33. * System Events delivery:
  34. *
  35. * |---------------|
  36. * | | default handler
  37. * | Wi-Fi stack | events |---------------------|
  38. * | | -------------> | |
  39. * |---------------| | |
  40. * | event task |
  41. * |---------------| events | |
  42. * | | -------------> | |
  43. * | LwIP stack | |---------------------|
  44. * | |--------|
  45. * |---------------| |
  46. * | mesh event callback handler
  47. * | |----------------------------|
  48. * |-----> | |
  49. * |---------------| | application |
  50. * | | events | task |
  51. * | mesh stack | -------------> | |
  52. * | | |----------------------------|
  53. * |---------------|
  54. *
  55. *
  56. * Mesh Stack
  57. *
  58. * Mesh event defines almost all system events applications tasks need.
  59. * Mesh event contains Wi-Fi connection states on station interface, children connection states on softAP interface and etc..
  60. * Applications need to register a mesh event callback handler by API esp_mesh_set_config() firstly.
  61. * This handler is to receive events posted from mesh stack and LwIP stack.
  62. * Applications could add relative handler for each event.
  63. * Examples:
  64. * (1) Applications could use Wi-Fi station connect states to decide when to send data to its parent, to the root or to external IP network;
  65. * (2) Applications could use Wi-Fi softAP states to decide when to send data to its children.
  66. *
  67. * In present implementation, applications are able to access mesh stack directly without having to go through LwIP stack.
  68. * Applications use esp_mesh_send() and esp_mesh_recv() to send and receive messages over the mesh network.
  69. * In mesh stack design, normal devices don't require LwIP stack. But since IDF hasn't supported system without initializing LwIP stack yet,
  70. * applications still need to do LwIP initialization and two more things are required to be done
  71. * (1) stop DHCP server on softAP interface by default
  72. * (2) stop DHCP client on station interface by default.
  73. * Examples:
  74. * tcpip_adapter_init();
  75. * tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
  76. * tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
  77. *
  78. * Over the mesh network, only the root is able to access external IP network.
  79. * In application mesh event handler, once a device becomes a root, start DHCP client immediately whether DHCP is chosen.
  80. */
  81. #ifndef __ESP_MESH_H__
  82. #define __ESP_MESH_H__
  83. #include "esp_err.h"
  84. #include "esp_wifi.h"
  85. #include "esp_wifi_types.h"
  86. #include "esp_mesh_internal.h"
  87. #ifdef __cplusplus
  88. extern "C" {
  89. #endif
  90. /*******************************************************
  91. * Constants
  92. *******************************************************/
  93. #define MESH_ROOT_LAYER (1) /**< root layer value */
  94. #define MESH_MTU (1500) /**< max transmit unit(in bytes) */
  95. #define MESH_MPS (1472) /**< max payload size(in bytes) */
  96. /**
  97. * @brief Mesh error code definition
  98. */
  99. #define ESP_ERR_MESH_WIFI_NOT_START (ESP_ERR_MESH_BASE + 1) /**< Wi-Fi isn't started */
  100. #define ESP_ERR_MESH_NOT_INIT (ESP_ERR_MESH_BASE + 2) /**< mesh isn't initialized */
  101. #define ESP_ERR_MESH_NOT_CONFIG (ESP_ERR_MESH_BASE + 3) /**< mesh isn't configured */
  102. #define ESP_ERR_MESH_NOT_START (ESP_ERR_MESH_BASE + 4) /**< mesh isn't started */
  103. #define ESP_ERR_MESH_NOT_SUPPORT (ESP_ERR_MESH_BASE + 5) /**< not supported yet */
  104. #define ESP_ERR_MESH_NOT_ALLOWED (ESP_ERR_MESH_BASE + 6) /**< operation is not allowed */
  105. #define ESP_ERR_MESH_NO_MEMORY (ESP_ERR_MESH_BASE + 7) /**< out of memory */
  106. #define ESP_ERR_MESH_ARGUMENT (ESP_ERR_MESH_BASE + 8) /**< illegal argument */
  107. #define ESP_ERR_MESH_EXCEED_MTU (ESP_ERR_MESH_BASE + 9) /**< packet size exceeds MTU */
  108. #define ESP_ERR_MESH_TIMEOUT (ESP_ERR_MESH_BASE + 10) /**< timeout */
  109. #define ESP_ERR_MESH_DISCONNECTED (ESP_ERR_MESH_BASE + 11) /**< disconnected with parent on station interface */
  110. #define ESP_ERR_MESH_QUEUE_FAIL (ESP_ERR_MESH_BASE + 12) /**< queue fail */
  111. #define ESP_ERR_MESH_QUEUE_FULL (ESP_ERR_MESH_BASE + 13) /**< queue full */
  112. #define ESP_ERR_MESH_NO_PARENT_FOUND (ESP_ERR_MESH_BASE + 14) /**< no parent found to join the mesh network */
  113. #define ESP_ERR_MESH_NO_ROUTE_FOUND (ESP_ERR_MESH_BASE + 15) /**< no route found to forward the packet */
  114. #define ESP_ERR_MESH_OPTION_NULL (ESP_ERR_MESH_BASE + 16) /**< no option found */
  115. #define ESP_ERR_MESH_OPTION_UNKNOWN (ESP_ERR_MESH_BASE + 17) /**< unknown option */
  116. #define ESP_ERR_MESH_XON_NO_WINDOW (ESP_ERR_MESH_BASE + 18) /**< no window for software flow control on upstream */
  117. #define ESP_ERR_MESH_INTERFACE (ESP_ERR_MESH_BASE + 19) /**< low-level Wi-Fi interface error */
  118. #define ESP_ERR_MESH_DISCARD_DUPLICATE (ESP_ERR_MESH_BASE + 20) /**< discard the packet due to the duplicate sequence number */
  119. #define ESP_ERR_MESH_DISCARD (ESP_ERR_MESH_BASE + 21) /**< discard the packet */
  120. #define ESP_ERR_MESH_VOTING (ESP_ERR_MESH_BASE + 22) /**< vote in progress */
  121. /**
  122. * @brief Flags bitmap for esp_mesh_send() and esp_mesh_recv()
  123. */
  124. #define MESH_DATA_ENC (0x01) /**< data encrypted (Unimplemented) */
  125. #define MESH_DATA_P2P (0x02) /**< point-to-point delivery over the mesh network */
  126. #define MESH_DATA_FROMDS (0x04) /**< receive from external IP network */
  127. #define MESH_DATA_TODS (0x08) /**< identify this packet is target to external IP network */
  128. #define MESH_DATA_NONBLOCK (0x10) /**< esp_mesh_send() non-block */
  129. #define MESH_DATA_DROP (0x20) /**< in the situation of the root having been changed, identify this packet can be dropped by new root */
  130. #define MESH_DATA_GROUP (0x40) /**< identify this packet is target to a group address */
  131. /**
  132. * @brief Option definitions for esp_mesh_send() and esp_mesh_recv()
  133. */
  134. #define MESH_OPT_SEND_GROUP (7) /**< data transmission by group; used with esp_mesh_send() and shall have payload */
  135. #define MESH_OPT_RECV_DS_ADDR (8) /**< return a remote IP address; used with esp_mesh_send() and esp_mesh_recv() */
  136. /**
  137. * @brief Flag of mesh networking IE
  138. */
  139. #define MESH_ASSOC_FLAG_VOTE_IN_PROGRESS (0x02) /**< vote in progress */
  140. #define MESH_ASSOC_FLAG_NETWORK_FREE (0x08) /**< no root in current network */
  141. #define MESH_ASSOC_FLAG_ROOTS_FOUND (0x20) /**< root conflict is found */
  142. #define MESH_ASSOC_FLAG_ROOT_FIXED (0x40) /**< fixed root */
  143. /*******************************************************
  144. * Enumerations
  145. *******************************************************/
  146. /**
  147. * @brief Enumerated list of mesh event id
  148. */
  149. typedef enum {
  150. MESH_EVENT_STARTED, /**< mesh is started */
  151. MESH_EVENT_STOPPED, /**< mesh is stopped */
  152. MESH_EVENT_CHANNEL_SWITCH, /**< channel switch */
  153. MESH_EVENT_CHILD_CONNECTED, /**< a child is connected on softAP interface */
  154. MESH_EVENT_CHILD_DISCONNECTED, /**< a child is disconnected on softAP interface */
  155. MESH_EVENT_ROUTING_TABLE_ADD, /**< routing table is changed by adding newly joined children */
  156. MESH_EVENT_ROUTING_TABLE_REMOVE, /**< routing table is changed by removing leave children */
  157. MESH_EVENT_PARENT_CONNECTED, /**< parent is connected on station interface */
  158. MESH_EVENT_PARENT_DISCONNECTED, /**< parent is disconnected on station interface */
  159. MESH_EVENT_NO_PARENT_FOUND, /**< no parent found */
  160. MESH_EVENT_LAYER_CHANGE, /**< layer changes over the mesh network */
  161. MESH_EVENT_TODS_STATE, /**< state represents whether the root is able to access external IP network */
  162. MESH_EVENT_VOTE_STARTED, /**< the process of voting a new root is started either by children or by the root */
  163. MESH_EVENT_VOTE_STOPPED, /**< the process of voting a new root is stopped */
  164. MESH_EVENT_ROOT_ADDRESS, /**< the root address is obtained. It is posted by mesh stack automatically. */
  165. MESH_EVENT_ROOT_SWITCH_REQ, /**< root switch request sent from a new voted root candidate */
  166. MESH_EVENT_ROOT_SWITCH_ACK, /**< root switch acknowledgment responds the above request sent from current root */
  167. MESH_EVENT_ROOT_ASKED_YIELD, /**< the root is asked yield by a more powerful existing root. If self organized is disabled
  168. and this device is specified to be a root by users, users should set a new parent
  169. for this device. if self organized is enabled, this device will find a new parent
  170. by itself, users could ignore this event. */
  171. MESH_EVENT_ROOT_FIXED, /**< when devices join a network, if the setting of Fixed Root for one device is different
  172. from that of its parent, the device will update the setting the same as its parent's.
  173. Fixed Root Setting of each device is variable as that setting changes of the root. */
  174. MESH_EVENT_SCAN_DONE, /**< if self-organized networking is disabled, user can call esp_wifi_scan_start() to trigger
  175. this event, and add the corresponding scan done handler in this event. */
  176. MESH_EVENT_NETWORK_STATE, /**< network state, such as whether current mesh network has a root. */
  177. MESH_EVENT_STOP_RECONNECTION, /**< the root stops reconnecting to the router and non-root devices stop reconnecting to their parents. */
  178. MESH_EVENT_FIND_NETWORK, /**< when the channel field in mesh configuration is set to zero, mesh stack will perform a
  179. full channel scan to find a mesh network that can join, and return the channel value
  180. after finding it. */
  181. MESH_EVENT_ROUTER_SWITCH, /**< if users specify BSSID of the router in mesh configuration, when the root connects to another
  182. router with the same SSID, this event will be posted and the new router information is attached. */
  183. MESH_EVENT_MAX,
  184. } mesh_event_id_t;
  185. /** @brief ESP-MESH event base declaration */
  186. ESP_EVENT_DECLARE_BASE(MESH_EVENT);
  187. /**
  188. * @brief Device type
  189. */
  190. typedef enum {
  191. MESH_IDLE, /**< hasn't joined the mesh network yet */
  192. MESH_ROOT, /**< the only sink of the mesh network. Has the ability to access external IP network */
  193. MESH_NODE, /**< intermediate device. Has the ability to forward packets over the mesh network */
  194. MESH_LEAF, /**< has no forwarding ability */
  195. } mesh_type_t;
  196. /**
  197. * @brief Protocol of transmitted application data
  198. */
  199. typedef enum {
  200. MESH_PROTO_BIN, /**< binary */
  201. MESH_PROTO_HTTP, /**< HTTP protocol */
  202. MESH_PROTO_JSON, /**< JSON format */
  203. MESH_PROTO_MQTT, /**< MQTT protocol */
  204. } mesh_proto_t;
  205. /**
  206. * @brief For reliable transmission, mesh stack provides three type of services
  207. */
  208. typedef enum {
  209. MESH_TOS_P2P, /**< provide P2P (point-to-point) retransmission on mesh stack by default */
  210. MESH_TOS_E2E, /**< provide E2E (end-to-end) retransmission on mesh stack (Unimplemented) */
  211. MESH_TOS_DEF, /**< no retransmission on mesh stack */
  212. } mesh_tos_t;
  213. /**
  214. * @brief Vote reason
  215. */
  216. typedef enum {
  217. MESH_VOTE_REASON_ROOT_INITIATED = 1, /**< vote is initiated by the root */
  218. MESH_VOTE_REASON_CHILD_INITIATED, /**< vote is initiated by children */
  219. } mesh_vote_reason_t;
  220. /**
  221. * @brief Mesh disconnect reason code
  222. */
  223. typedef enum {
  224. MESH_REASON_CYCLIC = 100, /**< cyclic is detected */
  225. MESH_REASON_PARENT_IDLE, /**< parent is idle */
  226. MESH_REASON_LEAF, /**< the connected device is changed to a leaf */
  227. MESH_REASON_DIFF_ID, /**< in different mesh ID */
  228. MESH_REASON_ROOTS, /**< root conflict is detected */
  229. MESH_REASON_PARENT_STOPPED, /**< parent has stopped the mesh */
  230. MESH_REASON_SCAN_FAIL, /**< scan fail */
  231. MESH_REASON_IE_UNKNOWN, /**< unknown IE */
  232. MESH_REASON_WAIVE_ROOT, /**< waive root */
  233. MESH_REASON_PARENT_WORSE, /**< parent with very poor RSSI */
  234. MESH_REASON_EMPTY_PASSWORD, /**< use an empty password to connect to an encrypted parent */
  235. MESH_REASON_PARENT_UNENCRYPTED, /**< connect to an unencrypted parent/router */
  236. } mesh_disconnect_reason_t;
  237. /*******************************************************
  238. * Structures
  239. *******************************************************/
  240. /**
  241. * @brief IP address and port
  242. */
  243. typedef struct {
  244. ip4_addr_t ip4; /**< IP address */
  245. uint16_t port; /**< port */
  246. } __attribute__((packed)) mip_t;
  247. /**
  248. * @brief Mesh address
  249. */
  250. typedef union {
  251. uint8_t addr[6]; /**< mac address */
  252. mip_t mip; /**< mip address */
  253. } mesh_addr_t;
  254. /**
  255. * @brief Channel switch information
  256. */
  257. typedef struct {
  258. uint8_t channel; /**< new channel */
  259. } mesh_event_channel_switch_t;
  260. /**
  261. * @brief Parent connected information
  262. */
  263. typedef struct {
  264. system_event_sta_connected_t connected; /**< parent information, same as Wi-Fi event SYSTEM_EVENT_STA_CONNECTED does */
  265. uint8_t self_layer; /**< layer */
  266. } mesh_event_connected_t;
  267. /**
  268. * @brief No parent found information
  269. */
  270. typedef struct {
  271. int scan_times; /**< scan times being through */
  272. } mesh_event_no_parent_found_t;
  273. /**
  274. * @brief Layer change information
  275. */
  276. typedef struct {
  277. uint8_t new_layer; /**< new layer */
  278. } mesh_event_layer_change_t;
  279. /**
  280. * @brief The reachability of the root to a DS (distribute system)
  281. */
  282. typedef enum {
  283. MESH_TODS_UNREACHABLE, /**< the root isn't able to access external IP network */
  284. MESH_TODS_REACHABLE, /**< the root is able to access external IP network */
  285. } mesh_event_toDS_state_t;
  286. /**
  287. * @brief vote started information
  288. */
  289. typedef struct {
  290. int reason; /**< vote reason, vote could be initiated by children or by the root itself */
  291. int attempts; /**< max vote attempts before stopped */
  292. mesh_addr_t rc_addr; /**< root address specified by users via API esp_mesh_waive_root() */
  293. } mesh_event_vote_started_t;
  294. /**
  295. * @brief find a mesh network that this device can join
  296. */
  297. typedef struct {
  298. uint8_t channel; /**< channel number of the new found network */
  299. uint8_t router_bssid[6]; /**< router BSSID */
  300. } mesh_event_find_network_t;
  301. /**
  302. * @brief IP settings from LwIP stack
  303. */
  304. typedef system_event_sta_got_ip_t mesh_event_root_got_ip_t;
  305. /**
  306. * @brief Root address
  307. */
  308. typedef mesh_addr_t mesh_event_root_address_t;
  309. /**
  310. * @brief Parent disconnected information
  311. */
  312. typedef system_event_sta_disconnected_t mesh_event_disconnected_t;
  313. /**
  314. * @brief Child connected information
  315. */
  316. typedef system_event_ap_staconnected_t mesh_event_child_connected_t;
  317. /**
  318. * @brief Child disconnected information
  319. */
  320. typedef system_event_ap_stadisconnected_t mesh_event_child_disconnected_t;
  321. /**
  322. * @brief Root switch request information
  323. */
  324. typedef struct {
  325. int reason; /**< root switch reason, generally root switch is initialized by users via API esp_mesh_waive_root() */
  326. mesh_addr_t rc_addr; /**< the address of root switch requester */
  327. } mesh_event_root_switch_req_t;
  328. /**
  329. * @brief Other powerful root address
  330. */
  331. typedef struct {
  332. int8_t rssi; /**< rssi with router */
  333. uint16_t capacity; /**< the number of devices in current network */
  334. uint8_t addr[6]; /**< other powerful root address */
  335. } mesh_event_root_conflict_t;
  336. /**
  337. * @brief Routing table change
  338. */
  339. typedef struct {
  340. uint16_t rt_size_new; /**< the new value */
  341. uint16_t rt_size_change; /**< the changed value */
  342. } mesh_event_routing_table_change_t;
  343. /**
  344. * @brief Root fixed
  345. */
  346. typedef struct {
  347. bool is_fixed; /**< status */
  348. } mesh_event_root_fixed_t;
  349. /**
  350. * @brief Scan done event information
  351. */
  352. typedef struct {
  353. uint8_t number; /**< the number of APs scanned */
  354. } mesh_event_scan_done_t;
  355. /**
  356. * @brief Network state information
  357. */
  358. typedef struct {
  359. bool is_rootless; /**< whether current mesh network has a root */
  360. } mesh_event_network_state_t;
  361. /**
  362. * @brief New router information
  363. */
  364. typedef system_event_sta_connected_t mesh_event_router_switch_t;
  365. /**
  366. * @brief Mesh event information
  367. */
  368. typedef union {
  369. mesh_event_channel_switch_t channel_switch; /**< channel switch */
  370. mesh_event_child_connected_t child_connected; /**< child connected */
  371. mesh_event_child_disconnected_t child_disconnected; /**< child disconnected */
  372. mesh_event_routing_table_change_t routing_table; /**< routing table change */
  373. mesh_event_connected_t connected; /**< parent connected */
  374. mesh_event_disconnected_t disconnected; /**< parent disconnected */
  375. mesh_event_no_parent_found_t no_parent; /**< no parent found */
  376. mesh_event_layer_change_t layer_change; /**< layer change */
  377. mesh_event_toDS_state_t toDS_state; /**< toDS state, devices shall check this state firstly before trying to send packets to
  378. external IP network. This state indicates right now whether the root is capable of sending
  379. packets out. If not, devices had better to wait until this state changes to be
  380. MESH_TODS_REACHABLE. */
  381. mesh_event_vote_started_t vote_started; /**< vote started */
  382. mesh_event_root_got_ip_t got_ip; /**< root obtains IP address */
  383. mesh_event_root_address_t root_addr; /**< root address */
  384. mesh_event_root_switch_req_t switch_req; /**< root switch request */
  385. mesh_event_root_conflict_t root_conflict; /**< other powerful root */
  386. mesh_event_root_fixed_t root_fixed; /**< fixed root */
  387. mesh_event_scan_done_t scan_done; /**< scan done */
  388. mesh_event_network_state_t network_state; /**< network state, such as whether current mesh network has a root. */
  389. mesh_event_find_network_t find_network; /**< network found that can join */
  390. mesh_event_router_switch_t router_switch; /**< new router information */
  391. } mesh_event_info_t;
  392. /**
  393. * @brief Mesh option
  394. */
  395. typedef struct {
  396. uint8_t type; /**< option type */
  397. uint16_t len; /**< option length */
  398. uint8_t *val; /**< option value */
  399. } __attribute__((packed)) mesh_opt_t;
  400. /**
  401. * @brief Mesh data for esp_mesh_send() and esp_mesh_recv()
  402. */
  403. typedef struct {
  404. uint8_t *data; /**< data */
  405. uint16_t size; /**< data size */
  406. mesh_proto_t proto; /**< data protocol */
  407. mesh_tos_t tos; /**< data type of service */
  408. } mesh_data_t;
  409. /**
  410. * @brief Router configuration
  411. */
  412. typedef struct {
  413. uint8_t ssid[32]; /**< SSID */
  414. uint8_t ssid_len; /**< length of SSID */
  415. uint8_t bssid[6]; /**< BSSID, if this value is specified, users should also specify "allow_router_switch". */
  416. uint8_t password[64]; /**< password */
  417. bool allow_router_switch; /**< if the BSSID is specified and this value is also set, when the router of this specified BSSID
  418. fails to be found after "fail" (mesh_attempts_t) times, the whole network is allowed to switch
  419. to another router with the same SSID. The new router might also be on a different channel.
  420. The default value is false.
  421. There is a risk that if the password is different between the new switched router and the previous
  422. one, the mesh network could be established but the root will never connect to the new switched router. */
  423. } mesh_router_t;
  424. /**
  425. * @brief Mesh softAP configuration
  426. */
  427. typedef struct {
  428. uint8_t password[64]; /**< mesh softAP password */
  429. uint8_t max_connection; /**< max number of stations allowed to connect in, max 10 */
  430. } mesh_ap_cfg_t;
  431. /**
  432. * @brief Mesh initialization configuration
  433. */
  434. typedef struct {
  435. uint8_t channel; /**< channel, the mesh network on */
  436. bool allow_channel_switch; /**< if this value is set, when "fail" (mesh_attempts_t) times is reached, device will change to
  437. a full channel scan for a network that could join. The default value is false. */
  438. mesh_addr_t mesh_id; /**< mesh network identification */
  439. mesh_router_t router; /**< router configuration */
  440. mesh_ap_cfg_t mesh_ap; /**< mesh softAP configuration */
  441. const mesh_crypto_funcs_t *crypto_funcs; /**< crypto functions */
  442. } mesh_cfg_t;
  443. /**
  444. * @brief Vote address configuration
  445. */
  446. typedef union {
  447. int attempts; /**< max vote attempts before a new root is elected automatically by mesh network. (min:15, 15 by default) */
  448. mesh_addr_t rc_addr; /**< a new root address specified by users for API esp_mesh_waive_root() */
  449. } mesh_rc_config_t;
  450. /**
  451. * @brief Vote
  452. */
  453. typedef struct {
  454. float percentage; /**< vote percentage threshold for approval of being a root */
  455. bool is_rc_specified; /**< if true, rc_addr shall be specified (Unimplemented).
  456. if false, attempts value shall be specified to make network start root election. */
  457. mesh_rc_config_t config; /**< vote address configuration */
  458. } mesh_vote_t;
  459. /**
  460. * @brief The number of packets pending in the queue waiting to be sent by the mesh stack
  461. */
  462. typedef struct {
  463. int to_parent; /**< to parent queue */
  464. int to_parent_p2p; /**< to parent (P2P) queue */
  465. int to_child; /**< to child queue */
  466. int to_child_p2p; /**< to child (P2P) queue */
  467. int mgmt; /**< management queue */
  468. int broadcast; /**< broadcast and multicast queue */
  469. } mesh_tx_pending_t;
  470. /**
  471. * @brief The number of packets available in the queue waiting to be received by applications
  472. */
  473. typedef struct {
  474. int toDS; /**< to external DS */
  475. int toSelf; /**< to self */
  476. } mesh_rx_pending_t;
  477. /*******************************************************
  478. * Variable Declaration
  479. *******************************************************/
  480. /* mesh IE crypto callback function */
  481. extern const mesh_crypto_funcs_t g_wifi_default_mesh_crypto_funcs;
  482. #define MESH_INIT_CONFIG_DEFAULT() { \
  483. .crypto_funcs = &g_wifi_default_mesh_crypto_funcs, \
  484. }
  485. /*******************************************************
  486. * Function Definitions
  487. *******************************************************/
  488. /**
  489. * @brief Mesh initialization
  490. * - Check whether Wi-Fi is started.
  491. * - Initialize mesh global variables with default values.
  492. *
  493. * @attention This API shall be called after Wi-Fi is started.
  494. *
  495. * @return
  496. * - ESP_OK
  497. * - ESP_FAIL
  498. */
  499. esp_err_t esp_mesh_init(void);
  500. /**
  501. * @brief Mesh de-initialization
  502. *
  503. * - Release resources and stop the mesh
  504. *
  505. * @return
  506. * - ESP_OK
  507. * - ESP_FAIL
  508. */
  509. esp_err_t esp_mesh_deinit(void);
  510. /**
  511. * @brief Start mesh
  512. * - Initialize mesh IE.
  513. * - Start mesh network management service.
  514. * - Create TX and RX queues according to the configuration.
  515. * - Register mesh packets receive callback.
  516. *
  517. * @attention  This API shall be called after mesh initialization and configuration.
  518. *
  519. * @return
  520. * - ESP_OK
  521. * - ESP_FAIL
  522. * - ESP_ERR_MESH_NOT_INIT
  523. * - ESP_ERR_MESH_NOT_CONFIG
  524. * - ESP_ERR_MESH_NO_MEMORY
  525. */
  526. esp_err_t esp_mesh_start(void);
  527. /**
  528. * @brief Stop mesh
  529. * - Deinitialize mesh IE.
  530. * - Disconnect with current parent.
  531. * - Disassociate all currently associated children.
  532. * - Stop mesh network management service.
  533. * - Unregister mesh packets receive callback.
  534. * - Delete TX and RX queues.
  535. * - Release resources.
  536. * - Restore Wi-Fi softAP to default settings if Wi-Fi dual mode is enabled.
  537. *
  538. * @return
  539. * - ESP_OK
  540. * - ESP_FAIL
  541. */
  542. esp_err_t esp_mesh_stop(void);
  543. /**
  544. * @brief Send a packet over the mesh network
  545. * - Send a packet to any device in the mesh network.
  546. * - Send a packet to external IP network.
  547. *
  548. * @attention This API is not reentrant.
  549. *
  550. * @param[in] to the address of the final destination of the packet
  551. * - If the packet is to the root, set this parameter to NULL.
  552. * - If the packet is to an external IP network, set this parameter to the IPv4:PORT combination.
  553. * This packet will be delivered to the root firstly, then the root will forward this packet to the final IP server address.
  554. * @param[in] data pointer to a sending mesh packet
  555. * - Field size should not exceed MESH_MPS. Note that the size of one mesh packet should not exceed MESH_MTU.
  556. * - Field proto should be set to data protocol in use (default is MESH_PROTO_BIN for binary).
  557. * - Field tos should be set to transmission tos (type of service) in use (default is MESH_TOS_P2P for point-to-point reliable).
  558. * @param[in] flag bitmap for data sent
  559. * - Speed up the route search
  560. * - If the packet is to the root and "to" parameter is NULL, set this parameter to 0.
  561. * - If the packet is to an internal device, MESH_DATA_P2P should be set.
  562. * - If the packet is to the root ("to" parameter isn't NULL) or to external IP network, MESH_DATA_TODS should be set.
  563. * - If the packet is from the root to an internal device, MESH_DATA_FROMDS should be set.
  564. * - Specify whether this API is block or non-block, block by default
  565. * - If needs non-block, MESH_DATA_NONBLOCK should be set.
  566. * - In the situation of the root change, MESH_DATA_DROP identifies this packet can be dropped by the new root
  567. * for upstream data to external IP network, we try our best to avoid data loss caused by the root change, but
  568. * there is a risk that the new root is running out of memory because most of memory is occupied by the pending data which
  569. * isn't read out in time by esp_mesh_recv_toDS().
  570. *
  571. * Generally, we suggest esp_mesh_recv_toDS() is called after a connection with IP network is created. Thus data outgoing
  572. * to external IP network via socket is just from reading esp_mesh_recv_toDS() which avoids unnecessary memory copy.
  573. *
  574. * @param[in] opt options
  575. * - In case of sending a packet to a certain group, MESH_OPT_SEND_GROUP is a good choice.
  576. * In this option, the value field should be set to the target receiver addresses in this group.
  577. * - Root sends a packet to an internal device, this packet is from external IP network in case the receiver device responds
  578. * this packet, MESH_OPT_RECV_DS_ADDR is required to attach the target DS address.
  579. * @param[in] opt_count option count
  580. * - Currently, this API only takes one option, so opt_count is only supported to be 1.
  581. *
  582. * @return
  583. * - ESP_OK
  584. * - ESP_FAIL
  585. * - ESP_ERR_MESH_ARGUMENT
  586. * - ESP_ERR_MESH_NOT_START
  587. * - ESP_ERR_MESH_DISCONNECTED
  588. * - ESP_ERR_MESH_OPT_UNKNOWN
  589. * - ESP_ERR_MESH_EXCEED_MTU
  590. * - ESP_ERR_MESH_NO_MEMORY
  591. * - ESP_ERR_MESH_TIMEOUT
  592. * - ESP_ERR_MESH_QUEUE_FULL
  593. * - ESP_ERR_MESH_NO_ROUTE_FOUND
  594. * - ESP_ERR_MESH_DISCARD
  595. */
  596. esp_err_t esp_mesh_send(const mesh_addr_t *to, const mesh_data_t *data,
  597. int flag, const mesh_opt_t opt[], int opt_count);
  598. /**
  599. * @brief Receive a packet targeted to self over the mesh network
  600. *
  601. * @attention Mesh RX queue should be checked regularly to avoid running out of memory.
  602. * - Use esp_mesh_get_rx_pending() to check the number of packets available in the queue waiting
  603. * to be received by applications.
  604. *
  605. * @param[out] from the address of the original source of the packet
  606. * @param[out] data pointer to the received mesh packet
  607. * - Field proto is the data protocol in use. Should follow it to parse the received data.
  608. * - Field tos is the transmission tos (type of service) in use.
  609. * @param[in] timeout_ms wait time if a packet isn't immediately available (0:no wait, portMAX_DELAY:wait forever)
  610. * @param[out] flag bitmap for data received
  611. * - MESH_DATA_FROMDS represents data from external IP network
  612. * - MESH_DATA_TODS represents data directed upward within the mesh network
  613. *
  614. * flag could be MESH_DATA_FROMDS or MESH_DATA_TODS.
  615. * @param[out] opt options desired to receive
  616. * - MESH_OPT_RECV_DS_ADDR attaches the DS address
  617. * @param[in] opt_count option count desired to receive
  618. * - Currently, this API only takes one option, so opt_count is only supported to be 1.
  619. *
  620. * @return
  621. * - ESP_OK
  622. * - ESP_ERR_MESH_ARGUMENT
  623. * - ESP_ERR_MESH_NOT_START
  624. * - ESP_ERR_MESH_TIMEOUT
  625. * - ESP_ERR_MESH_DISCARD
  626. */
  627. esp_err_t esp_mesh_recv(mesh_addr_t *from, mesh_data_t *data, int timeout_ms,
  628. int *flag, mesh_opt_t opt[], int opt_count);
  629. /**
  630. * @brief Receive a packet targeted to external IP network
  631. * - Root uses this API to receive packets destined to external IP network
  632. * - Root forwards the received packets to the final destination via socket.
  633. * - If no socket connection is ready to send out the received packets and this esp_mesh_recv_toDS()
  634. * hasn't been called by applications, packets from the whole mesh network will be pending in toDS queue.
  635. *
  636. * Use esp_mesh_get_rx_pending() to check the number of packets available in the queue waiting
  637. * to be received by applications in case of running out of memory in the root.
  638. *
  639. * Using esp_mesh_set_xon_qsize() users may configure the RX queue size, default:32. If this size is too large,
  640. * and esp_mesh_recv_toDS() isn't called in time, there is a risk that a great deal of memory is occupied
  641. * by the pending packets. If this size is too small, it will impact the efficiency on upstream. How to
  642. * decide this value depends on the specific application scenarios.
  643. *
  644. * @attention This API is only called by the root.
  645. *
  646. * @param[out] from the address of the original source of the packet
  647. * @param[out] to the address contains remote IP address and port (IPv4:PORT)
  648. * @param[out] data pointer to the received packet
  649. * - Contain the protocol and applications should follow it to parse the data.
  650. * @param[in] timeout_ms wait time if a packet isn't immediately available (0:no wait, portMAX_DELAY:wait forever)
  651. * @param[out] flag bitmap for data received
  652. * - MESH_DATA_TODS represents the received data target to external IP network. Root shall forward this data to external IP network via the association with router.
  653. *
  654. * flag could be MESH_DATA_TODS.
  655. * @param[out] opt options desired to receive
  656. * @param[in] opt_count option count desired to receive
  657. *
  658. * @return
  659. * - ESP_OK
  660. * - ESP_ERR_MESH_ARGUMENT
  661. * - ESP_ERR_MESH_NOT_START
  662. * - ESP_ERR_MESH_TIMEOUT
  663. * - ESP_ERR_MESH_DISCARD
  664. */
  665. esp_err_t esp_mesh_recv_toDS(mesh_addr_t *from, mesh_addr_t *to,
  666. mesh_data_t *data, int timeout_ms, int *flag, mesh_opt_t opt[],
  667. int opt_count);
  668. /**
  669. * @brief Set mesh stack configuration
  670. * - Use MESH_INIT_CONFIG_DEFAULT() to initialize the default values, mesh IE is encrypted by default.
  671. * - Mesh network is established on a fixed channel (1-14).
  672. * - Mesh event callback is mandatory.
  673. * - Mesh ID is an identifier of an MBSS. Nodes with the same mesh ID can communicate with each other.
  674. * - Regarding to the router configuration, if the router is hidden, BSSID field is mandatory.
  675. *
  676. * If BSSID field isn't set and there exists more than one router with same SSID, there is a risk that more
  677. * roots than one connected with different BSSID will appear. It means more than one mesh network is established
  678. * with the same mesh ID.
  679. *
  680. * Root conflict function could eliminate redundant roots connected with the same BSSID, but couldn't handle roots
  681. * connected with different BSSID. Because users might have such requirements of setting up routers with same SSID
  682. * for the future replacement. But in that case, if the above situations happen, please make sure applications
  683. * implement forward functions on the root to guarantee devices in different mesh networks can communicate with each other.
  684. * max_connection of mesh softAP is limited by the max number of Wi-Fi softAP supported (max:10).
  685. *
  686. * @attention This API shall be called before mesh is started after mesh is initialized.
  687. *
  688. * @param[in] config pointer to mesh stack configuration
  689. *
  690. * @return
  691. * - ESP_OK
  692. * - ESP_ERR_MESH_ARGUMENT
  693. * - ESP_ERR_MESH_NOT_ALLOWED
  694. */
  695. esp_err_t esp_mesh_set_config(const mesh_cfg_t *config);
  696. /**
  697. * @brief Get mesh stack configuration
  698. *
  699. * @param[out] config pointer to mesh stack configuration
  700. *
  701. * @return
  702. * - ESP_OK
  703. * - ESP_ERR_MESH_ARGUMENT
  704. */
  705. esp_err_t esp_mesh_get_config(mesh_cfg_t *config);
  706. /**
  707. * @brief Get router configuration
  708. *
  709. * @attention This API is used to dynamically modify the router configuration after mesh is configured.
  710. *
  711. * @param[in] router pointer to router configuration
  712. *
  713. * @return
  714. * - ESP_OK
  715. * - ESP_ERR_MESH_ARGUMENT
  716. */
  717. esp_err_t esp_mesh_set_router(const mesh_router_t *router);
  718. /**
  719. * @brief Get router configuration
  720. *
  721. * @param[out] router pointer to router configuration
  722. *
  723. * @return
  724. * - ESP_OK
  725. * - ESP_ERR_MESH_ARGUMENT
  726. */
  727. esp_err_t esp_mesh_get_router(mesh_router_t *router);
  728. /**
  729. * @brief Set mesh network ID
  730. *
  731. * @attention This API is used to dynamically modify the mesh network ID.
  732. *
  733. * @param[in] id pointer to mesh network ID
  734. *
  735. * @return
  736. * - ESP_OK
  737. * - ESP_ERR_MESH_ARGUMENT: invalid argument
  738. */
  739. esp_err_t esp_mesh_set_id(const mesh_addr_t *id);
  740. /**
  741. * @brief Get mesh network ID
  742. *
  743. * @param[out] id pointer to mesh network ID
  744. *
  745. * @return
  746. * - ESP_OK
  747. * - ESP_ERR_MESH_ARGUMENT
  748. */
  749. esp_err_t esp_mesh_get_id(mesh_addr_t *id);
  750. /**
  751. * @brief Designate device type over the mesh network
  752. * - MESH_ROOT: designates the root node for a mesh network
  753. * - MESH_LEAF: designates a device as a standalone Wi-Fi station
  754. *
  755. * @param[in] type device type
  756. *
  757. * @return
  758. * - ESP_OK
  759. * - ESP_ERR_MESH_NOT_ALLOWED
  760. */
  761. esp_err_t esp_mesh_set_type(mesh_type_t type);
  762. /**
  763. * @brief Get device type over mesh network
  764. *
  765. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  766. *
  767. * @return mesh type
  768. *
  769. */
  770. mesh_type_t esp_mesh_get_type(void);
  771. /**
  772. * @brief Set network max layer value (max:25, default:25)
  773. * - Network max layer limits the max hop count.
  774. *
  775. * @attention This API shall be called before mesh is started.
  776. *
  777. * @param[in] max_layer max layer value
  778. *
  779. * @return
  780. * - ESP_OK
  781. * - ESP_ERR_MESH_ARGUMENT
  782. * - ESP_ERR_MESH_NOT_ALLOWED
  783. */
  784. esp_err_t esp_mesh_set_max_layer(int max_layer);
  785. /**
  786. * @brief Get max layer value
  787. *
  788. * @return max layer value
  789. */
  790. int esp_mesh_get_max_layer(void);
  791. /**
  792. * @brief Set mesh softAP password
  793. *
  794. * @attention This API shall be called before mesh is started.
  795. *
  796. * @param[in] pwd pointer to the password
  797. * @param[in] len password length
  798. *
  799. * @return
  800. * - ESP_OK
  801. * - ESP_ERR_MESH_ARGUMENT
  802. * - ESP_ERR_MESH_NOT_ALLOWED
  803. */
  804. esp_err_t esp_mesh_set_ap_password(const uint8_t *pwd, int len);
  805. /**
  806. * @brief Set mesh softAP authentication mode
  807. *
  808. * @attention This API shall be called before mesh is started.
  809. *
  810. * @param[in] authmode authentication mode
  811. *
  812. * @return
  813. * - ESP_OK
  814. * - ESP_ERR_MESH_ARGUMENT
  815. * - ESP_ERR_MESH_NOT_ALLOWED
  816. */
  817. esp_err_t esp_mesh_set_ap_authmode(wifi_auth_mode_t authmode);
  818. /**
  819. * @brief Get mesh softAP authentication mode
  820. *
  821. * @return authentication mode
  822. */
  823. wifi_auth_mode_t esp_mesh_get_ap_authmode(void);
  824. /**
  825. * @brief Set mesh softAP max connection value
  826. *
  827. * @attention This API shall be called before mesh is started.
  828. *
  829. * @param[in] connections the number of max connections
  830. *
  831. * @return
  832. * - ESP_OK
  833. * - ESP_ERR_MESH_ARGUMENT
  834. */
  835. esp_err_t esp_mesh_set_ap_connections(int connections);
  836. /**
  837. * @brief Get mesh softAP max connection configuration
  838. *
  839. * @return the number of max connections
  840. */
  841. int esp_mesh_get_ap_connections(void);
  842. /**
  843. * @brief Get current layer value over the mesh network
  844. *
  845. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  846. *
  847. * @return layer value
  848. *
  849. */
  850. int esp_mesh_get_layer(void);
  851. /**
  852. * @brief Get the parent BSSID
  853. *
  854. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  855. *
  856. * @param[out] bssid pointer to parent BSSID
  857. *
  858. * @return
  859. * - ESP_OK
  860. * - ESP_FAIL
  861. */
  862. esp_err_t esp_mesh_get_parent_bssid(mesh_addr_t *bssid);
  863. /**
  864. * @brief Return whether the device is the root node of the network
  865. *
  866. * @return true/false
  867. */
  868. bool esp_mesh_is_root(void);
  869. /**
  870. * @brief Enable/disable self-organized networking
  871. * - Self-organized networking has three main functions:
  872. * select the root node;
  873. * find a preferred parent;
  874. * initiate reconnection if a disconnection is detected.
  875. * - Self-organized networking is enabled by default.
  876. * - If self-organized is disabled, users should set a parent for the device via esp_mesh_set_parent().
  877. *
  878. * @attention This API is used to dynamically modify whether to enable the self organizing.
  879. *
  880. * @param[in] enable enable or disable self-organized networking
  881. * @param[in] select_parent Only valid when self-organized networking is enabled.
  882. * - if select_parent is set to true, the root will give up its mesh root status and search for a new parent
  883. * like other non-root devices.
  884. *
  885. * @return
  886. * - ESP_OK
  887. * - ESP_FAIL
  888. */
  889. esp_err_t esp_mesh_set_self_organized(bool enable, bool select_parent);
  890. /**
  891. * @brief Return whether enable self-organized networking or not
  892. *
  893. * @return true/false
  894. */
  895. bool esp_mesh_get_self_organized(void);
  896. /**
  897. * @brief Cause the root device to give up (waive) its mesh root status
  898. * - A device is elected root primarily based on RSSI from the external router.
  899. * - If external router conditions change, users can call this API to perform a root switch.
  900. * - In this API, users could specify a desired root address to replace itself or specify an attempts value
  901. * to ask current root to initiate a new round of voting. During the voting, a better root candidate would
  902. * be expected to find to replace the current one.
  903. * - If no desired root candidate, the vote will try a specified number of attempts (at least 15). If no better
  904. * root candidate is found, keep the current one. If a better candidate is found, the new better one will
  905. * send a root switch request to the current root, current root will respond with a root switch acknowledgment.
  906. * - After that, the new candidate will connect to the router to be a new root, the previous root will disconnect
  907. * with the router and choose another parent instead.
  908. *
  909. * Root switch is completed with minimal disruption to the whole mesh network.
  910. *
  911. * @attention This API is only called by the root.
  912. *
  913. * @param[in] vote vote configuration
  914. * - If this parameter is set NULL, the vote will perform the default 15 times.
  915. *
  916. * - Field percentage threshold is 0.9 by default.
  917. * - Field is_rc_specified shall be false.
  918. * - Field attempts shall be at least 15 times.
  919. * @param[in] reason only accept MESH_VOTE_REASON_ROOT_INITIATED for now
  920. *
  921. * @return
  922. * - ESP_OK
  923. * - ESP_ERR_MESH_QUEUE_FULL
  924. * - ESP_ERR_MESH_DISCARD
  925. * - ESP_FAIL
  926. */
  927. esp_err_t esp_mesh_waive_root(const mesh_vote_t *vote, int reason);
  928. /**
  929. * @brief Set vote percentage threshold for approval of being a root
  930. * - During the networking, only obtaining vote percentage reaches this threshold,
  931. * the device could be a root.
  932. *
  933. * @attention This API shall be called before mesh is started.
  934. *
  935. * @param[in] percentage vote percentage threshold
  936. *
  937. * @return
  938. * - ESP_OK
  939. * - ESP_FAIL
  940. */
  941. esp_err_t esp_mesh_set_vote_percentage(float percentage);
  942. /**
  943. * @brief Get vote percentage threshold for approval of being a root
  944. *
  945. * @return percentage threshold
  946. */
  947. float esp_mesh_get_vote_percentage(void);
  948. /**
  949. * @brief Set mesh softAP associate expired time (default:10 seconds)
  950. * - If mesh softAP hasn't received any data from an associated child within this time,
  951. * mesh softAP will take this child inactive and disassociate it.
  952. * - If mesh softAP is encrypted, this value should be set a greater value, such as 30 seconds.
  953. *
  954. * @param[in] seconds the expired time
  955. *
  956. * @return
  957. * - ESP_OK
  958. * - ESP_FAIL
  959. */
  960. esp_err_t esp_mesh_set_ap_assoc_expire(int seconds);
  961. /**
  962. * @brief Get mesh softAP associate expired time
  963. *
  964. * @return seconds
  965. */
  966. int esp_mesh_get_ap_assoc_expire(void);
  967. /**
  968. * @brief Get total number of devices in current network (including the root)
  969. *
  970. * @attention The returned value might be incorrect when the network is changing.
  971. **
  972. * @return total number of devices (including the root)
  973. */
  974. int esp_mesh_get_total_node_num(void);
  975. /**
  976. * @brief Get the number of devices in this device's sub-network (including self)
  977. *
  978. * @return the number of devices over this device's sub-network (including self)
  979. */
  980. int esp_mesh_get_routing_table_size(void);
  981. /**
  982. * @brief Get routing table of this device's sub-network (including itself)
  983. *
  984. * @param[out] mac pointer to routing table
  985. * @param[in] len routing table size(in bytes)
  986. * @param[out] size pointer to the number of devices in routing table (including itself)
  987. *
  988. * @return
  989. * - ESP_OK
  990. * - ESP_ERR_MESH_ARGUMENT
  991. */
  992. esp_err_t esp_mesh_get_routing_table(mesh_addr_t *mac, int len, int *size);
  993. /**
  994. * @brief Post the toDS state to the mesh stack
  995. *
  996. * @attention This API is only for the root.
  997. *
  998. * @param[in] reachable this state represents whether the root is able to access external IP network
  999. *
  1000. * @return
  1001. * - ESP_OK
  1002. * - ESP_FAIL
  1003. */
  1004. esp_err_t esp_mesh_post_toDS_state(bool reachable);
  1005. /**
  1006. * @brief Return the number of packets pending in the queue waiting to be sent by the mesh stack
  1007. *
  1008. * @param[out] pending pointer to the TX pending
  1009. *
  1010. * @return
  1011. * - ESP_OK
  1012. * - ESP_FAIL
  1013. */
  1014. esp_err_t esp_mesh_get_tx_pending(mesh_tx_pending_t *pending);
  1015. /**
  1016. * @brief Return the number of packets available in the queue waiting to be received by applications
  1017. *
  1018. * @param[out] pending pointer to the RX pending
  1019. *
  1020. * @return
  1021. * - ESP_OK
  1022. * - ESP_FAIL
  1023. */
  1024. esp_err_t esp_mesh_get_rx_pending(mesh_rx_pending_t *pending);
  1025. /**
  1026. * @brief Return the number of packets could be accepted from the specified address
  1027. *
  1028. * @param[in] addr self address or an associate children address
  1029. * @param[out] xseqno_in sequence number of the last received packet from the specified address
  1030. *
  1031. * @return the number of upQ for a certain address
  1032. */
  1033. int esp_mesh_available_txupQ_num(const mesh_addr_t *addr, uint32_t *xseqno_in);
  1034. /**
  1035. * @brief Set the number of queue
  1036. *
  1037. * @attention This API shall be called before mesh is started.
  1038. *
  1039. * @param[in] qsize default:32 (min:16)
  1040. *
  1041. * @return
  1042. * - ESP_OK
  1043. * - ESP_FAIL
  1044. */
  1045. esp_err_t esp_mesh_set_xon_qsize(int qsize);
  1046. /**
  1047. * @brief Get queue size
  1048. *
  1049. * @return the number of queue
  1050. */
  1051. int esp_mesh_get_xon_qsize(void);
  1052. /**
  1053. * @brief Set whether allow more than one root existing in one network
  1054. *
  1055. * @param[in] allowed allow or not
  1056. *
  1057. * @return
  1058. * - ESP_OK
  1059. * - ESP_WIFI_ERR_NOT_INIT
  1060. * - ESP_WIFI_ERR_NOT_START
  1061. */
  1062. esp_err_t esp_mesh_allow_root_conflicts(bool allowed);
  1063. /**
  1064. * @brief Check whether allow more than one root to exist in one network
  1065. *
  1066. * @return true/false
  1067. */
  1068. bool esp_mesh_is_root_conflicts_allowed(void);
  1069. /**
  1070. * @brief Set group ID addresses
  1071. *
  1072. * @param[in] addr pointer to new group ID addresses
  1073. * @param[in] num the number of group ID addresses
  1074. *
  1075. * @return
  1076. * - ESP_OK
  1077. * - ESP_MESH_ERR_ARGUMENT
  1078. */
  1079. esp_err_t esp_mesh_set_group_id(const mesh_addr_t *addr, int num);
  1080. /**
  1081. * @brief Delete group ID addresses
  1082. *
  1083. * @param[in] addr pointer to deleted group ID address
  1084. * @param[in] num the number of group ID addresses
  1085. *
  1086. * @return
  1087. * - ESP_OK
  1088. * - ESP_MESH_ERR_ARGUMENT
  1089. */
  1090. esp_err_t esp_mesh_delete_group_id(const mesh_addr_t *addr, int num);
  1091. /**
  1092. * @brief Get the number of group ID addresses
  1093. *
  1094. * @return the number of group ID addresses
  1095. */
  1096. int esp_mesh_get_group_num(void);
  1097. /**
  1098. * @brief Get group ID addresses
  1099. *
  1100. * @param[out] addr pointer to group ID addresses
  1101. * @param[in] num the number of group ID addresses
  1102. *
  1103. * @return
  1104. * - ESP_OK
  1105. * - ESP_MESH_ERR_ARGUMENT
  1106. */
  1107. esp_err_t esp_mesh_get_group_list(mesh_addr_t *addr, int num);
  1108. /**
  1109. * @brief Check whether the specified group address is my group
  1110. *
  1111. * @return true/false
  1112. */
  1113. bool esp_mesh_is_my_group(const mesh_addr_t *addr);
  1114. /**
  1115. * @brief Set mesh network capacity (max:1000, default:300)
  1116. *
  1117. * @attention This API shall be called before mesh is started.
  1118. *
  1119. * @param[in] num mesh network capacity
  1120. *
  1121. * @return
  1122. * - ESP_OK
  1123. * - ESP_ERR_MESH_NOT_ALLOWED
  1124. * - ESP_MESH_ERR_ARGUMENT
  1125. */
  1126. esp_err_t esp_mesh_set_capacity_num(int num);
  1127. /**
  1128. * @brief Get mesh network capacity
  1129. *
  1130. * @return mesh network capacity
  1131. */
  1132. int esp_mesh_get_capacity_num(void);
  1133. /**
  1134. * @brief Set mesh IE crypto functions
  1135. *
  1136. * @attention This API can be called at any time after mesh is initialized.
  1137. *
  1138. * @param[in] crypto_funcs crypto functions for mesh IE
  1139. * - If crypto_funcs is set to NULL, mesh IE is no longer encrypted.
  1140. * @return
  1141. * - ESP_OK
  1142. */
  1143. esp_err_t esp_mesh_set_ie_crypto_funcs(const mesh_crypto_funcs_t *crypto_funcs);
  1144. /**
  1145. * @brief Set mesh IE crypto key
  1146. *
  1147. * @attention This API can be called at any time after mesh is initialized.
  1148. *
  1149. * @param[in] key ASCII crypto key
  1150. * @param[in] len length in bytes, range:8~64
  1151. *
  1152. * @return
  1153. * - ESP_OK
  1154. * - ESP_MESH_ERR_ARGUMENT
  1155. */
  1156. esp_err_t esp_mesh_set_ie_crypto_key(const char *key, int len);
  1157. /**
  1158. * @brief Get mesh IE crypto key
  1159. *
  1160. * @param[out] key ASCII crypto key
  1161. * @param[in] len length in bytes, range:8~64
  1162. *
  1163. * @return
  1164. * - ESP_OK
  1165. * - ESP_MESH_ERR_ARGUMENT
  1166. */
  1167. esp_err_t esp_mesh_get_ie_crypto_key(char *key, int len);
  1168. /**
  1169. * @brief Set delay time before starting root healing
  1170. *
  1171. * @param[in] delay_ms delay time in milliseconds
  1172. *
  1173. * @return
  1174. * - ESP_OK
  1175. */
  1176. esp_err_t esp_mesh_set_root_healing_delay(int delay_ms);
  1177. /**
  1178. * @brief Get delay time before network starts root healing
  1179. *
  1180. * @return delay time in milliseconds
  1181. */
  1182. int esp_mesh_get_root_healing_delay(void);
  1183. /**
  1184. * @brief Enable network Fixed Root Setting
  1185. * - Enabling fixed root disables automatic election of the root node via voting.
  1186. * - All devices in the network shall use the same Fixed Root Setting (enabled or disabled).
  1187. * - If Fixed Root is enabled, users should make sure a root node is designated for the network.
  1188. *
  1189. * @param[in] enable enable or not
  1190. *
  1191. * @return
  1192. * - ESP_OK
  1193. */
  1194. esp_err_t esp_mesh_fix_root(bool enable);
  1195. /**
  1196. * @brief Check whether network Fixed Root Setting is enabled
  1197. * - Enable/disable network Fixed Root Setting by API esp_mesh_fix_root().
  1198. * - Network Fixed Root Setting also changes with the "flag" value in parent networking IE.
  1199. *
  1200. * @return true/false
  1201. */
  1202. bool esp_mesh_is_root_fixed(void);
  1203. /**
  1204. * @brief Set a specified parent for the device
  1205. *
  1206. * @attention This API can be called at any time after mesh is configured.
  1207. *
  1208. * @param[in] parent parent configuration, the SSID and the channel of the parent are mandatory.
  1209. * - If the BSSID is set, make sure that the SSID and BSSID represent the same parent,
  1210. * otherwise the device will never find this specified parent.
  1211. * @param[in] parent_mesh_id parent mesh ID,
  1212. * - If this value is not set, the original mesh ID is used.
  1213. * @param[in] my_type mesh type
  1214. * - If the parent set for the device is the same as the router in the network configuration,
  1215. * then my_type shall set MESH_ROOT and my_layer shall set MESH_ROOT_LAYER.
  1216. * @param[in] my_layer mesh layer
  1217. * - my_layer of the device may change after joining the network.
  1218. * - If my_type is set MESH_NODE, my_layer shall be greater than MESH_ROOT_LAYER.
  1219. * - If my_type is set MESH_LEAF, the device becomes a standalone Wi-Fi station and no longer
  1220. * has the ability to extend the network.
  1221. *
  1222. * @return
  1223. * - ESP_OK
  1224. * - ESP_ERR_ARGUMENT
  1225. * - ESP_ERR_MESH_NOT_CONFIG
  1226. */
  1227. esp_err_t esp_mesh_set_parent(const wifi_config_t *parent, const mesh_addr_t *parent_mesh_id, mesh_type_t my_type, int my_layer);
  1228. /**
  1229. * @brief Get mesh networking IE length of one AP
  1230. *
  1231. * @param[out] len mesh networking IE length
  1232. *
  1233. * @return
  1234. * - ESP_OK
  1235. * - ESP_ERR_WIFI_NOT_INIT
  1236. * - ESP_ERR_WIFI_ARG
  1237. * - ESP_ERR_WIFI_FAIL
  1238. */
  1239. esp_err_t esp_mesh_scan_get_ap_ie_len(int *len);
  1240. /**
  1241. * @brief Get AP record
  1242. *
  1243. * @attention Different from esp_wifi_scan_get_ap_records(), this API only gets one of APs scanned each time.
  1244. * See "manual_networking" example.
  1245. *
  1246. * @param[out] ap_record pointer to one AP record
  1247. * @param[out] buffer pointer to the mesh networking IE of this AP
  1248. *
  1249. * @return
  1250. * - ESP_OK
  1251. * - ESP_ERR_WIFI_NOT_INIT
  1252. * - ESP_ERR_WIFI_ARG
  1253. * - ESP_ERR_WIFI_FAIL
  1254. */
  1255. esp_err_t esp_mesh_scan_get_ap_record(wifi_ap_record_t *ap_record, void *buffer);
  1256. /**
  1257. * @brief Flush upstream packets pending in to_parent queue and to_parent_p2p queue
  1258. *
  1259. * @return
  1260. * - ESP_OK
  1261. */
  1262. esp_err_t esp_mesh_flush_upstream_packets(void);
  1263. /**
  1264. * @brief Get the number of nodes in the subnet of a specific child
  1265. *
  1266. * @param[in] child_mac an associated child address of this device
  1267. * @param[out] nodes_num pointer to the number of nodes in the subnet of a specific child
  1268. *
  1269. * @return
  1270. * - ESP_OK
  1271. * - ESP_ERR_MESH_NOT_START
  1272. * - ESP_ERR_MESH_ARGUMENT
  1273. */
  1274. esp_err_t esp_mesh_get_subnet_nodes_num(const mesh_addr_t *child_mac, int *nodes_num);
  1275. /**
  1276. * @brief Get nodes in the subnet of a specific child
  1277. *
  1278. * @param[in] child_mac an associated child address of this device
  1279. * @param[out] nodes pointer to nodes in the subnet of a specific child
  1280. * @param[in] nodes_num the number of nodes in the subnet of a specific child
  1281. *
  1282. * @return
  1283. * - ESP_OK
  1284. * - ESP_ERR_MESH_NOT_START
  1285. * - ESP_ERR_MESH_ARGUMENT
  1286. */
  1287. esp_err_t esp_mesh_get_subnet_nodes_list(const mesh_addr_t *child_mac, mesh_addr_t *nodes, int nodes_num);
  1288. /**
  1289. * @brief Disconnect from current parent
  1290. *
  1291. * @return
  1292. * - ESP_OK
  1293. */
  1294. esp_err_t esp_mesh_disconnect(void);
  1295. /**
  1296. * @brief Connect to current parent
  1297. *
  1298. * @return
  1299. * - ESP_OK
  1300. */
  1301. esp_err_t esp_mesh_connect(void);
  1302. /**
  1303. * @brief Flush scan result
  1304. *
  1305. * @return
  1306. * - ESP_OK
  1307. */
  1308. esp_err_t esp_mesh_flush_scan_result(void);
  1309. /**
  1310. * @brief Cause the root device to add Channel Switch Announcement Element (CSA IE) to beacon
  1311. * - Set the new channel
  1312. * - Set how many beacons with CSA IE will be sent before changing a new channel
  1313. * - Enable the channel switch function
  1314. *
  1315. * @attention This API is only called by the root.
  1316. *
  1317. * @param[in] new_bssid the new router BSSID if the router changes
  1318. * @param[in] csa_newchan the new channel number to which the whole network is moving
  1319. * @param[in] csa_count channel switch period(beacon count), unit is based on beacon interval of its softAP, the default value is 15.
  1320. *
  1321. * @return
  1322. * - ESP_OK
  1323. */
  1324. esp_err_t esp_mesh_switch_channel(const uint8_t *new_bssid, int csa_newchan, int csa_count);
  1325. /**
  1326. * @brief Get the router BSSID
  1327. *
  1328. * @param[out] router_bssid pointer to the router BSSID
  1329. *
  1330. * @return
  1331. * - ESP_OK
  1332. * - ESP_ERR_WIFI_NOT_INIT
  1333. * - ESP_ERR_WIFI_ARG
  1334. */
  1335. esp_err_t esp_mesh_get_router_bssid(uint8_t *router_bssid);
  1336. /**
  1337. * @brief Get the TSF time
  1338. *
  1339. * @return the TSF time
  1340. */
  1341. int64_t esp_mesh_get_tsf_time(void);
  1342. #ifdef __cplusplus
  1343. }
  1344. #endif
  1345. #endif /* __ESP_MESH_H__ */