Android,RPC原理,C语言实现Binder跨进程通信Demo

RPC原理图

在这里插入图片描述

Binder C语言层的Demo演示

新建目录

在这里插入图片描述

把两个文件拷贝到我们的Demo下面

在这里插入图片描述

1.binder_server.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/types.h>
#include <stdbool.h>
#include <string.h>
#include "binder.h"

#define LOG_TAG "BinderServer"
#include <log/log.h>

#define HELLO_BINDER     1
#define HELLO_BINDER_TO  2


//服务就是被调用的函数
void hellobinder(void)
{
	static int cnt = 0;
    ALOGW("hello : %d\n", ++cnt);
}


int hellobinder_to(char *name)
{
	static int cnt = 0;
    ALOGW("hello to %s : %d\n", name, ++cnt);
	return cnt;
}


//回调函数
int hellobinder_service_handler(struct binder_state *bs,
                   struct binder_transaction_data_secctx *txn_secctx,
                   struct binder_io *msg,
                   struct binder_io *reply)
{
    struct binder_transaction_data *txn = &txn_secctx->transaction_data;

	/* 根据txn->code知道要调用哪一个函数
	 * 参数, 从msg取出
	 * 返回结果, 把结果放入reply
	 */

	/* sayhello
	 * sayhello_to
	 */
	
    uint16_t *s;
	char name[512];
    size_t len;
    //uint32_t handle;
    uint32_t strict_policy;
	int i;


    // Equivalent to Parcel::enforceInterface(), reading the RPC
    // header with the strict mode policy mask and the interface name.
    // Note that we ignore the strict_policy and don't propagate it
    // further (since we do no outbound RPCs anyway).
    strict_policy = bio_get_uint32(msg);
	//code 用于判断我们需要调用哪一个函数,客户端远程调用哪个服务端函数
    switch(txn->code) { 
    case HELLO_BINDER:
		hellobinder();
            //给reply写一个值为0
		bio_put_uint32(reply, 0); /* no exception */
        return 0;

    case HELLO_BINDER_TO:
		/* 从msg里取出字符串 */
		s = bio_get_string16(msg, &len);  //"IHelloService"
		s = bio_get_string16(msg, &len);  // name
		if (s == NULL) {
			return -1;
		}
		for (i = 0; i < len; i++)
			name[i] = s[i];
		name[i] = '\0';

		/* 处理 */
		i = hellobinder_to(name);

		/* 把结果放入reply 给回客户端*/
		bio_put_uint32(reply, 0); /* no exception */
		bio_put_uint32(reply, i);
		
        break;

    default:
        fprintf(stderr, "unknown code %d\n", txn->code);
        return -1;
    }

    return 0;
}

// 进程的 Binder 回调函数
//binder_transaction_data_secctx主要的数据,msg客户端传递过来的函数的参数,reply返回给客户端的数据
int test_server_handler(struct binder_state *bs,
                struct binder_transaction_data_secctx *txn_secctx,
                struct binder_io *msg,
                struct binder_io *reply)
{
    //取出数据,
    struct binder_transaction_data *txn = &txn_secctx->transaction_data;
	//函数指针,
    int (*handler)(struct binder_state *bs,
                   struct binder_transaction_data *txn,
                   struct binder_io *msg,
                   struct binder_io *reply);

    // txn->target.ptr 是 svcmgr_publish 传入的第二个参数
	handler = (int (*)(struct binder_state *bs,
                   struct binder_transaction_data *txn,
                   struct binder_io *msg,
                   struct binder_io *reply))txn->target.ptr;
	//调用函数指针,那么就会调用txn->target.ptr;这个指针,那么就是hellobinder_service_handler
	return handler(bs, txn, msg, ,reply);
}


