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 #include <vector> 23 24 #include "hci/address_with_type.h" 25 #include "hci/hci_layer.h" 26 #include "hci/hci_packets.h" 27 #include "hci/security_interface.h" 28 #include "l2cap/classic/l2cap_classic_module.h" 29 #include "l2cap/classic/link_security_interface.h" 30 31 namespace bluetooth { 32 namespace security { 33 namespace channel { 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::EventPacketView 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 * Sets the listener to listen for channel events 88 * 89 * @param listener the caller interested in events 90 */ SetChannelListener(ISecurityManagerChannelListener * listener)91 void SetChannelListener(ISecurityManagerChannelListener* listener) { 92 listener_ = listener; 93 } 94 SetSecurityInterface(l2cap::classic::SecurityInterface * security_interface)95 void SetSecurityInterface(l2cap::classic::SecurityInterface* security_interface) { 96 l2cap_security_interface_ = security_interface; 97 } 98 99 /** 100 * Called when an incoming HCI event happens 101 * 102 * @param event_packet 103 */ 104 void OnHciEventReceived(hci::EventPacketView packet); 105 106 /** 107 * Called when an HCI command is completed 108 * 109 * @param on_complete 110 */ 111 void OnCommandComplete(hci::CommandCompleteView packet); 112 113 // Interface overrides 114 void OnLinkConnected(std::unique_ptr<l2cap::classic::LinkSecurityInterface> link) override; 115 void OnLinkDisconnected(hci::Address address) override; 116 117 private: 118 ISecurityManagerChannelListener* listener_{nullptr}; 119 hci::SecurityInterface* hci_security_interface_{nullptr}; 120 os::Handler* handler_{nullptr}; 121 l2cap::classic::SecurityInterface* l2cap_security_interface_{nullptr}; 122 std::unordered_map<hci::Address, std::unique_ptr<l2cap::classic::LinkSecurityInterface>> link_map_; 123 }; 124 125 } // namespace channel 126 } // namespace security 127 } // namespace bluetooth 128