esp_mesh.h 66 KB

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