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