一.HIDL介绍
HIDL的全称是HAL interface definition language(硬件抽象层接口定义语言),是Android
Framework 与Android HAL之间的接口。HIDL 旨在用于进程间通信 (IPC),进程之间的通信
采用 Binder 机制。
二.HIDL 与AIDL 的对比
三.HIDL接口添加
HIDL接口路径:
hardware\interfaces\imw\1.0\
路径下有两个文件:
Android.bp
IImwManager.hal
Android.bp:
hidl_interface {
name: "vendor.isolution.hardware.imw@1.0", //FQName 的全名
root: "vendor.isolution.hardware", //定义好的 package root name
//system_ext_specific: true,
srcs: [
"IImwManager.hal",
],
interfaces: [
"android.hidl.base@1.0", //编译过程中依赖的接口名称
],
gen_java: true, //是否编译为 Java 使用的接口
}
IImwManager.hal文件:
package vendor.isolution.hardware.imw@1.0; // 当前package包名
import IImwManagerClientCallback; // 导入其它package包
import IUpdateEngineClientCallback;
interface IImwManager // 定义一个interface
{
onCreate();
onInitStart();
onInitCompleted();
onStart();
onSystemAppStart();
onBootanimationExit();
onBootCompleted();
onReboot();
onRecovery();
onSleep();
onShutdown();
};
HIDL生成的文件路径:
/out/soong/.intermediates/vendor/isolution/hardware/interfaces/imw/1.0/vendor.isolution.hardware.imw@1.0
编译后模块对应的文件:
vendor.isolution.hardware.imw@1.0.so
当 IHelloworld.hal 创建完成就可以创建对应的 HIDL 实现代码(Hidl Interface 和 service):
service.端java代码:
import vendor.isolution.hardware.imw.V1_0.IImwManager; // 导入package包
private IImwManager halManager = null;
halManager.onCreate();
halManager.onInitStart();
halManager.onInitCompleted();
halManager.onStart();
halManager.onSystemAppStart();
halManager.onBootanimationExit();
halManager.onBootCompleted();
halManager.onReboot();
halManager.onRecovery();
halManager.onSleep();
halManager.onShutdown();
实现Hidl Interface:
hardware\interfaces\imw\1.0\default\ImwManager.h
#ifndef VENDOR_ISOLUTION_HARDWARE_IMW_V1_0_IMWMANAGER_H_
#define VENDOR_ISOLUTION_HARDWARE_IMW_V1_0_IMWMANAGER_H_
#include <log/log.h>
#include <android/log.h>
#include <hardware/hardware.h>
#include <hardware/fingerprint.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
#include <vendor/isolution/hardware/imw/1.0/IImwManager.h>
using ::vendor::isolution::hardware::imw::V1_0::IImwManager;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::hidl_vec;
using ::android::hardware::hidl_string;
using ::android::sp;
namespace vendor {
namespace isolution {
namespace hardware {
namespace imw {
namespace V1_0 {
namespace implementation {
struct ImwManager : public IImwManager {
public:
ImwManager();
~ImwManager();
static IImwManager* getInstance();
Return<void> setNotify(const sp<IImwManagerClientCallback>& clientCallback) override;
Return<void> setNotifyUpdateEngine(const sp<IUpdateEngineClientCallback>& clientCallback) override;
Return<void> onCreate() override;
Return<void> onInitStart() override;
Return<void> onInitCompleted() override;
Return<void> onStart() override;
Return<void> onSystemAppStart() override;
Return<void> onBootanimationExit() override;
Return<void> onBootCompleted() override;
Return<void> onReboot() override;
Return<void> onRecovery() override;
Return<void> onSleep() override;
Return<void> onShutdown() override;
};
} // namespace implementation
} // namespace V1_0
} // namespace imw
} // namespace hardware
} // namespace isolution
} // namespace vendor
hardware\interfaces\imw\1.0\default\ImwManager.cpp
#include <utils/Log.h>
#include <log/log.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <cutils/atomic.h>
#include <cutils/properties.h> // for property_get
#include <utils/misc.h>
#include <stdio.h>
#include <utils/threads.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <sys/shm.h>
#include <signal.h>
#include "ImwManager.h"
namespace vendor {
namespace isolution {
namespace hardware {
namespace imw {
namespace V1_0 {
namespace implementation {
ImwManager::ImwManager() : mClientCallback(nullptr) {
sInstance = this; // keep track of the most recent instance
onCreate();
}
ImwManager::~ImwManager()
{
ALOGW("~ImwManager\n");
}
IImwManager* ImwManager::getInstance() {
if (!sInstance) {
sInstance = new ImwManager();
}
return sInstance;
}
Return<void> ImwManager::onCreate()
{
ALOGW("onCreate\n");
return Void();
}
Return<void> ImwManager::onInitStart()
{
ALOGW("onInitStart\n");
return Void();
}
Return<void> ImwManager::onInitCompleted()
{
ALOGW("onInitCompleted\n");
return Void();
}
Return<void> ImwManager::onStart()
{
ALOGW("onStart\n");
return Void();
}
Return<void> ImwManager::onSystemAppStart()
{
ALOGW("onSystemAppStart\n");
return Void();
}
Return<void> ImwManager::onBootanimationExit()
{
ALOGW("onBootanimationExit\n");
return Void();
}
Return<void> ImwManager::onBootCompleted()
{
ALOGW("onBootCompleted\n");
return Void();
}
Return<void> ImwManager::onReboot()
{
ALOGW("onReboot\n");
return Void();
}
Return<void> ImwManager::onRecovery()
{
ALOGW("onRecovery\n");
return Void();
}
Return<void> ImwManager::onSleep()
{
ALOGW("onSleep\n");
return Void();
}
Return<void> ImwManager::onShutdown()
{
ALOGW("onShutdown\n");
return Void();
}
ImwManager *ImwManager::sInstance = nullptr;
} // namespace implementation
} // namespace V1_0
} // namespace imw
} // namespace hardware
} // namespace isolution
} // namespace vendor
添加rc文件:
rc 是 “runcom” 的缩写,这个术语最初来源于 Unix 系统,在早期的系统中使用了一个叫做 “runcom” 的目录,用于存放启动时需要运行的一些脚本和配置文件。
在实现了 serivce 和 impl 代码后需要添加 rc 文件,文件名为 vendor.isolution.hardware.imw@1.0-service.rc
service imw-hal-1-0 /vendor/bin/hw/vendor.isolution.hardware.imw@1.0-service
class hal
user root
group root media system graphics input bluetooth shell drmrpc media_rw audio