前面我们已经添加了驱动层的代码,接下来添加下HAL层的代码。
先看下HAL层模块结构体的定义规范。
hardware/libhardware/include/hardware/hardware.h
struct hw_module_t
/**
* Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
* and the fields of this data structure must begin with hw_module_t
* followed by module specific information.
*/
typedef struct hw_module_t {
/** tag must be initialized to HARDWARE_MODULE_TAG */
uint32_t tag;
/**
* The API version of the implemented module. The module owner is
* responsible for updating the version when a module interface has
* changed.
*
* The derived modules such as gralloc and audio own and manage this field.
* The module user must interpret the version field to decide whether or
* not to inter-operate with the supplied module implementation.
* For example, SurfaceFlinger is responsible for making sure that
* it knows how to manage different versions of the gralloc-module API,
* and AudioFlinger must know how to do the same for audio-module API.
*
* The module API version should include a major and a minor component.
* For example, version 1.0 could be represented as 0x0100. This format
* implies that versions 0x0100-0x01ff are all API-compatible.
*
* In the future, libhardware will expose a hw_get_module_version()
* (or equivalent) function that will take minimum/maximum supported
* versions as arguments and would be able to reject modules with
* versions outside of the supplied range.
*/
uint16_t module_api_version;
#define version_major module_api_version
/**
* version_major/version_minor defines are supplied here for temporary
* source code compatibility. They will be removed in the next version.
* ALL clients must convert to the new version format.
*/
/**
* The API version of the HAL module interface. This is meant to
* version the hw_module_t, hw_module_methods_t, and hw_device_t
* structures and definitions.
*
* The HAL interface owns this field. Module users/implementations
* must NOT rely on this value for version information.
*
* Presently, 0 is the only valid value.
*/
uint16_t hal_api_version;
#define version_minor hal_api_version
/** Identifier of module */
const char *id;
/** Name of this module */
const char *name;
/** Author/owner/implementor of the module */
const char *author;
/** Modules methods */
struct hw_module_methods_t* methods;
/** module's dso */
void* dso;
#ifdef __LP64__
uint64_t reserved[32-7];
#else
/** padding to 128 bytes, reserved for future use */
uint32_t reserved[32-7];
#endif
} hw_module_t;
注意几点:
1)、/**
- Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
- and the fields of this data structure must begin with hw_module_t
- followed by module specific information.
*/
每个模块必须自定义一个硬件抽象层模块结构体,而且第一个成员变量必须是hw_module_t
每个模块都必须存在一个导出符号HAL_MODULE_INFO_SYM
#define HAL_MODULE_INFO_SYM HMI
2)、 /** tag must be initialized to HARDWARE_MODULE_TAG */
#define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT(‘H’, ‘W’, ‘M’, ‘T’)
用来标志这是一个硬件抽象层模块结构体
3)、/** module’s dso */
成员变量dso用来保存加载硬件抽象层模块后得到的句柄值。
4)、 /** Modules methods */
成员变量methods 定义了硬件抽象层模块的操作方法列表,类型为hw_module_methods_t
hw_module_methods_t
typedef struct hw_module_methods_t {
/** Open a specific device */
int (*open)(const struct hw_module_t* module, const char* id,
struct hw_device_t** device);
} hw_module_methods_t;
struct hw_device_t
/**
* Every device data structure must begin with hw_device_t
* followed by module specific public methods and attributes.
*/
typedef struct hw_device_t {
/** tag must be initialized to HARDWARE_DEVICE_TAG */
//#define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T'),标志这是一个硬件抽象层中的硬件设备结构体。
uint32_t tag;
/**
* Version of the module-specific device API. This value is used by
* the derived-module user to manage different device implementations.
*
* The module user is responsible for checking the module_api_version
* and device version fields to ensure that the user is capable of
* communicating with the specific module implementation.
*
* One module can support multiple devices with different versions. This
* can be useful when a device interface changes in an incompatible way
* but it is still necessary to support older implementations at the same
* time. One such example is the Camera 2.0 API.
*
* This field is interpreted by the module user and is ignored by the
* HAL interface itself.
*/
uint32_t version;
/** reference to the module this device belongs to */
struct hw_module_t* module;
/** padding reserved for future use */
#ifdef __LP64__
uint64_t reserved[12];
#else
uint32_t reserved[12];
#endif
/** Close this device */
int (*close)(struct hw_device_t* device);
} hw_device_t;
接下来我们来编写硬件抽象层的接口
HAl层目录结构:
hardware
----libhardware/include/hardware/demo.h
----libhardware/modules/demo
|—demo.cpp
|—Android.mk
demo.h: 按照android对HAL层规范的要求,分别定义模块ID、模块结构体以及硬件接口结构体.
#ifndef ANDROID_INCLUDE_DEMO_H
#define ANDROID_INCLUDE_DEMO_H
#include <hardware/hardware.h>
__BEGIN_DECLS
/**
* The id of this module
*/
#define DEMO_HARDWARE_MODULE_ID "demo"
/**
* The id of this device
*/
#define DEMO_HARDWARE_DEVICE_ID "demo"
struct demo_module_t {
struct hw_module_t common;
};
struct demo_device_t {
struct hw_device_t common;
int fd;
int (*set_val)(struct demo_device_t* dev, int val);
int (*get_val)(struct demo_device_t* dev, int* val);
};
__END_DECLS
#endif
demo.cpp
#define LOG_TAG "demoHALStub"
#include <hardware/hardware.h>
#include <hardware/demo.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#include <malloc.h>
#include <stdint.h>
#include <sys/time.h>
#include <hardware/hardware.h>
#include <system/audio.h>
#include <hardware/audio.h>
#define DEVICE_NAME "/dev/demo"
#define MODULE_NAME "Demo"
#define MODULE_AUTHOR "shyluo@gmail.com"
static int demo_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device);
static int demo_device_close(struct hw_device_t* device);
static int demo_set_val(struct demo_device_t* dev, int val);
static int demo_get_val(struct demo_device_t* dev, int* val);
static struct hw_module_methods_t demo_module_methods = {
open: demo_device_open
};
struct demo_module_t HAL_MODULE_INFO_SYM = {
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: DEMO_HARDWARE_MODULE_ID,
name: MODULE_NAME,
author: MODULE_AUTHOR,
methods: &demo_module_methods,
}
};
static int demo_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device) {
if(!strcmp(id, DEMO_HARDWARE_DEVICE_ID)) {
struct demo_device_t* dev;
dev = (struct demo_device_t*)malloc(sizeof(struct demo_device_t));
if(!dev) {
ALOGE("Failed to alloc space for demo_device_t.");
return -EFAULT;
}
memset(dev, 0, sizeof(struct demo_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = demo_device_close;
dev->set_val = demo_set_val;
dev->get_val = demo_get_val;
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
ALOGE("Failed to open device file /dev/demo -- %s.", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
ALOGI("Open device file /dev/demo successfully.");
return 0;
}
return -EFAULT;
}
static int demo_device_close(struct hw_device_t* device) {
struct demo_device_t* demo_device = (struct demo_device_t*)device;
if(demo_device) {
close(demo_device->fd);
free(demo_device);
}
return 0;
}
static int demo_set_val(struct demo_device_t* dev, int val) {
if(!dev) {
ALOGE("Null dev pointer.");
return -EFAULT;
}
ALOGI("Set value %d to device file /dev/demo.", val);
write(dev->fd, &val, sizeof(val));
return 0;
}
static int demo_get_val(struct demo_device_t* dev, int* val) {
if(!dev) {
ALOGE("Null dev pointer.");
return -EFAULT;
}
if(!val) {
ALOGE("Null val pointer.");
return -EFAULT;
}
read(dev->fd, val, sizeof(*val));
ALOGI("Get value %d from device file /dev/demo.", *val);
return 0;
}
LOCAL_PATH := $(call my-dir)
# HAL module implemenation stored in
# hw/<OVERLAY_HARDWARE_MODULE_ID>.<ro.product.board>.so
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 := demo.cpp
LOCAL_MODULE := demo.default
include $(BUILD_SHARED_LIBRARY)
编译
mmm hardware/libhardware/modules/demo/
之后会在out/target/product/generic/system/lib/hw目录下看到demo.default.so文件.
打包system image :
make snod
HAL层书写完毕