@@ -111,7 +111,7 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
111111 if (!fhss_structure ) {
112112 return ;
113113 }
114- // TODO: check parent broadcast timing constantly and calculate drift error
114+
115115 if (fhss_structure -> ws -> is_on_bc_channel == false) {
116116 fhss_start_timer (fhss_structure , fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval * 1000 , fhss_broadcast_handler );
117117 fhss_structure -> ws -> is_on_bc_channel = true;
@@ -526,13 +526,20 @@ int fhss_ws_set_parent(fhss_structure_t *fhss_structure, const uint8_t eui64[8],
526526 if (fhss_structure -> ws -> fhss_configuration .ws_channel_function == WS_FIXED_CHANNEL ) {
527527 return 0 ;
528528 }
529- // Start broadcast schedule when BC intervals are known
530- if (bc_timing_info -> broadcast_interval && bc_timing_info -> broadcast_dwell_interval ) {
531- fhss_start_timer (fhss_structure , (bc_timing_info -> broadcast_interval - bc_timing_info -> broadcast_interval_offset )* 1000 , fhss_broadcast_handler );
532- fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval = bc_timing_info -> broadcast_dwell_interval ;
533- fhss_structure -> ws -> fhss_configuration .fhss_broadcast_interval = bc_timing_info -> broadcast_interval ;
534- fhss_structure -> ws -> bc_slot = bc_timing_info -> broadcast_slot ;
529+ if (!bc_timing_info -> broadcast_interval || !bc_timing_info -> broadcast_dwell_interval ) {
530+ return -1 ;
535531 }
532+
533+ uint32_t time_from_reception_ms = (fhss_structure -> fhss_api -> read_timestamp (fhss_structure -> fhss_api ) - bc_timing_info -> bt_rx_timestamp )/1000 ;
534+ uint32_t true_bc_interval_offset = (bc_timing_info -> broadcast_interval_offset + time_from_reception_ms ) % bc_timing_info -> broadcast_interval ;
535+ uint32_t timeout = ((bc_timing_info -> broadcast_interval - true_bc_interval_offset )* 1000 );
536+ fhss_start_timer (fhss_structure , timeout , fhss_broadcast_handler );
537+ uint16_t slots_since_reception = (bc_timing_info -> broadcast_interval_offset + time_from_reception_ms ) / bc_timing_info -> broadcast_interval ;
538+ // TODO: Calculate drift error
539+ fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval = bc_timing_info -> broadcast_dwell_interval ;
540+ fhss_structure -> ws -> fhss_configuration .fhss_broadcast_interval = bc_timing_info -> broadcast_interval ;
541+ fhss_structure -> ws -> bc_slot = bc_timing_info -> broadcast_slot + slots_since_reception ;
542+
536543 //TODO: support multiple parents
537544 fhss_structure -> ws -> parent_bc_info = bc_timing_info ;
538545 return 0 ;
0 commit comments