-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbluetooth_service.cpp
More file actions
237 lines (195 loc) · 6.38 KB
/
Copy pathbluetooth_service.cpp
File metadata and controls
237 lines (195 loc) · 6.38 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "bluetooth_service.h"
#include <QBluetoothSocket>
#include <QBluetoothLocalDevice>
#include <QBluetoothServer>
#define GET_SOCKET(var) \
QBluetoothSocket *(var) = qobject_cast<QBluetoothSocket *>(sender()); \
if (socket == nullptr) { \
qDebug() << "Failed to retrieve socket"; \
return; \
}
#define TRY_RESTART_SERVICE_MS 5000
#define POLL_ADAPTER_STATUS_MS 5000
static const QLatin1String BLUEPASS_UUID("e4d56fb3-b86d-4572-9b0d-44d483eb1eee");
BluetoothService::BluetoothService(QObject *parent)
: QObject(parent),
server_(nullptr),
was_made_discoverable_(false)
{
connect(&retry_timer_, &QTimer::timeout, this, &BluetoothService::retryStart);
connect(&poll_status_timer_, &QTimer::timeout, this, &BluetoothService::updateAdapterStatus);
}
const QString BluetoothService::adapterAddress() const
{
return adapter_address_;
}
void BluetoothService::terminate()
{
stop();
retry_timer_.stop();
poll_status_timer_.stop();
}
BluetoothService::~BluetoothService()
{
terminate();
}
void BluetoothService::start(const QString& localAdapterAddress)
{
if (server_ != nullptr) {
qDebug() << "Server is already running";
return;
}
adapter_address_ = localAdapterAddress;
if (localAdapterAddress.isEmpty()) {
qDebug() << "Can't start service with no local adapter address";
return;
}
server_ = new QBluetoothServer(QBluetoothServiceInfo::RfcommProtocol, this);
server_->setSecurityFlags(QBluetooth::Authorization |
QBluetooth::Authentication |
QBluetooth::Encryption |
QBluetooth::Secure);
connect(server_, &QBluetoothServer::newConnection, this, QOverload<>::of(&BluetoothService::connected));
bool success = server_->listen(QBluetoothAddress(localAdapterAddress));
if (!success) {
qDebug() << "Failed to bind on " << localAdapterAddress;
serverFailed();
return;
}
connect(server_, QOverload<QBluetoothServer::Error>::of(&QBluetoothServer::error), this, &BluetoothService::listeningSocketError);
info_.setAttribute(QBluetoothServiceInfo::ServiceName, "BluePassServer");
info_.setServiceUuid(QBluetoothUuid(BLUEPASS_UUID));
QBluetoothServiceInfo::Sequence protocolDescriptorList;
QBluetoothServiceInfo::Sequence protocol;
protocol << QVariant::fromValue(QBluetoothUuid(QBluetoothUuid::ProtocolUuid::Rfcomm))
<< QVariant::fromValue(quint8(server_->serverPort()));
protocolDescriptorList << QVariant::fromValue(protocol);
info_.setAttribute(QBluetoothServiceInfo::ProtocolDescriptorList,
protocolDescriptorList);
info_.registerService(QBluetoothAddress(localAdapterAddress));
qDebug() << "Bluetooth service started";
poll_status_timer_.start(POLL_ADAPTER_STATUS_MS);
emit started();
}
void BluetoothService::stop()
{
if (server_ == nullptr) {
qDebug() << "Not started yet...";
return;
}
info_.unregisterService();
// disconnect all clients
qDeleteAll(clients_);
delete server_;
server_ = nullptr;
if (was_made_discoverable_) {
setDiscoverable(false);
was_made_discoverable_ = false;
}
qDebug() << "Bluetooth service stopped";
poll_status_timer_.stop();
emit stopped();
}
void BluetoothService::restart(const QString &localAdapterAddress)
{
if (localAdapterAddress == adapter_address_) {
return;
}
if (server_ != nullptr) {
stop();
}
start(localAdapterAddress);
}
void BluetoothService::setDiscoverable(bool discoverable)
{
if (server_ == nullptr) {
qWarning() << "Service has not yet been started";
return;
}
QBluetoothAddress addr(adapter_address_);
QBluetoothLocalDevice adapter(addr, nullptr);
if (discoverable) {
qDebug() << "Making adapter " << adapter_address_ << " discoverable";
adapter.setHostMode(QBluetoothLocalDevice::HostDiscoverable);
was_made_discoverable_ = true;
} else {
qDebug() << "Stop ";
adapter.setHostMode(QBluetoothLocalDevice::HostConnectable);
was_made_discoverable_ = false;
}
}
void BluetoothService::connected()
{
QBluetoothSocket *socket = server_->nextPendingConnection();
if (!socket) {
qDebug() << "Failed to get newly connected socket";
return;
}
clients_.append(socket);
qDebug() << "Connected peer " << socket->peerAddress();
connect(socket, &QBluetoothSocket::readyRead, this, &BluetoothService::readSocket);
connect(socket, &QBluetoothSocket::disconnected, this, &BluetoothService::disconnected);
}
void BluetoothService::disconnected()
{
GET_SOCKET(socket);
tryReadSocket(socket);
clients_.removeAll(socket);
socket->deleteLater();
}
void BluetoothService::readSocket()
{
GET_SOCKET(socket);
tryReadSocket(socket);
}
void BluetoothService::listeningSocketError(const QBluetoothServer::Error& error)
{
qWarning() << "Bluetooth service received an error: " << error;
serverFailed();
}
void BluetoothService::retryStart()
{
qDebug() << "retry to start service";
retry_timer_.stop();
if (!adapter_address_.isEmpty()) {
qDebug() << "Try to restart service on " << adapter_address_;
start(adapter_address_);
}
}
void BluetoothService::updateAdapterStatus()
{
if (server_ != nullptr) {
bool is_available = isAdapterAvailable(adapter_address_);
if (!is_available) {
qDebug() << "Adapter " << adapter_address_ << " is not available anymore";
serverFailed();
}
}
}
bool BluetoothService::isAdapterAvailable(const QString &address)
{
foreach (auto adapter, QBluetoothLocalDevice::allDevices()) {
if (address == adapter.address().toString()) {
return true;
}
}
return false;
}
void BluetoothService::tryReadSocket(QBluetoothSocket *socket)
{
while (socket->canReadLine()) {
QByteArray line = socket->readLine().trimmed();
if (!line.isEmpty()) {
emit codeReceived(QString::fromUtf8(line.constData(), line.length()));
break;
}
}
socket->close();
}
void BluetoothService::serverFailed()
{
emit bindFailed();
stop();
qDebug() << "Failed to start: retry in " << TRY_RESTART_SERVICE_MS << "ms";
retry_timer_.start(TRY_RESTART_SERVICE_MS);
}