summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-10-25 02:19:34 +0100
committerSven Gothel <[email protected]>2020-10-25 02:19:34 +0100
commit2565b396380f1dc0c30161439c2dde30df083a0d (patch)
treeda80840edca85529b8eb5d546c2da35f2a07debb /src
parent5d3d7df23b1f499e27f1956e885f1a40d7a0de3b (diff)
Refine/add state queries in [DBT|Bluetooth]Adapter, BluetoothManager
[DBT|Bluetooth]Adapter: C++ and Java: Redefine/add state queries: - isValid(): true if this adapter references are valid and hasn't been DBTAdapter::close() 'ed - isPowered(): true if DBTAdapter::isValid(), HCIHandler::isOpen() and AdapterSetting::POWERED state is set - isSuspended(): true if DBTAdapter::isValid(), HCIHandler::isOpen() and AdapterSetting::POWERED state is not set - dev_id / getDevID(): Added definition, added method + implementation to Java BluetoothAdapter: Java - getPowered() -> getPoweredState() - to differentiate with isPowered() BluetoothManager: Java - getAdapter(dev_id) added - setDefaultAdapter(..) implementation removed in DBTManager -> nonsense - getDefaultAdapter(): DBT: Return 1st isPowered() adapter (redfinition, aligned with C++)
Diffstat (limited to 'src')
-rw-r--r--src/direct_bt/DBTAdapter.cpp28
-rw-r--r--src/direct_bt/DBTDevice.cpp18
-rw-r--r--src/direct_bt/DBTManager.cpp25
3 files changed, 37 insertions, 34 deletions
diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp
index caf09a28..776a508c 100644
--- a/src/direct_bt/DBTAdapter.cpp
+++ b/src/direct_bt/DBTAdapter.cpp
@@ -164,7 +164,7 @@ bool DBTAdapter::validateDevInfo() noexcept {
}
hci.setBTMode(btMode);
- if( isPowered() ) {
+ if( adapterInfo->isCurrentSettingBitSet(AdapterSetting::POWERED) ) {
HCILocalVersion version;
HCIStatusCode status = hci.getLocalVersion(version);
if( HCIStatusCode::SUCCESS != status ) {
@@ -206,7 +206,7 @@ bool DBTAdapter::validateDevInfo() noexcept {
DBTAdapter::DBTAdapter() noexcept
: debug_event(jau::environment::getBooleanProperty("direct_bt.debug.adapter.event", false)),
mgmt( DBTManager::get(BTMode::NONE /* use env default */) ),
- dev_id( mgmt.getDefaultAdapterDevId() ),
+ dev_id( mgmt.getDefaultAdapterDevID() ),
hci( dev_id )
{
valid = validateDevInfo();
@@ -224,7 +224,7 @@ DBTAdapter::DBTAdapter(EUI48 &mac) noexcept
DBTAdapter::DBTAdapter(const int _dev_id) noexcept
: debug_event(jau::environment::getBooleanProperty("direct_bt.debug.adapter.event", false)),
mgmt( DBTManager::get(BTMode::NONE /* use env default */) ),
- dev_id( 0 <= _dev_id ? _dev_id : mgmt.getDefaultAdapterDevId() ),
+ dev_id( 0 <= _dev_id ? _dev_id : mgmt.getDefaultAdapterDevID() ),
hci( dev_id )
{
valid = validateDevInfo();
@@ -239,10 +239,11 @@ DBTAdapter::~DBTAdapter() noexcept {
void DBTAdapter::close() noexcept {
DBG_PRINT("DBTAdapter::close: ... %p %s", this, toString().c_str());
keep_le_scan_alive = false;
+
// mute all listener first
{
int count = mgmt.removeMgmtEventCallback(dev_id);
- DBG_PRINT("DBTAdapter removeMgmtEventCallback(DISCOVERING): %d callbacks", count);
+ DBG_PRINT("DBTAdapter::close removeMgmtEventCallback: %d callbacks", count);
}
statusListenerList.clear();
@@ -350,8 +351,8 @@ bool DBTAdapter::isDeviceWhitelisted(const EUI48 &address) noexcept {
bool DBTAdapter::addDeviceToWhitelist(const EUI48 &address, const BDAddressType address_type, const HCIWhitelistConnectType ctype,
const uint16_t conn_interval_min, const uint16_t conn_interval_max,
const uint16_t conn_latency, const uint16_t timeout) {
- if( !isEnabled() ) {
- ERR_PRINT("DBTAdapter::startDiscovery: Adapter not enabled/powered: %s", toString().c_str());
+ if( !isPowered() ) {
+ ERR_PRINT("DBTAdapter::startDiscovery: Adapter not powered: %s", toString().c_str());
return false;
}
if( mgmt.isDeviceWhitelisted(dev_id, address) ) {
@@ -367,7 +368,6 @@ bool DBTAdapter::addDeviceToWhitelist(const EUI48 &address, const BDAddressType
}
bool DBTAdapter::removeDeviceFromWhitelist(const EUI48 &address, const BDAddressType address_type) {
- checkValidAdapter();
return mgmt.removeDeviceFromWhitelist(dev_id, address, address_type);
}
@@ -375,7 +375,6 @@ static jau::cow_vector<std::shared_ptr<AdapterStatusListener>>::equal_comparator
[](const std::shared_ptr<AdapterStatusListener> &a, const std::shared_ptr<AdapterStatusListener> &b) -> bool { return *a == *b; };
bool DBTAdapter::addStatusListener(std::shared_ptr<AdapterStatusListener> l) {
- checkValidAdapter();
if( nullptr == l ) {
throw jau::IllegalArgumentException("DBTAdapterStatusListener ref is null", E_FILE_LINE);
}
@@ -387,7 +386,6 @@ bool DBTAdapter::addStatusListener(std::shared_ptr<AdapterStatusListener> l) {
}
bool DBTAdapter::removeStatusListener(std::shared_ptr<AdapterStatusListener> l) {
- checkValidAdapter();
if( nullptr == l ) {
throw jau::IllegalArgumentException("DBTAdapterStatusListener ref is null", E_FILE_LINE);
}
@@ -396,7 +394,6 @@ bool DBTAdapter::removeStatusListener(std::shared_ptr<AdapterStatusListener> l)
}
bool DBTAdapter::removeStatusListener(const AdapterStatusListener * l) {
- checkValidAdapter();
if( nullptr == l ) {
throw jau::IllegalArgumentException("DBTAdapterStatusListener ref is null", E_FILE_LINE);
}
@@ -420,7 +417,6 @@ bool DBTAdapter::removeStatusListener(const AdapterStatusListener * l) {
}
int DBTAdapter::removeAllStatusListener() {
- checkValidAdapter();
int count = statusListenerList.size();
statusListenerList.clear();
return count;
@@ -457,8 +453,8 @@ HCIStatusCode DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAdd
// ERR_PRINT("Test");
// throw jau::RuntimeException("Test", E_FILE_LINE);
- if( !isEnabled() ) {
- WARN_PRINT("DBTAdapter::startDiscovery: Adapter not enabled/powered: %s", toString().c_str());
+ if( !isPowered() ) {
+ WARN_PRINT("DBTAdapter::startDiscovery: Adapter not powered: %s", toString().c_str());
return HCIStatusCode::UNSPECIFIED_ERROR;
}
const std::lock_guard<std::mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor
@@ -502,8 +498,8 @@ HCIStatusCode DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAdd
void DBTAdapter::startDiscoveryBackground() noexcept {
// FIXME: Respect DBTAdapter::btMode, i.e. BTMode::BREDR, BTMode::LE or BTMode::DUAL to setup BREDR, LE or DUAL scanning!
- if( !isEnabled() ) {
- WARN_PRINT("DBTAdapter::startDiscoveryBackground: Adapter not enabled/powered: %s", toString().c_str());
+ if( !isPowered() ) {
+ WARN_PRINT("DBTAdapter::startDiscoveryBackground: Adapter not powered: %s", toString().c_str());
return;
}
const std::lock_guard<std::mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor
@@ -556,7 +552,7 @@ HCIStatusCode DBTAdapter::stopDiscovery() noexcept {
}
HCIStatusCode status;
- if( !isPowered() ) {
+ if( !adapterInfo->isCurrentSettingBitSet(AdapterSetting::POWERED) ) {
WARN_PRINT("DBTAdapter::stopDiscovery: Powered off: %s", toString().c_str());
hci.setCurrentScanType(ScanType::NONE);
currentMetaScanType = ScanType::NONE;
diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp
index 13cc837d..931e8a46 100644
--- a/src/direct_bt/DBTDevice.cpp
+++ b/src/direct_bt/DBTDevice.cpp
@@ -261,8 +261,10 @@ HCIStatusCode DBTDevice::connectLE(uint16_t le_scan_interval, uint16_t le_scan_w
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();
-
+ if( !adapter.isPowered() ) {
+ WARN_PRINT("DBTDevice::connectLE: Adapter not powered: %s", adapter.toString().c_str());
+ return HCIStatusCode::UNSPECIFIED_ERROR;
+ }
HCILEOwnAddressType hci_own_mac_type;
HCILEPeerAddressType hci_peer_mac_type;
@@ -339,7 +341,10 @@ HCIStatusCode DBTDevice::connectLE(uint16_t le_scan_interval, uint16_t le_scan_w
HCIStatusCode 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( !adapter.isPowered() ) {
+ WARN_PRINT("DBTDevice::connectBREDR: Adapter not powered: %s", adapter.toString().c_str());
+ return HCIStatusCode::UNSPECIFIED_ERROR;
+ }
if( isConnected ) {
ERR_PRINT("DBTDevice::connectBREDR: Already connected: %s", toString().c_str());
@@ -451,15 +456,10 @@ HCIStatusCode DBTDevice::disconnect(const HCIStatusCode reason) noexcept {
}
if( !adapter.isPowered() ) {
- WARN_PRINT("DBTDevice::disconnect: Powered off: %s", toString().c_str());
+ WARN_PRINT("DBTDevice::disconnect: Adapter not powered: %s", toString().c_str());
res = HCIStatusCode::UNSPECIFIED_ERROR; // powered-off
goto exit;
}
- if( !hci.isOpen() ) {
- ERR_PRINT("DBTDevice::disconnect: HCI closed: %s", toString().c_str());
- res = HCIStatusCode::UNSPECIFIED_ERROR;
- goto exit;
- }
res = hci.disconnect(hciConnHandle.load(), address, addressType, reason);
if( HCIStatusCode::SUCCESS != res ) {
diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp
index c7a74572..fa5513f1 100644
--- a/src/direct_bt/DBTManager.cpp
+++ b/src/direct_bt/DBTManager.cpp
@@ -346,7 +346,6 @@ DBTManager::DBTManager(const BTMode _defaultBTMode) noexcept
PERF_TS_T0();
- bool ok = true;
// Mandatory
{
MgmtCommand req0(MgmtOpcode::READ_VERSION, MgmtConstU16::MGMT_INDEX_NONE);
@@ -420,16 +419,14 @@ next1:
// Not required: CTOR: const std::lock_guard<std::recursive_mutex> lock(adapterInfos.get_write_mutex());
std::shared_ptr<std::vector<std::shared_ptr<AdapterInfo>>> snapshot = adapterInfos.get_snapshot();
- for(int i=0; ok && i < num_adapter; i++) {
+ for(int i=0; i < num_adapter; i++) {
const uint16_t dev_id = jau::get_uint16(data, 2+i*2, true /* littleEndian */);
std::shared_ptr<AdapterInfo> adapterInfo = initAdapter(dev_id, defaultBTMode);
if( nullptr != adapterInfo ) {
snapshot->push_back(adapterInfo);
DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: %s", i, num_adapter, dev_id, adapterInfo->toString().c_str());
- ok = true;
} else {
DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: FAILED", i, num_adapter, dev_id);
- ok = false;
}
}
// Not required: CTOR: adapterInfos.set_store(std::move(snapshot));
@@ -457,10 +454,14 @@ next1:
PERF_TS_TD("DBTManager::open.ok");
return;
}
+ PERF_TS_TD("DBTManager::ctor.ok");
+ DBG_PRINT("DBTManager::ctor: OK");
+ return;
fail:
close();
- PERF_TS_TD("DBTManager::open.fail");
+ PERF_TS_TD("DBTManager::ctor.fail");
+ DBG_PRINT("DBTManager::ctor: FAIL");
return;
}
@@ -591,7 +592,7 @@ std::shared_ptr<AdapterInfo> DBTManager::getDefaultAdapterInfo() const noexcept
}
}
-int DBTManager::getDefaultAdapterDevId() const noexcept {
+int DBTManager::getDefaultAdapterDevID() const noexcept {
std::shared_ptr<AdapterInfo> ai = getDefaultAdapterInfo();
if( nullptr == ai ) {
return -1;
@@ -818,7 +819,7 @@ bool DBTManager::addMgmtEventCallback(const int dev_id, const MgmtEvent::Opcode
return false;
}
MgmtAdapterEventCallbackList &l = mgmtAdapterEventCallbackLists[static_cast<uint16_t>(opc)];
- /* const bool added = */ l.push_back_unique(MgmtAdapterEventCallback(dev_id, cb), _mgmtAdapterEventCallbackEqComp_ID_CB);
+ /* const bool added = */ l.push_back_unique(MgmtAdapterEventCallback(dev_id, opc, cb), _mgmtAdapterEventCallbackEqComp_ID_CB);
return true;
}
int DBTManager::removeMgmtEventCallback(const MgmtEvent::Opcode opc, const MgmtEventCallback &cb) noexcept {
@@ -827,13 +828,19 @@ int DBTManager::removeMgmtEventCallback(const MgmtEvent::Opcode opc, const MgmtE
return 0;
}
MgmtAdapterEventCallbackList &l = mgmtAdapterEventCallbackLists[static_cast<uint16_t>(opc)];
- return l.erase_matching(MgmtAdapterEventCallback(0, cb), true /* all_matching */, _mgmtAdapterEventCallbackEqComp_CB);
+ return l.erase_matching( MgmtAdapterEventCallback( 0, MgmtEvent::Opcode::INVALID, cb ),
+ true /* all_matching */, _mgmtAdapterEventCallbackEqComp_CB);
}
int DBTManager::removeMgmtEventCallback(const int dev_id) noexcept {
+ if( 0 > dev_id ) {
+ // skip dev_id -1 case, use clearAllMgmtEventCallbacks() here
+ return 0;
+ }
int count = 0;
for(size_t i=0; i<mgmtAdapterEventCallbackLists.size(); i++) {
MgmtAdapterEventCallbackList &l = mgmtAdapterEventCallbackLists[i];
- count += l.erase_matching(MgmtAdapterEventCallback(dev_id, MgmtEventCallback()), true /* all_matching */, _mgmtAdapterEventCallbackEqComp_ID);
+ count += l.erase_matching( MgmtAdapterEventCallback( dev_id, MgmtEvent::Opcode::INVALID, MgmtEventCallback() ),
+ true /* all_matching */, _mgmtAdapterEventCallbackEqComp_ID);
}
return count;
}