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 <lib/fit/function.h>
17 
18 #include "pw_bluetooth_sapphire/internal/host/att/bearer.h"
19 #include "pw_bluetooth_sapphire/internal/host/att/database.h"
20 #include "pw_bluetooth_sapphire/internal/host/common/uuid.h"
21 #include "pw_bluetooth_sapphire/internal/host/gatt/gatt_defs.h"
22 #include "pw_bluetooth_sapphire/internal/host/gatt/local_service_manager.h"
23 
24 namespace bt {
25 
26 namespace att {
27 class Attribute;
28 class Database;
29 class PacketReader;
30 }  // namespace att
31 
32 namespace gatt {
33 using IndicationCallback = att::ResultCallback<>;
34 
35 // A GATT Server implements the server-role of the ATT protocol over a single
36 // ATT Bearer. A unique Server instance should exist for each logical link that
37 // supports GATT.
38 //
39 // A Server responds to incoming requests by querying the database that it
40 // is initialized with. Each Server shares an att::Bearer with a Client.
41 class Server {
42  public:
43   // Constructs a new Server bearer.
44   // |peer_id| is the unique system identifier for the peer device.
45   // |local_services| will be used to resolve inbound/outbound transactions.
46   // |bearer| is the ATT data bearer that this Server operates on. It must
47   // outlive this Server.
48   static std::unique_ptr<Server> Create(
49       PeerId peer_id,
50       LocalServiceManager::WeakPtr local_services,
51       att::Bearer::WeakPtr bearer);
52   // Servers can be constructed without production att::Bearers (e.g. for
53   // testing), so the FactoryFunction type reflects that.
54   using FactoryFunction = fit::function<std::unique_ptr<Server>(
55       PeerId, LocalServiceManager::WeakPtr)>;
56 
57   virtual ~Server() = default;
58 
59   // Sends a Handle-Value notification or indication PDU on the given |chrc_id|
60   // within |service_id|. If |indicate_cb| is nullptr, a notification is sent.
61   // Otherwise, an indication is sent, and indicate_cb is called with the result
62   // of the indication. The underlying att::Bearer will disconnect the link if a
63   // confirmation is not received in a timely manner.
64   virtual void SendUpdate(IdType service_id,
65                           IdType chrc_id,
66                           BufferView value,
67                           IndicationCallback indicate_cb) = 0;
68 
69   // Shuts down the transport on which this Server operates, which may also
70   // disconnect any other objects using the same transport, like the
71   // gatt::Client.
72   virtual void ShutDown() = 0;
73 };
74 
75 }  // namespace gatt
76 }  // namespace bt
77