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" |
| 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 | |
| 25 | using namespace ::std; |
| 26 | using namespace ::android::base; |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | std::string findConfigPath() { |
Ruchi Kandoi | 7fbc86b | 2018-02-21 18:50:03 -0800 | [diff] [blame^] | 31 | const vector<string> search_path = {"/odm/etc/", "/product/etc/", "/etc/"}; |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 32 | 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 | |
| 45 | NfcConfig::NfcConfig() { |
| 46 | string config_path = findConfigPath(); |
| 47 | CHECK(config_path != ""); |
| 48 | config_.parseFromFile(config_path); |
| 49 | } |
| 50 | |
| 51 | NfcConfig& NfcConfig::getInstance() { |
| 52 | static NfcConfig theInstance; |
| 53 | return theInstance; |
| 54 | } |
| 55 | |
| 56 | bool NfcConfig::hasKey(const std::string& key) { |
| 57 | return getInstance().config_.hasKey(key); |
| 58 | } |
| 59 | |
| 60 | std::string NfcConfig::getString(const std::string& key) { |
| 61 | return getInstance().config_.getString(key); |
| 62 | } |
| 63 | |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 64 | std::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 Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 70 | unsigned NfcConfig::getUnsigned(const std::string& key) { |
| 71 | return getInstance().config_.getUnsigned(key); |
| 72 | } |
| 73 | |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 74 | unsigned NfcConfig::getUnsigned(const std::string& key, |
| 75 | unsigned default_value) { |
| 76 | if (hasKey(key)) return getUnsigned(key); |
| 77 | return default_value; |
| 78 | } |
| 79 | |
Andre Eisenbach | 3800328 | 2017-11-17 16:52:05 -0800 | [diff] [blame] | 80 | std::vector<uint8_t> NfcConfig::getBytes(const std::string& key) { |
| 81 | return getInstance().config_.getBytes(key); |
| 82 | } |
Andre Eisenbach | a34d7d1 | 2017-11-27 13:16:18 -0800 | [diff] [blame] | 83 | |
| 84 | void NfcConfig::clear() { getInstance().config_.clear(); } |