diff options
author | Sven Gothel <[email protected]> | 2020-08-27 23:18:30 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-08-27 23:18:30 +0200 |
commit | 15686be6e6a53f266515bb4bd3f88cd7e7372512 (patch) | |
tree | 937823a211691a3e0cd8da56c8c815357fe60b22 /api/direct_bt | |
parent | 59a333efa97cf5e7fbc57ed011f06a81b90f48f9 (diff) |
DBTManager, HCIHandler, GATTHandler: Have all timeout+ config values read from environment via DBTEnv
Only default value change:
- GATTHandler::L2CAP_READER_THREAD_POLL_TIMEOUT: 3s -> 10s (aligning w/ others)
- GATTHandler::GATT_WRITE_COMMAND_REPLY_TIMEOUT: Back to 500ms default like GATT_READ_COMMAND_REPLY_TIMEOUT
Using int32_t timeout, i.e. 31 bits suitable for extreme maximum.
Diffstat (limited to 'api/direct_bt')
-rw-r--r-- | api/direct_bt/DBTDevice.hpp | 2 | ||||
-rw-r--r-- | api/direct_bt/DBTEnv.hpp | 22 | ||||
-rw-r--r-- | api/direct_bt/DBTManager.hpp | 16 | ||||
-rw-r--r-- | api/direct_bt/GATTHandler.hpp | 33 | ||||
-rw-r--r-- | api/direct_bt/HCIComm.hpp | 9 | ||||
-rw-r--r-- | api/direct_bt/HCIHandler.hpp | 27 | ||||
-rw-r--r-- | api/direct_bt/HCITypes.hpp | 6 | ||||
-rw-r--r-- | api/direct_bt/L2CAPComm.hpp | 2 |
8 files changed, 67 insertions, 50 deletions
diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp index fe9fc24c..85a217ef 100644 --- a/api/direct_bt/DBTDevice.hpp +++ b/api/direct_bt/DBTDevice.hpp @@ -346,7 +346,7 @@ namespace direct_bt { * May return nullptr if not connected or failure. * </p> */ - std::shared_ptr<GATTHandler> connectGATT(int replyTimeoutMS=GATTHandler::number(GATTHandler::Defaults::GATT_READ_COMMAND_REPLY_TIMEOUT)); + std::shared_ptr<GATTHandler> connectGATT(); /** Returns already opened GATTHandler, see connectGATT(..) and disconnectGATT(). */ std::shared_ptr<GATTHandler> getGATTHandler(); diff --git a/api/direct_bt/DBTEnv.hpp b/api/direct_bt/DBTEnv.hpp index 7e28c2fb..c87a0e8c 100644 --- a/api/direct_bt/DBTEnv.hpp +++ b/api/direct_bt/DBTEnv.hpp @@ -93,6 +93,28 @@ namespace direct_bt { */ static bool getBooleanProperty(const std::string & name, const bool default_value); + /** + * Returns the int32_t value of the environment's variable 'name', + * or the 'default_value' if the environment variable's value is null + * or not within int32_t value range. + * <p> + * Implementation uses getProperty(const char *name). + * </p> + */ + static int32_t getInt32Property(const std::string & name, const int32_t default_value, + const int32_t min_allowed=INT32_MIN, const int32_t max_allowed=INT32_MAX); + + /** + * Returns the uint32_t value of the environment's variable 'name', + * or the 'default_value' if the environment variable's value is null + * or not within uint32_t value range. + * <p> + * Implementation uses getProperty(const char *name). + * </p> + */ + static uint32_t getUint32Property(const std::string & name, const uint32_t default_value, + const uint32_t min_allowed=0, const uint32_t max_allowed=UINT32_MAX); + static DBTEnv& get() { /** * Thread safe starting with C++11 6.7: diff --git a/api/direct_bt/DBTManager.hpp b/api/direct_bt/DBTManager.hpp index f5e75d7a..c5a7de18 100644 --- a/api/direct_bt/DBTManager.hpp +++ b/api/direct_bt/DBTManager.hpp @@ -59,18 +59,18 @@ namespace direct_bt { */ class DBTManager : public JavaUplink { public: - enum Defaults : int { + enum Defaults : int32_t { /* BT Core Spec v5.2: Vol 3, Part F 3.2.8: Maximum length of an attribute value. */ ClientMaxMTU = 512, - - /** 10s poll timeout for mgmt reader thread */ - MGMT_READER_THREAD_POLL_TIMEOUT = 10000, - /** 3s timeout for mgmt command replies */ - MGMT_COMMAND_REPLY_TIMEOUT = 3000, - /** Small ringbuffer capacity for synchronized commands */ - MGMTEVT_RING_CAPACITY = 64 }; + /** Poll timeout for mgmt reader thread, defaults to 10s. */ + static const int32_t MGMT_READER_THREAD_POLL_TIMEOUT; + /** Timeout for mgmt command replies, defaults to 3s. */ + static const int32_t MGMT_COMMAND_REPLY_TIMEOUT; + /** Small ringbuffer capacity for synchronized commands, defaults to 64 messages. */ + static const int32_t MGMTEVT_RING_CAPACITY; + static const pid_t pidSelf; private: diff --git a/api/direct_bt/GATTHandler.hpp b/api/direct_bt/GATTHandler.hpp index 0994b142..1d2f2209 100644 --- a/api/direct_bt/GATTHandler.hpp +++ b/api/direct_bt/GATTHandler.hpp @@ -70,27 +70,27 @@ namespace direct_bt { */ class GATTHandler { public: - enum class Defaults : int { + enum class Defaults : int32_t { /* BT Core Spec v5.2: Vol 3, Part F 3.2.8: Maximum length of an attribute value. */ MAX_ATT_MTU = 512, /* BT Core Spec v5.2: Vol 3, Part G GATT: 5.2.1 ATT_MTU */ - MIN_ATT_MTU = 23, - - /** 3s poll timeout for l2cap reader thread */ - L2CAP_READER_THREAD_POLL_TIMEOUT = 3000, - /** 500ms timeout for GATT read command replies */ - GATT_READ_COMMAND_REPLY_TIMEOUT = 500, - /** 20,000ms timeout for GATT write command replies, extremely long as seen on certain adapter / command combinations. */ - GATT_WRITE_COMMAND_REPLY_TIMEOUT = 20000, // FIXME: Needs to be analyzed further - /** 2500ms timeout for l2cap _initial_ command reply, long timeout. */ - GATT_INITIAL_COMMAND_REPLY_TIMEOUT = 2500, - - /** Medium ringbuffer capacity... */ - ATTPDU_RING_CAPACITY = 128 + MIN_ATT_MTU = 23 }; static inline int number(const Defaults d) { return static_cast<int>(d); } + /** L2CAP poll timeout for reader thread, defaults to 10s. */ + static const int32_t L2CAP_READER_THREAD_POLL_TIMEOUT; + /** Timeout for GATT read command replies, defaults to 500ms. */ + static const int32_t GATT_READ_COMMAND_REPLY_TIMEOUT; + /** Timeout for GATT write command replies, defaults to 500ms. */ + static const int32_t GATT_WRITE_COMMAND_REPLY_TIMEOUT; + /** Timeout for l2cap _initial_ command reply, defaults to 2500ms. */ + static const int32_t GATT_INITIAL_COMMAND_REPLY_TIMEOUT; + + /** Medium ringbuffer capacity, defaults to 128 messages. */ + static const int32_t ATTPDU_RING_CAPACITY; + private: const bool debug_data; @@ -103,7 +103,6 @@ namespace direct_bt { POctets rbuffer; L2CAPComm l2cap; - const int replyTimeoutMS; std::atomic<bool> isConnected; // reflects state std::atomic<bool> hasIOError; // reflects state @@ -138,10 +137,10 @@ namespace direct_bt { * Returns the server-mtu if successful, otherwise 0. * </p> */ - uint16_t exchangeMTU(const uint16_t clientMaxMTU, const int timeout); + uint16_t exchangeMTU(const uint16_t clientMaxMTU); public: - GATTHandler(const std::shared_ptr<DBTDevice> & device, const int replyTimeoutMS = number(Defaults::GATT_READ_COMMAND_REPLY_TIMEOUT)); + GATTHandler(const std::shared_ptr<DBTDevice> & device); ~GATTHandler(); diff --git a/api/direct_bt/HCIComm.hpp b/api/direct_bt/HCIComm.hpp index 2e95375f..05a0afb8 100644 --- a/api/direct_bt/HCIComm.hpp +++ b/api/direct_bt/HCIComm.hpp @@ -52,14 +52,13 @@ namespace direct_bt { static int hci_close_dev(int dd); std::recursive_mutex mtx; - const int timeoutMS; const uint16_t dev_id; const uint16_t channel; int _dd; // the hci socket public: - HCIComm(const uint16_t dev_id, const uint16_t channel, const int timeoutMS=number(HCIConstInt::TO_SEND_REQ_POLL_MS)) - : timeoutMS(timeoutMS), dev_id(dev_id), channel(channel), _dd(-1) { + HCIComm(const uint16_t dev_id, const uint16_t channel) + : dev_id(dev_id), channel(channel), _dd(-1) { _dd = hci_open_dev(dev_id, channel); } @@ -79,9 +78,7 @@ namespace direct_bt { std::recursive_mutex & mutex() { return mtx; } /** Generic read w/ own timeoutMS. Not protected by mutex. */ - int read(uint8_t* buffer, const int capacity, const int timeoutMS); - /** Generic read, reusing set timeoutMS from ctor. Not protected by mutex */ - int read(uint8_t* buffer, const int capacity); + int read(uint8_t* buffer, const int capacity, const int32_t timeoutMS); /** Generic write */ int write(const uint8_t* buffer, const int size); diff --git a/api/direct_bt/HCIHandler.hpp b/api/direct_bt/HCIHandler.hpp index ffaeb293..073e30fe 100644 --- a/api/direct_bt/HCIHandler.hpp +++ b/api/direct_bt/HCIHandler.hpp @@ -104,24 +104,25 @@ namespace direct_bt { */ class HCIHandler { public: - enum Defaults : int { - HCI_MAX_MTU = static_cast<uint8_t>(HCIConstU8::PACKET_MAX_SIZE), - - /** 10s poll timeout for HCI reader thread */ - HCI_READER_THREAD_POLL_TIMEOUT = 10000, - /** 3s timeout for HCI command status replies, excluding command complete. */ - HCI_COMMAND_STATUS_REPLY_TIMEOUT = 3000, - /** 10s timeout for HCI command complete replies. This timeout is rather longer, as it may include waiting for pending command complete. */ - HCI_COMMAND_COMPLETE_REPLY_TIMEOUT = 10000, - /** Small ringbuffer capacity for synchronized commands */ - HCI_EVT_RING_CAPACITY = 64, - /** Maximum number of packets to wait for until matching a sequential command. Won't block as timeout will limit. */ - HCI_READ_PACKET_MAX_RETRY = HCI_EVT_RING_CAPACITY + enum Defaults : int32_t { + HCI_MAX_MTU = static_cast<uint8_t>(HCIConstU8::PACKET_MAX_SIZE) }; + /** Poll timeout for HCI reader thread, defaults to 10s */ + static const int32_t HCI_READER_THREAD_POLL_TIMEOUT; + /** Timeout for HCI command status replies, excluding command complete, defaults to 3s. */ + static const int32_t HCI_COMMAND_STATUS_REPLY_TIMEOUT; + /** Timeout for HCI command complete replies, defaults to 10s. This timeout is rather longer, as it may include waiting for pending command complete. */ + static const int32_t HCI_COMMAND_COMPLETE_REPLY_TIMEOUT; + /** Small ringbuffer capacity for synchronized commands, defaults to 64 messages. */ + static const int32_t HCI_EVT_RING_CAPACITY; + static const pid_t pidSelf; private: + /** Maximum number of packets to wait for until matching a sequential command. Won't block as timeout will limit. */ + static const int32_t HCI_READ_PACKET_MAX_RETRY; + static MgmtEvent::Opcode translate(HCIEventType evt, HCIMetaEventType met); const bool debug_event; diff --git a/api/direct_bt/HCITypes.hpp b/api/direct_bt/HCITypes.hpp index 68f8816d..104e7a08 100644 --- a/api/direct_bt/HCITypes.hpp +++ b/api/direct_bt/HCITypes.hpp @@ -68,13 +68,11 @@ namespace direct_bt { : HCIException("HCIOpcodeException", m, file, line) {} }; - enum class HCIConstInt : int { - /** 3s poll timeout for complete HCI replies */ - TO_SEND_REQ_POLL_MS = 3000, + enum class HCIConstInt : int32_t { /** 10s le connection timeout, supervising max is 32s (v5.2 Vol 4, Part E - 7.8.12) */ LE_CONN_TIMEOUT_MS = 10000 }; - inline int number(const HCIConstInt rhs) { + inline int32_t number(const HCIConstInt rhs) { return static_cast<int>(rhs); } diff --git a/api/direct_bt/L2CAPComm.hpp b/api/direct_bt/L2CAPComm.hpp index 5af7b4c8..1d59e871 100644 --- a/api/direct_bt/L2CAPComm.hpp +++ b/api/direct_bt/L2CAPComm.hpp @@ -91,7 +91,7 @@ namespace direct_bt { bool isOpen() const { return 0 <= _dd; } int dd() const { return _dd; } - int read(uint8_t* buffer, const int capacity, const int timeoutMS); + int read(uint8_t* buffer, const int capacity, const int32_t timeoutMS); int write(const uint8_t *buffer, const int length); }; |