From 61ef43a4520faa2c2c3118013b7bf26cac2e76bb Mon Sep 17 00:00:00 2001 From: Matt Ranostay Date: Tue, 30 Oct 2018 09:49:21 -0700 Subject: libqtappfw: bluetooth: update to rewrite of bluetooth binding Bug-AGL: SPEC-1630 Change-Id: I705df82deefa371afa1f1d6ee57bb5457479408d Signed-off-by: Matt Ranostay --- bluetooth/bluetooth.cpp | 173 +++++++++++++++++++++++-------------------- bluetooth/bluetooth.h | 29 ++++---- bluetooth/bluetoothmessage.h | 15 ++-- 3 files changed, 112 insertions(+), 105 deletions(-) (limited to 'bluetooth') diff --git a/bluetooth/bluetooth.cpp b/bluetooth/bluetooth.cpp index 7ae8240..091cb26 100644 --- a/bluetooth/bluetooth.cpp +++ b/bluetooth/bluetooth.cpp @@ -31,6 +31,7 @@ Bluetooth::Bluetooth (QUrl &url, QObject * parent) : uuids.insert("a2dp", "0000110a-0000-1000-8000-00805f9b34fb"); uuids.insert("avrcp", "0000110e-0000-1000-8000-00805f9b34fb"); + uuids.insert("hfp", "0000111f-0000-1000-8000-00805f9b34fb"); } Bluetooth::~Bluetooth() @@ -38,14 +39,9 @@ Bluetooth::~Bluetooth() delete m_mloop; } -void Bluetooth::generic_command(QString verb, QString value) +void Bluetooth::send_command(QString verb, QJsonObject parameter) { BluetoothMessage *tmsg = new BluetoothMessage(); - QJsonObject parameter; - - if (!value.isEmpty()) - parameter.insert("value", value); - tmsg->createRequest(verb, parameter); m_mloop->sendMessage(tmsg); delete tmsg; @@ -53,163 +49,178 @@ void Bluetooth::generic_command(QString verb, QString value) void Bluetooth::setPower(bool state) { - generic_command("power", state ? "true": "false"); - - m_power = state; - - emit powerChanged(m_power); + QJsonObject parameter; + parameter.insert("powered", state ? "true" : "false"); + send_command("adapter_state", parameter); } void Bluetooth::setDiscoverable(bool state) { - const QStringList properties { "Pairable", "Discoverable" }; - QStringListIterator propertyIterator(properties); + QJsonObject parameter; + parameter.insert("discoverable", state ? "true" : "false"); + send_command("adapter_state", parameter); - while (propertyIterator.hasNext()) { - BluetoothMessage *tmsg = new BluetoothMessage(); - QJsonObject parameter; + m_discoverable = state; - parameter.insert("Property", propertyIterator.next()); - parameter.insert("value", state ? "true" : "false"); + emit discoverableChanged(); +} - tmsg->createRequest("set_property", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; - } +void Bluetooth::discovery_command(bool state) +{ + QJsonObject parameter; + parameter.insert("discovery", state ? "true" : "false"); - m_discoverable = state; + set_discovery_filter(); - emit discoverableChanged(); + send_command("adapter_state", parameter); } void Bluetooth::start_discovery() { - generic_command("start_discovery", ""); - generic_command("discovery_result", ""); + discovery_command(true); + + // temp workaround to list already discovered devices + send_command("managed_objects", QJsonObject()); } void Bluetooth::stop_discovery() { - generic_command("stop_discovery", ""); + discovery_command(false); } -void Bluetooth::remove_device(QString address) +void Bluetooth::remove_device(QString device) { - generic_command("remove_device", address); + QJsonObject parameter; + parameter.insert("device", device); + + send_command("remove_device", parameter); } -void Bluetooth::pair(QString address) +void Bluetooth::pair(QString device) { - generic_command("pair", address); + QJsonObject parameter; + parameter.insert("device", device); + + send_command("pair", parameter); } -void Bluetooth::cancel_pair(QString address) +void Bluetooth::cancel_pair(QString device) { - generic_command("cancel_pair", address); + send_command("cancel_pairing", QJsonObject()); } -void Bluetooth::connect(QString address, QString uuid) +void Bluetooth::connect(QString device, QString uuid) { - BluetoothMessage *tmsg = new BluetoothMessage(); QJsonObject parameter; - uuid = process_uuid(uuid); - - parameter.insert("value", address); + parameter.insert("device", device); parameter.insert("uuid", uuid); - tmsg->createRequest("connect", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + send_command("connect", parameter); } -void Bluetooth::connect(QString address) +void Bluetooth::connect(QString device) { - generic_command("connect", address); + QJsonObject parameter; + parameter.insert("device", device); + send_command("connect", parameter); } -void Bluetooth::disconnect(QString address, QString uuid) +void Bluetooth::disconnect(QString device, QString uuid) { - BluetoothMessage *tmsg = new BluetoothMessage(); QJsonObject parameter; - uuid = process_uuid(uuid); - - parameter.insert("value", address); + parameter.insert("device", device); parameter.insert("uuid", uuid); - tmsg->createRequest("disconnect", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + send_command("disconnect", parameter); } -void Bluetooth::disconnect(QString address) +void Bluetooth::disconnect(QString device) { - generic_command("disconnect", address); + QJsonObject parameter; + parameter.insert("device", device); + send_command("disconnect", parameter); } -void Bluetooth::send_confirmation() +void Bluetooth::send_confirmation(int pincode) { - generic_command("send_confirmation", "yes"); + QJsonObject parameter; + parameter.insert("pincode", pincode); + send_command("confirm_pairing", parameter); +} + + +void Bluetooth::set_discovery_filter() +{ + QStringListIterator eventIterator(uuids.values()); + QJsonObject parameter; + QJsonArray array; + + while (eventIterator.hasNext()) + array.push_back(eventIterator.next()); + + // send inital adapter state + discovery filter + parameter.insert("filter", array); + send_command("adapter_state", parameter); } void Bluetooth::onConnected() { QStringListIterator eventIterator(events); - BluetoothMessage *tmsg; while (eventIterator.hasNext()) { - tmsg = new BluetoothMessage(); QJsonObject parameter; parameter.insert("value", eventIterator.next()); - tmsg->createRequest("subscribe", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + send_command("subscribe", parameter); } - // get initial power state - generic_command("power", QString()); - // send initial list - generic_command("discovery_result", ""); + send_command("managed_objects", QJsonObject()); + + // get initial power state + send_command("adapter_state", QJsonObject()); } void Bluetooth::onDisconnected() { QStringListIterator eventIterator(events); - BluetoothMessage *tmsg; while (eventIterator.hasNext()) { - tmsg = new BluetoothMessage(); QJsonObject parameter; parameter.insert("value", eventIterator.next()); - tmsg->createRequest("unsubscribe", parameter); - m_mloop->sendMessage(tmsg); - delete tmsg; + send_command("unsubscribe", parameter); } } +void Bluetooth::processDeviceChangesEvent(QJsonObject data) +{ + QString action = data.value("action").toString(); + + if (action == "added") + emit deviceAddedEvent(data); + else if (action == "changed") + emit deviceUpdatedEvent(data); + else if (action == "removed") + emit deviceRemovedEvent(data); +} + void Bluetooth::onMessageReceived(MessageType type, Message *msg) { if (msg->isEvent() && type == BluetoothEventMessage) { BluetoothMessage *tmsg = qobject_cast(msg); - if (tmsg->isConnectionEvent()) { - emit connectionEvent(tmsg->eventData()); - } else if (tmsg->isRequestConfirmationEvent()) { + if (tmsg->isDeviceChangesEvent()) { + processDeviceChangesEvent(tmsg->eventData()); + } else if (tmsg->isAgentEvent()) { emit requestConfirmationEvent(tmsg->eventData()); - } else if (tmsg->isDeviceAddedEvent()) { - emit deviceAddedEvent(tmsg->eventData()); - } else if (tmsg->isDeviceRemovedEvent()) { - emit deviceRemovedEvent(tmsg->eventData()); - } else if (tmsg->isDeviceUpdatedEvent()) { - emit deviceUpdatedEvent(tmsg->eventData()); } + } else if (msg->isReply() && type == ResponseRequestMessage) { ResponseMessage *tmsg = qobject_cast(msg); - if (tmsg->requestVerb() == "discovery_result") { + if (tmsg->requestVerb() == "managed_objects") { emit deviceListEvent(tmsg->replyData()); - } else if (tmsg->requestVerb() == "power") { - m_power = tmsg->replyData().value("power").toString() == "on"; + } else if (tmsg->requestVerb() == "adapter_state") { + m_power = tmsg->replyData().value("powered").toBool(); emit powerChanged(m_power); } } diff --git a/bluetooth/bluetooth.h b/bluetooth/bluetooth.h index d8bbb80..0f38381 100644 --- a/bluetooth/bluetooth.h +++ b/bluetooth/bluetooth.h @@ -19,6 +19,7 @@ #include #include +#include #include "messageengine.h" @@ -38,17 +39,17 @@ class Bluetooth : public QObject Q_INVOKABLE void start_discovery(void); Q_INVOKABLE void stop_discovery(void); - Q_INVOKABLE void remove_device(QString address); - Q_INVOKABLE void pair(QString address); - Q_INVOKABLE void cancel_pair(QString address); + Q_INVOKABLE void remove_device(QString device); + Q_INVOKABLE void pair(QString device); + Q_INVOKABLE void cancel_pair(QString device); - Q_INVOKABLE void connect(QString address, QString uuid); - Q_INVOKABLE void connect(QString address); + Q_INVOKABLE void connect(QString device, QString uuid); + Q_INVOKABLE void connect(QString device); - Q_INVOKABLE void disconnect(QString address, QString uuid); - Q_INVOKABLE void disconnect(QString address); + Q_INVOKABLE void disconnect(QString device, QString uuid); + Q_INVOKABLE void disconnect(QString device); - Q_INVOKABLE void send_confirmation(void); + Q_INVOKABLE void send_confirmation(int pincode); bool power() const { return m_power; }; bool discoverable() const { return m_discoverable; }; @@ -66,7 +67,10 @@ class Bluetooth : public QObject private: MessageEngine *m_mloop; - void generic_command(QString, QString); + void send_command(QString, QJsonObject); + void set_discovery_filter(); + void discovery_command(bool); + void processDeviceChangesEvent(QJsonObject data); // slots void onConnected(); @@ -82,11 +86,8 @@ class Bluetooth : public QObject QMap uuids; const QStringList events { - "connection", - "request_confirmation", - "device_added", - "device_removed", - "device_updated", + "device_changes", + "agent", }; }; diff --git a/bluetooth/bluetoothmessage.h b/bluetooth/bluetoothmessage.h index 89aef1a..4fc9124 100644 --- a/bluetooth/bluetoothmessage.h +++ b/bluetooth/bluetoothmessage.h @@ -23,20 +23,15 @@ class BluetoothMessage : public Message { Q_OBJECT public: - bool isConnectionEvent() { return (this->eventName() == "connection"); }; - bool isRequestConfirmationEvent() { return (this->eventName() == "request_confirmation"); }; - bool isDeviceAddedEvent() { return (this->eventName() == "device_added"); }; - bool isDeviceRemovedEvent() { return (this->eventName() == "device_removed"); }; - bool isDeviceUpdatedEvent() { return (this->eventName() == "device_updated"); }; + bool isDeviceChangesEvent() { return (this->eventName() == "device_changes"); }; + bool isAgentEvent() { return (this->eventName() == "agent"); }; bool createRequest(QString verb, QJsonObject parameter); private: QStringList verbs { - "start_discovery" , "stop_discovery", "power", - "remove_device", "pair", "cancel_pair", - "connect", "disconnect", "device_priorities", - "set_device_property", "set_property", "discovery_result", - "set_avrcp_controls", "send_confirmation", "version", + "connect", "disconnect", "managed_objects", + "adapter_state", "pair", "cancel_pairing", + "confirm_pairing", "remove_device", "version", "subscribe", "unsubscribe", }; }; -- cgit 1.2.3-korg