Android HIDL and Project Treble

One of the problems in Android is the fact that when a new version is released only a few devices can be updated. Actually, if you want to enjoy the latest version, you need to buy a new smartphone every 6 months. Google solution is the Project trebel that separates the vendor implementation from the Android OS framework via a new vendor interface

HIDL

HAL interface definition language used to describe the interface between the framework and the vendor. All hardware interfaces are located in hardware/interfaces in .hal files

Let’s go over simple example

Create all the path

 

Create a hal file in ISimphw.hal inĀ  hardware/interfaces/simple/2.0

Generate the HAL files

To generate the HAL files you need to use the hidl-gen tool run:

And to update all makefile (Android.mk, Android.bp) run:

Now add 2 empty files to hardware/interfaces/simple/2.0/default:

  • android.hardware.simple@2.0-service.rc
  • service.cpp

Now the directory hardware/interfaces/simple should look like this:

Implementing the HAL shared object:

we need to add a new static function to return the service object (usually as a singleton)

Simphw.h

Simphw.cpp

Note that if you want to support pass-through mode, you need to uncomment the HIDL_FETCH_ISimphw function

In this example, we implemented the function as simple as possible (usually we will load the hardware module here)

The generated Android.bp file build a shared library with the implementation – android.hardware.simple@2.0-impl

Creating the Service

To host the library we need to create a simple executable:

service.cpp

We create an instance of our implementation, Create a thread pool for the binder and register the current process as a service

Note that the function regsiterAsService() is auto-generated by hidl-gen tool

To make this service run automatically add init file:

android.hardware.simple@2.0-service.rc

For testing purpose I set the security label to su , we need to set SE Linux rules (I wrote it in the init.te file):

To tell the build system to build the service add the following to Android.bp (in directory default)

Add the following components to build/make/target/product/emulator.mk

 

Now build the ROM and run it, connect with adb and run ps -A to see all the hardware services:

The lshal tool list all the hardware services by categories:

Writing the Client

To use the service we will write a simple client application. Usually, it will be part of the Android framework written by Google. For example, if the generic framework wants to access the vibrator service:

In fileĀ  frameworks/base/services/core/jni/com_android_server_VibratorService.cpp

Add a new directory in device/generic/goldfish – simphaltest

Add the source and Android.bp files:

Android.bp

Add the client test to build/make/target/product/emulator.mk

Add hal entry to the Manifest.xml file of your device (device/generic/goldfish/Manifest.xml in this case)

Build the ROM again and test our service:

 

Tagged , ,

Leave a Reply

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