Hardware概述
不管是出于什么樣地考慮,android系統(tǒng)終究是提供了hardware層來(lái)封裝了對(duì)Linux的驅(qū)動(dòng)的訪問(wèn),同時(shí)為上層提供了一個(gè)統(tǒng)一的硬件接口和硬件形態(tài)。
一.Hardware概述
在Hardware層中的一個(gè)模塊中,主要設(shè)計(jì)一下三個(gè)結(jié)構(gòu):
struct hw_module_t struct hw_module_methods_t struct hw_device_t這三個(gè)結(jié)構(gòu)體的關(guān)系是這樣的:我們?cè)谏蠈釉L問(wèn)linux驅(qū)動(dòng)時(shí),需要首先獲得hardware層的對(duì)應(yīng)的module,使用hw_get_module()方法實(shí)現(xiàn),這個(gè)函數(shù)通過(guò)給定模塊的ID來(lái)尋找硬件模塊的動(dòng)態(tài)鏈接庫(kù),找到后使用load()函數(shù)打開(kāi)這個(gè)庫(kù),并通過(guò)一個(gè)固定的符號(hào):HAL_MODULE_INFO_SYM尋找hw_module_t結(jié)構(gòu)體,我們會(huì)在hw_module_t結(jié)構(gòu)體中會(huì)有一個(gè)methods屬性,指向hw_module_methods_t結(jié)構(gòu)體,這個(gè)結(jié)構(gòu)體中會(huì)提供一個(gè)open方法用來(lái)打開(kāi)模塊,在open的參數(shù)中會(huì)傳入一個(gè)hw_device_t**的數(shù)據(jù)結(jié)構(gòu),這樣我們就可以把對(duì)模塊的操作函數(shù)等數(shù)據(jù)保存在這個(gè)hw_device_t結(jié)構(gòu)中,這樣,這樣用戶可以通過(guò)hw_device_t來(lái)和模塊交互。
具體的說(shuō),我們?cè)谏蠈涌梢赃@樣調(diào)用HAL層的module:
xxx_module_t *module;
hw_get_module(XXXID,(struct hw_module_t **)&module);
以上兩步我們就獲得了對(duì)應(yīng)ID的 hw_module_t結(jié)構(gòu)體。
struct xxx_device_t *device;
module->methods->open(module,XXXID,(struct hw_device_t **)&device);
這樣我們就獲得了hw_device_t結(jié)構(gòu)體,通過(guò)hw_device_t結(jié)構(gòu)體我們就可以訪問(wèn)HAL層對(duì)應(yīng)的module了。
這個(gè)思路很重要,后面我們測(cè)試我們的HAL層module的時(shí)候,就需要上面的代碼。
整個(gè)過(guò)程可用一張圖展示:
二.使用Android?HAL規(guī)范封裝對(duì)Linux驅(qū)動(dòng)的訪問(wèn)
在hardware/libhardware/modules新建hellotest目錄,添加兩個(gè)文件:hellotest.c 和 Android.mk
1.hellotest.c
這個(gè)文件把對(duì)linux驅(qū)動(dòng)的訪問(wèn)封裝成了Android要求的格式。
?| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 | #include <hardware hardware.h="">? #include <hardware hellotest.h="">? #include <fcntl.h>? #include <errno.h>? #include <cutils log.h="">? #include <cutils atomic.h="">? #define DEVICE_NAME "/dev/hello"#define MODULE_NAME "HelloTest"?#define MODULE_AUTHOR "jinwei"#define LOG_TAG "HelloTest"?/* 定義LOG */#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOG_TAG, __VA_ARGS__)#define LOGI(...) __android_log_print(ANDROID_LOG_INFO? , LOG_TAG, __VA_ARGS__)#define LOGW(...) __android_log_print(ANDROID_LOG_WARN? , LOG_TAG, __VA_ARGS__)#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR? , LOG_TAG, __VA_ARGS__)/*打開(kāi)和關(guān)閉設(shè)備的方法*/?static int hellotest_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);? static int hellotest_device_close(struct hw_device_t* device);? /*讀寫(xiě)linux驅(qū)動(dòng)的接口*/?static int hellotest_write_string(struct hellotest_device_t* dev, char * str);? static int hellotest_read_string(struct hellotest_device_t* dev, char **str);? /*模塊方法結(jié)構(gòu)體*/?static struct hw_module_methods_t hellotest_module_methods = {? ????open: hellotest_device_open? };? /*模塊實(shí)例變量*/?struct hellotest_module_t HAL_MODULE_INFO_SYM = {? ????common: {? ????????tag: HARDWARE_MODULE_TAG,? ????????version_major: 1,? ????????version_minor: 0,? ????????id: HELLOTEST_HARDWARE_MODULE_ID,? ????????name: MODULE_NAME,? ????????author: MODULE_AUTHOR,? ????????methods: &hellotest_module_methods,? ????}? };? static int hellotest_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {? ????struct hellotest_device_t* dev;????dev = (struct hellotest_device_t*)malloc(sizeof(struct hellotest_device_t));? ????if(!dev) {? ????????LOGE("HelloTest: failed to alloc space");? ????????return -EFAULT;? ????}? ????memset(dev, 0, sizeof(struct hellotest_device_t));? ????dev->common.tag = HARDWARE_DEVICE_TAG;? ????dev->common.version = 0;? ????dev->common.module = (hw_module_t*)module;? ????dev->common.close = hellotest_device_close;? ????dev->write_string = hellotest_write_string;????dev->read_string = hellotest_read_string;? ????if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {? ????????LOGE("HelloTest: open /dev/hello fail-- %s.", strerror(errno));free(dev);? ????????return -EFAULT;? ????}? ????*device = &(dev->common);? ?????LOGI("HelloTest: open /dev/hello successfully.");? ????return 0;? }? static int hellotest_device_close(struct hw_device_t* device) {? ????struct hellotest_device_t* hello_device = (struct hellotest_device_t*)device;? ????if(hello_device) {? ????????close(hello_device->fd);? ????????free(hello_device);? ????}? ????return 0;? }? static int hellotest_write_string(struct hellotest_device_t* dev,char * str) {? ????LOGI("HelloTest:write string: %s", str);? ????write(dev->fd, str, sizeof(str));? ????return 0;? }? static int hellotest_read_string(struct hellotest_device_t* dev, char ** str) {? ?????LOGI("HelloTest:read string: %s", *str);? ????read(dev->fd, *str, sizeof(*str));? ????return 0;? }? </cutils></cutils></errno.h></fcntl.h></hardware></hardware> |
這個(gè)程序就是把我們讀寫(xiě)/dev/hello的代碼做了一次封裝而已,經(jīng)過(guò)封裝以后,我們有了hw_module_t,hw_module_methods_t,hw_device_t這三個(gè)結(jié)構(gòu)體。正因?yàn)樗绱艘?guī)范,所以上層才可以按照這種規(guī)范的格式獲取的hw_module_t結(jié)構(gòu)體,進(jìn)而使用hw_module_methods_t中的open函數(shù)獲取hw_device_t結(jié)構(gòu)體,然后使用hw_device_t結(jié)構(gòu)體中的方法操作Linux驅(qū)動(dòng)。
2.Android.mk
?| 1234567891011121314 | ??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 += \libcutils libutils liblogLOCAL_LDLIBS:=? -L$(SYSROOT)/usr/lib -llog ??LOCAL_SRC_FILES := hellotest.c??LOCAL_MODULE := hellotest.default??include $(BUILD_SHARED_LIBRARY) |
LOCAL_MODULE 的值為hellotest.default,注意,一定要加上default,不然使用hw_get_module函數(shù)找不到我們的HAL模塊。
然后在hardware/libhardware/include/hardware新建對(duì)應(yīng)的頭文件hellotest.h
3.hellotest.h
?| 12345678910111213141516171819202122232425 | #ifndef ANDROID_HELLO_TEST_H? #define ANDROID_HELLO_TEST_H? #include <hardware hardware.h="">? __BEGIN_DECLS? /*定義模塊ID,必須的,用與上層程序獲取該模塊*/?#define HELLOTEST_HARDWARE_MODULE_ID "hellotest"/*硬件模塊結(jié)構(gòu)體*/?struct hellotest_module_t {? ????struct hw_module_t common;? };? /*硬件接口結(jié)構(gòu)體*/?struct hellotest_device_t {? ????struct hw_device_t common;? ????int fd;? ????int (*write_string)(struct hellotest_device_t* dev, int val);? ????int (*read_string)(struct hellotest_device_t* dev, int* val);? };? __END_DECLS? #endif? </hardware> |
做好這三個(gè)文件以后,我們就可以編譯該模塊了。進(jìn)入到hardware/libhardware/modules目錄,執(zhí)行mm命令進(jìn)行編譯,編譯后的文件如圖所示:
三.寫(xiě)測(cè)試代碼
封裝好了我們的HAL層module以后,我希望立刻測(cè)試它是否正確運(yùn)行,下面我們將寫(xiě)一個(gè)簡(jiǎn)單的C程序,用來(lái)測(cè)試module是否正確工作。
首先在hardware/libhardware/tests/目錄下新建一個(gè)testhellotest目錄,新增test.c和Android.mk文件:
1.test.c
?| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 | #include <hardware hardware.h="">? #include <hardware hellotest.h="">? #include <fcntl.h>#include <stdio.h>struct hw_module_t * module;struct hw_device_t * device;int main(){????char *read_str;????char *write_str="nihao";????read_str = malloc(100);????printf("----begin main------\n");????if(hw_get_module(HELLOTEST_HARDWARE_MODULE_ID,(struct hw_module_t const **)&module)==0){????????printf("get module sucess\n");????}else{????????printf("get module fail\n");????????return -1;????}????if(module->methods->open(module,HELLOTEST_HARDWARE_MODULE_ID,(struct hw_device_t const**)&device)==0){????????printf("open module sucess\n");????}else{????????printf("open module error\n");????????return -2;????}????struct hellotest_device_t* dev = (struct hellotest_device_t *)device;????dev->read_string(dev,&read_str);????if(read_str == NULL){????????printf("read error");????}else{????????printf("read data: %s\n",read_str);????}????dev->write_string(dev,write_str);????printf("write data: %s\n",write_str);????dev->read_string(dev,&read_str);????if(read_str == NULL){????????printf("read error");????}else{????????printf("read data: %s\n",read_str);????}????printf("----end main------\n");return 0;}</stdio.h></fcntl.h></hardware></hardware> |
測(cè)試的流程正如文章開(kāi)頭描述的那樣,首先使用hw_get_module函數(shù)獲得hw_module_t結(jié)構(gòu)體,然后調(diào)用hw_module_methods_t結(jié)構(gòu)體中的open方法過(guò)得hw_device_t結(jié)構(gòu)體,然后使用hw_device_t結(jié)構(gòu)體中的read_string和write_string方法與linux驅(qū)動(dòng)交互。注意,驅(qū)動(dòng)的打開(kāi)是在hw_module_methods_t結(jié)構(gòu)體中的open方法中做的。
2.Android.mk
?| 12345678910111213 | ??LOCAL_PATH := $(call my-dir)??include $(CLEAR_VARS)??LOCAL_MODULE_TAGS := optional??LOCAL_MODULE := my_testLOCAL_LDLIBS:=? -lhardware??LOCAL_SRC_FILES := $(call all-subdir-c-files)??include $(BUILD_EXECUTABLE) |
然后進(jìn)入到該目錄執(zhí)行mm命令進(jìn)行編譯,編譯后生成文件如圖所示:
四.測(cè)試
1.把生成的hellotest.default.so文件拷貝到android設(shè)備的/system/lib/hw目錄下,這需要root權(quán)限,沒(méi)有root權(quán)限請(qǐng)自行解決。
2.獲得root權(quán)限后會(huì)提示/system是一個(gè)只讀文件系統(tǒng),所以需要重新掛載為可讀可寫(xiě)的文件系統(tǒng),執(zhí)行mount -o remount,rw /system 即可。
3.修改hellotest.default.so文件的的權(quán)限為644,執(zhí)行chmod 644 /system/lib/dw/hellotest.default.so
4.裝載上一節(jié)實(shí)現(xiàn)的hello.ko驅(qū)動(dòng):insmod hello.ko
5.把生成的my_test可執(zhí)行文件拷貝的android設(shè)備上,建議push my_test /data
6.給my_test文件添加可執(zhí)行權(quán)限. chmod +x my_test
7.執(zhí)行my_test。 ./my_test
打印如下:
—-begin main——
get module sucess
open module sucess
read data: hell
write data: nihao
read data: niha
—-end main——
可見(jiàn),測(cè)試成功。
如果有問(wèn)題,可以使用logcat看下HAL層的打印,看看問(wèn)題出在什么地方。
總結(jié)
以上是生活随笔為你收集整理的Hardware概述的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C语言直线拟合函数
- 下一篇: 小球碰撞(理解ing)