1 | // Copyright (C) 2016 Alexander Volkov <a.volkov@rusbitech.ru> |
2 | // Copyright (C) 2016 The Qt Company Ltd. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include "iiosensorproxysensorbase.h" |
6 | #include "sensorproxy_interface.h" |
7 | #include "properties_interface.h" |
8 | |
9 | #include <QtDBus/QDBusConnection> |
10 | #include <QtDBus/QDBusServiceWatcher> |
11 | #include <QtDBus/QDBusConnectionInterface> |
12 | |
13 | #include <time.h> |
14 | |
15 | quint64 IIOSensorProxySensorBase::produceTimestamp() |
16 | { |
17 | struct timespec tv; |
18 | int ok; |
19 | |
20 | #ifdef CLOCK_MONOTONIC_RAW |
21 | ok = clock_gettime(CLOCK_MONOTONIC_RAW, tp: &tv); |
22 | if (ok != 0) |
23 | #endif |
24 | ok = clock_gettime(CLOCK_MONOTONIC, tp: &tv); |
25 | Q_ASSERT(ok == 0); |
26 | |
27 | quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds |
28 | return result; |
29 | } |
30 | |
31 | IIOSensorProxySensorBase::IIOSensorProxySensorBase(const QString& dbusPath, const QString dbusIface, QSensor *sensor) |
32 | : QSensorBackend(sensor) |
33 | , m_dbusInterface(dbusIface) |
34 | { |
35 | QDBusServiceWatcher *watcher = new QDBusServiceWatcher(serviceName(), QDBusConnection::systemBus(), |
36 | QDBusServiceWatcher::WatchForRegistration | |
37 | QDBusServiceWatcher::WatchForUnregistration, this); |
38 | connect(sender: watcher, SIGNAL(serviceRegistered(QString)), |
39 | receiver: this, SLOT(serviceRegistered())); |
40 | connect(sender: watcher, SIGNAL(serviceUnregistered(QString)), |
41 | receiver: this, SLOT(serviceUnregistered())); |
42 | |
43 | m_serviceRunning = QDBusConnection::systemBus().interface()->isServiceRegistered(serviceName: serviceName()); |
44 | |
45 | m_propertiesInterface = new OrgFreedesktopDBusPropertiesInterface(serviceName(), dbusPath, |
46 | QDBusConnection::systemBus(), this); |
47 | connect(m_propertiesInterface, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList)), |
48 | this, SLOT(propertiesChanged(QString,QVariantMap,QStringList))); |
49 | } |
50 | |
51 | IIOSensorProxySensorBase::~IIOSensorProxySensorBase() |
52 | { |
53 | } |
54 | |
55 | QString IIOSensorProxySensorBase::serviceName() const |
56 | { |
57 | return QLatin1String("net.hadess.SensorProxy" ); |
58 | } |
59 | |
60 | void IIOSensorProxySensorBase::serviceRegistered() |
61 | { |
62 | m_serviceRunning = true; |
63 | } |
64 | |
65 | void IIOSensorProxySensorBase::serviceUnregistered() |
66 | { |
67 | m_serviceRunning = false; |
68 | sensorStopped(); |
69 | } |
70 | |
71 | void IIOSensorProxySensorBase::propertiesChanged(const QString &interface, |
72 | const QVariantMap &changedProperties, |
73 | const QStringList &/*invalidatedProperties*/) |
74 | { |
75 | if (interface == m_dbusInterface) |
76 | updateProperties(changedProperties); |
77 | } |
78 | |