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/properties.h>
23 #include <android-base/strings.h>
24
25 #include <config.h>
26
27 using namespace ::std;
28 using namespace ::android::base;
29
30 namespace {
searchConfigPath(std::string file_name)31 std::string searchConfigPath(std::string file_name) {
32 const std::vector<std::string> search_path = {
33 "/product/etc/", "/odm/etc/", "/vendor/etc/", "/system_ext/etc/", "/etc/",
34 };
35 for (std::string path : search_path) {
36 path.append(file_name);
37 struct stat file_stat;
38 if (stat(path.c_str(), &file_stat) != 0) continue;
39 if (S_ISREG(file_stat.st_mode)) return path;
40 }
41 return "";
42 }
43 // Configuration File Search sequence
44 // 1. If prop_config_file_name is defined.(where prop_config_file_name is the
45 // value of the property persist.nfc_cfg.config_file_name)
46 // Search a file matches prop_config_file_name.
47 // 2. If SKU is defined (where SKU is the value of the property
48 // ro.boot.product.hardware.sku)
49 // Search a file matches libnfc-nci-SKU.conf
50 // 3. If none of 1,2 is defined, search a default file name "libnfc-nci.conf".
findConfigPath()51 std::string findConfigPath() {
52 string f_path = searchConfigPath(
53 android::base::GetProperty("persist.nfc_cfg.config_file_name", ""));
54 if (!f_path.empty()) return f_path;
55
56 // Search for libnfc-nci-SKU.conf
57 f_path = searchConfigPath(
58 "libnfc-nci-" +
59 android::base::GetProperty("ro.boot.product.hardware.sku", "") + ".conf");
60 if (!f_path.empty()) return f_path;
61
62 // load default file if the desired file not found.
63 return searchConfigPath("libnfc-nci.conf");
64 }
65
66 } // namespace
67
loadConfig()68 void NfcConfig::loadConfig() {
69 string config_path = findConfigPath();
70 CHECK(config_path != "");
71 config_.parseFromFile(config_path);
72 /* Read vendor specific configs */
73 NfcAdaptation& theInstance = NfcAdaptation::GetInstance();
74 std::map<std::string, ConfigValue> configMap;
75 theInstance.GetVendorConfigs(configMap);
76 for (auto config : configMap) {
77 config_.addConfig(config.first, config.second);
78 }
79 }
80
NfcConfig()81 NfcConfig::NfcConfig() { loadConfig(); }
82
getInstance()83 NfcConfig& NfcConfig::getInstance() {
84 static NfcConfig theInstance;
85 if (theInstance.config_.isEmpty()) {
86 theInstance.loadConfig();
87 }
88 return theInstance;
89 }
90
hasKey(const std::string & key)91 bool NfcConfig::hasKey(const std::string& key) {
92 return getInstance().config_.hasKey(key);
93 }
94
getString(const std::string & key)95 std::string NfcConfig::getString(const std::string& key) {
96 return getInstance().config_.getString(key);
97 }
98
getString(const std::string & key,std::string default_value)99 std::string NfcConfig::getString(const std::string& key,
100 std::string default_value) {
101 if (hasKey(key)) return getString(key);
102 return default_value;
103 }
104
getUnsigned(const std::string & key)105 unsigned NfcConfig::getUnsigned(const std::string& key) {
106 return getInstance().config_.getUnsigned(key);
107 }
108
getUnsigned(const std::string & key,unsigned default_value)109 unsigned NfcConfig::getUnsigned(const std::string& key,
110 unsigned default_value) {
111 if (hasKey(key)) return getUnsigned(key);
112 return default_value;
113 }
114
getBytes(const std::string & key)115 std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) {
116 return getInstance().config_.getBytes(key);
117 }
118
clear()119 void NfcConfig::clear() { getInstance().config_.clear(); }
120