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.
找這個關鍵字一個HelloWorld
http://blog.csdn.net/luoshengyang/article/details/6573809
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);
- };
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 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: open failed /dev/hello -- %s.", strerror(errno));
- free(dev);
- return -EFAULT;
- }
- *device = &(dev->common);
- LOGI("Hello Stub: open /dev/hello successfully.");
- return 0;
- }
沒有留言:
張貼留言