From 7523bb08a36dbf699e5f5ba7577cc307cd68cb24 Mon Sep 17 00:00:00 2001 From: Francois Berder Date: Wed, 26 May 2021 16:50:19 +0200 Subject: [PATCH 1/2] 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) From 9056e14e74ff51aafec2257427f58d8d92c12ddb Mon Sep 17 00:00:00 2001 From: Francois Berder Date: Wed, 26 May 2021 16:50:57 +0200 Subject: [PATCH 2/2] gnrc/gomach: Reduce code duplication The function gomach_vtdma_end is nearly identical to _no_vtdma_after_cp expect the first few lines. This commit replaces the duplicating code in gomach_vtdma_end by a call to _no_vtdma_after_cp. Signed-off-by: Francois Berder --- sys/net/gnrc/link_layer/gomach/gomach.c | 48 +------------------------ 1 file changed, 1 insertion(+), 47 deletions(-) diff --git a/sys/net/gnrc/link_layer/gomach/gomach.c b/sys/net/gnrc/link_layer/gomach/gomach.c index 90ab50bb1326..d220327ca566 100644 --- a/sys/net/gnrc/link_layer/gomach/gomach.c +++ b/sys/net/gnrc/link_layer/gomach/gomach.c @@ -1788,53 +1788,7 @@ static void gomach_vtdma_end(gnrc_netif_t *netif) /* Switch the radio to the public-channel. */ gnrc_gomach_turn_channel(netif, netif->mac.prot.gomach.cur_pub_channel); - /* Check if there is packet to send. */ - if (gnrc_gomach_find_next_tx_neighbor(netif)) { - if (netif->mac.tx.current_neighbor == &netif->mac.tx.neighbors[0]) { - /* The packet is for broadcasting. */ - if (!gnrc_gomach_get_unintd_preamble(netif)) { - netif->mac.prot.gomach.basic_state = GNRC_GOMACH_TRANSMIT; - netif->mac.tx.transmit_state = GNRC_GOMACH_BROADCAST; - } - else { - netif->mac.rx.listen_state = GNRC_GOMACH_LISTEN_SLEEP_INIT; - } - } - else { - switch (netif->mac.tx.current_neighbor->mac_type) { - /* The packet waiting to be sent is for unicast. */ - case GNRC_GOMACH_TYPE_UNKNOWN: { - /* The neighbor's phase is unknown yet, try to run t2u (transmission - * to unknown device) procedure to phase-lock the neighbor. */ - if (!gnrc_gomach_get_unintd_preamble(netif)) { - netif->mac.prot.gomach.basic_state = GNRC_GOMACH_TRANSMIT; - netif->mac.tx.transmit_state = GNRC_GOMACH_TRANS_TO_UNKNOWN; - } - else { - netif->mac.rx.listen_state = GNRC_GOMACH_LISTEN_SLEEP_INIT; - } - } break; - case GNRC_GOMACH_TYPE_KNOWN: { - /* If the neighbor's phase is known, go to t2k (transmission - * to known device) procedure. Here, we don't worry that the t2k - * unicast transmission will interrupt with possible ongoing - * preamble transmissions of other devices. */ - netif->mac.prot.gomach.basic_state = GNRC_GOMACH_TRANSMIT; - netif->mac.tx.transmit_state = GNRC_GOMACH_TRANS_TO_KNOWN; - } break; - default: { - LOG_ERROR("ERROR: [GOMACH] vTDMA: unknown MAC type of the neighbor.\n"); - break; - } - } - } - } - else { - /* No packet to send, go to sleep. */ - netif->mac.rx.listen_state = GNRC_GOMACH_LISTEN_SLEEP_INIT; - } - - gnrc_gomach_set_update(netif, true); + _no_vtdma_after_cp(netif); } static void gomach_sleep_init(gnrc_netif_t *netif)