summaryrefslogtreecommitdiffstats
path: root/bluetooth
diff options
context:
space:
mode:
Diffstat (limited to 'bluetooth')
-rw-r--r--bluetooth/CMakeLists.txt4
-rw-r--r--bluetooth/bluetooth.cpp64
-rw-r--r--bluetooth/bluetooth.h7
3 files changed, 42 insertions, 33 deletions
diff --git a/bluetooth/CMakeLists.txt b/bluetooth/CMakeLists.txt
index cdefb0d..0e05d3b 100644
--- a/bluetooth/CMakeLists.txt
+++ b/bluetooth/CMakeLists.txt
@@ -1,2 +1,2 @@
-add_headers(bluetooth.h bluetoothmessage.h bluetoothmodel.h)
-add_sources(bluetooth.cpp bluetoothmessage.cpp bluetoothmodel.cpp)
+add_headers(bluetooth.h bluetoothmodel.h)
+add_sources(bluetooth.cpp bluetoothmodel.cpp)
diff --git a/bluetooth/bluetooth.cpp b/bluetooth/bluetooth.cpp
index f74d60d..e39270a 100644
--- a/bluetooth/bluetooth.cpp
+++ b/bluetooth/bluetooth.cpp
@@ -16,13 +16,13 @@
#include <QDebug>
-#include "message.h"
-#include "bluetoothmessage.h"
+#include "callmessage.h"
+#include "eventmessage.h"
#include "responsemessage.h"
+#include "messagefactory.h"
#include "messageengine.h"
-#include "bluetoothmodel.h"
#include "bluetooth.h"
-
+#include "bluetoothmodel.h"
Bluetooth::Bluetooth (QUrl &url, QQmlContext *context, QObject * parent) :
@@ -58,10 +58,13 @@ Bluetooth::~Bluetooth()
void Bluetooth::send_command(QString verb, QJsonObject parameter)
{
- BluetoothMessage *tmsg = new BluetoothMessage();
- tmsg->createRequest(verb, parameter);
- m_mloop->sendMessage(tmsg);
- delete tmsg;
+ std::unique_ptr<Message> msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call);
+ if (!msg)
+ return;
+
+ CallMessage* btmsg = static_cast<CallMessage*>(msg.get());
+ btmsg->createRequest("Bluetooth-Manager", verb, parameter);
+ m_mloop->sendMessage(std::move(msg));
}
void Bluetooth::setPower(bool state)
@@ -261,32 +264,39 @@ void Bluetooth::processAdapterChangesEvent(QJsonObject data)
m_discoverable = false;
}
-void Bluetooth::onMessageReceived(MessageType type, Message *msg)
+void Bluetooth::onMessageReceived(std::shared_ptr<Message> msg)
{
- if (msg->isEvent() && type == MessageType::BluetoothEventMessage) {
- BluetoothMessage *tmsg = qobject_cast<BluetoothMessage*>(msg);
-
- if (tmsg->isDeviceChangesEvent()) {
- processDeviceChangesEvent(tmsg->eventData());
- } else if (tmsg->isAdapterChangesEvent()) {
- processAdapterChangesEvent(tmsg->eventData());
- } else if (tmsg->isAgentEvent()) {
- emit requestConfirmationEvent(tmsg->eventData());
- }
+ if (!msg)
+ return;
- } else if (msg->isReply() && type == MessageType::ResponseRequestMessage) {
- ResponseMessage *tmsg = qobject_cast<ResponseMessage*>(msg);
+ if (msg->isEvent()) {
+ std::shared_ptr<EventMessage> emsg = std::static_pointer_cast<EventMessage>(msg);
+ QString ename = emsg->eventName();
+ QString eapi = emsg->eventApi();
+ QJsonObject data = emsg->eventData();
+ if (eapi != "Bluetooth-Manager")
+ return;
+ if (ename == "device_changes") {
+ processDeviceChangesEvent(data);
+ } else if (ename == "adapter_changes") {
+ processAdapterChangesEvent(data);
+ } else if (ename == "agent") {
+ emit requestConfirmationEvent(data);
+ }
- if (tmsg->requestVerb() == "managed_objects") {
- populateDeviceList(tmsg->replyData());
- } else if (tmsg->requestVerb() == "adapter_state") {
- bool powered = tmsg->replyData().value("powered").toBool();
+ } else if (msg->isReply()) {
+ std::shared_ptr<ResponseMessage> rmsg = std::static_pointer_cast<ResponseMessage>(msg);
+ //get api name
+ QString verb = rmsg->requestVerb();
+ QJsonObject data = rmsg->replyData();
+ if (verb == "managed_objects") {
+ populateDeviceList(data);
+ } else if (verb == "adapter_state") {
+ bool powered = data.value("powered").toBool();
if (m_power != powered) {
m_power = powered;
emit powerChanged(m_power);
}
}
}
-
- msg->deleteLater();
}
diff --git a/bluetooth/bluetooth.h b/bluetooth/bluetooth.h
index 815e180..3953fe6 100644
--- a/bluetooth/bluetooth.h
+++ b/bluetooth/bluetooth.h
@@ -17,17 +17,16 @@
#ifndef BLUETOOTH_H
#define BLUETOOTH_H
+#include <memory>
#include <QObject>
-#include <QJsonArray>
#include <QJsonObject>
+#include <QJsonArray>
#include <QtQml/QQmlContext>
class BluetoothModel;
class MessageEngine;
class Message;
-enum class MessageType;
-
class Bluetooth : public QObject
{
Q_OBJECT
@@ -80,7 +79,7 @@ class Bluetooth : public QObject
// slots
void onConnected();
void onDisconnected();
- void onMessageReceived(MessageType, Message*);
+ void onMessageReceived(std::shared_ptr<Message>);
QString process_uuid(QString uuid) { if (uuid.length() == 36) return uuid; return uuids.value(uuid); };