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);
40 if (!esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
44 if (ezb_bdb_start_top_level_commissioning(
mode) != EZB_ERR_NONE) {
45 ESP_LOGE(TAG,
"Start top level commissioning failed!");
47 esp_zigbee_lock_release();
51 static uint8_t steering_retry_count = 0;
52 ezb_app_signal_type_t signal_type = ezb_app_signal_get_type(app_signal);
53 switch (signal_type) {
54 case EZB_ZDO_SIGNAL_SKIP_STARTUP:
55 ESP_LOGD(TAG,
"Zigbee stack initialized");
56 ezb_bdb_start_top_level_commissioning(EZB_BDB_MODE_INITIALIZATION);
58 case EZB_BDB_SIGNAL_DEVICE_FIRST_START:
59 case EZB_BDB_SIGNAL_DEVICE_REBOOT: {
60 ezb_bdb_comm_status_t
status = *((ezb_bdb_comm_status_t *) ezb_app_signal_get_params(app_signal));
61 if (
status == EZB_BDB_STATUS_SUCCESS) {
62 ESP_LOGD(TAG,
"Device started up in %sfactory-reset mode", ezb_bdb_is_factory_new() ?
"" :
"non ");
64 if (ezb_bdb_is_factory_new()) {
66 ESP_LOGD(TAG,
"Start network steering");
67 ezb_bdb_start_top_level_commissioning(EZB_BDB_MODE_NETWORK_STEERING);
69 ESP_LOGD(TAG,
"Device rebooted");
70 global_zigbee->
joined =
true;
74 ESP_LOGW(TAG,
"The %s failed with status(0x%02x), please retry", ezb_app_signal_to_string(signal_type),
status);
80 case EZB_BDB_SIGNAL_STEERING: {
81 ezb_bdb_comm_status_t
status = *((ezb_bdb_comm_status_t *) ezb_app_signal_get_params(app_signal));
82 if (
status == EZB_BDB_STATUS_SUCCESS) {
83 steering_retry_count = 0;
84 ezb_extpanid_t extended_pan_id;
85 ezb_nwk_get_extended_panid(&extended_pan_id);
86 ESP_LOGD(TAG,
"Joined network successfully: PAN ID(0x%04hx, EXT: 0x%llx), Channel(%d), Short Address(0x%04hx)",
87 ezb_nwk_get_panid(), extended_pan_id.u64, ezb_nwk_get_current_channel(), ezb_nwk_get_short_address());
88 global_zigbee->
joined =
true;
91 ESP_LOGD(TAG,
"Failed to join network with status(0x%02x)",
status);
92 if (steering_retry_count < 10) {
93 steering_retry_count++;
98 global_zigbee->
set_timeout(
"zb_init", 600 * 1000, []() {
104 case EZB_ZDO_SIGNAL_LEAVE: {
105 const ezb_zdo_signal_leave_params_t *leave_params =
106 (
const ezb_zdo_signal_leave_params_t *) ezb_app_signal_get_params(app_signal);
107 if (leave_params->leave_type == EZB_ZDO_LEAVE_TYPE_RESET) {
108 esp_zigbee_factory_reset();
112 ESP_LOGD(TAG,
"Zigbee APP Signal: %s(type: 0x%02x)", ezb_app_signal_to_string(signal_type), signal_type);
118static void zb_attribute_handler(ezb_zcl_set_attr_value_message_t *
message) {
119 ESP_RETURN_ON_FALSE(
message, , TAG,
"Empty message");
120 ESP_RETURN_ON_FALSE(
message->info.status == EZB_ZCL_STATUS_SUCCESS, , TAG,
"Received message: error status(%d)",
122 ESP_LOGD(TAG,
"ZCL SetAttributeValue message for endpoint(%d) cluster(0x%04x) %s with status(0x%02x)",
124 message->info.cluster_role == EZB_ZCL_CLUSTER_SERVER ?
"server" :
"client",
message->info.status);
127static void zb_action_handler(ezb_zcl_core_action_callback_id_t callback_id,
void *
message) {
128 switch (callback_id) {
129 case EZB_ZCL_CORE_SET_ATTR_VALUE_CB_ID:
130 zb_attribute_handler((ezb_zcl_set_attr_value_message_t *)
message);
132 case EZB_ZCL_CORE_DEFAULT_RSP_CB_ID: {
133#ifdef ESPHOME_LOG_HAS_VERBOSE
134 ezb_zcl_cmd_default_rsp_message_t *default_rsp = (ezb_zcl_cmd_default_rsp_message_t *)
message;
135 ESP_LOGV(TAG,
"Received ZCL Default Response: 0x%02x", default_rsp->in.status_code);
139 ESP_LOGD(TAG,
"Receive Zigbee action(0x%04x) callback",
static_cast<unsigned>(callback_id));
145 ezb_af_ep_config_t config = {
146 .ep_id = endpoint_id,
147 .app_profile_id = EZB_AF_HA_PROFILE_ID,
148 .app_device_id = device_id,
149 .app_device_version = 0,
151 ezb_af_ep_desc_t ep_desc = ezb_af_create_endpoint_desc(&config);
152 if (ezb_af_device_add_endpoint_desc(this->
dev_desc_, ep_desc) != EZB_ERR_NONE) {
153 ESP_LOGE(TAG,
"Could not create endpoint %u", endpoint_id);
158 this->
add_cluster(endpoint_id, EZB_ZCL_CLUSTER_ID_IDENTIFY, EZB_ZCL_CLUSTER_SERVER);
162 if (cluster_id == EZB_ZCL_CLUSTER_ID_BASIC) {
165 ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->
dev_desc_, endpoint_id);
166 if (ep_desc == NULL) {
167 ESP_LOGE(TAG,
"Endpoint %u does not exist, cannot add cluster 0x%04X", endpoint_id, cluster_id);
171 ESP_LOGD(TAG,
"Endpoint %u: Added cluster 0x%04X with role %u", endpoint_id, cluster_id, role);
177 struct tm *timeinfo =
localtime(&time_val);
178 strftime(date_buf,
sizeof(date_buf),
"%Y%m%d %H%M%S", timeinfo);
188 ezb_zcl_cluster_desc_t cluster_desc =
189 ezb_af_endpoint_get_cluster_desc(ep_desc, EZB_ZCL_CLUSTER_ID_BASIC, EZB_ZCL_CLUSTER_SERVER);
190 if (cluster_desc == NULL) {
191 ezb_zcl_basic_cluster_config_t basic_cluster_cfg = {
192 .zcl_version = EZB_ZCL_BASIC_ZCL_VERSION_DEFAULT_VALUE,
195 cluster_desc = ezb_zcl_basic_create_cluster_desc(&basic_cluster_cfg, EZB_ZCL_CLUSTER_SERVER);
197 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_MANUFACTURER_NAME_ID,
199 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_MODEL_IDENTIFIER_ID,
201 ezb_zcl_basic_cluster_desc_add_attr(cluster_desc, EZB_ZCL_ATTR_BASIC_DATE_CODE_ID, this->
basic_cluster_data_.date);
202 ezb_af_endpoint_add_cluster_desc(ep_desc, cluster_desc);
206 if (ezb_af_device_desc_register(this->
dev_desc_) != EZB_ERR_NONE) {
207 ESP_LOGE(TAG,
"Could not register the endpoint list");
214static void ezb_task(
void *pv_parameters) {
219 if (esp_zigbee_start(
false) != ESP_OK) {
220 ESP_LOGE(TAG,
"Could not setup Zigbee");
227 vTaskPrioritySet(NULL, 5);
229 esp_zigbee_launch_mainloop();
237 esp_zigbee_platform_config_t platform_config = {
238 .storage_partition_name =
"nvs",
239 .radio_config = EZB_DEFAULT_RADIO_CONFIG(),
241 esp_zigbee_device_config_t device_config = {
243 .install_code_policy =
false,
246 esp_zigbee_zczr_config_s zb_zczr_cfg = {
247 .max_children = MAX_CHILDREN,
249 device_config.zczr_config = zb_zczr_cfg;
251 esp_zigbee_zed_config_s zb_zed_cfg = {
252 .ed_timeout = EZB_NWK_ED_TIMEOUT_64MIN,
253 .keep_alive = ED_KEEP_ALIVE,
255 device_config.zed_config = zb_zed_cfg;
257 esp_zigbee_config_t config = {.device_config = device_config, .platform_config = platform_config};
258 if (esp_zigbee_init(&config) != ESP_OK) {
259 ESP_LOGE(TAG,
"Could not initialize Zigbee");
263 this->
dev_desc_ = ezb_af_create_device_desc();
267 global_zigbee =
this;
269 if (esp_coex_wifi_i154_enable() != ESP_OK) {
274 ezb_aps_secur_enable_distributed_security(
false);
275 ezb_nwk_set_min_join_lqi(32);
277 ESP_LOGE(TAG,
"Could not set application signal handler");
282 ezb_zcl_core_action_handler_register(zb_action_handler);
284 if (ezb_bdb_set_primary_channel_set(EZB_PRIMARY_CHANNEL_MASK) != ESP_OK) {
285 ESP_LOGE(TAG,
"Could not setup Zigbee");
291 : EZB_AF_NODE_POWER_SOURCE_CONSTANT_POWER);
292 ezb_af_node_power_desc_t desc = {
293 .current_power_mode = EZB_AF_NODE_POWER_MODE_SYNC_ON_WHEN_IDLE,
296 .current_power_source_level = EZB_AF_NODE_POWER_SOURCE_LEVEL_100_PERCENT,
298 ezb_af_set_node_power_desc(&desc);
301 xTaskCreate(ezb_task,
"Zigbee_main", 4096, NULL, 1, NULL);
306 if (this->
joined.exchange(
false)) {
314 if (esp_zigbee_lock_acquire(10 / portTICK_PERIOD_MS)) {
319 " Device is joined to the network: %s\n"
320 " Current channel: %d\n"
321 " Short addr: 0x%04X\n"
322 " Short pan id: 0x%04X",
324 reinterpret_cast<const char *
>(this->basic_cluster_data_.model + 1),
325 YESNO(this->
device_role_ == EZB_NWK_DEVICE_TYPE_ROUTER), YESNO(ezb_bdb_dev_joined()),
326 ezb_nwk_get_current_channel(), ezb_nwk_get_short_address(), ezb_nwk_get_panid());
327 esp_zigbee_lock_release();
334 reinterpret_cast<const char *
>(this->basic_cluster_data_.model + 1),
335 YESNO(this->
device_role_ == EZB_NWK_DEVICE_TYPE_ROUTER));
BedjetMode mode
BedJet operating mode.
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 set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
void disable_loop()
Disable this component's loop.
CallbackManager< void(bool)> join_cb_
bool is_battery_powered()
void create_default_cluster(uint8_t endpoint_id, uint16_t device_id)
struct esphome::zigbee::ZigbeeComponent::@200 basic_cluster_data_
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)
std::atomic< bool > joined
static void esp_zigbee_alarm_bdb_commissioning(ezb_bdb_comm_mode_mask_t mode)
static bool app_signal_handler(const ezb_app_signal_t *app_signal)
std::atomic< bool > factory_new
ezb_nwk_device_type_t device_role_
void dump_config() override
void set_basic_cluster(const char *model, const char *manufacturer, uint8_t power_source)
std::atomic< bool > started
ezb_af_device_desc_t dev_desc_
const LogString * message
uint8_t * get_zcl_string(const char *str, uint8_t max_size, bool use_max_size)
Application App
Global storage of Application pointer - only one Application can exist.
struct tm * localtime(const time_t *timer)
ezb_err_t esphome_zb_add_or_update_cluster(uint16_t cluster_id, ezb_af_ep_desc_t ep_desc, uint8_t role_mask)