Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4d0fe45

Browse files
committedJun 18, 2024·
move the border router delegate to generic directory
1 parent e567348 commit 4d0fe45

File tree

4 files changed

+54
-41
lines changed

4 files changed

+54
-41
lines changed
 

‎config/esp32/components/chip/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ else()
231231
endif()
232232

233233
if (CONFIG_OPENTHREAD_BORDER_ROUTER)
234-
chip_gn_arg_append("chip_enable_thread_border_router" "true")
234+
chip_gn_arg_append("chip_openthread_border_router" "true")
235235
else()
236-
chip_gn_arg_append("chip_enable_thread_border_router" "false")
236+
chip_gn_arg_append("chip_openthread_border_router" "false")
237237
endif()
238238

239239
if (CONFIG_ENABLE_OTA_REQUESTOR)

‎src/platform/ESP32/BUILD.gn

+3-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ declare_args() {
3232
chip_use_esp32_ecdsa_peripheral = false
3333
chip_enable_ethernet = false
3434
chip_enable_route_hook = false
35-
chip_enable_thread_border_router = false
3635
}
3736

3837
defines = [
@@ -172,10 +171,10 @@ static_library("ESP32") {
172171
"../OpenThread/OpenThreadDnssdImpl.h",
173172
]
174173
}
175-
if (chip_enable_thread_border_router) {
174+
if (chip_openthread_border_router) {
176175
sources += [
177-
"ESP32ThreadBorderRouterDelegate.cpp",
178-
"ESP32ThreadBorderRouterDelegate.h",
176+
"../OpenThread/GenericThreadBorderRouterDelegate.cpp",
177+
"../OpenThread/GenericThreadBorderRouterDelegate.h",
179178
]
180179
}
181180
configs -= [ "${chip_root}/build/config/compiler:warnings_default" ]

‎src/platform/ESP32/ESP32ThreadBorderRouterDelegate.cpp ‎src/platform/OpenThread/GenericThreadBorderRouterDelegate.cpp

+46-30
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
#include "ESP32ThreadBorderRouterDelegate.h"
18+
#include "GenericThreadBorderRouterDelegate.h"
1919

20-
#include <esp_openthread.h>
2120
#include <openthread/border_agent.h>
2221
#include <openthread/dataset.h>
2322
#include <openthread/error.h>
@@ -53,23 +52,25 @@ class ScopedThreadLock
5352
~ScopedThreadLock() { DeviceLayer::ThreadStackMgr().UnlockThreadStack(); }
5453
};
5554

56-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::Init()
55+
CHIP_ERROR GenericThreadBorderRouterDelegate::Init()
5756
{
5857
mCallback = nullptr;
5958
ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().AddEventHandler(OnPlatformEventHandler, reinterpret_cast<intptr_t>(this)));
6059
ReturnErrorOnFailure(RevertActiveDataset());
6160
return CHIP_NO_ERROR;
6261
}
6362

