From bbb19c2aa294dbc4c542e71af81d4539971dc237 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Tue, 9 Apr 2019 12:24:52 +0800 Subject: [PATCH] [radio] only allow disable in sleep state (#3733) There's missing error definition for otPlatRadioDisable(). This commit adds an error case OT_ERROR_INVALID_STATE to make sure this is only called when the radio is in sleep mode. --- include/openthread/platform/radio.h | 3 ++- src/core/mac/sub_mac.cpp | 5 ++-- src/posix/platform/radio_spinel.cpp | 36 ++++++++++++++--------------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/include/openthread/platform/radio.h b/include/openthread/platform/radio.h index 7712b2c18..6f6334e4f 100644 --- a/include/openthread/platform/radio.h +++ b/include/openthread/platform/radio.h @@ -422,7 +422,8 @@ otError otPlatRadioEnable(otInstance *aInstance); * * @param[in] aInstance The OpenThread instance structure. * - * @retval OT_ERROR_NONE Successfully transitioned to Disabled. + * @retval OT_ERROR_NONE Successfully transitioned to Disabled. + * @retval OT_ERROR_INVALID_STATE The radio was not in sleep state. * */ otError otPlatRadioDisable(otInstance *aInstance); diff --git a/src/core/mac/sub_mac.cpp b/src/core/mac/sub_mac.cpp index 0b7d0a463..1b45a722e 100644 --- a/src/core/mac/sub_mac.cpp +++ b/src/core/mac/sub_mac.cpp @@ -148,10 +148,11 @@ otError SubMac::Disable(void) otError error; mTimer.Stop(); - error = otPlatRadioDisable(&GetInstance()); - assert(error == OT_ERROR_NONE); + SuccessOrExit(error = otPlatRadioSleep(&GetInstance())); + SuccessOrExit(error = otPlatRadioDisable(&GetInstance())); SetState(kStateDisabled); +exit: return error; } diff --git a/src/posix/platform/radio_spinel.cpp b/src/posix/platform/radio_spinel.cpp index 6d3b07160..95ee2bb30 100644 --- a/src/posix/platform/radio_spinel.cpp +++ b/src/posix/platform/radio_spinel.cpp @@ -1308,22 +1308,24 @@ otError RadioSpinel::Enable(otInstance *aInstance) { otError error = OT_ERROR_NONE; - if (!otPlatRadioIsEnabled(mInstance)) - { - mInstance = aInstance; + VerifyOrExit(!IsEnabled()); - SuccessOrExit(error = Set(SPINEL_PROP_PHY_ENABLED, SPINEL_DATATYPE_BOOL_S, true)); - SuccessOrExit(error = Set(SPINEL_PROP_MAC_15_4_PANID, SPINEL_DATATYPE_UINT16_S, mPanId)); - SuccessOrExit(error = Set(SPINEL_PROP_MAC_15_4_SADDR, SPINEL_DATATYPE_UINT16_S, mShortAddress)); + mInstance = aInstance; - error = Get(SPINEL_PROP_PHY_RX_SENSITIVITY, SPINEL_DATATYPE_INT8_S, &mRxSensitivity); - VerifyOrExit(error == OT_ERROR_NONE); + SuccessOrExit(error = Set(SPINEL_PROP_PHY_ENABLED, SPINEL_DATATYPE_BOOL_S, true)); + SuccessOrExit(error = Set(SPINEL_PROP_MAC_15_4_PANID, SPINEL_DATATYPE_UINT16_S, mPanId)); + SuccessOrExit(error = Set(SPINEL_PROP_MAC_15_4_SADDR, SPINEL_DATATYPE_UINT16_S, mShortAddress)); + SuccessOrExit(error = Get(SPINEL_PROP_PHY_RX_SENSITIVITY, SPINEL_DATATYPE_INT8_S, &mRxSensitivity)); - mState = kStateSleep; - } + mState = kStateSleep; exit: - assert(error == OT_ERROR_NONE); + if (error != OT_ERROR_NONE) + { + otLogWarnPlat("RadioSpinel enable: %s", otThreadErrorToString(error)); + error = OT_ERROR_FAILED; + } + return error; } @@ -1331,14 +1333,12 @@ otError RadioSpinel::Disable(void) { otError error = OT_ERROR_NONE; - if (otPlatRadioIsEnabled(mInstance)) - { - mInstance = NULL; - error = sRadioSpinel.Set(SPINEL_PROP_PHY_ENABLED, SPINEL_DATATYPE_BOOL_S, false); - VerifyOrExit(error == OT_ERROR_NONE); + VerifyOrExit(IsEnabled()); + VerifyOrExit(mState == kStateSleep, error = OT_ERROR_INVALID_STATE); - mState = kStateDisabled; - } + SuccessOrDie(sRadioSpinel.Set(SPINEL_PROP_PHY_ENABLED, SPINEL_DATATYPE_BOOL_S, false)); + mState = kStateDisabled; + mInstance = NULL; exit: return error;