-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.cpp
244 lines (192 loc) · 6.88 KB
/
controller.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
#include "controller.h"
#include "robotservice.h"
#include "utilities.h"
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothLocalDevice>
#include <QLoggingCategory>
#include <QLowEnergyController>
namespace EvoBot {
namespace {
Q_LOGGING_CATEGORY(lcController, "evobot.controller")
} // namespace
class Controller::Private
{
class StateGuard
{
public:
StateGuard(Private *d) : d{d} {}
~StateGuard() { d->checkState(); }
private:
Private *const d;
};
public:
explicit Private(Controller *q)
: q{q}
{
if (!m_localDevice.isValid()) {
raiseError(BluetoothMissingError, tr("No Bluetooth controller available"));
return;
}
connect(&m_localDevice, &QBluetoothLocalDevice::hostModeStateChanged,
q, [this](auto state) { this->onHostStateChanged(state); });
connect(&m_deviceDiscovery, &QBluetoothDeviceDiscoveryAgent::deviceDiscovered,
q, [this](const auto &device) { this->onDeviceDiscovered(device); });
connect(&m_deviceDiscovery, QOverload<QBluetoothDeviceDiscoveryAgent::Error>::of(&QBluetoothDeviceDiscoveryAgent::error),
q, [this](auto error) { this->onDeviceDiscoveryError(error); });
connect(&m_deviceDiscovery, &QBluetoothDeviceDiscoveryAgent::finished,
q, [this] { onDeviceDiscoveryFinished(); });
connect(&m_robotService, &RobotService::stateChanged, q, [this] { checkState(); });
onHostStateChanged(m_localDevice.hostMode());
}
Error error() const { return m_error; }
QString errorString() const { return m_errorString; }
State state() const
{
if (m_error != NoError)
return ErrorState;
if (m_robotService.state() == RobotService::ConnectedState)
return ConnectedState;
if (m_central)
return ServiceDiscoveryState;
if (m_deviceDiscovery.isActive())
return DeviceDiscoveryState;
return UninitializedState;
}
RobotService *robotService()
{
return &m_robotService;
}
private:
void checkState()
{
const auto newState = state();
if (m_oldState != newState) {
qCInfo(lcController, "state changed: %s => %s", key(m_oldState), key(newState));
emit q->stateChanged(newState, m_oldState);
m_oldState = newState;
}
}
void raiseError(Error error, const QString &errorString)
{
StateGuard stateGuard{this};
m_error = error;
m_errorString = errorString;
qCCritical(lcController, "%ls", qUtf16Printable(errorString));
emit q->errorOccured(m_error, m_errorString);
}
// QBluetoothLocalDevice
void onDeviceDiscovered(const QBluetoothDeviceInfo &device)
{
const auto last = end(m_knownDevices);
if (std::find(begin(m_knownDevices), last, device.address()) != last)
return;
m_knownDevices.emplace_back(device.address());
qCDebug(lcController, "Bluetooth device `%ls' (%ls) discovered",
qUtf16Printable(device.name()), qUtf16Printable(device.address().toString()));
if (device.name() == "Evolution-Robot" && !m_central) {
StateGuard stateGuard{this};
m_deviceDiscovery.stop();
m_central = QLowEnergyController::createCentral(device, q);
connect(m_central, &QLowEnergyController::connected, q, [this] { onDeviceConnected(); });
connect(m_central, QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error),
q, [this](auto error) { this->onDeviceError(error); });
connect(m_central, &QLowEnergyController::discoveryFinished,
q, [this] { onServiceDiscoveryFinished(); });
qCInfo(lcController, "Connecting to `%ls' (%ls)", qUtf16Printable(device.name()),
qUtf16Printable(device.address().toString()));
m_central->connectToDevice();
}
}
void onHostStateChanged(QBluetoothLocalDevice::HostMode state)
{
StateGuard stateGuard{this};
if (state == QBluetoothLocalDevice::HostPoweredOff) {
qCInfo(lcController, "Activating Bluetooth controller");
m_localDevice.powerOn();
} else {
m_deviceDiscovery.start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
}
}
// QBluetoothDeviceDiscoveryAgent
void onDeviceDiscoveryError(QBluetoothDeviceDiscoveryAgent::Error error)
{
raiseError(DeviceDiscoveryError, tr("Device discovery failed: %ls (%d)").
arg(m_deviceDiscovery.errorString()).arg(error));
}
void onDeviceDiscoveryFinished()
{
StateGuard stateGuard{this};
qCInfo(lcController, "Device discovery has finished");
}
// QLowEnergyController
void onDeviceConnected()
{
StateGuard stateGuard{this};
qCInfo(lcController, "Connected to %ls (%ls)", qUtf16Printable(m_central->remoteName()),
qUtf16Printable(m_central->remoteAddress().toString()));
m_central->discoverServices();
}
void onDeviceError(QLowEnergyController::Error error)
{
raiseError(DeviceError, tr("Device communication failed (%1 (%2))").
arg(m_central->errorString()).arg(error));
resetCentral();
}
void onServiceDiscoveryFinished()
{
StateGuard stateGuard{this};
qCInfo(lcController, "Service discovery has finished");
if (!m_robotService.attach(m_central)) {
qCWarning(lcController, "Could not find Evolution Robot service at `%ls' (%ls)",
qUtf16Printable(m_central->remoteName()), qUtf16Printable(m_central->remoteAddress().toString()));
resetCentral();
}
}
void resetCentral()
{
StateGuard stateGuard{this};
if (auto central = std::exchange(m_central, {})) {
central->disconnect(q);
central->deleteLater();
}
}
//
Controller *const q;
Error m_error = NoError;
QString m_errorString;
State m_oldState = UninitializedState;
QBluetoothLocalDevice m_localDevice;
QBluetoothDeviceDiscoveryAgent m_deviceDiscovery;
std::vector<QBluetoothAddress> m_knownDevices;
QLowEnergyController *m_central = {};
RobotService m_robotService;
};
Controller::Controller(QObject *parent)
: QObject{parent}
, d{new Private{this}}
{}
Controller::~Controller()
{
delete d;
}
Controller::Error Controller::error() const
{
return d->error();
}
QString Controller::errorString() const
{
return d->errorString();
}
Controller::State Controller::state() const
{
return d->state();
}
const char *Controller::stateName(Controller::State state)
{
return key(state);
}
RobotService *Controller::robotService() const
{
return d->robotService();
}
} // namespace EvoBot