Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 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" |
Ruchi Kandoi | f406415 | 2018-02-28 16:27:41 -0800 | [diff] [blame] | 17 | #include "NfcAdaptation.h" |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 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 | |
| 26 | using namespace ::std; |
| 27 | using namespace ::android::base; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | std::string findConfigPath() { |
Ruchi Kandoi | 7fbc86b | 2018-02-21 18:50:03 -0800 | [diff] [blame] | 32 | const vector<string> search_path = {"/odm/etc/", "/product/etc/", "/etc/"}; |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 33 | 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 | |
Ruchi Kandoi | af71628 | 2018-03-13 14:31:11 -0700 | [diff] [blame^] | 46 | void NfcConfig::loadConfig() { |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 47 | string config_path = findConfigPath(); |
| 48 | CHECK(config_path != ""); |
| 49 | config_.parseFromFile(config_path); |
Ruchi Kandoi | f406415 | 2018-02-28 16:27:41 -0800 | [diff] [blame] | 50 | /* 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 Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Ruchi Kandoi | af71628 | 2018-03-13 14:31:11 -0700 | [diff] [blame^] | 59 | NfcConfig::NfcConfig() { loadConfig(); } |
| 60 | |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 61 | NfcConfig& NfcConfig::getInstance() { |
| 62 | static NfcConfig theInstance; |
Ruchi Kandoi | af71628 | 2018-03-13 14:31:11 -0700 | [diff] [blame^] | 63 | if (theInstance.config_.isEmpty()) { |
| 64 | theInstance.loadConfig(); |
| 65 | } |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 66 | return theInstance; |
| 67 | } |
| 68 | |
| 69 | bool NfcConfig::hasKey(const std::string& key) { |
| 70 | return getInstance().config_.hasKey(key); |
| 71 | } |
| 72 | |
| 73 | std::string NfcConfig::getString(const std::string& key) { |
| 74 | return getInstance().config_.getString(key); |
| 75 | } |
| 76 | |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 77 | std::string NfcConfig::getString(const std::string& key, |
| 78 | std::string default_value) { |
| 79 | if (hasKey(key)) return getString(key); |
| 80 | return default_value; |
| 81 | } |
| 82 | |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 83 | unsigned NfcConfig::getUnsigned(const std::string& key) { |
| 84 | return getInstance().config_.getUnsigned(key); |
| 85 | } |
| 86 | |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 87 | unsigned NfcConfig::getUnsigned(const std::string& key, |
| 88 | unsigned default_value) { |
| 89 | if (hasKey(key)) return getUnsigned(key); |
| 90 | return default_value; |
| 91 | } |
| 92 | |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 93 | std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) { |
| 94 | return getInstance().config_.getBytes(key); |
| 95 | } |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 96 | |
| 97 | void NfcConfig::clear() { getInstance().config_.clear(); } |