summaryrefslogtreecommitdiffstats
path: root/tradeshow/iot-sensortag/bluetoothdevice.cpp
blob: 2ede116190d8e75821f88f315948802dcf65e5be (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of Qt for Device Creation.
**
** $QT_BEGIN_LICENSE:BSD$
** 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.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "bluetoothdevice.h"

#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QLowEnergyService>
#include <QLoggingCategory>
#include <QList>
#include <QTimer>
#include <QtMath>
#include <QDateTime>
#include "bluetoothapiconstants.h"

Q_DECLARE_LOGGING_CATEGORY(boot2QtDemos)

typedef struct {
    qint16 gyrox;
    qint16 gyroy;
    qint16 gyroz;
    qint16 accelx;
    qint16 accely;
    qint16 accelz;
    qint16 magnetomx;
    qint16 magnetomy;
    qint16 magnetomz;
} movement_data_t;

BluetoothDevice::BluetoothDevice()
    : m_controller(0)
    , m_irTemperatureService(0)
    , m_baroService(0)
    , m_humidityService(0)
    , m_lightService(0)
    , m_motionService(0)
    , m_deviceState(DeviceState::Disconnected)
    , m_temperatureMeasurementStarted(false)
    , m_barometerMeasurementStarted(false)
    , m_humidityMeasurementStarted(false)
    , m_lightIntensityMeasurementStarted(false)
    , m_motionMeasurementStarted(false)
{
    m_lastMilliseconds = QDateTime::currentMSecsSinceEpoch();
    statusUpdated("Device created");

    m_serviceDetailsTimer = new QTimer(this);
    m_serviceDetailsTimer->setInterval(5000);
    m_serviceDetailsTimer->setSingleShot(true);
    connect(m_serviceDetailsTimer, &QTimer::timeout, [=]() {
        qCDebug(boot2QtDemos) << "Service details timer timeout. No more services details found";
        if (isDeviceReady())
            setState(Connected);
        else
            setState(Disconnected);
    });
}

BluetoothDevice::BluetoothDevice(const QBluetoothDeviceInfo &d)
    : BluetoothDevice()
{
    m_deviceInfo = d;
}

BluetoothDevice::~BluetoothDevice()
{
    if (m_controller) {
        m_controller->disconnect();
        delete m_controller;
    }
}

QString BluetoothDevice::getAddress() const
{
#if defined(Q_OS_DARWIN)
    // On Apple platforms we do not have addresses,
    // only unique UUIDs generated by Core Bluetooth.
    return m_deviceInfo.deviceUuid().toString();
#else
    return m_deviceInfo.address().toString();
#endif
}

QString BluetoothDevice::getName() const
{
    return m_deviceInfo.name();
}

void BluetoothDevice::scanServices()
{
    setState(Scanning);
    if (!m_controller) {
        statusUpdated("(Connecting to device...)");
        // Connecting signals and slots for connecting to LE services.
        m_controller = new QLowEnergyController(m_deviceInfo);
        connect(m_controller, &QLowEnergyController::connected,
                this, &BluetoothDevice::deviceConnected);
        connect(m_controller, QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error),
                this, &BluetoothDevice::errorReceived);
        connect(m_controller, &QLowEnergyController::disconnected,
                this, &BluetoothDevice::deviceDisconnected);
        connect(m_controller, &QLowEnergyController::serviceDiscovered,
                this, &BluetoothDevice::addLowEnergyService);
        connect(m_controller, &QLowEnergyController::discoveryFinished,
                this, &BluetoothDevice::serviceScanDone);

        m_controller->setRemoteAddressType(QLowEnergyController::PublicAddress);
    }
    m_controller->connectToDevice();
}

