diff options
-rw-r--r-- | api/direct_bt/BTTypes.hpp | 34 | ||||
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 4 | ||||
-rw-r--r-- | api/direct_bt/DBTManager.hpp | 6 | ||||
-rw-r--r-- | api/direct_bt/HCIHandler.hpp | 2 | ||||
-rw-r--r-- | api/direct_bt/MgmtTypes.hpp | 17 | ||||
-rw-r--r-- | java/jni/direct_bt/DBTManager.cxx | 2 | ||||
-rw-r--r-- | src/direct_bt/BTTypes.cpp | 43 | ||||
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 28 | ||||
-rw-r--r-- | src/direct_bt/DBTManager.cpp | 26 |
9 files changed, 107 insertions, 55 deletions
diff --git a/api/direct_bt/BTTypes.hpp b/api/direct_bt/BTTypes.hpp index cb48303e..3b590ec0 100644 --- a/api/direct_bt/BTTypes.hpp +++ b/api/direct_bt/BTTypes.hpp @@ -38,12 +38,36 @@ namespace direct_bt { - enum BTMode : uint8_t { - BT_MODE_DUAL = 1, - BT_MODE_BREDR = 2, - BT_MODE_LE = 3 + enum class BTMode : uint8_t { + DUAL = 1, + BREDR = 2, + LE = 3 }; - std::string BTModeString(const BTMode v); + inline uint8_t number(const BTMode rhs) { + return static_cast<uint8_t>(rhs); + } + std::string getBTModeString(const BTMode v); + + /** + * Meta ScanType as derived from BTMode, + * with defined value mask consisting of BDAddressType bits. + * <p> + * This ScanType is natively compatible with DBTManager's implementation + * for start and stop discovery. + * </p> + */ + enum class ScanType : uint8_t { + NONE = 0, + BREDR = 1 << BDAddressType::BDADDR_BREDR, + LE = ( 1 << BDAddressType::BDADDR_LE_PUBLIC ) | ( 1 << BDAddressType::BDADDR_LE_RANDOM ), + DUAL = BREDR | LE + }; + inline uint8_t number(const ScanType rhs) { + return static_cast<uint8_t>(rhs); + } + std::string getScanTypeString(const ScanType v); + + ScanType getScanType(BTMode btMode); /** * HCI Whitelist connection type. diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index d3b3c23a..db9134ba 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -159,7 +159,7 @@ namespace direct_bt { DBTManager& mgmt; std::shared_ptr<AdapterInfo> adapterInfo; NameAndShortName localName; - std::atomic<ScanType> currentScanType; // = ScanType::SCAN_TYPE_NONE + std::atomic<ScanType> currentScanType; // = ScanType::NONE std::atomic<bool> keepDiscoveringAlive; // = false; std::shared_ptr<HCIHandler> hci; @@ -434,7 +434,7 @@ namespace direct_bt { * Returns the discovering state the adapter. It can be modified through startDiscovery(..) and stopDiscovery(). */ bool getDiscovering() const { - return ScanType::SCAN_TYPE_NONE != currentScanType; + return ScanType::NONE != currentScanType; } /** diff --git a/api/direct_bt/DBTManager.hpp b/api/direct_bt/DBTManager.hpp index 7a363bae..cd6aa3b7 100644 --- a/api/direct_bt/DBTManager.hpp +++ b/api/direct_bt/DBTManager.hpp @@ -129,7 +129,6 @@ namespace direct_bt { bool mgmtEvDeviceWhilelistRemovedCB(std::shared_ptr<MgmtEvent> e); bool mgmtEvPinCodeRequestCB(std::shared_ptr<MgmtEvent> e); bool mgmtEvUserPasskeyRequestCB(std::shared_ptr<MgmtEvent> e); - void sendMgmtEvent(std::shared_ptr<MgmtEvent> event); public: /** @@ -172,7 +171,7 @@ namespace direct_bt { } std::string toString() const override { - return "MgmtHandler["+BTModeString(btMode)+", "+std::to_string(adapterInfos.size())+" adapter, "+javaObjectToString()+"]"; + return "MgmtHandler[BTMode "+getBTModeString(btMode)+", "+std::to_string(adapterInfos.size())+" adapter, "+javaObjectToString()+"]"; } /** retrieve information gathered at startup */ @@ -280,6 +279,9 @@ namespace direct_bt { void clearMgmtEventCallbacks(const MgmtEvent::Opcode opc); /** Removes all MgmtEventCallbacks from all MgmtEvent::Opcode lists. */ void clearAllMgmtEventCallbacks(); + + /** Manually send a MgmtEvent to all of its listeners. */ + void sendMgmtEvent(std::shared_ptr<MgmtEvent> event); }; } // namespace direct_bt diff --git a/api/direct_bt/HCIHandler.hpp b/api/direct_bt/HCIHandler.hpp index 08f967f2..082763d1 100644 --- a/api/direct_bt/HCIHandler.hpp +++ b/api/direct_bt/HCIHandler.hpp @@ -214,7 +214,7 @@ namespace direct_bt { return comm.isOpen(); } - std::string toString() const { return "HCIHandler[BTMode "+BTModeString(btMode)+", dev_id "+std::to_string(dev_id)+"]"; } + std::string toString() const { return "HCIHandler[BTMode "+getBTModeString(btMode)+", dev_id "+std::to_string(dev_id)+"]"; } /** * BT Core Spec v5.2: Vol 4, Part E HCI: 7.3.2 Reset command diff --git a/api/direct_bt/MgmtTypes.hpp b/api/direct_bt/MgmtTypes.hpp index 10481378..aa7797d3 100644 --- a/api/direct_bt/MgmtTypes.hpp +++ b/api/direct_bt/MgmtTypes.hpp @@ -177,13 +177,6 @@ namespace direct_bt { PUBLIC_ADDRESS = 0x00000002 }; - enum ScanType : uint8_t { - SCAN_TYPE_NONE = 0, - SCAN_TYPE_BREDR = 1 << BDAddressType::BDADDR_BREDR, - SCAN_TYPE_LE = ( 1 << BDAddressType::BDADDR_LE_PUBLIC ) | ( 1 << BDAddressType::BDADDR_LE_RANDOM ), - SCAN_TYPE_DUAL = SCAN_TYPE_BREDR | SCAN_TYPE_LE - }; - class MgmtCommand { protected: @@ -723,7 +716,7 @@ namespace direct_bt { protected: std::string baseString() const override { - return MgmtEvent::baseString()+", scan-type="+uint8HexString(getScanType(), true)+ + return MgmtEvent::baseString()+", scan-type "+getScanTypeString(getScanType())+ ", enabled "+std::to_string(getEnabled()); } @@ -733,6 +726,14 @@ namespace direct_bt { { checkOpcode(getOpcode(), Opcode::DISCOVERING); } + + MgmtEvtDiscovering(const uint16_t dev_id, const ScanType scanType, const bool enabled) + : MgmtEvent(Opcode::DISCOVERING, dev_id, 1+1) + { + pdu.put_uint8(MGMT_HEADER_SIZE, number(scanType)); + pdu.put_uint8(MGMT_HEADER_SIZE+1, enabled); + } + ScanType getScanType() const { return static_cast<ScanType>( pdu.get_uint8(MGMT_HEADER_SIZE) ); } bool getEnabled() const { return 0 != pdu.get_uint8(MGMT_HEADER_SIZE+1); } diff --git a/java/jni/direct_bt/DBTManager.cxx b/java/jni/direct_bt/DBTManager.cxx index 3a02540f..71cbe591 100644 --- a/java/jni/direct_bt/DBTManager.cxx +++ b/java/jni/direct_bt/DBTManager.cxx @@ -42,7 +42,7 @@ void Java_direct_1bt_tinyb_DBTManager_initImpl(JNIEnv *env, jobject obj, jboolea { directBTJNISettings.setUnifyUUID128Bit(unifyUUID128Bit); try { - DBTManager *manager = &DBTManager::get(BTMode::BT_MODE_LE); // special: static singleton + DBTManager *manager = &DBTManager::get(BTMode::LE); // special: static singleton setInstance<DBTManager>(env, obj, manager); java_exception_check_and_throw(env, E_FILE_LINE); manager->setJavaObject( std::shared_ptr<JavaAnonObj>( new JavaGlobalObj(obj, nullptr) ) ); diff --git a/src/direct_bt/BTTypes.cpp b/src/direct_bt/BTTypes.cpp index 40a994bc..b08b2eb9 100644 --- a/src/direct_bt/BTTypes.cpp +++ b/src/direct_bt/BTTypes.cpp @@ -112,7 +112,7 @@ std::string direct_bt::getBDAddressTypeString(const BDAddressType type) { CHAR_DECL_BDADDRESSTYPE_ENUM(CASE_TO_STRING) default: ; // fall through intended } - return "Unknown address type"; + return "Unknown BDAddressType"; } #define CHAR_DECL_LERANDOMADDRESSTYPE_ENUM(X) \ @@ -194,13 +194,42 @@ static inline const int8_t * const_uint8_to_const_int8_ptr(const uint8_t* p) { return static_cast<const int8_t *>( static_cast<void *>( const_cast<uint8_t*>( p ) ) ); } -std::string direct_bt::BTModeString(const BTMode v) { +std::string direct_bt::getBTModeString(const BTMode v) { switch(v) { - case BTMode::BT_MODE_DUAL: return "BT_MODE_DUAL"; - case BTMode::BT_MODE_BREDR: return "BT_MODE_BREDR"; - case BTMode::BT_MODE_LE: return "BT_MODE_LE"; + case BTMode::DUAL: return "DUAL"; + case BTMode::BREDR: return "BREDR"; + case BTMode::LE: return "LE"; } - return "Unknown BT_MODE"; + return "Unknown BTMode"; +} + +ScanType direct_bt::getScanType(BTMode btMode) { + switch ( btMode ) { + case BTMode::DUAL: + return ScanType::DUAL; + case BTMode::BREDR: + return ScanType::BREDR; + case BTMode::LE: + return ScanType::LE; + default: + throw new IllegalArgumentException("Unsupported BTMode "+getBTModeString(btMode), E_FILE_LINE); + } +} + +#define SCANTYPE_ENUM(X) \ + X(NONE) \ + X(BREDR) \ + X(LE) \ + X(DUAL) \ + +#define SCANTYPE_CASE_TO_STRING(V) case ScanType::V: return #V; + +std::string direct_bt::getScanTypeString(const ScanType v) { + switch(v) { + SCANTYPE_ENUM(SCANTYPE_CASE_TO_STRING) + default: ; // fall through intended + } + return "Unknown ScanType"; } #define APPEARANCECAT_ENUM(X) \ @@ -270,7 +299,7 @@ std::string direct_bt::getAppearanceCatString(const AppearanceCat v) { APPEARANCECAT_ENUM(APPEARANCE_CASE_TO_STRING) default: ; // fall through intended } - return "Unknown"; + return "Unknown AppearanceCat"; } // ************************************************* diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index b38d211e..37a38876 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -155,7 +155,7 @@ bool DBTAdapter::closeHCI() } bool DBTAdapter::validateDevInfo() { - currentScanType = ScanType::SCAN_TYPE_NONE; + currentScanType = ScanType::NONE; keepDiscoveringAlive = false; if( 0 > dev_id ) { @@ -190,19 +190,19 @@ bool DBTAdapter::validateDevInfo() { } DBTAdapter::DBTAdapter() -: mgmt(DBTManager::get(BTMode::BT_MODE_LE)), btMode(mgmt.getBTMode()), dev_id(nullptr != mgmt.getDefaultAdapterInfo() ? 0 : -1) +: mgmt(DBTManager::get(BTMode::LE)), btMode(mgmt.getBTMode()), dev_id(nullptr != mgmt.getDefaultAdapterInfo() ? 0 : -1) { valid = validateDevInfo(); } DBTAdapter::DBTAdapter(EUI48 &mac) -: mgmt(DBTManager::get(BTMode::BT_MODE_LE)), btMode(mgmt.getBTMode()), dev_id(mgmt.findAdapterInfoIdx(mac)) +: mgmt(DBTManager::get(BTMode::LE)), btMode(mgmt.getBTMode()), dev_id(mgmt.findAdapterInfoIdx(mac)) { valid = validateDevInfo(); } DBTAdapter::DBTAdapter(const int dev_id) -: mgmt(DBTManager::get(BTMode::BT_MODE_LE)), btMode(mgmt.getBTMode()), dev_id(dev_id) +: mgmt(DBTManager::get(BTMode::LE)), btMode(mgmt.getBTMode()), dev_id(dev_id) { valid = validateDevInfo(); } @@ -346,12 +346,16 @@ bool DBTAdapter::startDiscovery(const bool keepAlive, const HCILEOwnAddressType { checkValidAdapter(); const std::lock_guard<std::recursive_mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor - if( ScanType::SCAN_TYPE_NONE != currentScanType ) { + if( ScanType::NONE != currentScanType ) { removeDiscoveredDevices(); if( keepDiscoveringAlive == keepAlive ) { - DBG_PRINT("DBTAdapter::startDiscovery: Already discovering, unchanged keepAlive %d -> %d ...", keepDiscoveringAlive.load(), keepAlive); + DBG_PRINT("DBTAdapter::startDiscovery: Already discovering, unchanged keepAlive %d -> %d, currentScanType[native %s, meta %s] ...", + keepDiscoveringAlive.load(), keepAlive, + getScanTypeString(currentNativeScanType).c_str(), getScanTypeString(currentScanType).c_str()); } else { - DBG_PRINT("DBTAdapter::startDiscovery: Already discovering, changed keepAlive %d -> %d ...", keepDiscoveringAlive.load(), keepAlive); + DBG_PRINT("DBTAdapter::startDiscovery: Already discovering, changed keepAlive %d -> %d, currentScanType[native %s, meta %s] ...", + keepDiscoveringAlive.load(), keepAlive, + getScanTypeString(currentNativeScanType).c_str(), getScanTypeString(currentScanType).c_str()); keepDiscoveringAlive = keepAlive; } return true; @@ -375,8 +379,10 @@ void DBTAdapter::startDiscoveryBackground() { void DBTAdapter::stopDiscovery() { const std::lock_guard<std::recursive_mutex> lock(mtx_discovery); // RAII-style acquire and relinquish via destructor keepDiscoveringAlive = false; - if( ScanType::SCAN_TYPE_NONE == currentScanType ) { - DBG_PRINT("DBTAdapter::stopDiscovery: Already disabled"); + if( ScanType::NONE == currentScanType ) { + DBG_PRINT("DBTAdapter::stopDiscovery: Already disabled, keepAlive %d, currentScanType[native %s, meta %s] ...", + keepDiscoveringAlive.load(), + getScanTypeString(currentNativeScanType).c_str(), getScanTypeString(currentScanType).c_str()); return; } DBG_PRINT("DBTAdapter::stopDiscovery: ..."); @@ -462,7 +468,9 @@ std::shared_ptr<DBTDevice> DBTAdapter::findSharedDevice (EUI48 const & mac, cons } std::string DBTAdapter::toString() const { - std::string out("Adapter["+BTModeString(btMode)+", "+getAddressString()+", '"+getName()+"', id="+std::to_string(dev_id)+", "+javaObjectToString()+"]"); + std::string out("Adapter[BTMode "+getBTModeString(btMode)+", "+getAddressString()+", '"+getName()+"', id "+std::to_string(dev_id)+ + ", scanType[native "+getScanTypeString(currentNativeScanType)+", meta "+getScanTypeString(currentScanType)+"]" + ", "+javaObjectToString()+"]"); std::vector<std::shared_ptr<DBTDevice>> devices = getDiscoveredDevices(); if(devices.size() > 0 ) { out.append("\n"); diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp index a5fa8865..0818455b 100644 --- a/src/direct_bt/DBTManager.cpp +++ b/src/direct_bt/DBTManager.cpp @@ -221,17 +221,17 @@ std::shared_ptr<AdapterInfo> DBTManager::initAdapter(const uint16_t dev_id, cons } switch ( btMode ) { - case BTMode::BT_MODE_DUAL: + case BTMode::DUAL: setMode(dev_id, MgmtOpcode::SET_SSP, 1); setMode(dev_id, MgmtOpcode::SET_BREDR, 1); setMode(dev_id, MgmtOpcode::SET_LE, 1); break; - case BTMode::BT_MODE_BREDR: + case BTMode::BREDR: setMode(dev_id, MgmtOpcode::SET_SSP, 1); setMode(dev_id, MgmtOpcode::SET_BREDR, 1); setMode(dev_id, MgmtOpcode::SET_LE, 0); break; - case BTMode::BT_MODE_LE: + case BTMode::LE: setMode(dev_id, MgmtOpcode::SET_SSP, 0); setMode(dev_id, MgmtOpcode::SET_BREDR, 0); setMode(dev_id, MgmtOpcode::SET_LE, 1); @@ -489,25 +489,13 @@ bool DBTManager::setMode(const int dev_id, const MgmtOpcode opc, const uint8_t m } ScanType DBTManager::startDiscovery(const int dev_id) { - ScanType scanType; - switch ( btMode ) { - case BTMode::BT_MODE_DUAL: - scanType = ScanType::SCAN_TYPE_DUAL; - break; - case BTMode::BT_MODE_BREDR: - scanType = ScanType::SCAN_TYPE_BREDR; - break; - case BTMode::BT_MODE_LE: - scanType = ScanType::SCAN_TYPE_LE; - break; - } - return startDiscovery(dev_id, scanType); + return startDiscovery(dev_id, getScanType(btMode)); } ScanType DBTManager::startDiscovery(const int dev_id, const ScanType scanType) { - MgmtUint8Cmd req(MgmtOpcode::START_DISCOVERY, dev_id, scanType); + MgmtUint8Cmd req(MgmtOpcode::START_DISCOVERY, dev_id, number(scanType)); std::shared_ptr<MgmtEvent> res = sendWithReply(req); - ScanType type = ScanType::SCAN_TYPE_NONE; + ScanType type = ScanType::NONE; if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) { const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get()); if( MgmtStatus::SUCCESS == res1.getStatus() && 1 == res1.getDataSize() ) { @@ -517,7 +505,7 @@ ScanType DBTManager::startDiscovery(const int dev_id, const ScanType scanType) { return type; } bool DBTManager::stopDiscovery(const int dev_id, const ScanType type) { - MgmtUint8Cmd req(MgmtOpcode::STOP_DISCOVERY, dev_id, type); + MgmtUint8Cmd req(MgmtOpcode::STOP_DISCOVERY, dev_id, number(type)); std::shared_ptr<MgmtEvent> res = sendWithReply(req); if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) { const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get()); |