From 044486f8b41b9ae7bdfad838be79ec5b2782fed5 Mon Sep 17 00:00:00 2001 From: Lorn Potter Date: Tue, 8 Jul 2014 07:43:27 +1000 Subject: [sensorfw] need to run init after sensord has been restarted This ensures things are setup and work properly. Change-Id: I23c8b53e760d49695c5ae207eed77a2e7d2be3a4 Reviewed-by: Alex Blasche --- src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp | 7 +++++++ src/plugins/sensors/sensorfw/sensorfwaccelerometer.h | 1 + src/plugins/sensors/sensorfw/sensorfwals.cpp | 2 ++ src/plugins/sensors/sensorfw/sensorfwcompass.cpp | 7 +++++++ src/plugins/sensors/sensorfw/sensorfwcompass.h | 1 + src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp | 7 +++++++ src/plugins/sensors/sensorfw/sensorfwgyroscope.h | 1 + src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp | 7 +++++++ src/plugins/sensors/sensorfw/sensorfwirproximitysensor.h | 1 + src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp | 7 +++++++ src/plugins/sensors/sensorfw/sensorfwlightsensor.h | 1 + src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp | 2 ++ src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp | 2 ++ src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp | 2 ++ src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp | 7 +++++++ src/plugins/sensors/sensorfw/sensorfwrotationsensor.h | 1 + src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp | 3 +++ src/plugins/sensors/sensorfw/sensorfwsensorbase.h | 1 + src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp | 3 +++ 19 files changed, 63 insertions(+) diff --git a/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp b/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp index d38f1a0d..8efd127b 100644 --- a/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp @@ -96,3 +96,10 @@ void sensorfwaccelerometer::init() m_initDone = false; initSensor(m_initDone); } + +void sensorfwaccelerometer::start() +{ + if (reinitIsNeeded) + init(); + SensorfwSensorBase::start(); +} diff --git a/src/plugins/sensors/sensorfw/sensorfwaccelerometer.h b/src/plugins/sensors/sensorfw/sensorfwaccelerometer.h index e0e84cc3..e3ded24a 100644 --- a/src/plugins/sensors/sensorfw/sensorfwaccelerometer.h +++ b/src/plugins/sensors/sensorfw/sensorfwaccelerometer.h @@ -60,6 +60,7 @@ protected: bool doConnect() Q_DECL_OVERRIDE; QString sensorName() const Q_DECL_OVERRIDE; qreal correctionFactor() const Q_DECL_OVERRIDE; + void start() Q_DECL_OVERRIDE; virtual void init(); private: diff --git a/src/plugins/sensors/sensorfw/sensorfwals.cpp b/src/plugins/sensors/sensorfw/sensorfwals.cpp index 0de7e716..e5dd051b 100644 --- a/src/plugins/sensors/sensorfw/sensorfwals.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwals.cpp @@ -59,6 +59,8 @@ Sensorfwals::Sensorfwals(QSensor *sensor) void Sensorfwals::start() { + if (reinitIsNeeded) + init(); if (m_sensorInterface) { Unsigned data(((ALSSensorChannelInterface*)m_sensorInterface)->lux()); m_reading.setLightLevel(getLightLevel(data.x())); diff --git a/src/plugins/sensors/sensorfw/sensorfwcompass.cpp b/src/plugins/sensors/sensorfw/sensorfwcompass.cpp index ad41cfeb..e565bce3 100644 --- a/src/plugins/sensors/sensorfw/sensorfwcompass.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwcompass.cpp @@ -84,3 +84,10 @@ void SensorfwCompass::init() m_initDone = false; initSensor(m_initDone); } + +void SensorfwCompass::start() +{ + if (reinitIsNeeded) + init(); + SensorfwSensorBase::start(); +} diff --git a/src/plugins/sensors/sensorfw/sensorfwcompass.h b/src/plugins/sensors/sensorfw/sensorfwcompass.h index 951435c8..aa8e3bda 100644 --- a/src/plugins/sensors/sensorfw/sensorfwcompass.h +++ b/src/plugins/sensors/sensorfw/sensorfwcompass.h @@ -59,6 +59,7 @@ public: protected: bool doConnect() Q_DECL_OVERRIDE; QString sensorName() const Q_DECL_OVERRIDE; + void start() Q_DECL_OVERRIDE; virtual void init(); private: QCompassReading m_reading; diff --git a/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp b/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp index deca190f..6001d477 100644 --- a/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp @@ -95,3 +95,10 @@ void SensorfwGyroscope::init() m_initDone = false; initSensor(m_initDone); } + +void SensorfwGyroscope::start() +{ + if (reinitIsNeeded) + init(); + SensorfwSensorBase::start(); +} diff --git a/src/plugins/sensors/sensorfw/sensorfwgyroscope.h b/src/plugins/sensors/sensorfw/sensorfwgyroscope.h index f489bf6b..bfbad62c 100644 --- a/src/plugins/sensors/sensorfw/sensorfwgyroscope.h +++ b/src/plugins/sensors/sensorfw/sensorfwgyroscope.h @@ -62,6 +62,7 @@ protected: bool doConnect() Q_DECL_OVERRIDE; QString sensorName() const Q_DECL_OVERRIDE; qreal correctionFactor() const Q_DECL_OVERRIDE; + void start() Q_DECL_OVERRIDE; virtual void init(); private: diff --git a/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp b/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp index 4207735b..6855158e 100644 --- a/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp @@ -83,3 +83,10 @@ void SensorfwIrProximitySensor::init() m_initDone = false; initSensor(m_initDone); } + +void SensorfwIrProximitySensor::start() +{ + if (reinitIsNeeded) + init(); + SensorfwSensorBase::start(); +} diff --git a/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.h b/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.h index 642570d3..3c176ff1 100644 --- a/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.h +++ b/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.h @@ -58,6 +58,7 @@ public: protected: bool doConnect() Q_DECL_OVERRIDE; QString sensorName() const Q_DECL_OVERRIDE; + void start() Q_DECL_OVERRIDE; virtual void init(); private: QIRProximityReading m_reading; diff --git a/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp b/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp index 6607433e..6badd7ce 100644 --- a/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp @@ -75,3 +75,10 @@ void SensorfwLightSensor::init() m_initDone = false; initSensor(m_initDone); } + +void SensorfwLightSensor::start() +{ + if (reinitIsNeeded) + init(); + SensorfwSensorBase::start(); +} diff --git a/src/plugins/sensors/sensorfw/sensorfwlightsensor.h b/src/plugins/sensors/sensorfw/sensorfwlightsensor.h index 244f795f..911ec55e 100644 --- a/src/plugins/sensors/sensorfw/sensorfwlightsensor.h +++ b/src/plugins/sensors/sensorfw/sensorfwlightsensor.h @@ -59,6 +59,7 @@ public: protected: bool doConnect() Q_DECL_OVERRIDE; QString sensorName() const Q_DECL_OVERRIDE; + void start() Q_DECL_OVERRIDE; virtual void init(); private: QLightReading m_reading; diff --git a/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp b/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp index 10b1afe5..12465a30 100644 --- a/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp @@ -59,6 +59,8 @@ SensorfwMagnetometer::SensorfwMagnetometer(QSensor *sensor) void SensorfwMagnetometer::start() { + if (reinitIsNeeded) + init(); QMagnetometer *const magnetometer = qobject_cast(sensor()); if (magnetometer) m_isGeoMagnetometer = magnetometer->returnGeoValues(); diff --git a/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp b/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp index b45b7310..bd91ac26 100644 --- a/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp @@ -57,6 +57,8 @@ SensorfwOrientationSensor::SensorfwOrientationSensor(QSensor *sensor) void SensorfwOrientationSensor::start() { + if (reinitIsNeeded) + init(); if (m_sensorInterface) { Unsigned data(((OrientationSensorChannelInterface*)m_sensorInterface)->orientation()); m_reading.setOrientation(SensorfwOrientationSensor::getOrientation(data.x())); diff --git a/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp b/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp index d98dfd1a..4d91a024 100644 --- a/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp @@ -57,6 +57,8 @@ SensorfwProximitySensor::SensorfwProximitySensor(QSensor *sensor) void SensorfwProximitySensor::start() { + if (reinitIsNeeded) + init(); if (m_sensorInterface) { Unsigned data(((ProximitySensorChannelInterface*)m_sensorInterface)->proximity()); m_reading.setClose(data.x()? true: false); diff --git a/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp b/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp index c0ba0696..b63e7f6e 100644 --- a/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp @@ -86,3 +86,10 @@ void SensorfwRotationSensor::init() m_initDone = false; initSensor(m_initDone); } + +void SensorfwRotationSensor::start() +{ + if (reinitIsNeeded) + init(); + SensorfwSensorBase::start(); +} diff --git a/src/plugins/sensors/sensorfw/sensorfwrotationsensor.h b/src/plugins/sensors/sensorfw/sensorfwrotationsensor.h index dd415201..944d2c11 100644 --- a/src/plugins/sensors/sensorfw/sensorfwrotationsensor.h +++ b/src/plugins/sensors/sensorfw/sensorfwrotationsensor.h @@ -61,6 +61,7 @@ public: protected: bool doConnect() Q_DECL_OVERRIDE; QString sensorName() const Q_DECL_OVERRIDE; + void start() Q_DECL_OVERRIDE; virtual void init(); private: QRotationReading m_reading; diff --git a/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp b/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp index 299c20ed..299a75c9 100644 --- a/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp @@ -56,6 +56,7 @@ SensorfwSensorBase::SensorfwSensorBase(QSensor *sensor) : QSensorBackend(sensor), m_sensorInterface(0), m_bufferSize(-1), + reinitIsNeeded(false), m_prevOutputRange(0), m_efficientBufferSize(1), m_maxBufferSize(1), @@ -216,7 +217,9 @@ void SensorfwSensorBase::connectToSensord() m_remoteSensorManager = &SensorManagerInterface::instance(); if (running) { stop(); + reinitIsNeeded = true; start(); + reinitIsNeeded = false; } } diff --git a/src/plugins/sensors/sensorfw/sensorfwsensorbase.h b/src/plugins/sensors/sensorfw/sensorfwsensorbase.h index 769a8cbb..7cf56844 100644 --- a/src/plugins/sensors/sensorfw/sensorfwsensorbase.h +++ b/src/plugins/sensors/sensorfw/sensorfwsensorbase.h @@ -163,6 +163,7 @@ protected: int m_bufferSize; int bufferSize() const; virtual qreal correctionFactor() const; + bool reinitIsNeeded; private: diff --git a/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp b/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp index 1d7e950c..e6f61d9b 100644 --- a/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp @@ -58,6 +58,9 @@ SensorfwTapSensor::SensorfwTapSensor(QSensor *sensor) void SensorfwTapSensor::start() { + if (reinitIsNeeded) + init(); + QTapSensor * const tapSensor = qobject_cast(sensor()); bool b = tapSensor->returnDoubleTapEvents(); -- cgit v1.2.3 From 9f377d1fa286817a17a5e5de3786a2d73aeaa822 Mon Sep 17 00:00:00 2001 From: Lorn Potter Date: Mon, 7 Jul 2014 16:58:22 +1000 Subject: Series of crash fixes from Mer, squashed 1)tracing plugin loading 2)m_remoteSensorManager can be nullptr But it seems it does not solve incorrect initialization in the case manager interface is nullptr: init() for different sensors does not check initDone 3)split initSensor() to template function and function most of initSensor() does not depend on template param, so it is better to split it -> less code should be generated 4)do not use sensor manager if it is invalid 5)correct class members initialization order 6)assert sensor interface is not null From what I saw according to logic it should not happen so using Q_ASSERT for better maintainability 7)check local interface pointer in SensorfwTapSensor::start() Signed-off-by: Denis Zalevskiy Change-Id: I5c1bf3999ad2268c0dba9b3fe511d999c2e63fd9 Reviewed-by: Alex Blasche --- .../sensors/sensorfw/sensorfwaccelerometer.cpp | 1 + src/plugins/sensors/sensorfw/sensorfwals.cpp | 1 + src/plugins/sensors/sensorfw/sensorfwcompass.cpp | 1 + src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp | 1 + .../sensors/sensorfw/sensorfwirproximitysensor.cpp | 1 + .../sensors/sensorfw/sensorfwlightsensor.cpp | 1 + .../sensors/sensorfw/sensorfwmagnetometer.cpp | 1 + .../sensors/sensorfw/sensorfworientationsensor.cpp | 1 + .../sensors/sensorfw/sensorfwproximitysensor.cpp | 3 +- .../sensors/sensorfw/sensorfwrotationsensor.cpp | 1 + .../sensors/sensorfw/sensorfwsensorbase.cpp | 70 ++++++++++++++++++++ src/plugins/sensors/sensorfw/sensorfwsensorbase.h | 77 ++-------------------- src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp | 14 ++-- src/sensors/qsensormanager.cpp | 28 ++++++-- 14 files changed, 120 insertions(+), 81 deletions(-) diff --git a/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp b/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp index 8efd127b..0ac4e905 100644 --- a/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwaccelerometer.cpp @@ -74,6 +74,7 @@ void sensorfwaccelerometer::slotFrameAvailable(const QVector& frame) bool sensorfwaccelerometer::doConnect() { + Q_ASSERT(m_sensorInterface); if (m_bufferSize==1) return QObject::connect(m_sensorInterface, SIGNAL(dataAvailable(XYZ)), this, SLOT(slotDataAvailable(XYZ))); return QObject::connect(m_sensorInterface, SIGNAL(frameAvailable(QVector)),this, SLOT(slotFrameAvailable(QVector))); diff --git a/src/plugins/sensors/sensorfw/sensorfwals.cpp b/src/plugins/sensors/sensorfw/sensorfwals.cpp index e5dd051b..a061f14e 100644 --- a/src/plugins/sensors/sensorfw/sensorfwals.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwals.cpp @@ -83,6 +83,7 @@ void Sensorfwals::slotDataAvailable(const Unsigned& data) bool Sensorfwals::doConnect() { + Q_ASSERT(m_sensorInterface); return QObject::connect(m_sensorInterface, SIGNAL(ALSChanged(Unsigned)), this, SLOT(slotDataAvailable(Unsigned))); } diff --git a/src/plugins/sensors/sensorfw/sensorfwcompass.cpp b/src/plugins/sensors/sensorfw/sensorfwcompass.cpp index e565bce3..d2f02888 100644 --- a/src/plugins/sensors/sensorfw/sensorfwcompass.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwcompass.cpp @@ -70,6 +70,7 @@ void SensorfwCompass::slotDataAvailable(const Compass& data) bool SensorfwCompass::doConnect() { + Q_ASSERT(m_sensorInterface); return QObject::connect(m_sensorInterface, SIGNAL(dataAvailable(Compass)), this, SLOT(slotDataAvailable(Compass))); } diff --git a/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp b/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp index 6001d477..f802d0de 100644 --- a/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwgyroscope.cpp @@ -75,6 +75,7 @@ void SensorfwGyroscope::slotFrameAvailable(const QVector& frame) bool SensorfwGyroscope::doConnect() { + Q_ASSERT(m_sensorInterface); if (m_bufferSize==1) return QObject::connect(m_sensorInterface, SIGNAL(dataAvailable(XYZ)), this, SLOT(slotDataAvailable(XYZ))); return QObject::connect(m_sensorInterface, SIGNAL(frameAvailable(QVector)),this, SLOT(slotFrameAvailable(QVector))); diff --git a/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp b/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp index 6855158e..145276e3 100644 --- a/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwirproximitysensor.cpp @@ -67,6 +67,7 @@ void SensorfwIrProximitySensor::slotDataAvailable(const Proximity& proximity) bool SensorfwIrProximitySensor::doConnect() { + Q_ASSERT(m_sensorInterface); return QObject::connect(m_sensorInterface, SIGNAL(reflectanceDataAvailable(Proximity)), this, SLOT(slotDataAvailable(Proximity))); } diff --git a/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp b/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp index 6badd7ce..dda87ea5 100644 --- a/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwlightsensor.cpp @@ -61,6 +61,7 @@ void SensorfwLightSensor::slotDataAvailable(const Unsigned& data) bool SensorfwLightSensor::doConnect() { + Q_ASSERT(m_sensorInterface); return QObject::connect(m_sensorInterface, SIGNAL(ALSChanged(Unsigned)), this, SLOT(slotDataAvailable(Unsigned))); } diff --git a/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp b/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp index 12465a30..913d301a 100644 --- a/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwmagnetometer.cpp @@ -88,6 +88,7 @@ void SensorfwMagnetometer::slotFrameAvailable(const QVector& fram bool SensorfwMagnetometer::doConnect() { + Q_ASSERT(m_sensorInterface); if (m_bufferSize==1) return QObject::connect(m_sensorInterface, SIGNAL(dataAvailable(MagneticField)), this, SLOT(slotDataAvailable(MagneticField))); diff --git a/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp b/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp index bd91ac26..722d61b6 100644 --- a/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfworientationsensor.cpp @@ -78,6 +78,7 @@ void SensorfwOrientationSensor::slotDataAvailable(const Unsigned& data) bool SensorfwOrientationSensor::doConnect() { + Q_ASSERT(m_sensorInterface); return QObject::connect(m_sensorInterface, SIGNAL(orientationChanged(Unsigned)), this, SLOT(slotDataAvailable(Unsigned))); } diff --git a/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp b/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp index 4d91a024..b8d782d9 100644 --- a/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwproximitysensor.cpp @@ -46,8 +46,8 @@ char const * const SensorfwProximitySensor::id("sensorfw.proximitysensor"); SensorfwProximitySensor::SensorfwProximitySensor(QSensor *sensor) : SensorfwSensorBase(sensor), + m_initDone(false), m_exClose(false) - , m_initDone(false) { init(); setReading(&m_reading); @@ -81,6 +81,7 @@ void SensorfwProximitySensor::slotDataAvailable(const Unsigned& data) bool SensorfwProximitySensor::doConnect() { + Q_ASSERT(m_sensorInterface); return (QObject::connect(m_sensorInterface, SIGNAL(dataAvailable(Unsigned)), this, SLOT(slotDataAvailable(Unsigned)))); } diff --git a/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp b/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp index b63e7f6e..87d547c6 100644 --- a/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwrotationsensor.cpp @@ -71,6 +71,7 @@ void SensorfwRotationSensor::slotFrameAvailable(const QVector& frame) bool SensorfwRotationSensor::doConnect() { + Q_ASSERT(m_sensorInterface); if (m_bufferSize==1) return QObject::connect(m_sensorInterface, SIGNAL(dataAvailable(XYZ)), this, SLOT(slotDataAvailable(XYZ))); return QObject::connect(m_sensorInterface, SIGNAL(frameAvailable(QVector)),this, SLOT(slotFrameAvailable(QVector))); diff --git a/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp b/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp index 299a75c9..b8e40771 100644 --- a/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwsensorbase.cpp @@ -215,6 +215,11 @@ qreal SensorfwSensorBase::correctionFactor() const void SensorfwSensorBase::connectToSensord() { m_remoteSensorManager = &SensorManagerInterface::instance(); + if (!m_remoteSensorManager->isValid()) { + qWarning() << "SensorManagerInterface is invalid"; + m_remoteSensorManager = 0; + return; + } if (running) { stop(); reinitIsNeeded = true; @@ -228,3 +233,68 @@ void SensorfwSensorBase::sensordUnregistered() m_bufferSize = -1; } +bool SensorfwSensorBase::initSensorInterface(QString const &name) +{ + if (!m_sensorInterface) { + sensorError(KErrNotFound); + return false; + } + + //metadata + const QList intervals = m_sensorInterface->getAvailableIntervals(); + + for (int i = 0, l = intervals.size(); i < l; i++) { + qreal intervalMax = intervals.at(i).max; + qreal intervalMin = intervals.at(i).min; + + if (intervalMin == 0 && intervalMax == 0) { + // 0 interval has different meanings in e.g. magge/acce + // magge -> best-effort + // acce -> lowest possible + // in Qt API setting 0 means default + continue; + } + + qreal rateMin = intervalMax < 1 ? 1 : 1 / intervalMax * 1000; + rateMin = rateMin < 1 ? 1 : rateMin; + + intervalMin = intervalMin < 1 ? 10: intervalMin; // do not divide with 0 + qreal rateMax = 1 / intervalMin * 1000; + addDataRate(rateMin, rateMax); + } + + //bufferSizes + if (m_bufferingSensors.contains(sensor()->identifier())) { + + IntegerRangeList sizes = m_sensorInterface->getAvailableBufferSizes(); + for (int i = 0; i < sizes.size(); i++) { + int second = sizes.at(i).second; + m_maxBufferSize = second > m_bufferSize ? second : m_maxBufferSize; + } + m_maxBufferSize = m_maxBufferSize < 0 ? 1 : m_maxBufferSize; + //SensorFW guarantees to provide the most efficient size first + //TODO: remove from comments + //m_efficientBufferSize = m_sensorInterface->hwBuffering()? (l>0?sizes.at(0).first:1) : 1; + } else { + m_maxBufferSize = 1; + } + + sensor()->setMaxBufferSize(m_maxBufferSize); + sensor()->setEfficientBufferSize(m_efficientBufferSize); + + // TODO deztructor: Leaking abstraction detected. Just copied code + // from initSensor<>() here, need to + QByteArray type = sensor()->type(); + if ((type == QAmbientLightSensor::type) // SensorFW returns lux values, plugin enumerated values + || (type == QIRProximitySensor::type) // SensorFW returns raw reflectance values, plugin % of max reflectance + || (name == "accelerometersensor") // SensorFW returns milliGs, plugin m/s^2 + || (name == "magnetometersensor") // SensorFW returns nanoTeslas, plugin Teslas + || (name == "gyroscopesensor")) // SensorFW returns DSPs, plugin milliDSPs + return true; + + setDescription(m_sensorInterface->description()); + + if (name == "tapsensor") return true; + setRanges(); + return true; +} diff --git a/src/plugins/sensors/sensorfw/sensorfwsensorbase.h b/src/plugins/sensors/sensorfw/sensorfwsensorbase.h index 7cf56844..26bc4aae 100644 --- a/src/plugins/sensors/sensorfw/sensorfwsensorbase.h +++ b/src/plugins/sensors/sensorfw/sensorfwsensorbase.h @@ -77,85 +77,20 @@ protected: template void initSensor(bool &initDone) { - const QString name = sensorName(); if (!initDone) { + if (!m_remoteSensorManager) { + qDebug() << "There is no sensor manager yet, do not initialize" << name; + return; + } if (!m_remoteSensorManager->loadPlugin(name)) { sensorError(KErrNotFound); return; } m_remoteSensorManager->registerSensorInterface(name); } - m_sensorInterface = T::controlInterface(name); - if (!m_sensorInterface) { - m_sensorInterface = const_cast(T::listenInterface(name)); - } - if (!m_sensorInterface) { - sensorError(KErrNotFound); - return; - } - if (!m_sensorInterface) { - sensorError(KErrNotFound); - return; - } - - initDone = true; - - //metadata - QList intervals = m_sensorInterface->getAvailableIntervals(); - - for (int i = 0, l = intervals.size(); i < l; i++) { - qreal intervalMax = ((DataRange)(intervals.at(i))).max; - qreal intervalMin =((DataRange)(intervals.at(i))).min; - - if (intervalMin == 0 && intervalMax == 0) { - // 0 interval has different meanings in e.g. magge/acce - // magge -> best-effort - // acce -> lowest possible - // in Qt API setting 0 means default - continue; - } - - qreal rateMin = intervalMax < 1 ? 1 : 1 / intervalMax * 1000; - rateMin = rateMin < 1 ? 1 : rateMin; - - intervalMin = intervalMin < 1 ? 10: intervalMin; // do not divide with 0 - qreal rateMax = 1 / intervalMin * 1000; - addDataRate(rateMin, rateMax); - } - - //bufferSizes - if (m_bufferingSensors.contains(sensor()->identifier())) { - - IntegerRangeList sizes = m_sensorInterface->getAvailableBufferSizes(); - int l = sizes.size(); - for (int i = 0; i < l; i++) { - int second = sizes.at(i).second; - m_maxBufferSize = second > m_bufferSize ? second : m_maxBufferSize; - } - m_maxBufferSize = m_maxBufferSize < 0 ? 1 : m_maxBufferSize; - //SensorFW guarantees to provide the most efficient size first - //TODO: remove from comments - //m_efficientBufferSize = m_sensorInterface->hwBuffering()? (l>0?sizes.at(0).first:1) : 1; - } - else - m_maxBufferSize = 1; - - sensor()->setMaxBufferSize(m_maxBufferSize); - sensor()->setEfficientBufferSize(m_efficientBufferSize); - - QByteArray type = sensor()->type(); - if (type == QAmbientLightSensor::type) return; // SensorFW returns lux values, plugin enumerated values - if (type == QIRProximitySensor::type) return; // SensorFW returns raw reflectance values, plugin % of max reflectance - if (name == "accelerometersensor") return; // SensorFW returns milliGs, plugin m/s^2 - if (name == "magnetometersensor") return; // SensorFW returns nanoTeslas, plugin Teslas - if (name == "gyroscopesensor") return; // SensorFW returns DSPs, plugin milliDSPs - - setDescription(m_sensorInterface->description()); - - if (name == "tapsensor") return; - setRanges(); + initDone = initSensorInterface(name); }; @@ -166,7 +101,7 @@ protected: bool reinitIsNeeded; private: - + bool initSensorInterface(QString const &); static SensorManagerInterface* m_remoteSensorManager; int m_prevOutputRange; bool doConnectAfterCheck(); diff --git a/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp b/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp index e6f61d9b..01ee7789 100644 --- a/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp +++ b/src/plugins/sensors/sensorfw/sensorfwtapsensor.cpp @@ -45,8 +45,8 @@ char const * const SensorfwTapSensor::id("sensorfw.tapsensor"); SensorfwTapSensor::SensorfwTapSensor(QSensor *sensor) : SensorfwSensorBase(sensor), + m_initDone(false), m_isOnceStarted(false) - , m_initDone(false) { init(); setReading(&m_reading); @@ -71,9 +71,14 @@ void SensorfwTapSensor::start() } else m_isDoubleTapSensor = b; - if (!m_isOnceStarted || (m_isOnceStarted && isDoubleTapSensor != m_isDoubleTapSensor)) - ((TapSensorChannelInterface*)m_sensorInterface)-> - setTapType(m_isDoubleTapSensor?TapSensorChannelInterface::Double:TapSensorChannelInterface::Single); + if (!m_isOnceStarted || (m_isOnceStarted && isDoubleTapSensor != m_isDoubleTapSensor)) { + TapSensorChannelInterface *iface = static_cast(m_sensorInterface); + if (!iface) { + qWarning() << "Sensor interface is not initialized"; + return; + } + iface->setTapType(m_isDoubleTapSensor?TapSensorChannelInterface::Double:TapSensorChannelInterface::Single); + } SensorfwSensorBase::start(); // Set tap type (single/double) @@ -106,6 +111,7 @@ void SensorfwTapSensor::slotDataAvailable(const Tap& data) bool SensorfwTapSensor::doConnect() { + Q_ASSERT(m_sensorInterface); return QObject::connect(m_sensorInterface, SIGNAL(dataAvailable(Tap)), this, SLOT(slotDataAvailable(Tap))); } diff --git a/src/sensors/qsensormanager.cpp b/src/sensors/qsensormanager.cpp index 29bd4b5f..abe5f853 100644 --- a/src/sensors/qsensormanager.cpp +++ b/src/sensors/qsensormanager.cpp @@ -48,12 +48,15 @@ #include "sensorlog_p.h" #include #include +#include QT_BEGIN_NAMESPACE typedef QHash FactoryForIdentifierMap; typedef QHash BackendIdentifiersForTypeMap; +static QLoggingCategory sensorsCategory("qt.sensors"); + class QSensorManagerPrivate : public QObject { friend class QSensorManager; @@ -77,7 +80,6 @@ public: loadExternalPlugins = false; } } - bool loadExternalPlugins; PluginLoadingState pluginLoadingState; QFactoryLoader *loader; @@ -101,9 +103,16 @@ public: if (config.isEmpty()) return; // QStandardPaths is broken? config += QLatin1String("/QtProject/Sensors.conf"); #endif - if (!QFile::exists(config)) return; + qCDebug(sensorsCategory) << "Loading config from" << config; + if (!QFile::exists(config)) { + qCWarning(sensorsCategory) << "There is no config file" << config; + return; + } QFile cfgfile(config); - if (!cfgfile.open(QFile::ReadOnly)) return; + if (!cfgfile.open(QFile::ReadOnly)) { + qCWarning(sensorsCategory) << "Can't open config file" << config; + return; + } QTextStream stream(&cfgfile); QString line; @@ -169,13 +178,19 @@ Q_GLOBAL_STATIC(QSensorManagerPrivate, sensorManagerPrivate) static void initPlugin(QObject *o) { - if (!o) return; + qCDebug(sensorsCategory) << "Init plugin" << o; + if (!o) { + qCWarning(sensorsCategory) << "Null plugin" << o; + return; + } QSensorManagerPrivate *d = sensorManagerPrivate(); if (!d) return; // hardly likely but just in case... - if (d->seenPlugins.contains(o)) + if (d->seenPlugins.contains(o)) { + qCDebug(sensorsCategory) << "Plugin is seen" << o; return; + } QSensorChangesInterface *changes = qobject_cast(o); if (changes) @@ -184,8 +199,11 @@ static void initPlugin(QObject *o) QSensorPluginInterface *plugin = qobject_cast(o); if (plugin) { + qCDebug(sensorsCategory) << "Register sensors for " << plugin; d->seenPlugins.insert(o); plugin->registerSensors(); + } else { + qCWarning(sensorsCategory) << "Can't cast to plugin" << o; } } -- cgit v1.2.3 From c5a18346589b88ed9fc7970ad0857aef9d333b69 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Thu, 17 Jul 2014 14:07:15 +0000 Subject: Restore setting of sensors interface. Change I5c1bf3999ad2268c0dba9b3fe511d999c2e63fd9's forward porting from Qt 5.1 removed this, thus the interface was never set, thus sensors all broke. Change-Id: If3b14b5ebd20e6cb64bc2000b23a2c1e37d36b05 Reviewed-by: Alex Blasche --- src/plugins/sensors/sensorfw/sensorfwsensorbase.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/plugins/sensors/sensorfw/sensorfwsensorbase.h b/src/plugins/sensors/sensorfw/sensorfwsensorbase.h index 26bc4aae..39e4147e 100644 --- a/src/plugins/sensors/sensorfw/sensorfwsensorbase.h +++ b/src/plugins/sensors/sensorfw/sensorfwsensorbase.h @@ -90,6 +90,10 @@ protected: } m_remoteSensorManager->registerSensorInterface(name); } + m_sensorInterface = T::controlInterface(name); + if (!m_sensorInterface) { + m_sensorInterface = const_cast(T::listenInterface(name)); + } initDone = initSensorInterface(name); }; -- cgit v1.2.3 From 4c29e9149a583b48e563ad44b59666a9d45f9347 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Fri, 18 Jul 2014 11:28:41 +0200 Subject: Add missing word to QCompass documentation. Change-Id: I46b9a3c20b5169843292057439c80c859ccc7e82 Reviewed-by: Lorn Potter --- src/sensors/qcompass.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sensors/qcompass.cpp b/src/sensors/qcompass.cpp index 8bfca073..f2157d66 100644 --- a/src/sensors/qcompass.cpp +++ b/src/sensors/qcompass.cpp @@ -74,7 +74,7 @@ IMPLEMENT_READING(QCompassReading) \property QCompassReading::azimuth \brief the azimuth of the device. - Measured in degrees from magnetic north in a clockwise direction based + Measured in degrees from magnetic north in a clockwise direction based on the top of the UI. \sa {QCompassReading Units} */ -- cgit v1.2.3 From 825ae95d8984a3daa49ad81a91e85483918799af Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Mon, 11 Aug 2014 12:41:02 +0200 Subject: Clarify the QCompass coordinate system The compass behaves like the other sensors: it is measured relative to the top of the device. Change-Id: Iddc4ad6a16f8b09d26a9d300155324bb8d378b76 Reviewed-by: Alex Blasche Reviewed-by: Lorn Potter --- src/sensors/qcompass.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/sensors/qcompass.cpp b/src/sensors/qcompass.cpp index 8bfca073..6564d362 100644 --- a/src/sensors/qcompass.cpp +++ b/src/sensors/qcompass.cpp @@ -56,7 +56,8 @@ IMPLEMENT_READING(QCompassReading) \section2 QCompassReading Units The compass returns the azimuth of the device as degrees from - magnetic north in a clockwise direction based on the top of the UI. + magnetic north in a clockwise direction based on the top of the device, + as defined by QPlatformScreen::nativeOrientation. There is also a value to indicate the calibration status of the device. If the device is not calibrated the azimuth may not be accurate. @@ -75,7 +76,7 @@ IMPLEMENT_READING(QCompassReading) \brief the azimuth of the device. Measured in degrees from magnetic north in a clockwise direction based - the top of the UI. + the top of the device, as defined by QPlatformScreen::nativeOrientation. \sa {QCompassReading Units} */ -- cgit v1.2.3 From ea7af7f00886f77941b81262e597c3b7f3477f48 Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Fri, 8 Aug 2014 18:31:20 +0200 Subject: Compass sensor implementation for Android Change-Id: Ic13d2d1e400a92b2f0c3c83e279c58b799f9d06b Reviewed-by: Lorn Potter --- .../qtproject/qt5/android/sensors/QtSensors.java | 19 +++ src/plugins/sensors/android/src/androidcompass.cpp | 173 +++++++++++++++++++++ src/plugins/sensors/android/src/androidcompass.h | 76 +++++++++ .../sensors/android/src/androidjnisensors.cpp | 16 ++ .../sensors/android/src/androidjnisensors.h | 1 + src/plugins/sensors/android/src/main.cpp | 11 ++ src/plugins/sensors/android/src/src.pro | 2 + 7 files changed, 298 insertions(+) create mode 100644 src/plugins/sensors/android/src/androidcompass.cpp create mode 100644 src/plugins/sensors/android/src/androidcompass.h diff --git a/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java b/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java index efd1ff6d..e7e309ed 100644 --- a/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java +++ b/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java @@ -153,6 +153,25 @@ public class QtSensors implements SensorEventListener return angles; } + private static float[] mRotation = new float[9]; + private static float[] mOrientation = new float[3]; + private static float[] mAcc = new float[3]; + private static float[] mMag = new float[3]; + + private static float getCompassAzimuth(float a0, float a1, float a2, float m0, float m1, float m2) + { + mAcc[0] = a0; + mAcc[1] = a1; + mAcc[2] = a2; + mMag[0] = m0; + mMag[1] = m1; + mMag[2] = m2; + + SensorManager.getRotationMatrix(mRotation, null, mAcc, mMag); + SensorManager.getOrientation(mRotation, mOrientation); + return mOrientation[0]; + } + public static native void accuracyChanged(int sensorType, int accuracy); public static native void sensorChanged(int sensorType, long timestamp, float[] values); diff --git a/src/plugins/sensors/android/src/androidcompass.cpp b/src/plugins/sensors/android/src/androidcompass.cpp new file mode 100644 index 00000000..f2b4bd25 --- /dev/null +++ b/src/plugins/sensors/android/src/androidcompass.cpp @@ -0,0 +1,173 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "androidcompass.h" + +#include +#include +#include "androidjnisensors.h" + + +class AndroidAccelerometerListener : public AndroidSensors::AndroidSensorsListenerInterface +{ +public: + + AndroidAccelerometerListener(AndroidCompass *parent) + : m_compass(parent) + { + } + + void start(int dataRate) + { + AndroidSensors::registerListener(AndroidSensors::TYPE_ACCELEROMETER, this, dataRate); + } + + void stop() + { + AndroidSensors::unregisterListener(AndroidSensors::TYPE_ACCELEROMETER, this); + } + + void onAccuracyChanged(jint accuracy) Q_DECL_OVERRIDE + { + Q_UNUSED(accuracy); + } + + void onSensorChanged(jlong /*timestamp*/, const jfloat *values, uint size) Q_DECL_OVERRIDE + { + if (size < 3) + return; + reading[0] = values[0]; + reading[1] = values[1]; + reading[2] = values[2]; + m_compass->testStuff(); + } + + jfloat reading[3]; + +private: + AndroidCompass *m_compass; +}; + +class AndroidMagnetometerListener : public AndroidSensors::AndroidSensorsListenerInterface +{ +public: + AndroidMagnetometerListener(AndroidCompass *parent) + :m_compass(parent) + { + + } + + void start(int dataRate) + { + AndroidSensors::registerListener(AndroidSensors::TYPE_MAGNETIC_FIELD, this, dataRate); + } + + void stop() + { + AndroidSensors::unregisterListener(AndroidSensors::TYPE_MAGNETIC_FIELD, this); + } + + void onAccuracyChanged(jint accuracy) Q_DECL_OVERRIDE + { + Q_UNUSED(accuracy); + } + + void onSensorChanged(jlong /*timestamp*/, const jfloat *values, uint size) Q_DECL_OVERRIDE + { + if (size < 3) + return; + reading[0] = values[0]; + reading[1] = values[1]; + reading[2] = values[2]; + m_compass->testStuff(); + } + + jfloat reading[3]; +private: + AndroidCompass *m_compass; +}; + +char const * const AndroidCompass::id("android.synthetic.compass"); + +AndroidCompass::AndroidCompass(QSensor *sensor) + : QSensorBackend(sensor), m_accelerometerListener(0), m_magnetometerListener(0), m_isStarted(false) +{ + setReading(&m_reading); + m_isStarted = false; +} + +AndroidCompass::~AndroidCompass() +{ + if (m_isStarted) + stop(); + delete m_accelerometerListener; + delete m_magnetometerListener; +} + +void AndroidCompass::start() +{ + if (!m_accelerometerListener) + m_accelerometerListener = new AndroidAccelerometerListener(this); + m_accelerometerListener->start(sensor()->dataRate()); + if (!m_magnetometerListener) + m_magnetometerListener = new AndroidMagnetometerListener(this); + m_magnetometerListener->start(sensor()->dataRate()); + + m_isStarted = true; +} + +void AndroidCompass::stop() +{ + if (m_isStarted) { + m_isStarted = false; + m_accelerometerListener->stop(); + m_magnetometerListener->stop(); + } +} + +void AndroidCompass::testStuff() +{ + qreal azimuth = AndroidSensors::getCompassAzimuth(m_accelerometerListener->reading, m_magnetometerListener->reading); + + azimuth = azimuth * 180.0 / M_PI; + m_reading.setAzimuth(azimuth); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/android/src/androidcompass.h b/src/plugins/sensors/android/src/androidcompass.h new file mode 100644 index 00000000..897dd463 --- /dev/null +++ b/src/plugins/sensors/android/src/androidcompass.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ANDROIDCOMPASS_H +#define ANDROIDCOMPASS_H +#include + +#include "androidcommonsensor.h" + +class AndroidAccelerometerListener; +class AndroidMagnetometerListener; + +class AndroidCompass : public QSensorBackend +{ + Q_OBJECT + +public: + static char const * const id; + + AndroidCompass(QSensor *sensor); + ~AndroidCompass(); + + void start() Q_DECL_OVERRIDE; + void stop() Q_DECL_OVERRIDE; + +private: + AndroidAccelerometerListener *m_accelerometerListener; + AndroidMagnetometerListener *m_magnetometerListener; + + QCompassReading m_reading; + bool m_isStarted; + +public Q_SLOTS: + void testStuff(); + +}; + +#endif // ANDROIDCOMPASS_H diff --git a/src/plugins/sensors/android/src/androidjnisensors.cpp b/src/plugins/sensors/android/src/androidjnisensors.cpp index 87889a89..9a7b671a 100644 --- a/src/plugins/sensors/android/src/androidjnisensors.cpp +++ b/src/plugins/sensors/android/src/androidjnisensors.cpp @@ -54,6 +54,7 @@ static jmethodID registerSensorMethodId; static jmethodID unregisterSensorMethodId; static jmethodID getSensorDescriptionMethodId; static jmethodID getSensorMaximumRangeMethodId; +static jmethodID getCompassAzimuthId; static QHash > listenersHash; QReadWriteLock listenersLocker; @@ -166,6 +167,20 @@ namespace AndroidSensors } return true; } + + qreal getCompassAzimuth(jfloat *accelerometerReading, jfloat *magnetometerReading) + { + AttachedJNIEnv aenv; + if (!aenv.jniEnv) + return 0.0; + return aenv.jniEnv->CallStaticFloatMethod(sensorsClass, getCompassAzimuthId, + accelerometerReading[0], + accelerometerReading[1], + accelerometerReading[2], + magnetometerReading[0], + magnetometerReading[1], + magnetometerReading[2]); + } } static const char logTag[] = "Qt"; @@ -227,6 +242,7 @@ static bool registerNatives(JNIEnv *env) GET_AND_CHECK_STATIC_METHOD(unregisterSensorMethodId, sensorsClass, "unregisterSensor", "(I)Z"); GET_AND_CHECK_STATIC_METHOD(getSensorDescriptionMethodId, sensorsClass, "getSensorDescription", "(I)Ljava/lang/String;"); GET_AND_CHECK_STATIC_METHOD(getSensorMaximumRangeMethodId, sensorsClass, "getSensorMaximumRange", "(I)F"); + GET_AND_CHECK_STATIC_METHOD(getCompassAzimuthId, sensorsClass, "getCompassAzimuth", "(FFFFFF)F"); return true; } diff --git a/src/plugins/sensors/android/src/androidjnisensors.h b/src/plugins/sensors/android/src/androidjnisensors.h index 30aab6cc..9d2ccf55 100644 --- a/src/plugins/sensors/android/src/androidjnisensors.h +++ b/src/plugins/sensors/android/src/androidjnisensors.h @@ -83,6 +83,7 @@ namespace AndroidSensors qreal sensorMaximumRange(AndroidSensorType sensor); bool registerListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener, int dataRate = 0); bool unregisterListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener); + qreal getCompassAzimuth(jfloat *accelerometerReading, jfloat *magnetometerReading); } #endif // ANDROIDJNISENSORS_H diff --git a/src/plugins/sensors/android/src/main.cpp b/src/plugins/sensors/android/src/main.cpp index 3d8604fe..b8d14960 100644 --- a/src/plugins/sensors/android/src/main.cpp +++ b/src/plugins/sensors/android/src/main.cpp @@ -44,7 +44,9 @@ #include #include #include +#include #include "androidaccelerometer.h" +#include "androidcompass.h" #include "androidgyroscope.h" #include "androidlight.h" #include "androidmagnetometer.h" @@ -63,10 +65,13 @@ class AndroidSensorPlugin : public QObject, public QSensorPluginInterface, publi public: void registerSensors() { + bool accelerometer = false; + bool magnetometer = false; foreach (AndroidSensorType sensor, availableSensors()) { switch (sensor) { case TYPE_ACCELEROMETER: QSensorManager::registerBackend(QAccelerometer::type, QByteArray::number(sensor), this); + accelerometer = true; break; case TYPE_AMBIENT_TEMPERATURE: case TYPE_TEMPERATURE: @@ -84,6 +89,7 @@ public: break; // add the linear acceleration sensor backend case TYPE_MAGNETIC_FIELD: QSensorManager::registerBackend(QMagnetometer::type, QByteArray::number(sensor), this); + magnetometer = true; break; case TYPE_ORIENTATION: break; // add the orientation sensor backend @@ -106,10 +112,15 @@ public: break; // add backends for API level 18 sensors } } + if (accelerometer && magnetometer) + QSensorManager::registerBackend(QCompass::type, AndroidCompass::id, this); } QSensorBackend *createBackend(QSensor *sensor) { + if (sensor->identifier() == AndroidCompass::id) + return new AndroidCompass(sensor); + AndroidSensorType type = static_cast(sensor->identifier().toInt()); switch (type) { case TYPE_ACCELEROMETER: { diff --git a/src/plugins/sensors/android/src/src.pro b/src/plugins/sensors/android/src/src.pro index 21423efb..23a6bead 100644 --- a/src/plugins/sensors/android/src/src.pro +++ b/src/plugins/sensors/android/src/src.pro @@ -12,6 +12,7 @@ DEFINES += QT_STATICPLUGIN HEADERS = \ androidjnisensors.h \ androidaccelerometer.h \ + androidcompass.h \ androidcommonsensor.h \ androidgyroscope.h \ androidmagnetometer.h \ @@ -25,6 +26,7 @@ SOURCES = \ main.cpp \ androidjnisensors.cpp \ androidaccelerometer.cpp \ + androidcompass.cpp \ androidgyroscope.cpp \ androidmagnetometer.cpp \ androidpressure.cpp \ -- cgit v1.2.3