void BluetoothDevice::addLowEnergyService(const QBluetoothUuid &serviceUuid)
{
    if (serviceUuid == QBluetoothUuid(QLatin1String(IRTEMPERATURESENSOR_SERVICE_UUID))) {
        qCDebug(boot2QtDemos) << "Found infrared temperature service.";
        m_irTemperatureService = m_controller->createServiceObject(serviceUuid);
        if (!m_irTemperatureService) {
            qWarning() << "Could not create infrared temperature service object.";
            return;
        }
        connect(m_irTemperatureService, &QLowEnergyService::stateChanged,
                this, &BluetoothDevice::temperatureDetailsDiscovered);
        connect(m_irTemperatureService, &QLowEnergyService::characteristicChanged,
                this, &BluetoothDevice::updateTemperature);
        m_irTemperatureService->discoverDetails();
    } else if (serviceUuid == QBluetoothUuid(QLatin1String(BAROMETER_SERVICE_UUID))) {
        qCDebug(boot2QtDemos) << "Found barometer service.";
        m_baroService = m_controller->createServiceObject(serviceUuid);
        if (!m_baroService) {
            qWarning() << "Could not create barometer service object.";
            return;
        }
        connect(m_baroService, &QLowEnergyService::stateChanged,
                this, &BluetoothDevice::barometerDetailsDiscovered);
        connect(m_baroService, &QLowEnergyService::characteristicChanged,
                this, &BluetoothDevice::updatePressure);
        m_baroService->discoverDetails();
    } else if (serviceUuid == QBluetoothUuid(QLatin1String(HUMIDITYSENSOR_SERVICE_UUID))) {
        qCDebug(boot2QtDemos) << "Found humidity service.";
        m_humidityService = m_controller->createServiceObject(serviceUuid);
        if (!m_humidityService) {
            qWarning() << "Could not create humidity service object.";
            return;
        }
        connect(m_humidityService, &QLowEnergyService::stateChanged,
                this, &BluetoothDevice::humidityDetailsDiscovered);
        connect(m_humidityService, &QLowEnergyService::characteristicChanged,
                this, &BluetoothDevice::updateHumidity);
        m_humidityService->discoverDetails();
    } else if (serviceUuid == QBluetoothUuid(QLatin1String(LIGHTSENSOR_SERVICE_UUID))) {
        qCDebug(boot2QtDemos) << "Found light service.";
        m_lightService = m_controller->createServiceObject(serviceUuid);
        if (!m_lightService) {
            qWarning() << "Could not create light service object.";
            return;
        }
        connect(m_lightService, &QLowEnergyService::stateChanged,
                this, &BluetoothDevice::lightIntensityDetailsDiscovered);
        connect(m_lightService, &QLowEnergyService::characteristicChanged,
                this, &BluetoothDevice::updateLight);
        m_lightService->discoverDetails();
    } else if (serviceUuid == QBluetoothUuid(QLatin1String(MOTIONSENSOR_SERVICE_UUID))) {
        qCDebug(boot2QtDemos) << "Found motion service.";
        m_motionService = m_controller->createServiceObject(serviceUuid);
        if (!m_motionService) {
            qWarning() << "Could not create motion service object.";
            return;
        }
        connect(m_motionService, &QLowEnergyService::stateChanged,
                this, &BluetoothDevice::motionDetailsDiscovered);
        connect(m_motionService, &QLowEnergyService::characteristicChanged,
                this, &BluetoothDevice::updateMotionValue);
        m_motionService->discoverDetails();
    } else {
        qCDebug(boot2QtDemos) << "Found unhandled service with id" << serviceUuid << ".";
    }
}

void BluetoothDevice::serviceScanDone()
{
    statusUpdated("(Service scan done!)");
    qCDebug(boot2QtDemos) << "ServiceScan done.";
    if (!m_irTemperatureService)
        qCDebug(boot2QtDemos) << "Infrared temperature service not found.";

    if (!m_baroService)
        qCDebug(boot2QtDemos) << "Barometer service not found.";

    if (!m_humidityService)
        qCDebug(boot2QtDemos) << "Humidity service not found.";

    if (!m_lightService)
        qCDebug(boot2QtDemos) << "Light service not found.";

    if (!m_motionService)
        qCDebug(boot2QtDemos) << "Motion service not found.";

    m_serviceDetailsTimer->start();
}

