1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include <lib/fit/function.h>
17 
18 #include "pw_bluetooth_sapphire/internal/host/common/identifier.h"
19 #include "pw_bluetooth_sapphire/internal/host/common/weak_self.h"
20 #include "pw_bluetooth_sapphire/internal/host/sm/error.h"
21 #include "pw_bluetooth_sapphire/internal/host/sm/smp.h"
22 
23 namespace bt::gap {
24 
25 // An object that implements PairingDelegate is responsible for fulfilling user
26 // authentication challenges during pairing.
27 class PairingDelegate : public WeakSelf<PairingDelegate> {
28  public:
29   using ConfirmCallback = fit::callback<void(bool confirm)>;
30 
31   // Selects the intent of the passkey provided to the underlying pairing
32   // provider.
33   enum DisplayMethod {
34     kComparison,  // Both sides display a passkey which the user compares.
35     kPeerEntry,   // User enters the displayed passkey into the peer device.
36   };
37 
38   virtual ~PairingDelegate() = default;
39 
40   // Returns the I/O capability of this delegate.
41   virtual sm::IOCapability io_capability() const = 0;
42 
43   // Terminate any ongoing pairing challenge for the peer device with the given
44   // |identifier|.
45   virtual void CompletePairing(PeerId peer_id, sm::Result<> status) = 0;
46 
47   // Ask the user to confirm the pairing request from the device with the given
48   // |id| and confirm or reject by calling |confirm|.
49   virtual void ConfirmPairing(PeerId peer_id, ConfirmCallback confirm) = 0;
50 
51   // Show the user the 6-digit |passkey| that should be compared to the peer's
52   // passkey or entered into the peer. |confirm| may be called to accept a
53   // comparison or to reject the pairing.
54   virtual void DisplayPasskey(PeerId peer_id,
55                               uint32_t passkey,
56                               DisplayMethod method,
57                               ConfirmCallback confirm) = 0;
58 
59   // Ask the user to enter a 6-digit passkey or reject pairing. Report the
60   // result by invoking |respond|.
61   //
62   // A valid |passkey| must be a non-negative integer. Pass a negative value to
63   // reject pairing.
64   using PasskeyResponseCallback = fit::callback<void(int64_t passkey)>;
65   virtual void RequestPasskey(PeerId peer_id,
66                               PasskeyResponseCallback respond) = 0;
67 
68  protected:
PairingDelegate()69   PairingDelegate() : WeakSelf(this) {}
70 
71  private:
72   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(PairingDelegate);
73 };
74 
75 }  // namespace bt::gap
76