HIDL 接口文件定义
进入代码,我们假设Naruto作为标准AOSP的HAL,我们就把代码揉进标准HAL层去,进入代码目录创建HIDL目录:
mkdir -p hardware/interfaces/naruto/1.0/default
接着创建接口描述文件INaruto.hal,放在刚才创建的目录中
packageandroid.hardware.naruto@1.0;
interfaceINaruto{
helloWorld(string name)generates(string result);
};
生成HAL 相关文件
既然Google在Android 8.1要我们把HAL层换一次血,那么他肯定会有一些列相关的工具来方便我们开发喽,不然谁搞啊,对不对。
所以呢,Google还是帮我们提供了一些工具来生成HAL层相关的代码框架和代码实例,这样子我们只需要关心实现部分,而不需要写一堆无用代码,浪费时间在搞Makefile和一些低级错误上。
使用hidl-gen工具
# PACKAGE=android.hardware.naruto@1.0
# LOC=hardware/interfaces/naruto/1.0/default/
# make hidl-gen -j64
# hidl-gen -o $LOC -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
# hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
然后使用脚本来更新Makefile,自动生成Android,mk, Android.bp
# ./hardware/interfaces/update-makefiles.sh
现在,我们来添加两个空文件:
touch hardware/interfaces/naruto/1.0/default/android.hardware.naruto@1.0-service.rc
touch hardware/interfaces/naruto/1.0/default/service.cpp
实现HAL实现端的共享库
来来来,vim走起来,打开Naruto.h和Naruto.cpp文件,开始要写代码了,
打开Naruto.h文件,
structNaruto:public INaruto{// Methods from INaruto follow.Return<void>helloWorld(consthidl_string&name,helloWorld_cb _hidl_cb)override;// Methods from ::android::hidl::base::V1_0::IBase follow.};// FIXME: most likely delete, this is only for passthrough implementations// extern "C" INaruto* HIDL_FETCH_INaruto(const char* name);
我们知道,HIDL的实现有两种方式,一种是Binderized模式,另一种是Passthrough模式,我们看到上面有两行注释掉的代码,看来这个代码是关键,来选择实现方式是Binderized还是Passthrough。
我们这里使用Passthrough模式来演示,其实大家后面尝试这两种方式后会发现其实这两种本质是一样的,目前大部分厂商使用的都是Passthrough来延续以前的很多代码,但是慢慢的都会被改掉的,所以我们来打开这个注释。
Naruto.h
#ifndefANDROID_HARDWARE_NARUTO_V1_0_NARUTO_H#defineANDROID_HARDWARE_NARUTO_V1_0_NARUTO_H#include<android/hardware/naruto/1.0/INaruto.h>#include<hidl/MQDescriptor.h>#include<hidl/Status.h>namespace android{namespace hardware{namespace naruto{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;structNaruto:public INaruto{// Methods from INaruto follow.Return<void>helloWorld(consthidl_string&name,helloWorld_cb _hidl_cb)override;// Methods from ::android::hidl::base::V1_0::IBase follow.};// FIXME: most likely delete, this is only for passthrough implementationsextern"C"INaruto*HIDL_FETCH_INaruto(constchar*name);}// namespace implementation}// namespace V1_0}// namespace naruto}// namespace hardware}// namespace android#endif// ANDROID_HARDWARE_NARUTO_V1_0_NARUTO_H
Naruto.cpp
#include"Naruto.h"namespace android{namespace hardware{namespace naruto{namespace V1_0{namespace implementation{// Methods from INaruto follow.Return<void>Naruto::helloWorld(consthidl_string&name,helloWorld_cb _hidl_cb){// TODO implementcharbuf[100];::memset(buf,0x00,100);::snprintf(buf,100,"Hello World, %s",name.c_str());hidl_stringresult(buf);_hidl_cb(result);returnVoid();}// Methods from ::android::hidl::base::V1_0::IBase follow.INaruto*HIDL_FETCH_INaruto(constchar*/* name */){returnnewNaruto();}}// namespace implementation}// namespace V1_0}// namespace naruto}// namespace hardware}// namespace android
我们打开了HIDL_FETCH的注释,让我们的HIDL使用Passthrough方式去实现
添加helloWorld函数的实现,简单的做了字符串拼接(学过C/C++)的同学应该都看得懂
然后可以查看一下Android.bp文件看一下编译生成个啥
cc_library_shared { name: "android.hardware.naruto@1.0-impl", relative_install_path: "hw", proprietary: true, srcs: [ "Naruto.cpp", ], shared_libs: [ "libhidlbase", "libhidltransport", "libutils", "android.hardware.naruto@1.0", ],}
最终会生成一个android.hardware.naruto@1.0-impl.so, 生成在/vendor/lib64/hw/下,我们用mmm编译生成看看
$ mmm hardware/interfaces/naruto/1.0/default/PLATFORM_VERSION_CODENAME=RELPLATFORM_VERSION=8.1.0TARGET_PRODUCT=hon660TARGET_BUILD_VARIANT=userdebugTARGET_BUILD_TYPE=releaseTARGET_ARCH=arm64TARGET_ARCH_VARIANT=armv8-aTARGET_CPU_VARIANT=genericTARGET_2ND_ARCH=armTARGET_2ND_ARCH_VARIANT=armv7-a-neonTARGET_2ND_CPU_VARIANT=cortex-a53HOST_ARCH=x86_64HOST_2ND_ARCH=x86HOST_OS=linuxHOST_OS_EXTRA=Linux-3.16.0-48-generic-x86_64-with-Ubuntu-14.04-trustyHOST_CROSS_OS=windowsHOST_CROSS_ARCH=x86HOST_CROSS_2ND_ARCH=x86_64HOST_BUILD_TYPE=releaseBUILD_ID=OPM1.171019.011# OUT_DIR=out[2/2] bootstrap out/soong/.minibootstrap/build.ninja.in[1/1] out/soong/.bootstrap/bin/minibp out/soong/.bootstrap/build.ninja[2/3] glob hardware/interfaces/*/Android.bp[1/1] out/soong/.bootstrap/bin/soong_build out/soong/build.ninjaNo need to regenerate ninja file[100% 3/3] out/soong/.bootstrap/bin/soong_build out/soong/build.ninja[100% 18/18] build 'out/target/product/hon660/obj/SHARED_LIBRARIES/android.hardware.naruto@1.0-impl_intermediates/android.hardware.naruto@1.0-impl.so.toc'#### build completed successfully (02:35 (mm:ss))
为了让 HAL 在直通模式下运行,您必须具有 HIDL_FETCH_IModuleName 函数,该函数位于 /(system|vendor|...)/lib(64)?/hw/android.hardware.package@3.0-impl(OPTIONAL_IDENTIFIER).so 中,其中 OPTIONAL_IDENTIFIER 是一个标识直通实现的字符串。直通模式要求会通过上述命令自动满足,这些命令也会创建 android.hardware.nfc@1.0-impl 目标,但可以使用任何扩展。例如,android.hardware.nfc@1.0-impl-foo 使用 -foo 来区分自身。
如果某个 HAL 是次要版本或另一个 HAL 的扩展,应使用基础 HAL 来为此二进制文件命名。例如,android.hardware.graphics.mapper@2.1 实现应仍然在一个名为 android.hardware.graphics.mapper@2.0-impl(OPTIONAL_IDENTIFIER) 的二进制文件中。通常,此处的 OPTIONAL_IDENTIFIER 会包含实际的 HAL 版本。通过以这种方式来为二进制文件命名,2.0 客户端可以直接对其进行检索,而 2.1 客户端可以向上转换实现的类型。
接下来,使用功能填写存根并设置守护程序。守护程序代码(支持直通)示例:
#include <hidl/LegacySupport.h>
int main(int /* argc */, char* /* argv */ []) {return defaultPassthroughServiceImplementation<INfc>("nfc");}
defaultPassthroughServiceImplementation 将对提供的 -impl 库执行 dlopen() 操作,并将其作为 Binder 化服务提供。守护程序代码(对于纯绑定式服务)示例:
int main(int /* argc */, char* /* argv */ []) {// This function must be called before you join to ensure the proper// number of threads are created. The threadpool will never exceed// size one because of this call.::android::hardware::configureRpcThreadpool(1 /*threads*/, true /*willJoin*/);
sp
<INfc> nfc = new Nfc();const status_t status = nfc->registerAsService();if (status != ::android::OK) {return 1; // or handle error}
// Adds this thread to the threadpool, resulting in one total// thread in the threadpool. We could also do other things, but// would have to specify 'false' to willJoin in configureRpcThreadpool.::android::hardware::joinRpcThreadpool();return 1; // joinRpcThreadpool should never return}
此守护程序通常存在于 $PACKAGE + "-service-suffix"(例如 android.hardware.nfc@1.0-service)中,但也可以位于任何位置。HAL 的特定类的 sepolicy 是 hal_<module> 属性(例如 hal_nfc))。您必须将此属性应用到运行特定 HAL 的守护程序(如果同一进程提供多个 HAL,可以将多个属性应用到该进程)。