新建一个hidl/aidl car property

2025-08-26

创建一个HIDL car property

创建types.hal文件

vendor/braisedp/interfaces/automotive/vehicle/2.0/新建文件:

// types.hal
package vendor.braisedp.hardware.automotive.vehicle@2.0;

import android.hardware.automotive.vehicle@2.0::VehiclePropertyGroup;
import android.hardware.automotive.vehicle@2.0::VehiclePropertyType;
import android.hardware.automotive.vehicle@2.0::VehicleArea;

enum ExtentVehicleProperty: int32_t{
    EXT_PROP = (
        0x1000
        |VehiclePropertyType:INT32
        |VehicleArea:GLOBAL
        |VehiclePropertyGroup:VENDOR),
};

通过hidl-gen生成.bp文件

执行shell命令:

hidl-gen -h
Usage: hidl-gen -o <output path> -L <language> [-O <owner>] [-p <root path>] (-r <interface root>)+ [-R] [-v] [-d <depfile>] FQNAME...

Process FQNAME, PACKAGE(.SUBPACKAGE)*@[0-9]+.[0-9]+(::TYPE)?, to create output.

        -h: Prints this menu.
        -L <language>: The following options are available:
            check           : Parses the interface to see if valid but doesn't write any files.
            c++             : (internal) (deprecated) Generates C++ interface files for talking to HIDL interfaces.
            c++-headers     : (internal) Generates C++ headers for interface files for talking to HIDL interfaces.
            c++-sources     : (internal) Generates C++ sources for interface files for talking to HIDL interfaces.
            export-header   : Generates a header file from @export enumerations to help maintain legacy code.
            c++-impl        : Generates boilerplate implementation of a hidl interface in C++ (for convenience).
            c++-impl-headers: c++-impl but headers only.
            c++-impl-sources: c++-impl but sources only.
            c++-adapter     : Takes a x.(y+n) interface and mocks an x.y interface.
            c++-adapter-headers: c++-adapter but helper headers only.
            c++-adapter-sources: c++-adapter but helper sources only.
            c++-adapter-main: c++-adapter but the adapter binary source only.
            java            : (internal) Generates Java library for talking to HIDL interfaces in Java.
            java-impl       : Generates boilerplate implementation of a hidl interface in Java (for convenience).
            java-constants  : (internal) Like export-header but for Java (always created by -Lmakefile if @export exists).
            vts             : (internal) Generates vts proto files for use in vtsd.
            makefile        : (removed) Used to generate makefiles for -Ljava and -Ljava-constants.
            androidbp       : (internal) Generates Soong bp files for -Lc++-headers, -Lc++-sources, -Ljava, -Ljava-constants, and -Lc++-adapter.
            androidbp-impl  : Generates boilerplate bp files for implementation created with -Lc++-impl.
            hash            : Prints hashes of interface in `current.txt` format to standard out.
            function-count  : Prints the total number of functions added by the package or interface.
            dependencies    : Prints all depended types.
            inheritance-hierarchy: Prints the hierarchy of inherited types as a JSON object.
            format          : Reformats the .hal files
        -O <owner>: The owner of the module for -Landroidbp(-impl)?.
        -o <output path>: Location to output 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.
        -r <package:path root>: E.g., android.hardware:hardware/interfaces.
        -v: verbose output.
        -d <depfile>: location of depfile to write to.

,查看hidl-gen的命令格式

执行shell命令:

hidl-gen -L androidbp -r vendor.braisedp.hardware:vendor/braisedp/interfaces -r android.hidl:system/libhidl/transport braisedp.hardware.automotive.vehicle@2.0

在当前目录下输出Android.bp文件:

// This file is autogenerated by hidl-gen -Landroidbp.

hidl_interface {
    name: "vendor.braisedp.hardware.automotive.vehicle@2.0",
    root: "vendor.braisedp.hardware",
    srcs: [
        "types.hal",
    ],
    interfaces: [
        "android.hidl.base@1.0",
        "android.hardware.automotive.vehicle@2.0",
    ],
    gen_java: true,
}

配置vendor\braisedp\interfaces下的Android.bp文件:

hidl_package_root{
    name: "vendor.braisedp.hardware",
    path: "vendor/braisedp/interfaces",
}

执行mm命令进行编译

进入out/target/product/<product-name>/system/framework/,发现生成了两个包:

generated jar

进入out/target/product/<product-name>/system/lib/,发现生成了如下.so文件: generated so

进入out/soong/.intermediates/vendor/braisedp/interfaces/automotive/vehicle/2.0/,可以看到生成如下内容:

