android 自定义驱动(第二篇:HAL)
简介
上篇文章说明了如何在Linux内核中实现编写驱动程序;
这篇文章主要介绍如何在硬件抽象层中添加硬件模块来和内核程序交互,通过这篇文章,我们还将学会如何在Android系统创建设备文件时使用Selinux的udev规则修改设备文件的访问权限;
完成内核驱动程序后,便可以在Android系统中得到三个文件/dev/hello、/sys/class/hello/hello/val和/proc/hello
。我们将通过/dev/hello
来连接硬件抽象层模块和Linux内核驱动程序模块。
源码地址:https://github.com/momxmo/aosp_driver/tree/master/2
一、定义HAl抽象层hello.h头文件
进入到在hardware/libhardware/include/hardware
目录,新建hello.h文件:
hello.h文件的内容如下:
#ifndef ANDROID_HELLO_INTERFACE_H
#define ANDROID_HELLO_INTERFACE_H
#include <hardware/hardware.h>
//__BEGIN_DECLS
#if defined(__cplusplus)
extern "C" {
#endif
/* define module ID */
#define HELLO_HARDWARE_MODULE_ID "hello"
/* hardware module struct */
struct hello_module_t {
struct hw_module_t common;
};
/* hardware interface struct */
struct hello_device_t {
struct hw_device_t common;
int fd; // 设备文件描述符,对应我们将要处理的设备文件"/dev/hello"
int (*set_val)(struct hello_device_t* dev, int val); // set_val 为HAL对上提供的函数接口
int (*get_val)(struct hello_device_t* dev, int* val); // // get_val 为HAL对上提供的函数接口
};
#if defined(__cplusplus)
}
#endif
//__END_DECLS
#endif
这里按照标准Android硬件标准抽象层规范要求,分别定义模块ID、模块结构体以及硬件接口结构体。在硬件接口结构体中,fd表示设备文件描述符,对应我们将要处理的设备/dev/hello
,set_val和get_val为该HAL对上提供的函数接口。
二、实现硬件访问hello.c
进入到hardware/libhardware/modules
目录,新建hello目录,并添加hello.c文件。
这里我们主要那代码中的几个部分来说明
①首先引入相关头文件和相关定义结构:
#define LOG_TAG "HelloStub"
#include <hardware/hardware.h>
#include <hardware/hello.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#define DEVICE_NAME "/dev/hello"
#define MODULE_NAME "Hello"
#define MODULE_AUTHOR "momxmo"
②定义几个静态函数方法,用于打开、关闭、访问驱动设备文件:
下面会分析具体的实现
/*设备打开和关闭接口*/
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);
static int hello_device_close(struct hw_device_t* device);
/*设备访问接口*/
static int hello_set_val(struct hello_device_t* dev, int val);
static int hello_get_val(struct hello_device_t* dev, int* val);
③最最最重点
这里,实例变量名必须为HAL_MODULE_INFO_SYM
,tag也必须为HARDWARE_MODULE_TAG
,这是Android硬件抽象层规范规定的。
/* 模块方法表*/
static struct hw_module_methods_t hello_module_methods = {
open: hello_device_open
};
/*模块实例变量*/
struct hello_module_t HAL_MODULE_INFO_SYM = {
// 这里,实例变量名必须为HAL_MODULE_INFO_SYM, tag也必须为HARDWARE_MODULE_TAG
// 这是android硬件抽象层规范规定的。
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id:HELLO_HARDWARE_MODULE_ID,
name: MODULE_NAME,
author: MODULE_AUTHOR,
methods: &hello_module_methods,
}
};
④打开设备
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {
struct hello_device_t* dev;
dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));
ALOGD("Hello Stub: hello_device_open");
if(!dev) {
ALOGE("Hello Stub: failed to alloc space");
return -EFAULT;
}
// memset是计算机中C/C++语言初始化函数。作用是将某一块内存中的内容全部设置为指定的值,
//这个函数通常为新申请的内存做初始化工作
memset(dev, 0, sizeof(struct hello_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = hello_device_close;
dev->set_val = hello_set_val;
dev->get_val = hello_get_val;
// open是UNIX系统(包括LINUX、Mac等)的系统调用函数,区别于C语言库函数fopen
// https://baike.baidu.com/item/open/13009226#1_1
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
ALOGE("Hello Stub:failed to open /dev/hello --%s.", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
ALOGI("Hello Stub: open /dev/hello successfully.");
return 0;
}
上面代码首先为hello_device_t
申请内存,并对其内部成员变量赋值,hw_module_t
为加载hal对应so库后得到的对应信息,这里使用指针方式记录(后面HDIL服务器加载的时候会传入);
dev->common.close = hello_device_close
对应设备的关闭方法;dev->set_val = hello_set_val
对应设备设置值方法;dev->get_val = hello_get_val
对应获取设备值方法;
open
是系统函数,负责打开/dev/hello
驱动节点文件;成功则返回文件描述符,否则返回-1;
⑤关闭设备
static int hello_device_close(struct hw_device_t* device) {
//强转类型
struct hello_device_t* hello_device = (struct hello_device_t*)device;
ALOGD("Hello Stub: hello_device_close");
if(hello_device) {
close(hello_device->fd);
ALOGI("Hello Stub:hello_device_close hello_device->fd:%d", hello_device->fd);
free(hello_device);
}
return 0;
}
close
同样是UNIX系统(包括LINUX、Mac等)的系统调用函数,传入文件描述符;最后记得释放内存
⑥设置数据
static int hello_set_val(struct hello_device_t * dev, int val) {
ALOGI("Hello Stub: hello_set_val set value %d to device.", val);
// write()会把参数buf所指的内存写入count个字节到参数fd所指的文件内。 fd,是open时打开的
// 例: int fp = open("/home/test.txt", O_RDWR|O_CREAT);
int result = write(dev->fd, &val, sizeof(val));
ALOGI("Hello Stub:hello_set_val result:%d", result);
return 0;
}
write
方法也是系统函数,负责写入数据到文件中
⑦读取数据
static int hello_get_val(struct hello_device_t* dev, int* val) {
ALOGD("Hello Stub: hello_get_val");
if(!val) {
ALOGE("Hello Stub: error val pointer");
return -EFAULT;
}
int result = read(dev->fd, val, sizeof(*val));
ALOGI("Hello Stub:hello_get_val result:%d", result);
ALOGI("Hello Stub: get value %d from device.", *val);
return 0;
}
这里通过int* val
指针回传的方式,将读取到的数据复制到val中;
三、节点权限问题
DEVICE_NAME定义为"/dev/hello"。由于设备文件是在内核驱动里面通过device_create创建的,而device_create创建的设备文件默认只有root用户可读写,而hello_device_open一般是由上层APP来调用的,这些APP一般不具有root权限,这时候就导致打开设备文件失败:
Hello Stub: failed to open /dev/hello -- Permission denied.
解决办法:
类似于Linux的udev规则,打开Android源代码工程目录下,修改system/core/rootdir/ueventd.rc
文件,往里面添加一行:
/dev/hello 0666 root root
四、新建编译脚本Android.mk文件
在hello目录下新建Android.mk文件,内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello.default
#LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_RELATIVE_PATH := hw
#LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_PROPRIETARY_MODULE := true
LOCAL_SRC_FILES := hello.c
LOCAL_SHARED_LIBRARIES := liblog libcutils libutils
LOCAL_CFLAGS += -Wno-implicit-function-declaration
#LOCAL_CFLAGS += -Wno-error
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)
注意:
LOCAL_MODULE
的定义规则,hello后面跟有default,
hello.default
能够保证我们的模块总能被硬象抽象层加载到
五、将hello模块添加到系统
这里的目的是保证在整编译系统的时候,才会将hello模块打包到vendor.img镜像中;
需要在/hardware/libhardware/modules/Android.mk
中添加hello模块:
hardware_modules := \
audio_remote_submix \
...........
vr \
hello
六、编译
执行命令:
$ mmm hardware/libhardware/modules/hello
编译成功后,就可以在Android/out/target/product/angler/vendor/lib64/hw
目录下看到hello.default.so
文件了。
七、so库导入系统中
如果不想重新打包镜像系统,这里我们可以通过adb push的方式将hello.default.so
文件push到手机系统/vendor/lib64/hw/
目录下,并添加777权限;
说明:我这里使用的是Nexus 6p, vendor.img镜像是官网提供的,编译时会直接拷贝到输出目录,没有将我新加的库编译合入,所以这一步我是通过adb push命令的方式