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 "pw_bluetooth_sapphire/internal/host/common/weak_self.h"
17 #include "pw_bluetooth_sapphire/internal/host/gap/pairing_delegate.h"
18 #include "pw_bluetooth_sapphire/internal/host/sm/error.h"
19 #include "pw_bluetooth_sapphire/internal/host/sm/types.h"
20 #include "pw_bluetooth_sapphire/internal/host/transport/error.h"
21 
22 namespace bt::sm {
23 
24 // Delegate interface for pairing and bonding events.
25 class Delegate {
26  public:
27   virtual ~Delegate() = default;
28 
29   using ConfirmCallback = fit::callback<void(bool confirm)>;
30   // Asks higher-level protocols outside bt-host to confirm the pairing request
31   // from the device.
32   virtual void ConfirmPairing(ConfirmCallback confirm) = 0;
33 
34   using DisplayMethod = gap::PairingDelegate::DisplayMethod;
35   // Show the user the 6-digit |passkey| that should be compared to the peer's
36   // passkey or entered into the peer. |confirm| may be called to accept a
37   // comparison or to reject the pairing.
38   virtual void DisplayPasskey(uint32_t passkey,
39                               DisplayMethod method,
40                               ConfirmCallback confirm) = 0;
41 
42   // Ask the user to enter a 6-digit passkey or reject pairing. Reports the
43   // result by invoking |respond| with |passkey| - a negative value of |passkey|
44   // indicates entry failed.
45   using PasskeyResponseCallback = fit::callback<void(int64_t passkey)>;
46   virtual void RequestPasskey(PasskeyResponseCallback respond) = 0;
47 
48   // Called to obtain the local identity information to distribute to the
49   // peer. The delegate should return std::nullopt if there is no identity
50   // information to share. Otherwise, the delegate should return the IRK and
51   // the identity address to distribute.
52   virtual std::optional<IdentityInfo> OnIdentityInformationRequest() = 0;
53 
54   // Called when an ongoing pairing is completed with the given |status|.
55   virtual void OnPairingComplete(Result<> status) = 0;
56 
57   // Called when new pairing data has been obtained for this peer.
58   virtual void OnNewPairingData(const PairingData& data) = 0;
59 
60   // Called when the link layer authentication procedure fails. This likely
61   // indicates that the LTK or STK used to encrypt the connection was rejected
62   // by the peer device.
63   //
64   // The underlying link should disconnect after this callback runs.
65   virtual void OnAuthenticationFailure(hci::Result<> status) = 0;
66 
67   // Called when the security properties of the link change.
68   virtual void OnNewSecurityProperties(const SecurityProperties& sec) = 0;
69 
70   using WeakPtr = WeakSelf<Delegate>::WeakPtr;
71 };
72 
73 }  // namespace bt::sm
74