Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
direct_bt::BTManager Class Reference

A thread safe singleton handler of the Linux Kernel's BlueZ manager control channel. More...

#include <BTManager.hpp>

Inheritance diagram for direct_bt::BTManager:
Collaboration diagram for direct_bt::BTManager:

Public Types

enum  Defaults : int32_t { ClientMaxMTU = 512 }
 

Public Member Functions

 ~BTManager () noexcept
 
void close () noexcept
 
std::string get_java_class () const noexcept override
 
BTMode getDefaultBTMode () noexcept
 Returns the default BTMode, adapters are tried to be initialized. More...
 
bool isOpen () const noexcept
 Returns true if this mgmt instance is open and hence valid, otherwise false. More...
 
std::string toString () const noexcept override
 
int getAdapterCount () const noexcept
 retrieve information gathered at startup More...
 
jau::darray< std::shared_ptr< BTAdapter > > getAdapters ()
 Returns a list of currently added DBTAdapter. More...
 
std::shared_ptr< BTAdaptergetAdapter (const uint16_t dev_id) const noexcept
 Returns the DBTAdapter with the given dev_id, or nullptr if not found. More...
 
std::shared_ptr< BTAdaptergetDefaultAdapter () const noexcept
 Returns the default AdapterInfo. More...
 
bool setIOCapability (const uint16_t dev_id, const SMPIOCapability io_cap, SMPIOCapability &pre_io_cap) noexcept
 
SMPIOCapability getIOCapability (const uint16_t dev_id) const noexcept
 
bool setMode (const uint16_t dev_id, const MgmtCommand::Opcode opc, const uint8_t mode, AdapterSetting &current_settings) noexcept
 
MgmtStatus setDiscoverable (const uint16_t dev_id, const uint8_t state, const uint16_t timeout, AdapterSetting &current_settings) noexcept
 
ScanType startDiscovery (const uint16_t dev_id, const BTMode btMode) noexcept
 Start discovery on given adapter dev_id with a ScanType matching the given BTMode. More...
 
ScanType startDiscovery (const uint16_t dev_id, const ScanType type) noexcept
 Start discovery on given adapter dev_id with given ScanType. More...
 
bool stopDiscovery (const uint16_t dev_id, const ScanType type) noexcept
 Stop discovery on given adapter dev_id. More...
 
bool uploadConnParam (const uint16_t dev_id, const BDAddressAndType &addressAndType, 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)) noexcept
 Uploads given connection parameter for given device to the kernel. More...
 
