diff options
author | Sven Gothel <[email protected]> | 2021-09-24 09:13:23 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2021-09-24 09:13:23 +0200 |
commit | e73bfa6898ad1fdd813f024990a70e81bc2d70ba (patch) | |
tree | 93388ea6a3aa0670741b1b5be0fdb950de39a155 /src/direct_bt/HCIHandler.cpp | |
parent | afd62e2ee8dddabc32658c4a0eb967ec7a876e01 (diff) |
Added HCIHandler::check_open_connection(..): Reuse same state and param checking, reduce code
Diffstat (limited to 'src/direct_bt/HCIHandler.cpp')
-rw-r--r-- | src/direct_bt/HCIHandler.cpp | 124 |
1 files changed, 43 insertions, 81 deletions
diff --git a/src/direct_bt/HCIHandler.cpp b/src/direct_bt/HCIHandler.cpp index 1b9bb0c7..ed0d1b99 100644 --- a/src/direct_bt/HCIHandler.cpp +++ b/src/direct_bt/HCIHandler.cpp @@ -782,38 +782,53 @@ bool HCIHandler::initSupCommands() noexcept { } } -HCIStatusCode HCIHandler::le_read_remote_features(const uint16_t conn_handle, const BDAddressAndType& peerAddressAndType) noexcept { +HCIStatusCode HCIHandler::check_open_connection(const std::string& caller, + const uint16_t conn_handle, const BDAddressAndType& peerAddressAndType, + const bool addUntrackedConn) { if( !isOpen() ) { - ERR_PRINT("HCIHandler::le_read_remote_features: Not connected %s", toString().c_str()); - return HCIStatusCode::INTERNAL_FAILURE; + ERR_PRINT("HCIHandler::%s: Not connected %s", caller.c_str(), toString().c_str()); + return HCIStatusCode::DISCONNECTED; } if( 0 == conn_handle ) { - ERR_PRINT("HCIHandler::le_read_remote_features: Null conn_handle given address%s (drop) - %s", - peerAddressAndType.toString().c_str(), toString().c_str()); + ERR_PRINT("HCIHandler::%s: Null conn_handle given address%s (drop) - %s", + caller.c_str(), peerAddressAndType.toString().c_str(), toString().c_str()); return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; } - { - const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor - HCIConnectionRef conn = findTrackerConnection(conn_handle); - if( nullptr == conn ) { - // called w/o being connected through this HCIHandler - ERR_PRINT("HCIHandler::le_read_remote_features: Not tracked handle %s (address%s) (drop) - %s", - jau::to_hexstring(conn_handle).c_str(), - peerAddressAndType.toString().c_str(), toString().c_str()); - return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; - } else if( !conn->equals(peerAddressAndType) ) { - ERR_PRINT("HCIHandler::le_read_remote_features: Mismatch given address%s and tracked %s (drop) - %s", - peerAddressAndType.toString().c_str(), + const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor + HCIConnectionRef conn = findTrackerConnection(conn_handle); + if( nullptr == conn ) { + // called w/o being connected through this HCIHandler + if( addUntrackedConn ) { + // add unknown connection to tracker + conn = addOrUpdateTrackerConnection(peerAddressAndType, conn_handle); + WORDY_PRINT("HCIHandler::%s: Not tracked address%s, added %s - %s", + caller.c_str(), peerAddressAndType.toString().c_str(), conn->toString().c_str(), toString().c_str()); + } else { + ERR_PRINT("HCIHandler::%s: Not tracked handle %s (address%s) (drop) - %s", + caller.c_str(), jau::to_hexstring(conn_handle).c_str(), + peerAddressAndType.toString().c_str(), toString().c_str()); return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; } - DBG_PRINT("HCIHandler::le_read_remote_features: address%s, handle %s, %s - %s", - peerAddressAndType.toString().c_str(), - jau::to_hexstring(conn_handle).c_str(), + } else if( !conn->equals(peerAddressAndType) ) { + ERR_PRINT("HCIHandler::%s: Mismatch given address%s and tracked %s (drop) - %s", + caller.c_str(), peerAddressAndType.toString().c_str(), conn->toString().c_str(), toString().c_str()); + return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; } - HCIStatusCode status; + DBG_PRINT("HCIHandler::%s: address%s, handle %s, %s - %s", + caller.c_str(), peerAddressAndType.toString().c_str(), + jau::to_hexstring(conn_handle).c_str(), + conn->toString().c_str(), toString().c_str()); + + return HCIStatusCode::SUCCESS; +} +HCIStatusCode HCIHandler::le_read_remote_features(const uint16_t conn_handle, const BDAddressAndType& peerAddressAndType) noexcept { + HCIStatusCode status = check_open_connection("le_read_remote_features", conn_handle, peerAddressAndType); + if( HCIStatusCode::SUCCESS != status ) { + return status; + } HCIStructCommand<hci_cp_le_read_remote_features> req0(HCIOpcode::LE_READ_REMOTE_FEATURES); hci_cp_le_read_remote_features * cp = req0.getWStruct(); cp->handle = jau::cpu_to_le(conn_handle); @@ -1348,38 +1363,11 @@ HCIStatusCode HCIHandler::create_conn(const EUI48 &bdaddr, HCIStatusCode HCIHandler::disconnect(const uint16_t conn_handle, const BDAddressAndType& peerAddressAndType, const HCIStatusCode reason) noexcept { - if( !isOpen() ) { - ERR_PRINT("HCIHandler::create_conn: Not connected %s", toString().c_str()); - return HCIStatusCode::INTERNAL_FAILURE; - } - if( 0 == conn_handle ) { - ERR_PRINT("HCIHandler::disconnect: Null conn_handle given address%s (drop) - %s", - peerAddressAndType.toString().c_str(), toString().c_str()); - return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; - } - { - const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor - HCIConnectionRef conn = findTrackerConnection(conn_handle); - if( nullptr == conn ) { - // disconnect called w/o being connected through this HCIHandler - conn = addOrUpdateTrackerConnection(peerAddressAndType, conn_handle); - WORDY_PRINT("HCIHandler::disconnect: Not tracked address%s, added %s - %s", - peerAddressAndType.toString().c_str(), - conn->toString().c_str(), toString().c_str()); - } else if( !conn->equals(peerAddressAndType) ) { - ERR_PRINT("HCIHandler::disconnect: Mismatch given address%s and tracked %s (drop) - %s", - peerAddressAndType.toString().c_str(), - conn->toString().c_str(), toString().c_str()); - return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; - } - DBG_PRINT("HCIHandler::disconnect: address%s, handle %s, %s - %s", - peerAddressAndType.toString().c_str(), - jau::to_hexstring(conn_handle).c_str(), - conn->toString().c_str(), toString().c_str()); + HCIStatusCode status = check_open_connection("disconnect", conn_handle, peerAddressAndType, true /* addUntrackedConn */); + if( HCIStatusCode::SUCCESS != status ) { + return status; } - HCIStatusCode status; - // Always issue DISCONNECT command, even in case of an ioError (lost-connection), // see Issue #124 fast re-connect on CSR adapter. // This will always notify the adapter of a disconnected device. @@ -1402,35 +1390,11 @@ HCIStatusCode HCIHandler::le_read_phy(const uint16_t conn_handle, const BDAddres resRx = LE_PHYs::NONE; resTx = LE_PHYs::NONE; - if( !isOpen() ) { - ERR_PRINT("HCIHandler::le_read_phy: Not connected %s", toString().c_str()); - return HCIStatusCode::INTERNAL_FAILURE; - } - if( 0 == conn_handle ) { - ERR_PRINT("HCIHandler::le_read_phy: Null conn_handle given address%s (drop) - %s", - peerAddressAndType.toString().c_str(), toString().c_str()); - return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; - } - { - const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor - HCIConnectionRef conn = findTrackerConnection(conn_handle); - if( nullptr == conn ) { - // called w/o being connected through this HCIHandler - ERR_PRINT("HCIHandler::le_read_phy: Not tracked handle %s (address%s) (drop) - %s", - jau::to_hexstring(conn_handle).c_str(), - peerAddressAndType.toString().c_str(), toString().c_str()); - return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; - } else if( !conn->equals(peerAddressAndType) ) { - ERR_PRINT("HCIHandler::le_read_phy: Mismatch given address%s and tracked %s (drop) - %s", - peerAddressAndType.toString().c_str(), - conn->toString().c_str(), toString().c_str()); - return HCIStatusCode::INVALID_HCI_COMMAND_PARAMETERS; - } - DBG_PRINT("HCIHandler::le_read_phy: address%s, handle %s, %s - %s", - peerAddressAndType.toString().c_str(), - jau::to_hexstring(conn_handle).c_str(), - conn->toString().c_str(), toString().c_str()); + HCIStatusCode status = check_open_connection("le_read_phy", conn_handle, peerAddressAndType); + if( HCIStatusCode::SUCCESS != status ) { + return status; } + struct hci_cp_le_read_phy { __le16 handle; } __packed; @@ -1441,8 +1405,6 @@ HCIStatusCode HCIHandler::le_read_phy(const uint16_t conn_handle, const BDAddres __u8 rx_phys; } __packed; - HCIStatusCode status; - HCIStructCommand<hci_cp_le_read_phy> req0(HCIOpcode::LE_READ_PHY); hci_cp_le_read_phy * cp = req0.getWStruct(); cp->handle = jau::cpu_to_le(conn_handle); |