diff options
author | Sven Gothel <[email protected]> | 2020-06-24 23:05:12 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-24 23:05:12 +0200 |
commit | 6c02b9355224f7af24c7547d00d7d546cd60f87b (patch) | |
tree | ebb9945e3c5a38969f8c63d14bd33801d5842457 /api/direct_bt | |
parent | 9764a6b07cfa55ee39a41a02d3ac1577736b0124 (diff) |
L2CAP/GATT: Replace State with dedicated atomic<bool> isConnected and hasIOError, allowing atomic compare_exchange_strong w/o mutex
Using an atomic threshold via compare_exchange_strong allows us to avoid using a mutex and ease complexity.
The State had been reduced to a tri-state earlier, hence the atomic<bool> tuple is sufficient.
Diffstat (limited to 'api/direct_bt')
-rw-r--r-- | api/direct_bt/GATTHandler.hpp | 12 | ||||
-rw-r--r-- | api/direct_bt/L2CAPComm.hpp | 24 |
2 files changed, 18 insertions, 18 deletions
diff --git a/api/direct_bt/GATTHandler.hpp b/api/direct_bt/GATTHandler.hpp index 67c932fd..253077d0 100644 --- a/api/direct_bt/GATTHandler.hpp +++ b/api/direct_bt/GATTHandler.hpp @@ -89,7 +89,8 @@ namespace direct_bt { L2CAPComm l2cap; const int replyTimeoutMS; - std::atomic<L2CAPComm::State> state; + std::atomic<bool> isConnected; // reflects state + std::atomic<bool> hasIOError; // reflects state LFRingbuffer<std::shared_ptr<const AttPDUMsg>, nullptr> attPDURing; std::atomic<pthread_t> l2capReaderThreadId; @@ -107,7 +108,7 @@ namespace direct_bt { uint16_t usedMTU; std::vector<GATTServiceRef> services; - L2CAPComm::State validateState(); + bool validateConnected(); void l2capReaderThreadImpl(); @@ -127,8 +128,9 @@ namespace direct_bt { ~GATTHandler(); - L2CAPComm::State getState() const { return state; } - std::string getStateString() const { return L2CAPComm::getStateString(state); } + bool getIsConnected() const { return isConnected; } + bool getHasIOError() const { return hasIOError; } + std::string getStateString() const { return L2CAPComm::getStateString(isConnected, hasIOError); } /** * After successful l2cap connection, the MTU will be exchanged. @@ -144,7 +146,7 @@ namespace direct_bt { */ bool disconnect(const bool disconnectDevice, const bool ioErrorCause); - bool isOpen() const { return L2CAPComm::State::Disconnected < state && l2cap.isOpen(); } + bool isOpen() const { return isConnected && l2cap.isOpen(); } uint16_t getServerMTU() const { return serverMTU; } uint16_t getUsedMTU() const { return usedMTU; } diff --git a/api/direct_bt/L2CAPComm.hpp b/api/direct_bt/L2CAPComm.hpp index b8dd1fcc..8e79085f 100644 --- a/api/direct_bt/L2CAPComm.hpp +++ b/api/direct_bt/L2CAPComm.hpp @@ -51,19 +51,15 @@ namespace direct_bt { class L2CAPComm { public: - enum class State : int { - Error = -1, - Disconnected = 0, - Connected = 1, - }; - - static std::string getStateString(const State state); - enum class Defaults : int { L2CAP_CONNECT_MAX_RETRY = 3 }; static inline int number(const Defaults d) { return static_cast<int>(d); } + static std::string getStateString(bool isConnected, bool hasIOError) { + return "State[connected "+std::to_string(isConnected)+", ioError "+std::to_string(hasIOError)+"]"; + } + private: static int l2cap_open_dev(const EUI48 & adapterAddress, const uint16_t psm, const uint16_t cid, const bool pubaddr); static int l2cap_close_dev(int dd); @@ -72,24 +68,26 @@ namespace direct_bt { const uint16_t psm; const uint16_t cid; const bool pubaddr; - std::atomic<State> state; std::atomic<int> _dd; // the l2cap socket + std::atomic<bool> isConnected; // reflects state + std::atomic<bool> hasIOError; // reflects state std::atomic<bool> interruptFlag; // for forced disconnect 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), - state(State::Disconnected), _dd(-1), interruptFlag(false), tid_connect(0) {} + _dd(-1), isConnected(false), hasIOError(false), interruptFlag(false), tid_connect(0) {} ~L2CAPComm() { disconnect(); } std::shared_ptr<DBTDevice> getDevice() { return device; } - State getState() const { return state; } - std::string getStateString() const { return getStateString(state); } + bool getIsConnected() const { return isConnected; } + bool getHasIOError() const { return hasIOError; } + std::string getStateString() const { return getStateString(isConnected, hasIOError); } /** BT Core Spec v5.2: Vol 3, Part A: L2CAP_CONNECTION_REQ */ - State connect(); + bool connect(); bool disconnect(); |