From dec7e3cf7f97a736307cee299373f58ef4bdc242 Mon Sep 17 00:00:00 2001 From: Chirag Atal Date: Tue, 18 Jan 2022 15:17:28 +0530 Subject: [PATCH] matter_command: Add callback support for custom commands. --- components/esp_matter/esp_matter_command.cpp | 27 ++++++++++++++++---- components/esp_matter/esp_matter_command.h | 5 ++++ components/esp_matter/esp_matter_core.cpp | 10 ++++++++ components/esp_matter/esp_matter_core.h | 2 ++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/components/esp_matter/esp_matter_command.cpp b/components/esp_matter/esp_matter_command.cpp index cd7fdebb5..9bf84d558 100644 --- a/components/esp_matter/esp_matter_command.cpp +++ b/components/esp_matter/esp_matter_command.cpp @@ -34,6 +34,16 @@ using chip::TLV::TLVReader; static const char *TAG = "esp_matter_command"; +static esp_matter_command_custom_callback_t custom_callback = NULL; +static void *custom_callback_priv_data = NULL; + +esp_err_t esp_matter_command_set_custom_callback(esp_matter_command_custom_callback_t callback, void *priv_data) +{ + custom_callback = callback; + custom_callback_priv_data = priv_data; + return ESP_OK; +} + void DispatchSingleClusterCommandCommon(const ConcreteCommandPath &command_path, TLVReader &tlv_data, void *command_obj) { int endpoint_id = command_path.mEndpointId; @@ -49,8 +59,15 @@ void DispatchSingleClusterCommandCommon(const ConcreteCommandPath &command_path, ESP_LOGE(TAG, "Command 0x%04X not found", command_id); return; } - esp_matter_command_callback_t callback = esp_matter_command_get_callback(command); - callback(command_obj, command_path, tlv_data); + int flags = esp_matter_command_get_flags(command); + if (flags & COMMAND_MASK_CUSTOM) { + if (custom_callback) { + custom_callback(endpoint_id, cluster_id, command_id, tlv_data, custom_callback_priv_data); + } + } else { + esp_matter_command_callback_t callback = esp_matter_command_get_callback(command); + callback(command_obj, command_path, tlv_data); + } } namespace chip { @@ -781,7 +798,7 @@ void esp_matter_command_callback_key_set_read_all_indices_response(void *command } } - emberAfGroupKeyManagementClusterKeySetReadAllIndicesResponseCallback(command_path.mEndpointId, + emberAfGroupKeyManagementClusterKeySetReadAllIndicesResponseCallback(command_path.mEndpointId, (CommandSender *)command_obj, group_key_set_indices); */ @@ -939,7 +956,7 @@ void esp_matter_command_callback_network_config_response(void *command_obj, cons } } - emberAfNetworkCommissioningClusterNetworkConfigResponseCallback(command_path.mEndpointId, + emberAfNetworkCommissioningClusterNetworkConfigResponseCallback(command_path.mEndpointId, (CommandSender *)command_obj, networking_status, debug_text); } @@ -1176,7 +1193,7 @@ void esp_matter_command_callback_apply_update_response(void *command_obj, const } } - emberAfOtaSoftwareUpdateProviderClusterApplyUpdateResponseCallback(command_path.mEndpointId, + emberAfOtaSoftwareUpdateProviderClusterApplyUpdateResponseCallback(command_path.mEndpointId, (CommandSender *)command_obj, action, delayed_action_time); } diff --git a/components/esp_matter/esp_matter_command.h b/components/esp_matter/esp_matter_command.h index 0163603f3..0436599f2 100644 --- a/components/esp_matter/esp_matter_command.h +++ b/components/esp_matter/esp_matter_command.h @@ -19,6 +19,11 @@ using chip::app::ConcreteCommandPath; using chip::TLV::TLVReader; +#define COMMAND_MASK_CUSTOM 0x80 + +typedef esp_err_t (*esp_matter_command_custom_callback_t)(int endpoint_id, int cluster_id, int command_id, + TLVReader &tlv_data, void *priv_data); + typedef void (*esp_matter_command_callback_t)(void *command_obj, const ConcreteCommandPath &command_path, TLVReader &tlv_data); diff --git a/components/esp_matter/esp_matter_core.cpp b/components/esp_matter/esp_matter_core.cpp index 8e7cfdf5d..ce131b50e 100644 --- a/components/esp_matter/esp_matter_core.cpp +++ b/components/esp_matter/esp_matter_core.cpp @@ -474,6 +474,16 @@ esp_matter_command_callback_t esp_matter_command_get_callback(esp_matter_command return current_command->callback; } +int esp_matter_command_get_flags(esp_matter_command_t *command) +{ + if (!command) { + ESP_LOGE(TAG, "Command cannot be NULL"); + return 0; + } + _esp_matter_command_t *current_command = (_esp_matter_command_t *)command; + return current_command->flags; +} + esp_matter_attribute_t *esp_matter_attribute_create(esp_matter_cluster_t *cluster, int attribute_id, uint8_t flags, esp_matter_attr_val_t val) { diff --git a/components/esp_matter/esp_matter_core.h b/components/esp_matter/esp_matter_core.h index 13aa4720b..00acd9bf5 100644 --- a/components/esp_matter/esp_matter_core.h +++ b/components/esp_matter/esp_matter_core.h @@ -89,3 +89,5 @@ esp_matter_command_t *esp_matter_command_get_first(esp_matter_cluster_t *cluster esp_matter_command_t *esp_matter_command_get_next(esp_matter_command_t *command); int esp_matter_command_get_id(esp_matter_command_t *command); esp_matter_command_callback_t esp_matter_command_get_callback(esp_matter_command_t *command); +int esp_matter_command_get_flags(esp_matter_command_t *command); +esp_err_t esp_matter_command_set_custom_callback(esp_matter_command_custom_callback_t callback, void *priv_data);