From 1f0e2cff1b0913e587d526dd055ef3ae25e10383 Mon Sep 17 00:00:00 2001 From: WanqQixiang Date: Fri, 8 Sep 2023 17:52:42 +0800 Subject: [PATCH] controller: Provide APIs to send the unsupported commands and write the unsupported attributes --- .../esp_matter_controller_cluster_command.cpp | 29 ++++++++++++++----- .../esp_matter_controller_cluster_command.h | 22 ++++++++++++-- .../esp_matter_controller_write_command.cpp | 29 ++++++++++++++----- .../esp_matter_controller_write_command.h | 8 ++++- 4 files changed, 69 insertions(+), 19 deletions(-) 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 d377cddb0..229a4e3d4 100644 --- a/components/esp_matter_controller/esp_matter_controller_cluster_command.cpp +++ b/components/esp_matter_controller/esp_matter_controller_cluster_command.cpp @@ -371,30 +371,39 @@ static esp_err_t send_command(command_data_t *command_data, peer_device_t *remot } // namespace clusters +cluster_command_handler_t cluster_command::unsupported_cluster_command_handler = NULL; +cluster_group_command_handler_t cluster_command::unsupported_cluster_group_command_handler = NULL; + void cluster_command::on_device_connected_fcn(void *context, ExchangeManager &exchangeMgr, const SessionHandle &sessionHandle) { + esp_err_t err = ESP_OK; cluster_command *cmd = reinterpret_cast(context); chip::OperationalDeviceProxy device_proxy(&exchangeMgr, sessionHandle); switch (cmd->m_command_data->cluster_id) { case OnOff::Id: - clusters::on_off::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); + err = clusters::on_off::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); break; case LevelControl::Id: - clusters::level_control::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); + err = clusters::level_control::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); break; case ColorControl::Id: - clusters::color_control::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); + err = clusters::color_control::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); break; case GroupKeyManagement::Id: - clusters::group_key_management::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); + err = clusters::group_key_management::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); break; case Groups::Id: - clusters::groups::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); + err = clusters::groups::send_command(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); break; default: + err = ESP_ERR_NOT_SUPPORTED; break; } + + if (err == ESP_ERR_NOT_SUPPORTED && unsupported_cluster_command_handler) { + unsupported_cluster_command_handler(cmd->m_command_data, &device_proxy, cmd->m_endpoint_id); + } chip::Platform::Delete(cmd); return; } @@ -408,16 +417,22 @@ void cluster_command::on_device_connection_failure_fcn(void *context, const Scop esp_err_t cluster_command::dispatch_group_command(void *context) { + esp_err_t err = ESP_OK; cluster_command *cmd = reinterpret_cast(context); uint16_t group_id = cmd->m_destination_id & 0xFFFF; switch (cmd->m_command_data->cluster_id) { case OnOff::Id: - return clusters::on_off::send_group_command(cmd->m_command_data, group_id); + err = clusters::on_off::send_group_command(cmd->m_command_data, group_id); break; default: + err = ESP_ERR_NOT_SUPPORTED; break; } - return ESP_ERR_NOT_SUPPORTED; + if (err == ESP_ERR_NOT_SUPPORTED && unsupported_cluster_group_command_handler) { + err = unsupported_cluster_group_command_handler(cmd->m_command_data, group_id); + } + chip::Platform::Delete(cmd); + return err; } esp_err_t cluster_command::send_command() diff --git a/components/esp_matter_controller/esp_matter_controller_cluster_command.h b/components/esp_matter_controller/esp_matter_controller_cluster_command.h index d9c2d7701..0fe24d94d 100644 --- a/components/esp_matter_controller/esp_matter_controller_cluster_command.h +++ b/components/esp_matter_controller/esp_matter_controller_cluster_command.h @@ -36,6 +36,11 @@ typedef struct command_data { char command_data_str[k_max_command_data_size][k_max_command_data_str_len]; } command_data_t; +typedef esp_err_t (*cluster_command_handler_t)(command_data_t *command_data, chip::OperationalDeviceProxy *device_proxy, + uint16_t endpoint_id); + +typedef esp_err_t (*cluster_group_command_handler_t)(command_data_t *command_data, uint16_t group_id); + class cluster_command { public: cluster_command(uint64_t destination_id, uint16_t endpoint_id, command_data_t *command_data) @@ -56,16 +61,27 @@ public: esp_err_t send_command(); - bool is_group_command() { - return chip::IsGroupId(m_destination_id); + bool is_group_command() { return chip::IsGroupId(m_destination_id); } + + static void set_unsupported_cluster_command_handler(cluster_command_handler_t handler) + { + unsupported_cluster_command_handler = handler; + } + + static void set_unsupported_cluster_group_command_handler(cluster_group_command_handler_t handler) + { + unsupported_cluster_group_command_handler = handler; } private: uint64_t m_destination_id; uint16_t m_endpoint_id; command_data_t *m_command_data; + static cluster_command_handler_t unsupported_cluster_command_handler; + static cluster_group_command_handler_t unsupported_cluster_group_command_handler; - static void on_device_connected_fcn(void *context, ExchangeManager &exchangeMgr, const SessionHandle &sessionHandle); + static void on_device_connected_fcn(void *context, ExchangeManager &exchangeMgr, + const SessionHandle &sessionHandle); static void on_device_connection_failure_fcn(void *context, const ScopedNodeId &peerId, CHIP_ERROR error); static esp_err_t dispatch_group_command(void *context); 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 6471aba60..d44a1d502 100644 --- a/components/esp_matter_controller/esp_matter_controller_write_command.cpp +++ b/components/esp_matter_controller/esp_matter_controller_write_command.cpp @@ -537,32 +537,45 @@ static esp_err_t write_attribute(uint64_t node_id, uint16_t endpoint_id, uint32_ } // namespace clusters +static attribute_write_handler_t s_unsupported_attribute_write_handler = NULL; + +void set_unsupported_attribute_write_handler(attribute_write_handler_t handler) +{ + s_unsupported_attribute_write_handler = handler; +} + esp_err_t send_write_attr_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, char *attribute_val_str) { + esp_err_t err = ESP_OK; switch (cluster_id) { case OnOff::Id: - return clusters::on_off::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); + err = clusters::on_off::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); break; case LevelControl::Id: - return clusters::level_control::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); + err = clusters::level_control::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); break; case ColorControl::Id: - return clusters::color_control::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); + err = clusters::color_control::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); break; case AccessControl::Id: - return clusters::access_control::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); + err = clusters::access_control::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); break; case Binding::Id: - return clusters::binding::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); + err = clusters::binding::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); break; case GroupKeyManagement::Id: - return clusters::group_key_management::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); + err = clusters::group_key_management::write_attribute(node_id, endpoint_id, attribute_id, attribute_val_str); break; default: - return ESP_ERR_NOT_SUPPORTED; + err = ESP_ERR_NOT_SUPPORTED; } - return ESP_ERR_NOT_SUPPORTED; + + if (err == ESP_ERR_NOT_SUPPORTED && s_unsupported_attribute_write_handler) { + err = s_unsupported_attribute_write_handler(node_id, endpoint_id, cluster_id, attribute_id, attribute_val_str); + } + + return err; } } // namespace controller diff --git a/components/esp_matter_controller/esp_matter_controller_write_command.h b/components/esp_matter_controller/esp_matter_controller_write_command.h index 0472fbd3b..0ed99d671 100644 --- a/components/esp_matter_controller/esp_matter_controller_write_command.h +++ b/components/esp_matter_controller/esp_matter_controller_write_command.h @@ -31,6 +31,9 @@ using chip::app::WriteClient; using chip::Messaging::ExchangeManager; using esp_matter::client::peer_device_t; +typedef esp_err_t (*attribute_write_handler_t)(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, + uint32_t attribute_id, char *attribute_val_str); + template class write_command : public WriteClient::Callback { public: @@ -100,13 +103,16 @@ private: attribute_free_handler m_attr_free; void *m_attr_free_ctx; - static void on_device_connected_fcn(void *context, ExchangeManager &exchangeMgr, const SessionHandle &sessionHandle); + static void on_device_connected_fcn(void *context, ExchangeManager &exchangeMgr, + const SessionHandle &sessionHandle); static void on_device_connection_failure_fcn(void *context, const ScopedNodeId &peerId, CHIP_ERROR error); chip::Callback::Callback on_device_connected_cb; chip::Callback::Callback on_device_connection_failure_cb; }; +void set_unsupported_attribute_write_handler(attribute_write_handler_t handler); + esp_err_t send_write_attr_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, char *attribute_val_str);