Skip to content

Commit 5cbfcaa

Browse files
committed
Merge pull request romasku#389 from RealByron/optimisation_again
immediate send parameter for hal_zigbee_notify_attribute_changed # Conflicts: # device_db.yaml
2 parents acad033 + 5afba98 commit 5cbfcaa

13 files changed

Lines changed: 80 additions & 41 deletions

File tree

device_db.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3462,7 +3462,7 @@ REMOTE_MOES_SWITCH_TS0044:
34623462
tuya_module: ZT3L
34633463
mcu_family: Telink
34643464
mcu: TLSR8258
3465-
config_str: zgyzgdua;TS0044-MOES;SD2d;IC4i;SC3d;IA0i;SC2d;ID7i;SB4d;ID4i;BTC5;M;
3465+
config_str: zgyzgdua;TS0044-MOES;SD2d;IC4i;SC3d;IA0i;SC2d;ID7i;SB4d;ID4i;BTC5;M;D0;
34663466
alt_config_str: null
34673467
old_manufacturer_names: null
34683468
old_zb_models: null

src/base_components/led.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77
#include <stdio.h>
88

9+
static void led_blink_handler(void *arg);
10+
911
void led_init(led_t *led) {
12+
led->blink_task.handler = led_blink_handler;
13+
led->blink_task.arg = led;
14+
hal_tasks_init(&led->blink_task);
1015
led_off(led);
1116
}
1217

@@ -34,7 +39,9 @@ static void led_blink_handler(void *arg) {
3439
if (led->blink_times_left != LED_BLINK_FOREVER) {
3540
led->blink_times_left--;
3641
}
37-
hal_tasks_schedule(&led->blink_task, led->blink_time_off);
42+
if (led->blink_times_left > 0) {
43+
hal_tasks_schedule(&led->blink_task, led->blink_time_off);
44+
}
3845
} else {
3946
led->on = 1;
4047
hal_gpio_write(led->pin, led->on_high);
@@ -57,9 +64,6 @@ void led_blink(led_t *led, uint16_t on_time_ms, uint16_t off_time_ms,
5764

5865
hal_gpio_write(led->pin, led->on_high);
5966
led->on = 1;
60-
led->blink_times_left = times;
61-
led->blink_task.handler = led_blink_handler;
62-
led->blink_task.arg = led;
63-
hal_tasks_init(&led->blink_task);
67+
led->blink_times_left = times;
6468
hal_tasks_schedule(&led->blink_task, on_time_ms);
6569
}

src/hal/zigbee.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,12 @@ void hal_zigbee_set_image_type(uint16_t image_type);
103103
* @param endpoint Endpoint number
104104
* @param cluster_id Cluster ID
105105
* @param attribute_id Attribute ID that changed
106+
* @param immediate If true, send report immediately instead of using SDK
107+
* reporting timers (avoids unnecessary wake-ups on battery
108+
* devices)
106109
*/
107110
void hal_zigbee_notify_attribute_changed(uint8_t endpoint, uint16_t cluster_id,
108-
uint16_t attribute_id);
111+
uint16_t attribute_id, bool immediate);
109112