int main(int argc, char **argv)
{
    struct binder_state *bs;
    //值为 0
    uint32_t svcmgr = BINDER_SERVICE_MANAGER;
    uint32_t handle;
	int ret;

    
    //初始化驱动
    bs = binder_open("/dev/binder", 128*1024);
    if (!bs) {
        fprintf(stderr, "failed to open binder driver\n");
        return -1;
    }

    //添加服务hellobinder_service_handler 是 hello 服务对应的回调函数
    //bs打开启动返回的一个int值,句柄,svcmgr表示把数据发到这个进程中,是ServiceManager,hello是注册的服务的名字
    //当我们注册的时候把这个hellobinder_service_handler指针传给驱动,驱动就记住hello服务回调是它,客户端需要调用hello服务的时候
	ret = svcmgr_publish(bs, svcmgr, "hello", hellobinder_service_handler);
    if (ret) {
        fprintf(stderr, "failed to publish hello service\n");
        return -1;
    }
    
    //test_server_handler  进程的 Binder 回调函数,进入循环服务端就不会挂掉,一直执行
    //binder收到数据,就解析,解析好就传给test_server_handler
    binder_loop(bs, test_server_handler);

    return 0;
}

在这里插入图片描述

binder_transaction_data_secctx

在这里插入图片描述

主要的数据结构是在这里

在这里插入图片描述

2.binder_client.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <linux/types.h>
#include <stdbool.h>
#include <string.h>
#include "binder.h"

#define HELLO_BINDER     1
#define HELLO_BINDER_TO  2


int g_handle = 0;
struct binder_state *g_bs;

void sayhello(void)
{
   
    unsigned iodata[512/4];
    struct binder_io msg, reply;

	/* 构造binder_io */
    //初始化msg数据,表示的是所调用的服务端函数所需的参数
    bio_init(&msg, iodata, sizeof(iodata), 4);
   

	/* 放入参数 */
    //把数据写入msg中
    bio_put_uint32(&msg, 0);  // strict mode header
    bio_put_string16_x(&msg, "IHelloService");

	/* 调用binder_call */
    //发起远程调用,g_bs是binder_open返回的一个句柄,msg函数的参数,reply服务端返回值,g_handle是hello服务在客户端的一个句柄索引,通过它才能找到对应的服务端,HELLO_BINDER是code,表示要调用服务端的哪个函数
    if (binder_call(g_bs, &msg, &reply, g_handle, HELLO_BINDER))
        return ;
	
	/* 从reply中解析出返回值 */
    //解析服务端返回的数据
    binder_done(g_bs, &msg, &reply);
	
}

int main(int argc, char **argv)
{
    int fd;
    struct binder_state *bs;
    uint32_t svcmgr = BINDER_SERVICE_MANAGER;
	int ret;

    bs = binder_open("/dev/binder", 128*1024);
    if (!bs) {
        fprintf(stderr, "failed to open binder driver\n");
        return -1;
    }

    g_bs = bs;

	/* get service 查找服务,bs是binder_open的返回值,svcmgr是servicemanager表示数据要发送给它,hello是查找服务的名字*/
    //g_handle句柄,索引
	g_handle = svcmgr_lookup(bs, svcmgr, "hello");
	if (!g_handle) {
        return -1;
	} 

    //调用服务,发起远程调用
    sayhello();

}

编写bp文件

cc_defaults {
    name: "bindertestflags",

    cflags: [
        "-Wall",
        "-Wextra",
        "-Werror",
        "-Wno-unused-parameter",
        "-Wno-missing-field-initializers",
        "-Wno-unused-parameter",
        "-Wno-unused-variable",
        "-Wno-incompatible-pointer-types",
        "-Wno-sign-compare",
    ],
    product_variables: {
        binder32bit: {
            cflags: ["-DBINDER_IPC_32BIT=1"],
        },
    },

    shared_libs: ["liblog"],
}
//c的可执行程序
cc_binary {
    name: "binderclient",
    defaults: ["bindertestflags"],
    vendor: true, 
    srcs: [
        "binder_client.c",
        "binder.c",
    ],
}

cc_binary {
    name: "binderserver",
    defaults: ["bindertestflags"],
    vendor: true, 
    srcs: [
        "binder_server.c",
        "binder.c",
    ],
}

// cc_binary {
//     name: "myservicemanager",
//     defaults: ["mybindertest_flags"],
//     srcs: [
//         "service_manager.c",
//         "binder.c",
//     ],
//     shared_libs: ["libcutils", "libselinux"],
// }


把编译出来的两个二进制文件push到设备中测试

在这里插入图片描述

