diff options
author | Sven Gothel <[email protected]> | 2020-07-03 18:39:51 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-07-03 18:39:51 +0200 |
commit | 0a58adf310f9ae29f0e753c9594e6879e0e2c0c7 (patch) | |
tree | f9116f578b469357f3d7a2b05b18f0f8ce1ccc98 | |
parent | 22e3abf3661d7de42b3e6c738d8167718e3ca218 (diff) |
DBTDevice lookup in DBTAdapter requires its EUI48 address _and_ BDAddressType
Concluding commit 22e3abf3661d7de42b3e6c738d8167718e3ca218,
having all DBTDevice lookups (find) in DBTAdapter using the BDAddressType as well.
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 11 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTAdapter.cxx | 12 | ||||
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 46 |
3 files changed, 24 insertions, 45 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 21dc4a52..f450c4ba 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -150,9 +150,8 @@ namespace direct_bt { { private: /** Returns index >= 0 if found, otherwise -1 */ - static int findDeviceIdx(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac); - static std::shared_ptr<DBTDevice> findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac); - static int countDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac); + static int findDeviceIdx(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac, const BDAddressType macType); + static std::shared_ptr<DBTDevice> findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac, const BDAddressType macType); std::shared_ptr<DBTDevice> findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, DBTDevice const & device); DBTManager& mgmt; @@ -189,7 +188,7 @@ namespace direct_bt { bool addConnectedDevice(const std::shared_ptr<DBTDevice> & device); bool removeConnectedDevice(const DBTDevice & device); int disconnectAllDevices(const HCIStatusCode reason=HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION ); - std::shared_ptr<DBTDevice> findConnectedDevice (EUI48 const & mac); + std::shared_ptr<DBTDevice> findConnectedDevice (EUI48 const & mac, const BDAddressType macType); bool addDiscoveredDevice(std::shared_ptr<DBTDevice> const &device); bool removeDiscoveredDevice(const DBTDevice & device); @@ -197,7 +196,7 @@ namespace direct_bt { bool addSharedDevice(std::shared_ptr<DBTDevice> const &device); std::shared_ptr<DBTDevice> getSharedDevice(const DBTDevice & device); void removeSharedDevice(const DBTDevice & device); - std::shared_ptr<DBTDevice> findSharedDevice (EUI48 const & mac); + std::shared_ptr<DBTDevice> findSharedDevice (EUI48 const & mac, const BDAddressType macType); bool mgmtEvDeviceDiscoveringMgmt(std::shared_ptr<MgmtEvent> e); bool mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e); @@ -453,7 +452,7 @@ namespace direct_bt { int removeDiscoveredDevices(); /** Returns shared DBTDevice if found, otherwise nullptr */ - std::shared_ptr<DBTDevice> findDiscoveredDevice (EUI48 const & mac); + std::shared_ptr<DBTDevice> findDiscoveredDevice (EUI48 const & mac, const BDAddressType macType); std::string toString() const override; diff --git a/java/jni/direct_bt/DBTAdapter.cxx b/java/jni/direct_bt/DBTAdapter.cxx index b98ce735..4aa4c434 100644 --- a/java/jni/direct_bt/DBTAdapter.cxx +++ b/java/jni/direct_bt/DBTAdapter.cxx @@ -621,17 +621,9 @@ jobject Java_direct_1bt_tinyb_DBTAdapter_connectDevice(JNIEnv *env, jobject obj, JavaGlobalObj::check(adapter->getJavaObject(), E_FILE_LINE); std::string saddress = from_jstring_to_string(env, jaddress); EUI48 address(saddress); - std::shared_ptr<DBTDevice> device = adapter->findDiscoveredDevice(address); + const BDAddressType addressType = fromJavaAdressTypeToBDAddressType(env, jaddressType); + std::shared_ptr<DBTDevice> device = adapter->findDiscoveredDevice(address, addressType); if( nullptr != device ) { - const BDAddressType addressType = fromJavaAdressTypeToBDAddressType(env, jaddressType); - const BDAddressType addressTypeDevice = device->getAddressType(); - if( addressTypeDevice != addressType ) { - // oops? - WARN_PRINT("DBTAdapter::connectDevice: AddressType mismatch, ignoring request: Requested %s, Device %s %s", - getBDAddressTypeString(addressType).c_str(), getBDAddressTypeString(addressTypeDevice).c_str(), - device->toString().c_str()); - } - std::shared_ptr<direct_bt::HCIHandler> hci = adapter->openHCI(); if( nullptr == hci ) { throw BluetoothException("Couldn't get or open adapter's HCI "+adapter->toString(), E_FILE_LINE); diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 76520fec..102c8791 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -51,40 +51,28 @@ extern "C" { using namespace direct_bt; -int DBTAdapter::findDeviceIdx(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac) { +int DBTAdapter::findDeviceIdx(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac, const BDAddressType macType) { const size_t size = devices.size(); for (size_t i = 0; i < size; i++) { std::shared_ptr<DBTDevice> & e = devices[i]; - if ( nullptr != e && mac == e->getAddress() ) { + if ( nullptr != e && mac == e->getAddress() && macType == e->getAddressType() ) { return i; } } return -1; } -std::shared_ptr<DBTDevice> DBTAdapter::findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac) { +std::shared_ptr<DBTDevice> DBTAdapter::findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac, const BDAddressType macType) { const size_t size = devices.size(); for (size_t i = 0; i < size; i++) { std::shared_ptr<DBTDevice> & e = devices[i]; - if ( nullptr != e && mac == e->getAddress() ) { + if ( nullptr != e && mac == e->getAddress() && macType == e->getAddressType() ) { return e; } } return nullptr; } -int DBTAdapter::countDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, EUI48 const & mac) { - int count = 0; - const size_t size = devices.size(); - for (size_t i = 0; i < size; i++) { - std::shared_ptr<DBTDevice> & e = devices[i]; - if ( nullptr != e && mac == e->getAddress() ) { - count++; - } - } - return count; -} - std::shared_ptr<DBTDevice> DBTAdapter::findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, DBTDevice const & device) { const size_t size = devices.size(); for (size_t i = 0; i < size; i++) { @@ -133,9 +121,9 @@ int DBTAdapter::disconnectAllDevices(const HCIStatusCode reason) { return count; } -std::shared_ptr<DBTDevice> DBTAdapter::findConnectedDevice (EUI48 const & mac) { +std::shared_ptr<DBTDevice> DBTAdapter::findConnectedDevice (EUI48 const & mac, const BDAddressType macType) { const std::lock_guard<std::recursive_mutex> lock(mtx_connectedDevices); // RAII-style acquire and relinquish via destructor - return findDevice(connectedDevices, mac); + return findDevice(connectedDevices, mac, macType); } @@ -387,9 +375,9 @@ void DBTAdapter::stopDiscovery() { DBG_PRINT("DBTAdapter::stopDiscovery: X"); } -std::shared_ptr<DBTDevice> DBTAdapter::findDiscoveredDevice (EUI48 const & mac) { +std::shared_ptr<DBTDevice> DBTAdapter::findDiscoveredDevice (EUI48 const & mac, const BDAddressType macType) { const std::lock_guard<std::recursive_mutex> lock(const_cast<DBTAdapter*>(this)->mtx_discoveredDevices); // RAII-style acquire and relinquish via destructor - return findDevice(discoveredDevices, mac); + return findDevice(discoveredDevices, mac, macType); } bool DBTAdapter::addDiscoveredDevice(std::shared_ptr<DBTDevice> const &device) { @@ -456,9 +444,9 @@ void DBTAdapter::removeSharedDevice(const DBTDevice & device) { } } -std::shared_ptr<DBTDevice> DBTAdapter::findSharedDevice (EUI48 const & mac) { +std::shared_ptr<DBTDevice> DBTAdapter::findSharedDevice (EUI48 const & mac, const BDAddressType macType) { const std::lock_guard<std::recursive_mutex> lock(mtx_sharedDevices); // RAII-style acquire and relinquish via destructor - return findDevice(sharedDevices, mac); + return findDevice(sharedDevices, mac, macType); } std::string DBTAdapter::toString() const { @@ -578,13 +566,13 @@ bool DBTAdapter::mgmtEvDeviceConnectedHCI(std::shared_ptr<MgmtEvent> e) { ad_report.read_data(event.getData(), event.getDataSize()); } int new_connect = 0; - std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress()); + std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType()); if( nullptr == device ) { - device = findDiscoveredDevice(event.getAddress()); + device = findDiscoveredDevice(event.getAddress(), event.getAddressType()); new_connect = nullptr != device ? 1 : 0; } if( nullptr == device ) { - device = findSharedDevice(event.getAddress()); + device = findSharedDevice(event.getAddress(), event.getAddressType()); if( nullptr != device ) { addDiscoveredDevice(device); new_connect = 2; @@ -641,7 +629,7 @@ bool DBTAdapter::mgmtEvDeviceConnectedHCI(std::shared_ptr<MgmtEvent> e) { bool DBTAdapter::mgmtEvConnectFailedHCI(std::shared_ptr<MgmtEvent> e) { DBG_PRINT("DBTAdapter::EventHCI:ConnectFailed: %s", e->toString().c_str()); const MgmtEvtDeviceConnectFailed &event = *static_cast<const MgmtEvtDeviceConnectFailed *>(e.get()); - std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress()); + std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType()); if( nullptr != device ) { DBG_PRINT("DBTAdapter::EventHCI:ConnectFailed(dev_id %d): %s\n -> %s", dev_id, event.toString().c_str(), device->toString().c_str()); @@ -671,7 +659,7 @@ bool DBTAdapter::mgmtEvConnectFailedHCI(std::shared_ptr<MgmtEvent> e) { bool DBTAdapter::mgmtEvDeviceDisconnectedHCI(std::shared_ptr<MgmtEvent> e) { const MgmtEvtDeviceDisconnected &event = *static_cast<const MgmtEvtDeviceDisconnected *>(e.get()); - std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress()); + std::shared_ptr<DBTDevice> device = findConnectedDevice(event.getAddress(), event.getAddressType()); if( nullptr != device ) { if( device->getConnectionHandle() != event.getHCIHandle() ) { INFO_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): ConnHandle mismatch %s\n -> %s", @@ -728,7 +716,7 @@ bool DBTAdapter::mgmtEvDeviceFoundMgmt(std::shared_ptr<MgmtEvent> e) { std::shared_ptr<DBTDevice> dev; { const std::lock_guard<std::recursive_mutex> lock(const_cast<DBTAdapter*>(this)->mtx_discoveredDevices); // RAII-style acquire and relinquish via destructor - dev = findDiscoveredDevice(ad_report.getAddress()); + dev = findDiscoveredDevice(ad_report.getAddress(), ad_report.getAddressType()); } if( nullptr != dev ) { // @@ -742,7 +730,7 @@ bool DBTAdapter::mgmtEvDeviceFoundMgmt(std::shared_ptr<MgmtEvent> e) { return true; } - dev = findSharedDevice(ad_report.getAddress()); + dev = findSharedDevice(ad_report.getAddress(), ad_report.getAddressType()); if( nullptr != dev ) { // // active shared device, but flushed from discovered devices |