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

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_DEVICE_H
0010 #define BLUEZQT_DEVICE_H
0011 
0012 #include <QObject>
0013 
0014 #include "bluezqt_export.h"
0015 #include "gattserviceremote.h"
0016 #include "input.h"
0017 #include "mediaplayer.h"
0018 #include "mediatransport.h"
0019 namespace BluezQt
0020 {
0021 class Adapter;
0022 class PendingCall;
0023 
0024 /**
0025  * @class BluezQt::Device device.h <BluezQt/Device>
0026  *
0027  * Bluetooth device.
0028  *
0029  * This class represents a Bluetooth device.
0030  */
0031 class BLUEZQT_EXPORT Device : public QObject
0032 {
0033     Q_OBJECT
0034 
0035     Q_PROPERTY(QString ubi READ ubi)
0036     Q_PROPERTY(QString address READ address NOTIFY addressChanged)
0037     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
0038     Q_PROPERTY(QString friendlyName READ friendlyName NOTIFY friendlyNameChanged)
0039     Q_PROPERTY(QString remoteName READ remoteName NOTIFY remoteNameChanged)
0040     Q_PROPERTY(quint32 deviceClass READ deviceClass NOTIFY deviceClassChanged)
0041     Q_PROPERTY(Type type READ type NOTIFY typeChanged)
0042     Q_PROPERTY(quint16 appearance READ appearance NOTIFY appearanceChanged)
0043     Q_PROPERTY(QString icon READ icon NOTIFY iconChanged)
0044     Q_PROPERTY(bool paired READ isPaired NOTIFY pairedChanged)
0045     Q_PROPERTY(bool trusted READ isTrusted WRITE setTrusted NOTIFY trustedChanged)
0046     Q_PROPERTY(bool blocked READ isBlocked WRITE setBlocked NOTIFY blockedChanged)
0047     Q_PROPERTY(bool legacyPairing READ hasLegacyPairing NOTIFY legacyPairingChanged)
0048     Q_PROPERTY(qint16 rssi READ rssi NOTIFY rssiChanged)
0049     Q_PROPERTY(ManData manufacturerData READ manufacturerData NOTIFY manufacturerDataChanged)
0050     Q_PROPERTY(bool servicesResolved READ isServicesResolved NOTIFY servicesResolvedChanged)
0051     Q_PROPERTY(bool connected READ isConnected NOTIFY connectedChanged)
0052     Q_PROPERTY(QStringList uuids READ uuids NOTIFY uuidsChanged)
0053     Q_PROPERTY(QString modalias READ modalias NOTIFY modaliasChanged)
0054     Q_PROPERTY(BatteryPtr battery READ battery NOTIFY batteryChanged)
0055     Q_PROPERTY(InputPtr input READ input NOTIFY inputChanged)
0056     Q_PROPERTY(MediaPlayerPtr mediaPlayer READ mediaPlayer NOTIFY mediaPlayerChanged)
0057     Q_PROPERTY(MediaTransportPtr mediaTransport READ mediaTransport NOTIFY mediaTransportChanged)
0058     Q_PROPERTY(AdapterPtr adapter READ adapter)
0059     Q_PROPERTY(QList<GattServiceRemotePtr> gattServices READ gattServices NOTIFY gattServicesChanged)
0060 
0061 public:
0062     /**
0063      * %Device types.
0064      */
0065     enum Type {
0066         /** The device is a phone. */
0067         Phone,
0068         /** The device is a modem. */
0069         Modem,
0070         /** The device is a computer. */
0071         Computer,
0072         /** The device is a network. */
0073         Network,
0074         /** The device is a headset. */
0075         Headset,
0076         /** The device is a headphones. */
0077         Headphones,
0078         /** The device is an uncategorized audio video device. */
0079         AudioVideo,
0080         /** The device is a keyboard. */
0081         Keyboard,
0082         /** The device is a mouse. */
0083         Mouse,
0084         /** The device is a joypad. */
0085         Joypad,
0086         /** The device is a graphics tablet (input device). */
0087         Tablet,
0088         /** The device is an uncategorized peripheral device. */
0089         Peripheral,
0090         /** The device is a camera. */
0091         Camera,
0092         /** The device is a printer. */
0093         Printer,
0094         /** The device is an uncategorized imaging device. */
0095         Imaging,
0096         /** The device is a wearable device. */
0097         Wearable,
0098         /** The device is a toy. */
0099         Toy,
0100         /** The device is a health device. */
0101         Health,
0102         /** The device is not of any of the known types. */
0103         Uncategorized,
0104     };
0105     Q_ENUM(Type)
0106 
0107     /**
0108      * Destroys a Device object.
0109      */
0110     ~Device() override;
0111 
0112     /**
0113      * Returns a shared pointer from this.
0114      *
0115      * @return DevicePtr
0116      */
0117     DevicePtr toSharedPtr() const;
0118 
0119     /**
0120      * Returns an UBI of the device.
0121      *
0122      * Example UBI: "/org/bluez/hci0/dev_40_79_6A_0C_39_75"
0123      *
0124      * @return UBI of device
0125      */
0126     QString ubi() const;
0127 
0128     /**
0129      * Returns an address of the device.
0130      *
0131      * Example address: "40:79:6A:0C:39:75"
0132      *
0133      * @return address of device
0134      */
0135     QString address() const;
0136 
0137     /**
0138      * Returns a name of the device.
0139      *
0140      * If the name of the device wasn't previously changed,
0141      * remoteName is returned.
0142      *
0143      * @return name of device
0144      */
0145     QString name() const;
0146 
0147     /**
0148      * Sets the name of the device.
0149      *
0150      * @param name name for device
0151      * @return void pending call
0152      */
0153     PendingCall *setName(const QString &name);
0154 
0155     /**
0156      * Returns a friendly name of the device.
0157      *
0158      * Friendly name is a string "name (remoteName)".
0159      * If the remoteName is same as name, it returns just name.
0160      *
0161      * @return friendly name of device
0162      */
0163     QString friendlyName() const;
0164 
0165     /**
0166      * Returns a remote name of the device.
0167      *
0168      * @return remote name of device
0169      */
0170     QString remoteName() const;
0171 
0172     /**
0173      * Returns a class of the device.
0174      *
0175      * In case of Bluetooth Low Energy only devices,
0176      * device class is invalid (0).
0177      *
0178      * @see type() const
0179      *
0180      * @return class of device
0181      */
0182     quint32 deviceClass() const;
0183 
0184     /**
0185      * Returns a type of the device.
0186      *
0187      * Type of device is deduced from its class (for Bluetooth Classic devices)
0188      * or its appearance (for Bluetooth Low Energy devices).
0189      *
0190      * @see deviceClass() const
0191      * @see appearance() const
0192      *
0193      * @return type of device
0194      */
0195     Device::Type type() const;
0196 
0197     /**
0198      * Returns an appearance of the device.
0199      *
0200      * @return appearance of device
0201      */
0202     quint16 appearance() const;
0203 
0204     /**
0205      * Returns an icon name of the device.
0206      *
0207      * In case the icon is empty, "preferences-system-bluetooth" is returned.
0208      *
0209      * @return icon name of device
0210      */
0211     QString icon() const;
0212 
0213     /**
0214      * Returns whether the device is paired.
0215      *
0216      * @return true if device is paired
0217      */
0218     bool isPaired() const;
0219 
0220     /**
0221      * Returns whether the device is trusted.
0222      *
0223      * @return true if device is trusted
0224      */
0225     bool isTrusted() const;
0226 
0227     /**
0228      * Sets the trusted state of the device.
0229      *
0230      * @param trusted trusted state
0231      * @return void pending call
0232      */
0233     PendingCall *setTrusted(bool trusted);
0234 
0235     /**
0236      * Returns whether the device is blocked.
0237      *
0238      * @return true if device is blocked
0239      */
0240     bool isBlocked() const;
0241 
0242     /**
0243      * Sets the blocked state of the device.
0244      *
0245      * @param blocked blocked state
0246      * @return void pending call
0247      */
0248     PendingCall *setBlocked(bool blocked);
0249 
0250     /**
0251      * Returns whether the device has legacy pairing.
0252      *
0253      * @return true if device has legacy pairing
0254      */
0255     bool hasLegacyPairing() const;
0256 
0257     /**
0258      * Returns Received Signal Strength Indicator of the device.
0259      *
0260      * The bigger value indicates better signal strength.
0261      *
0262      * @note RSSI is only updated during discovery.
0263      *
0264      * @return RSSI of device
0265      */
0266     qint16 rssi() const;
0267 
0268     /**
0269      * Returns manufacturer specific advertisement data.
0270      *
0271      * @note Keys are 16 bits Manufacturer ID followed by
0272      * its byte array value.
0273      *
0274      * @return manufacturerData of device.
0275      */
0276     ManData manufacturerData() const;
0277 
0278     /**
0279      * Returns whether or not service discovery has been resolved.
0280      *
0281      * @return true if servicesResolved
0282      */
0283     bool isServicesResolved() const;
0284 
0285     /**
0286      * Returns whether the device is connected.
0287      *
0288      * @return true if connected
0289      */
0290     bool isConnected() const;
0291 
0292     /**
0293      * Returns UUIDs of supported services by the device.
0294      *
0295      * UUIDs will always be returned in uppercase.
0296      *
0297      * @return UUIDs of supported services
0298      */
0299     QStringList uuids() const;
0300 
0301     /**
0302      * Returns remote device ID in modalias format.
0303      *
0304      * @return device modalias
0305      */
0306     QString modalias() const;
0307 
0308     /**
0309      * Returns the service advertisement data.
0310      *
0311      * @returns A hash with keys being the UUIDs in and values being the raw service data value.
0312      * @since 5.72
0313      */
0314     QHash<QString, QByteArray> serviceData() const;
0315 
0316     /**
0317      * Returns the battery interface for the device.
0318      *
0319      * @return null if device has no battery
0320      * @since 5.66
0321      */
0322     BatteryPtr battery() const;
0323 
0324     /**
0325      * Returns the input interface for the device.
0326      *
0327      * Only input devices will have valid input interface.
0328      *
0329      * @return null if device have no input
0330      */
0331     InputPtr input() const;
0332 
0333     /**
0334      * Returns the media player interface for the device.
0335      *
0336      * Only devices with connected appropriate profile will
0337      * have valid media player interface.
0338      *
0339      * @return null if device have no media player
0340      */
0341     MediaPlayerPtr mediaPlayer() const;
0342 
0343     /**
0344      * Returns the media transport interface for the device.
0345      *
0346      * Only devices with connected appropriate profile will
0347      * have valid media transport interface.
0348      *
0349      * @return null if device have no media transport
0350      */
0351     MediaTransportPtr mediaTransport() const;
0352 
0353     /**
0354      * Returns an adapter that discovered this device.
0355      *
0356      * @return adapter of device
0357      */
0358     AdapterPtr adapter() const;
0359 
0360     /**
0361      * Returns list of services known by the device.
0362      *
0363      * @return list of services
0364      */
0365     QList<GattServiceRemotePtr> gattServices() const;
0366     /**
0367      * Returns a string for device type.
0368      *
0369      * @param type device type
0370      * @return device type string
0371      */
0372     static QString typeToString(Device::Type type);
0373 
0374     /**
0375      * Returns a device type for string.
0376      *
0377      * @param typeString type string
0378      * @return device type
0379      */
0380     static Device::Type stringToType(const QString &typeString);
0381 
0382 public Q_SLOTS:
0383     /**
0384      * Connects all auto-connectable profiles of the device.
0385      *
0386      * This method indicates success if at least one profile was connected.
0387      *
0388      * Possible errors: PendingCall::NotReady, PendingCall::Failed,
0389      *                  PendingCall::InProgress, PendingCall::AlreadyConnected
0390      *
0391      * @return void pending call
0392      */
0393     PendingCall *connectToDevice();
0394 
0395     /**
0396      * Disconnects all connected profiles of the device.
0397      *
0398      * This method can be used to cancel not-yet finished connectDevice() call.
0399      *
0400      * Possible errors: PendingCall::NotConnected
0401      *
0402      * @return void pending call
0403      */
0404     PendingCall *disconnectFromDevice();
0405 
0406     /**
0407      * Connects a specific profile of the device.
0408      *
0409      * Possible errors: PendingCall::DoesNotExist, PendingCall::AlreadyConnected,
0410      *                  PendingCall::ConnectFailed
0411      *
0412      * @param uuid service UUID
0413      * @return void pending call
0414      */
0415     PendingCall *connectProfile(const QString &uuid);
0416 
0417     /**
0418      * Disconnects a specific profile of the device.
0419      *
0420      * Possible errors: PendingCall::DoesNotExist, PendingCall::Failed,
0421      *                  PendingCall::NotConnected, PendingCall::NotSupported
0422      *
0423      * @param uuid service UUID
0424      * @return void pending call
0425      */
0426     PendingCall *disconnectProfile(const QString &uuid);
0427 
0428     /**
0429      * Initiates a pairing with the device.
0430      *
0431      * Possible errors: PendingCall::InvalidArguments, PendingCall::Failed,
0432      *                  PendingCall::AlreadyExists, PendingCall::AuthenticationCanceled,
0433      *                  PendingCall::AuthenticationFailed, PendingCall::AuthenticationRejected,
0434      *                  PendingCall::AuthenticationTimeout, PendingCall::ConnectionAttemptFailed
0435      *
0436      * @return void pending call
0437      */
0438     PendingCall *pair();
0439 
0440     /**
0441      * Cancels a pairing with the device.
0442      *
0443      * This method can be used to cancel pairing operation initiated with pair().
0444      *
0445      * Possible errors: PendingCall::DoesNotExist, PendingCall::Failed
0446      *
0447      * @return void pending call
0448      */
0449     PendingCall *cancelPairing();
0450 
0451 Q_SIGNALS:
0452     /**
0453      * Indicates that the device was removed.
0454      */
0455     void deviceRemoved(DevicePtr device);
0456 
0457     /**
0458      * Indicates that at least one of the device's properties have changed.
0459      */
0460     void deviceChanged(DevicePtr device);
0461 
0462     /**
0463      * Indicates that a new service was added (eg. found by connection).
0464      */
0465     void gattServiceAdded(GattServiceRemotePtr service);
0466 
0467     /**
0468      * Indicates that device GATT services list has changed
0469      */
0470     void gattServicesChanged(QList<GattServiceRemotePtr> services);
0471 
0472     /**
0473      * Indicates that a service was removed.
0474      */
0475     void gattServiceRemoved(GattServiceRemotePtr service);
0476 
0477     /**
0478      * Indicates that at least one of the device's services have changed.
0479      */
0480     void gattServiceChanged(GattServiceRemotePtr service);
0481 
0482     /**
0483      * Indicates that device's name have changed.
0484      */
0485     void nameChanged(const QString &name);
0486 
0487     /**
0488      * Indicates that device's address have changed.
0489      */
0490     void addressChanged(const QString &address);
0491 
0492     /**
0493      * Indicates that device's friendly name have changed.
0494      */
0495     void friendlyNameChanged(const QString &friendlyName);
0496 
0497     /**
0498      * Indicates that device's remote name have changed.
0499      */
0500     void remoteNameChanged(const QString &remoteName);
0501 
0502     /**
0503      * Indicates that device's class have changed.
0504      */
0505     void deviceClassChanged(quint32 deviceClass);
0506 
0507     /**
0508      * Indicates that device's type have changed.
0509      */
0510     void typeChanged(Type type);
0511 
0512     /**
0513      * Indicates that device's appearance have changed.
0514      */
0515     void appearanceChanged(quint16 appearance);
0516 
0517     /**
0518      * Indicates that device's icon have changed.
0519      */
0520     void iconChanged(const QString &icon);
0521 
0522     /**
0523      * Indicates that device's paired state have changed.
0524      */
0525     void pairedChanged(bool paired);
0526 
0527     /**
0528      * Indicates that device's trusted state have changed.
0529      */
0530     void trustedChanged(bool trusted);
0531 
0532     /**
0533      * Indicates that device's blocked state have changed.
0534      */
0535     void blockedChanged(bool blocked);
0536 
0537     /**
0538      * Indicates that device's legacy pairing state have changed.
0539      */
0540     void legacyPairingChanged(bool legacyPairing);
0541 
0542     /**
0543      * Indicates that device's RSSI have changed.
0544      */
0545     void rssiChanged(qint16 rssi);
0546 
0547     /**
0548      * Indicates that device's manufacturer data have changed.
0549      */
0550     void manufacturerDataChanged(ManData man);
0551 
0552     /**
0553      * Indicates that device's servicesResolved state have changed.
0554      */
0555     void servicesResolvedChanged(bool servicesResolved);
0556 
0557     /**
0558      * Indicates that device's connected state have changed.
0559      */
0560     void connectedChanged(bool connected);
0561 
0562     /**
0563      * Indicates that device's UUIDs have changed.
0564      */
0565     void uuidsChanged(const QStringList &uuids);
0566 
0567     /**
0568      * Indicates that device's modalias have changed.
0569      */
0570     void modaliasChanged(const QString &modalias);
0571 
0572     /**
0573      * Indicates that the device's service data has changed.
0574      * @since 5.72
0575      */
0576     void serviceDataChanged(const QHash<QString, QByteArray> &serviceData);
0577 
0578     /**
0579      * Indicates that device's battery has changed.
0580      */
0581     void batteryChanged(BatteryPtr battery);
0582 
0583     /**
0584      * Indicates that device's input have changed.
0585      */
0586     void inputChanged(InputPtr input);
0587 
0588     /**
0589      * Indicates that device's media player have changed.
0590      */
0591     void mediaPlayerChanged(MediaPlayerPtr mediaPlayer);
0592 
0593     /**
0594      * Indicates that device's media transport have changed.
0595      */
0596     void mediaTransportChanged(MediaTransportPtr mediaTransport);
0597 
0598 private:
0599     BLUEZQT_NO_EXPORT explicit Device(const QString &path, const QVariantMap &properties, AdapterPtr adapter);
0600 
0601     class DevicePrivate *const d;
0602 
0603     friend class DevicePrivate;
0604     friend class ManagerPrivate;
0605     friend class Adapter;
0606 };
0607 
0608 } // namespace BluezQt
0609 
0610 Q_DECLARE_METATYPE(BluezQt::ManData)
0611 
0612 #endif // BLUEZQT_DEVICE_H