diff options
author | Sven Gothel <[email protected]> | 2020-11-11 09:05:58 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-11-11 09:05:58 +0100 |
commit | 869def483191c4f74f2ec586c3484624e7fe24e3 (patch) | |
tree | ffe20a03cd581364d76d864b5fc76186cf3bcf9a | |
parent | 84693a2460bf976db5af57cfa432a670e32ef5c8 (diff) |
Generalize SMPMsg: HCISMPSecurityReqCallbackList -> HCISMPMsgCallbackList (access all) and forward to matching DBTDevice
-rw-r--r-- | api/direct_bt/DBTAdapter.hpp | 2 | ||||
-rw-r--r-- | api/direct_bt/DBTDevice.hpp | 1 | ||||
-rw-r--r-- | api/direct_bt/HCIHandler.hpp | 10 | ||||
-rw-r--r-- | src/direct_bt/DBTAdapter.cpp | 24 | ||||
-rw-r--r-- | src/direct_bt/DBTDevice.cpp | 6 | ||||
-rw-r--r-- | src/direct_bt/HCIHandler.cpp | 44 |
6 files changed, 49 insertions, 38 deletions
diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 9c0163d5..35081820 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -254,7 +254,7 @@ namespace direct_bt { bool mgmtEvDeviceDiscoveringAny(std::shared_ptr<MgmtEvent> e, const bool hciSourced) noexcept; - bool smpSecurityReqCallback(const EUI48& address, BDAddressType addressType, uint16_t handle, std::shared_ptr<const SMPPDUMsg> msg) noexcept; + bool hciSMPMsgCallback(const EUI48& address, BDAddressType addressType, uint16_t handle, std::shared_ptr<const SMPPDUMsg> msg) noexcept; void startDiscoveryBackground() noexcept; void checkDiscoveryState() noexcept; diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp index 74572bb7..790522db 100644 --- a/api/direct_bt/DBTDevice.hpp +++ b/api/direct_bt/DBTDevice.hpp @@ -94,6 +94,7 @@ namespace direct_bt { void notifyDisconnected() noexcept; void notifyConnected(const uint16_t handle) noexcept; + void notifySMPMsg(std::shared_ptr<const SMPPDUMsg> msg) noexcept; /** * Returns a newly established GATT connection. diff --git a/api/direct_bt/HCIHandler.hpp b/api/direct_bt/HCIHandler.hpp index ee94de19..aed5ee08 100644 --- a/api/direct_bt/HCIHandler.hpp +++ b/api/direct_bt/HCIHandler.hpp @@ -148,8 +148,8 @@ namespace direct_bt { }; typedef jau::FunctionDef<bool, const EUI48& /* address */, BDAddressType /* addressType */, - uint16_t /* handle */, std::shared_ptr<const SMPPDUMsg>> HCISMPSecurityReqCallback; - typedef jau::cow_vector<HCISMPSecurityReqCallback> HCISMPSecurityReqCallbackList; + uint16_t /* connectionHandle */, std::shared_ptr<const SMPPDUMsg>> HCISMPMsgCallback; + typedef jau::cow_vector<HCISMPMsgCallback> HCISMPMsgCallbackList; /** * A thread safe singleton handler of the HCI control channel to one controller (BT adapter) @@ -301,7 +301,7 @@ namespace direct_bt { return static_cast<uint16_t>(opc) < mgmtEventCallbackLists.size(); } - HCISMPSecurityReqCallbackList smpSecurityReqCallbackList; + HCISMPMsgCallbackList hciSMPMsgCallbackList; std::shared_ptr<MgmtEvent> translate(std::shared_ptr<HCIEvent> ev) noexcept; @@ -546,8 +546,8 @@ namespace direct_bt { /** Removes all MgmtEventCallbacks from the to the named MgmtEvent::Opcode list. */ void clearMgmtEventCallbacks(const MgmtEvent::Opcode opc) noexcept; - void addSMPSecurityReqCallback(const HCISMPSecurityReqCallback & l); - int removeSMPSecurityReqCallback(const HCISMPSecurityReqCallback & l); + void addSMPMsgCallback(const HCISMPMsgCallback & l); + int removeSMPMsgCallback(const HCISMPMsgCallback & l); /** Removes all MgmtEventCallbacks from all MgmtEvent::Opcode lists and all SMPSecurityReqCallbacks. */ void clearAllCallbacks() noexcept; diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index a8e8141d..78d6229b 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -199,7 +199,7 @@ bool DBTAdapter::validateDevInfo() noexcept { ERR_PRINT("Could not add all required MgmtEventCallbacks to HCIHandler: %s of %s", hci.toString().c_str(), toString().c_str()); return false; // dtor local HCIHandler w/ closing } - hci.addSMPSecurityReqCallback(jau::bindMemberFunc(this, &DBTAdapter::smpSecurityReqCallback)); + hci.addSMPMsgCallback(jau::bindMemberFunc(this, &DBTAdapter::hciSMPMsgCallback)); return true; @@ -1004,7 +1004,7 @@ bool DBTAdapter::mgmtEvConnectFailedHCI(std::shared_ptr<MgmtEvent> e) noexcept { }); removeDiscoveredDevice(*device); // ensure device will cause a deviceFound event after disconnect } else { - WORDY_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): %s\n -> Device not tracked", + WORDY_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): Device not tracked: %s", dev_id, event.toString().c_str()); } return true; @@ -1042,7 +1042,7 @@ bool DBTAdapter::mgmtEvDeviceDisconnectedHCI(std::shared_ptr<MgmtEvent> e) noexc }); removeDiscoveredDevice(*device); // ensure device will cause a deviceFound event after disconnect } else { - WORDY_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): %s\n -> Device not tracked", + WORDY_PRINT("DBTAdapter::EventHCI:DeviceDisconnected(dev_id %d): Device not tracked: %s", dev_id, event.toString().c_str()); } return true; @@ -1145,9 +1145,21 @@ bool DBTAdapter::mgmtEvDeviceFoundHCI(std::shared_ptr<MgmtEvent> e) noexcept { return true; } -bool DBTAdapter::smpSecurityReqCallback(const EUI48& address, BDAddressType addressType, uint16_t handle, std::shared_ptr<const SMPPDUMsg> msg) noexcept { - DBG_PRINT("DBTAdapter:smp:SecurityReq: %s for address[%s, %s], handle %s", - msg->toString().c_str(), address.toString().c_str(), getBDAddressTypeString(addressType).c_str(), jau::uint16HexString(handle).c_str()); +bool DBTAdapter::hciSMPMsgCallback(const EUI48& address, BDAddressType addressType, uint16_t connectionHandle, std::shared_ptr<const SMPPDUMsg> msg) noexcept { + std::shared_ptr<DBTDevice> device = findConnectedDevice(address, addressType); + if( nullptr != device ) { + if( device->getConnectionHandle() != connectionHandle ) { + WORDY_PRINT("DBTAdapter:hci:SMP: dev_id %d: ConnHandle mismatch address[%s, %s], connHandle %s, %s\n -> %s", + dev_id, address.toString().c_str(), getBDAddressTypeString(addressType).c_str(), + jau::uint16HexString(connectionHandle).c_str(), msg->toString().c_str(), device->toString().c_str()); + } else { + device->notifySMPMsg(msg); + } + } else { + WORDY_PRINT("DBTAdapter:hci:SMP: dev_id %d: Device not tracked: address[%s, %s], connHandle %s, %s", + dev_id, address.toString().c_str(), getBDAddressTypeString(addressType).c_str(), + jau::uint16HexString(connectionHandle).c_str(), msg->toString().c_str()); + } return true; } diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp index d661336e..810ebe44 100644 --- a/src/direct_bt/DBTDevice.cpp +++ b/src/direct_bt/DBTDevice.cpp @@ -138,7 +138,7 @@ std::string DBTDevice::toString(bool includeDiscoveredServices) const noexcept { std::string msdstr = nullptr != advMSD ? advMSD->toString() : "MSD[null]"; std::string out("Device[address["+getAddressString()+", "+getBDAddressTypeString(getAddressType())+leaddrtype+"], name['"+name+ "'], age[total "+std::to_string(t0-ts_creation)+", ldisc "+std::to_string(t0-ts_last_discovery)+", lup "+std::to_string(t0-ts_last_update)+ - "]ms, connected["+std::to_string(allowDisconnect)+"/"+std::to_string(isConnected)+", "+jau::uint16HexString(hciConnHandle)+"], rssi "+std::to_string(getRSSI())+ + "]ms, connected["+std::to_string(allowDisconnect)+"/"+std::to_string(isConnected)+", handle "+jau::uint16HexString(hciConnHandle)+"], rssi "+std::to_string(getRSSI())+ ", tx-power "+std::to_string(tx_power)+ ", appearance "+jau::uint16HexString(static_cast<uint16_t>(appearance))+" ("+getAppearanceCatString(appearance)+ "), "+msdstr+", "+javaObjectToString()+"]"); @@ -397,6 +397,10 @@ void DBTDevice::notifyConnected(const uint16_t handle) noexcept { } } +void DBTDevice::notifySMPMsg(std::shared_ptr<const SMPPDUMsg> msg) noexcept { + DBG_PRINT("DBTDevice::notifySMPMsg: %s, %s", msg->toString().c_str(), toString().c_str()); +} + void DBTDevice::processNotifyConnectedOffThread() { DBG_PRINT("DBTDevice::processNotifyConnectedOffThread: %s", toString().c_str()); bool res0 = connectSMP(); diff --git a/src/direct_bt/HCIHandler.cpp b/src/direct_bt/HCIHandler.cpp index bd5098c3..f0c882eb 100644 --- a/src/direct_bt/HCIHandler.cpp +++ b/src/direct_bt/HCIHandler.cpp @@ -305,24 +305,18 @@ void HCIHandler::hciReaderThreadImpl() noexcept { HCIACLData::l2cap_frame l2cap = acldata->getL2CAPFrame(); std::shared_ptr<const SMPPDUMsg> smpPDU = l2cap.getSMPPDUMsg(); if( nullptr != smpPDU ) { - const SMPPDUMsg::Opcode opc = smpPDU->getOpcode(); - - if( SMPPDUMsg::Opcode::SECURITY_REQUEST == opc ) { - const uint16_t conn_handle = l2cap.handle; - HCIConnectionRef conn = findTrackerConnection(conn_handle); - - if( nullptr != conn ) { - COND_PRINT(env.DEBUG_EVENT, "HCIHandler-IO RECV (ACL.SMP) %s for %s", - smpPDU->toString().c_str(), conn->toString().c_str()); - jau::for_each_cow(smpSecurityReqCallbackList, [&](HCISMPSecurityReqCallback &cb) { - cb.invoke(conn->getAddress(), conn->getAddressType(), conn->getHandle(), smpPDU); - }); - } else { - WARN_PRINT("HCIHandler-IO RECV Drop (ACL.SMP): Not tracked conn_handle %s: %s", - jau::uint16HexString(conn_handle), conn->toString().c_str()); - } + const uint16_t conn_handle = l2cap.handle; + HCIConnectionRef conn = findTrackerConnection(conn_handle); + + if( nullptr != conn ) { + COND_PRINT(env.DEBUG_EVENT, "HCIHandler-IO RECV (ACL.SMP) %s for %s", + smpPDU->toString().c_str(), conn->toString().c_str()); + jau::for_each_cow(hciSMPMsgCallbackList, [&](HCISMPMsgCallback &cb) { + cb.invoke(conn->getAddress(), conn->getAddressType(), conn->getHandle(), smpPDU); + }); } else { - COND_PRINT(env.DEBUG_EVENT, "HCIHandler-IO RECV Drop (ACL.SMP) %s", smpPDU->toString().c_str()); + WARN_PRINT("HCIHandler-IO RECV Drop (ACL.SMP): Not tracked conn_handle %s: %s", + jau::uint16HexString(conn_handle), conn->toString().c_str()); } } else { COND_PRINT(env.DEBUG_EVENT, "HCIHandler-IO RECV Drop (ACL.L2CAP): %s", l2cap.toString().c_str()); @@ -1257,22 +1251,22 @@ void HCIHandler::clearAllCallbacks() noexcept { for(size_t i=0; i<mgmtEventCallbackLists.size(); i++) { mgmtEventCallbackLists[i].clear(); } - smpSecurityReqCallbackList.clear(); + hciSMPMsgCallbackList.clear(); } /** - * SMPSecurityReqCallback handling + * SMPMsgCallback handling */ -static HCISMPSecurityReqCallbackList::equal_comparator _changedHCISMPSecurityReqCallbackEqComp = - [](const HCISMPSecurityReqCallback& a, const HCISMPSecurityReqCallback& b) -> bool { return a == b; }; +static HCISMPMsgCallbackList::equal_comparator _changedHCISMPMsgCallbackEqComp = + [](const HCISMPMsgCallback& a, const HCISMPMsgCallback& b) -> bool { return a == b; }; -void HCIHandler::addSMPSecurityReqCallback(const HCISMPSecurityReqCallback & l) { - smpSecurityReqCallbackList.push_back(l); +void HCIHandler::addSMPMsgCallback(const HCISMPMsgCallback & l) { + hciSMPMsgCallbackList.push_back(l); } -int HCIHandler::removeSMPSecurityReqCallback(const HCISMPSecurityReqCallback & l) { - return smpSecurityReqCallbackList.erase_matching(l, true /* all_matching */, _changedHCISMPSecurityReqCallbackEqComp); +int HCIHandler::removeSMPMsgCallback(const HCISMPMsgCallback & l) { + return hciSMPMsgCallbackList.erase_matching(l, true /* all_matching */, _changedHCISMPMsgCallbackEqComp); } |