开发Android硬件抽象层(HAL)模块
文件列表
hardware/libhardware/include/hardware/hello.h
hardware/libhardware/modules/hello.c
hardware/libhardware/modules/Android.mk
HAL层包括两个部分
增加硬件抽象层(HAL)模块访问Linux内核驱动程序
硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
编写硬件抽象层(HAL)模块接口
本章节是将通过设备文件/dev/hello来连接硬件抽象层模块和Linux内核驱动程序模块
hello.h
在android\qiku\hardware\libhardware\include\hardware目录,新增hello.h
#ifndef ANDROIDDRIVER_HELLO_H
#define ANDROIDDRIVER_HELLO_H
#include
struct hello_module_t {
struct hw_module_t common;
};
struct hello_device_t {
struct hw_device_t common;
int fd;
int (*get_val)(struct hello_device_t *dev,int *val);
int (*set_val)(struct hello_device_t *dev,int val);
};
int hello_dri_open(const struct hw_module_t* module, const char* id,
struct hw_device_t** device);
#endif //ANDROIDDRIVER_HELLO_H
hello.c
在android\qiku\hardware\libhardware\modules\目录创建hello目录,创建Android.mk和hello.c文件
hello.c
#include
#include
#include
#include
#include
#include
#include
struct hw_module_methods_t hello_method = {
open : hello_dri_open
};
struct hello_module_t HAL_MODULE_INFO_SYM = {
common : {
tag : HARDWARE_MODULE_TAG,
version_major : 1,
version_minor : 0,
id : "hello",
name : "hello",
author : "jdf",
methods : &hello_method
}
};
int hello_get_val(struct hello_device_t *dev,int *val){
if(!dev) {
ALOGI("HelloHal Null dev pointer.");
return -EFAULT;
}
if(!val) {
ALOGI("HelloHal Null val pointer.");
return -EFAULT;
}
read(dev->fd, val, sizeof(*val));
ALOGI("HelloHal Get value %d from device file /dev/hello.", *val);
return *val;
}
int hello_set_val(struct hello_device_t *dev,int val){
if(!dev) {
ALOGI("HelloHal Null dev pointer.");
return -EFAULT;
}
ALOGI("HelloHal Set value %d to device file /dev/hello.", val);
write(dev->fd, &val, sizeof(val));
return 0;
}
int hello_close(struct hw_device_t* device){
//关闭文件,释放内存
struct hello_device_t *dev = (struct hello_device_t *)device;
if(dev) {
close(dev->fd);
free(dev);
}
return 0;
}
int hello_dri_open(const struct hw_module_t* module, const char* id,struct hw_device_t** device){
ALOGI("HelloHal hello_device_open...%s",id);
ALOGI("HelloHal YES,IT IS HELLO");
struct hello_device_t* dev;
dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));
if(!dev) {
ALOGI("HelloHal: failed to alloc space");
return -EFAULT;
}
memset(dev,0, sizeof(struct hello_device_t));
ALOGI("HelloHal memset sucess /dev/hello");
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = hello_close;
dev->set_val = hello_set_val;
dev->get_val = hello_get_val;
if((dev->fd = open("/dev/hello",O_RDWR))== -1){
ALOGI("HelloHal fail open /dev/hello -- %s.", strerror(errno));
return -EFAULT;
}
*device = &(dev->common);
ALOGI("HelloHal Open device file /dev/hello successfully.");
return 0;
}
OPEN方法中的 /dev/hello也就是我们上一节创建的字符虚拟设备
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := hello.c
LOCAL_MODULE := hello.default
include $(BUILD_SHARED_LIBRARY)
处理硬件访问权限问题
DEVICE_NAME定义为"/dev/hello"。由于设备文件是在内核驱动里面通过device_create创建的,而device_create创建的设备文件默认只有root用户可读写,而hello_device_open一般是由上层APP来调用的,这些APP一般不具有root权限,这时候为/dev/hello添加root权限
打开Android源代码工程目录下,进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:
diff --git a/core/rootdir/ueventd.rc b/core/rootdir/ueventd.rc
old mode 100644
new mode 100755
index b735dc3..d6607f4
--- a/core/rootdir/ueventd.rc
+++ b/core/rootdir/ueventd.rc
@@ -1,6 +1,7 @@
subsystem adf
devname uevent_devname
+/dev/hello 0666 root root
/dev/null 0666 root root
/dev/zero 0666 root root
/dev/full 0666 root root
编译验证
通过mmm单独编译下改模块
jdf@ubuntu:~/mtk_6737_6.x_wtwd_8_qiku_LE_PLUS/android/qiku$ mmm hardware/libhardware/modules/hello/ -j8
查看生成结果:
xuzhike@ubuntu:~/mtk_6737_6.x_wtwd_8_qiku_LE_PLUS/android/qiku$ ls out/target/product/yk658_37m_lwtg_35g/system/lib/hw | grep hello
hello.default.so
重新打包编译system.image或者全编工程
xuzhike@ubuntu:~/mtk_6737_6.x_wtwd_8_qiku_LE_PLUS/android/qiku$ make snod