-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbleuart.cpp
196 lines (171 loc) · 6.2 KB
/
bleuart.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
#include "app.h"
#include "bleuart.h"
#define NORDIC_SERVICE QString("6e400001-b5a3-f393-e0a9-e50e24dcca9e")
#define NORDIC_TX QString("6e400002-b5a3-f393-e0a9-e50e24dcca9e")
#define NORDIC_RX QString("6e400003-b5a3-f393-e0a9-e50e24dcca9e")
BleUART::BleUART(QObject *parent) :
QObject(parent)
{
m_foundUARTService = false;
m_control = 0;
m_service = 0;
}
void BleUART::connectToDevice(QBluetoothDeviceInfo device)
{
m_control = new QLowEnergyController(device, this);
m_control->setRemoteAddressType(QLowEnergyController::RandomAddress); // FIXME (or Public)
connect(m_control, &QLowEnergyController::serviceDiscovered,
this, &BleUART::serviceDiscovered);
connect(m_control, &QLowEnergyController::discoveryFinished,
this, &BleUART::serviceScanDone);
connect(m_control, &QLowEnergyController::connected, this, &BleUART::handleConnected);
connect(m_control, &QLowEnergyController::disconnected, this, &BleUART::handleDisconnected);
connect(m_control, static_cast<void (QLowEnergyController::*)(QLowEnergyController::Error)>(&QLowEnergyController::error),
this, &BleUART::handleError);
// Connect
m_control->connectToDevice();
}
BleUART::~BleUART() {
if (m_control) {
m_control->disconnectFromDevice();
delete m_control;
}
}
void BleUART::handleConnected()
{
g_app->log("BLE: Controller connected. Search services...");
m_control->discoverServices();
}
void BleUART::handleDisconnected()
{
if (m_service) {
delete m_service;
m_service = 0;
}
g_app->log("BLE: LowEnergy controller disconnected");
emit connectionChanged(false); // disconnected
}
void BleUART::handleError(QLowEnergyController::Error error)
{
Q_UNUSED(error);
g_app->error("BLE: Cannot connect to remote device.");
}
void BleUART::serviceDiscovered(const QBluetoothUuid &gatt)
{
if (gatt == QBluetoothUuid(NORDIC_SERVICE)) {
m_foundUARTService = true;
g_app->log("BLE: UART Service found.");
}
}
void BleUART::serviceScanDone()
{
g_app->log("BLE: Service scan done.");
// Delete old service if available
if (m_service) {
delete m_service;
m_service = 0;
}
if (m_foundUARTService)
m_service = m_control->createServiceObject(QBluetoothUuid(NORDIC_SERVICE), this);
if (m_service) {
connect(m_service, &QLowEnergyService::stateChanged, this, &BleUART::serviceStateChanged);
connect(m_service, &QLowEnergyService::characteristicChanged, this, &BleUART::updateCharacteristicValue);
connect(m_service, &QLowEnergyService::descriptorWritten, this, &BleUART::confirmedDescriptorWrite);
connect(m_service, &QLowEnergyService::characteristicWritten, this, &BleUART::confirmedCharacteristicWrite);
m_service->discoverDetails();
} else {
g_app->error("BLE: UART Service not found.");
}
}
void BleUART::serviceStateChanged(QLowEnergyService::ServiceState s)
{
switch (s) {
case QLowEnergyService::DiscoveringServices:
g_app->log("BLE: Discovering services...");
break;
case QLowEnergyService::ServiceDiscovered:
{
g_app->log("BLE: Service discovered.");
m_txChar = m_service->characteristic(QBluetoothUuid(NORDIC_TX));
if (!m_txChar.isValid()) {
g_app->error("BLE: TX Characteristic not found.");
}
m_rxChar = m_service->characteristic(QBluetoothUuid(NORDIC_RX));
if (!m_rxChar.isValid()) {
g_app->error("BLE: RX Characteristic not found.");
} else {
m_rxDesc = m_rxChar.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
if (m_rxDesc.isValid())
m_service->writeDescriptor(m_rxDesc, QByteArray::fromHex("0100"));
}
break;
}
default:
//nothing for now
break;
}
}
void BleUART::updateCharacteristicValue(const QLowEnergyCharacteristic &c, const QByteArray &value)
{
if (c == m_rxChar) {
g_app->log("BLE: RX Characteristic changed");
//const quint8 *data = reinterpret_cast<const quint8 *>(value.constData());
emit dataReceived(value);
} else {
g_app->warn("BLE: Unknown Characteristic changed");
}
}
void BleUART::confirmedDescriptorWrite(const QLowEnergyDescriptor &d, const QByteArray &value)
{
if (d.isValid() && d == m_rxDesc && value == QByteArray::fromHex("0100")) {
g_app->log("BLE: Descriptor written - notifications enabled");
emit connectionChanged(true); // properly connected
}
if (d.isValid() && d == m_rxDesc && value == QByteArray::fromHex("0000")) {
//disabled notifications -> assume disconnect intent
m_control->disconnectFromDevice();
delete m_service;
m_service = 0;
}
}
void BleUART::confirmedCharacteristicWrite(const QLowEnergyCharacteristic &d, const QByteArray &value)
{
//g_app->log("BLE: Characteristic written" + QString::fromUtf8(value));
if (d==m_txChar) {
if (bleWriteQueue.length() == 0)
emit dataWritten();
else {
if (m_service) {
QByteArray data = bleWriteQueue.left(BLE_MAX_WRITE_SIZE);
bleWriteQueue.remove(0,BLE_MAX_WRITE_SIZE);
m_service->writeCharacteristic(m_txChar, data, QLowEnergyService::WriteWithResponse);
}
}
} else {
g_app->warn("BLE: Unknown Characteristic written");
}
}
void BleUART::write(QByteArray data) {
if (m_txChar.isValid() && m_service) {
if (data.length() > BLE_MAX_WRITE_SIZE) {
bleWriteQueue.append(data);
data = bleWriteQueue.left(BLE_MAX_WRITE_SIZE);
bleWriteQueue.remove(0,BLE_MAX_WRITE_SIZE);
}
m_service->writeCharacteristic(m_txChar, data, QLowEnergyService::WriteWithResponse);
}
}
void BleUART::disconnect()
{
m_foundUARTService = false;
//disable notifications
if (m_rxDesc.isValid() && m_service
&& m_rxDesc.value() == QByteArray::fromHex("0100")) {
m_service->writeDescriptor(m_rxDesc, QByteArray::fromHex("0000"));
} else {
if (m_control)
m_control->disconnectFromDevice();
delete m_service;
m_service = 0;
}
}