From 00412aa3cb9e871bc81bd7e77fe264b05cf3b00e Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Thu, 24 Nov 2022 16:20:40 +0800 Subject: [PATCH] Controller: Make the commissioner feature optional --- components/esp_matter_controller/CMakeLists.txt | 6 ++++++ components/esp_matter_controller/Kconfig | 13 ++++++++++--- .../esp_matter_commissioner.h | 2 ++ .../esp_matter_controller_cluster_command.cpp | 11 +++++++++++ .../esp_matter_controller_console.cpp | 3 ++- .../esp_matter_controller_read_command.cpp | 12 ++++++++++++ .../esp_matter_controller_subscribe_command.cpp | 15 +++++++++++++++ .../esp_matter_controller_write_command.cpp | 13 ++++++++++++- examples/controller/CMakeLists.txt | 6 +++--- examples/controller/main/app_main.cpp | 15 ++++++++++++--- examples/controller/sdkconfig.defaults | 3 ++- 11 files changed, 87 insertions(+), 12 deletions(-) diff --git a/components/esp_matter_controller/CMakeLists.txt b/components/esp_matter_controller/CMakeLists.txt index 6e1ef193f..742261fe7 100644 --- a/components/esp_matter_controller/CMakeLists.txt +++ b/components/esp_matter_controller/CMakeLists.txt @@ -1,10 +1,16 @@ set(src_dirs_list ) set(include_dirs_list ) +set(exclude_srcs_list ) if (CONFIG_ESP_MATTER_CONTROLLER_ENABLE) list(APPEND src_dirs_list . logger/zap-generated) list(APPEND include_dirs_list . logger) endif() +if (NOT CONFIG_ESP_MATTER_COMMISSIONER_ENABLE) + list(APPEND exclude_srcs_list esp_matter_commissioner.cpp esp_matter_controller_pairing_command.cpp) +endif() + idf_component_register(SRC_DIRS ${src_dirs_list} + EXCLUDE_SRCS ${exclude_srcs_list} INCLUDE_DIRS ${include_dirs_list} REQUIRES chip esp_matter esp_matter_console json_parser) diff --git a/components/esp_matter_controller/Kconfig b/components/esp_matter_controller/Kconfig index efc49ab31..776f07a64 100644 --- a/components/esp_matter_controller/Kconfig +++ b/components/esp_matter_controller/Kconfig @@ -6,11 +6,18 @@ menu "ESP Matter Controller" help Enable the controller. - config ESP_MATTER_CONTROLLER_MAX_ACTIVE_DEVICES - int "Max active device" + config ESP_MATTER_COMMISSIONER_ENABLE + bool "Enable matter commissioner" depends on ESP_MATTER_CONTROLLER_ENABLE + default y + help + Enable the matter commissioner in the ESP Matter controller. + + config ESP_MATTER_COMMISSIONER_MAX_ACTIVE_DEVICES + int "Max active device" + depends on ESP_MATTER_COMMISSIONER_ENABLE default 5 help - Maximum number of active device the controller supports. + Maximum number of active device the commissioner supports. endmenu diff --git a/components/esp_matter_controller/esp_matter_commissioner.h b/components/esp_matter_controller/esp_matter_commissioner.h index 5576980ad..183693cdf 100644 --- a/components/esp_matter_controller/esp_matter_commissioner.h +++ b/components/esp_matter_controller/esp_matter_commissioner.h @@ -22,6 +22,7 @@ #include #include +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE using chip::NodeId; using chip::Controller::AutoCommissioner; using chip::Controller::DeviceCommissioner; @@ -39,3 +40,4 @@ CommissionerDiscoveryController *get_discovery_controller(); AutoCommissioner *get_auto_commissioner(); } // namespace commissioner } // namespace esp_matter +#endif // CONFIG_ESP_MATTER_COMMISSIONER_ENABLE diff --git a/components/esp_matter_controller/esp_matter_controller_cluster_command.cpp b/components/esp_matter_controller/esp_matter_controller_cluster_command.cpp index 727b03279..688666abb 100644 --- a/components/esp_matter_controller/esp_matter_controller_cluster_command.cpp +++ b/components/esp_matter_controller/esp_matter_controller_cluster_command.cpp @@ -13,7 +13,11 @@ // limitations under the License. #include +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE #include +#else +#include +#endif #include using namespace chip::app::Clusters; @@ -201,11 +205,18 @@ void cluster_command::on_device_connection_failure_fcn(void *context, const Scop esp_err_t cluster_command::send_command() { +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE if (CHIP_NO_ERROR == commissioner::get_device_commissioner()->GetConnectedDevice(m_node_id, &on_device_connected_cb, &on_device_connection_failure_cb)) { return ESP_OK; } +#else + chip::Server *server = &(chip::Server::GetInstance()); + server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(m_node_id, /* fabric index */ 1), + &on_device_connected_cb, &on_device_connection_failure_cb); + return ESP_OK; +#endif chip::Platform::Delete(this); return ESP_FAIL; } diff --git a/components/esp_matter_controller/esp_matter_controller_console.cpp b/components/esp_matter_controller/esp_matter_controller_console.cpp index 14db3a85a..aaa8b8815 100644 --- a/components/esp_matter_controller/esp_matter_controller_console.cpp +++ b/components/esp_matter_controller/esp_matter_controller_console.cpp @@ -15,7 +15,6 @@ * limitations under the License. */ -#include #include #include #include @@ -186,6 +185,7 @@ esp_err_t controller_register_commands() .description = "print this page", .handler = controller_help_handler, }, +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE { .name = "pairing", .description = "Pairing a node. " @@ -194,6 +194,7 @@ esp_err_t controller_register_commands() "controller pairing ble-thread [nodeid] [pincode] [discriminator] [dataset]", .handler = controller_pairing_handler, }, +#endif { .name = "invoke-cmd", .description = "Send command to the nodes. " diff --git a/components/esp_matter_controller/esp_matter_controller_read_command.cpp b/components/esp_matter_controller/esp_matter_controller_read_command.cpp index 980ee58a3..2b604e94a 100644 --- a/components/esp_matter_controller/esp_matter_controller_read_command.cpp +++ b/components/esp_matter_controller/esp_matter_controller_read_command.cpp @@ -14,7 +14,11 @@ #include #include +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE #include +#else +#include +#endif #include #include "DataModelLogger.h" @@ -74,11 +78,19 @@ void read_command::on_device_connection_failure_fcn(void *context, const ScopedN esp_err_t read_command::send_command() { +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE if (CHIP_NO_ERROR == commissioner::get_device_commissioner()->GetConnectedDevice(m_node_id, &on_device_connected_cb, &on_device_connection_failure_cb)) { return ESP_OK; } +#else + chip::Server *server = &(chip::Server::GetInstance()); + server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(m_node_id, /* fabric index */ 1), + &on_device_connected_cb, &on_device_connection_failure_cb); + return ESP_OK; +#endif + chip::Platform::Delete(this); return ESP_FAIL; } diff --git a/components/esp_matter_controller/esp_matter_controller_subscribe_command.cpp b/components/esp_matter_controller/esp_matter_controller_subscribe_command.cpp index 8bb011753..87668df1b 100644 --- a/components/esp_matter_controller/esp_matter_controller_subscribe_command.cpp +++ b/components/esp_matter_controller/esp_matter_controller_subscribe_command.cpp @@ -14,7 +14,11 @@ #include #include +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE #include +#else +#include +#endif #include #include "DataModelLogger.h" @@ -78,11 +82,18 @@ void subscribe_command::on_device_connection_failure_fcn(void *context, const Sc esp_err_t subscribe_command::send_command() { +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE if (CHIP_NO_ERROR == commissioner::get_device_commissioner()->GetConnectedDevice(m_node_id, &on_device_connected_cb, &on_device_connection_failure_cb)) { return ESP_OK; } +#else + chip::Server *server = &(chip::Server::GetInstance()); + server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(m_node_id, /* fabric index */ 1), + &on_device_connected_cb, &on_device_connection_failure_cb); + return ESP_OK; +#endif chip::Platform::Delete(this); return ESP_FAIL; } @@ -183,7 +194,11 @@ esp_err_t send_shutdown_subscription(uint64_t node_id, uint32_t subscription_id) { if (CHIP_NO_ERROR != InteractionModelEngine::GetInstance()->ShutdownSubscription( +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE ScopedNodeId(node_id, commissioner::get_device_commissioner()->GetFabricIndex()), subscription_id)) { +#else + ScopedNodeId(node_id, /* fabric index */ 1), subscription_id)) { +#endif ESP_LOGE(TAG, "Shutdown Subscription Failed"); return ESP_FAIL; } diff --git a/components/esp_matter_controller/esp_matter_controller_write_command.cpp b/components/esp_matter_controller/esp_matter_controller_write_command.cpp index 8d910d4bf..0771047af 100644 --- a/components/esp_matter_controller/esp_matter_controller_write_command.cpp +++ b/components/esp_matter_controller/esp_matter_controller_write_command.cpp @@ -13,10 +13,14 @@ // limitations under the License. #include -#include #include #include #include +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE +#include +#else +#include +#endif using namespace chip::app::Clusters; using chip::ByteSpan; @@ -69,11 +73,18 @@ void write_command::on_device_connection_failure_fcn(void *context, const Sco template esp_err_t write_command::send_command() { +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE if (CHIP_NO_ERROR == commissioner::get_device_commissioner()->GetConnectedDevice(m_node_id, &on_device_connected_cb, &on_device_connection_failure_cb)) { return ESP_OK; } +#else + chip::Server *server = &(chip::Server::GetInstance()); + server->GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(m_node_id, /* fabric index */ 1), + &on_device_connected_cb, &on_device_connection_failure_cb); + return ESP_OK; +#endif chip::Platform::Delete(this); return ESP_FAIL; } diff --git a/examples/controller/CMakeLists.txt b/examples/controller/CMakeLists.txt index 4c7fd7db5..10ab0b461 100644 --- a/examples/controller/CMakeLists.txt +++ b/examples/controller/CMakeLists.txt @@ -38,9 +38,9 @@ project(controller) idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++14;-Os;-DLWIP_IPV6_SCOPES=0;-DCHIP_HAVE_CONFIG_H" APPEND) idf_build_set_property(C_COMPILE_OPTIONS "-Os;-DLWIP_IPV6_SCOPES=0" APPEND) -if (CONFIG_ESP_MATTER_CONTROLLER_ENABLE) - idf_build_set_property(CXX_COMPILE_OPTIONS "-DCHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY=1;-DCHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES=${CONFIG_ESP_MATTER_CONTROLLER_MAX_ACTIVE_DEVICES}" APPEND) - idf_build_set_property(C_COMPILE_OPTIONS "-DCHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY=1;-DCHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES=${CONFIG_ESP_MATTER_CONTROLLER_MAX_ACTIVE_DEVICES}" APPEND) +if (CONFIG_ESP_MATTER_COMMISSIONER_ENABLE) + idf_build_set_property(CXX_COMPILE_OPTIONS "-DCHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY=1;-DCHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES=${CONFIG_ESP_MATTER_COMMISSIONER_MAX_ACTIVE_DEVICES}" APPEND) + idf_build_set_property(C_COMPILE_OPTIONS "-DCHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY=1;-DCHIP_CONFIG_CONTROLLER_MAX_ACTIVE_DEVICES=${CONFIG_ESP_MATTER_COMMISSIONER_MAX_ACTIVE_DEVICES}" APPEND) endif() # For RISCV chips, project_include.cmake sets -Wno-format, but does not clear various diff --git a/examples/controller/main/app_main.cpp b/examples/controller/main/app_main.cpp index 560eaa8f4..1af0ffdac 100644 --- a/examples/controller/main/app_main.cpp +++ b/examples/controller/main/app_main.cpp @@ -30,7 +30,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) { switch (event->Type) { case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kInterfaceIpAddressChanged: - ESP_LOGI(TAG, "Interface gets IP Address"); + ESP_LOGI(TAG, "Interface IP Address changed"); break; default: @@ -44,19 +44,28 @@ extern "C" void app_main() /* Initialize the ESP NVS layer */ nvs_flash_init(); +#if !CONFIG_ESP_MATTER_COMMISSIONER_ENABLE + // If there is no commissioner in the controller, we need a default node so that the controller can be commissioned + // to a specific fabric. + node::config_t node_config; + node_t *node = node::create(&node_config, NULL, NULL); +#endif /* Matter start */ err = esp_matter::start(app_event_cb); if (err != ESP_OK) { ESP_LOGE(TAG, "Matter start failed: %d", err); } - #if CONFIG_ENABLE_CHIP_SHELL esp_matter::console::diagnostics_register_commands(); esp_matter::console::init(); +#if CONFIG_ESP_MATTER_CONTROLLER_ENABLE +#if CONFIG_ESP_MATTER_COMMISSIONER_ENABLE esp_matter::lock::chip_stack_lock(portMAX_DELAY); esp_matter::commissioner::init(5580); esp_matter::lock::chip_stack_unlock(); +#endif // CONFIG_ESP_MATTER_COMMISSIONER_ENABLE esp_matter::console::controller_register_commands(); -#endif +#endif // CONFIG_ESP_MATTER_CONTROLLER_ENABLE +#endif // CONFIG_ENABLE_CHIP_SHELL } diff --git a/examples/controller/sdkconfig.defaults b/examples/controller/sdkconfig.defaults index 52d253f1a..abb53c66d 100644 --- a/examples/controller/sdkconfig.defaults +++ b/examples/controller/sdkconfig.defaults @@ -36,5 +36,6 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=10240 # Increase udp endpoints num for commissioner CONFIG_NUM_UDP_ENDPOINTS=16 -# Enable Commissioner +# Enable Controller and commissioner CONFIG_ESP_MATTER_CONTROLLER_ENABLE=y +CONFIG_ESP_MATTER_COMMISSIONER_ENABLE=y