@@ -282,7 +282,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
282
282
} else {
283
283
// Save the channel mask to NVRAM in case of reboot which may be on a different channel after a change in the network
284
284
Zigbee.setNVRAMChannelMask (1 << esp_zb_get_current_channel ());
285
- Zigbee._connected = true ; // Coordinator is always connected
285
+ Zigbee._connected = true ; // Coordinator is always connected
286
286
}
287
287
Zigbee.searchBindings ();
288
288
}
@@ -374,7 +374,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
374
374
log_d (" Device not bound to endpoint %d and it is free to bound!" , (*it)->getEndpoint ());
375
375
(*it)->findEndpoint (&cmd_req);
376
376
log_d (" Endpoint %d is searching for device" , (*it)->getEndpoint ());
377
- break ; // Only one endpoint per device
377
+ break ; // Only one endpoint per device
378
378
}
379
379
}
380
380
}
@@ -408,7 +408,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
408
408
if (!found) {
409
409
(*it)->findEndpoint (&cmd_req);
410
410
log_d (" Endpoint %d is searching for device" , (*it)->getEndpoint ());
411
- break ; // Only one endpoint per device
411
+ break ; // Only one endpoint per device
412
412
}
413
413
}
414
414
}
@@ -437,25 +437,27 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
437
437
}
438
438
439
439
// APS DATA INDICATION HANDLER to catch bind/unbind requests
440
- bool zb_apsde_data_indication_handler (esp_zb_apsde_data_ind_t ind)
441
- {
440
+ bool zb_apsde_data_indication_handler (esp_zb_apsde_data_ind_t ind) {
442
441
if (Zigbee.getDebugMode ()) {
443
442
log_d (" APSDE INDICATION - Received APSDE-DATA indication, status: %d" , ind.status );
444
- log_d (" APSDE INDICATION - dst_endpoint: %d, src_endpoint: %d, dst_addr_mode: %d, src_addr_mode: %d, cluster_id: 0x%04x, asdu_length: %d" , ind.dst_endpoint , ind.src_endpoint , ind.dst_addr_mode , ind.src_addr_mode , ind.cluster_id , ind.asdu_length );
445
- log_d (" APSDE INDICATION - dst_short_addr: 0x%04x, src_short_addr: 0x%04x, profile_id: 0x%04x, security_status: %d, lqi: %d, rx_time: %d" , ind.dst_short_addr , ind.src_short_addr , ind.profile_id , ind.security_status , ind.lqi , ind.rx_time );
443
+ log_d (
444
+ " APSDE INDICATION - dst_endpoint: %d, src_endpoint: %d, dst_addr_mode: %d, src_addr_mode: %d, cluster_id: 0x%04x, asdu_length: %d" , ind.dst_endpoint ,
445
+ ind.src_endpoint , ind.dst_addr_mode , ind.src_addr_mode , ind.cluster_id , ind.asdu_length
446
+ );
447
+ log_d (
448
+ " APSDE INDICATION - dst_short_addr: 0x%04x, src_short_addr: 0x%04x, profile_id: 0x%04x, security_status: %d, lqi: %d, rx_time: %d" , ind.dst_short_addr ,
449
+ ind.src_short_addr , ind.profile_id , ind.security_status , ind.lqi , ind.rx_time
450
+ );
446
451
}
447
- if (ind.status == 0x00 )
448
- {
449
- // Catch bind/unbind requests to update the bound devices list
450
- if (ind.cluster_id == 0x21 || ind.cluster_id == 0x22 ){
451
- Zigbee.searchBindings ();
452
- }
452
+ if (ind.status == 0x00 ) {
453
+ // Catch bind/unbind requests to update the bound devices list
454
+ if (ind.cluster_id == 0x21 || ind.cluster_id == 0x22 ) {
455
+ Zigbee.searchBindings ();
453
456
}
454
- else
455
- {
456
- log_e (" APSDE INDICATION - Invalid status of APSDE-DATA indication, error code: %d" , ind.status );
457
- }
458
- return false ; // False to let the stack process the message as usual
457
+ } else {
458
+ log_e (" APSDE INDICATION - Invalid status of APSDE-DATA indication, error code: %d" , ind.status );
459
+ }
460
+ return false ; // False to let the stack process the message as usual
459
461
}
460
462
461
463
void ZigbeeCore::factoryReset (bool restart) {
@@ -527,7 +529,7 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
527
529
esp_zb_zdo_mgmt_bind_param_t *req = (esp_zb_zdo_mgmt_bind_param_t *)user_ctx;
528
530
esp_zb_zdp_status_t zdo_status = (esp_zb_zdp_status_t )table_info->status ;
529
531
log_d (" Binding table callback for address 0x%04x with status %d" , req->dst_addr , zdo_status);
530
-
532
+
531
533
if (zdo_status == ESP_ZB_ZDP_STATUS_SUCCESS) {
532
534
// Print binding table log simple
533
535
log_d (" Binding table info: total %d, index %d, count %d" , table_info->total , table_info->index , table_info->count );
@@ -549,10 +551,14 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
549
551
uint16_t short_addr;
550
552
esp_zb_ieee_addr_t ieee_addr;
551
553
bool is_ieee;
552
-
553
- bool operator <(const DeviceIdentifier& other) const {
554
- if (endpoint != other.endpoint ) return endpoint < other.endpoint ;
555
- if (is_ieee != other.is_ieee ) return is_ieee < other.is_ieee ;
554
+
555
+ bool operator <(const DeviceIdentifier &other) const {
556
+ if (endpoint != other.endpoint ) {
557
+ return endpoint < other.endpoint ;
558
+ }
559
+ if (is_ieee != other.is_ieee ) {
560
+ return is_ieee < other.is_ieee ;
561
+ }
556
562
if (is_ieee) {
557
563
return memcmp (ieee_addr, other.ieee_addr , sizeof (esp_zb_ieee_addr_t )) < 0 ;
558
564
}
@@ -571,30 +577,31 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
571
577
// Add current records to our collection
572
578
esp_zb_zdo_binding_table_record_t *record = table_info->record ;
573
579
for (int i = 0 ; i < table_info->count ; i++) {
574
- log_d (" Processing record %d: src_endp %d, dst_endp %d, cluster_id 0x%04x, dst_addr_mode %d" ,
575
- i, record->src_endp , record->dst_endp , record->cluster_id , record->dst_addr_mode );
580
+ log_d (
581
+ " Processing record %d: src_endp %d, dst_endp %d, cluster_id 0x%04x, dst_addr_mode %d" , i, record->src_endp , record->dst_endp , record->cluster_id ,
582
+ record->dst_addr_mode
583
+ );
576
584
all_records.push_back (*record);
577
585
record = record->next ;
578
586
}
579
587
580
588
// If this is not the last chunk, request the next one
581
589
if (table_info->index + table_info->count < table_info->total ) {
582
- log_d (" Requesting next chunk of binding table (current index: %d, count: %d, total: %d)" ,
583
- table_info->index , table_info->count , table_info->total );
590
+ log_d (" Requesting next chunk of binding table (current index: %d, count: %d, total: %d)" , table_info->index , table_info->count , table_info->total );
584
591
req->start_index = table_info->index + table_info->count ;
585
592
esp_zb_zdo_binding_table_req (req, bindingTableCb, req);
586
593
} else {
587
594
// This is the last chunk, process all records
588
595
log_d (" Processing final chunk of binding table, total records: %d" , all_records.size ());
589
- for (const auto & record : all_records) {
596
+ for (const auto & record : all_records) {
590
597
591
598
DeviceIdentifier dev_id;
592
599
dev_id.endpoint = record.src_endp ;
593
600
dev_id.is_ieee = (record.dst_addr_mode == ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT);
594
-
601
+
595
602
if (dev_id.is_ieee ) {
596
603
memcpy (dev_id.ieee_addr , record.dst_address .addr_long , sizeof (esp_zb_ieee_addr_t ));
597
- dev_id.short_addr = 0xFFFF ; // Invalid short address
604
+ dev_id.short_addr = 0xFFFF ; // Invalid short address
598
605
} else {
599
606
dev_id.short_addr = record.dst_address .addr_short ;
600
607
memset (dev_id.ieee_addr , 0 , sizeof (esp_zb_ieee_addr_t ));
@@ -609,15 +616,15 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
609
616
log_d (" Processing endpoint %d" , (*it)->getEndpoint ());
610
617
std::list<zb_device_params_t *> bound_devices = (*it)->getBoundDevices ();
611
618
std::list<zb_device_params_t *> devices_to_remove;
612
-
619
+
613
620
// First, identify devices that need to be removed
614
621
for (std::list<zb_device_params_t *>::iterator dev_it = bound_devices.begin (); dev_it != bound_devices.end (); ++dev_it) {
615
622
DeviceIdentifier dev_id;
616
623
dev_id.endpoint = (*it)->getEndpoint ();
617
-
624
+
618
625
// Create both short and IEEE address identifiers for the device
619
626
bool found = false ;
620
-
627
+
621
628
// Check if device exists with short address
622
629
if ((*dev_it)->short_addr != 0xFFFF ) {
623
630
dev_id.is_ieee = false ;
@@ -627,7 +634,7 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
627
634
found = true ;
628
635
}
629
636
}
630
-
637
+
631
638
// Check if device exists with IEEE address
632
639
if (!found) {
633
640
dev_id.is_ieee = true ;
@@ -637,20 +644,20 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
637
644
found = true ;
638
645
}
639
646
}
640
-
647
+
641
648
if (!found) {
642
649
devices_to_remove.push_back (*dev_it);
643
650
}
644
651
}
645
-
652
+
646
653
// Remove devices that are no longer in the binding table
647
654
for (std::list<zb_device_params_t *>::iterator dev_it = devices_to_remove.begin (); dev_it != devices_to_remove.end (); ++dev_it) {
648
655
(*it)->removeBoundDevice (*dev_it);
649
656
free (*dev_it);
650
657
}
651
658
652
659
// Now add new devices from the binding table
653
- for (const auto & record : all_records) {
660
+ for (const auto & record : all_records) {
654
661
if (record.src_endp == (*it)->getEndpoint ()) {
655
662
log_d (" Processing binding record for EP %d" , record.src_endp );
656
663
zb_device_params_t *device = (zb_device_params_t *)calloc (1 , sizeof (zb_device_params_t ));
@@ -659,7 +666,7 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
659
666
continue ;
660
667
}
661
668
device->endpoint = record.dst_endp ;
662
-
669
+
663
670
bool is_ieee = (record.dst_addr_mode == ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT);
664
671
if (is_ieee) {
665
672
memcpy (device->ieee_addr , record.dst_address .addr_long , sizeof (esp_zb_ieee_addr_t ));
@@ -684,20 +691,16 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
684
691
}
685
692
}
686
693
}
687
-
694
+
688
695
if (!device_exists) {
689
696
(*it)->addBoundDevice (device);
690
697
log_d (
691
- " Device bound to EP %d -> device endpoint: %d, %s: %s" ,
692
- record.src_endp , device->endpoint ,
693
- is_ieee ? " ieee addr" : " short addr" ,
694
- is_ieee ?
695
- formatIEEEAddress (device->ieee_addr ) :
696
- formatShortAddress (device->short_addr )
698
+ " Device bound to EP %d -> device endpoint: %d, %s: %s" , record.src_endp , device->endpoint , is_ieee ? " ieee addr" : " short addr" ,
699
+ is_ieee ? formatIEEEAddress (device->ieee_addr ) : formatShortAddress (device->short_addr )
697
700
);
698
701
} else {
699
702
log_d (" Device already exists, freeing allocated memory" );
700
- free (device); // Free the device if it already exists
703
+ free (device); // Free the device if it already exists
701
704
}
702
705
}
703
706
}
0 commit comments