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