1 /******************************************************************************
2 *
3 * Copyright 2018 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
26 #include <config.h>
27
28 using namespace ::std;
29 using namespace ::android::base;
30
31 namespace {
32
findConfigPath()33 std::string findConfigPath() {
34 const vector<string> search_path = {"/odm/etc/", "/vendor/etc/", "/etc/"};
35 const string file_name = "libese-nxp.conf";
36
37 for (string path : search_path) {
38 path.append(file_name);
39 struct stat file_stat;
40 if (stat(path.c_str(), &file_stat) != 0) continue;
41 if (S_ISREG(file_stat.st_mode)) return path;
42 }
43 return "";
44 }
45
46 } // namespace
47
EseConfig()48 EseConfig::EseConfig() {
49 string config_path = findConfigPath();
50 CHECK(config_path != "");
51 config_.parseFromFile(config_path);
52 }
53
getInstance()54 EseConfig& EseConfig::getInstance() {
55 static EseConfig theInstance;
56 return theInstance;
57 }
58
hasKey(const std::string & key)59 bool EseConfig::hasKey(const std::string& key) {
60 return getInstance().config_.hasKey(key);
61 }
62
getString(const std::string & key)63 std::string EseConfig::getString(const std::string& key) {
64 return getInstance().config_.getString(key);
65 }
66
getString(const std::string & key,std::string default_value)67 std::string EseConfig::getString(const std::string& key,
68 std::string default_value) {
69 if (hasKey(key)) return getString(key);
70 return default_value;
71 }
72
getUnsigned(const std::string & key)73 unsigned EseConfig::getUnsigned(const std::string& key) {
74 return getInstance().config_.getUnsigned(key);
75 }
76
getUnsigned(const std::string & key,unsigned default_value)77 unsigned EseConfig::getUnsigned(const std::string& key,
78 unsigned default_value) {
79 if (hasKey(key)) return getUnsigned(key);
80 return default_value;
81 }
82
getBytes(const std::string & key)83 std::vector<uint8_t> EseConfig::getBytes(const std::string& key) {
84 return getInstance().config_.getBytes(key);
85 }
86
clear()87 void EseConfig::clear() { getInstance().config_.clear(); }
88