Skip to content

Commit

Permalink
driver/at86rf2xx: Fix possible race condition where at86rf2xx_configu…
Browse files Browse the repository at this point in the history
…re returns to the wrong state if waited on finished state in at86rf2xx_setstate
  • Loading branch information
DipSwitch authored and aabadie committed Jun 23, 2017
1 parent 786baac commit 4e695c7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
18 changes: 10 additions & 8 deletions drivers/at86rf2xx/at86rf2xx_getset.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,17 +444,12 @@ static inline void _set_state(at86rf2xx_t *dev, uint8_t state, uint8_t cmd)
dev->state = state;
}

void at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state)
uint8_t at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state)
{
uint8_t old_state = at86rf2xx_get_status(dev);

if (state == old_state) {
return;
}

if (state == AT86RF2XX_STATE_FORCE_TRX_OFF) {
_set_state(dev, AT86RF2XX_STATE_TRX_OFF, state);
return;
return old_state;
}

/* make sure there is no ongoing transmission, or state transition already
Expand All @@ -465,8 +460,13 @@ void at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state)
old_state = at86rf2xx_get_status(dev);
}

if (state == AT86RF2XX_STATE_FORCE_TRX_OFF) {
_set_state(dev, AT86RF2XX_STATE_TRX_OFF, state);
return old_state;
}

if (state == old_state) {
return;
return old_state;
}

/* we need to go via PLL_ON if we are moving between RX_AACK_ON <-> TX_ARET_ON */
Expand All @@ -493,6 +493,8 @@ void at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state)
} else {
_set_state(dev, state, state);
}

return old_state;
}

void at86rf2xx_reset_state_machine(at86rf2xx_t *dev)
Expand Down
4 changes: 2 additions & 2 deletions drivers/at86rf2xx/at86rf2xx_internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void at86rf2xx_hardware_reset(at86rf2xx_t *dev)

void at86rf2xx_configure_phy(at86rf2xx_t *dev)
{
uint8_t state = at86rf2xx_get_status(dev);
uint8_t prev_state = at86rf2xx_get_status(dev);

/* we must be in TRX_OFF before changing the PHY configuration */
at86rf2xx_set_state(dev, AT86RF2XX_STATE_TRX_OFF);
Expand Down Expand Up @@ -200,7 +200,7 @@ void at86rf2xx_configure_phy(at86rf2xx_t *dev)
#endif

/* Return to the state we had before reconfiguring */
at86rf2xx_set_state(dev, state);
at86rf2xx_set_state(dev, prev_state);
}

#if defined(MODULE_AT86RF233) || defined(MODULE_AT86RF231)
Expand Down
4 changes: 3 additions & 1 deletion drivers/include/at86rf2xx.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ void at86rf2xx_set_option(at86rf2xx_t *dev, uint16_t option, bool state);
*
* @param[in] dev device to change state of
* @param[in] state the targeted new state
*
* @return the previous error before the new state was set
*/
void at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state);
uint8_t at86rf2xx_set_state(at86rf2xx_t *dev, uint8_t state);

/**
* @brief Reset the internal state machine to TRX_OFF mode.
Expand Down

0 comments on commit 4e695c7

Please sign in to comment.