diff options
-rw-r--r-- | api/direct_bt/BTGattChar.hpp | 44 | ||||
-rw-r--r-- | api/direct_bt/BTGattDesc.hpp | 12 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTGattService.cxx | 5 | ||||
-rw-r--r-- | src/direct_bt/BTGattChar.cpp | 90 | ||||
-rw-r--r-- | src/direct_bt/BTGattDesc.cpp | 38 |
5 files changed, 103 insertions, 86 deletions
diff --git a/api/direct_bt/BTGattChar.hpp b/api/direct_bt/BTGattChar.hpp index ad739be2..c500ffc2 100644 --- a/api/direct_bt/BTGattChar.hpp +++ b/api/direct_bt/BTGattChar.hpp @@ -194,11 +194,8 @@ namespace direct_bt { } std::shared_ptr<BTGattService> getServiceUnchecked() const noexcept { return wbr_service.lock(); } - std::shared_ptr<BTGattService> getServiceChecked() const; std::shared_ptr<BTGattHandler> getGattHandlerUnchecked() const noexcept; - std::shared_ptr<BTGattHandler> getGattHandlerChecked() const; std::shared_ptr<BTDevice> getDeviceUnchecked() const noexcept; - std::shared_ptr<BTDevice> getDeviceChecked() const; bool hasProperties(const PropertyBitVal v) const noexcept { return v == ( properties & v ); } @@ -264,13 +261,10 @@ namespace direct_bt { * or there is no BTGattDesc of type ClientCharacteristicConfiguration, or if the operation has failed. * Otherwise returns true. * - * @throws IllegalStateException if notification or indication is set to be enabled - * and the {@link BTDevice's}'s {@link BTGattHandler} is null, i.e. not connected - * * @see enableNotificationOrIndication() * @see disableIndicationNotification() */ - bool configNotificationIndication(const bool enableNotification, const bool enableIndication, bool enabledState[2]); + bool configNotificationIndication(const bool enableNotification, const bool enableIndication, bool enabledState[2]) noexcept; /** * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration @@ -288,13 +282,10 @@ namespace direct_bt { * or there is no BTGattDesc of type ClientCharacteristicConfiguration, or if the operation has failed. * Otherwise returns true. * - * @throws IllegalStateException if notification or indication is set to be enabled - * and the {@link BTDevice's}'s {@link BTGattHandler} is null, i.e. not connected - * * @see configNotificationIndication() * @see disableIndicationNotification() */ - bool enableNotificationOrIndication(bool enabledState[2]); + bool enableNotificationOrIndication(bool enabledState[2]) noexcept; /** * BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3.3 Client Characteristic Configuration @@ -307,14 +298,11 @@ namespace direct_bt { * or there is no BTGattDesc of type ClientCharacteristicConfiguration, or if the operation has failed. * Otherwise returns true. * - * @throws IllegalStateException if notification or indication is set to be enabled - * and the {@link BTDevice's}'s {@link BTGattHandler} is null, i.e. not connected - * * @see configNotificationIndication() * @see enableNotificationOrIndication() * @since 2.4.0 */ - bool disableIndicationNotification(); + bool disableIndicationNotification() noexcept; /** * Add the given BTGattChar::Listener to the listener list if not already present. @@ -331,8 +319,6 @@ namespace direct_bt { * * Convenience delegation call to BTGattHandler via BTDevice * - * @throws IllegalStateException if the {@link BTDevice's}'s {@link BTGattHandler} is null, i.e. not connected - * * @see BTGattChar::disableIndicationNotification() * @see BTGattChar::enableNotificationOrIndication() * @see BTGattChar::configNotificationIndication() @@ -340,7 +326,7 @@ namespace direct_bt { * @see BTGattChar::removeCharListener() * @see BTGattChar::removeAllAssociatedCharListener() */ - bool addCharListener(std::shared_ptr<Listener> l); + bool addCharListener(std::shared_ptr<Listener> l) noexcept; /** * Add the given BTGattChar::Listener to the listener list if not already present @@ -364,8 +350,6 @@ namespace direct_bt { * @param enabledState array of size 2, holding the resulting enabled state for notification and indication * using enableNotificationOrIndication(bool[]) * - * @throws IllegalStateException if the {@link BTDevice's}'s {@link BTGattHandler} is null, i.e. not connected - * * @see BTGattChar::disableIndicationNotification() * @see BTGattChar::enableNotificationOrIndication() * @see BTGattChar::configNotificationIndication() @@ -373,7 +357,7 @@ namespace direct_bt { * @see BTGattChar::removeCharListener() * @see BTGattChar::removeAllAssociatedCharListener() */ - bool addCharListener(std::shared_ptr<Listener> l, bool enabledState[2]); + bool addCharListener(std::shared_ptr<Listener> l, bool enabledState[2]) noexcept; /** * Remove the given associated BTGattChar::Listener from the listener list if present. @@ -384,8 +368,6 @@ namespace direct_bt { * @param l * @return * - * @throws IllegalStateException if the {@link BTDevice's}'s {@link BTGattHandler} is null, i.e. not connected - * * @see BTGattChar::disableIndicationNotification() * @see BTGattChar::enableNotificationOrIndication() * @see BTGattChar::configNotificationIndication() @@ -394,7 +376,7 @@ namespace direct_bt { * @see BTGattChar::removeAllAssociatedCharListener() * @since 2.4.0 */ - bool removeCharListener(std::shared_ptr<Listener> l); + bool removeCharListener(std::shared_ptr<Listener> l) noexcept; /** * Removes all associated BTGattChar::Listener and and {@link BTGattCharListener} from the listener list. @@ -415,7 +397,7 @@ namespace direct_bt { * @see BTGattChar::removeCharListener() * @see BTGattChar::removeAllAssociatedCharListener() */ - int removeAllAssociatedCharListener(bool shallDisableIndicationNotification); + int removeAllAssociatedCharListener(bool shallDisableIndicationNotification) noexcept; /** * BT Core Spec v5.2: Vol 3, Part G GATT: 4.8.1 Read Characteristic Value @@ -437,10 +419,10 @@ namespace direct_bt { * Convenience delegation call to BTGattHandler via BTDevice * <p> * </p> - * If the BTDevice's BTGattHandler is null, i.e. not connected, an IllegalStateException is thrown. + * If the BTDevice's BTGattHandler is null, i.e. not connected, false is returned. * </p> */ - bool readValue(jau::POctets & res, int expectedLength=-1); + bool readValue(jau::POctets & res, int expectedLength=-1) noexcept; /** * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value @@ -448,10 +430,10 @@ namespace direct_bt { * Convenience delegation call to BTGattHandler via BTDevice * <p> * </p> - * If the BTDevice's BTGattHandler is null, i.e. not connected, an IllegalStateException is thrown. + * If the BTDevice's BTGattHandler is null, i.e. not connected, false is returned. * </p> */ - bool writeValue(const jau::TROOctets & value); + bool writeValue(const jau::TROOctets & value) noexcept; /** * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response @@ -459,10 +441,10 @@ namespace direct_bt { * Convenience delegation call to BTGattHandler via BTDevice * <p> * </p> - * If the BTDevice's BTGattHandler is null, i.e. not connected, an IllegalStateException is thrown. + * If the BTDevice's BTGattHandler is null, i.e. not connected, false is returned. * </p> */ - bool writeValueNoResp(const jau::TROOctets & value); + bool writeValueNoResp(const jau::TROOctets & value) noexcept; }; typedef std::shared_ptr<BTGattChar> BTGattCharRef; diff --git a/api/direct_bt/BTGattDesc.hpp b/api/direct_bt/BTGattDesc.hpp index 516c3f41..c1695445 100644 --- a/api/direct_bt/BTGattDesc.hpp +++ b/api/direct_bt/BTGattDesc.hpp @@ -125,8 +125,8 @@ namespace direct_bt { std::shared_ptr<BTGattChar> getGattCharUnchecked() const noexcept { return wbr_char.lock(); } std::shared_ptr<BTGattChar> getGattCharChecked() const; - std::shared_ptr<BTGattHandler> getGattHandlerChecked() const; - std::shared_ptr<BTDevice> getDeviceChecked() const; + std::shared_ptr<BTGattHandler> getGattHandlerUnchecked() const noexcept; + std::shared_ptr<BTDevice> getDeviceUnchecked() const noexcept; std::string toString() const noexcept override; @@ -157,10 +157,10 @@ namespace direct_bt { * </p> * <p> * Convenience delegation call to BTGattHandler via BTDevice - * If the BTDevice's BTGattHandler is null, i.e. not connected, an IllegalStateException is thrown. + * If the BTDevice's BTGattHandler is null, i.e. not connected, false is returned. * </p> */ - bool readValue(int expectedLength=-1); + bool readValue(int expectedLength=-1) noexcept; /** * BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors @@ -172,10 +172,10 @@ namespace direct_bt { * </p> * <p> * Convenience delegation call to BTGattHandler via BTDevice - * If the BTDevice's BTGattHandler is null, i.e. not connected, an IllegalStateException is thrown. + * If the BTDevice's BTGattHandler is null, i.e. not connected, false is returned. * </p> */ - bool writeValue(); + bool writeValue() noexcept; }; typedef std::shared_ptr<BTGattDesc> BTGattDescRef; diff --git a/java/jni/direct_bt/DBTGattService.cxx b/java/jni/direct_bt/DBTGattService.cxx index cfc07fd0..4dbcbfd8 100644 --- a/java/jni/direct_bt/DBTGattService.cxx +++ b/java/jni/direct_bt/DBTGattService.cxx @@ -98,7 +98,10 @@ jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj std::function<jobject(JNIEnv*, jclass, jmethodID, BTGattChar *)> ctor_char = [&gattCharPropSetClazz, &gattCharPropSetClazzCtor](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, BTGattChar *characteristic)->jobject { // prepare adapter ctor - std::shared_ptr<BTGattService> _service = characteristic->getServiceChecked(); + std::shared_ptr<BTGattService> _service = characteristic->getServiceUnchecked(); + if( nullptr == _service ) { + throw jau::RuntimeException("Characteristic's device null: "+characteristic->toString(), E_FILE_LINE); + } JavaGlobalObj::check(_service->getJavaObject(), E_FILE_LINE); jobject jservice = JavaGlobalObj::GetObject(_service->getJavaObject()); diff --git a/src/direct_bt/BTGattChar.cpp b/src/direct_bt/BTGattChar.cpp index fe9767d0..f33c4f80 100644 --- a/src/direct_bt/BTGattChar.cpp +++ b/src/direct_bt/BTGattChar.cpp @@ -180,14 +180,6 @@ std::string BTGattChar::toShortString() const noexcept { "], ccd-idx "+std::to_string(clientCharConfigIndex)+notify_str+"]"; } -std::shared_ptr<BTGattService> BTGattChar::getServiceChecked() const { - std::shared_ptr<BTGattService> ref = wbr_service.lock(); - if( nullptr == ref ) { - throw IllegalStateException("GATTCharacteristic's service already destructed: "+toShortString(), E_FILE_LINE); - } - return ref; -} - std::shared_ptr<BTGattHandler> BTGattChar::getGattHandlerUnchecked() const noexcept { std::shared_ptr<BTGattService> s = getServiceUnchecked(); if( nullptr != s ) { @@ -196,10 +188,6 @@ std::shared_ptr<BTGattHandler> BTGattChar::getGattHandlerUnchecked() const noexc return nullptr; } -std::shared_ptr<BTGattHandler> BTGattChar::getGattHandlerChecked() const { - return getServiceChecked()->getGattHandlerChecked(); -} - std::shared_ptr<BTDevice> BTGattChar::getDeviceUnchecked() const noexcept { std::shared_ptr<BTGattService> s = getServiceUnchecked(); if( nullptr != s ) { @@ -208,11 +196,7 @@ std::shared_ptr<BTDevice> BTGattChar::getDeviceUnchecked() const noexcept { return nullptr; } -std::shared_ptr<BTDevice> BTGattChar::getDeviceChecked() const { - return getServiceChecked()->getDeviceChecked(); -} - -bool BTGattChar::configNotificationIndication(const bool enableNotification, const bool enableIndication, bool enabledState[2]) { +bool BTGattChar::configNotificationIndication(const bool enableNotification, const bool enableIndication, bool enabledState[2]) noexcept { enabledState[0] = false; enabledState[1] = false; @@ -229,10 +213,10 @@ bool BTGattChar::configNotificationIndication(const bool enableNotification, con if( !enableNotification && !enableIndication ) { // OK to have GATTHandler being shutdown @ disable DBG_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str()); - return false; + } else { + ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str()); } - throw IllegalStateException("Characteristic's device GATTHandle not connected: "+ - toString(), E_FILE_LINE); + return false; } const bool resEnableNotification = hasEnableNotification && enableNotification; const bool resEnableIndication = hasEnableIndication && enableIndication; @@ -267,7 +251,7 @@ bool BTGattChar::configNotificationIndication(const bool enableNotification, con return res; } -bool BTGattChar::enableNotificationOrIndication(bool enabledState[2]) { +bool BTGattChar::enableNotificationOrIndication(bool enabledState[2]) noexcept { const bool hasEnableNotification = hasProperties(BTGattChar::PropertyBitVal::Notify); const bool hasEnableIndication = hasProperties(BTGattChar::PropertyBitVal::Indicate); @@ -277,7 +261,7 @@ bool BTGattChar::enableNotificationOrIndication(bool enabledState[2]) { return configNotificationIndication(enableNotification, enableIndication, enabledState); } -bool BTGattChar::disableIndicationNotification() { +bool BTGattChar::disableIndicationNotification() noexcept { bool enabledState[2]; return configNotificationIndication(false, false, enabledState); } @@ -341,44 +325,69 @@ bool DelegatedBTGattCharListener::operator==(const BTGattCharListener& rhs) cons return delegate.get() == rhs2.delegate.get(); } -bool BTGattChar::addCharListener(std::shared_ptr<BTGattChar::Listener> l) { - return getDeviceChecked()->addCharListener( std::make_shared<DelegatedBTGattCharListener>( this, l ) ); +bool BTGattChar::addCharListener(std::shared_ptr<BTGattChar::Listener> l) noexcept { + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Characteristic's device null: %s", toShortString().c_str()); + return false; + } + return device->addCharListener( std::make_shared<DelegatedBTGattCharListener>( this, l ) ); } -bool BTGattChar::addCharListener(std::shared_ptr<BTGattChar::Listener> l, bool enabledState[2]) { +bool BTGattChar::addCharListener(std::shared_ptr<BTGattChar::Listener> l, bool enabledState[2]) noexcept { if( !enableNotificationOrIndication(enabledState) ) { return false; } return addCharListener(l); } -bool BTGattChar::removeCharListener(std::shared_ptr<Listener> l) { - return getDeviceChecked()->removeCharListener( std::make_shared<DelegatedBTGattCharListener>( this, l ) ); +bool BTGattChar::removeCharListener(std::shared_ptr<Listener> l) noexcept { + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Characteristic's device null: %s", toShortString().c_str()); + return false; + } + return device->removeCharListener( std::make_shared<DelegatedBTGattCharListener>( this, l ) ); } -int BTGattChar::removeAllAssociatedCharListener(bool shallDisableIndicationNotification) { +int BTGattChar::removeAllAssociatedCharListener(bool shallDisableIndicationNotification) noexcept { if( shallDisableIndicationNotification ) { disableIndicationNotification(); } - return getDeviceChecked()->removeAllAssociatedCharListener(this); + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Characteristic's device null: %s", toShortString().c_str()); + return 0; + } + return device->removeAllAssociatedCharListener(this); } -bool BTGattChar::readValue(POctets & res, int expectedLength) { - std::shared_ptr<BTDevice> device = getDeviceChecked(); +bool BTGattChar::readValue(POctets & res, int expectedLength) noexcept { + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Characteristic's device null: %s", toShortString().c_str()); + return false; + } std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { - throw IllegalStateException("Characteristic's device GATTHandle not connected: "+toShortString(), E_FILE_LINE); + ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str()); + return false; } return gatt->readCharacteristicValue(*this, res, expectedLength); } /** * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value */ -bool BTGattChar::writeValue(const TROOctets & value) { - std::shared_ptr<BTDevice> device = getDeviceChecked(); +bool BTGattChar::writeValue(const TROOctets & value) noexcept { + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Characteristic's device null: %s", toShortString().c_str()); + return false; + } std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { - throw IllegalStateException("Characteristic's device GATTHandle not connected: "+toShortString(), E_FILE_LINE); + ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str()); + return false; } return gatt->writeCharacteristicValue(*this, value); } @@ -386,11 +395,16 @@ bool BTGattChar::writeValue(const TROOctets & value) { /** * BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response */ -bool BTGattChar::writeValueNoResp(const TROOctets & value) { - std::shared_ptr<BTDevice> device = getDeviceChecked(); +bool BTGattChar::writeValueNoResp(const TROOctets & value) noexcept { + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Characteristic's device null: %s", toShortString().c_str()); + return false; + } std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { - throw IllegalStateException("Characteristic's device GATTHandle not connected: "+toShortString(), E_FILE_LINE); + ERR_PRINT("Characteristic's device GATTHandle not connected: %s", toShortString().c_str()); + return false; } return gatt->writeCharacteristicValueNoResp(*this, value); } diff --git a/src/direct_bt/BTGattDesc.cpp b/src/direct_bt/BTGattDesc.cpp index ef737e50..ce518ef0 100644 --- a/src/direct_bt/BTGattDesc.cpp +++ b/src/direct_bt/BTGattDesc.cpp @@ -52,28 +52,46 @@ std::shared_ptr<BTGattChar> BTGattDesc::getGattCharChecked() const { return ref; } -std::shared_ptr<BTGattHandler> BTGattDesc::getGattHandlerChecked() const { - return getGattCharChecked()->getGattHandlerChecked(); +std::shared_ptr<BTGattHandler> BTGattDesc::getGattHandlerUnchecked() const noexcept { + std::shared_ptr<BTGattChar> c = getGattCharUnchecked(); + if( nullptr != c ) { + return c->getGattHandlerUnchecked(); + } + return nullptr; } -std::shared_ptr<BTDevice> BTGattDesc::getDeviceChecked() const { - return getGattCharChecked()->getDeviceChecked(); +std::shared_ptr<BTDevice> BTGattDesc::getDeviceUnchecked() const noexcept { + std::shared_ptr<BTGattChar> c = getGattCharUnchecked(); + if( nullptr != c ) { + return c->getDeviceUnchecked(); + } + return nullptr; } -bool BTGattDesc::readValue(int expectedLength) { - std::shared_ptr<BTDevice> device = getDeviceChecked(); +bool BTGattDesc::readValue(int expectedLength) noexcept { + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Descriptor's device null: %s", toShortString().c_str()); + return false; + } std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { - throw jau::IllegalStateException("Descriptor's device GATTHandle not connected: "+toShortString(), E_FILE_LINE); + ERR_PRINT("Descriptor's device GATTHandle not connected: %s", toShortString().c_str()); + return false; } return gatt->readDescriptorValue(*this, expectedLength); } -bool BTGattDesc::writeValue() { - std::shared_ptr<BTDevice> device = getDeviceChecked(); +bool BTGattDesc::writeValue() noexcept { + std::shared_ptr<BTDevice> device = getDeviceUnchecked(); + if( nullptr == device ) { + ERR_PRINT("Descriptor's device null: %s", toShortString().c_str()); + return false; + } std::shared_ptr<BTGattHandler> gatt = device->getGattHandler(); if( nullptr == gatt ) { - throw jau::IllegalStateException("Descriptor's device GATTHandle not connected: "+toShortString(), E_FILE_LINE); + ERR_PRINT("Descriptor's device GATTHandle not connected: %s", toShortString().c_str()); + return false; } return gatt->writeDescriptorValue(*this); } |