进入out/soong/.intermediates/vendor/braisedp/interfaces/automotive/vehicle/2.0/vendor.braisedp.hardware.automotive.vehicle@2.0_genc++_headers/gen/vendor/braisedp/hardware/automotive/vehicle/2.0/下,发现生成了.h文件: generated header

在外部使用包

在APK中使用jar包

查看生成的jar包结构内容: genereated jar struct ,生成了一个类ExtentVehicleProperty,查看类定义:

package vendor.braisedp.hardware.automotive.vehicle.V2_0;

import java.util.ArrayList;

public final class ExtentVechicleProperty {
    public static final int EXT_PROP = 557846528;

    public ExtentVechicleProperty() {
    }

    ...
}

,可以看到,生成了一个final类,并指定了一个final int类型的静态成员EXT_PROP

在native中使用动态库

查看生成的types.h头文件内容:

#ifndef HIDL_GENERATED_VENDOR_BRAISEDP_HARDWARE_AUTOMOTIVE_VEHICLE_V2_0_TYPES_H
#define HIDL_GENERATED_VENDOR_BRAISEDP_HARDWARE_AUTOMOTIVE_VEHICLE_V2_0_TYPES_H

#include <android/hardware/automotive/vehicle/2.0/types.h>

#include <hidl/HidlSupport.h>
#include <hidl/MQDescriptor.h>
#include <utils/NativeHandle.h>
#include <utils/misc.h>

namespace vendor {
namespace braisedp {
namespace hardware {
namespace automotive {
namespace vehicle {
namespace V2_0 {

// Forward declaration for forward reference support:
enum class ExtentVehicleProperty : int32_t;

enum class ExtentVehicleProperty : int32_t {
    EXT_PROP = 557846528 /* (0x1000 | VehiclePropertyType:INT32 | VehicleArea:GLOBAL | VehiclePropertyGroup:VENDOR) */,
};

...
}  // namespace V2_0
}  // namespace vehicle
}  // namespace automotive
}  // namespace hardware
}  // namespace braisedp
}  // namespace vendor
#endif  // HIDL_GENERATED_VENDOR_BRAISEDP_HARDWARE_AUTOMOTIVE_VEHICLE_V2_0_TYPES_H

,生成了一个枚举类ExtentVehicleProperty,并指定了一个成员EXT_PROP

创建一个AIDL car property属性

/vendor/braisedp/vehicle/aidl/vendor/braisedp/vehicle/ExtentVehicleProperty.aidl中创建内容:

package vendor.braisedp.vehicle;

import android.hardware.automotive.vehicle.VehiclePropertyGroup;
import android.hardware.automotive.vehicle.VehiclePropertyType;
import android.hardware.automotive.vehicle.VehicleArea;


@Backing(type="int")
enum ExtentVehicleProperty{
    EXT_PROP = 0x1000 + VehiclePropertyGroup.VENDOR + VehicleArea.GLOBAL + VehiclePropertyType.INT32,
}

,并编写/vendor/braisedp/vehicle/aidl/Android.bp文件:

aidl_interface{
    name : "vendor.braisedp.vehicle",
    srcs : ["vendor/braisedp/vehicle/ExtentVehicleProperty.aidl"],
    unstable: true,
    vendor_available: true,
    imports:["android.hardware.automotive.vehicle.property-V2"],
    backend:{
        java : {
            enabled : true,
            sdk_version: "module_current",
            apex_available:["com.android.car.framework",],
        },
        cpp : {
             enabled : false,
        },
        ndk : {
             enabled : true,
             vndk:{
                enabled: false,
             }
        },
    },
}

,打包后,可以在out/soong/.intermediates下看到对应的jar包。

集成property至car-lib

在外部开发时,会用到包android.car,于是要将生成的Property信息放入到该包中,修改/packages/services/Car/car-lib/src/android/car/VehiclePropertyIds.java,加入:

@RequiresPermission(Car.PERMISSION_CAR_INFO)
public static final int  EXT_PROP = 557846528;

,并在toString方法中加入:

case EXT_PROP:
    return "EXT_PROP";

,执行命令m update-api,看到/packages/services/Car/car-lib/api/current.txt内增加了如下内容:

field @RequiresPermission(android.car.Car.PERMISSION_CAR_INFO) public static final int EXT_PROP = 557846528; // 0x21401000

car-lib下执行mm命令,输出android.car.jar包,查看android.car.jar包中VehiclePropertyIds的内容:

public static final int EXT_PROP = 557846528;

,多出了一个静态成员,同样,在toString方法中:

case 557846528:
    return "EXT_PROP";

,多出了相应的条目