当前位置:   article > 正文

Android Hidl开发_device_framework_compatibility_matrix_file

device_framework_compatibility_matrix_file

1、环境准备

首先准备一份Android源码。

#源码中编译生成hidl-gen

make hidl-gen

下面是hidl-gen的语法:

usage: hidl-gen [-p <root path>] -o <output path> -L <language> [-O <owner>] (-r <interface root>)+ [-R] [-v] [-d <depfile>] FQNAME...

Process FQNAME, PACKAGE(.SUBPACKAGE)*@[0-9]+.[0-9]+(::TYPE)?, to create output.

-h: Prints this menu.
-L <language>: The following options are available:
check : Parses the interface to see if valid but doesn't write any files.
c++ : (internal) (deprecated) Generates C++ interface files for talking to HIDL interfaces.
c++-headers : (internal) Generates C++ headers for interface files for talking to HIDL interfaces.
c++-sources : (internal) Generates C++ sources for interface files for talking to HIDL interfaces.
export-header : Generates a header file from @export enumerations to help maintain legacy code.
c++-impl : Generates boilerplate implementation of a hidl interface in C++ (for convenience).
c++-impl-headers: c++-impl but headers only.
c++-impl-sources: c++-impl but sources only.
c++-adapter : Takes a x.(y+n) interface and mocks an x.y interface.
c++-adapter-headers: c++-adapter but helper headers only.
c++-adapter-sources: c++-adapter but helper sources only.
c++-adapter-main: c++-adapter but the adapter binary source only.
java : (internal) Generates Java library for talking to HIDL interfaces in Java.
java-constants : (internal) Like export-header but for Java (always created by -Lmakefile if @export exists).
vts : (internal) Generates vts proto files for use in vtsd.
makefile : (removed) Used to generate makefiles for -Ljava and -Ljava-constants.
androidbp : (internal) Generates Soong bp files for -Lc++-headers, -Lc++-sources, -Ljava, -Ljava-constants, and -Lc++-adapter.
androidbp-impl : Generates boilerplate bp files for implementation created with -Lc++-impl.
hash : Prints hashes of interface in `current.txt` format to standard out.
function-count : Prints the total number of functions added by the package or interface.
dependencies : Prints all depended types.
-O <owner>: The owner of the module for -Landroidbp(-impl)?.
-o <output path>: Location to output files.
-p <root path>: Android build root, defaults to $ANDROID_BUILD_TOP or pwd.
-R: Do not add default package roots if not specified in -r.
-r <package:path root>: E.g., android.hardware:hardware/interfaces.
-v: verbose output.
-d <depfile>: location of depfile to write to.

2、创建hidl 

在vendor/qcom/proprietary/interfaces下创建howie目录,在howie下创建1.0目录,在1.0目录下创建接口IHowie.hal,包名设置为自定义vendor.oem.hardware.howie的创建目录default。

  1. package vendor.qcom.hardware.howie@1.0;
  2. interface IHowie{
  3. helloWorld(string name) generates (string result);
  4. };

这时候,目录结构为:

howie/

└── 1.0

    ├── default

    └── IHowie.hal

在1.0下写一个hidl-gen.sh脚本

  1. # -o <output path>: Location to output files.
  2. # -r <package:path root>: E.g., android.hardware:hardware/interfaces.
  3. # -L <language>
  4. # androidbp : (internal) Generates Soong bp files for -Lc++-headers, -Lc++-sources, -Ljava, -Ljava-constants, and -Lc++-adapter.
  5. # androidbp-impl : Generates boilerplate bp files for implementation created with -Lc++-impl.
  6. # c++-impl : Generates boilerplate implementation of a hidl interface in C++ (for convenience).
  7. PACKAGE=vendor.qcom.hardware.howie@1.0
  8. LOC=default
  9. # 生成Android.bp
  10. hidl-gen -L androidbp -r vendor.qcom.hardware:vendor/qcom/proprietary/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
  11. # 生成Android.bp
  12. hidl-gen -o $LOC -L androidbp-impl -r vendor.qcom.hardware:vendor/qcom/proprietary/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
  13. # 生成C++文件
  14. hidl-gen -o $LOC -L c++-impl -r vendor.qcom.hardware:vendor/qcom/proprietary/interfaces -r android.hidl:system/libhidl/transport $PACKAGE

运行后,目录结构为:

.
└── 1.0
    ├── Android.bp
    ├── default
    │   ├── Android.bp
    │   ├── Howie.cpp
    │   └── Howie.h
    ├── hidl-gen.sh
    └── IHowie.hal

