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
That is an example of a structure with 2 properties(DummyPacket).
An interface example of .aidl file
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:
The next commands will help you to build AOSP with the implemented AIDL interface:
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:
You should find generated folders for C++, Java and NDK.
The source code of dummy_device is on Github.