# source build/envsetup.sh # lunch # make hidl-gen -j4 一、server端 1.创建INaruto.hal文件 # cd hardware # mkdir -p ~/android/hardware/interfaces/naruto/1.0/default # cd hardware/interfaces/naruto/1.0 # emacs INaruto.hal package [email protected]; interface INaruto { helloWorld(string name) generates (string result); }; 2.使用hidl-gen工具编译INaruto.hal自动生成.mk,.bp,.cpp文件 # cd ~/android # [email protected] # LOC=hardware/interfaces/naruto/1.0/default/ # make hidl-gen -j16 # 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 在default下生成Android.bp,Naruto.cpp,Naruto.h文件 # ./hardware/interfaces/update-makefiles.sh 在1.0目录下生成Android.bp,Android.mk文件 # touch hardware/interfaces/naruto/1.0/default/[email protected] # touch hardware/interfaces/naruto/1.0/default/service.cpp 3.修改源码,然后编译自动生成文件 # mmm hardware/interfaces/naruto/1.0/default/ 4.启动server端进程 # emacs hardware/interfaces/naruto/1.0/default/[email protected] service naruto_hal_service /vendor/bin/hw/[email protected] class hal user system group system 5.server端进程源码 # emacs hardware/interfaces/naruto/1.0/default/service.cpp #define LOG_TAG "[email protected]" #include <android/hardware/naruto/1.0/INaruto.h> #include <hidl/LegacySupport.h> using android::hardware::naruto::V1_0::INaruto; using android::hardware::defaultPassthroughServiceImplementation; int main() { return defaultPassthroughServiceImplementation<INaruto>(); } 6.编译server端代码 # mmm hardware/interfaces/naruto/1.0/default 7.编译vendor.img,刷机 # make vendorimage -j16 二、client端 1.测试代码: naruto_test # emacs test.cpp #include <android/hardware/naruto/1.0/INaruto.h> #include <hidl/Status.h> #include <hidl/LegacySupport.h> #include <utils/misc.h> #include <hidl/HidlSupport.h> #include <stdio.h> using android::hardware::naruto::V1_0::INaruto; using android::sp; using android::hardware::hidl_string; int main() { int ret; android::sp<INaruto> service = INaruto::getService(); if(service == nullptr) { printf("Failed to get service\n"); return -1; } service->helloWorld("JayZhang", [&](hidl_string result) { printf("%s\n", result.c_str()); }); return 0; } 2.Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_PROPRIETARY_MODULE := true LOCAL_MODULE := naruto_test LOCAL_SRC_FILES := \ client.cpp \ LOCAL_SHARED_LIBRARIES := \ liblog \ libhidlbase \ libutils \ [email protected] \ include $(BUILD_EXECUTABLE) 3.在manifest文件里添加vendor接口的定义,不然在client端是没法拿到service的,在相应的manifest.xml里面加入 # emacs manifest.xml … … <hal format="hidl"> <name>android.hardware.naruto</name> <transport>hwbinder</transport> <version>1.0</version> <interface> <name>INaruto</name> <instance>default</instance> </interface> </hal> # adb push manifest.xml /vendor 三、烧写vendor.img测试 # adb shell # /vendor/bin/hw/[email protected] # ./naruto_test