2 directories, 6 files

1.0目录下的Android.bp

  1. // This file is autogenerated by hidl-gen -Landroidbp.
  2. hidl_interface {
  3. name: "vendor.qcom.hardware.howie@1.0",
  4. root: "vendor.qcom.hardware",
  5. system_ext_specific: true,
  6. srcs: [
  7. "IHowie.hal",
  8. ],
  9. interfaces: [
  10. "android.hidl.base@1.0",
  11. ],
  12. gen_java: true,
  13. }

我们稍作修改

  1. // This file is autogenerated by hidl-gen -Landroidbp.
  2. hidl_interface {
  3. name: "vendor.qcom.hardware.howie@1.0",
  4. root: "vendor.qcom.hardware.howie",
  5. required: ["manifest_vendor.qcom.hardware.howie.xml"],
  6. //vendor: true,
  7. srcs: [
  8. "IHowie.hal",
  9. ],
  10. interfaces: [
  11. "android.hidl.base@1.0",
  12. ],
  13. gen_java: true,
  14. }

1.0/default目录下的Android.bp

  1. // FIXME: your file license if you have one
  2. cc_library_shared {
  3. // FIXME: this should only be -impl for a passthrough hal.
  4. // In most cases, to convert this to a binderized implementation, you should:
  5. // - change '-impl' to '-service' here and make it a cc_binary instead of a
  6. // cc_library_shared.
  7. // - add a *.rc file for this module.
  8. // - delete HIDL_FETCH_I* functions.
  9. // - call configureRpcThreadpool and registerAsService on the instance.
  10. // You may also want to append '-impl/-service' with a specific identifier like
  11. // '-vendor' or '-<hardware identifier>' etc to distinguish it.
  12. name: "vendor.qcom.hardware.howie@1.0-impl",
  13. relative_install_path: "hw",
  14. // FIXME: this should be 'vendor: true' for modules that will eventually be
  15. // on AOSP.
  16. proprietary: true,
  17. srcs: [
  18. "Howie.cpp",
  19. ],
  20. shared_libs: [
  21. "libhidlbase",
  22. "libutils",
  23. "vendor.qcom.hardware.howie@1.0",
  24. ],
  25. }

稍作修改

  1. cc_library_shared {
  2. // FIXME: this should only be -impl for a passthrough hal.
  3. // In most cases, to convert this to a binderized implementation, you should:
  4. // - change '-impl' to '-service' here and make it a cc_binary instead of a
  5. // cc_library_shared.
  6. // - add a *.rc file for this module.
  7. // - delete HIDL_FETCH_I* functions.
  8. // - call configureRpcThreadpool and registerAsService on the instance.
  9. // You may also want to append '-impl/-service' with a specific identifier like
  10. // '-vendor' or '-<hardware identifier>' etc to distinguish it.
  11. name: "vendor.qcom.hardware.howie@1.0-impl",
  12. //relative_install_path: "hw",
  13. // FIXME: this should be 'vendor: true' for modules that will eventually be
  14. // on AOSP.
  15. vendor: true,
  16. srcs: [
  17. "Howie.cpp",
  18. ],
  19. shared_libs: [
  20. "libhidlbase",
  21. "libutils",
  22. "vendor.qcom.hardware.howie@1.0",
  23. ],
  24. }

Howie.h

  1. // FIXME: your file license if you have one
  2. #pragma once
  3. #include <vendor/qcom/hardware/howie/1.0/IHowie.h>
  4. #include <hidl/MQDescriptor.h>
  5. #include <hidl/Status.h>
  6. namespace vendor::qcom::hardware::howie::implementation {
  7. using ::android::hardware::hidl_array;
  8. using ::android::hardware::hidl_memory;
  9. using ::android::hardware::hidl_string;
  10. using ::android::hardware::hidl_vec;
  11. using ::android::hardware::Return;
  12. using ::android::hardware::Void;
  13. using ::android::sp;
  14. struct Howie : public V1_0::IHowie {
  15. // Methods from ::vendor::qcom::hardware::howie::V1_0::IHowie follow.
  16. Return<void> helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) override;
  17. // Methods from ::android::hidl::base::V1_0::IBase follow.
  18. };
  19. // FIXME: most likely delete, this is only for passthrough implementations
  20. // extern "C" IHowie* HIDL_FETCH_IHowie(const char* name);
  21. } // namespace vendor::qcom::hardware::howie::implementation

