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 <map>
17 
18 #include "fake_l2cap.h"
19 #include "pw_bluetooth_sapphire/internal/host/att/att.h"
20 #include "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h"
21 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
22 
23 namespace bt::testing {
24 
25 class FakePeer;
26 
27 // Emulates a GATT server.
28 class FakeGattServer final {
29  public:
30   explicit FakeGattServer(FakePeer* dev);
31 
32   // Handle the ATT |pdu| received over link with handle |conn|.
33   void HandlePdu(hci_spec::ConnectionHandle conn, const ByteBuffer& pdu);
34 
35   // Register with FakleL2cap |l2cap_| associated with the device that owns
36   // the server such that this FakeGattServer instance receives all packets
37   // sent on kATTChannelId.
38   void RegisterWithL2cap(FakeL2cap* l2cap_);
39 
40  private:
41   struct Service {
42     att::Handle start_handle;
43     att::Handle end_handle;
44     UUID type;
45   };
46 
47   void HandleReadByGrpType(hci_spec::ConnectionHandle conn,
48                            const ByteBuffer& bytes);
49   void HandleFindByTypeValue(hci_spec::ConnectionHandle conn,
50                              const ByteBuffer& bytes);
51 
52   void Send(hci_spec::ConnectionHandle conn, const ByteBuffer& pdu);
53   void SendErrorRsp(hci_spec::ConnectionHandle conn,
54                     att::OpCode opcode,
55                     att::Handle handle,
56                     att::ErrorCode ecode);
57 
58   // Map of service start handles to services.
59   std::map<att::Handle, Service> services_;
60 
61   // The fake device that owns this server. Must outlive this instance.
62   FakePeer* dev_;
63 
64   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(FakeGattServer);
65 };
66 
67 }  // namespace bt::testing
68