安卓HAL开发指南

1、介绍

HIDL的全称是HAL interface definition language(硬件抽象层接口定义语言),在此之前Android 有AIDL,架构在Android binder 之上,用来定义Android 基于Binder通信的Client 与Service之间的接口。HIDL也是类似的作用,只不过定义的是Android Framework与Android HAL实现之间的接口。

android luncher开发 android hal开发_android

2、实现方式

2.1 旧版

传统HAL层

android luncher开发 android hal开发_xml_02

  • Android 7.x和更早的版本中,hal模块接口被定义为简单的C头文件。
  • HAL的实现 驻留在客户端的进程中(在很多情况下它是系统服务器),这导致了安全性和稳定性问题。
  • 版本控制发生在HAL接口规范级别。

2.2 新版

HIDL hal层

android luncher开发 android hal开发_Android_03

  • 在Android 8.0及更高版本中,底层被重写以采用新的、更模块化的架构。
  • 运行Android 8.0及更高版本的设备必须支持用HIDL编写的HALs

2.3 Vendor Interface

android luncher开发 android hal开发_xml_04

2.4 Vendor Test Suite(VTS)

android luncher开发 android hal开发_android_05

3.HIDL接口定义

HIDL | Android 开源项目 | Android Open Source Project (google.cn)

3.1 HIDL文件目录

在AOSP中,HIDL接口文件的路径如下:

  1. /hardware/interfaces
  2. /framework/hardware/interfaces
  3. /system/hardware/interfaces
  4. /system/libhidl/transport

实现车载音频 HAL | Android 开源项目 | Android Open Source Project (google.cn)

3.2 HIDL实例

android luncher开发 android hal开发_Android_06


android luncher开发 android hal开发_android_07

3.3 支持的数据格式

  • 基本类型: char, short, int, long float, double bool
  • 特殊类型: string,vec,数组,handle(非Java兼容的)
  • 结构体
  • union(非Java兼容的)
  • 枚举
  • 快速消息队列(基于环形缓冲区的队列)
  • memory

3.4 默认实现

  • 所有的HALs必须有默认实现
  • 在许多情况下,为了向后兼容,它只是对现有libhardware(传统的HAL)实现的包装
  • 默认实现与接口定义一起位于默认文件夹中。

4 如何创建HIDL HAL

(for Android Q)

4.1 在接口目录中创建你的模块

For Android common:

android/vendor/noch/common/interfaces

android luncher开发 android hal开发_android luncher开发_08

For v3.5 8155 project:

android/vendor/noch/project/v3.5/sa8155/interfaces

android luncher开发 android hal开发_android_09

For v3.5 6155 project:

android/vendor/noch/project/v3.5/sa6155/interfaces

android luncher开发 android hal开发_Android_10

现在开始创建示例HAL:

android luncher开发 android hal开发_xml_11

创建如下目录结构

android luncher开发 android hal开发_Android_12

4.2 创建接口文件

android luncher开发 android hal开发_xml_13

4.2.1 type definition

android luncher开发 android hal开发_Android_14

4.2.2 function interface

android luncher开发 android hal开发_android_15


android luncher开发 android hal开发_xml_16

4.2.3 callback interface

android luncher开发 android hal开发_Android_17

4.3 生成Android.bp文件

the working directory is /vendor/noch/common/interfaces/example/1.0

4.3.1 准备Android环境

source build/envsetup.sh
lunch sa8155_v35-userdebug

4.3.2 调用update-makefiles.sh

update-makefiles.sh文件如下:

#!/bin/bash
# Script to update Android make-files for HAL and VTS modules.


source $ANDROID_BUILD_TOP/system/tools/hidl/update-makefiles-helper.sh

do_makefiles_update \
  "vendor.gwm.common.hardware:vendor/noch/common/interfaces" \
  "android.hidl:system/libhidl/transport"
cd android/vendor/noch/common/interfaces/example/1.0
../../hidl-scripts/update-makefiles.sh

android luncher开发 android hal开发_android_18


Android.bp

android luncher开发 android hal开发_Android_19

4.4 generate HIDL source code

generate-source.sh文件如下:

#! /bin/bash
# Nobo Automotive Kun Li 
# v1.0 

if [ -z $1 ]; then
    echo "Please input module name"
    exit 1
else 
    MODULE=$1
fi


PACKAGE=vendor.gwm.common.hardware.$MODULE@1.0
echo "You package is: $PACKAGE"

LOC=default/impl
#make hidl-gen -j64
hidl-gen -o $LOC -Lc++-impl -rvendor.gwm.common.hardware:vendor/noch/common/interfaces \
	    -randroid.hidl:system/libhidl/transport $PACKAGE