bool isDeviceWhitelisted (const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
 Returns true, if the adapter's device is already whitelisted. More...
 
bool addDeviceToWhitelist (const uint16_t dev_id, const BDAddressAndType &addressAndType, const HCIWhitelistConnectType ctype) noexcept
 Add the given device to the adapter's autoconnect whitelist. More...
 
bool removeDeviceFromWhitelist (const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
 Remove the given device from the adapter's autoconnect whitelist. More...
 
int removeAllDevicesFromWhitelist () noexcept
 Remove all previously added devices from the autoconnect whitelist. More...
 
bool disconnect (const bool ioErrorCause, const uint16_t dev_id, const BDAddressAndType &addressAndType, const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION) noexcept
 
std::shared_ptr< ConnectionInfogetConnectionInfo (const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
 
std::shared_ptr< NameAndShortNamesetLocalName (const uint16_t dev_id, const std::string &name, const std::string &short_name) noexcept
 
MgmtStatus uploadLinkKey (const uint16_t dev_id, const bool debug_keys, const MgmtLinkKeyInfo &key) noexcept
 Security commands. More...
 
HCIStatusCode uploadLongTermKey (const uint16_t dev_id, const MgmtLongTermKeyInfo &key) noexcept
 
HCIStatusCode uploadLongTermKeyInfo (const uint16_t dev_id, const BDAddressAndType &addressAndType, const SMPLongTermKeyInfo &ltk) noexcept
 
MgmtStatus userPasskeyReply (const uint16_t dev_id, const BDAddressAndType &addressAndType, const uint32_t passkey) noexcept
 
MgmtStatus userPasskeyNegativeReply (const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
 
MgmtStatus userConfirmReply (const uint16_t dev_id, const BDAddressAndType &addressAndType, const bool positive) noexcept
 
bool pairDevice (const uint16_t dev_id, const BDAddressAndType &addressAndType, const SMPIOCapability iocap) noexcept
 
MgmtStatus unpairDevice (const uint16_t dev_id, const BDAddressAndType &addressAndType, const bool disconnect) noexcept
 
bool addMgmtEventCallback (const int dev_id, const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
 MgmtEventCallback handling
More...
 
int removeMgmtEventCallback (const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
 Returns count of removed given MgmtEventCallback from the named MgmtEvent::Opcode list. More...
 
int removeMgmtEventCallback (const int dev_id) noexcept
 Returns count of removed MgmtEventCallback from the named MgmtEvent::Opcode list matching the given adapter dev_id . More...
 
void clearMgmtEventCallbacks (const MgmtEvent::Opcode opc) noexcept
 Removes all MgmtEventCallbacks from the to the named MgmtEvent::Opcode list. More...
 
void clearAllCallbacks () noexcept
 Removes all MgmtEventCallbacks from all MgmtEvent::Opcode lists. More...
 
void sendMgmtEvent (const MgmtEvent &event) noexcept
 Manually send a MgmtEvent to all of its listeners. More...
 
void addChangedAdapterSetCallback (const ChangedAdapterSetCallback &l)
 ChangedAdapterSetCallback handling. More...
 
int removeChangedAdapterSetCallback (const ChangedAdapterSetCallback &l)
 Remove the given ChangedAdapterSetCallback from this manager. More...
 
void addChangedAdapterSetCallback (ChangedAdapterSetFunc f)
 Adds the given ChangedAdapterSetFunc to this manager. More...
 
int removeChangedAdapterSetCallback (ChangedAdapterSetFunc f)
 Remove the given ChangedAdapterSetFunc from this manager. More...
 

Static Public Member Functions

static BTManagerget (const BTMode defaultBTMode=BTMode::NONE)
 Retrieves the singleton instance. More...
 
static std::string java_class () noexcept
 

Static Public Attributes

static const pid_t pidSelf = getpid()
 

Detailed Description

A thread safe singleton handler of the Linux Kernel's BlueZ manager control channel.

Implementation utilizes a lock free ringbuffer receiving data within its separate thread.

Controlling Environment variables, see MgmtEnv.

Examples
dbt_scanner00.cpp, dbt_scanner01.cpp, and dbt_scanner10.cpp.

Definition at line 201 of file BTManager.hpp.

Member Enumeration Documentation

◆ Defaults

Enumerator
ClientMaxMTU 

Definition at line 203 of file BTManager.hpp.

Constructor & Destructor Documentation

◆ ~BTManager()

direct_bt::BTManager::~BTManager ( )
inlinenoexcept

Definition at line 334 of file BTManager.hpp.

Here is the call graph for this function:

Member Function Documentation

◆ addChangedAdapterSetCallback() [1/2]

void BTManager::addChangedAdapterSetCallback ( ChangedAdapterSetFunc  f)

Adds the given ChangedAdapterSetFunc to this manager.

When a new callback is added, all available adapter's will be reported as added, this allows a fully event driven workflow.

The callback is performed on a dedicated thread, allowing the user to perform complex operations.

Definition at line 1242 of file BTManager.cpp.

Here is the call graph for this function:

◆ addChangedAdapterSetCallback() [2/2]

void BTManager::addChangedAdapterSetCallback ( const ChangedAdapterSetCallback l)

ChangedAdapterSetCallback handling.

Adds the given ChangedAdapterSetCallback to this manager.

When a new callback is added, all available adapter's will be reported as added, this allows a fully event driven workflow.

The callback is performed on a dedicated thread, allowing the user to perform complex operations.

Examples
dbt_scanner10.cpp.

Definition at line 1230 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addDeviceToWhitelist()

bool BTManager::addDeviceToWhitelist ( const uint16_t  dev_id,
const BDAddressAndType addressAndType,
const HCIWhitelistConnectType  ctype 
)
noexcept

Add the given device to the adapter's autoconnect whitelist.

Make sure uploadConnParam(..) is invoked first, otherwise performance will lack.

Method will reject duplicate devices, in which case it should be removed first.

Definition at line 973 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ addMgmtEventCallback()

bool BTManager::addMgmtEventCallback ( const int  dev_id,
const MgmtEvent::Opcode  opc,
const MgmtEventCallback cb 
)
noexcept

MgmtEventCallback handling

Appends the given MgmtEventCallback for the given adapter dev_id to the named MgmtEvent::Opcode list, if it is not present already (dev_id + opcode + callback).

The adapter dev_id allows filtering the events only directed to the given adapter. Use dev_id -1 to receive the event for all adapter.

Parameters
dev_idthe associated adapter dev_id
opcopcode index for callback list, the callback shall be added to
cbthe to be added callback
Returns
true if newly added or already existing, false if given MgmtEvent::Opcode is out of supported range.

Definition at line 1117 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clearAllCallbacks()

void BTManager::clearAllCallbacks ( )
noexcept

Removes all MgmtEventCallbacks from all MgmtEvent::Opcode lists.

Definition at line 1155 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clearMgmtEventCallbacks()

void BTManager::clearMgmtEventCallbacks ( const MgmtEvent::Opcode  opc)
noexcept

Removes all MgmtEventCallbacks from the to the named MgmtEvent::Opcode list.

Definition at line 1148 of file BTManager.cpp.

Here is the call graph for this function:

◆ close()

void BTManager::close ( )
noexcept
Examples
dbt_scanner10.cpp.

Definition at line 538 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ disconnect()

bool BTManager::disconnect ( const bool  ioErrorCause,
const uint16_t  dev_id,
const BDAddressAndType addressAndType,
const HCIStatusCode  reason = HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION 
)
noexcept

Definition at line 1045 of file BTManager.cpp.

Here is the call graph for this function:

◆ get()

static BTManager& direct_bt::BTManager::get ( const BTMode  defaultBTMode = BTMode::NONE)
inlinestatic

Retrieves the singleton instance.

First call will open and initialize the bluetooth kernel.

Parameters
btModedefault BTMode when initializing new adapter. If BTMode::NONE given (default), MgmtEnv::DEFAULT_BTMODE is being used.
Returns
singleton instance.
Examples
dbt_scanner00.cpp, dbt_scanner01.cpp, and dbt_scanner10.cpp.

Definition at line 320 of file BTManager.hpp.

Here is the caller graph for this function:

◆ get_java_class()

std::string direct_bt::BTManager::get_java_class ( ) const
inlineoverridevirtualnoexcept

Implements jau::JavaUplink.

Definition at line 338 of file BTManager.hpp.

Here is the call graph for this function:

◆ getAdapter()

std::shared_ptr< BTAdapter > BTManager::getAdapter ( const uint16_t  dev_id) const
noexcept

Returns the DBTAdapter with the given dev_id, or nullptr if not found.

Examples
dbt_scanner00.cpp, and dbt_scanner01.cpp.

Definition at line 624 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAdapterCount()

int direct_bt::BTManager::getAdapterCount ( ) const
inlinenoexcept

retrieve information gathered at startup

Returns AdapterInfo count in list

Definition at line 362 of file BTManager.hpp.

◆ getAdapters()

jau::darray<std::shared_ptr<BTAdapter> > direct_bt::BTManager::getAdapters ( )
inline

Returns a list of currently added DBTAdapter.

Examples
dbt_scanner10.cpp.

Definition at line 367 of file BTManager.hpp.

Here is the caller graph for this function:

◆ getConnectionInfo()

std::shared_ptr< ConnectionInfo > BTManager::getConnectionInfo ( const uint16_t  dev_id,
const BDAddressAndType addressAndType 
)
noexcept

Definition at line 1072 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getDefaultAdapter()

std::shared_ptr< BTAdapter > BTManager::getDefaultAdapter ( ) const
noexcept

Returns the default AdapterInfo.

The default adapter is either the first AdapterSetting::POWERED adapter, or function returns nullptr if none is AdapterSetting::POWERED.

Definition at line 614 of file BTManager.cpp.

Here is the call graph for this function:

◆ getDefaultBTMode()

BTMode direct_bt::BTManager::getDefaultBTMode ( )
inlinenoexcept

Returns the default BTMode, adapters are tried to be initialized.

Definition at line 346 of file BTManager.hpp.

◆ getIOCapability()

SMPIOCapability BTManager::getIOCapability ( const uint16_t  dev_id) const
noexcept

Definition at line 714 of file BTManager.cpp.

Here is the call graph for this function:

◆ isDeviceWhitelisted()

bool BTManager::isDeviceWhitelisted ( const uint16_t  dev_id,
const BDAddressAndType addressAndType 
)
noexcept

Returns true, if the adapter's device is already whitelisted.

Definition at line 962 of file BTManager.cpp.

Here is the caller graph for this function:

◆ isOpen()

bool direct_bt::BTManager::isOpen ( ) const
inlinenoexcept

Returns true if this mgmt instance is open and hence valid, otherwise false.

Definition at line 349 of file BTManager.hpp.

Here is the call graph for this function:

◆ java_class()

static std::string direct_bt::BTManager::java_class ( )
inlinestaticnoexcept

Definition at line 341 of file BTManager.hpp.

Here is the caller graph for this function:

◆ pairDevice()

bool BTManager::pairDevice ( const uint16_t  dev_id,
const BDAddressAndType addressAndType,
const SMPIOCapability  iocap 
)
noexcept

Definition at line 937 of file BTManager.cpp.

Here is the caller graph for this function:

◆ removeAllDevicesFromWhitelist()

int BTManager::removeAllDevicesFromWhitelist ( )
noexcept

Remove all previously added devices from the autoconnect whitelist.

Returns number of removed devices.

Definition at line 992 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeChangedAdapterSetCallback() [1/2]

int BTManager::removeChangedAdapterSetCallback ( ChangedAdapterSetFunc  f)

Remove the given ChangedAdapterSetFunc from this manager.

Parameters
lthe to be removed element
Returns
the number of removed elements

Definition at line 1248 of file BTManager.cpp.

Here is the call graph for this function:

◆ removeChangedAdapterSetCallback() [2/2]

int BTManager::removeChangedAdapterSetCallback ( const ChangedAdapterSetCallback l)

Remove the given ChangedAdapterSetCallback from this manager.

Parameters
lthe to be removed element
Returns
the number of removed elements
Examples
dbt_scanner10.cpp.

Definition at line 1238 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeDeviceFromWhitelist()

bool BTManager::removeDeviceFromWhitelist ( const uint16_t  dev_id,
const BDAddressAndType addressAndType 
)
noexcept

Remove the given device from the adapter's autoconnect whitelist.

Definition at line 1019 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeMgmtEventCallback() [1/2]

int BTManager::removeMgmtEventCallback ( const int  dev_id)
noexcept

Returns count of removed MgmtEventCallback from the named MgmtEvent::Opcode list matching the given adapter dev_id .

Definition at line 1135 of file BTManager.cpp.

Here is the call graph for this function:

◆ removeMgmtEventCallback() [2/2]

int BTManager::removeMgmtEventCallback ( const MgmtEvent::Opcode  opc,
const MgmtEventCallback cb 
)
noexcept

Returns count of removed given MgmtEventCallback from the named MgmtEvent::Opcode list.

Definition at line 1126 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sendMgmtEvent()

void BTManager::sendMgmtEvent ( const MgmtEvent event)
noexcept

Manually send a MgmtEvent to all of its listeners.

Definition at line 154 of file BTManager.cpp.

Here is the call graph for this function:

◆ setDiscoverable()

MgmtStatus BTManager::setDiscoverable ( const uint16_t  dev_id,
const uint8_t  state,
const uint16_t  timeout,
AdapterSetting current_settings 
)
noexcept

Definition at line 750 of file BTManager.cpp.

Here is the call graph for this function:

◆ setIOCapability()

bool BTManager::setIOCapability ( const uint16_t  dev_id,
const SMPIOCapability  io_cap,
SMPIOCapability pre_io_cap 
)
noexcept

Definition at line 691 of file BTManager.cpp.

Here is the call graph for this function:

◆ setLocalName()

std::shared_ptr< NameAndShortName > BTManager::setLocalName ( const uint16_t  dev_id,
const std::string &  name,
const std::string &  short_name 
)
noexcept

Definition at line 1085 of file BTManager.cpp.

Here is the call graph for this function:

◆ setMode()

bool BTManager::setMode ( const uint16_t  dev_id,
const MgmtCommand::Opcode  opc,
const uint8_t  mode,
AdapterSetting current_settings 
)
noexcept

Definition at line 724 of file BTManager.cpp.

Here is the call graph for this function:

◆ startDiscovery() [1/2]

ScanType BTManager::startDiscovery ( const uint16_t  dev_id,
const BTMode  btMode 
)
noexcept

Start discovery on given adapter dev_id with a ScanType matching the given BTMode.

Returns set ScanType.

Definition at line 775 of file BTManager.cpp.

Here is the call graph for this function:

◆ startDiscovery() [2/2]

ScanType BTManager::startDiscovery ( const uint16_t  dev_id,
const ScanType  type 
)
noexcept

Start discovery on given adapter dev_id with given ScanType.

Returns set ScanType.

Definition at line 779 of file BTManager.cpp.

Here is the call graph for this function:

◆ stopDiscovery()

bool BTManager::stopDiscovery ( const uint16_t  dev_id,
const ScanType  type 
)
noexcept

Stop discovery on given adapter dev_id.

Definition at line 796 of file BTManager.cpp.

Here is the call graph for this function:

◆ toString()

std::string direct_bt::BTManager::toString ( ) const
inlineoverridevirtualnoexcept

Reimplemented from jau::JavaUplink.

Definition at line 353 of file BTManager.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unpairDevice()

MgmtStatus BTManager::unpairDevice ( const uint16_t  dev_id,
const BDAddressAndType addressAndType,
const bool  disconnect 
)
noexcept

Definition at line 946 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uploadConnParam()

bool BTManager::uploadConnParam ( const uint16_t  dev_id,
const BDAddressAndType addressAndType,
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) 
)
noexcept

Uploads given connection parameter for given device to the kernel.

Parameters
dev_id
address
address_type
conn_interval_minin units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
conn_interval_maxin units of 1.25ms, default value 12 for 15ms; Value range [6 .. 3200] for [7.5ms .. 4000ms]
conn_latencyslave latency in units of connection events, default value 0; Value range [0 .. 0x01F3].
supervision_timeoutin units of 10ms, default value >= 10 x conn_interval_max, we use HCIConstInt::LE_CONN_MIN_TIMEOUT_MS minimum; Value range [0xA-0x0C80] for [100ms - 32s].
Returns

Definition at line 806 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ uploadLinkKey()

MgmtStatus BTManager::uploadLinkKey ( const uint16_t  dev_id,
const bool  debug_keys,
const MgmtLinkKeyInfo key 
)
noexcept

Security commands.

Definition at line 819 of file BTManager.cpp.

Here is the call graph for this function:

◆ uploadLongTermKey()

HCIStatusCode BTManager::uploadLongTermKey ( const uint16_t  dev_id,
const MgmtLongTermKeyInfo key 
)
noexcept

Definition at line 834 of file BTManager.cpp.

Here is the call graph for this function:

◆ uploadLongTermKeyInfo()

HCIStatusCode BTManager::uploadLongTermKeyInfo ( const uint16_t  dev_id,
const BDAddressAndType addressAndType,
const SMPLongTermKeyInfo ltk 
)
noexcept

Definition at line 858 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ userConfirmReply()

MgmtStatus BTManager::userConfirmReply ( const uint16_t  dev_id,
const BDAddressAndType addressAndType,
const bool  positive 
)
noexcept

Definition at line 916 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ userPasskeyNegativeReply()

MgmtStatus BTManager::userPasskeyNegativeReply ( const uint16_t  dev_id,
const BDAddressAndType addressAndType 
)
noexcept

Definition at line 901 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ userPasskeyReply()

MgmtStatus BTManager::userPasskeyReply ( const uint16_t  dev_id,
const BDAddressAndType addressAndType,
const uint32_t  passkey 
)
noexcept

Definition at line 886 of file BTManager.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ pidSelf

const pid_t BTManager::pidSelf = getpid()
static

Definition at line 208 of file BTManager.hpp.


The documentation for this class was generated from the following files: