Converting HAL Modules

You can update pre-existing HAL modules to HIDL HAL modules by converting the header in hardware/libhardware/include/hardware.

Using c2hal

The c2hal tool handles most of the conversion work, reducing the number of required manual changes. For example, to generate a HIDL .hal file for the NFC HAL:

make c2hal
c2hal -r android.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport -p android.hardware.nfc@1.0 hardware/libhardware/include/hardware/nfc.h

These commands add files in hardware/interfaces/nfc/1.0/. Running hardware/interfaces/update-makefiles.sh from the $ANDROID_BUILD_TOP directory will also add the required makefile to the HAL. From here, you can make manual changes to fully convert the HAL.

c2hal activities

When you run c2hal, everything in the header file is transferred to .hal files.

c2hal identifies structs that contain function pointers in the provided header file and converts each struct into a separate interface file. For example, alloc_device_t is converted to the IAllocDevice HAL module (in the file IAllocDevice.hal).

All other data types are copied over into a types.hal file. Pound-defines are moved into enums, and items not a part of HIDL or not convertible (such as static-function declarations) are copied into comments marked with the text "NOTE".

Manual activities

The c2hal tool does not know what to do when it encounters certain constructs. For example, HIDL has no concept of raw pointers; because of this, when c2hal encounters a pointer in header files, it doesn't know whether the pointer should be interpreted as an array or as a reference to another object. Void pointers are also similarly opaque.

Field such as int reserved[7] must be manually removed during the transition to HIDL. Items such as the name of the return value should be updated to something more meaningful; for example, converting the return parameter of methods such as write in NFC from the autogenerated int32_t write_ret to Status status (where Status is a new enum containing possible NFC statuses).

Implementing the HAL

After you have created .hal files to represent your HAL, you must generate the makefiles (Make or Soong) that create the language support in C++ and Java (unless the HAL uses a feature unsupported in Java). The ./hardware/interfaces/update-makefiles.sh script can automatically generate makefiles for HALs located in the hardware/interfaces directory (for HALs in other locations, simply update the script).

When the makefiles are up to date, you are ready to generate header files and implement methods. For details on implementing the generated interface, see HIDL C++ (for C++ implementations) or HIDL Java (for Java implementations).