esp_mesh.h 57 KB

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