mirror of
https://github.com/espressif/openthread.git
synced 2026-08-18 08:29:52 +00:00
[radio-spinel] simplify state definitions in RadioSpinel. (#3314)
This commit defines as new set of `State` enumerations which combines the two state variables `otRadioState` and `TxState` into a single `mState` variable.
This commit is contained in:
committed by
Jonathan Hui
parent
71753b3cb6
commit
122b8bbfe2
@@ -96,13 +96,6 @@ enum
|
||||
IEEE802154_MACCMD_DATA_REQ = 4,
|
||||
};
|
||||
|
||||
enum
|
||||
{
|
||||
kIdle,
|
||||
kSent,
|
||||
kDone,
|
||||
};
|
||||
|
||||
static ot::PosixApp::RadioSpinel sRadioSpinel;
|
||||
|
||||
static inline bool isAckRequested(const uint8_t *frame)
|
||||
@@ -212,8 +205,7 @@ RadioSpinel::RadioSpinel(void)
|
||||
, mWaitingTid(0)
|
||||
, mWaitingKey(SPINEL_PROP_LAST_STATUS)
|
||||
, mRxSensitivity(0)
|
||||
, mTxState(kIdle)
|
||||
, mState(OT_RADIO_STATE_DISABLED)
|
||||
, mState(kStateDisabled)
|
||||
, mIsAckRequested(false)
|
||||
, mIsPromiscuous(false)
|
||||
, mIsReady(false)
|
||||
@@ -438,7 +430,11 @@ void RadioSpinel::HandleResponse(const uint8_t *aBuffer, uint16_t aLength)
|
||||
}
|
||||
else if (mTxRadioTid == SPINEL_HEADER_GET_TID(header))
|
||||
{
|
||||
HandleTransmitDone(cmd, key, data, static_cast<uint16_t>(len));
|
||||
if (mState == kStateTransmitting)
|
||||
{
|
||||
HandleTransmitDone(cmd, key, data, static_cast<uint16_t>(len));
|
||||
}
|
||||
|
||||
FreeTid(mTxRadioTid);
|
||||
mTxRadioTid = 0;
|
||||
}
|
||||
@@ -649,7 +645,21 @@ void RadioSpinel::ProcessFrameQueue(void)
|
||||
|
||||
void RadioSpinel::RadioReceive(void)
|
||||
{
|
||||
VerifyOrExit(mIsPromiscuous || mState == OT_RADIO_STATE_RECEIVE || mState == OT_RADIO_STATE_TRANSMIT);
|
||||
if (!mIsPromiscuous)
|
||||
{
|
||||
switch (mState)
|
||||
{
|
||||
case kStateDisabled:
|
||||
case kStateSleep:
|
||||
ExitNow();
|
||||
|
||||
case kStateReceive:
|
||||
case kStateTransmitPending:
|
||||
case kStateTransmitting:
|
||||
case kStateTransmitDone:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#if OPENTHREAD_ENABLE_DIAG
|
||||
if (otPlatDiagModeGet())
|
||||
@@ -670,7 +680,7 @@ void RadioSpinel::UpdateFdSet(fd_set &aReadFdSet, fd_set &aWriteFdSet, int &aMax
|
||||
{
|
||||
int sockFd = mHdlcInterface.GetSocket();
|
||||
|
||||
if ((mState != OT_RADIO_STATE_TRANSMIT || mTxState == kSent))
|
||||
if ((mState != kStateTransmitPending) && (mState != kStateTransmitDone))
|
||||
{
|
||||
FD_SET(sockFd, &aReadFdSet);
|
||||
|
||||
@@ -680,7 +690,7 @@ void RadioSpinel::UpdateFdSet(fd_set &aReadFdSet, fd_set &aWriteFdSet, int &aMax
|
||||
}
|
||||
}
|
||||
|
||||
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kIdle)
|
||||
if (mState == kStateTransmitPending)
|
||||
{
|
||||
FD_SET(sockFd, &aWriteFdSet);
|
||||
|
||||
@@ -690,7 +700,7 @@ void RadioSpinel::UpdateFdSet(fd_set &aReadFdSet, fd_set &aWriteFdSet, int &aMax
|
||||
}
|
||||
}
|
||||
|
||||
if (!mFrameQueue.IsEmpty() || (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kDone))
|
||||
if (!mFrameQueue.IsEmpty() || (mState == kStateTransmitDone))
|
||||
{
|
||||
aTimeout.tv_sec = 0;
|
||||
aTimeout.tv_usec = 0;
|
||||
@@ -711,9 +721,9 @@ void RadioSpinel::Process(const fd_set &aReadFdSet, const fd_set &aWriteFdSet)
|
||||
}
|
||||
}
|
||||
|
||||
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kDone)
|
||||
if (mState == kStateTransmitDone)
|
||||
{
|
||||
mState = OT_RADIO_STATE_RECEIVE;
|
||||
mState = kStateReceive;
|
||||
|
||||
#if OPENTHREAD_ENABLE_DIAG
|
||||
if (otPlatDiagModeGet())
|
||||
@@ -725,13 +735,11 @@ void RadioSpinel::Process(const fd_set &aReadFdSet, const fd_set &aWriteFdSet)
|
||||
{
|
||||
otPlatRadioTxDone(mInstance, mTransmitFrame, (mIsAckRequested ? &mAckRadioFrame : NULL), mTxError);
|
||||
}
|
||||
|
||||
mTxState = kIdle;
|
||||
}
|
||||
|
||||
if (FD_ISSET(mHdlcInterface.GetSocket(), &aWriteFdSet))
|
||||
{
|
||||
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kIdle)
|
||||
if (mState == kStateTransmitPending)
|
||||
{
|
||||
RadioTransmit();
|
||||
}
|
||||
@@ -1041,7 +1049,7 @@ void RadioSpinel::RadioTransmit(void)
|
||||
|
||||
assert(mTransmitFrame != NULL);
|
||||
otPlatRadioTxStarted(mInstance, mTransmitFrame);
|
||||
assert(mTxState == kIdle);
|
||||
assert(mState == kStateTransmitPending);
|
||||
|
||||
mIsAckRequested = isAckRequested(mTransmitFrame->mPsdu) && !mIsPromiscuous;
|
||||
|
||||
@@ -1051,11 +1059,11 @@ void RadioSpinel::RadioTransmit(void)
|
||||
|
||||
if (error == OT_ERROR_NONE)
|
||||
{
|
||||
mTxState = kSent;
|
||||
mState = kStateTransmitting;
|
||||
}
|
||||
else
|
||||
{
|
||||
mState = OT_RADIO_STATE_RECEIVE;
|
||||
mState = kStateReceive;
|
||||
|
||||
#if OPENTHREAD_ENABLE_DIAG
|
||||
|
||||
@@ -1068,8 +1076,6 @@ void RadioSpinel::RadioTransmit(void)
|
||||
{
|
||||
otPlatRadioTxDone(mInstance, mTransmitFrame, NULL, error);
|
||||
}
|
||||
|
||||
mTxState = kIdle;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1204,7 +1210,7 @@ void RadioSpinel::HandleTransmitDone(uint32_t aCommand,
|
||||
}
|
||||
|
||||
exit:
|
||||
mTxState = kDone;
|
||||
mState = kStateTransmitDone;
|
||||
mTxError = error;
|
||||
LogIfFail("Handle transmit done failed", error);
|
||||
}
|
||||
@@ -1213,8 +1219,8 @@ otError RadioSpinel::Transmit(otRadioFrame &aFrame)
|
||||
{
|
||||
otError error = OT_ERROR_INVALID_STATE;
|
||||
|
||||
VerifyOrExit(mState == OT_RADIO_STATE_RECEIVE);
|
||||
mState = OT_RADIO_STATE_TRANSMIT;
|
||||
VerifyOrExit(mState == kStateReceive);
|
||||
mState = kStateTransmitPending;
|
||||
error = OT_ERROR_NONE;
|
||||
mTransmitFrame = &aFrame;
|
||||
|
||||
@@ -1226,7 +1232,7 @@ otError RadioSpinel::Receive(uint8_t aChannel)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
VerifyOrExit(mState != OT_RADIO_STATE_DISABLED, error = OT_ERROR_INVALID_STATE);
|
||||
VerifyOrExit(mState != kStateDisabled, error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
if (mChannel != aChannel)
|
||||
{
|
||||
@@ -1235,7 +1241,7 @@ otError RadioSpinel::Receive(uint8_t aChannel)
|
||||
mChannel = aChannel;
|
||||
}
|
||||
|
||||
if (mState == OT_RADIO_STATE_SLEEP)
|
||||
if (mState == kStateSleep)
|
||||
{
|
||||
error = Set(SPINEL_PROP_MAC_RAW_STREAM_ENABLED, SPINEL_DATATYPE_BOOL_S, true);
|
||||
VerifyOrExit(error == OT_ERROR_NONE);
|
||||
@@ -1247,8 +1253,7 @@ otError RadioSpinel::Receive(uint8_t aChannel)
|
||||
mTxRadioTid = 0;
|
||||
}
|
||||
|
||||
mTxState = kIdle;
|
||||
mState = OT_RADIO_STATE_RECEIVE;
|
||||
mState = kStateReceive;
|
||||
|
||||
exit:
|
||||
assert(error == OT_ERROR_NONE);
|
||||
@@ -1261,14 +1266,14 @@ otError RadioSpinel::Sleep(void)
|
||||
|
||||
switch (mState)
|
||||
{
|
||||
case OT_RADIO_STATE_RECEIVE:
|
||||
case kStateReceive:
|
||||
error = sRadioSpinel.Set(SPINEL_PROP_MAC_RAW_STREAM_ENABLED, SPINEL_DATATYPE_BOOL_S, false);
|
||||
VerifyOrExit(error == OT_ERROR_NONE);
|
||||
|
||||
mState = OT_RADIO_STATE_SLEEP;
|
||||
mState = kStateSleep;
|
||||
break;
|
||||
|
||||
case OT_RADIO_STATE_SLEEP:
|
||||
case kStateSleep:
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -1294,7 +1299,7 @@ otError RadioSpinel::Enable(otInstance *aInstance)
|
||||
error = Get(SPINEL_PROP_PHY_RX_SENSITIVITY, SPINEL_DATATYPE_INT8_S, &mRxSensitivity);
|
||||
VerifyOrExit(error == OT_ERROR_NONE);
|
||||
|
||||
mState = OT_RADIO_STATE_SLEEP;
|
||||
mState = kStateSleep;
|
||||
}
|
||||
|
||||
exit:
|
||||
@@ -1312,7 +1317,7 @@ otError RadioSpinel::Disable(void)
|
||||
error = sRadioSpinel.Set(SPINEL_PROP_PHY_ENABLED, SPINEL_DATATYPE_BOOL_S, false);
|
||||
VerifyOrExit(error == OT_ERROR_NONE);
|
||||
|
||||
mState = OT_RADIO_STATE_DISABLED;
|
||||
mState = kStateDisabled;
|
||||
}
|
||||
|
||||
exit:
|
||||
@@ -1558,9 +1563,9 @@ void ot::PosixApp::RadioSpinel::Process(const Event &aEvent)
|
||||
ProcessFrameQueue();
|
||||
}
|
||||
|
||||
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kDone)
|
||||
if (mState == kStateTransmitDone)
|
||||
{
|
||||
mState = OT_RADIO_STATE_RECEIVE;
|
||||
mState = kStateReceive;
|
||||
|
||||
#if OPENTHREAD_ENABLE_DIAG
|
||||
if (otPlatDiagModeGet())
|
||||
@@ -1572,11 +1577,9 @@ void ot::PosixApp::RadioSpinel::Process(const Event &aEvent)
|
||||
{
|
||||
otPlatRadioTxDone(mInstance, mTransmitFrame, (mIsAckRequested ? &mAckRadioFrame : NULL), mTxError);
|
||||
}
|
||||
|
||||
mTxState = kIdle;
|
||||
}
|
||||
|
||||
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kIdle)
|
||||
if (mState == kStateTransmitPending)
|
||||
{
|
||||
RadioTransmit();
|
||||
}
|
||||
@@ -1585,7 +1588,7 @@ void ot::PosixApp::RadioSpinel::Process(const Event &aEvent)
|
||||
void ot::PosixApp::RadioSpinel::Update(struct timeval &aTimeout)
|
||||
{
|
||||
// Prevent sleep event when transmitting
|
||||
if (mState == OT_RADIO_STATE_TRANSMIT && mTxState == kIdle)
|
||||
if (mState == kStateTransmitPending)
|
||||
{
|
||||
aTimeout.tv_sec = 0;
|
||||
aTimeout.tv_usec = 0;
|
||||
|
||||
@@ -368,7 +368,7 @@ public:
|
||||
* @returns TRUE if the radio is enabled, FALSE otherwise.
|
||||
*
|
||||
*/
|
||||
bool IsEnabled(void) const { return mState != OT_RADIO_STATE_DISABLED; }
|
||||
bool IsEnabled(void) const { return mState != kStateDisabled; }
|
||||
|
||||
/**
|
||||
* This method updates the file descriptor sets with file descriptors used by the radio driver.
|
||||
@@ -458,6 +458,16 @@ private:
|
||||
kCapsBufferSize = 100, ///< Max buffer size used to store `SPINEL_PROP_CAPS` value.
|
||||
};
|
||||
|
||||
enum State
|
||||
{
|
||||
kStateDisabled, ///< Radio is disabled.
|
||||
kStateSleep, ///< Radio is sleep.
|
||||
kStateReceive, ///< Radio is in receive mode.
|
||||
kStateTransmitPending, ///< Frame transmission requested, waiting to pass frame to radio.
|
||||
kStateTransmitting, ///< Frame passed to radio for transmission, waiting for done event from radio.
|
||||
kStateTransmitDone, ///< Radio indicated frame transmission is done.
|
||||
};
|
||||
|
||||
otError CheckSpinelVersion(void);
|
||||
otError CheckCapabilities(void);
|
||||
otError CheckRadioCapabilities(void);
|
||||
@@ -595,15 +605,14 @@ private:
|
||||
otRadioCaps mRadioCaps;
|
||||
uint8_t mChannel;
|
||||
int8_t mRxSensitivity;
|
||||
uint8_t mTxState;
|
||||
otError mTxError;
|
||||
char mVersion[kVersionStringSize];
|
||||
|
||||
otRadioState mState;
|
||||
bool mIsAckRequested : 1; ///< Ack requested.
|
||||
bool mIsPromiscuous : 1; ///< Promiscuous mode.
|
||||
bool mIsReady : 1; ///< NCP ready.
|
||||
bool mSupportsLogStream : 1; ///< RCP supports `LOG_STREAM` property with OpenThread log meta-data format.
|
||||
State mState;
|
||||
bool mIsAckRequested : 1; ///< Ack requested.
|
||||
bool mIsPromiscuous : 1; ///< Promiscuous mode.
|
||||
bool mIsReady : 1; ///< NCP ready.
|
||||
bool mSupportsLogStream : 1; ///< RCP supports `LOG_STREAM` property with OpenThread log meta-data format.
|
||||
|
||||
#if OPENTHREAD_ENABLE_DIAG
|
||||
bool mDiagMode;
|
||||
|
||||
Reference in New Issue
Block a user