1 /******************************************************************************
2 *
3 * Copyright 2018,2023 NXP
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include "ese_config.h"
20
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/parseint.h>
24 #include <android-base/strings.h>
25 #include <config.h>
26
27 using namespace ::std;
28 using namespace ::android::base;
29
30 namespace {
31
findConfigPath()32 std::string findConfigPath() {
33 const vector<string> search_path = {"/odm/etc/", "/vendor/etc/", "/etc/"};
34 const string file_name = "libese-nxp.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
EseConfig()47 EseConfig::EseConfig() {
48 string config_path = findConfigPath();
49 CHECK(config_path != "");
50 config_.parseFromFile(config_path);
51 }
52
getInstance()53 EseConfig& EseConfig::getInstance() {
54 static EseConfig theInstance;
55 return theInstance;
56 }
57
hasKey(const std::string & key)58 bool EseConfig::hasKey(const std::string& key) {
59 return getInstance().config_.hasKey(key);
60 }
61
getString(const std::string & key)62 std::string EseConfig::getString(const std::string& key) {
63 return getInstance().config_.getString(key);
64 }
65
getString(const std::string & key,std::string default_value)66 std::string EseConfig::getString(const std::string& key,
67 std::string default_value) {
68 if (hasKey(key)) return getString(key);
69 return default_value;
70 }
71
getUnsigned(const std::string & key)72 unsigned EseConfig::getUnsigned(const std::string& key) {
73 return getInstance().config_.getUnsigned(key);
74 }
75
getUnsigned(const std::string & key,unsigned default_value)76 unsigned EseConfig::getUnsigned(const std::string& key,
77 unsigned default_value) {
78 if (hasKey(key)) return getUnsigned(key);
79 return default_value;
80 }
81