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:LGPL$ 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 Lesser General Public License Usage 18 ** Alternatively, this file may be used under the terms of the GNU Lesser 19 ** General Public License version 3 as published by the Free Software 20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 21 ** packaging of this file. Please review the following information to 22 ** ensure the GNU Lesser General Public License version 3 requirements 23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 24 ** 25 ** GNU General Public License Usage 26 ** Alternatively, this file may be used under the terms of the GNU 27 ** General Public License version 2.0 or (at your option) the GNU General 28 ** Public license version 3 or any later version approved by the KDE Free 29 ** Qt Foundation. The licenses are as published by the Free Software 30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 31 ** included in the packaging of this file. Please review the following 32 ** information to ensure the GNU General Public License requirements will 33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and 34 ** https://www.gnu.org/licenses/gpl-3.0.html. 35 ** 36 ** $QT_END_LICENSE$ 37 ** 38 ****************************************************************************/ 39 40 #ifndef QSENSOR_H 41 #define QSENSOR_H 42 43 #include <QtSensors/qsensorsglobal.h> 44 45 #include <QtCore/QObject> 46 #include <QtCore/QByteArray> 47 #include <QtCore/QMetaType> 48 #include <QtCore/QVariant> 49 #include <QtCore/QPair> 50 51 QT_BEGIN_NAMESPACE 52 53 class QSensorPrivate; 54 class QSensorBackend; 55 class QSensorReading; 56 class QSensorReadingPrivate; 57 class QSensorFilter; 58 59 // This type is no longer used in the API but third party apps may be using it 60 typedef quint64 qtimestamp; 61 62 typedef QPair<int,int> qrange; 63 typedef QList<qrange> qrangelist; 64 struct qoutputrange 65 { 66 qreal minimum; 67 qreal maximum; 68 qreal accuracy; 69 }; 70 typedef QList<qoutputrange> qoutputrangelist; 71 72 class Q_SENSORS_EXPORT QSensor : public QObject 73 { 74 friend class QSensorBackend; 75 76 Q_OBJECT 77 Q_ENUMS(Feature) 78 Q_ENUMS(AxesOrientationMode) 79 Q_PROPERTY(QByteArray identifier READ identifier WRITE setIdentifier) 80 Q_PROPERTY(QByteArray type READ type) 81 Q_PROPERTY(bool connectedToBackend READ isConnectedToBackend) 82 Q_PROPERTY(qrangelist availableDataRates READ availableDataRates) 83 Q_PROPERTY(int dataRate READ dataRate WRITE setDataRate NOTIFY dataRateChanged) 84 Q_PROPERTY(QSensorReading* reading READ reading NOTIFY readingChanged) 85 Q_PROPERTY(bool busy READ isBusy) 86 Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged) 87 Q_PROPERTY(qoutputrangelist outputRanges READ outputRanges) 88 Q_PROPERTY(int outputRange READ outputRange WRITE setOutputRange) 89 Q_PROPERTY(QString description READ description) 90 Q_PROPERTY(int error READ error NOTIFY sensorError) 91 Q_PROPERTY(bool alwaysOn READ isAlwaysOn WRITE setAlwaysOn NOTIFY alwaysOnChanged) 92 Q_PROPERTY(bool skipDuplicates READ skipDuplicates WRITE setSkipDuplicates NOTIFY skipDuplicatesChanged) 93 Q_PROPERTY(AxesOrientationMode axesOrientationMode READ axesOrientationMode WRITE setAxesOrientationMode NOTIFY axesOrientationModeChanged) 94 Q_PROPERTY(int currentOrientation READ currentOrientation NOTIFY currentOrientationChanged) 95 Q_PROPERTY(int userOrientation READ userOrientation WRITE setUserOrientation NOTIFY userOrientationChanged) 96 Q_PROPERTY(int maxBufferSize READ maxBufferSize NOTIFY maxBufferSizeChanged) 97 Q_PROPERTY(int efficientBufferSize READ efficientBufferSize NOTIFY efficientBufferSizeChanged) 98 Q_PROPERTY(int bufferSize READ bufferSize WRITE setBufferSize NOTIFY bufferSizeChanged) 99 public: 100 enum Feature { 101 Buffering, 102 AlwaysOn, 103 GeoValues, 104 FieldOfView, 105 AccelerationMode, 106 SkipDuplicates, 107 AxesOrientation, 108 PressureSensorTemperature, 109 Reserved = 257 // Make sure at least 2 bytes are used for the enum to avoid breaking BC later 110 }; 111 112 // Keep in sync with QmlSensor::AxesOrientationMode 113 enum AxesOrientationMode { 114 FixedOrientation, 115 AutomaticOrientation, 116 UserOrientation 117 }; 118 119 explicit QSensor(const QByteArray &type, QObject *parent = Q_NULLPTR); 120 virtual ~QSensor(); 121 122 QByteArray identifier() const; 123 void setIdentifier(const QByteArray &identifier); 124 125 QByteArray type() const; 126 127 Q_INVOKABLE bool connectToBackend(); 128 bool isConnectedToBackend() const; 129 130 bool isBusy() const; 131 132 void setActive(bool active); 133 bool isActive() const; 134 135 bool isAlwaysOn() const; 136 void setAlwaysOn(bool alwaysOn); 137 138 bool skipDuplicates() const; 139 void setSkipDuplicates(bool skipDuplicates); 140 141 qrangelist availableDataRates() const; 142 int dataRate() const; 143 void setDataRate(int rate); 144 145 qoutputrangelist outputRanges() const; 146 int outputRange() const; 147 void setOutputRange(int index); 148 149 QString description() const; 150 int error() const; 151 152 // Filters modify the reading 153 void addFilter(QSensorFilter *filter); 154 void removeFilter(QSensorFilter *filter); 155 QList<QSensorFilter*> filters() const; 156 157 // The readings are exposed via this object 158 QSensorReading *reading() const; 159 160 // Information about available sensors 161 // These functions are implemented in qsensormanager.cpp 162 static QList<QByteArray> sensorTypes(); 163 static QList<QByteArray> sensorsForType(const QByteArray &type); 164 static QByteArray defaultSensorForType(const QByteArray &type); 165 166 Q_INVOKABLE bool isFeatureSupported(Feature feature) const; 167 168 AxesOrientationMode axesOrientationMode() const; 169 void setAxesOrientationMode(AxesOrientationMode axesOrientationMode); 170 171 int currentOrientation() const; 172 void setCurrentOrientation(int currentOrientation); 173 174 int userOrientation() const; 175 void setUserOrientation(int userOrientation); 176 177 int maxBufferSize() const; 178 void setMaxBufferSize(int maxBufferSize); 179 180 int efficientBufferSize() const; 181 void setEfficientBufferSize(int efficientBufferSize); 182 183 int bufferSize() const; 184 void setBufferSize(int bufferSize); 185 186 public Q_SLOTS: 187 // Start receiving values from the sensor 188 bool start(); 189 190 // Stop receiving values from the sensor 191 void stop(); 192 193 Q_SIGNALS: 194 void busyChanged(); 195 void activeChanged(); 196 void readingChanged(); 197 void sensorError(int error); 198 void availableSensorsChanged(); 199 void alwaysOnChanged(); 200 void dataRateChanged(); 201 void skipDuplicatesChanged(bool skipDuplicates); 202 void axesOrientationModeChanged(AxesOrientationMode axesOrientationMode); 203 void currentOrientationChanged(int currentOrientation); 204 void userOrientationChanged(int userOrientation); 205 void maxBufferSizeChanged(int maxBufferSize); 206 void efficientBufferSizeChanged(int efficientBufferSize); 207 void bufferSizeChanged(int bufferSize); 208 209 protected: 210 explicit QSensor(const QByteArray &type, QSensorPrivate &dd, QObject* parent = Q_NULLPTR); 211 QSensorBackend *backend() const; 212 213 private: 214 void registerInstance(); 215 216 Q_DISABLE_COPY(QSensor) 217 Q_DECLARE_PRIVATE(QSensor) 218 }; 219 220 class Q_SENSORS_EXPORT QSensorFilter 221 { 222 friend class QSensor; 223 public: 224 virtual bool filter(QSensorReading *reading) = 0; 225 protected: 226 QSensorFilter(); 227 virtual ~QSensorFilter(); 228 virtual void setSensor(QSensor *sensor); 229 QSensor *m_sensor; 230 }; 231 232 class Q_SENSORS_EXPORT QSensorReading : public QObject 233 { 234 friend class QSensorBackend; 235 236 Q_OBJECT 237 Q_PROPERTY(quint64 timestamp READ timestamp) 238 public: 239 virtual ~QSensorReading(); 240 241 quint64 timestamp() const; 242 void setTimestamp(quint64 timestamp); 243 244 // Access properties of sub-classes by numeric index 245 // For name-based access use QObject::property() 246 int valueCount() const; 247 QVariant value(int index) const; 248 249 protected: 250 explicit QSensorReading(QObject *parent, QSensorReadingPrivate *d); d_ptr()251 QScopedPointer<QSensorReadingPrivate> *d_ptr() { return &d; } 252 virtual void copyValuesFrom(QSensorReading *other); 253 254 private: 255 QScopedPointer<QSensorReadingPrivate> d; 256 Q_DISABLE_COPY(QSensorReading) 257 }; 258 259 #define DECLARE_READING(classname)\ 260 DECLARE_READING_D(classname, classname ## Private) 261 262 #define DECLARE_READING_D(classname, pclassname)\ 263 public:\ 264 classname(QObject *parent = Q_NULLPTR);\ 265 virtual ~classname();\ 266 void copyValuesFrom(QSensorReading *other) override;\ 267 private:\ 268 QScopedPointer<pclassname> d; 269 270 #define IMPLEMENT_READING(classname)\ 271 IMPLEMENT_READING_D(classname, classname ## Private) 272 273 #define IMPLEMENT_READING_D(classname, pclassname)\ 274 classname::classname(QObject *parent)\ 275 : QSensorReading(parent, Q_NULLPTR)\ 276 , d(new pclassname)\ 277 {}\ 278 classname::~classname() {}\ 279 void classname::copyValuesFrom(QSensorReading *_other)\ 280 {\ 281 /* No need to verify types, only called by QSensorBackend */\ 282 classname *other = static_cast<classname *>(_other);\ 283 pclassname *my_ptr = d.data();\ 284 pclassname *other_ptr = other->d.data();\ 285 /* Do a direct copy of the private class */\ 286 *(my_ptr) = *(other_ptr);\ 287 /* We need to copy the parent too */\ 288 QSensorReading::copyValuesFrom(_other);\ 289 } 290 291 292 QT_END_NAMESPACE 293 294 Q_DECLARE_METATYPE(qrange) 295 Q_DECLARE_METATYPE(qrangelist) 296 Q_DECLARE_METATYPE(qoutputrangelist) 297 298 #endif 299 300