Android 9.0 编写HIDL

本文档详细介绍了如何在Android系统中创建和使用硬件接口描述语言(HIDL)服务。从创建接口定义开始,到构建服务提供商和服务消费者,再到将服务注册到系统并允许客户端访问,每个步骤都通过代码示例进行了清晰的说明。整个过程涵盖了HAL的实现、服务启动脚本、客户端测试以及构建配置的更新。
摘要由CSDN通过智能技术生成


#<1>
mkdir -p hardware/interfaces/hello/1.0

#<2>
cat > hardware/interfaces/hello/1.0/IHello.hal << EOF
package android.hardware.hello@1.0; 
interface IHello {
    //声明了一个函数名为 sayHello、参数为 字符串、返回值为 字符串 类型的接口。
    sayHello(string name) generates (string result);
};
EOF

#<3>update-makefiles.sh 1.0 Android.bp
./hardware/interfaces/update-makefiles.sh

#<4> gen tmpl
PACKAGE=android.hardware.hello@1.0
LOC=./hardware/interfaces/hello/1.0/default
# hidl-gen C++ files
hidl-gen -o $LOC -L c++-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE
# hidl-gen Android.bp
hidl-gen -o $LOC -L androidbp-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport $PACKAGE

sayHello函数实现
    char buf[128];
    ::memset(buf, 0, 128);
    ::snprintf(buf, 128, "Hello World, %s", name.c_str());
    
    hidl_string result(buf);
    _hidl_cb(result);

#<5> service provider
cat > hardware/interfaces/hello/1.0/default/service.cpp << EOF
# define LOG_TAG "android.hardware.hello@1.0-service"

#include <hidl/HidlTransportSupport.h>
#include <android/hardware/hello/1.0/IHello.h>

#include "Hello.h"

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

int main() {
    configureRpcThreadpool(4, true);
    sp<IHello> hello = new Hello();
    if(hello->registerAsService() != OK) {
        ALOGE("Could not register hello 1.0 service.");
    }
    joinRpcThreadpool();
    ALOGE("Service exited!");
    return 1;
}

EOF

#<6> startup script
cat > hardware/interfaces/hello/1.0/default/android.hardware.hello@1.0-service.rc << EOF
service hello_hal_service /vendor/bin/hw/android.hardware.hello@1.0-service
class hal
user system
group system
EOF


#<7> service consumer
cat > hardware/interfaces/hello/1.0/default/test.cpp << EOF
# define LOG_TAG "android.hardware.hello@1.0-test"

# include <android/hardware/hello/1.0/IHello.h>
# include <hidl/Status.h>
# include <hidl/LegacySupport.h>
# include <utils/misc.h>
# include <hidl/HidlSupport.h>
# include <stdio.h>
using android::hardware::hello::V1_0::IHello;
using android::sp;
using android::hardware::hidl_string;

int main()
{
    int ret = 0;

    android::sp<IHello> service = IHello::getService();
    if(service == nullptr) {
        printf("Failed to get service\n");
        return -1;
    }

    service->sayHello("say:", [&](hidl_string result) {
        printf("%s\n", result.c_str());
    });

    return ret;
}
EOF

#<8> Android.bp:build hal, service and client
cat > hardware/interfaces/hello/1.0/default/Android.bp << EOF
cc_binary {
    name: "android.hardware.hello@1.0-service",
    init_rc: ["android.hardware.hello@1.0-service.rc"],
    relative_install_path: "hw",
    vendor: true,

    srcs: [
        "Hello.cpp",
        "service.cpp",
    ],

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

    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "android.hardware.hello@1.0",
    ],
}

cc_binary {
    name: "android.hardware.hello@1.0-test",
    srcs: ["test.cpp"],
    vendor: true,

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

    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "android.hardware.hello@1.0",
    ],
}
EOF


#<9> let service can find android.hardware.hello@1.0.so
build/make/target/product/vndk/28.txt
build/make/target/product/vndk/current.txt
out/target/product/generic/obj/PACKAGING/vndk_intermediates/libs.txt
严格按字母顺序加入VNDK-core: android.hardware.hello@1.0.so
三个文件一样,若编译报错,按lib.txt重新添加

#<10> let client can get services
device/generic/goldfish/manifest.xml(/vendor/manifest.xml)文件加入client才能拿到services
    <hal format="hidl">
        <name>android.hardware.hello</name>
        <transport>hwbinder</transport>
        <version>1.0</version>
        <interface>
            <name>IHello</name>
            <instance>default</instance>
        </interface>
    </hal>

#<11>Add hello HIDL *-impl and -service libraries to product packages
build/make/target/product/emulator.mk文件加入
PRODUCT_PACKAGES += \
    android.hardware.hello@1.0-impl \
    android.hardware.hello@1.0-service


#<12>
mm hardware/interfaces/hello/1.0/
mmm hardware/interfaces/hello/1.0/default/
make -j 8


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值