Loading system/gd/Android.bp +8 −0 Original line number Diff line number Diff line Loading @@ -332,6 +332,14 @@ cc_benchmark { ], } filegroup { name: "BluetoothHciClassSources", srcs: [ "hci/address.cc", "hci/class_of_device.cc", ], } genrule { name: "BluetoothGeneratedPackets_h", tools: [ Loading system/gd/security/internal/security_manager_impl.cc +21 −31 Original line number Diff line number Diff line Loading @@ -22,10 +22,12 @@ #include "os/log.h" #include "security/pairing/classic_pairing_handler.h" #include "security/security_manager.h" using namespace bluetooth::security::internal; using bluetooth::hci::Device; using bluetooth::hci::DeviceType; using bluetooth::security::ISecurityManagerListener; using bluetooth::security::pairing::PairingHandler; namespace { Loading Loading @@ -98,55 +100,43 @@ void SecurityManagerImpl::RemoveBond(std::shared_ptr<hci::ClassicDevice> device) // Signal Remove from database } void SecurityManagerImpl::RegisterCallbackListener(ISecurityManagerListener* listener) { if (listeners_.size() < 1) { listeners_.push_back(listener); } else { bool found = false; void SecurityManagerImpl::RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler) { for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { found = *it == listener; if (found) break; } if (found) { LOG_ERROR("Listener has already been registered!"); } else { listeners_.push_back(listener); if (it->first == listener) { LOG_ALWAYS_FATAL("Listener has already been registered!"); } } listeners_.push_back({listener, handler}); } void SecurityManagerImpl::UnregisterCallbackListener(ISecurityManagerListener* listener) { if (listeners_.size() < 1) { LOG_ERROR("Listener has not been registered!"); } else { bool found = false; auto it = listeners_.begin(); while (it != listeners_.end()) { found = *it == listener; if (found) break; ++it; } if (found) { for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { if (it->first == listener) { listeners_.erase(it); return; } } LOG_ALWAYS_FATAL("Listener has not been registered!"); } void SecurityManagerImpl::FireDeviceBondedCallbacks(std::shared_ptr<Device> device) { void SecurityManagerImpl::NotifyDeviceBonded(std::shared_ptr<Device> device) { for (auto& iter : listeners_) { iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceBonded, common::Unretained(iter), device)); iter.second->Post(common::Bind(&ISecurityManagerListener::OnDeviceBonded, common::Unretained(iter.first), device)); } } void SecurityManagerImpl::FireBondFailedCallbacks(std::shared_ptr<Device> device) { void SecurityManagerImpl::NotifyDeviceBondFailed(std::shared_ptr<Device> device) { for (auto& iter : listeners_) { iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceBondFailed, common::Unretained(iter), device)); iter.second->Post( common::Bind(&ISecurityManagerListener::OnDeviceBondFailed, common::Unretained(iter.first), device)); } } void SecurityManagerImpl::FireUnbondCallbacks(std::shared_ptr<Device> device) { void SecurityManagerImpl::NotifyDeviceUnbonded(std::shared_ptr<Device> device) { for (auto& iter : listeners_) { iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceUnbonded, common::Unretained(iter), device)); iter.second->Post( common::Bind(&ISecurityManagerListener::OnDeviceUnbonded, common::Unretained(iter.first), device)); } } system/gd/security/internal/security_manager_impl.h +9 −40 Original line number Diff line number Diff line Loading @@ -22,45 +22,14 @@ #include "os/handler.h" #include "security/channel/security_manager_channel.h" #include <utility> namespace bluetooth { namespace security { namespace internal { /** * Interface for listening to the channel for SMP commands. */ class ISecurityManagerListener { public: ISecurityManagerListener(os::Handler* handler) : handler_(handler) {} virtual ~ISecurityManagerListener() = default; /** * Called when a device is successfully bonded. * * @param device pointer to the bonded device */ virtual void OnDeviceBonded(std::shared_ptr<bluetooth::hci::Device> device); /** * Called when a device is successfully un-bonded. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceUnbonded(std::shared_ptr<bluetooth::hci::Device> device); /** * Called as a result of a failure during the bonding process. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceBondFailed(std::shared_ptr<bluetooth::hci::Device> device); bool operator==(const ISecurityManagerListener& rhs) const { return &*this == &rhs; } class ISecurityManagerListener; os::Handler* handler_ = nullptr; }; namespace internal { class SecurityManagerImpl /*: public channel::ISecurityManagerChannelListener*/ { public: Loading Loading @@ -113,7 +82,7 @@ class SecurityManagerImpl /*: public channel::ISecurityManagerChannelListener*/ * * @param listener ISecurityManagerListener instance to handle callbacks */ void RegisterCallbackListener(ISecurityManagerListener* listener); void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler); /** * Unregister listener for callback events from SecurityManager Loading @@ -123,10 +92,10 @@ class SecurityManagerImpl /*: public channel::ISecurityManagerChannelListener*/ void UnregisterCallbackListener(ISecurityManagerListener* listener); protected: std::vector<ISecurityManagerListener*> listeners_; void FireDeviceBondedCallbacks(std::shared_ptr<bluetooth::hci::Device> device); void FireBondFailedCallbacks(std::shared_ptr<bluetooth::hci::Device> device); void FireUnbondCallbacks(std::shared_ptr<bluetooth::hci::Device> device); std::vector<std::pair<ISecurityManagerListener*, os::Handler*>> listeners_; void NotifyDeviceBonded(std::shared_ptr<bluetooth::hci::Device> device); void NotifyDeviceBondFailed(std::shared_ptr<bluetooth::hci::Device> device); void NotifyDeviceUnbonded(std::shared_ptr<bluetooth::hci::Device> device); // ISecurityManagerChannel void OnChangeConnectionLinkKeyComplete(std::shared_ptr<hci::Device> device, Loading system/gd/security/security_manager.cc +7 −6 Original line number Diff line number Diff line Loading @@ -21,6 +21,9 @@ using namespace bluetooth::security; // Definition of Pure Virtual Destructor ISecurityManagerListener::~ISecurityManagerListener() {} void SecurityManager::Init() { security_handler_->Post( common::BindOnce(&internal::SecurityManagerImpl::Init, common::Unretained(security_manager_impl_))); Loading @@ -44,14 +47,12 @@ void SecurityManager::RemoveBond(std::shared_ptr<hci::ClassicDevice> device) { std::forward<std::shared_ptr<hci::ClassicDevice>>(device))); } void SecurityManager::RegisterCallbackListener(internal::ISecurityManagerListener* listener) { void SecurityManager::RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler) { security_handler_->Post(common::BindOnce(&internal::SecurityManagerImpl::RegisterCallbackListener, common::Unretained(security_manager_impl_), std::forward<internal::ISecurityManagerListener*>(listener))); common::Unretained(security_manager_impl_), listener, handler)); } void SecurityManager::UnregisterCallbackListener(internal::ISecurityManagerListener* listener) { void SecurityManager::UnregisterCallbackListener(ISecurityManagerListener* listener) { security_handler_->Post(common::BindOnce(&internal::SecurityManagerImpl::UnregisterCallbackListener, common::Unretained(security_manager_impl_), std::forward<internal::ISecurityManagerListener*>(listener))); common::Unretained(security_manager_impl_), listener)); } system/gd/security/security_manager.h +31 −2 Original line number Diff line number Diff line Loading @@ -28,6 +28,35 @@ namespace bluetooth { namespace security { /** * Callback interface from SecurityManager. */ class ISecurityManagerListener { public: virtual ~ISecurityManagerListener() = 0; /** * Called when a device is successfully bonded. * * @param device pointer to the bonded device */ virtual void OnDeviceBonded(std::shared_ptr<bluetooth::hci::Device> device) = 0; /** * Called when a device is successfully un-bonded. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceUnbonded(std::shared_ptr<bluetooth::hci::Device> device) = 0; /** * Called as a result of a failure during the bonding process. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceBondFailed(std::shared_ptr<bluetooth::hci::Device> device) = 0; }; /** * Manages the security attributes, pairing, bonding of devices, and the * encryption/decryption of communications. Loading Loading @@ -67,14 +96,14 @@ class SecurityManager { * * @param listener ISecurityManagerListener instance to handle callbacks */ void RegisterCallbackListener(internal::ISecurityManagerListener* listener); void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler); /** * Unregister listener for callback events from SecurityManager * * @param listener ISecurityManagerListener instance to unregister */ void UnregisterCallbackListener(internal::ISecurityManagerListener* listener); void UnregisterCallbackListener(ISecurityManagerListener* listener); protected: SecurityManager(os::Handler* security_handler, internal::SecurityManagerImpl* security_manager_impl) Loading Loading
system/gd/Android.bp +8 −0 Original line number Diff line number Diff line Loading @@ -332,6 +332,14 @@ cc_benchmark { ], } filegroup { name: "BluetoothHciClassSources", srcs: [ "hci/address.cc", "hci/class_of_device.cc", ], } genrule { name: "BluetoothGeneratedPackets_h", tools: [ Loading
system/gd/security/internal/security_manager_impl.cc +21 −31 Original line number Diff line number Diff line Loading @@ -22,10 +22,12 @@ #include "os/log.h" #include "security/pairing/classic_pairing_handler.h" #include "security/security_manager.h" using namespace bluetooth::security::internal; using bluetooth::hci::Device; using bluetooth::hci::DeviceType; using bluetooth::security::ISecurityManagerListener; using bluetooth::security::pairing::PairingHandler; namespace { Loading Loading @@ -98,55 +100,43 @@ void SecurityManagerImpl::RemoveBond(std::shared_ptr<hci::ClassicDevice> device) // Signal Remove from database } void SecurityManagerImpl::RegisterCallbackListener(ISecurityManagerListener* listener) { if (listeners_.size() < 1) { listeners_.push_back(listener); } else { bool found = false; void SecurityManagerImpl::RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler) { for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { found = *it == listener; if (found) break; } if (found) { LOG_ERROR("Listener has already been registered!"); } else { listeners_.push_back(listener); if (it->first == listener) { LOG_ALWAYS_FATAL("Listener has already been registered!"); } } listeners_.push_back({listener, handler}); } void SecurityManagerImpl::UnregisterCallbackListener(ISecurityManagerListener* listener) { if (listeners_.size() < 1) { LOG_ERROR("Listener has not been registered!"); } else { bool found = false; auto it = listeners_.begin(); while (it != listeners_.end()) { found = *it == listener; if (found) break; ++it; } if (found) { for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { if (it->first == listener) { listeners_.erase(it); return; } } LOG_ALWAYS_FATAL("Listener has not been registered!"); } void SecurityManagerImpl::FireDeviceBondedCallbacks(std::shared_ptr<Device> device) { void SecurityManagerImpl::NotifyDeviceBonded(std::shared_ptr<Device> device) { for (auto& iter : listeners_) { iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceBonded, common::Unretained(iter), device)); iter.second->Post(common::Bind(&ISecurityManagerListener::OnDeviceBonded, common::Unretained(iter.first), device)); } } void SecurityManagerImpl::FireBondFailedCallbacks(std::shared_ptr<Device> device) { void SecurityManagerImpl::NotifyDeviceBondFailed(std::shared_ptr<Device> device) { for (auto& iter : listeners_) { iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceBondFailed, common::Unretained(iter), device)); iter.second->Post( common::Bind(&ISecurityManagerListener::OnDeviceBondFailed, common::Unretained(iter.first), device)); } } void SecurityManagerImpl::FireUnbondCallbacks(std::shared_ptr<Device> device) { void SecurityManagerImpl::NotifyDeviceUnbonded(std::shared_ptr<Device> device) { for (auto& iter : listeners_) { iter->handler_->Post(common::Bind(&ISecurityManagerListener::OnDeviceUnbonded, common::Unretained(iter), device)); iter.second->Post( common::Bind(&ISecurityManagerListener::OnDeviceUnbonded, common::Unretained(iter.first), device)); } }
system/gd/security/internal/security_manager_impl.h +9 −40 Original line number Diff line number Diff line Loading @@ -22,45 +22,14 @@ #include "os/handler.h" #include "security/channel/security_manager_channel.h" #include <utility> namespace bluetooth { namespace security { namespace internal { /** * Interface for listening to the channel for SMP commands. */ class ISecurityManagerListener { public: ISecurityManagerListener(os::Handler* handler) : handler_(handler) {} virtual ~ISecurityManagerListener() = default; /** * Called when a device is successfully bonded. * * @param device pointer to the bonded device */ virtual void OnDeviceBonded(std::shared_ptr<bluetooth::hci::Device> device); /** * Called when a device is successfully un-bonded. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceUnbonded(std::shared_ptr<bluetooth::hci::Device> device); /** * Called as a result of a failure during the bonding process. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceBondFailed(std::shared_ptr<bluetooth::hci::Device> device); bool operator==(const ISecurityManagerListener& rhs) const { return &*this == &rhs; } class ISecurityManagerListener; os::Handler* handler_ = nullptr; }; namespace internal { class SecurityManagerImpl /*: public channel::ISecurityManagerChannelListener*/ { public: Loading Loading @@ -113,7 +82,7 @@ class SecurityManagerImpl /*: public channel::ISecurityManagerChannelListener*/ * * @param listener ISecurityManagerListener instance to handle callbacks */ void RegisterCallbackListener(ISecurityManagerListener* listener); void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler); /** * Unregister listener for callback events from SecurityManager Loading @@ -123,10 +92,10 @@ class SecurityManagerImpl /*: public channel::ISecurityManagerChannelListener*/ void UnregisterCallbackListener(ISecurityManagerListener* listener); protected: std::vector<ISecurityManagerListener*> listeners_; void FireDeviceBondedCallbacks(std::shared_ptr<bluetooth::hci::Device> device); void FireBondFailedCallbacks(std::shared_ptr<bluetooth::hci::Device> device); void FireUnbondCallbacks(std::shared_ptr<bluetooth::hci::Device> device); std::vector<std::pair<ISecurityManagerListener*, os::Handler*>> listeners_; void NotifyDeviceBonded(std::shared_ptr<bluetooth::hci::Device> device); void NotifyDeviceBondFailed(std::shared_ptr<bluetooth::hci::Device> device); void NotifyDeviceUnbonded(std::shared_ptr<bluetooth::hci::Device> device); // ISecurityManagerChannel void OnChangeConnectionLinkKeyComplete(std::shared_ptr<hci::Device> device, Loading
system/gd/security/security_manager.cc +7 −6 Original line number Diff line number Diff line Loading @@ -21,6 +21,9 @@ using namespace bluetooth::security; // Definition of Pure Virtual Destructor ISecurityManagerListener::~ISecurityManagerListener() {} void SecurityManager::Init() { security_handler_->Post( common::BindOnce(&internal::SecurityManagerImpl::Init, common::Unretained(security_manager_impl_))); Loading @@ -44,14 +47,12 @@ void SecurityManager::RemoveBond(std::shared_ptr<hci::ClassicDevice> device) { std::forward<std::shared_ptr<hci::ClassicDevice>>(device))); } void SecurityManager::RegisterCallbackListener(internal::ISecurityManagerListener* listener) { void SecurityManager::RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler) { security_handler_->Post(common::BindOnce(&internal::SecurityManagerImpl::RegisterCallbackListener, common::Unretained(security_manager_impl_), std::forward<internal::ISecurityManagerListener*>(listener))); common::Unretained(security_manager_impl_), listener, handler)); } void SecurityManager::UnregisterCallbackListener(internal::ISecurityManagerListener* listener) { void SecurityManager::UnregisterCallbackListener(ISecurityManagerListener* listener) { security_handler_->Post(common::BindOnce(&internal::SecurityManagerImpl::UnregisterCallbackListener, common::Unretained(security_manager_impl_), std::forward<internal::ISecurityManagerListener*>(listener))); common::Unretained(security_manager_impl_), listener)); }
system/gd/security/security_manager.h +31 −2 Original line number Diff line number Diff line Loading @@ -28,6 +28,35 @@ namespace bluetooth { namespace security { /** * Callback interface from SecurityManager. */ class ISecurityManagerListener { public: virtual ~ISecurityManagerListener() = 0; /** * Called when a device is successfully bonded. * * @param device pointer to the bonded device */ virtual void OnDeviceBonded(std::shared_ptr<bluetooth::hci::Device> device) = 0; /** * Called when a device is successfully un-bonded. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceUnbonded(std::shared_ptr<bluetooth::hci::Device> device) = 0; /** * Called as a result of a failure during the bonding process. * * @param device pointer to the device that is no longer bonded */ virtual void OnDeviceBondFailed(std::shared_ptr<bluetooth::hci::Device> device) = 0; }; /** * Manages the security attributes, pairing, bonding of devices, and the * encryption/decryption of communications. Loading Loading @@ -67,14 +96,14 @@ class SecurityManager { * * @param listener ISecurityManagerListener instance to handle callbacks */ void RegisterCallbackListener(internal::ISecurityManagerListener* listener); void RegisterCallbackListener(ISecurityManagerListener* listener, os::Handler* handler); /** * Unregister listener for callback events from SecurityManager * * @param listener ISecurityManagerListener instance to unregister */ void UnregisterCallbackListener(internal::ISecurityManagerListener* listener); void UnregisterCallbackListener(ISecurityManagerListener* listener); protected: SecurityManager(os::Handler* security_handler, internal::SecurityManagerImpl* security_manager_impl) Loading