Friday, 7 July 2023

Implement the User App to call Kernel API

 Hi Guys!!!

Hope you are doing well !!!. Today I am going to put User App example calling reverse String method from Kernel using AIDLs. 
Here AIDL behave as HIDL. 

My previous post on AIDLs as HIDLs
  • Use getSystemService(Context.INVSTR_SERVICE) to obtain the instance of InvstrManager
  • Call to hardware through the InvstrManager APIs
packages/apps/Invstr/src/com/invstr/Invstr.java
package com.invstr;

import android.content.Context;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;
import android.os.ServiceManager;
import android.os.IBinder;
import android.hardware.invstr.IInvstr;

public class Invstr extends Activity {
private static final String TAG = "Invstr";
private static final String IINVstr_AIDL_INTERFACE = "android.hardware.invstr.IInvstr/default";
private static IInvstr invstrAJ; // AIDL Java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText editText = (EditText)findViewById(R.id.editText);
String txt = editText.getText().toString();
Log.d(TAG, "App: request= " + txt);

if(invstrAJ != null) {
try {
invstrAJ.putChars(txt);
} catch (android.os.RemoteException e) {
Log.e(TAG, "IInvstr-AIDL error", e);
}
}

String ret = "";

if(invstrAJ != null) {
try {
ret = invstrAJ.getChars();
} catch (android.os.RemoteException e) {
Log.e(TAG, "IInvstr-AIDL error", e);
}
}

Log.d(TAG, "App: get= " + ret);

TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(ret);
}
});

IBinder binder = ServiceManager.getService(IINVstr_AIDL_INTERFACE);
if (binder == null) {
Log.e(TAG, "Getting " + IINVstr_AIDL_INTERFACE + " service daemon binder failed!");
} else {
invstrAJ = IInvstr.Stub.asInterface(binder);
if (invstrAJ == null) {
Log.e(TAG, "Getting IInvstr AIDL daemon interface failed!");
} else {
Log.d(TAG, "IInvstr AIDL daemon interface is binded!");
}
}
}
}

Add User App to the Launcherpackages/apps/Invcase/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.invstr" >
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Invstr"
android:exported="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
on Android 12, must use android:exported="true"
Build User App

packages/apps/Invcase/Android.bpandroid_app { name: "Invstr", platform_apis: true, srcs: [ "src/**/*.java" ], static_libs: [ "android.hardware.invstr-V1-java" ] }
Add User App to system packages:

build/target/product/base_vendor.mk+ PRODUCT_PACKAGES += \ Invstr
Permission

The device /dev/invstr is created at boot with root permission.

The HAL Library is loaded when JNI Wrapper for Invcase Service is run, therefore, HAL code will run with system permission which attaches to the system_server process.

The Android Init Language uses init*.rc files to automatically do some actions when a condition is met. For the target hardware, Android Init will use init.<hardware>.rc file. On Emulator, it is init.ranchu.rc.

Add below lines to change the owner and permission on the /dev/invcase at boot:
device/generic/goldfish/init.ranchu.rc
on boot
   chown system system /dev/invcase
   chmod 0600 /dev/invstr
Build and Run
The Invstr Manager exports new Service APIs, therefore, need to rebuild the list of system APIs.
m all -j$(nproc)
Run the Emulator and run Invstr app:
Thanks
Saurabh
Happy Coding!!!!

No comments:

Post a Comment

Build a Custom Kernel Module for Android

Hi Guys!!!Hope you are doing well !!!. Today I will describe how you can write a custom kernel module(Hello world) for Android and load it a...