Android应用程序访问linux驱动第二步:实现并测试hardware层
不管是出于什么樣地考慮,android系統(tǒng)終究是提供了hardware層來封裝了對Linux的驅(qū)動(dòng)的訪問,同時(shí)為上層提供了一個(gè)統(tǒng)一的硬件接口和硬件形態(tài)。
一.Hardware概述
在Hardware層中的一個(gè)模塊中,主要設(shè)計(jì)一下三個(gè)結(jié)構(gòu):
這三個(gè)結(jié)構(gòu)體的關(guān)系是這樣的:我們在上層訪問linux驅(qū)動(dòng)時(shí),需要首先獲得hardware層的對應(yīng)的module,使用hw_get_module()方法實(shí)現(xiàn),這個(gè)函數(shù)通過給定模塊的ID來尋找硬件模塊的動(dòng)態(tài)鏈接庫,找到后使用load()函數(shù)打開這個(gè)庫,并通過一個(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方法用來打開模塊,在open的參數(shù)中會(huì)傳入一個(gè)hw_device_t**的數(shù)據(jù)結(jié)構(gòu),這樣我們就可以把對模塊的操作函數(shù)等數(shù)據(jù)保存在這個(gè)hw_device_t結(jié)構(gòu)中,這樣,這樣用戶可以通過hw_device_t來和模塊交互。
具體的說,我們在上層可以這樣調(diào)用HAL層的module:
xxx_module_t *module;
hw_get_module(XXXID,(struct hw_module_t **)&module);
以上兩步我們就獲得了對應(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)體,通過hw_device_t結(jié)構(gòu)體我們就可以訪問HAL層對應(yīng)的module了。
這個(gè)思路很重要,后面我們測試我們的HAL層module的時(shí)候,就需要上面的代碼。
整個(gè)過程可用一張圖展示:
二.使用Android HAL規(guī)范封裝對Linux驅(qū)動(dòng)的訪問
在hardware/libhardware/modules新建hellotest目錄,添加兩個(gè)文件:hellotest.c 和 Android.mk
1.hellotest.c
這個(gè)文件把對linux驅(qū)動(dòng)的訪問封裝成了Android要求的格式。
#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__)/*打開和關(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); /*讀寫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; }這個(gè)程序就是把我們讀寫/dev/hello的代碼做了一次封裝而已,經(jīng)過封裝以后,我們有了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
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_PRELINK_MODULE := falseLOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hwLOCAL_SHARED_LIBRARIES += \libcutils libutils liblogLOCAL_LDLIBS:= -L$(SYSROOT)/usr/lib -llog LOCAL_SRC_FILES := hellotest.cLOCAL_MODULE := hellotest.defaultinclude $(BUILD_SHARED_LIBRARY)LOCAL_MODULE 的值為hellotest.default,注意,一定要加上default,不然使用hw_get_module函數(shù)找不到我們的HAL模塊。
然后在hardware/libhardware/include/hardware新建對應(yīng)的頭文件hellotest.h
3.hellotest.h
#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, char *str); int (*read_string)(struct hellotest_device_t* dev, char ** str); }; __END_DECLS #endif做好這三個(gè)文件以后,我們就可以編譯該模塊了。進(jìn)入到hardware/libhardware/modules目錄,執(zhí)行mm命令進(jìn)行編譯,編譯后的文件如圖所示:
三.寫測試代碼
封裝好了我們的HAL層module以后,我希望立刻測試它是否正確運(yùn)行,下面我們將寫一個(gè)簡單的C程序,用來測試module是否正確工作。
首先在hardware/libhardware/tests/目錄下新建一個(gè)testhellotest目錄,新增test.c和Android.mk文件:
1.test.c
#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; }測試的流程正如文章開頭描述的那樣,首先使用hw_get_module函數(shù)獲得hw_module_t結(jié)構(gòu)體,然后調(diào)用hw_module_methods_t結(jié)構(gòu)體中的open方法過得hw_device_t結(jié)構(gòu)體,然后使用hw_device_t結(jié)構(gòu)體中的read_string和write_string方法與linux驅(qū)動(dòng)交互。注意,驅(qū)動(dòng)的打開是在hw_module_methods_t結(jié)構(gòu)體中的open方法中做的。
2.Android.mk
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_MODULE := my_testLOCAL_LDLIBS:= -lhardwareLOCAL_SRC_FILES := $(call all-subdir-c-files)include $(BUILD_EXECUTABLE)然后進(jìn)入到該目錄執(zhí)行mm命令進(jìn)行編譯,編譯后生成文件如圖所示:
四.測試
1.把生成的hellotest.default.so文件拷貝到android設(shè)備的/system/lib/hw目錄下,這需要root權(quán)限,沒有root權(quán)限請自行解決。
2.獲得root權(quán)限后會(huì)提示/system是一個(gè)只讀文件系統(tǒng),所以需要重新掛載為可讀可寫的文件系統(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: hello
write data: nihao
read data: nihao
—-end main——
可見,測試成功。
如果有問題,可以使用logcat看下HAL層的打印,看看問題出在什么地方。
總結(jié)
以上是生活随笔為你收集整理的Android应用程序访问linux驱动第二步:实现并测试hardware层的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小球运动及碰撞3D仿真模型
- 下一篇: 数据库索引详细介绍