esp_mesh.h 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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_ASKED_YIELD, /**< the root is asked yield by a more powerful existing root. If self organized is disabled
  168. and this device is specified to be a root by users, users should set a new parent
  169. for this device. if self organized is enabled, this device will find a new parent
  170. by itself, users could ignore this event. */
  171. MESH_EVENT_ROOT_FIXED, /**< when devices join a network, if the setting of Fixed Root for one device is different
  172. from that of its parent, the device will update the setting the same as its parent's.
  173. Fixed Root Setting of each device is variable as that setting changes of the root. */
  174. MESH_EVENT_SCAN_DONE, /**< if self-organized networking is disabled, user can call esp_wifi_scan_start() to trigger
  175. this event, and add the corresponding scan done handler in this event. */
  176. MESH_EVENT_NETWORK_STATE, /**< network state, such as whether current mesh network has a root. */
  177. MESH_EVENT_STOP_RECONNECTION, /**< the root stops reconnecting to the router and non-root devices stop reconnecting to their parents. */
  178. MESH_EVENT_FIND_NETWORK, /**< when the channel field in mesh configuration is set to zero, mesh stack will perform a
  179. full channel scan to find a mesh network that can join, and return the channel value
  180. after finding it. */
  181. MESH_EVENT_ROUTER_SWITCH, /**< if users specify BSSID of the router in mesh configuration, when the root connects to another
  182. router with the same SSID, this event will be posted and the new router information is attached. */
  183. MESH_EVENT_MAX,
  184. } mesh_event_id_t;
  185. /** @brief ESP-MESH event base declaration */
  186. ESP_EVENT_DECLARE_BASE(MESH_EVENT);
  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 option
  394. */
  395. typedef struct {
  396. uint8_t type; /**< option type */
  397. uint16_t len; /**< option length */
  398. uint8_t *val; /**< option value */
  399. } __attribute__((packed)) mesh_opt_t;
  400. /**
  401. * @brief Mesh data for esp_mesh_send() and esp_mesh_recv()
  402. */
  403. typedef struct {
  404. uint8_t *data; /**< data */
  405. uint16_t size; /**< data size */
  406. mesh_proto_t proto; /**< data protocol */
  407. mesh_tos_t tos; /**< data type of service */
  408. } mesh_data_t;
  409. /**
  410. * @brief Router configuration
  411. */
  412. typedef struct {
  413. uint8_t ssid[32]; /**< SSID */
  414. uint8_t ssid_len; /**< length of SSID */
  415. uint8_t bssid[6]; /**< BSSID, if this value is specified, users should also specify "allow_router_switch". */
  416. uint8_t password[64]; /**< password */
  417. bool allow_router_switch; /**< if the BSSID is specified and this value is also set, when the router of this specified BSSID
  418. fails to be found after "fail" (mesh_attempts_t) times, the whole network is allowed to switch
  419. to another router with the same SSID. The new router might also be on a different channel.
  420. The default value is false.
  421. There is a risk that if the password is different between the new switched router and the previous
  422. one, the mesh network could be established but the root will never connect to the new switched router. */
  423. } mesh_router_t;
  424. /**
  425. * @brief Mesh softAP configuration
  426. */
  427. typedef struct {
  428. uint8_t password[64]; /**< mesh softAP password */
  429. uint8_t max_connection; /**< max number of stations allowed to connect in, max 10 */
  430. } mesh_ap_cfg_t;
  431. /**
  432. * @brief Mesh initialization configuration
  433. */
  434. typedef struct {
  435. uint8_t channel; /**< channel, the mesh network on */
  436. bool allow_channel_switch; /**< if this value is set, when "fail" (mesh_attempts_t) times is reached, device will change to
  437. a full channel scan for a network that could join. The default value is false. */
  438. mesh_addr_t mesh_id; /**< mesh network identification */
  439. mesh_router_t router; /**< router configuration */
  440. mesh_ap_cfg_t mesh_ap; /**< mesh softAP configuration */
  441. const mesh_crypto_funcs_t *crypto_funcs; /**< crypto functions */
  442. } mesh_cfg_t;
  443. /**
  444. * @brief Vote address configuration
  445. */
  446. typedef union {
  447. int attempts; /**< max vote attempts before a new root is elected automatically by mesh network. (min:15, 15 by default) */
  448. mesh_addr_t rc_addr; /**< a new root address specified by users for API esp_mesh_waive_root() */
  449. } mesh_rc_config_t;
  450. /**
  451. * @brief Vote
  452. */
  453. typedef struct {
  454. float percentage; /**< vote percentage threshold for approval of being a root */
  455. bool is_rc_specified; /**< if true, rc_addr shall be specified (Unimplemented).
  456. if false, attempts value shall be specified to make network start root election. */
  457. mesh_rc_config_t config; /**< vote address configuration */
  458. } mesh_vote_t;
  459. /**
  460. * @brief The number of packets pending in the queue waiting to be sent by the mesh stack
  461. */
  462. typedef struct {
  463. int to_parent; /**< to parent queue */
  464. int to_parent_p2p; /**< to parent (P2P) queue */
  465. int to_child; /**< to child queue */
  466. int to_child_p2p; /**< to child (P2P) queue */
  467. int mgmt; /**< management queue */
  468. int broadcast; /**< broadcast and multicast queue */
  469. } mesh_tx_pending_t;
  470. /**
  471. * @brief The number of packets available in the queue waiting to be received by applications
  472. */
  473. typedef struct {
  474. int toDS; /**< to external DS */
  475. int toSelf; /**< to self */
  476. } mesh_rx_pending_t;
  477. /*******************************************************
  478. * Variable Declaration
  479. *******************************************************/
  480. /* mesh IE crypto callback function */
  481. extern const mesh_crypto_funcs_t g_wifi_default_mesh_crypto_funcs;
  482. #define MESH_INIT_CONFIG_DEFAULT() { \
  483. .crypto_funcs = &g_wifi_default_mesh_crypto_funcs, \
  484. }
  485. /*******************************************************
  486. * Function Definitions
  487. *******************************************************/
  488. /**
  489. * @brief Mesh initialization
  490. * - Check whether Wi-Fi is started.
  491. * - Initialize mesh global variables with default values.
  492. *
  493. * @attention This API shall be called after Wi-Fi is started.
  494. *
  495. * @return
  496. * - ESP_OK
  497. * - ESP_FAIL
  498. */
  499. esp_err_t esp_mesh_init(void);
  500. /**
  501. * @brief Mesh de-initialization
  502. *
  503. * - Release resources and stop the mesh
  504. *
  505. * @return
  506. * - ESP_OK
  507. * - ESP_FAIL
  508. */
  509. esp_err_t esp_mesh_deinit(void);
  510. /**
  511. * @brief Start mesh
  512. * - Initialize mesh IE.
  513. * - Start mesh network management service.
  514. * - Create TX and RX queues according to the configuration.
  515. * - Register mesh packets receive callback.
  516. *
  517. * @attention  This API shall be called after mesh initialization and configuration.
  518. *
  519. * @return
  520. * - ESP_OK
  521. * - ESP_FAIL
  522. * - ESP_ERR_MESH_NOT_INIT
  523. * - ESP_ERR_MESH_NOT_CONFIG
  524. * - ESP_ERR_MESH_NO_MEMORY
  525. */
  526. esp_err_t esp_mesh_start(void);
  527. /**
  528. * @brief Stop mesh
  529. * - Deinitialize mesh IE.
  530. * - Disconnect with current parent.
  531. * - Disassociate all currently associated children.
  532. * - Stop mesh network management service.
  533. * - Unregister mesh packets receive callback.
  534. * - Delete TX and RX queues.
  535. * - Release resources.
  536. * - Restore Wi-Fi softAP to default settings if Wi-Fi dual mode is enabled.
  537. *
  538. * @return
  539. * - ESP_OK
  540. * - ESP_FAIL
  541. */
  542. esp_err_t esp_mesh_stop(void);
  543. /**
  544. * @brief Send a packet over the mesh network
  545. * - Send a packet to any device in the mesh network.
  546. * - Send a packet to external IP network.
  547. *
  548. * @attention This API is not reentrant.
  549. *
  550. * @param[in] to the address of the final destination of the packet
  551. * - If the packet is to the root, set this parameter to NULL.
  552. * - If the packet is to an external IP network, set this parameter to the IPv4:PORT combination.
  553. * This packet will be delivered to the root firstly, then the root will forward this packet to the final IP server address.
  554. * @param[in] data pointer to a sending mesh packet
  555. * - Field size should not exceed MESH_MPS. Note that the size of one mesh packet should not exceed MESH_MTU.
  556. * - Field proto should be set to data protocol in use (default is MESH_PROTO_BIN for binary).
  557. * - Field tos should be set to transmission tos (type of service) in use (default is MESH_TOS_P2P for point-to-point reliable).
  558. * @param[in] flag bitmap for data sent
  559. * - Speed up the route search
  560. * - If the packet is to the root and "to" parameter is NULL, set this parameter to 0.
  561. * - If the packet is to an internal device, MESH_DATA_P2P should be set.
  562. * - If the packet is to the root ("to" parameter isn't NULL) or to external IP network, MESH_DATA_TODS should be set.
  563. * - If the packet is from the root to an internal device, MESH_DATA_FROMDS should be set.
  564. * - Specify whether this API is block or non-block, block by default
  565. * - If needs non-blocking, MESH_DATA_NONBLOCK should be set. Otherwise, may use esp_mesh_send_block_time() to specify a blocking time.
  566. * - In the situation of the root change, MESH_DATA_DROP identifies this packet can be dropped by the new root
  567. * for upstream data to external IP network, we try our best to avoid data loss caused by the root change, but
  568. * there is a risk that the new root is running out of memory because most of memory is occupied by the pending data which
  569. * isn't read out in time by esp_mesh_recv_toDS().
  570. *
  571. * Generally, we suggest esp_mesh_recv_toDS() is called after a connection with IP network is created. Thus data outgoing
  572. * to external IP network via socket is just from reading esp_mesh_recv_toDS() which avoids unnecessary memory copy.
  573. *
  574. * @param[in] opt options
  575. * - In case of sending a packet to a certain group, MESH_OPT_SEND_GROUP is a good choice.
  576. * In this option, the value field should be set to the target receiver addresses in this group.
  577. * - Root sends a packet to an internal device, this packet is from external IP network in case the receiver device responds
  578. * this packet, MESH_OPT_RECV_DS_ADDR is required to attach the target DS address.
  579. * @param[in] opt_count option count
  580. * - Currently, this API only takes one option, so opt_count is only supported to be 1.
  581. *
  582. * @return
  583. * - ESP_OK
  584. * - ESP_FAIL
  585. * - ESP_ERR_MESH_ARGUMENT
  586. * - ESP_ERR_MESH_NOT_START
  587. * - ESP_ERR_MESH_DISCONNECTED
  588. * - ESP_ERR_MESH_OPT_UNKNOWN
  589. * - ESP_ERR_MESH_EXCEED_MTU
  590. * - ESP_ERR_MESH_NO_MEMORY
  591. * - ESP_ERR_MESH_TIMEOUT
  592. * - ESP_ERR_MESH_QUEUE_FULL
  593. * - ESP_ERR_MESH_NO_ROUTE_FOUND
  594. * - ESP_ERR_MESH_DISCARD
  595. */
  596. esp_err_t esp_mesh_send(const mesh_addr_t *to, const mesh_data_t *data,
  597. int flag, const mesh_opt_t opt[], int opt_count);
  598. /**
  599. * @brief Set blocking time of esp_mesh_send()
  600. *
  601. * @attention This API shall be called before mesh is started.
  602. *
  603. * @param[in] time_ms blocking time of esp_mesh_send(), unit:ms
  604. *
  605. * @return
  606. * - ESP_OK
  607. */
  608. esp_err_t esp_mesh_send_block_time(uint32_t time_ms);
  609. /**
  610. * @brief Receive a packet targeted to self over the mesh network
  611. *
  612. * @attention Mesh RX queue should be checked regularly to avoid running out of memory.
  613. * - Use esp_mesh_get_rx_pending() to check the number of packets available in the queue waiting
  614. * to be received by applications.
  615. *
  616. * @param[out] from the address of the original source of the packet
  617. * @param[out] data pointer to the received mesh packet
  618. * - Field proto is the data protocol in use. Should follow it to parse the received data.
  619. * - Field tos is the transmission tos (type of service) in use.
  620. * @param[in] timeout_ms wait time if a packet isn't immediately available (0:no wait, portMAX_DELAY:wait forever)
  621. * @param[out] flag bitmap for data received
  622. * - MESH_DATA_FROMDS represents data from external IP network
  623. * - MESH_DATA_TODS represents data directed upward within the mesh network
  624. *
  625. * flag could be MESH_DATA_FROMDS or MESH_DATA_TODS.
  626. * @param[out] opt options desired to receive
  627. * - MESH_OPT_RECV_DS_ADDR attaches the DS address
  628. * @param[in] opt_count option count desired to receive
  629. * - Currently, this API only takes one option, so opt_count is only supported to be 1.
  630. *
  631. * @return
  632. * - ESP_OK
  633. * - ESP_ERR_MESH_ARGUMENT
  634. * - ESP_ERR_MESH_NOT_START
  635. * - ESP_ERR_MESH_TIMEOUT
  636. * - ESP_ERR_MESH_DISCARD
  637. */
  638. esp_err_t esp_mesh_recv(mesh_addr_t *from, mesh_data_t *data, int timeout_ms,
  639. int *flag, mesh_opt_t opt[], int opt_count);
  640. /**
  641. * @brief Receive a packet targeted to external IP network
  642. * - Root uses this API to receive packets destined to external IP network
  643. * - Root forwards the received packets to the final destination via socket.
  644. * - If no socket connection is ready to send out the received packets and this esp_mesh_recv_toDS()
  645. * hasn't been called by applications, packets from the whole mesh network will be pending in toDS queue.
  646. *
  647. * Use esp_mesh_get_rx_pending() to check the number of packets available in the queue waiting
  648. * to be received by applications in case of running out of memory in the root.
  649. *
  650. * Using esp_mesh_set_xon_qsize() users may configure the RX queue size, default:32. If this size is too large,
  651. * and esp_mesh_recv_toDS() isn't called in time, there is a risk that a great deal of memory is occupied
  652. * by the pending packets. If this size is too small, it will impact the efficiency on upstream. How to
  653. * decide this value depends on the specific application scenarios.
  654. *
  655. * @attention This API is only called by the root.
  656. *
  657. * @param[out] from the address of the original source of the packet
  658. * @param[out] to the address contains remote IP address and port (IPv4:PORT)
  659. * @param[out] data pointer to the received packet
  660. * - Contain the protocol and applications should follow it to parse the data.
  661. * @param[in] timeout_ms wait time if a packet isn't immediately available (0:no wait, portMAX_DELAY:wait forever)
  662. * @param[out] flag bitmap for data received
  663. * - 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.
  664. *
  665. * flag could be MESH_DATA_TODS.
  666. * @param[out] opt options desired to receive
  667. * @param[in] opt_count option count desired to receive
  668. *
  669. * @return
  670. * - ESP_OK
  671. * - ESP_ERR_MESH_ARGUMENT
  672. * - ESP_ERR_MESH_NOT_START
  673. * - ESP_ERR_MESH_TIMEOUT
  674. * - ESP_ERR_MESH_DISCARD
  675. */
  676. esp_err_t esp_mesh_recv_toDS(mesh_addr_t *from, mesh_addr_t *to,
  677. mesh_data_t *data, int timeout_ms, int *flag, mesh_opt_t opt[],
  678. int opt_count);
  679. /**
  680. * @brief Set mesh stack configuration
  681. * - Use MESH_INIT_CONFIG_DEFAULT() to initialize the default values, mesh IE is encrypted by default.
  682. * - Mesh network is established on a fixed channel (1-14).
  683. * - Mesh event callback is mandatory.
  684. * - Mesh ID is an identifier of an MBSS. Nodes with the same mesh ID can communicate with each other.
  685. * - Regarding to the router configuration, if the router is hidden, BSSID field is mandatory.
  686. *
  687. * If BSSID field isn't set and there exists more than one router with same SSID, there is a risk that more
  688. * roots than one connected with different BSSID will appear. It means more than one mesh network is established
  689. * with the same mesh ID.
  690. *
  691. * Root conflict function could eliminate redundant roots connected with the same BSSID, but couldn't handle roots
  692. * connected with different BSSID. Because users might have such requirements of setting up routers with same SSID
  693. * for the future replacement. But in that case, if the above situations happen, please make sure applications
  694. * implement forward functions on the root to guarantee devices in different mesh networks can communicate with each other.
  695. * max_connection of mesh softAP is limited by the max number of Wi-Fi softAP supported (max:10).
  696. *
  697. * @attention This API shall be called before mesh is started after mesh is initialized.
  698. *
  699. * @param[in] config pointer to mesh stack configuration
  700. *
  701. * @return
  702. * - ESP_OK
  703. * - ESP_ERR_MESH_ARGUMENT
  704. * - ESP_ERR_MESH_NOT_ALLOWED
  705. */
  706. esp_err_t esp_mesh_set_config(const mesh_cfg_t *config);
  707. /**
  708. * @brief Get mesh stack configuration
  709. *
  710. * @param[out] config pointer to mesh stack configuration
  711. *
  712. * @return
  713. * - ESP_OK
  714. * - ESP_ERR_MESH_ARGUMENT
  715. */
  716. esp_err_t esp_mesh_get_config(mesh_cfg_t *config);
  717. /**
  718. * @brief Get router configuration
  719. *
  720. * @attention This API is used to dynamically modify the router configuration after mesh is configured.
  721. *
  722. * @param[in] router pointer to router configuration
  723. *
  724. * @return
  725. * - ESP_OK
  726. * - ESP_ERR_MESH_ARGUMENT
  727. */
  728. esp_err_t esp_mesh_set_router(const mesh_router_t *router);
  729. /**
  730. * @brief Get router configuration
  731. *
  732. * @param[out] router pointer to router configuration
  733. *
  734. * @return
  735. * - ESP_OK
  736. * - ESP_ERR_MESH_ARGUMENT
  737. */
  738. esp_err_t esp_mesh_get_router(mesh_router_t *router);
  739. /**
  740. * @brief Set mesh network ID
  741. *
  742. * @attention This API is used to dynamically modify the mesh network ID.
  743. *
  744. * @param[in] id pointer to mesh network ID
  745. *
  746. * @return
  747. * - ESP_OK
  748. * - ESP_ERR_MESH_ARGUMENT: invalid argument
  749. */
  750. esp_err_t esp_mesh_set_id(const mesh_addr_t *id);
  751. /**
  752. * @brief Get mesh network ID
  753. *
  754. * @param[out] id pointer to mesh network ID
  755. *
  756. * @return
  757. * - ESP_OK
  758. * - ESP_ERR_MESH_ARGUMENT
  759. */
  760. esp_err_t esp_mesh_get_id(mesh_addr_t *id);
  761. /**
  762. * @brief Designate device type over the mesh network
  763. * - MESH_ROOT: designates the root node for a mesh network
  764. * - MESH_LEAF: designates a device as a standalone Wi-Fi station
  765. *
  766. * @param[in] type device type
  767. *
  768. * @return
  769. * - ESP_OK
  770. * - ESP_ERR_MESH_NOT_ALLOWED
  771. */
  772. esp_err_t esp_mesh_set_type(mesh_type_t type);
  773. /**
  774. * @brief Get device type over mesh network
  775. *
  776. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  777. *
  778. * @return mesh type
  779. *
  780. */
  781. mesh_type_t esp_mesh_get_type(void);
  782. /**
  783. * @brief Set network max layer value (max:25, default:25)
  784. * - Network max layer limits the max hop count.
  785. *
  786. * @attention This API shall be called before mesh is started.
  787. *
  788. * @param[in] max_layer max layer value
  789. *
  790. * @return
  791. * - ESP_OK
  792. * - ESP_ERR_MESH_ARGUMENT
  793. * - ESP_ERR_MESH_NOT_ALLOWED
  794. */
  795. esp_err_t esp_mesh_set_max_layer(int max_layer);
  796. /**
  797. * @brief Get max layer value
  798. *
  799. * @return max layer value
  800. */
  801. int esp_mesh_get_max_layer(void);
  802. /**
  803. * @brief Set mesh softAP password
  804. *
  805. * @attention This API shall be called before mesh is started.
  806. *
  807. * @param[in] pwd pointer to the password
  808. * @param[in] len password length
  809. *
  810. * @return
  811. * - ESP_OK
  812. * - ESP_ERR_MESH_ARGUMENT
  813. * - ESP_ERR_MESH_NOT_ALLOWED
  814. */
  815. esp_err_t esp_mesh_set_ap_password(const uint8_t *pwd, int len);
  816. /**
  817. * @brief Set mesh softAP authentication mode
  818. *
  819. * @attention This API shall be called before mesh is started.
  820. *
  821. * @param[in] authmode authentication mode
  822. *
  823. * @return
  824. * - ESP_OK
  825. * - ESP_ERR_MESH_ARGUMENT
  826. * - ESP_ERR_MESH_NOT_ALLOWED
  827. */
  828. esp_err_t esp_mesh_set_ap_authmode(wifi_auth_mode_t authmode);
  829. /**
  830. * @brief Get mesh softAP authentication mode
  831. *
  832. * @return authentication mode
  833. */
  834. wifi_auth_mode_t esp_mesh_get_ap_authmode(void);
  835. /**
  836. * @brief Set mesh softAP max connection value
  837. *
  838. * @attention This API shall be called before mesh is started.
  839. *
  840. * @param[in] connections the number of max connections
  841. *
  842. * @return
  843. * - ESP_OK
  844. * - ESP_ERR_MESH_ARGUMENT
  845. */
  846. esp_err_t esp_mesh_set_ap_connections(int connections);
  847. /**
  848. * @brief Get mesh softAP max connection configuration
  849. *
  850. * @return the number of max connections
  851. */
  852. int esp_mesh_get_ap_connections(void);
  853. /**
  854. * @brief Get current layer value over the mesh network
  855. *
  856. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  857. *
  858. * @return layer value
  859. *
  860. */
  861. int esp_mesh_get_layer(void);
  862. /**
  863. * @brief Get the parent BSSID
  864. *
  865. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  866. *
  867. * @param[out] bssid pointer to parent BSSID
  868. *
  869. * @return
  870. * - ESP_OK
  871. * - ESP_FAIL
  872. */
  873. esp_err_t esp_mesh_get_parent_bssid(mesh_addr_t *bssid);
  874. /**
  875. * @brief Return whether the device is the root node of the network
  876. *
  877. * @return true/false
  878. */
  879. bool esp_mesh_is_root(void);
  880. /**
  881. * @brief Enable/disable self-organized networking
  882. * - Self-organized networking has three main functions:
  883. * select the root node;
  884. * find a preferred parent;
  885. * initiate reconnection if a disconnection is detected.
  886. * - Self-organized networking is enabled by default.
  887. * - If self-organized is disabled, users should set a parent for the device via esp_mesh_set_parent().
  888. *
  889. * @attention This API is used to dynamically modify whether to enable the self organizing.
  890. *
  891. * @param[in] enable enable or disable self-organized networking
  892. * @param[in] select_parent Only valid when self-organized networking is enabled.
  893. * - if select_parent is set to true, the root will give up its mesh root status and search for a new parent
  894. * like other non-root devices.
  895. *
  896. * @return
  897. * - ESP_OK
  898. * - ESP_FAIL
  899. */
  900. esp_err_t esp_mesh_set_self_organized(bool enable, bool select_parent);
  901. /**
  902. * @brief Return whether enable self-organized networking or not
  903. *
  904. * @return true/false
  905. */
  906. bool esp_mesh_get_self_organized(void);
  907. /**
  908. * @brief Cause the root device to give up (waive) its mesh root status
  909. * - A device is elected root primarily based on RSSI from the external router.
  910. * - If external router conditions change, users can call this API to perform a root switch.
  911. * - In this API, users could specify a desired root address to replace itself or specify an attempts value
  912. * to ask current root to initiate a new round of voting. During the voting, a better root candidate would
  913. * be expected to find to replace the current one.
  914. * - If no desired root candidate, the vote will try a specified number of attempts (at least 15). If no better
  915. * root candidate is found, keep the current one. If a better candidate is found, the new better one will
  916. * send a root switch request to the current root, current root will respond with a root switch acknowledgment.
  917. * - After that, the new candidate will connect to the router to be a new root, the previous root will disconnect
  918. * with the router and choose another parent instead.
  919. *
  920. * Root switch is completed with minimal disruption to the whole mesh network.
  921. *
  922. * @attention This API is only called by the root.
  923. *
  924. * @param[in] vote vote configuration
  925. * - If this parameter is set NULL, the vote will perform the default 15 times.
  926. *
  927. * - Field percentage threshold is 0.9 by default.
  928. * - Field is_rc_specified shall be false.
  929. * - Field attempts shall be at least 15 times.
  930. * @param[in] reason only accept MESH_VOTE_REASON_ROOT_INITIATED for now
  931. *
  932. * @return
  933. * - ESP_OK
  934. * - ESP_ERR_MESH_QUEUE_FULL
  935. * - ESP_ERR_MESH_DISCARD
  936. * - ESP_FAIL
  937. */
  938. esp_err_t esp_mesh_waive_root(const mesh_vote_t *vote, int reason);
  939. /**
  940. * @brief Set vote percentage threshold for approval of being a root
  941. * - During the networking, only obtaining vote percentage reaches this threshold,
  942. * the device could be a root.
  943. *
  944. * @attention This API shall be called before mesh is started.
  945. *
  946. * @param[in] percentage vote percentage threshold
  947. *
  948. * @return
  949. * - ESP_OK
  950. * - ESP_FAIL
  951. */
  952. esp_err_t esp_mesh_set_vote_percentage(float percentage);
  953. /**
  954. * @brief Get vote percentage threshold for approval of being a root
  955. *
  956. * @return percentage threshold
  957. */
  958. float esp_mesh_get_vote_percentage(void);
  959. /**
  960. * @brief Set mesh softAP associate expired time (default:10 seconds)
  961. * - If mesh softAP hasn't received any data from an associated child within this time,
  962. * mesh softAP will take this child inactive and disassociate it.
  963. * - If mesh softAP is encrypted, this value should be set a greater value, such as 30 seconds.
  964. *
  965. * @param[in] seconds the expired time
  966. *
  967. * @return
  968. * - ESP_OK
  969. * - ESP_FAIL
  970. */
  971. esp_err_t esp_mesh_set_ap_assoc_expire(int seconds);
  972. /**
  973. * @brief Get mesh softAP associate expired time
  974. *
  975. * @return seconds
  976. */
  977. int esp_mesh_get_ap_assoc_expire(void);
  978. /**
  979. * @brief Get total number of devices in current network (including the root)
  980. *
  981. * @attention The returned value might be incorrect when the network is changing.
  982. **
  983. * @return total number of devices (including the root)
  984. */
  985. int esp_mesh_get_total_node_num(void);
  986. /**
  987. * @brief Get the number of devices in this device's sub-network (including self)
  988. *
  989. * @return the number of devices over this device's sub-network (including self)
  990. */
  991. int esp_mesh_get_routing_table_size(void);
  992. /**
  993. * @brief Get routing table of this device's sub-network (including itself)
  994. *
  995. * @param[out] mac pointer to routing table
  996. * @param[in] len routing table size(in bytes)
  997. * @param[out] size pointer to the number of devices in routing table (including itself)
  998. *
  999. * @return
  1000. * - ESP_OK
  1001. * - ESP_ERR_MESH_ARGUMENT
  1002. */
  1003. esp_err_t esp_mesh_get_routing_table(mesh_addr_t *mac, int len, int *size);
  1004. /**
  1005. * @brief Post the toDS state to the mesh stack
  1006. *
  1007. * @attention This API is only for the root.
  1008. *
  1009. * @param[in] reachable this state represents whether the root is able to access external IP network
  1010. *
  1011. * @return
  1012. * - ESP_OK
  1013. * - ESP_FAIL
  1014. */
  1015. esp_err_t esp_mesh_post_toDS_state(bool reachable);
  1016. /**
  1017. * @brief Return the number of packets pending in the queue waiting to be sent by the mesh stack
  1018. *
  1019. * @param[out] pending pointer to the TX pending
  1020. *
  1021. * @return
  1022. * - ESP_OK
  1023. * - ESP_FAIL
  1024. */
  1025. esp_err_t esp_mesh_get_tx_pending(mesh_tx_pending_t *pending);
  1026. /**
  1027. * @brief Return the number of packets available in the queue waiting to be received by applications
  1028. *
  1029. * @param[out] pending pointer to the RX pending
  1030. *
  1031. * @return
  1032. * - ESP_OK
  1033. * - ESP_FAIL
  1034. */
  1035. esp_err_t esp_mesh_get_rx_pending(mesh_rx_pending_t *pending);
  1036. /**
  1037. * @brief Return the number of packets could be accepted from the specified address
  1038. *
  1039. * @param[in] addr self address or an associate children address
  1040. * @param[out] xseqno_in sequence number of the last received packet from the specified address
  1041. *
  1042. * @return the number of upQ for a certain address
  1043. */
  1044. int esp_mesh_available_txupQ_num(const mesh_addr_t *addr, uint32_t *xseqno_in);
  1045. /**
  1046. * @brief Set the number of queue
  1047. *
  1048. * @attention This API shall be called before mesh is started.
  1049. *
  1050. * @param[in] qsize default:32 (min:16)
  1051. *
  1052. * @return
  1053. * - ESP_OK
  1054. * - ESP_FAIL
  1055. */
  1056. esp_err_t esp_mesh_set_xon_qsize(int qsize);
  1057. /**
  1058. * @brief Get queue size
  1059. *
  1060. * @return the number of queue
  1061. */
  1062. int esp_mesh_get_xon_qsize(void);
  1063. /**
  1064. * @brief Set whether allow more than one root existing in one network
  1065. *
  1066. * @param[in] allowed allow or not
  1067. *
  1068. * @return
  1069. * - ESP_OK
  1070. * - ESP_WIFI_ERR_NOT_INIT
  1071. * - ESP_WIFI_ERR_NOT_START
  1072. */
  1073. esp_err_t esp_mesh_allow_root_conflicts(bool allowed);
  1074. /**
  1075. * @brief Check whether allow more than one root to exist in one network
  1076. *
  1077. * @return true/false
  1078. */
  1079. bool esp_mesh_is_root_conflicts_allowed(void);
  1080. /**
  1081. * @brief Set group ID addresses
  1082. *
  1083. * @param[in] addr pointer to new group ID addresses
  1084. * @param[in] num the number of group ID addresses
  1085. *
  1086. * @return
  1087. * - ESP_OK
  1088. * - ESP_MESH_ERR_ARGUMENT
  1089. */
  1090. esp_err_t esp_mesh_set_group_id(const mesh_addr_t *addr, int num);
  1091. /**
  1092. * @brief Delete group ID addresses
  1093. *
  1094. * @param[in] addr pointer to deleted group ID address
  1095. * @param[in] num the number of group ID addresses
  1096. *
  1097. * @return
  1098. * - ESP_OK
  1099. * - ESP_MESH_ERR_ARGUMENT
  1100. */
  1101. esp_err_t esp_mesh_delete_group_id(const mesh_addr_t *addr, int num);
  1102. /**
  1103. * @brief Get the number of group ID addresses
  1104. *
  1105. * @return the number of group ID addresses
  1106. */
  1107. int esp_mesh_get_group_num(void);
  1108. /**
  1109. * @brief Get group ID addresses
  1110. *
  1111. * @param[out] addr pointer to group ID addresses
  1112. * @param[in] num the number of group ID addresses
  1113. *
  1114. * @return
  1115. * - ESP_OK
  1116. * - ESP_MESH_ERR_ARGUMENT
  1117. */
  1118. esp_err_t esp_mesh_get_group_list(mesh_addr_t *addr, int num);
  1119. /**
  1120. * @brief Check whether the specified group address is my group
  1121. *
  1122. * @return true/false
  1123. */
  1124. bool esp_mesh_is_my_group(const mesh_addr_t *addr);
  1125. /**
  1126. * @brief Set mesh network capacity (max:1000, default:300)
  1127. *
  1128. * @attention This API shall be called before mesh is started.
  1129. *
  1130. * @param[in] num mesh network capacity
  1131. *
  1132. * @return
  1133. * - ESP_OK
  1134. * - ESP_ERR_MESH_NOT_ALLOWED
  1135. * - ESP_MESH_ERR_ARGUMENT
  1136. */
  1137. esp_err_t esp_mesh_set_capacity_num(int num);
  1138. /**
  1139. * @brief Get mesh network capacity
  1140. *
  1141. * @return mesh network capacity
  1142. */
  1143. int esp_mesh_get_capacity_num(void);
  1144. /**
  1145. * @brief Set mesh IE crypto functions
  1146. *
  1147. * @attention This API can be called at any time after mesh is initialized.
  1148. *
  1149. * @param[in] crypto_funcs crypto functions for mesh IE
  1150. * - If crypto_funcs is set to NULL, mesh IE is no longer encrypted.
  1151. * @return
  1152. * - ESP_OK
  1153. */
  1154. esp_err_t esp_mesh_set_ie_crypto_funcs(const mesh_crypto_funcs_t *crypto_funcs);
  1155. /**
  1156. * @brief Set mesh IE crypto key
  1157. *
  1158. * @attention This API can be called at any time after mesh is initialized.
  1159. *
  1160. * @param[in] key ASCII crypto key
  1161. * @param[in] len length in bytes, range:8~64
  1162. *
  1163. * @return
  1164. * - ESP_OK
  1165. * - ESP_MESH_ERR_ARGUMENT
  1166. */
  1167. esp_err_t esp_mesh_set_ie_crypto_key(const char *key, int len);
  1168. /**
  1169. * @brief Get mesh IE crypto key
  1170. *
  1171. * @param[out] key ASCII crypto key
  1172. * @param[in] len length in bytes, range:8~64
  1173. *
  1174. * @return
  1175. * - ESP_OK
  1176. * - ESP_MESH_ERR_ARGUMENT
  1177. */
  1178. esp_err_t esp_mesh_get_ie_crypto_key(char *key, int len);
  1179. /**
  1180. * @brief Set delay time before starting root healing
  1181. *
  1182. * @param[in] delay_ms delay time in milliseconds
  1183. *
  1184. * @return
  1185. * - ESP_OK
  1186. */
  1187. esp_err_t esp_mesh_set_root_healing_delay(int delay_ms);
  1188. /**
  1189. * @brief Get delay time before network starts root healing
  1190. *
  1191. * @return delay time in milliseconds
  1192. */
  1193. int esp_mesh_get_root_healing_delay(void);
  1194. /**
  1195. * @brief Enable network Fixed Root Setting
  1196. * - Enabling fixed root disables automatic election of the root node via voting.
  1197. * - All devices in the network shall use the same Fixed Root Setting (enabled or disabled).
  1198. * - If Fixed Root is enabled, users should make sure a root node is designated for the network.
  1199. *
  1200. * @param[in] enable enable or not
  1201. *
  1202. * @return
  1203. * - ESP_OK
  1204. */
  1205. esp_err_t esp_mesh_fix_root(bool enable);
  1206. /**
  1207. * @brief Check whether network Fixed Root Setting is enabled
  1208. * - Enable/disable network Fixed Root Setting by API esp_mesh_fix_root().
  1209. * - Network Fixed Root Setting also changes with the "flag" value in parent networking IE.
  1210. *
  1211. * @return true/false
  1212. */
  1213. bool esp_mesh_is_root_fixed(void);
  1214. /**
  1215. * @brief Set a specified parent for the device
  1216. *
  1217. * @attention This API can be called at any time after mesh is configured.
  1218. *
  1219. * @param[in] parent parent configuration, the SSID and the channel of the parent are mandatory.
  1220. * - If the BSSID is set, make sure that the SSID and BSSID represent the same parent,
  1221. * otherwise the device will never find this specified parent.
  1222. * @param[in] parent_mesh_id parent mesh ID,
  1223. * - If this value is not set, the original mesh ID is used.
  1224. * @param[in] my_type mesh type
  1225. * - If the parent set for the device is the same as the router in the network configuration,
  1226. * then my_type shall set MESH_ROOT and my_layer shall set MESH_ROOT_LAYER.
  1227. * @param[in] my_layer mesh layer
  1228. * - my_layer of the device may change after joining the network.
  1229. * - If my_type is set MESH_NODE, my_layer shall be greater than MESH_ROOT_LAYER.
  1230. * - If my_type is set MESH_LEAF, the device becomes a standalone Wi-Fi station and no longer
  1231. * has the ability to extend the network.
  1232. *
  1233. * @return
  1234. * - ESP_OK
  1235. * - ESP_ERR_ARGUMENT
  1236. * - ESP_ERR_MESH_NOT_CONFIG
  1237. */
  1238. 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);
  1239. /**
  1240. * @brief Get mesh networking IE length of one AP
  1241. *
  1242. * @param[out] len mesh networking IE length
  1243. *
  1244. * @return
  1245. * - ESP_OK
  1246. * - ESP_ERR_WIFI_NOT_INIT
  1247. * - ESP_ERR_WIFI_ARG
  1248. * - ESP_ERR_WIFI_FAIL
  1249. */
  1250. esp_err_t esp_mesh_scan_get_ap_ie_len(int *len);
  1251. /**
  1252. * @brief Get AP record
  1253. *
  1254. * @attention Different from esp_wifi_scan_get_ap_records(), this API only gets one of APs scanned each time.
  1255. * See "manual_networking" example.
  1256. *
  1257. * @param[out] ap_record pointer to one AP record
  1258. * @param[out] buffer pointer to the mesh networking IE of this AP
  1259. *
  1260. * @return
  1261. * - ESP_OK
  1262. * - ESP_ERR_WIFI_NOT_INIT
  1263. * - ESP_ERR_WIFI_ARG
  1264. * - ESP_ERR_WIFI_FAIL
  1265. */
  1266. esp_err_t esp_mesh_scan_get_ap_record(wifi_ap_record_t *ap_record, void *buffer);
  1267. /**
  1268. * @brief Flush upstream packets pending in to_parent queue and to_parent_p2p queue
  1269. *
  1270. * @return
  1271. * - ESP_OK
  1272. */
  1273. esp_err_t esp_mesh_flush_upstream_packets(void);
  1274. /**
  1275. * @brief Get the number of nodes in the subnet of a specific child
  1276. *
  1277. * @param[in] child_mac an associated child address of this device
  1278. * @param[out] nodes_num pointer to the number of nodes in the subnet of a specific child
  1279. *
  1280. * @return
  1281. * - ESP_OK
  1282. * - ESP_ERR_MESH_NOT_START
  1283. * - ESP_ERR_MESH_ARGUMENT
  1284. */
  1285. esp_err_t esp_mesh_get_subnet_nodes_num(const mesh_addr_t *child_mac, int *nodes_num);
  1286. /**
  1287. * @brief Get nodes in the subnet of a specific child
  1288. *
  1289. * @param[in] child_mac an associated child address of this device
  1290. * @param[out] nodes pointer to nodes in the subnet of a specific child
  1291. * @param[in] nodes_num 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_list(const mesh_addr_t *child_mac, mesh_addr_t *nodes, int nodes_num);
  1299. /**
  1300. * @brief Disconnect from current parent
  1301. *
  1302. * @return
  1303. * - ESP_OK
  1304. */
  1305. esp_err_t esp_mesh_disconnect(void);
  1306. /**
  1307. * @brief Connect to current parent
  1308. *
  1309. * @return
  1310. * - ESP_OK
  1311. */
  1312. esp_err_t esp_mesh_connect(void);
  1313. /**
  1314. * @brief Flush scan result
  1315. *
  1316. * @return
  1317. * - ESP_OK
  1318. */
  1319. esp_err_t esp_mesh_flush_scan_result(void);
  1320. /**
  1321. * @brief Cause the root device to add Channel Switch Announcement Element (CSA IE) to beacon
  1322. * - Set the new channel
  1323. * - Set how many beacons with CSA IE will be sent before changing a new channel
  1324. * - Enable the channel switch function
  1325. *
  1326. * @attention This API is only called by the root.
  1327. *
  1328. * @param[in] new_bssid the new router BSSID if the router changes
  1329. * @param[in] csa_newchan the new channel number to which the whole network is moving
  1330. * @param[in] csa_count channel switch period(beacon count), unit is based on beacon interval of its softAP, the default value is 15.
  1331. *
  1332. * @return
  1333. * - ESP_OK
  1334. */
  1335. esp_err_t esp_mesh_switch_channel(const uint8_t *new_bssid, int csa_newchan, int csa_count);
  1336. /**
  1337. * @brief Get the router BSSID
  1338. *
  1339. * @param[out] router_bssid pointer to the router BSSID
  1340. *
  1341. * @return
  1342. * - ESP_OK
  1343. * - ESP_ERR_WIFI_NOT_INIT
  1344. * - ESP_ERR_WIFI_ARG
  1345. */
  1346. esp_err_t esp_mesh_get_router_bssid(uint8_t *router_bssid);
  1347. /**
  1348. * @brief Get the TSF time
  1349. *
  1350. * @return the TSF time
  1351. */
  1352. int64_t esp_mesh_get_tsf_time(void);
  1353. #ifdef __cplusplus
  1354. }
  1355. #endif
  1356. #endif /* __ESP_MESH_H__ */