我們介紹了如何為Android 系統的硬件編寫驅動程序,包括如何在Linux 內核空間實現內核驅動程序和在用戶空間實現硬件抽象
層接口。實現這兩者的目的是為了向更上一層提供硬件訪問接口,即為Android 的Application Frameworks 層提供硬件服務。
我們知道,Android 系統的應用程序是用Java 語言編寫的,而硬件驅動程序是用C 語言來實現的,那么,Java 接口如何去訪
問C 接口呢?眾所周知,Java 提供了JNI 方法調用,同樣,在Android 系統中,Java 應用程序通過JNI 來調用硬件抽象層接口。
在這一篇文章中,我們將介紹如何為Android 硬件抽象層接口編寫JNI 方法,以便使得上層的Java 應用程序能夠使用下層提
供的硬件服務。
step1: 進入到frameworks/base/services/jni 目錄,新建com_Android_server_HelloService.cpp 文件
#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/hello.h> #include <stdio.h> #include <android/log.h> #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"hello_stub",__VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,"hello_stub",__VA_ARGS__) #define LOG_TAG "HelloService" namespace Android { /*在硬件抽象層中定義的硬件訪問結構體,參考<hardware/hello.h>*/ struct hello_device_t* hello_device = NULL; /*通過硬件抽象層定義的硬件訪問接口設置硬件寄存器val 的值*/ static void hello_setVal(JNIEnv* env, jobject clazz, jint value) { int val = value; LOGI("Hello JNI: set value %d to device.", val); if(!hello_device) { LOGI("Hello JNI: device is not open."); return; } hello_device->set_val(hello_device, val); } /*通過硬件抽象層定義的硬件訪問接口讀取硬件寄存器val 的值*/ static jint hello_getVal(JNIEnv* env, jobject clazz) { int val = 0; if(!hello_device) { LOGI("Hello JNI: device is not open."); return val; } hello_device->get_val(hello_device, &val); LOGI("Hello JNI: get value %d from device.", val); return val; } /*通過硬件抽象層定義的硬件模塊打開接口打開硬件設備*/ static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) { return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device); } /*通過硬件模塊ID 來加載指定的硬件抽象層模塊並打開硬件*/ static jboolean hello_init(JNIEnv* env, jclass clazz) { hello_module_t* module; LOGI("Hello JNI: initializing......"); if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) { LOGI("Hello JNI: hello Stub found."); if(hello_device_open(&(module->common), &hello_device) == 0) { LOGI("Hello JNI: hello device is open."); return 0; } LOGE("Hello JNI: failed to open hello device."); return -1; } LOGE("Hello JNI: failed to get hello stub module."); return -1; } /*JNI 方法表*/ static const JNINativeMethod method_table[] = { {"init_native", "()Z", (void*)hello_init}, {"setVal_native", "(I)V", (void*)hello_setVal}, {"getVal_native", "()I", (void*)hello_getVal}, }; /*注冊JNI 方法*/ int register_Android_server_HelloService(JNIEnv *env) { return jniRegisterNativeMethods(env, "com/Android/server/HelloService", method_table, NELEM(method_table)); } };
step2: 修改同目錄下的onload.cpp 文件,首先在namespace Android 增加register_android_server_HelloService 函數聲明
namespace Android {
int register_Android_server_HelloService(JNIEnv *env);
};
在JNI_onLoad 增加register_Android_server_HelloService 函數調用:
extern "C" jint JNI_onLoad(JavaVM* vm, void* reserved)
{
register_android_server_HelloService(JNIEnv *env);
}
step3: 修改同目錄下的Android.mk 文件,在LOCAL_SRC_FILES 變量中增加一行:
LOCAL_SRC_FILES:= /
com_android_server_AlarmManagerService.cpp /
com_android_server_BatteryService.cpp /
com_android_server_InputManager.cpp /
com_android_server_LightsService.cpp /
com_android_server_PowerManagerService.cpp /
com_android_server_SystemServer.cpp /
com_android_server_UsbService.cpp /
com_android_server_VibratorService.cpp /
com_android_server_location_GpsLocationProvider.cpp /
com_android_server_HelloService.cpp /
onload.cpp