#<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
#include#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);
sphello = 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.naruto@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
# include
# include
# include
# include
# include
using android::hardware::hello::V1_0::IHello;
using android::sp;
using android::hardware::hidl_string;int main()
{
int ret = 0;android::sp
service = IHello::getService();
if(service == nullptr) {
printf("Failed to get servicen");
return -1;
}service->sayHello("say:", [&](hidl_string result) {
printf("%sn", 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
android.hardware.hello
hwbinder
1.0
IHello
default
#<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



