aboutsummaryrefslogtreecommitdiffstats
path: root/src/direct_bt
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-06-27 12:38:00 +0200
committerSven Gothel <[email protected]>2020-06-27 12:38:00 +0200
commit406c56e2ebe96358ffd6000b033e0f99f87c66ac (patch)
tree49f52b1a69a9c2b9de674c2372958386ff7eebf6 /src/direct_bt
parentaeb0f54cc1e622324ce06edff342180500e4ccd1 (diff)
Use HCIHandler *connect*/disconnect; Enhance native + java test
Use HCIHandler *connect*/disconnect incl related events (Drop DBTManager in this regard) > Add CONNECT_FAILED HCI listener mgmtEvConnectFailedHCI -- Issuing a DISCONNECT event > DBTAdapter: mgmtEvDeviceConnectedHCI -- Always pass through, just issue WARNING if not a new_connect (TBD) > DBTAdapter: mgmtEvDeviceDisconnectedHCI + mgmtEvDeviceConnectedHCI -- removeConnectedDevice pre event issuing -- removeDiscoveredDevice post event issuing > DBTDevice: -- Add isConnectIssued to differentiate isConnected on event -- Only use HCIHandler's *connect*/disconnect -- disconnect(..) no more issues removeConnectedDevice, rely ion correct event issuing/handling, as performed in time @ mgmtEvDeviceDisconnectedHCI... -- remove() issues removeConnectedDevice and removeDiscoveredDevice pre releaseSharedInstance to ensure a clean tracking state. The whole purpose of this command. +++ Enhance native + java test After disconnect() wait (poll) until no more connected, before issuing remove(), this shall ensure proper workflow pre remove() - validation of disconnect command.
Diffstat (limited to 'src/direct_bt')
-rw-r--r--src/direct_bt/DBTAdapter.cpp108
-rw-r--r--src/direct_bt/DBTDevice.cpp73
-rw-r--r--src/direct_bt/DBTManager.cpp29
-rw-r--r--src/direct_bt/GATTHandler.cpp10
-rw-r--r--src/direct_bt/L2CAPComm.cpp6
5 files changed, 111 insertions, 115 deletions
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index 58a66968..ef31dcc0 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -136,13 +136,13 @@ bool DBTAdapter::validateDevInfo() {
}
adapterInfo = mgmt.getAdapterInfo(dev_id);
- mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DISCOVERING, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDiscoveringCB));
- mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::NEW_SETTINGS, bindMemberFunc(this, &DBTAdapter::mgmtEvNewSettingsCB));
- mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::LOCAL_NAME_CHANGED, bindMemberFunc(this, &DBTAdapter::mgmtEvLocalNameChangedCB));
- mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DEVICE_CONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceConnectedCB));
- mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DEVICE_DISCONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDisconnectedCB));
- mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DEVICE_FOUND, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceFoundCB));
-
+ mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DISCOVERING, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDiscoveringMgmt));
+ mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::NEW_SETTINGS, bindMemberFunc(this, &DBTAdapter::mgmtEvNewSettingsMgmt));
+ mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::LOCAL_NAME_CHANGED, bindMemberFunc(this, &DBTAdapter::mgmtEvLocalNameChangedMgmt));
+ mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DEVICE_FOUND, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceFoundMgmt));
+#ifdef VERBOSE_ON
+ mgmt.addMgmtEventCallback(dev_id, MgmtEvent::Opcode::DEVICE_DISCONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDisconnectedMgmt));
+#endif
return true;
}
@@ -220,8 +220,9 @@ std::shared_ptr<HCIHandler> DBTAdapter::openHCI()
return nullptr;
}
hci = std::shared_ptr<HCIHandler>( s );
- hci->addMgmtEventCallback(MgmtEvent::Opcode::DEVICE_CONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceConnectedCB));
- hci->addMgmtEventCallback(MgmtEvent::Opcode::DEVICE_DISCONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDisconnectedCB));
+ hci->addMgmtEventCallback(MgmtEvent::Opcode::DEVICE_CONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceConnectedHCI));
+ hci->addMgmtEventCallback(MgmtEvent::Opcode::CONNECT_FAILED, bindMemberFunc(this, &DBTAdapter::mgmtEvConnectFailedHCI));
+ hci->addMgmtEventCallback(MgmtEvent::Opcode::DEVICE_DISCONNECTED, bindMemberFunc(this, &DBTAdapter::mgmtEvDeviceDisconnectedHCI));
return hci;
}
@@ -452,7 +453,7 @@ std::string DBTAdapter::toString() const {
// *************************************************
-bool DBTAdapter::mgmtEvDeviceDiscoveringCB(std::shared_ptr<MgmtEvent> e) {
+bool DBTAdapter::mgmtEvDeviceDiscoveringMgmt(std::shared_ptr<MgmtEvent> e) {
DBG_PRINT("DBTAdapter::EventCB:DeviceDiscovering(dev_id %d, keepDiscoveringAlive %d): %s",
dev_id, keepDiscoveringAlive.load(), e->toString().c_str());
const MgmtEvtDiscovering &event = *static_cast<const MgmtEvtDiscovering *>(e.get());
@@ -479,7 +480,7 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringCB(std::shared_ptr<MgmtEvent> e) {
return true;
}
-bool DBTAdapter::mgmtEvNewSettingsCB(std::shared_ptr<MgmtEvent> e) {
+bool DBTAdapter::mgmtEvNewSettingsMgmt(std::shared_ptr<MgmtEvent> e) {
DBG_PRINT("DBTAdapter::EventCB:NewSettings: %s", e->toString().c_str());
const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(e.get());
AdapterSetting old_setting = adapterInfo->getCurrentSetting();
@@ -504,7 +505,7 @@ bool DBTAdapter::mgmtEvNewSettingsCB(std::shared_ptr<MgmtEvent> e) {
return true;
}
-bool DBTAdapter::mgmtEvLocalNameChangedCB(std::shared_ptr<MgmtEvent> e) {
+bool DBTAdapter::mgmtEvLocalNameChangedMgmt(std::shared_ptr<MgmtEvent> e) {
DBG_PRINT("DBTAdapter::EventCB:LocalNameChanged: %s", e->toString().c_str());
const MgmtEvtLocalNameChanged &event = *static_cast<const MgmtEvtLocalNameChanged *>(e.get());
std::string old_name = localName.getName();
@@ -541,7 +542,7 @@ void DBTAdapter::sendDeviceUpdated(std::string cause, std::shared_ptr<DBTDevice>
});
}
-bool DBTAdapter::mgmtEvDeviceConnectedCB(std::shared_ptr<MgmtEvent> e) {
+bool DBTAdapter::mgmtEvDeviceConnectedHCI(std::shared_ptr<MgmtEvent> e) {
const MgmtEvtDeviceConnected &event = *static_cast<const MgmtEvtDeviceConnected *>(e.get());
EInfoReport ad_report;
{
@@ -578,42 +579,81 @@ bool DBTAdapter::mgmtEvDeviceConnectedCB(std::shared_ptr<MgmtEvent> e) {
new_connect = 4; // unknown reason...
}
}
- if( 0 < new_connect ) {
+ if( 2 <= new_connect ) {
device->ts_last_discovery = ad_report.getTimestamp();
}
- DBG_PRINT("DBTAdapter::EventCB:DeviceConnected(dev_id %d, new_connect %d, updated %s): %s,\n %s\n -> %s",
- dev_id, new_connect, getEIRDataMaskString(updateMask).c_str(), event.toString().c_str(), ad_report.toString().c_str(), device->toString().c_str());
+ if( 0 == new_connect ) {
+ WARN_PRINT("DBTAdapter::EventHCI:DeviceConnected(dev_id %d, already connected, updated %s): %s,\n %s\n -> %s",
+ dev_id, getEIRDataMaskString(updateMask).c_str(), event.toString().c_str(), ad_report.toString().c_str(), device->toString().c_str());
+ } else {
+ DBG_PRINT("DBTAdapter::EventHCI:DeviceConnected(dev_id %d, new_connect %d, updated %s): %s,\n %s\n -> %s",
+ dev_id, new_connect, getEIRDataMaskString(updateMask).c_str(), event.toString().c_str(), ad_report.toString().c_str(), device->toString().c_str());
+ }
device->notifyConnected(event.getHCIHandle());
- if( EIRDataType::NONE != updateMask || 0 < new_connect ) {
+ int i=0;
+ for_each_idx_mtx(mtx_statusListenerList, statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) {
+ try {
+ if( l->matchDevice(*device) ) {
+ if( EIRDataType::NONE != updateMask ) {
+ l->deviceUpdated(device, updateMask, ad_report.getTimestamp());
+ }
+ if( 0 < new_connect ) {
+ l->deviceConnected(device, event.getTimestamp());
+ }
+ }
+ } catch (std::exception &e) {
+ ERR_PRINT("DBTAdapter::EventHCI:DeviceConnected-CBs %d/%zd: %s of %s: Caught exception %s",
+ i+1, statusListenerList.size(),
+ l->toString().c_str(), device->toString().c_str(), e.what());
+ }
+ i++;
+ });
+ return true;
+}
+
+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());
+ if( nullptr != device ) {
+ DBG_PRINT("DBTAdapter::EventHCI:ConnectFailed(dev_id %d): %s\n -> %s",
+ dev_id, event.toString().c_str(), device->toString().c_str());
+ device->notifyDisconnected();
+ removeConnectedDevice(*device);
+
int i=0;
for_each_idx_mtx(mtx_statusListenerList, statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) {
try {
if( l->matchDevice(*device) ) {
- if( EIRDataType::NONE != updateMask ) {
- l->deviceUpdated(device, updateMask, ad_report.getTimestamp());
- }
- if( 0 < new_connect ) {
- l->deviceConnected(device, event.getTimestamp());
- }
+ l->deviceDisconnected(device, event.getHCIStatus(), event.getTimestamp());
}
} catch (std::exception &e) {
- ERR_PRINT("DBTAdapter::EventCB:DeviceConnected-CBs %d/%zd: %s of %s: Caught exception %s",
+ ERR_PRINT("DBTAdapter::EventHCI:DeviceDisconnected-CBs %d/%zd: %s of %s: Caught exception %s",
i+1, statusListenerList.size(),
l->toString().c_str(), device->toString().c_str(), e.what());
}
i++;
});
+ removeDiscoveredDevice(*device); // ensure device will cause a deviceFound event after disconnect
+ } else {
+ DBG_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): %s\n -> Device not tracked",
+ dev_id, event.toString().c_str());
}
return true;
}
-bool DBTAdapter::mgmtEvDeviceDisconnectedCB(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());
if( nullptr != device ) {
- DBG_PRINT("DBTAdapter::EventCB:DeviceDisconnected(dev_id %d): %s\n -> %s",
+ if( device->getConnectionHandle() != event.getHCIHandle() ) {
+ INFO_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): ConnHandle mismatch %s\n -> %s",
+ dev_id, event.toString().c_str(), device->toString().c_str());
+ return true;
+ }
+ DBG_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): %s\n -> %s",
dev_id, event.toString().c_str(), device->toString().c_str());
device->notifyDisconnected();
removeConnectedDevice(*device);
@@ -625,7 +665,7 @@ bool DBTAdapter::mgmtEvDeviceDisconnectedCB(std::shared_ptr<MgmtEvent> e) {
l->deviceDisconnected(device, event.getHCIReason(), event.getTimestamp());
}
} catch (std::exception &e) {
- ERR_PRINT("DBTAdapter::EventCB:DeviceDisconnected-CBs %d/%zd: %s of %s: Caught exception %s",
+ ERR_PRINT("DBTAdapter::EventHCI:DeviceDisconnected-CBs %d/%zd: %s of %s: Caught exception %s",
i+1, statusListenerList.size(),
l->toString().c_str(), device->toString().c_str(), e.what());
}
@@ -633,13 +673,20 @@ bool DBTAdapter::mgmtEvDeviceDisconnectedCB(std::shared_ptr<MgmtEvent> e) {
});
removeDiscoveredDevice(*device); // ensure device will cause a deviceFound event after disconnect
} else {
- DBG_PRINT("DBTAdapter::EventCB:DeviceDisconnected(dev_id %d): %s\n -> Device not tracked",
+ INFO_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): %s\n -> Device not tracked",
dev_id, event.toString().c_str());
}
return true;
}
-bool DBTAdapter::mgmtEvDeviceFoundCB(std::shared_ptr<MgmtEvent> e) {
+bool DBTAdapter::mgmtEvDeviceDisconnectedMgmt(std::shared_ptr<MgmtEvent> e) {
+ DBG_PRINT("DBTAdapter::EventCB:DeviceDisconnected: %s", e->toString().c_str());
+ const MgmtEvtDeviceDisconnected &event = *static_cast<const MgmtEvtDeviceDisconnected *>(e.get());
+ (void)event;
+ return true;
+}
+
+bool DBTAdapter::mgmtEvDeviceFoundMgmt(std::shared_ptr<MgmtEvent> e) {
DBG_PRINT("DBTAdapter::EventCB:DeviceFound(dev_id %d): %s", dev_id, e->toString().c_str());
const MgmtEvtDeviceFound &deviceFoundEvent = *static_cast<const MgmtEvtDeviceFound *>(e.get());
@@ -658,6 +705,7 @@ bool DBTAdapter::mgmtEvDeviceFoundCB(std::shared_ptr<MgmtEvent> e) {
// existing device
//
EIRDataType updateMask = dev->update(ad_report);
+ INFO_PRINT("DBTAdapter::EventCB:SharedDeviceFound.1: Old Discovered %s", dev->getAddressString().c_str());
if( EIRDataType::NONE != updateMask ) {
sendDeviceUpdated("DiscoveredDeviceFound", dev, ad_report.getTimestamp(), updateMask);
}
@@ -675,6 +723,7 @@ bool DBTAdapter::mgmtEvDeviceFoundCB(std::shared_ptr<MgmtEvent> e) {
EIRDataType updateMask = dev->update(ad_report);
addDiscoveredDevice(dev); // re-add to discovered devices!
dev->ts_last_discovery = ad_report.getTimestamp();
+ DBG_PRINT("DBTAdapter::EventCB:SharedDeviceFound.2: Old Shared %s", dev->getAddressString().c_str());
int i=0;
for_each_idx_mtx(mtx_statusListenerList, statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) {
@@ -701,6 +750,7 @@ bool DBTAdapter::mgmtEvDeviceFoundCB(std::shared_ptr<MgmtEvent> e) {
dev = std::shared_ptr<DBTDevice>(new DBTDevice(*this, ad_report));
addDiscoveredDevice(dev);
addSharedDevice(dev);
+ DBG_PRINT("DBTAdapter::EventCB:SharedDeviceFound.3: All New %s", dev->getAddressString().c_str());
int i=0;
for_each_idx_mtx(mtx_statusListenerList, statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) {
diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp
index 676dce12..02412b49 100644
--- a/src/direct_bt/DBTDevice.cpp
+++ b/src/direct_bt/DBTDevice.cpp
@@ -48,6 +48,7 @@ DBTDevice::DBTDevice(DBTAdapter & a, EInfoReport const & r)
ts_last_discovery = ts_creation;
hciConnHandle = 0;
isConnected = false;
+ isConnectIssued = false;
if( !r.isSet(EIRDataType::BDADDR) ) {
throw IllegalArgumentException("DBTDevice ctor: Address not set: "+r.toString(), E_FILE_LINE);
}
@@ -68,7 +69,7 @@ std::shared_ptr<DBTDevice> DBTDevice::getSharedInstance() const {
return adapter.getSharedDevice(*this);
}
void DBTDevice::releaseSharedInstance() const {
- adapter.releaseSharedDevice(*this);
+ adapter.removeSharedDevice(*this);
}
bool DBTDevice::addService(std::shared_ptr<uuid_t> const &uuid)
@@ -123,7 +124,7 @@ std::string DBTDevice::toString(bool includeDiscoveredServices) const {
std::string msdstr = nullptr != msd ? msd->toString() : "MSD[null]";
std::string out("Device[address["+getAddressString()+", "+getBDAddressTypeString(getAddressType())+"], name['"+name+
"'], age[total "+std::to_string(t0-ts_creation)+", ldisc "+std::to_string(t0-ts_last_discovery)+", lup "+std::to_string(t0-ts_last_update)+
- "]ms, connected "+std::to_string(isConnected)+", rssi "+std::to_string(getRSSI())+
+ "]ms, connected["+std::to_string(isConnectIssued)+"/"+std::to_string(isConnected)+", "+uint16HexString(hciConnHandle)+"], rssi "+std::to_string(getRSSI())+
", tx-power "+std::to_string(tx_power)+
", appearance "+uint16HexString(static_cast<uint16_t>(appearance))+" ("+getAppearanceCatString(appearance)+
"), "+msdstr+", "+javaObjectToString()+"]");
@@ -264,15 +265,11 @@ bool DBTDevice::connectLE(HCIAddressType peer_mac_type, HCIAddressType own_mac_t
return false;
}
- {
- // Currently doing nothing, but notifying
- DBTManager & mngr = adapter.getManager();
- mngr.create_connection(adapter.dev_id, address, addressType);
- }
HCIStatusCode status = hci->le_create_conn(address,
peer_mac_type, own_mac_type,
le_scan_interval, le_scan_window, conn_interval_min, conn_interval_max,
conn_latency, supervision_timeout);
+ isConnectIssued = true;
#if 0
if( HCIStatusCode::CONNECTION_ALREADY_EXISTS == status ) {
INFO_PRINT("DBTDevice::connectLE: Connection already exists: status 0x%2.2X (%s) on %s",
@@ -317,13 +314,8 @@ bool DBTDevice::connectBREDR(const uint16_t pkt_type, const uint16_t clock_offse
return false;
}
- {
- // Currently doing nothing, but notifying
- DBTManager & mngr = adapter.getManager();
- mngr.create_connection(adapter.dev_id, address, addressType);
- }
-
HCIStatusCode status = hci->create_conn(address, pkt_type, clock_offset, role_switch);
+ isConnectIssued = true;
if ( HCIStatusCode::SUCCESS != status ) {
ERR_PRINT("DBTDevice::connectBREDR: Could not create connection: status 0x%2.2X (%s), errno %d %s on %s",
static_cast<uint8_t>(status), getHCIStatusCodeString(status).c_str(), errno, strerror(errno), toString().c_str());
@@ -350,30 +342,28 @@ bool DBTDevice::connectDefault()
void DBTDevice::notifyConnected(const uint16_t handle) {
DBG_PRINT("DBTDevice::notifyConnected: handle %s, %s", uint16HexString(handle).c_str(), toString().c_str());
isConnected = true;
- if( 0 != handle ) {
- hciConnHandle = handle;
- } else {
- hciConnHandle = 0xffff; // dummy, as handle is not supported by manager
- }
+ hciConnHandle = handle;
}
void DBTDevice::notifyDisconnected() {
DBG_PRINT("DBTDevice::notifyDisconnected: %s", toString().c_str());
try {
- // coming from manager disconnect, ensure cleaning up!
- disconnect(true /* sentFromManager */, false /* ioErrorCause */);
+ // coming from disconnect callback, ensure cleaning up!
+ disconnect(true /* fromDisconnectCB */, false /* ioErrorCause */);
} catch (std::exception &e) {
ERR_PRINT("Exception caught on %s: %s", toString().c_str(), e.what());
}
isConnected = false;
+ hciConnHandle = 0;
}
-bool DBTDevice::disconnect(const bool sentFromManager, const bool ioErrorCause, const HCIStatusCode reason) {
+bool DBTDevice::disconnect(const bool fromDisconnectCB, const bool ioErrorCause, const HCIStatusCode reason) {
const std::lock_guard<std::recursive_mutex> lock_conn(mtx_connect); // RAII-style acquire and relinquish via destructor
- INFO_PRINT("DBTDevice::disconnect: Start: isConnected %d, sentFromManager %d, ioError %d, reason 0x%X (%s), gattHandler %d, hciConnHandle %d",
- isConnected.load(), sentFromManager, ioErrorCause, static_cast<uint8_t>(reason), getHCIStatusCodeString(reason).c_str(),
- (nullptr != gattHandler), (0 != hciConnHandle));
+ INFO_PRINT("DBTDevice::disconnect: Start: isConnected %d/%d, fromDisconnectCB %d, ioError %d, reason 0x%X (%s), gattHandler %d, hciConnHandle %s",
+ isConnectIssued.load(), isConnected.load(), fromDisconnectCB, ioErrorCause,
+ static_cast<uint8_t>(reason), getHCIStatusCodeString(reason).c_str(),
+ (nullptr != gattHandler), uint16HexString(hciConnHandle).c_str());
disconnectGATT();
bool res = false;
@@ -381,51 +371,36 @@ bool DBTDevice::disconnect(const bool sentFromManager, const bool ioErrorCause,
const std::lock_guard<std::recursive_mutex> lock_hci(adapter.mtx_hci); // RAII-style acquire and relinquish via destructor
std::shared_ptr<HCIHandler> hci = adapter.getHCI();
- if( !isConnected ) {
- DBG_PRINT("DBTDevice::disconnect: Skip disconnect: Not connected: %s", toString().c_str());
+ if( !isConnected || !isConnectIssued ) {
goto exit;
}
- isConnected = false;
+ isConnectIssued = false;
- if( ioErrorCause ) {
- DBG_PRINT("DBTDevice::disconnect: Skip HCI disconnect: IO Error: %s", toString().c_str());
- goto skip_hci_disconnect;
- }
-
- if( 0 == hciConnHandle ) {
- DBG_PRINT("DBTDevice::disconnect: Skip HCI disconnect: HCI not connected: %s", toString().c_str());
- goto skip_hci_disconnect;
+ if( fromDisconnectCB || 0 == hciConnHandle ) {
+ goto exit;
}
if( nullptr == hci || !hci->isOpen() ) {
DBG_PRINT("DBTDevice::disconnect: Skip HCI disconnect: HCI not Open: %s", toString().c_str());
- goto skip_hci_disconnect;
+ goto exit;
}
- if( HCIStatusCode::SUCCESS != hci->disconnect(hciConnHandle.load(), address, addressType, reason) ) {
+ if( HCIStatusCode::SUCCESS != hci->disconnect(ioErrorCause, hciConnHandle.load(), address, addressType, reason) ) {
DBG_PRINT("DBTDevice::disconnect: handle 0x%X, errno %d %s", hciConnHandle.load(), errno, strerror(errno));
} else {
res = true;
}
-skip_hci_disconnect:
- hciConnHandle = 0;
-
- if( !sentFromManager ) {
- // Also issue mngr.disconnect on non-HCI connect (whitelist),
- // which will also send the DISCONNECT event.
- DBTManager & mngr = adapter.getManager();
- res = mngr.disconnect(ioErrorCause, adapter.dev_id, address, addressType, reason) || res;
- }
-
exit:
- adapter.removeConnectedDevice(*this);
- INFO_PRINT("DBTDevice::disconnect: End: isConnected %d, sentFromManager %d, ioError %d", isConnected.load(), sentFromManager, ioErrorCause);
+ INFO_PRINT("DBTDevice::disconnect: End: isConnected %d/%d, fromDisconnectCB %d, ioError %d",
+ isConnectIssued.load(), isConnected.load(), fromDisconnectCB, ioErrorCause);
return res;
}
void DBTDevice::remove() {
disconnect();
+ adapter.removeConnectedDevice(*this); // usually done in DBTAdapter::mgmtEvDeviceDisconnectedHCI
+ adapter.removeDiscoveredDevice(*this); // usually done in DBTAdapter::mgmtEvDeviceDisconnectedHCI
releaseSharedInstance();
}
diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp
index e1dc256d..a5fa8865 100644
--- a/src/direct_bt/DBTManager.cpp
+++ b/src/direct_bt/DBTManager.cpp
@@ -621,35 +621,6 @@ bool DBTManager::removeDeviceFromWhitelist(const int dev_id, const EUI48 &addres
return false;
}
-uint16_t DBTManager::create_connection(const int dev_id,
- const EUI48 &peer_bdaddr,
- const BDAddressType peer_mac_type,
- const BDAddressType own_mac_type,
- const uint16_t interval, const uint16_t window,
- const uint16_t min_interval, const uint16_t max_interval,
- const uint16_t latency, const uint16_t supervision_timeout) {
- // MgmtUint8Cmd req(MgmtOpcode::, dev_id, scanType);
- DBG_PRINT("DBTManager::le_create_conn: %s", peer_bdaddr.toString().c_str());
- const uint16_t min_ce_length=0x0001;
- const uint16_t max_ce_length=0x0001;
- const uint8_t initiator_filter=0;
-
- (void)dev_id;
- (void)peer_bdaddr;
- (void)peer_mac_type;
- (void)own_mac_type;
- (void)interval;
- (void)window;
- (void)min_interval;
- (void)max_interval;
- (void)latency;
- (void)supervision_timeout;
- (void)min_ce_length;
- (void)max_ce_length;
- (void)initiator_filter;
- return 0;
-}
-
bool DBTManager::disconnect(const bool ioErrorCause,
const int dev_id, const EUI48 &peer_bdaddr, const BDAddressType peer_mac_type,
const HCIStatusCode reason) {
diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp
index 2f393c82..3252deb2 100644
--- a/src/direct_bt/GATTHandler.cpp
+++ b/src/direct_bt/GATTHandler.cpp
@@ -308,14 +308,14 @@ bool GATTHandler::disconnect(const bool disconnectDevice, const bool ioErrorCaus
bool expConn = true; // C++11, exp as value since C++20
if( !isConnected.compare_exchange_strong(expConn, false) ) {
// not connected
- INFO_PRINT("GATTHandler::disconnect: Not connected: disconnectDevice %d, ioErrorCause %d: GattHandler[%s], l2cap[%s]: %s",
- disconnectDevice, ioErrorCause, getStateString().c_str(), l2cap.getStateString().c_str(), deviceString.c_str());
+ DBG_PRINT("GATTHandler::disconnect: Not connected: disconnectDevice %d, ioErrorCause %d: GattHandler[%s], l2cap[%s]: %s",
+ disconnectDevice, ioErrorCause, getStateString().c_str(), l2cap.getStateString().c_str(), deviceString.c_str());
l2cap.disconnect(); // interrupt GATT's L2CAP ::connect(..), avoiding prolonged hang
return false;
}
hasIOError = false;
- INFO_PRINT("GATTHandler::disconnect: Start: disconnectDevice %d, ioErrorCause %d: GattHandler[%s], l2cap[%s]: %s",
- disconnectDevice, ioErrorCause, getStateString().c_str(), l2cap.getStateString().c_str(), deviceString.c_str());
+ DBG_PRINT("GATTHandler::disconnect: Start: disconnectDevice %d, ioErrorCause %d: GattHandler[%s], l2cap[%s]: %s",
+ disconnectDevice, ioErrorCause, getStateString().c_str(), l2cap.getStateString().c_str(), deviceString.c_str());
l2cap.disconnect(); // interrupt GATT's L2CAP ::connect(..), avoiding prolonged hang
@@ -344,7 +344,7 @@ bool GATTHandler::disconnect(const bool disconnectDevice, const bool ioErrorCaus
device->disconnect(false /* sentFromManager */, ioErrorCause, reason);
}
- INFO_PRINT("GATTHandler::disconnect: End: %s", deviceString.c_str());
+ DBG_PRINT("GATTHandler::disconnect: End: %s", deviceString.c_str());
return true;
}
diff --git a/src/direct_bt/L2CAPComm.cpp b/src/direct_bt/L2CAPComm.cpp
index fe98e95d..d974d35a 100644
--- a/src/direct_bt/L2CAPComm.cpp
+++ b/src/direct_bt/L2CAPComm.cpp
@@ -176,12 +176,12 @@ failure:
bool L2CAPComm::disconnect() {
bool expConn = true; // C++11, exp as value since C++20
if( !isConnected.compare_exchange_strong(expConn, false) ) {
- INFO_PRINT("L2CAPComm::disconnect: Not connected: %s, dd %d, %s, psm %u, cid %u, pubDevice %d",
+ DBG_PRINT("L2CAPComm::disconnect: Not connected: %s, dd %d, %s, psm %u, cid %u, pubDevice %d",
getStateString().c_str(), _dd.load(), deviceString.c_str(), psm, cid, pubaddr);
return false;
}
hasIOError = false;
- INFO_PRINT("L2CAPComm::disconnect: Start: %s, dd %d, %s, psm %u, cid %u, pubDevice %d",
+ DBG_PRINT("L2CAPComm::disconnect: Start: %s, dd %d, %s, psm %u, cid %u, pubDevice %d",
getStateString().c_str(), _dd.load(), deviceString.c_str(), psm, cid, pubaddr);
interruptFlag = true;
@@ -201,7 +201,7 @@ bool L2CAPComm::disconnect() {
l2cap_close_dev(_dd);
_dd = -1;
interruptFlag = false;
- INFO_PRINT("L2CAPComm::disconnect: End: dd %d", _dd.load());
+ DBG_PRINT("L2CAPComm::disconnect: End: dd %d", _dd.load());
return true;
}