Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 應用訪問 Linux 驅動(二):實作並測試 HAL 層

編輯:關於 Android 程式設計

無論出於什麼考量,Android 終究提供了 hardware 層來封裝對 Linux 驅動的訪問,同時向上層提供統一的硬體介面與形態。

一、Hardware 層概述

Hardware 層的每個模組主要圍繞三個結構:

struct hw_module_t
struct hw_module_methods_t
struct hw_device_t

它們的關係是這樣的:上層訪問 Linux 驅動時,先用 hw_get_module() 依模組 ID 找到對應的動態連結庫;找到後用 load() 打開這個庫,並透過固定符號 HAL_MODULE_INFO_SYM 取得 hw_module_t 結構體。在 hw_module_t 中有一個 methods 屬性指向 hw_module_methods_t,其中提供了 open 方法用來打開模組,而 open 的參數帶有一個 hw_device_t**。於是對模組的操作函式等資料就可以保存在這個 hw_device_t 中,使用者之後透過它與模組互動。

具體呼叫方式如下:

xxx_module_t *module;
hw_get_module(XXXID, (struct hw_module_t **)&module);   // 取得 hw_module_t

struct xxx_device_t *device;
module->methods->open(module, XXXID, (struct hw_device_t **)&device);  // 取得 hw_device_t

這個思路很關鍵,後面測試自己寫的 HAL 模組時,用的就是這段程式碼。

二、用 Android HAL 規範封裝對 Linux 驅動的訪問

在 hardware/libhardware/modules 下新建 hellotest 目錄,加入兩個檔案:hellotest.c 與 Android.mk。

1. hellotest.c

這個檔案把對 Linux 驅動的讀寫訪問包裝成 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"

#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__)

/* 裝置的開與關 */
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 驅動的介面 */
static int hellotest_write_string(struct hellotest_device_t* dev, char * str);
static int hellotest_read_string(struct hellotest_device_t* dev, char **str);

/* 模組方法結構體 */
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 =
            (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 的程式碼做了一次包裝,包裝完就具備了 hw_module_t、hw_module_methods_t、hw_device_t 三個結構體。正因為足夠規範,上層才能依同樣的格式取得 hw_module_t,進而透過 methods 中的 open 拿到 hw_device_t,最後用它操作 Linux 驅動。

2. Android.mk

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 liblog
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_SRC_FILES := hellotest.c
LOCAL_MODULE := hellotest.default

include $(BUILD_SHARED_LIBRARY)

注意:LOCAL_MODULE 一定要加上 .default 這個後綴,否則 hw_get_module 找不到你的模組。

接著在 hardware/libhardware/include/hardware 建立對應的標頭檔。

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"

/* 硬體模組結構體 */
struct hellotest_module_t {
    struct hw_module_t common;
};

/* 硬體介面結構體 */
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 執行 mm,結果如下:

三、撰寫測試程式

封裝好模組後,當然要立刻驗證它能否正常運作。在 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 = malloc(100);
    char *write_str = "nihao";

    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);
    printf("read data: %s\n", read_str == NULL ? "read error" : read_str);

    dev->write_string(dev, write_str);
    printf("write data: %s\n", write_str);

    dev->read_string(dev, &read_str);
    printf("read data: %s\n", read_str == NULL ? "read error" : read_str);

    printf("----end main------\n");
    return 0;
}

流程與開頭描述的完全一致:hw_get_module 取得 hw_module_t → 呼叫 methods 中的 open 取得 hw_device_t → 透過其中的 read_string 與 write_string 和驅動互動。請特別注意:真正 open 裝置的動作,是發生在 methods 的 open 裡面的。

2. Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := my_test
LOCAL_LDLIBS := -lhardware
LOCAL_SRC_FILES := $(call all-subdir-c-files)

include $(BUILD_EXECUTABLE)

進入該目錄執行 mm,編譯產物如下:

四、實際測試步驟

  1. 把 hellotest.default.so 複製到裝置的 /system/lib/hw,這需要 root 權限
  2. 取得 root 後通常會看到 /system 是唯讀,需重新掛載為可讀寫:mount -o remount,rw /system
  3. 調整權限:chmod 644 /system/lib/hw/hellotest.default.so
  4. 裝載上一節完成的驅動:insmod hello.ko
  5. 把 my_test 推到裝置,建議放 /data:push my_test /data
  6. 加上執行權限:chmod +x my_test
  7. 執行:./my_test

輸出如下:

----begin main------
get module sucess
open module sucess
read data: hell
write data: nihao
read data: niha
----end main------

測試成功。若結果不如預期,可以用 logcat 看 HAL 層的輸出,就能定位問題落在哪一層。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved