Skip to content

Commit

Permalink
gnrc/lwmac: Reduce code duplication
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
francois-berder authored and miri64 committed Mar 26, 2024
1 parent b424711 commit 7523bb0
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions sys/net/gnrc/link_layer/lwmac/lwmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -492,35 +483,28 @@ 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");
gnrc_lwmac_rx_stop(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)
Expand Down

0 comments on commit 7523bb0

Please sign in to comment.