blob: 57ae044072bf24f9afa92e27e0c589262570403a [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"
17
18#include <android-base/file.h>
19#include <android-base/logging.h>
20#include <android-base/parseint.h>
21#include <android-base/strings.h>
22
23#include <config.h>
24
25using namespace ::std;
26using namespace ::android::base;
27
28namespace {
29
30std::string findConfigPath() {
Ruchi Kandoi7fbc86b2018-02-21 18:50:03 -080031 const vector<string> search_path = {"/odm/etc/", "/product/etc/", "/etc/"};
Andre Eisenbach38003282017-11-17 16:52:05 -080032 const string file_name = "libnfc-nci.conf";
33
34 for (string path : search_path) {
35 path.append(file_name);
36 struct stat file_stat;
37 if (stat(path.c_str(), &file_stat) != 0) continue;
38 if (S_ISREG(file_stat.st_mode)) return path;
39 }
40 return "";
41}
42
43} // namespace
44
45NfcConfig::NfcConfig() {
46 string config_path = findConfigPath();
47 CHECK(config_path != "");
48 config_.parseFromFile(config_path);
49}
50
51NfcConfig& NfcConfig::getInstance() {
52 static NfcConfig theInstance;
53 return theInstance;
54}
55
56bool NfcConfig::hasKey(const std::string& key) {
57 return getInstance().config_.hasKey(key);
58}
59
60std::string NfcConfig::getString(const std::string& key) {
61 return getInstance().config_.getString(key);
62}
63
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080064std::string NfcConfig::getString(const std::string& key,
65 std::string default_value) {
66 if (hasKey(key)) return getString(key);
67 return default_value;
68}
69
Andre Eisenbach38003282017-11-17 16:52:05 -080070unsigned NfcConfig::getUnsigned(const std::string& key) {
71 return getInstance().config_.getUnsigned(key);
72}
73
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080074unsigned NfcConfig::getUnsigned(const std::string& key,
75 unsigned default_value) {
76 if (hasKey(key)) return getUnsigned(key);
77 return default_value;
78}
79
Andre Eisenbach38003282017-11-17 16:52:05 -080080std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) {
81 return getInstance().config_.getBytes(key);
82}
Andre Eisenbacha34d7d12017-11-27 13:16:18 -080083
84void NfcConfig::clear() { getInstance().config_.clear(); }