-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeularmanager.cpp
More file actions
219 lines (187 loc) · 7.19 KB
/
timeularmanager.cpp
File metadata and controls
219 lines (187 loc) · 7.19 KB
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
/*
Copyright 2019 Mike Krus
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. 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.
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 HOLDER 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.
*/
#include "timeularmanager.h"
#include <QDebug>
#include <QBluetoothUuid>
namespace {
const QBluetoothUuid zeiOrientationService(QLatin1Literal("{c7e70010-c847-11e6-8175-8c89a55d403c}"));
const QBluetoothUuid zeiOrientationCharacteristic(QLatin1Literal("{c7e70012-c847-11e6-8175-8c89a55d403c}"));
}
TimeularManager::TimeularManager(QObject *parent)
: QObject(parent)
{
m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(5000);
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished,
[this]() {
if (!m_service)
this->startDiscovery();
});
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
this, &TimeularManager::deviceDiscovered);
}
TimeularManager::~TimeularManager()
{
delete m_service;
}
TimeularManager::Orientation TimeularManager::orientation() const
{
return m_orientation;
}
TimeularManager::Status TimeularManager::status() const
{
return m_status;
}
void TimeularManager::setStatus(Status status)
{
if (status != m_status) {
m_status = status;
emit statusChanged(m_status);
}
}
void TimeularManager::startDiscovery()
{
if (m_status != Disconneted)
return;
qDebug() << "Starting Discovery";
setStatus(Connecting);
m_deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
void TimeularManager::deviceDiscovered(const QBluetoothDeviceInfo &info)
{
if (m_status != Connecting)
return;
if (!(info.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration))
return;
if (info.name() == QLatin1Literal("Timeular ZEI")) {
qDebug() << "Connecting to device";
if (!m_controller) {
m_controller = QLowEnergyController::createCentral(info, this);
m_controller ->setRemoteAddressType(QLowEnergyController::RandomAddress);
connect(m_controller, &QLowEnergyController::connected,
this, &TimeularManager::deviceConnected);
connect(m_controller, QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error),
this, &TimeularManager::errorReceived);
connect(m_controller, &QLowEnergyController::disconnected,
this, &TimeularManager::deviceDisconnected);
connect(m_controller, &QLowEnergyController::serviceDiscovered,
this, &TimeularManager::addLowEnergyService);
connect(m_controller, &QLowEnergyController::discoveryFinished,
this, &TimeularManager::serviceScanDone);
}
m_controller->connectToDevice();
}
}
void TimeularManager::deviceConnected()
{
m_controller->discoverServices();
}
void TimeularManager::deviceDisconnected()
{
setStatus(Disconneted);
delete m_service;
m_service = nullptr;
qDebug() << "Device Disconneted";
}
void TimeularManager::errorReceived(QLowEnergyController::Error /*error*/)
{
qWarning() << "Error: " << m_controller->errorString();
}
void TimeularManager::serviceScanDone()
{
if (m_service) {
delete m_service;
m_service = nullptr;
}
if (m_serviceDiscovered)
m_service = m_controller->createServiceObject(QBluetoothUuid(zeiOrientationService), this);
if (m_service) {
connect(m_service, &QLowEnergyService::stateChanged,
this, &TimeularManager::serviceStateChanged);
connect(m_service, &QLowEnergyService::characteristicChanged,
this, &TimeularManager::deviceDataChanged);
connect(m_service, &QLowEnergyService::descriptorWritten,
this, &TimeularManager::confirmedDescriptorWrite);
m_service->discoverDetails();
} else {
qDebug() << "Service not found";
}
}
void TimeularManager::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
{
if (d.isValid() && d == m_notificationDesc && value == QByteArray::fromHex("0000")) {
//disabled notifications -> assume disconnect intent
m_controller->disconnectFromDevice();
delete m_service;
m_service = nullptr;
}
}
void TimeularManager::addLowEnergyService(const QBluetoothUuid &serviceUuid)
{
if (serviceUuid == QBluetoothUuid(zeiOrientationService)) {
m_serviceDiscovered = true;
}
}
void TimeularManager::serviceStateChanged(QLowEnergyService::ServiceState newState)
{
switch (newState) {
case QLowEnergyService::DiscoveringServices:
break;
case QLowEnergyService::ServiceDiscovered: {
const QLowEnergyCharacteristic orientationChar = m_service->characteristic(QBluetoothUuid(zeiOrientationCharacteristic));
if (!orientationChar.isValid()) {
qDebug() << "Orientation data not found";
setStatus(Disconneted);
break;
}
m_notificationDesc = orientationChar.descriptor(QBluetoothUuid(QLatin1String("{00002902-0000-1000-8000-00805f9b34fb}")));
if (m_notificationDesc.isValid()) {
qDebug() << "Device Connected";
setStatus(Connected);
m_service->writeDescriptor(m_notificationDesc, QByteArray::fromHex("0100"));
} else {
setStatus(Disconneted);
}
break;
}
default:
//nothing for now
break;
}
}
void TimeularManager::deviceDataChanged(const QLowEnergyCharacteristic &c, const QByteArray &value)
{
if (c.uuid() != QBluetoothUuid(zeiOrientationCharacteristic))
return;
auto data = reinterpret_cast<const quint8 *>(value.constData());
quint8 orientation = *data;
qDebug() << "Orientation" << orientation;
if (orientation > 8)
orientation = 0;
if(orientation != m_orientation) {
m_orientation = static_cast<Orientation>(orientation);
emit orientationChanged(m_orientation);
}
}