void BluetoothDevice::temperatureDetailsDiscovered(QLowEnergyService::ServiceState newstate)
{
    if (newstate == QLowEnergyService::ServiceDiscovered) {
        connect(m_irTemperatureService, &QLowEnergyService::characteristicWritten, [=]() {
            qCDebug(boot2QtDemos) << "Wrote Characteristic - temperature";
        });

        connect(m_irTemperatureService, QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error),
            [=](QLowEnergyService::ServiceError newError) {
            qCDebug(boot2QtDemos) << "error while writing - temperature:" << newError;
        });

        // data characteristic
        QLowEnergyCharacteristic characteristic = m_irTemperatureService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa01-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            const QLowEnergyDescriptor notificationDescriptor = characteristic.descriptor(
                        QBluetoothUuid::ClientCharacteristicConfiguration);
            if (notificationDescriptor.isValid())
                m_irTemperatureService->writeDescriptor(notificationDescriptor, QByteArray::fromHex(ENABLE_NOTIF_STR));
        }

        // configuration characteristic
        characteristic = m_irTemperatureService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa02-0451-4000-b000-000000000000")));
        if (characteristic.isValid())
            m_irTemperatureService->writeCharacteristic(characteristic, QByteArray::fromHex(START_MEASUREMENT_STR), QLowEnergyService::WriteWithResponse);

        // timeout characteristic
        characteristic = m_irTemperatureService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa03-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            m_irTemperatureService->writeCharacteristic(characteristic, QByteArray::fromHex(SENSORTAG_SLOW_TIMER_TIMEOUT_STR), QLowEnergyService::WriteWithResponse); // Period 1 second
        }

        m_temperatureMeasurementStarted = true;
        updateServiceDetails();
    }
}

void BluetoothDevice::barometerDetailsDiscovered(QLowEnergyService::ServiceState newstate) {
    if (newstate == QLowEnergyService::ServiceDiscovered) {
        connect(m_baroService, &QLowEnergyService::characteristicWritten, [=]() {
            qCDebug(boot2QtDemos) << "Wrote Characteristic - barometer";
        });

        connect(m_baroService, QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error),
            [=](QLowEnergyService::ServiceError newError) {
            qCDebug(boot2QtDemos) << "error while writing - barometer:" << newError;
        });

        // data characteristic
        QLowEnergyCharacteristic characteristic = m_baroService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa41-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            const QLowEnergyDescriptor notificationDescriptor = characteristic.descriptor(
                        QBluetoothUuid::ClientCharacteristicConfiguration);
            if (notificationDescriptor.isValid())
                m_baroService->writeDescriptor(notificationDescriptor, QByteArray::fromHex(ENABLE_NOTIF_STR));
        }

        // configuration characteristic
        characteristic = m_baroService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa42-0451-4000-b000-000000000000")));
        if (characteristic.isValid())
            m_baroService->writeCharacteristic(characteristic, QByteArray::fromHex(START_MEASUREMENT_STR), QLowEnergyService::WriteWithResponse);

        // timeout characteristic
        characteristic = m_baroService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa44-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            m_baroService->writeCharacteristic(characteristic, QByteArray::fromHex(SENSORTAG_MEDIUM_TIMER_TIMEOUT_STR), QLowEnergyService::WriteWithResponse); // Period 500 ms
        }

        m_barometerMeasurementStarted = true;
        updateServiceDetails();
    }
}

