Skip to content

Commit 720c834

Browse files
authored
OpenThread: added Thread interface enabled support (#33860)
* OpenThread: added Thread interface enabled support * Added Thread autostart configuration and check for Thread interface * Update comments for RevertConfiguration * Fix CI * Fix CI
1 parent b274320 commit 720c834

4 files changed

+69
-0
lines changed

src/include/platform/CHIPDeviceConfig.h

+9
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,15 @@ static_assert(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN <= CHIP_DEVICE
11011101
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT 0
11021102
#endif
11031103

1104+
/**
1105+
* CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
1106+
*
1107+
* Enable starting provisioned Thread network automatically after device power-up.
1108+
*/
1109+
#ifndef CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
1110+
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART 1
1111+
#endif
1112+
11041113
// -------------------- Network Telemetry Configuration --------------------
11051114

11061115
/**

src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.cpp

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

57+
CheckInterfaceEnabled();
58+
5759
return CHIP_NO_ERROR;
5860
}
5961

@@ -95,6 +97,16 @@ CHIP_ERROR GenericThreadDriver::RevertConfiguration()
9597
// since the fail-safe was armed, so return with no error.
9698
ReturnErrorCodeIf(error == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND, CHIP_NO_ERROR);
9799

100+
if (!GetEnabled())
101+
{
102+
// When reverting configuration, set InterfaceEnabled to default value (true).
103+
// From the spec:
104+
// If InterfaceEnabled is written to false on the same interface as that which is used to write the value, the Administrator
105+
// could await the recovery of network configuration to prior safe values, before being able to communicate with the
106+
// node again.
107+
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Delete(kInterfaceEnabled));
108+
}
109+
98110
ChipLogProgress(NetworkProvisioning, "Reverting Thread operational dataset");
99111

100112
if (error == CHIP_NO_ERROR)
@@ -166,6 +178,12 @@ void GenericThreadDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * c
166178
{
167179
NetworkCommissioning::Status status = MatchesNetworkId(mStagingNetwork, networkId);
168180

181+
if (!GetEnabled())
182+
{
183+
// Set InterfaceEnabled to default value (true).
184+
ReturnOnFailure(PersistedStorage::KeyValueStoreMgr().Delete(kInterfaceEnabled));
185+
}
186+
169187
if (status == Status::kSuccess && BackupConfiguration() != CHIP_NO_ERROR)
170188
{
171189
status = Status::kUnknownError;
@@ -183,6 +201,30 @@ void GenericThreadDriver::ConnectNetwork(ByteSpan networkId, ConnectCallback * c
183201
}
184202
}
185203

204+
CHIP_ERROR GenericThreadDriver::SetEnabled(bool enabled)
205+
{
206+
if (enabled == GetEnabled())
207+
{
208+
return CHIP_NO_ERROR;
209+
}
210+
211+
ReturnErrorOnFailure(PersistedStorage::KeyValueStoreMgr().Put(kInterfaceEnabled, &enabled, sizeof(enabled)));
212+
213+
if ((!enabled && ThreadStackMgrImpl().IsThreadEnabled()) || (enabled && ThreadStackMgrImpl().IsThreadProvisioned()))
214+
{
215+
ReturnErrorOnFailure(ThreadStackMgrImpl().SetThreadEnabled(enabled));
216+
}
217+
return CHIP_NO_ERROR;
218+
}
219+
220+
bool GenericThreadDriver::GetEnabled()
221+
{
222+
bool value;
223+
// InterfaceEnabled default value is true.
224+
VerifyOrReturnValue(PersistedStorage::KeyValueStoreMgr().Get(kInterfaceEnabled, &value, sizeof(value)) == CHIP_NO_ERROR, true);
225+
return value;
226+
}
227+
186228
void GenericThreadDriver::ScanNetworks(ThreadDriver::ScanCallback * callback)
187229
{
188230
if (DeviceLayer::ThreadStackMgrImpl().StartThreadScan(callback) != CHIP_NO_ERROR)
@@ -223,6 +265,18 @@ CHIP_ERROR GenericThreadDriver::BackupConfiguration()
223265
return KeyValueStoreMgr().Put(DefaultStorageKeyAllocator::FailSafeNetworkConfig().KeyName(), dataset.data(), dataset.size());
224266
}
225267

268+
void GenericThreadDriver::CheckInterfaceEnabled()
269+
{
270+
#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
271+
// If the Thread interface is enabled and stack has been provisioned, but is not currently enabled, enable it now.
272+
if (GetEnabled() && ThreadStackMgrImpl().IsThreadProvisioned() && !ThreadStackMgrImpl().IsThreadEnabled())
273+
{
274+
ReturnOnFailure(ThreadStackMgrImpl().SetThreadEnabled(true));
275+
ChipLogProgress(DeviceLayer, "OpenThread ifconfig up and thread start");
276+
}
277+
#endif
278+
}
279+
226280
size_t GenericThreadDriver::ThreadNetworkIterator::Count()
227281
{
228282
return driver->mStagingNetwork.IsCommissioned() ? 1 : 0;

src/platform/OpenThread/GenericNetworkCommissioningThreadDriver.h

+4
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ class GenericThreadDriver final : public ThreadDriver
9090
// BaseDriver
9191
NetworkIterator * GetNetworks() override { return new ThreadNetworkIterator(this); }
9292
CHIP_ERROR Init(Internal::BaseDriver::NetworkStatusChangeCallback * statusChangeCallback) override;
93+
CHIP_ERROR SetEnabled(bool enabled) override;
94+
bool GetEnabled() override;
9395
void Shutdown() override;
9496

9597
// WirelessDriver
@@ -114,11 +116,13 @@ class GenericThreadDriver final : public ThreadDriver
114116
void ScanNetworks(ThreadDriver::ScanCallback * callback) override;
115117

116118
private:
119+
static constexpr const char * kInterfaceEnabled = "g/gtd/en";
117120
uint8_t scanNetworkTimeoutSeconds;
118121
uint8_t connectNetworkTimeout;
119122
static void OnThreadStateChangeHandler(const ChipDeviceEvent * event, intptr_t arg);
120123
Status MatchesNetworkId(const Thread::OperationalDataset & dataset, const ByteSpan & networkId) const;
121124
CHIP_ERROR BackupConfiguration();
125+
void CheckInterfaceEnabled();
122126

123127
ThreadNetworkIterator mThreadIterator = ThreadNetworkIterator(this);
124128
Thread::OperationalDataset mStagingNetwork = {};

src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstanc
11311131
memset(&mSrpClient, 0, sizeof(mSrpClient));
11321132
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
11331133

1134+
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD_AUTOSTART
11341135
// If the Thread stack has been provisioned, but is not currently enabled, enable it now.
11351136
if (otThreadGetDeviceRole(mOTInst) == OT_DEVICE_ROLE_DISABLED && otDatasetIsCommissioned(otInst))
11361137
{
@@ -1143,6 +1144,7 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::DoInit(otInstanc
11431144

11441145
ChipLogProgress(DeviceLayer, "OpenThread ifconfig up and thread start");
11451146
}
1147+
#endif
11461148

11471149
initNetworkCommissioningThreadDriver();
11481150

0 commit comments

Comments
 (0)