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 
19 namespace bt::gap {
20 
21 // Adapts PairingDelegate to generic callbacks that can perform any desired test
22 // checking. If an PairingDelegate call is made that does not have a
23 // corresponding callback set, a Google Test failure is added. If this object is
24 // destroyed and there are callback-assigned PairingDelegate calls that were not
25 // called, a Google Test failure is added.
26 class FakePairingDelegate final : public PairingDelegate {
27  public:
28   explicit FakePairingDelegate(sm::IOCapability io_capability);
29   ~FakePairingDelegate() override;
30 
set_io_capability(sm::IOCapability io_capability)31   void set_io_capability(sm::IOCapability io_capability) {
32     io_capability_ = io_capability;
33   }
34 
35   // If set, these will receive calls to their respective calls. If not set,
36   // the corresponding PairingDelegate call will result in a test failure.
37   using CompletePairingCallback = fit::function<void(PeerId, sm::Result<>)>;
SetCompletePairingCallback(CompletePairingCallback cb)38   void SetCompletePairingCallback(CompletePairingCallback cb) {
39     complete_pairing_cb_ = std::move(cb);
40   }
41   using ConfirmPairingCallback = fit::function<void(PeerId, ConfirmCallback)>;
SetConfirmPairingCallback(ConfirmPairingCallback cb)42   void SetConfirmPairingCallback(ConfirmPairingCallback cb) {
43     confirm_pairing_cb_ = std::move(cb);
44   }
45   using DisplayPasskeyCallback =
46       fit::function<void(PeerId, uint32_t, DisplayMethod, ConfirmCallback)>;
SetDisplayPasskeyCallback(DisplayPasskeyCallback cb)47   void SetDisplayPasskeyCallback(DisplayPasskeyCallback cb) {
48     display_passkey_cb_ = std::move(cb);
49   }
50   using RequestPasskeyCallback =
51       fit::function<void(PeerId, PasskeyResponseCallback)>;
SetRequestPasskeyCallback(RequestPasskeyCallback cb)52   void SetRequestPasskeyCallback(RequestPasskeyCallback cb) {
53     request_passkey_cb_ = std::move(cb);
54   }
55 
56   // PairingDelegate overrides.
io_capability()57   sm::IOCapability io_capability() const override { return io_capability_; }
58   void CompletePairing(PeerId peer_id, sm::Result<> status) override;
59   void ConfirmPairing(PeerId peer_id, ConfirmCallback confirm) override;
60   void DisplayPasskey(PeerId peer_id,
61                       uint32_t passkey,
62                       DisplayMethod method,
63                       ConfirmCallback confirm) override;
64   void RequestPasskey(PeerId peer_id, PasskeyResponseCallback respond) override;
65 
66  private:
67   sm::IOCapability io_capability_;
68   CompletePairingCallback complete_pairing_cb_;
69   ConfirmPairingCallback confirm_pairing_cb_;
70   DisplayPasskeyCallback display_passkey_cb_;
71   RequestPasskeyCallback request_passkey_cb_;
72   int complete_pairing_count_;
73   int confirm_pairing_count_;
74   int display_passkey_count_;
75   int request_passkey_count_;
76 };
77 
78 }  // namespace bt::gap
79