从事Android BSP开发一年有余,都是在驱动上的工作,每每涉及HAL层面的工作,总是“望而却步”却又心有不甘。前期系统性的学习了一些C++(记录整理后补上)后,再反过来看HAL相关的code,稍有熟悉感。趁热打铁,小试牛刀,尝试一下HIDL 的hal。
平台介绍:firefly RK 3399 pc pro, 系统是Android10。
第一步:创建文件夹
# mkdir -p hardware/interfaces/hidltest/1.0
第二步:新建 .hal 文件
# touch hardware/interfaces/hidltest/1.0/IHidlTest.hal
第三步: .hal 添加接口声明
package [email protected];
interface IHidlTest {
//声明了一个函数名为 helloWorld、参数为 字符串、返回值为 字符串 类型的接口。
helloWorld(string name) generates (string result);
};
第四步:执行 hardware/interface/ 1.0/下的脚本生成 Android.bp 文件
# ./hardware/interfaces/update-makefiles.sh
此时,hidltest 文件夹下的目录为
.
└── 1.0
├── Android.bp
└── IHidlTest.hal
1 directory, 2 files
第五步:编译试试
# make -j8 [email protected]
==》 生成:out/target/product/rk3399_roc_pc_plus/obj/SHARED_LIBRARIES/[email protected]_intermediates/[email protected]
不知道怎么用 ~_~.....
# make -j8 hidl-gen
==> 没生成啥,提示编译成功。
第六步:用 hidl-gen 生成.cpp 和 .h 文件
export [email protected]
export LOC=./hardware/interfaces/hidltest/1.0/default
hidl-gen -o $LOC -L c++-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
执行完成后,hidltest 文件夹下的目录为
readme 是手动添加的一个说明文档;HidlTest.cpp and HidlTest.h 是通过以上 hidl-gen 生成的。
HidlTest.cpp 有些什么内容?
// FIXME: your file license if you have one
#include "HidlTest.h"
namespace android {
namespace hardware {
namespace hidltest {
namespace V1_0 {
namespace implementation {
// Methods from ::android::hardware::hidltest::V1_0::IHidlTest follow.
Return HidlTest::helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) {
// TODO implement
return Void();
}
// Methods from ::android::hidl::base::V1_0::IBase follow.
//IHidlTest* HIDL_FETCH_IHidlTest(const char* /* name */) {
//return new HidlTest();
//}
//
} // namespace implementation
} // namespace V1_0
} // namespace hidltest
} // namespace hardware
} // namespace android
Hidltest.h 文件中的内容
// FIXME: your file license if you have one
#pragma once
#include
#include
#include
namespace android {
namespace hardware {
namespace hidltest {
namespace V1_0 {
namespace implementation {
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
struct HidlTest : public IHidlTest {
// Methods from ::android::hardware::hidltest::V1_0::IHidlTest follow.
Return helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) override;
// Methods from ::android::hidl::base::V1_0::IBase follow.
};
// FIXME: most likely delete, this is only for passthrough implementations
// extern "C" IHidlTest* HIDL_FETCH_IHidlTest(const char* name);
} // namespace implementation
} // namespace V1_0
} // namespace hidltest
} // namespace hardware
} // namespace android
第七步:在Hidltest.cpp添加功能 , 在 Hidltest.h中选择hidl实现的方式
cpp文件
// FIXME: your file license if you have one
#include "HidlTest.h"
namespace android {
namespace hardware {
namespace hidltest {
namespace V1_0 {
namespace implementation {
// Methods from ::android::hardware::hidltest::V1_0::IHidlTest follow.
Return HidlTest::helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) {
// TODO implement
char buf[128];
::memset(buf, 0, 128);
::snprintf(buf, 128, "Hello World, %s", name.c_str());
hidl_string result(buf);
_hidl_cb(result);
return Void();
}
// Methods from ::android::hidl::base::V1_0::IBase follow.
//IHidlTest* HIDL_FETCH_IHidlTest(const char* /* name */) {
//return new HidlTest();
//}
//
} // namespace implementation
} // namespace V1_0
} // namespace hidltest
} // namespace hardware
} // namespace android
添加红框内的code
h头文件
// FIXME: your file license if you have one
#pragma once
#include
#include
#include
namespace android {
namespace hardware {
namespace hidltest {
namespace V1_0 {
namespace implementation {
using ::android::hardware::hidl_array;
using ::android::hardware::hidl_memory;
using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;
struct HidlTest : public IHidlTest {
// Methods from ::android::hardware::hidltest::V1_0::IHidlTest follow.
Return helloWorld(const hidl_string& name, helloWorld_cb _hidl_cb) override;
// Methods from ::android::hidl::base::V1_0::IBase follow.
};
// FIXME: most likely delete, this is only for passthrough implementations
extern "C" IHidlTest* HIDL_FETCH_IHidlTest(const char* name);
} // namespace implementation
} // namespace V1_0
} // namespace hidltest
} // namespace hardware
} // namespace android
打开红框内的code
这里很多都是多说选择了Passthrough模式,还有一个是Binderized模式,具体而这有何区别,了解后写入专题。
第八步: 添加server.cpp 和编译脚本
这里产生一个问题:server.cpp 是用来干嘛的?
创建server.cpp文件
# touch hardware/interfaces/hidltest/1.0/default/service.cpp
添加code到 server.cpp
# define LOG_TAG "[email protected]"
# include
# include
using android::hardware::hidltest::V1_0::IHidlTest;
using android::hardware::defaultPassthroughServiceImplementation;
int main() {
return defaultPassthroughServiceImplementation();
}
再次生成Android.bp
# hidl-gen -o $LOC -L androidbp-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
增加了 defalut 文件夹下面的Android.bp 文件。
第八步:创建rc文件
# touch hardware/interfaces/hidltest/1.0/default/[email protected]
添加内容到rc
service hidltest_hal_service /vendor/bin/hw/[email protected]
class hal
user system
group system
rc 文件主要是限制 hidltest service的用户和用户组的权限。
第九步: 添加default 目录下的Android.bp 添加 server.cpp编译的指令并尝试编译
cc_binary {
proprietary: true,
relative_install_path: "hw",
defaults: ["hidl_defaults"],
name: "[email protected]",
init_rc: ["[email protected]"],
srcs: ["service.cpp"],
cflags: [
"-Wall",
"-Werror",
],
shared_libs: [
"liblog",
"libdl",
"libutils",
"libhardware",
"libhidlbase",
"libhidltransport",
"[email protected]",
"[email protected]",
],
}
编译
# mmm hardware/interfaces/hidltest/1.0/
# mmm hardware/interfaces/hidltest/1.0/default/
编译生成库
生成了so库
out/target/product/rk3399_roc_pc_plus/vendor/lib64/hw/[email protected]
out/target/product/rk3399_roc_pc_plus/vendor/lib64/[email protected]
out/target/product/rk3399_roc_pc_plus/vendor/bin/hw/[email protected]
out/target/product/rk3399_roc_pc_plus/obj/SHARED_LIBRARIES/[email protected]_intermediates/[email protected]
执行拷贝命令:
$ adb root
$ adb remount
$ adb push out/target/product/rk3399_roc_pc_plus/vendor/lib64/hw/[email protected] /vendor/lib64
$ adb push out/target/product/rk3399_roc_pc_plus/obj/SHARED_LIBRARIES/[email protected]_intermediates/[email protected] /vendor/lib64
$ adb push out/target/product/rk3399_roc_pc_plus/vendor/bin/hw/[email protected] /vendor/bin/hw
运行server 结果如下:
但是不能一值运行,ps -A查看不到服务,暂时还没有找到原因。如果有知道的师兄,可以留言哟~~
此外,本记录参考:
Android HIDL第一个HelloWorld demo_慢慢的燃烧的博客-CSDN博客
Android HIDL第一个demo编写: HIDL Test——实现Framework&App层与HAL进程IPC_Heisenberg海森堡的博客-CSDN博客
还有测试步骤暂时么有完善。