hidl-gen -o $LOC -Landroidbp-impl -rvendor.gwm.common.hardware:vendor/noch/common/interfaces \
	    -randroid.hidl:system/libhidl/transport $PACKAGE
../../hidl-scripts/generate-source.sh example

android luncher开发 android hal开发_xml_20


运行generate-source.sh时后面需加上模块名,这里是example

4.4.1 Example.h

android luncher开发 android hal开发_android_21


android luncher开发 android hal开发_Android_22


android luncher开发 android hal开发_android_23

4.4.2 Example.cpp

android luncher开发 android hal开发_android_24


android luncher开发 android hal开发_android_25


android luncher开发 android hal开发_android luncher开发_26

4.4.3 ExampleCallback.h

android luncher开发 android hal开发_android luncher开发_27


android luncher开发 android hal开发_android luncher开发_28

4.4.4 ExampleCallback.cpp

android luncher开发 android hal开发_android_29


android luncher开发 android hal开发_android_30


android luncher开发 android hal开发_android_31

4.5 创建项目编译环境

4.5.1 将代码文件移动到相关目录中

cd android/vendor/noch/common/interfaces/example/1.0/default/impl
mkdir include src

android luncher开发 android hal开发_android luncher开发_32

mv *.h include
mv *.cpp src 
mv Android.bp ../

4.5.2 Create Service file

cd android/vendor/noch/common/interfaces/example/1.0/default
touch ExampleService.cpp

android luncher开发 android hal开发_android luncher开发_33


此时的目录结构

android luncher开发 android hal开发_Android_34

4.5.3 create Service Script

vi vendor.noch.common.hardware.example@1.0-service.rc

android luncher开发 android hal开发_xml_35

4.5.4 修改Android.bp文件

cd android/vendor/noch/common/interfaces/example/1.0/default
vi Android.bp

android luncher开发 android hal开发_android_36


android luncher开发 android hal开发_android_37


android luncher开发 android hal开发_xml_38

4.6 Example Hal的实现

Example.cpp

android luncher开发 android hal开发_android_39


android luncher开发 android hal开发_Android_40


android luncher开发 android hal开发_android luncher开发_41

ExampleCallback.cpp

android luncher开发 android hal开发_xml_42


android luncher开发 android hal开发_Android_43

4.7 Debug build

cd android/vendor/noch/common/interfaces/example/1.0/default
mm

android luncher开发 android hal开发_Android_44

vendor.noch.common.hardware.example@1.0-service 将会install在vendor/bin/hw中
vendor.noch.common.hardware.example@1.0-service.rc 将会install在vendor/etc/init中

恭喜你,至此,你已经成功创建了HAL服务,你也可以将其push到vendor模块中去调试它。
但是,它不会在系统启动时自动运行
因为:

  1. 他没有在Android系统中注册
  2. 他没有在SELINUX中注册

4.8 在Android系统中注册

  1. For Android common(this example) cd android/device/noch/common
  2. For v3.5 8155 project cd android/device/noch/sa8155_v35/sepolicy
  3. For v3.5 6155 project cd android/device/noch/sa6155_v35/sepolicy

4.8.1 修改Android Manifest

vi common_manifest_overrides.xml

android luncher开发 android hal开发_android_45

4.8.2 修改Android compatibility Matrix

vi common_compatibility_matrix.xml

android luncher开发 android hal开发_android luncher开发_46

4.9 在SELINUX中注册

  1. For Android common(this example) cd
    android/device/noch/common/sepolicy
  2. For v3.5 8155 project cd android/device/noch/sa8155_v35/sepolicy
  3. For v3.5 6155 project cd android/device/noch/sa6155_v35/sepolicy

4.9.1 modify file_contexts to Register HAL

vi file_contexts

android luncher开发 android hal开发_Android_47

4.9.2 create sepolicy rule

vi hal_example.te

android luncher开发 android hal开发_android luncher开发_48

4.10 全编

android luncher开发 android hal开发_android_49


android luncher开发 android hal开发_xml_50

这样example HAL服务就能自动运行在目标板子上了

4.11 创建测试程序

cd android/vendor/noch/common/interfaces/example/1.0/default/tests
vi ExampleHalClient.cpp

android luncher开发 android hal开发_xml_51


android luncher开发 android hal开发_xml_52


android luncher开发 android hal开发_android luncher开发_53


android luncher开发 android hal开发_android_54

4.11 更新Android.bp文件

cd android/vendor/noch/common/interfaces/example/1.0/default
vi Android.bp

android luncher开发 android hal开发_android luncher开发_55

mm

android luncher开发 android hal开发_android_56

你可以将vendor.noch.common.hardware.example@1.0-client推到板子里的目标路径/vendor/bin/中