看到hello已经被调用了说明跨进程通信成功了

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

odoo的采购询价单,默认情况下显示‘draft‘,‘sent‘,‘purchase‘,请问什么情况下才会显示‘to approve‘?

odoo的采购询价单&#xff0c;默认情况下显示’draft’,‘sent’,‘purchase’&#xff0c;请问什么情况下才会显示’to approve’? 见下图&#xff1a; 这与操作人员的角色是相关的&#xff1a; 当操作人员是群组 “采购 / 用户”时&#xff0c;点击“confirm order/确认订…

越复杂的CoT越有效吗?Complexity-Based Prompting for Multi-step Reasoning

Complexity-Based Prompting for Multi-step Reasoning 论文&#xff1a;https://openreview.net/pdf?idyf1icZHC-l9 Github&#xff1a;https://github.com/FranxYao/chain-of-thought-hub 发表位置&#xff1a;ICLR 2023 Complexity-Based Prompting for Multi-step Reason…

【C语言】算法:二分查找

当我们想在一个有序的序列里面查找一个数字的时候&#xff0c;通常会想到使用循环遍历&#xff0c;也就是下面这种方法&#xff1a; 比如我们想在下面的数组里面找到7&#xff1a; int main() {int num 7;int arr[10] { 1,2,3,4,5,6,7,8,9,10 };for (int i 0; i < size…

5.树莓派4b+ubuntu18.04(ros版本melodic)+arduino mega自制两轮差速小车,实现建图导航功能

这一节介绍雷达的使用&#xff0c;我们使用的雷达型号是ydlidar x3 1.进入工作空间 cd catkin_ws/src2.下载官方提供的SDK文件 git clone https://github.com/YDLIDAR/YDLidar-SDK.git3.安装cmake sudo apt install cmake pkg-config4.编译和安装 进入YDLidar-SDK文件夹后如…

Linux动态网站架构(部署开发php代码)

动态网站架构&#xff08;部署开发php代码&#xff09; 测试能否直接部署nginx需要什么服务&#xff0c;及原理准备并进行开发测试部署代码 概述 静态网站&#xff1a;图片仅仅包含&#xff1a;html&#xff0c;css样式js脚本&#xff0c;图片及视频&#xff1b;nginx直接处…

DHCP原理1-单个局域网出现多个DHCP服务器会发生什么

1. 背景 DHCP全称是Dynamic Host Configuration Protocol。其协议标准是RFC1541&#xff08;已被RFC2131取代&#xff09;&#xff0c;主要实现服务器向客户端动态分配IP地址&#xff08;如IP地址、子网掩码、网关、DNS&#xff09;和配置信息。其系统架构是标准的C/S架构。RFC…

如何写出高效的代码?

1. 优化你的程序&#xff0c;拒绝创建不必要的对象 如果你的变量&#xff0c;后面的逻辑判断&#xff0c;一定会被赋值&#xff1b;或者说&#xff0c;只是一个字符串变量&#xff0c;直接初始化字符串常量就可以了&#xff0c;没有必要愣是要new String(). 反例&#xff1a;…

Redis 的安装与部署

本文为Redis的Linux版单机部署。 上传 redis-3.2.8 源码到 /opt/software/ 解压到 /opt/module/ [huweihadoop101 software]$ tar -zxvf redis-3.2.8.tar.gz -C /opt/module/安装依赖 [huweihadoop101 software]$ sudo yum -y install gcc-c tclRedis是C语言编写的 编译安装…

(南京观海微电子)——DC-DC和LDO的原理及应用区别

LDO: 低压差线性稳压器&#xff0c;故名思意为线性的稳压器&#xff0c;仅能使用在降压应用中&#xff0c;也就是输出电压必需小于输入电压。 优点&#xff1a;稳定性好&#xff0c;负载响应快&#xff0c;输出纹波小。 缺点&#xff1a; 效率低&#xff0c;输入输出的电压…

Linux使用——查看发行版本、内核、shell类型等基本命令

先做快照 虚拟机中编辑网络 关机 普通账户和管理员账户 互相对照 localhost相当于IP 参数: 短格式:以减号(-)开头&#xff0c;参数字母 长格式:以2个减号(--)后跟上完整的参数单词 当前发行版本 [rootserver ~]# cat /etc/redhat-release Red Hat Enterprise Linux release 9.…

