Skip to content

Commit a6c9d5c

Browse files
committed
fix(zigbee): Add missing locks + allow printBoundDevices to Serial
1 parent d45a267 commit a6c9d5c

File tree

6 files changed

+85
-24
lines changed

6 files changed

+85
-24
lines changed

Diff for: libraries/Zigbee/examples/Zigbee_On_Off_Switch/Zigbee_On_Off_Switch.ino

+9-10
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,17 @@ void setup() {
137137
Serial.printf(".");
138138
delay(500);
139139
}
140-
141-
// Optional: read manufacturer and model name from the bound light
140+
141+
// Optional: List all bound devices and read manufacturer and model name
142142
std::list<zb_device_params_t *> boundLights = zbSwitch.getBoundDevices();
143-
//List all bound lights
144143
for (const auto &device : boundLights) {
145-
Serial.printf("Device on endpoint %d, short address: 0x%x\n", device->endpoint, device->short_addr);
144+
Serial.printf("Device on endpoint %d, short address: 0x%x\r\n", device->endpoint, device->short_addr);
146145
Serial.printf(
147-
"IEEE Address: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", device->ieee_addr[0], device->ieee_addr[1], device->ieee_addr[2], device->ieee_addr[3],
148-
device->ieee_addr[4], device->ieee_addr[5], device->ieee_addr[6], device->ieee_addr[7]
146+
"IEEE Address: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\r\n", device->ieee_addr[7], device->ieee_addr[6], device->ieee_addr[5],
147+
device->ieee_addr[4], device->ieee_addr[3], device->ieee_addr[2], device->ieee_addr[1], device->ieee_addr[0]
149148
);
150-
Serial.printf("Light manufacturer: %s", zbSwitch.readManufacturer(device->endpoint, device->short_addr, device->ieee_addr));
151-
Serial.printf("Light model: %s", zbSwitch.readModel(device->endpoint, device->short_addr, device->ieee_addr));
149+
Serial.printf("Light manufacturer: %s\r\n", zbSwitch.readManufacturer(device->endpoint, device->short_addr, device->ieee_addr));
150+
Serial.printf("Light model: %s\r\n", zbSwitch.readModel(device->endpoint, device->short_addr, device->ieee_addr));
152151
}
153152

154153
Serial.println();
@@ -191,6 +190,6 @@ void loop() {
191190
static uint32_t lastPrint = 0;
192191
if (millis() - lastPrint > 10000) {
193192
lastPrint = millis();
194-
zbSwitch.printBoundDevices();
193+
zbSwitch.printBoundDevices(&Serial);
195194
}
196-
}
195+
}

Diff for: libraries/Zigbee/src/ZigbeeCore.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,6 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
411411
esp_zb_zdo_binding_table_record_t *record = table_info->record;
412412
for (int i = 0; i < table_info->count; i++) {
413413
log_d("Binding table record: src_endp %d, dst_endp %d, cluster_id 0x%04x, dst_addr_mode %d", record->src_endp, record->dst_endp, record->cluster_id, record->dst_addr_mode);
414-
log_d("Short address: 0x%04x", record->dst_address.addr_short);
415-
log_d("ieee_address: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", record->dst_address.addr_long[7], record->dst_address.addr_long[6], record->dst_address.addr_long[5], record->dst_address.addr_long[4], record->dst_address.addr_long[3], record->dst_address.addr_long[2], record->dst_address.addr_long[1], record->dst_address.addr_long[0]);
416414

417415
zb_device_params_t *device = (zb_device_params_t *)calloc(1, sizeof(zb_device_params_t));
418416
device->endpoint = record->dst_endp;
@@ -421,12 +419,13 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
421419
} else { //ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT
422420
memcpy(device->ieee_addr, record->dst_address.addr_long, sizeof(esp_zb_ieee_addr_t));
423421
}
424-
log_d("Bound device: endpoint %d, short address 0x%04x to endpoint %d", record->dst_endp, record->dst_address.addr_short, record->src_endp);
425422

