diff --git a/fingerprint/BiometricsFingerprint.cpp b/fingerprint/BiometricsFingerprint.cpp index 18642f5..e9d25a8 100644 --- a/fingerprint/BiometricsFingerprint.cpp +++ b/fingerprint/BiometricsFingerprint.cpp @@ -31,13 +31,12 @@ namespace implementation { // Supported fingerprint HAL version static const uint16_t kVersion = HARDWARE_MODULE_API_VERSION(2, 1); -using RequestStatus = - android::hardware::biometrics::fingerprint::V2_1::RequestStatus; +using RequestStatus = android::hardware::biometrics::fingerprint::V2_1::RequestStatus; -BiometricsFingerprint *BiometricsFingerprint::sInstance = nullptr; +BiometricsFingerprint* BiometricsFingerprint::sInstance = nullptr; BiometricsFingerprint::BiometricsFingerprint() : mClientCallback(nullptr), mDevice(nullptr) { - sInstance = this; // keep track of the most recent instance + sInstance = this; // keep track of the most recent instance mDevice = openHal(); if (!mDevice) { ALOGE("Can't open HAL module"); @@ -51,8 +50,7 @@ BiometricsFingerprint::~BiometricsFingerprint() { return; } int err; - if (0 != (err = mDevice->common.close( - reinterpret_cast(mDevice)))) { + if (0 != (err = mDevice->common.close(reinterpret_cast(mDevice)))) { ALOGE("Can't close fingerprint module, error: %d", err); return; } @@ -60,19 +58,31 @@ BiometricsFingerprint::~BiometricsFingerprint() { } Return BiometricsFingerprint::ErrorFilter(int32_t error) { - switch(error) { - case 0: return RequestStatus::SYS_OK; - case -2: return RequestStatus::SYS_ENOENT; - case -4: return RequestStatus::SYS_EINTR; - case -5: return RequestStatus::SYS_EIO; - case -11: return RequestStatus::SYS_EAGAIN; - case -12: return RequestStatus::SYS_ENOMEM; - case -13: return RequestStatus::SYS_EACCES; - case -14: return RequestStatus::SYS_EFAULT; - case -16: return RequestStatus::SYS_EBUSY; - case -22: return RequestStatus::SYS_EINVAL; - case -28: return RequestStatus::SYS_ENOSPC; - case -110: return RequestStatus::SYS_ETIMEDOUT; + switch (error) { + case 0: + return RequestStatus::SYS_OK; + case -2: + return RequestStatus::SYS_ENOENT; + case -4: + return RequestStatus::SYS_EINTR; + case -5: + return RequestStatus::SYS_EIO; + case -11: + return RequestStatus::SYS_EAGAIN; + case -12: + return RequestStatus::SYS_ENOMEM; + case -13: + return RequestStatus::SYS_EACCES; + case -14: + return RequestStatus::SYS_EFAULT; + case -16: + return RequestStatus::SYS_EBUSY; + case -22: + return RequestStatus::SYS_EINVAL; + case -28: + return RequestStatus::SYS_ENOSPC; + case -110: + return RequestStatus::SYS_ETIMEDOUT; default: ALOGE("An unknown error returned from fingerprint vendor library: %d", error); return RequestStatus::SYS_UNKNOWN; @@ -81,10 +91,9 @@ Return BiometricsFingerprint::ErrorFilter(int32_t error) { // Translate from errors returned by traditional HAL (see fingerprint.h) to // HIDL-compliant FingerprintError. -FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, - int32_t* vendorCode) { +FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, int32_t* vendorCode) { *vendorCode = 0; - switch(error) { + switch (error) { case FINGERPRINT_ERROR_HW_UNAVAILABLE: return FingerprintError::ERROR_HW_UNAVAILABLE; case FINGERPRINT_ERROR_UNABLE_TO_PROCESS: @@ -112,10 +121,10 @@ FingerprintError BiometricsFingerprint::VendorErrorFilter(int32_t error, // Translate acquired messages returned by traditional HAL (see fingerprint.h) // to HIDL-compliant FingerprintAcquiredInfo. -FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter( - int32_t info, int32_t* vendorCode) { +FingerprintAcquiredInfo BiometricsFingerprint::VendorAcquiredFilter(int32_t info, + int32_t* vendorCode) { *vendorCode = 0; - switch(info) { + switch (info) { case FINGERPRINT_ACQUIRED_GOOD: return FingerprintAcquiredInfo::ACQUIRED_GOOD; case FINGERPRINT_ACQUIRED_PARTIAL: @@ -150,14 +159,13 @@ Return BiometricsFingerprint::setNotify( return reinterpret_cast(mDevice); } -Return BiometricsFingerprint::preEnroll() { +Return BiometricsFingerprint::preEnroll() { return mDevice->pre_enroll(mDevice); } Return BiometricsFingerprint::enroll(const hidl_array& hat, - uint32_t gid, uint32_t timeoutSec) { - const hw_auth_token_t* authToken = - reinterpret_cast(hat.data()); + uint32_t gid, uint32_t timeoutSec) { + const hw_auth_token_t* authToken = reinterpret_cast(hat.data()); return ErrorFilter(mDevice->enroll(mDevice, authToken, gid, timeoutSec)); } @@ -173,7 +181,7 @@ Return BiometricsFingerprint::cancel() { return ErrorFilter(mDevice->cancel(mDevice)); } -Return BiometricsFingerprint::enumerate() { +Return BiometricsFingerprint::enumerate() { return ErrorFilter(mDevice->enumerate(mDevice)); } @@ -182,7 +190,7 @@ Return BiometricsFingerprint::remove(uint32_t gid, uint32_t fid) } Return BiometricsFingerprint::setActiveGroup(uint32_t gid, - const hidl_string& storePath) { + const hidl_string& storePath) { if (storePath.size() >= PATH_MAX || storePath.size() <= 0) { ALOGE("Bad path length: %zd", storePath.size()); return RequestStatus::SYS_EINVAL; @@ -191,25 +199,23 @@ Return BiometricsFingerprint::setActiveGroup(uint32_t gid, return RequestStatus::SYS_EINVAL; } - return ErrorFilter(mDevice->set_active_group(mDevice, gid, - storePath.c_str())); + return ErrorFilter(mDevice->set_active_group(mDevice, gid, storePath.c_str())); } -Return BiometricsFingerprint::authenticate(uint64_t operationId, - uint32_t gid) { +Return BiometricsFingerprint::authenticate(uint64_t operationId, uint32_t gid) { return ErrorFilter(mDevice->authenticate(mDevice, operationId, gid)); } IBiometricsFingerprint* BiometricsFingerprint::getInstance() { if (!sInstance) { - sInstance = new BiometricsFingerprint(); + sInstance = new BiometricsFingerprint(); } return sInstance; } -fingerprint_device_t* getDeviceForVendor(const char *class_name) { +fingerprint_device_t* getDeviceForVendor(const char* class_name) { int err; - const hw_module_t *hw_mdl = nullptr; + const hw_module_t* hw_mdl = nullptr; ALOGD("Opening fingerprint hal library..."); if (0 != (err = hw_get_module_by_class(FINGERPRINT_HARDWARE_MODULE_ID, class_name, &hw_mdl))) { ALOGE("Can't open fingerprint HW Module, class: %s, error: %d", class_name, err); @@ -221,14 +227,13 @@ fingerprint_device_t* getDeviceForVendor(const char *class_name) { return nullptr; } - fingerprint_module_t const *module = - reinterpret_cast(hw_mdl); + fingerprint_module_t const* module = reinterpret_cast(hw_mdl); if (module->common.methods->open == nullptr) { ALOGE("No valid open method, class: %s", class_name); return nullptr; } - hw_device_t *device = nullptr; + hw_device_t* device = nullptr; if (0 != (err = module->common.methods->open(hw_mdl, nullptr, &device))) { ALOGE("Can't open fingerprint methods, class: %s, error: %d", class_name, err); @@ -241,16 +246,15 @@ fingerprint_device_t* getDeviceForVendor(const char *class_name) { return nullptr; } - fingerprint_device_t* fp_device = - reinterpret_cast(device); + fingerprint_device_t* fp_device = reinterpret_cast(device); ALOGI("Loaded fingerprint module, class: %s", class_name); return fp_device; } fingerprint_device_t* getFingerprintDevice() { - fingerprint_device_t *fp_device; - std::string vendor_modules[] = { "fpc", "fpc_fod", "goodix", "goodix_fod" }; + fingerprint_device_t* fp_device; + std::string vendor_modules[] = {"fpc", "fpc_fod", "goodix", "goodix_fod"}; for (const auto& vendor : vendor_modules) { if ((fp_device = getDeviceForVendor(vendor.c_str())) == nullptr) { @@ -266,14 +270,13 @@ fingerprint_device_t* getFingerprintDevice() { fingerprint_device_t* BiometricsFingerprint::openHal() { int err; - fingerprint_device_t *fp_device; + fingerprint_device_t* fp_device; fp_device = getFingerprintDevice(); if (fp_device == nullptr) { return nullptr; } - if (0 != (err = - fp_device->set_notify(fp_device, BiometricsFingerprint::notify))) { + if (0 != (err = fp_device->set_notify(fp_device, BiometricsFingerprint::notify))) { ALOGE("Can't register fingerprint module callback, error: %d", err); return nullptr; } @@ -281,9 +284,9 @@ fingerprint_device_t* BiometricsFingerprint::openHal() { return fp_device; } -void BiometricsFingerprint::notify(const fingerprint_msg_t *msg) { - BiometricsFingerprint* thisPtr = static_cast( - BiometricsFingerprint::getInstance()); +void BiometricsFingerprint::notify(const fingerprint_msg_t* msg) { + BiometricsFingerprint* thisPtr = + static_cast(BiometricsFingerprint::getInstance()); std::lock_guard lock(thisPtr->mClientCallbackMutex); if (thisPtr == nullptr || thisPtr->mClientCallback == nullptr) { ALOGE("Receiving callbacks before the client callback is registered."); @@ -292,89 +295,83 @@ void BiometricsFingerprint::notify(const fingerprint_msg_t *msg) { const uint64_t devId = reinterpret_cast(thisPtr->mDevice); switch (msg->type) { case FINGERPRINT_ERROR: { - int32_t vendorCode = 0; - FingerprintError result = VendorErrorFilter(msg->data.error, &vendorCode); - ALOGD("onError(%d)", result); - if (!thisPtr->mClientCallback->onError(devId, result, vendorCode).isOk()) { - ALOGE("failed to invoke fingerprint onError callback"); - } + int32_t vendorCode = 0; + FingerprintError result = VendorErrorFilter(msg->data.error, &vendorCode); + ALOGD("onError(%d)", result); + if (!thisPtr->mClientCallback->onError(devId, result, vendorCode).isOk()) { + ALOGE("failed to invoke fingerprint onError callback"); } - break; + } break; case FINGERPRINT_ACQUIRED: { - int32_t vendorCode = 0; - FingerprintAcquiredInfo result = + int32_t vendorCode = 0; + FingerprintAcquiredInfo result = VendorAcquiredFilter(msg->data.acquired.acquired_info, &vendorCode); - ALOGD("onAcquired(%d)", result); - if (!thisPtr->mClientCallback->onAcquired(devId, result, vendorCode).isOk()) { - ALOGE("failed to invoke fingerprint onAcquired callback"); - } + ALOGD("onAcquired(%d)", result); + if (!thisPtr->mClientCallback->onAcquired(devId, result, vendorCode).isOk()) { + ALOGE("failed to invoke fingerprint onAcquired callback"); } - break; + } break; case FINGERPRINT_TEMPLATE_ENROLLING: - ALOGD("onEnrollResult(fid=%d, gid=%d, rem=%d)", - msg->data.enroll.finger.fid, - msg->data.enroll.finger.gid, - msg->data.enroll.samples_remaining); - if (!thisPtr->mClientCallback->onEnrollResult(devId, - msg->data.enroll.finger.fid, - msg->data.enroll.finger.gid, - msg->data.enroll.samples_remaining).isOk()) { + ALOGD("onEnrollResult(fid=%d, gid=%d, rem=%d)", msg->data.enroll.finger.fid, + msg->data.enroll.finger.gid, msg->data.enroll.samples_remaining); + if (!thisPtr->mClientCallback + ->onEnrollResult(devId, msg->data.enroll.finger.fid, + msg->data.enroll.finger.gid, + msg->data.enroll.samples_remaining) + .isOk()) { ALOGE("failed to invoke fingerprint onEnrollResult callback"); } break; case FINGERPRINT_TEMPLATE_REMOVED: - ALOGD("onRemove(fid=%d, gid=%d, rem=%d)", - msg->data.removed.finger.fid, - msg->data.removed.finger.gid, - msg->data.removed.remaining_templates); - if (!thisPtr->mClientCallback->onRemoved(devId, - msg->data.removed.finger.fid, - msg->data.removed.finger.gid, - msg->data.removed.remaining_templates).isOk()) { + ALOGD("onRemove(fid=%d, gid=%d, rem=%d)", msg->data.removed.finger.fid, + msg->data.removed.finger.gid, msg->data.removed.remaining_templates); + if (!thisPtr->mClientCallback + ->onRemoved(devId, msg->data.removed.finger.fid, + msg->data.removed.finger.gid, + msg->data.removed.remaining_templates) + .isOk()) { ALOGE("failed to invoke fingerprint onRemoved callback"); } break; case FINGERPRINT_AUTHENTICATED: if (msg->data.authenticated.finger.fid != 0) { - ALOGD("onAuthenticated(fid=%d, gid=%d)", - msg->data.authenticated.finger.fid, - msg->data.authenticated.finger.gid); - const uint8_t* hat = - reinterpret_cast(&msg->data.authenticated.hat); + ALOGD("onAuthenticated(fid=%d, gid=%d)", msg->data.authenticated.finger.fid, + msg->data.authenticated.finger.gid); + const uint8_t* hat = reinterpret_cast(&msg->data.authenticated.hat); const hidl_vec token( - std::vector(hat, hat + sizeof(msg->data.authenticated.hat))); - if (!thisPtr->mClientCallback->onAuthenticated(devId, - msg->data.authenticated.finger.fid, - msg->data.authenticated.finger.gid, - token).isOk()) { + std::vector(hat, hat + sizeof(msg->data.authenticated.hat))); + if (!thisPtr->mClientCallback + ->onAuthenticated(devId, msg->data.authenticated.finger.fid, + msg->data.authenticated.finger.gid, token) + .isOk()) { ALOGE("failed to invoke fingerprint onAuthenticated callback"); } } else { // Not a recognized fingerprint - if (!thisPtr->mClientCallback->onAuthenticated(devId, - msg->data.authenticated.finger.fid, - msg->data.authenticated.finger.gid, - hidl_vec()).isOk()) { + if (!thisPtr->mClientCallback + ->onAuthenticated(devId, msg->data.authenticated.finger.fid, + msg->data.authenticated.finger.gid, + hidl_vec()) + .isOk()) { ALOGE("failed to invoke fingerprint onAuthenticated callback"); } } break; case FINGERPRINT_TEMPLATE_ENUMERATING: - ALOGD("onEnumerate(fid=%d, gid=%d, rem=%d)", - msg->data.enumerated.finger.fid, - msg->data.enumerated.finger.gid, - msg->data.enumerated.remaining_templates); - if (!thisPtr->mClientCallback->onEnumerate(devId, - msg->data.enumerated.finger.fid, - msg->data.enumerated.finger.gid, - msg->data.enumerated.remaining_templates).isOk()) { + ALOGD("onEnumerate(fid=%d, gid=%d, rem=%d)", msg->data.enumerated.finger.fid, + msg->data.enumerated.finger.gid, msg->data.enumerated.remaining_templates); + if (!thisPtr->mClientCallback + ->onEnumerate(devId, msg->data.enumerated.finger.fid, + msg->data.enumerated.finger.gid, + msg->data.enumerated.remaining_templates) + .isOk()) { ALOGE("failed to invoke fingerprint onEnumerate callback"); } break; } } -} // namespace implementation +} // namespace implementation } // namespace V2_1 } // namespace fingerprint } // namespace biometrics diff --git a/fingerprint/BiometricsFingerprint.h b/fingerprint/BiometricsFingerprint.h index 35207e3..73c48f3 100644 --- a/fingerprint/BiometricsFingerprint.h +++ b/fingerprint/BiometricsFingerprint.h @@ -28,27 +28,30 @@ namespace fingerprint { namespace V2_1 { namespace implementation { +using ::android::sp; +using ::android::hardware::hidl_string; +using ::android::hardware::hidl_vec; +using ::android::hardware::Return; +using ::android::hardware::Void; using ::android::hardware::biometrics::fingerprint::V2_1::IBiometricsFingerprint; using ::android::hardware::biometrics::fingerprint::V2_1::IBiometricsFingerprintClientCallback; using ::android::hardware::biometrics::fingerprint::V2_1::RequestStatus; -using ::android::hardware::Return; -using ::android::hardware::Void; -using ::android::hardware::hidl_vec; -using ::android::hardware::hidl_string; -using ::android::sp; struct BiometricsFingerprint : public IBiometricsFingerprint { -public: + public: BiometricsFingerprint(); ~BiometricsFingerprint(); // Method to wrap legacy HAL with BiometricsFingerprint class static IBiometricsFingerprint* getInstance(); - // Methods from ::android::hardware::biometrics::fingerprint::V2_1::IBiometricsFingerprint follow. - Return setNotify(const sp& clientCallback) override; + // Methods from ::android::hardware::biometrics::fingerprint::V2_1::IBiometricsFingerprint + // follow. + Return setNotify( + const sp& clientCallback) override; Return preEnroll() override; - Return enroll(const hidl_array& hat, uint32_t gid, uint32_t timeoutSec) override; + Return enroll(const hidl_array& hat, uint32_t gid, + uint32_t timeoutSec) override; Return postEnroll() override; Return getAuthenticatorId() override; Return cancel() override; @@ -57,9 +60,10 @@ public: Return setActiveGroup(uint32_t gid, const hidl_string& storePath) override; Return authenticate(uint64_t operationId, uint32_t gid) override; -private: + private: static fingerprint_device_t* openHal(); - static void notify(const fingerprint_msg_t *msg); /* Static callback for legacy HAL implementation */ + static void notify( + const fingerprint_msg_t* msg); /* Static callback for legacy HAL implementation */ static Return ErrorFilter(int32_t error); static FingerprintError VendorErrorFilter(int32_t error, int32_t* vendorCode); static FingerprintAcquiredInfo VendorAcquiredFilter(int32_t error, int32_t* vendorCode); @@ -67,7 +71,7 @@ private: std::mutex mClientCallbackMutex; sp mClientCallback; - fingerprint_device_t *mDevice; + fingerprint_device_t* mDevice; }; } // namespace implementation diff --git a/fingerprint/service.cpp b/fingerprint/service.cpp index 319b6ab..c135fb6 100644 --- a/fingerprint/service.cpp +++ b/fingerprint/service.cpp @@ -20,11 +20,11 @@ #include "BiometricsFingerprint.h" -using android::hardware::biometrics::fingerprint::V2_1::IBiometricsFingerprint; -using android::hardware::biometrics::fingerprint::V2_1::implementation::BiometricsFingerprint; +using android::sp; using android::hardware::configureRpcThreadpool; using android::hardware::joinRpcThreadpool; -using android::sp; +using android::hardware::biometrics::fingerprint::V2_1::IBiometricsFingerprint; +using android::hardware::biometrics::fingerprint::V2_1::implementation::BiometricsFingerprint; int main() { android::sp bio = BiometricsFingerprint::getInstance(); @@ -41,5 +41,5 @@ int main() { joinRpcThreadpool(); - return 0; // should never get here + return 0; // should never get here }