From 6a8cdaf2f718fef8a826fd98241050a7d3cbfb3d Mon Sep 17 00:00:00 2001 From: Tomasz Sterna Date: Fri, 11 Jul 2014 23:31:28 +0200 Subject: Use Log4Qt in daemon --- daemon/watchconnector.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'daemon/watchconnector.cpp') diff --git a/daemon/watchconnector.cpp b/daemon/watchconnector.cpp index dbcd831..d999229 100644 --- a/daemon/watchconnector.cpp +++ b/daemon/watchconnector.cpp @@ -18,10 +18,10 @@ void WatchConnector::deviceDiscovered(const QBluetoothDeviceInfo &device) { //FIXME TODO: Configurable if (device.name().startsWith("Pebble")) { - qDebug() << "Found Pebble:" << device.name() << '(' << device.address().toString() << ')'; + logger()->debug() << "Found Pebble: " << device.name() << " (" << device.address().toString() << ')'; handleWatch(device.name(), device.address().toString()); } else { - qDebug() << "Found other device:" << device.name() << '(' << device.address().toString() << ')'; + logger()->debug() << "Found other device: " << device.name() << " (" << device.address().toString() << ')'; } } @@ -32,7 +32,7 @@ void WatchConnector::deviceConnect(const QString &name, const QString &address) void WatchConnector::reconnect() { - qDebug() << "reconnect" << _last_name; + logger()->debug() << "reconnect " << _last_name; if (!_last_name.isEmpty() && !_last_address.isEmpty()) { deviceConnect(_last_name, _last_address); } @@ -40,14 +40,14 @@ void WatchConnector::reconnect() void WatchConnector::disconnect() { - qDebug() << __FUNCTION__; + logger()->debug() << __FUNCTION__; socket->close(); socket->deleteLater(); } void WatchConnector::handleWatch(const QString &name, const QString &address) { - qDebug() << "handleWatch" << name << address; + logger()->debug() << "handleWatch " << name << " " << address; if (socket != nullptr && socket->isOpen()) { socket->close(); socket->deleteLater(); @@ -58,7 +58,7 @@ void WatchConnector::handleWatch(const QString &name, const QString &address) _last_address = address; if (emit_name) emit nameChanged(); - qDebug() << "Creating socket"; + logger()->debug() << "Creating socket"; socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket); connect(socket, SIGNAL(readyRead()), SLOT(onReadSocket())); @@ -128,8 +128,8 @@ void WatchConnector::decodeMsg(QByteArray data) endpoint = (data.at(index) << 8) + data.at(index+1); index += 2; - qDebug() << "Length:" << datalen << " Endpoint:" << decodeEndpoint(endpoint); - qDebug() << "Data:" << data.mid(index).toHex(); + logger()->debug() << "Length:" << datalen << " Endpoint:" << decodeEndpoint(endpoint); + logger()->debug() << "Data:" << data.mid(index).toHex(); if (endpoint == watchPHONE_CONTROL) { if (data.length() >= 5) { if (data.at(4) == callHANGUP) { @@ -141,7 +141,7 @@ void WatchConnector::decodeMsg(QByteArray data) void WatchConnector::onReadSocket() { - qDebug() << "read"; + logger()->debug() << "read"; QBluetoothSocket *socket = qobject_cast(sender()); if (!socket) return; @@ -155,7 +155,7 @@ void WatchConnector::onReadSocket() void WatchConnector::onConnected() { - qDebug() << "Connected!"; + logger()->debug() << "Connected!"; bool was_connected = is_connected; is_connected = true; if (not was_connected) emit connectedChanged(); @@ -163,7 +163,7 @@ void WatchConnector::onConnected() void WatchConnector::onDisconnected() { - qDebug() << "Disconnected!"; + logger()->debug() << "Disconnected!"; bool was_connected = is_connected; is_connected = false; @@ -180,7 +180,7 @@ void WatchConnector::onDisconnected() } void WatchConnector::onError(QBluetoothSocket::SocketError error) { - qWarning() << "Error connecting Pebble" << error << socket->errorString(); + logger()->error() << "Error connecting Pebble: " << error << socket->errorString(); } void WatchConnector::sendData(const QByteArray &data) @@ -192,7 +192,7 @@ void WatchConnector::sendData(const QByteArray &data) void WatchConnector::sendMessage(unsigned int endpoint, QByteArray data) { - qDebug() << "Sending message"; + logger()->debug() << "Sending message"; QByteArray msg; // First send the length -- cgit v1.2.3 From c84773de3af76832d15806647d8529c2e5b75257 Mon Sep 17 00:00:00 2001 From: Tomasz Sterna Date: Fri, 11 Jul 2014 23:41:27 +0200 Subject: Fixed reconnect on disconnection Reconnection timeut will now raise gradually, to be 1000ms * reconnect attempt. This should help preserve power in the phone, when no Pebble is around. You can speed-up this process by reconnecting manually from manager app or its cover. Also force channel 1 for RFCOMM, as discovery does not work anymore after disconnection. --- app/qml/cover/CoverPage.qml | 1 + app/qml/pages/ManagerPage.qml | 2 +- daemon/watchconnector.cpp | 19 ++++++++++++++----- daemon/watchconnector.h | 1 + 4 files changed, 17 insertions(+), 6 deletions(-) (limited to 'daemon/watchconnector.cpp') diff --git a/app/qml/cover/CoverPage.qml b/app/qml/cover/CoverPage.qml index 767799e..186de30 100644 --- a/app/qml/cover/CoverPage.qml +++ b/app/qml/cover/CoverPage.qml @@ -58,6 +58,7 @@ CoverBackground { CoverActionList { id: coverAction + enabled: pebbled.active CoverAction { iconSource: pebbled.connected ? "image://theme/icon-cover-transfers" : "image://theme/icon-cover-sync" diff --git a/app/qml/pages/ManagerPage.qml b/app/qml/pages/ManagerPage.qml index adb1cf4..92d79b1 100644 --- a/app/qml/pages/ManagerPage.qml +++ b/app/qml/pages/ManagerPage.qml @@ -107,7 +107,7 @@ Page { if (pebbled.connected) { pebbled.disconnect(); } else { - pebbled.connect(); + pebbled.reconnect(); } } } diff --git a/daemon/watchconnector.cpp b/daemon/watchconnector.cpp index d999229..d0fcdad 100644 --- a/daemon/watchconnector.cpp +++ b/daemon/watchconnector.cpp @@ -4,11 +4,14 @@ using namespace watch; -static int __reconnect_timeout = 5000; //ms +static int __reconnect_timeout = 1000; //ms WatchConnector::WatchConnector(QObject *parent) : QObject(parent), socket(nullptr), is_connected(false) -{} +{ + reconnectTimer.setSingleShot(true); + connect(&reconnectTimer, SIGNAL(timeout()), SLOT(reconnect())); +} WatchConnector::~WatchConnector() { @@ -43,11 +46,14 @@ void WatchConnector::disconnect() logger()->debug() << __FUNCTION__; socket->close(); socket->deleteLater(); + reconnectTimer.stop(); + logger()->debug() << "Stopped reconnect timer"; } void WatchConnector::handleWatch(const QString &name, const QString &address) { logger()->debug() << "handleWatch " << name << " " << address; + reconnectTimer.stop(); if (socket != nullptr && socket->isOpen()) { socket->close(); socket->deleteLater(); @@ -67,7 +73,7 @@ void WatchConnector::handleWatch(const QString &name, const QString &address) connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(onError(QBluetoothSocket::SocketError))); // FIXME: Assuming port 1 (with Pebble) - socket->connectToService(QBluetoothAddress(address), QBluetoothUuid(QBluetoothUuid::SerialPort)); + socket->connectToService(QBluetoothAddress(address), 1); } QString WatchConnector::decodeEndpoint(unsigned int val) @@ -158,6 +164,8 @@ void WatchConnector::onConnected() logger()->debug() << "Connected!"; bool was_connected = is_connected; is_connected = true; + reconnectTimer.stop(); + reconnectTimer.setInterval(0); if (not was_connected) emit connectedChanged(); } @@ -175,8 +183,9 @@ void WatchConnector::onDisconnected() socket->deleteLater(); - // Try to connect again after a timeout - QTimer::singleShot(__reconnect_timeout, this, SLOT(reconnect())); + reconnectTimer.setInterval(reconnectTimer.interval() + __reconnect_timeout); + reconnectTimer.start(); + logger()->debug() << "Will reconnect in " << reconnectTimer.interval() << " ms"; } void WatchConnector::onError(QBluetoothSocket::SocketError error) { diff --git a/daemon/watchconnector.h b/daemon/watchconnector.h index 4015870..36d0936 100644 --- a/daemon/watchconnector.h +++ b/daemon/watchconnector.h @@ -130,6 +130,7 @@ private: QPointer socket; bool is_connected; + QTimer reconnectTimer; QString _last_name; QString _last_address; }; -- cgit v1.2.3 From 4d55e3d01c1c75a979ad6f53ac18648fc90c6934 Mon Sep 17 00:00:00 2001 From: Tomasz Sterna Date: Sat, 12 Jul 2014 01:13:04 +0200 Subject: Reworked PebbledInterface DBus handling --- app/pebbledinterface.cpp | 62 +++++++++++++++++++++++++---------------------- app/pebbledinterface.h | 6 +++-- daemon/manager.cpp | 1 - daemon/watchconnector.cpp | 4 +-- 4 files changed, 39 insertions(+), 34 deletions(-) (limited to 'daemon/watchconnector.cpp') diff --git a/app/pebbledinterface.cpp b/app/pebbledinterface.cpp index 390964a..1bd9b50 100644 --- a/app/pebbledinterface.cpp +++ b/app/pebbledinterface.cpp @@ -1,25 +1,28 @@ #include "pebbledinterface.h" QString PebbledInterface::PEBBLED_SYSTEMD_UNIT("pebbled.service"); -QString PebbledInterface::SYSTEMD_UNIT_IFACE("org.freedesktop.systemd1.Unit"); +QString PebbledInterface::PEBBLED_DBUS_SERVICE("org.pebbled"); +QString PebbledInterface::PEBBLED_DBUS_PATH("/"); +QString PebbledInterface::PEBBLED_DBUS_IFACE("org.pebbled"); PebbledInterface::PebbledInterface(QObject *parent) : - QObject(parent), pebbled(0), systemd(0), unitprops(0) + QObject(parent), pebbled(0), systemd(0) { - pebbled = new QDBusInterface("org.pebbled", - "/", - "org.pebbled", - QDBusConnection::sessionBus(), this); - pebbled->connection() - .connect(pebbled->service(), pebbled->path(), pebbled->interface(), - "connectedChanged", this, SIGNAL(connectedChanged())); - pebbled->connection() - .connect(pebbled->service(), pebbled->path(), pebbled->interface(), - "pebbleChanged", this, SLOT(onPebbleChanged())); + QDBusConnection::sessionBus().connect( + PEBBLED_DBUS_SERVICE, PEBBLED_DBUS_PATH, PEBBLED_DBUS_IFACE, + "connectedChanged", this, SIGNAL(connectedChanged())); + + QDBusConnection::sessionBus().connect( + PEBBLED_DBUS_SERVICE, PEBBLED_DBUS_PATH, PEBBLED_DBUS_IFACE, + "pebbleChanged", this, SLOT(onPebbleChanged())); // simulate connected change on active changed - connect(this, SIGNAL(activeChanged()), this, SIGNAL(connectedChanged())); + // as the daemon might not had a chance to send connectedChanged() + connect(this, SIGNAL(activeChanged()), SIGNAL(connectedChanged())); + + pebbled = new QDBusInterface(PEBBLED_DBUS_SERVICE, PEBBLED_DBUS_PATH, PEBBLED_DBUS_IFACE, + QDBusConnection::sessionBus(), this); systemd = new QDBusInterface("org.freedesktop.systemd1", "/org/freedesktop/systemd1", @@ -29,29 +32,30 @@ PebbledInterface::PebbledInterface(QObject *parent) : systemd->call("Subscribe"); QDBusReply unit = systemd->call("LoadUnit", PEBBLED_SYSTEMD_UNIT); - if (not unit.isValid()) { - qWarning() << unit.error().message(); - } else { - unitprops = new QDBusInterface("org.freedesktop.systemd1", - unit.value().path(), - "org.freedesktop.DBus.Properties", - QDBusConnection::sessionBus(), this); + if (unit.isValid()) { + unitPath = unit.value(); + getUnitProperties(); - unitprops->connection() - .connect("org.freedesktop.systemd1", - unitprops->path(), - "org.freedesktop.DBus.Properties", - "PropertiesChanged", - this, - SLOT(onPropertiesChanged(QString,QMap,QStringList)) - ); + QDBusConnection::sessionBus().connect( + "org.freedesktop.systemd1", + unitPath.path(), + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + this, + SLOT(onPropertiesChanged(QString,QMap,QStringList))); + } else { + qWarning() << unit.error().message(); } } void PebbledInterface::getUnitProperties() { - QDBusReply reply = unitprops->call("GetAll", SYSTEMD_UNIT_IFACE); + QDBusMessage request = QDBusMessage::createMethodCall( + "org.freedesktop.systemd1", unitPath.path(), + "org.freedesktop.DBus.Properties", "GetAll"); + request << "org.freedesktop.systemd1.Unit"; + QDBusReply reply = QDBusConnection::sessionBus().call(request); if (reply.isValid()) { QVariantMap newProperties = reply.value(); bool emitEnabledChanged = (properties["UnitFileState"] != newProperties["UnitFileState"]); diff --git a/app/pebbledinterface.h b/app/pebbledinterface.h index eccc766..df9cd3d 100644 --- a/app/pebbledinterface.h +++ b/app/pebbledinterface.h @@ -10,7 +10,9 @@ class PebbledInterface : public QObject Q_OBJECT static QString PEBBLED_SYSTEMD_UNIT; - static QString SYSTEMD_UNIT_IFACE; + static QString PEBBLED_DBUS_SERVICE; + static QString PEBBLED_DBUS_PATH; + static QString PEBBLED_DBUS_IFACE; Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) bool enabled() const; @@ -58,7 +60,7 @@ private slots: private: QDBusInterface *pebbled; QDBusInterface *systemd; - QDBusInterface *unitprops; + QDBusObjectPath unitPath; QVariantMap properties; }; diff --git a/daemon/manager.cpp b/daemon/manager.cpp index c045c1b..8ff6785 100644 --- a/daemon/manager.cpp +++ b/daemon/manager.cpp @@ -24,7 +24,6 @@ Manager::Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallMan connect(voice, SIGNAL(activeVoiceCallChanged()), SLOT(onActiveVoiceCallChanged())); connect(voice, SIGNAL(error(const QString &)), SLOT(onVoiceError(const QString &))); - // Watch instantiated hangup, follow the orders connect(watch, SIGNAL(hangup()), SLOT(hangupAll())); connect(watch, SIGNAL(connectedChanged()), SLOT(onConnectedChanged())); diff --git a/daemon/watchconnector.cpp b/daemon/watchconnector.cpp index d0fcdad..088384d 100644 --- a/daemon/watchconnector.cpp +++ b/daemon/watchconnector.cpp @@ -176,11 +176,11 @@ void WatchConnector::onDisconnected() bool was_connected = is_connected; is_connected = false; + if (was_connected) emit connectedChanged(); + QBluetoothSocket *socket = qobject_cast(sender()); if (!socket) return; - if (was_connected) emit connectedChanged(); - socket->deleteLater(); reconnectTimer.setInterval(reconnectTimer.interval() + __reconnect_timeout); -- cgit v1.2.3