From 7523bb08a36dbf699e5f5ba7577cc307cd68cb24 Mon Sep 17 00:00:00 2001 From: Francois Berder Date: Wed, 26 May 2021 16:50:19 +0200 Subject: [PATCH] gnrc/lwmac: Reduce code duplication Both _rx_management_failed and _rx_management_success functions attempt to sleep after handling the packet reception failure/success. This commit extracts the sleep attempt in a new _rx_management_attempt_sleep function. Signed-off-by: Francois Berder --- sys/net/gnrc/link_layer/lwmac/lwmac.c | 48 +++++++++------------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/sys/net/gnrc/link_layer/lwmac/lwmac.c b/sys/net/gnrc/link_layer/lwmac/lwmac.c index 786ec394d9e0..2cde9649af97 100644 --- a/sys/net/gnrc/link_layer/lwmac/lwmac.c +++ b/sys/net/gnrc/link_layer/lwmac/lwmac.c @@ -457,17 +457,8 @@ static void _sleep_management(gnrc_netif_t *netif) } } -static void _rx_management_failed(gnrc_netif_t *netif) +static void _rx_management_attempt_sleep(gnrc_netif_t *netif) { - /* This may happen frequently because we'll receive WA from - * every node in range. */ - LOG_DEBUG("[LWMAC] Reception was NOT successful\n"); - gnrc_lwmac_rx_stop(netif); - - if (netif->mac.rx.rx_bad_exten_count >= CONFIG_GNRC_LWMAC_MAX_RX_EXTENSION_NUM) { - gnrc_lwmac_set_quit_rx(netif, true); - } - /* Here we check if we are close to the end of the cycle. If yes, * go to sleep. Firstly, get the relative phase. */ uint32_t phase = rtt_get_counter(); @@ -492,6 +483,20 @@ static void _rx_management_failed(gnrc_netif_t *netif) } } +static void _rx_management_failed(gnrc_netif_t *netif) +{ + /* This may happen frequently because we'll receive WA from + * every node in range. */ + LOG_DEBUG("[LWMAC] Reception was NOT successful\n"); + gnrc_lwmac_rx_stop(netif); + + if (netif->mac.rx.rx_bad_exten_count >= CONFIG_GNRC_LWMAC_MAX_RX_EXTENSION_NUM) { + gnrc_lwmac_set_quit_rx(netif, true); + } + + _rx_management_attempt_sleep(netif); +} + static void _rx_management_success(gnrc_netif_t *netif) { LOG_DEBUG("[LWMAC] Reception was successful\n"); @@ -499,28 +504,7 @@ static void _rx_management_success(gnrc_netif_t *netif) /* Dispatch received packets, timing is not critical anymore */ gnrc_mac_dispatch(&netif->mac.rx); - /* Here we check if we are close to the end of the cycle. If yes, - * go to sleep. Firstly, get the relative phase. */ - uint32_t phase = rtt_get_counter(); - if (phase < netif->mac.prot.lwmac.last_wakeup) { - phase = (RTT_US_TO_TICKS(GNRC_LWMAC_PHASE_MAX) - netif->mac.prot.lwmac.last_wakeup) + - phase; - } - else { - phase = phase - netif->mac.prot.lwmac.last_wakeup; - } - /* If the relative phase is beyond 4/5 cycle time, go to sleep. */ - if (phase > (4 * RTT_US_TO_TICKS(CONFIG_GNRC_LWMAC_WAKEUP_INTERVAL_US) / 5)) { - gnrc_lwmac_set_quit_rx(netif, true); - } - - if (gnrc_lwmac_get_quit_rx(netif)) { - lwmac_set_state(netif, GNRC_LWMAC_SLEEPING); - } - else { - /* Go back to LISTENING after successful reception */ - lwmac_set_state(netif, GNRC_LWMAC_LISTENING); - } + _rx_management_attempt_sleep(netif); } static void _rx_management(gnrc_netif_t *netif)