Tuesday, 6 June 2023

AIDL for HIDL : The new paradigm shift in Android HAL

Hi Guys!!!

Hope you are doing well !!!. Today I am going to discuss a fundamental change in the Android HIDL.

From Android 11 onward we can use AIDLs for HALs .

Official Announcement and  guide is at 

That means no need to use HIDLs , Just have AIDLs for every thing .   Only one recommendation from google that "HALs using AIDL to communicate between framework components must use Stable AIDL"

When I had written my first blogs on AIDL 6 year ago, I  had  no idea that google will use it for everything in future . My blog link : AIDL and its uses in Android

Why this change ?

  • AIDLs are getting used for IPC in Android from beginning But HIDL only introduced from Android 8 and going to be depricated in Android 11 onward. 
  • AIDLs are used in many places, such as between Android framework components or in apps. 
  • Now that AIDL has stability support, it’s possible to implement an entire stack with a single IPC runtime. AIDL also has a better versioning system than HIDL.
Note :-

HALs using AIDL to communicate between framework components, such as those in system.img, and hardware components, such as those in vendor.img, must use Stable AIDL. 
However, to communicate within a partition, for instance from one HAL to another, there's no restriction on the IPC mechanism to use.

In this First post I am going to put details explanation for AIDLs uses as HIDLs.
In second Post I will give practical example of AIDLs as HIDLs.

For an AIDL interface to be used between system and vendor, the interface needs two changes:
  • Every type definition must be annotated with @VintfStability
  • The aidl_interface declaration needs to include stability: "vintf"
Note :- AOSP Stable AIDL interfaces for HALs are in the same base directories as HIDL interfaces, in aidl folders.
hardware/interfaces
frameworks/hardware/interfaces
system/hardware/interfaces

For an example
hardware/interfaces/automotive/vehicle/aidl/Android.bp
aidl_interface {
name: "android.hardware.automotive.vehicle",
vendor_available: true,
srcs: [
"android/hardware/automotive/vehicle/**/*.aidl",
],
frozen: true,
stability: "vintf",
backend: {
cpp: {
enabled: false,
},
java: {
sdk_version: "module_current",
min_sdk_version: "31",
apex_available: [
"//apex_available:platform",
"com.android.car.framework",
],
},
},
versions_with_info: [
{
version: "1",
imports: [],
},
],
}
AIDL runtime library
AIDL has three different backends: 
  • Java, 
  • NDK, 
  • CPP. 
To use Stable AIDL, you must always use the system copy of libbinder at system/lib*/libbinder.so and talk on /dev/binder.
For code on the vendor image, this means that libbinder (from the VNDK) cannot be used. 

Instead, native vendor code must use the NDK backend of AIDL, link against libbinder_ndk (which is backed by system libbinder.so), and link against the -ndk_platform libraries created by aidl_interface entries.

Convert HIDL to AIDL
Build the tool hidl2aidl if it is not compiled:
m hidl2aidl
Create a new folder aidl in the HAL interface:
mkdir -p hardware/interfaces/skstest/aidl

Generate AIDL from a specific HIDL version:

hidl2aidl -o hardware/interfaces/skstest/aidl \
          -r android.hardware:hardware/interfaces \
          android.hardware.skstest@1.0

This will create aidl/android/hardware/skstest/ISkstest.aidl file.

AIDL

package android.hardware.skstest;

@VintfStability
interface ISkstest {
    String getChars();
    void putChars(in String msg);
}

HIDL

package android.hardware.skstest@1.0;

interface ISkstest {
    putChars(string msg);
    getChars() generates (string msg);
};

The tool also generates a makefile for the AIDL, but it is not usable at the moment:

AIDL

aidl_interface {
    name: "android.hardware.skstest",
    vendor: true,
    srcs: ["android/hardware/skstest*.aidl"],
    stability: "vintf",
    owner: "vqtrong",
    backend: {
        cpp: {
            enabled: false,
        },
        java: {
            sdk_version: "module_current",
        },
    },
}

HIDL

// This file is autogenerated by hidl-gen -Landroidbp.

hidl_interface {
    name: "android.hardware.skstest@1.0",
    root: "android.hardware",
    vndk: {
        enabled: true,
    },
    srcs: [
        "ISkstest.hal",
    ],
    interfaces: [
        "android.hidl.base@1.0",
    ],
    gen_java: false,
}
Soon I am going to share second Post : practical example of AIDLs as HIDLs.

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...