Rk3588 Android12 AIDL 开发

AIDL (Android Interface Definition Language)HIDL (HAL Interface Definition Language) 都是 Android 系统中用于定义接口的工具,但它们有不同的用途和特性。

AIDL (Android Interface Definition Language)

  1. 用途

    • 主要用于应用程序之间的进程间通信 (IPC)。
    • 适用于定义应用程序组件(如 Activity 和 Service)之间的接口。

HIDL (HAL Interface Definition Language)

  1. 用途

    • 主要用于定义硬件抽象层 (HAL) 接口。
    • 适用于访问底层硬件功能,如摄像头、传感器等。

未来趋势

从 Android 13 开始,HIDL 已被弃用,建议使用 AIDL 来实现 HAL 接口⁴。这意味着未来的 Android 版本将更多地依赖 AIDL 来实现硬件抽象层的接口。

本文主要记录自己实现一个AIDL的过程

主要的架构逻辑

1.使能hidl-gen工具

source build/envsetup.sh
lunch rk3588_s-userdebug
make hidl-gen

2.建立自己的AIDL ,test目录 和ITest.hal,ITestWarpper.hal

1.mkdir vendor/rockchip/hardware/interfaces/test
2.mkdir vendor/rockchip/hardware/interfaces/test/1.03.新建ITest.hal文件
//vendor/rockchip/hardware/interfaces/test/1.0/ITest.hal
package rockchip.hardware.test@1.0;
import ITestWarpper;interface ITest
{setTestWarpper(ITestWarpper testWarpper);GetString()generates (string printString);
};4.新建ITestWarpper.hal文件 用于数据传递
//vendor/rockchip/hardware/interfaces/test/1.0/ITestWarpper.hal
package rockchip.hardware.test@1.0;
interface ITestWarpper
{GetString()generates (string printString);
};

生成编译文件vendor/rockchip/hardware/interfaces/test/1.0/Android.bp  

 do_makefiles_update rockchip.hardware:vendor/rockchip/hardware android.hardware:hardware/interfaces android.hidl:system/libhidl/transport

AIDL 的没有返回值都是VOID ,出参都是 generates (string printString);实现,类似C++的别名功能

生成对应的CPP和头文件

1.hidl-gen -o vendor/rockchip/hardware/test/1.0/defualt -Lc++-impl -rrockchip.hardware:vendor/rockchip/hardware -randroid.hidl:system/libhidl/transport rockchip.hardware  //生成C++ 和 H2.hidl-gen -o vendor/rockchip/hardware/test/1.0/defualt -Landroidbp-impl -rrockchip.hardware:vendor/rockchip/hardware/ -randroid.hidl:system/libhidl/transport rockchip.hardware  //生成Android.dp在defult 目录下生成对应的cpp文件和头文件,Android.bp

也可以使用我写的脚本生成对应的文件
./generate-source2.sh vendor/rockchip/hardware/interfaces/ test rockchip.hardware 

#! /bin/bash
# v1.0if [ -z $1 ]; thenecho "Please input path "exit 1
elseLOC=$1
fiif [ -z $2 ]; thenecho "Please input module name"exit 1
elseMODULE=$2
fiif [ -z $3 ]; thenecho "Please input package head"exit 1
elsePACKAGE_HEAD=$3
fiPACKAGE=$PACKAGE_HEAD.$MODULE@1.0
echo "You package is: $PACKAGE"hidl-gen -o $LOC/$MODULE/1.0/defualt -Lc++-impl -r$PACKAGE_HEAD:$LOC \-randroid.hidl:system/libhidl/transport $PACKAGE
hidl-gen -o $LOC/$MODULE/1.0/defualt -Landroidbp-impl -r$PACKAGE_HEAD:$LOC \-randroid.hidl:system/libhidl/transport $PACKAGE

修改Test.cpp Test.h

