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