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