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 "iiosensorproxyorientationsensor.h"
6#include "sensorproxy_interface.h"
7
8#include <QtDBus/QDBusPendingReply>
9
10using namespace QtSensorsPrivate;
11
12char const * const IIOSensorProxyOrientationSensor::id("iio-sensor-proxy.orientationsensor");
13
14static inline QString dbusPath() { return QStringLiteral("/net/hadess/SensorProxy"); }
15
16IIOSensorProxyOrientationSensor::IIOSensorProxyOrientationSensor(QSensor *sensor)
17 : IIOSensorProxySensorBase(dbusPath(), NetHadessSensorProxyInterface::staticInterfaceName(), sensor)
18{
19 setReading<QOrientationReading>(&m_reading);
20 m_sensorProxyInterface = new NetHadessSensorProxyInterface(serviceName(), dbusPath(),
21 QDBusConnection::systemBus(), this);
22}
23
24IIOSensorProxyOrientationSensor::~IIOSensorProxyOrientationSensor()
25{
26}
27
28void IIOSensorProxyOrientationSensor::start()
29{
30 if (isServiceRunning()) {
31 if (m_sensorProxyInterface->hasAccelerometer()) {
32 QDBusPendingReply<> reply = m_sensorProxyInterface->ClaimAccelerometer();
33 reply.waitForFinished();
34 if (!reply.isError()) {
35 QString orientation = m_sensorProxyInterface->accelerometerOrientation();
36 updateOrientation(orientation);
37 return;
38 }
39 }
40 }
41 sensorStopped();
42}
43
44void IIOSensorProxyOrientationSensor::stop()
45{
46 if (isServiceRunning()) {
47 QDBusPendingReply<> reply = m_sensorProxyInterface->ReleaseAccelerometer();
48 reply.waitForFinished();
49 }
50 sensorStopped();
51}
52
53void IIOSensorProxyOrientationSensor::updateProperties(const QVariantMap &changedProperties)
54{
55 if (changedProperties.contains(key: "AccelerometerOrientation")) {
56 QString orientation = changedProperties.value(key: "AccelerometerOrientation").toString();
57 updateOrientation(orientation);
58 }
59}
60
61void IIOSensorProxyOrientationSensor::updateOrientation(const QString &orientation)
62{
63 QOrientationReading::Orientation o = QOrientationReading::Undefined;
64 if (orientation == QLatin1String("normal"))
65 o = QOrientationReading::TopUp;
66 else if (orientation == QLatin1String("bottom-up"))
67 o = QOrientationReading::TopDown;
68 else if (orientation == QLatin1String("left-up"))
69 o = QOrientationReading::LeftUp;
70 else if (orientation == QLatin1String("right-up"))
71 o = QOrientationReading::RightUp;
72
73 m_reading.setOrientation(o);
74 m_reading.setTimestamp(produceTimestamp());
75 newReadingAvailable();
76}
77

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