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 <neighbor/name_db.h>
21 
22 #include <utility>
23 #include <vector>
24 
25 #include "hci/address_with_type.h"
26 #include "hci/hci_packets.h"
27 #include "neighbor/name_db.h"
28 #include "security/channel/security_manager_channel.h"
29 #include "security/pairing/oob_data.h"
30 #include "security/record/security_record.h"
31 #include "security/ui.h"
32 
33 namespace bluetooth {
34 namespace security {
35 namespace pairing {
36 
37 /**
38  * Base structure for handling pairing events.
39  *
40  * <p>Extend this class in order to implement a new style of pairing.
41  */
42 class PairingHandler : public UICallbacks {
43  public:
PairingHandler(channel::SecurityManagerChannel * security_manager_channel,std::shared_ptr<record::SecurityRecord> record,neighbor::NameDbModule * name_db_module)44   PairingHandler(
45       channel::SecurityManagerChannel* security_manager_channel,
46       std::shared_ptr<record::SecurityRecord> record,
47       neighbor::NameDbModule* name_db_module)
48       : security_manager_channel_(security_manager_channel),
49         record_(std::move(record)),
50         name_db_module_(name_db_module) {}
51   ~PairingHandler() = default;
52 
53   // Classic
54   virtual void Initiate(
55       bool locally_initiated,
56       hci::IoCapability io_capability,
57       hci::AuthenticationRequirements auth_requirements,
58       OobData local_p192_oob_data,
59       OobData local_p256_oob_data) = 0;
60   virtual void Cancel() = 0;
61   virtual void OnReceive(hci::ChangeConnectionLinkKeyCompleteView packet) = 0;
62   virtual void OnReceive(hci::CentralLinkKeyCompleteView packet) = 0;
63   virtual void OnReceive(hci::PinCodeRequestView packet) = 0;
64   virtual void OnReceive(hci::LinkKeyRequestView packet) = 0;
65   virtual void OnReceive(hci::LinkKeyNotificationView packet) = 0;
66   virtual void OnReceive(hci::IoCapabilityRequestView packet) = 0;
67   virtual void OnReceive(hci::IoCapabilityResponseView packet) = 0;
68   virtual void OnReceive(hci::SimplePairingCompleteView packet) = 0;
69   virtual void OnReceive(hci::ReturnLinkKeysView packet) = 0;
70   virtual void OnReceive(hci::EncryptionChangeView packet) = 0;
71   virtual void OnReceive(hci::EncryptionKeyRefreshCompleteView packet) = 0;
72   virtual void OnReceive(hci::RemoteOobDataRequestView packet) = 0;
73   virtual void OnReceive(hci::UserPasskeyNotificationView packet) = 0;
74   virtual void OnReceive(hci::KeypressNotificationView packet) = 0;
75   virtual void OnReceive(hci::UserConfirmationRequestView packet) = 0;
76   virtual void OnReceive(hci::UserPasskeyRequestView packet) = 0;
77 
78   virtual void OnPairingPromptAccepted(const bluetooth::hci::AddressWithType& address, bool confirmed) = 0;
79   virtual void OnConfirmYesNo(const bluetooth::hci::AddressWithType& address, bool confirmed) = 0;
80   virtual void OnPasskeyEntry(const bluetooth::hci::AddressWithType& address, uint32_t passkey) = 0;
81   virtual void OnPinEntry(const bluetooth::hci::AddressWithType& address, std::vector<uint8_t> pin) override = 0;
82 
83  protected:
GetRecord()84   std::shared_ptr<record::SecurityRecord> GetRecord() {
85     return record_;
86   }
GetChannel()87   channel::SecurityManagerChannel* GetChannel() {
88     return security_manager_channel_;
89   }
GetNameDbModule()90   neighbor::NameDbModule* GetNameDbModule() {
91     return name_db_module_;
92   }
93 
94  private:
95   channel::SecurityManagerChannel* security_manager_channel_ __attribute__((unused));
96   std::shared_ptr<record::SecurityRecord> record_ __attribute__((unused));
97   neighbor::NameDbModule* name_db_module_;
98 };
99 
100 }  // namespace pairing
101 }  // namespace security
102 }  // namespace bluetooth
103