26#ifndef DBT_MANAGER_HPP_
27#define DBT_MANAGER_HPP_
123 const int32_t MGMT_READ_PACKET_MAX_RETRY;
219 struct WhitelistElem {
225 : dev_id(dev_id_), address_and_type(std::move(address_and_type_)), ctype(ctype_) { }
237 std::recursive_mutex mtx_sendReply;
243 inline bool isValidMgmtEventCallbackListsIndex(
const MgmtEvent::Opcode opc)
const noexcept {
244 return static_cast<uint16_t
>(opc) < mgmtAdapterEventCallbackLists.size();
270 std::unique_ptr<MgmtEvent> sendWithReply(MgmtCommand &req,
const jau::fraction_i64& timeout)
noexcept;
281 std::unique_ptr<MgmtEvent> sendWithReply(MgmtCommand &req)
noexcept {
285 bool send(MgmtCommand &req)
noexcept;
290 BTManager() noexcept;
291 bool initialize(const
std::shared_ptr<BTManager>& self) noexcept;
293 static const
std::shared_ptr<BTManager> make_shared() noexcept {
295 std::shared_ptr<BTManager> s(
new BTManager() );
298 }
catch (
const std::bad_alloc &e) {
299 ABORT(
"Error: bad_alloc: BTManager allocation failed");
304 std::unique_ptr<AdapterInfo> readAdapterInfo(
const uint16_t dev_id)
noexcept;
306 void processAdapterAdded(std::unique_ptr<MgmtEvent> e)
noexcept;
307 void processAdapterRemoved(std::unique_ptr<MgmtEvent> e)
noexcept;
308 void mgmtEvNewSettingsCB(
const MgmtEvent& e)
noexcept;
309 void mgmtEventAnyCB(
const MgmtEvent& e)
noexcept;
311 std::shared_ptr<BTAdapter> addAdapter(
const AdapterInfo& ai)
noexcept;
317 std::shared_ptr<BTAdapter> removeAdapter(
const uint16_t dev_id)
noexcept;
324 bool removeAdapter(BTAdapter* adapter)
noexcept;
326 MgmtStatus handleCurrentSettingsReply(std::unique_ptr<MgmtEvent>&& reply,
AdapterSetting& current_settings)
noexcept;
339 static const std::shared_ptr<BTManager>&
get() noexcept {
350 static std::shared_ptr<BTManager> s = make_shared();
356 void close() noexcept;
389 std::shared_ptr<BTAdapter>
getAdapter(
const uint16_t dev_id)
const noexcept;
447 const uint16_t conn_interval_min=8, const uint16_t conn_interval_max=40,
463 const uint16_t conn_interval_min=12, const uint16_t conn_interval_max=12,
Unique Bluetooth EUI48 address and BDAddressType tuple.
BTAdapter represents one local Bluetooth Controller.
A thread safe singleton handler of the BTAdapter manager, e.g.
bool addDeviceToWhitelist(const uint16_t dev_id, const BDAddressAndType &addressAndType, const HCIWhitelistConnectType ctype) noexcept
Add the given device to the adapter's autoconnect whitelist.
HCIStatusCode uploadLinkKey(const uint16_t dev_id, const MgmtLinkKeyInfo &key) noexcept
MgmtStatus userPINCodeNegativeReply(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
BTManager(const BTManager &)=delete
std::shared_ptr< ConnectionInfo > getConnectionInfo(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
void clearAllCallbacks() noexcept
Removes all MgmtEventCallbacks from all MgmtEvent::Opcode lists.
~BTManager() noexcept override
HCIStatusCode clearIdentityResolvingKeys(const uint16_t dev_id) noexcept
void operator=(const BTManager &)=delete
size_type removeMgmtEventCallback(const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
Returns count of removed given MgmtEventCallback from the named MgmtEvent::Opcode list.
HCIStatusCode setPrivacy(const uint16_t dev_id, const uint8_t privacy, const jau::uint128dp_t &irk, AdapterSetting ¤t_settings) noexcept
MgmtStatus setDiscoverable(const uint16_t dev_id, const uint8_t state, const uint16_t timeout, AdapterSetting ¤t_settings) noexcept
HCIStatusCode 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.
HCIStatusCode unpairDevice(const uint16_t dev_id, const BDAddressAndType &addressAndType, const bool disconnect) noexcept
MgmtStatus userPasskeyReply(const uint16_t dev_id, const BDAddressAndType &addressAndType, const uint32_t passkey) noexcept
bool setMode(const uint16_t dev_id, const MgmtCommand::Opcode opc, const uint8_t mode, AdapterSetting ¤t_settings) noexcept
std::shared_ptr< NameAndShortName > setLocalName(const uint16_t dev_id, const std::string &name, const std::string &short_name) noexcept
std::shared_ptr< BTAdapter > getAdapter(const uint16_t dev_id) const noexcept
Returns the DBTAdapter with the given dev_id, or nullptr if not found.
void sendMgmtEvent(const MgmtEvent &event) noexcept
Manually send a MgmtEvent to all of its listeners.
MgmtStatus userPasskeyNegativeReply(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
HCIStatusCode uploadIdentityResolvingKey(const uint16_t dev_id, const jau::darray< MgmtIdentityResolvingKey > &keys) noexcept
size_type removeAllDevicesFromWhitelist() noexcept
Remove all previously added devices from the autoconnect whitelist.
bool isOpen() const noexcept
Returns true if this mgmt instance is open and hence valid, otherwise false.
HCIStatusCode uploadLongTermKey(const uint16_t dev_id, const jau::darray< MgmtLongTermKey > &keys) noexcept
MgmtStatus userPINCodeReply(const uint16_t dev_id, const BDAddressAndType &addressAndType, const std::string &pinCode) noexcept
void addChangedAdapterSetCallback(const ChangedAdapterSetCallback &l)
ChangedAdapterSetCallback handling.
size_type removeAllChangedAdapterSetCallbacks() noexcept
Remove all added ChangedAdapterSetCallback entries from this manager.
HCIStatusCode initializeAdapter(AdapterInfo &adapterInfo, const uint16_t dev_id, const BTMode btMode, const bool powerOn) noexcept
Initialize the adapter with default values, including power-on.
HCIStatusCode setDefaultConnParam(const uint16_t dev_id, const uint16_t conn_interval_min=8, const uint16_t conn_interval_max=40, const uint16_t conn_latency=0, const uint16_t supervision_timeout=getHCIConnSupervisorTimeout(0, 50)) noexcept
Set default connection parameter for given adapter to the kernel.
bool isValidLongTermKeyAddressAndType(const EUI48 &address, const BDAddressType &address_type) const noexcept
Security commands.
bool setIOCapability(const uint16_t dev_id, const SMPIOCapability io_cap, SMPIOCapability &pre_io_cap) noexcept
std::string toString() const noexcept override
static const std::shared_ptr< BTManager > & get() noexcept
Retrieves the singleton instance.
SMPIOCapability getIOCapability(const uint16_t dev_id) const noexcept
jau::darray< std::shared_ptr< BTAdapter > > getAdapters()
Returns a list of currently added DBTAdapter.
std::vector< MgmtDefaultParam > readDefaultSysParam(const uint16_t dev_id) noexcept
Read default connection parameter for given adapter to the kernel.
std::string get_java_class() const noexcept override
std::shared_ptr< BTAdapter > getDefaultAdapter() const noexcept
Returns the default AdapterInfo.
bool addMgmtEventCallback(const int dev_id, const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept
MgmtEventCallback handling
bool isDeviceWhitelisted(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
Returns true, if the adapter's device is already whitelisted.
size_type removeChangedAdapterSetCallback(const ChangedAdapterSetCallback &l)
Remove the given ChangedAdapterSetCallback from this manager.
void clearMgmtEventCallbacks(const MgmtEvent::Opcode opc) noexcept
Removes all MgmtEventCallbacks from the to the named MgmtEvent::Opcode list.
static std::string java_class() noexcept
size_type getAdapterCount() const noexcept
retrieve information gathered at startup
bool removeDeviceFromWhitelist(const uint16_t dev_id, const BDAddressAndType &addressAndType) noexcept
Remove the given device from the adapter's autoconnect whitelist.
MgmtStatus userConfirmReply(const uint16_t dev_id, const BDAddressAndType &addressAndType, const bool positive) noexcept
mgmt_addr_info { EUI48, uint8_t type }, int8_t rssi, int8_t tx_power, int8_t max_tx_power;
Read/Write HCI communication channel.
bool is_open() const noexcept
Managment Singleton runtime environment properties.
const jau::fraction_i64 MGMT_COMMAND_REPLY_TIMEOUT
Timeout for mgmt command replies, defaults to 3s.
const jau::fraction_i64 MGMT_READER_THREAD_POLL_TIMEOUT
Poll timeout for mgmt reader thread, defaults to 10s.
const bool DEBUG_EVENT
Debug all Mgmt event communication.
const int32_t MGMT_EVT_RING_CAPACITY
Small ringbuffer capacity for synchronized commands, defaults to 64 messages.
const bool DEBUG_GLOBAL
Global Debug flag, retrieved first to triggers DBTEnv initialization.
const jau::fraction_i64 MGMT_SET_POWER_COMMAND_TIMEOUT
Timeout for mgmt SET_POWER command reply, defaults to max(MGMT_COMMAND_REPLY_TIMEOUT,...
static MgmtEnv & get() noexcept
uint16_t opcode, uint16_t dev-id, uint16_t param_size
Persistent endian aware octet data, i.e.
Implementation of a dynamic linear array storage, aka vector.
Class template jau::function is a general-purpose static-polymorphic function wrapper.
Sharing the anonymous Java object (JavaAnon), i.e.
std::string javaObjectToString() const noexcept
Ring buffer implementation, a.k.a circular buffer, exposing lock-free get*(..) and put*(....
Base jau environment class, merely to tag all environment settings by inheritance and hence documenta...
Service runner, a reusable dedicated thread performing custom user services.
#define ABORT(...)
Use for unconditional ::abort() call with given messages, prefix '[elapsed_time] ABORT @ file:line fu...
std::string to_string(const alphabet &v) noexcept
SMPIOCapability
Vol 3, Part H, 2.3.2 IO capabilities.
constexpr const bool USE_LINUX_BT_SECURITY
@ UNSET
Denoting unset value, i.e.
@ NO_INPUT_NO_OUTPUT
No input not output, value 3.
void(* ChangedAdapterSetFunc)(bool added, std::shared_ptr< BTAdapter > &adapter)
Callback function to receive change events regarding the system's adapter set, e.g.
std::shared_ptr< BTManager > BTManagerRef
BTMode
Bluetooth adapter operating mode.
HCIWhitelistConnectType
HCI Whitelist connection type.
AdapterSetting
Adapter Setting Bits.
BDAddressType
BT Core Spec v5.2: Vol 3, Part C Generic Access Profile (GAP): 15.1.1.1 Public Bluetooth address.
jau::function< void(bool, std::shared_ptr< BTAdapter > &)> ChangedAdapterSetCallback
Callback jau::function to receive change events regarding the system's adapter set,...
BTRole
Bluetooth roles from the perspective of the link layer (connection initiator).
jau::cow_darray< ChangedAdapterSetCallback > ChangedAdapterSetCallbackList
constexpr uint16_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...
HCIStatusCode
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
fraction< int64_t > fraction_i64
fraction using int64_t as integral type
uint_fast32_t nsize_t
Natural 'size_t' alternative using uint_fast32_t as its natural sized type.
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Used in MgmtReadDefaultSysParamCmd and MgmtSetDefaultConnParamCmd.
Used for MgmtLoadIdentityResolvingKeyCmd and MgmtEvtNewIdentityResolvingKey.
Used for MgmtLoadLinkKeyCmd and MgmtEvtNewLinkKey.
Used for MgmtLoadLongTermKeyCmd and MgmtEvtNewLongTermKey.
SMP Identity Resolving Key, used for platform agnostic persistence.
Local SMP Link Key, used for platform agnostic persistence, mapping to platform specific MgmtLoadLink...
SMP Long Term Key, used for platform agnostic persistence.
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...