hidl-impl

2020-08-05  本文已影响0人  xuefeng_apple

1-建立INaruto.hal文件

mkdir -p hardware/interfaces/naruto/1.0/default 

INaruto.hal:

package android.hardware.naruto@1.0;
interface INaruto {
    helloWorld(string name) generates (string result);
};

生成HAL 相关文件,使用google 提供的原生工具

# PACKAGE=android.hardware.naruto@1.0
# LOC=hardware/interfaces/naruto/1.0/default/
# make hidl-gen -j64
# hidl-gen -o $LOC -Lc++-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE  ----->生成.h .cpp
# hidl-gen -o $LOC -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport $PACKAGE---->生成Android.bp

然后使用脚本来更新Makefile,自动生成Android,mk, Android.bp
# ./hardware/interfaces/update-makefiles.sh

添加service 文件

touch hardware/interfaces/naruto/1.0/default/android.hardware.naruto@1.0-service.rc
touch hardware/interfaces/naruto/1.0/default/service.cpp

现在我们的代码目录: hardware/interface/naruto:

├── 1.0
│   ├── Android.bp
│   ├── Android.mk
│   ├── default
│   │   ├── Android.bp
│   │   ├── android.hardware.naruto@1.0-service.rc
│   │   ├── Naruto.cpp
│   │   ├── Naruto.h
│   │   └── service.cpp
│   └── INaruto.hal
└── Android.bp

打开Naruto.h文件,Naruto.cpp 把下面的两个注释放开,默认是binder ,采用直通要放开

// FIXME: most likely delete, this is only for passthrough implementations
extern "C" INaruto* HIDL_FETCH_INaruto(const char* name);
INaruto* HIDL_FETCH_INaruto(const char* /* name */) {
    return new Naruto();
}

android.hardware.naruto@1.0-service.rc:

service naruto_hal_service /vendor/bin/hw/android.hardware.naruto@1.0-service
    class hal
    user system
    group system

service.cpp:

# define LOG_TAG "android.hardware.naruto@1.0-service"
# include <android/hardware/naruto/1.0/INaruto.h>
# include <hidl/LegacySupport.h>

using android::hardware::naruto::V1_0::INaruto;
using android::hardware::defaultPassthroughServiceImplementation;

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

hardware/interfaces/naruto/1.0/default/Android.bp

cc_library_shared {
    name: "android.hardware.naruto@1.0-impl",
    relative_install_path: "hw",
    proprietary: true,
    srcs: [
        "Naruto.cpp",
    ],
    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "android.hardware.naruto@1.0",
    ],
}

cc_binary {
    name: "android.hardware.naruto@1.0-service",
    defaults: ["hidl_defaults"],
    proprietary: true,
    relative_install_path: "hw",
    srcs: ["service.cpp"],
    init_rc: ["android.hardware.naruto@1.0-service.rc"],
    shared_libs: [
        "libhidlbase",
        "libhidltransport",
        "libutils",
        "liblog",
        "android.hardware.naruto@1.0",
        "android.hardware.naruto@1.0-impl",
    ],
}

在目录下hardware/interfaces/naruto/1.0/defaulthardware/interfaces/naruto/1.0 build
需要push 到系统的文件
out/target/product/sdm845/system/lib/android.hardware.naruto@1.0.so
out/target/product/sdm845/system/lib64/android.hardware.naruto@1.0.so

out/target/product/sdm845/vendor/lib/hw/android.hardware.naruto@1.0-impl.so
out/target/product/sdm845/vendor/lib64/hw/android.hardware.naruto@1.0-impl.so

out/target/product/sdm845/vendor/bin/hw/android.hardware.naruto@1.0-service
out/target/product/sdm845/vendor/etc/init/android.hardware.naruto@1.0-service.rc

2- 如何测试hidl

测试bin
out/target/product/sdm845/vendor/bin/naruto_test

如何制作测试bin

LINUX/android/vendor/xxx/hidl-test$ tree 
.
├── Android.mk
└── client.cpp

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_PROPRIETARY_MODULE := true
LOCAL_MODULE := naruto_test
LOCAL_SRC_FILES := \
    client.cpp \

LOCAL_SHARED_LIBRARIES := \
   liblog \
   libhidlbase \
   libutils \
   android.hardware.naruto@1.0 \

include $(BUILD_EXECUTABLE)

client.cpp:

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

int main()
{
    int ret;
    android::sp<INaruto> service = INaruto::getService();
    if(service == nullptr) {
        printf("Failed to get service\n");
        return -1;
    }
    service->helloWorld("JayZhang", [&](hidl_string result) {
                printf("%s\n", result.c_str());
        });

    return 0;
}

device/qcom/项目/manifest.xml

<hal format="hidl">
    <name>android.hardware.naruto</name>
    <transport>hwbinder</transport>
    <version>1.0</version>
    <interface>
        <name>INaruto</name>
        <instance>default</instance>
    </interface>
</hal>

3- 进行开发调试

启动service ,本可以开启执行service 启动, 不过没有在调试,测试手动执行了

root:/vendor/bin/hw # ./android.hardware.naruto@1.0-service &
[1] 3073

查看service 是否启动:

root:/vendor/bin# ps -ef |grep naruto
root   3073  2851 0 07:54:11 pts/0 00:00:00 android.hardware.naruto@1.0-service

运行测试bin

root:/vendor/bin # ./naruto_test
Hello World, JayZhang

4-调用流程

HIDL软件包中自动生成的文件会链接到与软件包同名的单个共享库。该共享库还会导出单个头文件INruto.h,用于在binder客户端和服务端的接口文件,下面的图诠释了我们的INaruto.hal编译后生成的文件走向,从官网拷贝过来:


图片.png

这里目录下: 可以看到hidl机制文件
out/soong/.intermediates/hardware/interfaces/naruto/1.0/android.hardware.naruto@1.0_genc++

.
└── gen
    └── android
        └── hardware
            └── naruto
                └── 1.0
                    └── NarutoAll.cpp

out/soong/.intermediates/hardware/interfaces/naruto/1.0/android.hardware.naruto@1.0_genc++_headers

.
└── gen
    └── android
        └── hardware
            └── naruto
                └── 1.0
                    ├── BnHwNaruto.h
                    ├── BpHwNaruto.h
                    ├── BsNaruto.h
                    ├── IHwNaruto.h
                    └── INaruto.h

客户端和服务器实现不得直接引用除 IFoo 之外的任何内容。为了满足这项要求,请只包含 IFoo.h 并链接到生成的共享库。

图片.png

REF:
https://blog.csdn.net/u013357557/article/details/84561652
https://www.jianshu.com/p/ca6823b897b5

上一篇下一篇

猜你喜欢

热点阅读