一、背景
由于Google在Android 11及其之后的android版本中废弃了 HIDL作为 HAL接口的方式,官网推荐扩展了原有的 AIDL作为HAL的接口描述语言。
AIDL后端指的就是AIDL生成的目标代码
二、准备工作
Android 12 源码
采用 android12_0_0_r3
Ubuntu 20.04 LTS
三、AIDL
![](https://i-blog.csdnimg.cn/blog_migrate/2050a14e50368d7aa1e8a624739de459.png)
AIDL for hals 结构
四、编码准备
AIDL接口创建的目录
![](https://i-blog.csdnimg.cn/blog_migrate/388df812e50630873131836bf55dc08e.png)
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