diff --git a/components/esp_matter/esp_matter_attribute.cpp b/components/esp_matter/esp_matter_attribute.cpp index cefb8f5d7..81ffc1b06 100644 --- a/components/esp_matter/esp_matter_attribute.cpp +++ b/components/esp_matter/esp_matter_attribute.cpp @@ -582,6 +582,17 @@ esp_err_t esp_matter_attribute_callback_set(esp_matter_attribute_callback_t call return ESP_OK; } +esp_err_t esp_matter_attribute_get_val_raw(int endpoint_id, int cluster_id, int attribute_id, uint8_t *value, + uint16_t attribute_size) +{ + EmberAfStatus status = emberAfReadServerAttribute(endpoint_id, cluster_id, attribute_id, value, attribute_size); + if (status != EMBER_ZCL_STATUS_SUCCESS) { + ESP_LOGE(TAG, "Error getting raw value from matter"); + return ESP_FAIL; + } + return ESP_OK; +} + esp_err_t esp_matter_attribute_update(int endpoint_id, int cluster_id, int attribute_id, esp_matter_attr_val_t val) { EmberAfAttributeType attribute_type = 0; diff --git a/components/esp_matter/esp_matter_core.cpp b/components/esp_matter/esp_matter_core.cpp index 701e4d28a..8e7cfdf5d 100644 --- a/components/esp_matter/esp_matter_core.cpp +++ b/components/esp_matter/esp_matter_core.cpp @@ -174,7 +174,8 @@ esp_err_t esp_matter_endpoint_enable(esp_matter_endpoint_t *endpoint) static esp_err_t esp_matter_endpoint_enable_all() { if (!node) { - return ESP_FAIL; + /* Not returning error, since the node will not be initialized for application using the data model from zap */ + return ESP_OK; } _esp_matter_endpoint_t *endpoint = node->endpoint_list; @@ -246,7 +247,6 @@ esp_err_t esp_matter_start(esp_matter_event_callback_t callback) if (err != ESP_OK) { ESP_LOGE(TAG, "Error initializing matter"); } - return err; } diff --git a/components/esp_matter/esp_matter_core.h b/components/esp_matter/esp_matter_core.h index 07da45f00..13aa4720b 100644 --- a/components/esp_matter/esp_matter_core.h +++ b/components/esp_matter/esp_matter_core.h @@ -78,6 +78,8 @@ int esp_matter_attribute_get_id(esp_matter_attribute_t *attribute); /** Attribute val APIs */ esp_err_t esp_matter_attribute_set_val(esp_matter_attribute_t *attribute, esp_matter_attr_val_t val); esp_matter_attr_val_t esp_matter_attribute_get_val(esp_matter_attribute_t *attribute); +esp_err_t esp_matter_attribute_get_val_raw(int endpoint_id, int cluster_id, int attribute_id, uint8_t *value, + uint16_t attribute_size); /** Command APIs */ esp_matter_command_t *esp_matter_command_create(esp_matter_cluster_t *cluster, int command_id, uint8_t flags, diff --git a/examples/light/main/app_driver.cpp b/examples/light/main/app_driver.cpp index 62e7d8492..1f556eeae 100644 --- a/examples/light/main/app_driver.cpp +++ b/examples/light/main/app_driver.cpp @@ -37,7 +37,7 @@ static esp_err_t app_driver_console_handler(int argc, char **argv) int attribute_id = strtol((const char *)&argv[3][2], NULL, 16); int value = atoi(argv[4]); - esp_matter_attr_val_t val = esp_matter_int(value); + esp_matter_attr_val_t val = esp_matter_uint8(value); /* Change val if bool */ if (cluster_id == ZCL_ON_OFF_CLUSTER_ID && attribute_id == ZCL_ON_OFF_ATTRIBUTE_ID) { @@ -84,19 +84,19 @@ static esp_err_t app_driver_light_set_power(esp_matter_attr_val_t val) static esp_err_t app_driver_light_set_brightness(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS); return light_driver_set_brightness(value); } static esp_err_t app_driver_light_set_hue(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_HUE, STANDARD_HUE); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_HUE, STANDARD_HUE); return light_driver_set_hue(value); } static esp_err_t app_driver_light_set_saturation(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_SATURATION, STANDARD_SATURATION); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_SATURATION, STANDARD_SATURATION); return light_driver_set_saturation(value); } @@ -123,9 +123,35 @@ esp_err_t app_driver_attribute_update(int endpoint_id, int cluster_id, int attri return err; } +static esp_err_t app_driver_attribute_set_defaults() +{ + /* Get the default value (current value) from esp_matter and update the app_driver */ + esp_err_t err = ESP_OK; + esp_matter_node_t *node = esp_matter_node_get(); + esp_matter_endpoint_t *endpoint = esp_matter_endpoint_get_first(node); + while (endpoint) { + int endpoint_id = esp_matter_endpoint_get_id(endpoint); + esp_matter_cluster_t *cluster = esp_matter_cluster_get_first(endpoint); + while (cluster) { + int cluster_id = esp_matter_cluster_get_id(cluster); + esp_matter_attribute_t *attribute = esp_matter_attribute_get_first(cluster); + while (attribute) { + int attribute_id = esp_matter_attribute_get_id(attribute); + esp_matter_attr_val_t val = esp_matter_attribute_get_val(attribute); + err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, val); + attribute = esp_matter_attribute_get_next(attribute); + } + cluster = esp_matter_cluster_get_next(cluster); + } + endpoint = esp_matter_endpoint_get_next(endpoint); + } + return err; +} + esp_err_t app_driver_init() { device_init(); + app_driver_attribute_set_defaults(); app_driver_register_commands(); return ESP_OK; } diff --git a/examples/rainmaker_light/main/app_driver.cpp b/examples/rainmaker_light/main/app_driver.cpp index 62e7d8492..1f556eeae 100644 --- a/examples/rainmaker_light/main/app_driver.cpp +++ b/examples/rainmaker_light/main/app_driver.cpp @@ -37,7 +37,7 @@ static esp_err_t app_driver_console_handler(int argc, char **argv) int attribute_id = strtol((const char *)&argv[3][2], NULL, 16); int value = atoi(argv[4]); - esp_matter_attr_val_t val = esp_matter_int(value); + esp_matter_attr_val_t val = esp_matter_uint8(value); /* Change val if bool */ if (cluster_id == ZCL_ON_OFF_CLUSTER_ID && attribute_id == ZCL_ON_OFF_ATTRIBUTE_ID) { @@ -84,19 +84,19 @@ static esp_err_t app_driver_light_set_power(esp_matter_attr_val_t val) static esp_err_t app_driver_light_set_brightness(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS); return light_driver_set_brightness(value); } static esp_err_t app_driver_light_set_hue(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_HUE, STANDARD_HUE); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_HUE, STANDARD_HUE); return light_driver_set_hue(value); } static esp_err_t app_driver_light_set_saturation(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_SATURATION, STANDARD_SATURATION); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_SATURATION, STANDARD_SATURATION); return light_driver_set_saturation(value); } @@ -123,9 +123,35 @@ esp_err_t app_driver_attribute_update(int endpoint_id, int cluster_id, int attri return err; } +static esp_err_t app_driver_attribute_set_defaults() +{ + /* Get the default value (current value) from esp_matter and update the app_driver */ + esp_err_t err = ESP_OK; + esp_matter_node_t *node = esp_matter_node_get(); + esp_matter_endpoint_t *endpoint = esp_matter_endpoint_get_first(node); + while (endpoint) { + int endpoint_id = esp_matter_endpoint_get_id(endpoint); + esp_matter_cluster_t *cluster = esp_matter_cluster_get_first(endpoint); + while (cluster) { + int cluster_id = esp_matter_cluster_get_id(cluster); + esp_matter_attribute_t *attribute = esp_matter_attribute_get_first(cluster); + while (attribute) { + int attribute_id = esp_matter_attribute_get_id(attribute); + esp_matter_attr_val_t val = esp_matter_attribute_get_val(attribute); + err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, val); + attribute = esp_matter_attribute_get_next(attribute); + } + cluster = esp_matter_cluster_get_next(cluster); + } + endpoint = esp_matter_endpoint_get_next(endpoint); + } + return err; +} + esp_err_t app_driver_init() { device_init(); + app_driver_attribute_set_defaults(); app_driver_register_commands(); return ESP_OK; } diff --git a/examples/zap_light/main/app_driver.cpp b/examples/zap_light/main/app_driver.cpp index 080eaf3d3..60b1e79e0 100644 --- a/examples/zap_light/main/app_driver.cpp +++ b/examples/zap_light/main/app_driver.cpp @@ -63,7 +63,7 @@ static esp_err_t app_driver_console_handler(int argc, char **argv) int attribute_id = strtol((const char *)&argv[3][2], NULL, 16); int value = atoi(argv[4]); - esp_matter_attr_val_t val = esp_matter_int(value); + esp_matter_attr_val_t val = esp_matter_uint8(value); /* Change val if bool */ if (cluster_id == ZCL_ON_OFF_CLUSTER_ID && attribute_id == ZCL_ON_OFF_ATTRIBUTE_ID) { @@ -77,7 +77,7 @@ static esp_err_t app_driver_console_handler(int argc, char **argv) int attribute_id = strtol((const char *)&argv[3][2], NULL, 16); int value = app_driver_light_get_attribute(endpoint_id, cluster_id, attribute_id); - esp_matter_attr_val_t val = esp_matter_int(value); + esp_matter_attr_val_t val = esp_matter_uint8(value); /* Change val if bool */ if (cluster_id == ZCL_ON_OFF_CLUSTER_ID && attribute_id == ZCL_ON_OFF_ATTRIBUTE_ID) { @@ -113,19 +113,19 @@ static esp_err_t app_driver_light_set_power(esp_matter_attr_val_t val) static esp_err_t app_driver_light_set_brightness(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_BRIGHTNESS, STANDARD_BRIGHTNESS); return light_driver_set_brightness(value); } static esp_err_t app_driver_light_set_hue(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_HUE, STANDARD_HUE); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_HUE, STANDARD_HUE); return light_driver_set_hue(value); } static esp_err_t app_driver_light_set_saturation(esp_matter_attr_val_t val) { - int value = REMAP_TO_RANGE(val.val.i, MATTER_SATURATION, STANDARD_SATURATION); + int value = REMAP_TO_RANGE(val.val.u8, MATTER_SATURATION, STANDARD_SATURATION); return light_driver_set_saturation(value); } @@ -152,6 +152,49 @@ esp_err_t app_driver_attribute_update(int endpoint_id, int cluster_id, int attri return err; } +esp_err_t app_driver_attribute_set_defaults() +{ + /* When using static endpoints, i.e. using the data model from zap-generated, this needs to be done + after esp_matter_start() */ + /* Get the default value (current value) from matter submodule and update the app_driver */ + esp_err_t err = ESP_OK; + uint8_t value; + int endpoint_id = 0; + int cluster_id = 0; + int attribute_id = 0; + esp_matter_attr_val_t val = esp_matter_invalid(NULL); + + endpoint_id = ESP_MATTER_COLOR_DIMMABLE_LIGHT_ENDPOINT_ID; + cluster_id = ZCL_ON_OFF_CLUSTER_ID; + attribute_id = ZCL_ON_OFF_ATTRIBUTE_ID; + esp_matter_attribute_get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); + val = esp_matter_bool(value); + err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, val); + + endpoint_id = ESP_MATTER_COLOR_DIMMABLE_LIGHT_ENDPOINT_ID; + cluster_id = ZCL_LEVEL_CONTROL_CLUSTER_ID; + attribute_id = ZCL_CURRENT_LEVEL_ATTRIBUTE_ID; + esp_matter_attribute_get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); + val = esp_matter_uint8(value); + err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, val); + + endpoint_id = ESP_MATTER_COLOR_DIMMABLE_LIGHT_ENDPOINT_ID; + cluster_id = ZCL_COLOR_CONTROL_CLUSTER_ID; + attribute_id = ZCL_COLOR_CONTROL_CURRENT_HUE_ATTRIBUTE_ID; + esp_matter_attribute_get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); + val = esp_matter_uint8(value); + err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, val); + + endpoint_id = ESP_MATTER_COLOR_DIMMABLE_LIGHT_ENDPOINT_ID; + cluster_id = ZCL_COLOR_CONTROL_CLUSTER_ID; + attribute_id = ZCL_COLOR_CONTROL_CURRENT_SATURATION_ATTRIBUTE_ID; + esp_matter_attribute_get_val_raw(endpoint_id, cluster_id, attribute_id, &value, sizeof(uint8_t)); + val = esp_matter_uint8(value); + err |= app_driver_attribute_update(endpoint_id, cluster_id, attribute_id, val); + + return err; +} + esp_err_t app_driver_init() { device_init(); diff --git a/examples/zap_light/main/app_driver.h b/examples/zap_light/main/app_driver.h index e8656e837..05b5a0290 100644 --- a/examples/zap_light/main/app_driver.h +++ b/examples/zap_light/main/app_driver.h @@ -26,6 +26,8 @@ esp_err_t app_driver_init(void); esp_err_t app_driver_attribute_update(int endpoint_id, int cluster_id, int attribute_id, esp_matter_attr_val_t val); +esp_err_t app_driver_attribute_set_defaults(); + #ifdef __cplusplus } #endif diff --git a/examples/zap_light/main/app_main.cpp b/examples/zap_light/main/app_main.cpp index 8c0b6e1f7..11c17519a 100644 --- a/examples/zap_light/main/app_main.cpp +++ b/examples/zap_light/main/app_main.cpp @@ -64,6 +64,8 @@ extern "C" void app_main() } app_qrcode_print(); + app_driver_attribute_set_defaults(); + #if CONFIG_ENABLE_CHIP_SHELL esp_matter_console_diagnostics_register_commands(); esp_matter_console_init();