esp_mesh.h 56 KB

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