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 "pw_bluetooth_sapphire/fuchsia/host/fidl/server_base.h"
20 #include "pw_bluetooth_sapphire/internal/host/common/macros.h"
21 #include "pw_bluetooth_sapphire/internal/host/common/weak_self.h"
22 #include "pw_bluetooth_sapphire/internal/host/gatt/local_service_manager.h"
23 #include "pw_bluetooth_sapphire/internal/host/gatt/types.h"
24 
25 namespace bthost {
26 
27 // Implements the gatt::Server FIDL interface.
28 class GattServerServer
29     : public GattServerBase<fuchsia::bluetooth::gatt::Server> {
30  public:
31   // |adapter_manager| is used to lazily request a handle to the corresponding
32   // adapter. It MUST out-live this GattServerServer instance.
33   GattServerServer(
34       bt::gatt::GATT::WeakPtr gatt,
35       fidl::InterfaceRequest<fuchsia::bluetooth::gatt::Server> request);
36 
37   ~GattServerServer() override;
38 
39   // Removes the service with the given |id| if it is known.
40   // This can be called as a result of FIDL connection errors (such as handle
41   // closure) or as a result of gatt.Service.RemoveService().
42   void RemoveService(uint64_t id);
43 
44  private:
45   class LocalServiceImpl;
46 
47   // ::fuchsia::bluetooth::gatt::Server overrides:
48   void PublishService(
49       fuchsia::bluetooth::gatt::ServiceInfo service_info,
50       fidl::InterfaceHandle<fuchsia::bluetooth::gatt::LocalServiceDelegate>
51           delegate,
52       fidl::InterfaceRequest<fuchsia::bluetooth::gatt::LocalService>
53           service_iface,
54       PublishServiceCallback callback) override;
55 
56   // Called when a remote device issues a read request to one of our services.
57   void OnReadRequest(bt::gatt::IdType service_id,
58                      bt::gatt::IdType id,
59                      uint16_t offset,
60                      bt::gatt::ReadResponder responder);
61 
62   // Called when a remote device issues a write request to one of our services.
63   void OnWriteRequest(bt::gatt::IdType service_id,
64                       bt::gatt::IdType id,
65                       uint16_t offset,
66                       const bt::ByteBuffer& value,
67                       bt::gatt::WriteResponder responder);
68 
69   // Called when a remote device has configured notifications or indications on
70   // a local characteristic.
71   void OnCharacteristicConfig(bt::gatt::IdType service_id,
72                               bt::gatt::IdType chrc_id,
73                               bt::gatt::PeerId peer_id,
74                               bool notify,
75                               bool indicate);
76 
77   // The mapping between service identifiers and FIDL Service implementations.
78   std::unordered_map<uint64_t, std::unique_ptr<LocalServiceImpl>> services_;
79 
80   // Keep this as the last member to make sure that all weak pointers are
81   // invalidated before other members get destroyed.
82   WeakSelf<GattServerServer> weak_self_;
83 
84   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(GattServerServer);
85 };
86 
87 }  // namespace bthost
88