ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
zigbee_esp32.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_ESP32
5#ifdef USE_ZIGBEE
6
7#include <map>
8#include <tuple>
9#include <atomic>
10
11#include "esp_zigbee.h"
12#include "ezbee/zha.h"
18#include "zigbee_event_esp32.h"
19
20namespace esphome::zigbee {
21
22/* Zigbee configuration */
23static const uint8_t MAX_CHILDREN = 10;
24static const uint32_t EZB_PRIMARY_CHANNEL_MASK = 0x07FFF800U; /* channels 11-26 */
25static constexpr uint8_t MAX_ZB_QUEUE_SIZE = 32;
26
27#define EZB_DEFAULT_RADIO_CONFIG() \
28 { \
29 .radio_mode = ESP_ZIGBEE_RADIO_MODE_NATIVE, \
30 }
31
32uint8_t *get_zcl_string(const char *str, uint8_t max_size, bool use_max_size = false);
33
34class ZigbeeAttribute;
35
36class ZigbeeComponent final : public Component {
37 public:
38 void setup() override;
39 void loop() override;
40 void dump_config() override;
41
42 void set_sleepy(bool sleepy) { this->sleepy_ = sleepy; }
43 void set_keep_alive(uint32_t keep_alive) { this->keep_alive_ = keep_alive; }
44 void set_basic_cluster(const char *model, const char *manufacturer, const char *location, const char *sw_build_id,
45 uint8_t power_source, uint8_t app_version);
46 void add_cluster(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role);
47 void create_default_cluster(uint8_t endpoint_id, uint16_t device_id);
48 bool register_device();
49
50 template<typename T>
51 void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
52 uint8_t max_size, T value);
53
54 template<typename T>
55 void add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value);
56
57 static bool app_signal_handler(const ezb_app_signal_t *app_signal);
58 static void esp_zigbee_alarm_bdb_commissioning(ezb_bdb_comm_mode_mask_t mode);
59
60 void factory_reset();
61 void report();
62
63 template<typename F> void add_on_join_callback(F &&cb) { this->join_cb_.add(std::forward<F>(cb)); }
64 template<typename F> void add_on_start_callback(F &&cb) { this->start_cb_.add(std::forward<F>(cb)); }
65
66 bool is_battery_powered() { return this->basic_cluster_data_.power_source == EZB_ZCL_BASIC_POWER_SOURCE_BATTERY; }
67
68 // True after the Zigbee stack has been initialized and the device has started up. Is set before the stack started
69 // network commissioning or has joined a network and won't be reset until the device is rebooted.
70 bool is_started() { return this->started_; }
71
72 // True if the device has joined a network and is ready to send and receive messages.
73 bool is_joined() { return this->joined_; }
74
75 protected:
76 bool sleepy_{false};
78 struct {
79 uint8_t *model;
80 uint8_t *manufacturer;
81 uint8_t *date;
82 uint8_t *location;
83 uint8_t *sw_build_id;
84 uint8_t power_source;
85 uint8_t app_version;
87#ifdef CONFIG_ZB_ZED
88 ezb_nwk_device_type_t device_role_ = EZB_NWK_DEVICE_TYPE_END_DEVICE;
89#else
90 ezb_nwk_device_type_t device_role_ = EZB_NWK_DEVICE_TYPE_ROUTER;
91#endif
92 void update_basic_cluster_(ezb_af_ep_desc_t ep_desc);
94 void end_parent_link_failures_(uint8_t seq);
96 template<typename T>
97 void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
98 T *value_p);
99 void handle_attribute_(ezb_zcl_message_info_t info, ezb_zcl_attribute_t attribute);
100 // attributes_ will be used during operation in zigbee callbacks to update the attribute values and trigger
101 // automations
102 // key tuple could be replaced by single 64 (48) bit int with bit fields for endpoint, cluster, role and attr_id
103 std::map<std::tuple<uint8_t, uint16_t, uint8_t, uint16_t>, ZigbeeAttribute *> attributes_;
104 ezb_af_device_desc_t dev_desc_ = ezb_af_create_device_desc();
106 template<typename... Args> friend void enqueue_zb_event(Args... args);
108 // Pool sized to queue capacity (SIZE-1) because LockFreeQueue<T,N> is a ring
109 // buffer that holds N-1 elements (one slot distinguishes full from empty).
110 // This guarantees allocate() returns nullptr before push() can fail, which:
111 // 1. Prevents leaking a pool slot (the Nth allocate succeeds but push fails)
112 // 2. Avoids needing release() on the producer path after a failed push(),
113 // preserving the SPSC contract on the pool's internal free list
116 bool start_reported_{false};
117 std::atomic<bool> started_ = false;
118 std::atomic<bool> joined_ = false;
119 std::atomic<bool> join_pending_ = false;
120 std::atomic<bool> factory_new_ = false;
121 // Current series of parent link failures, only used with the Zigbee stack lock held
125 uint8_t link_failures_{0};
127};
128
129template<typename T>
130void ZigbeeComponent::add_attr(uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id,
131 uint8_t max_size, T value) {
132 this->add_attr<T>(nullptr, endpoint_id, cluster_id, role, attr_id, max_size, value);
133}
134
135template<typename T>
136void ZigbeeComponent::add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
137 uint16_t attr_id, uint8_t max_size, T value) {
138 // The size byte of the zcl_str must be set to the maximum value,
139 // even though the initial string may be shorter.
140 if constexpr (std::is_same<T, std::string>::value) {
141 auto zcl_str = get_zcl_string(value.c_str(), max_size, true);
142 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, zcl_str);
143 delete[] zcl_str;
144 } else if constexpr (std::is_convertible<T, const char *>::value) {
145 auto zcl_str = get_zcl_string(value, max_size, true);
146 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, zcl_str);
147 delete[] zcl_str;
148 } else {
149 add_attr_(attr, endpoint_id, cluster_id, role, attr_id, &value);
150 }
151}
152
153template<typename T>
154void ZigbeeComponent::add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role,
155 uint16_t attr_id, T *value_p) {
156 ezb_af_ep_desc_t ep_desc = ezb_af_device_get_endpoint_desc(this->dev_desc_, endpoint_id);
157 if (ep_desc == NULL) {
158 return;
159 }
160 ezb_zcl_cluster_desc_t cluster_desc = ezb_af_endpoint_get_cluster_desc(ep_desc, cluster_id, role);
161 if (cluster_desc == NULL) {
162 return;
163 }
164 esphome_zb_cluster_add_or_update_attr(cluster_id, cluster_desc, attr_id, value_p);
165
166 if (attr != nullptr) {
167 this->attributes_[{endpoint_id, cluster_id, role, attr_id}] = attr;
168 }
169}
170
171} // namespace esphome::zigbee
172
173#endif
174#endif
BedjetMode mode
BedJet operating mode.
CallbackManager< void(bool)> join_cb_
void create_default_cluster(uint8_t endpoint_id, uint16_t device_id)
friend void enqueue_zb_event(Args... args)
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)
void set_keep_alive(uint32_t keep_alive)
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_
void add_attr(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, uint8_t max_size, T value)
ezb_af_device_desc_t dev_desc_
void add_attr_(ZigbeeAttribute *attr, uint8_t endpoint_id, uint16_t cluster_id, uint8_t role, uint16_t attr_id, T *value_p)
uint8_t * get_zcl_string(const char *str, uint8_t max_size, bool use_max_size)
uint32_t value
Definition hal.cpp:41
static void uint32_t
uint16_t seq
ezb_err_t esphome_zb_cluster_add_or_update_attr(uint16_t cluster_id, ezb_zcl_cluster_desc_t cluster_desc, uint16_t attr_id, void *value_p)