1 // Copyright 2024 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 
17 #include <fuchsia/bluetooth/gatt/cpp/fidl.h>
18 
19 #include <unordered_map>
20 
21 #include "lib/fidl/cpp/binding.h"
22 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/server_base.h"
23 #include "pw_bluetooth_sapphire/internal/host/common/macros.h"
24 #include "pw_bluetooth_sapphire/internal/host/common/weak_self.h"
25 #include "pw_bluetooth_sapphire/internal/host/gatt/gatt.h"
26 
27 namespace bthost {
28 
29 // Implements the gatt::RemoteService FIDL interface.
30 class GattRemoteServiceServer
31     : public GattServerBase<fuchsia::bluetooth::gatt::RemoteService> {
32  public:
33   GattRemoteServiceServer(
34       bt::gatt::RemoteService::WeakPtr service,
35       bt::gatt::GATT::WeakPtr gatt,
36       bt::PeerId peer_id,
37       fidl::InterfaceRequest<fuchsia::bluetooth::gatt::RemoteService> request);
38   ~GattRemoteServiceServer() override;
39 
40  private:
41   // fuchsia::bluetooth::gatt::RemoteService overrides:
42   void DiscoverCharacteristics(
43       DiscoverCharacteristicsCallback callback) override;
44   void ReadCharacteristic(uint64_t id,
45                           ReadCharacteristicCallback callback) override;
46   void ReadLongCharacteristic(uint64_t id,
47                               uint16_t offset,
48                               uint16_t max_bytes,
49                               ReadLongCharacteristicCallback callback) override;
50   void WriteCharacteristic(uint64_t id,
51                            ::std::vector<uint8_t> value,
52                            WriteCharacteristicCallback callback) override;
53   void WriteLongCharacteristic(
54       uint64_t id,
55       uint16_t offset,
56       ::std::vector<uint8_t> value,
57       fuchsia::bluetooth::gatt::WriteOptions write_options,
58       WriteCharacteristicCallback callback) override;
59   void WriteCharacteristicWithoutResponse(
60       uint64_t id, ::std::vector<uint8_t> value) override;
61   void ReadDescriptor(uint64_t id, ReadDescriptorCallback callback) override;
62   void ReadLongDescriptor(uint64_t id,
63                           uint16_t offset,
64                           uint16_t max_bytes,
65                           ReadLongDescriptorCallback callback) override;
66   void WriteDescriptor(uint64_t _id,
67                        ::std::vector<uint8_t> value,
68                        WriteDescriptorCallback callback) override;
69   void WriteLongDescriptor(uint64_t _id,
70                            uint16_t offset,
71                            ::std::vector<uint8_t> value,
72                            WriteDescriptorCallback callback) override;
73   void ReadByType(fuchsia::bluetooth::Uuid uuid,
74                   ReadByTypeCallback callback) override;
75   void NotifyCharacteristic(uint64_t id,
76                             bool enable,
77                             NotifyCharacteristicCallback callback) override;
78 
79   // The remote GATT service that backs this service.
80   bt::gatt::RemoteService::WeakPtr service_;
81 
82   const bt::PeerId peer_id_;
83 
84   using HandlerId = bt::gatt::IdType;
85   std::unordered_map<bt::gatt::CharacteristicHandle, HandlerId>
86       notify_handlers_;
87 
88   WeakSelf<GattRemoteServiceServer> weak_self_;
89   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(GattRemoteServiceServer);
90 };
91 
92 }  // namespace bthost
93