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