自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
转换 HAL 模块
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
您可以通过转换 hardware/libhardware/include/hardware
中的头文件,将已有的 HAL 模块更新为 HIDL HAL 模块。
使用 c2hal
c2hal
工具可处理大部分转换工作,从而减少所需的手动更改次数。例如,如需为 NFC HAL 生成 HIDL .hal
文件,请使用以下命令:
make c2hal
c2hal -r android.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport -p android.hardware.nfc@1.0 hardware/libhardware/include/hardware/nfc.h
这些命令会在 hardware/interfaces/nfc/1.0/
中添加文件。从 $ANDROID_BUILD_TOP
目录运行 hardware/interfaces/update-makefiles.sh
还会向 HAL 添加所需的 makefile。在这里,您可以进行手动更改,以完全转换 HAL。
c2hal 操作
当您运行 c2hal
时,头文件中的所有内容都会转移到 .hal
文件。
c2hal
会识别提供的头文件中包含函数指针的结构体,并将每个结构体转换为单独的接口文件。例如,alloc_device_t
会转换为 IAllocDevice
HAL 模块(在 IAllocDevice.hal
文件中)。
其他所有数据类型都会复制到 types.hal
文件中。宏定义会移动到枚举中,而不属于 HIDL 的项或不可转换的项(如静态函数声明)会复制到标记有文字“NOTE
”的备注中。
手动操作
c2hal
工具在遇到某些构造时不知该如何应对。例如,HIDL 没有原始指针的概念;因此,当 c2hal
遇到头文件中的指针时,不知道应将指针解释为数组还是对其他对象的引用。它同样不理解无类型指针。
在转换为 HIDL 期间,必须手动移除 int reserved[7]
等字段。应将返回值的名称等项更新为更有意义的内容;例如,将方法的返回参数(如 NFC 中的 write
)从自动生成的 int32_t write_ret
转换为 Status status
(其中,Status
是包含可能的 NFC 状态的新枚举)。
实现 HAL
创建 .hal
文件以表示您的 HAL 后,您必须生成在 C++ 和 Java 中创建语言支持的 makefile(Make 或 Soong),除非 HAL 使用的功能在 Java 中不受支持。./hardware/interfaces/update-makefiles.sh
脚本可以为 hardware/interfaces
目录中的 HAL 自动生成 makefile(对于其他位置的 HAL,只需更新该脚本即可)。
当 makefile 是最新版本时,您就可以生成头文件并实现方法了。如需详细了解如何实现生成的接口,请参阅 HIDL C++(对于 C++ 实现)或 HIDL Java(对于 Java 实现)。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Convert HAL modules\n\nYou can update preexisting HAL modules to HIDL HAL modules by converting the\nheader in `hardware/libhardware/include/hardware`.\n\nUse c2hal\n---------\n\nThe\n[c2hal](https://android.googlesource.com/platform/system/tools/hidl/+/android16-release/c2hal/)\ntool handles most of the conversion work, reducing the number of required manual\nchanges. For example, to generate a HIDL `.hal` file for the NFC\nHAL: \n\n make c2hal\n c2hal -r android.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport -p android.hardware.nfc@1.0 hardware/libhardware/include/hardware/nfc.h\n\nThese commands add files in `hardware/interfaces/nfc/1.0/`. Running\n`hardware/interfaces/update-makefiles.sh` from the `$ANDROID_BUILD_TOP`\ndirectory also adds the required makefile to the HAL. From here, you can\nmake manual changes to fully convert the HAL.\n\nc2hal activities\n----------------\n\nWhen you run `c2hal`, everything in the header file is transferred\nto `.hal` files.\n\n`c2hal` identifies structs that contain function pointers in the\nprovided header file and converts each struct into a separate interface file.\nFor example, `alloc_device_t` is converted to the\n`IAllocDevice` HAL module (in the file\n`IAllocDevice.hal`).\n\nAll other data types are copied over into a `types.hal` file.\nPound-defines are moved into enums, and items not a part of HIDL or not\nconvertible (such as static-function declarations) are copied into comments\nmarked with the text \"`NOTE`\".\n\nManual activities\n-----------------\n\nThe `c2hal` tool doesn't know what to do when it encounters\ncertain constructs. For example, HIDL has no concept of raw pointers; because of\nthis, when `c2hal` encounters a pointer in header files, it doesn't\nknow whether the pointer should be interpreted as an array or as a reference to\nanother object. Void pointers are also similarly opaque.\n\nField such as `int reserved[7]` must be manually removed during\nthe transition to HIDL. Items such as the name of the return value should be\nupdated to something more meaningful; for example, converting the return\nparameter of methods such as `write` in NFC from the autogenerated\n`int32_t write_ret` to `Status status` (where\n`Status` is a new enum containing possible NFC statuses).\n\nImplement the HAL\n-----------------\n\nAfter you have created `.hal` files to represent your HAL, you\nmust generate the makefiles (Make or Soong) that create the language support in\nC++ and Java (unless the HAL uses a feature unsupported in Java). The\n`./hardware/interfaces/update-makefiles.sh` script can automatically\ngenerate makefiles for HALs located in the `hardware/interfaces`\ndirectory (for HALs in other locations, simply update the script).\n\nWhen the makefiles are up to date, you are ready to generate header files and\nimplement methods. For details on implementing the generated interface, see\n[HIDL C++](/docs/core/architecture/hidl-cpp) (for C++\nimplementations) or [HIDL\nJava](/docs/core/architecture/hidl-java) (for Java implementations)."]]