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/le_connection_parameters.h"
17 #include "pw_bluetooth_sapphire/internal/host/hci-spec/link_key.h"
18 #include "pw_bluetooth_sapphire/internal/host/hci/acl_connection.h"
19 
20 namespace bt::hci {
21 
22 class LowEnergyConnection : public AclConnection,
23                             public WeakSelf<LowEnergyConnection> {
24  public:
25   LowEnergyConnection(hci_spec::ConnectionHandle handle,
26                       const DeviceAddress& local_address,
27                       const DeviceAddress& peer_address,
28                       const hci_spec::LEConnectionParameters& params,
29                       pw::bluetooth::emboss::ConnectionRole role,
30                       const Transport::WeakPtr& hci);
31 
32   ~LowEnergyConnection() override;
33 
34   // Authenticate (i.e. encrypt) this connection using its current link key.
35   // Returns false if the procedure cannot be initiated. The result of the
36   // authentication procedure will be reported via the encryption change
37   // callback.  If the link layer procedure fails, the connection will be
38   // disconnected. The encryption change callback will be notified of the
39   // failure.
40   bool StartEncryption() override;
41 
42   // Sets the active LE parameters of this connection.
set_low_energy_parameters(const hci_spec::LEConnectionParameters & params)43   void set_low_energy_parameters(
44       const hci_spec::LEConnectionParameters& params) {
45     parameters_ = params;
46   }
47 
48   // The active LE connection parameters of this connection.
low_energy_parameters()49   const hci_spec::LEConnectionParameters& low_energy_parameters() const {
50     return parameters_;
51   }
52 
53   using AclConnection::set_ltk;
54 
55  private:
56   void HandleEncryptionStatus(Result<bool /*enabled*/> result,
57                               bool key_refreshed) override;
58 
59   // HCI event handlers.
60   CommandChannel::EventCallbackResult OnLELongTermKeyRequestEvent(
61       const EventPacket& event);
62 
63   // IDs for encryption related HCI event handlers.
64   CommandChannel::EventHandlerId le_ltk_request_id_;
65 
66   hci_spec::LEConnectionParameters parameters_;
67 };
68 
69 }  // namespace bt::hci
70