blob: 49f7cc8551947028dc0800974e677494b0e0a118 [file] [log] [blame]
Jeff Pu6ccd9562024-02-21 10:46:35 -05001/*
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
24using ::android::base::ParseInt;
25
26namespace aidl::android::hardware::biometrics {
27
28Config::Config() : mSource(Config::ConfigSourceType::SOURCE_SYSPROP) {}
29
30ConfigValue 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
Jeff Pudf81c962024-03-06 10:58:17 -050037 LOG(FATAL) << "ERROR: invalid bool " << value;
Jeff Pu6ccd9562024-02-21 10:46:35 -050038 return res;
39}
40
41ConfigValue Config::parseString(const std::string& value) {
42 OptString res;
43 if (!value.empty()) res.emplace(value);
44 return res;
45}
46
47ConfigValue Config::parseInt32(const std::string& value) {
48 OptInt32 res;
49 if (!value.empty()) {
50 std::int32_t val;
Jeff Pudf81c962024-03-06 10:58:17 -050051 if (ParseInt(value, &val)) {
52 res.emplace(val);
53 } else {
54 LOG(FATAL) << "ERROR: Could not parse " << value << " as Int32";
55 }
Jeff Pu6ccd9562024-02-21 10:46:35 -050056 }
57 return res;
58}
59
60ConfigValue 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);
Jeff Pudf81c962024-03-06 10:58:17 -050066 } else {
67 LOG(FATAL) << "ERROR: Could not parse " << value << " as Int64";
Jeff Pu6ccd9562024-02-21 10:46:35 -050068 }
69 }
70 return res;
71}
72
73ConfigValue 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
81void 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
93bool Config::setParam(const std::string& name, const std::string& value) {
94 auto it = mMap.find(name);
95 if (it == mMap.end()) {
Jeff Pudf81c962024-03-06 10:58:17 -050096 LOG(FATAL) << "ERROR: setParam unknown config name " << name;
Jeff Pu6ccd9562024-02-21 10:46:35 -050097 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
108ConfigValue Config::getInternal(const std::string& name) {
109 ConfigValue res;
110
Jeff Pudf81c962024-03-06 10:58:17 -0500111 auto& data = mMap[name];
Jeff Pu6ccd9562024-02-21 10:46:35 -0500112 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:
Jeff Pudf81c962024-03-06 10:58:17 -0500120 UNIMPLEMENTED(ERROR) << " File-based config is not supported yet";
Jeff Pu6ccd9562024-02-21 10:46:35 -0500121 break;
122 default:
Jeff Pudf81c962024-03-06 10:58:17 -0500123 LOG(FATAL) << "Wrong srouce type " << (int)mSource;
Jeff Pu6ccd9562024-02-21 10:46:35 -0500124 break;
125 }
126
127 return res;
128}
129
130ConfigValue Config::getDefault(const std::string& name) {
131 return mMap[name].value;
132}
133
134bool Config::setInternal(const std::string& name, const ConfigValue& val) {
Jeff Pueda68e42024-04-10 15:47:59 -0400135 LOG(INFO) << "Config::set " << name << " to " << toString(val);
Jeff Pu6ccd9562024-02-21 10:46:35 -0500136 bool res = false;
Jeff Pudf81c962024-03-06 10:58:17 -0500137 auto& data = mMap[name];
Jeff Pu6ccd9562024-02-21 10:46:35 -0500138
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:
Jeff Pudf81c962024-03-06 10:58:17 -0500148 UNIMPLEMENTED(ERROR) << " File-based config is not supported yet";
Jeff Pu6ccd9562024-02-21 10:46:35 -0500149 break;
150 default:
Jeff Pudf81c962024-03-06 10:58:17 -0500151 LOG(FATAL) << "Wrong srouce type " << (int)mSource;
Jeff Pu6ccd9562024-02-21 10:46:35 -0500152 break;
153 }
154
155 return res;
156}
157} // namespace aidl::android::hardware::biometrics