summaryrefslogtreecommitdiffstats
path: root/src/direct_bt/L2CAPComm.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/direct_bt/L2CAPComm.cpp')
-rw-r--r--src/direct_bt/L2CAPComm.cpp98
1 files changed, 80 insertions, 18 deletions
diff --git a/src/direct_bt/L2CAPComm.cpp b/src/direct_bt/L2CAPComm.cpp
index 3eeae8a6..049acfe7 100644
--- a/src/direct_bt/L2CAPComm.cpp
+++ b/src/direct_bt/L2CAPComm.cpp
@@ -116,7 +116,8 @@ L2CAPComm::L2CAPComm(const EUI48& adapterAddress_, const uint16_t psm_, const ui
is_open(false), has_ioerror(false), interrupt_flag(false), tid_connect(0), tid_read(0)
{ }
-bool L2CAPComm::open(const DBTDevice& device) {
+
+bool L2CAPComm::open(const DBTDevice& device, const BTSecurityLevel sec_level) {
bool expOpen = false; // C++11, exp as value since C++20
if( !is_open.compare_exchange_strong(expOpen, true) ) {
@@ -144,6 +145,14 @@ bool L2CAPComm::open(const DBTDevice& device) {
goto failure; // open failed
}
+#if USE_LINUX_BT_SECURITY
+ if( BTSecurityLevel::UNSET < sec_level ) {
+ if( !setBTSecurityLevelImpl(sec_level) ) {
+ goto failure; // sec_level failed
+ }
+ }
+#endif
+
tid_connect = pthread_self(); // temporary safe tid to allow interruption
// actual request to connect to remote device
@@ -246,25 +255,78 @@ bool L2CAPComm::setBTSecurityLevel(const BTSecurityLevel sec_level) {
}
const std::lock_guard<std::recursive_mutex> lock(mtx_write); // RAII-style acquire and relinquish via destructor
- if( BTSecurityLevel::NONE < sec_level ) {
- #if USE_LINUX_BT_SECURITY
- struct bt_security bt_sec;
- int result;
-
- bzero(&bt_sec, sizeof(bt_sec));
- bt_sec.level = direct_bt::number(sec_level);
- result = setsockopt(socket_descriptor, SOL_BLUETOOTH, BT_SECURITY, &bt_sec, sizeof(bt_sec));
- if ( 0 == result ) {
- DBG_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s, success", getBTSecurityLevelString(sec_level).c_str());
- return true;
- } else {
- ERR_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s, failed", getBTSecurityLevelString(sec_level).c_str());
- }
- #else
- DBG_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s, not implemented", getBTSecurityLevelString(sec_level).c_str());
- #endif
+ return setBTSecurityLevelImpl(sec_level);
+}
+
+bool L2CAPComm::setBTSecurityLevelImpl(const BTSecurityLevel sec_level) {
+ if( BTSecurityLevel::NONE > sec_level ) {
+ DBG_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s, not set", getBTSecurityLevelString(sec_level).c_str());
+ return false;
+ }
+
+#if USE_LINUX_BT_SECURITY
+ struct bt_security bt_sec;
+ int result;
+
+ BTSecurityLevel old_sec_level = BTSecurityLevel::UNSET;
+ if( jau::environment::get().debug ) {
+ getBTSecurityLevelImpl(old_sec_level);
+ }
+ bzero(&bt_sec, sizeof(bt_sec));
+ bt_sec.level = direct_bt::number(sec_level);
+ result = setsockopt(socket_descriptor, SOL_BLUETOOTH, BT_SECURITY, &bt_sec, sizeof(bt_sec));
+ if ( 0 == result ) {
+ DBG_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s -> %s, success",
+ getBTSecurityLevelString(old_sec_level).c_str(), getBTSecurityLevelString(sec_level).c_str());
+ return true;
+ } else {
+ ERR_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s -> %s, failed",
+ getBTSecurityLevelString(old_sec_level).c_str(), getBTSecurityLevelString(sec_level).c_str());
+ return false;
+ }
+#else
+ DBG_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s, not implemented", getBTSecurityLevelString(sec_level).c_str());
+ return false;
+#endif
+}
+
+bool L2CAPComm::getBTSecurityLevel(BTSecurityLevel & sec_level) {
+ if( !is_open ) {
+ DBG_PRINT("L2CAPComm::getBTSecurityLevel: Not connected: %s, dd %d, %s, psm %u, cid %u",
+ getStateString().c_str(), socket_descriptor.load(), deviceString.c_str(), psm, cid);
+ return false;
+ }
+ const std::lock_guard<std::recursive_mutex> lock(mtx_write); // RAII-style acquire and relinquish via destructor
+
+ return getBTSecurityLevelImpl(sec_level);
+}
+
+bool L2CAPComm::getBTSecurityLevelImpl(BTSecurityLevel & sec_level) {
+#if USE_LINUX_BT_SECURITY
+ struct bt_security bt_sec;
+ socklen_t optlen = sizeof(bt_sec);
+ int result;
+
+ bzero(&bt_sec, sizeof(bt_sec));
+ result = getsockopt(socket_descriptor, SOL_BLUETOOTH, BT_SECURITY, &bt_sec, &optlen);
+ if ( 0 == result ) {
+ if( optlen == sizeof(bt_sec) ) {
+ sec_level = static_cast<BTSecurityLevel>(bt_sec.level);
+ DBG_PRINT("L2CAPComm::getBTSecurityLevel: sec_level %s, success", getBTSecurityLevelString(sec_level).c_str());
+ return true;
+ } else {
+ ERR_PRINT("L2CAPComm::getBTSecurityLevel: sec_level %s, failed. Returned size %zd != %zd ",
+ getBTSecurityLevelString(sec_level).c_str(), optlen, sizeof(bt_sec));
+ return false;
+ }
+ } else {
+ ERR_PRINT("L2CAPComm::getBTSecurityLevel: sec_level %s, failed. Result %d", getBTSecurityLevelString(sec_level).c_str(), result);
+ return false;
}
+#else
+ DBG_PRINT("L2CAPComm::setBTSecurityLevel: sec_level %s, not implemented", getBTSecurityLevelString(sec_level).c_str());
return false;
+#endif
}
jau::snsize_t L2CAPComm::read(uint8_t* buffer, const jau::nsize_t capacity) {