android hardware解析
生活随笔
收集整理的這篇文章主要介紹了
android hardware解析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、HAL層的hardware module主要實現文件為:
Hareware.c中的內容在此就不做解釋,可自行學習。
2、hardware.c中三個重要的結構體:
struct hw_module_t; //描述一個硬件模塊 struct hw_module_methods_t;//定義了操作設備的方法,只有一個open函數 struct hw_device_t;//表示一個硬件設備,存儲了各種硬件設備的公共屬性和方法 3、三個結構的關系圖:
4、三個結構體的描述:
5、
舉個栗子:
JNI層文件(frameworks/base/services/jni)需包含一下頭文件
?hello.h文件
#ifndef ANDROID_HELLO_INTERFACE_H #define ANDROID_HELLO_INTERFACE_H #include <hardware/hardware.h>__BEGIN_DECLS/*定義模塊ID,即名字*/ #define HELLO_LED_HARDWARE_MODULE_ID "hello_led"/*硬件模塊結構體*/ struct hello_module_t {struct hw_module_t common; };/*硬件接口結構體,此提供給JNI層使用*/ 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); };__END_DECLS#endifhello.c文件#define LOG_TAG "HelloLedStub"#include <hardware/hardware.h> #include <hardware/hello_led.h> #include <fcntl.h> #include <errno.h> #include <cutils/log.h> #include <cutils/atomic.h>#define DEVICE_NAME "/dev/hello" #define MODULE_NAME "Hello" #define MODULE_AUTHOR "yanlei.liu@roiland.com"/*設備打開和關閉接口*/ static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device); static int hello_device_close(struct hw_device_t* device);/*設備訪問接口*/ static int hello_set_val(struct hello_device_t* dev, int val); static int hello_get_val(struct hello_device_t* dev, int* val);/*模塊方法表*/ static struct hw_module_methods_t hello_module_methods = {open: hello_device_open };/*模塊實例變量*/ struct hello_module_t HAL_MODULE_INFO_SYM = {common: {tag: HARDWARE_MODULE_TAG,version_major: 1,version_minor: 0,id: HELLO_LED_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) {ALOGE("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) {ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);return -EFAULT;}*device = &(dev->common);ALOGE("Hello Stub: open /dev/hello successfully.");return 0; }static int hello_device_close(struct hw_device_t* device) {struct hello_device_t* hello_device = (struct hello_device_t*)device;if(hello_device) {close(hello_device->fd);free(hello_device);}return 0; }static int hello_set_val(struct hello_device_t* dev, int val) {ALOGE("Hello Stub: set value %d to device.", val);write(dev->fd, &val, sizeof(val));return 0; }static int hello_get_val(struct hello_device_t* dev, int* val) {if(!val) {ALOGE("Hello Stub: error val pointer");return -EFAULT;}read(dev->fd, val, sizeof(*val));ALOGE("Hello Stub: get value %d from device", *val);return 0; } /* static int hello_hw_ioctl(struct file *file, unsigned int cmd, unsigned long arg){ALOGE("hello_hw_ioctl: set cmd %d to device.", cmd);hello_ioctl(file, cmd, arg);return 0; } */
?
參考:
?
http://blog.chinaunix.net/uid-22030783-id-3056757.html
http://blog.csdn.net/luoshengyang/article/details/6575988
http://blog.csdn.net/luoshengyang/article/details/6573809
總結
以上是生活随笔為你收集整理的android hardware解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cmd 、java获取硬盘的序列号(se
- 下一篇: IT 技能图谱