Skip to content

Commit

Permalink
Added Thread autostart configuration and check for Thread interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DejinChen committed Jun 14, 2024
1 parent a07b447 commit 4bda663
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/include/platform/CHIPDeviceConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,15 @@ static_assert(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN <= CHIP_DEVICE
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT 0
#endif

/**
* CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
*
* Enable starting provisioned Thread network automatically after device power-up.
*/
#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART 1
#endif

// -------------------- Network Telemetry Configuration --------------------

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ CHIP_ERROR GenericThreadDriver::Init(Internal::BaseDriver::NetworkStatusChangeCa
// must be restored on the boot. If there's no backup, the below function is a no-op.
RevertConfiguration();

CheckInterfaceEnabled();

return CHIP_NO_ERROR;
}

Expand Down Expand Up @@ -204,10 +206,9 @@ CHIP_ERROR GenericThreadDriver::SetEnabled(bool enabled)

ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kInterfaceEnabled, &enabled, sizeof(enabled)));

if ((!enabled && DeviceLayer::ThreadStackMgrImpl().IsThreadEnabled()) ||
(enabled && DeviceLayer::ThreadStackMgrImpl().IsThreadProvisioned()))
if ((!enabled && ThreadStackMgrImpl().IsThreadEnabled()) || (enabled && ThreadStackMgrImpl().IsThreadProvisioned()))
{
ReturnErrorOnFailure(DeviceLayer::ThreadStackMgrImpl().SetThreadEnabled(enabled));
ReturnErrorOnFailure(ThreadStackMgrImpl().SetThreadEnabled(enabled));
}
return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -260,6 +261,20 @@ CHIP_ERROR GenericThreadDriver::BackupConfiguration()
return KeyValueStoreMgr().Put(DefaultStorageKeyAllocator::FailSafeNetworkConfig().KeyName(), dataset.data(), dataset.size());
}

void GenericThreadDriver::CheckInterfaceEnabled()
{
bool enabled = GetEnabled();
ChipLogProgress(DeviceLayer, "OpenThread InterfaceEnabled: %s", enabled ? "true" : "false");
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
// If the Thread interface is enabled and stack has been provisioned, but is not currently enabled, enable it now.
if (enabled && ThreadStackMgrImpl().IsThreadProvisioned() && !ThreadStackMgrImpl().IsThreadEnabled())
{
ReturnOnFailure(ThreadStackMgrImpl().SetThreadEnabled(true));
ChipLogProgress(DeviceLayer, "OpenThread ifconfig up and thread start");
}
#endif
}

size_t GenericThreadDriver::ThreadNetworkIterator::Count()
{
return driver->mStagingNetwork.IsCommissioned() ? 1 : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class GenericThreadDriver final : public ThreadDriver
static void OnThreadStateChangeHandler(const ChipDeviceEvent * event, intptr_t arg);
Status MatchesNetworkId(const Thread::OperationalDataset & dataset, const ByteSpan & networkId) const;
CHIP_ERROR BackupConfiguration();
void CheckInterfaceEnabled();

ThreadNetworkIterator mThreadIterator = ThreadNetworkIterator(this);
Thread::OperationalDataset mStagingNetwork = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1094,9 +1094,6 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstanc
CHIP_ERROR err = CHIP_NO_ERROR;
otError otErr = OT_ERROR_NONE;

// If InterfaceEnabled is false, do not start Thread
bool InterfaceEnabled = true;

// Arrange for OpenThread errors to be translated to text.
RegisterOpenThreadErrorFormatter();

Expand Down Expand Up @@ -1134,12 +1131,9 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstanc
memset(&mSrpClient, 0, sizeof(mSrpClient));
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT

#ifndef _NO_NETWORK_COMMISSIONING_DRIVER_
InterfaceEnabled = sGenericThreadDriver.GetEnabled();
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
// If the Thread stack has been provisioned, but is not currently enabled, enable it now.
if (InterfaceEnabled && otThreadGetDeviceRole(mOTInst) == OT_DEVICE_ROLE_DISABLED && otDatasetIsCommissioned(otInst))
if (otThreadGetDeviceRole(mOTInst) == OT_DEVICE_ROLE_DISABLED && otDatasetIsCommissioned(otInst))
{
// Enable the Thread IPv6 interface.
otErr = otIp6SetEnabled(otInst, true);
Expand All @@ -1150,6 +1144,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstanc

ChipLogProgress(DeviceLayer, "OpenThread ifconfig up and thread start");
}
#endif

initNetworkCommissioningThreadDriver();

Expand Down

0 comments on commit 4bda663

Please sign in to comment.