1 /*
2 * Copyright 2017 The Android Open Source Project
3 * Copyright 2018-2019 NXP
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 #include "uwb_config.h"
18
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <android-base/parseint.h>
22 #include <android-base/strings.h>
23 #include <config.h>
24
25 #include "UwbAdaptation.h"
26
27 using namespace ::std;
28 using namespace ::android::base;
29
30 namespace {
31
findConfigPath()32 std::string findConfigPath() {
33 const vector<string> search_path = {"/odm/etc/", "/vendor/etc/",
34 "/product/etc/", "/etc/"};
35 const string file_name = "libuwb-uci.conf";
36
37 for (string path : search_path) {
38 path.append(file_name);
39 struct stat file_stat;
40 if (stat(path.c_str(), &file_stat) != 0) continue;
41 if (S_ISREG(file_stat.st_mode)) return path;
42 }
43 return "";
44 }
45
46 } // namespace
47
loadConfig()48 void UwbConfig::loadConfig() {
49 string config_path = findConfigPath();
50 CHECK(config_path != "");
51 config_.parseFromFile(config_path);
52 }
53
UwbConfig()54 UwbConfig::UwbConfig() { loadConfig(); }
55
getInstance()56 UwbConfig& UwbConfig::getInstance() {
57 static UwbConfig theInstance;
58 if (theInstance.config_.isEmpty()) {
59 theInstance.loadConfig();
60 }
61 return theInstance;
62 }
63
hasKey(const std::string & key)64 bool UwbConfig::hasKey(const std::string& key) {
65 return getInstance().config_.hasKey(key);
66 }
67
getString(const std::string & key)68 std::string UwbConfig::getString(const std::string& key) {
69 return getInstance().config_.getString(key);
70 }
71
getString(const std::string & key,std::string default_value)72 std::string UwbConfig::getString(const std::string& key,
73 std::string default_value) {
74 if (hasKey(key)) return getString(key);
75 return default_value;
76 }
77
getUnsigned(const std::string & key)78 unsigned UwbConfig::getUnsigned(const std::string& key) {
79 return getInstance().config_.getUnsigned(key);
80 }
81
getUnsigned(const std::string & key,unsigned default_value)82 unsigned UwbConfig::getUnsigned(const std::string& key,
83 unsigned default_value) {
84 if (hasKey(key)) return getUnsigned(key);
85 return default_value;
86 }
87
getBytes(const std::string & key)88 std::vector<uint8_t> UwbConfig::getBytes(const std::string& key) {
89 return getInstance().config_.getBytes(key);
90 }
91
clear()92 void UwbConfig::clear() { getInstance().config_.clear(); }
93