您的位置:首页 > 移动开发 > Android开发

Android HAL层解析

2015-08-13 11:32 417 查看
[1] HAL(Hardware Abstract Layer)

标准化硬件操作, 使得Android系统不至于过渡依赖linux内核

[2] libhardware_legacy

直接将硬件操作实现成一个动态库, 存在如下确定:

应用程序或服务使用硬件时,每个进程会加载一次硬件操作动态库, 使得硬件

操作动态库,无法知道硬件是否被竞态访问。

[3] libhardware(stub)

硬件抽象层多了一个libruntime.so库, 该库通过dlopen机制打开并使用动态库,

这种机制保证多个进程使用同一个库,操作系统只会加载一次, 从而保证硬件操作

模块的动态库,同一个硬件只会加载一次, 硬件模块动态库中知道一个硬件是否被

多个进程使用,从而可以处理竞态。

[4] 系统框架

《系统服务框架.bmp》



[5] led程序数据结构继承

struct led_module_t {

struct hw_module_t module; // 继承hw_module_t数据结构

...

...

}

struct led_control_device_t {

struct hw_device_t led; // 继承hw_device_t数据结构

...

...

}

adb shell

hardware\farsight\LedDemo\led_java\Farsight_Test\src\com\farsight\service

LedService.java:

$ logcat | grep "Java Service"

// hardware\farsight\LedDemo\led_runtime

com_farsight_service_LedService.cpp:

$ logcat | grep LedService

int ()(int);

(I)I

#define LOG_TAG "LedService"
#include "utils/Log.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <jni.h>
#include "../../led_hal/include/led.h"
static led_control_device_t *sLedDevice = 0;
static led_module_t* sLedModule=0;
static jint get_count(void)
{
ALOGD("%s E", __func__);
if(sLedDevice)
sLedDevice->getcount_led(sLedDevice);
else
ALOGE("sLedDevice is null");
ALOGD("%s X", __func__);
return 0;
}

static jint led_setOn(JNIEnv* env, jobject thiz, jint arg) {
ALOGD("%s E", __func__);
if (sLedDevice) {
sLedDevice->set_on(sLedDevice, (int)arg);
}else{
ALOGE("sLedDevice is null");
}
ALOGD("%s X", __func__);
return 0;
}

static jint led_setOff(JNIEnv* env, jobject thiz, jint arg) {
ALOGD("%s E", __func__);
if (sLedDevice) {
sLedDevice->set_off(sLedDevice, (int)arg);
}else{
ALOGE("sLedDevice is null");
}
ALOGD("%s X", __func__);

return 0;
}

/** helper APIs */
static inline int led_control_open(const struct hw_module_t* module,
struct led_control_device_t** device) {
ALOGD("%s", __func__);
// led_hal/module/led.c:
// led_device_open
return module->methods->open(module,
LED_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
}

static jint led_init(JNIEnv *env, jclass clazz)
{
led_module_t * module;
ALOGD("%s E", __func__);

// 找到hw_module_t(led_module_t)的地址
// 根据LED_HARDWARE_MODULE_ID,找module
// hw_get_module---> libled.polaris.so--成员变量-->HMI-->HMI.id == LED_HARDWARE_MODULE_ID
if (hw_get_module(LED_HARDWARE_MODULE_ID, (hw_module_t const**)&module) == 0) {
ALOGI("get module OK");
sLedModule = (led_module_t *) module;
if (led_control_open(&module->common, &sLedDevice) != 0) {
ALOGE("led_control_open error");
return -1;
}
}

ALOGD("%s X", __func__);
return 0;
}

/*
*
** Array of methods.
** Each entry has three fields: the name of the method, the method
** signature, and a pointer to the native implementation.
*/
static const JNINativeMethod gMethods[] = {
{"_init",        "()Z", (void*)led_init},
{ "_set_on",     "(I)I", (void*)led_setOn },
{ "_set_off",    "(I)I", (void*)led_setOff },
{ "_get_count",  "()I", (void*)get_count },
};

static int registerMethods(JNIEnv* env) {
ALOGD("%s E", __func__);
static const char* const kClassName = "com/farsight/service/LedService";
jclass clazz;
/* look up the class */
clazz = env->FindClass(kClassName);
if (clazz == NULL) {
ALOGE("Can't find class %s\n", kClassName);
return -1;
}

/* register all the methods */
if (env->RegisterNatives(clazz, gMethods,
sizeof(gMethods) / sizeof(gMethods[0])) != JNI_OK)
{
ALOGE("Failed registering methods for %s\n", kClassName);
return -1;
}
ALOGD("%s X", __func__);
/* fill out the rest of the ID cache */
return 0;
}

/*
*
*   * This is called by the VM when the shared library is first loaded.
*/
jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env = NULL;
jint result = -1;
ALOGI("JNI_OnLoad");

if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
ALOGE("ERROR: GetEnv failed\n");
goto fail;
}

assert(env != NULL);
if (registerMethods(env) != 0) {
ALOGE("ERROR: PlatformLibrary native registration failed\n");
goto fail;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;

fail:
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: