1 /******************************************************************************
2  *
3  *  Copyright (C) 2018 ST Microelectronics S.A.
4  *  Copyright 2018 NXP
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *
19  ******************************************************************************/
20 
21 #include "ese_config.h"
22 
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/parseint.h>
26 #include <android-base/strings.h>
27 
28 #include <config.h>
29 
30 using namespace ::std;
31 using namespace ::android::base;
32 
33 namespace {
34 
findConfigPath()35 std::string findConfigPath() {
36   const vector<string> search_path = {"/odm/etc/", "/vendor/etc/", "/etc/"};
37   const string file_name = "libese-hal-st.conf";
38 
39   for (string path : search_path) {
40     path.append(file_name);
41     struct stat file_stat;
42     if (stat(path.c_str(), &file_stat) != 0) continue;
43     if (S_ISREG(file_stat.st_mode)) return path;
44   }
45   return "";
46 }
47 
48 }  // namespace
49 
EseConfig()50 EseConfig::EseConfig() {
51   string config_path = findConfigPath();
52   CHECK(config_path != "");
53   config_.parseFromFile(config_path);
54 }
55 
getInstance()56 EseConfig& EseConfig::getInstance() {
57   static EseConfig theInstance;
58   return theInstance;
59 }
60 
hasKey(const std::string & key)61 bool EseConfig::hasKey(const std::string& key) {
62   return getInstance().config_.hasKey(key);
63 }
64 
getString(const std::string & key)65 std::string EseConfig::getString(const std::string& key) {
66   return getInstance().config_.getString(key);
67 }
68 
getString(const std::string & key,std::string default_value)69 std::string EseConfig::getString(const std::string& key,
70                                  std::string default_value) {
71   if (hasKey(key)) return getString(key);
72   return default_value;
73 }
74 
getUnsigned(const std::string & key)75 unsigned EseConfig::getUnsigned(const std::string& key) {
76   return getInstance().config_.getUnsigned(key);
77 }
78 
getUnsigned(const std::string & key,unsigned default_value)79 unsigned EseConfig::getUnsigned(const std::string& key,
80                                 unsigned default_value) {
81   if (hasKey(key)) return getUnsigned(key);
82   return default_value;
83 }
84 
getBytes(const std::string & key)85 std::vector<uint8_t> EseConfig::getBytes(const std::string& key) {
86   return getInstance().config_.getBytes(key);
87 }
88 
clear()89 void EseConfig::clear() { getInstance().config_.clear(); }
90