From c2019c3692b7c56ca1ee2699301425644b0a1861 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 14 Sep 2020 22:22:16 +0200 Subject: C++ noexcept: GATTHandler We had to keep quite many methods due to 'exception bail out' usage on broken connection. --- api/direct_bt/GATTHandler.hpp | 36 ++++++++++++++++++------------------ src/direct_bt/GATTHandler.cpp | 22 +++++++++++----------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/api/direct_bt/GATTHandler.hpp b/api/direct_bt/GATTHandler.hpp index 170819a9..829deec2 100644 --- a/api/direct_bt/GATTHandler.hpp +++ b/api/direct_bt/GATTHandler.hpp @@ -65,7 +65,7 @@ namespace direct_bt { */ class GATTEnv : public DBTEnvrionment { private: - GATTEnv(); + GATTEnv() noexcept; const bool exploding; // just to trigger exploding properties @@ -119,7 +119,7 @@ namespace direct_bt { const bool DEBUG_DATA; public: - static GATTEnv& get() { + static GATTEnv& get() noexcept { /** * Thread safe starting with C++11 6.7: * @@ -187,7 +187,7 @@ namespace direct_bt { std::shared_ptr getDevice() const { return wbr_device.lock(); } - bool validateConnected(); + bool validateConnected() noexcept; void l2capReaderThreadImpl(); @@ -203,19 +203,19 @@ namespace direct_bt { uint16_t exchangeMTU(const uint16_t clientMaxMTU); public: - GATTHandler(const std::shared_ptr & device); + GATTHandler(const std::shared_ptr & device) noexcept; ~GATTHandler(); - bool getIsConnected() const { return isConnected; } - bool getHasIOError() const { return hasIOError; } - std::string getStateString() const { return L2CAPComm::getStateString(isConnected, hasIOError); } + bool getIsConnected() const noexcept { return isConnected; } + bool getHasIOError() const noexcept { return hasIOError; } + std::string getStateString() const noexcept { return L2CAPComm::getStateString(isConnected, hasIOError); } /** * After successful l2cap connection, the MTU will be exchanged. * See getServerMTU() and getUsedMTU(), the latter is in use. */ - bool connect(); + bool connect() noexcept; /** * Disconnect this GATTHandler and optionally the associated device @@ -223,12 +223,12 @@ namespace direct_bt { * @param ioErrorCause if true, reason for disconnection is an IO error * @return true if successful, otherwise false */ - bool disconnect(const bool disconnectDevice, const bool ioErrorCause); + bool disconnect(const bool disconnectDevice, const bool ioErrorCause) noexcept; - bool isOpen() const { return isConnected && l2cap.isOpen(); } + bool isOpen() const noexcept { return isConnected && l2cap.isOpen(); } - uint16_t getServerMTU() const { return serverMTU; } - uint16_t getUsedMTU() const { return usedMTU; } + inline uint16_t getServerMTU() const noexcept { return serverMTU; } + inline uint16_t getUsedMTU() const noexcept { return usedMTU; } /** * Find and return the GATTCharacterisicsDecl within internal primary services @@ -237,7 +237,7 @@ namespace direct_bt { * Returns nullptr if not found. *

*/ - GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle); + GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle) noexcept; /** * Find and return the GATTCharacterisicsDecl within given list of primary services @@ -246,7 +246,7 @@ namespace direct_bt { * Returns nullptr if not found. *

*/ - GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, std::vector &services); + GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, std::vector &services) noexcept; /** * Find and return the GATTCharacterisicsDecl within given primary service @@ -255,7 +255,7 @@ namespace direct_bt { * Returns nullptr if not found. *

*/ - GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, GATTServiceRef service); + GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, GATTServiceRef service) noexcept; /** * Discover all primary services _and_ all its characteristics declarations @@ -273,7 +273,7 @@ namespace direct_bt { * The internal list will be populated via {@link #discoverCompletePrimaryServices()}. *

*/ - std::vector & getServices() { return services; } + inline std::vector & getServices() noexcept { return services; } /** * Discover all primary services _only_. @@ -441,7 +441,7 @@ namespace direct_bt { * Returns the number of removed event listener. *

*/ - int removeAllCharacteristicListener(); + int removeAllCharacteristicListener() noexcept ; /** * Enable or disable sending an immediate confirmation for received indication events from the device. @@ -463,7 +463,7 @@ namespace direct_bt { * This setting is per GATTHandler and hence per DBTDevice. *

*/ - bool getSendIndicationConfirmation(); + bool getSendIndicationConfirmation() noexcept; /*****************************************************/ /** Higher level semantic functionality **/ diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp index b3ee8b24..c0157be5 100644 --- a/src/direct_bt/GATTHandler.cpp +++ b/src/direct_bt/GATTHandler.cpp @@ -65,7 +65,7 @@ extern "C" { using namespace direct_bt; -GATTEnv::GATTEnv() +GATTEnv::GATTEnv() noexcept : exploding( DBTEnv::getExplodingProperties("direct_bt.gatt") ), L2CAP_READER_THREAD_POLL_TIMEOUT( DBTEnv::getInt32Property("direct_bt.gatt.reader.timeout", 10000, 1500 /* min */, INT32_MAX /* max */) ), GATT_READ_COMMAND_REPLY_TIMEOUT( DBTEnv::getInt32Property("direct_bt.gatt.cmd.read.timeout", 500, 250 /* min */, INT32_MAX /* max */) ), @@ -78,7 +78,7 @@ GATTEnv::GATTEnv() #define CASE_TO_STRING(V) case V: return #V; -bool GATTHandler::validateConnected() { +bool GATTHandler::validateConnected() noexcept { bool l2capIsConnected = l2cap.getIsConnected(); bool l2capHasIOError = l2cap.getHasIOError(); @@ -158,7 +158,7 @@ int GATTHandler::removeAllAssociatedCharacteristicListener(const GATTCharacteris return false; } -int GATTHandler::removeAllCharacteristicListener() { +int GATTHandler::removeAllCharacteristicListener() noexcept { const std::lock_guard lock(mtx_eventListenerList); // RAII-style acquire and relinquish via destructor int count = characteristicListenerList.size(); characteristicListenerList.clear(); @@ -170,7 +170,7 @@ void GATTHandler::setSendIndicationConfirmation(const bool v) { sendIndicationConfirmation = v; } -bool GATTHandler::getSendIndicationConfirmation() { +bool GATTHandler::getSendIndicationConfirmation() noexcept { const std::lock_guard lock(mtx_eventListenerList); // RAII-style acquire and relinquish via destructor return sendIndicationConfirmation; } @@ -267,7 +267,7 @@ void GATTHandler::l2capReaderThreadImpl() { disconnect(true /* disconnectDevice */, ioErrorCause); } -GATTHandler::GATTHandler(const std::shared_ptr &device) +GATTHandler::GATTHandler(const std::shared_ptr &device) noexcept : env(GATTEnv::get()), wbr_device(device), deviceString(device->getAddressString()), rbuffer(number(Defaults::MAX_ATT_MTU)), l2cap(device, L2CAP_PSM_UNDEF, L2CAP_CID_ATT), @@ -277,12 +277,12 @@ GATTHandler::GATTHandler(const std::shared_ptr &device) serverMTU(number(Defaults::MIN_ATT_MTU)), usedMTU(number(Defaults::MIN_ATT_MTU)) { } -GATTHandler::~GATTHandler() { +GATTHandler::~GATTHandler() noexcept { disconnect(false /* disconnectDevice */, false /* ioErrorCause */); services.clear(); } -bool GATTHandler::connect() { +bool GATTHandler::connect() noexcept { // Avoid connect re-entry -> potential deadlock bool expConn = false; // C++11, exp as value since C++20 if( !isConnected.compare_exchange_strong(expConn, true) ) { @@ -332,7 +332,7 @@ bool GATTHandler::connect() { return true; } -bool GATTHandler::disconnect(const bool disconnectDevice, const bool ioErrorCause) { +bool GATTHandler::disconnect(const bool disconnectDevice, const bool ioErrorCause) noexcept { // Interrupt GATT's L2CAP ::connect(..), avoiding prolonged hang // and pull all underlying l2cap read operations! l2cap.disconnect(); @@ -454,11 +454,11 @@ uint16_t GATTHandler::exchangeMTU(const uint16_t clientMaxMTU) { return mtu; } -GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle) { +GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle) noexcept { return findCharacterisicsByValueHandle(charValueHandle, services); } -GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle, std::vector &services) { +GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle, std::vector &services) noexcept { for(auto it = services.begin(); it != services.end(); it++) { GATTCharacteristicRef decl = findCharacterisicsByValueHandle(charValueHandle, *it); if( nullptr != decl ) { @@ -468,7 +468,7 @@ GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_ return nullptr; } -GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle, GATTServiceRef service) { +GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle, GATTServiceRef service) noexcept { for(auto it = service->characteristicList.begin(); it != service->characteristicList.end(); it++) { GATTCharacteristicRef decl = *it; if( charValueHandle == decl->value_handle ) { -- cgit v1.2.3