概述
HAL 是一个抽象层,具有供硬件供应商实现的标准接口。 HAL 允许 Android 不了解较低级别的驱动程序实现。使用 HAL 允许您在不影响或修改更高级别系统的情况下实现功能。
HAL 开始语言是HIDL,它是一种接口描述语言,用与指定HAL与用户之间的接口。具体意思就是当你用HIDL语法定义好接口之后,可以自动生成c++接口代码,aosp的开发者就可以按照这些接口开发底层的功能了,而上层framework 也可以按照这个接口调用,换句话说这个就是双方约定好的一个规则,好处就是对于APP开发者来说无论底层硬件如何变化,应用程序是不需要修改的。而对于系统工程师来说,只要按照HIDL实现对应的硬件驱动即可,不需要考虑适配应用的问题,这点有点类似NDK,只不过NDK相对灵活面向应用层多一些。
值得注意的是从 Android 10 开始,HIDL 已被弃用,Android 正在迁移以在任何地方使用AIDL。
无论HIDL 还是AIDL 目的是一样的,但是运行思路有一些差异。
HIDL
HIDL 的目标是无需重新构建 HAL 即可替换 Android 框架。 HAL 将由供应商或 SOC 制造商构建,并放在设备上的/vendor
分区中,从而使 Android 框架在其自己的分区中无需重新编译 HAL 即可替换为 OTA。
一般HIDL使用是直通模式,所谓直通模式就是HAL 包装在一个新的 HIDL 接口中,该接口以绑定和相同进程模式为 HAL 提供服务。这种包装对 HAL 和 Android 框架都是透明的。
直通模式仅适用于 C++ 客户端和实现。运行早期 Android 版本的设备没有用 Java 编写的 HAL,因此 Java HAL 本质上是绑定的。
HIDL也支持绑定模式,一般情况下给定一个 HAL 接口abcd@MN::IFoo,会创建2个包:
abcd@MN::IFoo-impl
。包含 HAL 的实现并公开函数IFoo* HIDL_FETCH_IFoo(const char* name)。
可以使用hidl-gen
和-Lc++-impl
和-Landroidbp-impl
生成基本代码。abcd@MN::IFoo-service
。打开直通 HAL 并将其自身注册为绑定服务,使相同的 HAL 实现既可用作直通,也可用作绑定服务。
HIDL C++
Android O 对 Android 操作系统的架构重新进行了设计,以在独立于设备的 Android 平台与特定于设备和供应商的代码之间定义清晰的接口。Android 已经以 HAL 接口的形式(在 hardware/libhardware
中定义为 C 头文件)定义了许多此类接口。HIDL 将这些 HAL 接口替换为带版本编号的稳定接口,它们可以是采用 C++(如下所述)或 JAVA的客户端和服务器端 HIDL 接口。
HIDL 的代码布局
HIDL 包括核心软件包和供应商软件包。
核心 HIDL 接口是指由 Google 指定的接口。此类接口所属的软件包以 android.hardware.
开头,并以子系统命名(可能采用嵌套层命名方式)。例如,NFC 软件包命名为 android.hardware.nfc
,而相机软件包命名为 android.hardware.camera
。一般来说,核心软件包的名称为 android.hardware.
[name1
].[name2
]…。HIDL 软件包除了其名称之外,还有版本。例如,软件包 android.hardware.camera
的版本可以是 3.4
;这一点非常重要,因为软件包的版本会影响其在源代码树中的位置。
所有核心软件包都位于构建系统中的 hardware/interfaces/
下。$m.$n
版本的软件包 android.hardware.
[name1
].[name2
]…位于 hardware/interfaces/name1/name2/
…/$m.$n/
下;3.4
版本的软件包 android.hardware.camera
位于目录 hardware/interfaces/camera/3.4/.
下。软件包前缀 android.hardware.
和路径 hardware/interfaces/
之间存在硬编码映射。
非核心(供应商)软件包是指由 SoC 供应商或 ODM 开发的软件包。非核心软件包的前缀是 vendor.$(VENDOR).hardware.
,其中 $(VENDOR)
是指 SoC 供应商或 OEM/ODM。此前缀会映射到源代码树中的路径 vendor/$(VENDOR)/interfaces
(此映射也属于硬编码映射)。
HAL 层驱动开发演示
- 生成hidl-gen工具
# 进入安卓源码路径
$cd $AOSP
# 导入安卓源码路径环境变量
$export ANDROID_BUILD_TOP=/home/android/AOSP/buildsystem-x9-ptg3.8/android/
# 导入环境变量
$source $ANDROID_BUILD_TOP/build/envsetup.sh
# 生成hidl-gen
$make hidl-gen
# 生成路径 $ANDROID_BUILD_TOP/out/host/linux-x86/bin/
# 讲路径添加到环境变量
$export PATH=$PATH:/home/android/AOSP/buildsystem-x9-ptg3.8/android/out/host/linux-x86/bin
- 创建文件
#在vendor中创建相关文件和目录
$mdkir -p $ANDROID_BUILD_TOP/vendor/sample/hardware/interfaces/helloworld/1.0
$cd $ANDROID_BUILD_TOP/vendor/sample/hardware/interfaces/helloworld/1.0
$touch IHelloWorld.hal IHelloWorldCallback.hal types.hal
$touch $ANDROID_BUILD_TOP/vendor/sample/hardware/interfaces/Android.bp
- 添加内容
helloworld/1.0/IHelloWorld.hal
package sample.hardware.helloworld@1.0;
import IHelloWorldCallback;
interface IHelloWorld {
initial();
getInt() generates (int32_t i);
setInt(int32_t val) generates (Result error);
oneway setCallback(IHelloWorldCallback callback);
};
helloworld/1.0/IHelloWorldCallback.hal
package sample.hardware.helloworld@1.0;
interface IHelloWorldCallback {
oneway onEvent(Event event);
};
helloworld/1.0/types.hal
package sample.hardware.helloworld@1.0;
enum Result : int32_t {
OK,
UNKNOWN,
INVALID_ARGUMENTS,
};
struct Event {
uint32_t type;
uint32_t code;
uint32_t value;
};
- 指定HIDL包根目录
添加文件vendor/sample/hardware/interfaces/Android.bp
hidl_package_root {
name: "sample.hardware",
path: "vendor/sample/hardware/interfaces",
}
- 生成HIDL 哈希
每个软件包根目录(即映射到 hardware/interfaces 的 android.hardware 或映射到 vendor/foo/hardware/interfaces 的 vendor.foo)都必须包含一个列出所有已发布 HIDL 接口文件的 current.txt 文件。
$hidl-gen -Lhash -rsample.hardware:vendor/sample/hardware/interfaces -randroid.hidl:system/libhidl/transport sample.hardware.helloworld@1.0>/home/android/AOSP/buildsystem-x9-ptg3.8/android/vendor/sample/hardware/interfaces/current.txt
- 使用hidl-gen工具生成HIDL相关文件
hidl-gen -o $ANDROID_BUILD_TOP/vendor/sample/hardware/interfaces/helloworld/1.0/default -Lc++-impl -rsample.hardware:$ANDROID_BUILD_TOP/vendor/sample/hardware/interfaces -randroid.hidl:$ANDROID_BUILD_TOP/system/libhidl/transport sample.hardware.helloworld@1.0
生成代码
- HelloWorld.cpp
// FIXME: your file license if you have one
#include "HelloWorld.h"
namespace sample {
namespace hardware {
namespace helloworld {
namespace V1_0 {
namespace implementation {
// Methods from ::sample::hardware::helloworld::V1_0::IHelloWorld follow.
Return<void> HelloWorld::initial() {
// TODO implement
return Void();
}
Return<int32_t> HelloWorld::getInt() {
// TODO implement
return int32_t {};
}
Return<::sample::hardware::helloworld::V1_0::Result> HelloWorld::setInt(int32_t val) {
// TODO implement
return ::sample::hardware::helloworld::V1_0::Result {};
}
Return<void> HelloWorld::setCallback(const sp<::sample::hardware::helloworld::V1_0::IHelloWorldCallback>& callback) {
// TODO implement
return Void();
}
// Methods from ::android::hidl::base::V1_0::IBase follow.
//IHelloWorld* HIDL_FETCH_IHelloWorld(const char* /* name */) {
//return new HelloWorld();
//}
//
} // namespace implementation
} // namespace V1_0
} // namespace helloworld
} // namespace hardware
} // namespace sample
- HelloWorld.h
// FIXME: your file license if you have one
#pragma once
#include <sample/hardware/helloworld/1.0/IHelloWorld.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
namespace sample {
namespace hardware {
namespace helloworld {
namespace V1_0 {
namespace implementation {
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
struct HelloWorld : public IHelloWorld {
// Methods from ::sample::hardware::helloworld::V1_0::IHelloWorld follow.
Return<void> initial() override;
Return<int32_t> getInt() override;
Return<::sample::hardware::helloworld::V1_0::Result> setInt(int32_t val) override;
Return<void> setCallback(const sp<::sample::hardware::helloworld::V1_0::IHelloWorldCallback>& callback) override;
// Methods from ::android::hidl::base::V1_0::IBase follow.
};
// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" IHelloWorld* HIDL_FETCH_IHelloWorld(const char* name);
} // namespace implementation
} // namespace V1_0
} // namespace helloworld
} // namespace hardware
} // namespace sample
- 使用hidl-gen生成helloworld/1.0/default/Android.bp
hidl-gen -o $ANDROID_BUILD_TOP/vendor/sample/hardware/interfaces/helloworld/1.0/default -Landroidbp-impl -rsample.hardware:$ANDROID_BUILD_TOP/vendor/sample/hardware/interfaces -randroid.hidl:$ANDROID_BUILD_TOP/system/libhidl/transport sample.hardware.helloworld@1.0
- 使用system/tools/hidl/update-makefiles-helper.sh生成helloworld/1.0/Android.bp
$source $ANDROID_BUILD_TOP/system/tools/hidl/update-makefiles-helper.sh
$do_makefiles_update sample.hardware:vendor/sample/hardware/interfaces android.hardware:hardware/interfaces android.hidl:system/libhidl/transport
helloworld/1.0/Android.bp内容如下:
hidl_interface {
name: "sample.hardware.helloworld@1.0",
root: "sample.hardware",
product_specific: true,
srcs: [
"types.hal",
"IHelloWorld.hal",
"IHelloWorldCallback.hal",
],
interfaces: [
"android.hidl.base@1.0",
],
gen_java: true,
}
将模块添加到device/$PROCUT_DIR/xxxboard.mk
# Overrides
PRODUCT_MANUFACTURER := SEMIDRIVE
PRODUCT_BRAND := X9
PRODUCT_NAME := $(TARGET_BOARD)
PRODUCT_DEVICE := $(TARGET_BOARD)
PRODUCT_MODEL := x9 for arm64
#SkinManager
CURRENT_PATH :=$(shell pwd)
PRODUCT_PACKAGES += \
sample.hardware.helloworld@1.0
编译
$ mmm vendor/sample/hardware/interfaces/helloworld/1.0/
编译hal文件生成的中间代码位于out/soong/.intermediates/vendor/sample/hardware/interfaces/helloworld/1.0/
default
sample.hardware.helloworld@1.0
sample.hardware.helloworld@1.0-adapter
sample.hardware.helloworld@1.0-adapter_genc++
sample.hardware.helloworld@1.0-adapter-helper
sample.hardware.helloworld@1.0-adapter-helper_genc++
sample.hardware.helloworld@1.0-adapter-helper_genc++_headers
sample.hardware.helloworld@1.0_genc++
sample.hardware.helloworld@1.0_genc++_headers
sample.hardware.helloworld@1.0-vts.driver
sample.hardware.helloworld@1.0-vts.driver_genc++
sample.hardware.helloworld@1.0-vts.driver_genc++_headers
sample.hardware.helloworld@1.0-vts.profiler
sample.hardware.helloworld@1.0-vts.profiler_genc++
sample.hardware.helloworld@1.0-vts.profiler_genc++_headers
sample.hardware.helloworld@1.0-vts.spec
sample.hardware.helloworld-V1.0-java
sample.hardware.helloworld-V1.0-java_gen_java
sample.hardware.helloworld-V1.0-java-shallow
test
"sample.hardware.helloworld@1.0_genc++"目录里面可以发现hal文件转换成了C++源码 ,里面就包含Binder Bn端,Binder Bp端的代码.
编译最终会在out/target/product/$TARGET_PRODUCT/目录下面生成了以下文件:
out/target/product/$TARGET_PRODUCT/system/lib/sample.hardware.helloworld@1.0.so
out/target/product/$TARGET_PRODUCT/system/lib/sample.hardware.helloworld@1.0-adapter-helper.so
out/target/product/$TARGET_PRODUCT/system/lib64/sample.hardware.helloworld@1.0.so
out/target/product/$TARGET_PRODUCT/system/lib64/sample.hardware.helloworld@1.0-adapter-helper.so
out/target/product/$TARGET_PRODUCT/system/framework/sample.hardware.helloworld-V1.0-java.jar
out/target/product/$TARGET_PRODUCT/system/framework/oat/arm/sample.hardware.helloworld-V1.0-java.odex
out/target/product/$TARGET_PRODUCT/system/framework/oat/arm/sample.hardware.helloworld-V1.0-java.vdex
out/target/product/$TARGET_PRODUCT/system/framework/oat/arm64/sample.hardware.helloworld-V1.0-java.odex
out/target/product/$TARGET_PRODUCT/system/framework/oat/arm64/sample.hardware.helloworld-V1.0-java.vdex
out/target/product/$TARGET_PRODUCT/vendor/lib/sample.hardware.helloworld@1.0-impl.so
out/target/product/$TARGET_PRODUCT/vendor/lib/sample.hardware.helloworld@1.0.so
out/target/product/$TARGET_PRODUCT/vendor/lib/sample.hardware.helloworld@1.0-adapter-helper.so
out/target/product/$TARGET_PRODUCT/vendor/lib64/sample.hardware.helloworld@1.0-impl.so
out/target/product/$TARGET_PRODUCT/vendor/lib64/sample.hardware.helloworld@1.0.so
out/target/product/$TARGET_PRODUCT/vendor/lib64/sample.hardware.helloworld@1.0-adapter-helper.so
out/target/product/$TARGET_PRODUCT/vendor/bin/hw/sample.hardware.helloworld@1.0-service
out/target/product/$TARGET_PRODUCT/vendor/bin/sample.hardware.helloworld_hidl_hal_test
out/target/product/$TARGET_PRODUCT/vendor/etc/init/sample.hardware.helloworld@1.0-service.rc
测试
将编译好的库和bin文件复制到设备中:
$ adb root
$ adb remount
$ adb push out/target/product/$TARGET_PRODUCT/vendor/lib64/sample.hardware.helloworld@1.0-impl.so /vendor/lib64/sample.hardware.helloworld@1.0-impl.so
$ adb push out/target/product/$TARGET_PRODUCT/vendor/lib64/sample.hardware.helloworld@1.0.so /vendor/lib64/sample.hardware.helloworld@1.0.so
$ adb push out/target/product/$TARGET_PRODUCT/vendor/bin/hw/sample.hardware.helloworld@1.0-service /vendor/bin/hw/sample.hardware.helloworld@1.0-service
$ adb push out/target/product/$TARGET_PRODUCT/vendor/bin/sample.hardware.helloworld_hidl_hal_test /vendor/bin/sample.hardware.helloworld_hidl_hal_test
启动服务和测试
# ./vendor/bin/hw/sample.hardware.helloworld@1.0-service& sample.hardware.helloworld_hidl_hal_test
initial
getInt 666
setInt result=0
HelloWorldCallback
setCallback
onEvent 0001 0001 0001
onEvent 0002 0002 0002
onEvent 0003 0003 0003