Skip to content

Commit

Permalink
Merge branch 'use-id-based-getter' into 'main'
Browse files Browse the repository at this point in the history
Use id based getter APIs to get endpoint, cluster, and attribute handle

See merge request app-frameworks/esp-matter!1012
  • Loading branch information
dhrishi committed Jan 29, 2025
2 parents 68a489b + 9d11f25 commit b92d52a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 68 deletions.
19 changes: 5 additions & 14 deletions components/esp_matter/esp_matter_attribute_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2104,10 +2104,7 @@ esp_err_t report(uint16_t endpoint_id, uint32_t cluster_id, uint32_t attribute_i
VerifyOrReturnError(lock_status != lock::FAILED, ESP_FAIL, ESP_LOGE(TAG, "Could not get task context"));

/* Get attribute */
node_t *node = node::get();
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = cluster::get(endpoint, cluster_id);
attribute_t *attribute = attribute::get(cluster, attribute_id);
attribute_t *attribute = attribute::get(endpoint_id, cluster_id, attribute_id);
if (!attribute) {
ESP_LOGE(TAG, "Could not find Endpoint 0x%04" PRIX16 "'s Cluster 0x%08" PRIX32 "'s Attribute 0x%08" PRIX32, endpoint_id, cluster_id,
attribute_id);
Expand Down Expand Up @@ -2183,11 +2180,8 @@ Status emberAfExternalAttributeReadCallback(EndpointId endpoint_id, ClusterId cl
{
/* Get value */
uint32_t attribute_id = matter_attribute->attributeId;
node_t *node = node::get();
VerifyOrReturnError(node, Status::Failure);
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = cluster::get(endpoint, cluster_id);
attribute_t *attribute = attribute::get(cluster, attribute_id);
attribute_t *attribute = attribute::get(endpoint_id, cluster_id, attribute_id);
VerifyOrReturnError(attribute, Status::Failure);
esp_matter_attr_val_t val = esp_matter_invalid(NULL);

int flags = attribute::get_flags(attribute);
Expand Down Expand Up @@ -2218,11 +2212,8 @@ Status emberAfExternalAttributeWriteCallback(EndpointId endpoint_id, ClusterId c
{
/* Get value */
uint32_t attribute_id = matter_attribute->attributeId;
node_t *node = node::get();
VerifyOrReturnError(node, Status::Failure);
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = cluster::get(endpoint, cluster_id);
attribute_t *attribute = attribute::get(cluster, attribute_id);
attribute_t *attribute = attribute::get(endpoint_id, cluster_id, attribute_id);
VerifyOrReturnError(attribute, Status::Failure);

/* Get val */
/* This creates a new variable val, and stores the new attribute value in the new variable.
Expand Down
15 changes: 3 additions & 12 deletions components/esp_matter/esp_matter_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,6 @@ static int get_next_index()

static esp_err_t disable(endpoint_t *endpoint)
{
VerifyOrReturnError(endpoint, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Endpoint cannot be NULL"));

/* Take lock if not already taken */
lock::status_t lock_status = lock::chip_stack_lock(portMAX_DELAY);

Expand Down Expand Up @@ -1912,20 +1910,15 @@ esp_err_t set_parent_endpoint(endpoint_t *endpoint, endpoint_t *parent_endpoint)

void *get_priv_data(uint16_t endpoint_id)
{
node_t *node = node::get();
/* This is not an error, since the node will not be initialized for application using the data model from zap */
VerifyOrReturnValue(node, NULL, ESP_LOGE(TAG, "Node not found"));
endpoint_t *endpoint = get(node, endpoint_id);
endpoint_t *endpoint = get(endpoint_id);
VerifyOrReturnValue(endpoint, NULL, ESP_LOGE(TAG, "Endpoint not found"));
_endpoint_t *current_endpoint = (_endpoint_t *)endpoint;
return current_endpoint->priv_data;
}

esp_err_t set_priv_data(uint16_t endpoint_id, void *priv_data)
{
node_t *node = node::get();
VerifyOrReturnError(node, ESP_ERR_INVALID_STATE, ESP_LOGE(TAG, "Node is not initialized"));
endpoint_t *endpoint = get(node, endpoint_id);
endpoint_t *endpoint = get(endpoint_id);
VerifyOrReturnError(endpoint, ESP_ERR_NOT_FOUND, ESP_LOGE(TAG, "Endpoint not found"));
_endpoint_t *current_endpoint = (_endpoint_t *)endpoint;
current_endpoint->priv_data = priv_data;
Expand All @@ -1934,9 +1927,7 @@ esp_err_t set_priv_data(uint16_t endpoint_id, void *priv_data)

esp_err_t set_identify(uint16_t endpoint_id, void *identify)
{
node_t *node = node::get();
VerifyOrReturnError(node, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Node not found"));
endpoint_t *endpoint = get(node, endpoint_id);
endpoint_t *endpoint = get(endpoint_id);
VerifyOrReturnError(endpoint, ESP_ERR_INVALID_ARG, ESP_LOGE(TAG, "Endpoint not found"));
_endpoint_t *current_endpoint = (_endpoint_t *)endpoint;
current_endpoint->identify = (Identify *)identify;
Expand Down
15 changes: 4 additions & 11 deletions components/esp_matter/esp_matter_delegate_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ namespace cluster {

static uint32_t get_feature_map_value(uint16_t endpoint_id, uint32_t cluster_id)
{
node_t *node = node::get();
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = cluster::get(endpoint, cluster_id);
uint32_t attribute_id = Globals::Attributes::FeatureMap::Id;
attribute_t *attribute = attribute::get(cluster, attribute_id);
attribute_t *attribute = attribute::get(endpoint_id, cluster_id, attribute_id);

esp_matter_attr_val_t val = esp_matter_invalid(NULL);
attribute::get_val(attribute, &val);
Expand Down Expand Up @@ -130,11 +127,9 @@ void EnergyEvseDelegateInitCB(void *delegate, uint16_t endpoint_id)
void MicrowaveOvenControlDelegateInitCB(void *delegate, uint16_t endpoint_id)
{
// Get delegates of MicrowaveOvenMode and OperationalState clusters.
node_t *node = node::get();
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = cluster::get(endpoint, MicrowaveOvenMode::Id);
cluster_t *cluster = cluster::get(endpoint_id, MicrowaveOvenMode::Id);
ModeBase::Delegate *microwave_oven_mode_delegate = static_cast<ModeBase::Delegate*>(get_delegate_impl(cluster));
cluster = cluster::get(endpoint, OperationalState::Id);
cluster = cluster::get(endpoint_id, OperationalState::Id);
OperationalState::Delegate *operational_state_delegate = static_cast<OperationalState::Delegate*>(get_delegate_impl(cluster));
VerifyOrReturn(delegate != nullptr && microwave_oven_mode_delegate != nullptr && operational_state_delegate != nullptr);
// Create instances of clusters.
Expand Down Expand Up @@ -304,10 +299,8 @@ void ModeSelectDelegateInitCB(void *delegate, uint16_t endpoint_id)
void ThreadBorderRouterManagementDelegateInitCB(void *delegate, uint16_t endpoint_id)
{
assert(delegate != nullptr);
esp_matter::cluster_t *cluster = esp_matter::cluster::get(endpoint_id, ThreadBorderRouterManagement::Id);
assert(cluster != nullptr);
/* Get the attribute */
attribute_t *attribute = attribute::get(cluster, Globals::Attributes::FeatureMap::Id);
attribute_t *attribute = attribute::get(endpoint_id, ThreadBorderRouterManagement::Id, Globals::Attributes::FeatureMap::Id);
assert(attribute != nullptr);
/* Update the value if the attribute already exists */
esp_matter_attr_val_t val = esp_matter_invalid(NULL);
Expand Down
3 changes: 1 addition & 2 deletions components/esp_matter_rainmaker/esp_matter_rainmaker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,7 @@ static esp_err_t sign_data_command_callback(const ConcreteCommandPath &command_p
static esp_err_t custom_cluster_create()
{
/* Get the endpoint */
node_t *node = node::get();
endpoint_t *endpoint = endpoint::get(node, cluster::rainmaker::endpoint_id);
endpoint_t *endpoint = endpoint::get(cluster::rainmaker::endpoint_id);

/* Create custom rainmaker cluster */
cluster_t *cluster = esp_matter::cluster::create(endpoint, cluster::rainmaker::Id, CLUSTER_FLAG_SERVER);
Expand Down
10 changes: 4 additions & 6 deletions examples/light/main/app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,16 +180,14 @@ extern "C" void app_main()
ESP_LOGI(TAG, "Light created with endpoint_id %d", light_endpoint_id);

/* Mark deferred persistence for some attributes that might be changed rapidly */
cluster_t *level_control_cluster = cluster::get(endpoint, LevelControl::Id);
attribute_t *current_level_attribute = attribute::get(level_control_cluster, LevelControl::Attributes::CurrentLevel::Id);
attribute_t *current_level_attribute = attribute::get(light_endpoint_id, LevelControl::Id, LevelControl::Attributes::CurrentLevel::Id);
attribute::set_deferred_persistence(current_level_attribute);

cluster_t *color_control_cluster = cluster::get(endpoint, ColorControl::Id);
attribute_t *current_x_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::CurrentX::Id);
attribute_t *current_x_attribute = attribute::get(light_endpoint_id, ColorControl::Id, ColorControl::Attributes::CurrentX::Id);
attribute::set_deferred_persistence(current_x_attribute);
attribute_t *current_y_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::CurrentY::Id);
attribute_t *current_y_attribute = attribute::get(light_endpoint_id, ColorControl::Id, ColorControl::Attributes::CurrentY::Id);
attribute::set_deferred_persistence(current_y_attribute);
attribute_t *color_temp_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::ColorTemperatureMireds::Id);
attribute_t *color_temp_attribute = attribute::get(light_endpoint_id, ColorControl::Id, ColorControl::Attributes::ColorTemperatureMireds::Id);
attribute::set_deferred_persistence(color_temp_attribute);

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD && CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
Expand Down
25 changes: 8 additions & 17 deletions examples/light_wifi_prov/main/app_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ static void app_driver_button_toggle_cb(void *arg, void *data)
uint32_t cluster_id = OnOff::Id;
uint32_t attribute_id = OnOff::Attributes::OnOff::Id;

node_t *node = node::get();
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = cluster::get(endpoint, cluster_id);
attribute_t *attribute = attribute::get(cluster, attribute_id);
attribute_t *attribute = attribute::get(endpoint_id, cluster_id, attribute_id);

esp_matter_attr_val_t val = esp_matter_invalid(NULL);
attribute::get_val(attribute, &val);
Expand Down Expand Up @@ -139,43 +136,37 @@ esp_err_t app_driver_light_set_defaults(uint16_t endpoint_id)
esp_err_t err = ESP_OK;
void *priv_data = endpoint::get_priv_data(endpoint_id);
led_indicator_handle_t handle = (led_indicator_handle_t)priv_data;
node_t *node = node::get();
endpoint_t *endpoint = endpoint::get(node, endpoint_id);
cluster_t *cluster = NULL;
attribute_t *attribute = NULL;
esp_matter_attr_val_t val = esp_matter_invalid(NULL);

/* Setting brightness */
cluster = cluster::get(endpoint, LevelControl::Id);
attribute = attribute::get(cluster, LevelControl::Attributes::CurrentLevel::Id);
attribute = attribute::get(endpoint_id, LevelControl::Id, LevelControl::Attributes::CurrentLevel::Id);
attribute::get_val(attribute, &val);
err |= app_driver_light_set_brightness(handle, &val);

/* Setting color */
cluster = cluster::get(endpoint, ColorControl::Id);
attribute = attribute::get(cluster, ColorControl::Attributes::ColorMode::Id);
attribute = attribute::get(endpoint_id, ColorControl::Id, ColorControl::Attributes::ColorMode::Id);
attribute::get_val(attribute, &val);
if (val.val.u8 == (uint8_t)ColorControl::ColorMode::kCurrentHueAndCurrentSaturation) {
/* Setting hue */
attribute = attribute::get(cluster, ColorControl::Attributes::CurrentHue::Id);
attribute = attribute::get(endpoint_id, ColorControl::Id, ColorControl::Attributes::CurrentHue::Id);
attribute::get_val(attribute, &val);
err |= app_driver_light_set_hue(handle, &val);
/* Setting saturation */
attribute = attribute::get(cluster, ColorControl::Attributes::CurrentSaturation::Id);
attribute = attribute::get(endpoint_id, ColorControl::Id, ColorControl::Attributes::CurrentSaturation::Id);
attribute::get_val(attribute, &val);
err |= app_driver_light_set_saturation(handle, &val);
} else if (val.val.u8 == (uint8_t)ColorControl::ColorMode::kColorTemperature) {
/* Setting temperature */
attribute = attribute::get(cluster, ColorControl::Attributes::ColorTemperatureMireds::Id);
attribute = attribute::get(endpoint_id, ColorControl::Id, ColorControl::Attributes::ColorTemperatureMireds::Id);
attribute::get_val(attribute, &val);
err |= app_driver_light_set_temperature(handle, &val);
} else {
ESP_LOGE(TAG, "Color mode not supported");
}

/* Setting power */
cluster = cluster::get(endpoint, OnOff::Id);
attribute = attribute::get(cluster, OnOff::Attributes::OnOff::Id);
attribute = attribute::get(endpoint_id, OnOff::Id, OnOff::Attributes::OnOff::Id);
attribute::get_val(attribute, &val);
err |= app_driver_light_set_power(handle, &val);

Expand All @@ -189,7 +180,7 @@ app_driver_handle_t app_driver_light_init()
led_indicator_handle_t leds[CONFIG_BSP_LEDS_NUM];
ESP_ERROR_CHECK(bsp_led_indicator_create(leds, NULL, CONFIG_BSP_LEDS_NUM));
led_indicator_set_hsv(leds[0], SET_HSV(DEFAULT_HUE, DEFAULT_SATURATION, DEFAULT_BRIGHTNESS));

return (app_driver_handle_t)leds[0];
#else
return NULL;
Expand Down
10 changes: 4 additions & 6 deletions examples/light_wifi_prov/main/app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,18 +240,16 @@ extern "C" void app_main()
ESP_LOGI(TAG, "Light created with endpoint_id %d", light_endpoint_id);

/* Mark deferred persistence for some attributes that might be changed rapidly */
cluster_t *level_control_cluster = cluster::get(endpoint, LevelControl::Id);
attribute_t *current_level_attribute =
attribute::get(level_control_cluster, LevelControl::Attributes::CurrentLevel::Id);
attribute::get(light_endpoint_id, LevelControl::Id, LevelControl::Attributes::CurrentLevel::Id);
attribute::set_deferred_persistence(current_level_attribute);

cluster_t *color_control_cluster = cluster::get(endpoint, ColorControl::Id);
attribute_t *current_x_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::CurrentX::Id);
attribute_t *current_x_attribute = attribute::get(light_endpoint_id, ColorControl::Id, ColorControl::Attributes::CurrentX::Id);
attribute::set_deferred_persistence(current_x_attribute);
attribute_t *current_y_attribute = attribute::get(color_control_cluster, ColorControl::Attributes::CurrentY::Id);
attribute_t *current_y_attribute = attribute::get(light_endpoint_id, ColorControl::Id, ColorControl::Attributes::CurrentY::Id);
attribute::set_deferred_persistence(current_y_attribute);
attribute_t *color_temp_attribute =
attribute::get(color_control_cluster, ColorControl::Attributes::ColorTemperatureMireds::Id);
attribute::get(light_endpoint_id, ColorControl::Id, ColorControl::Attributes::ColorTemperatureMireds::Id);
attribute::set_deferred_persistence(color_temp_attribute);

err = esp_event_loop_create_default();
Expand Down

0 comments on commit b92d52a

Please sign in to comment.