aboutsummaryrefslogtreecommitdiff
path: root/src/Mgmt.cpp
blob: 34537f0404a7c6c8354ae25397d5c642fcec47a4 (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
// 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 
// >> 
// 
// This file contains various functions for interacting with Bluetooth Management interface, which provides adapter configuration. 
// 
// >> 
// >>>  DISCUSSION 
// >> 
// 
// We only cover the basics here. If there are configuration features you need that aren't supported (such as configuring BR/EDR), 
// then this would be a good place for them. 
// 
// Note that this class relies on the `HciAdapter`, which is a very primitive implementation. Use with caution. 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
#include <string.h>
 
#include "Mgmt.h"
#include "Logger.h"
#include "Utils.h"
 
namespace ggk {
 
// Construct the Mgmt device 
// 
// Set `controllerIndex` to the zero-based index of the device as recognized by the OS. If this parameter is omitted, the index 
// of the first device (0) will be used. 
Mgmt::Mgmt(uint16_t controllerIndex)
: controllerIndex(controllerIndex)
{
}
 
// Returns the version information: 
// 
//    bits 0-15 = revision 
//    bits 16-23 = version 
// 
//    ... or -1 on error 
int Mgmt::getVersion()
{
struct SResponse : HciAdapter::ResponseEvent
{
uint8_t version;
uint16_t revision;
 
void toHost()
{
revision = Utils::endianToHost(revision);
}
} __attribute__((packed));
 
HciAdapter::Header request;
request.code = 1;
request.controllerId = kNonController;
request.dataSize = 0;
 
SResponse response;
if (!hciAdapter.sendCommand(request, response, sizeof(response)))
{
Logger::warn(SSTR << "  + Failed to get version information");
return -1;
}
 
response.toHost();
 
Logger::debug(SSTR << "  + Version response has version=" << Utils::hex(response.version) << " and revision=" << Utils::hex(response.revision));
return (response.version << 16) | response.revision;
}
 
// Returns information about the controller (address, name, current settings, etc.) 
// 
// See the definition for MgmtControllerInformation for details. 
// 
//    ... or nullptr on error 
Mgmt::ControllerInformation *Mgmt::getControllerInformation()
{
struct SResponse : HciAdapter::ResponseEvent
{
ControllerInformation info;
 
void toHost()
{
info.toHost();
}
} __attribute__((packed));
 
HciAdapter::Header request;
request.code = 4// Controller Information Command 
request.controllerId = controllerIndex;
request.dataSize = 0;
 
Logger::debug("Dumping device information after configuration...");
 
SResponse response;
if (!hciAdapter.sendCommand(request, response, sizeof(response)))
{
Logger::warn(SSTR << "  + Failed to get current settings");
return nullptr;
}
 
response.toHost();
 
// Copy it to our local 
controllerInfo = response.info;
 
Logger::debug("  + Controller information");
Logger::debug(SSTR << "    + Current settings   : " << Utils::hex(controllerInfo.currentSettings));
Logger::debug(SSTR << "    + Address            : " << Utils::bluetoothAddressString(controllerInfo.address));
Logger::debug(SSTR << "    + BT Version         : " << controllerInfo.bluetoothVersion);
Logger::debug(SSTR << "    + Manufacturer       : " << Utils::hex(controllerInfo.manufacturer));
Logger::debug(SSTR << "    + Supported settings : " << controllerSettingsString(controllerInfo.supportedSettings));
Logger::debug(SSTR << "    + Current settings   : " << controllerSettingsString(controllerInfo.currentSettings));
Logger::debug(SSTR << "    + Name               : " << controllerInfo.name);
Logger::debug(SSTR << "    + Short name         : " << controllerInfo.shortName);
 
return &controllerInfo;
}
 
// Set the adapter name and short name 
// 
// The inputs `name` and `shortName` may be truncated prior to setting them on the adapter. To ensure that `name` and 
// `shortName` conform to length specifications prior to calling this method, see the constants `kMaxAdvertisingNameLength` and 
// `kMaxAdvertisingShortNameLength`. In addition, the static methods `truncateName()` and `truncateShortName()` may be helpful. 
// 
// Returns true on success, otherwise false 
bool Mgmt::setName(std::string name, std::string shortName)
{
// Ensure their lengths are okay 
name = truncateName(name);
shortName = truncateShortName(shortName);
 
struct SRequest : HciAdapter::Header
{
char name[249];
char shortName[11];
} __attribute__((packed));
 
struct SResponse : HciAdapter::ResponseEvent
{
char name[249];
char shortName[11];
} __attribute__((packed));
 
SRequest request;
request.code = 0x000F;
request.controllerId = controllerIndex;
request.dataSize = sizeof(SRequest) - sizeof(HciAdapter::Header);
 
memset(request.name0sizeof(request.name));
snprintf(request.namesizeof(request.name), "%s", name.c_str());
 
memset(request.shortName0sizeof(request.shortName));
snprintf(request.shortNamesizeof(request.shortName), "%s", shortName.c_str());
 
SResponse response;
if (!hciAdapter.sendCommand(request, response, sizeof(response)))
{
Logger::warn(SSTR << "  + Failed to set name");
return false;
}
 
Logger::info(SSTR << "  + Name set to '" << request.name << "', short name set to '" << request.shortName << "'");
return true;
}
 
// Set a setting state to 'newState' 
// 
// Many settings are set the same way, this is just a convenience routine to handle them all 
// 
// Returns true on success, otherwise false 
bool Mgmt::setState(const char *pSettingName, uint16_t commandCode, uint16_t controllerId, uint8_t newState)
{
struct SRequest : HciAdapter::Header
{
uint8_t state;
} __attribute__((packed));
 
struct SResponse : HciAdapter::ResponseEvent
{
uint32_t currentSettings;
 
void toHost()
{
currentSettings = Utils::endianToHost(currentSettings);
}
} __attribute__((packed));
 
SRequest request;
request.code = commandCode;
request.controllerId = controllerId;
request.dataSize = sizeof(SRequest) - sizeof(HciAdapter::Header);
request.state = newState;
 
SResponse response;
if (!hciAdapter.sendCommand(request, response, sizeof(response)))
{
Logger::warn(SSTR << "  + Failed to set " << pSettingName << " state to: " << static_cast<int>(newState));
return false;
}
 
response.toHost();
 
Logger::debug(SSTR << "  + " << pSettingName << " set to " << static_cast<int>(newState) << "" << controllerSettingsString(response.currentSettings));
return true;
}
 
// Set the powered state to `newState` (true = powered on, false = powered off) 
// 
// Returns true on success, otherwise false 
bool Mgmt::setPowered(bool newState)
{
return setState("Powered"0x0005, controllerIndex, newState ? 1 : 0);
}
 
// Set the BR/EDR state to `newState` (true = enabled, false = disabled) 
// 
// Returns true on success, otherwise false 
bool Mgmt::setBredr(bool newState)
{
return setState("BR/EDR"0x002A, controllerIndex, newState ? 1 : 0);
}
 
// Set the Secure Connection state (0 = disabled, 1 = enabled, 2 = secure connections only mode) 
// 
// Returns true on success, otherwise false 
bool Mgmt::setSecureConnections(uint8_t newState)
{
return setState("SecureConnections"0x002D, controllerIndex, newState);
}
 
// Set the bondable state to `newState` (true = enabled, false = disabled) 
// 
// Returns true on success, otherwise false 
bool Mgmt::setBondable(bool newState)
{
return setState("SecureConnections"0x0009, controllerIndex, newState ? 1 : 0);
}
 
// Set the connectable state to `newState` (true = enabled, false = disabled) 
// 
// Returns true on success, otherwise false 
bool Mgmt::setConnectable(bool newState)
{
return setState("Connectable"0x0007, controllerIndex, newState ? 1 : 0);
}
 
// Set the LE state to `newState` (true = enabled, false = disabled) 
// 
// Returns true on success, otherwise false 
bool Mgmt::setLE(bool newState)
{
return setState("LowEnergy"0x000D, controllerIndex, newState ? 1 : 0);
}
 
// Set the advertising state to `newState` (0 = disabled, 1 = enabled (with consideration towards the connectable setting), 
// 2 = enabled in connectable mode). 
// 
// Returns true on success, otherwise false 
bool Mgmt::setAdvertising(uint8_t newState)
{
return setState("Advertising"0x0029, controllerIndex, newState);
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
// Utilitarian 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// Transforms a "Current_Settings" value (4 octets as defined by the Bluetooth Management API specification) into a human-readable 
// string of flags and settings. 
std::string Mgmt::controllerSettingsString(uint32_t bits)
{
std::string result = "";
 
if ((bits & EHciPowered) != 0{ result += "Powered, "}
if ((bits & EHciConnectable) != 0{ result += "Connectable, "}
if ((bits & EHciFastConnectable) != 0{ result += "FC, "}
if ((bits & EHciDiscoverable) != 0{ result += "Discov, "}
if ((bits & EHciBondable) != 0{ result += "Bondable, "}
if ((bits & EHciLinkLevelSecurity) != 0{ result += "LLS, "}
if ((bits & EHciSecureSimplePairing) != 0{ result += "SSP, "}
if ((bits & EHciBasicRate_EnhancedDataRate) != 0{ result += "BR/EDR, "}
if ((bits & EHciHighSpeed) != 0{ result += "HS, "}
if ((bits & EHciLowEnergy) != 0{ result += "LE, "}
if ((bits & EHciAdvertising) != 0{ result += "Adv, "}
if ((bits & EHciSecureConnections) != 0{ result += "SC, "}
if ((bits & EHciDebugKeys) != 0{ result += "DebugKeys, "}
if ((bits & EHciPrivacy) != 0{ result += "Privacy, "}
if ((bits & EHciControllerConfiguration) != 0{ result += "ControllerConfig, "}
if ((bits & EHciStaticAddress) != 0{ result += "StaticAddr, "}
 
if (result.length() != 0)
{
result = result.substr(0, result.length() - 2);
}
 
return result;
}
 
// Truncates the string `name` to the maximum allowed length for an adapter name. If `name` needs no truncation, a copy of 
// `name` is returned. 
std::string Mgmt::truncateName(const std::string &name)
{
if (name.length() <= kMaxAdvertisingNameLength)
{
return name;
}
 
return name.substr(0kMaxAdvertisingNameLength);
}
 
// Truncates the string `name` to the maximum allowed length for an adapter short-name. If `name` needs no truncation, a copy 
// of `name` is returned. 
std::string Mgmt::truncateShortName(const std::string &name)
{
if (name.length() <= kMaxAdvertisingShortNameLength)
{
return name;
}
 
return name.substr(0kMaxAdvertisingShortNameLength);
}
 
}// namespace ggk