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 "pw_bluetooth_sapphire/internal/host/hci-spec/link_key.h"
17 #include "pw_bluetooth_sapphire/internal/host/hci/acl_connection.h"
18 
19 namespace bt::hci {
20 
21 // BrEdrConnection represents a BR/EDR logical link connection to a peer. In
22 // addition to general link lifetime and encryption procedures provided by
23 // AclConnection, BrEdrConnection manages BR/EDR-specific encryption procedures.
24 class BrEdrConnection : public AclConnection, public WeakSelf<BrEdrConnection> {
25  public:
26   BrEdrConnection(hci_spec::ConnectionHandle handle,
27                   const DeviceAddress& local_address,
28                   const DeviceAddress& peer_address,
29                   pw::bluetooth::emboss::ConnectionRole role,
30                   const Transport::WeakPtr& hci);
31 
32   bool StartEncryption() override;
33 
34   // Assigns a link key with its corresponding HCI type to this BR/EDR
35   // connection. This will be used for bonding procedures and determines the
36   // resulting security properties of the link.
set_link_key(const hci_spec::LinkKey & link_key,hci_spec::LinkKeyType type)37   void set_link_key(const hci_spec::LinkKey& link_key,
38                     hci_spec::LinkKeyType type) {
39     set_ltk(link_key);
40     ltk_type_ = type;
41   }
42 
ltk_type()43   const std::optional<hci_spec::LinkKeyType>& ltk_type() { return ltk_type_; }
44 
45  private:
46   void HandleEncryptionStatus(Result<bool /*enabled*/> result,
47                               bool key_refreshed) override;
48 
49   void HandleEncryptionStatusValidated(Result<bool> result);
50 
51   void ValidateEncryptionKeySize(hci::ResultFunction<> key_size_validity_cb);
52 
53   // BR/EDR-specific type of the assigned link key.
54   std::optional<hci_spec::LinkKeyType> ltk_type_;
55 };
56 
57 }  // namespace bt::hci
58