#<1>
mkdir -p hardware/interfaces/hello/1.0#<2>
cat > hardware/interfaces/hello/1.0/IHello.hal << EOF
package android.hardware.hello@1.0;
interface IHello {
//声明了一个函数名为 sayHello、参数为 字符串、返回值为 字符串 类型的接口。
sayHello(string name) generates (string result);
};
EOF#<3>update-makefiles.sh 1.0 Android.bp
./hardware/interfaces/update-makefiles.sh#<4> gen tmpl
PACKAGE=android.hardware.hello@1.0
LOC=./hardware/interfaces/hello/1.0/default
# hidl-gen C++ files
hidl-gen -o $LOC -L c++-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
# hidl-gen Android.bp
hidl-gen -o $LOC -L androidbp-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGEsayHello函数实现
char buf[128];
::memset(buf, 0, 128);
::snprintf(buf, 128, "Hello World, %s", name.c_str());
hidl_string result(buf);
_hidl_cb(result);#<5> service provider
cat > hardware/interfaces/hello/1.0/default/service.cpp << EOF
# define LOG_TAG "android.hardware.hello@1.0-service"#include <hidl/HidlTransportSupport.h>
#include <android/hardware/hello/1.0/IHello.h>#include "Hello.h"
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::hardware::hello::V1_0::IHello;
using android::hardware::hello::V1_0::implementation::Hello;
using android::sp;
using android::OK;int main() {
configureRpcThreadpool(4, true);
sp<IHello> hello = new Hello();
if(hello->registerAsService() != OK) {
ALOGE("Could not register hello 1.0 service.");
}
joinRpcThreadpool();
ALOGE("Service exited!");
return 1;
}EOF
#<6> startup script
cat > hardware/interfaces/hello/1.0/default/android.hardware.hello@1.0-service.rc << EOF
service hello_hal_service /vendor/bin/hw/android.hardware.hello@1.0-service
class hal
user system
group system
EOF
#<7> service consumer
cat > hardware/interfaces/hello/1.0/default/test.cpp << EOF
# define LOG_TAG "android.hardware.hello@1.0-test"# include <android/hardware/hello/1.0/IHello.h>
# include <hidl/Status.h>
# include <hidl/LegacySupport.h>
# include <utils/misc.h>
# include <hidl/HidlSupport.h>
# include <stdio.h>
using android::hardware::hello::V1_0::IHello;
using android::sp;
using android::hardware::hidl_string;int main()
{
int ret = 0;android::sp<IHello> service = IHello::getService();
if(service == nullptr) {
printf("Failed to get service\n");
return -1;
}service->sayHello("say:", [&](hidl_string result) {
printf("%s\n", result.c_str());
});return ret;
}
EOF#<8> Android.bp:build hal, service and client
cat > hardware/interfaces/hello/1.0/default/Android.bp << EOF
cc_binary {
name: "android.hardware.hello@1.0-service",
init_rc: ["android.hardware.hello@1.0-service.rc"],
relative_install_path: "hw",
vendor: true,srcs: [
"Hello.cpp",
"service.cpp",
],cflags: [
"-Wall",
"-Werror",
],shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"liblog",
"android.hardware.hello@1.0",
],
}cc_binary {
name: "android.hardware.hello@1.0-test",
srcs: ["test.cpp"],
vendor: true,cflags: [
"-Wall",
"-Werror",
],shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"liblog",
"android.hardware.hello@1.0",
],
}
EOF
#<9> let service can find android.hardware.hello@1.0.so
build/make/target/product/vndk/28.txt
build/make/target/product/vndk/current.txt
out/target/product/generic/obj/PACKAGING/vndk_intermediates/libs.txt
严格按字母顺序加入VNDK-core: android.hardware.hello@1.0.so
三个文件一样,若编译报错,按lib.txt重新添加#<10> let client can get services
device/generic/goldfish/manifest.xml(/vendor/manifest.xml)文件加入client才能拿到services
<hal format="hidl">
<name>android.hardware.hello</name>
<transport>hwbinder</transport>
<version>1.0</version>
<interface>
<name>IHello</name>
<instance>default</instance>
</interface>
</hal>#<11>Add hello HIDL *-impl and -service libraries to product packages
build/make/target/product/emulator.mk文件加入
PRODUCT_PACKAGES += \
android.hardware.hello@1.0-impl \
android.hardware.hello@1.0-service
#<12>
mm hardware/interfaces/hello/1.0/
mmm hardware/interfaces/hello/1.0/default/
make -j 8
Android 9.0 编写HIDL
版权声明:本文为m0_37132481原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。