android 新增native binder service 方式(一)

关于之前说的native service 之前有写过类似的文章,今天主要介绍下如何通过binder 方式跨进程调用和回调,结合网上的各种文章,总结了3种常见的添加方式,供大家参考。

一,aidl 文件定义

先看下整体的目录结构

libserviceaidl 就是我定义的aidi接口

IService.aidl

package com.test;import com.test.IServiceCallback;interface IService {void registerCallback(IServiceCallback callback);void call(int type);int  callMcu(int file,in byte[] type); 
}

IServiceCallback.aidl

package com.test;interface IServiceCallback{void onEventString(int eventCode, String eventMessage);void onEventbyte(out byte[] data);
}

通过Android.bp 编译成so

cc_library_shared {name: "libserviceaidl",aidl: {export_aidl_headers: true,local_include_dirs: ["aidl"],include_dirs: [],},srcs: [":serviceaidl",],shared_libs: ["libbase","libcutils","libutils","liblog","libbinder","libgui",],cflags: ["-Werror","-Wall","-Wextra",],
}filegroup {name: "serviceaidl",srcs: ["aidl/com/test/IService.aidl","aidl/com/test/IServiceCallback.aidl",],path: "aidl",
}

make libserviceaidl 

会生成libserviceaidl.so ,需要push 到我们系统对应目录,这样调用的binder 库就定义好了。

二,编译Native service 

Android.bp 添加aidl 库依赖