Howie.cpp

  1. // FIXME: your file license if you have one
  2. #include "Howie.h"
  3. namespace vendor::qcom::hardware::howie::implementation {
  4. // Methods from ::vendor::qcom::hardware::howie::V1_0::IHowie follow.
  5. Return<void> Howie::helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) {
  6. // TODO implement
  7. return Void();
  8. }
  9. // Methods from ::android::hidl::base::V1_0::IBase follow.
  10. //IHowie* HIDL_FETCH_IHowie(const char* /* name */) {
  11. //return new Howie();
  12. //}
  13. //
  14. } // namespace vendor::qcom::hardware::howie::implementation

如上面的注释所说,如果将下面的几行注释去掉,hidl将采用直通式通信方式。

// extern "C" IHowie* HIDL_FETCH_IHowie(const char* name);

//IHowie* HIDL_FETCH_IHowie(const char* /* name */) {
    //return new Howie();
//}

下面,润色一下Howie.cpp

  1. // FIXME: your file license if you have one
  2. #include "Howie.h"
  3. namespace vendor::qcom::hardware::howie::implementation {
  4. // Methods from ::vendor::qcom::hardware::howie::V1_0::IHowie follow.
  5. Return<void> Howie::helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) {
  6. char buf[128];
  7. ::memset(buf, 0, 128);
  8. ::snprintf(buf, 128, "Hello World, %s", name.c_str());
  9. hidl_string result(buf);
  10. _hidl_cb(result);
  11. return Void();
  12. }
  13. // Methods from ::android::hidl::base::V1_0::IBase follow.
  14. //IHowie* HIDL_FETCH_IHowie(const char* /* name */) {
  15. //return new Howie();
  16. //}
  17. //
  18. } // namespace vendor::qcom::hardware::howie::implementation

我希望hidl接口和实现都编译到vendor分区,因此需要做出一点改动:

vendor.qcom.hardware.howie@1.0的root修改为"vendor.qcom.hardware.howie";

将1.0/Android.bp的"system_ext_specific: true,"改成"vendor: true,";

将1.0/default/Android.bp的"proprietary: true,"改成"vendor: true,",后面碰到编译问题,这一行可以注释掉;

vendor.qcom.hardware.howie@1.0-impl移除relative_install_path: "hw",因为会碰到找不到该动态库的问题;

除此以外,在howie目录下新增Android.bp

  1. subdirs = [
  2. "1.0",
  3. ]
  4. hidl_package_root {
  5. name: "vendor.qcom.hardware.howie",
  6. path: "vendor/qcom/proprietary/interface/howie",
  7. }

3、构建service

为了能调用到先前创建的hidl接口,我们在default下面再创建一个service.cpp,通过直通式注册服务。

  1. #define LOG_TAG "vendor.qcom..hardware.howie@1.0-service"
  2. #include <vendor/qcom/hardware/howie/1.0/IHowie.h>
  3. #include <hidl/LegacySupport.h>
  4. using vendor::qcom::hardware::howie::V1_0::IHowie;
  5. using android::hardware::defaultPassthroughServiceImplementation;
  6. int main() {
  7. return defaultPassthroughServiceImplementation<IHowie>(10);//10表示与/dev/hwbinder通信的最大的线程数
  8. }

绑定式写法:

  1. #define LOG_TAG "vendor.qcom..hardware.howie@1.0-service"
  2. #include <utils/Log.h>
  3. #include "android/log.h"
  4. #include <hidl/HidlSupport.h>
  5. #include <hidl/HidlTransportSupport.h>
  6. #include <utils/String16.h>
  7. #include <vendor/qcom/hardware/howie/1.0/IHowie.h>
  8. #include <hidl/LegacySupport.h>
  9. #include "Howie.h"
  10. using vendor::qcom::hardware::howie::V1_0::IHowie;
  11. using vendor::qcom::hardware::howie::implementation::Howie;
  12. using android::hardware::configureRpcThreadpool;
  13. using android::hardware::joinRpcThreadpool;
  14. using android::sp;
  15. using android::status_t;
  16. using android::OK;
  17. int main() {
  18. configureRpcThreadpool(10, true);
  19. sp<IHowie> howie = new Howie();
  20. status_t status = howie->registerAsService();
  21. LOG_ALWAYS_FATAL_IF(status != OK, "Could not register IHowie");
  22. // other interface registration comes here
  23. joinRpcThreadpool();
  24. return 0;
  25. }

