diff options
author | Sven Gothel <[email protected]> | 2023-10-20 03:40:23 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-10-20 03:40:23 +0200 |
commit | 252ae3889fc14432e032731fef3d4a24d17d32b1 (patch) | |
tree | 3f2eaf00e998651266e55142808b4f7c0de71db8 /src/direct_bt | |
parent | e233b445aab99c9ac5eb167aa3c756fedbdeaa27 (diff) |
MgmtPinCodeReplyCmd: Use given pin_code size and add std::string variant, zero pin-code array first; Support PinCode throughout BTManager/BTDevice.
Diffstat (limited to 'src/direct_bt')
-rw-r--r-- | src/direct_bt/BTDevice.cpp | 44 | ||||
-rw-r--r-- | src/direct_bt/BTManager.cpp | 30 |
2 files changed, 73 insertions, 1 deletions
diff --git a/src/direct_bt/BTDevice.cpp b/src/direct_bt/BTDevice.cpp index ad837f55..fc75e980 100644 --- a/src/direct_bt/BTDevice.cpp +++ b/src/direct_bt/BTDevice.cpp @@ -1724,6 +1724,48 @@ bool BTDevice::isConnSecurityAutoEnabled() const noexcept { return SMPIOCapability::UNSET != pairing_data.ioCap_auto; } +HCIStatusCode BTDevice::setPairingPINCode(const std::string& pinCode) noexcept { + const std::unique_lock<std::recursive_mutex> lock_pairing(mtx_pairing); // RAII-style acquire and relinquish via destructor + + if( !isSMPPairingAllowingInput(pairing_data.state, SMPPairingState::PASSKEY_EXPECTED) && + !isSMPPairingAllowingInput(pairing_data.state, SMPPairingState::KEY_DISTRIBUTION) ) + { + WARN_PRINT("BTDevice:mgmt:SMP: PINCODE '%s', state %s, wrong state", pinCode.c_str(), to_string(pairing_data.state).c_str()); + } + if constexpr ( USE_LINUX_BT_SECURITY ) { + const BTManagerRef& mngr = adapter.getManager(); + MgmtStatus res = mngr->userPINCodeReply(adapter.dev_id, addressAndType, pinCode); + DBG_PRINT("BTDevice:mgmt:SMP: PINCODE '%s', state %s, result %s", + pinCode.c_str(), to_string(pairing_data.state).c_str(), to_string(res).c_str()); + return HCIStatusCode::SUCCESS; + } else if constexpr ( SMP_SUPPORTED_BY_OS ) { + return HCIStatusCode::NOT_SUPPORTED; + } else { + return HCIStatusCode::NOT_SUPPORTED; + } +} + +HCIStatusCode BTDevice::setPairingPINCodeNegative() noexcept { + const std::unique_lock<std::recursive_mutex> lock_pairing(mtx_pairing); // RAII-style acquire and relinquish via destructor + + if( !isSMPPairingAllowingInput(pairing_data.state, SMPPairingState::PASSKEY_EXPECTED) && + !isSMPPairingAllowingInput(pairing_data.state, SMPPairingState::KEY_DISTRIBUTION) ) + { + WARN_PRINT("BTDevice:mgmt:SMP: PINCODE_NEGATIVE, state %s, wrong state", to_string(pairing_data.state).c_str()); + } + if constexpr ( USE_LINUX_BT_SECURITY ) { + const BTManagerRef& mngr = adapter.getManager(); + MgmtStatus res = mngr->userPINCodeNegativeReply(adapter.dev_id, addressAndType); + DBG_PRINT("BTDevice:mgmt:SMP: PINCODE NEGATIVE, state %s, result %s", + to_string(pairing_data.state).c_str(), to_string(res).c_str()); + return HCIStatusCode::SUCCESS; + } else if constexpr ( SMP_SUPPORTED_BY_OS ) { + return HCIStatusCode::NOT_SUPPORTED; + } else { + return HCIStatusCode::NOT_SUPPORTED; + } +} + HCIStatusCode BTDevice::setPairingPasskey(const uint32_t passkey) noexcept { const std::unique_lock<std::recursive_mutex> lock_pairing(mtx_pairing); // RAII-style acquire and relinquish via destructor @@ -1735,7 +1777,7 @@ HCIStatusCode BTDevice::setPairingPasskey(const uint32_t passkey) noexcept { if constexpr ( USE_LINUX_BT_SECURITY ) { const BTManagerRef& mngr = adapter.getManager(); MgmtStatus res = mngr->userPasskeyReply(adapter.dev_id, addressAndType, passkey); - DBG_PRINT("BTDevice:mgmt:SMP: PASSKEY '%d', state %s, result %s", + DBG_PRINT("BTDevice:mgmt:SMP: PASSKEY '%u', state %s, result %s", passkey, to_string(pairing_data.state).c_str(), to_string(res).c_str()); return HCIStatusCode::SUCCESS; } else if constexpr ( SMP_SUPPORTED_BY_OS ) { diff --git a/src/direct_bt/BTManager.cpp b/src/direct_bt/BTManager.cpp index 733f9cfd..7db3b425 100644 --- a/src/direct_bt/BTManager.cpp +++ b/src/direct_bt/BTManager.cpp @@ -876,6 +876,36 @@ HCIStatusCode BTManager::uploadLinkKey(const uint16_t dev_id, const BDAddressAnd } } +MgmtStatus BTManager::userPINCodeReply(const uint16_t dev_id, const BDAddressAndType & addressAndType, const std::string& pinCode) noexcept { + if constexpr ( USE_LINUX_BT_SECURITY ) { + MgmtPinCodeReplyCmd cmd(dev_id, addressAndType, pinCode); + std::unique_ptr<MgmtEvent> res = sendWithReply(cmd); + if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) { + const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get()); + // FIXME: Analyze address + addressType result? + return res1.getStatus(); + } + return MgmtStatus::TIMEOUT; + } else { + return MgmtStatus::NOT_SUPPORTED; + } +} + +MgmtStatus BTManager::userPINCodeNegativeReply(const uint16_t dev_id, const BDAddressAndType & addressAndType) noexcept { + if constexpr ( USE_LINUX_BT_SECURITY ) { + MgmtPinCodeNegativeReplyCmd cmd(dev_id, addressAndType); + std::unique_ptr<MgmtEvent> res = sendWithReply(cmd); + if( nullptr != res && res->getOpcode() == MgmtEvent::Opcode::CMD_COMPLETE ) { + const MgmtEvtCmdComplete &res1 = *static_cast<const MgmtEvtCmdComplete *>(res.get()); + // FIXME: Analyze address + addressType result? + return res1.getStatus(); + } + return MgmtStatus::TIMEOUT; + } else { + return MgmtStatus::NOT_SUPPORTED; + } +} + MgmtStatus BTManager::userPasskeyReply(const uint16_t dev_id, const BDAddressAndType & addressAndType, const uint32_t passkey) noexcept { if constexpr ( USE_LINUX_BT_SECURITY ) { MgmtUserPasskeyReplyCmd cmd(dev_id, addressAndType, passkey); |