blob: 96acfe4619a920a7f9b9fc618811e170b8985696 [file] [log] [blame]
Andre Eisenbach38003282017-11-17 16:52:05 -08001/*
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"
Ruchi Kandoif4064152018-02-28 16:27:41 -080017#include "NfcAdaptation.h"
Andre Eisenbach38003282017-11-17 16:52:05 -080018
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
26using namespace ::std;
27using namespace ::android::base;
28
29namespace {
30
31std::string findConfigPath() {
Ruchi Kandoi7fbc86b2018-02-21 18:50:03 -080032 const vector<string> search_path = {"/odm/etc/", "/product/etc/", "/etc/"};
Andre Eisenbach38003282017-11-17 16:52:05 -080033 const string file_name = "libnfc-nci.conf";
34
35 for (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
44} // namespace
45
46NfcConfig::NfcConfig() {
47 string config_path = findConfigPath();
48 CHECK(config_path != "");
49 config_.parseFromFile(config_path);
Ruchi Kandoif4064152018-02-28 16:27:41 -080050 /* Read vendor specific configs */
51 NfcAdaptation& theInstance = NfcAdaptation::GetInstance();
52 std::map<std::string, ConfigValue> configMap;
53 theInstance.GetVendorConfigs(configMap);
54 for (auto config : configMap) {
55 config_.addConfig(config.first, config.second);
56 }
Andre Eisenbach38003282017-11-17 16:52:05 -080057}
58
59NfcConfig& NfcConfig::getInstance() {
60 static NfcConfig theInstance;
61 return theInstance;
62}
63
64bool NfcConfig::hasKey(const std::string& key) {
65 return getInstance().config_.hasKey(key);
66}
67
68std::string NfcConfig::getString(const std::string& key) {
69 return getInstance().config_.getString(key);
70}
71
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080072std::string NfcConfig::getString(const std::string& key,
73 std::string default_value) {
74 if (hasKey(key)) return getString(key);
75 return default_value;
76}
77
Andre Eisenbach38003282017-11-17 16:52:05 -080078unsigned NfcConfig::getUnsigned(const std::string& key) {
79 return getInstance().config_.getUnsigned(key);
80}
81
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080082unsigned NfcConfig::getUnsigned(const std::string& key,
83 unsigned default_value) {
84 if (hasKey(key)) return getUnsigned(key);
85 return default_value;
86}
87
Andre Eisenbach38003282017-11-17 16:52:05 -080088std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) {
89 return getInstance().config_.getBytes(key);
90}
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080091
92void NfcConfig::clear() { getInstance().config_.clear(); }