這是一個系列文章:
該系列能對android的從下到上的各層有一個簡單的設計钮追,麻雀雖小,五臟俱全。enjoy now醉途!
在前面,我們添加里成功了驅動層的代碼,下來接著添加HAL層的代碼.讓我們的用戶空間的代碼也能使用到添加的新的虛擬寄存器設備.
hardware
----libhardware/include/hardware/freg.h
----libhardware/modules/freg
|---freg.cpp
|---Android.mk
freg.h:
#ifndef ANDROID_FREG_INTERFACE_H
#define ANDROID_FREG_INTERFACE_H
#include <hardware/hardware.h>
__BEGIN_DECLS
/**
* The id of this module
*/
#define FREG_HARDWARE_MODULE_ID "freg"
/**
* The id of this device
*/
#define FREG_HARDWARE_DEVICE_ID "freg"
struct freg_module_t {
struct hw_module_t common;
};
struct freg_device_t {
struct hw_device_t common;
int fd;
int (*set_val)(struct freg_device_t* dev, int val);
int (*get_val)(struct freg_device_t* dev, int* val);
};
__END_DECLS
#endif
freg.h按照android對HAL層規(guī)范的要求代芜,分別定義模塊ID指厌、模塊結構體以及硬件接口結構體.
freg.h:
#define LOG_TAG "FregHALStub"
#include <hardware/hardware.h>
#include <hardware/freg.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/freg"
#define MODULE_NAME "Freg"
#define MODULE_AUTHOR "shyluo@gmail.com"
static int freg_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device);
static int freg_device_close(struct hw_device_t* device);
static int freg_set_val(struct freg_device_t* dev, int val);
static int freg_get_val(struct freg_device_t* dev, int* val);
static struct hw_module_methods_t freg_module_methods = {
open: freg_device_open
};
struct freg_module_t HAL_MODULE_INFO_SYM = {
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: FREG_HARDWARE_MODULE_ID,
name: MODULE_NAME,
author: MODULE_AUTHOR,
methods: &freg_module_methods,
}
};
static int freg_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device) {
if(!strcmp(id, FREG_HARDWARE_DEVICE_ID)) {
struct freg_device_t* dev;
dev = (struct freg_device_t*)malloc(sizeof(struct freg_device_t));
if(!dev) {
ALOGE("Failed to alloc space for freg_device_t.");
return -EFAULT;
}
memset(dev, 0, sizeof(struct freg_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = freg_device_close;
dev->set_val = freg_set_val;
dev->get_val = freg_get_val;
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
ALOGE("Failed to open device file /dev/freg -- %s.", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
ALOGI("Open device file /dev/freg successfully.");
return 0;
}
return -EFAULT;
}
static int freg_device_close(struct hw_device_t* device) {
struct freg_device_t* freg_device = (struct freg_device_t*)device;
if(freg_device) {
close(freg_device->fd);
free(freg_device);
}
return 0;
}
static int freg_set_val(struct freg_device_t* dev, int val) {
if(!dev) {
ALOGE("Null dev pointer.");
return -EFAULT;
}
ALOGI("Set value %d to device file /dev/freg.", val);
write(dev->fd, &val, sizeof(val));
return 0;
}
static int freg_get_val(struct freg_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/freg.", *val);
return 0;
}
freg.cpp相對羅大神blog的修改是include文件,由于free函數(shù)和malloc方法的頭文件有變化,還有l(wèi)og系統(tǒng),LOGE->ALOGE,LOGI->ALOGI.這兩個改動是依據(jù)代碼編譯報錯修改而來.
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 := freg.cpp
LOCAL_MODULE := freg.default
include $(BUILD_SHARED_LIBRARY)
編譯該模塊:
mmm hardware/libhardware/modules/freg
這樣就會在out/target/product/generic/system/lib/hw
目錄下看到freg.default.so文件.
打包system image :make snod
HAL層書寫完畢.