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 <memory>
17 
18 #include "pw_bluetooth_sapphire/internal/host/common/macros.h"
19 #include "pw_bluetooth_sapphire/internal/host/gatt/gatt_defs.h"
20 #include "pw_bluetooth_sapphire/internal/host/gatt/remote_service_manager.h"
21 
22 namespace bt {
23 
24 namespace l2cap {
25 class Channel;
26 }  // namespace l2cap
27 
28 namespace att {
29 class Bearer;
30 }  // namespace att
31 
32 namespace gatt {
33 
34 class Server;
35 
36 namespace internal {
37 
38 // Represents the GATT data channel between the local adapter and a single
39 // remote peer. A Connection supports simultaneous GATT client and server
40 // functionality. An instance of Connection should exist on each ACL logical
41 // link.
42 class Connection final {
43  public:
44   // |client| is the GATT client for this connection, which uses |att_bearer| in
45   // production. |server| is the GATT server for this connection, which uses
46   // |att_bearer| in production. |svc_watcher| communicates updates about the
47   // peer's GATT services to the Connection's owner.
48   Connection(std::unique_ptr<Client> client,
49              std::unique_ptr<Server> server,
50              RemoteServiceWatcher svc_watcher);
51   ~Connection() = default;
52 
server()53   Server* server() const { return server_.get(); }
remote_service_manager()54   RemoteServiceManager* remote_service_manager() const {
55     return remote_service_manager_.get();
56   }
57 
58   // Performs MTU exchange, then primary service discovery. Shuts down the
59   // connection on failure. If |service_uuids| is non-empty, discovery is only
60   // performed for services with the indicated UUIDs. Returns the agreed-upon
61   // MTU via |mtu_cb|.
62   void Initialize(std::vector<UUID> service_uuids,
63                   fit::callback<void(uint16_t)> mtu_cb);
64 
65   // Closes the ATT bearer on which the connection operates.
66   void ShutDown();
67 
68  private:
69   std::unique_ptr<Server> server_;
70   std::unique_ptr<RemoteServiceManager> remote_service_manager_;
71 
72   WeakSelf<Connection> weak_self_;
73 
74   BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(Connection);
75 };
76 
77 }  // namespace internal
78 }  // namespace gatt
79 }  // namespace bt
80