64-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetBorderAgentId(MutableByteSpan & borderAgentIdSpan)
63+
CHIP_ERROR GenericThreadBorderRouterDelegate::GetBorderAgentId(MutableByteSpan & borderAgentIdSpan)
6564
{
65+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
66+
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
6667
otBorderAgentId borderAgentId;
6768
if (borderAgentIdSpan.size() < sizeof(borderAgentId.mId))
6869
{
6970
return CHIP_ERROR_INVALID_ARGUMENT;
7071
}
7172
ScopedThreadLock threadLock;
72-
otError err = otBorderAgentGetId(esp_openthread_get_instance(), &borderAgentId);
73+
otError err = otBorderAgentGetId(otInst, &borderAgentId);
7374
if (err == OT_ERROR_NONE)
7475
{
7576
memcpy(borderAgentIdSpan.data(), borderAgentId.mId, sizeof(borderAgentId.mId));
@@ -79,31 +80,36 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetBorderAgentId(MutableByteSpan & b
7980
return CHIP_ERROR_INTERNAL;
8081
}
8182

82-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetThreadVersion(uint16_t & threadVersion)
83+
CHIP_ERROR GenericThreadBorderRouterDelegate::GetThreadVersion(uint16_t & threadVersion)
8384
{
8485
threadVersion = otThreadGetVersion();
8586
return CHIP_NO_ERROR;
8687
}
8788

88-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetInterfaceEnabled(bool & interfaceEnabled)
89+
CHIP_ERROR GenericThreadBorderRouterDelegate::GetInterfaceEnabled(bool & interfaceEnabled)
8990
{
91+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
92+
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
9093
ScopedThreadLock threadLock;
91-
interfaceEnabled = otIp6IsEnabled(esp_openthread_get_instance());
94+
interfaceEnabled = otIp6IsEnabled(otInst);
9295
return CHIP_NO_ERROR;
9396
}
9497

95-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetDataset(Thread::OperationalDataset & dataset, DatasetType type)
98+
CHIP_ERROR GenericThreadBorderRouterDelegate::GetDataset(Thread::OperationalDataset & dataset, DatasetType type)
9699
{
100+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
101+
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
102+
97103
ScopedThreadLock threadLock;
98104
otError otErr = OT_ERROR_NONE;
99105
otOperationalDatasetTlvs datasetTlvs;
100106
if (type == DatasetType::kActive)
101107
{
102-
otErr = otDatasetGetActiveTlvs(esp_openthread_get_instance(), &datasetTlvs);
108+
otErr = otDatasetGetActiveTlvs(otInst, &datasetTlvs);
103109
}
104110
else
105111
{
106-
otErr = otDatasetGetPendingTlvs(esp_openthread_get_instance(), &datasetTlvs);
112+
otErr = otDatasetGetPendingTlvs(otInst, &datasetTlvs);
107113
}
108114
if (otErr == OT_ERROR_NONE)
109115
{
@@ -112,9 +118,12 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetDataset(Thread::OperationalDatase
112118
return CHIP_ERROR_NOT_FOUND;
113119
}
114120

115-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::SetActiveDataset(const Thread::OperationalDataset & activeDataset,
121+
CHIP_ERROR GenericThreadBorderRouterDelegate::SetActiveDataset(const Thread::OperationalDataset & activeDataset,
116122
ActivateDatasetCallback * callback)
117123
{
124+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
125+
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
126+
118127
VerifyOrReturnError(callback, CHIP_ERROR_INVALID_ARGUMENT);
119128
otOperationalDatasetTlvs datasetTlvs;
120129
memcpy(datasetTlvs.mTlvs, activeDataset.AsByteSpan().data(), activeDataset.AsByteSpan().size());
@@ -127,7 +136,7 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::SetActiveDataset(const Thread::Opera
127136
if (threadIsEnabled)
128137
{
129138
otOperationalDatasetTlvs stagingDataset;
130-
ReturnErrorCodeIf(otDatasetGetActiveTlvs(esp_openthread_get_instance(), &stagingDataset) != OT_ERROR_NONE,
139+
ReturnErrorCodeIf(otDatasetGetActiveTlvs(otInst, &stagingDataset) != OT_ERROR_NONE,
131140
CHIP_ERROR_INTERNAL);
132141
if (activeDataset.AsByteSpan().data_equal(ByteSpan(stagingDataset.mTlvs, stagingDataset.mLength)))
133142
{
@@ -138,15 +147,15 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::SetActiveDataset(const Thread::Opera
138147
stagingDataset.mTlvs, stagingDataset.mLength));
139148
}
140149
SetThreadEnabled(false);
141-
ReturnErrorCodeIf(otDatasetSetActiveTlvs(esp_openthread_get_instance(), &datasetTlvs) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
150+
ReturnErrorCodeIf(otDatasetSetActiveTlvs(otInst, &datasetTlvs) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
142151
SetThreadEnabled(true);
143152
mCallback = callback;
144153
return CHIP_NO_ERROR;
145154
}
146155

147-
void ESP32ThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
156+
void GenericThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
148157
{
149-
ESP32ThreadBorderRouterDelegate * delegate = reinterpret_cast<ESP32ThreadBorderRouterDelegate *>(arg);
158+
GenericThreadBorderRouterDelegate * delegate = reinterpret_cast<GenericThreadBorderRouterDelegate *>(arg);
150159
if (delegate && delegate->mCallback)
151160
{
152161
if (event->Type == DeviceLayer::DeviceEventType::kThreadConnectivityChange &&
@@ -161,8 +170,11 @@ void ESP32ThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceLayer::
161170
}
162171
}
163172

164-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::RevertActiveDataset()
173+
CHIP_ERROR GenericThreadBorderRouterDelegate::RevertActiveDataset()
165174
{
175+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
176+
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
177+
166178
bool threadIsEnabled = false;
167179
CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr().Get(kFailsafeThreadEnabledKey, &threadIsEnabled);
168180
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -185,7 +197,7 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::RevertActiveDataset()
185197
ReturnErrorOnFailure(err);
186198
stagingDataset.mLength = datasetTlvslen;
187199
ReturnErrorOnFailure(SetThreadEnabled(false));
188-
ReturnErrorCodeIf(otDatasetSetActiveTlvs(esp_openthread_get_instance(), &stagingDataset) != OT_ERROR_NONE,
200+
ReturnErrorCodeIf(otDatasetSetActiveTlvs(otInst, &stagingDataset) != OT_ERROR_NONE,
189201
CHIP_ERROR_INTERNAL);
190202
}
191203
ReturnErrorOnFailure(SetThreadEnabled(threadIsEnabled));
@@ -195,40 +207,44 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::RevertActiveDataset()
195207
return CHIP_NO_ERROR;
196208
}
197209

198-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::SetPendingDataset(const Thread::OperationalDataset & pendingDataset)
210+
CHIP_ERROR GenericThreadBorderRouterDelegate::SetPendingDataset(const Thread::OperationalDataset & pendingDataset)
199211
{
212+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
213+
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
214+
200215
ScopedThreadLock threadLock;
201216
otOperationalDatasetTlvs datasetTlvs;
202217
memcpy(datasetTlvs.mTlvs, pendingDataset.AsByteSpan().data(), pendingDataset.AsByteSpan().size());
203218
datasetTlvs.mLength = pendingDataset.AsByteSpan().size();
204-
ReturnErrorCodeIf(otDatasetSetPendingTlvs(esp_openthread_get_instance(), &datasetTlvs) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
219+
ReturnErrorCodeIf(otDatasetSetPendingTlvs(otInst, &datasetTlvs) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
205220
return CHIP_NO_ERROR;
206221
}
207222

208-
CHIP_ERROR ESP32ThreadBorderRouterDelegate::SetThreadEnabled(bool enabled)
223+
CHIP_ERROR GenericThreadBorderRouterDelegate::SetThreadEnabled(bool enabled)
209224
{
210-
otInstance * instance = esp_openthread_get_instance();
211-
bool isEnabled = (otThreadGetDeviceRole(instance) != OT_DEVICE_ROLE_DISABLED);
212-
bool isIp6Enabled = otIp6IsEnabled(instance);
225+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
226+
VerifyOrReturnError(otInst, CHIP_ERROR_INCORRECT_STATE);
227+
bool isEnabled = (otThreadGetDeviceRole(otInst) != OT_DEVICE_ROLE_DISABLED);
228+
bool isIp6Enabled = otIp6IsEnabled(otInst);
213229
if (enabled && !isIp6Enabled)
214230
{
215-
ReturnErrorCodeIf(otIp6SetEnabled(instance, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
231+
ReturnErrorCodeIf(otIp6SetEnabled(otInst, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
216232
}
217233
if (enabled != isEnabled)
218234
{
219-
ReturnErrorCodeIf(otThreadSetEnabled(instance, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
235+
ReturnErrorCodeIf(otThreadSetEnabled(otInst, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
220236
}
221237
if (!enabled && isIp6Enabled)
222238
{
223-
ReturnErrorCodeIf(otIp6SetEnabled(instance, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
239+
ReturnErrorCodeIf(otIp6SetEnabled(otInst, enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
224240
}
225241
return CHIP_NO_ERROR;
226242
}
227243

228-
bool ESP32ThreadBorderRouterDelegate::GetThreadEnabled()
244+
bool GenericThreadBorderRouterDelegate::GetThreadEnabled()
229245
{
230-
otInstance * instance = esp_openthread_get_instance();
231-
return otIp6IsEnabled(instance) && (otThreadGetDeviceRole(instance) != OT_DEVICE_ROLE_DISABLED);
246+
otInstance *otInst = DeviceLayer::ThreadStackMgrImpl().OTInstance();
247+
return otInst && otIp6IsEnabled(otInst) && (otThreadGetDeviceRole(otInst) != OT_DEVICE_ROLE_DISABLED);
232248
}
233249

234250
} // namespace ThreadBorderRouterManagement

‎src/platform/ESP32/ESP32ThreadBorderRouterDelegate.h ‎src/platform/OpenThread/GenericThreadBorderRouterDelegate.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,27 @@
1717

1818
#pragma once
1919

20-
#include "openthread/dataset.h"
2120
#include <app-common/zap-generated/cluster-enums.h>
2221
#include <app/clusters/thread-border-router-management-server/thread-br-delegate.h>
2322
#include <inet/IPAddress.h>
2423
#include <lib/core/CHIPError.h>
2524
#include <lib/support/Span.h>
26-
#include <openthread/netdiag.h>
2725

2826
namespace chip {
2927
namespace app {
3028
namespace Clusters {
3129

3230
namespace ThreadBorderRouterManagement {
3331

34-
class ESP32ThreadBorderRouterDelegate : public Delegate
32+
class GenericThreadBorderRouterDelegate : public Delegate
3533
{
3634
public:
3735
static constexpr char kThreadBorderRourterName[] = "Espressif-ThreadBR";
3836
static constexpr char kFailsafeThreadDatasetTlvsKey[] = "g/fs/td";
3937
static constexpr char kFailsafeThreadEnabledKey[] = "g/fs/te";
4038

41-
ESP32ThreadBorderRouterDelegate() = default;
42-
~ESP32ThreadBorderRouterDelegate() = default;
39+
GenericThreadBorderRouterDelegate() = default;
40+
~GenericThreadBorderRouterDelegate() = default;
4341

4442
CHIP_ERROR Init() override;
4543

0 commit comments

Comments
 (0)