指纹识别分析之framework初始化流程

来源:互联网 发布:fade to black知乎 编辑:程序博客网 时间:2024/06/07 15:32

1、概述

  在系统开机的时候,会创建systemserver进程,该进程会启动framework层的各种后台服务,其中包括FingerprintService、fingerprintd等服务。初始化流程时序如下:
这里写图片描述

2、SystemServer.java

文件路径:/frameworks/base/services/java/com/android/server/SystemServer.java

2.1systemserver进程入口

开机时会执行main函数,然后执行run方法去启动服务,而FingerprintService是在startOtherServices中启动的。

   public static void main(String[] args) {        new SystemServer().run();    }    private void run() {           //......            startBootstrapServices();            startCoreServices();            startOtherServices();           //.......          }

2.2 启动FingerprintService

private void startOtherServices() {    //......    //启动FingerprintService    mSystemServiceManager.startService(FingerprintService.class);}

3、SystemServiceManager.java

文件路径:/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java

    public <T extends SystemService> T startService(Class<T> serviceClass) {        //此事serviceClass为FingerprintService.class        final String name = serviceClass.getName();        Slog.i(TAG, "Starting " + name);        // Create the service.        if (!SystemService.class.isAssignableFrom(serviceClass)) {            throw new RuntimeException("Failed to create " + name                    + ": service must extend " + SystemService.class.getName());        }        final T service;        try {            Constructor<T> constructor = serviceClass.getConstructor(Context.class);            service = constructor.newInstance(mContext);        } catch (InstantiationException ex) {            //....        }        mServices.add(service);        // 启动服务        try {            service.onStart();        } catch (RuntimeException ex) {            throw new RuntimeException("Failed to start service " + name                    + ": onStart threw an exception", ex);        }        return service;    }

4、FingerprintService.java

文件路径:/frameworks/base/services/core/java/com/android/server/fingerprint/FingerprintService.java

    public void onStart() {        publishBinderService(Context.FINGERPRINT_SERVICE, new FingerprintServiceWrapper());//见本章第5节        IFingerprintDaemon daemon = getFingerprintDaemon();//见本章第6节        if (DEBUG) Slog.v(TAG, "Fingerprint HAL id: " + mHalDeviceId);        listenForUserSwitches();    }

5、SystemService.java

文件路径:/frameworks/base/services/core/java/com/android/server/SystemService.java

    protected final void publishBinderService(String name, IBinder service) {        publishBinderService(name, service, false);    }    protected final void publishBinderService(String name, IBinder service,            boolean allowIsolated) {        ServiceManager.addService(name, service, allowIsolated);//注册服务,将FingerprintService添加到ServiceManager中    }

6、获取fingerprintd服务

    public IFingerprintDaemon getFingerprintDaemon() {        if (mDaemon == null) {            mDaemon = IFingerprintDaemon.Stub.asInterface(ServiceManager.getService(FINGERPRINTD));//获取fingerprintd代理分身类            if (mDaemon != null) {                try {                    mDaemon.asBinder().linkToDeath(this, 0);                    //向fingerprintd注册回调函数mDaemonCallback                    mDaemon.init(mDaemonCallback);                    //调用获取fingerprintd的openhal函数                    mHalDeviceId = mDaemon.openHal();                    if (mHalDeviceId != 0) {                        updateActiveGroup(ActivityManager.getCurrentUser());                    } else {                        Slog.w(TAG, "Failed to open Fingerprint HAL!");                        mDaemon = null;                    }                } catch (RemoteException e) {                    Slog.e(TAG, "Failed to open fingeprintd HAL", e);                    mDaemon = null; // try again later!                }            } else {                Slog.w(TAG, "fingerprint service not available");            }        }        return mDaemon;    }

7、FingerprintDaemonProxy.cpp

7.1 mDaemon.init

void FingerprintDaemonProxy::init(const sp<IFingerprintDaemonCallback>& callback) {    if (mCallback != NULL && IInterface::asBinder(callback) != IInterface::asBinder(mCallback)) {        IInterface::asBinder(mCallback)->unlinkToDeath(this);    }    IInterface::asBinder(callback)->linkToDeath(this);    mCallback = callback;}

7.2 mDaemon.openHal

    int64_t FingerprintDaemonProxy::openHal()    {        ALOG(LOG_VERBOSE, LOG_TAG, "nativeOpenHal()\n");        int err;        const hw_module_t* hw_module = NULL;        //获取HAL层指纹模块        if (0 != (err = hw_get_module(FINGERPRINT_HARDWARE_MODULE_ID, &hw_module))) {            ALOGE("Can't open fingerprint HW Module, error: %d", err);            return 0;        }        if (NULL == hw_module) {            ALOGE("No valid fingerprint module");            return 0;        }        mModule = reinterpret_cast<const fingerprint_module_t*>(hw_module);        if (mModule->common.methods->open == NULL) {            ALOGE("No valid open method");            return 0;        }        hw_device_t* device = NULL;        //初始化HAL层        if (0 != (err = mModule->common.methods->open(hw_module, NULL, &device))) {            ALOGE("Can't open fingerprint methods, error: %d", err);            return 0;        }        if (kVersion != device->version) {            ALOGE("Wrong fp version. Expected %d, got %d", kVersion, device->version);            // return 0; // FIXME        }        mDevice = reinterpret_cast<fingerprint_device_t*>(device);        //设置回调函数        err = mDevice->set_notify(mDevice, hal_notify_callback);        if (err < 0) {            ALOGE("Failed in call to set_notify(), err=%d", err);            return 0;        }        // Sanity check - remove        if (mDevice->notify != hal_notify_callback) {            ALOGE("NOTIFY not set properly: %p != %p", mDevice->notify, hal_notify_callback);        }        ALOG(LOG_VERBOSE, LOG_TAG, "fingerprint HAL successfully initialized");        return reinterpret_cast<int64_t>(mDevice); // 返回句柄    }

8总结

自fingerprintd往下走,就是不同指纹厂商自己定义实现。本文简单分析了指纹服务的初始化过程,接下来将分析一下指纹的录入流程,未完待续。。。

原创粉丝点击