The client obtains the remote ibinder interface of the service.
Take cameraservice as an example (camera. cpp ):
Const sp <icameraservice> & camera: getcameraservice ()
{
Mutex: autolock _ L (mlock );
If (mcameraservice. Get () = 0 ){
Sp <iservicemanager> Sm = defaultservicemanager ();
Sp <ibinder> binder;
Do {
Binder = Sm-> getservice (string16 ("media. Camera "));
If (binder! = 0)
Break;
Logw ("cameraservice not published, waiting ...");
Usleep (500000); // 0.5 s
} While (true );
If (mdeathnotifier = NULL ){
Mdeathnotifier = new deathnotifier ();
}
Binder-> linktodeath (mdeathnotifier );
Mcameraservice = interface_cast <icameraservice> (binder );
}
Loge_if (mcameraservice = 0, "No cameraservice !? ");
Return mcameraservice;
}
The preceding analysis shows that SM is a bpcameraservice object:
Virtual sp <ibinder> getservice (const string16 & name) const
{
Unsigned N;
For (n = 0; n <5; n ++ ){
Sp <ibinder> SVC = checkservice (name );
If (SVC! = NULL) return SVC;
Logi ("waiting for sevice % s... \ n", string8 (name). String ());
Sleep (1 );
}
Return NULL;
}
Virtual sp <ibinder> checkservice (const string16 & name) const
{
Parcel data, reply;
Data. writeinterfacetoken (iservicemanager: getinterfacedescriptor ());
Data. writestring16 (name );
Remote ()-> transact (check_service_transaction, Data, & reply );
Return reply. readstrongbinder ();
}
Here, remote is the previously obtained bpbinder object. Therefore, checkservice will call the transact function in bpbinder:
Status_t bpbinder: transact (
Uint32_t code, const parcel & Data, parcel * reply, uint32_t flags)
{
// Once a binder has died, it will never come back to life.
If (malive ){
Status_t status = ipcthreadstate: Self ()-> transact (
Mhandle, code, Data, reply, flags );
If (status = dead_object) malive = 0;
Return status;
}
Return dead_object;
}
If mhandle is 0, bpbinder continues to call ipcthreadstate: The transact function sends data to the Service Manager process associated with mhandle.
Status_t ipcthreadstate: transact (int32_t handle,
Uint32_t code, const parcel & data,
Parcel * reply, uint32_t flags)
{
........................................ ....................
If (ERR = no_error ){
Log_oneway (">>>> send from PID % d uid % d % s", getpid (), getuid (),
(Flags & tf_one_way) = 0? "Read reply": "One Way ");
Err = writetransactiondata (bc_transaction, flags, handle, code, Data, null );
}
If (Err! = No_error ){
If (reply) reply-> seterror (ERR );
Return (mlasterror = ERR );
}
If (flags & tf_one_way) = 0 ){
If (reply ){
Err = waitforresponse (reply );
} Else {
Parcel fakereply;
Err = waitforresponse (& fakereply );
}
..............................
Return err;
}
Construct the data to be sent through writetransactiondata
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.tar get. Handle = handle; // This handle will be passed to service_manager
Tr. Code = code;
Tr. Flags = bindrflags;
..............
}
Waitforresponse calls talkwithdriver and performs read and write operations on the binder kernel. After the binder kernel receives the data, the threadpool of the service_mananger thread starts. After service_manager finds the cameraservice service, it calls binder_send_reply to write the returned data to the binder kernel and binder kernel.
Status_t ipcthreadstate: waitforresponse (parcel * reply, status_t * acquireresult)
{
Int32_t cmd;
Int32_t err;
While (1 ){
If (ERR = talkwithdriver () <no_error) break;
........................................ ......
}
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
Err = invalid_operation;
# Endif
........................................ ...........
}
Use binder_write_read in the above IOCTL system function to read and write the binder kernel.