Android系统--Binder系统具体框架分析(一)补充

补充:对Binder驱动分析一的代码补充,添加saygoobye和saygoodbye_to服务

test_server.h

#ifndef _TEST_SERVER_H#define _TEST_SERVER_H#define HELLO_SVR_CMD_SAYHELLO 0#define HELLO_SVR_CMD_SAYHELLO_TO 1#define GOODBYE_SVR_CMD_SAYGOODBYE 0#define GOODBYE_SVR_CMD_SAYGOODBYE_TO 1#endif // _TEST_SERVER_H
test_server.c

/* Copyright 2008 The Android Open Source Project*/#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <linux/types.h>#include<stdbool.h>#include <string.h>#include <private/android_filesystem_config.h>#include "binder.h"#include "test_server.h"int svcmgr_publish(struct binder_state *bs, uint32_t target, const char *name, void *ptr){int status;unsigned iodata[512/4];struct binder_io msg, reply;bio_init(&msg, iodata, sizeof(iodata), 4);  //分配binder_io结构体空间bio_put_uint32(&msg, 0);  // strict mode headerbio_put_string16_x(&msg, SVC_MGR_NAME);bio_put_string16_x(&msg, name);  bio_put_obj(&msg, ptr);  //构造结构体if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE))  //添加注册Binder服务return -1;status = bio_get_uint32(&reply);  //获得返回值binder_done(bs, &msg, &reply);   //结束标志return status;}void sayhello(void){static int cnt = 0;fprintf(stderr, "say hello : %d\n", cnt++);}int sayhello_to(char *name){static int cnt = 0;fprintf(stderr, "say hello to %s : %d\n", name, cnt++);return cnt;}void saygoodbye(void){static int cnt = 0;fprintf(stderr, "say goodbye : %d\n", cnt++);}int saygoodbye_to(char *name){static int cnt = 0;fprintf(stderr, "say goodbye to %s : %d\n", name, cnt++);return cnt;}int hello_service_handler(struct binder_state *bs,struct binder_transaction_data *txn,struct binder_io *msg,struct binder_io *reply){/* 根据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);//获得处理函数标志,并调用处理函数switch(txn->code) {case HELLO_SVR_CMD_SAYHELLO:sayhello();return 0;case HELLO_SVR_CMD_SAYHELLO_TO:/* 从msg里取出字符串 */s = bio_get_string16(msg, &len);if (s == NULL) {return -1;}for (i = 0; i < len; i++)name[i] = s[i];name[i] = '\0';/* 处理 */i = sayhello_to(name);/* 把结果放入reply */bio_put_uint32(reply, i);break;default:fprintf(stderr, "unknown code %d\n", txn->code);return -1;}return 0;}int goodbye_service_handler(struct binder_state *bs,struct binder_transaction_data *txn,struct binder_io *msg,struct binder_io *reply){/* 根据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);switch(txn->code) {case GOODBYE_SVR_CMD_SAYGOODBYE:saygoodbye();return 0;case GOODBYE_SVR_CMD_SAYGOODBYE_TO:/* 从msg里取出字符串 */s = bio_get_string16(msg, &len);if (s == NULL) {return -1;}for (i = 0; i < len; i++)name[i] = s[i];name[i] = '\0';/* 处理 */i = saygoodbye_to(name);/* 把结果放入reply */bio_put_uint32(reply, i);break;default:fprintf(stderr, "unknown code %d\n", txn->code);return -1;}return 0;}int test_server_handler(struct binder_state *bs,struct binder_transaction_data *txn,struct binder_io *msg,struct binder_io *reply){int (*handler)(struct binder_state *bs,struct binder_transaction_data *txn,struct binder_io *msg,struct binder_io *reply);  //构造处理方法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返回处理相应方法return handler(bs, txn, msg, reply);  //返回所调用的处理方法}int main(int argc, char **argv){int fd;struct binder_state *bs;uint32_t svcmgr = BINDER_SERVICE_MANAGER;uint32_t handle;int ret;bs = binder_open(128*1024);  //打开Binder驱动设备if (!bs) {fprintf(stderr, "failed to open binder driver\n");return -1;}/* add service */ret = svcmgr_publish(bs, svcmgr, "hello", hello_service_handler);  //注册if (ret) {fprintf(stderr, "failed to publish hello service\n");return -1;}ret = svcmgr_publish(bs, svcmgr, "goodbye", goodbye_service_handler);if (ret) {fprintf(stderr, "failed to publish goodbye service\n");}#if 0while (1){/* read data *//* parse data, and process *//* reply */}#endifbinder_loop(bs, test_server_handler);  //从队列中取出响应的处理函数return 0;}
server.c

