xref: /aosp_15_r20/hardware/interfaces/biometrics/face/aidl/default/Face.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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 #undef LOG_TAG
18 #define LOG_TAG "FaceVirtualHal"
19 
20 #include "Face.h"
21 #include "Session.h"
22 
23 #include "FakeFaceEngine.h"
24 
25 #include <android-base/properties.h>
26 #include <face.sysprop.h>
27 
28 #include <android-base/file.h>
29 #include <android-base/logging.h>
30 #include <android-base/stringprintf.h>
31 
32 using namespace ::android::face::virt;
33 
34 namespace aidl::android::hardware::biometrics::face {
35 
36 const int kSensorId = 4;
37 const int kMaxEnrollmentsPerUser = 5;
38 const bool kHalControlsPreview = true;
39 const std::string kHwComponentId = "faceSensor";
40 const std::string kHardwareVersion = "vendor/model/revision";
41 const std::string kFirmwareVersion = "1.01";
42 const std::string kSerialNumber = "00000001";
43 const std::string kSwComponentId = "matchingAlgorithm";
44 const std::string kSoftwareVersion = "vendor/version/revision";
45 
getSensorProps(std::vector<SensorProps> * return_val)46 ndk::ScopedAStatus Face::getSensorProps(std::vector<SensorProps>* return_val) {
47     common::ComponentInfo hw_component_info;
48     hw_component_info.componentId = kHwComponentId;
49     hw_component_info.hardwareVersion = kHardwareVersion;
50     hw_component_info.firmwareVersion = kFirmwareVersion;
51     hw_component_info.serialNumber = kSerialNumber;
52     hw_component_info.softwareVersion = "";
53 
54     common::ComponentInfo sw_component_info;
55     sw_component_info.componentId = kSwComponentId;
56     sw_component_info.hardwareVersion = "";
57     sw_component_info.firmwareVersion = "";
58     sw_component_info.serialNumber = "";
59     sw_component_info.softwareVersion = kSoftwareVersion;
60 
61     common::CommonProps commonProps;
62     commonProps.sensorId = kSensorId;
63     commonProps.sensorStrength = FakeFaceEngine::GetSensorStrength();
64     commonProps.maxEnrollmentsPerUser = kMaxEnrollmentsPerUser;
65     commonProps.componentInfo = {std::move(hw_component_info), std::move(sw_component_info)};
66 
67     SensorProps props;
68     props.commonProps = std::move(commonProps);
69     props.sensorType = FakeFaceEngine::GetSensorType();
70     props.halControlsPreview = kHalControlsPreview;
71     props.enrollPreviewWidth = 1080;
72     props.enrollPreviewHeight = 1920;
73     props.enrollTranslationX = 100.f;
74     props.enrollTranslationY = 50.f;
75     props.enrollPreviewScale = 1.f;
76 
77     *return_val = {std::move(props)};
78     return ndk::ScopedAStatus::ok();
79 }
80 
createSession(int32_t sensorId,int32_t userId,const std::shared_ptr<ISessionCallback> & cb,std::shared_ptr<ISession> * return_val)81 ndk::ScopedAStatus Face::createSession(int32_t sensorId, int32_t userId,
82                                        const std::shared_ptr<ISessionCallback>& cb,
83                                        std::shared_ptr<ISession>* return_val) {
84     mSession = SharedRefBase::make<Session>(std::make_unique<FakeFaceEngine>(), cb);
85     *return_val = mSession;
86 
87     mSession->linkToDeath(cb->asBinder().get());
88 
89     LOG(INFO) << __func__ << ": sensorId:" << sensorId << " userId:" << userId;
90     return ndk::ScopedAStatus::ok();
91 }
92 
dump(int fd,const char **,uint32_t numArgs)93 binder_status_t Face::dump(int fd, const char** /*args*/, uint32_t numArgs) {
94     if (fd < 0) {
95         LOG(ERROR) << __func__ << "fd invalid: " << fd;
96         return STATUS_BAD_VALUE;
97     } else {
98         LOG(INFO) << __func__ << " fd:" << fd << "numArgs:" << numArgs;
99     }
100 
101     dprintf(fd, "----- FaceVirtualHal::dump -----\n");
102     std::vector<SensorProps> sps(1);
103     getSensorProps(&sps);
104     for (auto& sp : sps) {
105         ::android::base::WriteStringToFd(sp.toString(), fd);
106     }
107     if (mSession != nullptr) {
108         ::android::base::WriteStringToFd(mSession->toString(), fd);
109     } else {
110         dprintf(fd, "\nWARNING: no ISession found\n");
111     }
112 
113     fsync(fd);
114     return STATUS_OK;
115 }
116 
handleShellCommand(int in,int out,int err,const char ** args,uint32_t numArgs)117 binder_status_t Face::handleShellCommand(int in, int out, int err, const char** args,
118                                          uint32_t numArgs) {
119     LOG(INFO) << __func__ << " in:" << in << " out:" << out << " err:" << err
120               << " numArgs:" << numArgs;
121 
122     if (numArgs == 0) {
123         LOG(INFO) << __func__ << ": available commands";
124         onHelp(out);
125         return STATUS_OK;
126     }
127 
128     for (auto&& str : std::vector<std::string_view>(args, args + numArgs)) {
129         std::string option = str.data();
130         if (option.find("clearconfig") != std::string::npos ||
131             option.find("resetconfig") != std::string::npos) {
132             resetConfigToDefault();
133         }
134         if (option.find("help") != std::string::npos) {
135             onHelp(out);
136         }
137     }
138 
139     return STATUS_OK;
140 }
141 
type2String(FaceSensorType type)142 const char* Face::type2String(FaceSensorType type) {
143     switch (type) {
144         case FaceSensorType::RGB:
145             return "rgb";
146         case FaceSensorType::IR:
147             return "ir";
148         default:
149             return "unknown";
150     }
151 }
152 
strength2String(common::SensorStrength strength)153 const char* Face::strength2String(common::SensorStrength strength) {
154     switch (strength) {
155         case common::SensorStrength::STRONG:
156             return "STRONG";
157         case common::SensorStrength::WEAK:
158             return "WEAK";
159         case common::SensorStrength::CONVENIENCE:
160             return "CONVENIENCE";
161         default:
162             return "unknown";
163     }
164 }
165 
onHelp(int fd)166 void Face::onHelp(int fd) {
167     dprintf(fd, "Virtual Face HAL commands:\n");
168     dprintf(fd, "         help: print this help\n");
169     dprintf(fd, "  resetconfig: reset all configuration to default\n");
170     dprintf(fd, "\n");
171     fsync(fd);
172 }
173 
resetConfigToDefault()174 void Face::resetConfigToDefault() {
175     LOG(INFO) << __func__ << ": reset virtual Face HAL configuration to default";
176 #define RESET_CONFIG_O(__NAME__) \
177     if (FaceHalProperties::__NAME__()) FaceHalProperties::__NAME__(std::nullopt)
178 #define RESET_CONFIG_V(__NAME__) \
179     if (!FaceHalProperties::__NAME__().empty()) FaceHalProperties::__NAME__({std::nullopt})
180 
181     RESET_CONFIG_O(type);
182     RESET_CONFIG_O(strength);
183     RESET_CONFIG_V(enrollments);
184     RESET_CONFIG_O(enrollment_hit);
185     RESET_CONFIG_V(features);
186     RESET_CONFIG_O(next_enrollment);
187     RESET_CONFIG_O(authenticator_id);
188     RESET_CONFIG_O(challenge);
189     RESET_CONFIG_O(lockout);
190     RESET_CONFIG_O(operation_authenticate_fails);
191     RESET_CONFIG_O(operation_detect_interaction_fails);
192     RESET_CONFIG_V(operation_authenticate_latency);
193     RESET_CONFIG_V(operation_detect_interaction_latency);
194     RESET_CONFIG_V(operation_enroll_latency);
195     RESET_CONFIG_O(operation_authenticate_duration);
196     RESET_CONFIG_O(operation_authenticate_error);
197     RESET_CONFIG_O(operation_authenticate_acquired);
198     RESET_CONFIG_O(lockout_enable);
199     RESET_CONFIG_O(lockout_timed_enable);
200     RESET_CONFIG_O(lockout_timed_threshold);
201     RESET_CONFIG_O(lockout_timed_duration);
202     RESET_CONFIG_O(lockout_permanent_threshold);
203 }
204 
205 }  // namespace aidl::android::hardware::biometrics::face
206