summaryrefslogtreecommitdiff
path: root/daemon
diff options
context:
space:
mode:
authorPhilipp Andreas <github@smurfy.de>2014-07-12 09:29:47 +0200
committerPhilipp Andreas <github@smurfy.de>2014-07-12 09:30:49 +0200
commit2101baf2911d7965c86e5308e4d8350790a6956c (patch)
tree2acb875cabca055797620458baad26d5f8612105 /daemon
parent959e251f2336e21d3615b1525a38773fe58ab2e7 (diff)
parent84e775d768f0675ee733132b3762db647f1409ac (diff)
Merge branch 'master' into notifications
Conflicts: daemon/manager.cpp
Diffstat (limited to 'daemon')
-rw-r--r--daemon/daemon.cpp75
-rw-r--r--daemon/daemon.pro31
-rw-r--r--daemon/dbusconnector.cpp12
-rw-r--r--daemon/dbusconnector.h2
-rw-r--r--daemon/manager.cpp41
-rw-r--r--daemon/manager.h2
-rw-r--r--daemon/voicecallhandler.cpp44
-rw-r--r--daemon/voicecallhandler.h3
-rw-r--r--daemon/voicecallmanager.cpp6
-rw-r--r--daemon/voicecallmanager.h3
-rw-r--r--daemon/watchconnector.cpp49
-rw-r--r--daemon/watchconnector.h6
12 files changed, 196 insertions, 78 deletions
diff --git a/daemon/daemon.cpp b/daemon/daemon.cpp
index 66352fd..cd5e781 100644
--- a/daemon/daemon.cpp
+++ b/daemon/daemon.cpp
@@ -30,6 +30,17 @@
#include <signal.h>
#include <QCoreApplication>
+#include <QStandardPaths>
+#include <QFile>
+#include <QDir>
+#include <QFileInfo>
+
+#include "LogManager"
+#include "SystemlogAppender"
+#include "FileAppender"
+#include "helpers/factory.h"
+#include "Appender"
+#include "PropertyConfigurator"
void signalhandler(int sig)
{
@@ -43,10 +54,74 @@ void signalhandler(int sig)
}
}
+// For some reason exactly SystemLogAppender doesn't have a factory registered in log4qt,
+// so in order for it to be instantiatable from .conf file (or even in general?) we declare factory
+// right here and will register it in initLoggin()
+Log4Qt::Appender *create_system_log_appender() {
+ return new Log4Qt::SystemLogAppender;
+}
+
+void initLogging()
+{
+ // Should really be done in log4qt, but somehow it's missing these
+ Log4Qt::Factory::registerAppender("org.apache.log4j.SystemLogAppender", create_system_log_appender);
+
+ // Sailfish OS-specific locations for the app settings files and app's own files
+ const QString logConfigFilePath(QStandardPaths::standardLocations(QStandardPaths::ConfigLocation).at(0)
+ + "pebble/log4qt.conf");
+ const QString fallbackLogConfigPath("/usr/share/pebble/log4qt.conf");
+
+ const QString& usedConfigFile = QFile::exists(logConfigFilePath) ? logConfigFilePath : fallbackLogConfigPath;
+ Log4Qt::PropertyConfigurator::configure(usedConfigFile);
+
+ // Uglyish hack for replacing $XDG_CACHE_HOME with the proper cache directory
+ // TODO: Implement replacing of $XDG_CACHE_HOME (and other vars?) with the proper values before configuring log4qt
+
+ // Iterate all appenders attached to root logger and whenever a FileAppender (or its descender found), replace
+ // $XDG_CACHE_HOME with the proper folder name
+ QList<Log4Qt::Appender *> appenders = Log4Qt::LogManager::rootLogger()->appenders();
+ QList<Log4Qt::Appender *>::iterator i;
+ QDir pathCreator;
+ for (i = appenders.begin(); i != appenders.end(); ++i) {
+ Log4Qt::FileAppender* fa = qobject_cast<Log4Qt::FileAppender*>(*i);
+ if(fa) {
+ QString filename = fa->file();
+
+ // As per March 2014 on emulator QStandardPaths::CacheLocation is /home/nemo/.cache
+ // while on device it is /home/nemo/.cache/app-name
+ // both things are fine, logging path will just be a little deeper on device
+ filename.replace("$XDG_CACHE_HOME",
+ QStandardPaths::standardLocations(QStandardPaths::CacheLocation).at(0)
+ );
+ // make sure loggin dir exists
+ QFileInfo fi(filename);
+ if(!pathCreator.mkpath(fi.path())) {
+ Log4Qt::LogManager::rootLogger()->error("Failed to create dir for logging: %1", fi.path());
+ }
+
+ fa->setFile(filename);
+ fa->activateOptions();
+ }
+ }
+
+ // For capturing qDebug() and console.log() messages
+ // Note that console.log() might fail in Sailfish OS device builds. Not sure why, but it seems like
+ // console.log() exactly in Sailfish OS device release builds doesn't go through the same qDebug() channel
+ Log4Qt::LogManager::setHandleQtMessages(true);
+
+ qDebug() << "Using following log config file: " << usedConfigFile;
+
+ Log4Qt::Logger::logger(QLatin1String("Main Logger"))->info("Logging started");
+}
+
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
+ // Init logging should be called after app object creation as initLogging() will examine
+ // QCoreApplication for determining the .conf files locations
+ initLogging();
+
watch::WatchConnector watch;
DBusConnector dbus;
VoiceCallManager voice;
diff --git a/daemon/daemon.pro b/daemon/daemon.pro
index 2c4894a..e52e8e8 100644
--- a/daemon/daemon.pro
+++ b/daemon/daemon.pro
@@ -1,7 +1,6 @@
TARGET = pebbled
CONFIG += console
-CONFIG -= app_bundle
CONFIG += link_pkgconfig
QT -= gui
@@ -9,6 +8,10 @@ QT += bluetooth dbus contacts
PKGCONFIG += commhistory-qt5 mlite5
QMAKE_CXXFLAGS += -std=c++0x
+LIBS += -L$$OUT_PWD/../ext/Log4Qt/ -llog4qt
+QMAKE_RPATHDIR += /usr/share/pebble/lib
+INCLUDEPATH += ../ext/Log4Qt/src ../ext/Log4Qt/deploy/include
+
SOURCES += \
daemon.cpp \
manager.cpp \
@@ -28,11 +31,33 @@ HEADERS += \
dbusadaptor.h \
notificationmanager.h
-INSTALLS += target pebbled
+OTHER_FILES += \
+ org.pebbled.xml \
+ ../log4qt-debug.conf \
+ ../log4qt-release.conf
+
+INSTALLS += target pebbled confile lib
target.path = /usr/bin
pebbled.files = $${TARGET}.service
pebbled.path = /usr/lib/systemd/user
-OTHER_FILES += org.pebbled.xml
+CONFIG(debug, debug|release) {
+ message(Debug build)
+ confile.extra = cp $$PWD/../log4qt-debug.conf $$OUT_PWD/../log4qt.conf
+}
+else {
+ message(Release build)
+ confile.extra = cp $$PWD/../log4qt-release.conf $$OUT_PWD/../log4qt.conf
+}
+
+confile.files = $$OUT_PWD/../log4qt.conf
+confile.path = /usr/share/pebble
+
+lib.files += $$OUT_PWD/../ext/Log4Qt/*.s*
+lib.path = /usr/share/pebble/lib
+
+# so QtCreator could find commhistory headers... :-(
+INCLUDEPATH += $$[QT_HOST_PREFIX]/include/commhistory-qt5
+INCLUDEPATH += $$[QT_HOST_PREFIX]/include/mlite5
diff --git a/daemon/dbusconnector.cpp b/daemon/dbusconnector.cpp
index e5bed8f..2ad753d 100644
--- a/daemon/dbusconnector.cpp
+++ b/daemon/dbusconnector.cpp
@@ -25,14 +25,14 @@ bool DBusConnector::findPebble()
"org.bluez.Manager",
"ListAdapters"));
if (not ListAdaptersReply.isValid()) {
- qWarning() << ListAdaptersReply.error().message();
+ logger()->error() << ListAdaptersReply.error().message();
return false;
}
QList<QDBusObjectPath> adapters = ListAdaptersReply.value();
if (adapters.isEmpty()) {
- qWarning() << "No BT adapters found";
+ logger()->debug() << "No BT adapters found";
return false;
}
@@ -41,7 +41,7 @@ bool DBusConnector::findPebble()
"org.bluez.Adapter",
"GetProperties"));
if (not AdapterPropertiesReply.isValid()) {
- qWarning() << AdapterPropertiesReply.error().message();
+ logger()->error() << AdapterPropertiesReply.error().message();
return false;
}
@@ -57,16 +57,16 @@ bool DBusConnector::findPebble()
"org.bluez.Device",
"GetProperties"));
if (not DevicePropertiesReply.isValid()) {
- qWarning() << DevicePropertiesReply.error().message();
+ logger()->error() << DevicePropertiesReply.error().message();
continue;
}
const QVariantMap &dict = DevicePropertiesReply.value();
QString tmp = dict["Name"].toString();
- qDebug() << "Found BT device:" << tmp;
+ logger()->debug() << "Found BT device:" << tmp;
if (tmp.startsWith("Pebble")) {
- qDebug() << "Found Pebble:" << tmp;
+ logger()->debug() << "Found Pebble:" << tmp;
pebbleProps = dict;
emit pebbleChanged();
return true;
diff --git a/daemon/dbusconnector.h b/daemon/dbusconnector.h
index e166238..98f6e58 100644
--- a/daemon/dbusconnector.h
+++ b/daemon/dbusconnector.h
@@ -3,10 +3,12 @@
#include <QObject>
#include <QVariantMap>
+#include "Logger"
class DBusConnector : public QObject
{
Q_OBJECT
+ LOG4QT_DECLARE_QCLASS_LOGGER
Q_PROPERTY(QVariantMap pebble READ pebble NOTIFY pebbleChanged)
diff --git a/daemon/manager.cpp b/daemon/manager.cpp
index f02dc0c..0a5d722 100644
--- a/daemon/manager.cpp
+++ b/daemon/manager.cpp
@@ -24,7 +24,6 @@ Manager::Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallMan
connect(notifications, SIGNAL(emailNotify(const QString &,const QString &,const QString &)), SLOT(onEmailNotify(const QString &,const QString &,const QString &)));
connect(notifications, SIGNAL(smsNotify(const QString &,const QString &)), SLOT(onSmsNotify(const QString &,const QString &)));
- // Watch instantiated hangup, follow the orders
connect(watch, SIGNAL(hangup()), SLOT(hangupAll()));
connect(watch, SIGNAL(connectedChanged()), SLOT(onConnectedChanged()));
@@ -32,7 +31,7 @@ Manager::Manager(watch::WatchConnector *watch, DBusConnector *dbus, VoiceCallMan
notification.setImage("icon-system-bluetooth-device");
if (btDevice.isValid()) {
- qDebug() << "BT local name:" << btDevice.name();
+ logger()->debug() << "BT local name:" << btDevice.name();
connect(dbus, SIGNAL(pebbleChanged()), SLOT(onPebbleChanged()));
dbus->findPebble();
}
@@ -51,7 +50,7 @@ void Manager::onPebbleChanged()
const QVariantMap & pebble = dbus->pebble();
QString name = pebble["Name"].toString();
if (name.isEmpty()) {
- qDebug() << "Pebble gone";
+ logger()->debug() << "Pebble gone";
} else {
watch->deviceConnect(name, pebble["Address"].toString());
}
@@ -62,19 +61,19 @@ void Manager::onConnectedChanged()
QString message = QString("%1 %2")
.arg(watch->name().isEmpty() ? "Pebble" : watch->name())
.arg(watch->isConnected() ? "connected" : "disconnected");
- qDebug() << message;
+ logger()->debug() << message;
if (notification.isPublished()) notification.remove();
notification.setBody(message);
if (!notification.publish()) {
- qDebug() << "Failed publishing notification";
+ logger()->debug() << "Failed publishing notification";
}
}
void Manager::onActiveVoiceCallChanged()
{
- qDebug() << "Manager::onActiveVoiceCallChanged()";
+ logger()->debug() << "Manager::onActiveVoiceCallChanged()";
VoiceCallHandler* handler = voice->activeVoiceCall();
if (handler) {
@@ -87,11 +86,11 @@ void Manager::onActiveVoiceCallStatusChanged()
{
VoiceCallHandler* handler = voice->activeVoiceCall();
if (!handler) {
- qWarning() << "ActiveVoiceCallStatusChanged but no activeVoiceCall??";
+ logger()->debug() << "ActiveVoiceCallStatusChanged but no activeVoiceCall??";
return;
}
- qDebug() << "handlerId:" << handler->handlerId()
+ logger()->debug() << "handlerId:" << handler->handlerId()
<< "providerId:" << handler->providerId()
<< "status:" << handler->status()
<< "statusText:" << handler->statusText()
@@ -99,28 +98,28 @@ void Manager::onActiveVoiceCallStatusChanged()
<< "incoming:" << handler->isIncoming();
if (!watch->isConnected()) {
- qDebug() << "Watch is not connected";
+ logger()->debug() << "Watch is not connected";
return;
}
switch ((VoiceCallHandler::VoiceCallStatus)handler->status()) {
case VoiceCallHandler::STATUS_ALERTING:
case VoiceCallHandler::STATUS_DIALING:
- qDebug() << "Tell outgoing:" << handler->lineId();
+ logger()->debug() << "Tell outgoing:" << handler->lineId();
watch->ring(handler->lineId(), findPersonByNumber(handler->lineId()), false);
break;
case VoiceCallHandler::STATUS_INCOMING:
case VoiceCallHandler::STATUS_WAITING:
- qDebug() << "Tell incoming:" << handler->lineId();
+ logger()->debug() << "Tell incoming:" << handler->lineId();
watch->ring(handler->lineId(), findPersonByNumber(handler->lineId()));
break;
case VoiceCallHandler::STATUS_NULL:
case VoiceCallHandler::STATUS_DISCONNECTED:
- qDebug() << "Endphone";
+ logger()->debug() << "Endphone";
watch->endPhoneCall();
break;
case VoiceCallHandler::STATUS_ACTIVE:
- qDebug() << "Startphone";
+ logger()->debug() << "Startphone";
watch->startPhoneCall();
break;
case VoiceCallHandler::STATUS_HELD:
@@ -142,7 +141,7 @@ QString Manager::findPersonByNumber(QString number)
void Manager::onVoiceError(const QString &message)
{
- qWarning() << "Error: " << message;
+ logger()->error() << "Error: " << message;
}
@@ -153,18 +152,18 @@ void Manager::onNotifyError(const QString &message)
void Manager::onSmsNotify(const QString &sender, const QString &data)
{
- qDebug() << "SMS:";
- qDebug() << sender;
- qDebug() << data;
+ logger()->debug() << "SMS:";
+ logger()->debug() << sender;
+ logger()->debug() << data;
watch->sendSMSNotification(sender, data);
}
void Manager::onEmailNotify(const QString &sender, const QString &data,const QString &subject)
{
- qDebug() << "Email:";
- qDebug() << sender;
- qDebug() << data;
- qDebug() << subject;
+ logger()->debug() << "Email:";
+ logger()->debug() << sender;
+ logger()->debug() << data;
+ logger()->debug() << subject;
watch->sendEmailNotification(sender, data, subject);
}
diff --git a/daemon/manager.h b/daemon/manager.h
index 4bf8f98..45ba50a 100644
--- a/daemon/manager.h
+++ b/daemon/manager.h
@@ -11,12 +11,14 @@
#include <QtContacts/QContactManager>
#include <QtContacts/QContactDetailFilter>
#include <MNotification>
+#include "Logger"
using namespace QtContacts;
class Manager : public QObject
{
Q_OBJECT
+ LOG4QT_DECLARE_QCLASS_LOGGER
friend class PebbledProxy;
diff --git a/daemon/voicecallhandler.cpp b/daemon/voicecallhandler.cpp
index 1a006f1..428785f 100644
--- a/daemon/voicecallhandler.cpp
+++ b/daemon/voicecallhandler.cpp
@@ -48,7 +48,7 @@ VoiceCallHandler::VoiceCallHandler(const QString &handlerId, QObject *parent)
: QObject(parent), d_ptr(new VoiceCallHandlerPrivate(this, handlerId))
{
Q_D(VoiceCallHandler);
- qDebug() << QString("Creating D-Bus interface to: ") + handlerId;
+ logger()->debug() << QString("Creating D-Bus interface to: ") + handlerId;
d->interface = new QDBusInterface("org.nemomobile.voicecall",
"/calls/" + handlerId,
"org.nemomobile.voicecall.VoiceCall",
@@ -66,7 +66,6 @@ VoiceCallHandler::~VoiceCallHandler()
void VoiceCallHandler::initialize(bool notifyError)
{
Q_D(VoiceCallHandler);
- bool success = false;
/*
method return sender=:1.13 -> dest=:1.150 reply_serial=2
@@ -154,31 +153,20 @@ method return sender=:1.13 -> dest=:1.150 reply_serial=2
"
*/
- if(d->interface->isValid())
- {
- success = true;
- success &= (bool)QObject::connect(d->interface, SIGNAL(error(QString)), SIGNAL(error(QString)));
- success &= (bool)QObject::connect(d->interface, SIGNAL(statusChanged()), SLOT(onStatusChanged()));
- success &= (bool)QObject::connect(d->interface, SIGNAL(lineIdChanged()), SLOT(onLineIdChanged()));
- success &= (bool)QObject::connect(d->interface, SIGNAL(durationChanged()), SLOT(onDurationChanged()));
- success &= (bool)QObject::connect(d->interface, SIGNAL(startedAtChanged()), SLOT(onStartedAtChanged()));
- success &= (bool)QObject::connect(d->interface, SIGNAL(emergencyChanged()), SLOT(onEmergencyChanged()));
- success &= (bool)QObject::connect(d->interface, SIGNAL(multipartyChanged()), SLOT(onMultipartyChanged()));
- success &= (bool)QObject::connect(d->interface, SIGNAL(forwardedChanged()), SLOT(onForwardedChanged()));
- }
-
- if(!(d->connected = success))
+ if (not d->connected)
{
QTimer::singleShot(2000, this, SLOT(initialize()));
if(notifyError) emit this->error("Failed to connect to VCM D-Bus service.");
- } else {
+ }
+ else if (d->interface->isValid()) {
QDBusInterface props(d->interface->service(), d->interface->path(),
"org.freedesktop.DBus.Properties", d->interface->connection());
QDBusReply<QVariantMap> reply = props.call("GetAll", d->interface->interface());
if (reply.isValid()) {
QVariantMap props = reply.value();
- qDebug() << props;
+ QString str; QDebug(&str) << props;
+ logger()->debug() << str;
d->providerId = props["providerId"].toString();
d->duration = props["duration"].toInt();
d->status = props["status"].toInt();
@@ -195,10 +183,20 @@ method return sender=:1.13 -> dest=:1.150 reply_serial=2
emit multipartyChanged();
emit emergencyChanged();
emit forwardedChanged();
- } else if (notifyError) {
- qWarning() << "Failed to get VoiceCall properties from VCM D-Bus service.";
- emit this->error("Failed to get VoiceCall properties from VCM D-Bus service.");
}
+ else {
+ logger()->error() << "Failed to get VoiceCall properties from VCM D-Bus service.";
+ if (notifyError) emit this->error("Failed to get VoiceCall properties from VCM D-Bus service.");
+ }
+
+ connect(d->interface, SIGNAL(error(QString)), SIGNAL(error(QString)));
+ connect(d->interface, SIGNAL(statusChanged()), SLOT(onStatusChanged()));
+ connect(d->interface, SIGNAL(lineIdChanged()), SLOT(onLineIdChanged()));
+ connect(d->interface, SIGNAL(durationChanged()), SLOT(onDurationChanged()));
+ connect(d->interface, SIGNAL(startedAtChanged()), SLOT(onStartedAtChanged()));
+ connect(d->interface, SIGNAL(emergencyChanged()), SLOT(onEmergencyChanged()));
+ connect(d->interface, SIGNAL(multipartyChanged()), SLOT(onMultipartyChanged()));
+ connect(d->interface, SIGNAL(forwardedChanged()), SLOT(onForwardedChanged()));
}
}
@@ -413,10 +411,10 @@ void VoiceCallHandler::onPendingCallFinished(QDBusPendingCallWatcher *watcher)
QDBusPendingReply<bool> reply = *watcher;
if (reply.isError()) {
- qWarning() << QString::fromLatin1("Received error reply for member: %1 (%2)").arg(reply.reply().member()).arg(reply.error().message());
+ logger()->error() << QString::fromLatin1("Received error reply for member: %1 (%2)").arg(reply.reply().member()).arg(reply.error().message());
emit this->error(reply.error().message());
watcher->deleteLater();
} else {
- qDebug() << QString::fromLatin1("Received successful reply for member: %1").arg(reply.reply().member());
+ logger()->debug() << QString::fromLatin1("Received successful reply for member: %1").arg(reply.reply().member());
}
}
diff --git a/daemon/voicecallhandler.h b/daemon/voicecallhandler.h
index 678d8f8..f96a97a 100644
--- a/daemon/voicecallhandler.h
+++ b/daemon/voicecallhandler.h
@@ -3,12 +3,13 @@
#include <QObject>
#include <QDateTime>
-
#include <QDBusPendingCallWatcher>
+#include "Logger"
class VoiceCallHandler : public QObject
{
Q_OBJECT
+ LOG4QT_DECLARE_QCLASS_LOGGER
Q_ENUMS(VoiceCallStatus)
diff --git a/daemon/voicecallmanager.cpp b/daemon/voicecallmanager.cpp
index 90a3812..21d3d49 100644
--- a/daemon/voicecallmanager.cpp
+++ b/daemon/voicecallmanager.cpp
@@ -98,7 +98,7 @@ QString VoiceCallManager::defaultProviderId() const
{
Q_D(const VoiceCallManager);
if(d->providers.count() == 0) {
- qWarning() << Q_FUNC_INFO << "No provider added";
+ logger()->debug() << Q_FUNC_INFO << "No provider added";
return QString::null;
}
@@ -288,7 +288,7 @@ void VoiceCallManager::onPendingCallFinished(QDBusPendingCallWatcher *watcher)
if (reply.isError()) {
emit this->error(reply.error().message());
} else {
- qDebug() << QString("Received successful reply for member: ") + reply.reply().member();
+ logger()->debug() << QString("Received successful reply for member: ") + reply.reply().member();
}
watcher->deleteLater();
@@ -301,7 +301,7 @@ void VoiceCallManager::onPendingSilenceFinished(QDBusPendingCallWatcher *watcher
if (reply.isError()) {
emit this->error(reply.error().message());
} else {
- qDebug() << QString("Received successful reply for member: ") + reply.reply().member();
+ logger()->debug() << QString("Received successful reply for member: ") + reply.reply().member();
}
watcher->deleteLater();
diff --git a/daemon/voicecallmanager.h b/daemon/voicecallmanager.h
index de18781..afb9a11 100644
--- a/daemon/voicecallmanager.h
+++ b/daemon/voicecallmanager.h
@@ -4,9 +4,9 @@
#include "voicecallhandler.h"
#include <QObject>
-
#include <QDBusInterface>
#include <QDBusPendingCallWatcher>
+#include "Logger"
class VoiceCallProviderData
{
@@ -27,6 +27,7 @@ typedef QList<VoiceCallHandler*> VoiceCallHandlerList;
class VoiceCallManager : public QObject
{
Q_OBJECT
+ LOG4QT_DECLARE_QCLASS_LOGGER
Q_PROPERTY(QDBusInterface* interface READ interface)
diff --git a/daemon/watchconnector.cpp b/daemon/watchconnector.cpp
index 9d8676e..48fd52f 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()
{
@@ -18,10 +21,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 +35,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 +43,17 @@ void WatchConnector::reconnect()
void WatchConnector::disconnect()
{
- qDebug() << __FUNCTION__;
+ logger()->debug() << __FUNCTION__;
socket->close();
socket->deleteLater();
+ reconnectTimer.stop();
+ logger()->debug() << "Stopped reconnect timer";
}
void WatchConnector::handleWatch(const QString &name, const QString &address)
{
- qDebug() << "handleWatch" << name << address;
+ logger()->debug() << "handleWatch " << name << " " << address;
+ reconnectTimer.stop();
if (socket != nullptr && socket->isOpen()) {
socket->close();
socket->deleteLater();
@@ -58,7 +64,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()));
@@ -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)
@@ -128,8 +134,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 +147,7 @@ void WatchConnector::decodeMsg(QByteArray data)
void WatchConnector::onReadSocket()
{
- qDebug() << "read";
+ logger()->debug() << "read";
QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
if (!socket) return;
@@ -155,32 +161,35 @@ void WatchConnector::onReadSocket()
void WatchConnector::onConnected()
{
- qDebug() << "Connected!";
+ logger()->debug() << "Connected!";
bool was_connected = is_connected;
is_connected = true;
+ reconnectTimer.stop();
+ reconnectTimer.setInterval(0);
if (not was_connected) emit connectedChanged();
}
void WatchConnector::onDisconnected()
{
- qDebug() << "Disconnected!";
+ logger()->debug() << "Disconnected!";
bool was_connected = is_connected;
is_connected = false;
+ if (was_connected) emit connectedChanged();
+
QBluetoothSocket *socket = qobject_cast<QBluetoothSocket *>(sender());
if (!socket) return;
- if (was_connected) emit connectedChanged();
-
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) {
- qWarning() << "Error connecting Pebble" << error << socket->errorString();
+ logger()->error() << "Error connecting Pebble: " << error << socket->errorString();
}
void WatchConnector::sendData(const QByteArray &data)
@@ -192,7 +201,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
diff --git a/daemon/watchconnector.h b/daemon/watchconnector.h
index eaeccdc..36d0936 100644
--- a/daemon/watchconnector.h
+++ b/daemon/watchconnector.h
@@ -33,9 +33,11 @@
#include <QObject>
#include <QPointer>
#include <QStringList>
+#include <QTimer>
#include <QBluetoothDeviceInfo>
#include <QBluetoothSocket>
#include <QBluetoothServiceInfo>
+#include "Logger"
using namespace QtBluetooth;
@@ -45,8 +47,11 @@ namespace watch
class WatchConnector : public QObject
{
Q_OBJECT
+ LOG4QT_DECLARE_QCLASS_LOGGER
+
Q_PROPERTY(QString name READ name NOTIFY nameChanged)
Q_PROPERTY(QString connected READ isConnected NOTIFY connectedChanged)
+
public:
enum {
watchTIME = 11,
@@ -125,6 +130,7 @@ private:
QPointer<QBluetoothSocket> socket;
bool is_connected;
+ QTimer reconnectTimer;
QString _last_name;
QString _last_address;
};