Android系统--Binder系统具体框架分析(一)补充
生活随笔
收集整理的這篇文章主要介紹了
Android系统--Binder系统具体框架分析(一)补充
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Android系統(tǒng)--Binder系統(tǒng)具體框架分析(一)補(bǔ)充
補(bǔ)充:對(duì)Binder驅(qū)動(dòng)分析一的代碼補(bǔ)充,添加saygoobye和saygoodbye_to服務(wù)
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_Htest_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結(jié)構(gòu)體空間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); //構(gòu)造結(jié)構(gòu)體if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE)) //添加注冊(cè)Binder服務(wù)return -1;status = bio_get_uint32(&reply); //獲得返回值binder_done(bs, &msg, &reply); //結(jié)束標(biāo)志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){/* 根據(jù)txn->code知道要調(diào)用哪一個(gè)函數(shù)* 如果需要參數(shù), 可以從msg取出* 如果要返回結(jié)果, 可以把結(jié)果放入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);//獲得處理函數(shù)標(biāo)志,并調(diào)用處理函數(shù)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);/* 把結(jié)果放入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){/* 根據(jù)txn->code知道要調(diào)用哪一個(gè)函數(shù)* 如果需要參數(shù), 可以從msg取出* 如果要返回結(jié)果, 可以把結(jié)果放入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);/* 把結(jié)果放入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); //構(gòu)造處理方法handler = (int (*)(struct binder_state *bs,struct binder_transaction_data *txn,struct binder_io *msg,struct binder_io *reply))txn->target.ptr; //根據(jù)txn->target.ptr返回處理相應(yīng)方法return handler(bs, txn, msg, reply); //返回所調(diào)用的處理方法}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驅(qū)動(dòng)設(shè)備if (!bs) {fprintf(stderr, "failed to open binder driver\n");return -1;}/* add service */ret = svcmgr_publish(bs, svcmgr, "hello", hello_service_handler); //注冊(cè)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); //從隊(duì)列中取出響應(yīng)的處理函數(shù)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;/* 構(gòu)造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode header/* 放入?yún)?shù) *//* 調(diào)用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;/* 構(gòu)造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode header/* 放入?yún)?shù) */bio_put_string16_x(&msg, name);/* 調(diào)用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;/* 構(gòu)造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode header/* 放入?yún)?shù) *//* 調(diào)用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;/* 構(gòu)造binder_io */bio_init(&msg, iodata, sizeof(iodata), 4);bio_put_uint32(&msg, 0); // strict mode header/* 放入?yún)?shù) */bio_put_string16_x(&msg, name);/* 調(diào)用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驅(qū)動(dòng)設(shè)備if (!bs) {fprintf(stderr, "failed to open binder driver\n");return -1;}g_bs = bs;/* get service */handle = svcmgr_lookup(bs, svcmgr, "goodbye"); //查找獲取服務(wù)的引用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;}轉(zhuǎn)載于:https://www.cnblogs.com/lkq1220/p/6480375.html
總結(jié)
以上是生活随笔為你收集整理的Android系统--Binder系统具体框架分析(一)补充的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 孕妇梦到吃李子预示着什么
- 下一篇: Android——线程通讯 Handle