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