Android12之HIDL转AIDL通信(一百三十七)

梅宏盛
2023-12-01

1.编译hidl2aidl bin

# make hidl2aidl
# ls out/soong/host/linux-x86/bin/hidl2aidl
out/soong/host/linux-x86/bin/hidl2aidl

# hidl2aidl --help
hidl2aidl: invalid option -- '-'
Usage: hidl2aidl [-fh] [-o <output path>] [-l <header file>] [-p <root path>] (-r <interface root>)+ [-R] [-F] [-v] [-d <depfile>] FQNAME

Converts FQNAME, PACKAGE(.SUBPACKAGE)*@[0-9]+.[0-9]+(::TYPE)? to an aidl equivalent.

        -f: Force hidl2aidl to convert older packages
        -e: Used for expanding extensions and types from other packages
        -h: Prints this menu.
        -o <output path>: Location to output files.
        -l <header file>: File containing a header to prepend to generated files.
        -p <root path>: Android build root, defaults to $ANDROID_BUILD_TOP or pwd.
        -R: Do not add default package roots if not specified in -r.
        -F: Require all referenced ASTs to be frozen.
        -r <package:path root>: E.g., android.hardware:hardware/interfaces.
        -v: verbose output.
        -d <depfile>: location of depfile to write to.

2.创建HIDL文件

# mkdir -p hardware/interfaces/open/1.0
# emacs IOpen.hal
package android.hardware.open@1.0;

interface IOpen {
    putChars(string msg);
    getChars() generates (string msg);
};

注意:interface IOpen接口中的IOpen,必须是I开头;如果定义为interface Open,则错误报错。

3.HIDL到AIDL转换

# cd hardware/interfaces/open
# mkdir aidl
# hidl2aidl -o hardware/interfaces/open/aidl -r android.hardware:hardware/interfaces android.hardware.open@1.0

# tree 
.
├── 1.0
│   └── IOpen.hal
└── aidl
    ├── Android.bp
    ├── android
    │   └── hardware
    │       └── open
    │           └── IOpen.aidl
    └── conversion.log

注意:以上均为转换后自动生成的目录和文件.

4.对比IOpen.hal与IOpen.aidl内容

<1>.IOpen.hal
package android.hardware.open@1.0;

interface IOpen {
    putChars(string msg);
    getChars() generates (string msg);
};
<2>.IOpen.aidl
// FIXME: license file, or use the -l option to generate the files with the header.

package android.hardware.open;

@VintfStability
interface IOpen {
    // Adding return type to method instead of out param String msg since there is only one return value.
    String getChars();

    void putChars(in String msg);
}
 类似资料: