aboutsummaryrefslogtreecommitdiffstats
path: root/api/direct_bt
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-06-09 07:22:37 +0200
committerSven Gothel <[email protected]>2020-06-09 07:22:37 +0200
commitf881c9a048b752cdd9a695dafd83003a8be2d0b0 (patch)
tree82c075cf29b986ccbba0985689d8bc7093cc598c /api/direct_bt
parente4956ec63fbcb96d57195a0a4243f4c36b22ee8b (diff)
L2CAPComm/GATTHandler: User 'enum class' constants for type safety; Drop GATTHandler::State for L2CAPComm::State
Diffstat (limited to 'api/direct_bt')
-rw-r--r--api/direct_bt/DBTDevice.hpp2
-rw-r--r--api/direct_bt/GATTHandler.hpp35
-rw-r--r--api/direct_bt/L2CAPComm.hpp6
3 files changed, 15 insertions, 28 deletions
diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp
index 9dd27ebe..c915ae18 100644
--- a/api/direct_bt/DBTDevice.hpp
+++ b/api/direct_bt/DBTDevice.hpp
@@ -267,7 +267,7 @@ namespace direct_bt {
* May return nullptr if not connected or failure.
* </p>
*/
- std::shared_ptr<GATTHandler> connectGATT(int replyTimeoutMS=GATTHandler::Defaults::L2CAP_COMMAND_REPLY_TIMEOUT);
+ std::shared_ptr<GATTHandler> connectGATT(int replyTimeoutMS=GATTHandler::number(GATTHandler::Defaults::L2CAP_COMMAND_REPLY_TIMEOUT));
/** Returns already opened GATTHandler, see connectGATT(..) and disconnectGATT(). */
std::shared_ptr<GATTHandler> getGATTHandler();
diff --git a/api/direct_bt/GATTHandler.hpp b/api/direct_bt/GATTHandler.hpp
index a915eec8..45fae8ed 100644
--- a/api/direct_bt/GATTHandler.hpp
+++ b/api/direct_bt/GATTHandler.hpp
@@ -64,24 +64,12 @@ namespace direct_bt {
*/
class GATTHandler {
public:
- enum State : int {
- Error = -1,
- Disconnected = 0,
- Connecting = 1,
- Connected = 2,
- RequestInProgress = 3,
- DiscoveringCharacteristics = 4,
- GetClientCharaceristicConfiguration = 5,
- WaitWriteResponse = 6,
- WaitReadResponse = 7
- };
-
- enum Defaults : int {
+ enum class Defaults : int {
/* BT Core Spec v5.2: Vol 3, Part F 3.2.8: Maximum length of an attribute value. */
- ClientMaxMTU = 512,
+ MAX_ATT_MTU = 512,
/* BT Core Spec v5.2: Vol 3, Part G GATT: 5.2.1 ATT_MTU */
- DEFAULT_MIN_ATT_MTU = 23,
+ MIN_ATT_MTU = 23,
/** 3s poll timeout for l2cap reader thread */
L2CAP_READER_THREAD_POLL_TIMEOUT = 3000,
@@ -90,8 +78,7 @@ namespace direct_bt {
ATTPDU_RING_CAPACITY = 128
};
-
- static std::string getStateString(const State state);
+ static inline int number(const Defaults d) { return static_cast<int>(d); }
private:
std::shared_ptr<DBTDevice> device;
@@ -101,7 +88,7 @@ namespace direct_bt {
L2CAPComm l2cap;
const int replyTimeoutMS;
- std::atomic<State> state;
+ std::atomic<L2CAPComm::State> state;
LFRingbuffer<std::shared_ptr<const AttPDUMsg>, nullptr> attPDURing;
std::atomic<pthread_t> l2capReaderThreadId;
@@ -117,7 +104,7 @@ namespace direct_bt {
uint16_t usedMTU;
std::vector<GATTServiceRef> services;
- State validateState();
+ L2CAPComm::State validateState();
void l2capReaderThreadImpl();
@@ -130,17 +117,17 @@ namespace direct_bt {
* Returns the server-mtu if successful, otherwise 0.
* </p>
*/
- uint16_t exchangeMTU(const uint16_t clientMaxMTU=ClientMaxMTU);
+ uint16_t exchangeMTU(const uint16_t clientMaxMTU=number(Defaults::MAX_ATT_MTU));
bool disconnect(const bool ioErrorCause);
public:
- GATTHandler(const std::shared_ptr<DBTDevice> & device, const int replyTimeoutMS = Defaults::L2CAP_COMMAND_REPLY_TIMEOUT);
+ GATTHandler(const std::shared_ptr<DBTDevice> & device, const int replyTimeoutMS = number(Defaults::L2CAP_COMMAND_REPLY_TIMEOUT));
~GATTHandler();
- State getState() const { return state; }
- std::string getStateString() const { return getStateString(state); }
+ L2CAPComm::State getState() const { return state; }
+ std::string getStateString() const { return L2CAPComm::getStateString(state); }
/**
* After successful l2cap connection, the MTU will be exchanged.
@@ -150,7 +137,7 @@ namespace direct_bt {
bool disconnect() {
return disconnect(false /* ioErrorCause */);
}
- bool isOpen() const { return Disconnected < state && l2cap.isOpen(); }
+ bool isOpen() const { return L2CAPComm::State::Disconnected < state && 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 686c6395..80e27e6e 100644
--- a/api/direct_bt/L2CAPComm.hpp
+++ b/api/direct_bt/L2CAPComm.hpp
@@ -51,7 +51,7 @@ namespace direct_bt {
class L2CAPComm {
public:
- enum State : int {
+ enum class State : int {
Error = -1,
Disconnected = 0,
Connecting = 1,
@@ -71,13 +71,13 @@ namespace direct_bt {
const bool blocking;
std::atomic<State> state;
std::atomic<int> _dd; // the l2cap socket
- std::atomic<bool> interruptReadFlag; // for forced disconnect
+ 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, const bool blocking=true)
: device(device), psm(psm), cid(cid), pubaddr(pubaddr), blocking(blocking),
- state(Disconnected), _dd(-1), interruptReadFlag(false), tid_connect(0) {}
+ state(State::Disconnected), _dd(-1), interruptFlag(false), tid_connect(0) {}
~L2CAPComm() { disconnect(); }
std::shared_ptr<DBTDevice> getDevice() { return device; }