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
15using namespace QtSensorsPrivate;
16
17quint64 IIOSensorProxySensorBase::produceTimestamp()
18{
19 struct timespec tv;
20 int ok;
21
22#ifdef CLOCK_MONOTONIC_RAW
23 ok = clock_gettime(CLOCK_MONOTONIC_RAW, tp: &tv);
24 if (ok != 0)
25#endif
26 ok = clock_gettime(CLOCK_MONOTONIC, tp: &tv);
27 Q_ASSERT(ok == 0);
28
29 quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds
30 return result;
31}
32
33IIOSensorProxySensorBase::IIOSensorProxySensorBase(const QString& dbusPath, const QString dbusIface, QSensor *sensor)
34 : QSensorBackend(sensor)
35 , m_dbusInterface(dbusIface)
36{
37 QDBusServiceWatcher *watcher = new QDBusServiceWatcher(serviceName(), QDBusConnection::systemBus(),
38 QDBusServiceWatcher::WatchForRegistration |
39 QDBusServiceWatcher::WatchForUnregistration, this);
40 connect(sender: watcher, SIGNAL(serviceRegistered(QString)),
41 receiver: this, SLOT(serviceRegistered()));
42 connect(sender: watcher, SIGNAL(serviceUnregistered(QString)),
43 receiver: this, SLOT(serviceUnregistered()));
44
45 m_serviceRunning = QDBusConnection::systemBus().interface()->isServiceRegistered(serviceName: serviceName());
46
47 m_propertiesInterface = new OrgFreedesktopDBusPropertiesInterface(serviceName(), dbusPath,
48 QDBusConnection::systemBus(), this);
49 connect(m_propertiesInterface, SIGNAL(PropertiesChanged(QString,QVariantMap,QStringList)),
50 this, SLOT(propertiesChanged(QString,QVariantMap,QStringList)));
51}
52
53IIOSensorProxySensorBase::~IIOSensorProxySensorBase()
54{
55}
56
57QString IIOSensorProxySensorBase::serviceName() const
58{
59 return QLatin1String("net.hadess.SensorProxy");
60}
61
62void IIOSensorProxySensorBase::serviceRegistered()
63{
64 m_serviceRunning = true;
65}
66
67void IIOSensorProxySensorBase::serviceUnregistered()
68{
69 m_serviceRunning = false;
70 sensorStopped();
71}
72
73void IIOSensorProxySensorBase::propertiesChanged(const QString &interface,
74 const QVariantMap &changedProperties,
75 const QStringList &/*invalidatedProperties*/)
76{
77 if (interface == m_dbusInterface)
78 updateProperties(changedProperties);
79}
80

source code of qtsensors/src/plugins/sensors/iio-sensor-proxy/iiosensorproxysensorbase.cpp