AIDL接口实现应用层调用HAL服务

一、背景

由于Google在Android 11及其之后的android版本中废弃了 HIDL作为 HAL接口的方式,官网推荐扩展了原有的 AIDL作为HAL的接口描述语言。

AIDL后端指的就是AIDL生成的目标代码

二、准备工作

Android 12 源码

采用 android12_0_0_r3

Ubuntu 20.04 LTS

三、AIDL

AIDL for hals 结构

四、编码准备

AIDL接口创建的目录

IInvcase.aidl

package android.hardware.invcase;
@VintfStability                  # 用作系统和供应商之间,需要指明这个注释
interface IInvcase {
    byte[] testbyte(in byte[] x,in byte[] y);  # 新建的接口
}
Invcase.cpp

#define LOG_TAG "Invcase"

#include <utils/Log.h>
#include <iostream>
#include <fstream>
#include "Invcase.h"

namespace aidl {
namespace android {
namespace hardware {
namespace invcase {
ndk::ScopedAStatus Invcase::testbyte(const std::vector<uint8_t>& in_x, 
                                     const std::vector<uint8_t>& in_y,
                                     std::vector<uint8_t>* _aidl_return)
    {
        std::vector<uint8_t> vec;
        vec.insert(vec.end(),in_x.begin(),in_x.end()); 
        vec.insert(vec.end(),in_y.begin(),in_y.end()); 
        *_aidl_return = vec;
        ALOGE("Hal build 2 Byte:");
        return ndk::ScopedAStatus::ok();
 }
service.cpp

int main() {
    // Enable vndbinder to allow vendor-to-venfor binder call
    android::ProcessState::initWithDriver("/dev/vndbinder");

    ABinderProcess_setThreadPoolMaxThreadCount(0);
    ABinderProcess_startThreadPool();

    std::shared_ptr<Invcase> invcase = ndk::SharedRefBase::make<Invcase>();
    const std::string name = Invcase::descriptor + "/default"s;

    if (invcase != nullptr) {
        if(AServiceManager_addService(invcase->asBinder().get(), name.c_str()) != STATUS_OK) {
            loge("Failed to register IInvcase service");
            return -1;
        }
    } else {
        loge("Failed to get IInvcase instance");
        return -1;
    }

    logd("IInvcase service starts to join service pool");
    ABinderProcess_joinThreadPool();

    return EXIT_FAILURE;  // should not reached
}
android.hardware.invcase-service.rc

service android.hardware.invcase-service /vendor/bin/hw/android.hardware.invcase-service
        interface aidl android.hardware.invcase.IInvcase/default
        class hal
        user system
        group system
        
android.hardware.invcase-service.xml
<manifest version="1.0" type="device">
    <hal format="aidl">
        <name>android.hardware.invcase</name>
        <version>1</version>
        <fqname>IInvcase/default</fqname>
    </hal>
</manifest>
hardware/interfaces/invcase/aidl/android.bp
aidl_interface {
    name: "android.hardware.invcase",    # 唯一的接口名字
    vendor: true,
    srcs: ["android/hardware/invcase/*.aidl"],   # 组成接口的源文件列表
    stability: "vintf",                      # 接口在使用期间保持稳定
    owner: "vqtrong",
    backend: {                                # 选择生成那个后端 这里默认是NDK后端为ture
        cpp: {
            enabled: false,
        },
        java: {
            sdk_version: "module_current",
        },
    },
}

hardware/interfaces/invcase/aidl/android.bp

cc_binary {
    name: "android.hardware.invcase-service",
    vendor: true,
    relative_install_path: "hw",
    init_rc: ["android.hardware.invcase-service.rc"],
    vintf_fragments: ["android.hardware.invcase-service.xml"],

    srcs: [
        "Invcase.cpp",
        "service.cpp",
    ],

    cflags: [
        "-Wall",
        "-Werror",
    ],

    shared_libs: [
        "libbase",
        "liblog",
        "libhardware",
        "libbinder_ndk",
        "libbinder",
        "libutils",
        "android.hardware.invcase-V1-ndk_platform",
    ],
}

五、测试

 push 对应的产物到手机中

a. android.hardware.invcase-V1-ndk.so ==> /vendor/lib64/

b. android.hardware.invcase-V1-ndk_platform.so ==> /vendor/lib64/

c. android.hardware.invcase-service ==>/vendor/bin/hw

d.****.xml   ==> /vendor/etc/mianfest/

跳过 selinux 权限

设置 为 permissive

参考:

https://www.codeinsideout.com/blog/android/hal/aidl/

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值