1、hello.h
/*定義模塊ID*/
#define HELLO_HARDWARE_MODULE_ID "hello"
/*硬件模塊結構體*/
struct hello_module_t {
??? struct hw_module_t common;
};
/*硬件接口結構體*/
struct hello_device_t {
??? struct hw_device_t common;
??? int fd;
??? int (*set_val)(struct hello_device_t* dev, int val);
??? int (*get_val)(struct hello_device_t* dev, int* val);
};
2.hello.c
/*模塊實例變量*/
struct hello_module_t HAL_MODULE_INFO_SYM = {
??? 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 struct hw_module_methods_t hello_module_methods = {
??? open: hello_device_open
};
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));
? ? if(!dev) {
?? ? ?? LOGE("Hello Stub: failed to alloc space");
?? ? ?? return -EFAULT;
??? }
? ? 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;
??? if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
?????? LOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);
??????? return -EFAULT;
??? }
??? *device = &(dev->common);
??? LOGI("Hello Stub: open /dev/hello successfully.");
??? return 0;
}
在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)
權限修改:
system/core/rootdir 目錄,uevent.rc 文件,往里面添加一行:
/dev/hello 0666 root root