HIDL的全称是HAL interface definition language(硬件抽象层接口定义语言),是Android Framework 与Android HAL实现之间的接口。
本项目是通过不同的APK来实现camera tunning的参数分离。
在代码目录下新建 hardware/interface/arcore/1.0/
新建Iarcore.hal 文件(主接口)如,
package [email protected];
interface Iarcore {
SetAppFlag(uint32_t appkey); //用与framework下发hal的实现.
GetAppFlag() generates (uint32_t ret);//用于hal获取framerwork的实现。
};
本项目只是需要设置一个变量下去实现,如果要定于新的数据类型可以新建type.hal文件.如,
package [email protected];
struct AppFlag{
int32_t AppKey;
string ApkName;
};
代码全遍完成后,查看out/host/linux-x86/bin/ 下,如果没有hidl-gen可执行文件,
可以source ,lunch 后,使用make hidl-gen -j4 命令编译后即可生成。
新建hardware/interface/arcore/1.0/default/
使用命令:
out/host/linux-x86/bin/hidl-gen -o hardware/interface/arcore/1.0/default/ -Lc+±impl - randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/[email protected]
使用命令:
out/host/linux-x86/bin/hidl-gen -o hardware/interface/arcore/1.0/default/ -Landroidbp-impl -randroid.hardware:hardware/interfaces -randroid.hidl:system/libhidl/[email protected]
使用update-makefiles.sh生成1.0目录下的Android.bp
./hardware/interfaces/update-makefiles.sh
#include "arcore.h"
#include
namespace android {
namespace hardware {
namespace arcore {
namespace V1_0 {
namespace implementation {
static uint32_t AppFlag;
// Methods from ::android::hardware::arcore::V1_0::Iarcore follow.
Return<void> arcore::SetAppFlag(uint32_t appkey) {
// TODO implement
ALOGE("setappflag:%d", appkey);
AppFlag = appkey;
return Void();
}
Return<uint32_t> arcore::GetAppFlag() {
// TODO implement
uint32_t appkey;
appkey = AppFlag;
ALOGE("getappflag:%d", appkey);
return appkey;
}
// Methods from ::android::hidl::base::V1_0::IBase follow.
Iarcore* HIDL_FETCH_Iarcore(const char* /* name */) { //由于我们使用的是passthrough 模式,此处的注释要取消掉。
return new arcore();
}
//
} // namespace implementation
} // namespace V1_0
} // namespace arcore
} // namespace hardware
} // namespace android
#ifndef ANDROID_HARDWARE_ARCORE_V1_0_ARCORE_H
#define ANDROID_HARDWARE_ARCORE_V1_0_ARCORE_H
#include
#include
#include
namespace android {
namespace hardware {
namespace arcore {
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 arcore : public Iarcore {
// Methods from ::android::hardware::arcore::V1_0::Iarcore follow.
Return<void> SetAppFlag(uint32_t appkey) override;
Return<uint32_t> GetAppFlag() override;
// Methods from ::android::hidl::base::V1_0::IBase follow.
};
// FIXME: most likely delete, this is only for passthrough implementations ////由于我们使用的是passthrough 模式,此处的注释要取消掉
extern "C" Iarcore* HIDL_FETCH_Iarcore(const char* name);
} // namespace implementation
} // namespace V1_0
} // namespace arcore
} // namespace hardware
} // namespace android
#endif // ANDROID_HARDWARE_ARCORE_V1_0_ARCORE_H
新建service 启动脚本[email protected]
service arcore_hal_service /vendor/bin/hw/[email protected]
class hal
user system
group system
#include
#include
#include
using android::hardware::defaultPassthroughServiceImplementation;
using android::hardware::arcore::V1_0::Iarcore;
int main()
{
android::ProcessState::initWithDriver("/dev/vndbinder");
return defaultPassthroughServiceImplementation<Iarcore>();
}
cc_library_shared {
name: "[email protected]",
relative_install_path: "hw",
defaults: ["hidl_defaults"],
proprietary: true,
srcs: [
"arcore.cpp",
],
shared_libs: [
"libhidlbase",
"libhidltransport",
"libutils",
"liblog",
"[email protected]",
],
}
cc_binary {
name: "[email protected]",
relative_install_path: "hw",
defaults: ["hidl_defaults"],
proprietary: true,
init_rc: ["[email protected]"],
srcs: [
"arcore.cpp",
"service.cpp", ],
shared_libs: [
"libbase",
"liblog",
"libdl",
"libutils",
"libhardware",
"libhidlbase",
"libhidltransport",
"libbinder",
"[email protected]",
],
}
在目录build\make\target\product\vndk 里 28.txt 和 current.txt 按照字母顺序新增
VNDK-core: [email protected]
调用 update-makefiles.sh更新一下
mmm ./hardware/interfaces/test/1.0
编译输出:
vendor/lib/hw/[email protected]
vendor/bin/hw/[email protected]
vendor/etc/init/[email protected]
system/lib/[email protected]
在device/sharp/Lila/manifest.xml 添加
<hal format="hidl">
<name>android.hardware.arcore</name>
<transport>hwbinder</transport>
<version>1.0</version>
<interface>
<name>Iarcore</name>
<instance>default</instance>
</interface>
</hal>
在Lila.mk添加
PRODUCT_PACKAGES += [email protected]
PRODUCT_PACKAGES += [email protected]
在device/sharp/common/sepolicy 下
type hal_arcore_default, domain;
hal_server_domain(hal_arcore_default, hal_arcore)
type hal_arcore_default_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(hal_arcore_default)
allow hal_arcore_default vndbinder_device:chr_file { read write open ioctl };
allow hal_arcore_default hal_arcore_hwservice:hwservice_manager { find add };
allow hal_arcore_default hidl_base_hwservice:hwservice_manager add;
allow hal_arcore_default hal_arcore_hwservice:binder call;
allow hal_arcore_default sysfs:file rw_file_perms;
allow hal_arcore_default hwservicemanager_prop:file r_file_perms;
allow hal_arcore_default hwservicemanager:binder { transfer call };
attribute hal_arcore;
attribute hal_arcore_client;
attribute hal_arcore_server;
/(vendor|system/vendor)/bin/hw/android\.hardware\.arcore@1\.0-service u:object_r:hal_arcore_default_exec:s0
android.hardware.arcore::Iarcore u:object_r:hal_arcore_hwservice:s0
allow hwservicemanager hal_arcore_default:process getattr;
allow hwservicemanager hal_arcore_default:binder { transfer call };
allow hwservicemanager hal_arcore_default:file r_file_perms;
allow hwservicemanager hal_arcore_default:dir search;
在当前代码里的Android.mk
LOCAL_SHARED_LIBRARIES += [email protected]
头文件引入
#include
sp<hardware::arcore::V1_0::Iarcore> mArcoreservice = hardware::arcore::V1_0::Iarcore::getService();
mArcoreservice->SetAppFlag(6);
由于本项目要在sensor.c 中调用来实现tuning 参数的分离。
因为vendor下基本都是使用C语言,所以我们要想实现调用,必须对C++进行封装。
我们是在vendor/qcom/proprietary/mm-camera/mm-camera2/
新建 arcore/arcore.h
#ifndef ARCORE_H
#define ARCORE_H
#ifdef __cplusplus
extern "C" {
#endif
uint32_t GetAppFlag();
#ifdef __cplusplus
}
#endif
新建arcore/arcore.cpp
#include
#include
#include "arcore.h"
#ifdef __cplusplus
using ::android::sp;
static android::sp<android::hardware::arcore::V1_0::Iarcore> mArcoreservice = NULL;
extern "C"{
#endif
uint32_t GetAppFlag()
{
if(mArcoreservice == NULL) {
mArcoreservice = android::hardware::arcore::V1_0::Iarcore::getService();
}
uint32_t appkey;
appkey = mArcoreservice->GetAppFlag();
ALOGI("vendor:getappflag:%d", appkey);
return appkey;
}
#ifdef __cplusplus
}
#endif
新建Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libarcore
LOCAL_SRC_FILES := arcore.cpp
LOCAL_SHARED_LIBRARIES := libc liblog libhidlbase libutils [email protected]
LOCAL_MODULE_TAGS := optional
LOCAL_PROPRIETARY_MODULE := true
LOCAL_MULTILIB := 32
$(warning $(LOCAL_PROPRIETARY_MODULE))
include $(BUILD_SHARED_LIBRARY)
到此,HIDL添加到客户端实现已经完成,接下来我们可以调用GetAppFlag 来实现不同apk连接时的tunning 参数分离。