Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
BTTypes1.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#ifndef BT_TYPES1_HPP_
27#define BT_TYPES1_HPP_
28
29#include <mutex>
30#include <atomic>
31
32#include <jau/java_uplink.hpp>
33#include <jau/basic_types.hpp>
34#include <jau/uuid.hpp>
35
36#include "BTAddress.hpp"
37#include "BTTypes0.hpp"
38
39namespace direct_bt {
40
41 class BTAdapter; // forward
42 class BTDevice; // forward
43
44 /** \addtogroup DBTUserAPI
45 *
46 * @{
47 */
48
50 {
51 protected:
52 std::atomic_bool instance_valid;
53
54 BTObject() noexcept : instance_valid(true) {}
55
56 public:
57 std::string toString() const noexcept override { return "BTObject["+jau::to_hexstring(this)+"]"; }
58
59 ~BTObject() noexcept override {
60 instance_valid = false;
61 }
62
63 /**
64 * Returns whether the object's reference is valid and in a general operational state.
65 */
66 inline bool isValidInstance() const noexcept { return instance_valid.load(); }
67
68 void checkValidInstance() const override {
69 if( !isValidInstance() ) {
70 throw jau::IllegalStateException("BTObject::checkValidInstance: Invalid object: "+jau::to_hexstring(this), E_FILE_LINE);
71 }
72 }
73 };
74 inline std::string to_string(const BTObject& o) noexcept { return o.toString(); }
75
76 /**
77 * mgmt_addr_info { EUI48, uint8_t type },
78 * int8_t rssi,
79 * int8_t tx_power,
80 * int8_t max_tx_power;
81 */
83 {
84 private:
85 jau::EUI48 address;
86 BDAddressType addressType;
87 int8_t rssi;
88 int8_t tx_power;
89 int8_t max_tx_power;
90
91 public:
92 static jau::nsize_t minimumDataSize() noexcept { return 6 + 1 + 1 + 1 + 1; }
93
94 ConnectionInfo(const jau::EUI48 &address_, BDAddressType addressType_, int8_t rssi_, int8_t tx_power_, int8_t max_tx_power_) noexcept
95 : address(address_), addressType(addressType_), rssi(rssi_), tx_power(tx_power_), max_tx_power(max_tx_power_) {}
96
97 const jau::EUI48 getAddress() const noexcept { return address; }
98 BDAddressType getAddressType() const noexcept { return addressType; }
99 int8_t getRSSI() const noexcept { return rssi; }
100 int8_t getTxPower() const noexcept { return tx_power; }
101 int8_t getMaxTxPower() const noexcept { return max_tx_power; }
102
103 std::string toString() const noexcept {
104 return "address="+getAddress().toString()+", addressType "+to_string(getAddressType())+
105 ", rssi "+std::to_string(rssi)+
106 ", tx_power[set "+std::to_string(tx_power)+", max "+std::to_string(tx_power)+"]";
107 }
108 };
109
111 {
112 friend class BTManager; // top manager
113 friend class BTAdapter; // direct manager
114
115 private:
116 std::string name;
117 std::string short_name;
118
119 protected:
120 void setName(std::string v) noexcept { name = std::move(v); }
121 void setShortName(std::string v) noexcept { short_name = std::move(v); }
122
123 public:
125 : name(), short_name() {}
126
127 NameAndShortName(std::string name_, std::string short_name_) noexcept
128 : name(std::move(name_)), short_name(std::move(short_name_)) {}
129
130 std::string getName() const noexcept { return name; }
131 std::string getShortName() const noexcept { return short_name; }
132
133 std::string toString() const noexcept {
134 return "name '"+getName()+"', shortName '"+getShortName()+"'";
135 }
136 };
137
138 /**
139 * Adapter Setting Bits.
140 * <p>
141 * Used to denote specific bits or as a bit-mask.
142 * </p>
143 */
144 enum class AdapterSetting : uint32_t {
145 NONE = 0,
146 POWERED = 0x00000001,
147 CONNECTABLE = 0x00000002,
148 FAST_CONNECTABLE = 0x00000004,
149 DISCOVERABLE = 0x00000008,
150 BONDABLE = 0x00000010,
151 LINK_SECURITY = 0x00000020,
152 SSP = 0x00000040,
153 BREDR = 0x00000080,
154 HS = 0x00000100,
155 LE = 0x00000200,
156 ADVERTISING = 0x00000400,
157 SECURE_CONN = 0x00000800,
158 DEBUG_KEYS = 0x00001000,
159 PRIVACY = 0x00002000,
160 CONFIGURATION = 0x00004000,
161 STATIC_ADDRESS = 0x00008000,
162 PHY_CONFIGURATION = 0x00010000
163 };
164 constexpr AdapterSetting operator ~(const AdapterSetting rhs) noexcept {
165 return static_cast<AdapterSetting> ( ~static_cast<uint32_t>(rhs) );
166 }
167 constexpr AdapterSetting operator ^(const AdapterSetting lhs, const AdapterSetting rhs) noexcept {
168 return static_cast<AdapterSetting> ( static_cast<uint32_t>(lhs) ^ static_cast<uint32_t>(rhs) );
169 }
170 constexpr AdapterSetting operator |(const AdapterSetting lhs, const AdapterSetting rhs) noexcept {
171 return static_cast<AdapterSetting> ( static_cast<uint32_t>(lhs) | static_cast<uint32_t>(rhs) );
172 }
173 constexpr AdapterSetting operator &(const AdapterSetting lhs, const AdapterSetting rhs) noexcept {
174 return static_cast<AdapterSetting> ( static_cast<uint32_t>(lhs) & static_cast<uint32_t>(rhs) );
175 }
176 constexpr bool operator ==(const AdapterSetting lhs, const AdapterSetting rhs) noexcept {
177 return static_cast<uint32_t>(lhs) == static_cast<uint32_t>(rhs);
178 }
179 constexpr bool operator !=(const AdapterSetting lhs, const AdapterSetting rhs) noexcept {
180 return !( lhs == rhs );
181 }
182
183 constexpr AdapterSetting getAdapterSettingMaskDiff(const AdapterSetting setting_a, const AdapterSetting setting_b) noexcept { return setting_a ^ setting_b; }
184
185 constexpr bool isAdapterSettingBitSet(const AdapterSetting mask, const AdapterSetting bit) noexcept { return AdapterSetting::NONE != ( mask & bit ); }
186
187 constexpr void setAdapterSettingMaskBit(AdapterSetting &mask, const AdapterSetting bit) noexcept { mask = mask | bit; }
188 constexpr void clrAdapterSettingMaskBit(AdapterSetting &mask, const AdapterSetting bit) noexcept { mask = mask & ~bit; }
189
190 std::string to_string(const AdapterSetting settingBitMask) noexcept;
191
192 /** Maps the given {@link AdapterSetting} to {@link BTMode} */
193 BTMode getAdapterSettingsBTMode(const AdapterSetting settingMask) noexcept;
194
196 {
197 public:
198 const uint16_t dev_id;
199 /**
200 * The adapter's address initially reported by the system is always its public address, i.e. BDAddressType::BDADDR_LE_PUBLIC.
201 * <p>
202 * Subsequent adapter setup using BDAddressType::BDADDR_LE_RANDOM must be handled within BTAdapter
203 * and is not reflected in AdapterInfo.
204 * </p>
205 */
207 const uint8_t version;
208 const uint16_t manufacturer;
209
210 private:
211 AdapterSetting supported_setting;
212 std::atomic<AdapterSetting> current_setting;
213 uint32_t dev_class;
214 std::string name;
215 std::string short_name;
216
217 public:
218 AdapterInfo(const uint16_t dev_id_, BDAddressAndType addressAndType_,
219 const uint8_t version_, const uint16_t manufacturer_,
220 const AdapterSetting supported_setting_, const AdapterSetting current_setting_,
221 const uint32_t dev_class_, std::string name_, std::string short_name_) noexcept
222 : dev_id(dev_id_), addressAndType( std::move(addressAndType_) ), version(version_),
223 manufacturer(manufacturer_),
224 supported_setting(supported_setting_),
225 current_setting(current_setting_), dev_class(dev_class_),
226 name( std::move(name_) ), short_name( std::move(short_name_) )
227 { }
228
229 AdapterInfo(const AdapterInfo &o) noexcept
230 : dev_id(o.dev_id), addressAndType(o.addressAndType), version(o.version),
231 manufacturer(o.manufacturer),
232 supported_setting(o.supported_setting),
233 current_setting(o.current_setting.load()), dev_class(o.dev_class),
234 name(o.name), short_name(o.short_name)
235 { }
237 if( this != &o ) {
238 if( dev_id != o.dev_id || addressAndType != o.addressAndType ) {
239 throw jau::IllegalArgumentException("Can't assign different device id's or address "+o.toString()+" -> "+toString(), E_FILE_LINE);
240 }
241 supported_setting = o.supported_setting;
242 current_setting = o.current_setting.load();
243 dev_class = o.dev_class;
244 name = o.name;
245 short_name = o.short_name;
246 }
247 return *this;
248 }
250 : dev_id(o.dev_id), addressAndType(o.addressAndType), version(o.version),
251 manufacturer(o.manufacturer),
252 supported_setting(o.supported_setting),
253 current_setting(o.current_setting.load()), dev_class(o.dev_class),
254 name(std::move(o.name)), short_name(std::move(o.short_name))
255 { }
256 AdapterInfo& operator=(AdapterInfo &&o) noexcept = delete;
257
258 /**
259 * Assigns the given 'new_setting & supported_setting' to the current_setting.
260 * @param new_setting assigned to current_setting after masking with supported_setting.
261 * @return 'new_setting & supported_setting', i.e. the new current_setting.
262 */
264 const AdapterSetting _current_setting = new_setting & supported_setting;
265 current_setting = _current_setting;
266 return _current_setting;
267 }
268 void setSettingMasks(const AdapterSetting supported_setting_, const AdapterSetting current_setting_) noexcept {
269 supported_setting = supported_setting_;
270 current_setting = current_setting_;
271 }
272 void setDevClass(const uint32_t v) noexcept { dev_class = v; }
273 void setName(std::string v) noexcept { name = std::move(v); }
274 void setShortName(std::string v) noexcept { short_name = std::move(v); }
275
276 constexpr const AdapterSetting& get_supportedSetting() const noexcept { return supported_setting; }
277
278 bool isSettingMaskSupported(const AdapterSetting setting) const noexcept {
279 return setting == ( setting & supported_setting );
280 }
281 AdapterSetting getCurrentSettingMask() const noexcept { return current_setting; }
282 bool isCurrentSettingBitSet(const AdapterSetting bit) const noexcept { return AdapterSetting::NONE != ( current_setting & bit ); }
283
284 /** Map {@link #getCurrentSettingMask()} to {@link BTMode} */
285 BTMode getCurrentBTMode() const noexcept { return getAdapterSettingsBTMode(current_setting); }
286
287 uint32_t getDevClass() const noexcept { return dev_class; }
288 std::string getName() const noexcept { return name; }
289 std::string getShortName() const noexcept { return short_name; }
290
291 std::string toString() const noexcept {
292 return "AdapterInfo[id "+std::to_string(dev_id)+", address "+addressAndType.toString()+", version "+std::to_string(version)+
293 ", manuf "+std::to_string(manufacturer)+
294 ", settings[sup "+to_string(supported_setting)+", cur "+to_string(current_setting)+
295 "], name '"+name+"', shortName '"+short_name+"']";
296 }
297 };
298 inline std::string to_string(const AdapterInfo& a) noexcept { return a.toString(); }
299
300 /**@}*/
301
302} // namespace direct_bt
303
304#endif /* BT_TYPES1_HPP_ */
#define E_FILE_LINE
std::string toString() const noexcept
Definition: BTTypes1.hpp:291
AdapterInfo & operator=(const AdapterInfo &o)
Definition: BTTypes1.hpp:236
AdapterSetting setCurrentSettingMask(const AdapterSetting new_setting) noexcept
Assigns the given 'new_setting & supported_setting' to the current_setting.
Definition: BTTypes1.hpp:263
void setSettingMasks(const AdapterSetting supported_setting_, const AdapterSetting current_setting_) noexcept
Definition: BTTypes1.hpp:268
const uint8_t version
Definition: BTTypes1.hpp:207
std::string getName() const noexcept
Definition: BTTypes1.hpp:288
BTMode getCurrentBTMode() const noexcept
Map getCurrentSettingMask() to BTMode.
Definition: BTTypes1.hpp:285
std::string getShortName() const noexcept
Definition: BTTypes1.hpp:289
const BDAddressAndType addressAndType
The adapter's address initially reported by the system is always its public address,...
Definition: BTTypes1.hpp:206
AdapterInfo & operator=(AdapterInfo &&o) noexcept=delete
AdapterInfo(AdapterInfo &&o) noexcept
Definition: BTTypes1.hpp:249
void setName(std::string v) noexcept
Definition: BTTypes1.hpp:273
constexpr const AdapterSetting & get_supportedSetting() const noexcept
Definition: BTTypes1.hpp:276
AdapterInfo(const AdapterInfo &o) noexcept
Definition: BTTypes1.hpp:229
const uint16_t manufacturer
Definition: BTTypes1.hpp:208
bool isSettingMaskSupported(const AdapterSetting setting) const noexcept
Definition: BTTypes1.hpp:278
bool isCurrentSettingBitSet(const AdapterSetting bit) const noexcept
Definition: BTTypes1.hpp:282
void setShortName(std::string v) noexcept
Definition: BTTypes1.hpp:274
AdapterSetting getCurrentSettingMask() const noexcept
Definition: BTTypes1.hpp:281
void setDevClass(const uint32_t v) noexcept
Definition: BTTypes1.hpp:272
const uint16_t dev_id
Definition: BTTypes1.hpp:198
uint32_t getDevClass() const noexcept
Definition: BTTypes1.hpp:287
AdapterInfo(const uint16_t dev_id_, BDAddressAndType addressAndType_, const uint8_t version_, const uint16_t manufacturer_, const AdapterSetting supported_setting_, const AdapterSetting current_setting_, const uint32_t dev_class_, std::string name_, std::string short_name_) noexcept
Definition: BTTypes1.hpp:218
Unique Bluetooth EUI48 address and BDAddressType tuple.
Definition: BTAddress.hpp:175
std::string toString() const noexcept
Definition: BTTypes0.cpp:186
BTAdapter represents one local Bluetooth Controller.
Definition: BTAdapter.hpp:324
A thread safe singleton handler of the BTAdapter manager, e.g.
Definition: BTManager.hpp:204
~BTObject() noexcept override
Definition: BTTypes1.hpp:59
std::atomic_bool instance_valid
Definition: BTTypes1.hpp:52
bool isValidInstance() const noexcept
Returns whether the object's reference is valid and in a general operational state.
Definition: BTTypes1.hpp:66
BTObject() noexcept
Definition: BTTypes1.hpp:54
std::string toString() const noexcept override
Definition: BTTypes1.hpp:57
void checkValidInstance() const override
Throws an IllegalStateException if instance is not valid.
Definition: BTTypes1.hpp:68
mgmt_addr_info { EUI48, uint8_t type }, int8_t rssi, int8_t tx_power, int8_t max_tx_power;
Definition: BTTypes1.hpp:83
int8_t getMaxTxPower() const noexcept
Definition: BTTypes1.hpp:101
int8_t getTxPower() const noexcept
Definition: BTTypes1.hpp:100
BDAddressType getAddressType() const noexcept
Definition: BTTypes1.hpp:98
ConnectionInfo(const jau::EUI48 &address_, BDAddressType addressType_, int8_t rssi_, int8_t tx_power_, int8_t max_tx_power_) noexcept
Definition: BTTypes1.hpp:94
static jau::nsize_t minimumDataSize() noexcept
Definition: BTTypes1.hpp:92
std::string toString() const noexcept
Definition: BTTypes1.hpp:103
int8_t getRSSI() const noexcept
Definition: BTTypes1.hpp:99
const jau::EUI48 getAddress() const noexcept
Definition: BTTypes1.hpp:97
NameAndShortName(std::string name_, std::string short_name_) noexcept
Definition: BTTypes1.hpp:127
std::string getName() const noexcept
Definition: BTTypes1.hpp:130
void setName(std::string v) noexcept
Definition: BTTypes1.hpp:120
void setShortName(std::string v) noexcept
Definition: BTTypes1.hpp:121
std::string toString() const noexcept
Definition: BTTypes1.hpp:133
std::string getShortName() const noexcept
Definition: BTTypes1.hpp:131
constexpr LE_Features operator^(const LE_Features lhs, const LE_Features rhs) noexcept
Definition: BTTypes0.hpp:204
bool operator==(const BTAdapter &lhs, const BTAdapter &rhs) noexcept
Definition: BTAdapter.hpp:1348
BTMode getAdapterSettingsBTMode(const AdapterSetting settingMask) noexcept
Maps the given AdapterSetting to BTMode.
Definition: BTTypes1.cpp:88
BTMode
Bluetooth adapter operating mode.
Definition: BTTypes0.hpp:112
std::string to_string(const DiscoveryPolicy v) noexcept
Definition: BTAdapter.cpp:58
constexpr void setAdapterSettingMaskBit(AdapterSetting &mask, const AdapterSetting bit) noexcept
Definition: BTTypes1.hpp:187
constexpr AdapterSetting getAdapterSettingMaskDiff(const AdapterSetting setting_a, const AdapterSetting setting_b) noexcept
Definition: BTTypes1.hpp:183
AdapterSetting
Adapter Setting Bits.
Definition: BTTypes1.hpp:144
constexpr bool isAdapterSettingBitSet(const AdapterSetting mask, const AdapterSetting bit) noexcept
Definition: BTTypes1.hpp:185
BDAddressType
BT Core Spec v5.2: Vol 3, Part C Generic Access Profile (GAP): 15.1.1.1 Public Bluetooth address.
Definition: BTAddress.hpp:60
constexpr ScanType operator~(const ScanType val) noexcept
Definition: BTTypes0.hpp:359
std::string to_string(const AdapterInfo &a) noexcept
Definition: BTTypes1.hpp:298
bool operator!=(const BTAdapter &lhs, const BTAdapter &rhs) noexcept
Definition: BTAdapter.hpp:1351
constexpr void clrAdapterSettingMaskBit(AdapterSetting &mask, const AdapterSetting bit) noexcept
Definition: BTTypes1.hpp:188
@ LE
LE only Bluetooth mode.
@ BREDR
BREDR only Bluetooth mode.
constexpr BTGattChar::PropertyBitVal operator&(const BTGattChar::PropertyBitVal lhs, const BTGattChar::PropertyBitVal rhs) noexcept
Definition: BTGattChar.hpp:424
constexpr BTGattChar::PropertyBitVal operator|(const BTGattChar::PropertyBitVal lhs, const BTGattChar::PropertyBitVal rhs) noexcept
Definition: BTGattChar.hpp:421
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
Definition: int_types.hpp:53
std::string to_hexstring(value_type const &v) noexcept
Produce a lower-case hexadecimal string representation of the given pointer.
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
Definition: eui48.hpp:324
std::string toString() const noexcept
Definition: eui48.cpp:167