ESPHome 2026.5.0b1
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 "freertos/FreeRTOS.h"
6#include "freertos/task.h"
7#include "esp_check.h"
8#include "nvs_flash.h"
10#include "zigbee_esp32.h"
12#include "esphome/core/log.h"
14#ifdef USE_WIFI
15#include "esp_coexist.h"
16#endif
17
18namespace esphome::zigbee {
19
20static const char *const TAG = "zigbee";
21
22static ZigbeeComponent *global_zigbee = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
23
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]; // string + length octet
28 zcl_str[0] = zcl_str_size;
29
30 // Initialize payload to avoid leaking uninitialized heap contents and clamp copy length
31 memset(zcl_str + 1, 0, zcl_str_size);
32 uint8_t copy_len = std::min(zcl_str_size, str_len);
33 if (copy_len > 0) {
34 memcpy(zcl_str + 1, str, copy_len);
35 }
36 return zcl_str;
37}
38
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!");
42 }
43}
44
45void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
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;
51 switch (sig_type) {
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);
55 break;
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 ");
60 global_zigbee->started = true;
61 if (esp_zb_bdb_is_factory_new()) {
62 global_zigbee->factory_new = true;
63 ESP_LOGD(TAG, "Start network steering");
64 esp_zb_bdb_start_top_level_commissioning(ESP_ZB_BDB_MODE_NETWORK_STEERING);
65 } else {
66 ESP_LOGD(TAG, "Device rebooted");
67 global_zigbee->joined = true;
68 global_zigbee->enable_loop_soon_any_context();
69 }
70 } else {
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,
75 1000);
76 }
77 break;
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;
84 global_zigbee->enable_loop_soon_any_context();
85 } else {
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);
91 } else {
92 esp_zb_scheduler_alarm((esp_zb_callback_t) bdb_start_top_level_commissioning_cb,
93 ESP_ZB_BDB_MODE_NETWORK_STEERING, 600 * 1000);
94 }
95 }
96 break;
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();
101 }
102 break;
103 default:
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));
106 break;
107 }
108}
109
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)",
116 message->info.dst_endpoint, message->info.cluster, message->attribute.id, message->attribute.data.size);
117 return ret;
118}
119
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);
125 break;
126 default:
127 ESP_LOGD(TAG, "Receive Zigbee action(0x%x) callback", callback_id);
128 break;
129 }
130 return ret;
131}
132
133void ZigbeeComponent::create_default_cluster(uint8_t endpoint_id, zb_ha_standard_devs_e device_id) {
134 esp_zb_cluster_list_t *cluster_list = esp_zb_zcl_cluster_list_create();
135 this->endpoint_list_[endpoint_id] =
136 std::tuple<zb_ha_standard_devs_e, esp_zb_cluster_list_t *>(device_id, cluster_list);
137 // Add basic cluster
138 this->add_cluster(endpoint_id, ESP_ZB_ZCL_CLUSTER_ID_BASIC, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
139 // Add identify cluster if not already present
140 if (esp_zb_cluster_list_get_cluster(cluster_list, ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE) ==
141 nullptr) {
142 this->add_cluster(endpoint_id, ESP_ZB_ZCL_CLUSTER_ID_IDENTIFY, ESP_ZB_ZCL_CLUSTER_SERVER_ROLE);
143 }
144}
145
146void ZigbeeComponent::add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role) {
147 esp_zb_attribute_list_t *attr_list;
148 if (cluster_id == 0) {
149 attr_list = create_basic_cluster_();
150 } else {
151 attr_list = esphome_zb_default_attr_list_create(cluster_id);
152 }
153 this->attribute_list_[{endpoint_id, cluster_id, role}] = attr_list;
154}
155
156void ZigbeeComponent::set_basic_cluster(const char *model, const char *manufacturer, uint8_t power_source) {
157 char date_buf[16];
158 time_t time_val = App.get_build_time();
159 struct tm *timeinfo = localtime(&time_val);
160 strftime(date_buf, sizeof(date_buf), "%Y%m%d %H%M%S", timeinfo);
161 this->basic_cluster_data_ = {
162 .model = get_zcl_string(model, 31),
163 .manufacturer = get_zcl_string(manufacturer, 31),
164 .date = get_zcl_string(date_buf, 15),
165 .power_source = power_source,
166 };
167}
168
169esp_zb_attribute_list_t *ZigbeeComponent::create_basic_cluster_() {
170 esp_zb_basic_cluster_cfg_t basic_cluster_cfg = {
171 .zcl_version = ESP_ZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE,
172 .power_source = this->basic_cluster_data_.power_source,
173 };
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,
176 this->basic_cluster_data_.manufacturer);
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);
179 return attr_list;
180}
181
182esp_err_t ZigbeeComponent::create_endpoint(uint8_t endpoint_id, zb_ha_standard_devs_e device_id,
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);
189}
190
191static void esp_zb_task_(void *pvParameters) {
192 if (esp_zb_start(false) != ESP_OK) {
193 ESP_LOGE(TAG, "Could not setup Zigbee");
194 vTaskDelete(NULL);
195 }
196 if (global_zigbee->is_battery_powered()) {
197 ESP_LOGD(TAG, "Battery powered!");
198 esp_zb_set_node_descriptor_power_source(0);
199 } else {
200 esp_zb_set_node_descriptor_power_source(1);
201 }
202 esp_zb_stack_main_loop();
203}
204
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();
210#ifdef USE_WIFI
211 if (esp_coex_wifi_i154_enable() != ESP_OK) {
212 this->mark_failed();
213 return;
214 }
215#endif
216 if (esp_zb_platform_config(&config) != ESP_OK) {
217 this->mark_failed();
218 return;
219 }
220
221 esp_zb_zed_cfg_t zb_zed_cfg = {
222 .ed_timeout = ESP_ZB_ED_AGING_TIMEOUT_64MIN,
223 .keep_alive = ED_KEEP_ALIVE,
224 };
225 esp_zb_zczr_cfg_t zb_zczr_cfg = {
226 .max_children = MAX_CHILDREN,
227 };
228 esp_zb_cfg_t zb_nwk_cfg = {
229 .esp_zb_role = this->device_role_,
230 .install_code_policy = false,
231 };
232#ifdef ZB_ROUTER_ROLE
233 zb_nwk_cfg.nwk_cfg.zczr_cfg = zb_zczr_cfg;
234#else
235 zb_nwk_cfg.nwk_cfg.zed_cfg = zb_zed_cfg;
236#endif
237 esp_zb_init(&zb_nwk_cfg);
238
239 esp_err_t ret;
240 for (auto const &[key, val] : this->attribute_list_) {
241 esp_zb_cluster_list_t *esp_zb_cluster_list = std::get<1>(this->endpoint_list_[std::get<0>(key)]);
242 ret = esphome_zb_cluster_list_add_or_update_cluster(std::get<1>(key), esp_zb_cluster_list, val, std::get<2>(key));
243 if (ret != ESP_OK) {
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));
246 } else {
247 ESP_LOGD(TAG, "Endpoint %u: Added cluster 0x%04X with role %u", std::get<0>(key), std::get<1>(key),
248 std::get<2>(key));
249#ifdef ESPHOME_LOG_HAS_VERBOSE
250 // Dump cluster attributes in verbose log
251 ESP_LOGV(TAG, "Cluster 0x%04X attributes:", std::get<1>(key));
252 esp_zb_attribute_list_t *attr_list = val;
253 while (attr_list) {
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;
257 }
258#endif
259 }
260 }
261 this->attribute_list_.clear();
262
263 for (auto const &[ep_id, dev_id] : this->endpoint_list_) {
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);
266 }
267 }
268 this->endpoint_list_.clear();
269
270 if (esp_zb_device_register(this->esp_zb_ep_list_) != ESP_OK) {
271 ESP_LOGE(TAG, "Could not register the endpoint list");
272 this->mark_failed();
273 return;
274 }
275
276 esp_zb_core_action_handler_register(zb_action_handler);
277
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");
280 this->mark_failed();
281 return;
282 }
283 for (auto &[_, attribute] : this->attributes_) {
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);
290 }
291 }
292 }
293 xTaskCreate(esp_zb_task_, "Zigbee_main", 4096, NULL, 24, NULL);
294 this->disable_loop(); // loop is only needed for processing events, so disable until we join a network
295}
296
298 if (this->joined.exchange(false)) {
299 this->connected_ = true;
300 this->join_cb_.call(this->factory_new);
301 }
302 this->disable_loop();
303}
304
306 if (esp_zb_lock_acquire(10 / portTICK_PERIOD_MS)) {
307 ESP_LOGCONFIG(TAG,
308 "Zigbee\n"
309 " Model: %s\n"
310 " Router: %s\n"
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",
315 this->basic_cluster_data_.model, YESNO(this->device_role_ == ESP_ZB_DEVICE_TYPE_ROUTER),
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();
319 } else {
320 ESP_LOGCONFIG(TAG,
321 "Zigbee\n"
322 " Model: %s\n"
323 " Router: %s\n",
324 this->basic_cluster_data_.model, YESNO(this->device_role_ == ESP_ZB_DEVICE_TYPE_ROUTER));
325 }
326}
327} // namespace esphome::zigbee
328
329#endif
330#endif
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_
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 > 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 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_
esp_zb_attribute_list_t * create_basic_cluster_()
const char * message
Definition component.cpp:35
mopeka_std_values val[3]
const char *const TAG
Definition spi.cpp:7
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)
Definition posix_tz.cpp:501
static void uint32_t
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)