-
Notifications
You must be signed in to change notification settings - Fork 237
/
rcp_host.cpp
433 lines (364 loc) · 12.3 KB
/
rcp_host.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* Copyright (c) 2019, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#define OTBR_LOG_TAG "RCP_HOST"
#include "ncp/rcp_host.hpp"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <openthread/backbone_router_ftd.h>
#include <openthread/border_routing.h>
#include <openthread/dataset.h>
#include <openthread/dnssd_server.h>
#include <openthread/link_metrics.h>
#include <openthread/logging.h>
#include <openthread/nat64.h>
#include <openthread/srp_server.h>
#include <openthread/tasklet.h>
#include <openthread/thread.h>
#include <openthread/thread_ftd.h>
#include <openthread/trel.h>
#include <openthread/platform/logging.h>
#include <openthread/platform/misc.h>
#include <openthread/platform/radio.h>
#include <openthread/platform/settings.h>
#include "common/code_utils.hpp"
#include "common/logging.hpp"
#include "common/types.hpp"
#if OTBR_ENABLE_FEATURE_FLAGS
#include "proto/feature_flag.pb.h"
#endif
namespace otbr {
namespace Ncp {
static const uint16_t kThreadVersion11 = 2; ///< Thread Version 1.1
static const uint16_t kThreadVersion12 = 3; ///< Thread Version 1.2
static const uint16_t kThreadVersion13 = 4; ///< Thread Version 1.3
static const uint16_t kThreadVersion14 = 5; ///< Thread Version 1.4
// =============================== OtNetworkProperties ===============================
OtNetworkProperties::OtNetworkProperties(void)
: mInstance(nullptr)
{
}
otDeviceRole OtNetworkProperties::GetDeviceRole(void) const
{
return otThreadGetDeviceRole(mInstance);
}
void OtNetworkProperties::SetInstance(otInstance *aInstance)
{
mInstance = aInstance;
}
// =============================== RcpHost ===============================
RcpHost::RcpHost(const char *aInterfaceName,
const std::vector<const char *> &aRadioUrls,
const char *aBackboneInterfaceName,
bool aDryRun,
bool aEnableAutoAttach)
: mInstance(nullptr)
, mEnableAutoAttach(aEnableAutoAttach)
{
VerifyOrDie(aRadioUrls.size() <= OT_PLATFORM_CONFIG_MAX_RADIO_URLS, "Too many Radio URLs!");
memset(&mConfig, 0, sizeof(mConfig));
mConfig.mInterfaceName = aInterfaceName;
mConfig.mBackboneInterfaceName = aBackboneInterfaceName;
mConfig.mDryRun = aDryRun;
for (const char *url : aRadioUrls)
{
mConfig.mCoprocessorUrls.mUrls[mConfig.mCoprocessorUrls.mNum++] = url;
}
mConfig.mSpeedUpFactor = 1;
}
RcpHost::~RcpHost(void)
{
// Make sure OpenThread Instance was gracefully de-initialized.
assert(mInstance == nullptr);
}
otbrLogLevel RcpHost::ConvertToOtbrLogLevel(otLogLevel aLogLevel)
{
otbrLogLevel otbrLogLevel;
switch (aLogLevel)
{
case OT_LOG_LEVEL_NONE:
otbrLogLevel = OTBR_LOG_EMERG;
break;
case OT_LOG_LEVEL_CRIT:
otbrLogLevel = OTBR_LOG_CRIT;
break;
case OT_LOG_LEVEL_WARN:
otbrLogLevel = OTBR_LOG_WARNING;
break;
case OT_LOG_LEVEL_NOTE:
otbrLogLevel = OTBR_LOG_NOTICE;
break;
case OT_LOG_LEVEL_INFO:
otbrLogLevel = OTBR_LOG_INFO;
break;
case OT_LOG_LEVEL_DEBG:
default:
otbrLogLevel = OTBR_LOG_DEBUG;
break;
}
return otbrLogLevel;
}
#if OTBR_ENABLE_FEATURE_FLAGS
/* Converts ProtoLogLevel to otbrLogLevel */
otbrLogLevel ConvertProtoToOtbrLogLevel(ProtoLogLevel aProtoLogLevel)
{
otbrLogLevel otbrLogLevel;
switch (aProtoLogLevel)
{
case PROTO_LOG_EMERG:
otbrLogLevel = OTBR_LOG_EMERG;
break;
case PROTO_LOG_ALERT:
otbrLogLevel = OTBR_LOG_ALERT;
break;
case PROTO_LOG_CRIT:
otbrLogLevel = OTBR_LOG_CRIT;
break;
case PROTO_LOG_ERR:
otbrLogLevel = OTBR_LOG_ERR;
break;
case PROTO_LOG_WARNING:
otbrLogLevel = OTBR_LOG_WARNING;
break;
case PROTO_LOG_NOTICE:
otbrLogLevel = OTBR_LOG_NOTICE;
break;
case PROTO_LOG_INFO:
otbrLogLevel = OTBR_LOG_INFO;
break;
case PROTO_LOG_DEBUG:
default:
otbrLogLevel = OTBR_LOG_DEBUG;
break;
}
return otbrLogLevel;
}
#endif
otError RcpHost::SetOtbrAndOtLogLevel(otbrLogLevel aLevel)
{
otError error = OT_ERROR_NONE;
otbrLogSetLevel(aLevel);
error = otLoggingSetLevel(ConvertToOtLogLevel(aLevel));
return error;
}
void RcpHost::Init(void)
{
otbrError error = OTBR_ERROR_NONE;
otLogLevel level = ConvertToOtLogLevel(otbrLogGetLevel());
#if OTBR_ENABLE_FEATURE_FLAGS && OTBR_ENABLE_TREL
FeatureFlagList featureFlagList;
#endif
VerifyOrExit(otLoggingSetLevel(level) == OT_ERROR_NONE, error = OTBR_ERROR_OPENTHREAD);
mInstance = otSysInit(&mConfig);
assert(mInstance != nullptr);
{
otError result = otSetStateChangedCallback(mInstance, &RcpHost::HandleStateChanged, this);
agent::ThreadHelper::LogOpenThreadResult("Set state callback", result);
VerifyOrExit(result == OT_ERROR_NONE, error = OTBR_ERROR_OPENTHREAD);
}
#if OTBR_ENABLE_FEATURE_FLAGS && OTBR_ENABLE_TREL
// Enable/Disable trel according to feature flag default value.
otTrelSetEnabled(mInstance, featureFlagList.enable_trel());
#endif
#if OTBR_ENABLE_SRP_ADVERTISING_PROXY
#if OTBR_ENABLE_SRP_SERVER_AUTO_ENABLE_MODE
// Let SRP server use auto-enable mode. The auto-enable mode delegates the control of SRP server to the Border
// Routing Manager. SRP server automatically starts when bi-directional connectivity is ready.
otSrpServerSetAutoEnableMode(mInstance, /* aEnabled */ true);
#else
otSrpServerSetEnabled(mInstance, /* aEnabled */ true);
#endif
#endif
#if !OTBR_ENABLE_FEATURE_FLAGS
// Bring up all features when feature flags is not supported.
#if OTBR_ENABLE_NAT64
otNat64SetEnabled(mInstance, /* aEnabled */ true);
#endif
#if OTBR_ENABLE_DNS_UPSTREAM_QUERY
otDnssdUpstreamQuerySetEnabled(mInstance, /* aEnabled */ true);
#endif
#if OTBR_ENABLE_DHCP6_PD
otBorderRoutingDhcp6PdSetEnabled(mInstance, /* aEnabled */ true);
#endif
#endif // OTBR_ENABLE_FEATURE_FLAGS
mThreadHelper = MakeUnique<otbr::agent::ThreadHelper>(mInstance, this);
OtNetworkProperties::SetInstance(mInstance);
exit:
SuccessOrDie(error, "Failed to initialize the RCP Host!");
}
#if OTBR_ENABLE_FEATURE_FLAGS
otError RcpHost::ApplyFeatureFlagList(const FeatureFlagList &aFeatureFlagList)
{
otError error = OT_ERROR_NONE;
// Save a cached copy of feature flags for debugging purpose.
mAppliedFeatureFlagListBytes = aFeatureFlagList.SerializeAsString();
#if OTBR_ENABLE_NAT64
otNat64SetEnabled(mInstance, aFeatureFlagList.enable_nat64());
#endif
if (aFeatureFlagList.enable_detailed_logging())
{
error = SetOtbrAndOtLogLevel(ConvertProtoToOtbrLogLevel(aFeatureFlagList.detailed_logging_level()));
}
else
{
error = SetOtbrAndOtLogLevel(otbrLogGetDefaultLevel());
}
#if OTBR_ENABLE_TREL
otTrelSetEnabled(mInstance, aFeatureFlagList.enable_trel());
#endif
#if OTBR_ENABLE_DNS_UPSTREAM_QUERY
otDnssdUpstreamQuerySetEnabled(mInstance, aFeatureFlagList.enable_dns_upstream_query());
#endif
#if OTBR_ENABLE_DHCP6_PD
otBorderRoutingDhcp6PdSetEnabled(mInstance, aFeatureFlagList.enable_dhcp6_pd());
#endif
#if OTBR_ENABLE_LINK_METRICS_TELEMETRY
otLinkMetricsManagerSetEnabled(mInstance, aFeatureFlagList.enable_link_metrics_manager());
#endif
return error;
}
#endif
void RcpHost::Deinit(void)
{
assert(mInstance != nullptr);
otSysDeinit();
mInstance = nullptr;
OtNetworkProperties::SetInstance(nullptr);
mThreadStateChangedCallbacks.clear();
mResetHandlers.clear();
}
void RcpHost::HandleStateChanged(otChangedFlags aFlags)
{
for (auto &stateCallback : mThreadStateChangedCallbacks)
{
stateCallback(aFlags);
}
mThreadHelper->StateChangedCallback(aFlags);
}
void RcpHost::Update(MainloopContext &aMainloop)
{
if (otTaskletsArePending(mInstance))
{
aMainloop.mTimeout = ToTimeval(Microseconds::zero());
}
otSysMainloopUpdate(mInstance, &aMainloop);
}
void RcpHost::Process(const MainloopContext &aMainloop)
{
otTaskletsProcess(mInstance);
otSysMainloopProcess(mInstance, &aMainloop);
if (IsAutoAttachEnabled() && mThreadHelper->TryResumeNetwork() == OT_ERROR_NONE)
{
DisableAutoAttach();
}
}
bool RcpHost::IsAutoAttachEnabled(void)
{
return mEnableAutoAttach;
}
void RcpHost::DisableAutoAttach(void)
{
mEnableAutoAttach = false;
}
void RcpHost::PostTimerTask(Milliseconds aDelay, TaskRunner::Task<void> aTask)
{
mTaskRunner.Post(std::move(aDelay), std::move(aTask));
}
void RcpHost::RegisterResetHandler(std::function<void(void)> aHandler)
{
mResetHandlers.emplace_back(std::move(aHandler));
}
void RcpHost::AddThreadStateChangedCallback(ThreadStateChangedCallback aCallback)
{
mThreadStateChangedCallbacks.emplace_back(std::move(aCallback));
}
void RcpHost::Reset(void)
{
gPlatResetReason = OT_PLAT_RESET_REASON_SOFTWARE;
otSysDeinit();
mInstance = nullptr;
Init();
for (auto &handler : mResetHandlers)
{
handler();
}
mEnableAutoAttach = true;
}
const char *RcpHost::GetThreadVersion(void)
{
const char *version;
switch (otThreadGetVersion())
{
case kThreadVersion11:
version = "1.1.1";
break;
case kThreadVersion12:
version = "1.2.0";
break;
case kThreadVersion13:
version = "1.3.0";
break;
case kThreadVersion14:
version = "1.4.0";
break;
default:
otbrLogEmerg("Unexpected thread version %hu", otThreadGetVersion());
exit(-1);
}
return version;
}
void RcpHost::Join(const otOperationalDatasetTlvs &aActiveOpDatasetTlvs, const AsyncResultReceiver &aReceiver)
{
OT_UNUSED_VARIABLE(aActiveOpDatasetTlvs);
// TODO: Implement Join under RCP mode.
mTaskRunner.Post([aReceiver](void) { aReceiver(OT_ERROR_NOT_IMPLEMENTED, "Not implemented!"); });
}
void RcpHost::Leave(const AsyncResultReceiver &aReceiver)
{
// TODO: Implement Leave under RCP mode.
mTaskRunner.Post([aReceiver](void) { aReceiver(OT_ERROR_NOT_IMPLEMENTED, "Not implemented!"); });
}
/*
* Provide, if required an "otPlatLog()" function
*/
extern "C" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
{
OT_UNUSED_VARIABLE(aLogRegion);
otbrLogLevel otbrLogLevel = RcpHost::ConvertToOtbrLogLevel(aLogLevel);
va_list ap;
va_start(ap, aFormat);
otbrLogvNoFilter(otbrLogLevel, aFormat, ap);
va_end(ap);
}
extern "C" void otPlatLogHandleLevelChanged(otLogLevel aLogLevel)
{
otbrLogSetLevel(RcpHost::ConvertToOtbrLogLevel(aLogLevel));
otbrLogInfo("OpenThread log level changed to %d", aLogLevel);
}
} // namespace Ncp
} // namespace otbr