You can update pre-existing HAL modules to HIDL HAL modules by converting the
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
make c2halc2hal -r android.hardware:hardware/interfaces -randroid.hidl:system/libhidl/transport -p email@example.com hardware/libhardware/include/hardware/nfc.h
These commands add files in
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.
When you run
c2hal, everything in the header file is transferred
c2hal identifies structs that contain function pointers in the
provided header file and converts each struct into a separate interface file.
alloc_device_t is converted to the
IAllocDevice HAL module (in the file
All other data types are copied over into a
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 "
c2hal tool does not know what to do when it encounters
certain constructs. For example, HIDL has no concept of raw pointers; because of
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 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
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).