aboutsummaryrefslogtreecommitdiff
path: root/src/HciAdapter.h
blob: a19d3953bde0ff4d2c0549c99591645165375651 (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2017 Paul Nettle. 
// 
// This file is part of Gobbledegook. 
// 
// Gobbledegook is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
// 
// Gobbledegook is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
// 
// You should have received a copy of the GNU General Public License 
// along with Gobbledegook.  If not, see <http://www.gnu.org/licenses/>. 
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
// 
// >> 
// >>>  INSIDE THIS FILE 
// >> 
// 
// Protocol-level code for the Bluetooth Management API, which is used to configure the Bluetooth adapter 
// 
// >> 
// >>>  DISCUSSION 
// >> 
// 
// This class is intended for use by `Mgmt` (see Mgmt.cpp). 
// 
// See the discussion at the top of HciAdapter.cpp 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
#pragma once
 
#include <stdint.h>
#include <vector>
#include <thread>
 
#include "HciSocket.h"
#include "Utils.h"
 
namespace ggk {
 
class HciAdapter
{
public:
 
// 
// Constants 
// 
 
// A constant referring to a 'non-controller' (for commands that do not require a controller index) 
static const uint16_t kNonController = 0xffff;
 
// Command code names 
static const int kMinCommandCode = 0x0001;
static const int kMaxCommandCode = 0x0043;
static const char * const kCommandCodeNames[kMaxCommandCode + 1];
 
// Event type names 
static const int kMinEventType = 0x0001;
static const int kMaxEventType = 0x0025;
static const char * const kEventTypeNames[kMaxEventType + 1];
 
static const int kMinStatusCode = 0x00;
static const int kMaxStatusCode = 0x14;
static const char * const kStatusCodes[kMaxStatusCode + 1];
 
// 
// Types 
// 
 
// HCI Controller Settings 
enum HciControllerSettings
{
EHciPowered = (1<<0),
EHciConnectable = (1<<1),
EHciFastConnectable = (1<<2),
EHciDiscoverable = (1<<3),
EHciBondable = (1<<4),
EHciLinkLevelSecurity = (1<<5),
EHciSecureSimplePairing = (1<<6),
EHciBasicRate_EnhancedDataRate = (1<<7),
EHciHighSpeed = (1<<8),
EHciLowEnergy = (1<<9),
EHciAdvertising = (1<<10),
EHciSecureConnections = (1<<11),
EHciDebugKeys = (1<<12),
EHciPrivacy = (1<<13),
EHciControllerConfiguration = (1<<14),
EHciStaticAddress = (1<<15)
};
 
struct HciHeader
{
uint16_t code;
uint16_t controllerId;
uint16_t dataSize;
 
void toNetwork()
{
code = Utils::endianToHci(code);
controllerId = Utils::endianToHci(controllerId);
dataSize = Utils::endianToHci(dataSize);
}
 
void toHost()
{
code = Utils::endianToHost(code);
controllerId = Utils::endianToHost(controllerId);
dataSize = Utils::endianToHost(dataSize);
}
 
std::string debugText()
{
std::string text = "";
text += "> Request header\n";
text += "  + Command code       : " + Utils::hex(code) + " (" + HciAdapter::kCommandCodeNames[code] + ")\n";
text += "  + Controller Id      : " + Utils::hex(controllerId) + "\n";
text += "  + Data size          : " + std::to_string(dataSize) + " bytes";
return text;
}
} __attribute__((packed));
 
struct CommandCompleteEvent
{
HciHeader header;
uint16_t commandCode;
uint8_t status;
 
void toNetwork()
{
header.toNetwork();
commandCode = Utils::endianToHci(commandCode);
}
 
void toHost()
{
header.toHost();
commandCode = Utils::endianToHost(commandCode);
}
 
std::string debugText()
{
std::string text = "";
text += "> Command complete event\n";
text += "  + Event code         : " + Utils::hex(header.code) + " (" + HciAdapter::kEventTypeNames[header.code] + ")\n";
text += "  + Controller Id      : " + Utils::hex(header.controllerId) + "\n";
text += "  + Data size          : " + std::to_string(header.dataSize) + " bytes\n";
text += "  + Command code       : " + Utils::hex(commandCode) + " (" + HciAdapter::kCommandCodeNames[commandCode] + ")\n";
text += "  + Status             : " + Utils::hex(status);
return text;
}
} __attribute__((packed));
 
struct CommandStatusEvent
{
HciHeader header;
uint16_t commandCode;
uint8_t status;
 
void toNetwork()
{
header.toNetwork();
commandCode = Utils::endianToHci(commandCode);
}
 
void toHost()
{
header.toHost();
commandCode = Utils::endianToHost(commandCode);
}
 
std::string debugText()
{
std::string text = "";
text += "> Command status event\n";
text += "  + Event code         : " + Utils::hex(header.code) + " (" + HciAdapter::kEventTypeNames[header.code] + ")\n";
text += "  + Controller Id      : " + Utils::hex(header.controllerId) + "\n";
text += "  + Data size          : " + std::to_string(header.dataSize) + " bytes\n";
text += "  + Command code       : " + Utils::hex(commandCode) + " (" + HciAdapter::kCommandCodeNames[commandCode] + ")\n";
text += "  + Status             : " + Utils::hex(status) + " (" + HciAdapter::kStatusCodes[status] + ")";
return text;
}
} __attribute__((packed));
 
struct DeviceConnectedEvent
{
HciHeader header;
uint8_t address[6];
uint8_t addressType;
uint32_t flags;
uint16_t eirDataLength;
 
void toNetwork()
{
header.toNetwork();
flags = Utils::endianToHci(flags);
eirDataLength = Utils::endianToHci(eirDataLength);
}
 
void toHost()
{
header.toHost();
flags = Utils::endianToHost(flags);
eirDataLength = Utils::endianToHost(eirDataLength);
}
 
std::string debugText()
{
std::string text = "";
text += "> DeviceConnected event\n";
text += "  + Event code         : " + Utils::hex(header.code) + " (" + HciAdapter::kEventTypeNames[header.code] + ")\n";
text += "  + Controller Id      : " + Utils::hex(header.controllerId) + "\n";
text += "  + Data size          : " + std::to_string(header.dataSize) + " bytes\n";
text += "  + Address            : " + Utils::bluetoothAddressString(address) + "\n";
text += "  + Address type       : " + Utils::hex(addressType) + "\n";
text += "  + Flags              : " + Utils::hex(flags) + "\n";
text += "  + EIR Data Length    : " + Utils::hex(eirDataLength);
if (eirDataLength > 0)
{
text += "\n";
text += "  + EIR Data           : " + Utils::hex(reinterpret_cast<uint8_t *>(&eirDataLength) + 2, eirDataLength);
}
return text;
}
} __attribute__((packed));
 
struct DeviceDisconnectedEvent
{
HciHeader header;
uint8_t address[6];
uint8_t addressType;
uint8_t reason;
 
void toNetwork()
{
header.toNetwork();
}
 
void toHost()
{
header.toHost();
}
 
std::string debugText()
{
std::string text = "";
text += "> DeviceDisconnected event\n";
text += "  + Event code         : " + Utils::hex(header.code) + " (" + HciAdapter::kEventTypeNames[header.code] + ")\n";
text += "  + Controller Id      : " + Utils::hex(header.controllerId) + "\n";
text += "  + Data size          : " + std::to_string(header.dataSize) + " bytes\n";
text += "  + Address            : " + Utils::bluetoothAddressString(address) + "\n";
text += "  + Address type       : " + Utils::hex(addressType) + "\n";
text += "  + Reason             : " + Utils::hex(reason);
return text;
}
} __attribute__((packed));
 
struct AdapterSettings
{
uint32_t masks;
 
void toHost()
{
masks = Utils::endianToHost(masks);
}
 
bool isSet(HciControllerSettings mask)
{
return (masks & mask) != 0;
}
 
// Returns a human-readable string of flags and settings 
std::string toString()
{
std::string text = "";
if (isSet(EHciPowered)) { text += "Powered, "}
if (isSet(EHciConnectable)) { text += "Connectable, "}
if (isSet(EHciFastConnectable)) { text += "FC, "}
if (isSet(EHciDiscoverable)) { text += "Discov, "}
if (isSet(EHciBondable)) { text += "Bondable, "}
if (isSet(EHciLinkLevelSecurity)) { text += "LLS, "}
if (isSet(EHciSecureSimplePairing)) { text += "SSP, "}
if (isSet(EHciBasicRate_EnhancedDataRate)) { text += "BR/EDR, "}
if (isSet(EHciHighSpeed)) { text += "HS, "}
if (isSet(EHciLowEnergy)) { text += "LE, "}
if (isSet(EHciAdvertising)) { text += "Adv, "}
if (isSet(EHciSecureConnections)) { text += "SC, "}
if (isSet(EHciDebugKeys)) { text += "DebugKeys, "}
if (isSet(EHciPrivacy)) { text += "Privacy, "}
if (isSet(EHciControllerConfiguration)) { text += "ControllerConfig, "}
if (isSet(EHciStaticAddress)) { text += "StaticAddr, "}
 
if (text.length() != 0)
{
text = text.substr(0, text.length() - 2);
}
 
return text;
}
 
std::string debugText()
{
std::string text = "";
text += "> Adapter settings\n";
text += "  + " + toString();
return text;
}
} __attribute__((packed));
 
// The comments documenting these fields are very high level. There is a lot of detailed information not present, for example 
// some values are not available at all times. This is fully documented in: 
// 
//     https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/doc/mgmt-api.txt 
struct ControllerInformation
{
uint8_t address[6];         // The Bluetooth address 
uint8_t bluetoothVersion;   // Bluetooth version 
uint16_t manufacturer;      // The manufacturer 
AdapterSettings supportedSettings; // Bits for various supported settings (see HciControllerSettings) 
AdapterSettings currentSettings;   // Bits for various currently configured settings (see HciControllerSettings) 
uint8_t classOfDevice[3];   // Um, yeah. That. 
char name[249];             // Null terminated name 
char shortName[11];         // Null terminated short name 
 
void toHost()
{
manufacturer = Utils::endianToHost(manufacturer);
supportedSettings.toHost();
currentSettings.toHost();
}
 
std::string debugText()
{
std::string text = "";
text += "> Controller information\n";
text += "  + Current settings   : " + Utils::hex(currentSettings.masks) + "\n";
text += "  + Address            : " + Utils::bluetoothAddressString(address) + "\n";
text += "  + BT Version         : " + std::to_string(static_cast<int>(bluetoothVersion)) + "\n";
text += "  + Manufacturer       : " + Utils::hex(manufacturer) + "\n";
text += "  + Supported settings : " + supportedSettings.toString() + "\n";
text += "  + Current settings   : " + currentSettings.toString() + "\n";
text += "  + Name               : " + std::string(name) + "\n";
text += "  + Short name         : " + std::string(shortName);
return text;
}
} __attribute__((packed));
 
struct VersionInformation
{
uint8_t version;
uint16_t revision;
 
void toHost()
{
revision = Utils::endianToHost(revision);
}
 
std::string debugText()
{
std::string text = "";
text += "> Version information\n";
text += "  + Version  : " + std::to_string(static_cast<int>(version)) + "\n";
text += "  + Revision : " + std::to_string(revision);
return text;
}
} __attribute__((packed));
 
struct LocalName
{
char name[249];
char shortName[11];
 
std::string debugText()
{
std::string text = "";
text += "> Local name information\n";
text += "  + Name       : '" + std::string(name) + "\n";
text += "  + Short name : '" + std::string(shortName);
return text;
}
} __attribute__((packed));
 
// 
// Accessors 
// 
 
// Returns the instance to this singleton class 
static HciAdapter &getInstance()
{
static HciAdapter instance;
return instance;
}
 
AdapterSettings getAdapterSettings();
ControllerInformation getControllerInformation();
VersionInformation getVersionInformation();
LocalName getLocalName();
 
int getActiveConnectionCount() { return activeConnections; }
 
// 
// Disallow copies of our singleton (c++11) 
// 
 
HciAdapter(HciAdapter const&) = delete;
void operator=(HciAdapter const&) = delete;
 
// Reads current values from the controller 
// 
// This effectively requests data from the controller but that data may not be available instantly, but within a few 
// milliseconds. Therefore, it is not recommended attempt to retrieve the results from their accessors immediately. 
void sync(uint16_t controllerIndex);
 
// Connects the HCI socket if a connection does not already exist 
// 
// If a connection already exists, this method will do nothing and return true. 
// 
// Note that it shouldn't be necessary to connect manually; any action requiring a connection will automatically connect 
// 
// Returns true if the HCI socket is connected (either via a new connection or an existing one), otherwise false 
bool connect();
 
// Returns true if connected to the HCI socket, otherwise false 
// 
// Note that it shouldn't be necessary to connect manually; any action requiring a connection will automatically connect 
bool isConnected() const;
 
// Disconnects from the HCI Socket 
// 
// If the connection is not connected, this method will do nothing. 
// 
// It isn't necessary to disconnect manually; the HCI socket will get disocnnected automatically upon destruction 
void disconnect();
 
// Sends a command over the HCI socket 
// 
// If the HCI socket is not connected, it will auto-connect prior to sending the command. In the case of a failed auto-connect, 
// a failure is returned. 
// 
// Returns true on success, otherwise false 
bool sendCommand(HciHeader &request);
 
// Event processor, responsible for receiving events from the HCI socket 
// 
// This mehtod should not be called directly. Rather, it runs continuously on a thread until the server shuts down 
void runEventThread();
 
private:
// Private constructor for our Singleton 
HciAdapter() : activeConnections(0{}
 
// Our HCI Socket, which allows us to talk directly to the kernel 
HciSocket hciSocket;
 
// Our event thread listens for events coming from the adapter and deals with them appropriately 
static std::thread eventThread;
 
// Our adapter information 
AdapterSettings adapterSettings;
ControllerInformation controllerInformation;
VersionInformation versionInformation;
LocalName localName;
 
// Our active connection count 
int activeConnections;
};
 
}// namespace ggk