-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbluetoothleinterface.cpp
276 lines (231 loc) · 11 KB
/
bluetoothleinterface.cpp
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
#include "bluetoothleinterface.h"
#include <QLowEnergyCharacteristic>
#include "qaesencryption.h"
BluetoothLEInterface::BluetoothLEInterface()
{
m_deviceDiscoveryAgent = new QBluetoothDeviceDiscoveryAgent(this);
m_deviceDiscoveryAgent->setLowEnergyDiscoveryTimeout(60000);
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered, this, &BluetoothLEInterface::addDevice);
connect(m_deviceDiscoveryAgent, static_cast<void (QBluetoothDeviceDiscoveryAgent::*)(QBluetoothDeviceDiscoveryAgent::Error)>(&QBluetoothDeviceDiscoveryAgent::error),
this, &BluetoothLEInterface::scanError);
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::finished, this, &BluetoothLEInterface::scanFinished);
connect(m_deviceDiscoveryAgent, &QBluetoothDeviceDiscoveryAgent::canceled, this, &BluetoothLEInterface::scanFinished);
}
void BluetoothLEInterface::startScan()
{
m_deviceDiscoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
void BluetoothLEInterface::scanError()
{
qDebug() << "Scan Error";
}
void BluetoothLEInterface::scanFinished()
{
qDebug() << "Scan Finished";
}
void BluetoothLEInterface::connectToDevice(const QString &address)
{
}
void BluetoothLEInterface::addDevice(const QBluetoothDeviceInfo &device)
{
qDebug() << "Found" << device.address() << device.name();
// If device is LowEnergy-device, add it to the list
if (device.coreConfigurations() & QBluetoothDeviceInfo::LowEnergyCoreConfiguration) {
m_devices.append(device);
qDebug() << "Low Energy device found. Scanning more...";
if (device.name().toLower().contains("amazfit")) {
qDebug() << "Found an amazfit, scanning services";
m_control = new QLowEnergyController(device, this);
connect(m_control, &QLowEnergyController::serviceDiscovered,
this, &BluetoothLEInterface::serviceDiscovered);
connect(m_control, &QLowEnergyController::discoveryFinished,
this, [this] {
qDebug() << "Service scan done.";
});
connect(m_control, static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
this, [this](QLowEnergyController::Error error) {
Q_UNUSED(error);
qDebug() << "Cannot connect to remote device.";
});
connect(m_control, &QLowEnergyController::connected, this, [this]() {
qDebug() << "Controller connected. Search services...";
m_control->discoverServices();
});
connect(m_control, &QLowEnergyController::disconnected, this, [this]() {
qDebug() << "LowEnergy controller disconnected";
});
// Connect
m_control->connectToDevice();
m_control->discoverServices();
}
}
}
void BluetoothLEInterface::serviceDiscovered(const QBluetoothUuid &gatt)
{
qDebug() << "Service discovered:" << gatt.toString();
if (gatt == QBluetoothUuid(UUID_SERVICE_BIP_AUTH)) {
qDebug() << "Creating service object";
m_service = m_control->createServiceObject(QBluetoothUuid(UUID_SERVICE_BIP_AUTH), this);
qDebug() << m_service->serviceName();
if (m_service) {
connect(m_service, &QLowEnergyService::stateChanged, this, &BluetoothLEInterface::serviceStateChanged);
connect(m_service, &QLowEnergyService::characteristicChanged, this, &BluetoothLEInterface::authCharacteristicChanged);
//connect(m_service, &QLowEnergyService::descriptorWritten, this, &DeviceHandler::confirmedDescriptorWrite);
m_service->discoverDetails();
} else {
qDebug() << "Service not found";
}
}
}
void BluetoothLEInterface::characteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue)
{
qDebug() << "Changed:" << characteristic.uuid() << "(" << characteristic.name() << "):" << newValue;
}
void BluetoothLEInterface::serviceStateChanged(QLowEnergyService::ServiceState s)
{
switch (s) {
case QLowEnergyService::DiscoveringServices:
qDebug() << "xDiscovering services...";
break;
case QLowEnergyService::ServiceDiscovered:
{
qDebug() << "xService discovered.";
Q_FOREACH(QLowEnergyCharacteristic c, m_service->characteristics()) {
qDebug() << "Characteristic:" << c.uuid() << c.name();
}
qDebug() << "Getting the auth characteristic";
m_authChar = m_service->characteristic(QBluetoothUuid(UUID_CHARACTERISTEC_AUTH));
if (!m_authChar.isValid()) {
qDebug() << "Auth service not found.";
break;
}
qDebug() << "Registering for notifications on the auth service";
m_notificationDesc = m_authChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
if (m_notificationDesc.isValid())
m_service->writeDescriptor(m_notificationDesc, QByteArray::fromHex("0100"));
qDebug() << "Writing special value to trigger pair dialog";
m_service->writeCharacteristic(m_authChar, QByteArray(&AUTH_SEND_KEY, 1) + QByteArray(&AUTH_BYTE, 1) + AUTH_SECRET_KEY);
break;
}
default:
//nothing for now
break;
}
}
void BluetoothLEInterface::authCharacteristicChanged(const QLowEnergyCharacteristic &c, const QByteArray &value)
{
// ignore any other characteristic change -> shouldn't really happen though
if (c.uuid() != QBluetoothUuid(UUID_CHARACTERISTEC_AUTH)) {
qDebug() << "Expecting auth UUID but got" << c.uuid();
return;
}
qDebug() << "Received data:" << value.size() << value.toHex();
if (value.size() < 3) {
return;
}
if (value[0] == AUTH_RESPONSE && value[1] == AUTH_SEND_KEY && value[2] == AUTH_SUCCESS) {
qDebug() << "Received initial auth success, requesting random auth number";
m_service->writeCharacteristic(m_authChar, QByteArray(&AUTH_REQUEST_RANDOM_AUTH_NUMBER, 1) + QByteArray(&AUTH_BYTE, 1));
} else if (value[0] == AUTH_RESPONSE && value[1] == AUTH_REQUEST_RANDOM_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
qDebug() << "Received random auth number, sending encrypted auth number";
m_service->writeCharacteristic(m_authChar, QByteArray(&AUTH_SEND_ENCRYPTED_AUTH_NUMBER, 1) + QByteArray(&AUTH_BYTE, 1) + handleAesAuth(value.mid(3, 17), AUTH_SECRET_KEY));
} else if (value[0] == AUTH_RESPONSE && value[1] == AUTH_SEND_ENCRYPTED_AUTH_NUMBER && value[2] == AUTH_SUCCESS) {
qDebug() << "Authenticated, go read the device information!";
} else {
qDebug() << "Unexpected data";
}
}
QByteArray BluetoothLEInterface::handleAesAuth(QByteArray data, QByteArray secretKey)
{
return QAESEncryption::Crypt(QAESEncryption::AES_128, QAESEncryption::ECB, data, secretKey, QByteArray(), QAESEncryption::ZERO);
}
void BluetoothLEInterface::requestDeviceInfo()
{
m_infoService = m_control->createServiceObject(QBluetoothUuid::DeviceInformation, this);
if (m_infoService) {
qDebug() << m_infoService->serviceName();
connect(m_infoService, &QLowEnergyService::stateChanged, this, &BluetoothLEInterface::infoServiceStateChanged);
connect(m_infoService, &QLowEnergyService::characteristicRead, this, &BluetoothLEInterface::characteristicRead);
m_infoService->discoverDetails();
} else {
qDebug() << "Service not found";
}
}
void BluetoothLEInterface::characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
{
qDebug() << "Read:" << characteristic.uuid() << "(" << characteristic.name() << "):" << value;
}
void BluetoothLEInterface::characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
{
qDebug() << "Written:" << characteristic.uuid() << "(" << characteristic.name() << "):" << value;
}
void BluetoothLEInterface::infoServiceStateChanged(QLowEnergyService::ServiceState s)
{
switch (s) {
case QLowEnergyService::DiscoveringServices:
qDebug() << "Discovering services...";
break;
case QLowEnergyService::ServiceDiscovered:
{
qDebug() << "Service discovered.";
Q_FOREACH(QLowEnergyCharacteristic c, m_infoService->characteristics()) {
qDebug() << "Characteristic:" << c.uuid() << c.name();
}
QLowEnergyCharacteristic c = m_infoService->characteristic(QBluetoothUuid(UUID_CHARACTERISTIC_INFO_SERIAL_NO));
m_infoService->readCharacteristic(c);
c = m_infoService->characteristic(QBluetoothUuid(UUID_CHARACTERISTIC_INFO_HARDWARE_REV));
m_infoService->readCharacteristic(c);
c = m_infoService->characteristic(QBluetoothUuid(UUID_CHARACTERISTIC_INFO_SOFTWARE_REV));
m_infoService->readCharacteristic(c);
c = m_infoService->characteristic(QBluetoothUuid(UUID_CHARACTERISTIC_INFO_SYSTEM_ID));
m_infoService->readCharacteristic(c);
c = m_infoService->characteristic(QBluetoothUuid(UUID_CHARACTERISTIC_INFO_PNP_ID));
m_infoService->readCharacteristic(c);
break;
}
default:
//nothing for now
break;
}
}
void BluetoothLEInterface::alertServiceStateChanged(QLowEnergyService::ServiceState s)
{
switch (s) {
case QLowEnergyService::DiscoveringServices:
qDebug() << "Discovering services...";
break;
case QLowEnergyService::ServiceDiscovered:
{
qDebug() << "Service discovered.";
Q_FOREACH(QLowEnergyCharacteristic c, m_alertService->characteristics()) {
qDebug() << "Characteristic:" << c.uuid() << c.name();
}
QLowEnergyCharacteristic control = m_alertService->characteristic(QBluetoothUuid(UUID_CHARACTERISTIC_ALERT_CONTROL));
m_alertService->writeCharacteristic(control, QByteArray::fromHex("0000"));
QLowEnergyCharacteristic c = m_alertService->characteristic(QBluetoothUuid(UUID_CHARACTERISTIC_NEW_ALERT));
m_alertService->writeCharacteristic(c, QByteArray::fromHex("01") + QByteArray::fromHex("01") + "TEST\nThis is a long message hope it displays ok");
break;
}
default:
//nothing for now
break;
}
}
void BluetoothLEInterface::sendMessage()
{
m_alertService = m_control->createServiceObject(QBluetoothUuid(UUID_SERVICE_ALERT_NOTIFICATION), this);
if (m_alertService) {
qDebug() << m_alertService->serviceName();
connect(m_alertService, &QLowEnergyService::stateChanged, this, &BluetoothLEInterface::alertServiceStateChanged);
connect(m_alertService, &QLowEnergyService::characteristicRead, this, &BluetoothLEInterface::characteristicRead);
connect(m_alertService, &QLowEnergyService::characteristicWritten, this, &BluetoothLEInterface::characteristicWritten);
//connect(m_alertService, &QLowEnergyService::error, this, &BluetoothLEInterface::error);
m_alertService->discoverDetails();
} else {
qDebug() << "Service not found";
}
}
void BluetoothLEInterface::error(QLowEnergyService::ServiceError newError)
{
qDebug() << "Error" << newError;
}