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 <cpp-string/string_printf.h>
17 
18 #include <cstdint>
19 #include <string>
20 
21 namespace bt::hci_spec {
22 
23 // All LE connection parameters in this file are given in terms of a
24 // "multiplier" that to controller uses to calculate actual duration values.
25 // The exact value calculations and ranges depend on the context in which these
26 // values are used (e.g. see LECreateConnectionCommand).
27 //
28 // See the specification section referenced above each LEConnectionParameters
29 // getter below. See hci_constants.h for min/max allowed ranges for each
30 // parameter.
31 
32 // Connection parameters for a LE connection. Used to represent parameters in
33 // active use by the link layer for a particular logical link.
34 class LEConnectionParameters final {
35  public:
36   LEConnectionParameters(uint16_t interval,
37                          uint16_t latency,
38                          uint16_t supervision_timeout);
39 
40   // Default constructor initializes all values to 0. This is intended for cases
41   // that require default initialization, e.g. when this structure is used
42   // inside a container that default initializes its contents.
43   LEConnectionParameters();
44 
45   // The connection interval indicates the frequency of link layer connection
46   // events over which data channel PDUs can be transmitted. See Core Spec v5.0,
47   // Vol 6, Part B, Section 4.5.1 for more information on the link layer
48   // connection events.
interval()49   uint16_t interval() const { return interval_; }
50 
51   // The maximum allowed connection latency. See Core Spec v5.0, Vol 6, Part
52   // B, Section 4.5.2.
latency()53   uint16_t latency() const { return latency_; }
54 
55   // This defines the maximum time between two received data packet PDUs
56   // before the connection is considered lost. See Core Spec v5.0, Vol 6, Part
57   // B, Section 4.5.2. This value is in centiseconds and must be within
58   // the range 100 ms - 32 s (10 cs - 3200 cs).
supervision_timeout()59   uint16_t supervision_timeout() const { return supervision_timeout_; }
60 
61   // Equality operator
62   bool operator==(const LEConnectionParameters& other) const;
63 
64   std::string ToString() const;
65 
66  private:
67   uint16_t interval_;
68   uint16_t latency_;
69   uint16_t supervision_timeout_;
70 };
71 
72 // Preferred connection parameters for LE connection. Used to represent
73 // parameters that act as a hint to the controller. This is used for:
74 //
75 //   * initiating a new connection as a central
76 //   * to represent a peripheral's preferred connection parameters
77 class LEPreferredConnectionParameters final {
78  public:
79   LEPreferredConnectionParameters(uint16_t min_interval,
80                                   uint16_t max_interval,
81                                   uint16_t max_latency,
82                                   uint16_t supervision_timeout);
83 
84   // Default constructor initializes all values to 0. This is intended for cases
85   // that require default initialization, e.g. when this structure is used
86   // inside a container that default initializes its contents.
87   LEPreferredConnectionParameters();
88 
89   // See hci_spec::LEConnectionParameters for a description of these fields. See
90   // hci_constants.h for the allowed ranges for these values.
min_interval()91   uint16_t min_interval() const { return min_interval_; }
max_interval()92   uint16_t max_interval() const { return max_interval_; }
max_latency()93   uint16_t max_latency() const { return max_latency_; }
supervision_timeout()94   uint16_t supervision_timeout() const { return supervision_timeout_; }
95 
96   // Equality operator
97   bool operator==(const LEPreferredConnectionParameters& other) const;
98 
99  private:
100   uint16_t min_interval_;
101   uint16_t max_interval_;
102   uint16_t max_latency_;
103   uint16_t supervision_timeout_;
104 };
105 
106 }  // namespace bt::hci_spec
107