summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/bluez/bluezclient.cpp
diff options
context:
space:
mode:
authorAndrew Branson <andrew.branson@cern.ch>2016-02-11 23:55:16 +0100
committerAndrew Branson <andrew.branson@cern.ch>2016-02-11 23:55:16 +0100
commit29aaea2d80a9eb1715b6cddfac2d2aacf76358bd (patch)
tree012795b6bec16c72f38d33cff46324c9a0225868 /rockworkd/libpebble/bluez/bluezclient.cpp
launchpad ~mzanetti/rockwork/trunk r87
Diffstat (limited to 'rockworkd/libpebble/bluez/bluezclient.cpp')
-rw-r--r--rockworkd/libpebble/bluez/bluezclient.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/rockworkd/libpebble/bluez/bluezclient.cpp b/rockworkd/libpebble/bluez/bluezclient.cpp
new file mode 100644
index 0000000..8cdf848
--- /dev/null
+++ b/rockworkd/libpebble/bluez/bluezclient.cpp
@@ -0,0 +1,84 @@
+#include "bluezclient.h"
+#include "dbus-shared.h"
+
+#include <QDBusConnection>
+#include <QDBusReply>
+#include <QDebug>
+
+BluezClient::BluezClient(QObject *parent):
+ QObject(parent),
+ m_dbus(QDBusConnection::systemBus()),
+ m_bluezManager("org.bluez", "/", m_dbus),
+ m_bluezAgentManager("org.bluez", "/org/bluez", m_dbus)
+{
+ qDBusRegisterMetaType<InterfaceList>();
+ qDBusRegisterMetaType<ManagedObjectList>();
+
+ if (m_bluezManager.isValid()) {
+ connect(&m_bluezManager, SIGNAL(InterfacesAdded(const QDBusObjectPath&, InterfaceList)),
+ this, SLOT(slotInterfacesAdded(const QDBusObjectPath&, InterfaceList)));
+
+ connect(&m_bluezManager, SIGNAL(InterfacesRemoved(const QDBusObjectPath&, const QStringList&)),
+ this, SLOT(slotInterfacesRemoved(const QDBusObjectPath&, const QStringList&)));
+
+ auto objectList = m_bluezManager.GetManagedObjects().argumentAt<0>();
+ for (QDBusObjectPath path : objectList.keys()) {
+ InterfaceList ifaces = objectList.value(path);
+ if (ifaces.contains(BLUEZ_DEVICE_IFACE)) {
+ QString candidatePath = path.path();
+
+ auto properties = ifaces.value(BLUEZ_DEVICE_IFACE);
+ addDevice(path, properties);
+ }
+ }
+ }
+}
+
+QList<Device> BluezClient::pairedPebbles() const
+{
+ QList<Device> ret;
+ if (m_bluezManager.isValid()) {
+ foreach (const Device &dev, m_devices) {
+ ret << dev;
+ }
+ }
+ return ret;
+}
+
+void BluezClient::addDevice(const QDBusObjectPath &path, const QVariantMap &properties)
+{
+ QString address = properties.value("Address").toString();
+ QString name = properties.value("Name").toString();
+ if (name.startsWith("Pebble") && !name.startsWith("Pebble Time LE") && !name.startsWith("Pebble-LE") && !m_devices.contains(address)) {
+ qDebug() << "Found new Pebble:" << address << name;
+ Device device;
+ device.address = QBluetoothAddress(address);
+ device.name = name;
+ device.path = path.path();
+ m_devices.insert(path.path(), device);
+ qDebug() << "emitting added";
+ emit devicesChanged();
+ }
+}
+
+void BluezClient::slotInterfacesAdded(const QDBusObjectPath &path, InterfaceList ifaces)
+{
+ qDebug() << "Interface added!";
+ if (ifaces.contains(BLUEZ_DEVICE_IFACE)) {
+ auto properties = ifaces.value(BLUEZ_DEVICE_IFACE);
+ addDevice(path, properties);
+ }
+}
+
+void BluezClient::slotInterfacesRemoved(const QDBusObjectPath &path, const QStringList &ifaces)
+{
+ qDebug() << "interfaces removed" << path.path() << ifaces;
+ if (!ifaces.contains(BLUEZ_DEVICE_IFACE)) {
+ return;
+ }
+ if (m_devices.contains(path.path())) {
+ m_devices.take(path.path());
+ qDebug() << "removing dev";
+ emit devicesChanged();
+ }
+}