HIDL demo

简单记录,可以直接使用。

1.添加hal目录、定义hal接口

1
2
foo@bar:~$ mkdir -p vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/default
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/IHello.hal

接口如下:

1
2
3
4
5
6
7
//IHello.hal

package vendor.realtek.hello@1.0;

interface IHello {
helloWorld (string name) generates (string result);
};

2.根据hal自动生成cpp实现

1
2
3
4
5
6
foo@bar:~$ source build/envsetup.sh
foo@bar:~$ lunch
foo@bar:~$ make hidl-gen
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/genImpHidl.sh
foo@bar:~$ chmod u+x vendor/realtek/common/ATV/hardware/interfaces/hello/genImpHidl.sh
foo@bar:~$ ./vendor/realtek/common/ATV/hardware/interfaces/hello/genImpHidl.sh
1
2
3
4
5
6
7
8
9
//genImpHidl.sh

PACKAGE=vendor.realtek.hello@1.0
LOC=vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/default

hidl-gen -o $LOC -Lc++-impl -rvendor.realtek:vendor/realtek/common/ATV/hardware/interfaces \
-randroid.hidl:system/libhidl/transport $PACKAGE
hidl-gen -o $LOC -Landroidbp-impl -rvendor.realtek:vendor/realtek/common/ATV/hardware/interfaces \
-randroid.hidl:system/libhidl/transport $PACKAGE

执行genImpHidl.sh后,default目录生成Android.bp、Hello.cpp和Hello.h文件。

1_genImplHidl.png

编写Hello.cpp

1
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/default/Hello.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//Hello.cpp

// FIXME: your file license if you have one

#include "Hello.h"

namespace vendor {
namespace realtek {
namespace hello {
namespace V1_0 {
namespace implementation {

// Methods from ::vendor::realtek::hello::V1_0::IHello follow.
Return<void> Hello::helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) {
// TODO implement
char buf[100];
::memset(buf,0,100);
::snprintf(buf, 100, "Hello World, %s", name.c_str());
hidl_string result(buf);
_hidl_cb(result);
return Void();
}


// Methods from ::android::hidl::base::V1_0::IBase follow.

//IHello* HIDL_FETCH_IHello(const char* /* name */) {
//return new Hello();
//}
//
} // namespace implementation
} // namespace V1_0
} // namespace hello
} // namespace realtek
} // namespace vendor

HIDL的实现有两种方式,如果打开HIDL_FETCH的注释,就是passthrough,否则是Binderized方式,这里使用默认的Binderized方式。

3.开机启动服务

添加service.cpp文件;

1
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/default/service.cpp

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//service.cpp

#define LOG_TAG "vendor.realtek.hello@1.0-service"

#include <vendor/realtek/hello/1.0/IHello.h>
#include <hidl/HidlTransportSupport.h>

#include "Hello.h"

using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using vendor::realtek::hello::V1_0::IHello;
using vendor::realtek::hello::V1_0::implementation::Hello;
using android::sp;
using android::status_t;
using android::OK;

using namespace vendor::realtek::hello::V1_0;

int main() {
// TODO: tune the max thread count.
configureRpcThreadpool(10, true);
sp<IHello> mHello = new Hello();
status_t status = mHello->registerAsService();
LOG_ALWAYS_FATAL_IF(status != OK, "Could not register IHello");
ALOGD("IHello vendor.realtek.hello@1.0-service start\n");
// other interface registration comes here
joinRpcThreadpool();
return 0;
}

添加vendor.realtek.hello@1.0-service.rc文件;

1
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/default/vendor.realtek.hello@1.0-service.rc

内容如下:

1
2
3
4
5
//vendor.realtek.hello@1.0-service.rc
service hello-1-0 /vendor/bin/hw/vendor.realtek.hello@1.0-service
class hal
user system
group system

修改Android.bp;

1
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/default/Android.bp

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//hello/1.0/default/Android.bp

// FIXME: your file license if you have one

