ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
zigbee_esp32.cpp
Go to the documentation of this file.
2#ifdef USE_ESP32
3#ifdef USE_ZIGBEE
4
5#include <algorithm>
6#include <cstdint>
7#include "freertos/FreeRTOS.h"
8#include "freertos/task.h"
9#include "esp_check.h"
11#include "zigbee_esp32.h"
13#include "esphome/core/hal.h"
14#include "esphome/core/log.h"
16#ifdef USE_WIFI
17#include "esp_coexist.h"
18#endif
19
20namespace esphome::zigbee {
21
22ESPHOME_LOG_TAG(TAG, "zigbee");
23
24static constexpr uint32_t COMMISSIONING_RETRY_TIMEOUT_ID = 0;
25static constexpr uint32_t PARENT_LINK_TIMEOUT_ID = 1;
26static constexpr uint32_t PARENT_LINK_RETRY_TIMEOUT_ID = 2;
27
28// Since esp-zigbee-lib 2.0.5 every lost poll or transmission raises PARENT_LINK_FAILURE, even during a short radio
29// outage such as a BLE scan. After a failure the device polls at least every 3 s. It rejoins only after failures in
30// 4 polls that span at least 6 s; failures less than two thirds of a polling interval apart (poll retries, fast
31// polls) count once. Two intervals without a counted failure mean that a poll got through.
32static constexpr uint8_t PARENT_LINK_MISSED_POLLS = 4;
33static constexpr uint32_t PARENT_LINK_POLL_INTERVAL_MS = 3000;
34static constexpr uint32_t PARENT_LINK_MIN_SPAN_MS = 6000;
35
36static ZigbeeComponent *global_zigbee = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
37
38uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size) {
39 uint8_t str_len = static_cast<uint8_t>(strlen(str));
40 uint8_t zcl_str_size = use_max_size ? max_size : std::min(max_size, str_len);
41 uint8_t *zcl_str = new uint8_t[zcl_str_size + 1]; // string + length octet
42 zcl_str[0] = zcl_str_size;
43
44 // Initialize payload to avoid leaking uninitialized heap contents and clamp copy length
45 memset(zcl_str + 1, 0, zcl_str_size);
46 uint8_t copy_len = std::min(zcl_str_size, str_len);
47 if (copy_len > 0) {
48 memcpy(zcl_str + 1, str, copy_len);
49 }
50 return zcl_str;
51}
52
54 esp_zigbee_lock_acquire(portMAX_DELAY);
55 if (this->joined_) {
56 // send leave request and trigger EZB_ZDO_SIGNAL_LEAVE
57 ezb_bdb_reset_via_local_action();
58 } else {
59 esp_zigbee_factory_reset(); // triggers a reboot
60 }
61 esp_zigbee_lock_release();
62}
63
65 for (const auto &[_, attribute] : this->attributes_) {
66 attribute->report();
67 }
68}
69
71 if (!esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
72 global_zigbee->set_timeout(COMMISSIONING_RETRY_TIMEOUT_ID, 100,
74 return;
75 }
76 if (ezb_bdb_start_top_level_commissioning(mode) != EZB_ERR_NONE) {
77 ESP_LOGE(TAG, "Start top level commissioning failed!");
78 }
79 esp_zigbee_lock_release();
80}
81
82bool ZigbeeComponent::app_signal_handler(const ezb_app_signal_t *app_signal) {
83 static uint8_t steering_retry_count = 0;
84 ezb_app_signal_type_t signal_type = ezb_app_signal_get_type(app_signal);
85 switch (signal_type) {
86 case EZB_ZDO_SIGNAL_SKIP_STARTUP:
87 ESP_LOGD(TAG, "Zigbee stack initialized");
88 global_zigbee->started_ = true;
89 global_zigbee->enable_loop_soon_any_context();
90 ezb_bdb_start_top_level_commissioning(EZB_BDB_MODE_INITIALIZATION);
91 break;
92 case EZB_BDB_SIGNAL_DEVICE_FIRST_START:
93 case EZB_BDB_SIGNAL_DEVICE_REBOOT: {
94 ezb_bdb_comm_status_t status = *((ezb_bdb_comm_status_t *) ezb_app_signal_get_params(app_signal));
95 if (status == EZB_BDB_STATUS_SUCCESS) {
96 ESP_LOGD(TAG, "Device started up in %sfactory-reset mode", ezb_bdb_is_factory_new() ? "" : "non ");
97 if (ezb_bdb_is_factory_new()) {
98 global_zigbee->factory_new_ = true;
99 ESP_LOGD(TAG, "Start network steering");
100 ezb_bdb_start_top_level_commissioning(EZB_BDB_MODE_NETWORK_STEERING);
101 } else {
102 ESP_LOGD(TAG, "Device rebooted");
103 global_zigbee->joined_ = true;
104 global_zigbee->join_pending_ = true;
105 global_zigbee->enable_loop_soon_any_context();
106 }
107 } else {
108 ESP_LOGW(TAG, "The %s failed with status(0x%02x), please retry", ezb_app_signal_to_string(signal_type), status);
109 global_zigbee->set_timeout(COMMISSIONING_RETRY_TIMEOUT_ID, 1000, []() {
110 ZigbeeComponent::esp_zigbee_alarm_bdb_commissioning(EZB_BDB_MODE_INITIALIZATION);
111 });
112 }
113 } break;
114 case EZB_BDB_SIGNAL_STEERING: {
115 ezb_bdb_comm_status_t status = *((ezb_bdb_comm_status_t *) ezb_app_signal_get_params(app_signal));
116 if (status == EZB_BDB_STATUS_SUCCESS) {
117 steering_retry_count = 0;
118 ezb_extpanid_t extended_pan_id;
119 ezb_nwk_get_extended_panid(&extended_pan_id);
120 ESP_LOGD(TAG, "Joined network successfully: PAN ID(0x%04hx, EXT: 0x%llx), Channel(%d), Short Address(0x%04hx)",
121 ezb_nwk_get_panid(), extended_pan_id.u64, ezb_nwk_get_current_channel(), ezb_nwk_get_short_address());
122 global_zigbee->joined_ = true;
123 global_zigbee->join_pending_ = true;
124 global_zigbee->enable_loop_soon_any_context();
125 } else {
126 ESP_LOGD(TAG, "Failed to join network with status(0x%02x)", status);
127 if (steering_retry_count < 10) {
128 steering_retry_count++;
129 global_zigbee->set_timeout(COMMISSIONING_RETRY_TIMEOUT_ID, 1000, []() {
130 ZigbeeComponent::esp_zigbee_alarm_bdb_commissioning(EZB_BDB_MODE_NETWORK_STEERING);
131 });
132 } else {
133 global_zigbee->set_timeout(COMMISSIONING_RETRY_TIMEOUT_ID, 600 * 1000, []() {
134 ZigbeeComponent::esp_zigbee_alarm_bdb_commissioning(EZB_BDB_MODE_NETWORK_STEERING);
135 });
136 }
137 }
138 } break;
139 case EZB_ZDO_SIGNAL_LEAVE: {
140 const ezb_zdo_signal_leave_params_t *leave_params =
141 (const ezb_zdo_signal_leave_params_t *) ezb_app_signal_get_params(app_signal);
142 if (leave_params->leave_type == EZB_ZDO_LEAVE_TYPE_RESET) {
143 esp_zigbee_factory_reset(); // triggers a reboot
144 }
145 global_zigbee->joined_ = false;
146 // For a leave asked for by the network: failures before it must not count after the rejoin
147 global_zigbee->reset_parent_link_failures_();
148 } break;
149 case EZB_NWK_SIGNAL_NETWORK_STATUS: {
150 const ezb_nwk_signal_network_status_params_t *network_status_params =
151 (const ezb_nwk_signal_network_status_params_t *) ezb_app_signal_get_params(app_signal);
152 if (network_status_params->status == EZB_NWK_NETWORK_STATUS_PARENT_LINK_FAILURE) {
153 if (global_zigbee->joined_) { // else not joined yet, or a rejoin is running
154 global_zigbee->on_parent_link_failure_();
155 }
156 } else {
157 ESP_LOGD(TAG, "Zigbee APP Signal NETWORK_STATUS: 0x%02x", network_status_params->status);
158 }
159 } break;
160 default:
161 ESP_LOGD(TAG, "Zigbee APP Signal: %s(type: 0x%02x)", ezb_app_signal_to_string(signal_type), signal_type);
162 break;
163 }
164 return true;
165}
166
167// Runs on the Zigbee task with the stack lock held
169 const uint32_t now = millis();
170 if (this->link_failures_ == 0) {
171 // Poll faster for now, so that a lost parent is noticed soon also with a long polling interval
172 this->saved_keep_alive_ = ezb_nwk_get_keepalive_interval();
173 if (this->saved_keep_alive_ > PARENT_LINK_POLL_INTERVAL_MS) {
174 ezb_nwk_set_keepalive_interval(PARENT_LINK_POLL_INTERVAL_MS);
175 }
176 }
177 // The polling interval during the series
178 const uint32_t interval = std::min(this->saved_keep_alive_, PARENT_LINK_POLL_INTERVAL_MS);
179 if (this->link_failures_ > 0) {
180 if (now - this->last_link_failure_ms_ >= 2 * interval) {
181 // The series has ended, but its timeout has not run yet: start a new one, keep the saved interval
182 this->link_failures_ = 0;
183 } else if (now - this->last_link_failure_ms_ < 2 * interval / 3) {
184 ESP_LOGV(TAG, "Parent link failure (same poll)");
185 return;
186 }
187 }
188 if (this->link_failures_ == 0) {
189 this->first_link_failure_ms_ = now;
190 }
191 this->last_link_failure_ms_ = now;
192 this->link_failure_seq_++;
193 if (this->link_failures_ < UINT8_MAX) {
194 this->link_failures_++;
195 }
197 ESP_LOGD(TAG, "Parent link failure %u", this->link_failures_);
198 // The stack does not report a poll that got through, so the series ends when no failure follows
199 const uint8_t seq = this->link_failure_seq_;
200 this->set_timeout(PARENT_LINK_TIMEOUT_ID, 2 * interval, [this, seq]() { this->end_parent_link_failures_(seq); });
201 return;
202 }
203 ESP_LOGW(TAG, "Parent link failed in %u polls, attempting rejoin", this->link_failures_);
204 this->cancel_timeout(PARENT_LINK_TIMEOUT_ID);
206 this->joined_ = false;
207 ezb_zdo_nwk_mgmt_leave_req_t leave_req = {
208 .dst_nwk_addr = ezb_nwk_get_short_address(),
209 .field =
210 {
211 .remove_children = false,
212 .rejoin = true,
213 },
214 };
215 // Send leave request to the network to rejoin
216 // triggers EZB_ZDO_SIGNAL_LEAVE signal first, then EZB_BDB_SIGNAL_DEVICE_REBOOT
217 if (ezb_zdo_nwk_mgmt_leave_req(&leave_req) != EZB_ERR_NONE) {
218 ESP_LOGW(TAG, "Leave request for rejoin failed");
219 this->joined_ = true; // still joined, so later failures can start a rejoin again
220 }
221}
222
223// Runs on the main loop when no failure was counted for two poll intervals
225 if (!esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
226 // Retry with its own id, so that it cannot replace the timeout of a newer failure
227 this->set_timeout(PARENT_LINK_RETRY_TIMEOUT_ID, 100, [this, seq]() { this->end_parent_link_failures_(seq); });
228 return;
229 }
230 // A newer failure has set its own timeout, which ends the series instead
231 const uint8_t failures = seq == this->link_failure_seq_ ? this->link_failures_ : 0;
232 if (failures > 0) {
234 }
235 esp_zigbee_lock_release();
236 if (failures > 0) { // after the release: the main task writes log lines out at once
237 ESP_LOGD(TAG, "Parent link failures ended (%u)", failures);
238 }
239}
240
241// Ends the current series, if any. Needs the stack lock
243 if (this->link_failures_ == 0) {
244 return; // no series: the saved interval is stale
245 }
246 this->link_failures_ = 0;
247 // Put the polling interval back, unless something else changed it in the meantime
248 if (this->saved_keep_alive_ > PARENT_LINK_POLL_INTERVAL_MS &&
249 ezb_nwk_get_keepalive_interval() == PARENT_LINK_POLL_INTERVAL_MS) {
250 ezb_nwk_set_keepalive_interval(this->saved_keep_alive_);
251 }
252}
253
254void load_zb_event(ZBEvent *event, ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute) {
255 event->load_set_attr_value_event(info, attribute);
256}
257
258template<typename... Args> void enqueue_zb_event(Args... args) {
259 // Allocate an event from the pool
260 ZBEvent *event = global_zigbee->zb_event_pool_.allocate();
261 if (event == nullptr) {
262 // No events available - queue is full or we're out of memory
263 global_zigbee->zb_events_.increment_dropped_count();
264 return;
265 }
266
267 // Load new event data (replaces previous event)
268 load_zb_event(event, args...);
269
270 // Push the event to the queue
271 global_zigbee->zb_events_.push(event);
272 // Push always succeeds because we're the only producer and the pool ensures we never exceed queue size
273 global_zigbee->enable_loop_soon_any_context();
274}
275
276// Explicit template instantiations for the friend function
277template void enqueue_zb_event(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute);
278
279static void zb_attribute_handler(ezb_zcl_set_attr_value_message_t *message) {
280 ESP_RETURN_ON_FALSE(message, , TAG, "Empty message");
281 ESP_RETURN_ON_FALSE(message->info.status == EZB_ZCL_STATUS_SUCCESS, , TAG, "Received message: error status(%d)",
282 message->info.status);
283 ESP_LOGD(TAG, "ZCL SetAttributeValue message for endpoint(%d) cluster(0x%04x) %s with status(0x%02x)",
284 message->info.dst_ep, message->info.cluster_id,
285 message->info.cluster_role == EZB_ZCL_CLUSTER_SERVER ? "server" : "client", message->info.status);
286 enqueue_zb_event(message->info, message->in.attribute);
287}
288
289static void zb_action_handler(ezb_zcl_core_action_callback_id_t callback_id, void *message) {
290 switch (callback_id) {
291 case EZB_ZCL_CORE_SET_ATTR_VALUE_CB_ID:
292 zb_attribute_handler((ezb_zcl_set_attr_value_message_t *) message);
293 break;
294 case EZB_ZCL_CORE_DEFAULT_RSP_CB_ID: {
295#ifdef ESPHOME_LOG_HAS_VERBOSE
296 ezb_zcl_cmd_default_rsp_message_t *default_rsp = (ezb_zcl_cmd_default_rsp_message_t *) message;
297 ESP_LOGV(TAG, "Received ZCL Default Response: 0x%02x", default_rsp->in.status_code);
298#endif
299 } break;
300 default:
301 ESP_LOGD(TAG, "Receive Zigbee action(0x%04x) callback", static_cast<unsigned>(callback_id));
302 break;
303 }
304}
305
306void ZigbeeComponent::handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute) {
307 auto it = this->attributes_.find({info.dst_ep, info.cluster_id, info.cluster_role, attribute.id});
308 if (it != this->attributes_.end()) {
309 it->second->on_value(attribute);
310 }
311}
312
313void ZigbeeComponent::create_default_cluster(uint8_t endpoint_id, uint16_t device_id) {
314 ezb_af_ep_desc_t ep_desc =
315 esphome_zb_zha_default_ep_desc_create(endpoint_id, device_id, this->basic_cluster_data_.power_source);
316 if (ezb_af_device_add_endpoint_desc(this->dev_desc_, ep_desc) != EZB_ERR_NONE) {
317 ESP_LOGE(TAG, "Could not create endpoint %u", endpoint_id);
318 }
319 // Add basic cluster
320 this->update_basic_cluster_(ep_desc);
321 // Add identify cluster if not already present
322 this->add_cluster(endpoint_id, EZB_ZCL_CLUSTER_ID_IDENTIFY, EZB_ZCL_CLUSTER_SERVER);
323}
324
325void ZigbeeComponent::add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role) {
326 if (cluster_id == EZB_ZCL_CLUSTER_ID_BASIC) {
327 return;
328 }
329 ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->dev_desc_, endpoint_id);
330 if (ep_desc == NULL) {
331 ESP_LOGE(TAG, "Endpoint %u does not exist, cannot add cluster 0x%04X", endpoint_id, cluster_id);
332 return;
333 }
334 esphome_zb_add_or_update_cluster(cluster_id, ep_desc, role);
335 ESP_LOGD(TAG, "Endpoint %u: Added cluster 0x%04X with role %u", endpoint_id, cluster_id, role);
336}
337
338void ZigbeeComponent::set_basic_cluster(const char *model, const char *manufacturer, const char *location,
339 const char *sw_build_id, uint8_t power_source, uint8_t app_version) {
340 char date_buf[16];
341 time_t time_val = App.get_build_time();
342 struct tm *timeinfo = localtime(&time_val);
343 strftime(date_buf, sizeof(date_buf), "%Y%m%d %H%M%S", timeinfo);
344 this->basic_cluster_data_ = {
345 .model = get_zcl_string(model, 31),
346 .manufacturer = get_zcl_string(manufacturer, 31),
347 .date = get_zcl_string(date_buf, 15),
348 .location = get_zcl_string(location, 15),
349 .sw_build_id = get_zcl_string(sw_build_id, 15),
350 .power_source = power_source,
351 .app_version = app_version,
352 };
353}
354
355void ZigbeeComponent::update_basic_cluster_(ezb_af_ep_desc_t ep_desc) {
356 ezb_zcl_cluster_desc_t cluster_desc =
357 ezb_af_endpoint_get_cluster_desc(ep_desc, EZB_ZCL_CLUSTER_ID_BASIC, EZB_ZCL_CLUSTER_SERVER);
358 if (cluster_desc == NULL) {
359 ezb_zcl_basic_cluster_config_t basic_cluster_cfg = {
360 .zcl_version = EZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE,
361 .power_source = this->basic_cluster_data_.power_source,
362 };
363 cluster_desc = ezb_zcl_basic_create_cluster_desc(&basic_cluster_cfg, EZB_ZCL_CLUSTER_SERVER);
364 ezb_af_endpoint_add_cluster_desc(ep_desc, cluster_desc);
365 }
366 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID,
367 this->basic_cluster_data_.manufacturer);
368 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID,
369 this->basic_cluster_data_.model);
370 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_DATE_CODE_ID, this->basic_cluster_data_.date);
371 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_APPLICATION_VERSION_ID,
372 &(this->basic_cluster_data_.app_version));
373 uint8_t stack_version = 0;
374 uint8_t hw_version = 0;
375 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_STACK_VERSION_ID, &stack_version);
376 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_HW_VERSION_ID, &hw_version);
377 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_LOCATION_DESCRIPTION_ID,
378 this->basic_cluster_data_.location);
379 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_SW_BUILD_ID_ID,
380 this->basic_cluster_data_.sw_build_id);
381}
382
384 if (ezb_af_device_desc_register(this->dev_desc_) != EZB_ERR_NONE) {
385 ESP_LOGE(TAG, "Could not register the endpoint list");
386 this->mark_failed();
387 return false;
388 }
389 return true;
390}
391
392static void ezb_task(void *pv_parameters) {
393 if (!global_zigbee->register_device()) {
394 vTaskDelete(NULL);
395 return;
396 }
397 if (esp_zigbee_start(false) != ESP_OK) {
398 ESP_LOGE(TAG, "Could not setup Zigbee");
399 global_zigbee->mark_failed();
400 vTaskDelete(NULL);
401 return; // vTaskDelete(NULL) never returns, but keep intent explicit
402 }
403
404 // Increase priority to 5 to align with openthread or BLE
405 vTaskPrioritySet(NULL, 5);
406
407 esp_zigbee_launch_mainloop();
408
409 esp_zigbee_deinit();
410
411 vTaskDelete(NULL);
412}
413
415 global_zigbee = this;
416
417 esp_zigbee_platform_config_t platform_config = {
418 .storage_partition_name = "nvs",
419 .radio_config = EZB_DEFAULT_RADIO_CONFIG(),
420 };
421 esp_zigbee_device_config_t device_config = {
422 .device_type = this->device_role_,
423 .install_code_policy = false,
424 };
425#ifdef CONFIG_ZB_ZCZR
426 esp_zigbee_zczr_config_s zb_zczr_cfg = {
427 .max_children = MAX_CHILDREN,
428 };
429 device_config.zczr_config = zb_zczr_cfg;
430#else
431 esp_zigbee_zed_config_s zb_zed_cfg = {
432 .ed_timeout = EZB_NWK_ED_TIMEOUT_64MIN,
433 .keep_alive = this->keep_alive_,
434 };
435 device_config.zed_config = zb_zed_cfg;
436#endif
437 esp_zigbee_config_t config = {.device_config = device_config, .platform_config = platform_config};
438 if (esp_zigbee_init(&config) != ESP_OK) {
439 ESP_LOGE(TAG, "Could not initialize Zigbee");
440 this->mark_failed();
441 return;
442 }
443
444#ifdef USE_WIFI
445 if (esp_coex_wifi_i154_enable() != ESP_OK) {
446 this->mark_failed();
447 return;
448 }
449#endif
450
451#ifdef CONFIG_ZB_ZCZR
452 ezb_bdb_set_router_rejoin_required(true);
453#else
454 ezb_nwk_set_rx_on_when_idle(!this->sleepy_); // if sleepy, disable RX when idle
455#endif
456
457 ezb_aps_secur_enable_distributed_security(false);
458 ezb_nwk_set_min_join_lqi(32);
459 if (ezb_app_signal_add_handler(ZigbeeComponent::app_signal_handler) != ESP_OK) {
460 ESP_LOGE(TAG, "Could not set application signal handler");
461 this->mark_failed();
462 return;
463 }
464
465 ezb_zcl_core_action_handler_register(zb_action_handler);
466
467 if (ezb_bdb_set_primary_channel_set(EZB_PRIMARY_CHANNEL_MASK) != ESP_OK) {
468 ESP_LOGE(TAG, "Could not setup Zigbee");
469 this->mark_failed();
470 return;
471 }
472
473 uint8_t power_source = static_cast<uint8_t>(this->is_battery_powered() ? EZB_AF_NODE_POWER_SOURCE_RECHARGEABLE_BATTERY
474 : EZB_AF_NODE_POWER_SOURCE_CONSTANT_POWER);
475 ezb_af_node_power_desc_t desc = {
476 .current_power_mode = EZB_AF_NODE_POWER_MODE_SYNC_ON_WHEN_IDLE,
477 .available_power_sources = power_source,
478 .current_power_source = power_source,
479 .current_power_source_level = EZB_AF_NODE_POWER_SOURCE_LEVEL_100_PERCENT,
480 };
481 ezb_af_set_node_power_desc(&desc);
482
483 // Start the Zigbee task with priority 1 to ensure main loop can still run even if Zigbee is busy
484 xTaskCreate(ezb_task, "Zigbee_main", 4096, NULL, 1, NULL);
485 this->disable_loop(); // loop is only needed for processing events, so disable until we join a network
486}
487
489 // Process all pending events
490 ZBEvent *event = this->zb_events_.pop();
491 while (event != nullptr) {
492 // Handle the event
493 switch (event->callback_id_) {
494 case EZB_ZCL_CORE_SET_ATTR_VALUE_CB_ID:
495 this->handle_attribute_(event->event_.set_attr.info, event->event_.set_attr.attribute);
496 break;
497 default:
498 ESP_LOGW(TAG, "Received event with unhandled callback id: 0x%x", static_cast<unsigned>(event->callback_id_));
499 break;
500 }
501
502 // Free the event back to the pool
503 this->zb_event_pool_.release(event);
504 // Get the next event
505 event = this->zb_events_.pop();
506 }
507 // Log dropped events periodically
508 uint16_t dropped = this->zb_events_.get_and_reset_dropped_count();
509 if (dropped > 0) {
510 ESP_LOGW(TAG, "Dropped %u Zigbee events due to buffer overflow", dropped);
511 }
512
513 if (!this->start_reported_ && this->started_) {
514 this->start_cb_.call();
515 this->start_reported_ = true;
516 }
517 if (this->join_pending_.exchange(false)) {
518 this->join_cb_.call(this->factory_new_);
519 this->factory_new_ = false;
520 }
521 this->disable_loop();
522}
523
525 ESP_LOGCONFIG(TAG,
526 "Zigbee\n"
527 " Model: %.*s\n"
528 " Manufacturer: %.*s\n"
529#ifdef CONFIG_ZB_ZCZR
530 " Router: %s\n"
531#else
532 " Sleepy end device: %s\n"
533 " Poll interval: %" PRIu32 " ms\n"
534#endif
535 " App Version: %u",
536 this->basic_cluster_data_.model[0], reinterpret_cast<const char *>(this->basic_cluster_data_.model + 1),
537 this->basic_cluster_data_.manufacturer[0],
538 reinterpret_cast<const char *>(this->basic_cluster_data_.manufacturer + 1),
539#ifdef CONFIG_ZB_ZCZR
540 YESNO(this->device_role_ == EZB_NWK_DEVICE_TYPE_ROUTER),
541#else
542 YESNO(this->sleepy_), this->keep_alive_,
543#endif
544 this->basic_cluster_data_.app_version);
545 if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
546 ESP_LOGCONFIG(TAG,
547 " Device is joined to the network: %s\n"
548 " Current channel: %d\n"
549 " Short addr: 0x%04X\n"
550 " Short pan id: 0x%04X",
551 YESNO(ezb_bdb_dev_joined()), ezb_nwk_get_current_channel(), ezb_nwk_get_short_address(),
552 ezb_nwk_get_panid());
553 esp_zigbee_lock_release();
554 }
555}
556} // namespace esphome::zigbee
557
558#endif
559#endif
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
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.
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void disable_loop()
Disable this component's loop.
ezb_zcl_core_action_callback_id_t callback_id_
union esphome::zigbee::ZBEvent::@210 event_
struct esphome::zigbee::ZBEvent::@210::set_attr_event set_attr
CallbackManager< void(bool)> join_cb_
void create_default_cluster(uint8_t endpoint_id, uint16_t device_id)
std::atomic< bool > factory_new_
void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute)
esphome::LockFreeQueue< ZBEvent, MAX_ZB_QUEUE_SIZE > zb_events_
std::map< std::tuple< uint8_t, uint16_t, uint8_t, uint16_t >, ZigbeeAttribute * > attributes_
void update_basic_cluster_(ezb_af_ep_desc_t ep_desc)
void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role)
static void esp_zigbee_alarm_bdb_commissioning(ezb_bdb_comm_mode_mask_t mode)
LazyCallbackManager< void()> start_cb_
static bool app_signal_handler(const ezb_app_signal_t *app_signal)
std::atomic< bool > join_pending_
ezb_nwk_device_type_t device_role_
void set_basic_cluster(const char *model, const char *manufacturer, const char *location, const char *sw_build_id, uint8_t power_source, uint8_t app_version)
void end_parent_link_failures_(uint8_t seq)
esphome::EventPool< ZBEvent, MAX_ZB_QUEUE_SIZE - 1 > zb_event_pool_
struct esphome::zigbee::ZigbeeComponent::@209 basic_cluster_data_
ezb_af_device_desc_t dev_desc_
const LogString * message
Definition component.cpp:35
void load_zb_event(ZBEvent *event, ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute)
uint8_t * get_zcl_string(const char *str, uint8_t max_size, bool use_max_size)
ESPHOME_LOG_TAG(TAG, "zigbee.time")
void enqueue_zb_event(Args... args)
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
struct tm * localtime(const time_t *timer)
Definition posix_tz.cpp:304
static void uint32_t
uint16_t seq
ezb_err_t esphome_zb_add_or_update_cluster(uint16_t cluster_id, ezb_af_ep_desc_t ep_desc, uint8_t role_mask)
ezb_af_ep_desc_t esphome_zb_zha_default_ep_desc_create(uint8_t ep_id, uint16_t device_id, uint8_t power_source)