xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/gatt/connection.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 #include "pw_bluetooth_sapphire/internal/host/gatt/connection.h"
16 
17 #include <numeric>
18 
19 #include "pw_bluetooth_sapphire/internal/host/common/assert.h"
20 #include "pw_bluetooth_sapphire/internal/host/common/log.h"
21 #include "pw_bluetooth_sapphire/internal/host/gatt/client.h"
22 #include "pw_bluetooth_sapphire/internal/host/gatt/local_service_manager.h"
23 #include "pw_bluetooth_sapphire/internal/host/gatt/server.h"
24 
25 namespace bt::gatt::internal {
26 
Connection(std::unique_ptr<Client> client,std::unique_ptr<Server> server,RemoteServiceWatcher svc_watcher)27 Connection::Connection(std::unique_ptr<Client> client,
28                        std::unique_ptr<Server> server,
29                        RemoteServiceWatcher svc_watcher)
30     : server_(std::move(server)), weak_self_(this) {
31   PW_CHECK(svc_watcher);
32 
33   remote_service_manager_ =
34       std::make_unique<RemoteServiceManager>(std::move(client));
35   remote_service_manager_->set_service_watcher(std::move(svc_watcher));
36 }
37 
Initialize(std::vector<UUID> service_uuids,fit::callback<void (uint16_t)> mtu_cb)38 void Connection::Initialize(std::vector<UUID> service_uuids,
39                             fit::callback<void(uint16_t)> mtu_cb) {
40   PW_CHECK(remote_service_manager_);
41 
42   auto uuids_count = service_uuids.size();
43   // status_cb must not capture att_ in order to prevent reference cycle.
44   auto status_cb = [self = weak_self_.GetWeakPtr(),
45                     uuids_count](att::Result<> status) {
46     if (!self.is_alive()) {
47       return;
48     }
49 
50     if (bt_is_error(status, ERROR, "gatt", "client setup failed")) {
51       // Signal a link error.
52       self->ShutDown();
53     } else if (uuids_count > 0) {
54       bt_log(DEBUG,
55              "gatt",
56              "primary service discovery complete for (%zu) service uuids",
57              uuids_count);
58     } else {
59       bt_log(DEBUG, "gatt", "primary service discovery complete");
60     }
61   };
62 
63   remote_service_manager_->Initialize(
64       std::move(status_cb), std::move(mtu_cb), std::move(service_uuids));
65 }
66 
ShutDown()67 void Connection::ShutDown() {
68   // We shut down the connection from the server not for any technical reason,
69   // but just because it was simpler to expose the att::Bearer's ShutDown
70   // behavior from the server.
71   server_->ShutDown();
72 }
73 }  // namespace bt::gatt::internal
74