1 /* 2 * 3 * Copyright 2019 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0; 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 #pragma once 19 20 #include <memory> 21 #include <unordered_map> 22 23 #include "common/contextual_callback.h" 24 #include "hci/hci_layer.h" 25 #include "hci/hci_packets.h" 26 #include "hci/security_interface.h" 27 #include "l2cap/classic/link_security_interface.h" 28 29 namespace bluetooth { 30 namespace security { 31 namespace channel { 32 33 using SecurityCommandStatusCallback = common::ContextualOnceCallback<void(hci::CommandCompleteView)>; 34 35 /** 36 * Interface for listening to the channel for SMP commands. 37 */ 38 class ISecurityManagerChannelListener { 39 public: 40 virtual ~ISecurityManagerChannelListener() = default; 41 virtual void OnHciEventReceived(hci::EventView packet) = 0; 42 virtual void OnConnectionClosed(hci::Address) = 0; 43 }; 44 45 /** 46 * Channel for consolidating traffic and making the transport agnostic. 47 */ 48 class SecurityManagerChannel : public l2cap::classic::LinkSecurityInterfaceListener { 49 public: 50 SecurityManagerChannel(os::Handler* handler, hci::HciLayer* hci_layer); 51 52 virtual ~SecurityManagerChannel(); 53 54 /** 55 * Creates a connection to the device which triggers pairing 56 * 57 * @param address remote address of device to pair with 58 */ 59 void Connect(hci::Address address); 60 61 /** 62 * Releases link hold so it can disconnect as normally 63 * 64 * i.e. signals we no longer need this if acl manager wants to clean it up 65 * 66 * @param address remote address to disconnect 67 */ 68 void Release(hci::Address address); 69 70 /** 71 * Immediately disconnects currently connected channel 72 * 73 * i.e. force disconnect 74 * 75 * @param address remote address to disconnect 76 */ 77 void Disconnect(hci::Address address); 78 79 /** 80 * Send a given SMP command over the SecurityManagerChannel 81 * 82 * @param command smp command to send 83 */ 84 void SendCommand(std::unique_ptr<hci::SecurityCommandBuilder> command); 85 86 /** 87 * Send a given SMP command over the SecurityManagerChannel 88 * 89 * @param command smp command to send 90 * @param callback listener to call when command status complete 91 */ 92 void SendCommand(std::unique_ptr<hci::SecurityCommandBuilder> command, SecurityCommandStatusCallback callback); 93 94 /** 95 * Sets the listener to listen for channel events 96 * 97 * @param listener the caller interested in events 98 */ SetChannelListener(ISecurityManagerChannelListener * listener)99 void SetChannelListener(ISecurityManagerChannelListener* listener) { 100 listener_ = listener; 101 } 102 SetSecurityInterface(l2cap::classic::SecurityInterface * security_interface)103 void SetSecurityInterface(l2cap::classic::SecurityInterface* security_interface) { 104 l2cap_security_interface_ = security_interface; 105 } 106 107 /** 108 * Called when an incoming HCI event happens 109 * 110 * @param event_packet 111 */ 112 void OnHciEventReceived(hci::EventView packet); 113 114 /** 115 * Called when an HCI command is completed 116 * 117 * @param on_complete 118 */ 119 void OnCommandComplete(hci::CommandCompleteView packet); 120 121 // Interface overrides 122 void OnLinkConnected(std::unique_ptr<l2cap::classic::LinkSecurityInterface> link) override; 123 void OnLinkDisconnected(hci::Address address) override; 124 void OnAuthenticationComplete(hci::ErrorCode hci_status, hci::Address remote) override; 125 void OnEncryptionChange(hci::Address, bool encrypted) override; 126 127 private: 128 ISecurityManagerChannelListener* listener_{nullptr}; 129 hci::SecurityInterface* hci_security_interface_{nullptr}; 130 os::Handler* handler_{nullptr}; 131 l2cap::classic::SecurityInterface* l2cap_security_interface_{nullptr}; 132 std::unordered_map<hci::Address, std::unique_ptr<l2cap::classic::LinkSecurityInterface>> link_map_; 133 std::set<hci::Address> outgoing_pairing_remote_devices_; 134 }; 135 136 } // namespace channel 137 } // namespace security 138 } // namespace bluetooth 139