esp_mesh.h 65 KB

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