Skip to content

Commit

Permalink
[spinel] stop Spinel time sync when Thread satck is not running
Browse files Browse the repository at this point in the history
The radio_spinel periodically send Spinel command
SPINEL_PROP_RCP_TIMESTAMP to RCP to synchronize time between host
and RCP. Even if Thread stack is not running, it will periodically
wake up the Thread host.

This commit stops the time sync when the Thread stack is not running
to save host power.
  • Loading branch information
zhanglongxia committed Aug 5, 2024
1 parent 7096928 commit f5248b9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/lib/spinel/radio_spinel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ RadioSpinel::RadioSpinel(void)
, mVendorRestorePropertiesCallback(nullptr)
, mVendorRestorePropertiesContext(nullptr)
#endif
, mEnableRcpTimeSync(false)
, mTimeSyncFeatureEnabled(false)
, mTimeSyncEnabled(false)
, mSpinelDriver(nullptr)
{
memset(&mRadioSpinelMetrics, 0, sizeof(mRadioSpinelMetrics));
Expand All @@ -134,7 +135,7 @@ void RadioSpinel::Init(bool aSkipRcpCompatibilityCheck,
mResetRadioOnStartup = aSoftwareReset;
#endif

mEnableRcpTimeSync = aEnableRcpTimeSync;
mTimeSyncFeatureEnabled = aEnableRcpTimeSync;

mSpinelDriver = aSpinelDriver;
mSpinelDriver->SetFrameHandler(&HandleReceivedFrame, &HandleSavedFrame, this);
Expand Down Expand Up @@ -798,7 +799,7 @@ void RadioSpinel::Process(const void *aContext)
ProcessRadioStateMachine();
RecoverFromRcpFailure();

if (mEnableRcpTimeSync)
if (mTimeSyncFeatureEnabled)
{
CalcRcpTimeOffset();
}
Expand Down Expand Up @@ -1919,6 +1920,7 @@ void RadioSpinel::CalcRcpTimeOffset(void)
* D = T1' - ((T0 + T2)/ 2)
*/

EXPECT(mTimeSyncEnabled, NO_ACTION);
EXPECT(!mIsTimeSynced || (otPlatTimeGet() >= GetNextRadioTimeRecalcStart()), NO_ACTION);

LogDebg("Trying to get RCP time offset");
Expand Down Expand Up @@ -2231,7 +2233,7 @@ void RadioSpinel::RestoreProperties(void)
}
#endif

if (mEnableRcpTimeSync)
if (mTimeSyncFeatureEnabled)
{
CalcRcpTimeOffset();
}
Expand Down
11 changes: 10 additions & 1 deletion src/lib/spinel/radio_spinel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,14 @@ class RadioSpinel : private Logger
void SetVendorRestorePropertiesCallback(otRadioSpinelVendorRestorePropertiesCallback aCallback, void *aContext);
#endif // OPENTHREAD_SPINEL_CONFIG_VENDOR_HOOK_ENABLE

/**
* Enables or disables the time synchronization between the host and RCP.
*
* @param[in] aEnabled TRUE to enable time synchronization, FALSE otherwise.
*
*/
void SetTimeSyncEnabled(bool aEnabled) { mTimeSyncEnabled = aEnabled; }

private:
enum
{
Expand Down Expand Up @@ -1342,7 +1350,8 @@ class RadioSpinel : private Logger
void *mVendorRestorePropertiesContext;
#endif

bool mEnableRcpTimeSync;
bool mTimeSyncFeatureEnabled : 1;
bool mTimeSyncEnabled : 1;

SpinelDriver *mSpinelDriver;
};
Expand Down
7 changes: 7 additions & 0 deletions src/posix/platform/openthread-core-posix-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,11 @@
#define OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE 1
#endif

#ifndef OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS
/**
* The `system.cpp` has registered a state-changed callback handler. Another state-changed callback handler
* is reserved for application use.
*/
#define OPENTHREAD_CONFIG_MAX_STATECHANGE_HANDLERS 2
#endif
#endif // OPENTHREAD_CORE_POSIX_CONFIG_H_
9 changes: 9 additions & 0 deletions src/posix/platform/platform-posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ void platformRadioInit(const char *aUrl);
*/
void platformRadioDeinit(void);

/**
* Handles the state change events for the radio driver.
*
* @param[in] aInstance A pointer to the OpenThread instance.
* @param[in] aFlags Flags that denote the state change events.
*
*/
void platformRadioHandleStateChange(otInstance *aInstance, otChangedFlags aFlags);

/**
* Inputs a received radio frame.
*
Expand Down
8 changes: 8 additions & 0 deletions src/posix/platform/radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ ot::Posix::RcpCapsDiag &GetRcpCapsDiag(void) { return sRadio.GetRcpCapsDiag(); }

void platformRadioDeinit(void) { GetRadioSpinel().Deinit(); }

void platformRadioHandleStateChange(otInstance *aInstance, otChangedFlags aFlags)
{
if (OT_CHANGED_THREAD_NETIF_STATE & aFlags)
{
GetRadioSpinel().SetTimeSyncEnabled(otIp6IsEnabled(aInstance));
}
}

void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeeeEui64)
{
OT_UNUSED_VARIABLE(aInstance);
Expand Down
6 changes: 2 additions & 4 deletions src/posix/platform/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ bool gDryRun = false;

CoprocessorType sCoprocessorType = OT_COPROCESSOR_UNKNOWN;

#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE || OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
static void processStateChange(otChangedFlags aFlags, void *aContext)
{
otInstance *instance = static_cast<otInstance *>(aContext);
Expand All @@ -80,8 +79,9 @@ static void processStateChange(otChangedFlags aFlags, void *aContext)
#if OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
ot::Posix::InfraNetif::Get().HandleBackboneStateChange(instance, aFlags);
#endif

platformRadioHandleStateChange(instance, aFlags);
}
#endif

static const char *get802154RadioUrl(const otPlatformCoprocessorUrls &aUrls)
{
Expand Down Expand Up @@ -245,9 +245,7 @@ void platformSetUp(otPlatformConfig *aPlatformConfig)
ot::Posix::Daemon::Get().SetUp();
#endif

#if OPENTHREAD_CONFIG_PLATFORM_NETIF_ENABLE || OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
SuccessOrDie(otSetStateChangedCallback(gInstance, processStateChange, gInstance));
#endif

exit:
return;
Expand Down

0 comments on commit f5248b9

Please sign in to comment.