@@ -243,6 +243,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
243243 } else {
244244 Zigbee._connected = true ;
245245 }
246+ Zigbee.searchBindings ();
246247 }
247248 } else {
248249 /* commissioning failed */
@@ -309,8 +310,7 @@ void esp_zb_app_signal_handler(esp_zb_app_signal_t *signal_struct) {
309310 Bit 6 – Security capability
310311 Bit 7 – Reserved
311312 */
312-
313- // for each endpoint in the list call the findEndpoint function if not bounded or allowed to bind multiple devices
313+ // for each endpoint in the list call the findEndpoint function if not bounded or allowed to bind multiple devices
314314 for (std::list<ZigbeeEP *>::iterator it = Zigbee.ep_objects .begin (); it != Zigbee.ep_objects .end (); ++it) {
315315 if (!(*it)->bound () || (*it)->epAllowMultipleBinding ()) {
316316 (*it)->findEndpoint (&cmd_req);
@@ -391,6 +391,71 @@ void ZigbeeCore::scanDelete() {
391391 _scan_status = ZB_SCAN_FAILED;
392392}
393393
394+ // Recall bounded devices from the binding table after reboot
395+ void ZigbeeCore::bindingTableCb (const esp_zb_zdo_binding_table_info_t *table_info, void *user_ctx)
396+ {
397+ bool done = true ;
398+ esp_zb_zdo_mgmt_bind_param_t *req = (esp_zb_zdo_mgmt_bind_param_t *)user_ctx;
399+ esp_zb_zdp_status_t zdo_status = (esp_zb_zdp_status_t )table_info->status ;
400+ log_d (" Binding table callback for address 0x%04x with status %d" , req->dst_addr , zdo_status);
401+ if (zdo_status == ESP_ZB_ZDP_STATUS_SUCCESS) {
402+ // Print binding table log simple
403+ log_d (" Binding table info: total %d, index %d, count %d" , table_info->total , table_info->index , table_info->count );
404+
405+ if (table_info->total == 0 ) {
406+ log_d (" No binding table entries found" );
407+ free (req);
408+ return ;
409+ }
410+
411+ esp_zb_zdo_binding_table_record_t *record = table_info->record ;
412+ for (int i = 0 ; i < table_info->count ; i++) {
413+ 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 ]);
416+
417+ zb_device_params_t *device = (zb_device_params_t *)calloc (1 , sizeof (zb_device_params_t ));
418+ device->endpoint = record->dst_endp ;
419+ if (record->dst_addr_mode == ESP_ZB_APS_ADDR_MODE_16_ENDP_PRESENT || record->dst_addr_mode == ESP_ZB_APS_ADDR_MODE_16_GROUP_ENDP_NOT_PRESENT) {
420+ device->short_addr = record->dst_address .addr_short ;
421+ } else { // ESP_ZB_APS_ADDR_MODE_64_ENDP_PRESENT
422+ memcpy (device->ieee_addr , record->dst_address .addr_long , sizeof (esp_zb_ieee_addr_t ));
423+ }
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 );
425+
426+ // Add to list of bound devices of proper endpoint
427+ for (std::list<ZigbeeEP *>::iterator it = Zigbee.ep_objects .begin (); it != Zigbee.ep_objects .end (); ++it) {
428+ if ((*it)->getEndpoint () == record->src_endp ) {
429+ (*it)->addBoundDevice (device);
430+ }
431+ }
432+ record = record->next ;
433+ }
434+
435+ // Continue reading the binding table
436+ if (table_info->index + table_info->count < table_info->total ) {
437+ /* There are unreported binding table entries, request for them. */
438+ req->start_index = table_info->index + table_info->count ;
439+ esp_zb_zdo_binding_table_req (req, bindingTableCb, req);
440+ done = false ;
441+ }
442+ }
443+
444+ if (done) {
445+ // Print bound devices
446+ log_d (" Filling bounded devices finished" );
447+ free (req);
448+ }
449+ }
450+
451+ void ZigbeeCore::searchBindings (){
452+ esp_zb_zdo_mgmt_bind_param_t *mb_req = (esp_zb_zdo_mgmt_bind_param_t *)malloc (sizeof (esp_zb_zdo_mgmt_bind_param_t ));
453+ mb_req->dst_addr = esp_zb_get_short_address ();
454+ mb_req->start_index = 0 ;
455+ log_d (" Requesting binding table for address 0x%04x" , mb_req->dst_addr );
456+ esp_zb_zdo_binding_table_req (mb_req, bindingTableCb, (void *)mb_req);
457+ }
458+
394459// Function to convert enum value to string
395460const char *ZigbeeCore::getDeviceTypeString (esp_zb_ha_standard_devices_t deviceId) {
396461 switch (deviceId) {
0 commit comments