Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
Go to the documentation of this file.
26 #ifndef BT_ADAPTER_HPP_
27 #define BT_ADAPTER_HPP_
117 (void)changedEnabled;
136 virtual bool deviceFound(std::shared_ptr<BTDevice> device,
const uint64_t timestamp) {
160 virtual void deviceConnected(std::shared_ptr<BTDevice> device,
const uint16_t handle,
const uint64_t timestamp) {
191 virtual void deviceReady(std::shared_ptr<BTDevice> device,
const uint64_t timestamp) {
221 {
return this == &rhs; }
224 {
return !(*
this == rhs); }
254 const bool debug_event, debug_lock;
258 bool hci_uses_ext_scan;
259 bool hci_uses_ext_conn;
278 std::atomic<AdapterSetting> old_settings;
281 std::atomic<ScanType> currentMetaScanType;
282 std::atomic<bool> keep_le_scan_alive;
285 const BTDevice* single_conn_device_ptr =
nullptr;
286 std::mutex mtx_single_conn_device;
287 std::condition_variable cv_single_conn_device;
298 mutable std::mutex mtx_discoveredDevices;
299 mutable std::mutex mtx_connectedDevices;
300 mutable std::mutex mtx_discovery;
301 mutable std::mutex mtx_sharedDevices;
304 bool updateDataFromHCI() noexcept;
305 bool validateDevInfo() noexcept;
312 class ctor_cookie {
friend BTAdapter; ctor_cookie(
const uint16_t secret) { (void)secret; } };
315 static std::shared_ptr<BTAdapter> make_shared(
BTManager& mgmt_,
const AdapterInfo& adapterInfo_) {
316 return std::make_shared<BTAdapter>(BTAdapter::ctor_cookie(0), mgmt_, adapterInfo_);
325 void poweredOff() noexcept;
335 uint16_t min_interval, uint16_t max_interval,
336 uint16_t latency, uint16_t supervision_timeout) noexcept;
337 friend
HCIStatusCode BTDevice::connectBREDR(const uint16_t pkt_type, const uint16_t clock_offset, const uint8_t role_switch) noexcept;
341 friend
void BTDevice::processDeviceReady(std::shared_ptr<
BTDevice> sthis, const uint64_t timestamp);
345 bool unlockConnect(const
BTDevice & device) noexcept;
346 bool unlockConnectAny() noexcept;
348 bool addConnectedDevice(const std::shared_ptr<
BTDevice> & device) noexcept;
349 bool removeConnectedDevice(const
BTDevice & device) noexcept;
353 bool addDiscoveredDevice(std::shared_ptr<
BTDevice> const &device) noexcept;
355 void removeDevice(
BTDevice & device) noexcept;
357 bool addSharedDevice(std::shared_ptr<
BTDevice> const &device) noexcept;
358 std::shared_ptr<
BTDevice> getSharedDevice(const
BTDevice & device) noexcept;
359 void removeSharedDevice(const
BTDevice & device) noexcept;
361 bool mgmtEvNewSettingsMgmt(const
MgmtEvent& e) noexcept;
362 bool mgmtEvDeviceDiscoveringMgmt(const
MgmtEvent& e) noexcept;
363 bool mgmtEvLocalNameChangedMgmt(const
MgmtEvent& e) noexcept;
364 bool mgmtEvDeviceFoundHCI(const
MgmtEvent& e) noexcept;
365 bool mgmtEvDeviceDisconnectedMgmt(const
MgmtEvent& e) noexcept;
366 bool mgmtEvPairDeviceCompleteMgmt(const
MgmtEvent& e) noexcept;
367 bool mgmtEvNewLongTermKeyMgmt(const
MgmtEvent& e) noexcept;
369 bool mgmtEvDeviceDiscoveringHCI(const
MgmtEvent& e) noexcept;
370 bool mgmtEvDeviceConnectedHCI(const
MgmtEvent& e) noexcept;
371 bool mgmtEvConnectFailedHCI(const
MgmtEvent& e) noexcept;
372 bool mgmtEvHCIEncryptionChangedHCI(const
MgmtEvent& e) noexcept;
373 bool mgmtEvHCIEncryptionKeyRefreshCompleteHCI(const
MgmtEvent& e) noexcept;
374 bool mgmtEvHCILERemoteUserFeaturesHCI(const
MgmtEvent& e) noexcept;
375 bool mgmtEvDeviceDisconnectedHCI(const
MgmtEvent& e) noexcept;
378 bool mgmtEvDeviceDiscoveringAny(const
MgmtEvent& e, const
bool hciSourced) noexcept;
380 bool mgmtEvPinCodeRequestMgmt(const
MgmtEvent& e) noexcept;
381 bool mgmtEvUserConfirmRequestMgmt(const
MgmtEvent& e) noexcept;
382 bool mgmtEvUserPasskeyRequestMgmt(const
MgmtEvent& e) noexcept;
383 bool mgmtEvAuthFailedMgmt(const
MgmtEvent& e) noexcept;
384 bool mgmtEvDeviceUnpairedMgmt(const
MgmtEvent& e) noexcept;
389 void sendDeviceReady(std::shared_ptr<
BTDevice> device, uint64_t timestamp) noexcept;
391 void startDiscoveryBackground() noexcept;
392 void checkDiscoveryState() noexcept;
395 const uint64_t timestampMS) noexcept;
398 void sendDeviceUpdated(std::
string cause, std::shared_ptr<
BTDevice> device, uint64_t timestamp,
EIRDataType updateMask) noexcept;
422 void close() noexcept;
537 std::shared_ptr<NameAndShortName>
setLocalName(
const std::string &name,
const std::string &short_name) noexcept;
602 const uint16_t conn_interval_min=12,
const uint16_t conn_interval_max=12,
739 return currentMetaScanType;
785 std::string
toString(
bool includeDiscoveredDevices)
const noexcept;
BDAddressAndType const & getAddressAndType() const noexcept
Returns the adapter's public BDAddressAndType.
@ UNSET
Denoting unset value, i.e.
HCIStatusCode stopDiscovery() noexcept
Closes the discovery session.
AdapterSetting
Adapter Setting Bits.
bool isDeviceWhitelisted(const BDAddressAndType &addressAndType) noexcept
Returns true, if the adapter's device is already whitelisted.
HCIHandler & getHCI() noexcept
Returns a reference to the aggregated HCIHandler instance.
virtual bool matchDevice(const BTDevice &device)
Custom filter for all 'device*' notification methods, which will not be called if this method returns...
constexpr bool hasHCIExtScan() const noexcept
Returns true if HCI_LE_Set_Extended_Scan_Parameters and HCI_LE_Set_Extended_Scan_Enable is supported ...
virtual ~AdapterStatusListener()
int removeAllStatusListener()
Remove all status listener from the list.
static const uint16_t le_scan_interval
EIRDataType
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
HCIStatusCode reset() noexcept
Reset the adapter.
BTMode getBTMode() const noexcept
Returns the current BTMode of this adapter.
BDAddressAndType const & getVisibleAddressAndType() const noexcept
Returns the adapter's currently visible BDAddressAndType.
constexpr LE_Features getLEFeatures() const noexcept
Return LE_Features for this controller.
virtual void devicePairingState(std::shared_ptr< BTDevice > device, const SMPPairingState state, const PairingMode mode, const uint64_t timestamp)
An already connected BTDevice's SMPPairingState has changed.
bool isSuspended() const noexcept
Returns whether the adapter is suspended, i.e.
const NameAndShortName & getLocalName() const noexcept
Returns the local friendly name and short_name.
@ NONE
Zero mode, neither DUAL, BREDR nor LE.
BTAdapter status listener for BTDevice discovery events: Added, updated and removed; as well as for c...
int removeDiscoveredDevices() noexcept
Discards all discovered devices.
HCIWhitelistConnectType
HCI Whitelist connection type.
std::string getShortName() const noexcept
bool setBondable(bool value) noexcept
Set the bondable (aka pairable) state of the adapter.
bool isValid() const noexcept
Returns whether the object's reference is valid and in a general operational state.
bool setPowered(bool value) noexcept
Set the power state of the adapter.
bool getDiscovering() const noexcept
Returns the meta discovering state.
void printDeviceLists() noexcept
Print the internally maintained BTDevice lists to stderr:
BT Core Spec v5.2: Vol 4, Part E HCI: 5.4.2 HCI ACL Data packets.
void close() noexcept
Closes this instance, usually being called by destructor or when this adapter is being removed as rec...
jau::darray< std::shared_ptr< BTDevice > > getDiscoveredDevices() const noexcept
Returns discovered devices from the last discovery.
ScanType getCurrentScanType() const noexcept
Representing a complete [Primary] Service Declaration including its list of Characteristic Declaratio...
ScanType
Meta ScanType as derived from BTMode, with defined value mask consisting of BDAddressType bits.
ScanType getCurrentNativeScanType() const noexcept
Returns the adapter's current native discovering ScanType.
bool removeDiscoveredDevice(const BDAddressAndType &addressAndType) noexcept
Discards matching discovered devices.
A thread safe singleton handler of the HCI control channel to one controller (BT adapter)
mgmt_addr_info { EUI48, uint8_t type }, int8_t rssi, int8_t tx_power, int8_t max_tx_power;
const uint16_t dev_id
Adapter's internal temporary device id.
constexpr bool hasHCIExtConn() const noexcept
Returns true if HCI_LE_Extended_Create_Connection is supported (Bluetooth 5.0).
virtual bool operator==(const AdapterStatusListener &rhs) const
Default comparison operator, merely testing for same memory reference.
bool isPowered() const noexcept
Returns whether the adapter is valid, plugged in and powered.
uint16_t opcode, uint16_t dev-id, uint16_t param_size
std::string getShortName() const noexcept
Returns the short system name.
HCIStatusCode
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
bool removeStatusListener(std::shared_ptr< AdapterStatusListener > l)
Remove the given listener from the list.
std::shared_ptr< BTDevice > findSharedDevice(const EUI48 &address, const BDAddressType addressType) noexcept
Returns shared BTDevice if found, otherwise nullptr.
Implementation of a dynamic linear array storage, aka vector.
std::weak_ptr< BTDevice > wbr_device
The optional weak device reference.
void printStatusListenerList() noexcept
LE_Features
HCI Supported Commands.
static const uint16_t le_scan_window
virtual void deviceUpdated(std::shared_ptr< BTDevice > device, const EIRDataType updateMask, const uint64_t timestamp)
An already discovered BTDevice has been updated.
std::string toString() const noexcept override
Handles the Security Manager Protocol (SMP) using Protocol Data Unit (PDU) encoded messages over L2CA...
std::string getName() const noexcept
std::shared_ptr< BTDevice > findDiscoveredDevice(const EUI48 &address, const BDAddressType addressType) noexcept
Returns shared BTDevice if found, otherwise nullptr.
PairingMode
Bluetooth secure pairing mode.
A thread safe singleton handler of the Linux Kernel's BlueZ manager control channel.
std::string get_java_class() const noexcept override
bool addStatusListener(std::shared_ptr< AdapterStatusListener > l)
Add the given listener to the list if not already present.
std::shared_ptr< AdapterStatusListener > listener
The actual listener.
static bool le_scan_active
virtual void adapterSettingsChanged(BTAdapter &adapter, const AdapterSetting oldmask, const AdapterSetting newmask, const AdapterSetting changedmask, const uint64_t timestamp)
BTAdapter setting(s) changed.
BTAdapter(const BTAdapter::ctor_cookie &cc, BTManager &mgmt_, const AdapterInfo &adapterInfo_) noexcept
Private ctor for private BTAdapter::make_shared() intended for friends.
bool hasSecureConnections() const noexcept
bool isOpen() const noexcept
Returns true if this mgmt instance is open, connected and hence valid, otherwise false.
constexpr int32_t getHCIConnSupervisorTimeout(const uint16_t conn_latency, const uint16_t conn_interval_max_ms, const uint16_t min_result_ms=number(HCIConstInt::LE_CONN_MIN_TIMEOUT_MS), const uint16_t multiplier=10) noexcept
Defining the supervising timeout for LE connections to be a multiple of the maximum connection interv...
std::shared_ptr< NameAndShortName > setLocalName(const std::string &name, const std::string &short_name) noexcept
Sets the local friendly name.
bool hasSecureSimplePairing() const noexcept
BTAdapter represents one Bluetooth Controller.
virtual std::string toString() const =0
BDAddressType
BT Core Spec v5.2: Vol 3, Part C Generic Access Profile (GAP): 15.1.1.1 Public Bluetooth address.
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
BTManager & getManager() const noexcept
Returns a reference to the used singleton BTManager instance, used to create this adapter.
bool isCurrentSettingBitSet(const AdapterSetting bit) const noexcept
SMPIOCapability
Vol 3, Part H, 2.3.2 IO capabilities.
bool isValid() const noexcept
Returns whether the adapter is valid, i.e.
bool setDiscoverable(bool value) noexcept
Set the discoverable state of the adapter.
virtual void deviceConnected(std::shared_ptr< BTDevice > device, const uint16_t handle, const uint64_t timestamp)
BTDevice got connected.
virtual void deviceReady(std::shared_ptr< BTDevice > device, const uint64_t timestamp)
BTDevice is ready for user (GATT) processing, i.e.
const BDAddressAndType addressAndType
The adapter's address initially reported by the system is always its public address,...
bool addDeviceToWhitelist(const BDAddressAndType &addressAndType, const HCIWhitelistConnectType ctype, const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12, const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 15))
Add the given device to the adapter's autoconnect whitelist.
virtual void discoveringChanged(BTAdapter &adapter, const ScanType currentMeta, const ScanType changedType, const bool changedEnabled, const bool keepAlive, const uint64_t timestamp)
BTAdapter's discovery state has changed, i.e.
BTMode getCurrentBTMode() const noexcept
Map getCurrentSettingMask() to BTMode.
static std::string java_class() noexcept
static const uint8_t filter_policy
virtual void deviceDisconnected(std::shared_ptr< BTDevice > device, const HCIStatusCode reason, const uint16_t handle, const uint64_t timestamp)
BTDevice got disconnected.
bool operator!=(const AdapterStatusListener &rhs) const
HCIStatusCode startDiscovery(const bool keepAlive=true, const bool le_scan_active=false, const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24, const uint8_t filter_policy=0x00)
Starts a new discovery session.
ScanType getCurrentScanType() const noexcept
Returns the current meta discovering ScanType.
SMPPairingState
SMP Pairing Process state definition.
Unique Bluetooth EUI48 address and BDAddressType tuple.
virtual bool deviceFound(std::shared_ptr< BTDevice > device, const uint64_t timestamp)
A BTDevice has been newly discovered.
std::string getName() const noexcept
Returns the system name.
BTMode
Bluetooth adapter operating mode.
bool removeDeviceFromWhitelist(const BDAddressAndType &addressAndType)
Remove the given device from the adapter's autoconnect whitelist.