aboutsummaryrefslogtreecommitdiff
path: root/src/Mgmt.cpp
blob: e61653ebbec25d76f8cd991e668daf9ef80a629e (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
// 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)
{
HciAdapter::getInstance().sync(controllerIndex);
}
 
// 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::HciHeader
{
char name[249];
char shortName[11];
} __attribute__((packed));
 
SRequest request;
request.code = Mgmt::ESetLocalNameCommand;
request.controllerId = controllerIndex;
request.dataSize = sizeof(SRequest) - sizeof(HciAdapter::HciHeader);
 
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());
 
if (!HciAdapter::getInstance().sendCommand(request))
{
Logger::warn(SSTR << "  + Failed to set name");
return false;
}
 
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(uint16_t commandCode, uint16_t controllerId, uint8_t newState)
{
struct SRequest : HciAdapter::HciHeader
{
uint8_t state;
} __attribute__((packed));
 
SRequest request;
request.code = commandCode;
request.controllerId = controllerId;
request.dataSize = sizeof(SRequest) - sizeof(HciAdapter::HciHeader);
request.state = newState;
 
if (!HciAdapter::getInstance().sendCommand(request))
{
Logger::warn(SSTR << "  + Failed to set " << HciAdapter::kCommandCodeNames[commandCode] << " state to: " << static_cast<int>(newState));
return false;
}
 
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(Mgmt::ESetPoweredCommand, 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(Mgmt::ESetBREDRCommand, 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(Mgmt::ESetSecureConnectionsCommand, 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(Mgmt::ESetBondableCommand, 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(Mgmt::ESetConnectableCommand, 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(Mgmt::ESetLowEnergyCommand, 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(Mgmt::ESetAdvertisingCommand, controllerIndex, newState);
}
 
// --------------------------------------------------------------------------------------------------------------------------------- 
// Utilitarian 
// --------------------------------------------------------------------------------------------------------------------------------- 
 
// 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