ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
zigbee_time_zephyr.cpp
Go to the documentation of this file.
2#if defined(USE_ZIGBEE) && defined(USE_NRF52) && defined(USE_TIME)
3#include "esphome/core/log.h"
4
5namespace esphome::zigbee {
6
7static const char *const TAG = "zigbee.time";
8
9// This time standard is the number of
10// seconds since 0 hrs 0 mins 0 sec on 1st January 2000 UTC (Universal Coordinated Time).
11constexpr time_t EPOCH_2000 = 946684800;
12
13ZigbeeTime *global_time = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
14
15void ZigbeeTime::sync_time(zb_ret_t status, zb_uint32_t auth_level, zb_uint16_t short_addr, zb_uint8_t endpoint,
16 zb_uint32_t nw_time) {
17 if (status == RET_OK && auth_level >= ZB_ZCL_TIME_HAS_SYNCHRONIZED_BIT) {
19 } else if (status != RET_TIMEOUT || !global_time->has_time_) {
20 ESP_LOGE(TAG, "Status: %d, auth_level: %u, short_addr: %d, endpoint: %d, nw_time: %u", status, auth_level,
21 short_addr, endpoint, nw_time);
22 }
23}
24
26 global_time = this;
27 this->parent_->add_callback(this->endpoint_, [this](zb_bufid_t bufid) { this->zcl_device_cb_(bufid); });
29 this->parent_->add_join_callback([this]() { zb_zcl_time_server_synchronize(this->endpoint_, sync_time); });
30}
31
33 ESP_LOGCONFIG(TAG,
34 "Zigbee Time\n"
35 " Endpoint: %d",
36 this->endpoint_);
37 RealTimeClock::dump_config();
38}
39
41 time_t time = timestamp_now();
42 this->cluster_attributes_->time = time - EPOCH_2000;
43}
44
45void ZigbeeTime::set_epoch_time(uint32_t epoch) {
46 this->defer([this, epoch]() {
47 this->synchronize_epoch_(epoch);
48 this->has_time_ = true;
49 });
50}
51
52void ZigbeeTime::zcl_device_cb_(zb_bufid_t bufid) {
53 zb_zcl_device_callback_param_t *p_device_cb_param = ZB_BUF_GET_PARAM(bufid, zb_zcl_device_callback_param_t);
54 zb_zcl_device_callback_id_t device_cb_id = p_device_cb_param->device_cb_id;
55 zb_uint16_t cluster_id = p_device_cb_param->cb_param.set_attr_value_param.cluster_id;
56 zb_uint16_t attr_id = p_device_cb_param->cb_param.set_attr_value_param.attr_id;
57
58 switch (device_cb_id) {
59 /* ZCL set attribute value */
60 case ZB_ZCL_SET_ATTR_VALUE_CB_ID:
61 if (cluster_id == ZB_ZCL_CLUSTER_ID_TIME) {
62 if (attr_id == ZB_ZCL_ATTR_TIME_TIME_ID) {
63 zb_uint32_t value = p_device_cb_param->cb_param.set_attr_value_param.values.data32;
64 ESP_LOGI(TAG, "Synchronize time to %u", value);
65 this->defer([this, value]() { synchronize_epoch_(value + EPOCH_2000); });
66 } else if (attr_id == ZB_ZCL_ATTR_TIME_TIME_STATUS_ID) {
67 zb_uint8_t value = p_device_cb_param->cb_param.set_attr_value_param.values.data8;
68 ESP_LOGI(TAG, "Time status %hd", value);
69 this->defer([this, value]() { this->has_time_ = ZB_ZCL_TIME_TIME_STATUS_SYNCHRONIZED_BIT_IS_SET(value); });
70 }
71 } else {
72 /* other clusters attribute handled here */
73 ESP_LOGI(TAG, "Unhandled cluster attribute id: %d", cluster_id);
74 p_device_cb_param->status = RET_NOT_IMPLEMENTED;
75 }
76 break;
77 default:
78 p_device_cb_param->status = RET_NOT_IMPLEMENTED;
79 break;
80 }
81
82 ESP_LOGD(TAG, "Zcl_device_cb_ status: %hd", p_device_cb_param->status);
83}
84
85} // namespace esphome::zigbee
86
87#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:479
time_t timestamp_now()
Get the current time as the UTC epoch since January 1st 1970.
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
void add_join_callback(std::function< void()> &&cb)
void add_callback(zb_uint8_t endpoint, std::function< void(zb_bufid_t bufid)> &&cb)
void set_epoch_time(uint32_t epoch)
static void sync_time(zb_ret_t status, zb_uint32_t auth_level, zb_uint16_t short_addr, zb_uint8_t endpoint, zb_uint32_t nw_time)
void zcl_device_cb_(zb_bufid_t bufid)
zb_zcl_time_attrs_t * cluster_attributes_
const char *const TAG
Definition spi.cpp:7
constexpr time_t EPOCH_2000