2525#include "fhss_ws.h"
2626#include "nsdynmemLIB.h"
2727#include "common_functions.h"
28+ #include "eventOS_callback_timer.h"
29+ #include "randLIB.h"
2830#include "ns_trace.h"
2931#include <string.h>
3032
@@ -48,6 +50,7 @@ struct ws_ie_t {
4850
4951static void fhss_ws_update_uc_channel_callback (fhss_structure_t * fhss_structure );
5052static void fhss_unicast_handler (const fhss_api_t * fhss_api , uint16_t delay );
53+ static void fhss_ws_start_tx_poll_timer (fhss_structure_t * fhss_structure , uint16_t queue_size , uint8_t channel_dwell_interval );
5154
5255fhss_structure_t * fhss_ws_enable (fhss_api_t * fhss_api , const fhss_ws_configuration_t * fhss_configuration , const fhss_timer_t * fhss_timer )
5356{
@@ -124,10 +127,8 @@ static void fhss_broadcast_handler(const fhss_api_t *fhss_api, uint16_t delay)
124127#endif /*FHSS_CHANNEL_DEBUG*/
125128 }
126129 fhss_structure -> callbacks .change_channel (fhss_structure -> fhss_api , next_channel );
127- if (fhss_structure -> callbacks .read_tx_queue_size (fhss_structure -> fhss_api , true)) {
128- // TODO: Randomize polling TX queue when on broadcast channel
129- fhss_structure -> callbacks .tx_poll (fhss_structure -> fhss_api );
130- }
130+ fhss_ws_start_tx_poll_timer (fhss_structure , fhss_structure -> callbacks .read_tx_queue_size (fhss_structure -> fhss_api , true),
131+ fhss_structure -> ws -> fhss_configuration .fhss_bc_dwell_interval );
131132}
132133
133134static int own_floor (float value )
@@ -454,9 +455,24 @@ static void fhss_unicast_handler(const fhss_api_t *fhss_api, uint16_t delay)
454455 timeout = fhss_ws_get_sf_timeout_callback (fhss_structure );
455456 fhss_start_timer (fhss_structure , timeout - (delay * fhss_structure -> platform_functions .fhss_resolution_divider ), fhss_unicast_handler );
456457 fhss_ws_update_uc_channel_callback (fhss_structure );
457- if (fhss_structure -> callbacks .read_tx_queue_size (fhss_structure -> fhss_api , false)) {
458- fhss_structure -> callbacks .tx_poll (fhss_structure -> fhss_api );
458+ fhss_ws_start_tx_poll_timer (fhss_structure , fhss_structure -> callbacks .read_tx_queue_size (fhss_structure -> fhss_api , false),
459+ fhss_structure -> ws -> fhss_configuration .fhss_uc_dwell_interval );
460+ }
461+
462+ static void fhss_ws_start_tx_poll_timer (fhss_structure_t * fhss_structure , uint16_t queue_size , uint8_t channel_dwell_interval )
463+ {
464+ if (!queue_size ) {
465+ return ;
459466 }
467+ /* Start timer with random timeout to trigger TX queue poll event.
468+ * Min random is 1/50 of the channel dwell interval.
469+ * Max random is 1/10 of the channel dwell interval.
470+ * Event timer resolution is 50us.
471+ * Divide random with TX queue size to transmit faster when TX queue is growing
472+ */
473+ uint16_t min_random = ((((uint32_t )channel_dwell_interval * 1000 ) / 50 ) / 50 ) / queue_size ;
474+ uint16_t max_random = ((((uint32_t )channel_dwell_interval * 1000 ) / 10 ) / 50 ) / queue_size ;
475+ eventOS_callback_timer_start (fhss_structure -> fhss_event_timer , randLIB_get_random_in_range (min_random , max_random ));
460476}
461477
462478int fhss_ws_set_callbacks (fhss_structure_t * fhss_structure )
0 commit comments