// FIXME: your file license if you have one#include "Test.h"
#include "log/log.h"
#define LOG_TAG "test1.0"
namespace rockchip::hardware::test::implementation {
sp<::rockchip::hardware::test::V1_0::ITestWarpper> mTestWarpper = nullptr;
hidl_string mDeviceId;
// Methods from ::rockchip::hardware::test::V1_0::ITest follow.
Return<void> Test::setTestWarpper(const sp<::rockchip::hardware::test::V1_0::ITestWarpper>& testWarpper) {// TODO implementmTestWarpper = testWarpper;return Void();
}Test::Test(){ALOGD("@%s.",__FUNCTION__);
}
Test::~Test(){ALOGD("@%s",__FUNCTION__);
}
V1_0::ITest* HIDL_FETCH_ITest(const char* /* name */) {ALOGD("@%s",__FUNCTION__);return new Test();
}
// [&](const ::android::hardware::hidl_string &id
Return<void> Test::GetString(GetString_cb _hidl_cb) {ALOGD("@%s,mDeviceId:%s",__FUNCTION__,mDeviceId.c_str());// TODO implementif(mTestWarpper!=nullptr){mTestWarpper->GetString([&](const ::android::hardware::hidl_string &id){ALOGD("@%s,GetString mDeviceId:%s",__FUNCTION__,id.c_str());_hidl_cb(id);});}return Void();
}// Methods from ::android::hidl::base::V1_0::IBase follow.//ITest* HIDL_FETCH_ITest(const char* /* name */) {//return new Test();
//}
//
}  // namespace rockchip::hardware::test::implementation
// FIXME: your file license if you have one#pragma once#include <rockchip/hardware/test/1.0/ITest.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>namespace rockchip::hardware::test::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 Test : public V1_0::ITest {// Methods from ::rockchip::hardware::test::V1_0::ITest follow.Return<void> setTestWarpper(const sp<::rockchip::hardware::test::V1_0::ITestWarpper>& testWarpper) override;Return<void> GetString(GetString_cb _hidl_cb) override;Test();~Test();// Methods from ::android::hidl::base::V1_0::IBase follow.};// FIXME: most likely delete, this is only for passthrough implementations
extern "C" V1_0::ITest* HIDL_FETCH_ITest(const char* name);}  // namespace rockchip::hardware::test::implementation

添加一个开机服务 test/1.0/defualt下添加rc 和 xml文件和service.cpp

vendor/rockchip/hardware/interfaces/test/1.0/defualt/rockchip.hardware.test@1.0-service.rc
service test_server /vendor/bin/hw/rockchip.hardware.test@1.0-serviceclass haluser systemgroup systemvendor/rockchip/hardware/interfaces/test/1.0/defualt/rockchip.hardware.test@1.0-service.xml
<manifest version="1.0" type="device"><hal format="hidl"><name>rockchip.hardware.test</name><transport>hwbinder</transport><version>1.0</version><interface><name>ITest</name><instance>default</instance></interface></hal>
</manifest>vendor/rockchip/hardware/interfaces/test/1.0/defualt/service.cpp
#define LOG_TAG "rockchip.hardware.test@1.0-service"#include <rockchip/hardware/test/1.0/ITest.h>
#include <hidl/LegacySupport.h>
#include <binder/ProcessState.h>using rockchip::hardware::test::V1_0::ITest;
using android::hardware::defaultPassthroughServiceImplementation;
using android::hardware::configureRpcThreadpool;
using android::hardware::joinRpcThreadpool;
using android::sp;int main() {android::ProcessState::initWithDriver("/dev/vndbinder");return defaultPassthroughServiceImplementation<ITest>("default", /*maxThreads*/ 6);
}

修改test/1.0/defualt/Android.dp