cc_library_shared {
// FIXME: this should only be -impl for a passthrough hal.
// In most cases, to convert this to a binderized implementation, you should:
// - change '-impl' to '-service' here and make it a cc_binary instead of a
// cc_library_shared.
// - add a *.rc file for this module.
// - delete HIDL_FETCH_I* functions.
// - call configureRpcThreadpool and registerAsService on the instance.
// You may also want to append '-impl/-service' with a specific identifier like
// '-vendor' or '-<hardware identifier>' etc to distinguish it.
name: "vendor.realtek.hello@1.0-impl",
relative_install_path: "hw",
// FIXME: this should be 'vendor: true' for modules that will eventually be
// on AOSP.
proprietary: true,
srcs: [
"Hello.cpp",
],
shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"vendor.realtek.hello@1.0",
],
}

cc_binary {
name: "vendor.realtek.hello@1.0-service",
defaults: ["hidl_defaults"],
proprietary: true,
relative_install_path: "hw",
srcs: [
"Hello.cpp",
"service.cpp"
],
init_rc: ["vendor.realtek.hello@1.0-service.rc"],
shared_libs: [
"liblog",
"libhidlbase",
"libhidltransport",
"libutils",
"libcutils",
"libbinder",
"vendor.realtek.hello@1.0",
],
}

4.生成hal接口的Android.bp

看下update-makefiles.sh内容是什么:

1
2
3
4
5
6
7
#!/bin/bash

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

do_makefiles_update \
"vendor.realtek:vendor/realtek/common/ATV/hardware/interfaces" \
"android.hidl:system/libhidl/transport"

执行该脚本:

1
foo@bar:~$ ./vendor/realtek/common/ATV/hardware/interfaces/update-makefiles.sh

1.0目录就生成了Android.bp文件。

2_update-makefiles.png

5.更新哈希值

编写genHash.sh

1
2
3
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/genHash.sh
foo@bar:~$ chmod u+x vendor/realtek/common/ATV/hardware/interfaces/hello/genHash.sh
foo@bar:~$ ./vendor/realtek/common/ATV/hardware/interfaces/hello/genHash.sh

内容如下:

1
2
3
PACKAGE=vendor.realtek.hello@1.0::IHello

hidl-gen -L hash -rvendor.realtek:vendor/realtek/common/ATV/hardware/interfaces/ -randroid.hidl:system/libhidl/transport $PACKAGE

把输出的hash添加到current.txt

1
foo@bar:~$ vim ./vendor/realtek/common/ATV/hardware/interfaces/current.txt

6.添加client测试

添加如下目录和文件:

1
2
3
foo@bar:~$ mkdir -p vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/vts/functional
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/vts/functional/VtsHalHelloV1_0TargetTest.cpp
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/1.0/vts/functional/Android.bp

