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 <unistd.h>
20 
21 #include <cerrno>
22 #include <mutex>
23 #include <string>
24 
25 #include "os/log.h"
26 
27 namespace bluetooth {
28 namespace os {
29 
30 namespace {
31 std::mutex parameter_mutex;
32 std::string config_file_path;
33 std::string snoop_log_file_path;
34 std::string snooz_log_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 \"%s\", returning default", strerror(errno));
48     return "bt_config.conf";
49   }
50   return std::string(cwd) + "/bt_config.conf";
51 }
52 
OverrideConfigFilePath(const std::string & path)53 void ParameterProvider::OverrideConfigFilePath(const std::string& path) {
54   std::lock_guard<std::mutex> lock(parameter_mutex);
55   config_file_path = path;
56 }
57 
SnoopLogFilePath()58 std::string ParameterProvider::SnoopLogFilePath() {
59   {
60     std::lock_guard<std::mutex> lock(parameter_mutex);
61     if (!snoop_log_file_path.empty()) {
62       return snoop_log_file_path;
63     }
64   }
65   char cwd[PATH_MAX] = {};
66   if (getcwd(cwd, sizeof(cwd)) == nullptr) {
67     LOG_ERROR("Failed to get current working directory due to \"%s\", returning default", strerror(errno));
68     return "btsnoop_hci.log";
69   }
70   return std::string(cwd) + "/btsnoop_hci.log";
71 }
72 
OverrideSnoopLogFilePath(const std::string & path)73 void ParameterProvider::OverrideSnoopLogFilePath(const std::string& path) {
74   std::lock_guard<std::mutex> lock(parameter_mutex);
75   snoop_log_file_path = path;
76 }
77 
78 // Return the path to the default snooz log file location
SnoozLogFilePath()79 std::string ParameterProvider::SnoozLogFilePath() {
80   {
81     std::lock_guard<std::mutex> lock(parameter_mutex);
82     if (!snooz_log_file_path.empty()) {
83       return snooz_log_file_path;
84     }
85   }
86   char cwd[PATH_MAX] = {};
87   if (getcwd(cwd, sizeof(cwd)) == nullptr) {
88     LOG_ERROR("Failed to get current working directory due to \"%s\", returning default", strerror(errno));
89     return "bt_config.conf";
90   }
91   return std::string(cwd) + "/btsnooz_hci.log";
92 }
93 
OverrideSnoozLogFilePath(const std::string & path)94 void ParameterProvider::OverrideSnoozLogFilePath(const std::string& path) {
95   std::lock_guard<std::mutex> lock(parameter_mutex);
96   snooz_log_file_path = path;
97 }
98 
99 }  // namespace os
100 }  // namespace bluetooth