• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2020, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <assert.h>
30 #include <net/if.h>
31 #include <string.h>
32 
33 #include <openthread/border_router.h>
34 #include <openthread/channel_monitor.h>
35 #include <openthread/dnssd_server.h>
36 #include <openthread/instance.h>
37 #include <openthread/joiner.h>
38 #include <openthread/link_raw.h>
39 #include <openthread/nat64.h>
40 #include <openthread/ncp.h>
41 #include <openthread/netdata.h>
42 #include <openthread/openthread-system.h>
43 #include <openthread/srp_server.h>
44 #include <openthread/thread_ftd.h>
45 #include <openthread/trel.h>
46 #include <openthread/platform/radio.h>
47 
48 #include "common/api_strings.hpp"
49 #include "common/byteswap.hpp"
50 #include "common/code_utils.hpp"
51 #include "dbus/common/constants.hpp"
52 #include "dbus/server/dbus_agent.hpp"
53 #include "dbus/server/dbus_thread_object.hpp"
54 #if OTBR_ENABLE_FEATURE_FLAGS
55 #include "proto/feature_flag.pb.h"
56 #endif
57 #if OTBR_ENABLE_TELEMETRY_DATA_API
58 #include "proto/thread_telemetry.pb.h"
59 #endif
60 #include "proto/capabilities.pb.h"
61 
62 using std::placeholders::_1;
63 using std::placeholders::_2;
64 
65 #if OTBR_ENABLE_NAT64
GetNat64StateName(otNat64State aState)66 static std::string GetNat64StateName(otNat64State aState)
67 {
68     std::string stateName;
69 
70     switch (aState)
71     {
72     case OT_NAT64_STATE_DISABLED:
73         stateName = OTBR_NAT64_STATE_NAME_DISABLED;
74         break;
75     case OT_NAT64_STATE_NOT_RUNNING:
76         stateName = OTBR_NAT64_STATE_NAME_NOT_RUNNING;
77         break;
78     case OT_NAT64_STATE_IDLE:
79         stateName = OTBR_NAT64_STATE_NAME_IDLE;
80         break;
81     case OT_NAT64_STATE_ACTIVE:
82         stateName = OTBR_NAT64_STATE_NAME_ACTIVE;
83         break;
84     }
85 
86     return stateName;
87 }
88 #endif // OTBR_ENABLE_NAT64
89 
90 namespace otbr {
91 namespace DBus {
92 
DBusThreadObject(DBusConnection * aConnection,const std::string & aInterfaceName,otbr::Ncp::ControllerOpenThread * aNcp,Mdns::Publisher * aPublisher)93 DBusThreadObject::DBusThreadObject(DBusConnection                  *aConnection,
94                                    const std::string               &aInterfaceName,
95                                    otbr::Ncp::ControllerOpenThread *aNcp,
96                                    Mdns::Publisher                 *aPublisher)
97     : DBusObject(aConnection, OTBR_DBUS_OBJECT_PREFIX + aInterfaceName)
98     , mNcp(aNcp)
99     , mPublisher(aPublisher)
100 {
101 }
102 
Init(void)103 otbrError DBusThreadObject::Init(void)
104 {
105     otbrError error        = OTBR_ERROR_NONE;
106     auto      threadHelper = mNcp->GetThreadHelper();
107 
108     SuccessOrExit(error = DBusObject::Init());
109 
110     threadHelper->AddDeviceRoleHandler(std::bind(&DBusThreadObject::DeviceRoleHandler, this, _1));
111     threadHelper->AddActiveDatasetChangeHandler(std::bind(&DBusThreadObject::ActiveDatasetChangeHandler, this, _1));
112     mNcp->RegisterResetHandler(std::bind(&DBusThreadObject::NcpResetHandler, this));
113 
114     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_SCAN_METHOD,
115                    std::bind(&DBusThreadObject::ScanHandler, this, _1));
116     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_ENERGY_SCAN_METHOD,
117                    std::bind(&DBusThreadObject::EnergyScanHandler, this, _1));
118     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_ATTACH_METHOD,
119                    std::bind(&DBusThreadObject::AttachHandler, this, _1));
120     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_DETACH_METHOD,
121                    std::bind(&DBusThreadObject::DetachHandler, this, _1));
122     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_FACTORY_RESET_METHOD,
123                    std::bind(&DBusThreadObject::FactoryResetHandler, this, _1));
124     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_RESET_METHOD,
125                    std::bind(&DBusThreadObject::ResetHandler, this, _1));
126     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_JOINER_START_METHOD,
127                    std::bind(&DBusThreadObject::JoinerStartHandler, this, _1));
128     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_JOINER_STOP_METHOD,
129                    std::bind(&DBusThreadObject::JoinerStopHandler, this, _1));
130     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PERMIT_UNSECURE_JOIN_METHOD,
131                    std::bind(&DBusThreadObject::PermitUnsecureJoinHandler, this, _1));
132     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_ADD_ON_MESH_PREFIX_METHOD,
133                    std::bind(&DBusThreadObject::AddOnMeshPrefixHandler, this, _1));
134     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_REMOVE_ON_MESH_PREFIX_METHOD,
135                    std::bind(&DBusThreadObject::RemoveOnMeshPrefixHandler, this, _1));
136     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_ADD_EXTERNAL_ROUTE_METHOD,
137                    std::bind(&DBusThreadObject::AddExternalRouteHandler, this, _1));
138     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_REMOVE_EXTERNAL_ROUTE_METHOD,
139                    std::bind(&DBusThreadObject::RemoveExternalRouteHandler, this, _1));
140     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_ATTACH_ALL_NODES_TO_METHOD,
141                    std::bind(&DBusThreadObject::AttachAllNodesToHandler, this, _1));
142     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_UPDATE_VENDOR_MESHCOP_TXT_METHOD,
143                    std::bind(&DBusThreadObject::UpdateMeshCopTxtHandler, this, _1));
144     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_GET_PROPERTIES_METHOD,
145                    std::bind(&DBusThreadObject::GetPropertiesHandler, this, _1));
146     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_LEAVE_NETWORK_METHOD,
147                    std::bind(&DBusThreadObject::LeaveNetworkHandler, this, _1));
148     RegisterMethod(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_SET_NAT64_ENABLED_METHOD,
149                    std::bind(&DBusThreadObject::SetNat64Enabled, this, _1));
150 
151     RegisterMethod(DBUS_INTERFACE_INTROSPECTABLE, DBUS_INTROSPECT_METHOD,
152                    std::bind(&DBusThreadObject::IntrospectHandler, this, _1));
153 
154     RegisterSetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_MESH_LOCAL_PREFIX,
155                                std::bind(&DBusThreadObject::SetMeshLocalPrefixHandler, this, _1));
156     RegisterSetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_LINK_MODE,
157                                std::bind(&DBusThreadObject::SetLinkModeHandler, this, _1));
158     RegisterSetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_ACTIVE_DATASET_TLVS,
159                                std::bind(&DBusThreadObject::SetActiveDatasetTlvsHandler, this, _1));
160     RegisterSetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_FEATURE_FLAG_LIST_DATA,
161                                std::bind(&DBusThreadObject::SetFeatureFlagListDataHandler, this, _1));
162     RegisterSetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RADIO_REGION,
163                                std::bind(&DBusThreadObject::SetRadioRegionHandler, this, _1));
164     RegisterSetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_DNS_UPSTREAM_QUERY_STATE,
165                                std::bind(&DBusThreadObject::SetDnsUpstreamQueryState, this, _1));
166     RegisterSetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NAT64_CIDR,
167                                std::bind(&DBusThreadObject::SetNat64Cidr, this, _1));
168 
169     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_LINK_MODE,
170                                std::bind(&DBusThreadObject::GetLinkModeHandler, this, _1));
171     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_DEVICE_ROLE,
172                                std::bind(&DBusThreadObject::GetDeviceRoleHandler, this, _1));
173     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NETWORK_NAME,
174                                std::bind(&DBusThreadObject::GetNetworkNameHandler, this, _1));
175 
176     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_PANID,
177                                std::bind(&DBusThreadObject::GetPanIdHandler, this, _1));
178     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_EXTPANID,
179                                std::bind(&DBusThreadObject::GetExtPanIdHandler, this, _1));
180     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_EUI64,
181                                std::bind(&DBusThreadObject::GetEui64Handler, this, _1));
182     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_CHANNEL,
183                                std::bind(&DBusThreadObject::GetChannelHandler, this, _1));
184     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NETWORK_KEY,
185                                std::bind(&DBusThreadObject::GetNetworkKeyHandler, this, _1));
186     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_CCA_FAILURE_RATE,
187                                std::bind(&DBusThreadObject::GetCcaFailureRateHandler, this, _1));
188     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_LINK_COUNTERS,
189                                std::bind(&DBusThreadObject::GetLinkCountersHandler, this, _1));
190     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_IP6_COUNTERS,
191                                std::bind(&DBusThreadObject::GetIp6CountersHandler, this, _1));
192     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_SUPPORTED_CHANNEL_MASK,
193                                std::bind(&DBusThreadObject::GetSupportedChannelMaskHandler, this, _1));
194     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_PREFERRED_CHANNEL_MASK,
195                                std::bind(&DBusThreadObject::GetPreferredChannelMaskHandler, this, _1));
196     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RLOC16,
197                                std::bind(&DBusThreadObject::GetRloc16Handler, this, _1));
198     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_EXTENDED_ADDRESS,
199                                std::bind(&DBusThreadObject::GetExtendedAddressHandler, this, _1));
200     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_ROUTER_ID,
201                                std::bind(&DBusThreadObject::GetRouterIdHandler, this, _1));
202     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_LEADER_DATA,
203                                std::bind(&DBusThreadObject::GetLeaderDataHandler, this, _1));
204     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NETWORK_DATA_PRPOERTY,
205                                std::bind(&DBusThreadObject::GetNetworkDataHandler, this, _1));
206     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_STABLE_NETWORK_DATA_PRPOERTY,
207                                std::bind(&DBusThreadObject::GetStableNetworkDataHandler, this, _1));
208     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_LOCAL_LEADER_WEIGHT,
209                                std::bind(&DBusThreadObject::GetLocalLeaderWeightHandler, this, _1));
210 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
211     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_CHANNEL_MONITOR_SAMPLE_COUNT,
212                                std::bind(&DBusThreadObject::GetChannelMonitorSampleCountHandler, this, _1));
213     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_CHANNEL_MONITOR_ALL_CHANNEL_QUALITIES,
214                                std::bind(&DBusThreadObject::GetChannelMonitorAllChannelQualities, this, _1));
215 #endif
216     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_CHILD_TABLE,
217                                std::bind(&DBusThreadObject::GetChildTableHandler, this, _1));
218     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NEIGHBOR_TABLE_PROEPRTY,
219                                std::bind(&DBusThreadObject::GetNeighborTableHandler, this, _1));
220     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_PARTITION_ID_PROEPRTY,
221                                std::bind(&DBusThreadObject::GetPartitionIDHandler, this, _1));
222     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_INSTANT_RSSI,
223                                std::bind(&DBusThreadObject::GetInstantRssiHandler, this, _1));
224     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RADIO_TX_POWER,
225                                std::bind(&DBusThreadObject::GetRadioTxPowerHandler, this, _1));
226     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_EXTERNAL_ROUTES,
227                                std::bind(&DBusThreadObject::GetExternalRoutesHandler, this, _1));
228     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_ON_MESH_PREFIXES,
229                                std::bind(&DBusThreadObject::GetOnMeshPrefixesHandler, this, _1));
230     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_ACTIVE_DATASET_TLVS,
231                                std::bind(&DBusThreadObject::GetActiveDatasetTlvsHandler, this, _1));
232     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_PENDING_DATASET_TLVS,
233                                std::bind(&DBusThreadObject::GetPendingDatasetTlvsHandler, this, _1));
234     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_FEATURE_FLAG_LIST_DATA,
235                                std::bind(&DBusThreadObject::GetFeatureFlagListDataHandler, this, _1));
236     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RADIO_REGION,
237                                std::bind(&DBusThreadObject::GetRadioRegionHandler, this, _1));
238     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_SRP_SERVER_INFO,
239                                std::bind(&DBusThreadObject::GetSrpServerInfoHandler, this, _1));
240     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_MDNS_TELEMETRY_INFO,
241                                std::bind(&DBusThreadObject::GetMdnsTelemetryInfoHandler, this, _1));
242     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_DNSSD_COUNTERS,
243                                std::bind(&DBusThreadObject::GetDnssdCountersHandler, this, _1));
244     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_OTBR_VERSION,
245                                std::bind(&DBusThreadObject::GetOtbrVersionHandler, this, _1));
246     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_OT_HOST_VERSION,
247                                std::bind(&DBusThreadObject::GetOtHostVersionHandler, this, _1));
248     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_OT_RCP_VERSION,
249                                std::bind(&DBusThreadObject::GetOtRcpVersionHandler, this, _1));
250     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_THREAD_VERSION,
251                                std::bind(&DBusThreadObject::GetThreadVersionHandler, this, _1));
252     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RADIO_SPINEL_METRICS,
253                                std::bind(&DBusThreadObject::GetRadioSpinelMetricsHandler, this, _1));
254     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RCP_INTERFACE_METRICS,
255                                std::bind(&DBusThreadObject::GetRcpInterfaceMetricsHandler, this, _1));
256     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_UPTIME,
257                                std::bind(&DBusThreadObject::GetUptimeHandler, this, _1));
258     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_RADIO_COEX_METRICS,
259                                std::bind(&DBusThreadObject::GetRadioCoexMetrics, this, _1));
260     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_BORDER_ROUTING_COUNTERS,
261                                std::bind(&DBusThreadObject::GetBorderRoutingCountersHandler, this, _1));
262     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NAT64_STATE,
263                                std::bind(&DBusThreadObject::GetNat64State, this, _1));
264     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NAT64_MAPPINGS,
265                                std::bind(&DBusThreadObject::GetNat64Mappings, this, _1));
266     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NAT64_PROTOCOL_COUNTERS,
267                                std::bind(&DBusThreadObject::GetNat64ProtocolCounters, this, _1));
268     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NAT64_ERROR_COUNTERS,
269                                std::bind(&DBusThreadObject::GetNat64ErrorCounters, this, _1));
270     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_NAT64_CIDR,
271                                std::bind(&DBusThreadObject::GetNat64Cidr, this, _1));
272     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_INFRA_LINK_INFO,
273                                std::bind(&DBusThreadObject::GetInfraLinkInfo, this, _1));
274     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_TREL_INFO,
275                                std::bind(&DBusThreadObject::GetTrelInfoHandler, this, _1));
276     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_DNS_UPSTREAM_QUERY_STATE,
277                                std::bind(&DBusThreadObject::GetDnsUpstreamQueryState, this, _1));
278     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_TELEMETRY_DATA,
279                                std::bind(&DBusThreadObject::GetTelemetryDataHandler, this, _1));
280     RegisterGetPropertyHandler(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_CAPABILITIES,
281                                std::bind(&DBusThreadObject::GetCapabilitiesHandler, this, _1));
282 
283     SuccessOrExit(error = Signal(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_SIGNAL_READY, std::make_tuple()));
284 
285 exit:
286     return error;
287 }
288 
DeviceRoleHandler(otDeviceRole aDeviceRole)289 void DBusThreadObject::DeviceRoleHandler(otDeviceRole aDeviceRole)
290 {
291     SignalPropertyChanged(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_DEVICE_ROLE, GetDeviceRoleName(aDeviceRole));
292 }
293 
NcpResetHandler(void)294 void DBusThreadObject::NcpResetHandler(void)
295 {
296     mNcp->GetThreadHelper()->AddDeviceRoleHandler(std::bind(&DBusThreadObject::DeviceRoleHandler, this, _1));
297     mNcp->GetThreadHelper()->AddActiveDatasetChangeHandler(
298         std::bind(&DBusThreadObject::ActiveDatasetChangeHandler, this, _1));
299     SignalPropertyChanged(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_DEVICE_ROLE,
300                           GetDeviceRoleName(OT_DEVICE_ROLE_DISABLED));
301 }
302 
ScanHandler(DBusRequest & aRequest)303 void DBusThreadObject::ScanHandler(DBusRequest &aRequest)
304 {
305     auto threadHelper = mNcp->GetThreadHelper();
306     threadHelper->Scan(std::bind(&DBusThreadObject::ReplyScanResult, this, aRequest, _1, _2));
307 }
308 
ReplyScanResult(DBusRequest & aRequest,otError aError,const std::vector<otActiveScanResult> & aResult)309 void DBusThreadObject::ReplyScanResult(DBusRequest                           &aRequest,
310                                        otError                                aError,
311                                        const std::vector<otActiveScanResult> &aResult)
312 {
313     std::vector<ActiveScanResult> results;
314 
315     if (aError != OT_ERROR_NONE)
316     {
317         aRequest.ReplyOtResult(aError);
318     }
319     else
320     {
321         for (const auto &r : aResult)
322         {
323             ActiveScanResult result = {};
324 
325             result.mExtAddress = ConvertOpenThreadUint64(r.mExtAddress.m8);
326             result.mPanId      = r.mPanId;
327             result.mChannel    = r.mChannel;
328             result.mRssi       = r.mRssi;
329             result.mLqi        = r.mLqi;
330 
331             results.emplace_back(result);
332         }
333 
334         aRequest.Reply(std::tie(results));
335     }
336 }
337 
EnergyScanHandler(DBusRequest & aRequest)338 void DBusThreadObject::EnergyScanHandler(DBusRequest &aRequest)
339 {
340     otError  error        = OT_ERROR_NONE;
341     auto     threadHelper = mNcp->GetThreadHelper();
342     uint32_t scanDuration;
343 
344     auto args = std::tie(scanDuration);
345 
346     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
347     threadHelper->EnergyScan(scanDuration, std::bind(&DBusThreadObject::ReplyEnergyScanResult, this, aRequest, _1, _2));
348 
349 exit:
350     if (error != OT_ERROR_NONE)
351     {
352         aRequest.ReplyOtResult(error);
353     }
354 }
355 
ReplyEnergyScanResult(DBusRequest & aRequest,otError aError,const std::vector<otEnergyScanResult> & aResult)356 void DBusThreadObject::ReplyEnergyScanResult(DBusRequest                           &aRequest,
357                                              otError                                aError,
358                                              const std::vector<otEnergyScanResult> &aResult)
359 {
360     std::vector<EnergyScanResult> results;
361 
362     if (aError != OT_ERROR_NONE)
363     {
364         aRequest.ReplyOtResult(aError);
365     }
366     else
367     {
368         for (const auto &r : aResult)
369         {
370             EnergyScanResult result;
371 
372             result.mChannel = r.mChannel;
373             result.mMaxRssi = r.mMaxRssi;
374 
375             results.emplace_back(result);
376         }
377 
378         aRequest.Reply(std::tie(results));
379     }
380 }
381 
AttachHandler(DBusRequest & aRequest)382 void DBusThreadObject::AttachHandler(DBusRequest &aRequest)
383 {
384     auto                 threadHelper = mNcp->GetThreadHelper();
385     std::string          name;
386     uint16_t             panid;
387     uint64_t             extPanId;
388     std::vector<uint8_t> networkKey;
389     std::vector<uint8_t> pskc;
390     uint32_t             channelMask;
391 
392     auto args = std::tie(networkKey, panid, name, extPanId, pskc, channelMask);
393 
394     if (IsDBusMessageEmpty(*aRequest.GetMessage()))
395     {
396         threadHelper->Attach([aRequest](otError aError, int64_t aAttachDelayMs) mutable {
397             OT_UNUSED_VARIABLE(aAttachDelayMs);
398 
399             aRequest.ReplyOtResult(aError);
400         });
401     }
402     else if (DBusMessageToTuple(*aRequest.GetMessage(), args) != OTBR_ERROR_NONE)
403     {
404         aRequest.ReplyOtResult(OT_ERROR_INVALID_ARGS);
405     }
406     else
407     {
408         threadHelper->Attach(name, panid, extPanId, networkKey, pskc, channelMask,
409                              [aRequest](otError aError, int64_t aAttachDelayMs) mutable {
410                                  OT_UNUSED_VARIABLE(aAttachDelayMs);
411 
412                                  aRequest.ReplyOtResult(aError);
413                              });
414     }
415 }
416 
AttachAllNodesToHandler(DBusRequest & aRequest)417 void DBusThreadObject::AttachAllNodesToHandler(DBusRequest &aRequest)
418 {
419     std::vector<uint8_t> dataset;
420     otError              error = OT_ERROR_NONE;
421 
422     auto args = std::tie(dataset);
423 
424     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
425 
426     mNcp->GetThreadHelper()->AttachAllNodesTo(dataset, [aRequest](otError error, int64_t aAttachDelayMs) mutable {
427         aRequest.ReplyOtResult<int64_t>(error, aAttachDelayMs);
428     });
429 
430 exit:
431     if (error != OT_ERROR_NONE)
432     {
433         aRequest.ReplyOtResult(error);
434     }
435 }
436 
DetachHandler(DBusRequest & aRequest)437 void DBusThreadObject::DetachHandler(DBusRequest &aRequest)
438 {
439     aRequest.ReplyOtResult(mNcp->GetThreadHelper()->Detach());
440 }
441 
FactoryResetHandler(DBusRequest & aRequest)442 void DBusThreadObject::FactoryResetHandler(DBusRequest &aRequest)
443 {
444     otError error = OT_ERROR_NONE;
445 
446     SuccessOrExit(error = mNcp->GetThreadHelper()->Detach());
447     SuccessOrExit(otInstanceErasePersistentInfo(mNcp->GetThreadHelper()->GetInstance()));
448     mNcp->Reset();
449 
450 exit:
451     aRequest.ReplyOtResult(error);
452 }
453 
ResetHandler(DBusRequest & aRequest)454 void DBusThreadObject::ResetHandler(DBusRequest &aRequest)
455 {
456     mNcp->Reset();
457     aRequest.ReplyOtResult(OT_ERROR_NONE);
458 }
459 
JoinerStartHandler(DBusRequest & aRequest)460 void DBusThreadObject::JoinerStartHandler(DBusRequest &aRequest)
461 {
462     auto        threadHelper = mNcp->GetThreadHelper();
463     std::string pskd, provisionUrl, vendorName, vendorModel, vendorSwVersion, vendorData;
464     auto        args = std::tie(pskd, provisionUrl, vendorName, vendorModel, vendorSwVersion, vendorData);
465 
466     if (DBusMessageToTuple(*aRequest.GetMessage(), args) != OTBR_ERROR_NONE)
467     {
468         aRequest.ReplyOtResult(OT_ERROR_INVALID_ARGS);
469     }
470     else
471     {
472         threadHelper->JoinerStart(pskd, provisionUrl, vendorName, vendorModel, vendorSwVersion, vendorData,
473                                   [aRequest](otError aError) mutable { aRequest.ReplyOtResult(aError); });
474     }
475 }
476 
JoinerStopHandler(DBusRequest & aRequest)477 void DBusThreadObject::JoinerStopHandler(DBusRequest &aRequest)
478 {
479     auto threadHelper = mNcp->GetThreadHelper();
480 
481     otJoinerStop(threadHelper->GetInstance());
482     aRequest.ReplyOtResult(OT_ERROR_NONE);
483 }
484 
PermitUnsecureJoinHandler(DBusRequest & aRequest)485 void DBusThreadObject::PermitUnsecureJoinHandler(DBusRequest &aRequest)
486 {
487 #ifdef OTBR_ENABLE_UNSECURE_JOIN
488     auto     threadHelper = mNcp->GetThreadHelper();
489     uint16_t port;
490     uint32_t timeout;
491     auto     args = std::tie(port, timeout);
492 
493     if (DBusMessageToTuple(*aRequest.GetMessage(), args) != OTBR_ERROR_NONE)
494     {
495         aRequest.ReplyOtResult(OT_ERROR_INVALID_ARGS);
496     }
497     else
498     {
499         aRequest.ReplyOtResult(threadHelper->PermitUnsecureJoin(port, timeout));
500     }
501 #else
502     aRequest.ReplyOtResult(OT_ERROR_NOT_IMPLEMENTED);
503 #endif
504 }
505 
AddOnMeshPrefixHandler(DBusRequest & aRequest)506 void DBusThreadObject::AddOnMeshPrefixHandler(DBusRequest &aRequest)
507 {
508     auto                 threadHelper = mNcp->GetThreadHelper();
509     OnMeshPrefix         onMeshPrefix;
510     auto                 args  = std::tie(onMeshPrefix);
511     otError              error = OT_ERROR_NONE;
512     otBorderRouterConfig config;
513 
514     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
515 
516     // size is guaranteed by parsing
517     std::copy(onMeshPrefix.mPrefix.mPrefix.begin(), onMeshPrefix.mPrefix.mPrefix.end(),
518               &config.mPrefix.mPrefix.mFields.m8[0]);
519     config.mPrefix.mLength = onMeshPrefix.mPrefix.mLength;
520     config.mPreference     = onMeshPrefix.mPreference;
521     config.mSlaac          = onMeshPrefix.mSlaac;
522     config.mDhcp           = onMeshPrefix.mDhcp;
523     config.mConfigure      = onMeshPrefix.mConfigure;
524     config.mDefaultRoute   = onMeshPrefix.mDefaultRoute;
525     config.mOnMesh         = onMeshPrefix.mOnMesh;
526     config.mStable         = onMeshPrefix.mStable;
527 
528     SuccessOrExit(error = otBorderRouterAddOnMeshPrefix(threadHelper->GetInstance(), &config));
529     SuccessOrExit(error = otBorderRouterRegister(threadHelper->GetInstance()));
530 
531 exit:
532     aRequest.ReplyOtResult(error);
533 }
534 
RemoveOnMeshPrefixHandler(DBusRequest & aRequest)535 void DBusThreadObject::RemoveOnMeshPrefixHandler(DBusRequest &aRequest)
536 {
537     auto        threadHelper = mNcp->GetThreadHelper();
538     Ip6Prefix   onMeshPrefix;
539     auto        args  = std::tie(onMeshPrefix);
540     otError     error = OT_ERROR_NONE;
541     otIp6Prefix prefix;
542 
543     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
544     // size is guaranteed by parsing
545     std::copy(onMeshPrefix.mPrefix.begin(), onMeshPrefix.mPrefix.end(), &prefix.mPrefix.mFields.m8[0]);
546     prefix.mLength = onMeshPrefix.mLength;
547 
548     SuccessOrExit(error = otBorderRouterRemoveOnMeshPrefix(threadHelper->GetInstance(), &prefix));
549     SuccessOrExit(error = otBorderRouterRegister(threadHelper->GetInstance()));
550 
551 exit:
552     aRequest.ReplyOtResult(error);
553 }
554 
AddExternalRouteHandler(DBusRequest & aRequest)555 void DBusThreadObject::AddExternalRouteHandler(DBusRequest &aRequest)
556 {
557     auto                  threadHelper = mNcp->GetThreadHelper();
558     ExternalRoute         route;
559     auto                  args  = std::tie(route);
560     otError               error = OT_ERROR_NONE;
561     otExternalRouteConfig otRoute;
562     otIp6Prefix          &prefix = otRoute.mPrefix;
563 
564     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
565 
566     // size is guaranteed by parsing
567     std::copy(route.mPrefix.mPrefix.begin(), route.mPrefix.mPrefix.end(), &prefix.mPrefix.mFields.m8[0]);
568     prefix.mLength      = route.mPrefix.mLength;
569     otRoute.mPreference = route.mPreference;
570     otRoute.mStable     = route.mStable;
571 
572     SuccessOrExit(error = otBorderRouterAddRoute(threadHelper->GetInstance(), &otRoute));
573     if (route.mStable)
574     {
575         SuccessOrExit(error = otBorderRouterRegister(threadHelper->GetInstance()));
576     }
577 
578 exit:
579     aRequest.ReplyOtResult(error);
580 }
581 
RemoveExternalRouteHandler(DBusRequest & aRequest)582 void DBusThreadObject::RemoveExternalRouteHandler(DBusRequest &aRequest)
583 {
584     auto        threadHelper = mNcp->GetThreadHelper();
585     Ip6Prefix   routePrefix;
586     auto        args  = std::tie(routePrefix);
587     otError     error = OT_ERROR_NONE;
588     otIp6Prefix prefix;
589 
590     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
591 
592     // size is guaranteed by parsing
593     std::copy(routePrefix.mPrefix.begin(), routePrefix.mPrefix.end(), &prefix.mPrefix.mFields.m8[0]);
594     prefix.mLength = routePrefix.mLength;
595 
596     SuccessOrExit(error = otBorderRouterRemoveRoute(threadHelper->GetInstance(), &prefix));
597     SuccessOrExit(error = otBorderRouterRegister(threadHelper->GetInstance()));
598 
599 exit:
600     aRequest.ReplyOtResult(error);
601 }
602 
IntrospectHandler(DBusRequest & aRequest)603 void DBusThreadObject::IntrospectHandler(DBusRequest &aRequest)
604 {
605     std::string xmlString(
606 #include "dbus/server/introspect.hpp"
607     );
608 
609     aRequest.Reply(std::tie(xmlString));
610 }
611 
SetMeshLocalPrefixHandler(DBusMessageIter & aIter)612 otError DBusThreadObject::SetMeshLocalPrefixHandler(DBusMessageIter &aIter)
613 {
614     auto                                      threadHelper = mNcp->GetThreadHelper();
615     otMeshLocalPrefix                         prefix;
616     std::array<uint8_t, OTBR_IP6_PREFIX_SIZE> data{};
617     otError                                   error = OT_ERROR_NONE;
618 
619     VerifyOrExit(DBusMessageExtractFromVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
620     memcpy(&prefix.m8, &data.front(), sizeof(prefix.m8));
621     error = otThreadSetMeshLocalPrefix(threadHelper->GetInstance(), &prefix);
622 
623 exit:
624     return error;
625 }
626 
SetLinkModeHandler(DBusMessageIter & aIter)627 otError DBusThreadObject::SetLinkModeHandler(DBusMessageIter &aIter)
628 {
629     auto             threadHelper = mNcp->GetThreadHelper();
630     LinkModeConfig   cfg;
631     otLinkModeConfig otCfg;
632     otError          error = OT_ERROR_NONE;
633 
634     VerifyOrExit(DBusMessageExtractFromVariant(&aIter, cfg) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
635     otCfg.mDeviceType   = cfg.mDeviceType;
636     otCfg.mNetworkData  = cfg.mNetworkData;
637     otCfg.mRxOnWhenIdle = cfg.mRxOnWhenIdle;
638     error               = otThreadSetLinkMode(threadHelper->GetInstance(), otCfg);
639 
640 exit:
641     return error;
642 }
643 
GetLinkModeHandler(DBusMessageIter & aIter)644 otError DBusThreadObject::GetLinkModeHandler(DBusMessageIter &aIter)
645 {
646     auto             threadHelper = mNcp->GetThreadHelper();
647     otLinkModeConfig otCfg        = otThreadGetLinkMode(threadHelper->GetInstance());
648     LinkModeConfig   cfg;
649     otError          error = OT_ERROR_NONE;
650 
651     cfg.mDeviceType   = otCfg.mDeviceType;
652     cfg.mNetworkData  = otCfg.mNetworkData;
653     cfg.mRxOnWhenIdle = otCfg.mRxOnWhenIdle;
654 
655     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, cfg) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
656 
657 exit:
658     return error;
659 }
660 
GetDeviceRoleHandler(DBusMessageIter & aIter)661 otError DBusThreadObject::GetDeviceRoleHandler(DBusMessageIter &aIter)
662 {
663     auto         threadHelper = mNcp->GetThreadHelper();
664     otDeviceRole role         = otThreadGetDeviceRole(threadHelper->GetInstance());
665     std::string  roleName     = GetDeviceRoleName(role);
666     otError      error        = OT_ERROR_NONE;
667 
668     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, roleName) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
669 
670 exit:
671     return error;
672 }
673 
GetNetworkNameHandler(DBusMessageIter & aIter)674 otError DBusThreadObject::GetNetworkNameHandler(DBusMessageIter &aIter)
675 {
676     auto        threadHelper = mNcp->GetThreadHelper();
677     std::string networkName  = otThreadGetNetworkName(threadHelper->GetInstance());
678     otError     error        = OT_ERROR_NONE;
679 
680     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, networkName) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
681 
682 exit:
683     return error;
684 }
685 
GetPanIdHandler(DBusMessageIter & aIter)686 otError DBusThreadObject::GetPanIdHandler(DBusMessageIter &aIter)
687 {
688     auto     threadHelper = mNcp->GetThreadHelper();
689     uint16_t panId        = otLinkGetPanId(threadHelper->GetInstance());
690     otError  error        = OT_ERROR_NONE;
691 
692     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, panId) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
693 
694 exit:
695     return error;
696 }
697 
GetExtPanIdHandler(DBusMessageIter & aIter)698 otError DBusThreadObject::GetExtPanIdHandler(DBusMessageIter &aIter)
699 {
700     auto                   threadHelper = mNcp->GetThreadHelper();
701     const otExtendedPanId *extPanId     = otThreadGetExtendedPanId(threadHelper->GetInstance());
702     uint64_t               extPanIdVal;
703     otError                error = OT_ERROR_NONE;
704 
705     extPanIdVal = ConvertOpenThreadUint64(extPanId->m8);
706 
707     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, extPanIdVal) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
708 
709 exit:
710     return error;
711 }
712 
GetChannelHandler(DBusMessageIter & aIter)713 otError DBusThreadObject::GetChannelHandler(DBusMessageIter &aIter)
714 {
715     auto     threadHelper = mNcp->GetThreadHelper();
716     uint16_t channel      = otLinkGetChannel(threadHelper->GetInstance());
717     otError  error        = OT_ERROR_NONE;
718 
719     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, channel) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
720 
721 exit:
722     return error;
723 }
724 
GetNetworkKeyHandler(DBusMessageIter & aIter)725 otError DBusThreadObject::GetNetworkKeyHandler(DBusMessageIter &aIter)
726 {
727     auto         threadHelper = mNcp->GetThreadHelper();
728     otNetworkKey networkKey;
729     otError      error = OT_ERROR_NONE;
730 
731     otThreadGetNetworkKey(threadHelper->GetInstance(), &networkKey);
732     std::vector<uint8_t> keyVal(networkKey.m8, networkKey.m8 + sizeof(networkKey.m8));
733     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, keyVal) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
734 
735 exit:
736     return error;
737 }
738 
GetCcaFailureRateHandler(DBusMessageIter & aIter)739 otError DBusThreadObject::GetCcaFailureRateHandler(DBusMessageIter &aIter)
740 {
741     auto     threadHelper = mNcp->GetThreadHelper();
742     uint16_t failureRate  = otLinkGetCcaFailureRate(threadHelper->GetInstance());
743     otError  error        = OT_ERROR_NONE;
744 
745     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, failureRate) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
746 
747 exit:
748     return error;
749 }
750 
GetLinkCountersHandler(DBusMessageIter & aIter)751 otError DBusThreadObject::GetLinkCountersHandler(DBusMessageIter &aIter)
752 {
753     auto                 threadHelper = mNcp->GetThreadHelper();
754     const otMacCounters *otCounters   = otLinkGetCounters(threadHelper->GetInstance());
755     MacCounters          counters;
756     otError              error = OT_ERROR_NONE;
757 
758     counters.mTxTotal              = otCounters->mTxTotal;
759     counters.mTxUnicast            = otCounters->mTxUnicast;
760     counters.mTxBroadcast          = otCounters->mTxBroadcast;
761     counters.mTxAckRequested       = otCounters->mTxAckRequested;
762     counters.mTxAcked              = otCounters->mTxAcked;
763     counters.mTxNoAckRequested     = otCounters->mTxNoAckRequested;
764     counters.mTxData               = otCounters->mTxData;
765     counters.mTxDataPoll           = otCounters->mTxDataPoll;
766     counters.mTxBeacon             = otCounters->mTxBeacon;
767     counters.mTxBeaconRequest      = otCounters->mTxBeaconRequest;
768     counters.mTxOther              = otCounters->mTxOther;
769     counters.mTxRetry              = otCounters->mTxRetry;
770     counters.mTxErrCca             = otCounters->mTxErrCca;
771     counters.mTxErrAbort           = otCounters->mTxErrAbort;
772     counters.mTxErrBusyChannel     = otCounters->mTxErrBusyChannel;
773     counters.mRxTotal              = otCounters->mRxTotal;
774     counters.mRxUnicast            = otCounters->mRxUnicast;
775     counters.mRxBroadcast          = otCounters->mRxBroadcast;
776     counters.mRxData               = otCounters->mRxData;
777     counters.mRxDataPoll           = otCounters->mRxDataPoll;
778     counters.mRxBeacon             = otCounters->mRxBeacon;
779     counters.mRxBeaconRequest      = otCounters->mRxBeaconRequest;
780     counters.mRxOther              = otCounters->mRxOther;
781     counters.mRxAddressFiltered    = otCounters->mRxAddressFiltered;
782     counters.mRxDestAddrFiltered   = otCounters->mRxDestAddrFiltered;
783     counters.mRxDuplicated         = otCounters->mRxDuplicated;
784     counters.mRxErrNoFrame         = otCounters->mRxErrNoFrame;
785     counters.mRxErrUnknownNeighbor = otCounters->mRxErrUnknownNeighbor;
786     counters.mRxErrInvalidSrcAddr  = otCounters->mRxErrInvalidSrcAddr;
787     counters.mRxErrSec             = otCounters->mRxErrSec;
788     counters.mRxErrFcs             = otCounters->mRxErrFcs;
789     counters.mRxErrOther           = otCounters->mRxErrOther;
790 
791     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, counters) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
792 
793 exit:
794     return error;
795 }
796 
GetIp6CountersHandler(DBusMessageIter & aIter)797 otError DBusThreadObject::GetIp6CountersHandler(DBusMessageIter &aIter)
798 {
799     auto                threadHelper = mNcp->GetThreadHelper();
800     const otIpCounters *otCounters   = otThreadGetIp6Counters(threadHelper->GetInstance());
801     IpCounters          counters;
802     otError             error = OT_ERROR_NONE;
803 
804     counters.mTxSuccess = otCounters->mTxSuccess;
805     counters.mTxFailure = otCounters->mTxFailure;
806     counters.mRxSuccess = otCounters->mRxSuccess;
807     counters.mRxFailure = otCounters->mRxFailure;
808 
809     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, counters) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
810 
811 exit:
812     return error;
813 }
814 
GetSupportedChannelMaskHandler(DBusMessageIter & aIter)815 otError DBusThreadObject::GetSupportedChannelMaskHandler(DBusMessageIter &aIter)
816 {
817     auto     threadHelper = mNcp->GetThreadHelper();
818     uint32_t channelMask  = otLinkGetSupportedChannelMask(threadHelper->GetInstance());
819     otError  error        = OT_ERROR_NONE;
820 
821     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, channelMask) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
822 
823 exit:
824     return error;
825 }
826 
GetPreferredChannelMaskHandler(DBusMessageIter & aIter)827 otError DBusThreadObject::GetPreferredChannelMaskHandler(DBusMessageIter &aIter)
828 {
829     auto     threadHelper = mNcp->GetThreadHelper();
830     uint32_t channelMask  = otPlatRadioGetPreferredChannelMask(threadHelper->GetInstance());
831     otError  error        = OT_ERROR_NONE;
832 
833     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, channelMask) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
834 
835 exit:
836     return error;
837 }
838 
GetRloc16Handler(DBusMessageIter & aIter)839 otError DBusThreadObject::GetRloc16Handler(DBusMessageIter &aIter)
840 {
841     auto     threadHelper = mNcp->GetThreadHelper();
842     otError  error        = OT_ERROR_NONE;
843     uint16_t rloc16       = otThreadGetRloc16(threadHelper->GetInstance());
844 
845     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, rloc16) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
846 
847 exit:
848     return error;
849 }
850 
GetExtendedAddressHandler(DBusMessageIter & aIter)851 otError DBusThreadObject::GetExtendedAddressHandler(DBusMessageIter &aIter)
852 {
853     auto                threadHelper    = mNcp->GetThreadHelper();
854     otError             error           = OT_ERROR_NONE;
855     const otExtAddress *addr            = otLinkGetExtendedAddress(threadHelper->GetInstance());
856     uint64_t            extendedAddress = ConvertOpenThreadUint64(addr->m8);
857 
858     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, extendedAddress) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
859 
860 exit:
861     return error;
862 }
863 
GetRouterIdHandler(DBusMessageIter & aIter)864 otError DBusThreadObject::GetRouterIdHandler(DBusMessageIter &aIter)
865 {
866     auto         threadHelper = mNcp->GetThreadHelper();
867     otError      error        = OT_ERROR_NONE;
868     uint16_t     rloc16       = otThreadGetRloc16(threadHelper->GetInstance());
869     otRouterInfo info;
870 
871     VerifyOrExit(otThreadGetRouterInfo(threadHelper->GetInstance(), rloc16, &info) == OT_ERROR_NONE,
872                  error = OT_ERROR_INVALID_STATE);
873     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, info.mRouterId) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
874 
875 exit:
876     return error;
877 }
878 
GetLeaderDataHandler(DBusMessageIter & aIter)879 otError DBusThreadObject::GetLeaderDataHandler(DBusMessageIter &aIter)
880 {
881     auto                threadHelper = mNcp->GetThreadHelper();
882     otError             error        = OT_ERROR_NONE;
883     struct otLeaderData data;
884     LeaderData          leaderData;
885 
886     SuccessOrExit(error = otThreadGetLeaderData(threadHelper->GetInstance(), &data));
887     leaderData.mPartitionId       = data.mPartitionId;
888     leaderData.mWeighting         = data.mWeighting;
889     leaderData.mDataVersion       = data.mDataVersion;
890     leaderData.mStableDataVersion = data.mStableDataVersion;
891     leaderData.mLeaderRouterId    = data.mLeaderRouterId;
892     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, leaderData) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
893 
894 exit:
895     return error;
896 }
897 
GetNetworkDataHandler(DBusMessageIter & aIter)898 otError DBusThreadObject::GetNetworkDataHandler(DBusMessageIter &aIter)
899 {
900     static constexpr size_t kNetworkDataMaxSize = 255;
901     auto                    threadHelper        = mNcp->GetThreadHelper();
902     otError                 error               = OT_ERROR_NONE;
903     uint8_t                 data[kNetworkDataMaxSize];
904     uint8_t                 len = sizeof(data);
905     std::vector<uint8_t>    networkData;
906 
907     SuccessOrExit(error = otNetDataGet(threadHelper->GetInstance(), /*stable=*/false, data, &len));
908     networkData = std::vector<uint8_t>(&data[0], &data[len]);
909     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, networkData) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
910 
911 exit:
912     return error;
913 }
914 
GetStableNetworkDataHandler(DBusMessageIter & aIter)915 otError DBusThreadObject::GetStableNetworkDataHandler(DBusMessageIter &aIter)
916 {
917     static constexpr size_t kNetworkDataMaxSize = 255;
918     auto                    threadHelper        = mNcp->GetThreadHelper();
919     otError                 error               = OT_ERROR_NONE;
920     uint8_t                 data[kNetworkDataMaxSize];
921     uint8_t                 len = sizeof(data);
922     std::vector<uint8_t>    networkData;
923 
924     SuccessOrExit(error = otNetDataGet(threadHelper->GetInstance(), /*stable=*/true, data, &len));
925     networkData = std::vector<uint8_t>(&data[0], &data[len]);
926     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, networkData) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
927 
928 exit:
929     return error;
930 }
931 
GetLocalLeaderWeightHandler(DBusMessageIter & aIter)932 otError DBusThreadObject::GetLocalLeaderWeightHandler(DBusMessageIter &aIter)
933 {
934     auto    threadHelper = mNcp->GetThreadHelper();
935     otError error        = OT_ERROR_NONE;
936     uint8_t weight       = otThreadGetLocalLeaderWeight(threadHelper->GetInstance());
937 
938     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, weight) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
939 
940 exit:
941     return error;
942 }
943 
GetChannelMonitorSampleCountHandler(DBusMessageIter & aIter)944 otError DBusThreadObject::GetChannelMonitorSampleCountHandler(DBusMessageIter &aIter)
945 {
946 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
947     auto     threadHelper = mNcp->GetThreadHelper();
948     otError  error        = OT_ERROR_NONE;
949     uint32_t cnt          = otChannelMonitorGetSampleCount(threadHelper->GetInstance());
950 
951     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, cnt) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
952 
953 exit:
954     return error;
955 #else  // OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
956     OTBR_UNUSED_VARIABLE(aIter);
957     return OT_ERROR_NOT_IMPLEMENTED;
958 #endif // OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
959 }
960 
GetChannelMonitorAllChannelQualities(DBusMessageIter & aIter)961 otError DBusThreadObject::GetChannelMonitorAllChannelQualities(DBusMessageIter &aIter)
962 {
963 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
964     auto                        threadHelper = mNcp->GetThreadHelper();
965     otError                     error        = OT_ERROR_NONE;
966     uint32_t                    channelMask  = otLinkGetSupportedChannelMask(threadHelper->GetInstance());
967     constexpr uint8_t           kNumChannels = sizeof(channelMask) * 8; // 8 bit per byte
968     std::vector<ChannelQuality> quality;
969 
970     for (uint8_t i = 0; i < kNumChannels; i++)
971     {
972         if (channelMask & (1U << i))
973         {
974             uint16_t occupancy = otChannelMonitorGetChannelOccupancy(threadHelper->GetInstance(), i);
975 
976             quality.emplace_back(ChannelQuality{i, occupancy});
977         }
978     }
979 
980     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, quality) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
981 
982 exit:
983     return error;
984 #else  // OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
985     OTBR_UNUSED_VARIABLE(aIter);
986     return OT_ERROR_NOT_IMPLEMENTED;
987 #endif // OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
988 }
989 
GetChildTableHandler(DBusMessageIter & aIter)990 otError DBusThreadObject::GetChildTableHandler(DBusMessageIter &aIter)
991 {
992     auto                   threadHelper = mNcp->GetThreadHelper();
993     otError                error        = OT_ERROR_NONE;
994     uint16_t               childIndex   = 0;
995     otChildInfo            childInfo;
996     std::vector<ChildInfo> childTable;
997 
998     while (otThreadGetChildInfoByIndex(threadHelper->GetInstance(), childIndex, &childInfo) == OT_ERROR_NONE)
999     {
1000         ChildInfo info;
1001 
1002         info.mExtAddress         = ConvertOpenThreadUint64(childInfo.mExtAddress.m8);
1003         info.mTimeout            = childInfo.mTimeout;
1004         info.mAge                = childInfo.mAge;
1005         info.mChildId            = childInfo.mChildId;
1006         info.mNetworkDataVersion = childInfo.mNetworkDataVersion;
1007         info.mLinkQualityIn      = childInfo.mLinkQualityIn;
1008         info.mAverageRssi        = childInfo.mAverageRssi;
1009         info.mLastRssi           = childInfo.mLastRssi;
1010         info.mFrameErrorRate     = childInfo.mFrameErrorRate;
1011         info.mMessageErrorRate   = childInfo.mMessageErrorRate;
1012         info.mRxOnWhenIdle       = childInfo.mRxOnWhenIdle;
1013         info.mFullThreadDevice   = childInfo.mFullThreadDevice;
1014         info.mFullNetworkData    = childInfo.mFullNetworkData;
1015         info.mIsStateRestoring   = childInfo.mIsStateRestoring;
1016         childTable.push_back(info);
1017         childIndex++;
1018     }
1019 
1020     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, childTable) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1021 
1022 exit:
1023     return error;
1024 }
1025 
GetNeighborTableHandler(DBusMessageIter & aIter)1026 otError DBusThreadObject::GetNeighborTableHandler(DBusMessageIter &aIter)
1027 {
1028     auto                      threadHelper = mNcp->GetThreadHelper();
1029     otError                   error        = OT_ERROR_NONE;
1030     otNeighborInfoIterator    iter         = OT_NEIGHBOR_INFO_ITERATOR_INIT;
1031     otNeighborInfo            neighborInfo;
1032     std::vector<NeighborInfo> neighborTable;
1033 
1034     while (otThreadGetNextNeighborInfo(threadHelper->GetInstance(), &iter, &neighborInfo) == OT_ERROR_NONE)
1035     {
1036         NeighborInfo info;
1037 
1038         info.mExtAddress       = ConvertOpenThreadUint64(neighborInfo.mExtAddress.m8);
1039         info.mAge              = neighborInfo.mAge;
1040         info.mRloc16           = neighborInfo.mRloc16;
1041         info.mLinkFrameCounter = neighborInfo.mLinkFrameCounter;
1042         info.mMleFrameCounter  = neighborInfo.mMleFrameCounter;
1043         info.mLinkQualityIn    = neighborInfo.mLinkQualityIn;
1044         info.mAverageRssi      = neighborInfo.mAverageRssi;
1045         info.mLastRssi         = neighborInfo.mLastRssi;
1046         info.mFrameErrorRate   = neighborInfo.mFrameErrorRate;
1047         info.mMessageErrorRate = neighborInfo.mMessageErrorRate;
1048         info.mVersion          = neighborInfo.mVersion;
1049         info.mRxOnWhenIdle     = neighborInfo.mRxOnWhenIdle;
1050         info.mFullThreadDevice = neighborInfo.mFullThreadDevice;
1051         info.mFullNetworkData  = neighborInfo.mFullNetworkData;
1052         info.mIsChild          = neighborInfo.mIsChild;
1053         neighborTable.push_back(info);
1054     }
1055 
1056     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, neighborTable) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1057 
1058 exit:
1059     return error;
1060 }
1061 
GetPartitionIDHandler(DBusMessageIter & aIter)1062 otError DBusThreadObject::GetPartitionIDHandler(DBusMessageIter &aIter)
1063 {
1064     auto     threadHelper = mNcp->GetThreadHelper();
1065     otError  error        = OT_ERROR_NONE;
1066     uint32_t partitionId  = otThreadGetPartitionId(threadHelper->GetInstance());
1067 
1068     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, partitionId) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1069 
1070 exit:
1071     return error;
1072 }
1073 
GetInstantRssiHandler(DBusMessageIter & aIter)1074 otError DBusThreadObject::GetInstantRssiHandler(DBusMessageIter &aIter)
1075 {
1076     auto    threadHelper = mNcp->GetThreadHelper();
1077     otError error        = OT_ERROR_NONE;
1078     int8_t  rssi         = otPlatRadioGetRssi(threadHelper->GetInstance());
1079 
1080     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, rssi) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1081 
1082 exit:
1083     return error;
1084 }
1085 
GetRadioTxPowerHandler(DBusMessageIter & aIter)1086 otError DBusThreadObject::GetRadioTxPowerHandler(DBusMessageIter &aIter)
1087 {
1088     auto    threadHelper = mNcp->GetThreadHelper();
1089     otError error        = OT_ERROR_NONE;
1090     int8_t  txPower;
1091 
1092     SuccessOrExit(error = otPlatRadioGetTransmitPower(threadHelper->GetInstance(), &txPower));
1093 
1094     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, txPower) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1095 
1096 exit:
1097     return error;
1098 }
1099 
GetExternalRoutesHandler(DBusMessageIter & aIter)1100 otError DBusThreadObject::GetExternalRoutesHandler(DBusMessageIter &aIter)
1101 {
1102     auto                       threadHelper = mNcp->GetThreadHelper();
1103     otError                    error        = OT_ERROR_NONE;
1104     otNetworkDataIterator      iter         = OT_NETWORK_DATA_ITERATOR_INIT;
1105     otExternalRouteConfig      config;
1106     std::vector<ExternalRoute> externalRouteTable;
1107 
1108     while (otNetDataGetNextRoute(threadHelper->GetInstance(), &iter, &config) == OT_ERROR_NONE)
1109     {
1110         ExternalRoute route;
1111 
1112         route.mPrefix.mPrefix      = std::vector<uint8_t>(&config.mPrefix.mPrefix.mFields.m8[0],
1113                                                      &config.mPrefix.mPrefix.mFields.m8[OTBR_IP6_PREFIX_SIZE]);
1114         route.mPrefix.mLength      = config.mPrefix.mLength;
1115         route.mRloc16              = config.mRloc16;
1116         route.mPreference          = config.mPreference;
1117         route.mStable              = config.mStable;
1118         route.mNextHopIsThisDevice = config.mNextHopIsThisDevice;
1119         externalRouteTable.push_back(route);
1120     }
1121 
1122     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, externalRouteTable) == OTBR_ERROR_NONE,
1123                  error = OT_ERROR_INVALID_ARGS);
1124 
1125 exit:
1126     return error;
1127 }
1128 
GetOnMeshPrefixesHandler(DBusMessageIter & aIter)1129 otError DBusThreadObject::GetOnMeshPrefixesHandler(DBusMessageIter &aIter)
1130 {
1131     auto                      threadHelper = mNcp->GetThreadHelper();
1132     otError                   error        = OT_ERROR_NONE;
1133     otNetworkDataIterator     iter         = OT_NETWORK_DATA_ITERATOR_INIT;
1134     otBorderRouterConfig      config;
1135     std::vector<OnMeshPrefix> onMeshPrefixes;
1136 
1137     while (otNetDataGetNextOnMeshPrefix(threadHelper->GetInstance(), &iter, &config) == OT_ERROR_NONE)
1138     {
1139         OnMeshPrefix prefix;
1140 
1141         prefix.mPrefix.mPrefix = std::vector<uint8_t>(&config.mPrefix.mPrefix.mFields.m8[0],
1142                                                       &config.mPrefix.mPrefix.mFields.m8[OTBR_IP6_PREFIX_SIZE]);
1143         prefix.mPrefix.mLength = config.mPrefix.mLength;
1144         prefix.mRloc16         = config.mRloc16;
1145         prefix.mPreference     = config.mPreference;
1146         prefix.mPreferred      = config.mPreferred;
1147         prefix.mSlaac          = config.mSlaac;
1148         prefix.mDhcp           = config.mDhcp;
1149         prefix.mConfigure      = config.mConfigure;
1150         prefix.mDefaultRoute   = config.mDefaultRoute;
1151         prefix.mOnMesh         = config.mOnMesh;
1152         prefix.mStable         = config.mStable;
1153         prefix.mNdDns          = config.mNdDns;
1154         prefix.mDp             = config.mDp;
1155         onMeshPrefixes.push_back(prefix);
1156     }
1157     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, onMeshPrefixes) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1158 
1159 exit:
1160     return error;
1161 }
1162 
SetActiveDatasetTlvsHandler(DBusMessageIter & aIter)1163 otError DBusThreadObject::SetActiveDatasetTlvsHandler(DBusMessageIter &aIter)
1164 {
1165     auto                     threadHelper = mNcp->GetThreadHelper();
1166     std::vector<uint8_t>     data;
1167     otOperationalDatasetTlvs datasetTlvs;
1168     otError                  error = OT_ERROR_NONE;
1169 
1170     VerifyOrExit(DBusMessageExtractFromVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1171     VerifyOrExit(data.size() <= sizeof(datasetTlvs.mTlvs));
1172     std::copy(std::begin(data), std::end(data), std::begin(datasetTlvs.mTlvs));
1173     datasetTlvs.mLength = data.size();
1174     error               = otDatasetSetActiveTlvs(threadHelper->GetInstance(), &datasetTlvs);
1175 
1176 exit:
1177     return error;
1178 }
1179 
GetActiveDatasetTlvsHandler(DBusMessageIter & aIter)1180 otError DBusThreadObject::GetActiveDatasetTlvsHandler(DBusMessageIter &aIter)
1181 {
1182     auto                     threadHelper = mNcp->GetThreadHelper();
1183     otError                  error        = OT_ERROR_NONE;
1184     std::vector<uint8_t>     data;
1185     otOperationalDatasetTlvs datasetTlvs;
1186 
1187     SuccessOrExit(error = otDatasetGetActiveTlvs(threadHelper->GetInstance(), &datasetTlvs));
1188     data = std::vector<uint8_t>{std::begin(datasetTlvs.mTlvs), std::begin(datasetTlvs.mTlvs) + datasetTlvs.mLength};
1189 
1190     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1191 
1192 exit:
1193     return error;
1194 }
1195 
GetPendingDatasetTlvsHandler(DBusMessageIter & aIter)1196 otError DBusThreadObject::GetPendingDatasetTlvsHandler(DBusMessageIter &aIter)
1197 {
1198     auto                     threadHelper = mNcp->GetThreadHelper();
1199     otError                  error        = OT_ERROR_NONE;
1200     std::vector<uint8_t>     data;
1201     otOperationalDatasetTlvs datasetTlvs;
1202 
1203     SuccessOrExit(error = otDatasetGetPendingTlvs(threadHelper->GetInstance(), &datasetTlvs));
1204     data = std::vector<uint8_t>{std::begin(datasetTlvs.mTlvs), std::begin(datasetTlvs.mTlvs) + datasetTlvs.mLength};
1205 
1206     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1207 
1208 exit:
1209     return error;
1210 }
1211 
SetFeatureFlagListDataHandler(DBusMessageIter & aIter)1212 otError DBusThreadObject::SetFeatureFlagListDataHandler(DBusMessageIter &aIter)
1213 {
1214 #if OTBR_ENABLE_FEATURE_FLAGS
1215     otError              error = OT_ERROR_NONE;
1216     std::vector<uint8_t> data;
1217     FeatureFlagList      featureFlagList;
1218 
1219     VerifyOrExit(DBusMessageExtractFromVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1220     VerifyOrExit(featureFlagList.ParseFromString(std::string(data.begin(), data.end())), error = OT_ERROR_INVALID_ARGS);
1221     VerifyOrExit((error = mNcp->ApplyFeatureFlagList(featureFlagList)) == OT_ERROR_NONE);
1222 exit:
1223     return error;
1224 #else
1225     OTBR_UNUSED_VARIABLE(aIter);
1226     return OT_ERROR_NOT_IMPLEMENTED;
1227 #endif
1228 }
1229 
GetFeatureFlagListDataHandler(DBusMessageIter & aIter)1230 otError DBusThreadObject::GetFeatureFlagListDataHandler(DBusMessageIter &aIter)
1231 {
1232 #if OTBR_ENABLE_FEATURE_FLAGS
1233     otError              error                       = OT_ERROR_NONE;
1234     const std::string    appliedFeatureFlagListBytes = mNcp->GetAppliedFeatureFlagListBytes();
1235     std::vector<uint8_t> data(appliedFeatureFlagListBytes.begin(), appliedFeatureFlagListBytes.end());
1236 
1237     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1238 
1239 exit:
1240     return error;
1241 #else
1242     OTBR_UNUSED_VARIABLE(aIter);
1243     return OT_ERROR_NOT_IMPLEMENTED;
1244 #endif
1245 }
1246 
SetRadioRegionHandler(DBusMessageIter & aIter)1247 otError DBusThreadObject::SetRadioRegionHandler(DBusMessageIter &aIter)
1248 {
1249     auto        threadHelper = mNcp->GetThreadHelper();
1250     std::string radioRegion;
1251     uint16_t    regionCode;
1252     otError     error = OT_ERROR_NONE;
1253 
1254     VerifyOrExit(DBusMessageExtractFromVariant(&aIter, radioRegion) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1255     VerifyOrExit(radioRegion.size() == sizeof(uint16_t), error = OT_ERROR_INVALID_ARGS);
1256     regionCode = radioRegion[0] << 8 | radioRegion[1];
1257 
1258     error = otPlatRadioSetRegion(threadHelper->GetInstance(), regionCode);
1259 
1260 exit:
1261     return error;
1262 }
1263 
UpdateMeshCopTxtHandler(DBusRequest & aRequest)1264 void DBusThreadObject::UpdateMeshCopTxtHandler(DBusRequest &aRequest)
1265 {
1266     auto                                        threadHelper = mNcp->GetThreadHelper();
1267     otError                                     error        = OT_ERROR_NONE;
1268     std::map<std::string, std::vector<uint8_t>> update;
1269     std::vector<TxtEntry>                       updatedTxtEntries;
1270     auto                                        args = std::tie(updatedTxtEntries);
1271 
1272     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1273     for (const auto &entry : updatedTxtEntries)
1274     {
1275         update[entry.mKey] = entry.mValue;
1276     }
1277     for (const auto reservedKey : {"rv", "tv", "sb", "nn", "xp", "at", "pt", "dn", "sq", "bb", "omr"})
1278     {
1279         VerifyOrExit(!update.count(reservedKey), error = OT_ERROR_INVALID_ARGS);
1280     }
1281     threadHelper->OnUpdateMeshCopTxt(std::move(update));
1282 
1283 exit:
1284     aRequest.ReplyOtResult(error);
1285 }
1286 
GetRadioRegionHandler(DBusMessageIter & aIter)1287 otError DBusThreadObject::GetRadioRegionHandler(DBusMessageIter &aIter)
1288 {
1289     auto        threadHelper = mNcp->GetThreadHelper();
1290     otError     error        = OT_ERROR_NONE;
1291     std::string radioRegion;
1292     uint16_t    regionCode;
1293 
1294     SuccessOrExit(error = otPlatRadioGetRegion(threadHelper->GetInstance(), &regionCode));
1295     radioRegion.resize(sizeof(uint16_t), '\0');
1296     radioRegion[0] = static_cast<char>((regionCode >> 8) & 0xff);
1297     radioRegion[1] = static_cast<char>(regionCode & 0xff);
1298 
1299     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, radioRegion) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1300 
1301 exit:
1302     return error;
1303 }
1304 
GetSrpServerInfoHandler(DBusMessageIter & aIter)1305 otError DBusThreadObject::GetSrpServerInfoHandler(DBusMessageIter &aIter)
1306 {
1307 #if OTBR_ENABLE_SRP_ADVERTISING_PROXY
1308     auto                               threadHelper = mNcp->GetThreadHelper();
1309     auto                               instance     = threadHelper->GetInstance();
1310     otError                            error        = OT_ERROR_NONE;
1311     SrpServerInfo                      srpServerInfo{};
1312     otSrpServerLeaseInfo               leaseInfo;
1313     const otSrpServerHost             *host             = nullptr;
1314     const otSrpServerResponseCounters *responseCounters = otSrpServerGetResponseCounters(instance);
1315 
1316     srpServerInfo.mState       = SrpServerState(static_cast<uint8_t>(otSrpServerGetState(instance)));
1317     srpServerInfo.mPort        = otSrpServerGetPort(instance);
1318     srpServerInfo.mAddressMode = SrpServerAddressMode(static_cast<uint8_t>(otSrpServerGetAddressMode(instance)));
1319 
1320     while ((host = otSrpServerGetNextHost(instance, host)))
1321     {
1322         const otSrpServerService *service = nullptr;
1323 
1324         if (otSrpServerHostIsDeleted(host))
1325         {
1326             ++srpServerInfo.mHosts.mDeletedCount;
1327         }
1328         else
1329         {
1330             ++srpServerInfo.mHosts.mFreshCount;
1331             otSrpServerHostGetLeaseInfo(host, &leaseInfo);
1332             srpServerInfo.mHosts.mLeaseTimeTotal += leaseInfo.mLease;
1333             srpServerInfo.mHosts.mKeyLeaseTimeTotal += leaseInfo.mKeyLease;
1334             srpServerInfo.mHosts.mRemainingLeaseTimeTotal += leaseInfo.mRemainingLease;
1335             srpServerInfo.mHosts.mRemainingKeyLeaseTimeTotal += leaseInfo.mRemainingKeyLease;
1336         }
1337 
1338         while ((service = otSrpServerHostGetNextService(host, service)))
1339         {
1340             if (otSrpServerServiceIsDeleted(service))
1341             {
1342                 ++srpServerInfo.mServices.mDeletedCount;
1343             }
1344             else
1345             {
1346                 ++srpServerInfo.mServices.mFreshCount;
1347                 otSrpServerServiceGetLeaseInfo(service, &leaseInfo);
1348                 srpServerInfo.mServices.mLeaseTimeTotal += leaseInfo.mLease;
1349                 srpServerInfo.mServices.mKeyLeaseTimeTotal += leaseInfo.mKeyLease;
1350                 srpServerInfo.mServices.mRemainingLeaseTimeTotal += leaseInfo.mRemainingLease;
1351                 srpServerInfo.mServices.mRemainingKeyLeaseTimeTotal += leaseInfo.mRemainingKeyLease;
1352             }
1353         }
1354     }
1355 
1356     srpServerInfo.mResponseCounters.mSuccess       = responseCounters->mSuccess;
1357     srpServerInfo.mResponseCounters.mServerFailure = responseCounters->mServerFailure;
1358     srpServerInfo.mResponseCounters.mFormatError   = responseCounters->mFormatError;
1359     srpServerInfo.mResponseCounters.mNameExists    = responseCounters->mNameExists;
1360     srpServerInfo.mResponseCounters.mRefused       = responseCounters->mRefused;
1361     srpServerInfo.mResponseCounters.mOther         = responseCounters->mOther;
1362 
1363     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, srpServerInfo) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1364 
1365 exit:
1366     return error;
1367 #else  // OTBR_ENABLE_SRP_ADVERTISING_PROXY
1368     OTBR_UNUSED_VARIABLE(aIter);
1369 
1370     return OT_ERROR_NOT_IMPLEMENTED;
1371 #endif // OTBR_ENABLE_SRP_ADVERTISING_PROXY
1372 }
1373 
GetMdnsTelemetryInfoHandler(DBusMessageIter & aIter)1374 otError DBusThreadObject::GetMdnsTelemetryInfoHandler(DBusMessageIter &aIter)
1375 {
1376     otError error = OT_ERROR_NONE;
1377 
1378     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, mPublisher->GetMdnsTelemetryInfo()) == OTBR_ERROR_NONE,
1379                  error = OT_ERROR_INVALID_ARGS);
1380 exit:
1381     return error;
1382 }
1383 
GetDnssdCountersHandler(DBusMessageIter & aIter)1384 otError DBusThreadObject::GetDnssdCountersHandler(DBusMessageIter &aIter)
1385 {
1386 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
1387     auto            threadHelper = mNcp->GetThreadHelper();
1388     auto            instance     = threadHelper->GetInstance();
1389     otError         error        = OT_ERROR_NONE;
1390     DnssdCounters   dnssdCounters;
1391     otDnssdCounters otDnssdCounters = *otDnssdGetCounters(instance);
1392 
1393     dnssdCounters.mSuccessResponse        = otDnssdCounters.mSuccessResponse;
1394     dnssdCounters.mServerFailureResponse  = otDnssdCounters.mServerFailureResponse;
1395     dnssdCounters.mFormatErrorResponse    = otDnssdCounters.mFormatErrorResponse;
1396     dnssdCounters.mNameErrorResponse      = otDnssdCounters.mNameErrorResponse;
1397     dnssdCounters.mNotImplementedResponse = otDnssdCounters.mNotImplementedResponse;
1398     dnssdCounters.mOtherResponse          = otDnssdCounters.mOtherResponse;
1399 
1400     dnssdCounters.mResolvedBySrp = otDnssdCounters.mResolvedBySrp;
1401 
1402     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, dnssdCounters) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1403 
1404 exit:
1405     return error;
1406 #else  // OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
1407     OTBR_UNUSED_VARIABLE(aIter);
1408 
1409     return OT_ERROR_NOT_IMPLEMENTED;
1410 #endif // OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
1411 }
1412 
GetTrelInfoHandler(DBusMessageIter & aIter)1413 otError DBusThreadObject::GetTrelInfoHandler(DBusMessageIter &aIter)
1414 {
1415 #if OTBR_ENABLE_TREL
1416     auto           instance = mNcp->GetThreadHelper()->GetInstance();
1417     otError        error    = OT_ERROR_NONE;
1418     TrelInfo       trelInfo;
1419     otTrelCounters otTrelCounters = *otTrelGetCounters(instance);
1420 
1421     trelInfo.mTrelCounters.mTxPackets = otTrelCounters.mTxPackets;
1422     trelInfo.mTrelCounters.mTxBytes   = otTrelCounters.mTxBytes;
1423     trelInfo.mTrelCounters.mTxFailure = otTrelCounters.mTxFailure;
1424     trelInfo.mTrelCounters.mRxPackets = otTrelCounters.mRxPackets;
1425     trelInfo.mTrelCounters.mRxBytes   = otTrelCounters.mRxBytes;
1426 
1427     trelInfo.mNumTrelPeers = otTrelGetNumberOfPeers(instance);
1428     trelInfo.mEnabled      = otTrelIsEnabled(instance);
1429 
1430     SuccessOrExit(DBusMessageEncodeToVariant(&aIter, trelInfo), error = OT_ERROR_INVALID_ARGS);
1431 exit:
1432     return error;
1433 #else  // OTBR_ENABLE_TREL
1434     OTBR_UNUSED_VARIABLE(aIter);
1435 
1436     return OT_ERROR_NOT_IMPLEMENTED;
1437 #endif // OTBR_ENABLE_TREL
1438 }
1439 
GetTelemetryDataHandler(DBusMessageIter & aIter)1440 otError DBusThreadObject::GetTelemetryDataHandler(DBusMessageIter &aIter)
1441 {
1442 #if OTBR_ENABLE_TELEMETRY_DATA_API
1443     otError                      error = OT_ERROR_NONE;
1444     threadnetwork::TelemetryData telemetryData;
1445     auto                         threadHelper = mNcp->GetThreadHelper();
1446 
1447     if (threadHelper->RetrieveTelemetryData(mPublisher, telemetryData) != OT_ERROR_NONE)
1448     {
1449         otbrLogWarning("Some metrics were not populated in RetrieveTelemetryData");
1450     }
1451 
1452     {
1453         const std::string    telemetryDataBytes = telemetryData.SerializeAsString();
1454         std::vector<uint8_t> data(telemetryDataBytes.begin(), telemetryDataBytes.end());
1455 
1456         VerifyOrExit(DBusMessageEncodeToVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1457     }
1458 
1459 exit:
1460     return error;
1461 #else
1462     OTBR_UNUSED_VARIABLE(aIter);
1463     return OT_ERROR_NOT_IMPLEMENTED;
1464 #endif
1465 }
1466 
GetCapabilitiesHandler(DBusMessageIter & aIter)1467 otError DBusThreadObject::GetCapabilitiesHandler(DBusMessageIter &aIter)
1468 {
1469     otError            error = OT_ERROR_NONE;
1470     otbr::Capabilities capabilities;
1471 
1472     capabilities.set_nat64(OTBR_ENABLE_NAT64);
1473     capabilities.set_dhcp6_pd(OTBR_ENABLE_DHCP6_PD);
1474 
1475     {
1476         const std::string    dataBytes = capabilities.SerializeAsString();
1477         std::vector<uint8_t> data(dataBytes.begin(), dataBytes.end());
1478 
1479         VerifyOrExit(DBusMessageEncodeToVariant(&aIter, data) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1480     }
1481 
1482 exit:
1483     return error;
1484 }
1485 
GetPropertiesHandler(DBusRequest & aRequest)1486 void DBusThreadObject::GetPropertiesHandler(DBusRequest &aRequest)
1487 {
1488     UniqueDBusMessage        reply(dbus_message_new_method_return(aRequest.GetMessage()));
1489     DBusMessageIter          iter;
1490     DBusMessageIter          replyIter;
1491     DBusMessageIter          replySubIter;
1492     std::vector<std::string> propertyNames;
1493     otError                  error = OT_ERROR_NONE;
1494 
1495     VerifyOrExit(reply != nullptr, error = OT_ERROR_NO_BUFS);
1496     VerifyOrExit(dbus_message_iter_init(aRequest.GetMessage(), &iter), error = OT_ERROR_FAILED);
1497     VerifyOrExit(DBusMessageExtract(&iter, propertyNames) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);
1498 
1499     dbus_message_iter_init_append(reply.get(), &replyIter);
1500     VerifyOrExit(
1501         dbus_message_iter_open_container(&replyIter, DBUS_TYPE_ARRAY, DBUS_TYPE_VARIANT_AS_STRING, &replySubIter),
1502         error = OT_ERROR_NO_BUFS);
1503 
1504     for (const std::string &propertyName : propertyNames)
1505     {
1506         auto handlerIter = mGetPropertyHandlers.find(propertyName);
1507 
1508         otbrLogInfo("GetPropertiesHandler getting property: %s", propertyName.c_str());
1509         VerifyOrExit(handlerIter != mGetPropertyHandlers.end(), error = OT_ERROR_NOT_FOUND);
1510 
1511         SuccessOrExit(error = handlerIter->second(replySubIter));
1512     }
1513 
1514     VerifyOrExit(dbus_message_iter_close_container(&replyIter, &replySubIter), error = OT_ERROR_NO_BUFS);
1515 
1516 exit:
1517     if (error == OT_ERROR_NONE)
1518     {
1519         dbus_connection_send(aRequest.GetConnection(), reply.get(), nullptr);
1520     }
1521     else
1522     {
1523         aRequest.ReplyOtResult(error);
1524     }
1525 }
1526 
RegisterGetPropertyHandler(const std::string & aInterfaceName,const std::string & aPropertyName,const PropertyHandlerType & aHandler)1527 void DBusThreadObject::RegisterGetPropertyHandler(const std::string         &aInterfaceName,
1528                                                   const std::string         &aPropertyName,
1529                                                   const PropertyHandlerType &aHandler)
1530 {
1531     DBusObject::RegisterGetPropertyHandler(aInterfaceName, aPropertyName, aHandler);
1532     mGetPropertyHandlers[aPropertyName] = aHandler;
1533 }
1534 
GetOtbrVersionHandler(DBusMessageIter & aIter)1535 otError DBusThreadObject::GetOtbrVersionHandler(DBusMessageIter &aIter)
1536 {
1537     otError     error   = OT_ERROR_NONE;
1538     std::string version = OTBR_PACKAGE_VERSION;
1539 
1540     SuccessOrExit(DBusMessageEncodeToVariant(&aIter, version), error = OT_ERROR_FAILED);
1541 
1542 exit:
1543     return error;
1544 }
1545 
GetOtHostVersionHandler(DBusMessageIter & aIter)1546 otError DBusThreadObject::GetOtHostVersionHandler(DBusMessageIter &aIter)
1547 {
1548     otError     error   = OT_ERROR_NONE;
1549     std::string version = otGetVersionString();
1550 
1551     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, version) == OTBR_ERROR_NONE, error = OT_ERROR_FAILED);
1552 
1553 exit:
1554     return error;
1555 }
1556 
GetEui64Handler(DBusMessageIter & aIter)1557 otError DBusThreadObject::GetEui64Handler(DBusMessageIter &aIter)
1558 {
1559     auto         threadHelper = mNcp->GetThreadHelper();
1560     otError      error        = OT_ERROR_NONE;
1561     otExtAddress extAddr;
1562     uint64_t     eui64;
1563 
1564     otLinkGetFactoryAssignedIeeeEui64(threadHelper->GetInstance(), &extAddr);
1565 
1566     eui64 = ConvertOpenThreadUint64(extAddr.m8);
1567 
1568     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, eui64) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1569 
1570 exit:
1571     return error;
1572 }
1573 
GetOtRcpVersionHandler(DBusMessageIter & aIter)1574 otError DBusThreadObject::GetOtRcpVersionHandler(DBusMessageIter &aIter)
1575 {
1576     auto        threadHelper = mNcp->GetThreadHelper();
1577     otError     error        = OT_ERROR_NONE;
1578     std::string version      = otGetRadioVersionString(threadHelper->GetInstance());
1579 
1580     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, version) == OTBR_ERROR_NONE, error = OT_ERROR_FAILED);
1581 
1582 exit:
1583     return error;
1584 }
1585 
GetThreadVersionHandler(DBusMessageIter & aIter)1586 otError DBusThreadObject::GetThreadVersionHandler(DBusMessageIter &aIter)
1587 {
1588     otError error = OT_ERROR_NONE;
1589 
1590     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, otThreadGetVersion()) == OTBR_ERROR_NONE, error = OT_ERROR_FAILED);
1591 
1592 exit:
1593     return error;
1594 }
1595 
GetRadioSpinelMetricsHandler(DBusMessageIter & aIter)1596 otError DBusThreadObject::GetRadioSpinelMetricsHandler(DBusMessageIter &aIter)
1597 {
1598     otError              error = OT_ERROR_NONE;
1599     RadioSpinelMetrics   radioSpinelMetrics;
1600     otRadioSpinelMetrics otRadioSpinelMetrics = *otSysGetRadioSpinelMetrics();
1601 
1602     radioSpinelMetrics.mRcpTimeoutCount         = otRadioSpinelMetrics.mRcpTimeoutCount;
1603     radioSpinelMetrics.mRcpUnexpectedResetCount = otRadioSpinelMetrics.mRcpUnexpectedResetCount;
1604     radioSpinelMetrics.mRcpRestorationCount     = otRadioSpinelMetrics.mRcpRestorationCount;
1605     radioSpinelMetrics.mSpinelParseErrorCount   = otRadioSpinelMetrics.mSpinelParseErrorCount;
1606 
1607     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, radioSpinelMetrics) == OTBR_ERROR_NONE,
1608                  error = OT_ERROR_INVALID_ARGS);
1609 
1610 exit:
1611     return error;
1612 }
1613 
GetRcpInterfaceMetricsHandler(DBusMessageIter & aIter)1614 otError DBusThreadObject::GetRcpInterfaceMetricsHandler(DBusMessageIter &aIter)
1615 {
1616     otError               error = OT_ERROR_NONE;
1617     RcpInterfaceMetrics   rcpInterfaceMetrics;
1618     otRcpInterfaceMetrics otRcpInterfaceMetrics = *otSysGetRcpInterfaceMetrics();
1619 
1620     rcpInterfaceMetrics.mRcpInterfaceType             = otRcpInterfaceMetrics.mRcpInterfaceType;
1621     rcpInterfaceMetrics.mTransferredFrameCount        = otRcpInterfaceMetrics.mTransferredFrameCount;
1622     rcpInterfaceMetrics.mTransferredValidFrameCount   = otRcpInterfaceMetrics.mTransferredValidFrameCount;
1623     rcpInterfaceMetrics.mTransferredGarbageFrameCount = otRcpInterfaceMetrics.mTransferredGarbageFrameCount;
1624     rcpInterfaceMetrics.mRxFrameCount                 = otRcpInterfaceMetrics.mRxFrameCount;
1625     rcpInterfaceMetrics.mRxFrameByteCount             = otRcpInterfaceMetrics.mRxFrameByteCount;
1626     rcpInterfaceMetrics.mTxFrameCount                 = otRcpInterfaceMetrics.mTxFrameCount;
1627     rcpInterfaceMetrics.mTxFrameByteCount             = otRcpInterfaceMetrics.mTxFrameByteCount;
1628 
1629     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, rcpInterfaceMetrics) == OTBR_ERROR_NONE,
1630                  error = OT_ERROR_INVALID_ARGS);
1631 
1632 exit:
1633     return error;
1634 }
1635 
GetUptimeHandler(DBusMessageIter & aIter)1636 otError DBusThreadObject::GetUptimeHandler(DBusMessageIter &aIter)
1637 {
1638     otError error = OT_ERROR_NONE;
1639 
1640     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, otInstanceGetUptime(mNcp->GetThreadHelper()->GetInstance())) ==
1641                      OTBR_ERROR_NONE,
1642                  error = OT_ERROR_INVALID_ARGS);
1643 
1644 exit:
1645     return error;
1646 }
1647 
GetRadioCoexMetrics(DBusMessageIter & aIter)1648 otError DBusThreadObject::GetRadioCoexMetrics(DBusMessageIter &aIter)
1649 {
1650     otError            error = OT_ERROR_NONE;
1651     otRadioCoexMetrics otRadioCoexMetrics;
1652     RadioCoexMetrics   radioCoexMetrics;
1653 
1654     SuccessOrExit(error = otPlatRadioGetCoexMetrics(mNcp->GetInstance(), &otRadioCoexMetrics));
1655 
1656     radioCoexMetrics.mNumGrantGlitch                     = otRadioCoexMetrics.mNumGrantGlitch;
1657     radioCoexMetrics.mNumTxRequest                       = otRadioCoexMetrics.mNumTxRequest;
1658     radioCoexMetrics.mNumTxGrantImmediate                = otRadioCoexMetrics.mNumTxGrantImmediate;
1659     radioCoexMetrics.mNumTxGrantWait                     = otRadioCoexMetrics.mNumTxGrantWait;
1660     radioCoexMetrics.mNumTxGrantWaitActivated            = otRadioCoexMetrics.mNumTxGrantWaitActivated;
1661     radioCoexMetrics.mNumTxGrantWaitTimeout              = otRadioCoexMetrics.mNumTxGrantWaitTimeout;
1662     radioCoexMetrics.mNumTxGrantDeactivatedDuringRequest = otRadioCoexMetrics.mNumTxGrantDeactivatedDuringRequest;
1663     radioCoexMetrics.mNumTxDelayedGrant                  = otRadioCoexMetrics.mNumTxDelayedGrant;
1664     radioCoexMetrics.mAvgTxRequestToGrantTime            = otRadioCoexMetrics.mAvgTxRequestToGrantTime;
1665     radioCoexMetrics.mNumRxRequest                       = otRadioCoexMetrics.mNumRxRequest;
1666     radioCoexMetrics.mNumRxGrantImmediate                = otRadioCoexMetrics.mNumRxGrantImmediate;
1667     radioCoexMetrics.mNumRxGrantWait                     = otRadioCoexMetrics.mNumRxGrantWait;
1668     radioCoexMetrics.mNumRxGrantWaitActivated            = otRadioCoexMetrics.mNumRxGrantWaitActivated;
1669     radioCoexMetrics.mNumRxGrantWaitTimeout              = otRadioCoexMetrics.mNumRxGrantWaitTimeout;
1670     radioCoexMetrics.mNumRxGrantDeactivatedDuringRequest = otRadioCoexMetrics.mNumRxGrantDeactivatedDuringRequest;
1671     radioCoexMetrics.mNumRxDelayedGrant                  = otRadioCoexMetrics.mNumRxDelayedGrant;
1672     radioCoexMetrics.mAvgRxRequestToGrantTime            = otRadioCoexMetrics.mAvgRxRequestToGrantTime;
1673     radioCoexMetrics.mNumRxGrantNone                     = otRadioCoexMetrics.mNumRxGrantNone;
1674     radioCoexMetrics.mStopped                            = otRadioCoexMetrics.mStopped;
1675 
1676     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, radioCoexMetrics) == OTBR_ERROR_NONE,
1677                  error = OT_ERROR_INVALID_ARGS);
1678 
1679 exit:
1680     return error;
1681 }
1682 
GetBorderRoutingCountersHandler(DBusMessageIter & aIter)1683 otError DBusThreadObject::GetBorderRoutingCountersHandler(DBusMessageIter &aIter)
1684 {
1685 #if OTBR_ENABLE_BORDER_ROUTING_COUNTERS
1686     auto                           threadHelper = mNcp->GetThreadHelper();
1687     auto                           instance     = threadHelper->GetInstance();
1688     otError                        error        = OT_ERROR_NONE;
1689     BorderRoutingCounters          borderRoutingCounters;
1690     const otBorderRoutingCounters *otBorderRoutingCounters = otIp6GetBorderRoutingCounters(instance);
1691 
1692     borderRoutingCounters.mInboundUnicast.mPackets    = otBorderRoutingCounters->mInboundUnicast.mPackets;
1693     borderRoutingCounters.mInboundUnicast.mBytes      = otBorderRoutingCounters->mInboundUnicast.mBytes;
1694     borderRoutingCounters.mInboundMulticast.mPackets  = otBorderRoutingCounters->mInboundMulticast.mPackets;
1695     borderRoutingCounters.mInboundMulticast.mBytes    = otBorderRoutingCounters->mInboundMulticast.mBytes;
1696     borderRoutingCounters.mOutboundUnicast.mPackets   = otBorderRoutingCounters->mOutboundUnicast.mPackets;
1697     borderRoutingCounters.mOutboundUnicast.mBytes     = otBorderRoutingCounters->mOutboundUnicast.mBytes;
1698     borderRoutingCounters.mOutboundMulticast.mPackets = otBorderRoutingCounters->mOutboundMulticast.mPackets;
1699     borderRoutingCounters.mOutboundMulticast.mBytes   = otBorderRoutingCounters->mOutboundMulticast.mBytes;
1700     borderRoutingCounters.mRaRx                       = otBorderRoutingCounters->mRaRx;
1701     borderRoutingCounters.mRaTxSuccess                = otBorderRoutingCounters->mRaTxSuccess;
1702     borderRoutingCounters.mRaTxFailure                = otBorderRoutingCounters->mRaTxFailure;
1703     borderRoutingCounters.mRsRx                       = otBorderRoutingCounters->mRsRx;
1704     borderRoutingCounters.mRsTxSuccess                = otBorderRoutingCounters->mRsTxSuccess;
1705     borderRoutingCounters.mRsTxFailure                = otBorderRoutingCounters->mRsTxFailure;
1706 
1707     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, borderRoutingCounters) == OTBR_ERROR_NONE,
1708                  error = OT_ERROR_INVALID_ARGS);
1709 
1710 exit:
1711     return error;
1712 #else
1713     OTBR_UNUSED_VARIABLE(aIter);
1714 
1715     return OT_ERROR_NOT_IMPLEMENTED;
1716 #endif
1717 }
1718 
ActiveDatasetChangeHandler(const otOperationalDatasetTlvs & aDatasetTlvs)1719 void DBusThreadObject::ActiveDatasetChangeHandler(const otOperationalDatasetTlvs &aDatasetTlvs)
1720 {
1721     std::vector<uint8_t> value(aDatasetTlvs.mLength);
1722     std::copy(aDatasetTlvs.mTlvs, aDatasetTlvs.mTlvs + aDatasetTlvs.mLength, value.begin());
1723     SignalPropertyChanged(OTBR_DBUS_THREAD_INTERFACE, OTBR_DBUS_PROPERTY_ACTIVE_DATASET_TLVS, value);
1724 }
1725 
LeaveNetworkHandler(DBusRequest & aRequest)1726 void DBusThreadObject::LeaveNetworkHandler(DBusRequest &aRequest)
1727 {
1728     constexpr int kExitCodeShouldRestart = 7;
1729 
1730     mNcp->GetThreadHelper()->DetachGracefully([aRequest, this](otError error) mutable {
1731         SuccessOrExit(error);
1732         mPublisher->Stop();
1733         SuccessOrExit(error = otInstanceErasePersistentInfo(mNcp->GetThreadHelper()->GetInstance()));
1734 
1735     exit:
1736         aRequest.ReplyOtResult(error);
1737         if (error == OT_ERROR_NONE)
1738         {
1739             Flush();
1740             exit(kExitCodeShouldRestart);
1741         }
1742     });
1743 }
1744 
1745 #if OTBR_ENABLE_NAT64
SetNat64Enabled(DBusRequest & aRequest)1746 void DBusThreadObject::SetNat64Enabled(DBusRequest &aRequest)
1747 {
1748     otError error = OT_ERROR_NONE;
1749     bool    enable;
1750     auto    args = std::tie(enable);
1751 
1752     VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1753     otNat64SetEnabled(mNcp->GetThreadHelper()->GetInstance(), enable);
1754 
1755 exit:
1756     aRequest.ReplyOtResult(error);
1757 }
1758 
GetNat64State(DBusMessageIter & aIter)1759 otError DBusThreadObject::GetNat64State(DBusMessageIter &aIter)
1760 {
1761     otError error = OT_ERROR_NONE;
1762 
1763     Nat64ComponentState state;
1764 
1765     state.mPrefixManagerState = GetNat64StateName(otNat64GetPrefixManagerState(mNcp->GetThreadHelper()->GetInstance()));
1766     state.mTranslatorState    = GetNat64StateName(otNat64GetTranslatorState(mNcp->GetThreadHelper()->GetInstance()));
1767 
1768     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, state) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1769 
1770 exit:
1771     return error;
1772 }
1773 
GetNat64Mappings(DBusMessageIter & aIter)1774 otError DBusThreadObject::GetNat64Mappings(DBusMessageIter &aIter)
1775 {
1776     otError error = OT_ERROR_NONE;
1777 
1778     std::vector<Nat64AddressMapping> mappings;
1779     otNat64AddressMappingIterator    iterator;
1780     otNat64AddressMapping            otMapping;
1781     Nat64AddressMapping              mapping;
1782 
1783     otNat64InitAddressMappingIterator(mNcp->GetThreadHelper()->GetInstance(), &iterator);
1784     while (otNat64GetNextAddressMapping(mNcp->GetThreadHelper()->GetInstance(), &iterator, &otMapping) == OT_ERROR_NONE)
1785     {
1786         mapping.mId = otMapping.mId;
1787         std::copy(std::begin(otMapping.mIp4.mFields.m8), std::end(otMapping.mIp4.mFields.m8), mapping.mIp4.data());
1788         std::copy(std::begin(otMapping.mIp6.mFields.m8), std::end(otMapping.mIp6.mFields.m8), mapping.mIp6.data());
1789         mapping.mRemainingTimeMs = otMapping.mRemainingTimeMs;
1790 
1791         mapping.mCounters.mTotal.m4To6Packets = otMapping.mCounters.mTotal.m4To6Packets;
1792         mapping.mCounters.mTotal.m4To6Bytes   = otMapping.mCounters.mTotal.m4To6Bytes;
1793         mapping.mCounters.mTotal.m6To4Packets = otMapping.mCounters.mTotal.m6To4Packets;
1794         mapping.mCounters.mTotal.m6To4Bytes   = otMapping.mCounters.mTotal.m6To4Bytes;
1795 
1796         mapping.mCounters.mIcmp.m4To6Packets = otMapping.mCounters.mIcmp.m4To6Packets;
1797         mapping.mCounters.mIcmp.m4To6Bytes   = otMapping.mCounters.mIcmp.m4To6Bytes;
1798         mapping.mCounters.mIcmp.m6To4Packets = otMapping.mCounters.mIcmp.m6To4Packets;
1799         mapping.mCounters.mIcmp.m6To4Bytes   = otMapping.mCounters.mIcmp.m6To4Bytes;
1800 
1801         mapping.mCounters.mUdp.m4To6Packets = otMapping.mCounters.mUdp.m4To6Packets;
1802         mapping.mCounters.mUdp.m4To6Bytes   = otMapping.mCounters.mUdp.m4To6Bytes;
1803         mapping.mCounters.mUdp.m6To4Packets = otMapping.mCounters.mUdp.m6To4Packets;
1804         mapping.mCounters.mUdp.m6To4Bytes   = otMapping.mCounters.mUdp.m6To4Bytes;
1805 
1806         mapping.mCounters.mTcp.m4To6Packets = otMapping.mCounters.mTcp.m4To6Packets;
1807         mapping.mCounters.mTcp.m4To6Bytes   = otMapping.mCounters.mTcp.m4To6Bytes;
1808         mapping.mCounters.mTcp.m6To4Packets = otMapping.mCounters.mTcp.m6To4Packets;
1809         mapping.mCounters.mTcp.m6To4Bytes   = otMapping.mCounters.mTcp.m6To4Bytes;
1810 
1811         mappings.push_back(mapping);
1812     }
1813 
1814     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, mappings) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1815 
1816 exit:
1817     return error;
1818 }
1819 
GetNat64ProtocolCounters(DBusMessageIter & aIter)1820 otError DBusThreadObject::GetNat64ProtocolCounters(DBusMessageIter &aIter)
1821 {
1822     otError error = OT_ERROR_NONE;
1823 
1824     otNat64ProtocolCounters otCounters;
1825     Nat64ProtocolCounters   counters;
1826     otNat64GetCounters(mNcp->GetThreadHelper()->GetInstance(), &otCounters);
1827 
1828     counters.mTotal.m4To6Packets = otCounters.mTotal.m4To6Packets;
1829     counters.mTotal.m4To6Bytes   = otCounters.mTotal.m4To6Bytes;
1830     counters.mTotal.m6To4Packets = otCounters.mTotal.m6To4Packets;
1831     counters.mTotal.m6To4Bytes   = otCounters.mTotal.m6To4Bytes;
1832     counters.mIcmp.m4To6Packets  = otCounters.mIcmp.m4To6Packets;
1833     counters.mIcmp.m4To6Bytes    = otCounters.mIcmp.m4To6Bytes;
1834     counters.mIcmp.m6To4Packets  = otCounters.mIcmp.m6To4Packets;
1835     counters.mIcmp.m6To4Bytes    = otCounters.mIcmp.m6To4Bytes;
1836     counters.mUdp.m4To6Packets   = otCounters.mUdp.m4To6Packets;
1837     counters.mUdp.m4To6Bytes     = otCounters.mUdp.m4To6Bytes;
1838     counters.mUdp.m6To4Packets   = otCounters.mUdp.m6To4Packets;
1839     counters.mUdp.m6To4Bytes     = otCounters.mUdp.m6To4Bytes;
1840     counters.mTcp.m4To6Packets   = otCounters.mTcp.m4To6Packets;
1841     counters.mTcp.m4To6Bytes     = otCounters.mTcp.m4To6Bytes;
1842     counters.mTcp.m6To4Packets   = otCounters.mTcp.m6To4Packets;
1843     counters.mTcp.m6To4Bytes     = otCounters.mTcp.m6To4Bytes;
1844 
1845     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, counters) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1846 
1847 exit:
1848     return error;
1849 }
1850 
GetNat64ErrorCounters(DBusMessageIter & aIter)1851 otError DBusThreadObject::GetNat64ErrorCounters(DBusMessageIter &aIter)
1852 {
1853     otError error = OT_ERROR_NONE;
1854 
1855     otNat64ErrorCounters otCounters;
1856     Nat64ErrorCounters   counters;
1857     otNat64GetErrorCounters(mNcp->GetThreadHelper()->GetInstance(), &otCounters);
1858 
1859     counters.mUnknown.m4To6Packets          = otCounters.mCount4To6[OT_NAT64_DROP_REASON_UNKNOWN];
1860     counters.mUnknown.m6To4Packets          = otCounters.mCount6To4[OT_NAT64_DROP_REASON_UNKNOWN];
1861     counters.mIllegalPacket.m4To6Packets    = otCounters.mCount4To6[OT_NAT64_DROP_REASON_ILLEGAL_PACKET];
1862     counters.mIllegalPacket.m6To4Packets    = otCounters.mCount6To4[OT_NAT64_DROP_REASON_ILLEGAL_PACKET];
1863     counters.mUnsupportedProto.m4To6Packets = otCounters.mCount4To6[OT_NAT64_DROP_REASON_UNSUPPORTED_PROTO];
1864     counters.mUnsupportedProto.m6To4Packets = otCounters.mCount6To4[OT_NAT64_DROP_REASON_UNSUPPORTED_PROTO];
1865     counters.mNoMapping.m4To6Packets        = otCounters.mCount4To6[OT_NAT64_DROP_REASON_NO_MAPPING];
1866     counters.mNoMapping.m6To4Packets        = otCounters.mCount6To4[OT_NAT64_DROP_REASON_NO_MAPPING];
1867 
1868     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, counters) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1869 
1870 exit:
1871     return error;
1872 }
1873 
GetNat64Cidr(DBusMessageIter & aIter)1874 otError DBusThreadObject::GetNat64Cidr(DBusMessageIter &aIter)
1875 {
1876     otError error = OT_ERROR_NONE;
1877 
1878     otIp4Cidr cidr;
1879     char      cidrString[OT_IP4_CIDR_STRING_SIZE];
1880 
1881     SuccessOrExit(error = otNat64GetCidr(mNcp->GetThreadHelper()->GetInstance(), &cidr));
1882     otIp4CidrToString(&cidr, cidrString, sizeof(cidrString));
1883 
1884     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, std::string(cidrString)) == OTBR_ERROR_NONE,
1885                  error = OT_ERROR_INVALID_ARGS);
1886 
1887 exit:
1888     return error;
1889 }
1890 
SetNat64Cidr(DBusMessageIter & aIter)1891 otError DBusThreadObject::SetNat64Cidr(DBusMessageIter &aIter)
1892 {
1893     otError     error = OT_ERROR_NONE;
1894     std::string cidrString;
1895     otIp4Cidr   cidr;
1896 
1897     VerifyOrExit(DBusMessageExtractFromVariant(&aIter, cidrString) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1898     SuccessOrExit(error = otIp4CidrFromString(cidrString.c_str(), &cidr));
1899     SuccessOrExit(error = otNat64SetIp4Cidr(mNcp->GetThreadHelper()->GetInstance(), &cidr));
1900 
1901 exit:
1902     return error;
1903 }
1904 #else  // OTBR_ENABLE_NAT64
SetNat64Enabled(DBusRequest & aRequest)1905 void DBusThreadObject::SetNat64Enabled(DBusRequest &aRequest)
1906 {
1907     OTBR_UNUSED_VARIABLE(aRequest);
1908     aRequest.ReplyOtResult(OT_ERROR_NOT_IMPLEMENTED);
1909 }
1910 
GetNat64State(DBusMessageIter & aIter)1911 otError DBusThreadObject::GetNat64State(DBusMessageIter &aIter)
1912 {
1913     OTBR_UNUSED_VARIABLE(aIter);
1914     return OT_ERROR_NOT_IMPLEMENTED;
1915 }
1916 
GetNat64Mappings(DBusMessageIter & aIter)1917 otError DBusThreadObject::GetNat64Mappings(DBusMessageIter &aIter)
1918 {
1919     OTBR_UNUSED_VARIABLE(aIter);
1920     return OT_ERROR_NOT_IMPLEMENTED;
1921 }
1922 
GetNat64ProtocolCounters(DBusMessageIter & aIter)1923 otError DBusThreadObject::GetNat64ProtocolCounters(DBusMessageIter &aIter)
1924 {
1925     OTBR_UNUSED_VARIABLE(aIter);
1926     return OT_ERROR_NOT_IMPLEMENTED;
1927 }
1928 
GetNat64ErrorCounters(DBusMessageIter & aIter)1929 otError DBusThreadObject::GetNat64ErrorCounters(DBusMessageIter &aIter)
1930 {
1931     OTBR_UNUSED_VARIABLE(aIter);
1932     return OT_ERROR_NOT_IMPLEMENTED;
1933 }
1934 
GetNat64Cidr(DBusMessageIter & aIter)1935 otError DBusThreadObject::GetNat64Cidr(DBusMessageIter &aIter)
1936 {
1937     OTBR_UNUSED_VARIABLE(aIter);
1938     return OT_ERROR_NOT_IMPLEMENTED;
1939 }
1940 
SetNat64Cidr(DBusMessageIter & aIter)1941 otError DBusThreadObject::SetNat64Cidr(DBusMessageIter &aIter)
1942 {
1943     OTBR_UNUSED_VARIABLE(aIter);
1944     return OT_ERROR_NOT_IMPLEMENTED;
1945 }
1946 #endif // OTBR_ENABLE_NAT64
1947 
GetInfraLinkInfo(DBusMessageIter & aIter)1948 otError DBusThreadObject::GetInfraLinkInfo(DBusMessageIter &aIter)
1949 {
1950 #if OTBR_ENABLE_BORDER_ROUTING
1951     otError                        error = OT_ERROR_NONE;
1952     otSysInfraNetIfAddressCounters addressCounters;
1953     uint32_t                       ifrFlags;
1954     InfraLinkInfo                  infraLinkInfo;
1955 
1956     ifrFlags = otSysGetInfraNetifFlags();
1957     otSysCountInfraNetifAddresses(&addressCounters);
1958 
1959     infraLinkInfo.mName                   = otSysGetInfraNetifName();
1960     infraLinkInfo.mIsUp                   = (ifrFlags & IFF_UP) != 0;
1961     infraLinkInfo.mIsRunning              = (ifrFlags & IFF_RUNNING) != 0;
1962     infraLinkInfo.mIsMulticast            = (ifrFlags & IFF_MULTICAST) != 0;
1963     infraLinkInfo.mLinkLocalAddresses     = addressCounters.mLinkLocalAddresses;
1964     infraLinkInfo.mUniqueLocalAddresses   = addressCounters.mUniqueLocalAddresses;
1965     infraLinkInfo.mGlobalUnicastAddresses = addressCounters.mGlobalUnicastAddresses;
1966 
1967     VerifyOrExit(DBusMessageEncodeToVariant(&aIter, infraLinkInfo) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1968 
1969 exit:
1970     return error;
1971 #else
1972     OTBR_UNUSED_VARIABLE(aIter);
1973 
1974     return OT_ERROR_NOT_IMPLEMENTED;
1975 #endif
1976 }
1977 
SetDnsUpstreamQueryState(DBusMessageIter & aIter)1978 otError DBusThreadObject::SetDnsUpstreamQueryState(DBusMessageIter &aIter)
1979 {
1980 #if OTBR_ENABLE_DNS_UPSTREAM_QUERY
1981     otError error = OT_ERROR_NONE;
1982     bool    enable;
1983 
1984     VerifyOrExit(DBusMessageExtractFromVariant(&aIter, enable) == OTBR_ERROR_NONE, error = OT_ERROR_INVALID_ARGS);
1985     otDnssdUpstreamQuerySetEnabled(mNcp->GetThreadHelper()->GetInstance(), enable);
1986 
1987 exit:
1988     return error;
1989 #else
1990     OTBR_UNUSED_VARIABLE(aIter);
1991 
1992     return OT_ERROR_NOT_IMPLEMENTED;
1993 #endif
1994 }
1995 
GetDnsUpstreamQueryState(DBusMessageIter & aIter)1996 otError DBusThreadObject::GetDnsUpstreamQueryState(DBusMessageIter &aIter)
1997 {
1998 #if OTBR_ENABLE_DNS_UPSTREAM_QUERY
1999     otError error = OT_ERROR_NONE;
2000 
2001     VerifyOrExit(DBusMessageEncodeToVariant(
2002                      &aIter, otDnssdUpstreamQueryIsEnabled(mNcp->GetThreadHelper()->GetInstance())) == OTBR_ERROR_NONE,
2003                  error = OT_ERROR_INVALID_ARGS);
2004 
2005 exit:
2006     return error;
2007 #else
2008     OTBR_UNUSED_VARIABLE(aIter);
2009 
2010     return OT_ERROR_NOT_IMPLEMENTED;
2011 #endif
2012 }
2013 
2014 static_assert(OTBR_SRP_SERVER_STATE_DISABLED == static_cast<uint8_t>(OT_SRP_SERVER_STATE_DISABLED),
2015               "OTBR_SRP_SERVER_STATE_DISABLED value is incorrect");
2016 static_assert(OTBR_SRP_SERVER_STATE_RUNNING == static_cast<uint8_t>(OT_SRP_SERVER_STATE_RUNNING),
2017               "OTBR_SRP_SERVER_STATE_RUNNING value is incorrect");
2018 static_assert(OTBR_SRP_SERVER_STATE_STOPPED == static_cast<uint8_t>(OT_SRP_SERVER_STATE_STOPPED),
2019               "OTBR_SRP_SERVER_STATE_STOPPED value is incorrect");
2020 
2021 static_assert(OTBR_SRP_SERVER_ADDRESS_MODE_UNICAST == static_cast<uint8_t>(OT_SRP_SERVER_ADDRESS_MODE_UNICAST),
2022               "OTBR_SRP_SERVER_ADDRESS_MODE_UNICAST value is incorrect");
2023 static_assert(OTBR_SRP_SERVER_ADDRESS_MODE_ANYCAST == static_cast<uint8_t>(OT_SRP_SERVER_ADDRESS_MODE_ANYCAST),
2024               "OTBR_SRP_SERVER_ADDRESS_MODE_ANYCAST value is incorrect");
2025 
2026 } // namespace DBus
2027 } // namespace otbr
2028