summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuha Vuolle <juha.vuolle@insta.fi>2021-06-08 16:46:33 +0300
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-06-15 09:39:08 +0000
commit7a82ee1645ef0df9c766031edd20ea99d5ea7b6e (patch)
tree6ba9bd20bdc08a7b98a416d9b6495cbfa39516e0
parent3fc0d1114a17d37a84ffc620394180c19a579deb (diff)
Make sensor possible to indicate it is no longer busy
The current implementation allowed sensor backend to only indicate that it is busy, but not to tell when it was freed again. This commit allows sensor backend to indicate also that it's busy state has cleared. It is up to the sensor implementation to decide if it makes sense / is possible to do that or not. Task-number: QTBUG-92513 Task-number: QTBUG-92505 Change-Id: Ied4857850e81346031fd83aa347d9955081118e8 Reviewed-by: Alex Blasche <alexander.blasche@qt.io> (cherry picked from commit f8445fdcbf75e455443304bc290c48c37961e9f1) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/sensors/doc/src/qt6-changes.qdoc13
-rw-r--r--src/sensors/qsensor.h2
-rw-r--r--src/sensors/qsensorbackend.cpp18
-rw-r--r--src/sensors/qsensorbackend.h2
-rw-r--r--src/sensorsquick/qmlsensor.cpp1
-rw-r--r--src/sensorsquick/qmlsensor_p.h3
-rw-r--r--tests/auto/common/test_backends.cpp9
-rw-r--r--tests/auto/common/test_backends.h1
-rw-r--r--tests/auto/qml/qml_quick/tst_sensors_basic.qml24
-rw-r--r--tests/auto/qml/qml_quick/tst_sensors_qmlquick.cpp4
-rw-r--r--tests/auto/qsensor/tst_qsensor.cpp18
11 files changed, 85 insertions, 10 deletions
diff --git a/src/sensors/doc/src/qt6-changes.qdoc b/src/sensors/doc/src/qt6-changes.qdoc
index 7baad9f0..f71cde15 100644
--- a/src/sensors/doc/src/qt6-changes.qdoc
+++ b/src/sensors/doc/src/qt6-changes.qdoc
@@ -66,4 +66,17 @@
The property name is now aligned with the \c frontLidClosed property of
the same QML type.
+ \section2 QSensorBackend::sensorBusy() and QSensor::isBusy()
+
+ The \c QSensor::busy property and its accessor, \c QSensor::isBusy(), can be used
+ to check if the sensor is busy or not. To allow this property to better reflect the
+ sensor backend's state, QSensorBackend::sensorBusy() now accepts a boolean parameter.
+ This \c busy parameter is used to set/unset the backend busy state and notify the
+ QSensor interface attached to it.
+
+ The default value for the QSensorBackend::sensorBusy() parameter is \e true which
+ results in the original behavior (except the added signal emission). This means
+ that in most if not all cases the changes are not mandatory. Instead it is up to the
+ individual sensor backend implementations to decide if the changes are useful.
+
*/
diff --git a/src/sensors/qsensor.h b/src/sensors/qsensor.h
index 9fdaab17..3bc4fd59 100644
--- a/src/sensors/qsensor.h
+++ b/src/sensors/qsensor.h
@@ -78,7 +78,7 @@ class Q_SENSORS_EXPORT QSensor : public QObject
Q_PROPERTY(qrangelist availableDataRates READ availableDataRates)
Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged)
Q_PROPERTY(QSensorReading* reading READ reading NOTIFY readingChanged)
- Q_PROPERTY(bool busy READ isBusy)
+ Q_PROPERTY(bool busy READ isBusy NOTIFY busyChanged)
Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
Q_PROPERTY(qoutputrangelist outputRanges READ outputRanges)
Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange)
diff --git a/src/sensors/qsensorbackend.cpp b/src/sensors/qsensorbackend.cpp
index 31e2e811..1e75644b 100644
--- a/src/sensors/qsensorbackend.cpp
+++ b/src/sensors/qsensorbackend.cpp
@@ -318,21 +318,27 @@ void QSensorBackend::sensorStopped()
}
/*!
- Inform the front end that the sensor is busy.
- This implicitly calls sensorStopped() and
- is typically called from start().
+ Inform the front end of the sensor's busy state according
+ to the provided \a busy parameter.
+
+ If the sensor is set \e busy this implicitly calls sensorStopped().
+ Busy indication is typically done in start().
Note that the front end must call QSensor::isBusy() to see if
the sensor is busy. If the sensor has stopped due to an error
the sensorError() function should be called to notify the class
of the error condition.
*/
-void QSensorBackend::sensorBusy()
+void QSensorBackend::sensorBusy(bool busy)
{
Q_D(QSensorBackend);
QSensorPrivate *sensorPrivate = d->m_sensor->d_func();
- sensorPrivate->active = false;
- sensorPrivate->busy = true;
+ if (sensorPrivate->busy == busy)
+ return;
+ if (busy)
+ sensorPrivate->active = false;
+ sensorPrivate->busy = busy;
+ emit d->m_sensor->busyChanged();
}
/*!
diff --git a/src/sensors/qsensorbackend.h b/src/sensors/qsensorbackend.h
index f786a9d9..58097c69 100644
--- a/src/sensors/qsensorbackend.h
+++ b/src/sensors/qsensorbackend.h
@@ -80,7 +80,7 @@ public:
// used by the backend to inform us of events
void newReadingAvailable();
void sensorStopped();
- void sensorBusy();
+ void sensorBusy(bool busy = true);
void sensorError(int error);
private:
diff --git a/src/sensorsquick/qmlsensor.cpp b/src/sensorsquick/qmlsensor.cpp
index 7957a663..14dc21c7 100644
--- a/src/sensorsquick/qmlsensor.cpp
+++ b/src/sensorsquick/qmlsensor.cpp
@@ -447,6 +447,7 @@ void QmlSensor::componentComplete()
connect(sensor(), SIGNAL(bufferSizeChanged(int)), this, SIGNAL(bufferSizeChanged(int)));
connect(sensor(), SIGNAL(maxBufferSizeChanged(int)), this, SIGNAL(maxBufferSizeChanged(int)));
connect(sensor(), SIGNAL(efficientBufferSizeChanged(int)), this, SIGNAL(efficientBufferSizeChanged(int)));
+ connect(sensor(), &QSensor::busyChanged, this, &QmlSensor::busyChanged);
// We need to set this on the sensor object now
sensor()->setIdentifier(m_identifier.toLocal8Bit());
diff --git a/src/sensorsquick/qmlsensor_p.h b/src/sensorsquick/qmlsensor_p.h
index 55d4eb42..2b893c99 100644
--- a/src/sensorsquick/qmlsensor_p.h
+++ b/src/sensorsquick/qmlsensor_p.h
@@ -78,7 +78,7 @@ class Q_SENSORSQUICK_PRIVATE_EXPORT QmlSensor : public QObject, public QQmlParse
Q_PROPERTY(QQmlListProperty<QmlSensorRange> availableDataRates READ availableDataRates NOTIFY availableDataRatesChanged)
Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged)
Q_PROPERTY(QmlSensorReading* reading READ reading NOTIFY readingChanged)
- Q_PROPERTY(bool busy READ isBusy)
+ Q_PROPERTY(bool busy READ isBusy NOTIFY busyChanged)
Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
Q_PROPERTY(QQmlListProperty<QmlSensorOutputRange> outputRanges READ outputRanges NOTIFY outputRangesChanged)
Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange NOTIFY outputRangeChanged)
@@ -173,6 +173,7 @@ Q_SIGNALS:
void descriptionChanged();
void errorChanged();
void alwaysOnChanged();
+ void busyChanged();
Q_REVISION(1) void skipDuplicatesChanged(bool skipDuplicates);
Q_REVISION(1) void axesOrientationModeChanged(AxesOrientationMode axesOrientationMode);
Q_REVISION(1) void currentOrientationChanged(int currentOrientation);
diff --git a/tests/auto/common/test_backends.cpp b/tests/auto/common/test_backends.cpp
index aed4746e..15419b0e 100644
--- a/tests/auto/common/test_backends.cpp
+++ b/tests/auto/common/test_backends.cpp
@@ -55,8 +55,16 @@ static bool registerTestBackend(const char *className, CreateFunc func)
// the sensor reading values in the backend
static QMap<QSensor*, QSensorBackend*> sensorToBackend;
+void set_test_backend_busy(QSensor* sensor, bool busy)
+{
+ Q_ASSERT(sensor->isConnectedToBackend());
+ QSensorBackend* backend = sensorToBackend.value(sensor);
+ backend->sensorBusy(busy);
+}
+
void set_test_backend_reading(QSensor* sensor, const QJsonObject& values)
{
+ Q_ASSERT(sensor->isConnectedToBackend());
QSensorBackend* backend = sensorToBackend.value(sensor);
backend->reading()->setTimestamp(values["timestamp"].toInt()); // timestamp is common to all
if (sensor->type() == "QAccelerometer") {
@@ -182,4 +190,3 @@ void unregister_test_backends()
for (const Record &record : records)
QSensorManager::unregisterBackend(record.type, record.type);
}
-
diff --git a/tests/auto/common/test_backends.h b/tests/auto/common/test_backends.h
index 492fe7c1..66699b5c 100644
--- a/tests/auto/common/test_backends.h
+++ b/tests/auto/common/test_backends.h
@@ -35,6 +35,7 @@
void register_test_backends();
void unregister_test_backends();
void set_test_backend_reading(QSensor* sensor, const QJsonObject& values);
+void set_test_backend_busy(QSensor* sensor, bool busy);
#include <qaccelerometer.h>
#include <qaltimeter.h>
diff --git a/tests/auto/qml/qml_quick/tst_sensors_basic.qml b/tests/auto/qml/qml_quick/tst_sensors_basic.qml
index 0655fc36..298ecef8 100644
--- a/tests/auto/qml/qml_quick/tst_sensors_basic.qml
+++ b/tests/auto/qml/qml_quick/tst_sensors_basic.qml
@@ -42,6 +42,11 @@ TestCase {
signalName: "readingChanged"
}
+ SignalSpy {
+ id: sensorBusySpy
+ signalName: "busyChanged"
+ }
+
function init() {
TestControl.registerTestBackends()
}
@@ -50,6 +55,25 @@ TestCase {
TestControl.unregisterTestBackends()
}
+ function test_busy() {
+ var sensor = Qt.createQmlObject("import QtSensors; Accelerometer {identifier: \"QAccelerometer\"}", testCase);
+ sensorBusySpy.target = sensor
+ compare(sensor.busy, false)
+ verify(sensor.start())
+
+ // set sensor busy and verify 'busy' property and its signaling
+ TestControl.setSensorBusy(sensor, true)
+ compare(sensorBusySpy.count, 1)
+ TestControl.setSensorBusy(sensor, false)
+ compare(sensorBusySpy.count, 2)
+ TestControl.setSensorBusy(sensor, false)
+ compare(sensorBusySpy.count, 2)
+
+ // tidy up
+ sensor.destroy()
+ sensorBusySpy.clear()
+ }
+
function test_reading(data) {
var sensor = Qt.createQmlObject(
diff --git a/tests/auto/qml/qml_quick/tst_sensors_qmlquick.cpp b/tests/auto/qml/qml_quick/tst_sensors_qmlquick.cpp
index 7e52d3b8..8162601d 100644
--- a/tests/auto/qml/qml_quick/tst_sensors_qmlquick.cpp
+++ b/tests/auto/qml/qml_quick/tst_sensors_qmlquick.cpp
@@ -55,6 +55,10 @@ public slots:
void setSensorReading(const QmlSensor* qmlSensor, const QJsonObject& values) {
set_test_backend_reading(qmlSensor->sensor(), values);
}
+
+ void setSensorBusy(const QmlSensor* qmlSensor, bool busy) {
+ set_test_backend_busy(qmlSensor->sensor(), busy);
+ }
};
QUICK_TEST_MAIN_WITH_SETUP(tst_sensors_qmlquick, TestSetup)
diff --git a/tests/auto/qsensor/tst_qsensor.cpp b/tests/auto/qsensor/tst_qsensor.cpp
index 7eb9bba9..69c5c5f1 100644
--- a/tests/auto/qsensor/tst_qsensor.cpp
+++ b/tests/auto/qsensor/tst_qsensor.cpp
@@ -933,6 +933,24 @@ private slots:
// Now we can start the second instance
sensor2.start();
QVERIFY(sensor2.isActive());
+
+ // test 'busy' going back and forth and verify indication to frontend
+ register_test_backends();
+ QAccelerometer accelerometer;
+ accelerometer.setIdentifier("QAccelerometer");
+ QSignalSpy busySpy(&accelerometer, SIGNAL(busyChanged()));
+ QVERIFY(accelerometer.connectToBackend());
+ QVERIFY(!accelerometer.isBusy());
+ QCOMPARE(busySpy.count(), 0);
+
+ set_test_backend_busy(&accelerometer, true);
+ QCOMPARE(busySpy.count(), 1);
+ QVERIFY(accelerometer.isBusy());
+
+ set_test_backend_busy(&accelerometer, false);
+ QCOMPARE(busySpy.count(), 2);
+ QVERIFY(!accelerometer.isBusy());
+ unregister_test_backends();
}
void testIdenfifierChanged()