一、HAL编写
/hardware/libhardware/include/hardware 添加hello.h
/hardware/libhardware/modules 添加hello文件夹 下面是hello.c和amdroid.bp文件
hello.h文件如下
#include <sys/cdefs.h>
#include <sys/types.h>
#include <hardware/hardware.h>
//HAL模块名
#define HELLO_HARDWARE_MODULE_ID "hello"
//HAL版本号
#define HELLO_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(0, 1)
//设备名
#define HARDWARE_HELLO "hello"
//自定义HAL模块结构体
typedef struct hello_module {
struct hw_module_t common;
} hello_module_t;
//自定义HAL设备结构体
typedef struct hello_device {
struct hw_device_t common;
//加法函数
int (*additionTest)(const struct hello_device *dev,int a,int b,int* total);
} hello_device_t;
//给外部调用提供打开设备的函数
static inline int _hello_open(const struct hw_module_t *module,
hello_device_t **device) {
return module->methods->open(module, HARDWARE_HELLO,
(struct hw_device_t **) device);
}
hello.c文件如下:
#define LOG_TAG "HelloHal"
#include <malloc.h>
#include <stdint.h>
#include <string.h>
#include <log/log.h>
#include <hardware/hello.h>
#include <hardware/hardware.h>
//加法函数实现
static int additionTest(const struct hello_device *dev,int a,int b,int *total)
{
if(!dev){
return -1;
}
*total = a + b;
return 0;
}
//关闭设备函数
static int hello_close(hw_device_t *dev)
{
if (dev) {
free(dev);
return 0;
} else {
return -1;
}
}
//打开设备函数
static int hello_open(const hw_module_t* module,const char __unused *id,
hw_device_t** device)
{
if (device == NULL) {
ALOGE("NULL device on open");
return -1;
}
hello_device_t *dev = malloc(sizeof(hello_device_t));
memset(dev, 0, sizeof(hello_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = HELLO_MODULE_API_VERSION_1_0;
dev->common.module = (struct hw_module_t*) module;
dev->common.close = hello_close;
dev->additionTest = additionTest;
*device = &(dev->common);
return 0;
}
static struct hw_module_methods_t hello_module_methods = {
.open = hello_open,
};
//导出符号HAL_MODULE_INFO_SYM,指向自定义模块
hello_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.module_api_version = HELLO_MODULE_API_VERSION_1_0,
.hal_api_version = HARDWARE_HAL_API_VERSION,
.id = HELLO_HARDWARE_MODULE_ID,
.name = "Demo Hello HAL",
.author = "dongjiao.tang@tcl.com",
.methods = &hello_module_methods,
},
};
```Android.bp文件如下
```bash
cc_library_shared {
name: "hello.default",
relative_install_path: "hw",
proprietary: true,
srcs: ["hello.c"],
header_libs: ["libhardware_headers"],
shared_libs: ["liblog"],
}
编译
mmm hardware/libhardware/modules/hello/