• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common/contextual_callback.h"
25 #include "hci/address_with_type.h"
26 #include "hci/hci_layer.h"
27 #include "hci/hci_packets.h"
28 #include "hci/security_interface.h"
29 #include "l2cap/classic/l2cap_classic_module.h"
30 #include "l2cap/classic/link_security_interface.h"
31 
32 namespace bluetooth {
33 namespace security {
34 namespace channel {
35 
36 using SecurityCommandStatusCallback = common::ContextualOnceCallback<void(hci::CommandCompleteView)>;
37 
38 /**
39  * Interface for listening to the channel for SMP commands.
40  */
41 class ISecurityManagerChannelListener {
42  public:
43   virtual ~ISecurityManagerChannelListener() = default;
44   virtual void OnHciEventReceived(hci::EventView packet) = 0;
45   virtual void OnConnectionClosed(hci::Address) = 0;
46 };
47 
48 /**
49  * Channel for consolidating traffic and making the transport agnostic.
50  */
51 class SecurityManagerChannel : public l2cap::classic::LinkSecurityInterfaceListener {
52  public:
53   SecurityManagerChannel(os::Handler* handler, hci::HciLayer* hci_layer);
54 
55   virtual ~SecurityManagerChannel();
56 
57   /**
58    * Creates a connection to the device which triggers pairing
59    *
60    * @param address remote address of device to pair with
61    */
62   void Connect(hci::Address address);
63 
64   /**
65    * Releases link hold so it can disconnect as normally
66    *
67    * i.e. signals we no longer need this if acl manager wants to clean it up
68    *
69    * @param address remote address to disconnect
70    */
71   void Release(hci::Address address);
72 
73   /**
74    * Immediately disconnects currently connected channel
75    *
76    * i.e. force disconnect
77    *
78    * @param address remote address to disconnect
79    */
80   void Disconnect(hci::Address address);
81 
82   /**
83    * Send a given SMP command over the SecurityManagerChannel
84    *
85    * @param command smp command to send
86    */
87   void SendCommand(std::unique_ptr<hci::SecurityCommandBuilder> command);
88 
89   /**
90    * Send a given SMP command over the SecurityManagerChannel
91    *
92    * @param command smp command to send
93    * @param callback listener to call when command status complete
94    */
95   void SendCommand(std::unique_ptr<hci::SecurityCommandBuilder> command, SecurityCommandStatusCallback callback);
96 
97   /**
98    * Sets the listener to listen for channel events
99    *
100    * @param listener the caller interested in events
101    */
SetChannelListener(ISecurityManagerChannelListener * listener)102   void SetChannelListener(ISecurityManagerChannelListener* listener) {
103     listener_ = listener;
104   }
105 
SetSecurityInterface(l2cap::classic::SecurityInterface * security_interface)106   void SetSecurityInterface(l2cap::classic::SecurityInterface* security_interface) {
107     l2cap_security_interface_ = security_interface;
108   }
109 
110   /**
111    * Called when an incoming HCI event happens
112    *
113    * @param event_packet
114    */
115   void OnHciEventReceived(hci::EventView packet);
116 
117   /**
118    * Called when an HCI command is completed
119    *
120    * @param on_complete
121    */
122   void OnCommandComplete(hci::CommandCompleteView packet);
123 
124   // Interface overrides
125   void OnLinkConnected(std::unique_ptr<l2cap::classic::LinkSecurityInterface> link) override;
126   void OnLinkDisconnected(hci::Address address) override;
127   void OnAuthenticationComplete(hci::ErrorCode hci_status, hci::Address remote) override;
128   void OnEncryptionChange(hci::Address, bool encrypted) override;
129 
130  private:
131   ISecurityManagerChannelListener* listener_{nullptr};
132   hci::SecurityInterface* hci_security_interface_{nullptr};
133   os::Handler* handler_{nullptr};
134   l2cap::classic::SecurityInterface* l2cap_security_interface_{nullptr};
135   std::unordered_map<hci::Address, std::unique_ptr<l2cap::classic::LinkSecurityInterface>> link_map_;
136   std::set<hci::Address> outgoing_pairing_remote_devices_;
137 };
138 
139 }  // namespace channel
140 }  // namespace security
141 }  // namespace bluetooth
142