There's a bunch of online Binder the principles of the article, are very long, I hope I can try to put this question briefly.
1) About Binder Drive
Binderin theAndroidwas designed as a driver, installed in/dev/binder, which isAndroidand theLinuxone of the important differences:Androida new inter-process communication method is proposed (IPC). In addition, this approach is done through a remote procedure call (RPC) is implemented.
theBinderoperation is the same as for other drivers, see this structure:
static const struct File_operations binder_fops= {. Owner = This_module,.poll = Binder_poll,.unlocked_ioctl = Binder_ioctl ,. mmap = Binder_mmap,.open = Binder_open,.flush = Binder_flush,.release = Binder_release,};
all the/dev/binderthisDriverall the things you do will be converted intoBinderfunctions that you define yourself. Like when a process is going to openBinderdevice is always called when
static int open_driver ()
{
...
int FD =open ("/dev/binder", O_RDWR);
...
}
in passingBinderafter the drive explanation, it becomesBinder_open.
Another example:
status_t Ipcthreadstate::talkwithdriver (bool doreceive) {
...
#if defined (have_android_os)
if (IOCTL (Mprocess->mdriverfd,binder_write_read, &BWR) >= 0)
err = No_error;
Else
err =-errno;
#else
...
{
Note that here's theIOCTLprocess switching is required because of the inter-process communication involved.
2) on how to use binder
aserivce:
int main () {proc (processstate::self ()); SM =defaultservicemanager (); Sm->addservice ("Service.name", Newxxxservice ()); Processstate::self ()->startthreadpool (); Ipcthreadstate::self ()->jointhreadpool ();}
Service Managerneed to put thisServiceput it in a global list.srvlist, in this way,Clientcan be based onServicename to find it out. LikeMedia.player
client tries to get Service of the Binder called when the GetService , as follows:
void Addbatterydata (uint32_t params) { sp<ibinder> binder = Defaultservicemanager ()->getservice ( String16 ("Media.player")); Sp<imediaplayerservice>service = interface_cast<imediaplayerservice> (binder); CHECK (Service.get ()! = NULL); Service->addbatterydata (params);}
How to write aClientit?
actuallyBinderMechanisms andSocketin programming.CSArchitecture,Servicehave aLoop, keep looking for notDriverThere are orders to write about their ownBuffer,Clientthen toBinderwrite the data and of course it's going to tellBinderwho needs to collect data.
aService, forServiceManager, too .Client.
Clientand theServiceWhat communication does it pass between them? is essentiallyBpbinderand theBbinder, but the upper layer has a package interface to themBpinterfaceand theBninterface.
Bpinterfacethe creation requires aBpbinder, the function of itRemote() This is the return.Bpbinder.
Bpbinderis aServiceon entityBinderan agent, talking to it is tantamount toServiceon theBinderTalk
bpbinder::bpbinder (int32_thandle) It has a handle Constructed, this handle service index. servicemanager Span style= "Color:rgb (51,51,51)" >0.
3) Binder Why is the Exchange times only once?
This is determined in the kernel, because Binder Use mmap put Binder The device is mapped to both the user space and the kernel space, so in a process there is no need for user space and kernel space Copy , and you only need to copy the data from the user space of the sending process to the kernel space of the receiving process. So you just need to copy it once.
Tbc...
On the basic principle of binder