/* Copyright 2008 The Android Open Source Project*/#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <linux/types.h>#include<stdbool.h>#include <string.h>#include <private/android_filesystem_config.h>#include "binder.h"#include "test_server.h"uint32_t svcmgr_lookup(struct binder_state *bs, uint32_t target, const char *name){uint32_t handle;unsigned iodata[512/4];struct binder_io msg, reply;bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0);  // strict mode headerbio_put_string16_x(&msg, SVC_MGR_NAME);bio_put_string16_x(&msg, name);if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE))return 0;handle = bio_get_ref(&reply);if (handle)binder_acquire(bs, handle);binder_done(bs, &msg, &reply);return handle;}struct binder_state *g_bs;uint32_t g_hello_handle;uint32_t g_goodbye_handle;void sayhello(void){unsigned iodata[512/4];struct binder_io msg, reply;/* 构造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0);  // strict mode header/* 放入参数 *//* 调用binder_call */if (binder_call(g_bs, &msg, &reply, g_hello_handle, HELLO_SVR_CMD_SAYHELLO))return ;/* 从reply中解析出返回值 */binder_done(g_bs, &msg, &reply);}int sayhello_to(char *name){unsigned iodata[512/4];struct binder_io msg, reply;int ret;/* 构造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0);  // strict mode header/* 放入参数 */bio_put_string16_x(&msg, name);/* 调用binder_call */if (binder_call(g_bs, &msg, &reply, g_hello_handle, HELLO_SVR_CMD_SAYHELLO_TO))return 0;/* 从reply中解析出返回值 */ret = bio_get_uint32(&reply);binder_done(g_bs, &msg, &reply);return ret;}void saygoodbye(void){unsigned iodata[512/4];struct binder_io msg, reply;/* 构造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0);  // strict mode header/* 放入参数 *//* 调用binder_call */if (binder_call(g_bs, &msg, &reply, g_goodbye_handle, GOODBYE_SVR_CMD_SAYGOODBYE))return ;/* 从reply中解析出返回值 */binder_done(g_bs, &msg, &reply);}int saygoodbye_to(char *name){unsigned iodata[512/4];struct binder_io msg, reply;int ret;/* 构造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0);  // strict mode header/* 放入参数 */bio_put_string16_x(&msg, name);/* 调用binder_call */if (binder_call(g_bs, &msg, &reply, g_goodbye_handle, GOODBYE_SVR_CMD_SAYGOODBYE_TO))return 0;/* 从reply中解析出返回值 */ret = bio_get_uint32(&reply);binder_done(g_bs, &msg, &reply);return ret;}/* ./test_client hello* ./test_client hello <name>*/int main(int argc, char **argv){int fd;struct binder_state *bs;uint32_t svcmgr = BINDER_SERVICE_MANAGER;uint32_t handle;int ret;if (argc < 2){fprintf(stderr, "Usage:\n");fprintf(stderr, "%s <hello|goodbye>\n", argv[0]);fprintf(stderr, "%s <hello|goodbye> <name>\n", argv[0]);return -1;}bs = binder_open(128*1024);  //打开Binder驱动设备if (!bs) {fprintf(stderr, "failed to open binder driver\n");return -1;}g_bs = bs;/* get service */handle = svcmgr_lookup(bs, svcmgr, "goodbye");  //查找获取服务的引用if (!handle) {fprintf(stderr, "failed to get goodbye service\n");return -1;}g_goodbye_handle = handle;fprintf(stderr, "Handle for goodbye service = %d\n", g_goodbye_handle);handle = svcmgr_lookup(bs, svcmgr, "hello");if (!handle) {fprintf(stderr, "failed to get hello service\n");return -1;}g_hello_handle = handle;fprintf(stderr, "Handle for hello service = %d\n", g_hello_handle);/* send data to server */if (!strcmp(argv[1], "hello")){if (argc == 2) {sayhello();} else if (argc == 3) {ret = sayhello_to(argv[2]);fprintf(stderr, "get ret of sayhello_to = %d\n", ret);      }} else if (!strcmp(argv[1], "goodbye")){if (argc == 2) {saygoodbye();} else if (argc == 3) {ret = saygoodbye_to(argv[2]);fprintf(stderr, "get ret of sayhello_to = %d\n", ret);      }}binder_release(bs, handle);  //释放binder_acquire(bs, handle);return 0;}

转载于:https://www.cnblogs.com/lkq1220/p/6480375.html

