Android深入浅出之Binder机制
2012-06-30 10:47:32 来源:WEB开发网核心提示:这个嘛,到底是继续addService操作的另一端BnServiceManager还是先说BnMediaPlayerService呢?还是先说BnServiceManager吧,Android深入浅出之Binder机制(10),顺便把系统的Binder架构说说,2.8 BnServiceManager上面说了,解析命令
这个嘛,到底是继续addService操作的另一端BnServiceManager还是先说
BnMediaPlayerService呢?
还是先说BnServiceManager吧。顺便把系统的Binder架构说说。
2.8 BnServiceManager
上面说了,defaultServiceManager返回的是一个BpServiceManager,通过它可以把命令请求发送到binder设备,而且handle的值为0。那么,系统的另外一端肯定有个接收命令的,那又是谁呢?
很可惜啊,BnServiceManager不存在,但确实有一个程序完成了BnServiceManager的工作,那就是service.exe(如果在windows上一定有exe后缀,叫service的名字太多了,这里加exe就表明它是一个程序)
位置在framework/base/cmds/servicemanger.c中。
int main(int argc,char **argv)
{
struct binder_state *bs;
void *svcmgr = BINDER_SERVICE_MANAGER;
bs = binder_open(128*1024);//应该是打开binder设备吧?
binder_become_context_manager(bs) //成为manager
svcmgr_handle = svcmgr;
binder_loop(bs, svcmgr_handler);//处理BpServiceManager发过来的命令
}
看看binder_open是不是和我们猜得一样?
struct binder_state*binder_open(unsigned mapsize)
{
struct binder_state *bs;
bs = malloc(sizeof(*bs));
....
bs->fd = open("/dev/binder",O_RDWR);//果然如此
....
bs->mapsize = mapsize;
bs->mapped = mmap(NULL, mapsize,PROT_READ, MAP_PRIVATE, bs->fd, 0);
}
再看看binder_become_context_manager
intbinder_become_context_manager(struct binder_state *bs)
{
return ioctl(bs->fd,BINDER_SET_CONTEXT_MGR, 0);//把自己设为MANAGER
}
binder_loop 肯定是从binder设备中读请求,写回复的这么一个循环吧?
voidbinder_loop(struct binder_state *bs, binder_handler func)
{
int res;
struct binder_write_read bwr;
readbuf[0] = BC_ENTER_LOOPER;
binder_write(bs, readbuf,sizeof(unsigned));
for (;;) {//果然是循环
bwr.read_size = sizeof(readbuf);
bwr.read_consumed = 0;
bwr.read_buffer = (unsigned) readbuf;
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
//哈哈,收到请求了,解析命令
res = binder_parse(bs, 0, readbuf,bwr.read_consumed, func);
}
这个...后面还要说吗??
恩,最后有一个类似handleMessage的地方处理各种各样的命令。这个就是
svcmgr_handler,就在ServiceManager.c中
intsvcmgr_handler(struct binder_state *bs,
struct binder_txn *txn,
struct binder_io *msg,
struct binder_io *reply)
{
struct svcinfo *si;
uint16_t *s;
unsigned len;
void *ptr;
s = bio_get_string16(msg, &len);
switch(txn->code) {
case SVC_MGR_ADD_SERVICE:
s = bio_get_string16(msg, &len);
ptr = bio_get_ref(msg);
if (do_add_service(bs, s, len, ptr,txn->sender_euid))
return -1;
break;
...
其中,do_add_service真正添加BnMediaService信息
intdo_add_service(struct binder_state *bs,
uint16_t *s, unsigned len,
void *ptr, unsigned uid)
{
struct svcinfo *si;
si = find_svc(s, len);s是一个list
si = malloc(sizeof(*si) + (len + 1) *sizeof(uint16_t));
si->ptr = ptr;
si->len = len;
memcpy(si->name, s, (len + 1) *sizeof(uint16_t));
BnMediaPlayerService呢?
还是先说BnServiceManager吧。顺便把系统的Binder架构说说。
2.8 BnServiceManager
上面说了,defaultServiceManager返回的是一个BpServiceManager,通过它可以把命令请求发送到binder设备,而且handle的值为0。那么,系统的另外一端肯定有个接收命令的,那又是谁呢?
很可惜啊,BnServiceManager不存在,但确实有一个程序完成了BnServiceManager的工作,那就是service.exe(如果在windows上一定有exe后缀,叫service的名字太多了,这里加exe就表明它是一个程序)
位置在framework/base/cmds/servicemanger.c中。
int main(int argc,char **argv)
{
struct binder_state *bs;
void *svcmgr = BINDER_SERVICE_MANAGER;
bs = binder_open(128*1024);//应该是打开binder设备吧?
binder_become_context_manager(bs) //成为manager
svcmgr_handle = svcmgr;
binder_loop(bs, svcmgr_handler);//处理BpServiceManager发过来的命令
}
看看binder_open是不是和我们猜得一样?
struct binder_state*binder_open(unsigned mapsize)
{
struct binder_state *bs;
bs = malloc(sizeof(*bs));
....
bs->fd = open("/dev/binder",O_RDWR);//果然如此
....
bs->mapsize = mapsize;
bs->mapped = mmap(NULL, mapsize,PROT_READ, MAP_PRIVATE, bs->fd, 0);
}
再看看binder_become_context_manager
intbinder_become_context_manager(struct binder_state *bs)
{
return ioctl(bs->fd,BINDER_SET_CONTEXT_MGR, 0);//把自己设为MANAGER
}
binder_loop 肯定是从binder设备中读请求,写回复的这么一个循环吧?
voidbinder_loop(struct binder_state *bs, binder_handler func)
{
int res;
struct binder_write_read bwr;
readbuf[0] = BC_ENTER_LOOPER;
binder_write(bs, readbuf,sizeof(unsigned));
for (;;) {//果然是循环
bwr.read_size = sizeof(readbuf);
bwr.read_consumed = 0;
bwr.read_buffer = (unsigned) readbuf;
res = ioctl(bs->fd, BINDER_WRITE_READ, &bwr);
//哈哈,收到请求了,解析命令
res = binder_parse(bs, 0, readbuf,bwr.read_consumed, func);
}
这个...后面还要说吗??
恩,最后有一个类似handleMessage的地方处理各种各样的命令。这个就是
svcmgr_handler,就在ServiceManager.c中
intsvcmgr_handler(struct binder_state *bs,
struct binder_txn *txn,
struct binder_io *msg,
struct binder_io *reply)
{
struct svcinfo *si;
uint16_t *s;
unsigned len;
void *ptr;
s = bio_get_string16(msg, &len);
switch(txn->code) {
case SVC_MGR_ADD_SERVICE:
s = bio_get_string16(msg, &len);
ptr = bio_get_ref(msg);
if (do_add_service(bs, s, len, ptr,txn->sender_euid))
return -1;
break;
...
其中,do_add_service真正添加BnMediaService信息
intdo_add_service(struct binder_state *bs,
uint16_t *s, unsigned len,
void *ptr, unsigned uid)
{
struct svcinfo *si;
si = find_svc(s, len);s是一个list
si = malloc(sizeof(*si) + (len + 1) *sizeof(uint16_t));
si->ptr = ptr;
si->len = len;
memcpy(si->name, s, (len + 1) *sizeof(uint16_t));
更多精彩
赞助商链接