1-建立INaruto.hal文件
mkdir -p hardware/interfaces/naruto/1.0/default
INaruto.hal:
package android.hardware.naruto@1.0;
interface INaruto {
helloWorld(string name) generates (string result);
};
生成HAL 相关文件,使用google 提供的原生工具
# 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 ----->生成.h .cpp
# hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE---->生成Android.bp
然后使用脚本来更新Makefile,自动生成Android,mk, Android.bp
# ./hardware/interfaces/update-makefiles.sh
添加service 文件
touch hardware/interfaces/naruto/1.0/default/android.hardware.naruto@1.0-service.rc
touch hardware/interfaces/naruto/1.0/default/service.cpp
现在我们的代码目录: hardware/interface/naruto:
├── 1.0
│ ├── Android.bp
│ ├── Android.mk
│ ├── default
│ │ ├── Android.bp
│ │ ├── android.hardware.naruto@1.0-service.rc
│ │ ├── Naruto.cpp
│ │ ├── Naruto.h
│ │ └── service.cpp
│ └── INaruto.hal
└── Android.bp
打开Naruto.h文件,Naruto.cpp 把下面的两个注释放开,默认是binder ,采用直通要放开
// FIXME: most likely delete, this is only for passthrough implementations
extern "C" INaruto* HIDL_FETCH_INaruto(const char* name);
INaruto* HIDL_FETCH_INaruto(const char* /* name */) {
return new Naruto();
}
android.hardware.naruto@1.0-service.rc:
service naruto_hal_service /vendor/bin/hw/android.hardware.naruto@1.0-service
class hal
user system
group system
service.cpp:
# define LOG_TAG "android.hardware.naruto@1.0-service"
# 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>();
}
hardware/interfaces/naruto/1.0/default/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",
],
}
cc_binary {
name: "android.hardware.naruto@1.0-service",
defaults: ["hidl_defaults"],
proprietary: true,
relative_install_path: "hw",
srcs: ["service.cpp"],
init_rc: ["android.hardware.naruto@1.0-service.rc"],
shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"liblog",
"android.hardware.naruto@1.0",
"android.hardware.naruto@1.0-impl",
],
}
在目录下hardware/interfaces/naruto/1.0/default
与hardware/interfaces/naruto/1.0
build
需要push 到系统的文件
out/target/product/sdm845/system/lib/android.hardware.naruto@1.0.so
out/target/product/sdm845/system/lib64/android.hardware.naruto@1.0.so
out/target/product/sdm845/vendor/lib/hw/android.hardware.naruto@1.0-impl.so
out/target/product/sdm845/vendor/lib64/hw/android.hardware.naruto@1.0-impl.so
out/target/product/sdm845/vendor/bin/hw/android.hardware.naruto@1.0-service
out/target/product/sdm845/vendor/etc/init/android.hardware.naruto@1.0-service.rc
2- 如何测试hidl
测试bin
out/target/product/sdm845/vendor/bin/naruto_test
如何制作测试bin
LINUX/android/vendor/xxx/hidl-test$ tree
.
├── Android.mk
└── client.cpp
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 \
android.hardware.naruto@1.0 \
include $(BUILD_EXECUTABLE)
client.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;
}
device/qcom/项目/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>
3- 进行开发调试
启动service ,本可以开启执行service 启动, 不过没有在调试,测试手动执行了
root:/vendor/bin/hw # ./android.hardware.naruto@1.0-service &
[1] 3073
查看service 是否启动:
root:/vendor/bin# ps -ef |grep naruto
root 3073 2851 0 07:54:11 pts/0 00:00:00 android.hardware.naruto@1.0-service
运行测试bin
root:/vendor/bin # ./naruto_test
Hello World, JayZhang
4-调用流程
HIDL软件包中自动生成的文件会链接到与软件包同名的单个共享库。该共享库还会导出单个头文件INruto.h,用于在binder客户端和服务端的接口文件,下面的图诠释了我们的INaruto.hal编译后生成的文件走向,从官网拷贝过来:
图片.png
-
IFoo.h
- 描述 C++ 类中的纯 IFoo 接口;它包含 IFoo.hal 文件中的 IFoo 接口中所定义的方法和类型,必要时会转换为 C++ 类型。不包含与用于实现此接口的 RPC 机制(例如 HwBinder)相关的详细信息。类的命名空间包含软件包名称和版本号,例如 ::android::hardware::samples::IFoo::V1_0。客户端和服务器都包含此标头:客户端用它来调用方法,服务器用它来实现这些方法。 -
IHwFoo.h
- 头文件,其中包含用于对接口中使用的数据类型进行序列化的函数的声明。开发者不得直接包含其标头(它不包含任何类)。 -
BpFoo.h
- 从 IFoo 继承的类,可描述接口的 HwBinder 代理(客户端)实现。开发者不得直接引用此类。 -
BnFoo.h
- 保存对 IFoo 实现的引用的类,可描述接口的 HwBinder 存根(服务器端)实现。开发者不得直接引用此类。 -
FooAll.cpp
- 包含 HwBinder 代理和 HwBinder 存根的实现的类。当客户端调用接口方法时,代理会自动从客户端封送参数,并将事务发送到绑定内核驱动程序,该内核驱动程序会将事务传送到另一端的存根(该存根随后会调用实际的服务器实现)。
这里目录下: 可以看到hidl机制文件
out/soong/.intermediates/hardware/interfaces/naruto/1.0/android.hardware.naruto@1.0_genc++
.
└── gen
└── android
└── hardware
└── naruto
└── 1.0
└── NarutoAll.cpp
out/soong/.intermediates/hardware/interfaces/naruto/1.0/android.hardware.naruto@1.0_genc++_headers
.
└── gen
└── android
└── hardware
└── naruto
└── 1.0
├── BnHwNaruto.h
├── BpHwNaruto.h
├── BsNaruto.h
├── IHwNaruto.h
└── INaruto.h
客户端和服务器实现不得直接引用除 IFoo 之外的任何内容。为了满足这项要求,请只包含 IFoo.h 并链接到生成的共享库。
-
[android.hardware.naruto@1.0-impl.so](mailto:android.hardware.naruto@1.0-impl.so)
: Naruto模块实现端的代码编译生成,binder server端 -
[android.hardware.naruto@1.0.so](mailto:android.hardware.naruto@1.0.so):
Naruto模块调用端的代码,binder client端 -
naruto_hal_service
: 通过直通式注册binder service,暴露接口给client调用 -
android.hardware.naruto@1.0-service.rc
: 启动service
REF:
https://blog.csdn.net/u013357557/article/details/84561652
https://www.jianshu.com/p/ca6823b897b5
网友评论