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

0001 /*
0002  * BluezQt - Asynchronous Bluez wrapper library
0003  *
0004  * SPDX-FileCopyrightText: 2014 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 #include "device.h"
0010 #include "device_p.h"
0011 #include "pendingcall.h"
0012 #include "utils.h"
0013 
0014 namespace BluezQt
0015 {
0016 Device::Device(const QString &path, const QVariantMap &properties, AdapterPtr adapter)
0017     : QObject()
0018     , d(new DevicePrivate(path, properties, adapter))
0019 {
0020 }
0021 
0022 Device::~Device()
0023 {
0024     delete d;
0025 }
0026 
0027 DevicePtr Device::toSharedPtr() const
0028 {
0029     return d->q.toStrongRef();
0030 }
0031 
0032 QString Device::ubi() const
0033 {
0034     return d->m_bluezDevice->path();
0035 }
0036 
0037 QString Device::address() const
0038 {
0039     return d->m_address;
0040 }
0041 
0042 QString Device::name() const
0043 {
0044     return d->m_alias;
0045 }
0046 
0047 PendingCall *Device::setName(const QString &name)
0048 {
0049     return new PendingCall(d->setDBusProperty(QStringLiteral("Alias"), name), PendingCall::ReturnVoid, this);
0050 }
0051 
0052 QString Device::friendlyName() const
0053 {
0054     if (name().isEmpty() || name() == remoteName()) {
0055         return name();
0056     }
0057     if (remoteName().isEmpty()) {
0058         return name();
0059     }
0060     return QStringLiteral("%1 (%2)").arg(name(), remoteName());
0061 }
0062 
0063 QString Device::remoteName() const
0064 {
0065     return d->m_name;
0066 }
0067 
0068 quint32 Device::deviceClass() const
0069 {
0070     return d->m_deviceClass;
0071 }
0072 
0073 Device::Type Device::type() const
0074 {
0075     if (deviceClass() == 0) {
0076         return appearanceToType(appearance());
0077     }
0078 
0079     return classToType(d->m_deviceClass);
0080 }
0081 
0082 quint16 Device::appearance() const
0083 {
0084     return d->m_appearance;
0085 }
0086 
0087 QString Device::icon() const
0088 {
0089     switch (type()) {
0090     case Headset:
0091         return QStringLiteral("audio-headset");
0092     case Headphones:
0093         return QStringLiteral("audio-headphones");
0094     default:
0095         return d->m_icon.isEmpty() ? QStringLiteral("preferences-system-bluetooth") : d->m_icon;
0096     }
0097 }
0098 
0099 bool Device::isPaired() const
0100 {
0101     return d->m_paired;
0102 }
0103 
0104 bool Device::isTrusted() const
0105 {
0106     return d->m_trusted;
0107 }
0108 
0109 PendingCall *Device::setTrusted(bool trusted)
0110 {
0111     return new PendingCall(d->setDBusProperty(QStringLiteral("Trusted"), trusted), PendingCall::ReturnVoid, this);
0112 }
0113 
0114 bool Device::isBlocked() const
0115 {
0116     return d->m_blocked;
0117 }
0118 
0119 PendingCall *Device::setBlocked(bool blocked)
0120 {
0121     return new PendingCall(d->setDBusProperty(QStringLiteral("Blocked"), blocked), PendingCall::ReturnVoid, this);
0122 }
0123 
0124 bool Device::hasLegacyPairing() const
0125 {
0126     return d->m_legacyPairing;
0127 }
0128 
0129 qint16 Device::rssi() const
0130 {
0131     return d->m_rssi;
0132 }
0133 
0134 ManData Device::manufacturerData() const
0135 {
0136     return d->m_manufacturerData;
0137 }
0138 
0139 bool Device::isServicesResolved() const
0140 {
0141     return d->m_servicesResolved;
0142 }
0143 
0144 bool Device::isConnected() const
0145 {
0146     return d->m_connected;
0147 }
0148 
0149 QStringList Device::uuids() const
0150 {
0151     return d->m_uuids;
0152 }
0153 
0154 QString Device::modalias() const
0155 {
0156     return d->m_modalias;
0157 }
0158 
0159 QHash<QString, QByteArray> Device::serviceData() const
0160 {
0161     return d->m_serviceData;
0162 }
0163 
0164 BatteryPtr Device::battery() const
0165 {
0166     return d->m_battery;
0167 }
0168 
0169 InputPtr Device::input() const
0170 {
0171     return d->m_input;
0172 }
0173 
0174 MediaPlayerPtr Device::mediaPlayer() const
0175 {
0176     return d->m_mediaPlayer;
0177 }
0178 
0179 MediaTransportPtr Device::mediaTransport() const
0180 {
0181     return d->m_mediaTransport;
0182 }
0183 
0184 AdapterPtr Device::adapter() const
0185 {
0186     return d->m_adapter;
0187 }
0188 
0189 QList<GattServiceRemotePtr> Device::gattServices() const
0190 {
0191     return d->m_services;
0192 }
0193 
0194 QString Device::typeToString(Device::Type type)
0195 {
0196     switch (type) {
0197     case Device::Phone:
0198         return QStringLiteral("phone");
0199     case Device::Modem:
0200         return QStringLiteral("modem");
0201     case Device::Computer:
0202         return QStringLiteral("computer");
0203     case Device::Network:
0204         return QStringLiteral("network");
0205     case Device::Headset:
0206         return QStringLiteral("headset");
0207     case Device::Headphones:
0208         return QStringLiteral("headphones");
0209     case Device::AudioVideo:
0210         return QStringLiteral("audiovideo");
0211     case Device::Keyboard:
0212         return QStringLiteral("keyboard");
0213     case Device::Mouse:
0214         return QStringLiteral("mouse");
0215     case Device::Joypad:
0216         return QStringLiteral("joypad");
0217     case Device::Tablet:
0218         return QStringLiteral("tablet");
0219     case Device::Peripheral:
0220         return QStringLiteral("peripheral");
0221     case Device::Camera:
0222         return QStringLiteral("camera");
0223     case Device::Printer:
0224         return QStringLiteral("printer");
0225     case Device::Imaging:
0226         return QStringLiteral("imaging");
0227     case Device::Wearable:
0228         return QStringLiteral("wearable");
0229     case Device::Toy:
0230         return QStringLiteral("toy");
0231     case Device::Health:
0232         return QStringLiteral("health");
0233     default:
0234         return QStringLiteral("uncategorized");
0235     }
0236 }
0237 
0238 Device::Type Device::stringToType(const QString &typeString)
0239 {
0240     if (typeString == QLatin1String("phone")) {
0241         return Device::Phone;
0242     } else if (typeString == QLatin1String("modem")) {
0243         return Device::Modem;
0244     } else if (typeString == QLatin1String("computer")) {
0245         return Device::Computer;
0246     } else if (typeString == QLatin1String("network")) {
0247         return Device::Network;
0248     } else if (typeString == QLatin1String("headset")) {
0249         return Device::Headset;
0250     } else if (typeString == QLatin1String("headphones")) {
0251         return Device::Headphones;
0252     } else if (typeString == QLatin1String("audio")) {
0253         return Device::AudioVideo;
0254     } else if (typeString == QLatin1String("keyboard")) {
0255         return Device::Keyboard;
0256     } else if (typeString == QLatin1String("mouse")) {
0257         return Device::Mouse;
0258     } else if (typeString == QLatin1String("joypad")) {
0259         return Device::Joypad;
0260     } else if (typeString == QLatin1String("tablet")) {
0261         return Device::Tablet;
0262     } else if (typeString == QLatin1String("peripheral")) {
0263         return Device::Peripheral;
0264     } else if (typeString == QLatin1String("camera")) {
0265         return Device::Camera;
0266     } else if (typeString == QLatin1String("printer")) {
0267         return Device::Printer;
0268     } else if (typeString == QLatin1String("imaging")) {
0269         return Device::Imaging;
0270     } else if (typeString == QLatin1String("wearable")) {
0271         return Device::Wearable;
0272     } else if (typeString == QLatin1String("toy")) {
0273         return Device::Toy;
0274     } else if (typeString == QLatin1String("health")) {
0275         return Device::Health;
0276     }
0277     return Device::Uncategorized;
0278 }
0279 
0280 PendingCall *Device::connectToDevice()
0281 {
0282     return new PendingCall(d->m_bluezDevice->Connect(), PendingCall::ReturnVoid, this);
0283 }
0284 
0285 PendingCall *Device::disconnectFromDevice()
0286 {
0287     return new PendingCall(d->m_bluezDevice->Disconnect(), PendingCall::ReturnVoid, this);
0288 }
0289 
0290 PendingCall *Device::connectProfile(const QString &uuid)
0291 {
0292     return new PendingCall(d->m_bluezDevice->ConnectProfile(uuid), PendingCall::ReturnVoid, this);
0293 }
0294 
0295 PendingCall *Device::disconnectProfile(const QString &uuid)
0296 {
0297     return new PendingCall(d->m_bluezDevice->DisconnectProfile(uuid), PendingCall::ReturnVoid, this);
0298 }
0299 
0300 PendingCall *Device::pair()
0301 {
0302     return new PendingCall(d->m_bluezDevice->Pair(), PendingCall::ReturnVoid, this);
0303 }
0304 
0305 PendingCall *Device::cancelPairing()
0306 {
0307     return new PendingCall(d->m_bluezDevice->CancelPairing(), PendingCall::ReturnVoid, this);
0308 }
0309 
0310 } // namespace BluezQt
0311 
0312 #include "moc_device.cpp"