110113
/** Function called when attribute is written via Zigbee */
111114
typedef void (*hal_attribute_change_callback_t)(uint8_t endpoint,

src/silabs/hal/zigbee.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void hal_zigbee_init(hal_zigbee_endpoint *endpoints, uint8_t endpoints_cnt) {
147147
}
148148

149149
void hal_zigbee_notify_attribute_changed(uint8_t endpoint, uint16_t cluster_id,
150-
uint16_t attribute_id) {
150+
uint16_t attribute_id, bool immediate) {
151151
hal_zigbee_cluster * cluster = find_hal_cluster(endpoint, cluster_id);
152152
hal_zigbee_attribute *attr =
153153
find_hal_attr(endpoint, cluster_id, attribute_id);

src/stub/hal/zigbee.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ uint32_t hal_zigbee_get_poll_rate_ms(void) {
147147
}
148148

149149
void hal_zigbee_notify_attribute_changed(uint8_t endpoint, uint16_t cluster_id,
150-
uint16_t attribute_id) {
150+
uint16_t attribute_id, bool immediate) {
151151
io_log("ZIGBEE", "Attribute changed: ep=%d, cluster=0x%04x, attr=0x%04x",
152152
endpoint, cluster_id, attribute_id);
153153
// In stub, do NOT call attr_change_callback here.

src/telink/hal/zigbee_network.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "telink_size_t_hack.h"
1010

11+
#include "device_config/config_parser.h"
1112
#include "hal/zigbee.h"
1213
#include "telink_zigbee_hal.h"
1314
#include "version_cfg.h"
@@ -80,7 +81,7 @@ void bdb_init_callback(u8 status, u8 joinedNetwork) {
8081
if (joinedNetwork) {
8182
ota_queryStart(OTA_QUERY_INTERVAL);
8283
#ifdef ZB_ED_ROLE
83-
zb_setPollRate(POLL_RATE);
84+
hal_zigbee_set_poll_rate_ms(POLL_RATE);
8485
#endif
8586
}
8687
} else {
@@ -100,7 +101,7 @@ void bdb_commissioning_callback(u8 status, void *arg) {
100101
// Need set poll rate manually,
101102
// to avoid bugs related to no poll task
102103
// after fast re-connect.
103-
zb_setPollRate(POLL_RATE);
104+
hal_zigbee_set_poll_rate_ms(POLL_RATE);
104105
printf("Set poll rate to %d\r\n", POLL_RATE);
105106
#endif
106107
steeringInProgress = 0;
@@ -186,6 +187,13 @@ hal_zigbee_status_t hal_zigbee_send_announce(void) {
186187

187188
void hal_zigbee_set_poll_rate_ms(uint32_t poll_rate_ms) {
188189
zb_setPollRate(poll_rate_ms);
190+
191+
// Disable the SDK's automatic quick data polls (3 extra MAC polls after
192+
// each send) in long-poll mode to avoid unnecessary wake-ups from
193+
// deep retention.
194+
if (battery.pin != HAL_INVALID_PIN) {
195+
AUTO_QUICK_DATA_POLL_ENABLE = (poll_rate_ms <= 10000);
196+
}
189197
}
190198

191199
uint32_t hal_zigbee_get_poll_rate_ms(void) {

src/telink/hal/zigbee_zcl.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "hal/zigbee.h"
1313
#include "telink_zigbee_hal.h"
14-
#include "zigbee/battery_cluster.h"
1514
#include "zigbee/consts.h"
1615

1716
// Storage for Telink endpoint configuration
@@ -236,8 +235,32 @@ void telink_zigbee_hal_zcl_init(hal_zigbee_endpoint *endpoints,
236235
}
237236

238237
void hal_zigbee_notify_attribute_changed(uint8_t endpoint, uint16_t cluster_id,
239-
uint16_t attribute_id) {
240-
report_handler(); // Trigger reporting if needed
238+
uint16_t attribute_id, bool immediate) {
239+
if (immediate) {
240+
// Send report directly to avoid SDK reporting timers
241+
// (reportingTimerCb ~100ms) which cause extra wake-ups
242+
// from deep retention.
243+
hal_zigbee_send_report_attr(endpoint, cluster_id, attribute_id,
244+
0, NULL, 0);
245+
// Sync prevData in the reporting table so report_handler() in
246+
// the main loop does not see a stale diff and schedule another
247+
// reporting timer for the same change.
248+
reportCfgInfo_t *e = zcl_reportCfgInfoEntryFind(endpoint,
249+
cluster_id,
250+
attribute_id);
251+
if (e) {
252+
zclAttrInfo_t *a = zcl_findAttribute(endpoint, cluster_id,
253+
attribute_id);
254+
if (a) {
255+
u8 len = zcl_getAttrSize(a->type, a->data);
256+
if (len > REPORTABLE_CHANGE_MAX_ANALOG_SIZE)
257+
len = REPORTABLE_CHANGE_MAX_ANALOG_SIZE;
258+
memcpy(e->prevData, a->data, len);
259+
}
260+
}
261+
} else {
262+
report_handler();
263+
}
241264
}
242265

243266
hal_zigbee_status_t hal_zigbee_send_cmd_to_bindings(const hal_zigbee_cmd *cmd) {

src/zigbee/battery_cluster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ void battery_cluster_update(zigbee_battery_cluster *cluster) {
5050
cluster->voltage_100mv = status.voltage_mv / 100;
5151

5252
hal_zigbee_notify_attribute_changed(cluster->endpoint, ZCL_CLUSTER_POWER_CFG,
53-
ZCL_ATTR_POWER_CFG_BATTERY_VOLTAGE);
53+
ZCL_ATTR_POWER_CFG_BATTERY_VOLTAGE, false);
5454
hal_zigbee_notify_attribute_changed(cluster->endpoint, ZCL_CLUSTER_POWER_CFG,
55-
ZCL_ATTR_POWER_CFG_BATTERY_PERCENTAGE);
55+
ZCL_ATTR_POWER_CFG_BATTERY_PERCENTAGE, false);
5656
hal_tasks_schedule(&cluster->refresh_values_task, BATTERY_REFRESH_INTERVAL_MS);
5757
}

src/zigbee/cover_cluster.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void cover_apply_movement(zigbee_cover_cluster *cluster, uint8_t moving) {
6161

6262
hal_zigbee_notify_attribute_changed(cluster->endpoint,
6363
ZCL_CLUSTER_WINDOW_COVERING,
64-
ZCL_ATTR_WINDOW_COVERING_MOVING);
64+
ZCL_ATTR_WINDOW_COVERING_MOVING, false);
6565
}
6666

6767
void cover_schedule_movement(zigbee_cover_cluster *cluster, uint8_t moving, uint32_t delay) {

src/zigbee/cover_switch_cluster.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ void cover_switch_cluster_update_present_value(zigbee_cover_switch_cluster *clus
176176
cluster->present_value = present_value;
177177
hal_zigbee_notify_attribute_changed(cluster->endpoint,
178178
ZCL_CLUSTER_MULTISTATE_INPUT_BASIC,
179-
ZCL_ATTR_MULTISTATE_INPUT_PRESENT_VALUE);
179+
ZCL_ATTR_MULTISTATE_INPUT_PRESENT_VALUE, true);
180180

181181
if (local_cmd != 0xFF) {
182182
cover_switch_trigger_local_cmd(cluster, local_cmd);
@@ -304,7 +304,7 @@ void cover_switch_cluster_on_write_attr(zigbee_cover_switch_cluster *cluster,
304304
}
305305
hal_zigbee_notify_attribute_changed(cluster->endpoint,
306306
ZCL_CLUSTER_MULTISTATE_INPUT_BASIC,
307-
ZCL_ATTR_MULTISTATE_INPUT_PRESENT_VALUE);
307+
ZCL_ATTR_MULTISTATE_INPUT_PRESENT_VALUE, false);
308308
cover_switch_cluster_store_attrs_to_nv(cluster);
309309
break;
310310
case ZCL_ATTR_COVER_SWITCH_CONFIG_COVER_INDEX:

0 commit comments

Comments
 (0)