15
15
* limitations under the License.
16
16
*/
17
17
18
- #include " ESP32ThreadBorderRouterDelegate .h"
18
+ #include " GenericThreadBorderRouterDelegate .h"
19
19
20
- #include < esp_openthread.h>
21
20
#include < openthread/border_agent.h>
22
21
#include < openthread/dataset.h>
23
22
#include < openthread/error.h>
@@ -53,23 +52,25 @@ class ScopedThreadLock
53
52
~ScopedThreadLock () { DeviceLayer::ThreadStackMgr ().UnlockThreadStack (); }
54
53
};
55
54
56
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::Init ()
55
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::Init ()
57
56
{
58
57
mCallback = nullptr ;
59
58
ReturnErrorOnFailure (DeviceLayer::PlatformMgrImpl ().AddEventHandler (OnPlatformEventHandler, reinterpret_cast <intptr_t >(this )));
60
59
ReturnErrorOnFailure (RevertActiveDataset ());
61
60
return CHIP_NO_ERROR;
62
61
}
63
62
64
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::GetBorderAgentId (MutableByteSpan & borderAgentIdSpan)
63
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::GetBorderAgentId (MutableByteSpan & borderAgentIdSpan)
65
64
{
65
+ otInstance *otInst = DeviceLayer::ThreadStackMgrImpl ().OTInstance ();
66
+ VerifyOrReturnError (otInst, CHIP_ERROR_INCORRECT_STATE);
66
67
otBorderAgentId borderAgentId;
67
68
if (borderAgentIdSpan.size () < sizeof (borderAgentId.mId ))
68
69
{
69
70
return CHIP_ERROR_INVALID_ARGUMENT;
70
71
}
71
72
ScopedThreadLock threadLock;
72
- otError err = otBorderAgentGetId (esp_openthread_get_instance () , &borderAgentId);
73
+ otError err = otBorderAgentGetId (otInst , &borderAgentId);
73
74
if (err == OT_ERROR_NONE)
74
75
{
75
76
memcpy (borderAgentIdSpan.data (), borderAgentId.mId , sizeof (borderAgentId.mId ));
@@ -79,31 +80,36 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetBorderAgentId(MutableByteSpan & b
79
80
return CHIP_ERROR_INTERNAL;
80
81
}
81
82
82
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::GetThreadVersion (uint16_t & threadVersion)
83
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::GetThreadVersion (uint16_t & threadVersion)
83
84
{
84
85
threadVersion = otThreadGetVersion ();
85
86
return CHIP_NO_ERROR;
86
87
}
87
88
88
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::GetInterfaceEnabled (bool & interfaceEnabled)
89
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::GetInterfaceEnabled (bool & interfaceEnabled)
89
90
{
91
+ otInstance *otInst = DeviceLayer::ThreadStackMgrImpl ().OTInstance ();
92
+ VerifyOrReturnError (otInst, CHIP_ERROR_INCORRECT_STATE);
90
93
ScopedThreadLock threadLock;
91
- interfaceEnabled = otIp6IsEnabled (esp_openthread_get_instance () );
94
+ interfaceEnabled = otIp6IsEnabled (otInst );
92
95
return CHIP_NO_ERROR;
93
96
}
94
97
95
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::GetDataset (Thread::OperationalDataset & dataset, DatasetType type)
98
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::GetDataset (Thread::OperationalDataset & dataset, DatasetType type)
96
99
{
100
+ otInstance *otInst = DeviceLayer::ThreadStackMgrImpl ().OTInstance ();
101
+ VerifyOrReturnError (otInst, CHIP_ERROR_INCORRECT_STATE);
102
+
97
103
ScopedThreadLock threadLock;
98
104
otError otErr = OT_ERROR_NONE;
99
105
otOperationalDatasetTlvs datasetTlvs;
100
106
if (type == DatasetType::kActive )
101
107
{
102
- otErr = otDatasetGetActiveTlvs (esp_openthread_get_instance () , &datasetTlvs);
108
+ otErr = otDatasetGetActiveTlvs (otInst , &datasetTlvs);
103
109
}
104
110
else
105
111
{
106
- otErr = otDatasetGetPendingTlvs (esp_openthread_get_instance () , &datasetTlvs);
112
+ otErr = otDatasetGetPendingTlvs (otInst , &datasetTlvs);
107
113
}
108
114
if (otErr == OT_ERROR_NONE)
109
115
{
@@ -112,9 +118,12 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::GetDataset(Thread::OperationalDatase
112
118
return CHIP_ERROR_NOT_FOUND;
113
119
}
114
120
115
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::SetActiveDataset (const Thread::OperationalDataset & activeDataset,
121
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::SetActiveDataset (const Thread::OperationalDataset & activeDataset,
116
122
ActivateDatasetCallback * callback)
117
123
{
124
+ otInstance *otInst = DeviceLayer::ThreadStackMgrImpl ().OTInstance ();
125
+ VerifyOrReturnError (otInst, CHIP_ERROR_INCORRECT_STATE);
126
+
118
127
VerifyOrReturnError (callback, CHIP_ERROR_INVALID_ARGUMENT);
119
128
otOperationalDatasetTlvs datasetTlvs;
120
129
memcpy (datasetTlvs.mTlvs , activeDataset.AsByteSpan ().data (), activeDataset.AsByteSpan ().size ());
@@ -127,7 +136,7 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::SetActiveDataset(const Thread::Opera
127
136
if (threadIsEnabled)
128
137
{
129
138
otOperationalDatasetTlvs stagingDataset;
130
- ReturnErrorCodeIf (otDatasetGetActiveTlvs (esp_openthread_get_instance () , &stagingDataset) != OT_ERROR_NONE,
139
+ ReturnErrorCodeIf (otDatasetGetActiveTlvs (otInst , &stagingDataset) != OT_ERROR_NONE,
131
140
CHIP_ERROR_INTERNAL);
132
141
if (activeDataset.AsByteSpan ().data_equal (ByteSpan (stagingDataset.mTlvs , stagingDataset.mLength )))
133
142
{
@@ -138,15 +147,15 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::SetActiveDataset(const Thread::Opera
138
147
stagingDataset.mTlvs , stagingDataset.mLength ));
139
148
}
140
149
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);
142
151
SetThreadEnabled (true );
143
152
mCallback = callback;
144
153
return CHIP_NO_ERROR;
145
154
}
146
155
147
- void ESP32ThreadBorderRouterDelegate ::OnPlatformEventHandler (const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
156
+ void GenericThreadBorderRouterDelegate ::OnPlatformEventHandler (const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
148
157
{
149
- ESP32ThreadBorderRouterDelegate * delegate = reinterpret_cast <ESP32ThreadBorderRouterDelegate *>(arg);
158
+ GenericThreadBorderRouterDelegate * delegate = reinterpret_cast <GenericThreadBorderRouterDelegate *>(arg);
150
159
if (delegate && delegate->mCallback )
151
160
{
152
161
if (event->Type == DeviceLayer::DeviceEventType::kThreadConnectivityChange &&
@@ -161,8 +170,11 @@ void ESP32ThreadBorderRouterDelegate::OnPlatformEventHandler(const DeviceLayer::
161
170
}
162
171
}
163
172
164
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::RevertActiveDataset ()
173
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::RevertActiveDataset ()
165
174
{
175
+ otInstance *otInst = DeviceLayer::ThreadStackMgrImpl ().OTInstance ();
176
+ VerifyOrReturnError (otInst, CHIP_ERROR_INCORRECT_STATE);
177
+
166
178
bool threadIsEnabled = false ;
167
179
CHIP_ERROR err = DeviceLayer::PersistedStorage::KeyValueStoreMgr ().Get (kFailsafeThreadEnabledKey , &threadIsEnabled);
168
180
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
@@ -185,7 +197,7 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::RevertActiveDataset()
185
197
ReturnErrorOnFailure (err);
186
198
stagingDataset.mLength = datasetTlvslen;
187
199
ReturnErrorOnFailure (SetThreadEnabled (false ));
188
- ReturnErrorCodeIf (otDatasetSetActiveTlvs (esp_openthread_get_instance () , &stagingDataset) != OT_ERROR_NONE,
200
+ ReturnErrorCodeIf (otDatasetSetActiveTlvs (otInst , &stagingDataset) != OT_ERROR_NONE,
189
201
CHIP_ERROR_INTERNAL);
190
202
}
191
203
ReturnErrorOnFailure (SetThreadEnabled (threadIsEnabled));
@@ -195,40 +207,44 @@ CHIP_ERROR ESP32ThreadBorderRouterDelegate::RevertActiveDataset()
195
207
return CHIP_NO_ERROR;
196
208
}
197
209
198
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::SetPendingDataset (const Thread::OperationalDataset & pendingDataset)
210
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::SetPendingDataset (const Thread::OperationalDataset & pendingDataset)
199
211
{
212
+ otInstance *otInst = DeviceLayer::ThreadStackMgrImpl ().OTInstance ();
213
+ VerifyOrReturnError (otInst, CHIP_ERROR_INCORRECT_STATE);
214
+
200
215
ScopedThreadLock threadLock;
201
216
otOperationalDatasetTlvs datasetTlvs;
202
217
memcpy (datasetTlvs.mTlvs , pendingDataset.AsByteSpan ().data (), pendingDataset.AsByteSpan ().size ());
203
218
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);
205
220
return CHIP_NO_ERROR;
206
221
}
207
222
208
- CHIP_ERROR ESP32ThreadBorderRouterDelegate ::SetThreadEnabled (bool enabled)
223
+ CHIP_ERROR GenericThreadBorderRouterDelegate ::SetThreadEnabled (bool enabled)
209
224
{
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);
213
229
if (enabled && !isIp6Enabled)
214
230
{
215
- ReturnErrorCodeIf (otIp6SetEnabled (instance , enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
231
+ ReturnErrorCodeIf (otIp6SetEnabled (otInst , enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
216
232
}
217
233
if (enabled != isEnabled)
218
234
{
219
- ReturnErrorCodeIf (otThreadSetEnabled (instance , enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
235
+ ReturnErrorCodeIf (otThreadSetEnabled (otInst , enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
220
236
}
221
237
if (!enabled && isIp6Enabled)
222
238
{
223
- ReturnErrorCodeIf (otIp6SetEnabled (instance , enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
239
+ ReturnErrorCodeIf (otIp6SetEnabled (otInst , enabled) != OT_ERROR_NONE, CHIP_ERROR_INTERNAL);
224
240
}
225
241
return CHIP_NO_ERROR;
226
242
}
227
243
228
- bool ESP32ThreadBorderRouterDelegate ::GetThreadEnabled ()
244
+ bool GenericThreadBorderRouterDelegate ::GetThreadEnabled ()
229
245
{
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);
232
248
}
233
249
234
250
} // namespace ThreadBorderRouterManagement
0 commit comments