openthread-br  0.3.0-72c0388
thread_api_dbus.hpp
Go to the documentation of this file.
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 
34 #ifndef OTBR_THREAD_API_DBUS_HPP_
35 #define OTBR_THREAD_API_DBUS_HPP_
36 
37 #include <functional>
38 
39 #include <dbus/dbus.h>
40 
41 #include "common/types.hpp"
43 #include "dbus/common/error.hpp"
44 #include "dbus/common/types.hpp"
45 
46 namespace otbr {
47 namespace DBus {
48 
49 bool IsThreadActive(DeviceRole aRole);
50 
52 {
53 public:
54  using DeviceRoleHandler = std::function<void(DeviceRole)>;
55  using ScanHandler = std::function<void(const std::vector<ActiveScanResult> &)>;
56  using EnergyScanHandler = std::function<void(const std::vector<EnergyScanResult> &)>;
57  using OtResultHandler = std::function<void(ClientError)>;
58 
67  ThreadApiDBus(DBusConnection *aConnection);
68 
76  ThreadApiDBus(DBusConnection *aConnection, const std::string &aInterfaceName);
77 
84  void AddDeviceRoleHandler(const DeviceRoleHandler &aHandler);
85 
97  ClientError PermitUnsecureJoin(uint16_t aPort, uint32_t aSeconds);
98 
109  ClientError Scan(const ScanHandler &aHandler);
110 
124  ClientError EnergyScan(uint32_t aScanDuration, const EnergyScanHandler &aHandler);
125 
141  ClientError Attach(const std::string & aNetworkName,
142  uint16_t aPanId,
143  uint64_t aExtPanId,
144  const std::vector<uint8_t> &aNetworkKey,
145  const std::vector<uint8_t> &aPSKc,
146  uint32_t aChannelMask,
147  const OtResultHandler & aHandler);
148 
162  ClientError Attach(const OtResultHandler &aHandler);
163 
172  ClientError Detach(const OtResultHandler &aHandler);
173 
191  ClientError AttachAllNodesTo(const std::vector<uint8_t> &aDataset);
192 
203  ClientError FactoryReset(const OtResultHandler &aHandler);
204 
213  ClientError Reset(void);
214 
233  ClientError JoinerStart(const std::string & aPskd,
234  const std::string & aProvisioningUrl,
235  const std::string & aVendorName,
236  const std::string & aVendorModel,
237  const std::string & aVendorSwVersion,
238  const std::string & aVendorData,
239  const OtResultHandler &aHandler);
240 
249  ClientError JoinerStop(void);
250 
261  ClientError AddOnMeshPrefix(const OnMeshPrefix &aPrefix);
262 
273  ClientError RemoveOnMeshPrefix(const Ip6Prefix &aPrefix);
274 
285  ClientError AddExternalRoute(const ExternalRoute &aExternalRoute);
286 
297  ClientError RemoveExternalRoute(const Ip6Prefix &aPrefix);
298 
309  ClientError SetMeshLocalPrefix(const std::array<uint8_t, OTBR_IP6_PREFIX_SIZE> &aPrefix);
310 
321  ClientError SetLegacyUlaPrefix(const std::array<uint8_t, OTBR_IP6_PREFIX_SIZE> &aPrefix);
322 
333  ClientError SetActiveDatasetTlvs(const std::vector<uint8_t> &aDataset);
334 
346  ClientError SetFeatureFlagListData(const std::vector<uint8_t> &aFeatureFlagListData);
347 
358  ClientError SetLinkMode(const LinkModeConfig &aConfig);
359 
370  ClientError SetRadioRegion(const std::string &aRadioRegion);
371 
382  ClientError SetNat64Enabled(bool aEnabled);
383 
395 
406  ClientError GetDeviceRole(DeviceRole &aDeviceRole);
407 
418  ClientError GetNetworkName(std::string &aName);
419 
430  ClientError GetPanId(uint16_t &aPanId);
431 
442  ClientError GetExtPanId(uint64_t &aExtPanId);
443 
454  ClientError GetChannel(uint16_t &aChannel);
455 
466  ClientError GetNetworkKey(std::vector<uint8_t> &aNetworkKey);
467 
478  ClientError GetCcaFailureRate(uint16_t &aFailureRate);
479 
490  ClientError GetLinkCounters(MacCounters &aCounters); // For telemetry
491 
502  ClientError GetIp6Counters(IpCounters &aCounters); // For telemetry
503 
514  ClientError GetSupportedChannelMask(uint32_t &aChannelMask);
515 
526  ClientError GetRloc16(uint16_t &aRloc16);
527 
538  ClientError GetExtendedAddress(uint64_t &aExtendedAddress);
539 
551  ClientError GetRouterId(uint8_t &aRouterId);
552 
563  ClientError GetLeaderData(LeaderData &aLeaderData);
564 
575  ClientError GetNetworkData(std::vector<uint8_t> &aNetworkData);
576 
587  ClientError GetStableNetworkData(std::vector<uint8_t> &aNetworkData);
588 
599  ClientError GetLocalLeaderWeight(uint8_t &aWeight);
600 
611  ClientError GetChannelMonitorSampleCount(uint32_t &aSampleCount);
612 
623  ClientError GetChannelMonitorAllChannelQualities(std::vector<ChannelQuality> &aChannelQualities);
624 
635  ClientError GetChildTable(std::vector<ChildInfo> &aChildTable);
636 
647  ClientError GetNeighborTable(std::vector<NeighborInfo> &aNeighborTable);
648 
659  ClientError GetPartitionId(uint32_t &aPartitionId);
660 
671  ClientError GetInstantRssi(int8_t &aRssi);
672 
683  ClientError GetRadioTxPower(int8_t &aTxPower);
684 
695  ClientError GetExternalRoutes(std::vector<ExternalRoute> &aExternalRoutes);
696 
707  ClientError GetOnMeshPrefixes(std::vector<OnMeshPrefix> &aOnMeshPrefixes);
708 
719  ClientError GetActiveDatasetTlvs(std::vector<uint8_t> &aDataset);
720 
732  ClientError GetFeatureFlagListData(std::vector<uint8_t> &aFeatureFlagListData);
733 
744  ClientError GetRadioRegion(std::string &aRadioRegion);
745 
756  ClientError GetSrpServerInfo(SrpServerInfo &aSrpServerInfo);
757 
769 
770 #if OTBR_ENABLE_DNSSD_DISCOVERY_PROXY
771 
781  ClientError GetDnssdCounters(DnssdCounters &aDnssdCounters);
782 #endif
783 
790  std::string GetInterfaceName(void);
791 
809  ClientError UpdateVendorMeshCopTxtEntries(std::vector<TxtEntry> &aUpdate);
810 
822 
833  ClientError GetNat64Mappings(std::vector<Nat64AddressMapping> &aMappings);
834 
846 
858 
859 private:
860  ClientError CallDBusMethodSync(const std::string &aMethodName);
861  ClientError CallDBusMethodAsync(const std::string &aMethodName, DBusPendingCallNotifyFunction aFunction);
862 
863  template <typename ArgType> ClientError CallDBusMethodSync(const std::string &aMethodName, const ArgType &aArgs);
864 
865  template <typename ArgType>
866  ClientError CallDBusMethodAsync(const std::string & aMethodName,
867  const ArgType & aArgs,
868  DBusPendingCallNotifyFunction aFunction);
869 
870  template <typename ValType> ClientError SetProperty(const std::string &aPropertyName, const ValType &aValue);
871 
872  template <typename ValType> ClientError GetProperty(const std::string &aPropertyName, ValType &aValue);
873 
874  ClientError SubscribeDeviceRoleSignal(void);
875  static DBusHandlerResult sDBusMessageFilter(DBusConnection *aConnection, DBusMessage *aMessage, void *aData);
876  DBusHandlerResult DBusMessageFilter(DBusConnection *aConnection, DBusMessage *aMessage);
877 
878  template <void (ThreadApiDBus::*Handler)(DBusPendingCall *aPending)>
879  static void sHandleDBusPendingCall(DBusPendingCall *aPending, void *aThreadApiDBus);
880 
881  void AttachPendingCallHandler(DBusPendingCall *aPending);
882  void DetachPendingCallHandler(DBusPendingCall *aPending);
883  void FactoryResetPendingCallHandler(DBusPendingCall *aPending);
884  void JoinerStartPendingCallHandler(DBusPendingCall *aPending);
885  static void sScanPendingCallHandler(DBusPendingCall *aPending, void *aThreadApiDBus);
886  void ScanPendingCallHandler(DBusPendingCall *aPending);
887  void EnergyScanPendingCallHandler(DBusPendingCall *aPending);
888 
889  static void EmptyFree(void *) {}
890 
891  std::string mInterfaceName;
892 
893  DBusConnection *mConnection;
894 
895  ScanHandler mScanHandler;
896  EnergyScanHandler mEnergyScanHandler;
897  OtResultHandler mAttachHandler;
898  OtResultHandler mDetachHandler;
899  OtResultHandler mFactoryResetHandler;
900  OtResultHandler mJoinerHandler;
901 
902  std::vector<DeviceRoleHandler> mDeviceRoleHandlers;
903 };
904 
905 } // namespace DBus
906 } // namespace otbr
907 
908 #endif // OTBR_THREAD_API_DBUS_HPP_
otbr::DBus::ThreadApiDBus::GetNetworkKey
ClientError GetNetworkKey(std::vector< uint8_t > &aNetworkKey)
Definition: thread_api_dbus.cpp:531
otbr::DBus::ThreadApiDBus::SetRadioRegion
ClientError SetRadioRegion(const std::string &aRadioRegion)
Definition: thread_api_dbus.cpp:490
otbr::DBus::ThreadApiDBus::AddOnMeshPrefix
ClientError AddOnMeshPrefix(const OnMeshPrefix &aPrefix)
Definition: thread_api_dbus.cpp:440
otbr::DBus::ThreadApiDBus::GetFeatureFlagListData
ClientError GetFeatureFlagListData(std::vector< uint8_t > &aFeatureFlagListData)
Definition: thread_api_dbus.cpp:641
otbr::DBus::ThreadApiDBus::GetDeviceRole
ClientError GetDeviceRole(DeviceRole &aDeviceRole)
Definition: thread_api_dbus.cpp:500
otbr::DBus::ThreadApiDBus::GetIp6Counters
ClientError GetIp6Counters(IpCounters &aCounters)
Definition: thread_api_dbus.cpp:546
otbr::DBus::ThreadApiDBus::GetNat64State
ClientError GetNat64State(Nat64ComponentState &aState)
Definition: thread_api_dbus.cpp:661
otbr::DBus::ThreadApiDBus::Attach
ClientError Attach(const std::string &aNetworkName, uint16_t aPanId, uint64_t aExtPanId, const std::vector< uint8_t > &aNetworkKey, const std::vector< uint8_t > &aPSKc, uint32_t aChannelMask, const OtResultHandler &aHandler)
Definition: thread_api_dbus.cpp:235
otbr::DBus::LinkModeConfig
Definition: types.hpp:79
otbr::DBus::ThreadApiDBus::GetChannelMonitorAllChannelQualities
ClientError GetChannelMonitorAllChannelQualities(std::vector< ChannelQuality > &aChannelQualities)
Definition: thread_api_dbus.cpp:596
otbr::DBus::ThreadApiDBus::SetFeatureFlagListData
ClientError SetFeatureFlagListData(const std::vector< uint8_t > &aFeatureFlagListData)
Definition: thread_api_dbus.cpp:480
otbr::DBus::ThreadApiDBus::GetChannelMonitorSampleCount
ClientError GetChannelMonitorSampleCount(uint32_t &aSampleCount)
Definition: thread_api_dbus.cpp:591
otbr::DBus::ThreadApiDBus::GetInstantRssi
ClientError GetInstantRssi(int8_t &aRssi)
Definition: thread_api_dbus.cpp:616
otbr::DBus::ThreadApiDBus::GetChannel
ClientError GetChannel(uint16_t &aChannel)
Definition: thread_api_dbus.cpp:526
otbr::DBus::Ip6Prefix
Definition: types.hpp:86
otbr::DBus::ThreadApiDBus::GetRadioTxPower
ClientError GetRadioTxPower(int8_t &aTxPower)
Definition: thread_api_dbus.cpp:621
otbr::DBus::ThreadApiDBus::GetStableNetworkData
ClientError GetStableNetworkData(std::vector< uint8_t > &aNetworkData)
Definition: thread_api_dbus.cpp:581
otbr::DBus::ThreadApiDBus::GetChildTable
ClientError GetChildTable(std::vector< ChildInfo > &aChildTable)
Definition: thread_api_dbus.cpp:601
otbr::DBus::ThreadApiDBus::AddDeviceRoleHandler
void AddDeviceRoleHandler(const DeviceRoleHandler &aHandler)
Definition: thread_api_dbus.cpp:160
otbr::DBus::ThreadApiDBus::SetLegacyUlaPrefix
ClientError SetLegacyUlaPrefix(const std::array< uint8_t, OTBR_IP6_PREFIX_SIZE > &aPrefix)
Definition: thread_api_dbus.cpp:470
otbr::DBus::ThreadApiDBus::GetSupportedChannelMask
ClientError GetSupportedChannelMask(uint32_t &aChannelMask)
Definition: thread_api_dbus.cpp:551
constants.hpp
otbr::DBus::ThreadApiDBus::AddExternalRoute
ClientError AddExternalRoute(const ExternalRoute &aExternalRoute)
Definition: thread_api_dbus.cpp:450
otbr::DBus::ThreadApiDBus::GetSrpServerInfo
ClientError GetSrpServerInfo(SrpServerInfo &aSrpServerInfo)
Definition: thread_api_dbus.cpp:651
otbr::DBus::ThreadApiDBus::SetNat64Enabled
ClientError SetNat64Enabled(bool aEnabled)
Definition: thread_api_dbus.cpp:460
otbr::DBus::LeaderData
Definition: types.hpp:509
otbr::DBus::ThreadApiDBus::SetMeshLocalPrefix
ClientError SetMeshLocalPrefix(const std::array< uint8_t, OTBR_IP6_PREFIX_SIZE > &aPrefix)
Definition: thread_api_dbus.cpp:465
otbr::DBus::ThreadApiDBus::UpdateVendorMeshCopTxtEntries
ClientError UpdateVendorMeshCopTxtEntries(std::vector< TxtEntry > &aUpdate)
Definition: thread_api_dbus.cpp:853
otbr::DBus::ThreadApiDBus::Detach
ClientError Detach(const OtResultHandler &aHandler)
Definition: thread_api_dbus.cpp:305
otbr::DBus::ThreadApiDBus::AttachAllNodesTo
ClientError AttachAllNodesTo(const std::vector< uint8_t > &aDataset)
Definition: thread_api_dbus.cpp:847
otbr::DBus::ExternalRoute
Definition: types.hpp:159
otbr::DBus::DnssdCounters
Definition: types.hpp:571
otbr::DBus::ThreadApiDBus::GetNat64ProtocolCounters
ClientError GetNat64ProtocolCounters(Nat64ProtocolCounters &aCounters)
Definition: thread_api_dbus.cpp:671
otbr::DBus::ThreadApiDBus::GetCcaFailureRate
ClientError GetCcaFailureRate(uint16_t &aFailureRate)
Definition: thread_api_dbus.cpp:536
otbr::DBus::ThreadApiDBus::SetActiveDatasetTlvs
ClientError SetActiveDatasetTlvs(const std::vector< uint8_t > &aDataset)
Definition: thread_api_dbus.cpp:475
otbr::DBus::ThreadApiDBus::JoinerStart
ClientError JoinerStart(const std::string &aPskd, const std::string &aProvisioningUrl, const std::string &aVendorName, const std::string &aVendorModel, const std::string &aVendorSwVersion, const std::string &aVendorData, const OtResultHandler &aHandler)
Definition: thread_api_dbus.cpp:388
types.hpp
otbr::DBus::ThreadApiDBus::GetNat64ErrorCounters
ClientError GetNat64ErrorCounters(Nat64ErrorCounters &aCounters)
Definition: thread_api_dbus.cpp:676
otbr::DBus::IpCounters
Definition: types.hpp:458
otbr::DBus::ThreadApiDBus::Reset
ClientError Reset(void)
Definition: thread_api_dbus.cpp:383
otbr::DBus::ThreadApiDBus::GetMdnsTelemetryInfo
ClientError GetMdnsTelemetryInfo(MdnsTelemetryInfo &aMdnsTelemetryInfo)
Definition: thread_api_dbus.cpp:656
otbr::DBus::ClientError
ClientError
Definition: error.hpp:51
otbr::DBus::ThreadApiDBus::GetLeaderData
ClientError GetLeaderData(LeaderData &aLeaderData)
Definition: thread_api_dbus.cpp:571
otbr::DBus::ThreadApiDBus::GetPanId
ClientError GetPanId(uint16_t &aPanId)
Definition: thread_api_dbus.cpp:516
otbr::DBus::ThreadApiDBus::GetRadioRegion
ClientError GetRadioRegion(std::string &aRadioRegion)
Definition: thread_api_dbus.cpp:646
otbr::DBus::ThreadApiDBus::SetLinkMode
ClientError SetLinkMode(const LinkModeConfig &aConfig)
Definition: thread_api_dbus.cpp:485
otbr::DBus::OnMeshPrefix
Definition: types.hpp:96
otbr::DBus::ThreadApiDBus::GetLinkCounters
ClientError GetLinkCounters(MacCounters &aCounters)
Definition: thread_api_dbus.cpp:541
otbr::DBus::ThreadApiDBus::GetOnMeshPrefixes
ClientError GetOnMeshPrefixes(std::vector< OnMeshPrefix > &aOnMeshPrefixes)
Definition: thread_api_dbus.cpp:631
otbr::DBus::ThreadApiDBus::GetNetworkName
ClientError GetNetworkName(std::string &aName)
Definition: thread_api_dbus.cpp:511
otbr::DBus::ThreadApiDBus::GetPartitionId
ClientError GetPartitionId(uint32_t &aPartitionId)
Definition: thread_api_dbus.cpp:611
otbr::DBus::ThreadApiDBus::GetExtendedAddress
ClientError GetExtendedAddress(uint64_t &aExtendedAddress)
Definition: thread_api_dbus.cpp:561
otbr::DBus::Nat64ProtocolCounters
Definition: types.hpp:660
otbr::DBus::ThreadApiDBus::GetRouterId
ClientError GetRouterId(uint8_t &aRouterId)
Definition: thread_api_dbus.cpp:566
otbr::DBus::ThreadApiDBus::Scan
ClientError Scan(const ScanHandler &aHandler)
Definition: thread_api_dbus.cpp:165
otbr::DBus::ThreadApiDBus
Definition: thread_api_dbus.hpp:51
otbr::DBus::ThreadApiDBus::FactoryReset
ClientError FactoryReset(const OtResultHandler &aHandler)
Definition: thread_api_dbus.cpp:344
otbr::DBus::Nat64ErrorCounters
Definition: types.hpp:678
otbr::MdnsTelemetryInfo
Definition: types.hpp:395
otbr::DBus::ThreadApiDBus::GetNat64Mappings
ClientError GetNat64Mappings(std::vector< Nat64AddressMapping > &aMappings)
Definition: thread_api_dbus.cpp:666
otbr::DBus::ThreadApiDBus::GetLinkMode
ClientError GetLinkMode(LinkModeConfig &aConfig)
Definition: thread_api_dbus.cpp:495
otbr::DBus::SrpServerInfo
Definition: types.hpp:537
otbr::DBus::ThreadApiDBus::RemoveExternalRoute
ClientError RemoveExternalRoute(const Ip6Prefix &aPrefix)
Definition: thread_api_dbus.cpp:455
otbr::DBus::ThreadApiDBus::RemoveOnMeshPrefix
ClientError RemoveOnMeshPrefix(const Ip6Prefix &aPrefix)
Definition: thread_api_dbus.cpp:445
otbr::DBus::ThreadApiDBus::GetActiveDatasetTlvs
ClientError GetActiveDatasetTlvs(std::vector< uint8_t > &aDataset)
Definition: thread_api_dbus.cpp:636
otbr::DBus::ThreadApiDBus::EnergyScan
ClientError EnergyScan(uint32_t aScanDuration, const EnergyScanHandler &aHandler)
Definition: thread_api_dbus.cpp:197
otbr::DBus::Nat64ComponentState
Definition: types.hpp:640
otbr::DBus::ThreadApiDBus::GetLocalLeaderWeight
ClientError GetLocalLeaderWeight(uint8_t &aWeight)
Definition: thread_api_dbus.cpp:586
otbr::DBus::ThreadApiDBus::GetNetworkData
ClientError GetNetworkData(std::vector< uint8_t > &aNetworkData)
Definition: thread_api_dbus.cpp:576
otbr::DBus::ThreadApiDBus::PermitUnsecureJoin
ClientError PermitUnsecureJoin(uint16_t aPort, uint32_t aSeconds)
Definition: thread_api_dbus.cpp:230
otbr::DBus::ThreadApiDBus::ThreadApiDBus
ThreadApiDBus(DBusConnection *aConnection)
Definition: thread_api_dbus.cpp:84
otbr::DBus::ThreadApiDBus::GetRloc16
ClientError GetRloc16(uint16_t &aRloc16)
Definition: thread_api_dbus.cpp:556
otbr::DBus::ThreadApiDBus::GetNeighborTable
ClientError GetNeighborTable(std::vector< NeighborInfo > &aNeighborTable)
Definition: thread_api_dbus.cpp:606
otbr::DBus::ThreadApiDBus::GetExternalRoutes
ClientError GetExternalRoutes(std::vector< ExternalRoute > &aExternalRoutes)
Definition: thread_api_dbus.cpp:626
otbr::DBus::ThreadApiDBus::JoinerStop
ClientError JoinerStop(void)
Definition: thread_api_dbus.cpp:435
otbr::DBus::ThreadApiDBus::GetExtPanId
ClientError GetExtPanId(uint64_t &aExtPanId)
Definition: thread_api_dbus.cpp:521
otbr::DBus::ThreadApiDBus::GetInterfaceName
std::string GetInterfaceName(void)
Definition: thread_api_dbus.cpp:688
error.hpp
otbr::DBus::MacCounters
Definition: types.hpp:196