From d3fdf84ee0c7b42a4af250d2c786c8972a40fdaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Duda?= Date: Mon, 15 Apr 2019 17:36:47 +0200 Subject: [PATCH] [platform] return error on incorrect otPlatRadioDisable usage (#3756) --- examples/platforms/nrf52811/radio.c | 55 +++++++++++++++--------- examples/platforms/nrf52840/radio.c | 51 ++++++++++++++-------- examples/platforms/posix/radio.c | 13 +++--- examples/platforms/posix/sim/radio-sim.c | 13 +++--- 4 files changed, 82 insertions(+), 50 deletions(-) diff --git a/examples/platforms/nrf52811/radio.c b/examples/platforms/nrf52811/radio.c index 6f6e0395b..48e2c3d7a 100644 --- a/examples/platforms/nrf52811/radio.c +++ b/examples/platforms/nrf52811/radio.c @@ -41,7 +41,8 @@ #include #include -#include +#include "utils/code_utils.h" + #include #include #include @@ -248,6 +249,8 @@ otRadioState otPlatRadioGetState(otInstance *aInstance) return OT_RADIO_STATE_RECEIVE; case NRF_802154_STATE_TRANSMIT: + case NRF_802154_STATE_CCA: + case NRF_802154_STATE_CONTINUOUS_CARRIER: return OT_RADIO_STATE_TRANSMIT; default: @@ -257,12 +260,19 @@ otRadioState otPlatRadioGetState(otInstance *aInstance) return OT_RADIO_STATE_RECEIVE; // It is the default state. Return it in case of unknown. } +bool otPlatRadioIsEnabled(otInstance *aInstance) +{ + OT_UNUSED_VARIABLE(aInstance); + + return !sDisabled; +} + otError otPlatRadioEnable(otInstance *aInstance) { - sInstance = aInstance; - otError error; + sInstance = aInstance; + if (sDisabled) { sDisabled = false; @@ -278,30 +288,18 @@ otError otPlatRadioEnable(otInstance *aInstance) otError otPlatRadioDisable(otInstance *aInstance) { - OT_UNUSED_VARIABLE(aInstance); + otError error = OT_ERROR_NONE; - otError error; + otEXPECT(otPlatRadioIsEnabled(aInstance)); + otEXPECT_ACTION(otPlatRadioGetState(aInstance) == OT_RADIO_STATE_SLEEP || isPendingEventSet(kPendingEventSleep), + error = OT_ERROR_INVALID_STATE); - if (!sDisabled) - { - sDisabled = true; - error = OT_ERROR_NONE; - } - else - { - error = OT_ERROR_INVALID_STATE; - } + sDisabled = true; +exit: return error; } -bool otPlatRadioIsEnabled(otInstance *aInstance) -{ - OT_UNUSED_VARIABLE(aInstance); - - return !sDisabled; -} - otError otPlatRadioSleep(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); @@ -550,6 +548,8 @@ otError otPlatRadioSetTransmitPower(otInstance *aInstance, int8_t aPower) void nrf5RadioProcess(otInstance *aInstance) { + bool isEventPending = false; + for (uint32_t i = 0; i < NRF_802154_RX_BUFFERS; i++) { if (sReceivedFrames[i].mPsdu != NULL) @@ -660,6 +660,10 @@ void nrf5RadioProcess(otInstance *aInstance) { resetPendingEvent(kPendingEventSleep); } + else + { + isEventPending = true; + } } if (isPendingEventSet(kPendingEventEnergyDetectionStart)) @@ -670,6 +674,15 @@ void nrf5RadioProcess(otInstance *aInstance) { resetPendingEvent(kPendingEventEnergyDetectionStart); } + else + { + isEventPending = true; + } + } + + if (isEventPending) + { + otSysEventSignalPending(); } } diff --git a/examples/platforms/nrf52840/radio.c b/examples/platforms/nrf52840/radio.c index 1ee0a7225..8d013c6a9 100644 --- a/examples/platforms/nrf52840/radio.c +++ b/examples/platforms/nrf52840/radio.c @@ -41,7 +41,8 @@ #include #include -#include +#include "utils/code_utils.h" + #include #include #include @@ -248,6 +249,8 @@ otRadioState otPlatRadioGetState(otInstance *aInstance) return OT_RADIO_STATE_RECEIVE; case NRF_802154_STATE_TRANSMIT: + case NRF_802154_STATE_CCA: + case NRF_802154_STATE_CONTINUOUS_CARRIER: return OT_RADIO_STATE_TRANSMIT; default: @@ -257,6 +260,13 @@ otRadioState otPlatRadioGetState(otInstance *aInstance) return OT_RADIO_STATE_RECEIVE; // It is the default state. Return it in case of unknown. } +bool otPlatRadioIsEnabled(otInstance *aInstance) +{ + OT_UNUSED_VARIABLE(aInstance); + + return !sDisabled; +} + otError otPlatRadioEnable(otInstance *aInstance) { otError error; @@ -278,30 +288,18 @@ otError otPlatRadioEnable(otInstance *aInstance) otError otPlatRadioDisable(otInstance *aInstance) { - otError error; + otError error = OT_ERROR_NONE; - OT_UNUSED_VARIABLE(aInstance); + otEXPECT(otPlatRadioIsEnabled(aInstance)); + otEXPECT_ACTION(otPlatRadioGetState(aInstance) == OT_RADIO_STATE_SLEEP || isPendingEventSet(kPendingEventSleep), + error = OT_ERROR_INVALID_STATE); - if (!sDisabled) - { - sDisabled = true; - error = OT_ERROR_NONE; - } - else - { - error = OT_ERROR_INVALID_STATE; - } + sDisabled = true; +exit: return error; } -bool otPlatRadioIsEnabled(otInstance *aInstance) -{ - OT_UNUSED_VARIABLE(aInstance); - - return !sDisabled; -} - otError otPlatRadioSleep(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); @@ -550,6 +548,8 @@ otError otPlatRadioSetTransmitPower(otInstance *aInstance, int8_t aPower) void nrf5RadioProcess(otInstance *aInstance) { + bool isEventPending = false; + for (uint32_t i = 0; i < NRF_802154_RX_BUFFERS; i++) { if (sReceivedFrames[i].mPsdu != NULL) @@ -660,6 +660,10 @@ void nrf5RadioProcess(otInstance *aInstance) { resetPendingEvent(kPendingEventSleep); } + else + { + isEventPending = true; + } } if (isPendingEventSet(kPendingEventEnergyDetectionStart)) @@ -670,6 +674,15 @@ void nrf5RadioProcess(otInstance *aInstance) { resetPendingEvent(kPendingEventEnergyDetectionStart); } + else + { + isEventPending = true; + } + } + + if (isEventPending) + { + otSysEventSignalPending(); } } diff --git a/examples/platforms/posix/radio.c b/examples/platforms/posix/radio.c index f5d94e8e1..0b4f7a0ef 100644 --- a/examples/platforms/posix/radio.c +++ b/examples/platforms/posix/radio.c @@ -478,12 +478,15 @@ otError otPlatRadioEnable(otInstance *aInstance) otError otPlatRadioDisable(otInstance *aInstance) { - if (otPlatRadioIsEnabled(aInstance)) - { - sState = OT_RADIO_STATE_DISABLED; - } + otError error = OT_ERROR_NONE; - return OT_ERROR_NONE; + otEXPECT(otPlatRadioIsEnabled(aInstance)); + otEXPECT_ACTION(sState == OT_RADIO_STATE_SLEEP, error = OT_ERROR_INVALID_STATE); + + sState = OT_RADIO_STATE_DISABLED; + +exit: + return error; } otError otPlatRadioSleep(otInstance *aInstance) diff --git a/examples/platforms/posix/sim/radio-sim.c b/examples/platforms/posix/sim/radio-sim.c index f15f79aa3..1d08fb35e 100644 --- a/examples/platforms/posix/sim/radio-sim.c +++ b/examples/platforms/posix/sim/radio-sim.c @@ -410,12 +410,15 @@ otError otPlatRadioEnable(otInstance *aInstance) otError otPlatRadioDisable(otInstance *aInstance) { - if (otPlatRadioIsEnabled(aInstance)) - { - sState = OT_RADIO_STATE_DISABLED; - } + otError error = OT_ERROR_NONE; - return OT_ERROR_NONE; + otEXPECT(otPlatRadioIsEnabled(aInstance)); + otEXPECT_ACTION(sState == OT_RADIO_STATE_SLEEP, error = OT_ERROR_INVALID_STATE); + + sState = OT_RADIO_STATE_DISABLED; + +exit: + return error; } otError otPlatRadioSleep(otInstance *aInstance)