aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-07-26 05:42:38 +0200
committerSven Gothel <[email protected]>2020-07-26 05:42:38 +0200
commitce4481eaf4856ab2d2e2c4a9f39093888b34617f (patch)
tree5afb24d72e91c155e1b7eda402b70bb351713206 /src
parent74d41274760a9087c7a8c57e145fe531b035baae (diff)
Robustness: Distinguish between get<Type>Checked() and get<Type>Unchecked(): Use it accordingly, reducing duplicated code etc.
Diffstat (limited to 'src')
-rw-r--r--src/direct_bt/GATTCharacteristic.cpp46
-rw-r--r--src/direct_bt/GATTDescriptor.cpp24
-rw-r--r--src/direct_bt/GATTHandler.cpp6
-rw-r--r--src/direct_bt/GATTService.cpp8
4 files changed, 51 insertions, 33 deletions
diff --git a/src/direct_bt/GATTCharacteristic.cpp b/src/direct_bt/GATTCharacteristic.cpp
index 4be91ab5..85d15265 100644
--- a/src/direct_bt/GATTCharacteristic.cpp
+++ b/src/direct_bt/GATTCharacteristic.cpp
@@ -65,14 +65,6 @@ using namespace direct_bt;
#define CASE_TO_STRING2(V,S) case V: return #S;
-std::shared_ptr<DBTDevice> GATTCharacteristic::getDevice() const {
- std::shared_ptr<GATTService> s = getService();
- if( nullptr != s ) {
- return s->getDevice();
- }
- return nullptr;
-}
-
std::string GATTCharacteristic::getPropertyString(const PropertyBitVal prop) {
switch(prop) {
CHAR_DECL_PROPS_ENUM(CASE_TO_STRING2)
@@ -114,7 +106,7 @@ std::vector<std::unique_ptr<std::string>> GATTCharacteristic::getPropertiesStrin
std::string GATTCharacteristic::toString() const {
std::shared_ptr<const uuid_t> service_uuid;
uint16_t service_handle_end = 0xffff;
- GATTServiceRef serviceRef = getService();
+ GATTServiceRef serviceRef = getServiceUnchecked();
std::string service_uuid_str = "";
std::string service_name = "";
std::string char_name = "";
@@ -146,6 +138,26 @@ std::string GATTCharacteristic::toString() const {
service_name+", enabled[notify "+std::to_string(enabledNotifyState)+", indicate "+std::to_string(enabledIndicateState)+"] ]";
}
+std::shared_ptr<GATTService> GATTCharacteristic::getServiceChecked() const {
+ std::shared_ptr<GATTService> ref = wbr_service.lock();
+ if( nullptr == ref ) {
+ throw IllegalStateException("GATTCharacteristic's service already destructed: "+toString(), E_FILE_LINE);
+ }
+ return ref;
+}
+
+std::shared_ptr<DBTDevice> GATTCharacteristic::getDeviceUnchecked() const {
+ std::shared_ptr<GATTService> s = getServiceUnchecked();
+ if( nullptr != s ) {
+ return s->getDeviceUnchecked();
+ }
+ return nullptr;
+}
+
+std::shared_ptr<DBTDevice> GATTCharacteristic::getDeviceChecked() const {
+ return getServiceChecked()->getDeviceChecked();
+}
+
bool GATTCharacteristic::configNotificationIndication(const bool enableNotification, const bool enableIndication, bool enabledState[2]) {
enabledState[0] = false;
enabledState[1] = false;
@@ -157,8 +169,8 @@ bool GATTCharacteristic::configNotificationIndication(const bool enableNotificat
return false;
}
- std::shared_ptr<DBTDevice> device = getDevice();
- std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
+ std::shared_ptr<DBTDevice> device = getDeviceUnchecked();
+ std::shared_ptr<GATTHandler> gatt = nullptr != device ? device->getGATTHandler() : nullptr;
if( nullptr == gatt ) {
if( !enableNotification && !enableIndication ) {
// OK to have GATTHandler being shutdown @ disable
@@ -212,7 +224,7 @@ bool GATTCharacteristic::enableNotificationOrIndication(bool enabledState[2]) {
}
bool GATTCharacteristic::addCharacteristicListener(std::shared_ptr<GATTCharacteristicListener> l) {
- return getDevice()->addCharacteristicListener(l);
+ return getDeviceChecked()->addCharacteristicListener(l);
}
bool GATTCharacteristic::addCharacteristicListener(std::shared_ptr<GATTCharacteristicListener> l, bool enabledState[2]) {
@@ -227,7 +239,7 @@ bool GATTCharacteristic::removeCharacteristicListener(std::shared_ptr<GATTCharac
bool enabledState[2];
configNotificationIndication(false, false, enabledState);
}
- return getDevice()->removeCharacteristicListener(l);
+ return getDeviceChecked()->removeCharacteristicListener(l);
}
int GATTCharacteristic::removeAllCharacteristicListener(bool disableIndicationNotification) {
@@ -235,11 +247,11 @@ int GATTCharacteristic::removeAllCharacteristicListener(bool disableIndicationNo
bool enabledState[2];
configNotificationIndication(false, false, enabledState);
}
- return getDevice()->removeAllCharacteristicListener();
+ return getDeviceChecked()->removeAllCharacteristicListener();
}
bool GATTCharacteristic::readValue(POctets & res, int expectedLength) {
- std::shared_ptr<DBTDevice> device = getDevice();
+ std::shared_ptr<DBTDevice> device = getDeviceChecked();
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
throw IllegalStateException("Characteristic's device GATTHandle not connected: "+
@@ -251,7 +263,7 @@ bool GATTCharacteristic::readValue(POctets & res, int expectedLength) {
* BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.3 Write Characteristic Value
*/
bool GATTCharacteristic::writeValue(const TROOctets & value) {
- std::shared_ptr<DBTDevice> device = getDevice();
+ std::shared_ptr<DBTDevice> device = getDeviceChecked();
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
throw IllegalStateException("Characteristic's device GATTHandle not connected: "+
@@ -264,7 +276,7 @@ bool GATTCharacteristic::writeValue(const TROOctets & value) {
* BT Core Spec v5.2: Vol 3, Part G GATT: 4.9.1 Write Characteristic Value Without Response
*/
bool GATTCharacteristic::writeValueNoResp(const TROOctets & value) {
- std::shared_ptr<DBTDevice> device = getDevice();
+ std::shared_ptr<DBTDevice> device = getDeviceChecked();
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
throw IllegalStateException("Characteristic's device GATTHandle not connected: "+
diff --git a/src/direct_bt/GATTDescriptor.cpp b/src/direct_bt/GATTDescriptor.cpp
index 5d23c97f..f646fdbb 100644
--- a/src/direct_bt/GATTDescriptor.cpp
+++ b/src/direct_bt/GATTDescriptor.cpp
@@ -43,19 +43,20 @@ const uuid16_t GATTDescriptor::TYPE_EXT_PROP(Type::CHARACTERISTIC_EXTENDED_PROPE
const uuid16_t GATTDescriptor::TYPE_USER_DESC(Type::CHARACTERISTIC_USER_DESCRIPTION);
const uuid16_t GATTDescriptor::TYPE_CCC_DESC(Type::CLIENT_CHARACTERISTIC_CONFIGURATION);
-std::shared_ptr<DBTDevice> GATTDescriptor::getDevice() const {
- std::shared_ptr<GATTCharacteristic> c = getCharacteristic();
- if( nullptr != c ) {
- return c->getDevice();
+std::shared_ptr<GATTCharacteristic> GATTDescriptor::getCharacteristicChecked() const {
+ std::shared_ptr<GATTCharacteristic> ref = wbr_characteristic.lock();
+ if( nullptr == ref ) {
+ throw IllegalStateException("GATTDescriptor's characteristic already destructed: "+toString(), E_FILE_LINE);
}
- return nullptr;
+ return ref;
+}
+
+std::shared_ptr<DBTDevice> GATTDescriptor::getDeviceChecked() const {
+ return getCharacteristicChecked()->getDeviceChecked();
}
bool GATTDescriptor::readValue(int expectedLength) {
- std::shared_ptr<DBTDevice> device = getDevice();
- if( nullptr == device ) {
- throw IllegalStateException("Descriptor's device already destructed: "+toString(), E_FILE_LINE);
- }
+ std::shared_ptr<DBTDevice> device = getDeviceChecked();
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
throw IllegalStateException("Descriptor's device GATTHandle not connected: "+
@@ -65,10 +66,7 @@ bool GATTDescriptor::readValue(int expectedLength) {
}
bool GATTDescriptor::writeValue() {
- std::shared_ptr<DBTDevice> device = getDevice();
- if( nullptr == device ) {
- throw IllegalStateException("Descriptor's device already destructed: "+toString(), E_FILE_LINE);
- }
+ std::shared_ptr<DBTDevice> device = getDeviceChecked();
std::shared_ptr<GATTHandler> gatt = device->getGATTHandler();
if( nullptr == gatt ) {
throw IllegalStateException("Descriptor's device GATTHandle not connected: "+
diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp
index efc09136..a3bcbb74 100644
--- a/src/direct_bt/GATTHandler.cpp
+++ b/src/direct_bt/GATTHandler.cpp
@@ -907,7 +907,7 @@ std::shared_ptr<GenericAccess> GATTHandler::getGenericAccess(std::vector<GATTCha
for(size_t i=0; i<genericAccessCharDeclList.size(); i++) {
const GATTCharacteristic & charDecl = *genericAccessCharDeclList.at(i);
- std::shared_ptr<GATTService> service = charDecl.getService();
+ std::shared_ptr<GATTService> service = charDecl.getServiceUnchecked();
if( nullptr == service || _GENERIC_ACCESS != *(service->type) ) {
continue;
}
@@ -951,7 +951,7 @@ bool GATTHandler::ping() {
for(size_t i=0; i<genericAccessCharDeclList.size(); i++) {
const GATTCharacteristic & charDecl = *genericAccessCharDeclList.at(i);
- std::shared_ptr<GATTService> service = charDecl.getService();
+ std::shared_ptr<GATTService> service = charDecl.getServiceUnchecked();
if( nullptr == service || _GENERIC_ACCESS != *(service->type) ) {
continue;
}
@@ -984,7 +984,7 @@ std::shared_ptr<DeviceInformation> GATTHandler::getDeviceInformation(std::vector
for(size_t i=0; i<characteristicDeclList.size(); i++) {
const GATTCharacteristic & charDecl = *characteristicDeclList.at(i);
- std::shared_ptr<GATTService> service = charDecl.getService();
+ std::shared_ptr<GATTService> service = charDecl.getServiceUnchecked();
if( nullptr == service || _DEVICE_INFORMATION != *(service->type) ) {
continue;
}
diff --git a/src/direct_bt/GATTService.cpp b/src/direct_bt/GATTService.cpp
index 4a004950..1818b39e 100644
--- a/src/direct_bt/GATTService.cpp
+++ b/src/direct_bt/GATTService.cpp
@@ -38,6 +38,14 @@
using namespace direct_bt;
+std::shared_ptr<DBTDevice> GATTService::getDeviceChecked() const {
+ std::shared_ptr<DBTDevice> ref = wbr_device.lock();
+ if( nullptr == ref ) {
+ throw IllegalStateException("GATTService's device already destructed: "+toString(), E_FILE_LINE);
+ }
+ return ref;
+}
+
std::string GATTService::toString() const {
std::string name = "";
if( uuid_t::UUID16_SZ == type->getTypeSize() ) {