diff options
author | Sven Gothel <[email protected]> | 2020-06-26 07:35:38 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-26 07:35:38 +0200 |
commit | 25df14701cc9b43ee656beab915de619c2054a3f (patch) | |
tree | 8045efbdddb8fcf8501224f6486963fe6e89261b | |
parent | aa912027163e577336b235220a8a237bd466b2da (diff) |
L2CAPComm/GATTHandler: Use prefetched 'deviceString' for verbose output
This potentially avoid a SIGSEGV in case of an error, i.e. destructed device instance.
-rw-r--r-- | api/direct_bt/L2CAPComm.hpp | 6 | ||||
-rw-r--r-- | src/direct_bt/GATTHandler.cpp | 10 | ||||
-rw-r--r-- | src/direct_bt/L2CAPComm.cpp | 15 |
3 files changed, 17 insertions, 14 deletions
diff --git a/api/direct_bt/L2CAPComm.hpp b/api/direct_bt/L2CAPComm.hpp index 8e79085f..0f2e484f 100644 --- a/api/direct_bt/L2CAPComm.hpp +++ b/api/direct_bt/L2CAPComm.hpp @@ -65,6 +65,7 @@ namespace direct_bt { static int l2cap_close_dev(int dd); std::shared_ptr<DBTDevice> device; + const std::string deviceString; const uint16_t psm; const uint16_t cid; const bool pubaddr; @@ -75,10 +76,7 @@ namespace direct_bt { std::atomic<pthread_t> tid_connect; public: - L2CAPComm(std::shared_ptr<DBTDevice> device, const uint16_t psm, const uint16_t cid, const bool pubaddr=true) - : device(device), psm(psm), cid(cid), pubaddr(pubaddr), - _dd(-1), isConnected(false), hasIOError(false), interruptFlag(false), tid_connect(0) {} - ~L2CAPComm() { disconnect(); } + L2CAPComm(std::shared_ptr<DBTDevice> device, const uint16_t psm, const uint16_t cid, const bool pubaddr=true); std::shared_ptr<DBTDevice> getDevice() { return device; } diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp index f44feedf..2f393c82 100644 --- a/src/direct_bt/GATTHandler.cpp +++ b/src/direct_bt/GATTHandler.cpp @@ -263,12 +263,12 @@ bool GATTHandler::connect() { if( !isConnected.compare_exchange_strong(expConn, true) ) { // already connected INFO_PRINT("GATTHandler::connect: Already connected: GattHandler[%s], l2cap[%s]: %s", - getStateString().c_str(), l2cap.getStateString().c_str(), device->toString().c_str()); + getStateString().c_str(), l2cap.getStateString().c_str(), deviceString.c_str()); return true; } hasIOError = false; INFO_PRINT("GATTHandler::connect: Start: GattHandler[%s], l2cap[%s]: %s", - getStateString().c_str(), l2cap.getStateString().c_str(), device->toString().c_str()); + getStateString().c_str(), l2cap.getStateString().c_str(), deviceString.c_str()); if( !l2cap.connect() || !validateConnected() ) { DBG_PRINT("GATTHandler.connect: Could not connect"); @@ -309,13 +309,13 @@ bool GATTHandler::disconnect(const bool disconnectDevice, const bool ioErrorCaus 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(), device->toString().c_str()); + 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(), device->toString().c_str()); + 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"); + INFO_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 e5e5fce1..c5e06891 100644 --- a/src/direct_bt/L2CAPComm.cpp +++ b/src/direct_bt/L2CAPComm.cpp @@ -97,18 +97,23 @@ int L2CAPComm::l2cap_close_dev(int dd) // ************************************************* // ************************************************* +L2CAPComm::L2CAPComm(std::shared_ptr<DBTDevice> device, const uint16_t psm, const uint16_t cid, const bool pubaddr=true) +: device(device), deviceString(device->getAddressString()), psm(psm), cid(cid), pubaddr(pubaddr), + _dd(-1), isConnected(false), hasIOError(false), interruptFlag(false), tid_connect(0) +{ } + bool L2CAPComm::connect() { /** BT Core Spec v5.2: Vol 3, Part A: L2CAP_CONNECTION_REQ */ bool expConn = false; // C++11, exp as value since C++20 if( !isConnected.compare_exchange_strong(expConn, true) ) { // already connected DBG_PRINT("L2CAPComm::connect: Already connected: %s, dd %d, %s, psm %u, cid %u, pubDevice %d", - getStateString().c_str(), _dd.load(), device->getAddress().toString().c_str(), psm, cid, pubaddr); + getStateString().c_str(), _dd.load(), deviceString.c_str(), psm, cid, pubaddr); return true; } hasIOError = false; DBG_PRINT("L2CAPComm::connect: Start: %s, dd %d, %s, psm %u, cid %u, pubDevice %d", - getStateString().c_str(), _dd.load(), device->getAddress().toString().c_str(), psm, cid, pubaddr); + getStateString().c_str(), _dd.load(), deviceString.c_str(), psm, cid, pubaddr); sockaddr_l2 req; int err, res; @@ -134,7 +139,7 @@ bool L2CAPComm::connect() { res = ::connect(_dd, (struct sockaddr*)&req, sizeof(req)); DBG_PRINT("L2CAPComm::connect: Result %d, errno 0%X %s, %s", - res, errno, strerror(errno), device->getAddress().toString().c_str()); + res, errno, strerror(errno), deviceString.c_str().c_str()); if( !res ) { @@ -172,12 +177,12 @@ 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", - getStateString().c_str(), _dd.load(), device->getAddress().toString().c_str(), psm, cid, pubaddr); + 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", - getStateString().c_str(), _dd.load(), device->getAddress().toString().c_str(), psm, cid, pubaddr); + getStateString().c_str(), _dd.load(), deviceString.c_str(), psm, cid, pubaddr); interruptFlag = true; // interrupt L2CAP ::connect(..), avoiding prolonged hang |