esp_mesh.h 65 KB

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