Android基于HIDL实现系统层与应用层跨进程通信应用实现

497 阅读3分钟

Android基于HIDL实现系统层与应用层跨进程通信应用实现

一、创建文件夹

# 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

五、在android.bp添加AIDL的接口配置信息

hidl_interface {
    name: "android.hardwar.HidlTeste@1.0",//指定了该 HIDL 接口的名称
    root: "android.hardware",
    vndk: {
        enabled: true,
    },
    srcs: [
        "types.hal",
        "IIHidlTest.hal"
        ],//列出了该接口所需的源文件
    interfaces: [
        "android.hidl.base@1.0",
    ],
    gen_java: true,//表示要生成 Java的HIDL接口的jar包,将该 HIDL 接口映射到 Java 类中,以便在 Java 层使用,架包名称为“接口名.jar”,即“android.hardwar.HidlTeste@1.0.jar”
}

六、用 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 文件夹下的目录为
-1.0
--Android.bp
--default
---HidlTest.cpp
---HidlTest.h
--IHidlTest.hal

HidlTest.cpp 内容

#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 文件中的内容

#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

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

七、创建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",
],

} 编译完成后,在./out/soong/.intermediates/hardware/interfaces/目录下会生成android.hardware.hidltest@1.0-service.jar文件

九、客户端

在android应用工程中 libs拷贝该架包,在android.mk文件中引用android.hardware.hidltest@1.0-service.jar架包,代码如下

LOCAL_STATIC_JAVA_LIBRARIES += \
   android.hardwar.HidlTeste@1.0 \

客户端调用hidl接口

MainActivity.java

javaCopy Code
import android.hardware.HidlTest.IHidlTest;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

public class MainActivity extends Activity {
    
    private static final String TAG = "HelloWorldClient";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        try {
            IHidlTest iHidlTestService = IHidlTest.getService();
            helloWorldService.helloWorld("hello world come form activity");
        } catch (RemoteException e) {
            Log.e(TAG, "Error calling sayHello: " + e.getMessage());
        }
    }

应用层在执行helloWorld方法后,android系统层android/hardware/hidltest/1.0/hidltest.cpp类中的

Return<void> HidlTest::helloWorld{}

会收到消息。