編輯:關於 Android 程式設計
無論出於什麼考量,Android 終究提供了 hardware 層來封裝對 Linux 驅動的訪問,同時向上層提供統一的硬體介面與形態。
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 模組時,用的就是這段程式碼。

在 hardware/libhardware/modules 下新建 hellotest 目錄,加入兩個檔案:hellotest.c 與 Android.mk。
這個檔案把對 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 驅動。
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 建立對應的標頭檔。
#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。
#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 裡面的。
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,編譯產物如下:

hellotest.default.so 複製到裝置的 /system/lib/hw,這需要 root 權限/system 是唯讀,需重新掛載為可讀寫:mount -o remount,rw /systemchmod 644 /system/lib/hw/hellotest.default.soinsmod hello.komy_test 推到裝置,建議放 /data:push my_test /datachmod +x my_test./my_test輸出如下:
----begin main------ get module sucess open module sucess read data: hell write data: nihao read data: niha ----end main------
測試成功。若結果不如預期,可以用 logcat 看 HAL 層的輸出,就能定位問題落在哪一層。
手機 MPT 模式和 USB 調試模式有什麼區別?
在手機與電腦的連接方式清單裡,常會看到一個叫 MPT 的選項。它到底是什麼?和開發者熟悉的 USB 調試模式又有何差異? 手機 MPT 模式是什麼? MPT 模式
Android 自訂 View:柱狀圖繪製思路
自訂 View 可以實現各種統計圖表。本文以柱狀圖為例,說明如何繪製單柱狀圖與雙柱狀圖,並加入座標軸、刻度與數值標籤。 整體效果 範例中有兩幅圖。第一幅是單柱狀圖,同一
Android 高仿雅虎天氣(二):代碼結構深度解析
本實例共使用了 6 個庫代碼和 1 個主工程代碼,下面逐一拆解。 一、6 個庫代碼 其中: ① MenuDrawer、ViewPagerIndicator、Showca
Glide 快速上手:對象池原理、依賴配置與 Volley/OkHttp 集成
Glide 是 Android 平台上快速而高效的开源多媒體資源管理庫,提供多媒體文件的壓縮、內存與磁盤緩存、資源池等能力。它支持獲取、解壓與展示圖片、視頻和 GIF,並