// FIXME: your file license if you have one
//生成一个AIDL so
cc_library_shared {// FIXME: this should only be -impl for a passthrough hal.// In most cases, to convert this to a binderized implementation, you should:// - change '-impl' to '-service' here and make it a cc_binary instead of a//   cc_library_shared.// - add a *.rc file for this module.// - delete HIDL_FETCH_I* functions.// - call configureRpcThreadpool and registerAsService on the instance.// You may also want to append '-impl/-service' with a specific identifier like// '-vendor' or '-<hardware identifier>' etc to distinguish it.name: "rockchip.hardware.test@1.0-impl",relative_install_path: "hw",// FIXME: this should be 'vendor: true' for modules that will eventually be// on AOSP.proprietary: true,srcs: ["Test.cpp","TestWarpper.cpp",],shared_libs: ["libhidlbase","libutils","liblog","rockchip.hardware.test@1.0",],
}
//生成一个服务进程cc_binary {name: "rockchip.hardware.test@1.0-service",init_rc: ["rockchip.hardware.test@1.0-service.rc"],vintf_fragments: ["rockchip.hardware.test@1.0-service.xml"],defaults: ["hidl_defaults"],proprietary: true,relative_install_path: "hw",srcs: ["service.cpp",],cflags: ["-Werror", "-Wno-unused-parameter"],shared_libs: ["liblog","libhardware","libhidlbase","libutils","libcutils","rockchip.hardware.test@1.0","libbinder","libhidlmemory",],
}
device 下根据自己的代码加入编译
PRODUCT_PACKAGES += \   rockchip.hardware.test@1.0-service \rockchip.hardware.test@1.0-impl

到这里我们的AIDL添加结束

二.修改系统te,不然服务跑不起来

添加文件device\rockchip\common\manifests\frameworks\rockchip.hardware.test@1.0-service.xml
<compatibility-matrix version="1.0" type="framework"><hal format="hidl" optional="true"><name>rockchip.hardware.test</name><version>1.0</version><interface><name>ITest</name><instance>default</instance></interface></hal>
</compatibility-matrix>添加文件device\rockchip\common\sepolicy\vendor\hal_test.te
type hal_test_default, domain;
type hal_test_default_exec, exec_type, vendor_file_type, file_type;init_daemon_domain(hal_test_default)allow hal_test_default device:dir { read open};
allow hal_test_default camera_device:chr_file{ read  write open ioctl };
allow hal_test_default vndbinder_device:chr_file { read write open ioctl map};
allow hal_test_default hidl_base_hwservice:hwservice_manager add;
allow hal_test_default hal_test_hwservice:hwservice_manager {add find};
allow system_app hal_test_hwservice:hwservice_manager { find };
binder_call(system_app, hal_test_default)
binder_call(hal_test_default, system_app)
#binder_call(hal_test_default, platform_app)
binder_call(hal_test_default, hwservicemanager)
#binder_call(hal_test_default, hal_camera_default)
# Client
#allow cameraserver hal_test_hwservice:hwservice_manager { find };
#allow hal_camera_default hal_test_hwservice:hwservice_manager { find };
get_prop(hal_test_default, hwservicemanager_prop)修改device\rockchip\common\sepolicy\vendor\hwservice_contexts
rockchip.hardware.tv.input::ITvInput                   u:object_r:hal_tv_input_hwservice:s0
+ rockchip.hardware.test::ITest                  u:object_r:hal_test_hwservice:s0修改device\rockchip\common\sepolicy\vendor\hwservice.te
+type hal_test_hwservice, hwservice_manager_type;

三。写一个测试demo

添加cpp和h 生成一个so 

//添加packages/apps/libtest_bridge/data_bridge.h
// #include <jni.h>
#include <functional>
#include <string>
#include <time.h>void init();
void deinit();//添加packages/apps/libtest_bridge/data_bridge.cpp
#define APK_VERSION	"V1.3"
#define LOG_TAG "test_bridge"
//#define LOG_NDEBUG 0#include <utils/Log.h>
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utils/Log.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/videodev2.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <linux/v4l2-subdev.h>#include "data_bridge.h"#include <ui/Fence.h>
#include <ui/GraphicBufferMapper.h>
#include <ui/GraphicBuffer.h>
#include <ui/Rect.h>#include <utils/Singleton.h>#define VIRTUAL_CAMERA
#define RGA_PROC #include <android/hardware_buffer_jni.h>
#include <vndk/hardware_buffer.h>#include <condition_variable>#ifdef RGA_PROC
#include <RockchipRga.h>
#include <im2d_api/im2d.h>
#include "im2d_api/im2d.hpp"
#include "im2d_api/im2d_common.h"
#endif#define LOGE(msg,...)   ALOGE("%s(%d): " msg ,__FUNCTION__,__LINE__,##__VA_ARGS__)
#define LOGD(msg,...)   ALOGD("%s(%d): " msg ,__FUNCTION__,__LINE__,##__VA_ARGS__)
#define LOGV(msg,...)   ALOGV("%s(%d): " msg ,__FUNCTION__,__LINE__,##__VA_ARGS__)using namespace std;
using namespace android;
#include <rockchip/hardware/test/1.0/ITest.h>
class FrameWarpperImp :public rockchip::hardware::test::V1_0::ITestWarpper{public:android::hardware::Return<void> GetString(GetString_cb _hidl_cb) {// TODO implement::android::hardware::hidl_string deviceId = "hellow world";_hidl_cb(deviceId);return android::hardware::Void();}~FrameWarpperImp(){ALOGD("%s",__FUNCTION__);}FrameWarpperImp(){ALOGD("%s",__FUNCTION__);}
};
FrameWarpperImp *frameWarpperImp;
void init()
{::android::hardware::hidl_string testId;ALOGD("%s",__FUNCTION__);android::sp<rockchip::hardware::test::V1_0::ITest> client = rockchip::hardware::test::V1_0::ITest::getService();if(client.get()!= nullptr){        if (frameWarpperImp != nullptr){client->setTestWarpper(nullptr);frameWarpperImp = nullptr;}        frameWarpperImp = new FrameWarpperImp();ALOGD("setFrameDecorator");client->setTestWarpper(frameWarpperImp);client->GetString([&](const ::android::hardware::hidl_string &id){testId = id;ALOGD("setFrameDecorator%s",testId.c_str());});}
}
void deinit(){ALOGD("%s",__FUNCTION__);android::sp<rockchip::hardware::test::V1_0::ITest> client = rockchip::hardware::test::V1_0::ITest::getService();ALOGE("client.get():%p",client.get());if(client.get()!= nullptr){ALOGD("setFrameDecorator nullptr");client->setTestWarpper(nullptr);//delete frameWarpperImp ;frameWarpperImp = nullptr;}
}

packages/apps/libtest_bridge/Android.bp

//生成一个libtest_bridge.so
cc_library_shared {name: "libtest_bridge",srcs: ["data_bridge.cpp"],header_libs: ["jni_headers",],include_dirs: ["hardware/rockchip/librga",],shared_libs: ["libbase","libandroid","liblog","libutils","libcutils","libnativehelper","libhidlbase","libui","librga",
//rga_proc        "librga","rockchip.hardware.test@1.0",],cflags: ["-Wall","-Wno-error","-Wextra","-Wno-unused-parameter",],sanitize: {scs: true,},
}
ndk_library {name: "libtest_bridge",symbol_file: "libtest_bridge.map.txt",first_version: "30",
}
prebuilt_etc {name: "public.libtest_bridge",src: "public.libtest_bridge.txt",filename_from_src: true,
}//生成一个可执行文件test1.0_service
cc_binary {name: "test1.0_service",proprietary: true,relative_install_path: "hw",srcs: ["service.cpp",],defaults: ["hidl_defaults"],vendor: true,sdk_version: "current",cflags: ["-Wall","-Wno-error","-Wextra","-Wno-unused-parameter",],shared_libs: ["liblog","libtest_bridge",],
}

添加文件packages\apps\libtest_bridge\libtest_bridge.map.txt
LIBDATA_BRIDGE {global:init;local:*;
};添加文件packages\apps\libtest_bridge\public.libtest_bridge.txt
libtest_bridge.so nopreload添加文件packages\apps\libtest_bridge\service.cpp
#define LOG_TAG "rockchip.hardware.test@1.0-service"
#include "data_bridge.h"int main() {init();
}

编译完成后将系统的test1.0_service 推到 data/loal/tmp

执行./test1.0_service 

09-05 07:08:06.633  2892  2892 D data_bridge: init
09-05 07:08:06.634  2892  2892 D data_bridge: FrameWarpperImp
09-05 07:08:06.634  2892  2892 D data_bridge: setFrameDecorator
09-05 07:08:06.635   454   454 D test1.0 : @GetString,mDeviceId:
09-05 07:08:06.635   454   454 D test1.0 : @operator(),GetString mDeviceId:
09-05 07:08:06.636  2892  2892 D data_bridge: setFrameDecoratorhellow world

本文章只做自己学习记录

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/52107.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Windows键盘快捷方式

键盘快捷方式是两个或多个键的组合&#xff0c;可用于执行通常需要鼠标或其他指针设备才能执行的任务。 使用键盘快捷方式你可以更轻松地与电脑进行交互&#xff0c;从而在使用 Windows 和其他应用时节省时间和精力。 大多数应用还提供加速键&#xff0c;以让你能够更轻松地使…

大数据-120 - Flink Window 窗口机制-滑动时间窗口、会话窗口-基于时间驱动基于事件驱动

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; 目前已经更新到了&#xff1a; Hadoop&#xff08;已更完&#xff09;HDFS&#xff08;已更完&#xff09;MapReduce&#xff08;已更完&am…

自定义 SpringBoot Starter

文章目录 一、自定义 starter1.1 创建 maven 项目1.2 创建邮件配置属性类1.3 创建模拟邮件发送服务类1.4 创建自动配置类1.5 spring.factories 相关配置1.6 打包成依赖 二、测试项目2.1 创建项目2.2 application.yml 配置2.3 测试应用 参考资料 本文源码位于 java-demos/spring…

Restful风格接口开发

一、项目搭建 安装nestjs脚手架 // 安装nestjs脚手架 npm i nestjs/cli// 新建 nest new [名字]//选择要用的工具 npm / yarn / pnpm 文件信息&#xff1a; 【main.ts】&#xff1a; 入口文件&#xff0c;通过NestFactory&#xff08;由nestjs/core库抛出的对象&#x…

微信小程序手写签名

微信小程序手写签名组件 该组件基于signature_pad封装&#xff0c;signature_pad本身是web端的插件&#xff0c;此处将插件代码修改为小程序端可用。 signature_pad.js /*!* Signature Pad v5.0.3 | https://github.com/szimek/signature_pad* (c) 2024 Szymon Nowak | Releas…

九盾叉车U型区域警示灯,高效照明和安全警示

叉车运作的环境比较复杂&#xff0c;在方便人们物流运输的同时也存在着很大的安全隐患&#xff0c;特别是叉车碰撞人的事故发生率很高&#xff0c;那我们该怎么在减少成本的同时又能避免碰撞事故的发生呢&#xff1f; 九盾叉车U型区域警示灯&#xff0c;仅需一盏灯安装在叉车尾…

十一 面向对象技术(考点篇)试题

A &#xff1b;D&#xff0c;D。实际答案&#xff1a;C&#xff1b;D&#xff0c;D 考的很偏了。UML 2.0基础结构的设计目标是定义一个元语言的核心 UML 2.0 【InfrastructureLibrary】,通过对此核心的复用&#xff0c;除了可以定义一个自展的UML元模型&#xff0c;也可以 Infr…

基于IP子网的VLAN典型配置举例(H3C,其他厂商同理)

基于IP子网的VLAN典型配置举例 1. 组网需求 如下图所示&#xff0c;办公区的主机属于不同的网段192.168.5.0/24和192.168.50.0/24&#xff0c;Device C在收到来自办公区主机的报文时&#xff0c;根据报文的源IP地址&#xff0c;使来自不同网段主机的报文分别在指定的VLAN中传…

7、Django Admin删除默认应用程序

admin文件 from django.contrib.auth.models import User, Groupadmin.site.unregister(User) admin.site.unregister(Group) 显示效果&#xff1a; 前 后

基于FreeRTOS的STM32多功能手表

前言 项目背景 项目演示 使用到的硬件 项目原理图 目前版本实现的功能 设计到的freertos知识 实现思路 代码讲解 初始化GPIO引脚、配置时钟 蜂鸣器初始化以及软件定时器创建 系统默认创建的defaultTaskHandle 创建七个Task&#xff0c;代表七个功能 ShowTimeTask …

2024.9自然语言及语言处理设计开发工程师专项培训通知!

为进一步贯彻落实中共中央印发《关于深化人才发展体制机制改革的意见》和国务院印发《关于“十四五”数字经济发展规划》等有关工作的部署要求&#xff0c;深入实施人才强国战略和创新驱动发展战略&#xff0c;加强全国数字化人才队伍建设&#xff0c;持续推进人工智能从业人员…

Epoll 用法

Epoll 监听 EPOLL_CTL_DEL EPOLL_CTL_ADD epoll_event event event.events event.data.fd

双指针(3)_快慢指针_快乐数问题

个人主页&#xff1a;C忠实粉丝 欢迎 点赞&#x1f44d; 收藏✨ 留言✉ 加关注&#x1f493;本文由 C忠实粉丝 原创 双指针(3)_快慢指针_快乐数问题 收录于专栏【经典算法练习】 本专栏旨在分享学习C的一点学习笔记&#xff0c;欢迎大家在评论区交流讨论&#x1f48c; 目录 1.…

Android Studio gradle下载太慢了!怎么办?(已解决)

Android Studio&#xff01;你到底干了什么&#xff1f;&#xff01; 不能高速下载gradle&#xff0c;我等如何进行app编程&#xff1f;&#xff01; 很简单&#xff0c;我修改gradle地址不就是了。 找到gradle-wrapper.properties文件 修改其中distributionUrl的地址。 将 ht…

ES配合高德地图JS-API实现地理位置查询

目录 实现功能点 技术选型 具体实现 Vue3整合高德地图JS API-2.0 添加商户&#xff1a;前端 添加商户&#xff1a;后端/ES 查询用户当前地理坐标 获取附近&#xff08;指定距离&#xff09;的商户 总结/测试Demo代码地址 测试概述&#xff1a;用户使用高德地图组件获取商户…

SpringBoot2:学SpringBoot前的知识准备-用IDEA创建传统的webapp工程,并整合SpringMVC

1、IDEA创建工程 基于Maven模板创建的SpringMVC工程 工程创建好后&#xff0c;只有webapp目录 这里&#xff0c;我们需要手动创建java目录和resources配置文件目录 创建好后&#xff0c;配置下目录属性 最终结构 至此&#xff0c;工程就创建好了 2、配置Tomcat 参考&am…

论文笔记:2023顶会SIGIR - Strategy-aware Bundle Recommender System

论文笔记&#xff1a;2023顶会SIGIR - Strategy-aware Bundle Recommender System

【Python报错已解决】`Provisional headers are shown Learn more`

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 文章目录 引言&#xff1a;一、问题描述&#xff1a;1.1 报错示例&#xff1a;1.2 报错分析&#xff1a;1.3 解决思路&#xff…

145. 利用 Redis Bitmap实践: 用户签到统计

文章目录 一、Redis Bitmap简介二、Bitmap 的主要应用三、Go使用Redis实现签到统计用户签到查询用户签到状态统计今年累计签到天数统计当月的签到情况 总结 在现代应用程序中&#xff0c;用户签到是一个常见的功能。我们通常使用 MySQL 数据库来存储用户的签到记录。然而&#…

智能家居系统(基于STM32F103C8T6标准库+FreeRTOS+Qt串口开发实现)

视频演示&#xff1a;基于STM32F103C8T6标准库FreeRTOSQt串口开发实现的智能家居项目_哔哩哔哩_bilibili 基于STM32F103C8T6标准库FreeRTOSQt串口开发实现的智能家居项目: https://pan.baidu.com/s/1f41gAfOOnlcQoKoMx3o84A?pwd6j2g 提取码: 6j2g 注&#xff1a;本项目为学习完…