Android HAL 之HIDL 的hello world 实现

从事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 android.hardware.hidltest@1.0;

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 android.hardware.hidltest@1.0

==》 生成:out/target/product/rk3399_roc_pc_plus/obj/SHARED_LIBRARIES/android.hardware.hidltest@1.0_intermediates/android.hardware.hidltest@1.0.so.toc

不知道怎么用 ~_~.....

# make -j8 hidl-gen

==> 没生成啥,提示编译成功。

第六步:用 hidl-gen 生成.cpp 和 .h 文件

export PACKAGE=android.hardware.hidltest@1.0
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<void> 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 <android/hardware/hidltest/1.0/IHidlTest.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

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<void> 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<void> 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 <android/hardware/hidltest/1.0/IHidlTest.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>

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<void> 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 "android.hardware.hidltest@1.0-service"

# include <android/hardware/hidltest/1.0/IHidlTest.h>

# include <hidl/LegacySupport.h>

using android::hardware::hidltest::V1_0::IHidlTest;
using android::hardware::defaultPassthroughServiceImplementation;

int main() {
    return defaultPassthroughServiceImplementation<IHidlTest>();
}

再次生成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/android.hardware.hidltest@1.0-service.rc

添加内容到rc

service hidltest_hal_service /vendor/bin/hw/android.hardware.naruto@1.0-service
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: "android.hardware.hidltest@1.0-service",
    init_rc: ["android.hardware.hidltest@1.0-service.rc"],
    srcs: ["service.cpp"],

    cflags: [
        "-Wall",
        "-Werror",
    ],

    shared_libs: [
        "liblog",
        "libdl",
        "libutils",
        "libhardware",
        "libhidlbase",
        "libhidltransport",
        "android.hardware.hidltest@1.0",
        "android.hardware.hidltest@1.0-impl",
    ],

}

 编译

# mmm hardware/interfaces/hidltest/1.0/
# mmm hardware/interfaces/hidltest/1.0/default/

编译生成库

 生成了so库

out/target/product/rk3399_roc_pc_plus/vendor/lib64/hw/android.hardware.hidltest@1.0-impl.so

out/target/product/rk3399_roc_pc_plus/vendor/lib64/android.hardware.hidltest@1.0-adapter-helper.so

out/target/product/rk3399_roc_pc_plus/vendor/bin/hw/android.hardware.hidltest@1.0-service

out/target/product/rk3399_roc_pc_plus/obj/SHARED_LIBRARIES/android.hardware.hidltest@1.0_intermediates/android.hardware.hidltest@1.0.so

执行拷贝命令:

$ adb root
$ adb remount 
$ adb push out/target/product/rk3399_roc_pc_plus/vendor/lib64/hw/android.hardware.hidltest@1.0-impl.so /vendor/lib64
$ adb push out/target/product/rk3399_roc_pc_plus/obj/SHARED_LIBRARIES/android.hardware.hidltest@1.0_intermediates/android.hardware.hidltest@1.0.so  /vendor/lib64
$  adb push out/target/product/rk3399_roc_pc_plus/vendor/bin/hw/android.hardware.hidltest@1.0-service /vendor/bin/hw

运行server 结果如下:

但是不能一值运行,ps -A查看不到服务,暂时还没有找到原因。如果有知道的师兄,可以留言哟~~

此外,本记录参考:

Android HIDL第一个HelloWorld demo_慢慢的燃烧的博客-CSDN博客

Android HIDL第一个demo编写: HIDL Test——实现Framework&App层与HAL进程IPC_Heisenberg海森堡的博客-CSDN博客

  还有测试步骤暂时么有完善。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值