Skip to content

Commit

Permalink
Merge branch 'esp_matter/utils/parse_uint64_large_num' into 'main'
Browse files Browse the repository at this point in the history
components/esp_matter: fix json parsing for large num

See merge request app-frameworks/esp-matter!908
  • Loading branch information
chshu committed Nov 5, 2024
2 parents 988a5d1 + d512236 commit af3da26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
24 changes: 18 additions & 6 deletions components/esp_matter/utils/json_to_tlv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,15 @@ static esp_err_t encode_tlv_element(const cJSON *val, TLV::TLVWriter &writer, co
break;
}
case TLVElementType::Int64: {
ESP_RETURN_ON_FALSE(val->type == cJSON_Number, ESP_ERR_INVALID_ARG, TAG, "Invalid type");
int64_t int64_val =
(val->valueint < INT32_MAX && val->valueint > INT32_MIN) ? val->valueint : (int64_t)val->valuedouble;
ESP_RETURN_ON_FALSE(val->type == cJSON_Number || val->type == cJSON_String, ESP_ERR_INVALID_ARG, TAG,
"Invalid type");
int64_t int64_val = 0;
if (val->type == cJSON_Number) {
int64_val =
(val->valueint < INT32_MAX && val->valueint > INT32_MIN) ? val->valueint : (int64_t)val->valuedouble;
} else {
int64_val = strtoll(val->valuestring, nullptr, 10);
}
ESP_RETURN_ON_FALSE(writer.Put(tag, int64_val) == CHIP_NO_ERROR, ESP_FAIL, TAG, "Failed to encode");
break;
}
Expand Down Expand Up @@ -288,9 +294,15 @@ static esp_err_t encode_tlv_element(const cJSON *val, TLV::TLVWriter &writer, co
break;
}
case TLVElementType::UInt64: {
ESP_RETURN_ON_FALSE(val->type == cJSON_Number, ESP_ERR_INVALID_ARG, TAG, "Invalid type");
ESP_RETURN_ON_FALSE(val->valueint >= 0, ESP_ERR_INVALID_ARG, TAG, "Invalid range");
uint64_t uint64_val = val->valueint < INT32_MAX ? val->valueint : (uint64_t)val->valuedouble;
ESP_RETURN_ON_FALSE(val->type == cJSON_Number || val->type == cJSON_String, ESP_ERR_INVALID_ARG, TAG,
"Invalid type");
uint64_t uint64_val = 0;
if (val->type == cJSON_Number) {
ESP_RETURN_ON_FALSE(val->valueint >= 0, ESP_ERR_INVALID_ARG, TAG, "Invalid range");
uint64_val = val->valueint < INT32_MAX ? val->valueint : (uint64_t)val->valuedouble;
} else {
uint64_val = strtoull(val->valuestring, nullptr, 10);
}
ESP_RETURN_ON_FALSE(writer.Put(tag, uint64_val) == CHIP_NO_ERROR, ESP_FAIL, TAG, "Failed to encode");
break;
}
Expand Down
5 changes: 5 additions & 0 deletions docs/en/developing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,11 @@ For ACL attribute of AccessControl cluster, you should use the following JSON st

matter esp controller write-attr <node_id> <endpoint_id> 31 0 "{\"0:ARR-OBJ\":[{\"1:U8\": 5, \"2:U8\": 2, \"3:ARR-U64\": [112233], \"4:NULL\": null}, {\"1:U8\": 4, \"2:U8\": 3, \"3:ARR-U64\": [1], \"4:NULL\": null}]}"

For attributes of type uint64_t or int64_t, if the absolute value is greater than (2^53), you should use string to represent number in JSON structure for precision
::

matter esp controller write-attr <node_id> <endpoint_id> 42 0 "{\"0:ARR-OBJ\":[{\"1:U64\": \"9007199254740993\", \"2:U8\": 0}]}"

2.10.6 Subscribe commands
~~~~~~~~~~~~~~~~~~~~~~~~~
The ``subscribe_command`` class is used for sending subscribe commands to other end-devices. Its constructor function could accept four callback inputs:
Expand Down

0 comments on commit af3da26

Please sign in to comment.