Skip to content

Commit

Permalink
handle setting empty strings
Browse files Browse the repository at this point in the history
Fixes #802
  • Loading branch information
shubhamdp committed Apr 8, 2024
1 parent d592c3f commit 8943861
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 14 additions & 4 deletions components/esp_matter/esp_matter_attribute_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions components/esp_matter/private/esp_matter_nvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8943861

Please sign in to comment.