ESPHome 2026.8.0b4
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 if (status == RET_OK) {
34 on_start_();
35 }
36 break;
37 case ZB_ZDO_SIGNAL_PRODUCTION_CONFIG_READY:
38 ESP_LOGD(TAG, "ZB_ZDO_SIGNAL_PRODUCTION_CONFIG_READY, status: %d", status);
39 break;
40 case ZB_ZDO_SIGNAL_LEAVE:
41 ESP_LOGD(TAG, "ZB_ZDO_SIGNAL_LEAVE, status: %d", status);
42 break;
43 case ZB_BDB_SIGNAL_DEVICE_REBOOT:
44 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_DEVICE_REBOOT, status: %d", status);
45 if (status == RET_OK) {
46 on_join_(false);
47 }
48 break;
49 case ZB_BDB_SIGNAL_STEERING:
50 break;
51 case ZB_COMMON_SIGNAL_CAN_SLEEP:
52 ESP_LOGV(TAG, "ZB_COMMON_SIGNAL_CAN_SLEEP, status: %d", status);
53 break;
54 case ZB_BDB_SIGNAL_DEVICE_FIRST_START:
55 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_DEVICE_FIRST_START, status: %d", status);
56 break;
57 case ZB_NLME_STATUS_INDICATION:
58 ESP_LOGD(TAG, "ZB_NLME_STATUS_INDICATION, status: %d", status);
59 break;
60 case ZB_BDB_SIGNAL_TC_REJOIN_DONE:
61 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_TC_REJOIN_DONE, status: %d", status);
62 break;
63 default:
64 ESP_LOGD(TAG, "zboss_signal_handler sig: %d, status: %d", sig, status);
65 break;
66 }
67
68 auto before = millis();
69 auto err = zigbee_default_signal_handler(bufid);
70 if (err != RET_OK) {
71 ESP_LOGE(TAG, "Zigbee_default_signal_handler ERROR %u [%s]", err, zb_error_to_string_get(err));
72 }
73
74 if (sig == ZB_COMMON_SIGNAL_CAN_SLEEP) {
75 this->sleep_remainder_ += millis() - before;
76 uint32_t seconds = this->sleep_remainder_ / 1000;
77 this->sleep_remainder_ -= seconds * 1000;
78 this->sleep_time_ += seconds;
79 }
80
81 switch (sig) {
82 case ZB_BDB_SIGNAL_STEERING:
83 ESP_LOGD(TAG, "ZB_BDB_SIGNAL_STEERING, status: %d", status);
84 if (status == RET_OK) {
85 zb_ext_pan_id_t extended_pan_id;
86 char ieee_addr_buf[IEEE_ADDR_BUF_SIZE] = {0};
87 int addr_len;
88
89 zb_get_extended_pan_id(extended_pan_id);
90 addr_len = ieee_addr_to_str(ieee_addr_buf, sizeof(ieee_addr_buf), extended_pan_id);
91
92 for (int i = 0; i < addr_len; ++i) {
93 if (ieee_addr_buf[i] != '0') {
94 on_join_(true);
95 break;
96 }
97 }
98 }
99 break;
100 }
101
102 /* All callbacks should either reuse or free passed buffers.
103 * If bufid == 0, the buffer is invalid (not passed).
104 */
105 if (bufid) {
106 zb_buf_free(bufid);
107 }
108}
109
110void ZigbeeComponent::zcl_device_cb(zb_bufid_t bufid) {
111 zb_zcl_device_callback_param_t *p_device_cb_param = ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t);
112 zb_zcl_device_callback_id_t device_cb_id = p_device_cb_param->device_cb_id;
113 zb_uint16_t cluster_id = p_device_cb_param->cb_param.set_attr_value_param.cluster_id;
114 zb_uint16_t attr_id = p_device_cb_param->cb_param.set_attr_value_param.attr_id;
115 auto endpoint = p_device_cb_param->endpoint;
116
117 ESP_LOGI(TAG, "%s id %hd, cluster_id %d, attr_id %d, endpoint: %d", __func__, device_cb_id, cluster_id, attr_id,
118 endpoint);
119
120 /* Set default response value. */
121 p_device_cb_param->status = RET_OK;
122
124
125 // endpoints are enumerated from 1
126 if (global_zigbee->callbacks_.size() >= endpoint) {
127 const auto &cb = global_zigbee->callbacks_[endpoint - 1];
128 if (cb) {
129 cb(bufid);
130 return;
131 }
132 }
133 p_device_cb_param->status = RET_NOT_IMPLEMENTED;
134}
135
136void ZigbeeComponent::on_join_(bool factory_new) {
137 this->defer([this, factory_new]() {
138 ESP_LOGD(TAG, "Joined the network");
139 this->join_cb_.call(factory_new);
140 });
141}
142
144 this->defer([this]() {
145 ESP_LOGD(TAG, "Started zigbee stack");
146 this->start_cb_.call();
147 });
148}
149
150#ifdef USE_ZIGBEE_WIPE_ON_BOOT
152 const struct flash_area *fap;
153 flash_area_open(area, &fap);
154 flash_area_erase(fap, 0, fap->fa_size);
155 flash_area_close(fap);
156}
157#endif
158
160 global_zigbee = this;
161 auto err = settings_subsys_init();
162 if (err) {
163 ESP_LOGE(TAG, "Failed to initialize settings subsystem, err: %d", err);
164 return;
165 }
166
167#ifdef USE_ZIGBEE_WIPE_ON_BOOT
168 bool wipe = true;
169#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
170 // unique hash to store preferences for this component
171 uint32_t hash = 88498616UL;
172 uint32_t wipe_value = 0;
173 auto wipe_pref = global_preferences->make_preference<uint32_t>(hash, true);
174 if (wipe_pref.load(&wipe_value)) {
175 wipe = wipe_value != USE_ZIGBEE_WIPE_ON_BOOT_MAGIC;
176 ESP_LOGD(TAG, "Wipe value in preferences %u, in firmware %u", wipe_value, USE_ZIGBEE_WIPE_ON_BOOT_MAGIC);
177 }
178#endif
179 if (wipe) {
180 erase_flash_(FIXED_PARTITION_ID(ZBOSS_NVRAM));
181 erase_flash_(FIXED_PARTITION_ID(ZBOSS_PRODUCT_CONFIG));
182 erase_flash_(FIXED_PARTITION_ID(SETTINGS_STORAGE));
183#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
184 wipe_value = USE_ZIGBEE_WIPE_ON_BOOT_MAGIC;
185 wipe_pref.save(&wipe_value);
186#endif
187 }
188#endif
189
190 ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb);
191 err = settings_load();
192 if (err) {
193 ESP_LOGE(TAG, "Cannot load settings, err: %d", err);
194 return;
195 }
196#ifdef CONFIG_ZIGBEE_ROLE_END_DEVICE
197 zigbee_configure_sleepy_behavior(this->sleepy_);
198#endif
199 zigbee_enable();
200}
201
202#ifdef ESPHOME_LOG_HAS_CONFIG
203static const char *role() {
204 switch (zb_get_network_role()) {
205 case ZB_NWK_DEVICE_TYPE_COORDINATOR:
206 return "coordinator";
207 case ZB_NWK_DEVICE_TYPE_ROUTER:
208 return "router";
209 case ZB_NWK_DEVICE_TYPE_ED:
210 return "end device";
211 }
212 return "unknown";
213}
214
215static const char *get_wipe_on_boot() {
216#ifdef USE_ZIGBEE_WIPE_ON_BOOT
217#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
218 return "ONCE";
219#else
220 return "YES";
221#endif
222#else
223 return "NO";
224#endif
225}
226#endif
227
229 char ieee_addr_buf[IEEE_ADDR_BUF_SIZE] = {0};
230 zb_ieee_addr_t addr;
231 zb_get_long_address(addr);
232 ieee_addr_to_str(ieee_addr_buf, sizeof(ieee_addr_buf), addr);
233 zb_ext_pan_id_t extended_pan_id;
234 char extended_pan_id_buf[IEEE_ADDR_BUF_SIZE] = {0};
235 zb_get_extended_pan_id(extended_pan_id);
236 ieee_addr_to_str(extended_pan_id_buf, sizeof(extended_pan_id_buf), extended_pan_id);
237 ESP_LOGCONFIG(TAG,
238 "Zigbee\n"
239 " Wipe on boot: %s\n"
240 " Device is joined to the network: %s\n"
241 " Sleep time: %us\n"
242 " Radio sleep time: %us\n"
243 " RX ON when idle: %s\n"
244 " Current channel: %d\n"
245 " Current page: %d\n"
246 " Sleep threshold: %ums\n"
247 " Role: %s\n"
248 " Long addr: 0x%s\n"
249 " Short addr: 0x%04X\n"
250 " Long pan id: 0x%s\n"
251 " Short pan id: 0x%04X",
252 get_wipe_on_boot(), YESNO(zb_zdo_joined()), this->sleep_time_, this->radio_sleep_time_,
253 YESNO(zb_get_rx_on_when_idle()), zb_get_current_channel(), zb_get_current_page(),
254 zb_get_sleep_threshold(), role(), ieee_addr_buf, zb_get_short_address(), extended_pan_id_buf,
255 zb_get_pan_id());
257}
258
259static void send_attribute_report(zb_bufid_t bufid, zb_uint16_t cmd_id) {
260 ESP_LOGD(TAG, "Force zboss scheduler to wake and send attribute report");
261 zb_buf_free(bufid);
262}
263
265
267 this->radio_sleep_remainder_ += ms;
268 uint32_t seconds = this->radio_sleep_remainder_ / 1000;
269 this->radio_sleep_remainder_ -= seconds * 1000;
270 this->radio_sleep_time_ += seconds;
271}
272
274 if (this->force_report_) {
275 this->force_report_ = false;
276 zb_buf_get_out_delayed_ext(send_attribute_report, 0, 0);
277 }
278}
279
281 ESP_LOGD(TAG, "Factory reset");
282 ZB_SCHEDULE_APP_CALLBACK(zb_bdb_reset_via_local_action, 0);
283}
284
285static void log_reporting_info(zb_zcl_reporting_info_t *rep_info) {
286 auto now = millis();
287 ESP_LOGD(TAG, "Reporting: endpoint %d, cluster_id 0x%04X, attr_id 0x%04X, flags 0x%02X, report in %ums", rep_info->ep,
288 rep_info->cluster_id, rep_info->attr_id, rep_info->flags,
289 ZB_ZCL_GET_REPORTING_FLAG(rep_info, ZB_ZCL_REPORT_TIMER_STARTED)
290 ? ZB_TIME_BEACON_INTERVAL_TO_MSEC(rep_info->run_time) - now
291 : 0);
292 ESP_LOGD(TAG, " min_interval %ds, max_interval %ds, def_min_interval %ds, def_max_interval %ds",
293 rep_info->u.send_info.min_interval, rep_info->u.send_info.max_interval,
294 rep_info->u.send_info.def_min_interval, rep_info->u.send_info.def_max_interval);
295}
296
298#ifdef ESPHOME_LOG_HAS_VERBOSE
299 for (zb_uint8_t j = 0; j < ZCL_CTX().device_ctx->ep_count; j++) {
300 if (ZCL_CTX().device_ctx->ep_desc_list[j]->reporting_info) {
301 zb_zcl_reporting_info_t *rep_info = ZCL_CTX().device_ctx->ep_desc_list[j]->reporting_info;
302 for (zb_uint8_t i = 0; i < ZCL_CTX().device_ctx->ep_desc_list[j]->rep_info_count; i++) {
303 log_reporting_info(rep_info);
304 rep_info++;
305 }
306 }
307 }
308#endif
309}
310
311void ZigbeeComponent::after_reporting_info(zb_zcl_configure_reporting_req_t *config_rep_req,
312 zb_zcl_attr_addr_info_t *attr_addr_info) {
313#ifdef ESPHOME_LOG_HAS_DEBUG
314 auto *rep_info =
315 zb_zcl_find_reporting_info_manuf(attr_addr_info->src_ep, attr_addr_info->cluster_id, attr_addr_info->cluster_role,
316 config_rep_req->attr_id, attr_addr_info->manuf_code);
317 if (rep_info == nullptr) {
318 ESP_LOGE(TAG,
319 "Failed to resolve reporting info (src_ep=%u cluster_id=0x%04x role=%u attr_id=0x%04x manuf_code=0x%04x)",
320 attr_addr_info->src_ep, attr_addr_info->cluster_id, attr_addr_info->cluster_role, config_rep_req->attr_id,
321 attr_addr_info->manuf_code);
322 return;
323 }
324 log_reporting_info(rep_info);
325#endif
326}
327
328} // namespace esphome::zigbee
329
330extern "C" {
331void zboss_signal_handler(zb_uint8_t param) { esphome::zigbee::global_zigbee->zboss_signal_handler_esphome(param); }
332void zb_osif_serial_put_bytes(const zb_uint8_t *buf, zb_short_t len) {
333 (void) buf;
334 (void) len;
335}
338
339// NOLINTBEGIN(readability-identifier-naming,bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
340extern zb_ret_t __real_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req,
341 zb_zcl_attr_addr_info_t *attr_addr_info);
342
343zb_ret_t __wrap_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req,
344 zb_zcl_attr_addr_info_t *attr_addr_info) {
345 zb_ret_t ret = __real_zb_zcl_put_reporting_info_from_req(config_rep_req, attr_addr_info);
346 esphome::zigbee::global_zigbee->after_reporting_info(config_rep_req, attr_addr_info);
347 return ret;
348}
349
352extern zb_bool_t __real_zb_trans_transmit(zb_uint8_t wait_type, zb_time_t tx_at, zb_uint8_t *tx_buf,
353 zb_uint8_t current_channel);
354
355static uint32_t radio_sleep_start_ms = 0;
356
357static void stop_radio_sleep_timer() {
358 if (radio_sleep_start_ms) {
359 esphome::zigbee::global_zigbee->add_radio_sleep_time_ms(esphome::millis() - radio_sleep_start_ms);
360 }
361 radio_sleep_start_ms = 0;
362}
363
366 radio_sleep_start_ms = esphome::millis();
367}
368
370 stop_radio_sleep_timer();
372}
373
374zb_bool_t __wrap_zb_trans_transmit(zb_uint8_t wait_type, zb_time_t tx_at, zb_uint8_t *tx_buf,
375 zb_uint8_t current_channel) {
376 stop_radio_sleep_timer();
377 return __real_zb_trans_transmit(wait_type, tx_at, tx_buf, current_channel);
378}
379// NOLINTEND(readability-identifier-naming,bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
380}
381#endif
uint8_t status
Definition bl0942.h:8
void defer(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call with a const char* name.
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)
LazyCallbackManager< void()> start_cb_
void on_join_(bool factory_new)
void zboss_signal_handler_esphome(zb_bufid_t bufid)
void add_radio_sleep_time_ms(uint32_t ms)
uint16_t addr_len
int ret
const char *const TAG
Definition spi.cpp:7
const uint8_t IEEE_ADDR_BUF_SIZE
void wake_loop_threadsafe()
Non-ISR: always inline.
const void size_t len
Definition hal.h:64
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.
void __real_zb_trans_enter_receive(void)
zb_bool_t __wrap_zb_trans_transmit(zb_uint8_t wait_type, zb_time_t tx_at, zb_uint8_t *tx_buf, zb_uint8_t current_channel)
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()
zb_bool_t __real_zb_trans_transmit(zb_uint8_t wait_type, zb_time_t tx_at, zb_uint8_t *tx_buf, zb_uint8_t current_channel)
void zb_osif_serial_flush()
void __wrap_zb_trans_enter_receive(void)
void __real_zb_trans_enter_sleep(void)
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)
void __wrap_zb_trans_enter_sleep(void)