From 0ed292d3ccf93c889734960676a321d1166d3f66 Mon Sep 17 00:00:00 2001 From: Raquel Medina Date: Wed, 18 Mar 2020 23:56:31 +0100 Subject: 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 Change-Id: I3a7a6325209ddeca2293f1ac745371861a947bfb --- bluetooth/CMakeLists.txt | 4 +-- bluetooth/bluetooth.cpp | 64 ++++++++++++++++++++++++++++-------------------- bluetooth/bluetooth.h | 7 +++--- 3 files changed, 42 insertions(+), 33 deletions(-) (limited to 'bluetooth') 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 -#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 msg = MessageFactory::getInstance().createOutboundMessage(MessageId::Call); + if (!msg) + return; + + CallMessage* btmsg = static_cast(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 msg) { - if (msg->isEvent() && type == MessageType::BluetoothEventMessage) { - BluetoothMessage *tmsg = qobject_cast(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(msg); + if (msg->isEvent()) { + std::shared_ptr emsg = std::static_pointer_cast(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 rmsg = std::static_pointer_cast(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 #include -#include #include +#include #include 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); QString process_uuid(QString uuid) { if (uuid.length() == 36) return uuid; return uuids.value(uuid); }; -- cgit 1.2.3-korg