| <html devsite><head> |
| <title>转换 HAL 模块</title> |
| <meta name="project_path" value="/_project.yaml"/> |
| <meta name="book_path" value="/_book.yaml"/> |
| </head> |
| <body> |
| <!-- |
| Copyright 2017 The Android Open Source Project |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --> |
| |
| <p>您可以通过转换 <code>hardware/libhardware/include/hardware</code> 中的标头将预装的 HAL 模块更新为 HIDL HAL 模块。</p> |
| |
| <h2 id="c2hal">使用 c2hal</h2> |
| <p><code><a href="https://android.googlesource.com/platform/system/tools/hidl/+/master/c2hal/">c2hal</a></code> 工具可处理大部分转换工作,从而减少所需进行的手动更改次数。例如,要为 NFC HAL 生成 HIDL <code>.hal</code> 文件,请使用以下命令:</p> |
| <pre class="devsite-click-to-copy"> |
| <code class="devsite-terminal">make c2hal</code> |
| c2hal -r android.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport -p android.hardware.nfc@1.0 hardware/libhardware/include/hardware/nfc.h |
| </pre> |
| |
| <p>这些命令会在 <code>hardware/interfaces/nfc/1.0/</code> 中添加文件。从 $ANDROID_BUILD_TOP 目录运行 <code>hardware/interfaces/update-makefiles.sh</code> 还会向 HAL 添加所需的 makefile。在这里,您可以进行手动更改,以完全转换 HAL。</p> |
| |
| <h2 id="activities">c2hal 操作</h2> |
| <p>当您运行 <code>c2hal</code> 时,标头文件中的所有内容都会转移到 <code>.hal</code> 文件。</p> |
| |
| <p><code>c2hal</code> 会识别在提供的标头文件中包含函数指针的结构体,并将每个结构体转换为单独的接口文件。 |
| 例如,<code>alloc_device_t</code> 会转换为 <code>IAllocDevice</code> HAL 模块(位于文件 <code>IAllocDevice.hal</code> 中)。</p> |
| |
| <p>所有其他数据类型都会复制到 <code>types.hal</code> 文件。 |
| 磅定义已移到枚举中,不属于 HIDL 的项或不可转换的项(例如静态函数声明)会复制到标记有文字“<code>NOTE</code>”的备注中。</p> |
| |
| <h2 id="manual">手动操作</h2> |
| <p><code>c2hal</code> 工具在遇到某些构造时不知该如何应对。例如,HIDL 没有原始指针的概念;因此,当 <code>c2hal</code> 遇到标头文件中的指针时,不知道应将指针解读为数组还是对其他对象的引用。它同样不理解无类型指针。</p> |
| |
| <p>在转换到 HIDL 期间,必须手动移除 <code>int reserved[7]</code> 等字段。应将返回值的名称等项更新为更有意义的内容;例如,将方法的返回参数(例如,NFC 中的 <code>write</code>)从自动生成的 <code>int32_t write_ret</code> 转换为 <code>Status status</code>(其中 <code>Status</code> 是包含可能的 NFC 状态的新枚举)。</p> |
| |
| <h2 id="implement">实现 HAL</h2> |
| <p>创建 <code>.hal</code> 文件以表示您的 HAL 后,您必须生成在 C++ 和 Java 中创建语言支持的 makefile(Make 或 Soong),除非 HAL 使用的功能在 Java 中不受支持。<code>./hardware/interfaces/update-makefiles.sh</code> 脚本可以为 <code>hardware/interfaces</code> 目录中的 HAL 自动生成 makefile(对于其他位置的 HAL,只需更新脚本即可)。</p> |
| |
| <p>如果 makefile 是最新版本,则表示您已准备好生成标头文件和实现方法了。要详细了解如何实现生成的接口,请参阅 <a href="/devices/architecture/hidl-cpp/index.html">HIDL C++</a>(用于 C++ 实现)或 <a href="/devices/architecture/hidl-java/index.html">HIDL Java</a>(用于 Java 实现)。</p> |
| |
| </body></html> |