blob: 5891cc401b357c1a7f416378c2391aedbf187783 [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() {
Hiroki Yamamoto4c168af2018-04-04 14:01:59 +090032 const vector<string> search_path = {"/odm/etc/", "/vendor/etc/",
33 "/product/etc/", "/etc/"};
Andre Eisenbach38003282017-11-17 16:52:05 -080034 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
Ruchi Kandoiaf716282018-03-13 14:31:11 -070047void NfcConfig::loadConfig() {
Andre Eisenbach38003282017-11-17 16:52:05 -080048 string config_path = findConfigPath();
49 CHECK(config_path != "");
50 config_.parseFromFile(config_path);
Ruchi Kandoif4064152018-02-28 16:27:41 -080051 /* 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 }
Andre Eisenbach38003282017-11-17 16:52:05 -080058}
59
Ruchi Kandoiaf716282018-03-13 14:31:11 -070060NfcConfig::NfcConfig() { loadConfig(); }
61
Andre Eisenbach38003282017-11-17 16:52:05 -080062NfcConfig& NfcConfig::getInstance() {
63 static NfcConfig theInstance;
Ruchi Kandoiaf716282018-03-13 14:31:11 -070064 if (theInstance.config_.isEmpty()) {
65 theInstance.loadConfig();
66 }
Andre Eisenbach38003282017-11-17 16:52:05 -080067 return theInstance;
68}
69
70bool NfcConfig::hasKey(const std::string& key) {
71 return getInstance().config_.hasKey(key);
72}
73
74std::string NfcConfig::getString(const std::string& key) {
75 return getInstance().config_.getString(key);
76}
77
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080078std::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
Andre Eisenbach38003282017-11-17 16:52:05 -080084unsigned NfcConfig::getUnsigned(const std::string& key) {
85 return getInstance().config_.getUnsigned(key);
86}
87
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080088unsigned NfcConfig::getUnsigned(const std::string& key,
89 unsigned default_value) {
90 if (hasKey(key)) return getUnsigned(key);
91 return default_value;
92}
93
Andre Eisenbach38003282017-11-17 16:52:05 -080094std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) {
95 return getInstance().config_.getBytes(key);
96}
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080097
98void NfcConfig::clear() { getInstance().config_.clear(); }