void BluetoothDevice::humidityDetailsDiscovered(QLowEnergyService::ServiceState newstate)
{
    if (newstate == QLowEnergyService::ServiceDiscovered) {
        connect(m_humidityService, &QLowEnergyService::characteristicWritten, [=]() {
            qCDebug(boot2QtDemos) << "Wrote Characteristic - humidity";
        });

        connect(m_humidityService, QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error),
            [=](QLowEnergyService::ServiceError newError) {
            qCDebug(boot2QtDemos) << "error while writing - humidity:" << newError;
        });

        // data characteristic
        QLowEnergyCharacteristic characteristic = m_humidityService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa21-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            const QLowEnergyDescriptor notificationDescriptor = characteristic.descriptor(
                        QBluetoothUuid::ClientCharacteristicConfiguration);
            if (notificationDescriptor.isValid())
                m_humidityService->writeDescriptor(notificationDescriptor, QByteArray::fromHex(ENABLE_NOTIF_STR));
        }

        // configuration characteristic
        characteristic = m_humidityService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa22-0451-4000-b000-000000000000")));
        if (characteristic.isValid())
            m_humidityService->writeCharacteristic(characteristic, QByteArray::fromHex(START_MEASUREMENT_STR), QLowEnergyService::WriteWithResponse);

        // timeout characteristic
        characteristic = m_humidityService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa23-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            m_humidityService->writeCharacteristic(characteristic, QByteArray::fromHex(SENSORTAG_SLOW_TIMER_TIMEOUT_STR), QLowEnergyService::WriteWithResponse); // Period 500 ms
        }

        m_humidityMeasurementStarted = true;
        updateServiceDetails();
    }
}

void BluetoothDevice::lightIntensityDetailsDiscovered(QLowEnergyService::ServiceState newstate)
{
    if (newstate == QLowEnergyService::ServiceDiscovered) {
        connect(m_lightService, &QLowEnergyService::characteristicWritten, [=]() {
            qCDebug(boot2QtDemos) << "Wrote Characteristic - light intensity";
        });

        connect(m_lightService, QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error),
            [=](QLowEnergyService::ServiceError newError) {
            qCDebug(boot2QtDemos) << "error while writing - light intensity:" << newError;
        });

        // data characteristic
        QLowEnergyCharacteristic characteristic = m_lightService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa71-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            const QLowEnergyDescriptor notificationDescriptor = characteristic.descriptor(
                        QBluetoothUuid::ClientCharacteristicConfiguration);
            if (notificationDescriptor.isValid())
                m_lightService->writeDescriptor(notificationDescriptor, QByteArray::fromHex(ENABLE_NOTIF_STR));
        }

        // configuration characteristic
        characteristic = m_lightService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa72-0451-4000-b000-000000000000")));
        if (characteristic.isValid())
            m_lightService->writeCharacteristic(characteristic, QByteArray::fromHex(START_MEASUREMENT_STR), QLowEnergyService::WriteWithResponse);

        // timeout characteristic
        characteristic = m_lightService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa73-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            m_lightService->writeCharacteristic(characteristic, QByteArray::fromHex(SENSORTAG_MEDIUM_TIMER_TIMEOUT_STR), QLowEnergyService::WriteWithResponse); // Period 500 ms
        }

        m_lightIntensityMeasurementStarted = true;
        updateServiceDetails();
    }
}

void BluetoothDevice::motionDetailsDiscovered(QLowEnergyService::ServiceState newstate)
{
    // reset the time once more before we start to receive measurements.
    m_lastMilliseconds = QDateTime::currentMSecsSinceEpoch();

    if (newstate == QLowEnergyService::ServiceDiscovered) {
        connect(m_motionService, &QLowEnergyService::characteristicWritten, [=]() {
            qCDebug(boot2QtDemos) << "Wrote Characteristic - gyro";
        });

        connect(m_motionService, QOverload<QLowEnergyService::ServiceError>::of(&QLowEnergyService::error),
            [=](QLowEnergyService::ServiceError newError) {
            qCDebug(boot2QtDemos) << "error while writing - gyro:" << newError;
        });

        // data characteristic
        QLowEnergyCharacteristic characteristic = m_motionService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa81-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            const QLowEnergyDescriptor notificationDescriptor = characteristic.descriptor(
                        QBluetoothUuid::ClientCharacteristicConfiguration);
            if (notificationDescriptor.isValid())
                m_motionService->writeDescriptor(notificationDescriptor, QByteArray::fromHex(ENABLE_NOTIF_STR));
        }

        // configuration characteristic
        characteristic = m_motionService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa82-0451-4000-b000-000000000000")));
        if (characteristic.isValid())
            m_motionService->writeCharacteristic(characteristic, QByteArray::fromHex(MOVEMENT_ENABLE_SENSORS_BITMASK_VALUE), QLowEnergyService::WriteWithResponse);

        // timeout characteristic
        characteristic = m_motionService->characteristic(
                    QBluetoothUuid(QLatin1String("f000aa83-0451-4000-b000-000000000000")));
        if (characteristic.isValid()) {
            m_motionService->writeCharacteristic(characteristic, QByteArray::fromHex(SENSORTAG_RAPID_TIMER_TIMEOUT_STR), QLowEnergyService::WriteWithResponse);
        }
        m_motionMeasurementStarted = true;
        updateServiceDetails();
    }
}

