Android Framework层添加一个HAL模块

例如添加一个名为hal_test的HAL模块

添加目录 hardware\libhardware\modules\hal_test
hal_test目录下添加c/cpp/h代码文件, 并添加Android.mk

Android.mk示例:
LOCAL_PATH := (callmydir)include (CLEAR_VARS)
LOCAL_MODULE := hal_test.default
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SRC_FILES := hal_test.c
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)

添加hardware/libhardware/include/hardware/hal_test.h
修改hardware/libhardware/modules/Android.mk, 将hal_test模块加进去
修改build/target/product/embedded.mk,添加hal_test.default编译模块

hal_test.h实现要点

typedef struct hal_test_device {

struct hw_device_t common;
void (*func_a)(void *msg);
void (*func_b)(void *msg);

} hal_test_device_t;

typedef struct hal_test_module {

struct hw_module_t common;

} hal_test_module_t;

hw_device_t 和 hw_module_t都是定义在hardware/libhardware/include/hardware/hardware.h,
使用HAL库的时候, 首先获取 hal_test_module_t实例, 通过common成员的open函数成员获取 hal_test_device_t实例, hal_test_device_t中可以定义该HAL库需要对外提供的接口函数, 如func_a, func_b等等.

hal_test.c实现要点

实例化 hal_test_module_t, 并且变量名称固定为HAL_MODULE_INFO_SYM, HAL_MODULE_INFO_SYM定义在hardware/libhardware/include/hardware/hardware.h, 实际名称为HMI, 获取 hal_test_module_t实例的时候, 通过符号名HAL_MODULE_INFO_SYM来获取.
实现hal_test_module_t的common成员的open函数.
open函数实例化并返回 hal_test_device_t实例对象, 创建对象时, 需要对hal_test_device_t的common成员的各个属性进行赋值,包括close函数指针, 并对hal_test_device_t中提供的HAL库对外接口的函数指针赋值.
c文件要实现hal_test_device_t中定义的对外提供的各个接口函数, 及该HAL库的核心功能函数.

参考指纹识别

static int fingerprint_open(const hw_module_t* module, const char __unused *id,
                            hw_device_t** device)
{
    if (device == NULL) {
        ALOGE("NULL device on open");
        return -EINVAL;
    }

    fingerprint_device_t *dev = malloc(sizeof(fingerprint_device_t));
    memset(dev, 0, sizeof(fingerprint_device_t));

    dev->common.tag = HARDWARE_DEVICE_TAG;
    dev->common.version = FINGERPRINT_MODULE_API_VERSION_2_0;
    dev->common.module = (struct hw_module_t*) module;
    dev->common.close = fingerprint_close;

    dev->pre_enroll = fingerprint_pre_enroll;
    dev->enroll = fingerprint_enroll;
    dev->get_authenticator_id = fingerprint_get_auth_id;
    dev->cancel = fingerprint_cancel;
    dev->remove = fingerprint_remove;
    dev->set_active_group = fingerprint_set_active_group;
    dev->authenticate = fingerprint_authenticate;
    dev->set_notify = set_notify_callback;
    dev->notify = NULL;

    *device = (hw_device_t*) dev;
    return 0;
}

static struct hw_module_methods_t fingerprint_module_methods = {
    .open = fingerprint_open,
};

fingerprint_module_t HAL_MODULE_INFO_SYM = {
    .common = {
        .tag                = HARDWARE_MODULE_TAG,
        .module_api_version = FINGERPRINT_MODULE_API_VERSION_2_0,
        .hal_api_version    = HARDWARE_HAL_API_VERSION,
        .id                 = FINGERPRINT_HARDWARE_MODULE_ID,
        .name               = "Demo Fingerprint HAL",
        .author             = "The Android Open Source Project",
        .methods            = &fingerprint_module_methods,
    },
};

你可能感兴趣的:(Android Framework层添加一个HAL模块)