1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <memory>
20 
21 #include "hci/address.h"
22 #include "hci/hci_packets.h"
23 
24 namespace bluetooth {
25 namespace l2cap {
26 namespace classic {
27 
28 /**
29  * This is a proxy for Security Module to unregister itself, or to initiate link connection.
30  */
31 class SecurityInterface {
32  public:
33   virtual ~SecurityInterface() = default;
34 
35   /**
36    * Page a remote device for ACL connection, when Security Module needs it for pairing. When the remote device is
37    * connected, Security Module will receive a callback through LinkSecurityInterfaceListener.
38    */
39   virtual void InitiateConnectionForSecurity(hci::Address remote) = 0;
40 
41   /**
42    * Unregister the security interface and the LinkSecurityInterfaceListener.
43    */
44   virtual void Unregister() = 0;
45 };
46 
47 /**
48  * This is a proxy for Security Module to access some link function. This object is passed to Security Module when a
49  * link is established.
50  */
51 class LinkSecurityInterface {
52  public:
53   virtual ~LinkSecurityInterface() = default;
54 
55   virtual hci::Address GetRemoteAddress() = 0;
56 
57   /**
58    * Hold the ACL link connection. Don't disconnect the link until Release() is called.
59    */
60   virtual void Hold() = 0;
61 
62   /**
63    * Release the ACL link connection. This doesn't guarantee link disconnection, if other L2cap services are using the
64    * link.
65    */
66   virtual void Release() = 0;
67 
68   /**
69    * Force the ACL link to disconnect.
70    */
71   virtual void Disconnect() = 0;
72 
73   /**
74    * Initiate pairing to HCI layer.
75    */
76   virtual void EnsureAuthenticated() = 0;
77 
78   /**
79    * Start encryption on an authenticated link (not necessarily MITM link key).
80    */
81   virtual void EnsureEncrypted() = 0;
82 
83   virtual uint16_t GetAclHandle() = 0;
84 
GetRole()85   virtual hci::Role GetRole() {
86     return hci::Role::CENTRAL;
87   }
88 };
89 
90 class LinkSecurityInterfaceListener {
91  public:
92   virtual ~LinkSecurityInterfaceListener() = default;
93 
94   /**
95    * Each time when an ACL link is connected, security manager receives this callback to use LinkSecurityInterface
96    * functions.
97    */
OnLinkConnected(std::unique_ptr<LinkSecurityInterface>)98   virtual void OnLinkConnected(std::unique_ptr<LinkSecurityInterface>) {}
99 
100   /**
101    * When an ACL link is disconnected, security manager receives this callback. The corresponding LinkSecurityInterface
102    * is invalidated then.
103    * @param remote
104    */
OnLinkDisconnected(hci::Address remote)105   virtual void OnLinkDisconnected(hci::Address remote) {}
106 
107   /**
108    * Invoked when AuthenticationComplete event is received for a given link
109    */
OnAuthenticationComplete(hci::ErrorCode hci_status,hci::Address remote)110   virtual void OnAuthenticationComplete(hci::ErrorCode hci_status, hci::Address remote) {}
111 
112   /**
113    * Invoked when EncryptionChange event is received for a given link
114    * @param encrypted
115    */
OnEncryptionChange(hci::Address remote,bool encrypted)116   virtual void OnEncryptionChange(hci::Address remote, bool encrypted) {}
117 };
118 
119 }  // namespace classic
120 }  // namespace l2cap
121 }  // namespace bluetooth
122