diff options
author | Sven Gothel <[email protected]> | 2020-06-26 07:15:52 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-06-26 07:15:52 +0200 |
commit | e7f6618a2b9eda80bef1901e331f7b7a4221d2bd (patch) | |
tree | c2c4fcadd59624f18e30d4acb9c72d79b735549b | |
parent | af5f9b2897ba50063b4e5af73190b32846413625 (diff) |
Align DBTDevice's C++ getGATTServices() w/ Java getServices(): Catch exception and return an empty list (on error/exception)
-rw-r--r-- | api/direct_bt/DBTDevice.hpp | 10 | ||||
-rw-r--r-- | src/direct_bt/DBTDevice.cpp | 59 |
2 files changed, 40 insertions, 29 deletions
diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp index e01ecaab..5dc1a614 100644 --- a/api/direct_bt/DBTDevice.hpp +++ b/api/direct_bt/DBTDevice.hpp @@ -156,7 +156,13 @@ namespace direct_bt { /** Return shared ManufactureSpecificData as recognized at discovery, pre GATT discovery. */ std::shared_ptr<ManufactureSpecificData> const getManufactureSpecificData() const; - /** Return a list of services as recognized at discovery, pre GATT discovery. */ + /** + * Return a list of advertised services as recognized at discovery, pre GATT discovery. + * <p> + * To receive a complete list of GATT services including characteristics etc, + * use {@link #getGATTServices()}. + * </p> + */ std::vector<std::shared_ptr<uuid_t>> getServices() const; /** Returns index >= 0 if found, otherwise -1 */ @@ -311,7 +317,7 @@ namespace direct_bt { /** * Returns a list of shared GATTService available on this device if successful, - * otherwise returns an empty list. + * otherwise returns an empty list if an error occurred. * <p> * If this method has been called for the first time or no services has been detected yet, * a list of GATTService will be discovered. diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp index a132c936..676dce12 100644 --- a/src/direct_bt/DBTDevice.cpp +++ b/src/direct_bt/DBTDevice.cpp @@ -457,38 +457,43 @@ std::shared_ptr<GATTHandler> DBTDevice::getGATTHandler() { std::vector<std::shared_ptr<GATTService>> DBTDevice::getGATTServices() { const std::lock_guard<std::recursive_mutex> lock(mtx_gatt); // RAII-style acquire and relinquish via destructor - if( nullptr == gattHandler || !gattHandler->isOpen() ) { - connectGATT(); + try { if( nullptr == gattHandler || !gattHandler->isOpen() ) { - ERR_PRINT("DBTDevice::getServices: connectGATT failed"); - return std::vector<std::shared_ptr<GATTService>>(); + connectGATT(); + if( nullptr == gattHandler || !gattHandler->isOpen() ) { + ERR_PRINT("DBTDevice::getServices: connectGATT failed"); + return std::vector<std::shared_ptr<GATTService>>(); + } } - } - std::vector<std::shared_ptr<GATTService>> & gattServices = gattHandler->getServices(); // reference of the GATTHandler's list - if( gattServices.size() > 0 ) { // reuse previous discovery result - return gattServices; - } - gattServices = gattHandler->discoverCompletePrimaryServices(); // same reference of the GATTHandler's list - if( gattServices.size() == 0 ) { // nothing discovered - return gattServices; - } - // discovery success, retrieve and parse GenericAccess - gattGenericAccess = gattHandler->getGenericAccess(gattServices); - if( nullptr != gattGenericAccess ) { - const uint64_t ts = getCurrentMilliseconds(); - EIRDataType updateMask = update(*gattGenericAccess, ts); - DBG_PRINT("DBTDevice::getGATTServices: updated %s:\n %s\n -> %s", - getEIRDataMaskString(updateMask).c_str(), gattGenericAccess->toString().c_str(), toString().c_str()); - if( EIRDataType::NONE != updateMask ) { - std::shared_ptr<DBTDevice> sharedInstance = getSharedInstance(); - if( nullptr == sharedInstance ) { - ERR_PRINT("DBTDevice::getGATTServices: Device unknown to adapter and not tracked: %s", toString().c_str()); - } else { - adapter.sendDeviceUpdated("getGATTServices", sharedInstance, ts, updateMask); + std::vector<std::shared_ptr<GATTService>> & gattServices = gattHandler->getServices(); // reference of the GATTHandler's list + if( gattServices.size() > 0 ) { // reuse previous discovery result + return gattServices; + } + gattServices = gattHandler->discoverCompletePrimaryServices(); // same reference of the GATTHandler's list + if( gattServices.size() == 0 ) { // nothing discovered + return gattServices; + } + // discovery success, retrieve and parse GenericAccess + gattGenericAccess = gattHandler->getGenericAccess(gattServices); + if( nullptr != gattGenericAccess ) { + const uint64_t ts = getCurrentMilliseconds(); + EIRDataType updateMask = update(*gattGenericAccess, ts); + DBG_PRINT("DBTDevice::getGATTServices: updated %s:\n %s\n -> %s", + getEIRDataMaskString(updateMask).c_str(), gattGenericAccess->toString().c_str(), toString().c_str()); + if( EIRDataType::NONE != updateMask ) { + std::shared_ptr<DBTDevice> sharedInstance = getSharedInstance(); + if( nullptr == sharedInstance ) { + ERR_PRINT("DBTDevice::getGATTServices: Device unknown to adapter and not tracked: %s", toString().c_str()); + } else { + adapter.sendDeviceUpdated("getGATTServices", sharedInstance, ts, updateMask); + } } } + return gattServices; + } catch (std::exception &e) { + WARN_PRINT("DBTDevice::getGATTServices: Caught exception: '%s' on %s", e.what(), toString().c_str()); } - return gattServices; + return std::vector<std::shared_ptr<GATTService>>(); } bool DBTDevice::pingGATT() { |