Android Interface Definition Language (AIDL) defines the programming interface that both the client and service agree upon,
in order to communicate with each other using Inter-Process Communication (IPC).
Create AIDL Interface
Compiling the AIDL interface auto-generates 2 classes - a Proxy class and a Stub class:
The Proxy class acts as a client-side representation, managing communication with the remote service and marshalling the request to a kernel driver (Binder)
The Stub class serves as the server-side implementation, un-marshalling incoming requests from the proxy (via the Binder) and executing the actual service operations defined in the AIDL
Create folder structure in vendor/trongvq/interfaces/virtio_rtc as below:
Directoryvendor/trongvq/interfaces/virtio_rtc
Android.bp
Directoryvendor/trongvq/virtio_rtc
IVirtio_RTC.aidl
Create an Android.bp file to define the build modules and build configuration:
Create IVirtio_RTC.aidl file which define Interface’s methods:
Tag AIDL current version:
Run make to build AIDL, but the first time, it will fail. Fix it with provided command:
Execute suggested command:
The command will create a tag current aidl api for the AIDL interface:
Directoryvendor/trongvq/interfaces/virtio_rtc
Android.bp
Directoryvendor/trongvq/virtio_rtc
IVirtio_RTC.aidl
Directoryaidl_api
Directoryvendor.trongvq.virtio_rtc
Directorycurrent
Directoryvendor/trongvq/virtio_rtc
IVirtio_RTC.aidl
The current folder will be renamed to 1 if there is a new api created. That new version will be new current folder then after.
AIDL has three different backends: Java, NDK, CPP:
Java vendor.trongvq.virtio_rtc-V1-java.jar: Stable Java Proxy and Java Stub classes, use find ./out/ -name *Virtio_RTC.java to locate the generated file.
NDK vendor.trongvq.virtio_rtc-V1-ndk.so: Stable Native Binder Proxy (BpVirio_RTC.h) class for client code and Binder Native Stub (BnVitio_RTC.h), linked to libbinder_ndk, use for vendor service and client, use find ./out/ -name *Virtio_RTC.h to locate the generated file.
CPP vendor.trongvq.virtio_rtc-V1-cpp.so: Unstable CPP Proxy (BpVirio_RTC.h) and Stube (BnVitio_RTC.h) classes, linked to libbinder, use find ./out/ -name *Virtio_RTC.h to locate the generated file.
Add AIDL to vendor packages:
Create a vendor make file:
Add vendor make file into Cuttlefish device make file:
Rebuild Android to include built AIDL library to target device
Create a vendor service in vendor/trongvq/services/virtio_rtc:
Directoryvendor/trongvq/
device_framework_matrix.xml # compatibility
Directoryinterfaces/virio_rtc
Android.bp
Directoryvendor/trongvq/virtio_rtc
IVirtio_RTC.aidl
Directoryservices/virtio_rtc
Android.bp
inc
Directorysrc
Virtio_RTC_Service.cpp
vendor.trongvq.virtio_rtc-service.rc # start service at startup
vendor.trongvq.virtio_rtc-service.xml # declare and configure interface
Create Android.bp to declare a Service binary:
Create Service RC file:
Create Service XML file:
Add Service to target device:
However, when rebuild system, it fails:
To fix this, move to the next step.
Add Service to Device Framework Compatibility Matrix (FCM):
The framework compatibility matrix consists of the system compatibility matrix, the product compatibility matrix, and the system_ext compatibility matrix.
The requirements of the FCM must be satisfied by the device manifest (requirements enforced at build time, runtime, and in VTS):
correct name
compatible versions
compatible interface instances
Add to vendor packages:
Rebuild again
The service binary is built and copied into output folder at vendor/bin/hw (noted that Android.bp uses relative path hw):
vendor.trongvq.virtio_rtc-service.rc# start service at startup
vendor.trongvq.virtio_rtc-service.xml# declare and configure interface
Launch Cuttlefish device and check in logcat or dmesg to see that the service could not start:
File vendor.trongvq.virtio_rtc-service is labeled as u:object_r:vendor_file:s0
which is incorrect. On target shell, use ls -lZ to see the label.
That default label is defined in file system/sepolicy/private/file_contexts:
Add SE Policy for vendor service:
The vendor service should have the label hal_virtio_rtc_default_exec defined in Device SE Policy:
When rebuild, it fails due to type hal_virtio_rtc_default_exec is not defined.
Create vendor SE Policy file:
Add a new file hal_virtio_rtc_default.te:
domain means it can be assigned to processes and that it is allowed to execute code.
exec_type means it can be executed, applied for vendor_file_type and general file_type.
init_daemon_domain is an SELinux macro that’s defined in the Android public policies,
details in system/sepolicy/public/te_macros. It’s used to transition the domain context from init to hal_virtio_rtc_default.
Rebuild the target and it should be compiled.
Go to the device shell, verify the label of the service binary:
vendor.trongvq.virtio_rtc-service.rc# start service at startup
vendor.trongvq.virtio_rtc-service.xml# declare and configure interface
The AIDL interface is generated as NDK stable source code:
The AIDL interace is defined in class IVirtio_RTC (inheriates ::ndk::ICInterface).
The Binder Proxy is defined in class BpVirtio_RTC (inheriates ::ndk::BpCInterface<IVirtio_RTC>),
and implemented in IVirtio_RTC.cpp. The methods of interface are fully implemented for Binder Proxy.
Any process having a Binder Proxy object can call its methods to communicate with the Binder Native Stub.
The Binder Native is defined in class BnVirtio_RTC (inheriates ::ndk::BnCInterface<IVirtio_RTC>),
but partially impplemented in IVirtio_RTC.cpp. It is required to implement virtual methods
declared in the AIDL interfaces. These methods are called through the Binder IPC,
and they are user-defined functionanities.
Create Service Header in Virtio_RTC.h:
Implement Service Methods in Virtio_RTC.cpp:
Update SE Policy:
Rebuild target and check service in the logcat or dmesg, expect it fails with avc: denied error:
When service calls AServiceManager_addService(), the process calls the Service Manager via the Binder to register its service. This step needs a permission!
Use tool audit2allow to convert SELinux audit messages into SELinux allow rules:
Now pull the policy file, and read logcat to check audit message:
Add suggested statement to vendor policy:
Rebuild again, and check service in the logcat or dmesg, expect it fails with another avc: denied error:
Run the tool and add suguested statement to vendor policy:
Rebuild again, and check service in the logcat or dmesg, expect it fails with another avc: denied error:
Run the tool and add suguested statement to vendor policy:
Building again and expect the following SELinux neverallow build error due to service_manager add permission:
Open system/sepolicy/public/domain.te, it has a specific neverallow rule
that does not allow service_manager to add a default_android_service:
To fix this, need to define a specific service type:
then assign it a new label:
then change default_android_service to hal_virtio_rtc_service in service_manager add:
The Android Vendor Test Suite (VTS) provides extensive testing on the following:
Kernel
Hardware abstraction layer (HAL)
VTS runs on a desktop machine and executes test cases directly on attached devices or on the emulators.
The test cases can be GTest-style tests, kernel tests, or JUnit-style tests written in Java.
Note:
VTS Trade Federation test framework will not execute the tests immmediately, it will take upto a minute to probe, set up the Device.
Test is scheduled, and re-run after a device is found.
Customize the test configuration:
Run make command to build the config, then search for a generated config file.