.hmallox勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复

导言&#xff1a; 在当今数字化时代&#xff0c;勒索病毒已经成为网络安全的一大威胁&#xff0c;其中包括了最近出现的.hmallox勒索病毒。这类恶意软件不仅能够对计算机系统进行加密&#xff0c;还会要求用户支付赎金以换取解密密钥&#xff0c;给个人用户和企业带来了严重的…

Java面试题:数据库索引

数据库索引 索引:index 帮助mysql高效获取数据的数据结构,在数据之外,数据库系统还维护着满足特定查找算法的数据结构(B树),这些数据结构以某种方式引用(指向数据),这样就可以在数据结构上实现高级查找算法 将数据以树的方式进行存储,提高查找效率 索引的底层结构 使用B树 …

React AntDesign Layout组件布局刷新页面错乱闪动

大家最近在使用React AntDesign Layout组件布局后刷新页面时&#xff0c;页面布局错乱闪动 经过组件属性的研究才发现&#xff0c;设置 hasSider 为 true 就能解决上面的问题&#xff0c;耽搁了半天的时间&#xff0c;接着踩坑接着加油&#xff01;&#xff01;&#xff01; …

XTDrone-多无人机精准降落-配置教程

1 编译AprilTag_ROS AprilTag是一个视觉基准系统&#xff0c;可用于机器人&#xff0c;增强现实和相机校准等。 根据AprilTag可以可靠地计算标签相对于相机的3D位置&#xff0c;方向和ID号。这里我们使用AprilTag的ROS库来实现位姿估计与ID号计算。 编译命令如下&#xff1a; …

Linux 7种 进程间通信方式

传统进程间通信 通过文件实现进程间通信 必须人为保证先后顺序 A--->硬盘---> B&#xff08;B不知道A什么时候把内容传到硬盘中&#xff09; 1.无名管道 2.有名管道 3.信号 IPC进程间通信 4.消息队列 5.共享内存 6.信号灯集 7.socket通信 一、无名管道&a…

基于I2C协议的AHT20温湿度传感器的数据采集

一、I2C总线通信协议 软件I2C 软件I2C&#xff0c;也称为模拟I2C或bit-bang I2C&#xff0c;是一种通过微控制器的通用输入输出&#xff08;GPIO&#xff09;引脚来模拟I2C总线通信的方式。它不依赖于专门的硬件I2C接口&#xff0c;而是通过编程控制GPIO引脚的电平状态来实现I…

我在高职教STM32——LCD液晶显示(3)

大家好&#xff0c;我是老耿&#xff0c;高职青椒一枚&#xff0c;一直从事单片机、嵌入式、物联网等课程的教学。对于高职的学生层次&#xff0c;同行应该都懂的&#xff0c;老师在课堂上教学几乎是没什么成就感的。正因如此&#xff0c;才有了借助 CSDN 平台寻求认同感和成就…

前端开发流程与技术选型

目录 一、简介 二、前端职责 三、开发步骤 四、技术选型 五、页面展示 一、简介 做一个网站时&#xff0c;能看到的一切都是前端程序员的工作&#xff0c;负责网页或者app的结构、样式、用户操作网站时的事件逻辑&#xff08;比如点击一个按钮&#xff09;。 二、前端职…

一、系统学习微服务遇到的问题集合

1、启动了nacos服务&#xff0c;没有在注册列表 应该是版本问题 Alibaba-nacos版本 nacos-文档 Spring Cloud Alibaba-中文 Spring-Cloud-Alibaba-英文 Spring-Cloud-Gateway 写的很好的一篇文章 在Spring initial上面配置 start.aliyun.com 重新下载 < 2、 No Feign…

嵌入式系统中的加解密签名

笔者来了解一下嵌入式系统中的加解密 1、背景与名词解释 笔者最近在做安全升级相关的模块&#xff0c;碰到了一些相关的概念和一些应用场景&#xff0c;特来学习记录一下。 1.1 名词解释 对称加密&#xff1a;对称加密是一种加密方法&#xff0c;使用相同的密钥&#xff08;…