void BluetoothDevice::updateTemperature(const QLowEnergyCharacteristic &, const QByteArray &value)
{
    irTemperatureReceived(value);
}

void BluetoothDevice::updatePressure(const QLowEnergyCharacteristic &, const QByteArray &value)
{
    barometerReceived(value);
}

void BluetoothDevice::updateHumidity(const QLowEnergyCharacteristic &, const QByteArray &value)
{
    humidityReceived(value);
}

void BluetoothDevice::updateLight(const QLowEnergyCharacteristic &, const QByteArray &value)
{
    lightIntensityReceived(value);
}

void BluetoothDevice::updateMotionValue(const QLowEnergyCharacteristic &, const QByteArray &value)
{
    motionReceived(value);
}

void BluetoothDevice::setState(BluetoothDevice::DeviceState state)
{
    if (m_deviceState != state) {
        m_deviceState = state;
        emit stateChanged();
    }
}

double BluetoothDevice::convertIrTemperatureAPIReadingToCelsius(quint16 rawReading)
{
    // Compute and filter final value according to TI Bluetooth LE API
    const float SCALE_LSB = 0.03125;
    int it = (int)((rawReading) >> 2);
    float t = (float)it;
    return t * SCALE_LSB;
}

bool BluetoothDevice::isDeviceReady() const
{
    if (m_temperatureMeasurementStarted
            || m_barometerMeasurementStarted
            || m_humidityMeasurementStarted
            || m_lightIntensityMeasurementStarted
            || m_motionMeasurementStarted) {
        return true;
    }
    return false;
}

void BluetoothDevice::updateServiceDetails()
{
    if (m_temperatureMeasurementStarted
            && m_barometerMeasurementStarted
            && m_humidityMeasurementStarted
            && m_lightIntensityMeasurementStarted
            && m_motionMeasurementStarted) {
        qCDebug(boot2QtDemos) << "Service details timer stop. All service details found";
        m_serviceDetailsTimer->stop();
        setState(Connected);
    } else {
        m_serviceDetailsTimer->start();
    }
}

void BluetoothDevice::irTemperatureReceived(const QByteArray &value)
{
    const unsigned int rawObjectTemperature = (((quint8)value.at(3)) << 8) + ((quint8)value.at(2));
    const double objectTemperature = convertIrTemperatureAPIReadingToCelsius(rawObjectTemperature);
    const unsigned int rawAmbientTemperature = (((quint8)value.at(1)) << 8) + ((quint8)value.at(0));
    const double ambientTemperature = convertIrTemperatureAPIReadingToCelsius(rawAmbientTemperature);
    emit temperatureChanged(ambientTemperature, objectTemperature);
}

void BluetoothDevice::barometerReceived(const QByteArray &value)
{
    //Merge bytes
    unsigned int temperature_raw;
    unsigned int barometer_raw;
    if (value.length() == 6) {
        temperature_raw = (((quint8)value.at(2)) << 16) + (((quint8)value.at(1)) << 8) + ((quint8)value.at(0));
        barometer_raw = (((quint8)value.at(5)) << 16) + (((quint8)value.at(4)) << 8) + ((quint8)value.at(3));
    } else {
        temperature_raw = (((quint8)value.at(1)) << 8) + ((quint8)value.at(0));
        barometer_raw = (((quint8)value.at(3)) << 8) + ((quint8)value.at(2));
    }

    double temperature = static_cast<double>(temperature_raw);
    temperature /= BAROMETER_API_READING_DIVIDER;

    double barometer = static_cast<double>(barometer_raw);
    barometer /= BAROMETER_API_READING_DIVIDER;
    emit barometerChanged(temperature, barometer);
}