同时在default目录下创建rc文件——vendor.qcom.hardware.howie@1.0-service.rc,用于启动vendor.qcom..hardware.howie@1.0-service。

  1. service howie_service /vendor/bin/hw/vendor.qcom.hardware.howie@1.0-service
  2. class hal
  3. user system
  4. group system

由于新增了两个文件,想要将这两个文件也编进系统,需要再次编辑1.0/default下的Android.bp,如下为新增的:

  1. //新增如下
  2. cc_binary {
  3. name: "vendor.qcom.hardware.howie@1.0-service",
  4. defaults: ["hidl_defaults"],
  5. vendor: true,
  6. relative_install_path: "hw",
  7. srcs: ["service.cpp"],
  8. init_rc: ["vendor.qcom.hardware.howie@1.0-service.rc"],
  9. shared_libs: [
  10. "libhidlbase",
  11. "libhidltransport",
  12. "libutils",
  13. "liblog",
  14. "vendor.qcom.hardware.howie@1.0",
  15. "vendor.qcom.hardware.howie@1.0-impl",
  16. ],
  17. }

4、设备清单和框架清单

为了能为server被client访问到,在howie目录下新增目录vintf,在vintf目录下新增设备清单manifest_vendor.qcom.hardware.howie.xml和框架清单vendor_framework_compatibility_matrix.xml。

  1. <manifest version="1.0" type="device">
  2. <!-- Howie service-->
  3. <hal format="hidl">
  4. <name>vendor.qcom.hardware.howie</name>
  5. <transport>hwbinder</transport>
  6. <version>1.0</version>
  7. <interface>
  8. <name>IHowie</name>
  9. <instance>default</instance>
  10. </interface>
  11. </hal>
  12. </manifest>
  1. <compatibility-matrix version="1.0" type="framework">
  2. <hal format="hidl" optional="true">
  3. <name>vendor.qcom.hardware.howie</name>
  4. <impl level="generic"></impl>
  5. <version>1.0</version>
  6. <interface>
  7. <name>IHowie</name>
  8. <instance>default</instance>
  9. </interface>
  10. </hal>
  11. </compatibility-matrix>

将两个清单文件加入编译,新增vendor_framework_compatibility_matrix.mk

  1. DEVICE_FRAMEWORK_COMPATIBILITY_MATRIX_FILE += \
  2. vendor/qcom/proprietary/interface/howie/vintf/vendor_framework_compatibility_matrix.xml

新增Android.bp

  1. prebuilt_etc_xml {
  2. name: "manifest_vendor.qcom.hardware.howie.xml",
  3. src: "manifest_vendor.qcom.hardware.howie.xml",
  4. vendor: true,
  5. installable: true,
  6. sub_dir: "vintf/manifest",
  7. }

同时,为了vintf下新增的vendor_framework_compatibility_matrix.mk被编译到,在howie目录下新增Android.mk

  1. LOCAL_PATH := $(call my-dir)
  2. include $(call all-makefiles-under,$(LOCAL_PATH))

将vintf也加入到howie下的Android.bp

  1. subdirs = [
  2. "1.0",
  3. "vintf",
  4. ]

5、新增测试client

在1.0目录下新增test目录,在test下面新增如下文件:

HowieTest.cpp

  1. #include <vendor/qcom/hardware/howie/1.0/IHowie.h>
  2. #include <hidl/Status.h>
  3. #include <hidl/LegacySupport.h>
  4. #include <utils/misc.h>
  5. #include <hidl/HidlSupport.h>
  6. #include <stdio.h>
  7. using ::android::hardware::hidl_string;
  8. using ::android::sp;
  9. using vendor::qcom::hardware::howie::V1_0::IHowie;
  10. int main(){
  11. android::sp<IHowie> service = IHowie::getService();
  12. if (service == nullptr){
  13. printf("Failed to get service\n");
  14. return -1;
  15. }
  16. service->helloWorld("Howie", [&](hidl_string result){
  17. printf("%s\n", result.c_str());
  18. });
  19. return 0;
  20. }

Android.bp

  1. cc_binary {
  2. name: "howie_client",
  3. defaults: ["hidl_defaults"],
  4. vendor: true,
  5. relative_install_path: "hw",
  6. srcs: ["HowieTest.cpp"],
  7. shared_libs: [
  8. "liblog",
  9. "libhardware",
  10. "libhidlbase",
  11. "libhidltransport",
  12. "libutils",
  13. "vendor.qcom.hardware.howie@1.0",
  14. ],
  15. }

