From ebd1849e18a6a0dcad4021311d4ea3dcc7a122ba Mon Sep 17 00:00:00 2001 From: qiyueixa Date: Thu, 26 Apr 2018 00:13:53 +0800 Subject: [PATCH] mesh: add fix root function --- components/esp32/include/esp_mesh.h | 56 ++++++++++++++++++- components/esp32/lib | 2 +- .../main/Kconfig.projbuild | 6 ++ .../internal_transceiver/main/mesh_main.c | 27 ++++++++- 4 files changed, 87 insertions(+), 4 deletions(-) diff --git a/components/esp32/include/esp_mesh.h b/components/esp32/include/esp_mesh.h index 12e2d55d8..7c1c166ac 100644 --- a/components/esp32/include/esp_mesh.h +++ b/components/esp32/include/esp_mesh.h @@ -171,6 +171,9 @@ typedef enum { and this device is specified to be a root by users, users should set a new parent for this device. if self organized is enabled, this device will find a new parent by itself, users could ignore this event. */ + MESH_EVENT_ROOT_FIXED, /**< When nodes join a network, if the setting of Root Fixed for one node is different + from that of its parent, the node will update the setting the same as its parent's. + Root Fixed setting of each node is variable as that setting changes of root. */ MESH_EVENT_MAX, } mesh_event_id_t; @@ -339,6 +342,13 @@ typedef struct { uint16_t rt_size_change; /**< the changed value */ } mesh_event_routing_table_change_t; +/** + * @brief root fixed + */ +typedef struct { + bool is_fixed; /**< status */ +} mesh_event_root_fixed_t; + /** * @brief mesh event information */ @@ -360,6 +370,7 @@ typedef union { mesh_event_root_address_t root_addr; /**< root address */ mesh_event_root_switch_req_t switch_req; /**< root switch request */ mesh_event_root_conflict_t root_conflict; /**< other powerful root */ + mesh_event_root_fixed_t root_fixed; /**< root fixed */ } mesh_event_info_t; /** @@ -864,7 +875,7 @@ bool esp_mesh_is_root(void); /** * @brief enable/disable mesh networking self-organized, self-organized by default * if self-organized is disabled, users should set a parent for this node via - * esp_mesh_set_parent()(Unimplemented); + * esp_mesh_set_parent(); * * @param enable * @@ -1059,6 +1070,13 @@ int esp_mesh_get_xon_qsize(void); */ esp_err_t esp_mesh_allow_root_conflicts(const bool allowed); +/** + * @brief check if allow more than one root to exist in one network + * + * @return true/false + */ +bool esp_mesh_is_root_conflicts_allowed(void); + /** * @brief set group ID addresses * @@ -1194,6 +1212,42 @@ int esp_mesh_get_root_healing_delay(void); */ esp_err_t esp_mesh_set_event_cb(const mesh_event_cb_t event_cb); +/** + * @brief set Root Fixed setting for nodes + * If Root Fixed setting of nodes is enabled, they won't compete to be a root. + * If a scenario needs a fixed root, all nodes in this network must enable this setting. + * + * @param enable enable or not + * + * @return + * - ESP_OK + */ +esp_err_t esp_mesh_fix_root(const bool enable); + +/** + * @brief check if Root Fixed setting is enabled + * Root Fixed setting can be changed by API esp_mesh_fix_root(). + * Root Fixed setting can also be changed by event MESH_EVENT_ROOT_FIXED. + * + * @return true/false + */ +bool esp_mesh_is_root_fixed(void); + +/** + * @brief set a specified parent + * self-organized networking will be disabled. + * + * @param parent parent configuration + * @param my_type my mesh type + * @param my_layer my mesh layer + * + * @return + * - ESP_OK + * - ESP_ERR_ARGUMENT + * - ESP_ERR_MESH_NOT_CONFIG + */ +esp_err_t esp_mesh_set_parent(wifi_config_t *parent, mesh_type_t my_type, int my_layer); + #ifdef __cplusplus } diff --git a/components/esp32/lib b/components/esp32/lib index 3ca7b8a9a..e9e457933 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit 3ca7b8a9afdc01a1f0714072cc59c8509481e395 +Subproject commit e9e4579336a539590a88ccac05ab9d0422506c3d diff --git a/examples/mesh/internal_transceiver/main/Kconfig.projbuild b/examples/mesh/internal_transceiver/main/Kconfig.projbuild index 74dee1a28..a3bad4bfd 100644 --- a/examples/mesh/internal_transceiver/main/Kconfig.projbuild +++ b/examples/mesh/internal_transceiver/main/Kconfig.projbuild @@ -70,5 +70,11 @@ config MESH_ROUTE_TABLE_SIZE default 50 help The number of devices over the network(max: 300). + +config MESH_PARENT_SSID + string "Parent SSID" + default "PARENT_SSID" + help + Parent SSID. endmenu diff --git a/examples/mesh/internal_transceiver/main/mesh_main.c b/examples/mesh/internal_transceiver/main/mesh_main.c index 6e6bebf12..bfdfa6f63 100644 --- a/examples/mesh/internal_transceiver/main/mesh_main.c +++ b/examples/mesh/internal_transceiver/main/mesh_main.c @@ -149,6 +149,7 @@ void esp_mesh_p2p_tx_main(void *arg) err, data.proto, data.tos); } } + /* if route_table_size is less than 10, add delay to avoid watchdog in this task. */ if (route_table_size < 10) { vTaskDelay(1 * 1000 / portTICK_RATE_MS); } @@ -334,14 +335,21 @@ void esp_mesh_event_handler(mesh_event_t event) event.info.switch_req.reason, MAC2STR( event.info.switch_req.rc_addr.addr)); break; + case MESH_EVENT_ROOT_SWITCH_ACK: ESP_LOGI(MESH_TAG, ""); break; + case MESH_EVENT_TODS_STATE: ESP_LOGI(MESH_TAG, "state:%d", event.info.toDS_state); - ; break; + + case MESH_EVENT_ROOT_FIXED: + ESP_LOGI(MESH_TAG, "%s", + event.info.root_fixed.is_fixed ? "fixed" : "not fixed"); + break; + default: ESP_LOGI(MESH_TAG, "unknown"); break; @@ -380,6 +388,9 @@ void app_main(void) ESP_ERROR_CHECK(esp_mesh_set_ap_authmode(CONFIG_MESH_AP_AUTHMODE)); ESP_ERROR_CHECK(esp_mesh_set_vote_percentage(1)); ESP_ERROR_CHECK(esp_mesh_set_ap_assoc_expire(10)); +#ifdef MESH_FIX_ROOT + ESP_ERROR_CHECK(esp_mesh_fix_root(1)); +#endif mesh_cfg_t cfg = MESH_INIT_CONFIG_DEFAULT(); /* mesh ID */ memcpy((uint8_t *) &cfg.mesh_id, MESH_ID, 6); @@ -401,7 +412,19 @@ void app_main(void) ESP_ERROR_CHECK(esp_mesh_get_switch_parent_paras(&switch_paras)); switch_paras.backoff_rssi = -45; ESP_ERROR_CHECK(esp_mesh_set_switch_parent_paras(&switch_paras)); + +#ifdef MESH_SET_PARENT + /* parent */ + wifi_config_t parent = { + .sta = { + .ssid = CONFIG_MESH_PARENT_SSID, + .channel = CONFIG_MESH_CHANNEL, + }, + }; + ESP_ERROR_CHECK(esp_mesh_set_parent(&parent, MESH_ROOT, 1)); +#endif /* mesh start */ ESP_ERROR_CHECK(esp_mesh_start()); - ESP_LOGI(MESH_TAG, "mesh starts successfully, heap:%d\n", esp_get_free_heap_size()); + ESP_LOGI(MESH_TAG, "mesh starts successfully, heap:%d, %s\n", esp_get_free_heap_size(), + esp_mesh_is_root_fixed() ? "root fixed" : "root not fixed"); }