2014年12月12日 星期五

Android HAL 初始入門

hw_module_t struct Reference

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
  1.   
  2. /*定义模块ID*/  
  3. #define HELLO_HARDWARE_MODULE_ID "hello"  
  4.   
  5. /*硬件模块结构体*/  
  6. struct hello_module_t {  
  7.     struct hw_module_t common;  
  8. };  
  9.   
  10. /*硬件接口结构体*/  
  11. struct hello_device_t {  
  12.     struct hw_device_t common;  
  13.     int fd;  
  14.     int (*set_val)(struct hello_device_t* dev, int val);  
  15.     int (*get_val)(struct hello_device_t* dev, int* val);  
  16. }; 




hello.c
  1.   
  2. /*模块实例变量*/  
  3. struct hello_module_t HAL_MODULE_INFO_SYM = {  
  4.     common: {  
  5.         tag: HARDWARE_MODULE_TAG,  
  6.         version_major: 1,  
  7.         version_minor: 0,  
  8.         id: HELLO_HARDWARE_MODULE_ID,  
  9.         name: MODULE_NAME,  
  10.         author: MODULE_AUTHOR,  
  11.         methods: &hello_module_methods,  
  12.     }  
  13. }; 

  14. static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {  
  15.     struct hello_device_t* dev;
  16.     dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));  
  17.       
  18.     if(!dev) {  
  19.         LOGE("Hello Stub: failed to alloc space");  
  20.         return -EFAULT;  
  21.     }  
  22.   
  23.     memset(dev, 0, sizeof(struct hello_device_t));  
  24.     dev->common.tag = HARDWARE_DEVICE_TAG;  
  25.     dev->common.version = 0;  
  26.     dev->common.module = (hw_module_t*)module;  
  27.     dev->common.close = hello_device_close;  
  28.     dev->set_val = hello_set_val;
  29.     dev->get_val = hello_get_val;  
  30.   
  31.     if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {  
  32.         LOGE("Hello Stub: open failed /dev/hello -- %s.", strerror(errno));
  33.         free(dev);  
  34.         return -EFAULT;  
  35.     }  
  36.   
  37.     *device = &(dev->common);  
  38.     LOGI("Hello Stub: open /dev/hello successfully.");  
  39.   
  40.     return 0;  
  41. }  





沒有留言:

張貼留言