aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2022-04-16 05:36:03 +0200
committerSven Gothel <[email protected]>2022-04-16 05:36:03 +0200
commit26db867ddf679db3da6555176070b08e032d241f (patch)
tree754e799fb88344bccfa62313dfa2921435d79d12 /src
parent0d3252c5b7996d0d0583e886eac102b23d39aa64 (diff)
BTGattHandler::disconnect(): Deref weak BTDevice owner at start to bail out if dtor - or - keep it alive for l2cap lifecycle etc
Diffstat (limited to 'src')
-rw-r--r--src/direct_bt/BTGattHandler.cpp22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/direct_bt/BTGattHandler.cpp b/src/direct_bt/BTGattHandler.cpp
index cd23c772..aba8bf0b 100644
--- a/src/direct_bt/BTGattHandler.cpp
+++ b/src/direct_bt/BTGattHandler.cpp
@@ -615,9 +615,16 @@ std::string BTGattHandler::getStateString() const noexcept {
}
bool BTGattHandler::disconnect(const bool disconnectDevice, const bool ioErrorCause) noexcept {
+ BTDeviceRef device = getDeviceUnchecked();
+ if( nullptr == device ) {
+ // If the device has been pulled already, so its l2cap instance.
+ ERR_PRINT("BTDevice null");
+ return false;
+ }
PERF3_TS_T0();
// Interrupt GATT's L2CAP::connect(..) and L2CAP::read(..), avoiding prolonged hang
// and pull all underlying l2cap read operations!
+ // l2cap is owned by BTDevice.
l2cap.close();
// Avoid disconnect re-entry -> potential deadlock
@@ -652,15 +659,12 @@ bool BTGattHandler::disconnect(const bool disconnectDevice, const bool ioErrorCa
l2cap_service_stop_res, disconnectDevice, toString().c_str());
if( disconnectDevice ) {
- BTDeviceRef device = getDeviceUnchecked();
- if( nullptr != device ) {
- // Cleanup device resources, proper connection state
- // Intentionally giving the POWER_OFF reason for the device in case of ioErrorCause!
- const HCIStatusCode reason = ioErrorCause ?
- HCIStatusCode::REMOTE_DEVICE_TERMINATED_CONNECTION_POWER_OFF :
- HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION;
- device->disconnect(reason);
- }
+ // Cleanup device resources, proper connection state
+ // Intentionally giving the POWER_OFF reason for the device in case of ioErrorCause!
+ const HCIStatusCode reason = ioErrorCause ?
+ HCIStatusCode::REMOTE_DEVICE_TERMINATED_CONNECTION_POWER_OFF :
+ HCIStatusCode::REMOTE_USER_TERMINATED_CONNECTION;
+ device->disconnect(reason);
}
return true;
}