summaryrefslogtreecommitdiffstats
path: root/bluetooth
diff options
context:
space:
mode:
authorRaquel Medina <raquel.medina@konsulko.com>2020-03-18 23:56:31 +0100
committerRaquel Medina <raquel.medina@konsulko.com>2020-03-23 14:02:28 +0100
commit0ed292d3ccf93c889734960676a321d1166d3f66 (patch)
treeb8d64d1685d2f4bf599ab3867a0d3ff557ff0479 /bluetooth
parent5c750385d02116a92fa4c120ccc26abb8267bc97 (diff)
rework message hierarchy
Rework message hierarchy with the final objective of splitting libqtappfw into several libraries. This commit carries the following changes: - Simplify message hierarchy, keeping abstract Message class, adding specialization for call and event messages, keeping ResponseMessage, and removing all module specific specializations. - Add MessageFactory class to create message objects. - Change messages life cycle: using smart pointers and removing QObject from message hierarchy (a Message is not a QObject anymore and thus 'deleteLater()' is not available). - Adapt all modules to use new message hierarchy. - Keep ResponseMessage original constructor to avoid breaking TaskManager. - Message constructors have been kept public, but will go private on a follow-up patch (once TaskManager class has been modified to use new MessageFactory). Bug-AGL: SPEC-3112 Signed-off-by: Raquel Medina <raquel.medina@konsulko.com> Change-Id: I3a7a6325209ddeca2293f1ac745371861a947bfb
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); };