5#include "freertos/FreeRTOS.h"
6#include "freertos/task.h"
15#include "esp_coexist.h"
20static const char *
const TAG =
"zigbee";
22static ZigbeeComponent *global_zigbee =
nullptr;
24uint8_t *
get_zcl_string(
const char *str, uint8_t max_size,
bool use_max_size) {
25 uint8_t str_len =
static_cast<uint8_t
>(strlen(str));
26 uint8_t zcl_str_size = use_max_size ? max_size : std::min(max_size, str_len);
27 uint8_t *zcl_str =
new uint8_t[zcl_str_size + 1];
28 zcl_str[0] = zcl_str_size;
31 memset(zcl_str + 1, 0, zcl_str_size);
32 uint8_t copy_len = std::min(zcl_str_size, str_len);
34 memcpy(zcl_str + 1, str, copy_len);
39static void bdb_start_top_level_commissioning_cb(uint8_t mode_mask) {
40 if (esp_zb_bdb_start_top_level_commissioning(mode_mask) != ESP_OK) {
41 ESP_LOGE(TAG,
"Start network steering failed!");
46 static uint8_t steering_retry_count = 0;
47 uint32_t *p_sg_p = signal_struct->p_app_signal;
48 esp_err_t err_status = signal_struct->esp_err_status;
49 esp_zb_app_signal_type_t sig_type = (esp_zb_app_signal_type_t) *p_sg_p;
50 esp_zb_zdo_signal_leave_params_t *leave_params = NULL;
52 case ESP_ZB_ZDO_SIGNAL_SKIP_STARTUP:
53 ESP_LOGD(TAG,
"Zigbee stack initialized");
54 esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_INITIALIZATION);
56 case ESP_ZB_BDB_SIGNAL_DEVICE_FIRST_START:
57 case ESP_ZB_BDB_SIGNAL_DEVICE_REBOOT:
58 if (err_status == ESP_OK) {
59 ESP_LOGD(TAG,
"Device started up in %sfactory-reset mode", esp_zb_bdb_is_factory_new() ?
"" :
"non ");
61 if (esp_zb_bdb_is_factory_new()) {
63 ESP_LOGD(TAG,
"Start network steering");
64 esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
66 ESP_LOGD(TAG,
"Device rebooted");
67 global_zigbee->
joined =
true;
71 ESP_LOGE(TAG,
"FIRST_START. Device started up in %sfactory-reset mode with an error %d (%s)",
72 esp_zb_bdb_is_factory_new() ?
"" :
"non ", err_status, esp_err_to_name(err_status));
73 ESP_LOGW(TAG,
"Failed to initialize Zigbee stack (status: %s)", esp_err_to_name(err_status));
74 esp_zb_scheduler_alarm((esp_zb_callback_t) bdb_start_top_level_commissioning_cb, ESP_ZB_BDB_MODE_INITIALIZATION,
78 case ESP_ZB_BDB_SIGNAL_STEERING:
79 if (err_status == ESP_OK) {
80 steering_retry_count = 0;
81 ESP_LOGI(TAG,
"Joined network successfully (PAN ID: 0x%04hx, Channel:%d)", esp_zb_get_pan_id(),
82 esp_zb_get_current_channel());
83 global_zigbee->
joined =
true;
86 ESP_LOGI(TAG,
"Network steering was not successful (status: %s)", esp_err_to_name(err_status));
87 if (steering_retry_count < 10) {
88 steering_retry_count++;
89 esp_zb_scheduler_alarm((esp_zb_callback_t) bdb_start_top_level_commissioning_cb,
90 ESP_ZB_BDB_MODE_NETWORK_STEERING, 1000);
92 esp_zb_scheduler_alarm((esp_zb_callback_t) bdb_start_top_level_commissioning_cb,
93 ESP_ZB_BDB_MODE_NETWORK_STEERING, 600 * 1000);
97 case ESP_ZB_ZDO_SIGNAL_LEAVE:
98 leave_params = (esp_zb_zdo_signal_leave_params_t *) esp_zb_app_signal_get_params(p_sg_p);
99 if (leave_params->leave_type == ESP_ZB_NWK_LEAVE_TYPE_RESET) {
100 esp_zb_factory_reset();
104 ESP_LOGD(TAG,
"ZDO signal: %s (0x%x), status: %s", esp_zb_zdo_signal_to_string(sig_type), sig_type,
105 esp_err_to_name(err_status));
110static esp_err_t zb_attribute_handler(
const esp_zb_zcl_set_attr_value_message_t *
message) {
111 esp_err_t ret = ESP_OK;
112 ESP_RETURN_ON_FALSE(
message, ESP_FAIL, TAG,
"Empty message");
113 ESP_RETURN_ON_FALSE(
message->info.status == ESP_ZB_ZCL_STATUS_SUCCESS, ESP_ERR_INVALID_ARG, TAG,
114 "Received message: error status(%d)",
message->info.status);
115 ESP_LOGD(TAG,
"Received message: endpoint(%d), cluster(0x%x), attribute(0x%x), data size(%d)",
120static esp_err_t zb_action_handler(esp_zb_core_action_callback_id_t callback_id,
const void *
message) {
121 esp_err_t ret = ESP_OK;
122 switch (callback_id) {
123 case ESP_ZB_CORE_SET_ATTR_VALUE_CB_ID:
124 ret = zb_attribute_handler((esp_zb_zcl_set_attr_value_message_t *)
message);
127 ESP_LOGD(TAG,
"Receive Zigbee action(0x%x) callback", callback_id);
134 esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
136 std::tuple<zb_ha_standard_devs_e, esp_zb_cluster_list_t *>(device_id, cluster_list);
138 this->
add_cluster(endpoint_id, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
140 if (esp_zb_cluster_list_get_cluster(cluster_list, ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE) ==
142 this->
add_cluster(endpoint_id, ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
147 esp_zb_attribute_list_t *attr_list;
148 if (cluster_id == 0) {
159 struct tm *timeinfo =
localtime(&time_val);
160 strftime(date_buf,
sizeof(date_buf),
"%Y%m%d %H%M%S", timeinfo);
170 esp_zb_basic_cluster_cfg_t basic_cluster_cfg = {
171 .zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE,
174 esp_zb_attribute_list_t *attr_list = esp_zb_basic_cluster_create(&basic_cluster_cfg);
175 esp_zb_basic_cluster_add_attr(attr_list, ESP_ZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID,
177 esp_zb_basic_cluster_add_attr(attr_list, ESP_ZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID, this->
basic_cluster_data_.model);
178 esp_zb_basic_cluster_add_attr(attr_list, ESP_ZB_ZCL_ATTR_BASIC_DATE_CODE_ID, this->
basic_cluster_data_.date);
183 esp_zb_cluster_list_t *esp_zb_cluster_list) {
184 esp_zb_endpoint_config_t endpoint_config = {.endpoint = endpoint_id,
185 .app_profile_id = ESP_ZB_AF_HA_PROFILE_ID,
186 .app_device_id = device_id,
187 .app_device_version = 0};
188 return esp_zb_ep_list_add_ep(this->
esp_zb_ep_list_, esp_zb_cluster_list, endpoint_config);
191static void esp_zb_task_(
void *pvParameters) {
192 if (esp_zb_start(
false) != ESP_OK) {
193 ESP_LOGE(TAG,
"Could not setup Zigbee");
197 ESP_LOGD(TAG,
"Battery powered!");
198 esp_zb_set_node_descriptor_power_source(0);
200 esp_zb_set_node_descriptor_power_source(1);
202 esp_zb_stack_main_loop();
206 global_zigbee =
this;
207 esp_zb_platform_config_t config = {};
208 config.radio_config = ESP_ZB_DEFAULT_RADIO_CONFIG();
209 config.host_config = ESP_ZB_DEFAULT_HOST_CONFIG();
211 if (esp_coex_wifi_i154_enable() != ESP_OK) {
216 if (esp_zb_platform_config(&config) != ESP_OK) {
221 esp_zb_zed_cfg_t zb_zed_cfg = {
222 .ed_timeout = ESP_ZB_ED_AGING_TIMEOUT_64MIN,
223 .keep_alive = ED_KEEP_ALIVE,
225 esp_zb_zczr_cfg_t zb_zczr_cfg = {
226 .max_children = MAX_CHILDREN,
228 esp_zb_cfg_t zb_nwk_cfg = {
230 .install_code_policy =
false,
233 zb_nwk_cfg.nwk_cfg.zczr_cfg = zb_zczr_cfg;
235 zb_nwk_cfg.nwk_cfg.zed_cfg = zb_zed_cfg;
237 esp_zb_init(&zb_nwk_cfg);
241 esp_zb_cluster_list_t *esp_zb_cluster_list = std::get<1>(this->
endpoint_list_[std::get<0>(key)]);
244 ESP_LOGE(TAG,
"Could not create cluster 0x%04X with role %u: %s", std::get<1>(key), std::get<2>(key),
245 esp_err_to_name(ret));
247 ESP_LOGD(TAG,
"Endpoint %u: Added cluster 0x%04X with role %u", std::get<0>(key), std::get<1>(key),
249#ifdef ESPHOME_LOG_HAS_VERBOSE
251 ESP_LOGV(TAG,
"Cluster 0x%04X attributes:", std::get<1>(key));
252 esp_zb_attribute_list_t *attr_list =
val;
254 esp_zb_zcl_attr_t *attr = &attr_list->attribute;
255 ESP_LOGV(TAG,
" Attr ID: 0x%04X, Type: 0x%02X, Access: 0x%02X", attr->id, attr->type, attr->access);
256 attr_list = attr_list->next;
261 this->attribute_list_.clear();
264 if (
create_endpoint(ep_id, std::get<0>(dev_id), std::get<1>(dev_id)) != ESP_OK) {
265 ESP_LOGE(TAG,
"Could not create endpoint %u", ep_id);
268 this->endpoint_list_.clear();
271 ESP_LOGE(TAG,
"Could not register the endpoint list");
276 esp_zb_core_action_handler_register(zb_action_handler);
278 if (esp_zb_set_primary_network_channel_set(ESP_ZB_TRANSCEIVER_ALL_CHANNELS_MASK) != ESP_OK) {
279 ESP_LOGE(TAG,
"Could not setup Zigbee");
284 if (attribute->report_enabled) {
285 esp_zb_zcl_reporting_info_t reporting_info = attribute->get_reporting_info();
286 ESP_LOGD(TAG,
"set reporting for cluster: %u", reporting_info.cluster_id);
287 if (esp_zb_zcl_update_reporting_info(&reporting_info) != ESP_OK) {
288 ESP_LOGE(TAG,
"Could not configure reporting for attribute 0x%04X in cluster 0x%04X in endpoint %u",
289 reporting_info.attr_id, reporting_info.cluster_id, reporting_info.ep);
293 xTaskCreate(esp_zb_task_,
"Zigbee_main", 4096, NULL, 24, NULL);
298 if (this->
joined.exchange(
false)) {
306 if (esp_zb_lock_acquire(10 / portTICK_PERIOD_MS)) {
311 " Device is joined to the network: %s\n"
312 " Current channel: %d\n"
313 " Short addr: 0x%04X\n"
314 " Short pan id: 0x%04X",
316 YESNO(esp_zb_bdb_dev_joined()), esp_zb_get_current_channel(), esp_zb_get_short_address(),
317 esp_zb_get_pan_id());
318 esp_zb_lock_release();
time_t get_build_time()
Get the build time as a Unix timestamp.
void mark_failed()
Mark this component as failed.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
CallbackManager< void(bool)> join_cb_
esp_zb_ep_list_t * esp_zb_ep_list_
bool is_battery_powered()
std::map< std::tuple< uint8_t, uint16_t, uint8_t, uint16_t >, ZigbeeAttribute * > attributes_
esp_zb_nwk_device_type_t device_role_
void create_default_cluster(uint8_t endpoint_id, zb_ha_standard_devs_e device_id)
void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role)
std::atomic< bool > joined
std::atomic< bool > factory_new
std::map< uint8_t, std::tuple< zb_ha_standard_devs_e, esp_zb_cluster_list_t * > > endpoint_list_
std::map< std::tuple< uint8_t, uint16_t, uint8_t >, esp_zb_attribute_list_t * > attribute_list_
void dump_config() override
void set_basic_cluster(const char *model, const char *manufacturer, uint8_t power_source)
esp_err_t create_endpoint(uint8_t endpoint_id, zb_ha_standard_devs_e device_id, esp_zb_cluster_list_t *esp_zb_cluster_list)
struct esphome::zigbee::ZigbeeComponent::@199 basic_cluster_data_
std::atomic< bool > started
esp_zb_attribute_list_t * create_basic_cluster_()
uint8_t * get_zcl_string(const char *str, uint8_t max_size, bool use_max_size)
void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct)
Application App
Global storage of Application pointer - only one Application can exist.
struct tm * localtime(const time_t *timer)
esp_err_t esphome_zb_cluster_list_add_or_update_cluster(uint16_t cluster_id, esp_zb_cluster_list_t *cluster_list, esp_zb_attribute_list_t *attr_list, uint8_t role_mask)
esp_zb_attribute_list_t * esphome_zb_default_attr_list_create(uint16_t cluster_id)