1 /*
2 * Copyright (C) 2020 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 #include "Fingerprint.h"
18 #include "Session.h"
19
20 #include <android-base/properties.h>
21 #include <fingerprint.sysprop.h>
22
23 #include <android-base/file.h>
24 #include <android-base/logging.h>
25 #include <android-base/stringprintf.h>
26
27 using namespace ::android::fingerprint::virt;
28
29 namespace aidl::android::hardware::biometrics::fingerprint {
30 namespace {
31 constexpr size_t MAX_WORKER_QUEUE_SIZE = 5;
32 constexpr int SENSOR_ID = 5;
33 constexpr common::SensorStrength SENSOR_STRENGTH = common::SensorStrength::STRONG;
34 constexpr int MAX_ENROLLMENTS_PER_USER = 5;
35 constexpr bool SUPPORTS_NAVIGATION_GESTURES = true;
36 constexpr char HW_COMPONENT_ID[] = "fingerprintSensor";
37 constexpr char HW_VERSION[] = "vendor/model/revision";
38 constexpr char FW_VERSION[] = "1.01";
39 constexpr char SERIAL_NUMBER[] = "00000001";
40 constexpr char SW_COMPONENT_ID[] = "matchingAlgorithm";
41 constexpr char SW_VERSION[] = "vendor/version/revision";
42
43 } // namespace
44
Fingerprint()45 Fingerprint::Fingerprint() : mWorker(MAX_WORKER_QUEUE_SIZE) {
46 std::string sensorTypeProp = FingerprintHalProperties::type().value_or("");
47 if (sensorTypeProp == "" || sensorTypeProp == "default" || sensorTypeProp == "rear") {
48 mSensorType = FingerprintSensorType::REAR;
49 mEngine = std::make_unique<FakeFingerprintEngineRear>();
50 } else if (sensorTypeProp == "udfps") {
51 mSensorType = FingerprintSensorType::UNDER_DISPLAY_OPTICAL;
52 mEngine = std::make_unique<FakeFingerprintEngineUdfps>();
53 } else if (sensorTypeProp == "side") {
54 mSensorType = FingerprintSensorType::POWER_BUTTON;
55 mEngine = std::make_unique<FakeFingerprintEngineSide>();
56 } else {
57 mSensorType = FingerprintSensorType::UNKNOWN;
58 mEngine = std::make_unique<FakeFingerprintEngineRear>();
59 UNIMPLEMENTED(FATAL) << "unrecognized or unimplemented fingerprint behavior: "
60 << sensorTypeProp;
61 }
62 LOG(INFO) << "sensorTypeProp:" << sensorTypeProp;
63 LOG(INFO) << "ro.product.name=" << ::android::base::GetProperty("ro.product.name", "UNKNOWN");
64 }
65
getSensorProps(std::vector<SensorProps> * out)66 ndk::ScopedAStatus Fingerprint::getSensorProps(std::vector<SensorProps>* out) {
67 std::vector<common::ComponentInfo> componentInfo = {
68 {HW_COMPONENT_ID, HW_VERSION, FW_VERSION, SERIAL_NUMBER, "" /* softwareVersion */},
69 {SW_COMPONENT_ID, "" /* hardwareVersion */, "" /* firmwareVersion */,
70 "" /* serialNumber */, SW_VERSION}};
71 auto sensorId = FingerprintHalProperties::sensor_id().value_or(SENSOR_ID);
72 auto sensorStrength =
73 FingerprintHalProperties::sensor_strength().value_or((int)SENSOR_STRENGTH);
74 auto maxEnrollments =
75 FingerprintHalProperties::max_enrollments().value_or(MAX_ENROLLMENTS_PER_USER);
76 auto navigationGuesture = FingerprintHalProperties::navigation_guesture().value_or(false);
77 auto detectInteraction = FingerprintHalProperties::detect_interaction().value_or(false);
78 auto displayTouch = FingerprintHalProperties::display_touch().value_or(true);
79 auto controlIllumination = FingerprintHalProperties::control_illumination().value_or(false);
80
81 common::CommonProps commonProps = {sensorId, (common::SensorStrength)sensorStrength,
82 maxEnrollments, componentInfo};
83
84 SensorLocation sensorLocation = mEngine->getSensorLocation();
85
86 LOG(INFO) << "sensor type:" << ::android::internal::ToString(mSensorType)
87 << " location:" << sensorLocation.toString();
88
89 *out = {{commonProps,
90 mSensorType,
91 {sensorLocation},
92 navigationGuesture,
93 detectInteraction,
94 displayTouch,
95 controlIllumination,
96 std::nullopt}};
97 return ndk::ScopedAStatus::ok();
98 }
99
createSession(int32_t sensorId,int32_t userId,const std::shared_ptr<ISessionCallback> & cb,std::shared_ptr<ISession> * out)100 ndk::ScopedAStatus Fingerprint::createSession(int32_t sensorId, int32_t userId,
101 const std::shared_ptr<ISessionCallback>& cb,
102 std::shared_ptr<ISession>* out) {
103 CHECK(mSession == nullptr || mSession->isClosed()) << "Open session already exists!";
104
105 mSession = SharedRefBase::make<Session>(sensorId, userId, cb, mEngine.get(), &mWorker);
106 *out = mSession;
107
108 mSession->linkToDeath(cb->asBinder().get());
109
110 LOG(INFO) << __func__ << ": sensorId:" << sensorId << " userId:" << userId;
111 return ndk::ScopedAStatus::ok();
112 }
113
dump(int fd,const char **,uint32_t numArgs)114 binder_status_t Fingerprint::dump(int fd, const char** /*args*/, uint32_t numArgs) {
115 if (fd < 0) {
116 LOG(ERROR) << __func__ << "fd invalid: " << fd;
117 return STATUS_BAD_VALUE;
118 } else {
119 LOG(INFO) << __func__ << " fd:" << fd << "numArgs:" << numArgs;
120 }
121
122 dprintf(fd, "----- FingerprintVirtualHal::dump -----\n");
123 std::vector<SensorProps> sps(1);
124 getSensorProps(&sps);
125 for (auto& sp : sps) {
126 ::android::base::WriteStringToFd(sp.toString(), fd);
127 }
128 ::android::base::WriteStringToFd(mEngine->toString(), fd);
129
130 fsync(fd);
131 return STATUS_OK;
132 }
133
handleShellCommand(int in,int out,int err,const char ** args,uint32_t numArgs)134 binder_status_t Fingerprint::handleShellCommand(int in, int out, int err, const char** args,
135 uint32_t numArgs) {
136 LOG(INFO) << __func__ << " in:" << in << " out:" << out << " err:" << err
137 << " numArgs:" << numArgs;
138
139 if (numArgs == 0) {
140 LOG(INFO) << __func__ << ": available commands";
141 onHelp(out);
142 return STATUS_OK;
143 }
144
145 for (auto&& str : std::vector<std::string_view>(args, args + numArgs)) {
146 std::string option = str.data();
147 if (option.find("clearconfig") != std::string::npos ||
148 option.find("resetconfig") != std::string::npos) {
149 resetConfigToDefault();
150 }
151 if (option.find("help") != std::string::npos) {
152 onHelp(out);
153 }
154 }
155
156 return STATUS_OK;
157 }
158
onHelp(int fd)159 void Fingerprint::onHelp(int fd) {
160 dprintf(fd, "Virtual HAL commands:\n");
161 dprintf(fd, " help: print this help\n");
162 dprintf(fd, " resetconfig: reset all configuration to default\n");
163 dprintf(fd, "\n");
164 fsync(fd);
165 }
166
resetConfigToDefault()167 void Fingerprint::resetConfigToDefault() {
168 LOG(INFO) << __func__ << ": reset virtual HAL configuration to default";
169 #define RESET_CONFIG_O(__NAME__) \
170 if (FingerprintHalProperties::__NAME__()) FingerprintHalProperties::__NAME__(std::nullopt)
171 #define RESET_CONFIG_V(__NAME__) \
172 if (!FingerprintHalProperties::__NAME__().empty()) \
173 FingerprintHalProperties::__NAME__({std::nullopt})
174
175 RESET_CONFIG_O(type);
176 RESET_CONFIG_V(enrollments);
177 RESET_CONFIG_O(enrollment_hit);
178 RESET_CONFIG_O(authenticator_id);
179 RESET_CONFIG_O(challenge);
180 RESET_CONFIG_O(lockout);
181 RESET_CONFIG_O(operation_authenticate_fails);
182 RESET_CONFIG_O(operation_detect_interaction_error);
183 RESET_CONFIG_O(operation_enroll_error);
184 RESET_CONFIG_V(operation_authenticate_latency);
185 RESET_CONFIG_V(operation_detect_interaction_latency);
186 RESET_CONFIG_V(operation_enroll_latency);
187 RESET_CONFIG_O(operation_authenticate_duration);
188 RESET_CONFIG_O(operation_authenticate_error);
189 RESET_CONFIG_O(sensor_location);
190 RESET_CONFIG_O(operation_authenticate_acquired);
191 RESET_CONFIG_O(operation_detect_interaction_duration);
192 RESET_CONFIG_O(operation_detect_interaction_acquired);
193 RESET_CONFIG_O(sensor_id);
194 RESET_CONFIG_O(sensor_strength);
195 RESET_CONFIG_O(max_enrollments);
196 RESET_CONFIG_O(navigation_guesture);
197 RESET_CONFIG_O(detect_interaction);
198 RESET_CONFIG_O(display_touch);
199 RESET_CONFIG_O(control_illumination);
200 RESET_CONFIG_O(lockout_enable);
201 RESET_CONFIG_O(lockout_timed_threshold);
202 RESET_CONFIG_O(lockout_timed_duration);
203 RESET_CONFIG_O(lockout_permanent_threshold);
204 }
205
206 } // namespace aidl::android::hardware::biometrics::fingerprint
207