esp_mesh.h 65 KB

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