以下是其内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cc_test {
name: "VtsHalHelloV1_0TargetTest",
defaults: ["hidl_defaults"],
srcs: ["VtsHalHelloV1_0TargetTest.cpp"],
shared_libs: [
"libbase",
"libhidlbase",
"liblog",
"libutils",
"vendor.realtek.hello@1.0",
],
static_libs: ["VtsHalHidlTargetTestBase"],
cflags: [
"-O0",
"-g",
]
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//VtsHalHelloV1_0TargetTest.cpp

#define LOG_TAG "HelloHidlHalTest"

#include <VtsHalHidlTargetTestBase.h>
#include <android-base/logging.h>

#include <vendor/realtek/hello/1.0/IHello.h>

using vendor::realtek::hello::V1_0::IHello;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;

int main()
{
int ret;

sp<IHello> hello = IHello::getService();

if(hello == nullptr) {
ALOGD("Failed to get hello service\n");
ret = -1;
} else {
hello->helloWorld("Hikari", [&](hidl_string result) {
ALOGD("%s\n",result.c_str());
});
ret = 0;
}

return ret;
}

7.添加hello目录的Android.bp

跑update-makefiles.sh的时候为啥没有生成这个? 跟rtk代码makefile架构有关吗。

1
foo@bar:~$ vim vendor/realtek/common/ATV/hardware/interfaces/hello/Android.bp

内容如下:

1
2
3
4
5
// This is an autogenerated file, do not edit.
subdirs = [
"1.0",
"1.0/vts/functional",
]

8.小编

hello最终文件架构如下:
3_end.png

小编试试添加的hello能不能编过:

1
foo@bar:~$ mmm vendor/realtek/common/ATV/hardware/interfaces/hello/

9.注册hello并添加进编译

注册:

1
foo@bar:~$ vim device/realtek/common/manifest.xml

添加内容如下:

1
2
3
4
5
6
7
8
9
<hal format="hidl">
<name>vendor.realtek.hello</name>
<transport>hwbinder</transport>
<version>1.0</version>
<interface>
<name>IHello</name>
<instance>default</instance>
</interface>
</hal>

编译:

1
foo@bar:~$ vim device/realtek/common/product/rtk_base.mk

添加内容如下:

1
2
3
4
PRODUCT_PACKAGES += \
vendor.realtek.hello@1.0 \
vendor.realtek.hello@1.0-impl \
vendor.realtek.hello@1.0-impl \

10.添加seLinux规则

如果没有此步骤,开机之后vendor.realtek.hello@1.0-service会跑不起来。
修改file_contexts文件

1
foo@bar:~$ vim device/realtek/common/sepolicy/file_contexts

补充如下:

1
/(vendor|system/vendor)/bin/hw/vendor\.realtek\.hello@1\.0-service        u:object_r:hal_hello_default_exec:s0

修改hwservice_contexts文件

1
foo@bar:~$ vim device/realtek/common/sepolicy/hwservice_contexts

补充如下:

1
vendor.realtek.hello::IHello        u:object_r:hal_hello_service:s0

修改hwservice.te文件

1
foo@bar:~$ vim device/realtek/common/sepolicy/hwservice.te

补充如下:

1
type hal_hello_service,   hwservice_manager_type;

添加hal_hello_default.te文件

1
foo@bar:~$ vim device/realtek/common/sepolicy/hal_hello_default.te

内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type hal_hello_default, domain;
type hal_hello_default_exec, exec_type, vendor_file_type, file_type;

init_daemon_domain(hal_hello_default);

hwbinder_use(hal_hello_default)
add_hwservice(hal_hello_default, hal_hello_service)


allow hal_hello_default hwservicemanager_prop:file r_file_perms;
allow hal_hello_default hwservicemanager:binder { transfer call };
allow hal_hello_default hal_hello_service:binder call;
allow hal_hello_default hal_hello_service:hwservice_manager { add find };
allow hal_hello_default hidl_base_hwservice:hwservice_manager add;

到此为止,开机之后服务依然有可能跑不起来,可以dmesg查看权限相关的avc报错,把相关的报错修掉之后,还有可能跑不起来,直接运行./vendor/bin/hw/vendor.realtek.hello@1.0-service,把avc denied修掉,应该就可以了。如果不确定是不是权限问题引起,可以先setenforce 0后直接运行服务,如果能跑起来,那么就是权限有问题。另外,客户端调用的时候如果看不到效果,可以先用client(参考第6点)调试。

11.全编代码验证

开机可以看到vendor.realtek.hello@1.0-service的log:

01-01 01:01:03.490 328 328 I ServiceManagement: Registered vendor.realtek.hello@1.0::IHello/default (start delay of 261ms)
01-01 01:01:03.491 328 328 I ServiceManagement: Removing namespace from process name vendor.realtek.hello@1.0-service to hello@1.0-service.
01-01 01:01:03.491 328 328 D vendor.realtek.hello@1.0-service: IHello vendor.realtek.hello@1.0-service start

在data/nativetest/VtsHalHelloV1_0TargetTest目录可以找到VtsHalHelloV1_0TargetTest文件,这个就是测试client,执行这个程序:

1
foo@bar:~$ ./data/nativetest/VtsHalHelloV1_0TargetTest/VtsHalHelloV1_0TargetTest

可以看到:

08-04 10:06:24.211 2063 2063 D HelloHidlHalTest: Hello World, Hikari