ESPHome 2026.10.2
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"
5#include <zephyr/settings/settings.h>
6#include <zephyr/storage/flash_map.h>
7#include "esphome/core/hal.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 });
142}
143
145 this->defer([this]() {
146 ESP_LOGD(TAG, "Started zigbee stack");
147 this->start_cb_.call();
148 });
150}
151
152#ifdef USE_ZIGBEE_WIPE_ON_BOOT
154 const struct flash_area *fap;
155 flash_area_open(area, &fap);
156 flash_area_erase(fap, 0, fap->fa_size);
157 flash_area_close(fap);
158}
159#endif
160
162 global_zigbee = this;
163 auto err = settings_subsys_init();
164 if (err) {
165 ESP_LOGE(TAG, "Failed to initialize settings subsystem, err: %d", err);
166 return;
167 }
168
169#ifdef USE_ZIGBEE_WIPE_ON_BOOT
170 bool wipe = true;
171#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
172 // unique hash to store preferences for this component
173 uint32_t hash = 88498616UL;
174 uint32_t wipe_value = 0;
175 auto wipe_pref = global_preferences->make_preference<uint32_t>(hash, true);
176 if (wipe_pref.load(&wipe_value)) {
177 wipe = wipe_value != USE_ZIGBEE_WIPE_ON_BOOT_MAGIC;
178 ESP_LOGD(TAG, "Wipe value in preferences %u, in firmware %u", wipe_value, USE_ZIGBEE_WIPE_ON_BOOT_MAGIC);
179 }
180#endif
181 if (wipe) {
182 erase_flash_(FIXED_PARTITION_ID(ZBOSS_NVRAM));
183 erase_flash_(FIXED_PARTITION_ID(ZBOSS_PRODUCT_CONFIG));
184 erase_flash_(FIXED_PARTITION_ID(SETTINGS_STORAGE));
185#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
186 wipe_value = USE_ZIGBEE_WIPE_ON_BOOT_MAGIC;
187 wipe_pref.save(&wipe_value);
188#endif
189 }
190#endif
191
192 ZB_ZCL_REGISTER_DEVICE_CB(zcl_device_cb);
193 err = settings_load();
194 if (err) {
195 ESP_LOGE(TAG, "Cannot load settings, err: %d", err);
196 return;
197 }
198#ifdef CONFIG_ZIGBEE_ROLE_END_DEVICE
199 zigbee_configure_sleepy_behavior(this->sleepy_);
200#endif
201 zigbee_enable();
202}
203
204#ifdef ESPHOME_LOG_HAS_CONFIG
205static const char *role() {
206 switch (zb_get_network_role()) {
207 case ZB_NWK_DEVICE_TYPE_COORDINATOR:
208 return "coordinator";
209 case ZB_NWK_DEVICE_TYPE_ROUTER:
210 return "router";
211 case ZB_NWK_DEVICE_TYPE_ED:
212 return "end device";
213 }
214 return "unknown";
215}
216
217static const char *get_wipe_on_boot() {
218#ifdef USE_ZIGBEE_WIPE_ON_BOOT
219#ifdef USE_ZIGBEE_WIPE_ON_BOOT_MAGIC
220 return "ONCE";
221#else
222 return "YES";
223#endif
224#else
225 return "NO";
226#endif
227}
228#endif
229
231 char ieee_addr_buf[IEEE_ADDR_BUF_SIZE] = {0};
232 zb_ieee_addr_t addr;
233 zb_get_long_address(addr);
234 ieee_addr_to_str(ieee_addr_buf, sizeof(ieee_addr_buf), addr);
235 zb_ext_pan_id_t extended_pan_id;
236 char extended_pan_id_buf[IEEE_ADDR_BUF_SIZE] = {0};
237 zb_get_extended_pan_id(extended_pan_id);
238 ieee_addr_to_str(extended_pan_id_buf, sizeof(extended_pan_id_buf), extended_pan_id);
239 ESP_LOGCONFIG(TAG,
240 "Zigbee\n"
241 " Wipe on boot: %s\n"
242 " Device is joined to the network: %s\n"
243 " Sleep time: %us\n"
244 " Radio sleep time: %us\n"
245 " RX ON when idle: %s\n"
246 " Current channel: %d\n"
247 " Current page: %d\n"
248 " Sleep threshold: %ums\n"
249 " Role: %s\n"
250 " Long addr: 0x%s\n"
251 " Short addr: 0x%04X\n"
252 " Long pan id: 0x%s\n"
253 " Short pan id: 0x%04X",
254 get_wipe_on_boot(), YESNO(zb_zdo_joined()), this->sleep_time_, this->radio_sleep_time_,
255 YESNO(zb_get_rx_on_when_idle()), zb_get_current_channel(), zb_get_current_page(),
256 zb_get_sleep_threshold(), role(), ieee_addr_buf, zb_get_short_address(), extended_pan_id_buf,
257 zb_get_pan_id());
259}
260
261static void send_attribute_report(zb_bufid_t bufid, zb_uint16_t cmd_id) {
262 ESP_LOGD(TAG, "Force zboss scheduler to wake and send attribute report");
263 zb_buf_free(bufid);
264}
265
267
269 this->radio_sleep_remainder_ += ms;
270 uint32_t seconds = this->radio_sleep_remainder_ / 1000;
271 this->radio_sleep_remainder_ -= seconds * 1000;
272 this->radio_sleep_time_ += seconds;
273}
274
276 if (this->force_report_) {
277 this->force_report_ = false;
278 zb_buf_get_out_delayed_ext(send_attribute_report, 0, 0);
279 }
280}
281
283 ESP_LOGD(TAG, "Factory reset");
284 ZB_SCHEDULE_APP_CALLBACK(zb_bdb_reset_via_local_action, 0);
285}
286
287static void log_reporting_info(zb_zcl_reporting_info_t *rep_info) {
288 auto now = millis();
289 ESP_LOGD(TAG, "Reporting: endpoint %d, cluster_id 0x%04X, attr_id 0x%04X, flags 0x%02X, report in %ums", rep_info->ep,
290 rep_info->cluster_id, rep_info->attr_id, rep_info->flags,
291 ZB_ZCL_GET_REPORTING_FLAG(rep_info, ZB_ZCL_REPORT_TIMER_STARTED)
292 ? ZB_TIME_BEACON_INTERVAL_TO_MSEC(rep_info->run_time) - now
293 : 0);
294 ESP_LOGD(TAG, " min_interval %ds, max_interval %ds, def_min_interval %ds, def_max_interval %ds",
295 rep_info->u.send_info.min_interval, rep_info->u.send_info.max_interval,
296 rep_info->u.send_info.def_min_interval, rep_info->u.send_info.def_max_interval);
297}
298
300#ifdef ESPHOME_LOG_HAS_VERBOSE
301 for (zb_uint8_t j = 0; j < ZCL_CTX().device_ctx->ep_count; j++) {
302 if (ZCL_CTX().device_ctx->ep_desc_list[j]->reporting_info) {
303 zb_zcl_reporting_info_t *rep_info = ZCL_CTX().device_ctx->ep_desc_list[j]->reporting_info;
304 for (zb_uint8_t i = 0; i < ZCL_CTX().device_ctx->ep_desc_list[j]->rep_info_count; i++) {
305 log_reporting_info(rep_info);
306 rep_info++;
307 }
308 }
309 }
310#endif
311}
312
313void ZigbeeComponent::after_reporting_info(zb_zcl_configure_reporting_req_t *config_rep_req,
314 zb_zcl_attr_addr_info_t *attr_addr_info) {
315#ifdef ESPHOME_LOG_HAS_DEBUG
316 auto *rep_info =
317 zb_zcl_find_reporting_info_manuf(attr_addr_info->src_ep, attr_addr_info->cluster_id, attr_addr_info->cluster_role,
318 config_rep_req->attr_id, attr_addr_info->manuf_code);
319 if (rep_info == nullptr) {
320 ESP_LOGE(TAG,
321 "Failed to resolve reporting info (src_ep=%u cluster_id=0x%04x role=%u attr_id=0x%04x manuf_code=0x%04x)",
322 attr_addr_info->src_ep, attr_addr_info->cluster_id, attr_addr_info->cluster_role, config_rep_req->attr_id,
323 attr_addr_info->manuf_code);
324 return;
325 }
326 log_reporting_info(rep_info);
327#endif
328}
329
330} // namespace esphome::zigbee
331
332extern "C" {
333void zboss_signal_handler(zb_uint8_t param) { esphome::zigbee::global_zigbee->zboss_signal_handler_esphome(param); }
334void zb_osif_serial_put_bytes(const zb_uint8_t *buf, zb_short_t len) {
335 (void) buf;
336 (void) len;
337}
340
341// NOLINTBEGIN(readability-identifier-naming,bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
342extern zb_ret_t __real_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req,
343 zb_zcl_attr_addr_info_t *attr_addr_info);
344
345zb_ret_t __wrap_zb_zcl_put_reporting_info_from_req(zb_zcl_configure_reporting_req_t *config_rep_req,
346 zb_zcl_attr_addr_info_t *attr_addr_info) {
347 zb_ret_t ret = __real_zb_zcl_put_reporting_info_from_req(config_rep_req, attr_addr_info);
348 esphome::zigbee::global_zigbee->after_reporting_info(config_rep_req, attr_addr_info);
349 return ret;
350}
351
354extern zb_bool_t __real_zb_trans_transmit(zb_uint8_t wait_type, zb_time_t tx_at, zb_uint8_t *tx_buf,
355 zb_uint8_t current_channel);
356
357static uint32_t radio_sleep_start_ms = 0;
358
359static void stop_radio_sleep_timer() {
360 if (radio_sleep_start_ms) {
361 esphome::zigbee::global_zigbee->add_radio_sleep_time_ms(esphome::millis() - radio_sleep_start_ms);
362 }
363 radio_sleep_start_ms = 0;
364}
365
368 radio_sleep_start_ms = esphome::millis();
369}
370
372 stop_radio_sleep_timer();
374}
375
376zb_bool_t __wrap_zb_trans_transmit(zb_uint8_t wait_type, zb_time_t tx_at, zb_uint8_t *tx_buf,
377 zb_uint8_t current_channel) {
378 stop_radio_sleep_timer();
379 return __real_zb_trans_transmit(wait_type, tx_at, tx_buf, current_channel);
380}
381// NOLINTEND(readability-identifier-naming,bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
382}
383#endif
uint8_t status
Definition bl0942.h:8
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
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
const void size_t len
Definition hal.h:64
ESPPreferences * global_preferences
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
ESPPreferenceObject make_preference(size_t, uint32_t, bool)
Definition preferences.h:24
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)