summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2020-07-29 17:46:28 +0200
committerSven Gothel <[email protected]>2020-07-29 17:46:28 +0200
commit7de95a3a7471703a022c67005449c96846fdb35a (patch)
treef4f86fe80b998db183aa2d1a4742d2c3c6083a6c /src
parentae16045cfaaac963331bbc3aaf42b3117f890125 (diff)
Refine enum BTMode and ScanType: Elevate to 'enum class', shorten value definition and add getScanTypeString(..) getScanType(BTMode)
Diffstat (limited to 'src')
-rw-r--r--src/direct_bt/BTTypes.cpp43
-rw-r--r--src/direct_bt/DBTAdapter.cpp28
-rw-r--r--src/direct_bt/DBTManager.cpp26
3 files changed, 61 insertions, 36 deletions
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());