1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtSensors module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "test_sensorimpl.h"
30#include <QDebug>
31
32const char *testsensorimpl::id("test sensor impl");
33
34static testsensorimpl *exclusiveHandle = 0;
35
36testsensorimpl::testsensorimpl(QSensor *sensor)
37 : QSensorBackend(sensor)
38{
39 setReading<TestSensorReading>(&m_reading);
40 setDescription("sensor description");
41 addOutputRange(min: 0, max: 1, accuracy: 0.5);
42 addOutputRange(min: 0, max: 2, accuracy: 1);
43 QString doThis = sensor->property(name: "doThis").toString();
44 if (doThis == "rates(0)") {
45 setDataRates(0);
46 } else if (doThis == "rates(nodef)") {
47 TestSensor *acc = new TestSensor(this);
48 setDataRates(acc);
49 delete acc;
50 } else if (doThis == "rates") {
51 TestSensor *acc = new TestSensor(this);
52 acc->connectToBackend();
53 setDataRates(acc);
54 delete acc;
55 } else {
56 addDataRate(min: 100, max: 100);
57 }
58 reading();
59}
60
61testsensorimpl::~testsensorimpl()
62{
63 Q_ASSERT(exclusiveHandle != this);
64}
65
66void testsensorimpl::start()
67{
68 QVariant _exclusive = sensor()->property(name: "exclusive");
69 bool exclusive = _exclusive.isValid()?_exclusive.toBool():false;
70 if (exclusive) {
71 if (!exclusiveHandle) {
72 exclusiveHandle = this;
73 } else {
74 // Hook up the busyChanged signal
75 connect(sender: exclusiveHandle, SIGNAL(emitBusyChanged()), receiver: sensor(), SIGNAL(busyChanged()));
76 sensorBusy(); // report the busy condition
77 return;
78 }
79 }
80
81 QString doThis = sensor()->property(name: "doThis").toString();
82 if (doThis == "stop")
83 sensorStopped();
84 else if (doThis == "error")
85 sensorError(error: 1);
86 else if (doThis == "setOne") {
87 m_reading.setTimestamp(1);
88 m_reading.setTest(1);
89 newReadingAvailable();
90 } else {
91 m_reading.setTimestamp(2);
92 m_reading.setTest(2);
93 newReadingAvailable();
94 }
95}
96
97void testsensorimpl::stop()
98{
99 QVariant _exclusive = sensor()->property(name: "exclusive");
100 bool exclusive = _exclusive.isValid()?_exclusive.toBool():false;
101 if (exclusive && exclusiveHandle == this) {
102 exclusiveHandle = 0;
103 emit emitBusyChanged(); // notify any waiting instances that they can try to grab the sensor now
104 }
105}
106
107bool testsensorimpl::isFeatureSupported(QSensor::Feature feature) const
108{
109 return (feature == QSensor::AlwaysOn || feature == QSensor::GeoValues);
110}
111
112

source code of qtsensors/tests/auto/qsensor/test_sensorimpl.cpp