cc_binary {name: "testservverfirst",srcs: ["servertest.cpp",],shared_libs: ["liblog","libcutils","libutils","libbinder","libserviceaidl",// 上面编译生成的],init_rc: ["testservver.rc"],
}cc_binary {name: "clienttest",srcs: ["clienttest.cpp", ],shared_libs: ["liblog","libcutils","libutils","libbinder","libserviceaidl",],
}

servertest.cpp 源文件

#define LOG_TAG "testservverfirst"
#include <log/log.h>#include <unistd.h>
#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <termios.h> 
#include <utils/String16.h>
#include <com/test/BnService.h>using namespace std;
using namespace android;class TestService : public com::test::BnService
{public:TestService(){}binder::Status nativecall(int i) {ALOGI("server nativecall function is running %d",i);if(mycallback != nullptr){mycallback->onEventString(4,String16("callbacksuccuess"));}return binder::Status();}binder::Status  callMcu(int32_t file,const vector<uint8_t>& type, int32_t* _aidl_return){*_aidl_return = 1;ALOGI("server callMcu function is running _aidl_return",*_aidl_return);return binder::Status();}binder::Status  call(int32_t type){ALOGI("server call function is running call",type);return binder::Status();}// 实现回调注册binder::Status registerCallback(const sp<com::test::IServiceCallback>& callback){mycallback  = callback;return binder::Status();}private:sp<::com::test::IServiceCallback> mycallback;};int main(int argc, char const *argv[])
{ALOGD("Server is runing");//发布服务defaultServiceManager()->addService(String16("TestService"), new TestService());ProcessState::self()->startThreadPool();IPCThreadState::self()->joinThreadPool();return 0;
}

编译testservverfirst ,会生成testservverfirst 可执行文件,我们push 到系统/system/bin/ 下

手动执行:./system/bin/testservverfirst 会看到

01-18 17:59:16.174  4144  4144 D testservverfirst: Server is runing

说明我们的native service 运行成功了,也可以通过命令查看:

查看native service 是否运行成功

device:/ # ps -A| grep testservverfirst                                                                                                                                                                  
root           4144   3449   15340   4952 binder_ioctl_write_read 0 S testservverfirst

查看binder service 是否运行成功

130|monaco_go:/ # dumpsys | grep TestService,                                                                                                                                                               
--------- 0.009s was the duration of dumpsys TestService, ending at: 2025-01-18 18:18:52
 

三,编译client 

我们编译一个client 来本地验证下,binder 通信是否成功

make  clienttest

#define LOG_TAG "clienttest"
#include <log/log.h>#include <stdlib.h>
#include <utils/RefBase.h>
#include <utils/Log.h>
#include <binder/TextOutput.h>
#include <binder/IInterface.h>
#include <binder/IBinder.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <com/test/BnService.h>using namespace android;int main(int argc, char const *argv[])
{sp<IServiceManager> sm = defaultServiceManager();sp<IBinder> binder = sm->getService(String16("TestService"));sp<com::test::IService> simservice = interface_cast<com::test::IService>(binder);simservice->call(3);return 0;
}


生成的clienttest ,push 到系统system/bin/,手动执行 ./system/bin/clienttest,查看service 的log 

01-18 18:00:25.336  4144  4145 I testservverfirst: server call function is running call

说明调用成功了。

四,添加开机启动

这里我们加了一个rc 文件,开机会解析。

testservver.rc

on post-fschown system system /system/bin/testservverfirstchmod 0777 /system/bin/testservverfirstrestorecon_recursive /system/bin/testservverfirstservice testservverfirst /system/bin/testservverfirstclass coreconsoledisabledseclabel u:r:su:s0on property:sys.xss=2setprop xss.home 2start testservverfirst  on property:sys.boot_completed=1start testservverfirst  

这里我们监听sys.boot_completed 开机完成的属性,来启动服务,还有其他的方式,参考之前写的文章

Android 新增可执行脚本方式总结_android脚本-CSDN博客

五,增加selinux 权限

上面四个步骤,我们走完过后,发现开机服务是启动不了的,因为缺少selinux 权限,这个之前文章也介绍过,这个需要耐心,慢慢添加,大概步骤如下。

主要修改

file_contexts  添加native service 标签

testservverfirst.te 添加native 权限

service.te 添加binder service 类型

service_contexts 添加binder service 标签

调试的时候记得先关闭selinux 这样方便一次性把权限都加完。

diff --git a/prebuilts/api/30.0/private/testservverfirst.te b/prebuilts/api/30.0/private/testservverfirst.te
new file mode 100644
index 0000000..5a72981
--- /dev/null
+++ b/prebuilts/api/30.0/private/testservverfirst.te
@@ -0,0 +1,15 @@
+type testservverfirst, domain,mlstrustedsubject;
+typeattribute testservverfirst coredomain;
+type testservverfirst_exec, system_file_type, exec_type, file_type;
+binder_use(testservverfirst)
+init_daemon_domain(testservverfirst)
+
+allow testservverfirst servicemanager:binder { call transfer };
+allow testservverfirst testservverfirst_service:service_manager { add find };
+allow testservverfirst tty_device:chr_file { read write };
+allow testservverfirst untrusted_app:binder call;
+
+binder_use(testservverfirst)
+add_service(testservverfirst,testservverfirst_service)
+
+
diff --git a/prebuilts/api/30.0/private/file_contexts b/prebuilts/api/30.0/private/file_contexts
index b774920..37049f6 100644
--- a/prebuilts/api/30.0/private/file_contexts
+++ b/prebuilts/api/30.0/private/file_contexts
@@ -738,3 +738,4 @@############################## mount point for read-write product partitions/mnt/product(/.*)?          u:object_r:mnt_product_file:s0
+/system/bin/testservverfirst u:object_r:testservverfirst_exec:s0
diff --git a/prebuilts/api/30.0/private/service.te b/prebuilts/api/30.0/private/service.te
index 6c17521..77d585a 100644
--- a/prebuilts/api/30.0/private/service.te
+++ b/prebuilts/api/30.0/private/service.te
@@ -6,3 +6,4 @@type statscompanion_service,        system_server_service, service_manager_type;type statsmanager_service,          system_api_service, system_server_service, service_manager_type;type uce_service,                   service_manager_type;
+type testservverfirst_service,        service_manager_type;
diff --git a/prebuilts/api/30.0/private/service_contexts b/prebuilts/api/30.0/private/service_contexts
index 7c9f396..c1687f7 100644
--- a/prebuilts/api/30.0/private/service_contexts
+++ b/prebuilts/api/30.0/private/service_contexts
@@ -249,3 +249,4 @@window                                    u:object_r:window_service:s0glasses                                   u:object_r:sim_glasses_service:s0*                                         u:object_r:default_android_service:s0
+TestService                            u:object_r:testservverfirst_service:s0
diff --git a/prebuilts/api/30.0/private/untrusted_app.te b/prebuilts/api/30.0/private/untrusted_app.te
index 6e7a99c..97a84e3 100644
--- a/prebuilts/api/30.0/private/untrusted_app.te
+++ b/prebuilts/api/30.0/private/untrusted_app.te
@@ -14,3 +14,5 @@untrusted_app_domain(untrusted_app)net_domain(untrusted_app)bluetooth_domain(untrusted_app)
+allow untrusted_app testservverfirst:binder { transfer call };
+allow untrusted_app testservverfirst_service:service_manager find;
diff --git a/private/testservverfirst.te b/private/testservverfirst.te
new file mode 100644
index 0000000..5a72981
--- /dev/null
+++ b/private/testservverfirst.te
@@ -0,0 +1,15 @@
+type testservverfirst, domain,mlstrustedsubject;
+typeattribute testservverfirst coredomain;
+type testservverfirst_exec, system_file_type, exec_type, file_type;
+binder_use(testservverfirst)
+init_daemon_domain(testservverfirst)
+
+allow testservverfirst servicemanager:binder { call transfer };
+allow testservverfirst testservverfirst_service:service_manager { add find };
+allow testservverfirst tty_device:chr_file { read write };
+allow testservverfirst untrusted_app:binder call;
+
+binder_use(testservverfirst)
+add_service(testservverfirst,testservverfirst_service)
+
+
diff --git a/private/file_contexts b/private/file_contexts
index b774920..37049f6 100644
--- a/private/file_contexts
+++ b/private/file_contexts
@@ -738,3 +738,4 @@############################## mount point for read-write product partitions/mnt/product(/.*)?          u:object_r:mnt_product_file:s0
+/system/bin/testservverfirst u:object_r:testservverfirst_exec:s0
diff --git a/private/service.te b/private/service.te
index 6c17521..77d585a 100644
--- a/private/service.te
+++ b/private/service.te
@@ -6,3 +6,4 @@type statscompanion_service,        system_server_service, service_manager_type;type statsmanager_service,          system_api_service, system_server_service, service_manager_type;type uce_service,                   service_manager_type;
+type testservverfirst_service,        service_manager_type;
diff --git a/private/service_contexts b/private/service_contexts
index 7c9f396..c1687f7 100755
--- a/private/service_contexts
+++ b/private/service_contexts
@@ -249,3 +249,4 @@window                                    u:object_r:window_service:s0glasses                                   u:object_r:sim_glasses_service:s0*                                         u:object_r:default_android_service:s0
+TestService                            u:object_r:testservverfirst_service:s0
diff --git a/private/untrusted_app.te b/private/untrusted_app.te
index 6e7a99c..97a84e3 100644
--- a/private/untrusted_app.te
+++ b/private/untrusted_app.te
@@ -14,3 +14,5 @@untrusted_app_domain(untrusted_app)net_domain(untrusted_app)bluetooth_domain(untrusted_app)
+allow untrusted_app testservverfirst:binder { transfer call };
+allow untrusted_app testservverfirst_service:service_manager find;

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

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

相关文章

【大模型系列篇】大模型微调工具 LLama-Factory、Unsloth、ms-SWIFT

今日号外&#xff1a;&#x1f525;&#x1f525;&#x1f525; DeepSeek团队正式启动为期五天的开源计划 Day3&#xff1a;DeepGEMM。DeepGEMM 是一个专为简洁高效的 FP8 通用矩阵乘法&#xff08;GEMM&#xff09;设计的库&#xff0c;具有细粒度缩放功能&#xff0c;如 Deep…

安宝特科技 | Vuzix Z100智能眼镜+AugmentOS:重新定义AI可穿戴设备的未来——从操作系统到硬件生态,如何掀起无感智能革命?

一、AugmentOS&#xff1a;AI可穿戴的“操作系统革命” 2025年2月3日&#xff0c;Vuzix与AI人机交互团队Mentra联合推出的AugmentOS&#xff0c;被业内视为智能眼镜领域的“iOS时刻”。这款全球首个专为智能眼镜设计的通用操作系统&#xff0c;通过三大突破重新定义了AI可穿戴…

自然语言处理(NLP):文本向量化从文字到数字的原理

在人工智能领域&#xff0c;尤其是自然语言处理&#xff08;NLP&#xff09;中&#xff0c;将文本信息转化为机器可以理解的形式是一个至关重要的步骤。本文探讨如何将文本转换为向量表示的过程&#xff0c;包括分词、ID映射、One-hot编码以及最终的词嵌入&#xff08;Embeddin…

如何免费使用稳定的deepseek

0、背景&#xff1a; 在AI辅助工作中&#xff0c;除了使用cursor做编程外&#xff0c;使用deepseek R1进行问题分析、数据分析、代码分析效果非常好。现在我经常会去拿行业信息、遇到的问题等去咨询R1&#xff0c;也给了自己不少启示。但是由于官网稳定性很差&#xff0c;很多…

Cherry Studio + 火山引擎 构建个人AI智能知识库

&#x1f349;在信息化时代&#xff0c;个人知识库的构建对于提高工作效率、知识管理和信息提取尤为重要。尤其是当这些知识库能结合人工智能来智能化地整理、分类和管理数据时&#xff0c;效果更为显著。我最近尝试通过 Cherry Studio 和 火山引擎 来搭建个人智能知识库&#…

Python:循环

while循环&#xff1a; 基本格式如下&#xff1a; i1 while i<100: print(好好学习天天向上) i1 同理还有while循环嵌套&#xff1a; for循环&#xff08;迭代循环&#xff09; 基本格式&#xff1a; strhello for i in str print(i)#int整型不是迭代对象&#xff0c;需…

【leetcode hot 100 15】三数之和

一、两数之和的扩展 class Solution {public List<List<Integer>> threeSum(int[] nums) {// 将得到的结果存入Set中&#xff0c;保证不重复Set<List<Integer>> set new HashSet<>();// 模拟两数之和&#xff0c;作为第一个循环中的内容for(in…

Cesium高级开发教程之四十三:缓冲区分析#线

一、简介 基本概念:线缓冲区分析是指以 Cesium 中的线要素(如道路、河流等)为基础,在其两侧创建一定宽度的带状区域。例如,在地图上有一条河流的线数据,通过线缓冲区分析,可以得到河流两侧一定范围内的缓冲区域,用于表示河流的影响范围或进行相关的分析。实现原理:在 …

制造执行系统(MES)应用分析

全文概述 本文详细阐述了制造执行系统(MES)应用研究的主要内容,包括MES的定义、市场需求、企业选型与实施、应用现状、面临的挑战以及未来发展趋势。文章中基于广泛的行业调研,提供了详实的分析和见解。首先介绍了MES的基本概念和重要性,随后探讨了MES市场的投资、需求和选…

使用 VSCode 代替 BeyondStudio for NXP 开发 JN 5169

使用 VSCode 代替 BeyondStudio for NXP 开发 JN 5169 一、安装 VSCode二、搭建 NXP JN5169 ZigBee 3.0 开发环境和下载示例工程三、配置 VSCode1、配置环境变量 MYSYS_HOME2、VSCode 安装以下插件3、VSCode 配置头文件路径 四、编译工程1、JN-AN-1219 有 6 个构建选项2、修改 …

Spring集成 Spring AI + DeepSeek

当 Spring Boot 与 DeepSeek 相遇&#xff0c;两者的结合为开发 AI 应用程序带来了前所未有的机遇。Spring Boot 的强大功能和便捷性&#xff0c;使得开发者能够快速搭建稳定的后端服务&#xff0c;而 DeepSeek 的先进大语言模型则为应用赋予了强大的智能交互和处理能力。通过将…

CentOS中shell脚本对多台机器执行下载安装

1.建立免密ssh连接 详情见这篇&#xff1a; CentOS建立ssh免密连接&#xff08;含流程剖析&#xff09;-CSDN博客 2.脚本编写 我这里只是简单写了个demo进行演示&#xff0c;如果服务器很多可以先暂存成文件再逐行读取host进行连接并执行命令 用node1去ssh连接node2和node…

华为AP 4050DN-HD的FIT AP模式改为FAT AP,家用FAT基本配置

在某鱼买了两台华为AP 4050DN-HD , AP是二手的 , 在AC上上过线 , 所以就不能开机自选为FIP模式了 我没有AC无线控制器 , 就是买一个自己玩 , AP又是FIT瘦AP模式 ,所以我就想把AP的瘦AP模式改为FAT胖AP模式 1. 准备工作 1.1下载好对应软件&#xff0c;进入到 企业业务网站去下…

C++ 中的reduce函数使用指南

本文与reduce函数学习的知识深度很浅&#xff0c;主要是记录一下使用reduce来简化代码&#xff0c;高效解题。 参考资料&#xff1a; https://cppreference.cn/w/cpp/algorithm/reduce 1. reduce 函数概述 reduce 函数类似于 accumulate&#xff0c;但它允许更灵活的并行化和…

stm32四种方式精密控制步进电机

在搭建完clion的开发环境后&#xff0c;我决定重写之前的项目并优化完善&#xff0c;争取做出完全可落地的东西&#xff0c;也结合要写的论文内容一同学习下去。 因此&#xff0c;首当其冲的就是回到步进电机控制领域&#xff0c;把之前使用中断溢出进行步进电机控制的方案进行…

HTML——前端基础1

目录 前端概述 前端能做的事情​编辑 两步完成一个网页程序 前端工具的选择与安装 HTML HTML5介绍 HTML5的DOCTYPE声明 HTML基本骨架 文字标签 标题之标签 标签之段落、换行、水平线 标签之图片 标签之超文本链接 标签之文本 列表标签之有序列表 列表标签之无序…

【QT问题】Ubantu环境下解决已经下载好的qt怎么添加或卸载其他组件

1、找到自己qt的安装目录->双击打开MaintenanceTool.exe 2、点击next进去&#xff0c;此时需要登录qt账户&#xff08;如果没有去官网注册一个&#xff0c;很快且免费&#xff09; 我这里随便填的账号&#xff0c;如果是正确的下面next就能够点击。 这里随便提一下&#xf…

TaskBuilder设置排序条件

在整个向导的最后一步&#xff0c;可以设置是否按指定字段的值对查询结果进行排序&#xff0c;支持正序和倒序两种排序方式。如果没有设置任何排序字段&#xff0c;则默认按数据库里现有数据记录的实际存储的先后顺序排序。如果设置了多个排序条件&#xff0c;则按这些条件从上…

Taylor龙哥库塔。各种稳定性

——Taylor 龙额库塔 需要利用&#xff1a; 1.k1f 2.b21a1 3.k1fuffu | | 稳定区域 | | | | | | BDFq 方法是 A(φq)-稳定的&#xff0c;其中 φ1 φ2 90◦, φ3 ≈86.03◦, φ4 ≈73.35◦, φ5 ≈51.84◦, 和 φ6 ≈17.84◦; 参见 2 3. | | | WSBDFq 方法是 A( ˜ φ…

AI如何通过大数据分析提升制造效率和决策智能化

人工智能&#xff08;AI&#xff09;与大数据技术的融合&#xff0c;不仅重新定义了生产流程&#xff0c;更让企业实现了从“经验驱动”到“数据智能驱动”的跨越式升级。 从“模糊经验”到“精准洞察”​​ 传统制造业依赖人工经验制定生产计划&#xff0c;但面对复杂多变的市…