summaryrefslogtreecommitdiffstats
path: root/src/plugins/sensors/sensortag/sensortagbase.h
blob: 4dfcc6aa69dc572e8b411d12ea745522aaf30779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/****************************************************************************
**
** Copyright (C) 2017 Lorn Potter
** Copyright (C) 2017 Canonical, Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtSensors module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#ifndef SENSORTAGBASEPRIVATE_H
#define SENSORTAGBASEPRIVATE_H

#include <QObject>
#include <QSensorBackend>

#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QLowEnergyController>
#include <QLowEnergyService>

#include <QAccelerometerReading>
#include <QGyroscopeReading>
#include <QMagnetometerReading>

class SensorTagBase;

class SensorTagBasePrivate : public QObject
{
    Q_OBJECT
    Q_DECLARE_PUBLIC(SensorTagBase)
public:
    explicit SensorTagBasePrivate(QObject *parent = 0);

    QLowEnergyService *lightService = nullptr;
    QLowEnergyService *temperatureService = nullptr;
    QLowEnergyService *barometerService = nullptr;
    QLowEnergyService *humidityService = nullptr;
    QLowEnergyService *movementService = nullptr;
    QLowEnergyService *acceleratorService = nullptr;
    QLowEnergyService *magnetometerService = nullptr;
    QLowEnergyService *gyroscopeService = nullptr;

    void enableService(const QBluetoothUuid &uuid);
    void disableService(const QBluetoothUuid &uuid);

    static SensorTagBasePrivate *instance();

signals:
    void accelDataAvailable(const QAccelerometerReading &);
    void gyroDataAvailable(const QGyroscopeReading &);
    void magDataAvailable(const QMagnetometerReading &);

    void luxDataAvailable(qreal);
    void tempDataAvailable(qreal);
    void humidityDataAvailable(qreal);
    void pressureDataAvailable(qreal);
    void compassDataAvailable(qreal);

public slots:
    void enableLight(bool);
    void enableTemp(bool);
    void enablePressure(bool);
    void enableHumidity(bool);
    void enableMovement(bool);

    void deviceFound(const QBluetoothDeviceInfo&);
    void scanFinished();

    void deviceScanError(QBluetoothDeviceDiscoveryAgent::Error);
    void serviceDiscovered(const QBluetoothUuid &);

    void controllerError(QLowEnergyController::Error);

    void sensortagDeviceConnected();
    void deviceDisconnected();

    void serviceStateChanged(QLowEnergyService::ServiceState s);
    void updateCharacteristic(const QLowEnergyCharacteristic &c,
                              const QByteArray &value);
    void serviceError(QLowEnergyService::ServiceError e);
    void doConnections(QLowEnergyService *service);

private slots:
    void deviceSearch();
    void serviceDiscoveryFinished();
    void deviceSearchTimeout();

private:

    void convertLux(const QByteArray &value);
    void convertTemperature(const QByteArray &value);
    void convertBarometer(const QByteArray &value);
    void convertHumidity(const QByteArray &value);
    void convertAccelerometer(const QByteArray &value);
    void convertMagnetometer(const QByteArray &value);
    void convertGyroscope(const QByteArray &value);

    QBluetoothDeviceDiscoveryAgent *m_deviceDiscoveryAgent = nullptr;
    QLowEnergyDescriptor m_notificationDesc;

    QLowEnergyController *m_control = nullptr;
    QLowEnergyService *infoService = nullptr;

    QAccelerometerReading accelReading;
    QGyroscopeReading gyroReading;
    QMagnetometerReading magReading;

    bool discoveryDone = false;
    QList <QBluetoothUuid> enabledServiceUuids;
    SensorTagBase *q_ptr;

    bool accelerometerEnabled = false;
    bool gyroscopeEnabled = false;
    bool magnetometerEnabled = false;

    const QByteArray enableSensorCharacteristic = QByteArrayLiteral("\x01");
    const QByteArray disableSensorCharacteristic = QByteArrayLiteral("\x00");

    const QByteArray enableNotificationsCharacteristic = QByteArrayLiteral("\x01\x00");
    const QByteArray disableNotificationsCharacteristic = QByteArrayLiteral("\x00\x00");
};

const QBluetoothUuid TI_SENSORTAG_TEMPERATURE_SERVICE(QUuid("{f000aa00-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_IR_TEMPERATURE_DATA(QUuid("{f000aa01-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_IR_TEMPERATURE_CONTROL(QUuid("{f000aa02-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_IR_TEMPERATURE_PERIOD(QUuid("{f000aa03-0451-4000-b000-000000000000}"));

const QBluetoothUuid TI_SENSORTAG_ACCELEROMETER_SERVICE(QUuid("{f000aa10-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_ACCELEROMETER_DATA(QUuid("{f000aa11-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_ACCELEROMETER_CONTROL(QUuid("{f000aa12-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_ACCELEROMETER_PERIOD(QUuid("{f000aa13-0451-4000-b000-000000000000}"));

const QBluetoothUuid TI_SENSORTAG_HUMIDTIY_SERVICE(QUuid("{f000aa20-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_HUMIDTIY_DATA(QUuid("{f000aa21-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_HUMIDTIY_CONTROL(QUuid("{f000aa22-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_HUMIDTIY_PERIOD(QUuid("{f000aa23-0451-4000-b000-000000000000}"));

const QBluetoothUuid TI_SENSORTAG_MAGNETOMETER_SERVICE(QUuid("{f000aa30-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_MAGNETOMETER_DATA(QUuid("{f000aa31-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_MAGNETOMETER_CONTROL(QUuid("{f000aa32-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_MAGNETOMETER_PERIOD(QUuid("{f000aa33-0451-4000-b000-000000000000}"));

const QBluetoothUuid TI_SENSORTAG_BAROMETER_SERVICE(QUuid("{f000aa40-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_BAROMETER_DATA(QUuid("{f000aa41-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_BAROMETER_CONTROL(QUuid("{f000aa42-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_BAROMETER_CALIBRATION(QUuid("{f000aa43-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_BAROMETER_PERIOD(QUuid("{f000aa44-0451-4000-b000-000000000000}"));

const QBluetoothUuid TI_SENSORTAG_GYROSCOPE_SERVICE(QUuid("{f000aa50-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_GYROSCOPE_DATA(QUuid("{f000aa51-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_GYROSCOPE_CONTROL(QUuid("{f000aa52-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_GYROSCOPE_PERIOD(QUuid("{f000aa53-0451-4000-b000-000000000000}"));

// test service                              "{f000aa60-0451-4000-b000-000000000000}"

const QBluetoothUuid TI_SENSORTAG_IO_SERVICE(QUuid("{f000aa64-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_IO_DATA(QUuid("{f000aa65-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_IO_CONTROL(QUuid("{f000aa66-0451-4000-b000-000000000000}"));

const QBluetoothUuid TI_SENSORTAG_LIGHT_SERVICE(QUuid("{f000aa70-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_LIGHT_DATA(QUuid("{f000aa71-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_LIGHT_CONTROL(QUuid("{f000aa72-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_LIGHT_PERIOD(QUuid("{f000aa73-0451-4000-b000-000000000000}"));

const QBluetoothUuid TI_SENSORTAG_MOVEMENT_SERVICE(QUuid("{f000aa80-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_MOVEMENT_DATA(QUuid("{f000aa81-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_MOVEMENT_CONTROL(QUuid("{f000aa82-0451-4000-b000-000000000000}"));
const QBluetoothUuid TI_SENSORTAG_MOVEMENT_PERIOD(QUuid("{f000aa83-0451-4000-b000-000000000000}"));

// keypress                                  "{f000ffe0-0000-1000-8000-00805f9b34fb"

// Connection Control Service                "{f000ccc0-0451-4000-b000-000000000000}"
// OAD (over air download) Service           "{f000ffc0-0451-4000-b000-000000000000}"

const QBluetoothUuid TI_SENSORTAG_INFO_SERVICE(QUuid("{0000180a-0000-1000-8000-00805f9b34fb}"));
//we might want to use these somehow
//const QBluetoothUuid TI_SENSORTAG_BATTERY_SERVICE         "180F"
//const QBluetoothUuid TI_SENSORTAG_BATTERY_BATTERY_LEVEL   "2A19"

// {00001800-0000-1000-8000-00805f9b34fb} //Generic Access Service
// {00001801-0000-1000-8000-00805f9b34fb} //Generic Attribute Service

class SensorTagBase : public QSensorBackend
{
    Q_OBJECT
    Q_DECLARE_PRIVATE(SensorTagBase)
public:
    SensorTagBase(QSensor *sensor);
    virtual ~SensorTagBase();

    quint64 produceTimestamp();
signals:
    void accelDataAvailable(const QAccelerometerReading &);
    void luxDataAvailable(qreal);
    void tempDataAvailable(qreal);
    void humidityDataAvailable(qreal);
    void pressureDataAvailable(qreal);
    void gyroDataAvailable(const QGyroscopeReading &);
    void magDataAvailable(const QMagnetometerReading &);

protected:
    void start() override;
    void stop() override;
    QLowEnergyService *leService;
    QBluetoothUuid *serviceId;

private:
    SensorTagBasePrivate *d_ptr;
    friend class SensorTagTemperatureSensor;
    friend class SensorTagAls;
    friend class SensorTagHumiditySensor;
    friend class SensorTagLightSensor;

    friend class SensorTagPressureSensor;
    friend class SensorTagAccelerometer;

    friend class SensorTagGyroscope;
    friend class SensorTagMagnetometer;
};

#endif // SENSORTAGBASEPRIVATE_H