Android系统--Binder系统具体框架分析(一)补充相关推荐

  1. Android系统硬件访问服务框架分析

    怎么实现硬件访问服务 1.JNI和HAL com_andorid_server_ledService.cpp (JNI文件注册JNI本地方法:供app应用程序调用) hal_led.c (C库:具体操 ...

  2. Android Binder 系统学习笔记(一)Binder系统的基本使用方法

    1.什么是RPC(远程过程调用) Binder系统的目的是实现远程过程调用(RPC),即进程A去调用进程B的某个函数,它是在进程间通信(IPC)的基础上实现的.RPC的一个应用场景如下: A进程想去打 ...

  3. Android 系统源码代码情景分析

    一.本书简介 <Android系统源代码情景分析>是2012年10月出版的图书,作者是罗升阳. 在内容上,本书结合使用情景,全面.深入.细致地分析了Android系统的源代码,涉及到Lin ...

  4. Android 8.0系统源码分析--开篇

    个人分类: Android框架总结Android源码解析android framework 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/sinat ...

  5. linux v4l2系统详解,Linux摄像头驱动学习之:(一)V4L2_框架分析

    这段时间开始搞安卓camera底层驱动了,把以前的的Linux视频驱动回顾一下,本篇主要概述一下vfl2(video for linux 2). 一. V4L2框架: video for linux ...

  6. 由Asset中的double free引发的Android系统及APP崩溃问题分析

    前言 这个问题在来小米之前就遇到并解决过,当时的解决方案与朴老师的初步解决方案一样,本文在之前的初步分析结果之上进一步进行了深入分析,最终得出了当前看起来相对合理并符合原来架构设计的最终方案. 文中引 ...

  7. Android 8.0系统源码分析--Camera processCaptureResult结果回传源码分析

    相机,从上到下概览一下,真是太大了,上面的APP->Framework->CameraServer->CameraHAL,HAL进程中Pipeline.接各种算法的Node.再往下的 ...

  8. Android 核心分析 之六 -----IPC框架分析 Binder,Service,Se...

    2019独角兽企业重金招聘Python工程师标准>>> 我首先从宏观的角度观察 Binder,Service,Service Manager,并阐述 各自的概念.从 Linux 的概 ...

  9. 【Android Binder 系统】一、Binder 系统核心 ( IPC 进程间通信 | RPC 远程调用 )

    文章目录 一.Binder 系统两个核心 二.IPC 进程间通信 三.RPC 远程过程调用 一.Binder 系统两个核心 Binder 系统 最重要的两个核心是 IPC 和 RPC ; IPC ( ...

最新文章

  1. 扒一扒 @SpringBootApplication 注解背后的奥秘!
  2. php屏蔽审查元素,前端:屏蔽F12审查元素,禁止修改页面代码
  3. Java性能优化推荐书!JAVA-注解的基本原理
  4. m1mac安装linux,M1 Mac 能安装 Ubuntu 和 Linux 了 ??
  5. 华为平板安装python_极致安卓—Termux/Aid Learning安装宇宙最强VS Code
  6. SAP Commerce Cloud CMS page 和 page template 的概念
  7. php延迟更新,ThinkPHP开发指南-模型-高级模型之延迟更新
  8. C# linq Expression left join如何使用
  9. hdfs的实验总结_实验2-熟悉常用的HDFS操作.doc
  10. qt5python gui cookbook_Python GUI Programming Cookbook学习笔记
  11. 记:Ubuntu 14.04 安装32位库支持库失败
  12. IOS推送消息怎么实现icon图标的数字累加
  13. 潮流设计师创作灵感|是时候设计一下蒸汽波海报了!
  14. 阿里巴巴CTO王坚:省长也是开发者
  15. securecrt8.1破解版安装与注册机的使用方法
  16. 为博聆网用户编写的userscript
  17. 利用TravisCI持续集成自动测试GitHub项目
  18. 我37岁,从互联网大厂跳槽到国企后,发现没有一劳永逸的工作。。。
  19. 如何批量修改文件名?教你一招,轻松解决
  20. 刚才先入为主到了令人发指的地步

热门文章

  1. 一个数据仓库转型者眼中的数据挖掘
  2. 数据格式转换(一)PDF转换技术
  3. Head.First.Object-Oriented.Design.and.Analysis《深入浅出面向对象的分析与设计》读书笔记(七)...
  4. c#,xp系统,Matlab6.5
  5. 符号 RUNOOB python练习题 51
  6. SpringBoot项目属性配置
  7. mybatis if-else(写法)
  8. BeanShell自动装箱拆箱
  9. Netbackup detected IBM drives as unusable
  10. 比赛一买香蕉问题---解题报告