Android9.0 HAL添加HIDL项目实例

一.hidl简单介绍

HIDL的全称是HAL interface definition language(硬件抽象层接口定义语言),是Android Framework 与Android HAL实现之间的接口。

二.hidl的实现

本项目是通过不同的APK来实现camera tunning的参数分离。

1. hardware部分

1.1编写.hal文件

在代码目录下新建 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;
  }; 

1.2 使用hidl-gen工具生成C++文件与Android.bp文件。

1.2.1 hidl-gen工具的获取。

代码全遍完成后,查看out/host/linux-x86/bin/ 下,如果没有hidl-gen可执行文件,
可以source ,lunch 后,使用make hidl-gen -j4 命令编译后即可生成。

1.2.2 使用hidl-gen工具生成C++文件。

新建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]

1.2.3 使用hidl-gen工具生成Android.bp文件

使用命令:

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

1.3.cpp文件的实现

#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

1.4 .h文件

#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

1.5 添加启动service

新建service 启动脚本[email protected]

 service arcore_hal_service /vendor/bin/hw/[email protected]
    class hal
    user  system
    group system

1.6 编写service.cpp 文件

#include 
#include 
#include 
using android::hardware::defaultPassthroughServiceImplementation;
using android::hardware::arcore::V1_0::Iarcore;
int main()
{
    android::ProcessState::initWithDriver("/dev/vndbinder");
    return defaultPassthroughServiceImplementation<Iarcore>(); 
}

1.7 修改Android.bp 文件

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]",
   ],

}

1.8 编译相关

在目录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]

2.device 部分

在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]

3.selinux 相关

在device/sharp/common/sepolicy 下

3.1 新建 hal_arcore_default.te

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 };

3.2 在attributes添加

attribute hal_arcore;
attribute hal_arcore_client;
attribute hal_arcore_server;

3.3 在file_contexts添加

/(vendor|system/vendor)/bin/hw/android\.hardware\.arcore@1\.0-service         u:object_r:hal_arcore_default_exec:s0

3.4 在hwservice_contexts添加

android.hardware.arcore::Iarcore                                u:object_r:hal_arcore_hwservice:s0

3.5 在hwservicemanager.te 添加

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;

三.客户端调用实现

1.framework处调用实现数据到hal传递

在当前代码里的Android.mk

LOCAL_SHARED_LIBRARIES += [email protected]

头文件引入

#include 
sp<hardware::arcore::V1_0::Iarcore> mArcoreservice = hardware::arcore::V1_0::Iarcore::getService();
mArcoreservice->SetAppFlag(6);

2.vendor层的get

由于本项目要在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 参数分离。

你可能感兴趣的:(Android9.0 HAL添加HIDL项目实例)