esp_mesh.h 56 KB

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