esp_mesh.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. * | | WiFi Driver | |
  28. * | |--------------------------------------------------| |
  29. * | | |
  30. * | | Platform HAL |
  31. * |------------------------------------------------------------------------------|
  32. *
  33. * System Events delivery:
  34. *
  35. * |---------------|
  36. * | | default handler
  37. * | WiFi 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 WiFi 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 WiFi station connect states to decide when to send data to its parent, to root or to external IP network;
  65. * (2)Applications could use WiFi 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 root is able to access external IP network.
  79. * In application mesh event handler, once a device becomes a root, start DHCP client immediately if 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) /**< WiFi 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 WiFi 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 used with 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 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) /**< root is fixed */
  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_PARNET_FOUND, /**< no parent found */
  160. MESH_EVENT_LAYER_CHANGE, /**< layer changes over the mesh network */
  161. MESH_EVENT_TODS_STATE, /**< state represents if 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 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, /**< root obtains the IP address. It is posted by LwIP stack automatically */
  168. MESH_EVENT_ROOT_LOST_IP, /**< root loses the IP address. It is posted by LwIP stack automatically */
  169. MESH_EVENT_ROOT_ASKED_YIELD, /**< 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 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_MAX,
  179. } mesh_event_id_t;
  180. /**
  181. * @brief device type
  182. */
  183. typedef enum {
  184. MESH_IDLE, /**< hasn't joined the mesh network yet */
  185. MESH_ROOT, /**< the only sink of the mesh network. Has the ability to access external IP network */
  186. MESH_NODE, /**< intermediate device. Has the ability to forward packets over the mesh network */
  187. MESH_LEAF, /**< has no forwarding ability */
  188. } mesh_type_t;
  189. /**
  190. * @brief protocol of transmitted application data
  191. */
  192. typedef enum {
  193. MESH_PROTO_BIN, /**< binary */
  194. MESH_PROTO_HTTP, /**< HTTP protocol */
  195. MESH_PROTO_JSON, /**< JSON format */
  196. MESH_PROTO_MQTT, /**< MQTT protocol */
  197. } mesh_proto_t;
  198. /**
  199. * @brief for reliable transmission, mesh stack provides three type of services
  200. */
  201. typedef enum {
  202. MESH_TOS_P2P, /**< provide P2P(point-to-point) retransmission on mesh stack by default */
  203. MESH_TOS_E2E, /**< provide E2E(end-to-end) retransmission on mesh stack (Unimplemented) */
  204. MESH_TOS_DEF, /**< no retransmission on mesh stack */
  205. } mesh_tos_t;
  206. /**
  207. * @brief vote reason
  208. */
  209. typedef enum {
  210. MESH_VOTE_REASON_ROOT_INITIATED = 1, /**< vote is initiated by root */
  211. MESH_VOTE_REASON_CHILD_INITIATED, /**< vote is initiated by children */
  212. } mesh_vote_reason_t;
  213. /**
  214. * @brief mesh disconnect reason code
  215. */
  216. typedef enum {
  217. MESH_REASON_CYCLIC = 100, /**< cyclic is detected */
  218. MESH_REASON_PARENT_IDLE, /**< parent is idle */
  219. MESH_REASON_LEAF, /**< the connected device is changed to a leaf */
  220. MESH_REASON_DIFF_ID, /**< in different mesh ID */
  221. MESH_REASON_ROOTS, /**< root conflict is detected */
  222. MESH_REASON_PARENT_STOPPED, /**< parent has stopped the mesh */
  223. MESH_REASON_SCAN_FAIL, /**< scan fail */
  224. } mesh_disconnect_reason_t;
  225. /*******************************************************
  226. * Structures
  227. *******************************************************/
  228. /**
  229. * @brief IP address and port
  230. */
  231. typedef struct {
  232. ip4_addr_t ip4; /**< IP address */
  233. uint16_t port; /**< port */
  234. } __attribute__((packed)) mip_t;
  235. /**
  236. * @brief mesh address
  237. */
  238. typedef union {
  239. uint8_t addr[6]; /**< mac address */
  240. mip_t mip; /**< mip address */
  241. } mesh_addr_t;
  242. /**
  243. * @brief channel switch information
  244. */
  245. typedef struct {
  246. uint8_t channel; /**< new channel */
  247. } mesh_event_channel_switch_t;
  248. /**
  249. * @brief parent connected information
  250. */
  251. typedef struct {
  252. system_event_sta_connected_t connected; /**< parent information, same as WiFi event SYSTEM_EVENT_STA_CONNECTED does */
  253. uint8_t self_layer; /**< layer */
  254. } mesh_event_connected_t;
  255. /**
  256. * @brief no parent found information
  257. */
  258. typedef struct {
  259. int scan_times; /**< scan times being through */
  260. } mesh_event_no_parent_found_t;
  261. /**
  262. * @brief layer change information
  263. */
  264. typedef struct {
  265. uint8_t new_layer; /**< new layer */
  266. } mesh_event_layer_change_t;
  267. /**
  268. * @brief the reachability of root to a DS(distribute system)
  269. */
  270. typedef enum {
  271. MESH_TODS_UNREACHABLE, /**< root isn't able to access external IP network */
  272. MESH_TODS_REACHABLE, /**< root is able to access external IP network */
  273. } mesh_event_toDS_state_t;
  274. /**
  275. * @brief vote started information
  276. */
  277. typedef struct {
  278. int reason; /**< vote reason, vote could be initiated by children or by root itself */
  279. int attempts; /**< max vote attempts before stopped */
  280. mesh_addr_t rc_addr; /**< root address specified by users via API esp_mesh_waive_root() */
  281. } mesh_event_vote_started_t;
  282. /**
  283. * @brief IP settings from LwIP stack
  284. */
  285. typedef system_event_sta_got_ip_t mesh_event_root_got_ip_t;
  286. /**
  287. * @brief root address
  288. */
  289. typedef mesh_addr_t mesh_event_root_address_t;
  290. /**
  291. * @brief parent disconnected information
  292. */
  293. typedef system_event_sta_disconnected_t mesh_event_disconnected_t;
  294. /**
  295. * @brief child connected information
  296. */
  297. typedef system_event_ap_staconnected_t mesh_event_child_connected_t;
  298. /**
  299. * @brief child disconnected information
  300. */
  301. typedef system_event_ap_stadisconnected_t mesh_event_child_disconnected_t;
  302. /**
  303. * @brief root switch request information
  304. */
  305. typedef struct {
  306. int reason; /**< root switch reason, generally root switch is initialized by users via API esp_mesh_waive_root() */
  307. mesh_addr_t rc_addr; /**< the address of root switch requester */
  308. } mesh_event_root_switch_req_t;
  309. /**
  310. * @brief other powerful root address
  311. */
  312. typedef struct {
  313. int8_t rssi; /**< rssi with router */
  314. uint16_t capacity; /**< the number of devices in current network */
  315. uint8_t addr[6]; /**< other powerful root address */
  316. } mesh_event_root_conflict_t;
  317. /**
  318. * @brief routing table change
  319. */
  320. typedef struct {
  321. uint16_t rt_size_new; /**< the new value */
  322. uint16_t rt_size_change; /**< the changed value */
  323. } mesh_event_routing_table_change_t;
  324. /**
  325. * @brief root fixed
  326. */
  327. typedef struct {
  328. bool is_fixed; /**< status */
  329. } mesh_event_root_fixed_t;
  330. /**
  331. * @brief scan done event information
  332. */
  333. typedef struct {
  334. uint8_t number; /**< the number of scanned APs */
  335. } mesh_event_scan_done_t;
  336. /**
  337. * @brief mesh event information
  338. */
  339. typedef union {
  340. mesh_event_channel_switch_t channel_switch; /**< channel switch */
  341. mesh_event_child_connected_t child_connected; /**< child connected */
  342. mesh_event_child_disconnected_t child_disconnected; /**< child disconnected */
  343. mesh_event_routing_table_change_t routing_table; /**< routing table change */
  344. mesh_event_connected_t connected; /**< parent connected */
  345. mesh_event_disconnected_t disconnected; /**< parent disconnected */
  346. mesh_event_no_parent_found_t no_parent; /**< no parent found */
  347. mesh_event_layer_change_t layer_change; /**< layer change */
  348. mesh_event_toDS_state_t toDS_state; /**< toDS state, devices shall check this state firstly before trying to send packets to
  349. external IP network. This state indicates right now if root is capable of sending
  350. packets out. If not, devices had better to wait until this state changes to be
  351. MESH_TODS_REACHABLE. */
  352. mesh_event_vote_started_t vote_started; /**< vote started */
  353. mesh_event_root_got_ip_t got_ip; /**< root obtains IP address */
  354. mesh_event_root_address_t root_addr; /**< root address */
  355. mesh_event_root_switch_req_t switch_req; /**< root switch request */
  356. mesh_event_root_conflict_t root_conflict; /**< other powerful root */
  357. mesh_event_root_fixed_t root_fixed; /**< root fixed */
  358. mesh_event_scan_done_t scan_done; /**< scan done */
  359. } mesh_event_info_t;
  360. /**
  361. * @brief mesh event
  362. */
  363. typedef struct {
  364. mesh_event_id_t id; /**< mesh event id */
  365. mesh_event_info_t info; /**< mesh event info */
  366. } mesh_event_t;
  367. /**
  368. * @brief mesh event callback handler prototype definition
  369. *
  370. * @param event mesh_event_t
  371. */
  372. typedef void (*mesh_event_cb_t)(mesh_event_t event);
  373. /**
  374. * @brief mesh option
  375. */
  376. typedef struct {
  377. uint8_t type; /**< option type */
  378. uint16_t len; /**< option length */
  379. uint8_t *val; /**< option value */
  380. } __attribute__((packed)) mesh_opt_t;
  381. /**
  382. * @brief mesh data for esp_mesh_send() and esp_mesh_recv()
  383. */
  384. typedef struct {
  385. uint8_t *data; /**< data */
  386. uint16_t size; /**< data size */
  387. mesh_proto_t proto; /**< data protocol */
  388. mesh_tos_t tos; /**< data type of service */
  389. } mesh_data_t;
  390. /**
  391. * @brief router configuration
  392. */
  393. typedef struct {
  394. uint8_t ssid[32]; /**< SSID */
  395. uint8_t ssid_len; /**< length of SSID */
  396. uint8_t bssid[6]; /**< BSSID, if router is hidden, this value is mandatory */
  397. uint8_t password[64]; /**< password */
  398. } mesh_router_t;
  399. /**
  400. * @brief mesh softAP configuration
  401. */
  402. typedef struct {
  403. uint8_t password[64]; /**< mesh softAP password */
  404. uint8_t max_connection; /**< max number of stations allowed to connect in, max 10 */
  405. } mesh_ap_cfg_t;
  406. /**
  407. * @brief mesh initialization configuration
  408. */
  409. typedef struct {
  410. uint8_t channel; /**< channel, the mesh network on */
  411. mesh_event_cb_t event_cb; /**< mesh event callback */
  412. mesh_addr_t mesh_id; /**< mesh network identification */
  413. mesh_router_t router; /**< router configuration */
  414. mesh_ap_cfg_t mesh_ap; /**< mesh softAP configuration */
  415. const mesh_crypto_funcs_t *crypto_funcs; /**< crypto functions */
  416. } mesh_cfg_t;
  417. /**
  418. * @brief vote address configuration
  419. */
  420. typedef union {
  421. int attempts; /**< max vote attempts before a new root is elected automatically by mesh network. (min:15, 15 by default) */
  422. mesh_addr_t rc_addr; /**< a new root address specified by users for API esp_mesh_waive_root() */
  423. } mesh_rc_config_t;
  424. /**
  425. * @brief vote
  426. */
  427. typedef struct {
  428. float percentage; /**< vote percentage threshold for approval of being a root */
  429. bool is_rc_specified; /**< if true, rc_addr shall be specified(Unimplemented).
  430. if false, attempts value shall be specified to make network start root election. */
  431. mesh_rc_config_t config; /**< vote address configuration */
  432. } mesh_vote_t;
  433. /**
  434. * @brief the number of packets pending in the queue waiting to be sent by the mesh stack
  435. */
  436. typedef struct {
  437. int to_parent; /**< to parent queue */
  438. int to_parent_p2p; /**< to parent(P2P) queue */
  439. int to_child; /**< to child queue */
  440. int to_child_p2p; /**< to child(P2P) queue */
  441. int mgmt; /**< management queue */
  442. int broadcast; /**< broadcast and multicast queue */
  443. } mesh_tx_pending_t;
  444. /**
  445. * @brief the number of packets available in the queue waiting to be received by applications
  446. */
  447. typedef struct {
  448. int toDS; /**< to external DS */
  449. int toSelf; /**< to self */
  450. } mesh_rx_pending_t;
  451. /*******************************************************
  452. * Variable Declaration
  453. *******************************************************/
  454. /* mesh vendor IE crypto callback function */
  455. extern const mesh_crypto_funcs_t g_wifi_default_mesh_crypto_funcs;
  456. /* mesh event callback handler */
  457. extern mesh_event_cb_t g_mesh_event_cb;
  458. #define MESH_INIT_CONFIG_DEFAULT() { \
  459. .crypto_funcs = &g_wifi_default_mesh_crypto_funcs, \
  460. }
  461. /*******************************************************
  462. * Function Definitions
  463. *******************************************************/
  464. /**
  465. * @brief mesh initialization
  466. * Check if WiFi is started.
  467. * Initialize mesh global variables with default values.
  468. *
  469. * @attention This API shall be called after WiFi is started.
  470. *
  471. * @return
  472. * - ESP_OK
  473. * - ESP_FAIL
  474. */
  475. esp_err_t esp_mesh_init(void);
  476. /**
  477. * @brief mesh de-initialization
  478. * Release resources and stop the mesh
  479. *
  480. * @return
  481. * - ESP_OK
  482. * - ESP_FAIL
  483. */
  484. esp_err_t esp_mesh_deinit(void);
  485. /**
  486. * @brief start mesh
  487. * Initialize mesh vendor IE
  488. * Start mesh network management service
  489. * Create TX and RX queues according to the configuration
  490. * Register mesh packets receive callback
  491. *
  492. * @attention This API shall be called after esp_mesh_init() and esp_mesh_set_config().
  493. *
  494. * @return
  495. * - ESP_OK
  496. * - ESP_FAIL
  497. * - ESP_ERR_MESH_NOT_INIT
  498. * - ESP_ERR_MESH_NOT_CONFIG
  499. * - ESP_ERR_MESH_NO_MEMORY
  500. */
  501. esp_err_t esp_mesh_start(void);
  502. /**
  503. * @brief stop mesh
  504. * Deinitialize mesh vendor IE
  505. * Disconnect with current parent
  506. * Disassociate all currently associated children
  507. * Stop mesh network management service
  508. * Unregister mesh packets receive callback
  509. * Delete TX and RX queues
  510. * Release resources
  511. * Restore WiFi softAP to default settings if WiFi dual mode is enabled
  512. *
  513. * @return
  514. * - ESP_OK
  515. * - ESP_FAIL
  516. */
  517. esp_err_t esp_mesh_stop(void);
  518. /**
  519. * @brief send a packet over the mesh network
  520. * Send a packet to any device in the mesh network.
  521. * Send a packet to external IP network.
  522. *
  523. * @attention This API is not reentrant.
  524. *
  525. * @param to the address of the final destination of the packet
  526. * (1)if the packet is to root, just set "to" to NULL and set flag to zero.
  527. * (2)if the packet is outgoing to external IP network such as an IP server address, translate IPv4:PORT known as "to".
  528. * This packet will be delivered to root firstly, then root will forward this packet to the final IP server address.
  529. * @param data pointer to a sending mesh packet
  530. * Should specify the data protocol applications used, binary by default.
  531. * Should specify the transmission tos(type of service), P2P reliable by default.
  532. * @param flag
  533. * (1)used to speed up the route selection
  534. * if the packet is target to an internal device, MESH_DATA_P2P should be set.
  535. * if the packet is outgoing to root or to external IP network, MESH_DATA_TODS should be set.
  536. * if the packet is from root to an internal device, MESH_DATA_FROMDS should be set.
  537. * (2)specify if this API is block or non-block, block by default
  538. * if needs non-block, MESH_DATA_NONBLOCK should be set.
  539. * (3)in the situation of root having been changed, MESH_DATA_DROP identifies this packet can be dropped by new root
  540. * for upstream data to external IP network, we try our best to avoid data loss caused by root having been changed, but
  541. * there is a risk that new root is running out of memory because most of memory is occupied by the pending data which
  542. * isn't read out in time by esp_mesh_recv_toDS().
  543. * Generally, we suggest esp_mesh_recv_toDS() is called after a connection with IP network is created. Thus data outgoing
  544. * to external IP network via socket is just from reading esp_mesh_recv_toDS() which avoids unnecessary memory copy.
  545. *
  546. * @param opt options
  547. * (1)in case of sending a packet to a specified group, MESH_OPT_SEND_GROUP is a good choice.
  548. * In this option, the value field should specify the target receiver addresses in this group.
  549. * (2)root sends a packet to an internal device, this packet is from external IP network in case the receiver device responds
  550. * this packet, MESH_OPT_RECV_DS_ADDR is required to attach the target DS address.
  551. * @param opt_count option count
  552. * Currently, this API only takes one option, so opt_count is only supported to be 1.
  553. *
  554. * @return
  555. * - ESP_OK
  556. * - ESP_FAIL
  557. * - ESP_ERR_MESH_ARGUMENT
  558. * - ESP_ERR_MESH_NOT_START
  559. * - ESP_ERR_MESH_DISCONNECTED
  560. * - ESP_ERR_MESH_OPT_UNKNOWN
  561. * - ESP_ERR_MESH_EXCEED_MTU
  562. * - ESP_ERR_MESH_NO_MEMORY
  563. * - ESP_ERR_MESH_TIMEOUT
  564. * - ESP_ERR_MESH_QUEUE_FULL
  565. * - ESP_ERR_MESH_NO_ROUTE_FOUND
  566. * - ESP_ERR_MESH_DISCARD
  567. */
  568. esp_err_t esp_mesh_send(const mesh_addr_t *to, const mesh_data_t *data,
  569. int flag, const mesh_opt_t opt[], int opt_count);
  570. /**
  571. * @brief receive a packet targeted to self over the mesh network
  572. * Use esp_mesh_get_rx_pending() to check the number of packets available in the queue waiting
  573. * to be received by applications in case of running out of memory.
  574. *
  575. * @param from the address of the original source of the packet
  576. * @param data pointer to the received mesh packet
  577. * Contain the protocol and applications should follow it to parse the data.
  578. * @param timeout_ms wait time if a packet isn't immediately available(0:no wait, portMAX_DELAY:wait forever)
  579. * @param flag
  580. * MESH_DATA_FROMDS represents data from external IP network
  581. * MESH_DATA_TODS represents data directed upward within the mesh network
  582. * @param opt options desired to receive
  583. * MESH_OPT_RECV_DS_ADDR attaches the DS address
  584. * @param opt_count option count desired to receive
  585. * Currently, this API only takes one option, so opt_count is only supported to be 1.
  586. *
  587. * @return
  588. * - ESP_OK
  589. * - ESP_ERR_MESH_ARGUMENT
  590. * - ESP_ERR_MESH_NOT_START
  591. * - ESP_ERR_MESH_TIMEOUT
  592. * - ESP_ERR_MESH_DISCARD
  593. */
  594. esp_err_t esp_mesh_recv(mesh_addr_t *from, mesh_data_t *data, int timeout_ms,
  595. int *flag, mesh_opt_t opt[], int opt_count);
  596. /**
  597. * @brief receive a packet targeted to external IP network
  598. * root uses this API to receive packets destined to external IP network
  599. * root forwards the received packets to the final destination via socket.
  600. * if no socket connection is ready to send out the received packets and this esp_mesh_recv_toDS()
  601. * hasn't been called by applications, packets from the whole mesh network will be pending in toDS queue.
  602. * Use esp_mesh_get_rx_pending() to check the number of packets available in the queue waiting
  603. * to be received by applications in case of running out of memory in root.
  604. * Use esp_mesh_set_xon_qsize() could configure the RX queue size, default:72. If this size is too large,
  605. * and esp_mesh_recv_toDS() isn't called in time, there is a risk that a great deal of memory is occupied
  606. * by the pending packets. If this size is too small, it will impact the efficiency on upstream. How to
  607. * decide this value depends on the specific application scenarios.
  608. *
  609. * @attention This API is only called by root.
  610. *
  611. * @param from the address of the original source of the packet
  612. * @param to the address contains remote IP address and port(IPv4:PORT)
  613. * @param data pointer to the received packet
  614. * Contain the protocol and applications should follow it to parse the data.
  615. * @param timeout_ms wait time if a packet isn't immediately available(0:no wait, portMAX_DELAY:wait forever)
  616. * @param flag
  617. * MESH_DATA_TODS represents data to external IP network
  618. * @param opt options desired to receive
  619. * @param opt_count option count desired to receive
  620. *
  621. * @return
  622. * - ESP_OK
  623. * - ESP_ERR_MESH_ARGUMENT
  624. * - ESP_ERR_MESH_NOT_START
  625. * - ESP_ERR_MESH_TIMEOUT
  626. * - ESP_ERR_MESH_DISCARD
  627. */
  628. esp_err_t esp_mesh_recv_toDS(mesh_addr_t *from, mesh_addr_t *to,
  629. mesh_data_t *data, int timeout_ms, int *flag, mesh_opt_t opt[],
  630. int opt_count);
  631. /**
  632. * @brief set mesh stack configuration
  633. * Use MESH_INIT_CONFIG_DEFAULT() to initialize the default values, mesh vendor IE is encrypted by default.
  634. * mesh network is established on a fixed channel(1-14).
  635. * mesh event callback is mandatory.
  636. * mesh ID is an identifier of an MBSS. Nodes with the same mesh ID can communicate with each other.
  637. * Regarding to the router configuration, if the router is hidden, BSSID field is mandatory.
  638. * If BSSID field isn't set and there exists more than one router with same SSID, there is a risk that more
  639. * roots than one connected with different BSSID will appear. It means more than one mesh network is established
  640. * with the same mesh ID.
  641. * Root conflict function could eliminate redundant roots connected with the same BSSID, but couldn't handle roots
  642. * connected with different BSSID. Because users might have such requirements of setting up routers with same SSID
  643. * for the future replacement. But in that case, if the above situations happen, please make sure applications
  644. * implement forward functions on root to guarantee devices in different mesh network can communicate with each other.
  645. * max_connection of mesh softAP is limited by the max number of WiFi softAP supported(max:10).
  646. *
  647. * @attention This API shall be called between esp_mesh_init() and esp_mesh_start().
  648. *
  649. * @param config pointer to mesh stack configuration
  650. *
  651. * @return
  652. * - ESP_OK
  653. * - ESP_ERR_MESH_ARGUMENT
  654. * - ESP_ERR_MESH_NOT_ALLOWED
  655. */
  656. esp_err_t esp_mesh_set_config(const mesh_cfg_t *config);
  657. /**
  658. * @brief get mesh stack configuration
  659. *
  660. * @param config pointer to mesh stack configuration
  661. *
  662. * @return
  663. * - ESP_OK
  664. * - ESP_ERR_MESH_ARGUMENT
  665. */
  666. esp_err_t esp_mesh_get_config(mesh_cfg_t *config);
  667. /**
  668. * @brief set router configuration
  669. *
  670. * @attention This API shall be called between esp_mesh_init() and esp_mesh_start().
  671. *
  672. * @param router pointer to router configuration
  673. *
  674. * @return
  675. * - ESP_OK
  676. * - ESP_ERR_MESH_ARGUMENT
  677. */
  678. esp_err_t esp_mesh_set_router(const mesh_router_t *router);
  679. /**
  680. * @brief get router configuration
  681. *
  682. * @param router pointer to router configuration
  683. *
  684. * @return
  685. * - ESP_OK
  686. * - ESP_ERR_MESH_ARGUMENT
  687. */
  688. esp_err_t esp_mesh_get_router(mesh_router_t *router);
  689. /**
  690. * @brief set mesh network ID
  691. *
  692. * @attention This API could be called either before esp_mesh_start() or after esp_mesh_start().
  693. *
  694. * @param id pointer to mesh network ID
  695. *
  696. * @return
  697. * - ESP_OK
  698. * - ESP_ERR_MESH_ARGUMENT: invalid argument
  699. */
  700. esp_err_t esp_mesh_set_id(const mesh_addr_t *id);
  701. /**
  702. * @brief get mesh network ID
  703. *
  704. * @param id pointer to mesh network ID
  705. *
  706. * @return
  707. * - ESP_OK
  708. * - ESP_ERR_MESH_ARGUMENT
  709. */
  710. esp_err_t esp_mesh_get_id(mesh_addr_t *id);
  711. /**
  712. * @brief set device type over the mesh network(Unimplemented)
  713. *
  714. * @param type device type
  715. *
  716. * @return
  717. * - ESP_OK
  718. * - ESP_ERR_MESH_NOT_ALLOWED
  719. */
  720. esp_err_t esp_mesh_set_type(mesh_type_t type);
  721. /**
  722. * @brief get device type over mesh network
  723. *
  724. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  725. *
  726. * @return mesh type
  727. *
  728. */
  729. mesh_type_t esp_mesh_get_type(void);
  730. /**
  731. * @brief set max layer configuration(max:15, default:15)
  732. *
  733. * @attention This API shall be called before esp_mesh_start().
  734. *
  735. * @param max_layer max layer value
  736. *
  737. * @return
  738. * - ESP_OK
  739. * - ESP_ERR_MESH_ARGUMENT
  740. * - ESP_ERR_MESH_NOT_ALLOWED
  741. */
  742. esp_err_t esp_mesh_set_max_layer(int max_layer);
  743. /**
  744. * @brief get max layer configuration
  745. *
  746. * @return max layer value
  747. */
  748. int esp_mesh_get_max_layer(void);
  749. /**
  750. * @brief set mesh softAP password
  751. *
  752. * @attention This API shall be called before esp_mesh_start().
  753. *
  754. * @param pwd pointer to the password
  755. * @param len password length
  756. *
  757. * @return
  758. * - ESP_OK
  759. * - ESP_ERR_MESH_ARGUMENT
  760. * - ESP_ERR_MESH_NOT_ALLOWED
  761. */
  762. esp_err_t esp_mesh_set_ap_password(const uint8_t *pwd, int len);
  763. /**
  764. * @brief set mesh softAP authentication mode value
  765. *
  766. * @attention This API shall be called before esp_mesh_start().
  767. *
  768. * @param authmode authentication mode
  769. *
  770. * @return
  771. * - ESP_OK
  772. * - ESP_ERR_MESH_ARGUMENT
  773. * - ESP_ERR_MESH_NOT_ALLOWED
  774. */
  775. esp_err_t esp_mesh_set_ap_authmode(wifi_auth_mode_t authmode);
  776. /**
  777. * @brief get mesh softAP authentication mode
  778. *
  779. * @return authentication mode
  780. *
  781. */
  782. wifi_auth_mode_t esp_mesh_get_ap_authmode(void);
  783. /**
  784. * @brief set mesh softAP max connection value
  785. *
  786. * @attention This API shall be called before esp_mesh_start().
  787. *
  788. * @param connections the number of max connections
  789. *
  790. * @return
  791. * - ESP_OK
  792. * - ESP_ERR_MESH_ARGUMENT
  793. */
  794. esp_err_t esp_mesh_set_ap_connections(int connections);
  795. /**
  796. * @brief get mesh softAP max connection configuration
  797. *
  798. * @return the number of max connections
  799. *
  800. */
  801. int esp_mesh_get_ap_connections(void);
  802. /**
  803. * @brief get current layer value over the mesh network
  804. *
  805. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  806. *
  807. * @return layer value
  808. *
  809. */
  810. int esp_mesh_get_layer(void);
  811. /**
  812. * @brief get parent BSSID
  813. *
  814. * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
  815. *
  816. * @param bssid pointer to parent BSSID
  817. *
  818. * @return
  819. * - ESP_OK
  820. * - ESP_FAIL
  821. */
  822. esp_err_t esp_mesh_get_parent_bssid(mesh_addr_t *bssid);
  823. /**
  824. * @brief return if the device is root
  825. *
  826. * @return true/false
  827. *
  828. */
  829. bool esp_mesh_is_root(void);
  830. /**
  831. * @brief enable/disable mesh networking self-organized, self-organized by default
  832. * if self-organized is disabled, users should set a parent for this device via
  833. * esp_mesh_set_parent();
  834. *
  835. * @attention This API could be called either before esp_mesh_start() or after esp_mesh_start().
  836. *
  837. * @param enable enable or disable self-organized networking
  838. * @param select_parent if enable self-organized networking, let the device select a new parent or
  839. * keep connecting to the previous parent.
  840. *
  841. * @return
  842. * - ESP_OK
  843. * - ESP_FAIL
  844. */
  845. esp_err_t esp_mesh_set_self_organized(bool enable, bool select_parent);
  846. /**
  847. * @brief return if mesh networking is self-organized or not
  848. *
  849. * @return true/false
  850. *
  851. */
  852. bool esp_mesh_get_self_organized(void);
  853. /**
  854. * @brief root waive itself
  855. * A device is elected to be a root during the networking mostly because it has a strong RSSI with router.
  856. * If such superior conditions change, users could call this API to perform a root switch.
  857. *
  858. * In this API, users could specify a desired root address to replace itself or specify an attempts value
  859. * to ask current root to initiate a new round of voting. During the voting, a better root candidate would
  860. * be expected to find to replace the current one.
  861. * If no desired root candidate, the vote will try a specified attempts(at least 10 times), if no better
  862. * root candidate is found, keep the current one. If a better candidate is found, the new better one will
  863. * send a root switch request to the current root, current root will respond with a root switch acknowledgement.
  864. * After that, the new candidate will connect to the router to be a new root, the previous root will disconnect
  865. * with the router and choose another parent instead.
  866. * So far, root switch is completed with minimal disruption to the whole mesh network.
  867. *
  868. * @attention This API is only called by root.
  869. *
  870. * @param vote vote configuration
  871. * Specify a desired root address(Unimplemented)
  872. * Attempts should be at least 10 times.
  873. * if "vote" is set NULL, the vote will perform the default 10 times.
  874. * @param reason only accept MESH_VOTE_REASON_ROOT_INITIATED for now
  875. *
  876. * @return
  877. * - ESP_OK
  878. * - ESP_FAIL
  879. */
  880. esp_err_t esp_mesh_waive_root(const mesh_vote_t *vote, int reason);
  881. /**
  882. * @brief set vote percentage threshold for approval of being a root
  883. * During the networking, only obtaining vote percentage reaches this threshold,
  884. * the device could be a root.
  885. *
  886. * @attention This API shall be called before esp_mesh_start().
  887. *
  888. * @param percentage vote percentage threshold
  889. *
  890. * @return
  891. * - ESP_OK
  892. * - ESP_FAIL
  893. */
  894. esp_err_t esp_mesh_set_vote_percentage(float percentage);
  895. /**
  896. * @brief get vote percentage threshold for approval of being a root
  897. *
  898. * @return percentage threshold
  899. */
  900. float esp_mesh_get_vote_percentage(void);
  901. /**
  902. * @brief set mesh softAP associate expired time
  903. * If mesh softAP hasn't received any data from an associated child within this time,
  904. * mesh softAP will take this child inactive and disassociate it.
  905. *
  906. * @param seconds
  907. *
  908. * @return
  909. * - ESP_OK
  910. * - ESP_FAIL
  911. */
  912. esp_err_t esp_mesh_set_ap_assoc_expire(int seconds);
  913. /**
  914. * @brief get mesh softAP associate expired time
  915. *
  916. * @return seconds
  917. */
  918. int esp_mesh_get_ap_assoc_expire(void);
  919. /**
  920. * @brief get total number of devices in current network(including root)
  921. *
  922. * @attention The returned value might be incorrect when the network is changing.
  923. **
  924. * @return total number of devices(including root)
  925. */
  926. int esp_mesh_get_total_node_num(void);
  927. /**
  928. * @brief get the number of devices in this device's sub-network(including self)
  929. *
  930. * @return the number of devices over this device's sub-network(including self)
  931. */
  932. int esp_mesh_get_routing_table_size(void);
  933. /**
  934. * @brief get routing table of this device's sub-network(including itself)
  935. *
  936. * @param mac pointer to routing table
  937. * @param len routing table size(in bytes)
  938. * @param size pointer to the number of devices in routing table(including itself)
  939. *
  940. * @return
  941. * - ESP_OK
  942. * - ESP_ERR_MESH_ARGUMENT
  943. */
  944. esp_err_t esp_mesh_get_routing_table(mesh_addr_t *mac, int len, int *size);
  945. /**
  946. * @brief post the toDS state to the mesh stack
  947. *
  948. * @attention This API is only for root.
  949. *
  950. * @param reachable this state represents if root is able to access external IP network
  951. *
  952. * @return
  953. * - ESP_OK
  954. * - ESP_FAIL
  955. */
  956. esp_err_t esp_mesh_post_toDS_state(bool reachable);
  957. /**
  958. * @brief return the number of packets pending in the queue waiting to be sent by the mesh stack
  959. *
  960. * @param pending pointer to the TX pending
  961. *
  962. * @return
  963. * - ESP_OK
  964. * - ESP_FAIL
  965. */
  966. esp_err_t esp_mesh_get_tx_pending(mesh_tx_pending_t *pending);
  967. /**
  968. * @brief return the number of packets available in the queue waiting to be received by applications
  969. *
  970. * @param pending pointer to the RX pending
  971. *
  972. * @return
  973. * - ESP_OK
  974. * - ESP_FAIL
  975. */
  976. esp_err_t esp_mesh_get_rx_pending(mesh_rx_pending_t *pending);
  977. /**
  978. * @brief return the number of packets could be accepted from the specified address
  979. *
  980. * @param addr self address or an associate children address
  981. * @param xseqno_in sequence number of the last received packet from the specified address
  982. *
  983. * @return the number of upQ for a specified address
  984. */
  985. int esp_mesh_available_txupQ_num(const mesh_addr_t *addr, uint32_t *xseqno_in);
  986. /**
  987. * @brief set queue size
  988. *
  989. * @attention This API shall be called before esp_mesh_start().
  990. *
  991. * @param qsize default:32(min:16)
  992. *
  993. * @return
  994. * - ESP_OK
  995. * - ESP_FAIL
  996. */
  997. esp_err_t esp_mesh_set_xon_qsize(int qsize);
  998. /**
  999. * @brief get queue size
  1000. *
  1001. * @return qsize
  1002. */
  1003. int esp_mesh_get_xon_qsize(void);
  1004. /**
  1005. * @brief set if allow more than one root existing in one network
  1006. *
  1007. * @param allowed allow or not
  1008. *
  1009. * @return
  1010. * - ESP_OK
  1011. * - ESP_WIFI_ERR_NOT_INIT
  1012. * - ESP_WIFI_ERR_NOT_START
  1013. */
  1014. esp_err_t esp_mesh_allow_root_conflicts(bool allowed);
  1015. /**
  1016. * @brief check if allow more than one root to exist in one network
  1017. *
  1018. * @return true/false
  1019. */
  1020. bool esp_mesh_is_root_conflicts_allowed(void);
  1021. /**
  1022. * @brief set group ID addresses
  1023. *
  1024. * @param addr pointer to new group ID addresses
  1025. * @param num the number of group ID addresses
  1026. *
  1027. * @return
  1028. * - ESP_OK
  1029. * - ESP_MESH_ERR_ARGUMENT
  1030. */
  1031. esp_err_t esp_mesh_set_group_id(const mesh_addr_t *addr, int num);
  1032. /**
  1033. * @brief delete group ID addresses
  1034. *
  1035. * @param addr pointer to deleted group ID address
  1036. * @param num the number of group ID addresses
  1037. *
  1038. * @return
  1039. * - ESP_OK
  1040. * - ESP_MESH_ERR_ARGUMENT
  1041. */
  1042. esp_err_t esp_mesh_delete_group_id(const mesh_addr_t *addr, int num);
  1043. /**
  1044. * @brief get the number of group ID addresses
  1045. *
  1046. * @return the number of group ID addresses
  1047. */
  1048. int esp_mesh_get_group_num(void);
  1049. /**
  1050. * @brief get group ID addresses
  1051. *
  1052. * @param addr pointer to group ID addresses
  1053. * @param num the number of group ID addresses
  1054. *
  1055. * @return
  1056. * - ESP_OK
  1057. * - ESP_MESH_ERR_ARGUMENT
  1058. */
  1059. esp_err_t esp_mesh_get_group_list(mesh_addr_t *addr, int num);
  1060. /**
  1061. * @brief check if the specified group address is my group
  1062. *
  1063. * @return true/false
  1064. */
  1065. bool esp_mesh_is_my_group(const mesh_addr_t *addr);
  1066. /**
  1067. * @brief set mesh network capacity
  1068. *
  1069. * @attention This API shall be called before esp_mesh_start().
  1070. *
  1071. * @param num mesh network capacity
  1072. *
  1073. * @return
  1074. * - ESP_OK
  1075. * - ESP_ERR_MESH_NOT_ALLOWED
  1076. * - ESP_MESH_ERR_ARGUMENT
  1077. */
  1078. esp_err_t esp_mesh_set_capacity_num(int num);
  1079. /**
  1080. * @brief get mesh network capacity
  1081. *
  1082. * @return mesh network capacity
  1083. */
  1084. int esp_mesh_get_capacity_num(void);
  1085. /**
  1086. * @brief set mesh ie crypto functions
  1087. *
  1088. * @param crypto_funcs crypto functions for mesh ie
  1089. *
  1090. * @return
  1091. * - ESP_OK
  1092. */
  1093. esp_err_t esp_mesh_set_ie_crypto_funcs(const mesh_crypto_funcs_t *crypto_funcs);
  1094. /**
  1095. * @brief set mesh ie crypto key
  1096. *
  1097. * @attention This API shall be called after esp_mesh_set_config() and before esp_mesh_start().
  1098. *
  1099. * @param key ASCII crypto key
  1100. * @param len length in bytes, range:8~64
  1101. *
  1102. * @return
  1103. * - ESP_OK
  1104. * - ESP_ERR_MESH_NOT_ALLOWED
  1105. * - ESP_ERR_MESH_NOT_CONFIG
  1106. * - ESP_MESH_ERR_ARGUMENT
  1107. */
  1108. esp_err_t esp_mesh_set_ie_crypto_key(const char *key, int len);
  1109. /**
  1110. * @brief get mesh ie crypto key
  1111. *
  1112. * @param key ASCII crypto key
  1113. * @param len length in bytes, range:8~64
  1114. *
  1115. * @return
  1116. * - ESP_OK
  1117. * - ESP_MESH_ERR_ARGUMENT
  1118. */
  1119. esp_err_t esp_mesh_get_ie_crypto_key(char *key, int len);
  1120. /**
  1121. * @brief set delay time before starting root healing
  1122. *
  1123. * @param delay_ms delay time in milliseconds
  1124. *
  1125. * @return
  1126. * - ESP_OK
  1127. */
  1128. esp_err_t esp_mesh_set_root_healing_delay(int delay_ms);
  1129. /**
  1130. * @brief get delay time before starting root healing
  1131. *
  1132. * @return delay time in milliseconds
  1133. */
  1134. int esp_mesh_get_root_healing_delay(void);
  1135. /**
  1136. * @brief set mesh event callback
  1137. *
  1138. * @param event_cb mesh event call back
  1139. *
  1140. * @return
  1141. * - ESP_OK
  1142. */
  1143. esp_err_t esp_mesh_set_event_cb(const mesh_event_cb_t event_cb);
  1144. /**
  1145. * @brief set Fixed Root setting for the device
  1146. * If Fixed Root setting of the device is enabled, it won't compete to be a root.
  1147. * If a scenario needs a fixed root, all devices in this network shall enable this setting.
  1148. *
  1149. * @param enable enable or not
  1150. *
  1151. * @return
  1152. * - ESP_OK
  1153. */
  1154. esp_err_t esp_mesh_fix_root(bool enable);
  1155. /**
  1156. * @brief check if Fixed Root setting is enabled
  1157. * Fixed Root setting can be changed by API esp_mesh_fix_root().
  1158. * Fixed Root setting can also be changed by event MESH_EVENT_ROOT_FIXED.
  1159. *
  1160. * @return true/false
  1161. */
  1162. bool esp_mesh_is_root_fixed(void);
  1163. /**
  1164. * @brief set a specified parent
  1165. *
  1166. * @param parent parent configuration, the ssid and the channel of the parent are mandatory.
  1167. * @param parent_mesh_id parent mesh ID, if not set, use the device default one.
  1168. * @param my_type my mesh type
  1169. * @param my_layer my mesh layer
  1170. *
  1171. * @return
  1172. * - ESP_OK
  1173. * - ESP_ERR_ARGUMENT
  1174. * - ESP_ERR_MESH_NOT_CONFIG
  1175. */
  1176. 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);
  1177. /**
  1178. * @brief get mesh networking IE length of one AP
  1179. *
  1180. * @param len mesh networking IE length
  1181. *
  1182. * @return
  1183. * - ESP_OK
  1184. * - ESP_ERR_WIFI_NOT_INIT
  1185. * - ESP_ERR_WIFI_ARG
  1186. * - ESP_ERR_WIFI_FAIL
  1187. */
  1188. esp_err_t esp_mesh_scan_get_ap_ie_len(int *len);
  1189. /**
  1190. * @brief get AP record
  1191. * Different from esp_wifi_scan_get_ap_records(), this API only gets one of scanned APs each time.
  1192. *
  1193. * @param ap_record pointer to the AP record
  1194. * @param buffer pointer to the mesh networking IE of this AP
  1195. *
  1196. * @return
  1197. * - ESP_OK
  1198. * - ESP_ERR_WIFI_NOT_INIT
  1199. * - ESP_ERR_WIFI_ARG
  1200. * - ESP_ERR_WIFI_FAIL
  1201. */
  1202. esp_err_t esp_mesh_scan_get_ap_record(wifi_ap_record_t *ap_record, void *buffer);
  1203. #ifdef __cplusplus
  1204. }
  1205. #endif
  1206. #endif /* __ESP_MESH_H__ */