diff options
author | Matt Ranostay <matt.ranostay@konsulko.com> | 2019-04-02 18:12:14 -0700 |
---|---|---|
committer | Matt Ranostay <matt.ranostay@konsulko.com> | 2019-04-04 11:43:43 -0700 |
commit | fd1810addb8bfc69c4ca3dc218f3d14e5ad3343e (patch) | |
tree | 2b6e6c104f329e7d8107d9f3e33e5ca034bb0428 /bluetooth | |
parent | 5ba2e7752c478a386de6c6cf06adb6f9cf7ee79e (diff) |
libqtappfw: bluetooth: add adapter_changes event processing
Process events from adapter_changes to emit the respective signal.
This has the advantage of avoiding race conditions of waiting for
powering up of an adapter, and to allow Settings UI to detect events.
Bug-AGL: SPEC-2295 SPEC-2290
Change-Id: I9cd391ed01cab709dad6801331eade7494155af5
Signed-off-by: Matt Ranostay <matt.ranostay@konsulko.com>
Diffstat (limited to 'bluetooth')
-rw-r--r-- | bluetooth/bluetooth.cpp | 45 | ||||
-rw-r--r-- | bluetooth/bluetooth.h | 2 | ||||
-rw-r--r-- | bluetooth/bluetoothmessage.h | 1 |
3 files changed, 35 insertions, 13 deletions
diff --git a/bluetooth/bluetooth.cpp b/bluetooth/bluetooth.cpp index eb00df4..79cc5f1 100644 --- a/bluetooth/bluetooth.cpp +++ b/bluetooth/bluetooth.cpp @@ -225,23 +225,37 @@ void Bluetooth::processDeviceChangesEvent(QJsonObject data) BluetoothDevice *device = m_bluetooth->updateDeviceProperties(nullptr, data); m_bluetooth->addDevice(device); } else if (action == "changed") { - auto powered = data.find("powered").value(); - - if (powered.isBool()) { - m_power = powered.toBool(); - if (!m_power) - m_bluetooth->removeAllDevices(); - emit powerChanged(m_power); - } else { - BluetoothDevice *device = m_bluetooth->getDevice(id); - m_bluetooth->updateDeviceProperties(device, data); - } + BluetoothDevice *device = m_bluetooth->getDevice(id); + m_bluetooth->updateDeviceProperties(device, data); } else if (action == "removed") { BluetoothDevice *device = m_bluetooth->getDevice(id); m_bluetooth->removeDevice(device); } } +void Bluetooth::processAdapterChangesEvent(QJsonObject data) +{ + QString action = data.value("action").toString(); + if (action != "changed") + return; + + QJsonObject properties = data.value("properties").toObject(); + if (!properties.contains("powered")) + return; + + bool powered = properties.find("powered").value().toBool(); + if (!powered) + m_bluetooth->removeAllDevices(); + + if (m_power != powered) { + m_power = powered; + emit powerChanged(powered); + } + + if (!m_power) + m_discoverable = false; +} + void Bluetooth::onMessageReceived(MessageType type, Message *msg) { if (msg->isEvent() && type == BluetoothEventMessage) { @@ -249,6 +263,8 @@ void Bluetooth::onMessageReceived(MessageType type, Message *msg) if (tmsg->isDeviceChangesEvent()) { processDeviceChangesEvent(tmsg->eventData()); + } else if (tmsg->isAdapterChangesEvent()) { + processAdapterChangesEvent(tmsg->eventData()); } else if (tmsg->isAgentEvent()) { emit requestConfirmationEvent(tmsg->eventData()); } @@ -259,8 +275,11 @@ void Bluetooth::onMessageReceived(MessageType type, Message *msg) if (tmsg->requestVerb() == "managed_objects") { populateDeviceList(tmsg->replyData()); } else if (tmsg->requestVerb() == "adapter_state") { - m_power = tmsg->replyData().value("powered").toBool(); - emit powerChanged(m_power); + bool powered = tmsg->replyData().value("powered").toBool(); + if (m_power != powered) { + m_power = powered; + emit powerChanged(m_power); + } } } diff --git a/bluetooth/bluetooth.h b/bluetooth/bluetooth.h index 6cef0ce..b79e0f4 100644 --- a/bluetooth/bluetooth.h +++ b/bluetooth/bluetooth.h @@ -71,6 +71,7 @@ class Bluetooth : public QObject void discovery_command(bool); void populateDeviceList(QJsonObject data); void processDeviceChangesEvent(QJsonObject data); + void processAdapterChangesEvent(QJsonObject data); // slots void onConnected(); @@ -86,6 +87,7 @@ class Bluetooth : public QObject QMap<QString, QString> uuids; const QStringList events { + "adapter_changes", "device_changes", "agent", }; diff --git a/bluetooth/bluetoothmessage.h b/bluetooth/bluetoothmessage.h index 4fc9124..a561061 100644 --- a/bluetooth/bluetoothmessage.h +++ b/bluetooth/bluetoothmessage.h @@ -24,6 +24,7 @@ class BluetoothMessage : public Message Q_OBJECT public: bool isDeviceChangesEvent() { return (this->eventName() == "device_changes"); }; + bool isAdapterChangesEvent() { return (this->eventName() == "adapter_changes"); }; bool isAgentEvent() { return (this->eventName() == "agent"); }; bool createRequest(QString verb, QJsonObject parameter); |