How to implement an AIDL interface within AOSP

How to implement the AIDL interface

The second part discusses implementing an AIDL interface that will be used by HAL service and the application layer we will work on through the next posts. AIDL is an IPC mechanism providing inter-process communication. A .aidl file is more like a schema in that you will define what you need and what you’re going to implement. It can consist of the data structure or even a class interface that is the base of the communication between any processes such as a Hal service (daemon) and an application layer. Of course, it can be used for communication between two application components.

How it works

.

How an AIDL interface will be implemented

An AIDL interface implementation is based on .aidl files that as mentioned before can include a data structure or an interface class. The Android build system will generate sources and libraries for target languages specified in Android.bp file. It supports directly C++, Java and Rust however the libraries can be used to develop an AIDL backend process with NDK, C++, Java or even an Android application layer with Java and Kotlin. 3

Adata structure example of .aidl file

package aospinsight.hardware.dummy_device;

@VintfStability
parcelable DummyPacket{
    int id;
    String msg;
}

That is an example of a structure with 2 properties(DummyPacket).

An interface example of .aidl file

package aospinsight.hardware.dummy_device;

@VintfStability
interface IDummy{
    void getPacketByCallback(in aospinsight.hardware.dummy_device.IDummyCallback callback);

    void getPacket(out aospinsight.hardware.dummy_device.DummyPacket packet);
}

This interface consists of two abstract methods. As you can see the first one has an input argument of another interface type (IDummyCallback). Later you will see how will send a callback handle through this argument that makes it possible to be called from the HAL service.

The Hash file

Each AIDL version goes with a hash file that will be checked at the build time to be sure that the AIDL hasn’t been changed accidentally. It’s a kind of validation process based on the Hash file. To generate a Hash file for your AIDL interface you should be inside the version you want and then the command below on your terminal.

(find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo latest-version ) | sha1sum | cut -d " " -f 1

How to build

The first step is cloning the code from the GitHub repo to your local machine. It should be cloned inside your AOSP directory. Then you can find AIDL-HAL-Service folder that should be renamed to vendor.

git clone https://github.com/Heydarchi/AIDL-HAL-Service.git

If you haven’t set up AOSP on your local machine you can read this post:

How to build AOSP

The next commands will help you to build AOSP with the implemented AIDL interface:

source build/enviroment
lunch aospinsight_car-userdebug
m -j4

I added a new device/devboard variant that can be found inside the vendor folder.

To check if the AIDL interface is generated you can check the path below inside your AOSP folder:

out/soong/.intermediates/vendor/aospinsight/hardware/interfaces/dummy_device/aidl

You should find generated folders for C++, Java and NDK.

The source code of dummy_device is on Github.

3 thoughts on “How to implement the AIDL interface”

    1. Mohammad Hossein Heydarchi

      If the repo has been cloned successfully it should be:
      AIDL-HAL-Service/aospinsight/device/devboard

Leave a Comment

Your email address will not be published. Required fields are marked *