blob: 143e23132a8fc7e62968854925c3958a5d29dddb [file] [log] [blame]
Ilya Matyukhina9a3c852020-08-18 03:09:41 -07001/*
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
Jeff Pudef5b042023-05-25 14:28:16 -040020#include <android-base/properties.h>
Jeff Pu52653182022-10-12 16:27:23 -040021#include <fingerprint.sysprop.h>
22
23#include <android-base/file.h>
Joe Bolingerde94aa02021-12-09 17:00:32 -080024#include <android-base/logging.h>
Jeff Pu52653182022-10-12 16:27:23 -040025#include <android-base/stringprintf.h>
Joe Bolingerde94aa02021-12-09 17:00:32 -080026
27using namespace ::android::fingerprint::virt;
28
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070029namespace aidl::android::hardware::biometrics::fingerprint {
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080030namespace {
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080031constexpr size_t MAX_WORKER_QUEUE_SIZE = 5;
Jeff Pu63f33c72022-07-28 16:06:23 -040032constexpr int SENSOR_ID = 5;
Kevin Chyn0ec2e0c2021-03-26 18:07:24 -070033constexpr common::SensorStrength SENSOR_STRENGTH = common::SensorStrength::STRONG;
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080034constexpr int MAX_ENROLLMENTS_PER_USER = 5;
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080035constexpr bool SUPPORTS_NAVIGATION_GESTURES = true;
Haining Chen48618552021-03-14 17:30:09 -070036constexpr char HW_COMPONENT_ID[] = "fingerprintSensor";
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080037constexpr char HW_VERSION[] = "vendor/model/revision";
Haining Chen48618552021-03-14 17:30:09 -070038constexpr char FW_VERSION[] = "1.01";
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080039constexpr char SERIAL_NUMBER[] = "00000001";
Haining Chen48618552021-03-14 17:30:09 -070040constexpr char SW_COMPONENT_ID[] = "matchingAlgorithm";
41constexpr char SW_VERSION[] = "vendor/version/revision";
Kevin Chyn7d3fdf52020-09-15 13:01:40 -070042
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080043} // namespace
Kevin Chync306b762020-09-17 12:40:09 -070044
Jeff Pu63f33c72022-07-28 16:06:23 -040045Fingerprint::Fingerprint() : mWorker(MAX_WORKER_QUEUE_SIZE) {
Jeff Pu6ccd9562024-02-21 10:46:35 -050046 std::string sensorTypeProp = Fingerprint::cfg().get<std::string>("type");
Jeff Pu63f33c72022-07-28 16:06:23 -040047 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>();
Jeff Pu0b8ea042024-12-19 21:11:16 +000053 } else if (sensorTypeProp == "udfps-us") {
54 mSensorType = FingerprintSensorType::UNDER_DISPLAY_ULTRASONIC;
55 mEngine = std::make_unique<FakeFingerprintEngineUdfps>();
Jeff Pu63f33c72022-07-28 16:06:23 -040056 } else if (sensorTypeProp == "side") {
57 mSensorType = FingerprintSensorType::POWER_BUTTON;
58 mEngine = std::make_unique<FakeFingerprintEngineSide>();
59 } else {
60 mSensorType = FingerprintSensorType::UNKNOWN;
61 mEngine = std::make_unique<FakeFingerprintEngineRear>();
62 UNIMPLEMENTED(FATAL) << "unrecognized or unimplemented fingerprint behavior: "
63 << sensorTypeProp;
64 }
65 LOG(INFO) << "sensorTypeProp:" << sensorTypeProp;
Jeff Pudef5b042023-05-25 14:28:16 -040066 LOG(INFO) << "ro.product.name=" << ::android::base::GetProperty("ro.product.name", "UNKNOWN");
Jeff Pu63f33c72022-07-28 16:06:23 -040067}
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080068
69ndk::ScopedAStatus Fingerprint::getSensorProps(std::vector<SensorProps>* out) {
Haining Chen48618552021-03-14 17:30:09 -070070 std::vector<common::ComponentInfo> componentInfo = {
71 {HW_COMPONENT_ID, HW_VERSION, FW_VERSION, SERIAL_NUMBER, "" /* softwareVersion */},
72 {SW_COMPONENT_ID, "" /* hardwareVersion */, "" /* firmwareVersion */,
73 "" /* serialNumber */, SW_VERSION}};
Jeff Pu6ccd9562024-02-21 10:46:35 -050074 auto sensorId = Fingerprint::cfg().get<std::int32_t>("sensor_id");
75 auto sensorStrength = Fingerprint::cfg().get<std::int32_t>("sensor_strength");
76 auto maxEnrollments = Fingerprint::cfg().get<std::int32_t>("max_enrollments");
77 auto navigationGuesture = Fingerprint::cfg().get<bool>("navigation_guesture");
78 auto detectInteraction = Fingerprint::cfg().get<bool>("detect_interaction");
79 auto displayTouch = Fingerprint::cfg().get<bool>("display_touch");
80 auto controlIllumination = Fingerprint::cfg().get<bool>("control_illumination");
Jeff Pu343ca942022-09-14 15:56:30 -040081
82 common::CommonProps commonProps = {sensorId, (common::SensorStrength)sensorStrength,
83 maxEnrollments, componentInfo};
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080084
Jeff Pu63f33c72022-07-28 16:06:23 -040085 SensorLocation sensorLocation = mEngine->getSensorLocation();
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080086
Jeff Pu52653182022-10-12 16:27:23 -040087 LOG(INFO) << "sensor type:" << ::android::internal::ToString(mSensorType)
88 << " location:" << sensorLocation.toString();
Joe Bolingerde94aa02021-12-09 17:00:32 -080089
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080090 *out = {{commonProps,
Jeff Pu63f33c72022-07-28 16:06:23 -040091 mSensorType,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -080092 {sensorLocation},
Jeff Pu343ca942022-09-14 15:56:30 -040093 navigationGuesture,
94 detectInteraction,
95 displayTouch,
Austin Delgado88ded642023-02-16 11:34:50 -080096 controlIllumination,
97 std::nullopt}};
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070098 return ndk::ScopedAStatus::ok();
99}
100
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800101ndk::ScopedAStatus Fingerprint::createSession(int32_t sensorId, int32_t userId,
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700102 const std::shared_ptr<ISessionCallback>& cb,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -0800103 std::shared_ptr<ISession>* out) {
Ilya Matyukhinc605e0b2021-02-25 16:04:34 -0800104 CHECK(mSession == nullptr || mSession->isClosed()) << "Open session already exists!";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800105
Ilya Matyukhinc605e0b2021-02-25 16:04:34 -0800106 mSession = SharedRefBase::make<Session>(sensorId, userId, cb, mEngine.get(), &mWorker);
107 *out = mSession;
Jeff Pu63f33c72022-07-28 16:06:23 -0400108
Jeff Pu87e9f2b2023-05-03 17:59:21 +0000109 mSession->linkToDeath(cb->asBinder().get());
110
Jeff Pudef5b042023-05-25 14:28:16 -0400111 LOG(INFO) << __func__ << ": sensorId:" << sensorId << " userId:" << userId;
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700112 return ndk::ScopedAStatus::ok();
113}
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800114
Jeff Pu52653182022-10-12 16:27:23 -0400115binder_status_t Fingerprint::dump(int fd, const char** /*args*/, uint32_t numArgs) {
116 if (fd < 0) {
Jeff Pudef5b042023-05-25 14:28:16 -0400117 LOG(ERROR) << __func__ << "fd invalid: " << fd;
Jeff Pu52653182022-10-12 16:27:23 -0400118 return STATUS_BAD_VALUE;
119 } else {
Jeff Pudef5b042023-05-25 14:28:16 -0400120 LOG(INFO) << __func__ << " fd:" << fd << "numArgs:" << numArgs;
Jeff Pu52653182022-10-12 16:27:23 -0400121 }
122
123 dprintf(fd, "----- FingerprintVirtualHal::dump -----\n");
124 std::vector<SensorProps> sps(1);
125 getSensorProps(&sps);
126 for (auto& sp : sps) {
127 ::android::base::WriteStringToFd(sp.toString(), fd);
128 }
129 ::android::base::WriteStringToFd(mEngine->toString(), fd);
130
Jeff Pueda68e42024-04-10 15:47:59 -0400131 ::android::base::WriteStringToFd(Fingerprint::cfg().toString(), fd);
132
Jeff Pu52653182022-10-12 16:27:23 -0400133 fsync(fd);
134 return STATUS_OK;
135}
136
137binder_status_t Fingerprint::handleShellCommand(int in, int out, int err, const char** args,
138 uint32_t numArgs) {
Jeff Pudef5b042023-05-25 14:28:16 -0400139 LOG(INFO) << __func__ << " in:" << in << " out:" << out << " err:" << err
Jeff Pu52653182022-10-12 16:27:23 -0400140 << " numArgs:" << numArgs;
141
142 if (numArgs == 0) {
Jeff Pudef5b042023-05-25 14:28:16 -0400143 LOG(INFO) << __func__ << ": available commands";
Jeff Pu52653182022-10-12 16:27:23 -0400144 onHelp(out);
145 return STATUS_OK;
146 }
147
148 for (auto&& str : std::vector<std::string_view>(args, args + numArgs)) {
149 std::string option = str.data();
150 if (option.find("clearconfig") != std::string::npos ||
151 option.find("resetconfig") != std::string::npos) {
152 resetConfigToDefault();
153 }
154 if (option.find("help") != std::string::npos) {
155 onHelp(out);
156 }
157 }
158
159 return STATUS_OK;
160}
161
162void Fingerprint::onHelp(int fd) {
163 dprintf(fd, "Virtual HAL commands:\n");
164 dprintf(fd, " help: print this help\n");
165 dprintf(fd, " resetconfig: reset all configuration to default\n");
166 dprintf(fd, "\n");
167 fsync(fd);
168}
169
170void Fingerprint::resetConfigToDefault() {
Jeff Pudef5b042023-05-25 14:28:16 -0400171 LOG(INFO) << __func__ << ": reset virtual HAL configuration to default";
Jeff Pu6ccd9562024-02-21 10:46:35 -0500172 Fingerprint::cfg().init();
173#ifdef FPS_DEBUGGABLE
174 clearConfigSysprop();
175#endif
176}
177
178void Fingerprint::clearConfigSysprop() {
Jeff Pudf81c962024-03-06 10:58:17 -0500179 LOG(INFO) << __func__ << ": clear all sysprop configuration";
Jeff Pu52653182022-10-12 16:27:23 -0400180#define RESET_CONFIG_O(__NAME__) \
181 if (FingerprintHalProperties::__NAME__()) FingerprintHalProperties::__NAME__(std::nullopt)
182#define RESET_CONFIG_V(__NAME__) \
183 if (!FingerprintHalProperties::__NAME__().empty()) \
184 FingerprintHalProperties::__NAME__({std::nullopt})
185
186 RESET_CONFIG_O(type);
187 RESET_CONFIG_V(enrollments);
188 RESET_CONFIG_O(enrollment_hit);
189 RESET_CONFIG_O(authenticator_id);
190 RESET_CONFIG_O(challenge);
191 RESET_CONFIG_O(lockout);
192 RESET_CONFIG_O(operation_authenticate_fails);
193 RESET_CONFIG_O(operation_detect_interaction_error);
194 RESET_CONFIG_O(operation_enroll_error);
195 RESET_CONFIG_V(operation_authenticate_latency);
196 RESET_CONFIG_V(operation_detect_interaction_latency);
197 RESET_CONFIG_V(operation_enroll_latency);
198 RESET_CONFIG_O(operation_authenticate_duration);
199 RESET_CONFIG_O(operation_authenticate_error);
200 RESET_CONFIG_O(sensor_location);
201 RESET_CONFIG_O(operation_authenticate_acquired);
202 RESET_CONFIG_O(operation_detect_interaction_duration);
203 RESET_CONFIG_O(operation_detect_interaction_acquired);
204 RESET_CONFIG_O(sensor_id);
205 RESET_CONFIG_O(sensor_strength);
206 RESET_CONFIG_O(max_enrollments);
207 RESET_CONFIG_O(navigation_guesture);
208 RESET_CONFIG_O(detect_interaction);
209 RESET_CONFIG_O(display_touch);
210 RESET_CONFIG_O(control_illumination);
211 RESET_CONFIG_O(lockout_enable);
212 RESET_CONFIG_O(lockout_timed_threshold);
213 RESET_CONFIG_O(lockout_timed_duration);
214 RESET_CONFIG_O(lockout_permanent_threshold);
215}
216
Jeff Pudf81c962024-03-06 10:58:17 -0500217const char* Fingerprint::type2String(FingerprintSensorType type) {
218 switch (type) {
219 case FingerprintSensorType::REAR:
220 return "rear";
221 case FingerprintSensorType::POWER_BUTTON:
222 return "side";
223 case FingerprintSensorType::UNDER_DISPLAY_OPTICAL:
224 return "udfps";
225 case FingerprintSensorType::UNDER_DISPLAY_ULTRASONIC:
Jeff Pu0b8ea042024-12-19 21:11:16 +0000226 return "udfps-us";
Jeff Pudf81c962024-03-06 10:58:17 -0500227 default:
228 return "unknown";
229 }
230}
231
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700232} // namespace aidl::android::hardware::biometrics::fingerprint