summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-07-22 02:46:40 +0200
committerSven Gothel <[email protected]>2020-07-22 02:46:40 +0200
commit3ca996e648da55bef278a61f35912dabf7be4123 (patch)
treecbf0c3e32f025587a80d216330b3d846453b213a
parentb74bd36827f4e13e8fbeb9193f7176acc743aff0 (diff)
DBTAdapter/Device: Simplify adapter's HCIHandler access, as we can rely on its existence if adapter.isValid()
-rw-r--r--api/direct_bt/DBTAdapter.hpp14
-rw-r--r--api/direct_bt/DBTTypes.hpp2
-rw-r--r--src/direct_bt/DBTAdapter.cpp12
-rw-r--r--src/direct_bt/DBTDevice.cpp24
4 files changed, 22 insertions, 30 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp
index b6cbcd13..727f70f1 100644
--- a/api/direct_bt/DBTAdapter.hpp
+++ b/api/direct_bt/DBTAdapter.hpp
@@ -171,11 +171,6 @@ namespace direct_bt {
std::recursive_mutex mtx_statusListenerList;
std::recursive_mutex mtx_discovery;
- inline void checkValid() {
- if( !valid ) {
- throw IllegalStateException("Adapter state invalid: "+toString(), E_FILE_LINE);
- }
- }
bool validateDevInfo();
bool openHCI();
bool closeHCI();
@@ -249,6 +244,15 @@ namespace direct_bt {
return std::string(JAVA_DBT_PACKAGE "DBTAdapter");
}
+ /**
+ * Throws an IllegalStateException if isValid() == false
+ */
+ inline void checkValid() const {
+ if( !isValid() ) {
+ throw IllegalStateException("Adapter state invalid: "+toString(), E_FILE_LINE);
+ }
+ }
+
bool hasDevId() const { return 0 <= dev_id; }
EUI48 const & getAddress() const { return adapterInfo->address; }
diff --git a/api/direct_bt/DBTTypes.hpp b/api/direct_bt/DBTTypes.hpp
index de827604..c5efa3cd 100644
--- a/api/direct_bt/DBTTypes.hpp
+++ b/api/direct_bt/DBTTypes.hpp
@@ -69,7 +69,7 @@ namespace direct_bt {
valid = false;
}
- bool isValid() { return valid; }
+ bool isValid() const { return valid; }
};
/**
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index 6cefa689..8e2b858e 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -133,21 +133,19 @@ std::shared_ptr<DBTDevice> DBTAdapter::findConnectedDevice (EUI48 const & mac, c
bool DBTAdapter::openHCI()
{
- HCIHandler *s = new HCIHandler(btMode, dev_id, HCIHandler::Defaults::HCI_COMMAND_REPLY_TIMEOUT);
- if( !s->isOpen() ) {
- delete s;
- ERR_PRINT("Could not open HCIHandler: %s of %s", s->toString().c_str(), toString().c_str());
+ hci = std::shared_ptr<HCIHandler>( new HCIHandler(btMode, dev_id, HCIHandler::Defaults::HCI_COMMAND_REPLY_TIMEOUT) );
+ if( !hci->isOpen() ) {
+ ERR_PRINT("Could not open HCIHandler: %s of %s", hci->toString().c_str(), toString().c_str());
return false;
}
- hci = std::shared_ptr<HCIHandler>( s );
return true;
}
bool DBTAdapter::closeHCI()
{
DBG_PRINT("DBTAdapter::closeHCI: ...");
- if( nullptr == hci || !hci->isOpen() ) {
- DBG_PRINT("DBTAdapter::closeHCI: Not open");
+ if( nullptr == hci ) {
+ DBG_PRINT("DBTAdapter::closeHCI: HCI null");
return false;
}
hci->close();
diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp
index f5fc9713..4a7fafee 100644
--- a/src/direct_bt/DBTDevice.cpp
+++ b/src/direct_bt/DBTDevice.cpp
@@ -265,6 +265,7 @@ bool DBTDevice::connectLE(uint16_t le_scan_interval, uint16_t le_scan_window,
uint16_t conn_latency, uint16_t supervision_timeout)
{
const std::lock_guard<std::recursive_mutex> lock_conn(mtx_connect); // RAII-style acquire and relinquish via destructor
+ adapter.checkValid();
HCILEOwnAddressType hci_own_mac_type;
HCILEPeerAddressType hci_peer_mac_type;
@@ -313,13 +314,7 @@ bool DBTDevice::connectLE(uint16_t le_scan_interval, uint16_t le_scan_window,
return false;
}
- std::shared_ptr<HCIHandler> hci = adapter.getHCI();
- if( nullptr == hci || !hci->isOpen() ) {
- ERR_PRINT("DBTDevice::connectLE: Adapter's HCI not open: %s", toString().c_str());
- return false;
- }
-
- HCIStatusCode status = hci->le_create_conn(address,
+ HCIStatusCode status = adapter.getHCI()->le_create_conn(address,
hci_peer_mac_type, hci_own_mac_type,
le_scan_interval, le_scan_window, conn_interval_min, conn_interval_max,
conn_latency, supervision_timeout);
@@ -358,22 +353,18 @@ bool DBTDevice::connectLE(uint16_t le_scan_interval, uint16_t le_scan_window,
bool DBTDevice::connectBREDR(const uint16_t pkt_type, const uint16_t clock_offset, const uint8_t role_switch)
{
const std::lock_guard<std::recursive_mutex> lock_conn(mtx_connect); // RAII-style acquire and relinquish via destructor
+ adapter.checkValid();
if( isConnected ) {
ERR_PRINT("DBTDevice::connectBREDR: Already connected: %s", toString().c_str());
return false;
}
- std::shared_ptr<HCIHandler> hci = adapter.getHCI();
- if( nullptr == hci || !hci->isOpen() ) {
- ERR_PRINT("DBTDevice::connectBREDR: Adapter's HCI not open: %s", toString().c_str());
- return false;
- }
if( !isBREDRAddressType() ) {
ERR_PRINT("DBTDevice::connectBREDR: Not a BDADDR_BREDR address: %s", toString().c_str());
return false;
}
- HCIStatusCode status = hci->create_conn(address, pkt_type, clock_offset, role_switch);
+ HCIStatusCode status = adapter.getHCI()->create_conn(address, pkt_type, clock_offset, role_switch);
isConnectIssued = true;
if ( HCIStatusCode::SUCCESS != status ) {
ERR_PRINT("DBTDevice::connectBREDR: Could not create connection: status 0x%2.2X (%s), errno %d %s on %s",
@@ -425,9 +416,8 @@ bool DBTDevice::disconnect(const bool fromDisconnectCB, const bool ioErrorCause,
(nullptr != gattHandler), uint16HexString(hciConnHandle).c_str());
disconnectGATT();
- bool res = false;
-
std::shared_ptr<HCIHandler> hci = adapter.getHCI();
+ bool res = false;
if( !isConnected || !isConnectIssued ) {
goto exit;
@@ -438,8 +428,8 @@ bool DBTDevice::disconnect(const bool fromDisconnectCB, const bool ioErrorCause,
goto exit;
}
- if( nullptr == hci || !hci->isOpen() ) {
- DBG_PRINT("DBTDevice::disconnect: Skip disconnect: HCI not Open: %s", toString().c_str());
+ if( nullptr == hci ) {
+ DBG_PRINT("DBTDevice::disconnect: Skip disconnect: HCI is null: %s", toString().c_str());
goto exit;
}