void BluetoothDevice::humidityReceived(const QByteArray &value)
{
    //Merge bytes
    unsigned int humidity_raw = (((quint8)value.at(3)) << 8) + ((quint8)value.at(2));
    double humidity = static_cast<double>(humidity_raw);
    humidity = humidity * HUMIDITY_API_READING_MULTIPLIER;
    emit humidityChanged(humidity);
}

void BluetoothDevice::lightIntensityReceived(const QByteArray &value)
{
    //Merge bytes
    uint16_t lightIntensity_raw;
    lightIntensity_raw = (((quint8)value.at(1)) << 8) + ((quint8)value.at(0));
    uint16_t e, m;
    m = lightIntensity_raw & 0x0FFF;
    e = (lightIntensity_raw & 0xF000) >> 12;

    // Compute and final value according to TI Bluetooth LE API
    double lightIntensity = ((double)m) * (0.01 * (double)qPow(2.0,(qreal)e));
    emit lightIntensityChanged(lightIntensity);
}

void BluetoothDevice::motionReceived(const QByteArray &value)
{
    static MotionSensorData data;
    data.msSincePreviousData = m_lastMilliseconds;
    m_lastMilliseconds = QDateTime::currentMSecsSinceEpoch();
    data.msSincePreviousData = m_lastMilliseconds - data.msSincePreviousData;
    movement_data_t values;
    quint8* writePtr = (quint8*)(&values);

    for (int i = 0; i < 18; ++i) {
        *writePtr = (quint8)value.at(i);
        writePtr++;
    }

    // Data is in little endian. Fix here if needed.

    //Convert gyroscope and accelometer readings to proper units
    data.gyroScope_x = double(values.gyrox) * GYROSCOPE_API_READING_MULTIPLIER;
    data.gyroScope_y = double(values.gyroy) * GYROSCOPE_API_READING_MULTIPLIER;
    data.gyroScope_z = double(values.gyroz) * GYROSCOPE_API_READING_MULTIPLIER;
    // Accelometer at 8G
    data.accelometer_x = double(values.accelx) * ACCELOMETER_API_READING_MULTIPLIER;
    data.accelometer_y = double(values.accely) * ACCELOMETER_API_READING_MULTIPLIER;
    data.accelometer_z = double(values.accelz) * ACCELOMETER_API_READING_MULTIPLIER;
    data.magnetometer_x = double(values.magnetomx);
    data.magnetometer_y = double(values.magnetomy);
    data.magnetometer_z = double(values.magnetomz);
    emit motionChanged(data);
}

void BluetoothDevice::deviceConnected()
{
    if (isDeviceReady()) {
        setState(Connected);
    } else {
        setState(DeviceState::Scanning);
        statusUpdated("(Discovering services...)");
        m_controller->discoverServices();
    }
}

void BluetoothDevice::errorReceived(QLowEnergyController::Error /*error*/)
{
    setState(DeviceState::Error);
    statusUpdated(QString("Error: %1)").arg(m_controller->errorString()));
}

void BluetoothDevice::disconnectFromDevice()
{
    // UI always expects disconnect() signal when calling this signal
    // TODO what is really needed is to extend state() to a multi value
    // and thus allowing UI to keep track of controller progress in addition to
    // device scan progress

    if (m_controller->state() != QLowEnergyController::UnconnectedState)
        m_controller->disconnectFromDevice();
    else
        deviceDisconnected();
}

void BluetoothDevice::deviceDisconnected()
{
    statusUpdated("Disconnect from device");
    setState(BluetoothDevice::Disconnected);
}

BluetoothDevice::DeviceState BluetoothDevice::state() const
{
    return m_deviceState;
}