File indexing completed on 2024-04-28 15:17:50

0001 /*
0002  * BluezQt - Asynchronous BlueZ wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2014-2015 David Rosca <nowrep@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #ifndef BLUEZQT_ADAPTER_H
0010 #define BLUEZQT_ADAPTER_H
0011 
0012 #include <QList>
0013 #include <QObject>
0014 #include <QStringList>
0015 
0016 #include "bluezqt_export.h"
0017 #include "device.h"
0018 #include "leadvertisingmanager.h"
0019 #include "media.h"
0020 
0021 namespace BluezQt
0022 {
0023 class PendingCall;
0024 
0025 /**
0026  * @class BluezQt::Adapter adapter.h <BluezQt/Adapter>
0027  *
0028  * Bluetooth adapter.
0029  *
0030  * This class represents a Bluetooth adapter.
0031  */
0032 class BLUEZQT_EXPORT Adapter : public QObject
0033 {
0034     Q_OBJECT
0035 
0036     Q_PROPERTY(QString ubi READ ubi)
0037     Q_PROPERTY(QString address READ address)
0038     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
0039     Q_PROPERTY(QString systemName READ systemName NOTIFY systemNameChanged)
0040     Q_PROPERTY(quint32 adapterClass READ adapterClass NOTIFY adapterClassChanged)
0041     Q_PROPERTY(bool powered READ isPowered WRITE setPowered NOTIFY poweredChanged)
0042     Q_PROPERTY(bool discoverable READ isDiscoverable WRITE setDiscoverable NOTIFY discoverableChanged)
0043     Q_PROPERTY(quint32 discoverableTimeout READ discoverableTimeout WRITE setDiscoverableTimeout NOTIFY discoverableTimeoutChanged)
0044     Q_PROPERTY(bool pairable READ isPairable WRITE setPairable NOTIFY pairableChanged)
0045     Q_PROPERTY(quint32 pairableTimeout READ pairableTimeout WRITE setPairableTimeout NOTIFY pairableTimeoutChanged)
0046     Q_PROPERTY(bool discovering READ isDiscovering NOTIFY discoveringChanged)
0047     Q_PROPERTY(QStringList uuids READ uuids NOTIFY uuidsChanged)
0048     Q_PROPERTY(QString modalias READ modalias NOTIFY modaliasChanged)
0049     Q_PROPERTY(LEAdvertisingManagerPtr leAdvertisingManager READ leAdvertisingManager NOTIFY leAdvertisingManagerChanged)
0050     Q_PROPERTY(MediaPtr media READ media NOTIFY mediaChanged)
0051     Q_PROPERTY(QList<DevicePtr> devices READ devices)
0052 
0053 public:
0054     /**
0055      * Destroys an Adapter object.
0056      */
0057     ~Adapter() override;
0058 
0059     /**
0060      * Returns a shared pointer from this.
0061      *
0062      * @return AdapterPtr
0063      */
0064     AdapterPtr toSharedPtr() const;
0065 
0066     /**
0067      * Returns an UBI of the adapter.
0068      *
0069      * Example UBI: "/org/bluez/hci0"
0070      *
0071      * @return UBI of adapter
0072      */
0073     QString ubi() const;
0074 
0075     /**
0076      * Returns an address of the adapter.
0077      *
0078      * Example address: "1C:E5:C3:BC:94:7E"
0079      *
0080      * @return address of adapter
0081      */
0082     QString address() const;
0083 
0084     /**
0085      * Returns a name of the adapter.
0086      *
0087      * @return name of adapter
0088      */
0089     QString name() const;
0090 
0091     /**
0092      * Sets the name of the adapter.
0093      *
0094      * @param name name of adapter
0095      * @return void pending call
0096      */
0097     PendingCall *setName(const QString &name);
0098 
0099     /**
0100      * Returns a system name (hostname) of the adapter.
0101      *
0102      * @return system name of adapter
0103      */
0104     QString systemName() const;
0105 
0106     /**
0107      * Returns a class of the adapter.
0108      *
0109      * @return class of adapter
0110      */
0111     quint32 adapterClass() const;
0112 
0113     /**
0114      * Returns whether the adapter is powered on.
0115      *
0116      * @return true if adapter is powered on
0117      */
0118     bool isPowered() const;
0119 
0120     /**
0121      * Sets the powered state of the adapter.
0122      *
0123      * @param powered powered state
0124      * @return void pending call
0125      */
0126     PendingCall *setPowered(bool powered);
0127 
0128     /**
0129      * Returns whether the adapter is discoverable by other devices.
0130      *
0131      * @return true if adapter is discoverable
0132      */
0133     bool isDiscoverable() const;
0134 
0135     /**
0136      * Sets the discoverable state of the adapter.
0137      *
0138      * @param discoverable discoverable state
0139      * @return void pending call
0140      */
0141     PendingCall *setDiscoverable(bool discoverable);
0142 
0143     /**
0144      * Returns the discoverable timeout in seconds of the adapter.
0145      *
0146      * Discoverable timeout defines how long the adapter stays in
0147      * discoverable state after calling setDiscoverable(true).
0148      *
0149      * Timeout 0 means infinitely.
0150      *
0151      * @return discoverable timeout of adapter
0152      */
0153     quint32 discoverableTimeout() const;
0154 
0155     /**
0156      * Sets the discoverable timeout of the adapter.
0157      *
0158      * @param timeout timeout in seconds
0159      * @return void pending call
0160      */
0161     PendingCall *setDiscoverableTimeout(quint32 timeout);
0162 
0163     /**
0164      * Returns whether the adapter is pairable with other devices.
0165      *
0166      * @return true if adapter is pairable
0167      */
0168     bool isPairable() const;
0169 
0170     /**
0171      * Sets the pairable state of the adapter.
0172      *
0173      * @param pairable pairable state
0174      * @return void pending call
0175      */
0176     PendingCall *setPairable(bool pairable);
0177 
0178     /**
0179      * Returns the pairable timeout in seconds of the adapter.
0180      *
0181      * Pairable timeout defines how long the adapter stays in
0182      * pairable state after calling setPairable(true).
0183      *
0184      * Timeout 0 means infinitely.
0185      *
0186      * @return pairable timeout of adapter
0187      */
0188     quint32 pairableTimeout() const;
0189 
0190     /**
0191      * Sets the pairable timeout of the adapter.
0192      *
0193      * @param timeout timeout in seconds
0194      * @return void pending call
0195      */
0196     PendingCall *setPairableTimeout(quint32 timeout);
0197 
0198     /**
0199      * Returns whether the adapter is discovering for other devices
0200      *
0201      * @return true if adapter is discovering
0202      */
0203     bool isDiscovering();
0204 
0205     /**
0206      * Returns UUIDs of supported services by the adapter.
0207      *
0208      * UUIDs will always be returned in uppercase.
0209      *
0210      * @return UUIDs of supported services
0211      */
0212     QStringList uuids() const;
0213 
0214     /**
0215      * Returns local device ID in modalias format.
0216      *
0217      * @return adapter modalias
0218      */
0219     QString modalias() const;
0220 
0221     /**
0222      * Returns the GATT manager interface for the adapter.
0223      *
0224      * @return null if adapter have no GATT manager
0225      */
0226     GattManagerPtr gattManager() const;
0227 
0228     /**
0229      * Returns the LE advertising manager interface for the adapter.
0230      *
0231      * @return null if adapter have no Bluetooth LE
0232      */
0233     LEAdvertisingManagerPtr leAdvertisingManager() const;
0234 
0235     /**
0236      * Returns the media interface for the adapter.
0237      *
0238      * @return null if adapter have no media
0239      */
0240     MediaPtr media() const;
0241 
0242     /**
0243      * Returns list of devices known by the adapter.
0244      *
0245      * @return list of devices
0246      */
0247     QList<DevicePtr> devices() const;
0248 
0249     /**
0250      * Returns a device for specified address.
0251      *
0252      * @param address address of device (eg. "40:79:6A:0C:39:75")
0253      * @return null if there is no device with specified address
0254      */
0255     DevicePtr deviceForAddress(const QString &address) const;
0256 
0257     /**
0258      * Starts device discovery.
0259      *
0260      * Possible errors: PendingCall::NotReady, PendingCall::Failed
0261      *
0262      * @return void pending call
0263      * @see discoverableTimeout() const
0264      */
0265     PendingCall *startDiscovery();
0266 
0267     /**
0268      * Stops device discovery.
0269      *
0270      * Possible errors: PendingCall::NotReady, PendingCall::Failed, PendingCall::NotAuthorized
0271      *
0272      * @return void pending call
0273      */
0274     PendingCall *stopDiscovery();
0275 
0276     /**
0277      * Removes the specified device.
0278      *
0279      * It will also remove the pairing information.
0280      *
0281      * Possible errors: PendingCall::InvalidArguments, PendingCall::Failed
0282      *
0283      * @param device device to be removed
0284      * @return void pending call
0285      */
0286     PendingCall *removeDevice(DevicePtr device);
0287 
0288     /**
0289      * Set the discovery filter for the caller.
0290      *
0291      * When this method is called with no filter parameter, the filter is removed.
0292      *
0293      * For details and available filter options, see [Bluez documentation for Adapter object](https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt)
0294      *
0295      * Possible errors: PendingCall::InvalidArguments, PendingCall::Failed
0296      *
0297      * @param filter options dictionary
0298      * @return void pending call
0299      */
0300      PendingCall *setDiscoveryFilter(const QVariantMap& filter);
0301 
0302     /**
0303      * Get the discovery filters for the caller.
0304      *
0305      * This returns the available filters that can be given to setDiscoveryFilter, for details see [Bluez documentation for Adapter object](https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/adapter-api.txt)
0306      *
0307      * Possible errors: PendingCall::Failed
0308      *
0309      * @return string list pending call
0310      */
0311      PendingCall *getDiscoveryFilters();
0312 
0313 Q_SIGNALS:
0314     /**
0315      * Indicates that the adapter was removed.
0316      */
0317     void adapterRemoved(AdapterPtr adapter);
0318 
0319     /**
0320      * Indicates that at least one of the adapter's properties have changed.
0321      */
0322     void adapterChanged(AdapterPtr adapter);
0323 
0324     /**
0325      * Indicates that adapter's name have changed.
0326      */
0327     void nameChanged(const QString &name);
0328 
0329     /**
0330      * Indicates that adapter's system name have changed.
0331      */
0332     void systemNameChanged(const QString &systemName);
0333 
0334     /**
0335      * Indicates that adapter's class have changed.
0336      */
0337     void adapterClassChanged(quint32 adapterClass);
0338 
0339     /**
0340      * Indicates that adapter's powered state have changed.
0341      */
0342     void poweredChanged(bool powered);
0343 
0344     /**
0345      * Indicates that adapter's discoverable state have changed.
0346      */
0347     void discoverableChanged(bool discoverable);
0348 
0349     /**
0350      * Indicates that adapter's discoverable timeout have changed.
0351      */
0352     void discoverableTimeoutChanged(quint32 timeout);
0353 
0354     /**
0355      * Indicates that adapter's pairable state have changed.
0356      */
0357     void pairableChanged(bool pairable);
0358 
0359     /**
0360      * Indicates that adapter's pairable timeout have changed.
0361      */
0362     void pairableTimeoutChanged(quint32 timeout);
0363 
0364     /**
0365      * Indicates that adapter's discovering state have changed.
0366      */
0367     void discoveringChanged(bool discovering);
0368 
0369     /**
0370      * Indicates that adapter's UUIDs have changed.
0371      */
0372     void uuidsChanged(const QStringList &uuids);
0373 
0374     /**
0375      * Indicates that adapter's modalias have changed.
0376      */
0377     void modaliasChanged(const QString &modalias);
0378 
0379     /**
0380      * Indicates that adapter's GATT manager have changed.
0381      */
0382     void gattManagerChanged(GattManagerPtr gattManager);
0383 
0384     /**
0385      * Indicates that adapter's LE advertising manager have changed.
0386      */
0387     void leAdvertisingManagerChanged(LEAdvertisingManagerPtr leAdvertisingManager);
0388 
0389     /**
0390      * Indicates that adapter's media have changed.
0391      */
0392     void mediaChanged(MediaPtr media);
0393 
0394     /**
0395      * Indicates that a new device was added (eg. found by discovery).
0396      */
0397     void deviceAdded(DevicePtr device);
0398 
0399     /**
0400      * Indicates that a device was removed.
0401      */
0402     void deviceRemoved(DevicePtr device);
0403 
0404     /**
0405      * Indicates that at least one of the device's properties have changed.
0406      */
0407     void deviceChanged(DevicePtr device);
0408 
0409 private:
0410     BLUEZQT_NO_EXPORT explicit Adapter(const QString &path, const QVariantMap &properties);
0411 
0412     class AdapterPrivate *const d;
0413 
0414     friend class AdapterPrivate;
0415     friend class ManagerPrivate;
0416     friend class InitAdaptersJobPrivate;
0417 };
0418 
0419 } // namespace BluezQt
0420 
0421 #endif // BLUEZQT_ADAPTER_H