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 26using namespace ::std; 27using namespace ::android::base; 28 29namespace { 30 31std::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 47void 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 60NfcConfig::NfcConfig() { loadConfig(); } 61 62NfcConfig& NfcConfig::getInstance() { 63 static NfcConfig theInstance; 64 if (theInstance.config_.isEmpty()) { 65 theInstance.loadConfig(); 66 } 67 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 78std::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 84unsigned NfcConfig::getUnsigned(const std::string& key) { 85 return getInstance().config_.getUnsigned(key); 86} 87 88unsigned NfcConfig::getUnsigned(const std::string& key, 89 unsigned default_value) { 90 if (hasKey(key)) return getUnsigned(key); 91 return default_value; 92} 93 94std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) { 95 return getInstance().config_.getBytes(key); 96} 97 98void NfcConfig::clear() { getInstance().config_.clear(); } 99