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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Android应用程序访问linux驱动第二步:实现并测试hardware层

發(fā)布時間:2024/8/1 linux 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android应用程序访问linux驱动第二步:实现并测试hardware层 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

不管是出于什么樣地考慮,android系統(tǒng)終究是提供了hardware層來封裝了對Linux的驅(qū)動的訪問,同時為上層提供了一個統(tǒng)一的硬件接口和硬件形態(tài)。

一.Hardware概述

在Hardware層中的一個模塊中,主要設(shè)計一下三個結(jié)構(gòu):

  • struct hw_module_t
  • struct hw_module_methods_t
  • struct hw_device_t
    這三個結(jié)構(gòu)體的關(guān)系是這樣的:我們在上層訪問linux驅(qū)動時,需要首先獲得hardware層的對應(yīng)的module,使用hw_get_module()方法實現(xiàn),這個函數(shù)通過給定模塊的ID來尋找硬件模塊的動態(tài)鏈接庫,找到后使用load()函數(shù)打開這個庫,并通過一個固定的符號:HAL_MODULE_INFO_SYM尋找hw_module_t結(jié)構(gòu)體,我們會在hw_module_t結(jié)構(gòu)體中會有一個methods屬性,指向hw_module_methods_t結(jié)構(gòu)體,這個結(jié)構(gòu)體中會提供一個open方法用來打開模塊,在open的參數(shù)中會傳入一個hw_device_t**的數(shù)據(jù)結(jié)構(gòu),這樣我們就可以把對模塊的操作函數(shù)等數(shù)據(jù)保存在這個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了。
    這個思路很重要,后面我們測試我們的HAL層module的時候,就需要上面的代碼。
    整個過程可用一張圖展示:
  • 二.使用Android HAL規(guī)范封裝對Linux驅(qū)動的訪問

    在hardware/libhardware/modules新建hellotest目錄,添加兩個文件:hellotest.c 和 Android.mk

    1.hellotest.c

    這個文件把對linux驅(qū)動的訪問封裝成了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ū)動的接口*/ 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 }; /*模塊實例變量*/ 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; }

    這個程序就是把我們讀寫/dev/hello的代碼做了一次封裝而已,經(jīng)過封裝以后,我們有了hw_module_t,hw_module_methods_t,hw_device_t這三個結(jié)構(gòu)體。正因為它如此規(guī)范,所以上層才可以按照這種規(guī)范的格式獲取的hw_module_t結(jié)構(gòu)體,進而使用hw_module_methods_t中的open函數(shù)獲取hw_device_t結(jié)構(gòu)體,然后使用hw_device_t結(jié)構(gòu)體中的方法操作Linux驅(qū)動。

    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

    做好這三個文件以后,我們就可以編譯該模塊了。進入到hardware/libhardware/modules目錄,執(zhí)行mm命令進行編譯,編譯后的文件如圖所示:

    三.寫測試代碼

    封裝好了我們的HAL層module以后,我希望立刻測試它是否正確運行,下面我們將寫一個簡單的C程序,用來測試module是否正確工作。
    首先在hardware/libhardware/tests/目錄下新建一個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ū)動交互。注意,驅(qū)動的打開是在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)

    然后進入到該目錄執(zhí)行mm命令進行編譯,編譯后生成文件如圖所示:

    四.測試

    1.把生成的hellotest.default.so文件拷貝到android設(shè)備的/system/lib/hw目錄下,這需要root權(quán)限,沒有root權(quán)限請自行解決。
    2.獲得root權(quán)限后會提示/system是一個只讀文件系統(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é)實現(xiàn)的hello.ko驅(qū)動: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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。