@@ -64,6 +64,12 @@ static void fhss_unicast_handler(const fhss_api_t *fhss_api, uint16_t delay);
6464static bool fhss_ws_check_tx_allowed (fhss_structure_t * fhss_structure );
6565static uint8_t fhss_set_txrx_slot_length (fhss_structure_t * fhss_structure );
6666
67+ // This function supports rounding up
68+ static uint32_t divide_integer (uint32_t dividend , uint32_t divisor )
69+ {
70+ return (dividend + divisor /2 ) / divisor ;
71+ }
72+
6773fhss_structure_t * fhss_ws_enable (fhss_api_t * fhss_api , const fhss_ws_configuration_t * fhss_configuration , const fhss_timer_t * fhss_timer )
6874{
6975 if (!fhss_api || !fhss_configuration || !fhss_timer ) {
@@ -164,12 +170,12 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
164170 // Should return to own (unicast) listening channel after broadcast channel
165171 next_channel = fhss_structure -> rx_channel ;
166172 /* Start timer with random timeout to trigger unicast TX queue poll event.
167- * Min random is 50us .
173+ * Min random is 1/30 of the TX slot length .
168174 * Max random is 1/10 of the TX slot length.
169175 * Event timer resolution is 50us.
170176 */
171177 uint32_t txrx_slot_length_us = fhss_structure -> ws -> txrx_slot_length_ms * 1000 ;
172- uint16_t uc_min_random = 1 ;
178+ uint16_t uc_min_random = ( txrx_slot_length_us / 30 ) / 50 ;
173179 uint16_t uc_max_random = (txrx_slot_length_us / 10 ) / 50 ;
174180 bool tx_allowed = fhss_ws_check_tx_allowed (fhss_structure );
175181 if (!tx_allowed ) {
@@ -254,14 +260,14 @@ static uint32_t fhss_ws_calculate_broadcast_interval_offset(fhss_structure_t *fh
254260 uint8_t dwell_time = fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval ;
255261 uint32_t broadcast_interval = fhss_structure -> ws -> fhss_configuration .fhss_broadcast_interval ;
256262
257- uint32_t remaining_time = (fhss_structure -> platform_functions .fhss_get_remaining_slots (fhss_broadcast_handler , fhss_structure -> fhss_api ) / 1000 );
263+ uint32_t remaining_time = divide_integer (fhss_structure -> platform_functions .fhss_get_remaining_slots (fhss_broadcast_handler , fhss_structure -> fhss_api ), 1000 );
258264 if (fhss_structure -> ws -> is_on_bc_channel == true) {
259265 remaining_time += (broadcast_interval - dwell_time );
260266 }
261267 uint32_t time_to_tx = 0 ;
262268 uint32_t cur_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
263269 if (cur_time < tx_time ) {
264- time_to_tx = (tx_time - cur_time ) / 1000 ;
270+ time_to_tx = divide_integer (tx_time - cur_time , 1000 ) ;
265271 }
266272 return (broadcast_interval - remaining_time ) + time_to_tx ;
267273}
@@ -360,9 +366,17 @@ static int fhss_ws_tx_handle_callback(const fhss_api_t *api, bool is_broadcast_a
360366 if (!fhss_structure ) {
361367 return -1 ;
362368 }
363- if (is_broadcast_addr || ( fhss_structure -> ws -> is_on_bc_channel == true) ) {
369+ if (is_broadcast_addr ) {
364370 return 0 ;
365371 }
372+ // Do not allow unicast destination on broadcast channel
373+ if (!is_broadcast_addr && (fhss_structure -> ws -> is_on_bc_channel == true)) {
374+ return -1 ;
375+ }
376+ // Check TX/RX slot
377+ if (!fhss_ws_check_tx_allowed (fhss_structure )) {
378+ return -1 ;
379+ }
366380 if (fhss_structure -> fhss_state == FHSS_SYNCHRONIZED ) {
367381 fhss_ws_neighbor_timing_info_t * neighbor_timing_info = fhss_structure -> ws -> get_neighbor_info (api , destination_address );
368382 if (!neighbor_timing_info ) {
@@ -673,7 +687,7 @@ int fhss_ws_set_parent(fhss_structure_t *fhss_structure, const uint8_t eui64[8],
673687 fhss_structure -> ws -> synchronization_time = fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api );
674688 platform_enter_critical ();
675689 fhss_stop_timer (fhss_structure , fhss_broadcast_handler );
676- uint32_t time_from_reception_ms = (fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api ) - bc_timing_info -> bt_rx_timestamp )/ 1000 ;
690+ uint32_t time_from_reception_ms = divide_integer (fhss_structure -> callbacks .read_timestamp (fhss_structure -> fhss_api ) - bc_timing_info -> bt_rx_timestamp , 1000 ) ;
677691 uint32_t true_bc_interval_offset = (bc_timing_info -> broadcast_interval_offset + time_from_reception_ms ) % bc_timing_info -> broadcast_interval ;
678692 uint32_t timeout = ((bc_timing_info -> broadcast_interval - true_bc_interval_offset )* 1000 );
679693
0 commit comments