From 8943861e30b6f96c17dfc3a8986f517977dc0451 Mon Sep 17 00:00:00 2001 From: Shubham Patil <shubham.patil@espressif.com> Date: Mon, 8 Apr 2024 19:41:07 +0530 Subject: [PATCH] handle setting empty strings Fixes https://github.com/espressif/esp-matter/issues/802 --- .../esp_matter/esp_matter_attribute_utils.cpp | 18 ++++++++++++++---- .../esp_matter/private/esp_matter_nvs.cpp | 4 ++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/components/esp_matter/esp_matter_attribute_utils.cpp b/components/esp_matter/esp_matter_attribute_utils.cpp index edc70c707..a36226ad4 100644 --- a/components/esp_matter/esp_matter_attribute_utils.cpp +++ b/components/esp_matter/esp_matter_attribute_utils.cpp @@ -1271,7 +1271,10 @@ esp_err_t get_data_from_attr_val(esp_matter_attr_val_t *val, EmberAfAttributeTyp if (attribute_type) { *attribute_type = ZCL_CHAR_STRING_ATTRIBUTE_TYPE; } - size_t string_len = strnlen((const char *)val->val.a.b, val->val.a.s); + size_t string_len = 0; + if (val->val.a.b) { + string_len = strnlen((const char *)val->val.a.b, val->val.a.s); + } size_t data_size_len = val->val.a.t - val->val.a.s; if (string_len >= UINT8_MAX || data_size_len != 1) { return ESP_ERR_INVALID_ARG; @@ -1298,7 +1301,10 @@ esp_err_t get_data_from_attr_val(esp_matter_attr_val_t *val, EmberAfAttributeTyp if (attribute_type) { *attribute_type = ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE; } - size_t string_len = strnlen((const char *)val->val.a.b, val->val.a.s); + size_t string_len = 0; + if (val->val.a.b) { + string_len = strnlen((const char *)val->val.a.b, val->val.a.s); + } size_t data_size_len = val->val.a.t - val->val.a.s; if (string_len >= UINT8_MAX || data_size_len != 2) { return ESP_ERR_INVALID_ARG; @@ -1926,11 +1932,15 @@ void val_print(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_id, ESP_LOGI(TAG, "********** %c : Endpoint 0x%04" PRIX16 "'s Cluster 0x%08" PRIX32 "'s Attribute 0x%08" PRIX32 " is %" PRIu64 " **********", action, endpoint_id, cluster_id, attribute_id, val->val.u64); } else if (val->type == ESP_MATTER_VAL_TYPE_CHAR_STRING) { + const char *b = val->val.a.b ? (const char *)val->val.a.b : "(empty)"; + uint16_t s = val->val.a.b ? s : strlen("(empty)"); ESP_LOGI(TAG, "********** %c : Endpoint 0x%04" PRIX16 "'s Cluster 0x%08" PRIX32 "'s Attribute 0x%08" PRIX32 " is %.*s **********", action, - endpoint_id, cluster_id, attribute_id, val->val.a.s, val->val.a.b); + endpoint_id, cluster_id, attribute_id, s, b); } else if (val->type == ESP_MATTER_VAL_TYPE_LONG_CHAR_STRING) { + const char *b = val->val.a.b ? (const char *)val->val.a.b : "(empty)"; + uint16_t s = val->val.a.b ? s : strlen("(empty)"); ESP_LOGI(TAG, "********** %c : Endpoint 0x%04" PRIX16 "'s Cluster 0x%08" PRIX32 "'s Attribute 0x%08" PRIX32 " is %.*s **********", action, - endpoint_id, cluster_id, attribute_id, val->val.a.s, val->val.a.b); + endpoint_id, cluster_id, attribute_id, s, b); } else { ESP_LOGI(TAG, "********** %c : Endpoint 0x%04" PRIX16 "'s Cluster 0x%08" PRIX32 "'s Attribute 0x%08" PRIX32 " is <invalid type: %d> **********", action, endpoint_id, cluster_id, attribute_id, val->type); diff --git a/components/esp_matter/private/esp_matter_nvs.cpp b/components/esp_matter/private/esp_matter_nvs.cpp index 1597b6662..de5bc7fd5 100644 --- a/components/esp_matter/private/esp_matter_nvs.cpp +++ b/components/esp_matter/private/esp_matter_nvs.cpp @@ -203,10 +203,10 @@ static esp_err_t nvs_store_val(const char *nvs_namespace, const char *attribute_ /* Store only if value is not NULL */ if (val.val.a.b) { err = nvs_set_blob(handle, attribute_key, val.val.a.b, val.val.a.s); - nvs_commit(handle); } else { - err = ESP_OK; + err = nvs_erase_key(handle, attribute_key); } + nvs_commit(handle); } else { // Handling how to store attributes in NVS based on config option. #if CONFIG_ESP_MATTER_NVS_USE_COMPACT_ATTR_STORAGE