Android Layer Interaction: Application to Kernel

Like any other OS platform, Android also designed in layered approach. Theoretically explaining the software layer interaction is bit difficult. So let me share my understanding, with an simple Vibrator application example. In this, we will see the control flow from application to kernel.

Application User Interface:

Screenshot-2

Application Source Code:

 public class MyActivity extends Activity{
 Vibrator vs;
 Button bn1;
 @Override
 public void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  vs = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
  bn1 = (Button) findViewById(R.id.button1);
  bn1.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0){
    vs.vibrate(1000);
    }
  });
 }
} 

Layer Interaction or Control Flow:
To access the vibrator service, first we are getting the local manager object or handler (vs). In Application Layer, vibrator service is getting called inside onClick() of the Button1. After calling vs.vibrate(), the control goes into vibrator service (Framework Layer), thru the binder interface.

Vibrator Service source code is in the following path:
Android_SRC/frameworks/base/services/java/com/android/server/VibratorService.java

Control reach the below function:
public void vibrate(long milliseconds, IBinder token)

Inside vibrate():

  1. Check for Permission
  2. Check the value (!= 0) and object (currently no vibration activity going on)
  3. Create Vibration object
  4. Start the vibration – startVibrationLocked(vib)

Inside startVibrationLocked():

  1. If mTimeout != 0, then call the native/JNI function vibratorOn().
  2. else, call the code, which handle the rhythmic vibration pattern, (Let us not worry about rhythmic vibration), which intern call the vibratorOn().

Next, to execute the vibratorOn(), control goes into the Native Layer thru JNI.

Native layer code is in the below path:
Android_SRC/frameworks/base/services/jni/com_android_server_VibratorService.cpp

Control reach the below function:
static void vibratorOn(JNIEnv *env, jobject clazz, jlong timeout_ms)

Inside vibratorOn():

  1. Call the vibrator_on(), which is a HAL (Hardware Abstraction Layer) function.

HAL code is in the below path:
Android_SRC/hardware/libhardware_legacy/vibrator/vibrator.c

Control reach the below function:
int vibrator_on()

Inside vibrator_on():

  1. Call sendit().
  2. In sendit, we will open the vibrator device file and write the time-out value into it, which intern pass the value to vibrator device driver (Kernel Layer).
  3. The driver can activate the vibrator thru some hardware interface like I2C, GPIO (Hardware Layer).
Advertisements
Tagged with: ,
Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: