From e20a3bb968fec39c19414de81f682b26ddc6208a Mon Sep 17 00:00:00 2001 From: chendejin Date: Fri, 29 Nov 2024 14:09:15 +0800 Subject: [PATCH] components/esp_matter_controller: support timed write --- .../commands/esp_matter_controller_write_command.cpp | 8 ++++---- .../commands/esp_matter_controller_write_command.h | 9 +++++++-- .../core/esp_matter_controller_console.cpp | 12 ++++++++++-- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/components/esp_matter_controller/commands/esp_matter_controller_write_command.cpp b/components/esp_matter_controller/commands/esp_matter_controller_write_command.cpp index 6731c8c49..b9723743a 100644 --- a/components/esp_matter_controller/commands/esp_matter_controller_write_command.cpp +++ b/components/esp_matter_controller/commands/esp_matter_controller_write_command.cpp @@ -47,7 +47,7 @@ void write_command::on_device_connected_fcn(void *context, ExchangeManager &exch write_command *cmd = (write_command *)context; chip::OperationalDeviceProxy device_proxy(&exchangeMgr, sessionHandle); esp_err_t err = interaction::write::send_request(&device_proxy, cmd->m_attr_path, cmd->m_attr_val, - cmd->m_chunked_callback, chip::NullOptional); + cmd->m_chunked_callback, cmd->m_timed_write_timeout_ms); if (err != ESP_OK) { chip::Platform::Delete(cmd); } @@ -88,14 +88,14 @@ esp_err_t write_command::send_command() } esp_err_t send_write_attr_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, - const char *attr_val_json_str) + const char *attr_val_json_str, chip::Optional timed_write_timeout_ms) { if (!attr_val_json_str) { ESP_LOGE(TAG, "attribute value json string cannot be NULL"); return ESP_ERR_INVALID_ARG; } - write_command *cmd = - chip::Platform::New(node_id, endpoint_id, cluster_id, attribute_id, attr_val_json_str); + write_command *cmd = chip::Platform::New(node_id, endpoint_id, cluster_id, attribute_id, + attr_val_json_str, timed_write_timeout_ms); if (!cmd) { ESP_LOGE(TAG, "Failed to alloc memory for cluster_command"); diff --git a/components/esp_matter_controller/commands/esp_matter_controller_write_command.h b/components/esp_matter_controller/commands/esp_matter_controller_write_command.h index 3e4ea5ab8..daf8649e1 100644 --- a/components/esp_matter_controller/commands/esp_matter_controller_write_command.h +++ b/components/esp_matter_controller/commands/esp_matter_controller_write_command.h @@ -40,11 +40,13 @@ class write_command : public WriteClient::Callback { public: /** Constructor for command with an attribute path**/ write_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, - const char *attribute_val_str) + const char *attribute_val_str, + const chip::Optional timed_write_timeout_ms = chip::NullOptional) : m_node_id(node_id) , m_attr_path(endpoint_id, cluster_id, attribute_id) , m_chunked_callback(this) , m_attr_val(attribute_val_str, custom_encodable_type::interaction_type::k_write_attr) + , m_timed_write_timeout_ms(timed_write_timeout_ms) , on_device_connected_cb(on_device_connected_fcn, this) , on_device_connection_failure_cb(on_device_connection_failure_fcn, this) {} @@ -77,6 +79,7 @@ private: AttributePathParams m_attr_path; ChunkedWriteCallback m_chunked_callback; custom_encodable_type m_attr_val; + chip::Optional m_timed_write_timeout_ms; static void on_device_connected_fcn(void *context, ExchangeManager &exchangeMgr, const SessionHandle &sessionHandle); @@ -94,12 +97,14 @@ private: * @param[in] attribute_id AttributeId * @param[in] attr_val_json_str Attribute value string with JSON format * (https://docs.espressif.com/projects/esp-matter/en/latest/esp32/developing.html#write-attribute-commands) + * @param[in] timed_write_timeout_ms Timeout in millisecond for timed-write attribute * * @return ESP_OK on success. * @return error in case of failure. */ esp_err_t send_write_attr_command(uint64_t node_id, uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, - const char *attr_val_json_str); + const char *attr_val_json_str, + chip::Optional timed_write_timeout_ms = chip::NullOptional); } // namespace controller } // namespace esp_matter diff --git a/components/esp_matter_controller/core/esp_matter_controller_console.cpp b/components/esp_matter_controller/core/esp_matter_controller_console.cpp index c0a76ec13..e7b5cf695 100644 --- a/components/esp_matter_controller/core/esp_matter_controller_console.cpp +++ b/components/esp_matter_controller/core/esp_matter_controller_console.cpp @@ -455,7 +455,7 @@ static esp_err_t controller_read_attr_handler(int argc, char **argv) static esp_err_t controller_write_attr_handler(int argc, char **argv) { - if (argc != 5) { + if (argc < 5) { return ESP_ERR_INVALID_ARG; } @@ -465,6 +465,14 @@ static esp_err_t controller_write_attr_handler(int argc, char **argv) uint32_t attribute_id = string_to_uint32(argv[3]); char *attribute_val_str = argv[4]; + if (argc > 5) { + uint16_t timed_write_timeout_ms = string_to_uint16(argv[5]); + if (timed_write_timeout_ms > 0) { + return controller::send_write_attr_command(node_id, endpoint_id, cluster_id, attribute_id, + attribute_val_str, chip::MakeOptional(timed_write_timeout_ms)); + } + } + return controller::send_write_attr_command(node_id, endpoint_id, cluster_id, attribute_id, attribute_val_str); } @@ -619,7 +627,7 @@ esp_err_t controller_register_commands() .name = "write-attr", .description = "Write attributes of the nodes.\n" - "\tUsage: controller write-attr \n" + "\tUsage: controller write-attr [timed_write_timeout_ms]\n" "\tNotes: attr-value should be a JSON object that contains the attribute value JSON item." "You can get the format of the attr-value from " "https://docs.espressif.com/projects/esp-matter/en/latest/esp32/"