1 // Copyright 2017-2018 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 * Software Stack demonstrated:
17 * |------------------------------------------------------------------------------|
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 * | |-----------------------------------| |---------------| |
29 * | | Wi-Fi Driver | |
30 * | |--------------------------------------------------| |
33 * |------------------------------------------------------------------------------|
35 * System Events delivery:
39 * | Wi-Fi stack | events |---------------------|
40 * | | -------------> | |
41 * |---------------| | |
43 * |---------------| events | |
44 * | | -------------> | |
45 * | LwIP stack | |---------------------|
48 * | mesh event callback handler
49 * | |----------------------------|
51 * |---------------| | application |
53 * | mesh stack | -------------> | |
54 * | | |----------------------------|
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.
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.
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.
76 * tcpip_adapter_init();
77 * tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP);
78 * tcpip_adapter_dhcpc_stop(TCPIP_ADAPTER_IF_STA);
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.
84 #ifndef __ESP_MESH_H__
85 #define __ESP_MESH_H__
89 #include "esp_wifi_types.h"
90 #include "esp_mesh_internal.h"
96 /*******************************************************
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) */
103 * @brief Mesh error code definition
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 */
129 * @brief Flags bitmap for esp_mesh_send() and esp_mesh_recv()
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 */
140 * @brief Option definitions for esp_mesh_send() and esp_mesh_recv()
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() */
146 * @brief Flag of mesh networking IE
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 */
153 /*******************************************************
155 *******************************************************/
157 * @brief Enumerated list of mesh event id
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
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. */
196 /** @brief ESP-MESH event base declaration */
197 ESP_EVENT_DECLARE_BASE(MESH_EVENT);
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 */
210 * @brief Protocol of transmitted application data
213 MESH_PROTO_BIN, /**< binary */
214 MESH_PROTO_HTTP, /**< HTTP protocol */
215 MESH_PROTO_JSON, /**< JSON format */
216 MESH_PROTO_MQTT, /**< MQTT protocol */
220 * @brief For reliable transmission, mesh stack provides three type of services
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 */
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;
237 * @brief Mesh disconnect reason code
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;
254 /*******************************************************
256 *******************************************************/
258 * @brief IP address and port
261 ip4_addr_t ip4; /**< IP address */
262 uint16_t port; /**< port */
263 } __attribute__((packed)) mip_t;
266 * @brief Mesh address
269 uint8_t addr[6]; /**< mac address */
270 mip_t mip; /**< mip address */
274 * @brief Channel switch information
277 uint8_t channel; /**< new channel */
278 } mesh_event_channel_switch_t;
281 * @brief Parent connected information
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;
289 * @brief No parent found information
292 int scan_times; /**< scan times being through */
293 } mesh_event_no_parent_found_t;
296 * @brief Layer change information
299 uint8_t new_layer; /**< new layer */
300 } mesh_event_layer_change_t;
303 * @brief The reachability of the root to a DS (distribute system)
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;
311 * @brief vote started information
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;
320 * @brief find a mesh network that this device can join
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;
328 * @brief IP settings from LwIP stack
330 typedef system_event_sta_got_ip_t mesh_event_root_got_ip_t;
333 * @brief Root address
335 typedef mesh_addr_t mesh_event_root_address_t;
338 * @brief Parent disconnected information
340 typedef system_event_sta_disconnected_t mesh_event_disconnected_t;
343 * @brief Child connected information
345 typedef system_event_ap_staconnected_t mesh_event_child_connected_t;
348 * @brief Child disconnected information
350 typedef system_event_ap_stadisconnected_t mesh_event_child_disconnected_t;
353 * @brief Root switch request information
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;
361 * @brief Other powerful root address
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;
370 * @brief Routing table change
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;
381 bool is_fixed; /**< status */
382 } mesh_event_root_fixed_t;
385 * @brief Scan done event information
388 uint8_t number; /**< the number of APs scanned */
389 } mesh_event_scan_done_t;
392 * @brief Network state information
395 bool is_rootless; /**< whether current mesh network has a root */
396 } mesh_event_network_state_t;
399 * @brief New router information
401 typedef system_event_sta_connected_t mesh_event_router_switch_t;
404 * @brief Mesh event information
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 */
435 uint8_t type; /**< option type */
436 uint16_t len; /**< option length */
437 uint8_t *val; /**< option value */
438 } __attribute__((packed)) mesh_opt_t;
441 * @brief Mesh data for esp_mesh_send() and esp_mesh_recv()
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 */
451 * @brief Router configuration
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. */
467 * @brief Mesh softAP configuration
470 uint8_t password[64]; /**< mesh softAP password */
471 uint8_t max_connection; /**< max number of stations allowed to connect in, max 10 */
475 * @brief Mesh initialization configuration
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 */
488 * @brief Vote address configuration
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() */
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 */
506 * @brief The number of packets pending in the queue waiting to be sent by the mesh stack
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 */
518 * @brief The number of packets available in the queue waiting to be received by applications
521 int toDS; /**< to external DS */
522 int toSelf; /**< to self */
525 /*******************************************************
526 * Variable Declaration
527 *******************************************************/
528 /* mesh IE crypto callback function */
529 extern const mesh_crypto_funcs_t g_wifi_default_mesh_crypto_funcs;
531 #define MESH_INIT_CONFIG_DEFAULT() { \
532 .crypto_funcs = &g_wifi_default_mesh_crypto_funcs, \
535 /*******************************************************
536 * Function Definitions
537 *******************************************************/
539 * @brief Mesh initialization
540 * - Check whether Wi-Fi is started.
541 * - Initialize mesh global variables with default values.
543 * @attention This API shall be called after Wi-Fi is started.
549 esp_err_t esp_mesh_init(void);
552 * @brief Mesh de-initialization
554 * - Release resources and stop the mesh
560 esp_err_t esp_mesh_deinit(void);
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.
569 * @attention This API shall be called after mesh initialization and configuration.
574 * - ESP_ERR_MESH_NOT_INIT
575 * - ESP_ERR_MESH_NOT_CONFIG
576 * - ESP_ERR_MESH_NO_MEMORY
578 esp_err_t esp_mesh_start(void);
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.
595 esp_err_t esp_mesh_stop(void);
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.
602 * @attention This API is not reentrant.
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().
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.
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.
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
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);
654 * @brief Receive a packet targeted to self over the mesh network
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.
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
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.
677 * - ESP_ERR_MESH_ARGUMENT
678 * - ESP_ERR_MESH_NOT_START
679 * - ESP_ERR_MESH_TIMEOUT
680 * - ESP_ERR_MESH_DISCARD
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);
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.
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.
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.
700 * @attention This API is only called by the root.
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.
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
716 * - ESP_ERR_MESH_ARGUMENT
717 * - ESP_ERR_MESH_NOT_START
718 * - ESP_ERR_MESH_TIMEOUT
719 * - ESP_ERR_MESH_DISCARD
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[],
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.
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.
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).
743 * @attention This API shall be called before mesh is started after mesh is initialized.
745 * @param[in] config pointer to mesh stack configuration
749 * - ESP_ERR_MESH_ARGUMENT
750 * - ESP_ERR_MESH_NOT_ALLOWED
752 esp_err_t esp_mesh_set_config(const mesh_cfg_t *config);
755 * @brief Get mesh stack configuration
757 * @param[out] config pointer to mesh stack configuration
761 * - ESP_ERR_MESH_ARGUMENT
763 esp_err_t esp_mesh_get_config(mesh_cfg_t *config);
766 * @brief Get router configuration
768 * @attention This API is used to dynamically modify the router configuration after mesh is configured.
770 * @param[in] router pointer to router configuration
774 * - ESP_ERR_MESH_ARGUMENT
776 esp_err_t esp_mesh_set_router(const mesh_router_t *router);
779 * @brief Get router configuration
781 * @param[out] router pointer to router configuration
785 * - ESP_ERR_MESH_ARGUMENT
787 esp_err_t esp_mesh_get_router(mesh_router_t *router);
790 * @brief Set mesh network ID
792 * @attention This API is used to dynamically modify the mesh network ID.
794 * @param[in] id pointer to mesh network ID
798 * - ESP_ERR_MESH_ARGUMENT: invalid argument
800 esp_err_t esp_mesh_set_id(const mesh_addr_t *id);
803 * @brief Get mesh network ID
805 * @param[out] id pointer to mesh network ID
809 * - ESP_ERR_MESH_ARGUMENT
811 esp_err_t esp_mesh_get_id(mesh_addr_t *id);
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
818 * @param[in] type device type
822 * - ESP_ERR_MESH_NOT_ALLOWED
824 esp_err_t esp_mesh_set_type(mesh_type_t type);
827 * @brief Get device type over mesh network
829 * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
834 mesh_type_t esp_mesh_get_type(void);
837 * @brief Set network max layer value (max:25, default:25)
838 * - Network max layer limits the max hop count.
840 * @attention This API shall be called before mesh is started.
842 * @param[in] max_layer max layer value
846 * - ESP_ERR_MESH_ARGUMENT
847 * - ESP_ERR_MESH_NOT_ALLOWED
849 esp_err_t esp_mesh_set_max_layer(int max_layer);
852 * @brief Get max layer value
854 * @return max layer value
856 int esp_mesh_get_max_layer(void);
859 * @brief Set mesh softAP password
861 * @attention This API shall be called before mesh is started.
863 * @param[in] pwd pointer to the password
864 * @param[in] len password length
868 * - ESP_ERR_MESH_ARGUMENT
869 * - ESP_ERR_MESH_NOT_ALLOWED
871 esp_err_t esp_mesh_set_ap_password(const uint8_t *pwd, int len);
874 * @brief Set mesh softAP authentication mode
876 * @attention This API shall be called before mesh is started.
878 * @param[in] authmode authentication mode
882 * - ESP_ERR_MESH_ARGUMENT
883 * - ESP_ERR_MESH_NOT_ALLOWED
885 esp_err_t esp_mesh_set_ap_authmode(wifi_auth_mode_t authmode);
888 * @brief Get mesh softAP authentication mode
890 * @return authentication mode
892 wifi_auth_mode_t esp_mesh_get_ap_authmode(void);
895 * @brief Set mesh softAP max connection value
897 * @attention This API shall be called before mesh is started.
899 * @param[in] connections the number of max connections
903 * - ESP_ERR_MESH_ARGUMENT
905 esp_err_t esp_mesh_set_ap_connections(int connections);
908 * @brief Get mesh softAP max connection configuration
910 * @return the number of max connections
912 int esp_mesh_get_ap_connections(void);
915 * @brief Get current layer value over the mesh network
917 * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
919 * @return layer value
922 int esp_mesh_get_layer(void);
925 * @brief Get the parent BSSID
927 * @attention This API shall be called after having received the event MESH_EVENT_PARENT_CONNECTED.
929 * @param[out] bssid pointer to parent BSSID
935 esp_err_t esp_mesh_get_parent_bssid(mesh_addr_t *bssid);
938 * @brief Return whether the device is the root node of the network
942 bool esp_mesh_is_root(void);
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().
953 * @attention This API is used to dynamically modify whether to enable the self organizing.
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.
964 esp_err_t esp_mesh_set_self_organized(bool enable, bool select_parent);
967 * @brief Return whether enable self-organized networking or not
971 bool esp_mesh_get_self_organized(void);
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.
986 * Root switch is completed with minimal disruption to the whole mesh network.
988 * @attention This API is only called by the root.
990 * @param[in] vote vote configuration
991 * - If this parameter is set NULL, the vote will perform the default 15 times.
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
1000 * - ESP_ERR_MESH_QUEUE_FULL
1001 * - ESP_ERR_MESH_DISCARD
1004 esp_err_t esp_mesh_waive_root(const mesh_vote_t *vote, int reason);
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.
1011 * @attention This API shall be called before mesh is started.
1013 * @param[in] percentage vote percentage threshold
1019 esp_err_t esp_mesh_set_vote_percentage(float percentage);
1022 * @brief Get vote percentage threshold for approval of being a root
1024 * @return percentage threshold
1026 float esp_mesh_get_vote_percentage(void);
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.
1034 * @param[in] seconds the expired time
1040 esp_err_t esp_mesh_set_ap_assoc_expire(int seconds);
1043 * @brief Get mesh softAP associate expired time
1047 int esp_mesh_get_ap_assoc_expire(void);
1050 * @brief Get total number of devices in current network (including the root)
1052 * @attention The returned value might be incorrect when the network is changing.
1054 * @return total number of devices (including the root)
1056 int esp_mesh_get_total_node_num(void);
1059 * @brief Get the number of devices in this device's sub-network (including self)
1061 * @return the number of devices over this device's sub-network (including self)
1063 int esp_mesh_get_routing_table_size(void);
1066 * @brief Get routing table of this device's sub-network (including itself)
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)
1074 * - ESP_ERR_MESH_ARGUMENT
1076 esp_err_t esp_mesh_get_routing_table(mesh_addr_t *mac, int len, int *size);
1079 * @brief Post the toDS state to the mesh stack
1081 * @attention This API is only for the root.
1083 * @param[in] reachable this state represents whether the root is able to access external IP network
1089 esp_err_t esp_mesh_post_toDS_state(bool reachable);
1092 * @brief Return the number of packets pending in the queue waiting to be sent by the mesh stack
1094 * @param[out] pending pointer to the TX pending
1100 esp_err_t esp_mesh_get_tx_pending(mesh_tx_pending_t *pending);
1103 * @brief Return the number of packets available in the queue waiting to be received by applications
1105 * @param[out] pending pointer to the RX pending
1111 esp_err_t esp_mesh_get_rx_pending(mesh_rx_pending_t *pending);
1114 * @brief Return the number of packets could be accepted from the specified address
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
1119 * @return the number of upQ for a certain address
1121 int esp_mesh_available_txupQ_num(const mesh_addr_t *addr, uint32_t *xseqno_in);
1124 * @brief Set the number of queue
1126 * @attention This API shall be called before mesh is started.
1128 * @param[in] qsize default:32 (min:16)
1134 esp_err_t esp_mesh_set_xon_qsize(int qsize);
1137 * @brief Get queue size
1139 * @return the number of queue
1141 int esp_mesh_get_xon_qsize(void);
1144 * @brief Set whether allow more than one root existing in one network
1146 * @param[in] allowed allow or not
1150 * - ESP_WIFI_ERR_NOT_INIT
1151 * - ESP_WIFI_ERR_NOT_START
1153 esp_err_t esp_mesh_allow_root_conflicts(bool allowed);
1156 * @brief Check whether allow more than one root to exist in one network
1158 * @return true/false
1160 bool esp_mesh_is_root_conflicts_allowed(void);
1163 * @brief Set group ID addresses
1165 * @param[in] addr pointer to new group ID addresses
1166 * @param[in] num the number of group ID addresses
1170 * - ESP_MESH_ERR_ARGUMENT
1172 esp_err_t esp_mesh_set_group_id(const mesh_addr_t *addr, int num);
1175 * @brief Delete group ID addresses
1177 * @param[in] addr pointer to deleted group ID address
1178 * @param[in] num the number of group ID addresses
1182 * - ESP_MESH_ERR_ARGUMENT
1184 esp_err_t esp_mesh_delete_group_id(const mesh_addr_t *addr, int num);
1187 * @brief Get the number of group ID addresses
1189 * @return the number of group ID addresses
1191 int esp_mesh_get_group_num(void);
1194 * @brief Get group ID addresses
1196 * @param[out] addr pointer to group ID addresses
1197 * @param[in] num the number of group ID addresses
1201 * - ESP_MESH_ERR_ARGUMENT
1203 esp_err_t esp_mesh_get_group_list(mesh_addr_t *addr, int num);
1206 * @brief Check whether the specified group address is my group
1208 * @return true/false
1210 bool esp_mesh_is_my_group(const mesh_addr_t *addr);
1213 * @brief Set mesh network capacity (max:1000, default:300)
1215 * @attention This API shall be called before mesh is started.
1217 * @param[in] num mesh network capacity
1221 * - ESP_ERR_MESH_NOT_ALLOWED
1222 * - ESP_MESH_ERR_ARGUMENT
1224 esp_err_t esp_mesh_set_capacity_num(int num);
1227 * @brief Get mesh network capacity
1229 * @return mesh network capacity
1231 int esp_mesh_get_capacity_num(void);
1234 * @brief Set mesh IE crypto functions
1236 * @attention This API can be called at any time after mesh is initialized.
1238 * @param[in] crypto_funcs crypto functions for mesh IE
1239 * - If crypto_funcs is set to NULL, mesh IE is no longer encrypted.
1243 esp_err_t esp_mesh_set_ie_crypto_funcs(const mesh_crypto_funcs_t *crypto_funcs);
1246 * @brief Set mesh IE crypto key
1248 * @attention This API can be called at any time after mesh is initialized.
1250 * @param[in] key ASCII crypto key
1251 * @param[in] len length in bytes, range:8~64
1255 * - ESP_MESH_ERR_ARGUMENT
1257 esp_err_t esp_mesh_set_ie_crypto_key(const char *key, int len);
1260 * @brief Get mesh IE crypto key
1262 * @param[out] key ASCII crypto key
1263 * @param[in] len length in bytes, range:8~64
1267 * - ESP_MESH_ERR_ARGUMENT
1269 esp_err_t esp_mesh_get_ie_crypto_key(char *key, int len);
1272 * @brief Set delay time before starting root healing
1274 * @param[in] delay_ms delay time in milliseconds
1279 esp_err_t esp_mesh_set_root_healing_delay(int delay_ms);
1282 * @brief Get delay time before network starts root healing
1284 * @return delay time in milliseconds
1286 int esp_mesh_get_root_healing_delay(void);
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.
1294 * @param[in] enable enable or not
1299 esp_err_t esp_mesh_fix_root(bool enable);
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.
1306 * @return true/false
1308 bool esp_mesh_is_root_fixed(void);
1311 * @brief Set a specified parent for the device
1313 * @attention This API can be called at any time after mesh is configured.
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.
1331 * - ESP_ERR_ARGUMENT
1332 * - ESP_ERR_MESH_NOT_CONFIG
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);
1337 * @brief Get mesh networking IE length of one AP
1339 * @param[out] len mesh networking IE length
1343 * - ESP_ERR_WIFI_NOT_INIT
1344 * - ESP_ERR_WIFI_ARG
1345 * - ESP_ERR_WIFI_FAIL
1347 esp_err_t esp_mesh_scan_get_ap_ie_len(int *len);
1350 * @brief Get AP record
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.
1355 * @param[out] ap_record pointer to one AP record
1356 * @param[out] buffer pointer to the mesh networking IE of this AP
1360 * - ESP_ERR_WIFI_NOT_INIT
1361 * - ESP_ERR_WIFI_ARG
1362 * - ESP_ERR_WIFI_FAIL
1364 esp_err_t esp_mesh_scan_get_ap_record(wifi_ap_record_t *ap_record, void *buffer);
1367 * @brief Flush upstream packets pending in to_parent queue and to_parent_p2p queue
1372 esp_err_t esp_mesh_flush_upstream_packets(void);
1375 * @brief Get the number of nodes in the subnet of a specific child
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
1382 * - ESP_ERR_MESH_NOT_START
1383 * - ESP_ERR_MESH_ARGUMENT
1385 esp_err_t esp_mesh_get_subnet_nodes_num(const mesh_addr_t *child_mac, int *nodes_num);
1388 * @brief Get nodes in the subnet of a specific child
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
1396 * - ESP_ERR_MESH_NOT_START
1397 * - ESP_ERR_MESH_ARGUMENT
1399 esp_err_t esp_mesh_get_subnet_nodes_list(const mesh_addr_t *child_mac, mesh_addr_t *nodes, int nodes_num);
1402 * @brief Disconnect from current parent
1407 esp_err_t esp_mesh_disconnect(void);
1410 * @brief Connect to current parent
1415 esp_err_t esp_mesh_connect(void);
1418 * @brief Flush scan result
1423 esp_err_t esp_mesh_flush_scan_result(void);
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
1431 * @attention This API is only called by the root.
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.
1440 esp_err_t esp_mesh_switch_channel(const uint8_t *new_bssid, int csa_newchan, int csa_count);
1443 * @brief Get the router BSSID
1445 * @param[out] router_bssid pointer to the router BSSID
1449 * - ESP_ERR_WIFI_NOT_INIT
1450 * - ESP_ERR_WIFI_ARG
1452 esp_err_t esp_mesh_get_router_bssid(uint8_t *router_bssid);
1455 * @brief Get the TSF time
1457 * @return the TSF time
1459 int64_t esp_mesh_get_tsf_time(void);
1464 #endif /* __ESP_MESH_H__ */