2 * Copyright (c) 2020, The OpenThread Authors.
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.
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.
31 * This file includes definitions for d-bus client API.
34 #ifndef OTBR_THREAD_API_DBUS_HPP_
35 #define OTBR_THREAD_API_DBUS_HPP_
39 #include <dbus/dbus.h>
41 #include "common/types.hpp"
42 #include "dbus/common/constants.hpp"
43 #include "dbus/common/error.hpp"
44 #include "dbus/common/types.hpp"
49 bool IsThreadActive(DeviceRole aRole);
54 using DeviceRoleHandler = std::function<void(DeviceRole)>;
55 using ScanHandler = std::function<void(const std::vector<ActiveScanResult> &)>;
56 using OtResultHandler = std::function<void(ClientError)>;
59 * The constructor of a d-bus object.
61 * Will use the default interfacename
63 * @param[in] aConnection The dbus connection.
66 ThreadApiDBus(DBusConnection *aConnection);
69 * The constructor of a d-bus object.
71 * @param[in] aConnection The dbus connection.
72 * @param[in] aInterfaceName The network interface name.
75 ThreadApiDBus(DBusConnection *aConnection, const std::string &aInterfaceName);
78 * This method adds a callback for device role change.
80 * @param[in] aHandler The device role handler.
83 void AddDeviceRoleHandler(const DeviceRoleHandler &aHandler);
86 * This method permits unsecure join on port.
88 * @param[in] aPort The port number.
89 * @param[in] aSeconds The timeout to close the port, 0 for never close.
91 * @retval ERROR_NONE successfully performed the dbus function call
92 * @retval ERROR_DBUS dbus encode/decode error
93 * @retval ... OpenThread defined error value otherwise
96 ClientError PermitUnsecureJoin(uint16_t aPort, uint32_t aSeconds);
99 * This method performs a Thread network scan.
101 * @param[in] aHandler The scan result handler.
103 * @retval ERROR_NONE successfully performed the dbus function call
104 * @retval ERROR_DBUS dbus encode/decode error
105 * @retval ... OpenThread defined error value otherwise
108 ClientError Scan(const ScanHandler &aHandler);
111 * This method attaches the device to the Thread network.
112 * @param[in] aNetworkName The network name.
113 * @param[in] aPanId The pan id, UINT16_MAX for random.
114 * @param[in] aExtPanId The extended pan id, UINT64_MAX for random.
115 * @param[in] aMasterKey The master key, empty for random.
116 * @param[in] aPSKc The pre-shared commissioner key, empty for random.
117 * @param[in] aChannelMask A bitmask for valid channels, will random select one.
118 * @param[in] aHandler The attach result handler.
120 * @retval ERROR_NONE successfully performed the dbus function call
121 * @retval ERROR_DBUS dbus encode/decode error
122 * @retval ... OpenThread defined error value otherwise
125 ClientError Attach(const std::string & aNetworkName,
128 const std::vector<uint8_t> &aMasterKey,
129 const std::vector<uint8_t> &aPSKc,
130 uint32_t aChannelMask,
131 const OtResultHandler & aHandler);
134 * This method attaches the device to the Thread network.
136 * The network parameters will be set with the active dataset under this
139 * @param[in] aHandler The attach result handler.
141 * @retval ERROR_NONE successfully performed the dbus function call
142 * @retval ERROR_DBUS dbus encode/decode error
143 * @retval ... OpenThread defined error value otherwise
146 ClientError Attach(const OtResultHandler &aHandler);
149 * This method performs a factory reset.
151 * @param[in] aHandler The reset result handler.
153 * @retval ERROR_NONE successfully performed the dbus function call
154 * @retval ERROR_DBUS dbus encode/decode error
155 * @retval ... OpenThread defined error value otherwise
158 ClientError FactoryReset(const OtResultHandler &aHandler);
161 * This method performs a soft reset.
163 * @retval ERROR_NONE successfully performed the dbus function call
164 * @retval ERROR_DBUS dbus encode/decode error
165 * @retval ... OpenThread defined error value otherwise
168 ClientError Reset(void);
171 * This method triggers a thread join process.
173 * @note The joiner start and the attach proccesses are exclusive
175 * @param[in] aPskd The pre-shared key for device.
176 * @param[in] aProvisioningUrl The provision url.
177 * @param[in] aVendorName The vendor name.
178 * @param[in] aVendorModel The vendor model.
179 * @param[in] aVendorSwVersion The vendor software version.
180 * @param[in] aVendorData The vendor custom data.
181 * @param[in] aHandler The join result handler.
183 * @retval ERROR_NONE successfully performed the dbus function call
184 * @retval ERROR_DBUS dbus encode/decode error
185 * @retval ... OpenThread defined error value otherwise
188 ClientError JoinerStart(const std::string & aPskd,
189 const std::string & aProvisioningUrl,
190 const std::string & aVendorName,
191 const std::string & aVendorModel,
192 const std::string & aVendorSwVersion,
193 const std::string & aVendorData,
194 const OtResultHandler &aHandler);
197 * This method stops the joiner process
199 * @retval ERROR_NONE successfully performed the dbus function call
200 * @retval ERROR_DBUS dbus encode/decode error
201 * @retval ... OpenThread defined error value otherwise
204 ClientError JoinerStop(void);
207 * This method adds a on-mesh address prefix.
209 * @param[in] aPrefix The address prefix.
211 * @retval ERROR_NONE successfully performed the dbus function call
212 * @retval ERROR_DBUS dbus encode/decode error
213 * @retval ... OpenThread defined error value otherwise
216 ClientError AddOnMeshPrefix(const OnMeshPrefix &aPrefix);
219 * This method removes a on-mesh address prefix.
221 * @param[in] aPrefix The address prefix.
223 * @retval ERROR_NONE successfully performed the dbus function call
224 * @retval ERROR_DBUS dbus encode/decode error
225 * @retval ... OpenThread defined error value otherwise
228 ClientError RemoveOnMeshPrefix(const Ip6Prefix &aPrefix);
231 * This method adds an external route.
233 * @param[in] aExternalroute The external route config
235 * @retval ERROR_NONE successfully performed the dbus function call
236 * @retval ERROR_DBUS dbus encode/decode error
237 * @retval ... OpenThread defined error value otherwise
240 ClientError AddExternalRoute(const ExternalRoute &aExternalRoute);
243 * This method removes an external route.
245 * @param[in] aPrefix The route prefix.
247 * @retval ERROR_NONE successfully performed the dbus function call
248 * @retval ERROR_DBUS dbus encode/decode error
249 * @retval ... OpenThread defined error value otherwise
252 ClientError RemoveExternalRoute(const Ip6Prefix &aPrefix);
255 * This method sets the mesh-local prefix.
257 * @param[in] aPrefix The address prefix.
259 * @retval ERROR_NONE successfully performed the dbus function call
260 * @retval ERROR_DBUS dbus encode/decode error
261 * @retval ... OpenThread defined error value otherwise
264 ClientError SetMeshLocalPrefix(const std::array<uint8_t, OTBR_IP6_PREFIX_SIZE> &aPrefix);
267 * This method sets the legacy prefix of ConnectIP.
269 * @param[in] aPrefix The address prefix.
271 * @retval ERROR_NONE successfully performed the dbus function call
272 * @retval ERROR_DBUS dbus encode/decode error
273 * @retval ... OpenThread defined error value otherwise
276 ClientError SetLegacyUlaPrefix(const std::array<uint8_t, OTBR_IP6_PREFIX_SIZE> &aPrefix);
279 * This method sets the active operational dataset.
281 * @param[out] aDataset The active operational dataset
283 * @retval ERROR_NONE successfully performed the dbus function call
284 * @retval ERROR_DBUS dbus encode/decode error
285 * @retval ... OpenThread defined error value otherwise
288 ClientError SetActiveDatasetTlvs(const std::vector<uint8_t> &aDataset);
291 * This method sets the link operating mode.
293 * @param[in] aConfig The operating mode config.
295 * @retval ERROR_NONE successfully performed the dbus function call
296 * @retval ERROR_DBUS dbus encode/decode error
297 * @retval ... OpenThread defined error value otherwise
300 ClientError SetLinkMode(const LinkModeConfig &aConfig);
303 * This method sets the radio region.
305 * @param[in] aRadioRegion The radio region.
307 * @retval ERROR_NONE successfully performed the dbus function call
308 * @retval ERROR_DBUS dbus encode/decode error
309 * @retval ... OpenThread defined error value otherwise
312 ClientError SetRadioRegion(const std::string &aRadioRegion);
315 * This method gets the link operating mode.
317 * @param[out] aConfig The operating mode config.
319 * @retval ERROR_NONE successfully performed the dbus function call
320 * @retval ERROR_DBUS dbus encode/decode error
321 * @retval ... OpenThread defined error value otherwise
324 ClientError GetLinkMode(LinkModeConfig &aConfig);
327 * This method gets the current device role.
329 * @param[out] aDeviceRole The device role
331 * @retval ERROR_NONE successfully performed the dbus function call
332 * @retval ERROR_DBUS dbus encode/decode error
333 * @retval ... OpenThread defined error value otherwise
336 ClientError GetDeviceRole(DeviceRole &aDeviceRole);
339 * This method gets the network name.
341 * @param[out] aName The network name.
343 * @retval ERROR_NONE successfully performed the dbus function call
344 * @retval ERROR_DBUS dbus encode/decode error
345 * @retval ... OpenThread defined error value otherwise
348 ClientError GetNetworkName(std::string &aName);
351 * This method gets the network pan id.
353 * @param[out] aPanId The pan id.
355 * @retval ERROR_NONE successfully performed the dbus function call
356 * @retval ERROR_DBUS dbus encode/decode error
357 * @retval ... OpenThread defined error value otherwise
360 ClientError GetPanId(uint16_t &aPanId);
363 * This method gets the extended pan id.
365 * @param[out] aExtPanId The extended pan id.
367 * @retval ERROR_NONE successfully performed the dbus function call
368 * @retval ERROR_DBUS dbus encode/decode error
369 * @retval ... OpenThread defined error value otherwise
372 ClientError GetExtPanId(uint64_t &aExtPanId);
375 * This method gets the extended pan id.
377 * @param[out] aChannel The extended pan id.
379 * @retval ERROR_NONE successfully performed the dbus function call
380 * @retval ERROR_DBUS dbus encode/decode error
381 * @retval ... OpenThread defined error value otherwise
384 ClientError GetChannel(uint16_t &aChannel);
387 * This method gets the network master key.
389 * @param[out] aMasterKey The network master key.
391 * @retval ERROR_NONE successfully performed the dbus function call
392 * @retval ERROR_DBUS dbus encode/decode error
393 * @retval ... OpenThread defined error value otherwise
396 ClientError GetMasterKey(std::vector<uint8_t> &aMasterKey);
399 * This method gets the Clear Channel Assessment failure rate.
401 * @param[out] aFailureRate The failure rate.
403 * @retval ERROR_NONE successfully performed the dbus function call
404 * @retval ERROR_DBUS dbus encode/decode error
405 * @retval ... OpenThread defined error value otherwise
408 ClientError GetCcaFailureRate(uint16_t &aFailureRate);
411 * This method gets the mac level statistics counters.
413 * @param[out] aCounters The statistic counters.
415 * @retval ERROR_NONE successfully performed the dbus function call
416 * @retval ERROR_DBUS dbus encode/decode error
417 * @retval ... OpenThread defined error value otherwise
420 ClientError GetLinkCounters(MacCounters &aCounters); // For telemetry
423 * This method gets the ip level statistics counters.
425 * @param[out] aCounters The statistic counters.
427 * @retval ERROR_NONE successfully performed the dbus function call
428 * @retval ERROR_DBUS dbus encode/decode error
429 * @retval ... OpenThread defined error value otherwise
432 ClientError GetIp6Counters(IpCounters &aCounters); // For telemetry
435 * This method gets the supported channel mask.
437 * @param[out] aChannelMask The channel mask.
439 * @retval ERROR_NONE successfully performed the dbus function call
440 * @retval ERROR_DBUS dbus encode/decode error
441 * @retval ... OpenThread defined error value otherwise
444 ClientError GetSupportedChannelMask(uint32_t &aChannelMask);
447 * This method gets the Thread routing locator
449 * @param[out] aRloc16 The routing locator
451 * @retval ERROR_NONE successfully performed the dbus function call
452 * @retval ERROR_DBUS dbus encode/decode error
453 * @retval ... OpenThread defined error value otherwise
456 ClientError GetRloc16(uint16_t &aRloc16);
459 * This method gets 802.15.4 extended address
461 * @param[out] aExtendedAddress The extended address
463 * @retval ERROR_NONE successfully performed the dbus function call
464 * @retval ERROR_DBUS dbus encode/decode error
465 * @retval ... OpenThread defined error value otherwise
468 ClientError GetExtendedAddress(uint64_t &aExtendedAddress);
471 * This method gets the node's router id.
473 * @param[out] aRouterId The router id.
475 * @retval ERROR_NONE Successfully performed the dbus function call.
476 * @retval ERROR_DBUS dbus encode/decode error
477 * @retval OT_ERROR_INVALID_STATE The node is not a router.
478 * @retval ... OpenThread defined error value otherwise
481 ClientError GetRouterId(uint8_t &aRouterId);
484 * This method gets the network's leader data.
486 * @param[out] aLeaderData The leader data.
488 * @retval ERROR_NONE successfully performed the dbus function call
489 * @retval ERROR_DBUS dbus encode/decode error
490 * @retval ... OpenThread defined error value otherwise
493 ClientError GetLeaderData(LeaderData &aLeaderData);
496 * This method gets the network data.
498 * @param[out] aNetworkData The network data.
500 * @retval ERROR_NONE successfully performed the dbus function call
501 * @retval ERROR_DBUS dbus encode/decode error
502 * @retval ... OpenThread defined error value otherwise
505 ClientError GetNetworkData(std::vector<uint8_t> &aNetworkData);
508 * This method gets the stable network data.
510 * @param[out] aNetworkData The stable network data.
512 * @retval ERROR_NONE successfully performed the dbus function call
513 * @retval ERROR_DBUS dbus encode/decode error
514 * @retval ... OpenThread defined error value otherwise
517 ClientError GetStableNetworkData(std::vector<uint8_t> &aNetworkData);
520 * This method gets the node's local leader weight.
522 * @param[out] aWeight The local leader weight.
524 * @retval ERROR_NONE successfully performed the dbus function call
525 * @retval ERROR_DBUS dbus encode/decode error
526 * @retval ... OpenThread defined error value otherwise
529 ClientError GetLocalLeaderWeight(uint8_t &aWeight);
532 * This method gets the channel monitor sample count.
534 * @param[out] aSampleCount The channel monitor sample count.
536 * @retval ERROR_NONE successfully performed the dbus function call
537 * @retval ERROR_DBUS dbus encode/decode error
538 * @retval ... OpenThread defined error value otherwise
541 ClientError GetChannelMonitorSampleCount(uint32_t &aSampleCount);
544 * This method gets the channel qualities
546 * @param[out] aChannelQualities The channel qualities.
548 * @retval ERROR_NONE successfully performed the dbus function call
549 * @retval ERROR_DBUS dbus encode/decode error
550 * @retval ... OpenThread defined error value otherwise
553 ClientError GetChannelMonitorAllChannelQualities(std::vector<ChannelQuality> &aChannelQualities);
556 * This method gets the child table.
558 * @param[out] aChildTable The child table.
560 * @retval ERROR_NONE successfully performed the dbus function call
561 * @retval ERROR_DBUS dbus encode/decode error
562 * @retval ... OpenThread defined error value otherwise
565 ClientError GetChildTable(std::vector<ChildInfo> &aChildTable);
568 * This method gets the neighbor table.
570 * @param[out] aNeighborTable The neighbor table.
572 * @retval ERROR_NONE successfully performed the dbus function call
573 * @retval ERROR_DBUS dbus encode/decode error
574 * @retval ... OpenThread defined error value otherwise
577 ClientError GetNeighborTable(std::vector<NeighborInfo> &aNeighborTable);
580 * This method gets the network's parition id.
582 * @param[out] aPartitionId The partition id.
584 * @retval ERROR_NONE successfully performed the dbus function call
585 * @retval ERROR_DBUS dbus encode/decode error
586 * @retval ... OpenThread defined error value otherwise
589 ClientError GetPartitionId(uint32_t &aPartitionId);
592 * This method gets the rssi of the latest packet.
594 * @param[out] aRssi The rssi of the latest packet.
596 * @retval ERROR_NONE successfully performed the dbus function call
597 * @retval ERROR_DBUS dbus encode/decode error
598 * @retval ... OpenThread defined error value otherwise
601 ClientError GetInstantRssi(int8_t &aRssi);
604 * This method gets the radio transmit power.
606 * @param[out] aTxPower The radio transmit power.
608 * @retval ERROR_NONE successfully performed the dbus function call
609 * @retval ERROR_DBUS dbus encode/decode error
610 * @retval ... OpenThread defined error value otherwise
613 ClientError GetRadioTxPower(int8_t &aTxPower);
616 * This method gets the external route table
618 * @param[out] aExternalRoutes The external route table
620 * @retval ERROR_NONE successfully performed the dbus function call
621 * @retval ERROR_DBUS dbus encode/decode error
622 * @retval ... OpenThread defined error value otherwise
625 ClientError GetExternalRoutes(std::vector<ExternalRoute> &aExternalRoutes);
628 * This method gets the active operational dataset
630 * @param[out] aDataset The active operational dataset
632 * @retval ERROR_NONE successfully performed the dbus function call
633 * @retval ERROR_DBUS dbus encode/decode error
634 * @retval ... OpenThread defined error value otherwise
637 ClientError GetActiveDatasetTlvs(std::vector<uint8_t> &aDataset);
640 * This method gets the radio region.
642 * @param[out] aRadioRegion The radio region.
644 * @retval ERROR_NONE successfully performed the dbus function call
645 * @retval ERROR_DBUS dbus encode/decode error
646 * @retval ... OpenThread defined error value otherwise
649 ClientError GetRadioRegion(std::string &aRadioRegion);
652 * This method returns the network interface name the client is bound to.
654 * @returns The network interface name.
657 std::string GetInterfaceName(void);
660 ClientError CallDBusMethodSync(const std::string &aMethodName);
661 ClientError CallDBusMethodAsync(const std::string &aMethodName, DBusPendingCallNotifyFunction aFunction);
663 template <typename ArgType> ClientError CallDBusMethodSync(const std::string &aMethodName, const ArgType &aArgs);
665 template <typename ArgType>
666 ClientError CallDBusMethodAsync(const std::string & aMethodName,
667 const ArgType & aArgs,
668 DBusPendingCallNotifyFunction aFunction);
670 template <typename ValType> ClientError SetProperty(const std::string &aPropertyName, const ValType &aValue);
672 template <typename ValType> ClientError GetProperty(const std::string &aPropertyName, ValType &aValue);
674 ClientError SubscribeDeviceRoleSignal(void);
675 static DBusHandlerResult sDBusMessageFilter(DBusConnection *aConnection, DBusMessage *aMessage, void *aData);
676 DBusHandlerResult DBusMessageFilter(DBusConnection *aConnection, DBusMessage *aMessage);
678 template <void (ThreadApiDBus::*Handler)(DBusPendingCall *aPending)>
679 static void sHandleDBusPendingCall(DBusPendingCall *aPending, void *aThreadApiDBus);
681 void AttachPendingCallHandler(DBusPendingCall *aPending);
682 void FactoryResetPendingCallHandler(DBusPendingCall *aPending);
683 void JoinerStartPendingCallHandler(DBusPendingCall *aPending);
684 static void sScanPendingCallHandler(DBusPendingCall *aPending, void *aThreadApiDBus);
685 void ScanPendingCallHandler(DBusPendingCall *aPending);
687 static void EmptyFree(void *) {}
689 std::string mInterfaceName;
691 DBusConnection *mConnection;
693 ScanHandler mScanHandler;
694 OtResultHandler mAttachHandler;
695 OtResultHandler mFactoryResetHandler;
696 OtResultHandler mJoinerHandler;
698 std::vector<DeviceRoleHandler> mDeviceRoleHandlers;
704 #endif // OTBR_THREAD_API_DBUS_HPP_