1. 程式人生 > >指紋識別分析之framework初始化流程

指紋識別分析之framework初始化流程

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往下走,就是不同指紋廠商自己定義實現。本文簡單分析了指紋服務的初始化過程,接下來將分析一下指紋的錄入流程,未完待續。。。