diff --git a/components/esp_matter/esp_matter_attribute.cpp b/components/esp_matter/esp_matter_attribute.cpp index cf4102f8b..2053e798b 100644 --- a/components/esp_matter/esp_matter_attribute.cpp +++ b/components/esp_matter/esp_matter_attribute.cpp @@ -1743,5 +1743,66 @@ attribute_t *create_supported_calendar_types(cluster_t *cluster, uint8_t *value, } /* attribute */ } /* time_format_localization */ +namespace illuminance_measurement { +namespace attribute { + +attribute_t *create_illuminance_measured_value(cluster_t *cluster, nullable value, nullable min, nullable max) +{ + attribute_t *attribute = esp_matter::attribute::create(cluster, IlluminanceMeasurement::Attributes::MeasuredValue::Id, ATTRIBUTE_FLAG_NULLABLE, esp_matter_nullable_uint16(value)); + if (!attribute) { + ESP_LOGE(TAG, "Could not create attribute"); + return NULL; + } + esp_matter::attribute::add_bounds(attribute, esp_matter_nullable_uint16(min), esp_matter_nullable_uint16(max)); + return attribute; +} + +attribute_t *create_illuminance_min_measured_value(cluster_t *cluster, nullable value, nullable min, nullable max) +{ + attribute_t *attribute = esp_matter::attribute::create(cluster, IlluminanceMeasurement::Attributes::MinMeasuredValue::Id, ATTRIBUTE_FLAG_NULLABLE, esp_matter_nullable_uint16(value)); + if (!attribute) { + ESP_LOGE(TAG, "Could not create attribute"); + return NULL; + } + esp_matter::attribute::add_bounds(attribute, esp_matter_nullable_uint16(min), esp_matter_nullable_uint16(max)); + return attribute; +} + +attribute_t *create_illuminance_max_measured_value(cluster_t *cluster, nullable value, nullable min, nullable max) +{ + attribute_t *attribute = esp_matter::attribute::create(cluster, IlluminanceMeasurement::Attributes::MaxMeasuredValue::Id, ATTRIBUTE_FLAG_NULLABLE, esp_matter_nullable_uint16(value)); + if (!attribute) { + ESP_LOGE(TAG, "Could not create attribute"); + return NULL; + } + esp_matter::attribute::add_bounds(attribute, esp_matter_nullable_uint16(min), esp_matter_nullable_uint16(max)); + return attribute; +} + +attribute_t *create_illuminance_tolerance(cluster_t *cluster, uint16_t value, uint16_t min, uint16_t max) +{ + attribute_t *attribute = esp_matter::attribute::create(cluster, IlluminanceMeasurement::Attributes::Tolerance::Id, ATTRIBUTE_FLAG_NONE, esp_matter_uint16(value)); + if (!attribute) { + ESP_LOGE(TAG, "Could not create attribute"); + return NULL; + } + esp_matter::attribute::add_bounds(attribute, esp_matter_uint16(min), esp_matter_uint16(max)); + return attribute; +} + +attribute_t *create_illuminance_light_sensor_type(cluster_t *cluster, nullable value, nullable min, nullable max) +{ + attribute_t *attribute = esp_matter::attribute::create(cluster, IlluminanceMeasurement::Attributes::LightSensorType::Id, ATTRIBUTE_FLAG_NULLABLE, esp_matter_nullable_enum8(value)); + if (!attribute) { + ESP_LOGE(TAG, "Could not create attribute"); + return NULL; + } + esp_matter::attribute::add_bounds(attribute, esp_matter_nullable_enum8(min), esp_matter_nullable_enum8(max)); + return attribute; +} + +} /* attribute */ +} /* illuminance_measurement */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_attribute.h b/components/esp_matter/esp_matter_attribute.h index 957ac9e08..588fd6581 100644 --- a/components/esp_matter/esp_matter_attribute.h +++ b/components/esp_matter/esp_matter_attribute.h @@ -433,5 +433,15 @@ attribute_t *create_supported_calendar_types(cluster_t *cluster, uint8_t *value, } /* attribute */ } /* time_format_localization */ +namespace illuminance_measurement { +namespace attribute { +attribute_t *create_illuminance_measured_value(cluster_t *cluster, nullable value, nullable min, nullable max); +attribute_t *create_illuminance_min_measured_value(cluster_t *cluster, nullable value, nullable min, nullable max); +attribute_t *create_illuminance_max_measured_value(cluster_t *cluster, nullable value, nullable min, nullable max); +attribute_t *create_illuminance_tolerance(cluster_t *cluster, uint16_t value, uint16_t min, uint16_t max); +attribute_t *create_illuminance_light_sensor_type(cluster_t *cluster, nullable value, nullable min, nullable max); +} /* attribute */ +} /* illuminance_measurement */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_cluster.cpp b/components/esp_matter/esp_matter_cluster.cpp index 977bb6a7e..d1324ecd8 100644 --- a/components/esp_matter/esp_matter_cluster.cpp +++ b/components/esp_matter/esp_matter_cluster.cpp @@ -1645,8 +1645,45 @@ cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags, uint32_ return cluster; } - } /* time_format_localization */ +namespace illuminance_measurement { +const function_generic_t *function_list = NULL; +const int function_flags = CLUSTER_FLAG_NONE; + +cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags) +{ + cluster_t *cluster = cluster::create(endpoint, IlluminanceMeasurement::Id, flags); + if (!cluster) { + ESP_LOGE(TAG, "Could not create cluster"); + return NULL; + } + if (flags & CLUSTER_FLAG_SERVER) { + set_plugin_server_init_callback(cluster, MatterIlluminanceMeasurementPluginServerInitCallback); + add_function_list(cluster, function_list, function_flags); + } + if (flags & CLUSTER_FLAG_CLIENT) { + set_plugin_client_init_callback(cluster, MatterIlluminanceMeasurementPluginClientInitCallback); + create_default_binding_cluster(endpoint); + } + + if (flags & CLUSTER_FLAG_SERVER) { + /* Attributes managed internally */ + global::attribute::create_feature_map(cluster, 0); + /** Attributes not managed internally **/ + if (config) { + global::attribute::create_cluster_revision(cluster, config->cluster_revision); + attribute::create_illuminance_measured_value(cluster, config->illuminance_measured_value, 0x0000, 0xFFFF); + attribute::create_illuminance_min_measured_value(cluster, config->illuminance_min_measured_value, 0x0001, 0xFFFD); + attribute::create_illuminance_max_measured_value(cluster, config->illuminance_max_measured_value, 0x0002, 0xFFFE); + } else { + ESP_LOGE(TAG, "Config is NULL. Cannot add some attributes."); + } + } + + return cluster; +} +} /* illuminance_measurement */ + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_cluster.h b/components/esp_matter/esp_matter_cluster.h index 32b33b741..289b8b38f 100644 --- a/components/esp_matter/esp_matter_cluster.h +++ b/components/esp_matter/esp_matter_cluster.h @@ -414,5 +414,17 @@ typedef struct config { cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags, uint32_t features); } /* time_format_localization */ +namespace illuminance_measurement { +typedef struct config { + uint16_t cluster_revision; + nullable illuminance_measured_value; + nullable illuminance_min_measured_value; + nullable illuminance_max_measured_value; + config() : cluster_revision(3), illuminance_measured_value(0), illuminance_min_measured_value(1), illuminance_max_measured_value(2) {} +} config_t; + +cluster_t *create(endpoint_t *endpoint, config_t *config, uint8_t flags); +} + } /* cluster */ } /* esp_matter */ diff --git a/components/esp_matter/esp_matter_endpoint.cpp b/components/esp_matter/esp_matter_endpoint.cpp index 6bad23dd3..8d9bc7750 100644 --- a/components/esp_matter/esp_matter_endpoint.cpp +++ b/components/esp_matter/esp_matter_endpoint.cpp @@ -741,6 +741,40 @@ endpoint_t *add(endpoint_t *endpoint, config_t *config) return endpoint; } } /* contact_sensor */ + +namespace light_sensor { +uint32_t get_device_type_id() +{ + return ESP_MATTER_LIGHT_SENSOR_DEVICE_TYPE_ID; +} + +uint8_t get_device_type_version() +{ + return ESP_MATTER_LIGHT_SENSOR_DEVICE_TYPE_VERSION; +} + +endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data) +{ + endpoint_t *endpoint = endpoint::create(node, flags, priv_data); + return add(endpoint, config); +} + +endpoint_t *add(endpoint_t *endpoint, config_t *config) +{ + if (!endpoint) { + ESP_LOGE(TAG, "Endpoint cannot be NULL"); + return NULL; + } + add_device_type(endpoint, get_device_type_id(), get_device_type_version()); + + descriptor::create(endpoint, CLUSTER_FLAG_SERVER); + identify::create(endpoint, &(config->identify), CLUSTER_FLAG_SERVER); + illuminance_measurement::create(endpoint, &(config->illuminance_measurement), CLUSTER_FLAG_SERVER); + + return endpoint; +} +} /* light_sensor */ + } /* endpoint */ namespace node { diff --git a/components/esp_matter/esp_matter_endpoint.h b/components/esp_matter/esp_matter_endpoint.h index 295424a8f..4ba4ca000 100644 --- a/components/esp_matter/esp_matter_endpoint.h +++ b/components/esp_matter/esp_matter_endpoint.h @@ -55,6 +55,8 @@ #define ESP_MATTER_OCCUPANCY_SENSOR_DEVICE_TYPE_VERSION 2 #define ESP_MATTER_CONTACT_SENSOR_DEVICE_TYPE_ID 0x0015 #define ESP_MATTER_CONTACT_SENSOR_DEVICE_TYPE_VERSION 1 +#define ESP_MATTER_LIGHT_SENSOR_DEVICE_TYPE_ID 0x0106 +#define ESP_MATTER_LIGHT_SENSOR_DEVICE_TYPE_VERSION 2 #define ESP_MATTER_FAN_DEVICE_TYPE_ID 0x002B #define ESP_MATTER_FAN_DEVICE_TYPE_VERSION 1 @@ -340,6 +342,18 @@ endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_dat endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* contact_sensor */ +namespace light_sensor { +typedef struct config { + cluster::identify::config_t identify; + cluster::illuminance_measurement::config_t illuminance_measurement; +} config_t; + +uint32_t get_device_type_id(); +uint8_t get_device_type_version(); +endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); +} /* light_sensor */ + } /* endpoint */ namespace node {