From 3c451a73a507bab84ec1195c0e73eb5d873b40a9 Mon Sep 17 00:00:00 2001 From: Li Ya Shuai Date: Tue, 6 Feb 2024 11:13:21 +0800 Subject: [PATCH] fix node label read failure issue --- .../esp_matter/esp_matter_attribute_utils.cpp | 74 +++++++++++-------- components/esp_matter/esp_matter_core.cpp | 8 ++ 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/components/esp_matter/esp_matter_attribute_utils.cpp b/components/esp_matter/esp_matter_attribute_utils.cpp index 3964fac55..95b630254 100644 --- a/components/esp_matter/esp_matter_attribute_utils.cpp +++ b/components/esp_matter/esp_matter_attribute_utils.cpp @@ -1239,42 +1239,56 @@ esp_err_t get_data_from_attr_val(esp_matter_attr_val_t *val, EmberAfAttributeTyp break; case ESP_MATTER_VAL_TYPE_CHAR_STRING: - if (attribute_type) { - *attribute_type = ZCL_CHAR_STRING_ATTRIBUTE_TYPE; - } - if (attribute_size) { - *attribute_size = val->val.a.t; - } - if (value) { - int data_size_len = val->val.a.t - val->val.a.s; - memcpy(value, (uint8_t *)&val->val.a.s, data_size_len); - if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST) - { - ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer." - "Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST"); - return ESP_FAIL; + { + 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 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; + } + uint8_t data_size = string_len; + if (attribute_size) { + *attribute_size = string_len + data_size_len; + } + if (value) { + memcpy(value, (uint8_t *)&data_size, data_size_len); + if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST) + { + ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer." + "Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST"); + return ESP_FAIL; + } + memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); } - memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); } break; case ESP_MATTER_VAL_TYPE_LONG_CHAR_STRING: - if (attribute_type) { - *attribute_type = ZCL_LONG_CHAR_STRING_ATTRIBUTE_TYPE; - } - if (attribute_size) { - *attribute_size = val->val.a.t; - } - if (value) { - int data_size_len = val->val.a.t - val->val.a.s; - memcpy(value, (uint8_t *)&val->val.a.s, data_size_len); - if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST) - { - ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer." - "Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST"); - return ESP_FAIL; + { + 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 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; + } + uint16_t data_size = string_len; + if (attribute_size) { + *attribute_size = string_len + data_size_len; + } + if (value) { + memcpy(value, (uint8_t *)&data_size, data_size_len); + if (*attribute_size > CONFIG_ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST) + { + ESP_LOGE(TAG, "Attribute buffer not enough, cannot copy the data to the attribute buffer." + "Please configure the buffer size through menuconfig ESP_MATTER_ATTRIBUTE_BUFFER_LARGEST"); + return ESP_FAIL; + } + memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); } - memcpy((value + data_size_len), (uint8_t *)val->val.a.b, (*attribute_size - data_size_len)); } break; diff --git a/components/esp_matter/esp_matter_core.cpp b/components/esp_matter/esp_matter_core.cpp index c8076a79a..9a1b05bf3 100644 --- a/components/esp_matter/esp_matter_core.cpp +++ b/components/esp_matter/esp_matter_core.cpp @@ -607,6 +607,14 @@ esp_err_t enable(endpoint_t *endpoint) attribute::get_data_from_attr_val(&attribute->val, &matter_attributes[attribute_index].attributeType, &matter_attributes[attribute_index].size, NULL); + /* The length is not fixed for string attribute, so set it to the max size (32) to avoid overflow issue + * when writing a longer string. + */ + if (attribute->val.type == ESP_MATTER_VAL_TYPE_CHAR_STRING || + attribute->val.type == ESP_MATTER_VAL_TYPE_LONG_CHAR_STRING) { + matter_attributes[attribute_index].size = attribute->val.val.a.s; + } + matter_clusters[cluster_index].clusterSize += matter_attributes[attribute_index].size; attribute = attribute->next; attribute_index++;