1 /*
2 * Copyright 2017 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 #include "nfc_config.h"
17 #include "NfcAdaptation.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
24 #include <config.h>
25
26 using namespace ::std;
27 using namespace ::android::base;
28
29 namespace {
30
findConfigPath()31 std::string findConfigPath() {
32 const vector<string> search_path = {"/odm/etc/", "/vendor/etc/",
33 "/product/etc/", "/etc/"};
34 const string file_name = "libnfc-nci.conf";
35
36 for (string path : search_path) {
37 path.append(file_name);
38 struct stat file_stat;
39 if (stat(path.c_str(), &file_stat) != 0) continue;
40 if (S_ISREG(file_stat.st_mode)) return path;
41 }
42 return "";
43 }
44
45 } // namespace
46
loadConfig()47 void NfcConfig::loadConfig() {
48 string config_path = findConfigPath();
49 CHECK(config_path != "");
50 config_.parseFromFile(config_path);
51 /* Read vendor specific configs */
52 NfcAdaptation& theInstance = NfcAdaptation::GetInstance();
53 std::map<std::string, ConfigValue> configMap;
54 theInstance.GetVendorConfigs(configMap);
55 for (auto config : configMap) {
56 config_.addConfig(config.first, config.second);
57 }
58 }
59
NfcConfig()60 NfcConfig::NfcConfig() { loadConfig(); }
61
getInstance()62 NfcConfig& NfcConfig::getInstance() {
63 static NfcConfig theInstance;
64 if (theInstance.config_.isEmpty()) {
65 theInstance.loadConfig();
66 }
67 return theInstance;
68 }
69
hasKey(const std::string & key)70 bool NfcConfig::hasKey(const std::string& key) {
71 return getInstance().config_.hasKey(key);
72 }
73
getString(const std::string & key)74 std::string NfcConfig::getString(const std::string& key) {
75 return getInstance().config_.getString(key);
76 }
77
getString(const std::string & key,std::string default_value)78 std::string NfcConfig::getString(const std::string& key,
79 std::string default_value) {
80 if (hasKey(key)) return getString(key);
81 return default_value;
82 }
83
getUnsigned(const std::string & key)84 unsigned NfcConfig::getUnsigned(const std::string& key) {
85 return getInstance().config_.getUnsigned(key);
86 }
87
getUnsigned(const std::string & key,unsigned default_value)88 unsigned NfcConfig::getUnsigned(const std::string& key,
89 unsigned default_value) {
90 if (hasKey(key)) return getUnsigned(key);
91 return default_value;
92 }
93
getBytes(const std::string & key)94 std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) {
95 return getInstance().config_.getBytes(key);
96 }
97
clear()98 void NfcConfig::clear() { getInstance().config_.clear(); }
99