第五章:Binder系统(2)-C程序示例_编写程序

上一小节介绍了Binder系统的框架,为了加深理解,这小节别写一个示例程序,最要有以下程序c文件
1.test_server.c:
提供“hello”服务,
提供函数:void sayhello(void),int sayhello_to(char *name)。

下面我们开始编写test_server.c。

test_server.c

首先我们复制文件SDK\frameworks\native\cmds\servicemanager\bctest.c重命令为test_server.c,上小节多次提到该文件为一个半成品,我们在现有的基础上进行修改。存放在文件夹APP_0003_binder_C_App下。然后打开test_server.c,编写如下:

/* Copyright 2008 The Android Open Source Project
 */

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


#define ALOGI(x...) fprintf(stderr, "svcmgr: " x)
#define ALOGE(x...) fprintf(stderr, "svcmgr: " x)

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 header
    bio_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;
}

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);
    bio_put_uint32(&msg, 0);  // strict mode header
    bio_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))
        return -1;

    status = bio_get_uint32(&reply);

    binder_done(bs, &msg, &reply);

    return status;
}

unsigned token;

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;
}

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] = {0};
    size_t len;
    uint32_t handle;
    uint32_t strict_policy;
	int i = 0;

    //ALOGI("target=%p code=%d pid=%d uid=%d\n",
    //      (void*) txn->target.ptr, txn->code, txn->sender_pid, txn->sender_euid);

    if (txn->target.ptr != BINDER_SERVICE_MANAGER)
        return -1;

    if (txn->code == PING_TRANSACTION)
        return 0;

    // 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();
			break;
			
		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;
			
		case SVC_MGR_ADD_SERVICE:
			break;
		case SVC_MGR_LIST_SERVICES: 
			break;
		default:
			ALOGE("unknown code %d\n", txn->code);
			return -1;
    }

    bio_put_uint32(reply, 0);
    return 0;
}



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

    bs = binder_open(128*1024);
    if (!bs) {
        fprintf(stderr, "failed to open binder driver\n");
        return -1;
    }
	/*add server*/
	//向进程svcmgr注册一个"hello"服务
	ret = svcmgr_publish(bs,svcmgr,"hello","123");
	if (!ret) {
		fprintf(stderr, "failed to  publish hello service\n");
		return -1;
    }
	ret = svcmgr_publish(bs,svcmgr,"goodbye","1234");
	if (!ret) {
		fprintf(stderr, "failed to publish goodbye service\n");
		return -1;
    }
	
#if 0
	while(1){
		/*read data*/
		
		/*parse data,and process*/
		
		/*reply*/	
	}
#endif  

	binder_loop(bs, hello_service_handler);
    return 0;
}

简述代码的流程如下

main(int argc, char **argv) //从main函数开始
	bs = binder_open(128*1024); //打开binder驱动
	svcmgr_publish(bs,svcmgr,"hello",NULL)

下面我们再编写 test_client.c

test_client.c

/* Copyright 2008 The Android Open Source Project
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <linux/types.h>
#include <stdbool.h>
#include "binder.h"
#include "test_server.h"
#include <sys/types.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 header
    bio_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;
}

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);
    bio_put_uint32(&msg, 0);  // strict mode header
    bio_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))
        return -1;

    status = bio_get_uint32(&reply);

    binder_done(bs, &msg, &reply);

    return status;
}

struct binder_state *g_bs;
uint32_t g_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(此处必须填写一个0)
	
	/*放入参数*/
	
	/*调用binder_call*/
	if (binder_call(g_bs, &msg, &reply, g_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(此处必须填写一个0)
	
	/*放入参数*/
	bio_put_string16_x(&msg, name);
	
	/*调用binder_call*/
	if (binder_call(g_bs, &msg, &reply, g_handle, HELLO_SVR_CMD_SAYHELLO_TO))
		return 0;
	
	/*从reply中解析出返回值*/
	ret = bio_get_uint32(&reply);
	
	binder_done(g_bs, &msg, &reply);
	
	return ret;
}
/*程序如何运行
* ./test_client hello
* ./test_client hello <name>
*/

unsigned token;

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

    bs = binder_open(128*1024);
    if (!bs) {
        fprintf(stderr, "failed to open binder driver\n");
        return -1;
    }
	g_bs = bs;
	/*get service(服务)*/
	handle =  svcmgr_lookup(bs, svcmgr, "hello");
	if (!handle){
		fprintf(stderr,"failed to get hello service\n");
		return -1;
	}
	g_handle = handle;
	
	
	/*send data to server(服务器)*/
	if(argc == 2){
		sayhello();
	}else if(argc == 3){
		ret = sayhello_to(argv[2]);
		fprintf(stderr,"get ret of sayhello_to %d\n",ret);
	}
	
	binder_release(bs, handle);
	
    return 0;
}

程序编译

编写好程序之后,我们肯定需要去编译,那么我们应该怎么编译呢?我们cd到SDK/frameworks\native\cmds\servicemanager\目录下,然后假装修改一下binder.c,退出保存之后,执行命令如下:
source build/envsetup.sh
lunch rk3399_all-userdebug
mmm ./ showcommands

猜你喜欢

转载自blog.csdn.net/weixin_43013761/article/details/87780363