重新修改目录howie下的Android.bp,将test加入其中。

  1. subdirs = [
  2. "1.0",
  3. "vintf",
  4. "test",
  5. ]

 6、编译

mmm vendor/qcom/proprietary/interfaces/howie

按照之前的编译规则,我们可以在以下的路径中找到编译产物,out/target/product/${PRODUCT}/下,找到这些产物后,push到手机对应目录下即可:

/vendor/lib64/vendor.qcom.hardware.howie@1.0.so

/vendor/lib64/vendor.qcom.hardware.howie@1.0-impl.so

/vendor/bin/hw/vendor.qcom.hardware.howie@1.0-service

/vendor/etc/init/vendor.qcom.hardware.howie@1.0-service.rc

/vendor/etc/vintf/manifest/manifest_vendor.qcom.hardware.howie.xml

产物vendor_framework_compatibility_matrix.xml没找到,需要将手机中的/vendor/etc/vintf/compatibility_matrix.xml手动pull出来,然后添加以下内容,并push手机中原文件位置处。

  1. <hal format="hidl" optional="true">
  2. <name>vendor.qcom.hardware.howie</name>
  3. <impl level="generic"></impl>
  4. <version>1.0</version>
  5. <interface>
  6. <name>IHowie</name>
  7. <instance>default</instance>
  8. </interface>
  9. </hal>

7、运行测试

上述文件全部push后重启手机。重启后,在终端模拟器中做如下操作:

#获取root权限

adb root

#运行service

adb shell

cd vendor/bin/hw

./vendor.qcom.hardware.howie@1.0-service

另起一个终端模拟器

adb shell

cd vendor/bin/hw

./howie_client

这时候,终端模拟器会打印信息:

Hello World, Howie

8、添加HIDL接口的hash值

上述添加hidl接口后,会报VTS问题,这时候需要添加hidl接口的hash值

通过修改hidl-gen.sh,注意,这里需要将之前的生成C++文件和bp文件的语句注释掉,否则前面写的内容就全部没了

  1. # -o <output path>: Location to output files.
  2. # -r <package:path root>: E.g., android.hardware:hardware/interfaces.
  3. # -L <language>
  4. # androidbp : (internal) Generates Soong bp files for -Lc++-headers, -Lc++-sources, -Ljava, -Ljava-constants, and -Lc++-adapter.
  5. # androidbp-impl : Generates boilerplate bp files for implementation created with -Lc++-impl.
  6. # c++-impl : Generates boilerplate implementation of a hidl interface in C++ (for convenience).
  7. PACKAGE=vendor.qcom.hardware.howie@1.0
  8. # 生成Android.bp
  9. #hidl-gen -Landroidbp -rvendor.qcom.hardware.howie:vendor/qcom/proprietary/interfaces/howie -randroid.hidl:system/libhidl/transport $PACKAGE
  10. #LOC=default
  11. # 生成C++文件
  12. #hidl-gen -o $LOC -Lc++-impl -rvendor.qcom.hardware:vendor/qcom/proprietary/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
  13. # 生成Android.bp
  14. #hidl-gen -o $LOC -Landroidbp-impl -rvendor.qcom.hardware:vendor/qcom/proprietary/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE
  15. CURRENT_PATH="$(dirname $(readlink -f "$0"))"
  16. CURRENT_FILE="${CURRENT_PATH}/../current.txt"
  17. if [ ! -f ${CURRENT_FILE} ]
  18. then
  19. touch ${CURRENT_FILE}
  20. fi
  21. hidl-gen -Lhash -rvendor.qcom.hardware.howie:vendor/qcom/proprietary/interfaces/howie $PACKAGE >> ${CURRENT_FILE}

然后运行脚本,就会在vendor/qcom/proprietary/interfaces/howie下看到current.txt文件了

9c2be150b345854b44ea713cbd64ea64e45932f78ff30770e736286100df5777 vendor.qcom.hardware.howie@1.0::IHowie

9、资源链接

androidhidlhowie.zip-Android文档类资源-CSDN下载

参考文章:

[1] HIDL最全编译流程_Gunder的博客-CSDN博客_hidl编译

[2] https://www.jianshu.com/p/fd73ab98e423

[3] https://www.jianshu.com/p/ca6823b897b5

[4] Android P HIDL服务绑定模式与直通模式的分析 (原创) - five.li - 博客园

[5] 添加HIDL接口hash值(解VTS问题)_JoggingPig的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/255960
推荐阅读
相关标签
  

闽ICP备14008679号