1 /*
2 * Copyright (C) 2024 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
17 #define LOG_TAG "VirtualHalConfig"
18
19 #include "config/Config.h"
20 #include <android-base/logging.h>
21 #include <android-base/parseint.h>
22 #include "../../util/include/util/Util.h"
23
24 using ::android::base::ParseInt;
25
26 namespace aidl::android::hardware::biometrics {
27
Config()28 Config::Config() : mSource(Config::ConfigSourceType::SOURCE_SYSPROP) {}
29
parseBool(const std::string & value)30 ConfigValue Config::parseBool(const std::string& value) {
31 OptBool res;
32 if (value == "true")
33 res.emplace(true);
34 else if (value == "false")
35 res.emplace(false);
36 else
37 LOG(FATAL) << "ERROR: invalid bool " << value;
38 return res;
39 }
40
parseString(const std::string & value)41 ConfigValue Config::parseString(const std::string& value) {
42 OptString res;
43 if (!value.empty()) res.emplace(value);
44 return res;
45 }
46
parseInt32(const std::string & value)47 ConfigValue Config::parseInt32(const std::string& value) {
48 OptInt32 res;
49 if (!value.empty()) {
50 std::int32_t val;
51 if (ParseInt(value, &val)) {
52 res.emplace(val);
53 } else {
54 LOG(FATAL) << "ERROR: Could not parse " << value << " as Int32";
55 }
56 }
57 return res;
58 }
59
parseInt64(const std::string & value)60 ConfigValue Config::parseInt64(const std::string& value) {
61 OptInt64 res;
62 if (!value.empty()) {
63 std::int64_t val = std::strtoull(value.c_str(), nullptr, 10);
64 if (val != 0LL or (val == 0LL && value == "0")) {
65 res.emplace(val);
66 } else {
67 LOG(FATAL) << "ERROR: Could not parse " << value << " as Int64";
68 }
69 }
70 return res;
71 }
72
parseIntVec(const std::string & value)73 ConfigValue Config::parseIntVec(const std::string& value) {
74 OptIntVec res;
75 for (auto& i : Util::parseIntSequence(value)) {
76 res.push_back(i);
77 }
78 return res;
79 }
80
init()81 void Config::init() {
82 LOG(INFO) << "calling init()";
83 int len = 0;
84 Config::Data* pd = getConfigData(&len);
85 for (int i = 0; i < len; i++) {
86 LOG(INFO) << "init():" << pd->name;
87 pd->value = (this->*(pd->parser))(pd->defaultValue);
88 setConfig(pd->name, *pd);
89 ++pd;
90 }
91 }
92
setParam(const std::string & name,const std::string & value)93 bool Config::setParam(const std::string& name, const std::string& value) {
94 auto it = mMap.find(name);
95 if (it == mMap.end()) {
96 LOG(FATAL) << "ERROR: setParam unknown config name " << name;
97 return false;
98 }
99 LOG(INFO) << "setParam name=" << name << "=" << value;
100
101 it->second.value = (this->*(it->second.parser))(value);
102
103 mSource = ConfigSourceType::SOURCE_AIDL;
104
105 return true;
106 }
107
getInternal(const std::string & name)108 ConfigValue Config::getInternal(const std::string& name) {
109 ConfigValue res;
110
111 auto& data = mMap[name];
112 switch (mSource) {
113 case ConfigSourceType::SOURCE_SYSPROP:
114 res = data.getter();
115 break;
116 case ConfigSourceType::SOURCE_AIDL:
117 res = data.value;
118 break;
119 case ConfigSourceType::SOURCE_FILE:
120 UNIMPLEMENTED(ERROR) << " File-based config is not supported yet";
121 break;
122 default:
123 LOG(FATAL) << "Wrong srouce type " << (int)mSource;
124 break;
125 }
126
127 return res;
128 }
129
getDefault(const std::string & name)130 ConfigValue Config::getDefault(const std::string& name) {
131 return mMap[name].value;
132 }
133
setInternal(const std::string & name,const ConfigValue & val)134 bool Config::setInternal(const std::string& name, const ConfigValue& val) {
135 LOG(INFO) << "Config::set " << name << " to " << toString(val);
136 bool res = false;
137 auto& data = mMap[name];
138
139 switch (mSource) {
140 case ConfigSourceType::SOURCE_SYSPROP:
141 res = data.setter(val);
142 break;
143 case ConfigSourceType::SOURCE_AIDL:
144 data.value = val;
145 res = true;
146 break;
147 case ConfigSourceType::SOURCE_FILE:
148 UNIMPLEMENTED(ERROR) << " File-based config is not supported yet";
149 break;
150 default:
151 LOG(FATAL) << "Wrong srouce type " << (int)mSource;
152 break;
153 }
154
155 return res;
156 }
157 } // namespace aidl::android::hardware::biometrics
158