blob: 21d16f5018383ba4992dda982afd56d47646eab1 [file] [log] [blame]
/*
* Copyright (C) 2024 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "Fingerprint.h"
#include "VendorConstants.h"
#include <fingerprint.sysprop.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/strings.h>
#include <fcntl.h>
#include <linux/uinput.h>
using namespace ::android::fingerprint::samsung;
using ::android::base::ParseInt;
using ::android::base::Split;
namespace aidl {
namespace android {
namespace hardware {
namespace biometrics {
namespace fingerprint {
namespace {
constexpr int SENSOR_ID = 0;
constexpr common::SensorStrength SENSOR_STRENGTH = common::SensorStrength::STRONG;
constexpr int MAX_ENROLLMENTS_PER_USER = 4;
constexpr bool SUPPORTS_NAVIGATION_GESTURES = false;
constexpr char HW_COMPONENT_ID[] = "fingerprintSensor";
constexpr char HW_VERSION[] = "vendor/model/revision";
constexpr char FW_VERSION[] = "1.01";
constexpr char SERIAL_NUMBER[] = "00000001";
constexpr char SW_COMPONENT_ID[] = "matchingAlgorithm";
constexpr char SW_VERSION[] = "vendor/version/revision";
}
static Fingerprint* sInstance;
Fingerprint::Fingerprint() {
sInstance = this; // keep track of the most recent instance
if (!mHal.openHal(Fingerprint::notify)) {
LOG(ERROR) << "Can't open HAL module";
}
std::string sensorTypeProp = FingerprintHalProperties::type().value_or("");
if (sensorTypeProp == "" || sensorTypeProp == "default" || sensorTypeProp == "rear")
mSensorType = FingerprintSensorType::REAR;
else if (sensorTypeProp == "udfps")
mSensorType = FingerprintSensorType::UNDER_DISPLAY_ULTRASONIC;
else if (sensorTypeProp == "udfps_optical")
mSensorType = FingerprintSensorType::UNDER_DISPLAY_OPTICAL;
else if (sensorTypeProp == "side")
mSensorType = FingerprintSensorType::POWER_BUTTON;
else if (sensorTypeProp == "home")
mSensorType = FingerprintSensorType::HOME_BUTTON;
else
mSensorType = FingerprintSensorType::UNKNOWN;
mMaxEnrollmentsPerUser =
FingerprintHalProperties::max_enrollments_per_user().value_or(MAX_ENROLLMENTS_PER_USER);
mSupportsGestures =
FingerprintHalProperties::supports_gestures().value_or(SUPPORTS_NAVIGATION_GESTURES);
if (mSupportsGestures) {
mHal.request(FINGERPRINT_REQUEST_NAVIGATION_MODE_START, 1);
uinputFd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (uinputFd < 0) {
LOG(ERROR) << "Unable to open uinput node";
goto skip_uinput_setup;
}
int err = ioctl(uinputFd, UI_SET_EVBIT, EV_KEY) |
ioctl(uinputFd, UI_SET_KEYBIT, KEY_UP) |
ioctl(uinputFd, UI_SET_KEYBIT, KEY_DOWN);
if (err != 0) {
LOG(ERROR) << "Unable to enable key events";
goto skip_uinput_setup;
}
struct uinput_user_dev uidev;
sprintf(uidev.name, "uinput-sec-fp");
uidev.id.bustype = BUS_VIRTUAL;
err = write(uinputFd, &uidev, sizeof(uidev));
if (err < 0) {
LOG(ERROR) << "Write user device to uinput node failed";
goto skip_uinput_setup;
}
err = ioctl(uinputFd, UI_DEV_CREATE);
if (err < 0) {
LOG(ERROR) << "Unable to create uinput device";
goto skip_uinput_setup;
}
LOG(INFO) << "Successfully registered uinput-sec-fp for fingerprint gestures";
}
skip_uinput_setup:
return;
}
ndk::ScopedAStatus Fingerprint::getSensorProps(std::vector<SensorProps>* out) {
std::vector<common::ComponentInfo> componentInfo = {
{HW_COMPONENT_ID, HW_VERSION, FW_VERSION, SERIAL_NUMBER, "" /* softwareVersion */},
{SW_COMPONENT_ID, "" /* hardwareVersion */, "" /* firmwareVersion */,
"" /* serialNumber */, SW_VERSION}};
common::CommonProps commonProps = {SENSOR_ID, SENSOR_STRENGTH,
mMaxEnrollmentsPerUser, componentInfo};
SensorLocation sensorLocation;
std::string loc = FingerprintHalProperties::sensor_location().value_or("");
std::vector<std::string> dim = Split(loc, "|");
if (dim.size() >= 3 && dim.size() <= 4) {
ParseInt(dim[0], &sensorLocation.sensorLocationX);
ParseInt(dim[1], &sensorLocation.sensorLocationY);
ParseInt(dim[2], &sensorLocation.sensorRadius);
if (dim.size() >= 4)
sensorLocation.display = dim[3];
} else if(loc.length() > 0) {
LOG(WARNING) << "Invalid sensor location input (x|y|radius|display): " << loc;
}
LOG(INFO) << "Sensor type: " << ::android::internal::ToString(mSensorType)
<< " location: " << sensorLocation.toString();
*out = {{commonProps,
mSensorType,
{sensorLocation},
mSupportsGestures,
false,
false,
false,
std::nullopt}};
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus Fingerprint::createSession(int32_t /*sensorId*/, int32_t userId,
const std::shared_ptr<ISessionCallback>& cb,
std::shared_ptr<ISession>* out) {
CHECK(mSession == nullptr || mSession->isClosed()) << "Open session already exists!";
mSession = SharedRefBase::make<Session>(mHal, userId, cb, mLockoutTracker);
*out = mSession;
mSession->linkToDeath(cb->asBinder().get());
return ndk::ScopedAStatus::ok();
}
void Fingerprint::notify(const fingerprint_msg_t* msg) {
Fingerprint* thisPtr = sInstance;
if (msg->type == FINGERPRINT_ACQUIRED
&& msg->data.acquired.acquired_info > SEM_FINGERPRINT_EVENT_BASE) {
thisPtr->handleEvent(msg->data.acquired.acquired_info);
return;
}
if (thisPtr->mSession == nullptr || thisPtr->mSession->isClosed()) {
LOG(ERROR) << "Receiving callbacks before a session is opened.";
return;
}
thisPtr->mSession->notify(msg);
}
void Fingerprint::handleEvent(int eventCode) {
switch (eventCode) {
case SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_DOWN:
case SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_UP: {
if (!mSupportsGestures) return;
struct input_event event {};
int keycode = eventCode == SEM_FINGERPRINT_EVENT_GESTURE_SWIPE_UP ?
KEY_UP : KEY_DOWN;
// Report the key
event.type = EV_KEY;
event.code = keycode;
event.value = 1;
if (write(uinputFd, &event, sizeof(event)) < 0) {
LOG(ERROR) << "Write EV_KEY to uinput node failed";
return;
}
// Force a flush with an EV_SYN
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
if (write(uinputFd, &event, sizeof(event)) < 0) {
LOG(ERROR) << "Write EV_SYN to uinput node failed";
return;
}
// Report the key
event.type = EV_KEY;
event.code = keycode;
event.value = 0;
if (write(uinputFd, &event, sizeof(event)) < 0) {
LOG(ERROR) << "Write EV_KEY to uinput node failed";
return;
}
// Force a flush with an EV_SYN
event.type = EV_SYN;
event.code = SYN_REPORT;
event.value = 0;
if (write(uinputFd, &event, sizeof(event)) < 0) {
LOG(ERROR) << "Write EV_SYN to uinput node failed";
return;
}
} break;
case SEM_FINGERPRINT_EVENT_CAPTURE_READY: {
if (mSession != nullptr && !mSession->isClosed()) {
mSession->onCaptureReady();
}
} break;
}
}
} // namespace fingerprint
} // namespace biometrics
} // namespace hardware
} // namespace android
} // namespace aidl