1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
/*
* Copyright (C) 2013-2015 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Charles Kerr <charles.kerr@canonical.com>
*/
#include "device.h"
#include <QDBusReply>
#include <QDebug> // qWarning()
#include <QThread>
#include <QTimer>
#include "dbus-shared.h"
Device::Device(const QString &path, QDBusConnection &bus) :
m_name("unknown"),
m_strength(Device::None)
{
initDevice(path, bus);
}
void Device::initDevice(const QString &path, QDBusConnection &bus)
{
/* whenever any of the properties changes,
trigger the catch-all deviceChanged() signal */
QObject::connect(this, SIGNAL(nameChanged()), this, SIGNAL(deviceChanged()));
QObject::connect(this, SIGNAL(iconNameChanged()), this, SIGNAL(deviceChanged()));
QObject::connect(this, SIGNAL(addressChanged()), this, SIGNAL(deviceChanged()));
QObject::connect(this, SIGNAL(pairedChanged()), this, SIGNAL(deviceChanged()));
QObject::connect(this, SIGNAL(trustedChanged()), this, SIGNAL(deviceChanged()));
QObject::connect(this, SIGNAL(typeChanged()), this, SIGNAL(deviceChanged()));
QObject::connect(this, SIGNAL(connectionChanged()), this, SIGNAL(deviceChanged()));
QObject::connect(this, SIGNAL(strengthChanged()), this, SIGNAL(deviceChanged()));
m_bluezDevice.reset(new BluezDevice1(BLUEZ_SERVICE, path, bus));
/* Give our calls a bit more time than the default 25 seconds to
* complete whatever they are doing. In some situations (e.g. with
* specific devices) the default doesn't seem to be enough to. */
m_bluezDevice->setTimeout(60 * 1000 /* 60 seconds */);
m_bluezDeviceProperties.reset(new FreeDesktopProperties(BLUEZ_SERVICE, path, bus));
QObject::connect(m_bluezDeviceProperties.data(), SIGNAL(PropertiesChanged(const QString&, const QVariantMap&, const QStringList&)),
this, SLOT(slotPropertiesChanged(const QString&, const QVariantMap&, const QStringList&)));
Q_EMIT(pathChanged());
watchCall(m_bluezDeviceProperties->GetAll(BLUEZ_DEVICE_IFACE), [=](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QVariantMap> reply = *watcher;
if (reply.isError()) {
qWarning() << "Failed to retrieve properties for device" << m_bluezDevice->path();
watcher->deleteLater();
return;
}
auto properties = reply.argumentAt<0>();
setProperties(properties);
watcher->deleteLater();
});
}
void Device::slotPropertiesChanged(const QString &interface, const QVariantMap &changedProperties,
const QStringList &invalidatedProperties)
{
Q_UNUSED(invalidatedProperties);
if (interface != BLUEZ_DEVICE_IFACE)
return;
setProperties(changedProperties);
}
void Device::setProperties(const QMap<QString,QVariant> &properties)
{
QMapIterator<QString,QVariant> it(properties);
while (it.hasNext()) {
it.next();
updateProperty(it.key(), it.value());
}
}
void Device::setConnectAfterPairing(bool value)
{
if (m_connectAfterPairing == value)
return;
m_connectAfterPairing = value;
}
void Device::disconnect()
{
setConnection(Device::Disconnecting);
QDBusPendingCall call = m_bluezDevice->Disconnect();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<void> reply = *watcher;
if (reply.isError()) {
qWarning() << "Could not disconnect device:"
<< reply.error().message();
// Make sure we switch the connection indicator back to
// a sane state
updateConnection();
}
watcher->deleteLater();
});
}
void Device::connectAfterPairing()
{
if (!m_connectAfterPairing)
return;
connect();
}
void Device::pair()
{
if (m_paired) {
// If we are already paired we just have to make sure we
// trigger the connection process if we have to
connectAfterPairing();
return;
}
setConnection(Device::Connecting);
m_isPairing = true;
auto call = m_bluezDevice->asyncCall("Pair");
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<void> reply = *watcher;
bool success = true;
if (reply.isError()) {
qWarning() << "Failed to pair with device:"
<< reply.error().message();
updateConnection();
success = false;
}
m_isPairing = false;
Q_EMIT(pairingDone(success));
watcher->deleteLater();
});
}
void Device::cancelPairing()
{
if (!m_isPairing)
return;
auto call = m_bluezDevice->asyncCall("CancelPairing");
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<void> reply = *watcher;
if (reply.isError()) {
qWarning() << "Failed to cancel pairing attempt with device:"
<< reply.error().message();
updateConnection();
} else {
// Only mark us a not pairing when call succeeded
m_isPairing = false;
}
watcher->deleteLater();
});
}
void Device::connect()
{
// If we have just paired then the device switched to connected = true for
// a short moment as BlueZ opened up a RFCOMM channel to perform SDP. If
// we should connect with the device on specific profiles now we go ahead
// here even if we're marked as connected as this still doesn't mean we're
// connected on any profile. Calling org.bluez.Device1.Connect multiple
// times doesn't hurt an will not fail.
if (m_isConnected && !m_connectAfterPairing)
return;
setConnection(Device::Connecting);
QDBusPendingCall call = m_bluezDevice->asyncCall("Connect");
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(call, this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<void> reply = *watcher;
if (reply.isError()) {
qWarning() << "Could not connect device:"
<< reply.error().message();
} else {
makeTrusted(true);
}
// Regardless if the Connected property has changed or not we update
// the connection state here as the connection process is over now
// and we should have received any state change already at this
// point.
updateConnection();
watcher->deleteLater();
});
}
void Device::slotMakeTrustedDone(QDBusPendingCallWatcher *call)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
qWarning() << "Could not mark device as trusted:"
<< reply.error().message();
}
call->deleteLater();
}
void Device::makeTrusted(bool trusted)
{
auto call = m_bluezDeviceProperties->Set(BLUEZ_DEVICE_IFACE, "Trusted", QDBusVariant(trusted));
auto watcher = new QDBusPendingCallWatcher(call, this);
QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
this, SLOT(slotMakeTrustedDone(QDBusPendingCallWatcher*)));
}
void Device::setName(const QString &name)
{
if (m_name != name) {
m_name = name;
Q_EMIT(nameChanged());
}
}
void Device::setIconName(const QString &iconName)
{
if (m_iconName != iconName) {
m_iconName = iconName;
Q_EMIT(iconNameChanged());
}
}
void Device::setAddress(const QString &address)
{
if (m_address != address) {
m_address = address;
Q_EMIT(addressChanged());
}
}
void Device::setType(Type type)
{
if (m_type != type) {
m_type = type;
Q_EMIT(typeChanged());
updateIcon();
}
}
void Device::setPaired(bool paired)
{
if (m_paired != paired) {
m_paired = paired;
Q_EMIT(pairedChanged());
}
}
void Device::setTrusted(bool trusted)
{
if (m_trusted != trusted) {
m_trusted = trusted;
Q_EMIT(trustedChanged());
}
}
void Device::setConnection(Connection connection)
{
if (m_connection != connection) {
m_connection = connection;
Q_EMIT(connectionChanged());
}
}
void Device::setValid(bool valid)
{
if (m_valid != valid) {
m_valid = valid;
Q_EMIT(validChanged());
}
}
void Device::updateIcon()
{
/* bluez-provided icon is unreliable? In testing I'm getting
an "audio-card" icon from bluez for my NoiseHush N700 headset.
Try to guess the icon from the device type,
and use the bluez-provided icon as a fallback */
const auto type = getType();
switch (type) {
case Type::Headset:
setIconName("image://theme/audio-headset-symbolic");
break;
case Type::Headphones:
setIconName("image://theme/audio-headphones-symbolic");
break;
case Type::Carkit:
setIconName("image://theme/audio-carkit-symbolic");
break;
case Type::Speakers:
case Type::OtherAudio:
setIconName("image://theme/audio-speakers-symbolic");
break;
case Type::Mouse:
setIconName("image://theme/input-mouse-symbolic");
break;
case Type::Keyboard:
setIconName("image://theme/input-keyboard-symbolic");
break;
case Type::Cellular:
setIconName("image://theme/phone-cellular-symbolic");
break;
case Type::Smartphone:
setIconName("image://theme/phone-smartphone-symbolic");
break;
case Type::Phone:
setIconName("image://theme/phone-uncategorized-symbolic");
break;
case Type::Computer:
setIconName("image://theme/computer-symbolic");
break;
default:
setIconName(QString("image://theme/%1").arg(m_fallbackIconName));
}
}
void Device::updateConnection()
{
Connection c;
c = m_isConnected ? Connection::Connected : Connection::Disconnected;
setConnection(c);
}
void Device::updateProperty(const QString &key, const QVariant &value)
{
if (key == "Name") {
setName(value.toString());
if (value.toString().isEmpty()) {
qInfo() << "Bluetooth device is unknown";
} else {
setValid(true);
}
} else if (key == "Address") {
setAddress(value.toString());
} else if (key == "Connected") {
m_isConnected = value.toBool();
updateConnection();
} else if (key == "Class") {
setType(getTypeFromClass(value.toUInt()));
} else if (key == "Appearance") {
setType(getTypeFromAppearance(value.toUInt()));
} else if (key == "Paired") {
setPaired(value.toBool());
if (m_paired && m_connectAfterPairing) {
connectAfterPairing();
return;
}
updateConnection();
} else if (key == "Trusted") {
setTrusted(value.toBool());
} else if (key == "Icon") {
m_fallbackIconName = value.toString();
updateIcon ();
} else if (key == "RSSI") {
m_strength = getStrengthFromRssi(value.toInt());
Q_EMIT(strengthChanged());
}
}
/* Determine the Type from the bits in the Appearance field.
https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.gap.appearance.xml */
Device::Type Device::getTypeFromAppearance(quint32 c)
{
/* is it a HID class device? */
if (c & 0x03C0) {
switch (c & 0x000f) {
case 0x1:
return Type::Keyboard;
case 0x02:
return Type::Mouse;
}
}
return Type::Other;
}
/* Determine the Type from the bits in the Class of Device (CoD) field.
https://www.bluetooth.org/en-us/specification/assigned-numbers/baseband */
Device::Type Device::getTypeFromClass (quint32 c)
{
switch ((c & 0x1f00) >> 8) {
case 0x01:
return Type::Computer;
case 0x02:
switch ((c & 0xfc) >> 2) {
case 0x01:
return Type::Cellular;
case 0x03:
return Type::Smartphone;
case 0x04:
return Type::Modem;
default:
return Type::Phone;
}
break;
case 0x03:
return Type::Network;
case 0x04:
switch ((c & 0xfc) >> 2) {
case 0x01:
case 0x02:
return Type::Headset;
case 0x05:
return Type::Speakers;
case 0x06:
return Type::Headphones;
case 0x08:
return Type::Carkit;
case 0x0b: // vcr
case 0x0c: // video camera
case 0x0d: // camcorder
return Type::Video;
default:
return Type::OtherAudio;
}
break;
case 0x05:
switch ((c & 0xc0) >> 6) {
case 0x00:
switch ((c & 0x1e) >> 2) {
case 0x01:
case 0x02:
return Type::Joypad;
}
break;
case 0x01:
return Type::Keyboard;
case 0x02:
switch ((c & 0x1e) >> 2) {
case 0x05:
return Type::Tablet;
default:
return Type::Mouse;
}
}
break;
case 0x06:
if ((c & 0x80) != 0)
return Type::Printer;
if ((c & 0x20) != 0)
return Type::Camera;
break;
case 0x07:
if ((c & 0x4) != 0)
return Type::Watch;
break;
}
return Type::Other;
}
Device::Strength Device::getStrengthFromRssi(int rssi)
{
/* Modelled similar to what Mac OS X does.
* See http://www.cnet.com/how-to/how-to-check-bluetooth-connection-strength-in-os-x/ */
if (rssi >= -60)
return Excellent;
else if (rssi < -60 && rssi >= -70)
return Good;
else if (rssi < -70 && rssi >= -90)
return Fair;
else if (rssi < -90)
return Poor;
return None;
}
|