日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android hardware解析

發布時間:2024/8/1 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android hardware解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、HAL層的hardware module主要實現文件為:

hardware/libhardware/hareware.c hardware/libhardware/include/hardware/hardware.h

Hareware.c中的內容在此就不做解釋,可自行學習。

2、hardware.c中三個重要的結構體:

struct hw_module_t; //描述一個硬件模塊 struct hw_module_methods_t;//定義了操作設備的方法,只有一個open函數 struct hw_device_t;//表示一個硬件設備,存儲了各種硬件設備的公共屬性和方法 3、三個結構的關系圖:

4、三個結構體的描述:

typedef struct hw_device_t {uint32_t tag; /** tag must be initialized to HARDWARE_DEVICE_TAG */ 標識符uint32_t version; /** version number for hw_device_t */ 版本號struct hw_module_t* module; /** reference to the module this device belongs to */ 該硬件屬于哪個moduleuint32_t reserved[12]; /** padding reserved for future use */int (*close)(struct hw_device_t* device); /** Close this device */ 關閉設備操作 } hw_device_t; typedef struct hw_module_t {uint32_t tag; /** tag must be initialized to HARDWARE_MODULE_TAG */ 標識符uint16_t version_major; /** major version number for the module */ 主版本號uint16_t version_minor; /** minor version number of the module */ 次版本號const char *id; /** Identifier of module */ id,決定了庫的文件名const char *name; /** Name of this module */ 模塊的名字const char *author; /** Author/owner/implementor of the module */ 模塊作者struct hw_module_methods_t* methods; /** Modules methods */ 模塊的操作方法void* dso; /** module's dso */uint32_t reserved[32-7]; /** padding to 128 bytes, reserved for future use */ } hw_module_t; typedef struct hw_module_methods_t {int (*open)(const struct hw_module_t* module, const char* id,struct hw_device_t** device); } hw_module_methods_t;
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解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。