ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
zigbee_zephyr.cpp
Go to the documentation of this file.
1#include "zigbee_zephyr.h"
2#if defined(USE_ZIGBEE) && defined(USE_NRF52)
3#include "esphome/core/log.h"
4#include <zephyr/settings/settings.h>
5#include <zephyr/storage/flash_map.h>
6#include "esphome/core/hal.h"
7#include "esphome/core/wake.h"
8
9extern "C" {
10#include <zboss_api.h>
11#include <zboss_api_addons.h>
12#include <zb_nrf_platform.h>
13#include <zigbee/zigbee_app_utils.h>
14#include <zb_error_to_string.h>
15}
16
17namespace esphome::zigbee {
18
19static const char *const TAG = "zigbee";
20
21ZigbeeComponent *global_zigbee = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
22
23const uint8_t IEEE_ADDR_BUF_SIZE = 17;
24
26 zb_zdo_app_signal_hdr_t *sig_hndler = nullptr;
27 zb_zdo_app_signal_type_t sig = zb_get_app_signal(bufid, &sig_hndler);
28 zb_ret_t status = ZB_GET_APP_SIGNAL_STATUS(bufid);
29
30 switch (sig) {
31 case ZB_ZDO_SIGNAL_SKIP_STARTUP:
32 ESP_LOGD(TAG, "ZB_ZDO_SIGNAL_SKIP_STARTUP, status: %d", status);
33 break;
34 case ZB_ZDO_SIGNAL_PRODUCTION_CONFIG_READY:
35 ESP_LOGD(TAG, "ZB_ZDO_SIGNAL_PRODUCTION_CONFIG_READY, status: %d", status);
36 break;
37 case ZB_ZDO_SIGNAL_LEAVE:
38 ESP_LOGD(TAG, "ZB_ZDO_SIGNAL_LEAVE, status: %d", status);
39 break;
40 case ZB_BDB_SIGNAL_DEVICE_REBOOT:
41 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_DEVICE_REBOOT, status: %d", status);
42 if (status == RET_OK) {
43 on_join_(false);
44 }
45 break;
46 case ZB_BDB_SIGNAL_STEERING:
47 break;
48 case ZB_COMMON_SIGNAL_CAN_SLEEP:
49 ESP_LOGV(TAG, "ZB_COMMON_SIGNAL_CAN_SLEEP, status: %d", status);
50 break;
51 case ZB_BDB_SIGNAL_DEVICE_FIRST_START:
52 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_DEVICE_FIRST_START, status: %d", status);
53 break;
54 case ZB_NLME_STATUS_INDICATION:
55 ESP_LOGD(TAG, "ZB_NLME_STATUS_INDICATION, status: %d", status);
56 break;
57 case ZB_BDB_SIGNAL_TC_REJOIN_DONE:
58 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_TC_REJOIN_DONE, status: %d", status);
59 break;
60 default:
61 ESP_LOGD(TAG, "zboss_signal_handler sig: %d, status: %d", sig, status);
62 break;
63 }
64
65 auto before = millis();
66 auto err = zigbee_default_signal_handler(bufid);
67 if (err != RET_OK) {
68 ESP_LOGE(TAG, "Zigbee_default_signal_handler ERROR %u [%s]", err, zb_error_to_string_get(err));
69 }
70
71 if (sig == ZB_COMMON_SIGNAL_CAN_SLEEP) {
72 this->sleep_remainder_ += millis() - before;
73 uint32_t seconds = this->sleep_remainder_ / 1000;
74 this->sleep_remainder_ -= seconds * 1000;
75 this->sleep_time_ += seconds;
76 }
77
78 switch (sig) {
79 case ZB_BDB_SIGNAL_STEERING:
80 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_STEERING, status: %d", status);
81 if (status == RET_OK) {
82 zb_ext_pan_id_t extended_pan_id;
83 char ieee_addr_buf[IEEE_ADDR_BUF_SIZE] = {0};
84 int addr_len;
85
86 zb_get_extended_pan_id(extended_pan_id);
87 addr_len = ieee_addr_to_str(ieee_addr_buf, sizeof(ieee_addr_buf), extended_pan_id);
88
89 for (int i = 0; i < addr_len; ++i) {
90 if (ieee_addr_buf[i] != '0') {
91 on_join_(true);
92 break;
93 }
94 }
95 }
96 break;
97 }
98
99 /* All callbacks should either reuse or free passed buffers.
100 * If bufid == 0, the buffer is invalid (not passed).
101 */
102 if (bufid) {
103 zb_buf_free(bufid);
104 }
105}
106
107void ZigbeeComponent::zcl_device_cb(zb_bufid_t bufid) {
108 zb_zcl_device_callback_param_t *p_device_cb_param = ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t);
109 zb_zcl_device_callback_id_t device_cb_id = p_device_cb_param->device_cb_id;
110 zb_uint16_t cluster_id = p_device_cb_param->cb_param.set_attr_value_param.cluster_id;
111 zb_uint16_t attr_id = p_device_cb_param->cb_param.set_attr_value_param.attr_id;
112 auto endpoint = p_device_cb_param->endpoint;
113
114 ESP_LOGI(TAG, "%s id %hd, cluster_id %d, attr_id %d, endpoint: %d", __func__, device_cb_id, cluster_id, attr_id,
115 endpoint);
116
117 /* Set default response value. */
118 p_device_cb_param->status = RET_OK;
119
121
122 // endpoints are enumerated from 1
123 if (global_zigbee->callbacks_.size() >= endpoint) {
124 const auto &cb = global_zigbee->callbacks_[endpoint - 1];
125 if (cb) {
126 cb(bufid);
127 return;
128 }
129 }
130 p_device_cb_param->status = RET_NOT_IMPLEMENTED;
131}
132
133void ZigbeeComponent::on_join_(bool factory_new) {
134 this->defer([this, factory_new]() {
135 ESP_LOGD(TAG, "Joined the network");
136 this->join_cb_.call(factory_new);
137 });
138}
139
140#ifdef USE_ZIGBEE_WIPE_ON_BOOT
142 const struct flash_area *fap;
143 flash_area_open(area, &fap);
144 flash_area_erase(fap, 0, fap->fa_size);
145 flash_area_close(fap);
146}
147#endif
148
150 global_zigbee = this;
151 auto err = settings_subsys_init();
152 if (err) {
153 ESP_LOGE(TAG, "Failed to initialize settings subsystem, err: %d", err);
154 return;
155 }
156
157#ifdef USE_ZIGBEE_WIPE_ON_BOOT
158 bool wipe = true;
159#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
160 // unique hash to store preferences for this component
161 uint32_t hash = 88498616UL;
162 uint32_t wipe_value = 0;
163 auto wipe_pref = global_preferences->make_preference<uint32_t>(hash, true);
164 if (wipe_pref.load(&wipe_value)) {
165 wipe = wipe_value != USE_ZIGBEE_WIPE_ON_BOOT_MAGIC;
166 ESP_LOGD(TAG, "Wipe value in preferences %u, in firmware %u", wipe_value, USE_ZIGBEE_WIPE_ON_BOOT_MAGIC);
167 }
168#endif
169 if (wipe) {
170 erase_flash_(FIXED_PARTITION_ID(ZBOSS_NVRAM));
171 erase_flash_(FIXED_PARTITION_ID(ZBOSS_PRODUCT_CONFIG));
172 erase_flash_(FIXED_PARTITION_ID(SETTINGS_STORAGE));
173#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
174 wipe_value = USE_ZIGBEE_WIPE_ON_BOOT_MAGIC;
175 wipe_pref.save(&wipe_value);
176#endif
177 }
178#endif
179
180 ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb);
181 err = settings_load();
182 if (err) {
183 ESP_LOGE(TAG, "Cannot load settings, err: %d", err);
184 return;
185 }
186#ifdef CONFIG_ZIGBEE_ROLE_END_DEVICE
187 zigbee_configure_sleepy_behavior(this->sleepy_);
188#endif
189 zigbee_enable();
190}
191
192#ifdef ESPHOME_LOG_HAS_CONFIG
193static const char *role() {
194 switch (zb_get_network_role()) {
195 case ZB_NWK_DEVICE_TYPE_COORDINATOR:
196 return "coordinator";
197 case ZB_NWK_DEVICE_TYPE_ROUTER:
198 return "router";
199 case ZB_NWK_DEVICE_TYPE_ED:
200 return "end device";
201 }
202 return "unknown";
203}
204
205static const char *get_wipe_on_boot() {
206#ifdef USE_ZIGBEE_WIPE_ON_BOOT
207#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
208 return "ONCE";
209#else
210 return "YES";
211#endif
212#else
213 return "NO";
214#endif
215}
216#endif
217
219 char ieee_addr_buf[IEEE_ADDR_BUF_SIZE] = {0};
220 zb_ieee_addr_t addr;
221 zb_get_long_address(addr);
222 ieee_addr_to_str(ieee_addr_buf, sizeof(ieee_addr_buf), addr);
223 zb_ext_pan_id_t extended_pan_id;
224 char extended_pan_id_buf[IEEE_ADDR_BUF_SIZE] = {0};
225 zb_get_extended_pan_id(extended_pan_id);
226 ieee_addr_to_str(extended_pan_id_buf, sizeof(extended_pan_id_buf), extended_pan_id);
227 ESP_LOGCONFIG(TAG,
228 "Zigbee\n"
229 " Wipe on boot: %s\n"
230 " Device is joined to the network: %s\n"
231 " Sleep time: %us\n"
232 " RX ON when idle: %s\n"
233 " Current channel: %d\n"
234 " Current page: %d\n"
235 " Sleep threshold: %ums\n"
236 " Role: %s\n"
237 " Long addr: 0x%s\n"
238 " Short addr: 0x%04X\n"
239 " Long pan id: 0x%s\n"
240 " Short pan id: 0x%04X",
241 get_wipe_on_boot(), YESNO(zb_zdo_joined()), this->sleep_time_, YESNO(zb_get_rx_on_when_idle()),
242 zb_get_current_channel(), zb_get_current_page(), zb_get_sleep_threshold(), role(), ieee_addr_buf,
243 zb_get_short_address(), extended_pan_id_buf, zb_get_pan_id());
245}
246
247static void send_attribute_report(zb_bufid_t bufid, zb_uint16_t cmd_id) {
248 ESP_LOGD(TAG, "Force zboss scheduler to wake and send attribute report");
249 zb_buf_free(bufid);
250}
251
253
255 if (this->force_report_) {
256 this->force_report_ = false;
257 zb_buf_get_out_delayed_ext(send_attribute_report, 0, 0);
258 }
259}
260
262 ESP_LOGD(TAG, "Factory reset");
263 ZB_SCHEDULE_APP_CALLBACK(zb_bdb_reset_via_local_action, 0);
264}
265
266static void log_reporting_info(zb_zcl_reporting_info_t *rep_info) {
267 auto now = millis();
268 ESP_LOGD(TAG, "Reporting: endpoint %d, cluster_id 0x%04X, attr_id 0x%04X, flags 0x%02X, report in %ums", rep_info->ep,
269 rep_info->cluster_id, rep_info->attr_id, rep_info->flags,
270 ZB_ZCL_GET_REPORTING_FLAG(rep_info, ZB_ZCL_REPORT_TIMER_STARTED)
271 ? ZB_TIME_BEACON_INTERVAL_TO_MSEC(rep_info->run_time) - now
272 : 0);
273 ESP_LOGD(TAG, " min_interval %ds, max_interval %ds, def_min_interval %ds, def_max_interval %ds",
274 rep_info->u.send_info.min_interval, rep_info->u.send_info.max_interval,
275 rep_info->u.send_info.def_min_interval, rep_info->u.send_info.def_max_interval);
276}
277
279#ifdef ESPHOME_LOG_HAS_VERBOSE
280 for (zb_uint8_t j = 0; j < ZCL_CTX().device_ctx->ep_count; j++) {
281 if (ZCL_CTX().device_ctx->ep_desc_list[j]->reporting_info) {
282 zb_zcl_reporting_info_t *rep_info = ZCL_CTX().device_ctx->ep_desc_list[j]->reporting_info;
283 for (zb_uint8_t i = 0; i < ZCL_CTX().device_ctx->ep_desc_list[j]->rep_info_count; i++) {
284 log_reporting_info(rep_info);
285 rep_info++;
286 }
287 }
288 }
289#endif
290}
291
292void ZigbeeComponent::after_reporting_info(zb_zcl_configure_reporting_req_t *config_rep_req,
293 zb_zcl_attr_addr_info_t *attr_addr_info) {
294#ifdef ESPHOME_LOG_HAS_DEBUG
295 auto *rep_info =
296 zb_zcl_find_reporting_info_manuf(attr_addr_info->src_ep, attr_addr_info->cluster_id, attr_addr_info->cluster_role,
297 config_rep_req->attr_id, attr_addr_info->manuf_code);
298 if (rep_info == nullptr) {
299 ESP_LOGE(TAG,
300 "Failed to resolve reporting info (src_ep=%u cluster_id=0x%04x role=%u attr_id=0x%04x manuf_code=0x%04x)",
301 attr_addr_info->src_ep, attr_addr_info->cluster_id, attr_addr_info->cluster_role, config_rep_req->attr_id,
302 attr_addr_info->manuf_code);
303 return;
304 }
305 log_reporting_info(rep_info);
306#endif
307}
308
309} // namespace esphome::zigbee
310
311extern "C" {
312void zboss_signal_handler(zb_uint8_t param) { esphome::zigbee::global_zigbee->zboss_signal_handler_esphome(param); }
313void zb_osif_serial_put_bytes(const zb_uint8_t *buf, zb_short_t len) {
314 (void) buf;
315 (void) len;
316}
319
320// NOLINTBEGIN(readability-identifier-naming,bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
321extern zb_ret_t __real_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req,
322 zb_zcl_attr_addr_info_t *attr_addr_info);
323
324zb_ret_t __wrap_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req,
325 zb_zcl_attr_addr_info_t *attr_addr_info) {
326 zb_ret_t ret = __real_zb_zcl_put_reporting_info_from_req(config_rep_req, attr_addr_info);
327 esphome::zigbee::global_zigbee->after_reporting_info(config_rep_req, attr_addr_info);
328 return ret;
329}
330// NOLINTEND(readability-identifier-naming,bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
331}
332#endif
uint8_t status
Definition bl0942.h:8
ESPDEPRECATED("Use const char* overload instead. Removed in 2026.7.0", "2026.1.0") void defer(const std voi defer)(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call.
Definition component.h:560
void after_reporting_info(zb_zcl_configure_reporting_req_t *config_rep_req, zb_zcl_attr_addr_info_t *attr_addr_info)
CallbackManager< void(bool)> join_cb_
static void zcl_device_cb(zb_bufid_t bufid)
void on_join_(bool factory_new)
void zboss_signal_handler_esphome(zb_bufid_t bufid)
uint16_t addr_len
const char *const TAG
Definition spi.cpp:7
const uint8_t IEEE_ADDR_BUF_SIZE
void wake_loop_threadsafe()
Non-ISR: always inline.
std::string size_t len
ESPPreferences * global_preferences
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
Definition preferences.h:24
Platform-specific main loop wake primitives.
zb_ret_t __wrap_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req, zb_zcl_attr_addr_info_t *attr_addr_info)
void zb_osif_serial_init()
void zb_osif_serial_flush()
void zboss_signal_handler(zb_uint8_t param)
zb_ret_t __real_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req, zb_zcl_attr_addr_info_t *attr_addr_info)
void zb_osif_serial_put_bytes(const zb_uint8_t *buf, zb_short_t len)