Implementing Java bindings for Discriminated Unions in HIDL

This change augments hidl-gen to generate Java code for HIDL safe_union
constructs. The HIDL declaration is similar to regular structs/unions:

safe_union SafeUnion {
    uint8_t a;
    string b;
}

The emitted Java code has the form:

public final static class SafeUnion {
    public static final class hidl_discriminator {
        public static final byte a = 0;
        public static final byte b = 1;
        public static final byte hidl_no_init = 2;
    }

    private byte hidl_d = hidl_discriminator.hidl_no_init;
    private Object hidl_o;

    public void a(byte a) {
        hidl_d = hidl_discriminator.a;
        hidl_o = a;
    }

    public byte a() {
        if (hidl_d != hidl_discriminator.a) {
            throw new IllegalStateException();
	}

        return ((byte) hidl_o);
    }

    ...
}

Also synthesizes the appropriate methods for serializing/deserializing
to/from Parcels.

Bug: 79878527
Test: Ran the existing hidl_test_java suite. Implemented additional
test-cases to exercise the tests.safeunion@1.0 HAL (included in a
separate CL).

Change-Id: I744e4bd8f28b93357d0e65df6eb9350c42273a4a
14 files changed
tree: 01e3ae6054bd856ca3d79890d1f9629c45a50dea
  1. build/
  2. c2hal/
  3. docs/
  4. include_hash/
  5. test/
  6. utils/
  7. .clang-format
  8. Android.bp
  9. Annotation.cpp
  10. Annotation.h
  11. ArrayType.cpp
  12. ArrayType.h
  13. AST.cpp
  14. AST.h
  15. CleanSpec.mk
  16. CompoundType.cpp
  17. CompoundType.h
  18. ConstantExpression.cpp
  19. ConstantExpression.h
  20. Coordinator.cpp
  21. Coordinator.h
  22. DeathRecipientType.cpp
  23. DeathRecipientType.h
  24. DocComment.cpp
  25. DocComment.h
  26. EnumType.cpp
  27. EnumType.h
  28. FmqType.cpp
  29. FmqType.h
  30. generateCpp.cpp
  31. generateCppAdapter.cpp
  32. generateCppImpl.cpp
  33. generateJava.cpp
  34. generateVts.cpp
  35. HandleType.cpp
  36. HandleType.h
  37. Hash.cpp
  38. hidl-gen_l.h
  39. hidl-gen_l.ll
  40. hidl-gen_y.yy
  41. HidlTypeAssertion.cpp
  42. HidlTypeAssertion.h
  43. Interface.cpp
  44. Interface.h
  45. Location.cpp
  46. Location.h
  47. main.cpp
  48. MemoryType.cpp
  49. MemoryType.h
  50. Method.cpp
  51. Method.h
  52. MODULE_LICENSE_APACHE2
  53. NamedType.cpp
  54. NamedType.h
  55. NOTICE
  56. OWNERS
  57. PointerType.cpp
  58. PointerType.h
  59. PREUPLOAD.cfg
  60. README.md
  61. Reference.h
  62. RefType.cpp
  63. RefType.h
  64. ScalarType.cpp
  65. ScalarType.h
  66. Scope.cpp
  67. Scope.h
  68. StringType.cpp
  69. StringType.h
  70. Type.cpp
  71. Type.h
  72. TypeDef.cpp
  73. TypeDef.h
  74. update-all-google-makefiles.sh
  75. update-makefiles-helper.sh
  76. VectorType.cpp
  77. VectorType.h
README.md

hidl-gen

Full documentation can be found here: https://source.android.com/devices/architecture/hidl/

hidl-gen is a compiler for the HIDL (HAL Interface Design Language) which generates C++ and Java endpoints for RPC mechanisms. The main userspace libraries which this compiler uses can be found at system/libhidl.

1. Build

m hidl-gen

2. Run

Note that options for hidl-gen expected to be invoked by the build system are marked with 'internal' in the help menu.

hidl-gen -h

hidl-gen -o output -L c++-impl -r android.hardware:hardware/interfaces -r android.hidl:system/libhidl/transport android.hardware.nfc@1.0

Some defaults for package roots are also provided

hidl-gen -o output -L c++-impl android.hardware.nfc@1.0
hidl-gen -o output -L vts android.hardware.nfc@1.0
hidl-gen -L hash android.hardware.nfc@1.0

Example command for vendor project

hidl-gen -L c++-impl -r vendor.foo:vendor/foo/interfaces vendor.foo.nfc@1.0

See update-makefiles-helper.sh and update-all-google-makefiles.sh for examples of how to generate HIDL makefiles (using the -Landroidbp option).

c2hal

This is a helper tool to convert C headers to valid .hal files.

m c2hal && c2hal -h

docs

This tool generates html documentation for hal interfaces.

m hidl-doc && hidl-doc -h