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