426423
// Add to list of bound devices of proper endpoint
427424
for (std::list<ZigbeeEP *>::iterator it = Zigbee.ep_objects.begin(); it != Zigbee.ep_objects.end(); ++it) {
428425
if ((*it)->getEndpoint() == record->src_endp) {
429426
(*it)->addBoundDevice(device);
427+
log_d("Device bound to EP %d -> device endpoint: %d, short addr: 0x%04x, ieee addr: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", record->src_endp, device->endpoint, device->short_addr,
428+
device->ieee_addr[7], device->ieee_addr[6], device->ieee_addr[5], device->ieee_addr[4], device->ieee_addr[3], device->ieee_addr[2], device->ieee_addr[1], device->ieee_addr[0]);
430429
}
431430
}
432431
record = record->next;

Diff for: libraries/Zigbee/src/ZigbeeEP.cpp

+19-7
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ char *ZigbeeEP::readManufacturer(uint8_t endpoint, uint16_t short_addr, esp_zb_i
139139
// clear read manufacturer
140140
_read_manufacturer = nullptr;
141141

142+
esp_zb_lock_acquire(portMAX_DELAY);
142143
esp_zb_zcl_read_attr_cmd_req(&read_req);
144+
esp_zb_lock_release();
143145

144146
//Wait for response or timeout
145147
if (xSemaphoreTake(lock, ZB_CMD_TIMEOUT) != pdTRUE) {
@@ -173,22 +175,32 @@ char *ZigbeeEP::readModel(uint8_t endpoint, uint16_t short_addr, esp_zb_ieee_add
173175
// clear read model
174176
_read_model = nullptr;
175177

178+
esp_zb_lock_acquire(portMAX_DELAY);
176179
esp_zb_zcl_read_attr_cmd_req(&read_req);
180+
esp_zb_lock_release();
177181

178182
//Wait for response or timeout
179-
//Semaphore take
180183
if (xSemaphoreTake(lock, ZB_CMD_TIMEOUT) != pdTRUE) {
181184
log_e("Error while reading model");
182185
}
183186
return _read_model;
184187
}
185188

186-
void ZigbeeEP::printBoundDevices() {
187-
log_i("Bound devices:");
188-
for ([[maybe_unused]]
189-
const auto &device : _bound_devices) {
190-
log_i("Device on endpoint %d, short address: 0x%x", device->endpoint, device->short_addr);
191-
print_ieee_addr(device->ieee_addr);
189+
void ZigbeeEP::printBoundDevices(Print *print) {
190+
if (print == nullptr) {
191+
log_i("Bound devices:");
192+
for ([[maybe_unused]]
193+
const auto &device : _bound_devices) {
194+
log_i("Device on endpoint %d, short address: 0x%x, ieee address: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", device->endpoint, device->short_addr,
195+
device->ieee_addr[7], device->ieee_addr[6], device->ieee_addr[5], device->ieee_addr[4], device->ieee_addr[3], device->ieee_addr[2], device->ieee_addr[1], device->ieee_addr[0]);
196+
}
197+
} else {
198+
print->println("Bound devices:");
199+
for ([[maybe_unused]]
200+
const auto &device : _bound_devices) {
201+
print->printf("Device on endpoint %d, short address: 0x%x, ieee address: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n", device->endpoint, device->short_addr,
202+
device->ieee_addr[7], device->ieee_addr[6], device->ieee_addr[5], device->ieee_addr[4], device->ieee_addr[3], device->ieee_addr[2], device->ieee_addr[1], device->ieee_addr[0]);
203+
}
192204
}
193205
}
194206

Diff for: libraries/Zigbee/src/ZigbeeEP.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
/* Useful defines */
1111
#define ZB_ARRAY_LENTH(arr) (sizeof(arr) / sizeof(arr[0]))
12-
#define print_ieee_addr(addr) \
13-
log_i("IEEE Address: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7])
1412
#define XYZ_TO_RGB(X, Y, Z, r, g, b) \
1513
{ \
1614
r = (float)(3.240479 * (X) - 1.537150 * (Y) - 0.498535 * (Z)); \
@@ -68,7 +66,7 @@ class ZigbeeEP {
6866
return _endpoint;
6967
}
7068

71-
void printBoundDevices();
69+
void printBoundDevices(Print *print = nullptr);
7270
std::list<zb_device_params_t *> getBoundDevices() const {
7371
return _bound_devices;
7472
}
@@ -128,7 +126,6 @@ class ZigbeeEP {
128126
_bound_devices.push_back(device);
129127
_is_bound = true;
130128
}
131-
132129
friend class ZigbeeCore;
133130
};
134131

Diff for: libraries/Zigbee/src/ep/ZigbeeColorDimmerSwitch.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ void ZigbeeColorDimmerSwitch::lightToggle() {
9999
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
100100
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_TOGGLE_ID;
101101
log_v("Sending 'light toggle' command");
102+
esp_zb_lock_acquire(portMAX_DELAY);
102103
esp_zb_zcl_on_off_cmd_req(&cmd_req);
104+
esp_zb_lock_release();
103105
} else {
104106
log_e("Light not bound");
105107
}
@@ -113,7 +115,9 @@ void ZigbeeColorDimmerSwitch::lightToggle(uint16_t group_addr) {
113115
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_GROUP_ENDP_NOT_PRESENT;
114116
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_TOGGLE_ID;
115117
log_v("Sending 'light toggle' command to group address 0x%x", group_addr);
118+
esp_zb_lock_acquire(portMAX_DELAY);
116119
esp_zb_zcl_on_off_cmd_req(&cmd_req);
120+
esp_zb_lock_release();
117121
} else {
118122
log_e("Light not bound");
119123
}
@@ -128,7 +132,9 @@ void ZigbeeColorDimmerSwitch::lightToggle(uint8_t endpoint, uint16_t short_addr)
128132
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT;
129133
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_TOGGLE_ID;
130134
log_v("Sending 'light toggle' command to endpoint %d, address 0x%x", endpoint, short_addr);
135+
esp_zb_lock_acquire(portMAX_DELAY);
131136
esp_zb_zcl_on_off_cmd_req(&cmd_req);
137+
esp_zb_lock_release();
132138
} else {
133139
log_e("Light not bound");
134140
}
@@ -144,7 +150,9 @@ void ZigbeeColorDimmerSwitch::lightToggle(uint8_t endpoint, esp_zb_ieee_addr_t i
144150
memcpy(cmd_req.zcl_basic_cmd.dst_addr_u.addr_long, ieee_addr, sizeof(esp_zb_ieee_addr_t));
145151
log_v("Sending 'light toggle' command to endpoint %d, ieee address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
146152
endpoint, ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4], ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
153+
esp_zb_lock_acquire(portMAX_DELAY);
147154
esp_zb_zcl_on_off_cmd_req(&cmd_req);
155+
esp_zb_lock_release();
148156
} else {
149157
log_e("Light not bound");
150158
}
@@ -157,7 +165,9 @@ void ZigbeeColorDimmerSwitch::lightOn() {
157165
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
158166
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_ON_ID;
159167
log_v("Sending 'light on' command");
168+
esp_zb_lock_acquire(portMAX_DELAY);
160169
esp_zb_zcl_on_off_cmd_req(&cmd_req);
170+
esp_zb_lock_release();
161171
} else {
162172
log_e("Light not bound");
163173
}
@@ -171,7 +181,9 @@ void ZigbeeColorDimmerSwitch::lightOn(uint16_t group_addr) {
171181
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_GROUP_ENDP_NOT_PRESENT;
172182
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_ON_ID;
173183
log_v("Sending 'light on' command to group address 0x%x", group_addr);
184+
esp_zb_lock_acquire(portMAX_DELAY);
174185
esp_zb_zcl_on_off_cmd_req(&cmd_req);
186+
esp_zb_lock_release();
175187
} else {
176188
log_e("Light not bound");
177189
}
@@ -186,7 +198,9 @@ void ZigbeeColorDimmerSwitch::lightOn(uint8_t endpoint, uint16_t short_addr) {
186198
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT;
187199
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_ON_ID;
188200
log_v("Sending 'light on' command to endpoint %d, address 0x%x", endpoint, short_addr);
201+
esp_zb_lock_acquire(portMAX_DELAY);
189202
esp_zb_zcl_on_off_cmd_req(&cmd_req);
203+
esp_zb_lock_release();
190204
} else {
191205
log_e("Light not bound");
192206
}
@@ -202,7 +216,9 @@ void ZigbeeColorDimmerSwitch::lightOn(uint8_t endpoint, esp_zb_ieee_addr_t ieee_
202216
memcpy(cmd_req.zcl_basic_cmd.dst_addr_u.addr_long, ieee_addr, sizeof(esp_zb_ieee_addr_t));
203217
log_v("Sending 'light on' command to endpoint %d, ieee address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
204218
endpoint, ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4], ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
219+
esp_zb_lock_acquire(portMAX_DELAY);
205220
esp_zb_zcl_on_off_cmd_req(&cmd_req);
221+
esp_zb_lock_release();
206222
} else {
207223
log_e("Light not bound");
208224
}
@@ -215,7 +231,9 @@ void ZigbeeColorDimmerSwitch::lightOff() {
215231
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_DST_ADDR_ENDP_NOT_PRESENT;
216232
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_OFF_ID;
217233
log_v("Sending 'light off' command");
234+
esp_zb_lock_acquire(portMAX_DELAY);
218235
esp_zb_zcl_on_off_cmd_req(&cmd_req);
236+
esp_zb_lock_release();
219237
} else {
220238
log_e("Light not bound");
221239
}
@@ -229,7 +247,9 @@ void ZigbeeColorDimmerSwitch::lightOff(uint16_t group_addr) {
229247
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_GROUP_ENDP_NOT_PRESENT;
230248
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_OFF_ID;
231249
log_v("Sending 'light off' command to group address 0x%x", group_addr);
250+
esp_zb_lock_acquire(portMAX_DELAY);
232251
esp_zb_zcl_on_off_cmd_req(&cmd_req);
252+
esp_zb_lock_release();
233253
} else {
234254
log_e("Light not bound");
235255
}
@@ -244,7 +264,9 @@ void ZigbeeColorDimmerSwitch::lightOff(uint8_t endpoint, uint16_t short_addr) {
244264
cmd_req.address_mode = ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT;
245265
cmd_req.on_off_cmd_id = ESP_ZB_ZCL_CMD_ON_OFF_OFF_ID;
246266
log_v("Sending 'light off' command to endpoint %d, address 0x%x", endpoint, short_addr);
267+
esp_zb_lock_acquire(portMAX_DELAY);
247268
esp_zb_zcl_on_off_cmd_req(&cmd_req);
269+
esp_zb_lock_release();
248270
} else {
249271
log_e("Light not bound");
250272
}
@@ -260,7 +282,9 @@ void ZigbeeColorDimmerSwitch::lightOff(uint8_t endpoint, esp_zb_ieee_addr_t ieee
260282
memcpy(cmd_req.zcl_basic_cmd.dst_addr_u.addr_long, ieee_addr, sizeof(esp_zb_ieee_addr_t));
261283
log_v("Sending 'light off' command to endpoint %d, ieee address %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
262284
endpoint, ieee_addr[7], ieee_addr[6], ieee_addr[5], ieee_addr[4], ieee_addr[3], ieee_addr[2], ieee_addr[1], ieee_addr[0]);
285+
esp_zb_lock_acquire(portMAX_DELAY);
263286
esp_zb_zcl_on_off_cmd_req(&cmd_req);
287+
esp_zb_lock_release();
264288
} else {
265289
log_e("Light not bound");
266290
}

0 commit comments

Comments
 (0)