android深入浅出之binder机制,Android深入浅出之Binder机制

BpBinder的位置在framework\base\libs\binder\BpBinder.cpp

status_t BpBinder::transact(

uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)

{

//又绕回去了,调用IPCThreadState的transact。

//注意啊,这里的mHandle为0,code是ADD_SERVICE_TRANSACTION,data是命令包

//reply是回复包,flags=0

status_t status = IPCThreadState::self()->transact(

mHandle, code, data, reply, flags);

if (status == DEAD_OBJECT) mAlive = 0;

return status;

}

...

}

再看看IPCThreadState的transact函数把

status_t IPCThreadState::transact(int32_t handle,

uint32_t code, const Parcel& data,

Parcel* reply, uint32_t flags)

{

status_t err = data.errorCheck();

flags |= TF_ACCEPT_FDS;

if (err == NO_ERROR) {

//调用writeTransactionData发送数据

err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, NULL);

}

if ((flags & TF_ONE_WAY) == 0) {

if (reply) {

err = waitForResponse(reply);

} else {

Parcel fakeReply;

err = waitForResponse(&fakeReply);

}

....等回复

err = waitForResponse(NULL, NULL);

....

return err;

}

再进一步,瞧瞧这个...

status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags,

int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer)

{

binder_transaction_data tr;

tr.target.handle = handle;

tr.code = code;

tr.flags = binderFlags;

const status_t err = data.errorCheck();

if (err == NO_ERROR) {

tr.data_size = data.ipcDataSize();

tr.data.ptr.buffer = data.ipcData();

tr.offsets_size = data.ipcObjectsCount()*sizeof(size_t);

tr.data.ptr.offsets = data.ipcObjects();

}

....

上面把命令数据封装成binder_transaction_data,然后

写到mOut中,mOut是命令的缓冲区,也是一个Parcel

mOut.writeInt32(cmd);

mOut.write(&tr, sizeof(tr));

//仅仅写到了Parcel中,Parcel好像没和/dev/binder设备有什么关联啊?

恩,那只能在另外一个地方写到binder设备中去了。难道是在?

return NO_ERROR;

}

//说对了,就是在waitForResponse中

status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult)

{

int32_t cmd;

int32_t err;

while (1) {

//talkWithDriver,哈哈,应该是这里了

if ((err=talkWithDriver()) < NO_ERROR) break;

err = mIn.errorCheck();

if (err < NO_ERROR) break;

if (mIn.dataAvail() == 0) continue;

//看见没?这里开始操作mIn了,看来talkWithDriver中

//把mOut发出去,然后从driver中读到数据放到mIn中了。

cmd = mIn.readInt32();

switch (cmd) {

case BR_TRANSACTION_COMPLETE:

if (!reply && !acquireResult) goto finish;

break;

.....

return err;

}

status_t IPCThreadState::talkWithDriver(bool doReceive)

{

binder_write_read bwr;

//中间东西太复杂了,不就是把mOut数据和mIn接收数据的处理后赋值给bwr吗?

status_t err;

do {

//用ioctl来读写

if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0)

err = NO_ERROR;

else

err = -errno;

} while (err == -EINTR);

//到这里,回复数据就在bwr中了,bmr接收回复数据的buffer就是mIn提供的

if (bwr.read_consumed > 0) {

mIn.setDataSize(bwr.read_consumed);

mIn.setDataPosition(0);

}

return NO_ERROR;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值