aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-05-24 03:23:49 +0200
committerSven Gothel <[email protected]>2020-05-24 03:23:49 +0200
commit1183b02ba218bfd32d17fce4a425136b700754a6 (patch)
tree6d39fe50fb0e50b45f3a20bad693850db07ff536
parentc760eac8addfda8e63ae213827840d251ecadfa0 (diff)
Fix Whitelist to DBTManager/DBTAdapter and test (Result: Auto-connection but 1+ second slower)
Essential for the whitelist autoconnect is the 'action' parameter, which is now declared as HCIWhitelistConnectType (BTTypes). BlueZ Linux kernel sources describe the semantics. We are now technically enabled to use the auto-connect and don't even required a discovery phase. However, the device name is not included in the connect EIR data. Further, performance lacks heavily similar to no connect and is more than 1s slower than our procedure below: - startDiscovery - device-found - stopDiscovery - connect - GATT operation ... - startDiscovery
-rw-r--r--api/direct_bt/BTTypes.hpp12
-rw-r--r--api/direct_bt/DBTAdapter.hpp2
-rw-r--r--api/direct_bt/DBTManager.hpp2
-rw-r--r--api/direct_bt/MgmtTypes.hpp4
-rw-r--r--examples/direct_bt_scanner10/dbt_scanner10.cpp44
-rw-r--r--src/direct_bt/DBTAdapter.cpp11
-rw-r--r--src/direct_bt/DBTManager.cpp4
7 files changed, 48 insertions, 31 deletions
diff --git a/api/direct_bt/BTTypes.hpp b/api/direct_bt/BTTypes.hpp
index 8e98adc5..b65f76dc 100644
--- a/api/direct_bt/BTTypes.hpp
+++ b/api/direct_bt/BTTypes.hpp
@@ -44,6 +44,18 @@ namespace direct_bt {
BT_MODE_LE = 3
};
+ /**
+ * HCI Whitelist connection type.
+ */
+ enum HCIWhitelistConnectType : uint8_t {
+ /** Report Connection: Only supported for LE on Linux .. */
+ HCI_AUTO_CONN_REPORT = 0x00,
+ /** Incoming Connections: Only supported type for BDADDR_BREDR (!LE) on Linux .. */
+ HCI_AUTO_CONN_DIRECT = 0x01,
+ /** Auto Connect: Only supported for LE on Linux .. */
+ HCI_AUTO_CONN_ALWAYS = 0x02
+ };
+
enum AD_Type_Const : uint8_t {
AD_FLAGS_LIMITED_MODE_BIT = 0x01,
AD_FLAGS_GENERAL_MODE_BIT = 0x02
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp
index 08614bfc..88e146e8 100644
--- a/api/direct_bt/DBTAdapter.hpp
+++ b/api/direct_bt/DBTAdapter.hpp
@@ -265,7 +265,7 @@ namespace direct_bt {
bool closeHCI();
/** Add the given device to the adapter's autoconnect whitelist. */
- bool addDeviceToWhitelist(const EUI48 &address, const BDAddressType address_type);
+ bool addDeviceToWhitelist(const EUI48 &address, const BDAddressType address_type, const HCIWhitelistConnectType ctype);
/** Remove the given device from the adapter's autoconnect whitelist. */
bool removeDeviceFromWhitelist(const EUI48 &address, const BDAddressType address_type);
diff --git a/api/direct_bt/DBTManager.hpp b/api/direct_bt/DBTManager.hpp
index 6e36736e..4f174662 100644
--- a/api/direct_bt/DBTManager.hpp
+++ b/api/direct_bt/DBTManager.hpp
@@ -234,7 +234,7 @@ namespace direct_bt {
bool stopDiscovery(const int dev_id, const ScanType type);
/** Add the given device to the adapter's autoconnect whitelist. */
- bool addDeviceToWhitelist(const int dev_id, const EUI48 &address, const BDAddressType address_type);
+ bool addDeviceToWhitelist(const int dev_id, const EUI48 &address, const BDAddressType address_type, const HCIWhitelistConnectType ctype);
/** Remove the given device from the adapter's autoconnect whitelist. */
bool removeDeviceFromWhitelist(const int dev_id, const EUI48 &address, const BDAddressType address_type);
diff --git a/api/direct_bt/MgmtTypes.hpp b/api/direct_bt/MgmtTypes.hpp
index a5583a51..0114a2e3 100644
--- a/api/direct_bt/MgmtTypes.hpp
+++ b/api/direct_bt/MgmtTypes.hpp
@@ -319,12 +319,12 @@ namespace direct_bt {
class MgmtAddDeviceToWhitelistCmd : public MgmtCommand
{
public:
- MgmtAddDeviceToWhitelistCmd(const uint16_t dev_id, const EUI48 &address, const BDAddressType addressType, const uint8_t action)
+ MgmtAddDeviceToWhitelistCmd(const uint16_t dev_id, const EUI48 &address, const BDAddressType addressType, const HCIWhitelistConnectType ctype)
: MgmtCommand(MgmtOpcode::ADD_DEVICE_WHITELIST, dev_id, 6+1+1)
{
pdu.put_eui48(MGMT_HEADER_SIZE, address);
pdu.put_uint8(MGMT_HEADER_SIZE+6, addressType);
- pdu.put_uint8(MGMT_HEADER_SIZE+6+1, action);
+ pdu.put_uint8(MGMT_HEADER_SIZE+6+1, ctype);
}
};
diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp
index e5457795..57df64bb 100644
--- a/examples/direct_bt_scanner10/dbt_scanner10.cpp
+++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp
@@ -235,21 +235,16 @@ class MyGATTEventListener : public SpecificGATTCharacteristicListener {
static void deviceConnectTask(std::shared_ptr<DBTDevice> device) {
fprintf(stderr, "****** Device Connector: Start %s\n", device->toString().c_str());
+ fprintf(stderr, "****** Device Connector: stopDiscovery()\n");
device->getAdapter().stopDiscovery();
bool res = false;
if( !USE_WHITELIST ) {
res = device->connectHCIDefault();
}
fprintf(stderr, "****** Device Connector: End result %d of %s\n", res, device->toString().c_str());
- if( !USE_WHITELIST ) {
- if( BLOCK_DISCOVERY ) {
- if( !res && 0 == getDeviceTaskCount() ) {
- fprintf(stderr, "****** Device Connector: startDiscovery()\n");
- device->getAdapter().startDiscovery(true);
- }
- } else {
- device->getAdapter().startDiscovery(false);
- }
+ if( !USE_WHITELIST && !BLOCK_DISCOVERY ) {
+ fprintf(stderr, "****** Device Connector: startDiscovery()\n");
+ device->getAdapter().startDiscovery(false);
}
}
@@ -307,20 +302,22 @@ static void deviceProcessTask(std::shared_ptr<DBTDevice> device) {
// FIXME sleep 1s for potential callbacks ..
sleep(1);
}
- if( BLOCK_DISCOVERY ) {
+ if( USE_WHITELIST || BLOCK_DISCOVERY ) {
device->disconnect();
} else {
+ fprintf(stderr, "****** Device Process: stopDiscovery()\n");
device->getAdapter().stopDiscovery();
device->disconnect();
+ fprintf(stderr, "****** Device Process: startDiscovery()\n");
device->getAdapter().startDiscovery(false);
}
out:
addDevicesProcessed(device->getAddress());
- if( !USE_WHITELIST && BLOCK_DISCOVERY ) {
+ if( BLOCK_DISCOVERY ) {
if( 1 >= getDeviceTaskCount() ) {
fprintf(stderr, "****** Device Process: startDiscovery()\n");
- device->getAdapter().startDiscovery(true);
+ device->getAdapter().startDiscovery( !USE_WHITELIST && BLOCK_DISCOVERY );
}
}
removeDeviceTask(device);
@@ -380,25 +377,26 @@ int main(int argc, char *argv[])
adapter.addStatusListener(std::shared_ptr<AdapterStatusListener>(new MyAdapterStatusListener()));
+ std::shared_ptr<HCIComm> hci = adapter.openHCI();
+ if( nullptr == hci || !hci->isOpen() ) {
+ fprintf(stderr, "Couldn't open HCI from %s\n", adapter.toString().c_str());
+ exit(1);
+ }
+
if( USE_WHITELIST ) {
for (auto it = whitelist.begin(); it != whitelist.end(); ++it) {
std::shared_ptr<EUI48> wlmac = *it;
- bool res = adapter.addDeviceToWhitelist(*wlmac, BDAddressType::BDADDR_LE_PUBLIC);
+ bool res = adapter.addDeviceToWhitelist(*wlmac, BDAddressType::BDADDR_LE_PUBLIC, HCIWhitelistConnectType::HCI_AUTO_CONN_ALWAYS);
fprintf(stderr, "Added to whitelist: res %d, address %s\n", res, wlmac->toString().c_str());
}
- } else {
- std::shared_ptr<HCIComm> hci = adapter.openHCI();
- if( nullptr == hci || !hci->isOpen() ) {
- fprintf(stderr, "Couldn't open HCI from %s\n", adapter.toString().c_str());
- exit(1);
- }
}
// if( !USE_WHITELIST ) {
- if( !adapter.startDiscovery(BLOCK_DISCOVERY) ) {
- perror("Adapter start discovery failed");
- goto out;
- }
+ fprintf(stderr, "****** Main: startDiscovery()\n");
+ if( !adapter.startDiscovery( !USE_WHITELIST && BLOCK_DISCOVERY ) ) {
+ perror("Adapter start discovery failed");
+ goto out;
+ }
// }
do {
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index f92133bf..70b10994 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -218,8 +218,8 @@ bool DBTAdapter::closeHCI()
return true;
}
-bool DBTAdapter::addDeviceToWhitelist(const EUI48 &address, const BDAddressType address_type) {
- return mgmt.addDeviceToWhitelist(dev_id, address, address_type);
+bool DBTAdapter::addDeviceToWhitelist(const EUI48 &address, const BDAddressType address_type, const HCIWhitelistConnectType ctype) {
+ return mgmt.addDeviceToWhitelist(dev_id, address, address_type, ctype);
}
bool DBTAdapter::removeDeviceFromWhitelist(const EUI48 &address, const BDAddressType address_type) {
@@ -498,6 +498,13 @@ bool DBTAdapter::mgmtEvDeviceConnectedCB(std::shared_ptr<MgmtEvent> e) {
new_connect = 2;
}
}
+ if( nullptr == device ) {
+ // a whitelist auto-connect w/o previous discovery
+ device = std::shared_ptr<DBTDevice>(new DBTDevice(*this, ad_report));
+ addDiscoveredDevice(device);
+ addSharedDevice(device);
+ new_connect = 3;
+ }
if( nullptr != device ) {
EIRDataType updateMask = device->update(ad_report);
DBG_PRINT("DBTAdapter::EventCB:DeviceConnected(dev_id %d, new_connect %d, updated %s): %s,\n %s\n -> %s",
diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp
index 9bbd6b81..0743f427 100644
--- a/src/direct_bt/DBTManager.cpp
+++ b/src/direct_bt/DBTManager.cpp
@@ -504,8 +504,8 @@ bool DBTManager::stopDiscovery(const int dev_id, const ScanType type) {
}
}
-bool DBTManager::addDeviceToWhitelist(const int dev_id, const EUI48 &address, const BDAddressType address_type) {
- MgmtAddDeviceToWhitelistCmd req(dev_id, address, address_type, 0x01);
+bool DBTManager::addDeviceToWhitelist(const int dev_id, const EUI48 &address, const BDAddressType address_type, const HCIWhitelistConnectType ctype) {
+ MgmtAddDeviceToWhitelistCmd req(dev_id, address, address_type, ctype);
DBG_PRINT("DBTManager::addDeviceToWhitelist: %s", req.toString().c_str());
std::shared_ptr<MgmtEvent> res = sendWithReply(req);
if( nullptr == res ) {