1 /*
2  * Copyright 2019 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 "os/parameter_provider.h"
18 
19 #include <bluetooth/log.h>
20 #include <unistd.h>
21 
22 #include <cerrno>
23 #include <mutex>
24 #include <string>
25 
26 namespace bluetooth {
27 namespace os {
28 
29 namespace {
30 std::mutex parameter_mutex;
31 std::string config_file_path;
32 std::string snoop_log_file_path;
33 std::string snooz_log_file_path;
34 std::string sysprops_file_path;
35 }  // namespace
36 
37 // Write to $PWD/bt_stack.conf if $PWD can be found, otherwise, write to $HOME/bt_stack.conf
ConfigFilePath()38 std::string ParameterProvider::ConfigFilePath() {
39   {
40     std::lock_guard<std::mutex> lock(parameter_mutex);
41     if (!config_file_path.empty()) {
42       return config_file_path;
43     }
44   }
45   char cwd[PATH_MAX] = {};
46   if (getcwd(cwd, sizeof(cwd)) == nullptr) {
47     log::error("Failed to get current working directory due to \"{}\", returning default",
48                strerror(errno));
49     return "bt_config.conf";
50   }
51   return std::string(cwd) + "/bt_config.conf";
52 }
53 
OverrideConfigFilePath(const std::string & path)54 void ParameterProvider::OverrideConfigFilePath(const std::string& path) {
55   std::lock_guard<std::mutex> lock(parameter_mutex);
56   config_file_path = path;
57 }
58 
SnoopLogFilePath()59 std::string ParameterProvider::SnoopLogFilePath() {
60   {
61     std::lock_guard<std::mutex> lock(parameter_mutex);
62     if (!snoop_log_file_path.empty()) {
63       return snoop_log_file_path;
64     }
65   }
66   char cwd[PATH_MAX] = {};
67   if (getcwd(cwd, sizeof(cwd)) == nullptr) {
68     log::error("Failed to get current working directory due to \"{}\", returning default",
69                strerror(errno));
70     return "btsnoop_hci.log";
71   }
72   return std::string(cwd) + "/btsnoop_hci.log";
73 }
74 
OverrideSnoopLogFilePath(const std::string & path)75 void ParameterProvider::OverrideSnoopLogFilePath(const std::string& path) {
76   std::lock_guard<std::mutex> lock(parameter_mutex);
77   snoop_log_file_path = path;
78 }
79 
80 // Return the path to the default snooz log file location
SnoozLogFilePath()81 std::string ParameterProvider::SnoozLogFilePath() {
82   {
83     std::lock_guard<std::mutex> lock(parameter_mutex);
84     if (!snooz_log_file_path.empty()) {
85       return snooz_log_file_path;
86     }
87   }
88   char cwd[PATH_MAX] = {};
89   if (getcwd(cwd, sizeof(cwd)) == nullptr) {
90     log::error("Failed to get current working directory due to \"{}\", returning default",
91                strerror(errno));
92     return "bt_config.conf";
93   }
94   return std::string(cwd) + "/btsnooz_hci.log";
95 }
96 
OverrideSnoozLogFilePath(const std::string & path)97 void ParameterProvider::OverrideSnoozLogFilePath(const std::string& path) {
98   std::lock_guard<std::mutex> lock(parameter_mutex);
99   snooz_log_file_path = path;
100 }
101 
SyspropsFilePath()102 std::string ParameterProvider::SyspropsFilePath() {
103   std::lock_guard<std::mutex> lock(parameter_mutex);
104   return sysprops_file_path;
105 }
106 
OverrideSyspropsFilePath(const std::string & path)107 void ParameterProvider::OverrideSyspropsFilePath(const std::string& path) {
108   std::lock_guard<std::mutex> lock(parameter_mutex);
109   sysprops_file_path = path;
110 }
111 
GetBtKeystoreInterface()112 bluetooth_keystore::BluetoothKeystoreInterface* ParameterProvider::GetBtKeystoreInterface() {
113   return nullptr;
114 }
115 
SetBtKeystoreInterface(bluetooth_keystore::BluetoothKeystoreInterface *)116 void ParameterProvider::SetBtKeystoreInterface(
117         bluetooth_keystore::BluetoothKeystoreInterface* /* bt_keystore */) {}
118 
IsCommonCriteriaMode()119 bool ParameterProvider::IsCommonCriteriaMode() { return false; }
120 
SetCommonCriteriaMode(bool)121 void ParameterProvider::SetCommonCriteriaMode(bool /* enable */) {}
122 
GetCommonCriteriaConfigCompareResult()123 int ParameterProvider::GetCommonCriteriaConfigCompareResult() { return 0b11; }
124 
SetCommonCriteriaConfigCompareResult(int)125 void ParameterProvider::SetCommonCriteriaConfigCompareResult(int /* result */) {}
126 
127 }  // namespace os
128 }  // namespace bluetooth
129