@@ -282,7 +282,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
282282          } else  {
283283            //  Save the channel mask to NVRAM in case of reboot which may be on a different channel after a change in the network
284284            Zigbee.setNVRAMChannelMask (1  << esp_zb_get_current_channel ());
285-             Zigbee._connected  = true ; //  Coordinator is always connected
285+             Zigbee._connected  = true ;   //  Coordinator is always connected
286286          }
287287          Zigbee.searchBindings ();
288288        }
@@ -374,7 +374,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
374374                log_d (" Device not bound to endpoint %d and it is free to bound!" getEndpoint ());
375375                (*it)->findEndpoint (&cmd_req);
376376                log_d (" Endpoint %d is searching for device" getEndpoint ());
377-                 break ; //  Only one endpoint per device
377+                 break ;   //  Only one endpoint per device
378378              }
379379            }
380380          }
@@ -408,7 +408,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
408408              if  (!found) {
409409                (*it)->findEndpoint (&cmd_req);
410410                log_d (" Endpoint %d is searching for device" getEndpoint ());
411-                 break ; //  Only one endpoint per device
411+                 break ;   //  Only one endpoint per device
412412              }
413413            }
414414          }
@@ -437,25 +437,27 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
437437}
438438
439439//  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) {
442441  if  (Zigbee.getDebugMode ()) {
443442    log_d (" APSDE INDICATION - Received APSDE-DATA indication, status: %d" 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" 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" 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" 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" dst_short_addr ,
449+       ind.src_short_addr , ind.profile_id , ind.security_status , ind.lqi , ind.rx_time 
450+     );
446451  }
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 ();
453456    }
454-     else 
455-     {
456-       log_e (" APSDE INDICATION - Invalid status of APSDE-DATA indication, error code: %d" 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" status );
459+   }
460+   return  false ;  // False to let the stack process the message as usual
459461}
460462
461463void  ZigbeeCore::factoryReset (bool  restart) {
@@ -527,7 +529,7 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
527529  esp_zb_zdo_mgmt_bind_param_t  *req = (esp_zb_zdo_mgmt_bind_param_t  *)user_ctx;
528530  esp_zb_zdp_status_t  zdo_status = (esp_zb_zdp_status_t )table_info->status ;
529531  log_d (" Binding table callback for address 0x%04x with status %d" dst_addr , zdo_status);
530-    
532+ 
531533  if  (zdo_status == ESP_ZB_ZDP_STATUS_SUCCESS) {
532534    //  Print binding table log simple
533535    log_d (" Binding table info: total %d, index %d, count %d" total , table_info->index , table_info->count );
@@ -549,10 +551,14 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
549551      uint16_t  short_addr;
550552      esp_zb_ieee_addr_t  ieee_addr;
551553      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+         }
556562        if  (is_ieee) {
557563          return  memcmp (ieee_addr, other.ieee_addr , sizeof (esp_zb_ieee_addr_t )) < 0 ;
558564        }
@@ -571,30 +577,31 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
571577    //  Add current records to our collection
572578    esp_zb_zdo_binding_table_record_t  *record = table_info->record ;
573579    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" src_endp , record->dst_endp , record->cluster_id ,
582+         record->dst_addr_mode 
583+       );
576584      all_records.push_back (*record);
577585      record = record->next ;
578586    }
579587
580588    //  If this is not the last chunk, request the next one
581589    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)" index , table_info->count , table_info->total );
584591      req->start_index  = table_info->index  + table_info->count ;
585592      esp_zb_zdo_binding_table_req (req, bindingTableCb, req);
586593    } else  {
587594      //  This is the last chunk, process all records
588595      log_d (" Processing final chunk of binding table, total records: %d" size ());
589-       for  (const  auto &  record : all_records) {
596+       for  (const  auto  & record : all_records) {
590597
591598        DeviceIdentifier dev_id;
592599        dev_id.endpoint  = record.src_endp ;
593600        dev_id.is_ieee  = (record.dst_addr_mode  == ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT);
594-          
601+ 
595602        if  (dev_id.is_ieee ) {
596603          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
598605        } else  {
599606          dev_id.short_addr  = record.dst_address .addr_short ;
600607          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
609616        log_d (" Processing endpoint %d" getEndpoint ());
610617        std::list<zb_device_params_t  *> bound_devices = (*it)->getBoundDevices ();
611618        std::list<zb_device_params_t  *> devices_to_remove;
612-          
619+ 
613620        //  First, identify devices that need to be removed
614621        for  (std::list<zb_device_params_t  *>::iterator dev_it = bound_devices.begin (); dev_it != bound_devices.end (); ++dev_it) {
615622          DeviceIdentifier dev_id;
616623          dev_id.endpoint  = (*it)->getEndpoint ();
617-            
624+ 
618625          //  Create both short and IEEE address identifiers for the device
619626          bool  found = false ;
620-            
627+ 
621628          //  Check if device exists with short address
622629          if  ((*dev_it)->short_addr  != 0xFFFF ) {
623630            dev_id.is_ieee  = false ;
@@ -627,7 +634,7 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
627634              found = true ;
628635            }
629636          }
630-            
637+ 
631638          //  Check if device exists with IEEE address
632639          if  (!found) {
633640            dev_id.is_ieee  = true ;
@@ -637,20 +644,20 @@ void ZigbeeCore::bindingTableCb(const esp_zb_zdo_binding_table_info_t *table_inf
637644              found = true ;
638645            }
639646          }
640-            
647+ 
641648          if  (!found) {
642649            devices_to_remove.push_back (*dev_it);
643650          }
644651        }
645-          
652+ 
646653        //  Remove devices that are no longer in the binding table
647654        for  (std::list<zb_device_params_t  *>::iterator dev_it = devices_to_remove.begin (); dev_it != devices_to_remove.end (); ++dev_it) {
648655          (*it)->removeBoundDevice (*dev_it);
649656          free (*dev_it);
650657        }
651658
652659        //  Now add new devices from the binding table
653-         for  (const  auto &  record : all_records) {
660+         for  (const  auto  & record : all_records) {
654661          if  (record.src_endp  == (*it)->getEndpoint ()) {
655662            log_d (" Processing binding record for EP %d" src_endp );
656663            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
659666              continue ;
660667            }
661668            device->endpoint  = record.dst_endp ;
662-              
669+ 
663670            bool  is_ieee = (record.dst_addr_mode  == ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT);
664671            if  (is_ieee) {
665672              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
684691                }
685692              }
686693            }
687-              
694+ 
688695            if  (!device_exists) {
689696              (*it)->addBoundDevice (device);
690697              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" src_endp , device->endpoint , is_ieee ? " ieee addr" " short addr" 
699+                 is_ieee ? formatIEEEAddress (device->ieee_addr ) : formatShortAddress (device->short_addr )
697700              );
698701            } else  {
699702              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
701704            }
702705          }
703706        }
0 commit comments