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 = Fingerprint::cfg().get<std::string>("type");
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 == "udfps-us") {
54 mSensorType = FingerprintSensorType::UNDER_DISPLAY_ULTRASONIC;
55 mEngine = std::make_unique<FakeFingerprintEngineUdfps>();
56 } 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;
66 LOG(INFO) << "ro.product.name=" << ::android::base::GetProperty("ro.product.name", "UNKNOWN");
67 }
68
getSensorProps(std::vector<SensorProps> * out)69 ndk::ScopedAStatus Fingerprint::getSensorProps(std::vector<SensorProps>* out) {
70 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}};
74 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 navigationGesture = Fingerprint::cfg().get<bool>("navigation_gesture");
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");
81
82 common::CommonProps commonProps = {sensorId, (common::SensorStrength)sensorStrength,
83 maxEnrollments, componentInfo};
84
85 std::vector<SensorLocation> sensorLocation;
86 mEngine->getSensorLocation(sensorLocation);
87 LOG(INFO) << "sensor type:" << ::android::internal::ToString(mSensorType);
88 for (auto location : sensorLocation) {
89 LOG(INFO) << "sensor location: " << location.toString();
90 }
91
92 *out = {{commonProps, mSensorType, sensorLocation, navigationGesture, detectInteraction,
93 displayTouch, controlIllumination, std::nullopt}};
94 return ndk::ScopedAStatus::ok();
95 }
96
createSession(int32_t sensorId,int32_t userId,const std::shared_ptr<ISessionCallback> & cb,std::shared_ptr<ISession> * out)97 ndk::ScopedAStatus Fingerprint::createSession(int32_t sensorId, int32_t userId,
98 const std::shared_ptr<ISessionCallback>& cb,
99 std::shared_ptr<ISession>* out) {
100 CHECK(mSession == nullptr || mSession->isClosed()) << "Open session already exists!";
101
102 mSession = SharedRefBase::make<Session>(sensorId, userId, cb, mEngine.get(), &mWorker);
103 *out = mSession;
104
105 mSession->linkToDeath(cb->asBinder().get());
106
107 LOG(INFO) << __func__ << ": sensorId:" << sensorId << " userId:" << userId;
108 return ndk::ScopedAStatus::ok();
109 }
110
dump(int fd,const char **,uint32_t numArgs)111 binder_status_t Fingerprint::dump(int fd, const char** /*args*/, uint32_t numArgs) {
112 if (fd < 0) {
113 LOG(ERROR) << __func__ << "fd invalid: " << fd;
114 return STATUS_BAD_VALUE;
115 } else {
116 LOG(INFO) << __func__ << " fd:" << fd << "numArgs:" << numArgs;
117 }
118
119 dprintf(fd, "----- FingerprintVirtualHal::dump -----\n");
120 std::vector<SensorProps> sps(1);
121 getSensorProps(&sps);
122 for (auto& sp : sps) {
123 ::android::base::WriteStringToFd(sp.toString(), fd);
124 }
125 ::android::base::WriteStringToFd(mEngine->toString(), fd);
126
127 ::android::base::WriteStringToFd(Fingerprint::cfg().toString(), fd);
128
129 fsync(fd);
130 return STATUS_OK;
131 }
132
handleShellCommand(int in,int out,int err,const char ** args,uint32_t numArgs)133 binder_status_t Fingerprint::handleShellCommand(int in, int out, int err, const char** args,
134 uint32_t numArgs) {
135 LOG(INFO) << __func__ << " in:" << in << " out:" << out << " err:" << err
136 << " numArgs:" << numArgs;
137
138 if (numArgs == 0) {
139 LOG(INFO) << __func__ << ": available commands";
140 onHelp(out);
141 return STATUS_OK;
142 }
143
144 for (auto&& str : std::vector<std::string_view>(args, args + numArgs)) {
145 std::string option = str.data();
146 if (option.find("clearconfig") != std::string::npos ||
147 option.find("resetconfig") != std::string::npos) {
148 resetConfigToDefault();
149 }
150 if (option.find("help") != std::string::npos) {
151 onHelp(out);
152 }
153 }
154
155 return STATUS_OK;
156 }
157
onHelp(int fd)158 void Fingerprint::onHelp(int fd) {
159 dprintf(fd, "Virtual HAL commands:\n");
160 dprintf(fd, " help: print this help\n");
161 dprintf(fd, " resetconfig: reset all configuration to default\n");
162 dprintf(fd, "\n");
163 fsync(fd);
164 }
165
resetConfigToDefault()166 void Fingerprint::resetConfigToDefault() {
167 LOG(INFO) << __func__ << ": reset virtual HAL configuration to default";
168 Fingerprint::cfg().init();
169 #ifdef FPS_DEBUGGABLE
170 clearConfigSysprop();
171 #endif
172 }
173
clearConfigSysprop()174 void Fingerprint::clearConfigSysprop() {
175 LOG(INFO) << __func__ << ": clear all sysprop configuration";
176 #define RESET_CONFIG_O(__NAME__) \
177 if (FingerprintHalProperties::__NAME__()) FingerprintHalProperties::__NAME__(std::nullopt)
178 #define RESET_CONFIG_V(__NAME__) \
179 if (!FingerprintHalProperties::__NAME__().empty()) \
180 FingerprintHalProperties::__NAME__({std::nullopt})
181
182 RESET_CONFIG_O(type);
183 RESET_CONFIG_V(enrollments);
184 RESET_CONFIG_O(enrollment_hit);
185 RESET_CONFIG_O(authenticator_id);
186 RESET_CONFIG_O(challenge);
187 RESET_CONFIG_O(lockout);
188 RESET_CONFIG_O(operation_authenticate_fails);
189 RESET_CONFIG_O(operation_detect_interaction_error);
190 RESET_CONFIG_O(operation_enroll_error);
191 RESET_CONFIG_V(operation_authenticate_latency);
192 RESET_CONFIG_V(operation_detect_interaction_latency);
193 RESET_CONFIG_V(operation_enroll_latency);
194 RESET_CONFIG_O(operation_authenticate_duration);
195 RESET_CONFIG_O(operation_authenticate_error);
196 RESET_CONFIG_O(sensor_location);
197 RESET_CONFIG_O(operation_authenticate_acquired);
198 RESET_CONFIG_O(operation_detect_interaction_duration);
199 RESET_CONFIG_O(operation_detect_interaction_acquired);
200 RESET_CONFIG_O(sensor_id);
201 RESET_CONFIG_O(sensor_strength);
202 RESET_CONFIG_O(max_enrollments);
203 RESET_CONFIG_O(navigation_gesture);
204 RESET_CONFIG_O(detect_interaction);
205 RESET_CONFIG_O(display_touch);
206 RESET_CONFIG_O(control_illumination);
207 RESET_CONFIG_O(lockout_enable);
208 RESET_CONFIG_O(lockout_timed_threshold);
209 RESET_CONFIG_O(lockout_timed_duration);
210 RESET_CONFIG_O(lockout_permanent_threshold);
211 }
212
type2String(FingerprintSensorType type)213 const char* Fingerprint::type2String(FingerprintSensorType type) {
214 switch (type) {
215 case FingerprintSensorType::REAR:
216 return "rear";
217 case FingerprintSensorType::POWER_BUTTON:
218 return "side";
219 case FingerprintSensorType::UNDER_DISPLAY_OPTICAL:
220 return "udfps";
221 case FingerprintSensorType::UNDER_DISPLAY_ULTRASONIC:
222 return "udfps-us";
223 default:
224 return "unknown";
225 }
226 }
227
228 } // namespace aidl::android::hardware::biometrics::fingerprint
229