Direct-BT
2.3.1
Direct-BT - Direct Bluetooth Programming.
|
Go to the documentation of this file.
26 #ifndef BT_DEVICE_HPP_
27 #define BT_DEVICE_HPP_
40 #include "HCIIoctl.hpp"
54 class AdapterStatusListener;
64 uint64_t ts_last_discovery;
65 uint64_t ts_last_update;
68 int8_t tx_power = 127;
72 std::shared_ptr<ManufactureSpecificData> advMSD =
nullptr;
74 #if SMP_SUPPORTED_BY_OS
75 std::shared_ptr<SMPHandler> smpHandler =
nullptr;
76 std::recursive_mutex mtx_smpHandler;
78 std::shared_ptr<BTGattHandler> gattHandler =
nullptr;
79 mutable std::recursive_mutex mtx_gattHandler;
80 mutable std::recursive_mutex mtx_connect;
81 mutable std::mutex mtx_data;
82 std::atomic<bool> isConnected;
83 std::atomic<bool> allowDisconnect;
94 bool res_requested_sec;
100 uint8_t maxEncsz_init, maxEncsz_resp;
110 bool is_static_random_address;
115 PairingData pairing_data;
116 mutable std::mutex mtx_pairing;
117 std::condition_variable cv_pairing_state_changed;
121 class ctor_cookie {
friend BTDevice; ctor_cookie(
const uint16_t secret) { (void)secret; } };
125 return std::make_shared<BTDevice>(BTDevice::ctor_cookie(0), adapter, r);
129 bool addAdvService(std::shared_ptr<uuid_t>
const &uuid) noexcept;
131 bool addAdvServices(
jau::darray<std::shared_ptr<uuid_t>>
const & services) noexcept;
136 int findAdvService(std::shared_ptr<uuid_t>
const &uuid)
const noexcept;
141 void notifyDisconnected() noexcept;
142 void notifyConnected(std::shared_ptr<BTDevice> sthis,
const uint16_t handle,
const SMPIOCapability io_cap) noexcept;
143 void notifyLEFeatures(std::shared_ptr<BTDevice> sthis,
const LE_Features features) noexcept;
152 void processL2CAPSetup(std::shared_ptr<BTDevice> sthis);
170 bool connectSMP(std::shared_ptr<BTDevice> sthis,
const BTSecurityLevel sec_level) noexcept;
172 bool checkPairingKeyDistributionComplete(
const std::string& timestamp)
const noexcept;
192 void processDeviceReady(std::shared_ptr<BTDevice> sthis,
const uint64_t timestamp);
203 bool connectGATT(std::shared_ptr<BTDevice> sthis) noexcept;
208 void disconnectGATT(
const int caller) noexcept;
213 void disconnectSMP(
const int caller) noexcept;
215 void clearSMPStates(
const bool connected) noexcept;
217 void sendMgmtEvDeviceDisconnected(std::unique_ptr<MgmtEvent> evt) noexcept;
272 uint64_t
getLastUpdateAge(
const uint64_t ts_now)
const noexcept {
return ts_now - ts_last_update; }
281 int8_t
getRSSI() const noexcept {
return rssi; }
289 std::string
const getName() const noexcept;
305 std::string
toString(
bool includeDiscoveredServices)
const noexcept;
410 const uint16_t conn_interval_min=12,
const uint16_t conn_interval_max=12,
437 const uint16_t clock_offset=0x0000, const uint8_t role_switch=0x01) noexcept;
943 {
return lhs.getAddressAndType() == rhs.getAddressAndType(); }
946 {
return !(lhs == rhs); }
@ UNSET
Denoting unset value, i.e.
HCIStatusCode connectDefault() noexcept
Establish a default HCI connection to this device, using certain default parameter.
PairingMode getPairingMode() const noexcept
Returns the current PairingMode used by the device.
void remove() noexcept
Disconnects this device via disconnect(..) if getConnected()==true and explicitly removes its shared ...
SMPPairingState getPairingState() const noexcept
Returns the current SMPPairingState.
bool setConnSecurityAuto(const SMPIOCapability iocap_auto) noexcept
Set automatic security negotiation of BTSecurityLevel and SMPIOCapability pairing mode.
SMPSignatureResolvingKeyInfo getSignatureResolvingKeyInfo(const bool responder) const noexcept
Returns a copy of the Signature Resolving Key (LTK) info, valid after connection and SMP pairing has ...
static const uint16_t le_scan_interval
Representing ACL Datas' L2CAP Frame.
EIRDataType
Bit mask of 'Extended Inquiry Response' (EIR) data fields, indicating a set of related data.
int8_t getTxPower() const noexcept
Return Tx Power of device as recognized at discovery and connect.
SMPLongTermKeyInfo getLongTermKeyInfo(const bool responder) const noexcept
Returns a copy of the Long Term Key (LTK) info, valid after connection and SMP pairing has been compl...
SMPKeyType
SMP Key Type for Distribution, indicates keys distributed in the Transport Specific Key Distribution ...
Collection of 'Extended Advertising Data' (EAD), 'Advertising Data' (AD) or 'Extended Inquiry Respons...
~BTDevice() noexcept
Releases this instance after calling remove().
bool addCharListener(std::shared_ptr< BTGattCharListener > l)
Add the given BTGattCharListener to the listener list if not already present.
bool setConnSecurity(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept
Sets the given BTSecurityLevel and SMPIOCapability used to connect to this device on the upcoming con...
HCIStatusCode getConnectedLE_PHY(LE_PHYs &resRx, LE_PHYs &resTx) noexcept
Request and return LE_PHYs bit for the given connection.
HCIStatusCode setPairingNumericComparison(const bool equal) noexcept
Method sets the numeric comparison result, see PairingMode::NUMERIC_COMPARE_ini.
uint64_t getLastDiscoveryTimestamp() const noexcept
Returns the timestamp in monotonic milliseconds when this device instance has discovered or connected...
bool pingGATT() noexcept
Issues a GATT ping to the device, validating whether it is still reachable.
HCIStatusCode connectLE(const uint16_t le_scan_interval=24, const uint16_t le_scan_window=24, 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
Establish a HCI BDADDR_LE_PUBLIC or BDADDR_LE_RANDOM connection to this device.
Read/Write L2CAP communication channel.
std::shared_ptr< ManufactureSpecificData > const getManufactureSpecificData() const noexcept
Return shared ManufactureSpecificData as recognized at discovery, pre GATT discovery.
SMPIOCapability getConnIOCapability() const noexcept
Return the set SMPIOCapability value, determined when the connection is established.
uint64_t getLastUpdateAge(const uint64_t ts_now) const noexcept
uint64_t getCreationTimestamp() const noexcept
Returns the timestamp in monotonic milliseconds when this device instance has been created,...
Representing a complete [Primary] Service Declaration including its list of Characteristic Declaratio...
HCIStatusCode connectBREDR(const uint16_t pkt_type=HCI_DM1|HCI_DM3|HCI_DM5|HCI_DH1|HCI_DH3|HCI_DH5, const uint16_t clock_offset=0x0000, const uint8_t role_switch=0x01) noexcept
Establish a HCI BDADDR_BREDR connection to this device.
std::string toString() const noexcept override
constexpr BDAddressAndType const & getAddressAndType() const noexcept
Returns the unique device EUI48 address and BDAddressType type.
SMPOOBDataFlag
Vol 3, Part H, 2.3.3 OOB authentication data.
BTSecurityLevel
Bluetooth Security Level.
uint16_t opcode, uint16_t dev-id, uint16_t param_size
AppearanceCat getAppearance() const noexcept
Return AppearanceCat of device as recognized at discovery, connect and GATT discovery.
HCIStatusCode
BT Core Spec v5.2: Vol 1, Part F Controller Error Codes: 1.3 List of Error Codes.
SMPKeyType getAvailableSMPKeys(const bool responder) const noexcept
Returns the available SMPKeyType mask for the responder (LL slave) or initiator (LL master).
std::atomic<T> type with predefined fixed std::memory_order, not allowing changing the memory model o...
Implementation of a dynamic linear array storage, aka vector.
bool removeCharListener(std::shared_ptr< BTGattCharListener > l) noexcept
Remove the given BTGattCharListener from the listener list.
bool setConnSecurityLevel(const BTSecurityLevel sec_level) noexcept
Set the BTSecurityLevel used to connect to this device on the upcoming connection.
std::shared_ptr< GattGenericAccessSvc > getGattGenericAccess()
Returns the shared GenericAccess instance, retrieved by getGattService() or nullptr if not available.
int removeAllAssociatedCharListener(std::shared_ptr< BTGattChar > associatedCharacteristic) noexcept
Remove all BTGattCharListener from the list, which are associated to the given BTGattChar.
LE_Features
HCI Supported Commands.
BTGattChar event listener for notification and indication events.
static const uint16_t le_scan_window
HCIStatusCode setPairingPasskey(const uint32_t passkey) noexcept
Method sets the given passkey entry, see PairingMode::PASSKEY_ENTRY_ini.
Generic Access Service is a mandatory GATT service all peripherals are required to implement.
void operator=(const BTDevice &)=delete
HCIStatusCode disconnect(const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION) noexcept
Disconnect the LE or BREDR peer's GATT and HCI connection.
bool removeStatusListener(std::shared_ptr< AdapterStatusListener > l)
Remove the given listener from the list.
Handles the Security Manager Protocol (SMP) using Protocol Data Unit (PDU) encoded messages over L2CA...
const uint64_t ts_creation
SMP Signature Resolving Key Info, used for platform agnostic persistence.
@ UNSET
Security Level not set, value 0.
PairingMode
Bluetooth secure pairing mode.
int removeAllCharListener() noexcept
Remove all BTGattCharListener from the list.
bool setConnSecurityBest(const BTSecurityLevel sec_level, const SMPIOCapability io_cap) noexcept
Convenience method to determine the best practice BTSecurityLevel and SMPIOCapability based on the gi...
std::shared_ptr< ConnectionInfo > getConnectionInfo() noexcept
Retrieves the current connection info for this device and returns the ConnectionInfo reference if suc...
const BDAddressAndType addressAndType
Device's unique mac address and type tuple.
A thread safe GATT handler associated to one device via one L2CAP connection.
bool isConnSecurityAutoEnabled() const noexcept
Returns true if automatic security negotiation has been enabled via setConnSecurityAuto(),...
SMPAuthReqs
SMP Authentication Requirements Bits, denotes specific bits or whole protocol uint8_t bit-mask.
static std::string java_class() noexcept
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...
jau::darray< std::shared_ptr< uuid_t > > getAdvertisedServices() const noexcept
Return a list of advertised services as recognized at discovery, pre GATT discovery.
BTAdapter represents one Bluetooth Controller.
std::shared_ptr< BTDevice > getSharedInstance() const noexcept
Returns the shared pointer of this instance managed by the adapter.
std::string get_java_class() const noexcept override
A packed 48 bit EUI-48 identifier, formerly known as MAC-48 or simply network device MAC address (Med...
bool setConnIOCapability(const SMPIOCapability io_cap) noexcept
Sets the given SMPIOCapability used to connect to this device on the upcoming connection.
SMPIOCapability
Vol 3, Part H, 2.3.2 IO capabilities.
BTDevice(const BTDevice::ctor_cookie &cc, BTAdapter &adapter, EInfoReport const &r)
Private ctor for private BTDevice::make_shared() intended for friends.
jau::darray< std::shared_ptr< BTGattService > > getGattServices() noexcept
Returns a list of shared GATTService available on this device if successful, otherwise returns an emp...
HCIStatusCode setPairingPasskeyNegative() noexcept
Method replies with a negative passkey response, i.e.
bool addStatusListener(std::shared_ptr< AdapterStatusListener > l)
Add the given AdapterStatusListener to the list if not already present, intended to listen only for e...
std::shared_ptr< BTGattHandler > getGattHandler() noexcept
Returns the connected GATTHandler or nullptr, see connectGATT(), getGattService() and disconnect().
bool getConnected() noexcept
Return true if the device has been successfully connected, otherwise false.
int8_t getRSSI() const noexcept
Return RSSI of device as recognized at discovery and connect.
std::string const getName() const noexcept
uint64_t getLastUpdateTimestamp() const noexcept
Returns the timestamp in monotonic milliseconds when this device instance underlying data has been up...
HCIStatusCode pair(const SMPIOCapability io_cap) noexcept
Experimental only.
bool operator!=(const EUI48Sub &lhs, const EUI48Sub &rhs) noexcept
HCIStatusCode unpair() noexcept
Unpairs this device from the adapter while staying connected.
SMPPairingState
SMP Pairing Process state definition.
BTSecurityLevel getConnSecurityLevel() const noexcept
Return the BTSecurityLevel, determined when the connection is established.
@ REMOTE_USER_TERMINATED_CONNECTION
HCIStatusCode setLongTermKeyInfo(const SMPLongTermKeyInfo <k) noexcept
Sets the long term ket (LTK) info of this device to reuse pre-paired encryption.
LE_PHYs
LE Transport PHY bit values.
Unique Bluetooth EUI48 address and BDAddressType tuple.
BTDevice(const BTDevice &)=delete
BTAdapter & getAdapter() const
Returns the managing adapter.
uint16_t getConnectionHandle() const noexcept
Return the HCI connection handle to the LE or BREDR peer, zero if not connected.
std::shared_ptr< BTGattService > findGattService(std::shared_ptr< uuid_t > const &uuid)
Returns the matching GATTService for the given uuid.
SMP Long Term Key Info, used for platform agnostic persistence.