系列文章
Android 12 S Native Service的创建流程
Android 12 S Binder原理之BpBinder,BnBinder以及IInterface介绍
Android 12 S 自定义Hal服务selinux权限添加
Android 12 S 自定义Native服务selinux权限添加
Android 12 S 自定义native服务访问java服务
目录
6. 在1.0/default/下面生成Android.bp
8. 在主目录android下执行如下命令生成current.txt
9. 在1.0/default/下添加vendor.qti.hardware.customizehidl-service.rc
10. 在1.0/default/下添加vendor.qti.hardware.customizehidl-service.xml
11. 在device/qcom/common/vendor_compatibility_matrix.xml中添加如下内容
12. 在1.0/default/下添加service.cpp
HIDL 这个机制的目的,主要是为了把框架(framework)与 HAL 进行隔离,使得框架部分可以直接被覆盖、更新,而不需要重新对 HAL 进行编译。HAL 的部分将会放在设备的 /vendor 分区中,并且是由设备供应商(vendors)或 SOC 制造商来构建。这使得框架部分可以通过 OTA 方式更新,同时不需要重新编译 HAL。
这种设计被称为Treble机制,从Android 8.0引入。在此之前framework需要调用dlopen打开HAL编译的.so才能和HAL 层通信,这样framework 与 HAL 处在同一个进程 , 两者耦合严重,导致版本升级时供应商需要做大量的适配工作。
Treble机制解决目前Android 版本之间升级麻烦的问题,将OEM适配的部分vendor与google 对android 大框架升级的部分system部分做了分离,一旦适配了一个版本的vendor信息之后,之后的版本再进行升级时,直接升级system即可,这个就不会给OEM厂商升级带来太大的工作量,直接升级最新功能,可以解决目前市面上Android版本过于凌乱的问题。
Treble机制在Vendor分区中有两种模式,直通模式(Passthrough)和绑定模式(Binderized),大致框架图如下,对于Android O之前的设备,对应图1,对于从之前的设备升级到O的版本,对应图2、图3. 对于直接基于Android O开发的设备,对应图4。
一般新增HIDL Service都是在vendor目录下新增,最好在自己的产品名下新增文件夹,创建相应的HIDL服务,假设目前要在qcom下新增HIDL服务,流程如下:
如何新增HIDL服务
1.新增hidl服务文件夹
在vendor/qcom/opensource/interfaces下新增文件夹,这里新增customizehidl文件夹
在customizehidl下新建1.0文件夹
在1.0下新增default文件夹
2. 新增.hal文件
在customizehidl下新增ICustomizehidl.hal以及types.hal文件
vendor/qcom/opensource/interfaces/ICustomizehidl.halpackage vendor.qti.hardware.customizehidl@1.0;interface ICustomizeHidl{ resetOption(int32_t side) generates (Result result);};
vendor/qcom/opensource/interfaces/types.halpackage vendor.qti.hardware.customizehidl@1.0;enum Result : int32_t { OK = 0, ERR, OUT_OF_RANGE,};
3. 编译hidl-gen
source build/envsetup.sh
lunch xx
make hidl-gen
这样后面就可以用hidl-gen工具了,
4. 配置hidl的package root
PACKAGE=vendor.qti.hardware.customizehidl@1.0
LOC=vendor/qcom/opensource/interfaces/customizehidl/1.0/default/
如果有如下报错的话
ERROR: Package root not specified for vendor.qti.hardware.customizehidl@1.0
ERROR: Could not get sources for vendor.qti.hardware.customizehidl@1.0.
需要在以下目录下添加如下代码:
vendor/qcom/opensource/interfaces/Android.bphidl_package_root { name:"vendor.qti.hardware.customizehidl", path:"vendor/qcom/opensource/interfaces/customizehidl",}
5. 在1.0/default/下生成.cpp和.h文件
在vendor/qcom/opensource/interfaces/customizehidl/1.0/default/下生成.cpp和.h文件:
hidl-gen -o $LOC -Lc++-impl -rvendor.qti.hardware:vendor/qcom/opensource/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
或者
hidl-gen -o vendor/qcom/opensource/interfaces/customizehidl/1.0/default -Lc++-impl -rvendor.qti.hardware:vendor/qcom/opensource/interfaces –randroid.hidl:system/libhidl/transport vendor.qti.hardware.customizehidl@1.0
然后会生成CustomizeHidl.cpp和CustomizeHidl.h文件。生成后需要做相关修改,修改后内容如下:
vendor/qcom/opensource/interfaces/customizehidl/1.0/default/CustomizeHidl.cpp#include "CustomizeHidl.h"#include #include #include
头文件内容如下:
vendor/qcom/opensource/interfaces/customizehidl/1.0/default/CustomizeHidl.h#include #include #include #include #include namespace vendor {namespace qti {namespace hardware {namespace customizehidl {namespace V1_0 {namespace implementation {using namespace std;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;struct CustomizeHidl : public ICustomizeHidl { CustomizeHidl(); ~CustomizeHidl(); Return<::vendor::qti::hardware::customizehidl::V1_0::Result> resetOption(int32_t side) override;};// FIXME: most likely delete, this is only for passthrough implementations// extern "C" ICustomizeHidl* HIDL_FETCH_ICustomizeHidl(const char* name);} // implementation} //customizehidl} // V1_0} // hardware} // qti} // vendor
6. 在1.0/default/下面生成Android.bp
在vendor/qcom/opensource/interfaces/customizehidl/1.0/default/下面生成Android.bp:
hidl-gen -o $LOC -Landroidbp-impl -rvendor.qti.hardware:vendor/qcom/opensource/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
生成后需要做相关修改,修改后内容如下:
cc_defaults{ name: "customizehidl_defaults", //relative_install_path: "hw", defaults: ["hidl_defaults"], //根据项目需求 //vendor: true, proprietary: true, cflags: [ "-Wno-unused-parameter", "-Wall", ], shared_libs: [ "libcutils", "liblog", "libbase", "libsysutils", "libhidlbase", "libhidltransport", "libutils", "vendor.qti.hardware.customizehidl@1.0", ],}cc_library_shared { name: "vendor.qti.hardware.customizehidl@1.0-impl", relative_install_path: "hw", //根据项目需求 //vendor: true, proprietary: true, cflags: [ "-Wno-unused-parameter", "-Wall", "-Wunused-variable", "-Wunused-const-variable", "-Wunused-variable", ], srcs: [ "CustomizeHidl.cpp", ], defaults: ["customizehidl_defaults"],}cc_binary { name: "vendor.qti.hardware.customizehidl@1.0-service", proprietary: true, compile_multilib: "both", relative_install_path: "hw", defaults: ["customizehidl_defaults"], //rc引用 init_rc: ["vendor.qti.hardware.customizehidl-service.rc"], //vintf_fragments引用 vintf_fragments: ["vendor.qti.hardware.customizehidl-service.xml"], srcs: [ "service.cpp", ], shared_libs: [ "libcutils", "liblog", "libbase", "libsysutils", "libhidlbase", "libhidltransport", "libutils", "vendor.qti.hardware.customizehidl@1.0-impl", ],}
7. 在1.0下生成Android.bp
在vendor/qcom/opensource/interfaces/customizehidl/1.0下生成Android.bp
source system/tools/hidl/update-makefiles-helper.sh
do_makefiles_update vendor.qti.hardware:vendor/qcom/opensource/interfaces android.hardware:hardware/interfaces android.hidl:system/libhidl/transport
生成后需要做相关修改,修改后内容如下:
hidl_interface { name: "vendor.qti.hardware.customizehidl@1.0", root: "vendor.qti.hardware.customizehidl", //根据需求进行设置 //product_specific: true, system_ext_specific: true, srcs: [ "types.hal", "ICustomizeHidl.hal", ], interfaces: [ "android.hidl.base@1.0", ], gen_java: true,}
8. 在主目录android下执行如下命令生成current.txt
hidl-gen -Lhash -rvendor.qti.hardware:vendor/qcom/opensource/interfaces -randroid.hidl:system/libhidl/transport vendor.qti.hardware.customizehidl@1.0 > vendor/qcom/opensource/interfaces/customizehidl/current.txt
9. 在1.0/default/下添加vendor.qti.hardware.customizehidl-service.rc
添加如下内容
service vendor.qti.hardware.customizehidl-1-0 /vendor/bin/hw/vendor.qti.hardware.customizehidl@1.0-serviceclass haluser systemgroup system
10. 在1.0/default/下添加vendor.qti.hardware.customizehidl-service.xml
vendor.qti.hardware.customizehidl hwbinder 1.0 ICustomizeHidl default
11. 在device/qcom/common/vendor_compatibility_matrix.xml中添加如下内容
vendor.qti.hardware.customizehidl hwbinder 1.0 ICustomizeHidl default
12. 在1.0/default/下添加service.cpp
如果采用Binderized方式,如下
#include #include #include "CustomizeHidl.h"#include #include using namespace android;using vendor::qti::hardware::customizehidl::V1_0::ICustomizeHidl;using vendor::qti::hardware::customizehidl::V1_0::implementation::CustomizeHidl;using android::hardware::configureRpcThreadpool;using android::hardware::joinRpcThreadpool;using android::sp;int main(int , char ** ) { sp service = new CustomizeHidl(); configureRpcThreadpool(1, true ); if (::android::OK != service->registerAsService()) { return -1; } joinRpcThreadpool(); return 0;}
如果要采用Passthrough方式的话,则需要按如下方法写main,但同时需要将CustomizeHidl.cpp以及CustomizeHidl.h中的HIDL_FETCH_ICustomizeHidl方法放开才可以。
#include #include #include "CustomizeHidl.h"#include #include using namespace android;using vendor::qti::hardware::customizehidl::V1_0::ICustomizeHidl;using android::hardware::configureRpcThreadpool;using android::hardware::joinRpcThreadpool;using android::hardware::defaultPassthroughServiceImplementation;using android::hardware::registerPassthroughServiceImplementation;int main() {android::status_t status;configureRpcThreadpool(10, false);status = registerPassthroughServiceImplementation();LOG_ALWAYS_FATAL_IF(status != OK, "Error while registering ICustomizeHidl: %d", status);joinRpcThreadpool();return status;}
13. 编写测试bin程序
vendor/qcom/opensource/interfaces/customizehidl/下新增hidl_test_bin目录,并添加Android.bp
cc_binary { name: "hidl_test_bin", srcs: [ "hidl_test_bin.cpp", ], shared_libs: [ "libcutils", "libutils", "liblog",//以下so一定要添加,否则可能会有报错 "libbinder", "libhidlbase", "libbase", "libhidltransport", "vendor.qti.hardware.customizehidl@1.0", ], cflags: [ "-Werror", "-Wno-error=deprecated-declarations", "-Wno-unused-parameter", "-Wall", ], }
添加hidl_test_bin.cpp文件
#define LOG_TAG "hidl_test_bin"#include #include #include #include #include #include #include #include #include using namespace android;using namespace std;using vendor::qti::hardware::customizehidl::V1_0::ICustomizeHidl;using vendor::qti::hardware::customizehidl::V1_0::Result;using android::hardware::Return;using android::hardware::details::return_status;using android::hardware::Void;sp halService;int main(int argc, char* argv[]) { halService = ICustomizeHidl::getService(); if (halService == NULL){ cout << "get hal service failed" <resetOption(value); return 0;}
14. 测试
14.1 单编方法:
source build/envsetup.sh
lunch xxx
cd vendor/qcom/opensource/interfaces/customizehidl
mma(编译hidl服务相关)
cd vendor/qcom/opensource/interfaces/customizehidl/hidl_test_bin
mma(编译test bin相关)
14.2 测试方法:
1. adb pull vendor/etc/vintf/compatibility_matrix.xml
在最后增加
2. 再push回去
adb root
adb remount
adb push compatibility_matrix.xml vendor/etc/vintf/compatibility_matrix.xml
3. 在out/target/product/qssi下执行
adb push ./product/lib/vendor.qti.hardware.customizehidl@1.0.so product/lib/
adb push ./product/lib64/vendor.qti.hardware.customizehidl@1.0.so product/lib64/
adb push ./vendor/lib/hw/vendor.qti.hardware.customizehidl@1.0-impl.so vendor/lib/hw/
adb push ./vendor/lib/vendor.qti.hardware.customizehidl@1.0.so vendor/lib/
adb push ./vendor/lib64/hw/vendor.qti.hardware.customizehidl@1.0-impl.so vendor/lib64/hw/
adb push ./vendor/lib64/vendor.qti.hardware.customizehidl@1.0.so vendor/lib64/
adb push ./vendor/etc/vintf/manifest/vendor.qti.hardware.customizehidl-service.xml vendor/etc/vintf/manifest
adb push ./vendor/bin/hw/vendor.qti.hardware.customizehidl@1.0-service vendor/bin/hw/
adb push ./system_ext/lib/vendor.qti.hardware.customizehidl@1.0.so system_ext/lib/
adb push ./system_ext/lib64/vendor.qti.hardware.customizehidl@1.0.so system_ext/lib64/
adb push system/bin/hidl_test_bin system/bin(测试的bin程序)
然后执行
adb reboot
重启后
关闭selinux: adb shell setenforce 0
窗口1:起hidl服务
adb shell
cd vendor/bin/hw
./vendor.qti.hardware.customizehidl@1.0-service
窗口2:起test bin程序
adb shell
hidl_test_bin 100
然后观察窗口2输出什么
如果输出如下则代表两者通信成功
get hal service scuess
value is 100
15. 可能碰到的问题
15.1 错误1
04-14 12:27:02.103 599 599 I hwservicemanager: getTransport: Cannot find entry vendor.qti.hardware.customizehidl@1.0::ICustomizeHidl/default in either framework or device VINTF manifest.
04-14 12:27:02.104 4169 4169 E HidlServiceManagement: Service vendor.qti.hardware.customizehidl@1.0::ICustomizeHidl/default must be in VINTF manifest in order to register/get
那就是vintf manifest相关的配置出现问题,可以检查下是否有push相应的manifest文件,接口的内容是否书写正确等。
15.2 新增目录放hal服务
如遇到新建的目录编译不出来相关hidl的out下相关文件,需要添加update-makefiles.sh文件,可以从其他hidl的目录中拷贝过来,修改其中的目录即可
set -e
if [ -z "$ANDROID_BUILD_TOP" ]; then
echo "Missing ANDROID_BUILD_TOP env variable. Run 'lunch' first."
exit 1
fisource $ANDROID_BUILD_TOP/system/tools/hidl/update-makefiles-helper.sh
do_makefiles_update \
"vendor/newname/interfaces/customizehidl"
16. 自定义Hal 服务Selinux添加
请参考我的另一篇文章
自定义Hal服务selinux权限添加_加油干(◍>∇<◍)ノ゙的博客-CSDN博客
来源地址:https://blog.csdn.net/weixin_41028555/article/details/130424627