blob: 01ae86461d9b0fefce3842a21425cfa40f446c77 [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
37 LOG(ERROR) << "ERROR: invalid bool " << value;
38 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;
51 if (ParseInt(value, &val)) res.emplace(val);
52 }
53 return res;
54}
55
56ConfigValue Config::parseInt64(const std::string& value) {
57 OptInt64 res;
58 if (!value.empty()) {
59 std::int64_t val = std::strtoull(value.c_str(), nullptr, 10);
60 if (val != 0LL or (val == 0LL && value == "0")) {
61 res.emplace(val);
62 }
63 }
64 return res;
65}
66
67ConfigValue Config::parseIntVec(const std::string& value) {
68 OptIntVec res;
69 for (auto& i : Util::parseIntSequence(value)) {
70 res.push_back(i);
71 }
72 return res;
73}
74
75void Config::init() {
76 LOG(INFO) << "calling init()";
77 int len = 0;
78 Config::Data* pd = getConfigData(&len);
79 for (int i = 0; i < len; i++) {
80 LOG(INFO) << "init():" << pd->name;
81 pd->value = (this->*(pd->parser))(pd->defaultValue);
82 setConfig(pd->name, *pd);
83 ++pd;
84 }
85}
86
87bool Config::setParam(const std::string& name, const std::string& value) {
88 auto it = mMap.find(name);
89 if (it == mMap.end()) {
90 LOG(ERROR) << "ERROR: setParam unknown config name " << name;
91 return false;
92 }
93 LOG(INFO) << "setParam name=" << name << "=" << value;
94
95 it->second.value = (this->*(it->second.parser))(value);
96
97 mSource = ConfigSourceType::SOURCE_AIDL;
98
99 return true;
100}
101
102ConfigValue Config::getInternal(const std::string& name) {
103 ConfigValue res;
104
105 auto data = mMap[name];
106 switch (mSource) {
107 case ConfigSourceType::SOURCE_SYSPROP:
108 res = data.getter();
109 break;
110 case ConfigSourceType::SOURCE_AIDL:
111 res = data.value;
112 break;
113 case ConfigSourceType::SOURCE_FILE:
114 LOG(WARNING) << "Unsupported";
115 break;
116 default:
117 LOG(ERROR) << " wrong srouce type " << (int)mSource;
118 break;
119 }
120
121 return res;
122}
123
124ConfigValue Config::getDefault(const std::string& name) {
125 return mMap[name].value;
126}
127
128bool Config::setInternal(const std::string& name, const ConfigValue& val) {
129 bool res = false;
130 auto data = mMap[name];
131
132 switch (mSource) {
133 case ConfigSourceType::SOURCE_SYSPROP:
134 res = data.setter(val);
135 break;
136 case ConfigSourceType::SOURCE_AIDL:
137 data.value = val;
138 res = true;
139 break;
140 case ConfigSourceType::SOURCE_FILE:
141 LOG(WARNING) << "Unsupported";
142 break;
143 default:
144 LOG(ERROR) << " wrong srouce type " << (int)mSource;
145 break;
146 }
147
148 return res;
149}
150} // namespace aidl::android::hardware::biometrics