• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "le_conn_params"
18 
19 #include "common/le_conn_params.h"
20 
21 #include <bluetooth/log.h>
22 
23 #include <cstdint>
24 
25 #include "os/system_properties.h"
26 #include "stack/include/btm_ble_api_types.h"
27 
28 using namespace bluetooth;
29 
30 const std::string LeConnectionParameters::kPropertyAggressiveConnThreshold =
31         "bluetooth.core.le.aggressive_connection_threshold";
32 const std::string LeConnectionParameters::kPropertyMinConnIntervalAggressive =
33         "bluetooth.core.le.min_connection_interval_aggressive";
34 const std::string LeConnectionParameters::kPropertyMaxConnIntervalAggressive =
35         "bluetooth.core.le.max_connection_interval_aggressive";
36 const std::string LeConnectionParameters::kPropertyMinConnIntervalRelaxed =
37         "bluetooth.core.le.min_connection_interval_relaxed";
38 const std::string LeConnectionParameters::kPropertyMaxConnIntervalRelaxed =
39         "bluetooth.core.le.max_connection_interval_relaxed";
40 
41 bool LeConnectionParameters::initialized = false;
42 uint32_t LeConnectionParameters::aggressive_conn_threshold = kAggressiveConnThreshold;
43 uint32_t LeConnectionParameters::min_conn_interval_aggressive = kMinConnIntervalAggressive;
44 uint32_t LeConnectionParameters::max_conn_interval_aggressive = kMaxConnIntervalAggressive;
45 uint32_t LeConnectionParameters::min_conn_interval_relaxed = kMinConnIntervalRelaxed;
46 uint32_t LeConnectionParameters::max_conn_interval_relaxed = kMaxConnIntervalRelaxed;
47 
InitConnParamsWithSystemProperties()48 void LeConnectionParameters::InitConnParamsWithSystemProperties() {
49   aggressive_conn_threshold =
50           os::GetSystemPropertyUint32(kPropertyAggressiveConnThreshold, kAggressiveConnThreshold);
51   min_conn_interval_aggressive = os::GetSystemPropertyUint32(kPropertyMinConnIntervalAggressive,
52                                                              kMinConnIntervalAggressive);
53   max_conn_interval_aggressive = os::GetSystemPropertyUint32(kPropertyMaxConnIntervalAggressive,
54                                                              kMaxConnIntervalAggressive);
55   min_conn_interval_relaxed =
56           os::GetSystemPropertyUint32(kPropertyMinConnIntervalRelaxed, kMinConnIntervalRelaxed);
57   max_conn_interval_relaxed =
58           os::GetSystemPropertyUint32(kPropertyMaxConnIntervalRelaxed, kMaxConnIntervalRelaxed);
59 
60   log::debug("Before checking validity: threshold={}, aggressive={}/{}, relaxed={}/{}",
61              aggressive_conn_threshold, min_conn_interval_aggressive, max_conn_interval_aggressive,
62              min_conn_interval_relaxed, max_conn_interval_relaxed);
63 
64   // Check validity of each values
65   if (aggressive_conn_threshold < 0) {
66     log::warn("Invalid aggressive connection threshold. Using default value.",
67               aggressive_conn_threshold);
68     aggressive_conn_threshold = kAggressiveConnThreshold;
69   }
70 
71   if (min_conn_interval_aggressive < BTM_BLE_CONN_INT_MIN ||
72       min_conn_interval_aggressive > BTM_BLE_CONN_INT_MAX ||
73       max_conn_interval_aggressive < BTM_BLE_CONN_INT_MIN ||
74       max_conn_interval_aggressive > BTM_BLE_CONN_INT_MAX ||
75       max_conn_interval_aggressive < min_conn_interval_aggressive) {
76     log::warn("Invalid aggressive connection intervals. Using default values.");
77     min_conn_interval_aggressive = kMinConnIntervalAggressive;
78     max_conn_interval_aggressive = kMaxConnIntervalAggressive;
79   }
80 
81   if (min_conn_interval_relaxed < BTM_BLE_CONN_INT_MIN ||
82       min_conn_interval_relaxed > BTM_BLE_CONN_INT_MAX ||
83       max_conn_interval_relaxed < BTM_BLE_CONN_INT_MIN ||
84       max_conn_interval_relaxed > BTM_BLE_CONN_INT_MAX ||
85       max_conn_interval_relaxed < min_conn_interval_relaxed) {
86     log::warn("Invalid relaxed connection intervals. Using default values.");
87     min_conn_interval_relaxed = kMinConnIntervalRelaxed;
88     max_conn_interval_relaxed = kMaxConnIntervalRelaxed;
89   }
90 
91   if ((min_conn_interval_aggressive > min_conn_interval_relaxed) &&
92       (max_conn_interval_aggressive > max_conn_interval_relaxed)) {
93     log::warn(
94             "Relaxed connection intervals are more aggressive than aggressive ones."
95             " Setting all intervals to default values.");
96     min_conn_interval_aggressive = kMinConnIntervalAggressive;
97     max_conn_interval_aggressive = kMaxConnIntervalAggressive;
98     min_conn_interval_relaxed = kMinConnIntervalRelaxed;
99     max_conn_interval_relaxed = kMaxConnIntervalRelaxed;
100   }
101 
102   log::debug("After checking validity: threshold={}, aggressive={}/{}, relaxed={}/{}",
103              aggressive_conn_threshold, min_conn_interval_aggressive, max_conn_interval_aggressive,
104              min_conn_interval_relaxed, max_conn_interval_relaxed);
105 
106   initialized = true;
107 }
108 
GetAggressiveConnThreshold()109 uint32_t LeConnectionParameters::GetAggressiveConnThreshold() {
110   if (!initialized) {
111     InitConnParamsWithSystemProperties();
112   }
113   return aggressive_conn_threshold;
114 }
115 
GetMinConnIntervalAggressive()116 uint32_t LeConnectionParameters::GetMinConnIntervalAggressive() {
117   if (!initialized) {
118     InitConnParamsWithSystemProperties();
119   }
120   return min_conn_interval_aggressive;
121 }
122 
GetMaxConnIntervalAggressive()123 uint32_t LeConnectionParameters::GetMaxConnIntervalAggressive() {
124   if (!initialized) {
125     InitConnParamsWithSystemProperties();
126   }
127   return max_conn_interval_aggressive;
128 }
129 
GetMinConnIntervalRelaxed()130 uint32_t LeConnectionParameters::GetMinConnIntervalRelaxed() {
131   if (!initialized) {
132     InitConnParamsWithSystemProperties();
133   }
134   return min_conn_interval_relaxed;
135 }
136 
GetMaxConnIntervalRelaxed()137 uint32_t LeConnectionParameters::GetMaxConnIntervalRelaxed() {
138   if (!initialized) {
139     InitConnParamsWithSystemProperties();
140   }
141   return max_conn_interval_relaxed;
142 }
143