Android Binder Tutorial [Part One]: Introduction to Binder.

Introduction:


The different components in Android may need to exchange data and it is realized through inter component/ inter process communication .As we learn about components and interactions it becomes increasingly evident that IPC forms a major part of  the Android framework.


Binder is an Android-specific interprocess communication mechanism, and remote method invocation system.

Android process can call a routine in another Android process, using binder to indentify the method to invoke and pass the arguments between processes.


While the underlying platform makes use of Linux concepts, the Android applications are unique. Unlike traditional Java apps, Android apps do not have an entry point (like a main () function).  The framework could invoke a particular entry point within an application to achieve the requested functionality.


Question:Why does [Binder] need to be done in the kernel? Couldn't any of the current Linux IPC mechanisms be re-used to accomplish this?
Answer:
1.     Avoiding copies by having the kernel copy from the writer into a ring buffer in the reader's address space directly (allocating space if necessary).
2.     Managing the lifespan of proxied remoted userspace objects that can be shared and passed between processes (upon which the userspace binder library builds its remote reference counting model)

Binder was originally OpenBinder and used as the IPC in BeOS . The current Binder implementation in Android is a customized implementation of the OpenBinder. This was mainly to ensure the new implementation uses a license that is compatible with the Android’s user space code.

Binder Terminology:

BinderThe Binder refers to the overall Binder architecture, whereas a Binder refers to a particular implementation of a Binder interface.

Binder Object is an instance of a class that implements the Binder interface. A Binder object can implement multiple Binders.

Binder Protocol The Binder middleware uses a very low level protocol to communicate with the driver.

IBinder Interface A Binder interface is a well-defined set of methods, properties and events that a Binder can implement. It is usually described by AIDL1 language. 

Binder Token A numeric value that uniquely identifies a Binder.

Binder 's Working Architecture.


The Binder framework involves four actors.
Server
Client.
ServiceManager.
driver [/dev/binder]


Client, Server, and ServiceManager are implemented in user space, Binder driver implemented in kernel space

The Binder Driver and ServiceManager have been implemented in the Android aosp platform. Developers only need to implement their own Client and Server in user space.

The Binder driver provides the device file /dev/binder to interact with the user space.

The Client, Server, and Service Manager communicate with the Binder driver through open, mmap and ioctl file operation functions.

Inter-process communication between Client and Server is implemented indirectly through the Binder driver.

Service Manager is a daemon process to manage Server and provide Client with the ability to query the Server interface

ServiceManager also called as real name Binder helps in identifying the client through a name which was registered earlier. This works just like each website has its own URL in addition to an IP address. 

Server creates a Binder entity, Assigns a readable and easy to remember name to this Binder for the ServiceManager when contacted.



Simple analysis

Remember binder communication is C/S architecture.

1. Server registers the service to ServiceManager, we can be seen as a server to our ServiceManager.

2. Client wants to use the service to query in the ServiceManager, as the Client is another client of the ServiceManager .

3. Then Client service information is obtained through inquiries with the registered Server to establish communication, direct use of Server service, the Client is Server client.

However, all the above are based on and use Binder communication.

In the subsequent parts of this blog tutorial we will get into more details to understand the detailed flow and various components involved.





Comments

  1. Great article,keep sharing more articles with us.

    thank you....

    android online training

    ReplyDelete
  2. Hi , I have a doubt I feel both ashmem and binder are same then why to go for binder ? Why can't we use ashmem or shared memory ?

    ReplyDelete
    Replies
    1. This is not shared memory, its IOCTL based communication using mmap mechanism.
      Since its CS architecture, so stability is more than ashm

      Delete
  3. Nice article. One can easily understand the concept.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Audio Tutorial [Part Three] : AudioFlinger Introduction and Initialization

Android External Storage Support: Volume Daemon (vold) Architecture

Android Audio Tutorial [Part One] : Introduction