1 /*
2 * Copyright 2020 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 #include "sysprops/sysprops_module.h"
18
19 #include <filesystem>
20
21 #include "os/handler.h"
22 #include "os/log.h"
23 #include "os/parameter_provider.h"
24 #include "os/system_properties.h"
25 #include "storage/legacy_config_file.h"
26
27 namespace bluetooth {
28 namespace sysprops {
29
30 static const size_t kDefaultCapacity = 10000;
31
__anon55dacedb0102() 32 const ModuleFactory SyspropsModule::Factory = ModuleFactory([]() { return new SyspropsModule(); });
33
34 struct SyspropsModule::impl {
implbluetooth::sysprops::SyspropsModule::impl35 impl(os::Handler* sysprops_handler) : sysprops_handler_(sysprops_handler) {}
36
37 os::Handler* sysprops_handler_;
38 };
39
ListDependencies(ModuleList * list) const40 void SyspropsModule::ListDependencies(ModuleList* list) const {}
41
Start()42 void SyspropsModule::Start() {
43 std::string file_path = os::ParameterProvider::SyspropsFilePath();
44 if (!file_path.empty()) {
45 parse_config(file_path);
46 // Merge config fragments
47 std::string override_dir = file_path + ".d";
48 if (std::filesystem::exists(override_dir)) {
49 for (const auto& entry : std::filesystem::directory_iterator(override_dir)) {
50 parse_config(entry.path());
51 }
52 }
53 }
54
55 pimpl_ = std::make_unique<impl>(GetHandler());
56 }
57
Stop()58 void SyspropsModule::Stop() {
59 pimpl_.reset();
60 }
61
ToString() const62 std::string SyspropsModule::ToString() const {
63 return "Sysprops Module";
64 }
65
parse_config(std::string file_path)66 void SyspropsModule::parse_config(std::string file_path) {
67 const std::list<std::string> supported_sysprops = {
68 // General
69 "bluetooth.device.default_name",
70 "bluetooth.core.gap.le.privacy.enabled",
71 "bluetooth.core.gap.le.conn.only_init_1m_phy.enabled",
72 "bluetooth.device.class_of_device",
73 "bluetooth.device_id.product_id",
74 "bluetooth.device_id.product_version",
75 "bluetooth.device_id.vendor_id",
76 "bluetooth.device_id.vendor_id_source",
77 // BR/EDR
78 "bluetooth.core.classic.page_scan_type",
79 "bluetooth.core.classic.page_scan_interval",
80 "bluetooth.core.classic.page_scan_window",
81 "bluetooth.core.classic.inq_scan_type",
82 "bluetooth.core.classic.inq_scan_interval",
83 "bluetooth.core.classic.inq_scan_window",
84 "bluetooth.core.acl.link_supervision_timeout",
85 "bluetooth.core.classic.page_timeout",
86 "bluetooth.core.classic.sniff_max_intervals",
87 "bluetooth.core.classic.sniff_min_intervals",
88 "bluetooth.core.classic.sniff_attempts",
89 "bluetooth.core.classic.sniff_timeouts",
90 // LE
91 "bluetooth.core.le.min_connection_interval",
92 "bluetooth.core.le.max_connection_interval",
93 "bluetooth.core.le.connection_latency",
94 "bluetooth.core.le.connection_supervision_timeout",
95 "bluetooth.core.le.direct_connection_timeout",
96 "bluetooth.core.le.connection_scan_interval_fast",
97 "bluetooth.core.le.connection_scan_window_fast",
98 "bluetooth.core.le.connection_scan_window_2m_fast",
99 "bluetooth.core.le.connection_scan_window_coded_fast",
100 "bluetooth.core.le.connection_scan_interval_slow",
101 "bluetooth.core.le.connection_scan_window_slow",
102 "bluetooth.core.le.inquiry_scan_interval",
103 "bluetooth.core.le.inquiry_scan_window",
104 "bluetooth.core.le.vendor_capabilities.enabled",
105 // SCO
106 "bluetooth.sco.disable_enhanced_connection",
107 // Profile
108 "persist.bluetooth.avrcpcontrolversion",
109 };
110
111 auto config = storage::LegacyConfigFile::FromPath(file_path).Read(kDefaultCapacity);
112 if (!config) {
113 return;
114 }
115
116 for (auto s = supported_sysprops.begin(); s != supported_sysprops.end(); s++) {
117 auto str = config->GetProperty("Sysprops", *s);
118 if (str) {
119 bluetooth::os::SetSystemProperty(*s, *str);
120 }
121 }
122 }
123
124 } // namespace sysprops
125 } // namespace bluetooth
126