xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/gap/low_energy_connection_request.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/gap/low_energy_connection_request.h"
16 
17 namespace bt::gap::internal {
18 
LowEnergyConnectionRequest(PeerId peer_id,ConnectionResultCallback first_callback,LowEnergyConnectionOptions connection_options,Peer::InitializingConnectionToken peer_conn_token)19 LowEnergyConnectionRequest::LowEnergyConnectionRequest(
20     PeerId peer_id,
21     ConnectionResultCallback first_callback,
22     LowEnergyConnectionOptions connection_options,
23     Peer::InitializingConnectionToken peer_conn_token)
24     : peer_id_(peer_id, MakeToStringInspectConvertFunction()),
25       callbacks_(/*convert=*/[](const auto& cbs) { return cbs.size(); }),
26       connection_options_(connection_options),
27       peer_conn_token_(std::move(peer_conn_token)) {
28   callbacks_.Mutable()->push_back(std::move(first_callback));
29 }
30 
NotifyCallbacks(fit::result<HostError,RefFunc> result)31 void LowEnergyConnectionRequest::NotifyCallbacks(
32     fit::result<HostError, RefFunc> result) {
33   peer_conn_token_.reset();
34 
35   for (const auto& callback : *callbacks_) {
36     if (result.is_error()) {
37       callback(fit::error(result.error_value()));
38       continue;
39     }
40     auto conn_ref = result.value()();
41     callback(fit::ok(std::move(conn_ref)));
42   }
43 }
44 
AttachInspect(inspect::Node & parent,std::string name)45 void LowEnergyConnectionRequest::AttachInspect(inspect::Node& parent,
46                                                std::string name) {
47   inspect_node_ = parent.CreateChild(name);
48   peer_id_.AttachInspect(inspect_node_, "peer_id");
49   callbacks_.AttachInspect(inspect_node_, "callbacks");
50 }
51 
52 }  // namespace bt::gap::internal
53