編輯:關於 Android 程式設計
Android 標準的硬體驅動架構分成兩半:一半是跑在 Linux 核心裡的驅動程式,另一半是跑在使用者空間的硬體抽象層(HAL)。這樣切分有兩個好處——系統因此可以具備硬體無關性,同時也保護了部分廠商的核心實作不被輕易窺探。
前一篇已經完成核心側的驅動撰寫,這一篇接著在 HAL 層建立模組,讓上層應用能透過它與核心驅動溝通,完成硬體控制。
在 hardware/libhardware/include/hardware 目錄下新增 android_gpio.h:
#ifndef ANDROID_ANDROID_GPIO_INTERFACE_H
#define ANDROID_ANDROID_GPIO_INTERFACE_H
#include <hardware/hardware.h>
__BEGIN_DECLS
/* module ID */
#define ANDROID_GPIO_HARDWARE_MODULE_ID "android_gpio"
/* module struct */
struct android_gpio_module_t {
struct hw_module_t common;
};
/* interface struct */
struct android_gpio_device_t {
struct hw_device_t common;
int fd;
int (*set_val)(struct android_gpio_device_t* dev, int val);
int (*get_val)(struct android_gpio_device_t* dev, int* val);
};
__END_DECLS
#endif
其中 set_val 與 get_val 就是 HAL 向上層應用提供的 API 介面。
切到 hardware/libhardware/modules 目錄,新增 android_gpio 資料夾,在其中建立 android.c:
#include <hardware/hardware.h>
#include <hardware/android_gpio.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#define DEVICE_NAME "/dev/AdrIO"
#define MODULE_NAME "Android_gpio"
// open and close
static int android_gpio_device_open(const struct hw_module_t* module,
const char* name, struct hw_device_t** device);
static int android_gpio_device_close(struct hw_device_t* device);
// device access
static int android_gpio_set_val(struct android_gpio_device_t* dev, int val);
static int android_gpio_get_val(struct android_gpio_device_t* dev, int* val);
static struct hw_module_methods_t android_gpio_module_methods = {
open: android_gpio_device_open
};
struct android_gpio_module_t HAL_MODULE_INFO_SYM = {
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: ANDROID_GPIO_HARDWARE_MODULE_ID,
name: MODULE_NAME,
author: "HAL",
methods: &android_gpio_module_methods,
}
};
static int android_gpio_device_open(const struct hw_module_t* module,
const char* name, struct hw_device_t** device) {
struct android_gpio_device_t* dev;
dev = (struct android_gpio_device_t*)malloc(sizeof(struct android_gpio_device_t));
memset(dev, 0, sizeof(struct android_gpio_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = android_gpio_device_close;
dev->set_val = android_gpio_set_val;
dev->get_val = android_gpio_get_val;
if ((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
LOGE("android_gpio: failed to open /dev/AdrIO -- %s.", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
return 0;
}
static int android_gpio_device_close(struct hw_device_t* device) {
struct android_gpio_device_t* android_gpio_device =
(struct android_gpio_device_t*)device;
if (android_gpio_device) {
close(android_gpio_device->fd);
free(android_gpio_device);
}
return 0;
}
static int android_gpio_set_val(struct android_gpio_device_t* dev, int val) {
LOGI("android_gpio: set value %d to device.", val);
write(dev->fd, &val, sizeof(val));
return 0;
}
static int android_gpio_get_val(struct android_gpio_device_t* dev, int* val) {
return 0;
}
為了避免呼叫時出現 Permission denied,打開 system/core/rootdir 目錄下的 ueventd.rc,加上一行:
/dev/android_gpio 0666 root root
在 android_gpio 目錄中繼續新增:
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 := liblog LOCAL_SRC_FILES := android_gpio.c LOCAL_MODULE := android_gpio.default include $(BUILD_SHARED_LIBRARY)
mmm hardware/libhardware/modules/android_gpio
常見的兩個錯誤:一是提示找不到 mmm 指令,通常是沒有先 source build/envsetup.sh;二是提示沒有規則可以建立 /lib/liblog.so,代表 LOCAL_SHARED_LIBRARIES 或來源樹環境有問題,逐一確認即可。
編譯成功後會看到:
Install: out/target/product/generic/system/lib/hw/android_gpio.default.so
這就是我們要的 HAL 模組。完成這一步之後還要繼續往上走,透過 JNI 或 service 把它接到應用層,硬體調用的路才算走完。
我的 Android 進階之旅——查看應用簽名信息
一、查看自己的證書簽名信息 如上一篇《我的 Android 進階之旅——製作和查看自訂的 Debug 版本簽名證書》所述,我們可以透過 keytool 命令查看生成的 k
Android 仿 QQ 好友分組列表:ExpandableListView 增刪改與持久化
案例目標本案例以 ExpandableListView 為基礎,打造類似 QQ 好友分組的互動效果。除了展示分組與子項資料,還加入新增、刪除、修改與本地持久化能力,讓列表
Android 官方文件導讀:Location and Sensors APIs(中)
本文接續介紹 Android 裝置中的各類感測器(Sensor),包含感測器概述、分類、框架,以及如何在程式中監聽感測器事件。感測器概述(Sensors Overview
Android NDK 開發入門:環境搭建與官方範例實測
NDK 對很多開發者來說是個既好奇又陌生的名詞。近期做專案時,為了讓輸出的 .apk 不容易被反編譯解讀,查了不少資料,其中就有人提到用 NDK 來保護核心邏輯。抱著一探