Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 驅動開發系列(四):編寫 HAL 層對應的 JNI 方法

編輯:關於 Android 程式設計

時隔多日,終於抽出時間寫部落格了。廢話不多說,接續上一篇,本文將介紹如何編寫 HAL 層(硬體抽象層)對應的 JNI 方法。以下內容皆以 ICS 源碼(android-4.0.4_r1.2)編譯為例。

步驟一:定義 JNI 層接口

進入 hardware/libhardware/include/hardware 目錄,建立 ttt.h 檔,內容如下:

#ifndef ANDROID_TTT_INTERFACE_H
#define ANDROID_TTT_INTERFACE_H
#include <hardware/hardware.h>

__BEGIN_DECLS

// 定義模塊 ID
#define HELLO_HARDWARE_MODULE_ID   "ttt"

// 硬件模塊結構體
struct ttt_module_t{
    struct hw_module_t common;
};

// hardware interface struct
struct ttt_device_t{
    struct hw_device_t common;
    int fd;
    int(*set_val)(struct ttt_device_t* dev, int val);
    int(*get_val)(struct ttt_device_t* dev, int* val);
};

__END_DECLS

#endif

步驟二:實現 JNI 層接口功能

進入 frameworks/base/services/jni 目錄,建立 com_android_server_TTTService.cpp 檔:

#define LOG_TAG     "TTTService"

#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include <utils/misc.h>
#include <utils/Log.h>
#include <hardware/hardware.h>
#include <hardware/ttt.h>
#include <stdio.h>

namespace android
{
    struct ttt_device_t* ttt_device = NULL;

    // 透過 HAL 接口設定暫存器值
    static void ttt_setVal(JNIEnv* env, jobject clazz, jint value){
        int val = value;
        LOGI("TTT JNI: set value %d to device.", val);
        if(!ttt_device){
            LOGI("TTT JNI: device is not open.");
            return;
        }

        ttt_device->set_val(ttt_device, val);
    }

    // 透過 HAL 接口讀取暫存器值
    static jint ttt_getVal(JNIEnv* env, jobject clazz){
        int val = 0;
        if(!ttt_device){
            LOGI("TTT JNI: device is not open.");
            return val;
        }
        ttt_device->get_val(ttt_device, &val);

        LOGI("TTT JNI: get value %d from device.", val);

        return val;
    }

    // 透過 HAL 接口開啟硬體裝置
    static inline int ttt_device_open(const hw_module_t* module, struct ttt_device_t** device){
        return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
    }

    // 透過硬體模塊 ID 加載 HAL 模塊並開啟裝置
    static jboolean ttt_init(JNIEnv* env, jclass clazz){
        ttt_module_t* module;

        LOGI("TTT JNI: initializing...");
        if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0){
            LOGI("TTT JNI: ttt stub found.");
            if(ttt_device_open(&(module->common), &ttt_device) == 0){
                LOGI("TTT JNI: ttt device is open.");
                return 0;
            }
            LOGE("TTT JNI: failed to open ttt device.");
            return -1;
        }

        LOGE("TTT JNI: failed to get ttt stub module.");
        return -1;
    }

    // JNI methods table
    static const JNINativeMethod method_table[] = {
        {"init_native", "()Z", (void*)ttt_init},
        {"setVal_native", "(I)V", (void*)ttt_setVal},
        {"getVal_native", "()I", (void*)ttt_getVal},
    };

    // 註冊 JNI method
    int register_android_server_TTTService(JNIEnv* env){
        return jniRegisterNativeMethods(env, "com/android/server/TTTService", method_table, NELEM(method_table));
    }
};

步驟三:添加 JNI 初始化調用

修改 frameworks/base/services/jni 目錄下的 onload.cpp 檔,在 JNI_OnLoad 函數的 return 之前加入:

register_android_server_TTTService(env);

同時,在該檔案的 namespace 中加上這句聲明:

int register_android_server_TTTService(JNIEnv* env);

如此一來,系統初始化時就會調用 register_android_server_TTTService 方法來加載 JNI 方法。

步驟四:添加編譯 JNI 的配置

修改 frameworks/base/services/jni 目錄下的 Android.mk 檔,在 LOCAL_SRC_FILES 變量中加入一行:

com_android_server_TTTService.cpp \

步驟五:開始編譯

先執行環境設定腳本:

. build/envsetup.sh

接著用 mmm 針對 jni 目錄進行增量編譯:

mmm frameworks/base/services/jni

這裡可能會遇到一個常見錯誤:提示找不到 libsystem_server.so 檔,導致 make 停止。這是因為該動態庫尚未生成。只要先執行以下命令產出它即可:

make libsystem_server

看到「Install: out/target/product/generic/system/lib/libsystem_server.so」出現,就代表已成功生成。此後再次執行 mmm frameworks/base/services/jni,便能看到 SharedLib、Symbolic、Strip 與 Install 等訊息,代表 libandroid_servers.so 已順利編譯完成。

步驟六:重新打包 system.img

最後,把我們編寫的 JNI 方法打包進 system.img:

make snod

當看到「Target system fs image: out/target/product/generic/system.img」並顯示映像檔大小時,就代表已成功把自訂的 JNI 打包進 system.img,整個 HAL 到 JNI 的串接流程至此完成。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved