Modular v1

This commit is contained in:
2026-01-15 23:11:17 +00:00
parent 2b9642ebd2
commit 675c6aadcd
8 changed files with 783 additions and 39 deletions
+53 -33
View File
@@ -23,6 +23,8 @@
#include "zcl_utility.h"
#include "esp_zb_switch.h"
#include "led_indicator.h"
#include "device_config.h"
#include "device_registry.h"
typedef struct light_bulb_device_params_s {
esp_zb_ieee_addr_t ieee_addr;
@@ -59,15 +61,29 @@ static void zb_buttons_handler(switch_func_pair_t *button_func_pair)
static esp_err_t deferred_driver_init(void)
{
/* Inicializa o LED RGB */
/* Get device configuration */
const device_type_t *device_config = device_get_config();
#if FEATURE_LED_INDICATOR
/* Initialize LED RGB */
led_indicator_init(GPIO_LED_RGB);
#endif
/* Inicializa o relay */
relay_driver_init();
/* Initialize device-specific hardware */
if (device_config->hardware_init) {
device_config->hardware_init();
}
/* Inicializa o botão */
#ifdef HAS_BUTTON_TOGGLE
/* Initialize button with toggle functionality */
ESP_RETURN_ON_FALSE(switch_driver_init(button_func_pair, PAIR_SIZE(button_func_pair), zb_buttons_handler), ESP_FAIL, TAG,
"Failed to initialize switch driver");
#else
/* Initialize button without toggle (only pairing/factory reset) */
ESP_RETURN_ON_FALSE(switch_driver_init(button_func_pair, PAIR_SIZE(button_func_pair), NULL), ESP_FAIL, TAG,
"Failed to initialize switch driver");
#endif
return ESP_OK;
}
@@ -125,25 +141,23 @@ static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id,
static esp_err_t zb_attribute_handler(const esp_zb_zcl_set_attr_value_message_t *message)
{
esp_err_t ret = ESP_OK;
bool relay_state = false;
ESP_RETURN_ON_FALSE(message, ESP_FAIL, TAG, "Empty message");
ESP_RETURN_ON_FALSE(message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS, ESP_ERR_INVALID_ARG, TAG, "Received message: error status(%d)",
message->info.status);
ESP_LOGI(TAG, "Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)", message->info.dst_endpoint, message->info.cluster,
message->attribute.id, message->attribute.data.size);
ESP_RETURN_ON_FALSE(message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS, ESP_ERR_INVALID_ARG, TAG,
"Received message: error status(%d)", message->info.status);
if (message->info.dst_endpoint == HA_ONOFF_SWITCH_ENDPOINT) {
if (message->info.cluster == ESP_ZB_ZCL_CLUSTER_ID_ON_OFF) {
if (message->attribute.id == ESP_ZB_ZCL_ATTR_ON_OFF_ON_OFF_ID && message->attribute.data.type == ESP_ZB_ZCL_ATTR_TYPE_BOOL) {
relay_state = message->attribute.data.value ? *(bool *)message->attribute.data.value : relay_state;
ESP_LOGI(TAG, "Relay sets to %s", relay_state ? "On" : "Off");
relay_set_state(relay_state);
}
ESP_LOGI(TAG, "Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)",
message->info.dst_endpoint, message->info.cluster, message->attribute.id, message->attribute.data.size);
/* Get device configuration and delegate to device-specific handler */
const device_type_t *device_config = device_get_config();
if (message->info.dst_endpoint == device_config->endpoint) {
if (device_config->attribute_handler) {
return device_config->attribute_handler(message);
}
}
return ret;
return ESP_ERR_NOT_FOUND;
}
void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
@@ -243,31 +257,37 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
static void esp_zb_task(void *pvParameters)
{
/* initialize Zigbee stack */
/* Initialize Zigbee stack */
esp_zb_cfg_t zb_nwk_cfg = ESP_ZB_ZR_CONFIG();
esp_zb_init(&zb_nwk_cfg);
/* Get device configuration from registry */
const device_type_t *device_config = device_get_config();
ESP_LOGI(TAG, "Creating device: %s (ID: 0x%04x)", device_config->name, device_config->device_id);
/* Create endpoint list */
esp_zb_ep_list_t *esp_zb_ep_list = esp_zb_ep_list_create();
/* Create on/off light endpoint */
esp_zb_on_off_light_cfg_t light_cfg = ESP_ZB_DEFAULT_ON_OFF_LIGHT_CONFIG();
/* Create device-specific clusters */
esp_zb_cluster_list_t *esp_zb_cluster_list = device_config->create_clusters();
/* Configure endpoint */
esp_zb_endpoint_config_t endpoint_config = {
.endpoint = HA_ONOFF_SWITCH_ENDPOINT,
.endpoint = device_config->endpoint,
.app_profile_id = ESP_ZB_AF_HA_PROFILE_ID,
.app_device_id = ESP_ZB_HA_ON_OFF_OUTPUT_DEVICE_ID,
.app_device_id = device_config->device_id,
.app_device_version = 0
};
esp_zb_cluster_list_t *esp_zb_cluster_list = esp_zb_zcl_cluster_list_create();
esp_zb_attribute_list_t *esp_zb_basic_cluster = esp_zb_basic_cluster_create(&(light_cfg.basic_cfg));
ESP_ERROR_CHECK(esp_zb_basic_cluster_add_attr(esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)ESP_MANUFACTURER_NAME));
ESP_ERROR_CHECK(esp_zb_basic_cluster_add_attr(esp_zb_basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)ESP_MODEL_IDENTIFIER));
ESP_ERROR_CHECK(esp_zb_cluster_list_add_basic_cluster(esp_zb_cluster_list, esp_zb_basic_cluster, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
ESP_ERROR_CHECK(esp_zb_cluster_list_add_identify_cluster(esp_zb_cluster_list, esp_zb_identify_cluster_create(&(light_cfg.identify_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
ESP_ERROR_CHECK(esp_zb_cluster_list_add_groups_cluster(esp_zb_cluster_list, esp_zb_groups_cluster_create(&(light_cfg.groups_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
ESP_ERROR_CHECK(esp_zb_cluster_list_add_scenes_cluster(esp_zb_cluster_list, esp_zb_scenes_cluster_create(&(light_cfg.scenes_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
ESP_ERROR_CHECK(esp_zb_cluster_list_add_on_off_cluster(esp_zb_cluster_list, esp_zb_on_off_cluster_create(&(light_cfg.on_off_cfg)), ESP_ZB_ZCL_CLUSTER_SERVER_ROLE));
/* Add manufacturer and model info to basic cluster */
esp_zb_attribute_list_t *basic_cluster = esp_zb_cluster_list_get_cluster(
esp_zb_cluster_list, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
if (basic_cluster) {
esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID, (void *)ESP_MANUFACTURER_NAME);
esp_zb_basic_cluster_add_attr(basic_cluster, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, (void *)ESP_MODEL_IDENTIFIER);
}
/* Register endpoint */
ESP_ERROR_CHECK(esp_zb_ep_list_add_ep(esp_zb_ep_list, esp_zb_cluster_list, endpoint_config));
esp_zb_device_register(esp_zb_ep_list);