Android Automotive(四) Vehicle Property

Android Automotive(四) Vehicle Property

在介绍CarService之前,先介绍一下Vehicle Property(车辆属性),车辆属性是Android Automotive 中最重要的内容之一,使用它来传递车辆上的一些重要信息。

车辆属性是硬件抽象层定义的汽车设备制造商可以实现的属性,并会包含属性元数据(例如,属性是否为 int 以及允许使用哪些更改模式)。Vehicle HAL 提供接口对车辆属性进行访问(读取、写入、订阅)。

车辆属性使用HIDL语言定义,代码位于hardware/interfaces/automotive/vehicle/2.0/types.hal中。

车辆属性可以是只读、只写、也可以是读写。每个车辆属性都由一个int32键唯一标识,且具有预定义的数据类型。区域属性可能具有多个值,具体取决于属性支持区域的数量

车辆属性的定义是由四个整型值做或运算取得的。它们描述了车辆属性的ID、分组、类型和区域。

INFO_VIN = (
    0x0100
    | VehiclePropertyGroup:SYSTEM
    | VehiclePropertyType:STRING
    | VehicleArea:GLOBAL)

除了定义所需的必须属性,车辆属性的额外信息以注释的形式标记在文件中。包括车辆属性的变化类型、读写性、单位、取值的数据结构等等。

/**
 * VIN of vehicle
 *
 * @change_mode VehiclePropertyChangeMode:STATIC
 * @access VehiclePropertyAccess:READ
 */
INFO_VIN = 

具体如下

注释描述类型
@change_mode变化模式VehiclePropertyChangeMode
STATIC 静态
ON_CHANGE 变化
CONTINUOUS 持续
@access读写性质VehiclePropertyAccess
READ 只读
WRITE 只写
READ_WRITE 读写
@unit单位VehicleUnit
@data_enum数据结构
@since增加的版本

属性ID

是一个int数值,通常是4位16进制,占半个字节。在车辆属性中使用“4567”后四位表示属性ID,如0x0000****。通常省略高位0,写成0x****的形式。

属性分组

分组表示了车辆属性的来源,区分是原生属性还是扩展属性。原生属性是指AOSP定义的属性,扩展属性是指OEM厂商自己定义增加的属性。在车辆属性中使用第0位占位0x*000000。

//types.hal中定义
enum VehiclePropertyGroup : int32_t {
    /**
     * Properties declared in AOSP must have this flag.
     */
    SYSTEM         = 0x10000000, //268435456

    /**
     * Properties declared by vendors must have this flag.
     */
    VENDOR         = 0x20000000, //536870912

    MASK           = 0xf0000000, //-268435456
};
  • SYSTEM:Google原生属性定义使用
  • VENDOR:OME自定义属性使用
属性十六进制十进制
SYSTEM0x10000000268435456
VENDOR0x20000000536870912

扩展属性必须使用VENDOR声明

属性类型

表示车辆属性对应值的数据类型,占0x00**0000第“23”位。
目前支持的数据类型如下

  • STRING
  • BOOLEAN
  • INT32
  • INT32_VEC (INT32[])
  • INT64
  • INT64_VEC (INT64[])
  • FLOAT
  • FLOAT_VEC (FLOAT[])
  • BYTES
//types.hal中定义
/**
 * Enumerates supported data type for VehicleProperty.
 *
 * Used to create property ID in VehicleProperty enum.
 */
enum VehiclePropertyType : int32_t {
    STRING          = 0x00100000,
    BOOLEAN         = 0x00200000,
    INT32           = 0x00400000,
    INT32_VEC       = 0x00410000,
    INT64           = 0x00500000,
    INT64_VEC       = 0x00510000,
    FLOAT           = 0x00600000,
    FLOAT_VEC       = 0x00610000,
    BYTES           = 0x00700000,

    /**
     * Any combination of scalar or vector types. The exact format must be
     * provided in the description of the property.
     *
     * For vendor MIXED type properties, configArray needs to be formatted in this
     * structure.
     * configArray[0], 1 indicates the property has a String value
     * configArray[1], 1 indicates the property has a Boolean value .
     * configArray[2], 1 indicates the property has an Integer value.
     * configArray[3], the number indicates the size of Integer[] in the property.
     * configArray[4], 1 indicates the property has a Long value.
     * configArray[5], the number indicates the size of Long[] in the property.
     * configArray[6], 1 indicates the property has a Float value.
     * configArray[7], the number indicates the size of Float[] in the property.
     * configArray[8], the number indicates the size of byte[] in the property.
     * For example:
     * {@code configArray = {1, 1, 1, 3, 0, 0, 0, 0, 0}} indicates the property has
     * a String value, a Boolean value, an Integer value and an array with 3 integers.
     */
    MIXED           = 0x00e00000,

    MASK            = 0x00ff0000
};
属性十六进制十进制
STRING0x001000001048576
BOOLEAN0x002000002097152
INT320x004000004194304
INT32_VEC0x004100004259840
INT640x005000005242880
INT64_VEC0x005100005308416
FLOAT0x006000006291456
FLOAT_VEC0x006100006356992
BYTES0x007000007340032
MIXED0x00e0000014680064

属性区域

属性区域定义了车辆属性归属车辆的哪一个模块,比如门或者窗。占位0x0*000000,第1位。

Android P 删除了ZONE区域,新增WHEEL区域。

每个区域属性都必须使用预定义的区域类型。每种区域类型都有一组在区域类型的枚举中定义的位标记。

//types.hal中定义
/**
 * Vehicle Areas
 * Used to construct property IDs in the VehicleProperty enum.
 *
 * Some properties may be associated with particular vehicle areas. For
 * example, VehicleProperty:DOOR_LOCK property must be associated with
 * particular door, thus this property must be marked with
 * VehicleArea:DOOR flag.
 *
 * Other properties may not be associated with particular vehicle area.
 * These kinds of properties must have VehicleArea:GLOBAL flag.
 *
 * [Definition] Area: An area represents a unique element of an AreaType.
 *   For instance, if AreaType is WINDOW, then an area may be FRONT_WINDSHIELD.
 *
 * [Definition] AreaID: An AreaID is a combination of one or more areas,
 *   and is represented using a bitmask of Area enums. Different AreaTypes may
 *   not be mixed in a single AreaID. For instance, a window area cannot be
 *   combined with a seat area in an AreaID.
 *
 * Rules for mapping a zoned property to AreaIDs:
 *  - A property must be mapped to an array of AreaIDs that are impacted when
 *    the property value changes.
 *  - Each element in the array must represent an AreaID, in which the
 *    property value can only be changed together in all the areas within
 *    the AreaID and never independently. That is, when the property value
 *    changes in one of the areas in an AreaID in the array, then it must
 *    automatically change in all other areas in the AreaID.
 *  - The property value must be independently controllable in any two
 *    different AreaIDs in the array.
 *  - An area must only appear once in the array of AreaIDs. That is, an
 *    area must only be part of a single AreaID in the array.
 *
 * [Definition] Global Property: A property that applies to the entire car
 *   and is not associated with a specific area. For example, FUEL_LEVEL,
 *   HVAC_STEERING_WHEEL_HEAT.
 *
 * Rules for mapping a global property to AreaIDs:
 *  - A global property must not be mapped to AreaIDs.
*/
enum VehicleArea : int32_t {
    GLOBAL      = 0x01000000,
    /** WINDOW maps to enum VehicleAreaWindow */
    //ZONE         = 0x02000000, //android P开始删除
    WINDOW      = 0x03000000,
    /** MIRROR maps to enum VehicleAreaMirror */
    MIRROR      = 0x04000000,
    /** SEAT maps to enum VehicleAreaSeat */
    SEAT        = 0x05000000,
    /** DOOR maps to enum VehicleAreaDoor */
    DOOR        = 0x06000000,
    /** WHEEL maps to enum VehicleAreaWheel */
    WHEEL       = 0x07000000,

    MASK        = 0x0f000000,
};
属性十六进制十进制
GLOBAL0x0100000016777216
ZONE0x0200000033554432
WINDOW0x0300000050331648
MIRROR0x0400000067108864
SEAT0x0500000083886080
DOOR0x06000000100663296
WHEEL0x07000000117440512

GLOBAL

此属性是单一实例,不具备多个区域。

ZONE

用于空调并将汽车划分为物理区域。

在Android P版本删除。使用枚举类VehicleAreaZone。Android P 开始删除此属性

//types.hal中定义
/**
 * Various zones in the car.
 *
 * Zones are used for Air Conditioning purposes and divide the car into physical
 * area zones.
 */
enum VehicleAreaZone : int32_t {
  ROW_1_LEFT = 0x00000001,
  ROW_1_CENTER = 0x00000002,
  ROW_1_RIGHT = 0x00000004,
  ROW_1 = 0x00000008,
  ROW_2_LEFT = 0x00000010,
  ROW_2_CENTER = 0x00000020,
  ROW_2_RIGHT = 0x00000040,
  ROW_2 = 0x00000080,
  ROW_3_LEFT = 0x00000100,
  ROW_3_CENTER = 0x00000200,
  ROW_3_RIGHT = 0x00000400,
  ROW_3 = 0x00000800,
  ROW_4_LEFT = 0x00001000,
  ROW_4_CENTER = 0x00002000,
  ROW_4_RIGHT = 0x00004000,
  ROW_4 = 0x00008000,
  WHOLE_CABIN = 0x80000000,
};

WINDOW

基于车窗的区域。使用枚举类VehicleAreaWindow

  • FRONT_WINDSHIELD 前挡风玻璃
  • REAR_WINDSHIELD 后挡风玻璃
  • ROW_1_LEFT 第一排左侧车窗
  • ROW_1_RIGHT 第一排右侧车窗
  • ROW_2_LEFT 第二排左侧车窗
  • ROW_2_RIGHT 第二排右侧车窗
  • ROW_3_LEFT 第三排左侧车窗
  • ROW_3_RIGHT 第三排右侧车窗
  • ROOF_TOP_1 天窗1
  • ROOF_TOP_2 天窗2
// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**
 * Various windshields/windows in the car.
 */
enum VehicleAreaWindow : int32_t {
    FRONT_WINDSHIELD  = 0x00000001,
    REAR_WINDSHIELD   = 0x00000002,
    ROW_1_LEFT        = 0x00000010,
    ROW_1_RIGHT       = 0x00000040,
    ROW_2_LEFT        = 0x00000100,
    ROW_2_RIGHT       = 0x00000400,
    ROW_3_LEFT        = 0x00001000,
    ROW_3_RIGHT       = 0x00004000,

    ROOF_TOP_1        = 0x00010000,
    ROOF_TOP_2        = 0x00020000,

};
属性十六进制十进制
FRONT_WINDSHIELD0x000000011
REAR_WINDSHIELD0x000000022
ROW_1_LEFT0x0000001016
ROW_1_RIGHT0x0000004064
ROW_2_LEFT0x00000100256
ROW_2_RIGHT0x000004001024
ROW_3_LEFT0x000010004096
ROW_3_RIGHT0x0000400016384
ROOF_TOP_10x0001000065536
ROOF_TOP_20x00020000131072

MIRROR

基于车镜的区域。使用枚举类VehicleAreaMirror

  • DRIVER_LEFT 左后视镜
  • DRIVER_RIGHT 右后视镜
  • DRIVER_CENTER 中央后视镜
// hardware/interfaces/automotive/vehicle/2.0/types.hal
enum VehicleAreaMirror : int32_t {
    DRIVER_LEFT = 0x00000001,
    DRIVER_RIGHT = 0x00000002,
    DRIVER_CENTER = 0x00000004,
};
属性十六进制十进制
DRIVER_LEFT0x000000011
DRIVER_RIGHT0x000000022
DRIVER_CENTER0x000000044

SEAT

基于座椅的区域,使用枚举类VehicleAreaSeat

  • ROW_1_LEFT 第一排左侧
  • ROW_1_CENTER 第一排中间
  • ROW_1_RIGHT 第一排右侧
  • ROW_2_LEFT 第二排左侧
  • ROW_2_CENTER 第二排中间
  • ROW_2_RIGHT 第二排右侧
  • ROW_3_LEFT 第三排左侧
  • ROW_3_CENTER 第三排中间
  • ROW_3_RIGHT 第三排右侧
// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**
 * Various Seats in the car.
 */
enum VehicleAreaSeat : int32_t {
    ROW_1_LEFT   = 0x0001,
    ROW_1_CENTER = 0x0002,
    ROW_1_RIGHT  = 0x0004,
    ROW_2_LEFT   = 0x0010,
    ROW_2_CENTER = 0x0020,
    ROW_2_RIGHT  = 0x0040,
    ROW_3_LEFT   = 0x0100,
    ROW_3_CENTER = 0x0200,
    ROW_3_RIGHT  = 0x0400
};
属性十六进制十进制
ROW_1_LEFT0x00011
ROW_1_CENTER0x00022
ROW_1_RIGHT0x00044
ROW_2_LEFT0x001016
ROW_2_CENTER0x002032
ROW_2_RIGHT0x004064
ROW_3_LEFT0x0100256
ROW_3_CENTER0x0200512
ROW_3_RIGHT0x04001024

DOOR

基于车门的区域。使用枚举类VehicleAreaDoor

  • ROW_1_LEFT 第一排左侧门
  • ROW_1_RIGHT 第一排右侧门
  • ROW_2_LEFT 第二排左侧门
  • ROW_2_RIGHT 第二排右侧门
  • ROW_3_LEFT 第三排左侧门
  • ROW_3_RIGHT 第三排右侧门
  • HOOD 前车厢门
  • REAR 后车厢门
// hardware/interfaces/automotive/vehicle/2.0/types.hal
enum VehicleAreaDoor : int32_t {
    ROW_1_LEFT = 0x00000001,
    ROW_1_RIGHT = 0x00000004,
    ROW_2_LEFT = 0x00000010,
    ROW_2_RIGHT = 0x00000040,
    ROW_3_LEFT = 0x00000100,
    ROW_3_RIGHT = 0x00000400,
    HOOD = 0x10000000,
    REAR = 0x20000000,
};
属性十六进制十进制
ROW_1_LEFT0x000000011
ROW_1_RIGHT0x000000044
ROW_2_LEFT0x0000001016
ROW_2_RIGHT0x0000004064
ROW_3_LEFT0x00000100256
ROW_3_RIGHT0x000004001024
HOOD0x10000000268435456
REAR0x20000000536870912

WHEEL

基于车轮的区域。使用枚举类VehicleAreaWheel

  • UNKNOWN 未知
  • LEFT_FRONT 左前轮胎
  • RIGHT_FRONT 右前轮胎
  • LEFT_REAR 左后轮胎
  • RIGHT_REAR 右后轮胎
// hardware/interfaces/automotive/vehicle/2.0/types.hal
enum VehicleAreaWheel : int32_t {
    UNKNOWN = 0x0,

    LEFT_FRONT = 0x1,
    RIGHT_FRONT = 0x2,
    LEFT_REAR = 0x4,
    RIGHT_REAR = 0x8,
};
属性十六进制十进制
UNKNOWN0x00
LEFT_FRONT0x11
RIGHT_FRONT0x22
LEFT_REAR0x44
RIGHT_REAR0x88

每个区域属性都必须使用预定义的区域类型。每种区域类型都有一组在区域类型的枚举中定义的位标记

区域 ID

区域属性通过区域 ID 进行处理。每个区域属性都可以支持一个或多个区域 ID。区域 ID 由其各自枚举中的一个或多个标记组成。例如,使用 VehicleAreaSeat 的属性可能会使用以下区域 ID:

  • ROW_1_LEFT | ROW_1_RIGHT
    区域 ID 适用于两个前排座椅。
  • ROW_2_LEFT
    仅适用于左后座椅。
  • ROW_2_RIGHT
    仅适用于右后座椅。

属性操作

车辆属性的可见性标识该属性是否只读,只写,读写。在types.hal中以注释@access的形式出现
如:@access VehiclePropertyAccess::READ_WRITE

  • NONE 不可读不可写
  • READ 只读
  • WRITE 只写
  • READ_WRITE 读写
// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**
 * Property config defines the capabilities of it. User of the API
 * must first get the property config to understand the output from get()
 * commands and also to ensure that set() or events commands are in sync with
 * the expected output.
 */
enum VehiclePropertyAccess : int32_t {
    NONE = 0x00,

    READ = 0x01,
    WRITE = 0x02,
    READ_WRITE = 0x03,
};
属性说明Android版本
NONE不可读不可写
READ只读8.0~
WRITE只写8.0~
READ_WRITE读写8.0~
属性十六进制十进制
NONE0x000
READ0x011
WRITE0x022
READ_WRITE0x033

属性变化模式

属性的变化模式,在types.hal中以注释@change_mode的形式出现

如:@change_mode VehiclePropertyChangeMode::ON_CHANGE

// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**

 * This describes how value of property can change.
 */
enum VehiclePropertyChangeMode : int32_t {
    /**
   * Property of this type must never be changed. Subscription is not supported
   * for these properties.
   */
    STATIC = 0x00,

  /**
   * Property of this type must be reported when there is a change.
   * IVehicle#get call must return the current value.
   * Set operation for this property is assumed to be asynchronous. When the
   * property is read (using IVehicle#get) after IVehicle#set, it may still
   * return old value until underlying H/W backing this property has actually
   * changed the state. Once state is changed, the property must dispatch
   * changed value as event.
      */
    ON_CHANGE = 0x01,

  /**
   * Property of this type change continuously and requires fixed rate of
   * sampling to retrieve the data.
      */
    CONTINUOUS = 0x02,

  /**
   * Property of this type may be polled to get the current value.
      */
    POLL = 0x03,

  /**
   * This is for property where change event must be sent only when the
   * value is set from external component. Normal value change must not trigger
   * event. For example, clock property can send change event only when it is
   * set, outside android, for case like user setting time or time getting
   * update. There is no need to send it per every value change.
      */
    ON_SET = 0x04,
};
属性说明Android版本
STATIC决不能更改此类型的属性。不支持订阅这些属性8.0~
ON_CHANGE发生改变时,必须报告此类型的属性。 IVehicle#get调用必须返回当前值。假如设置是异步的,当在IVehicle#set之后读取属性时,它可能返回旧值,直到此属性的底层H/W实际更改了状态。状态更改后,属性必须将更改的值作为事件分派。8.0~
CONTINUOUS此种类型的属性不断变化,需要固定的采样率来检索数据。实现者可以选择在重大值更改时发送额外的通知。如车速8.0~
POLL可以轮询此类型的属性以获取当前值8.0
ON_SET变化时不需要一直上报,只有上层主动获取才上报,比如时间更新8.0

POLL 和 ON_SET在Android P上被删除,不再使用

属性单位

某些属性的值需要配合国际单位来表示,比如车速的单位是米每秒(MILLIMETER)。

属性值的单位, 在types.hal中以注释@unit的形式出现,表示车辆属性表示含义的单位。

如:@unit VehicleUnit:YEAR表示单位是年

// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**

 * Units used for int or float type with no attached enum types.
 */
enum VehicleUnit : int32_t {
    SHOULD_NOT_USE      = 0x000,

    METER_PER_SEC       = 0x01,
    RPM                 = 0x02,
    HERTZ               = 0x03,
    PERCENTILE          = 0x10,
    MILLIMETER          = 0x20,
    METER               = 0x21,
    KILOMETER           = 0x23,
    MILE                = 0x24,
    CELSIUS             = 0x30,
    FAHRENHEIT          = 0x31,
    KELVIN              = 0x32,
    MILLILITER          = 0x40,
    LITER               = 0x41,

    /** deprecated. Use US_GALLON instead. */
    GALLON              = 0x42,
    US_GALLON           = 0x42,
    IMPERIAL_GALLON     = 0x43,
    NANO_SECS           = 0x50,
    SECS                = 0x53,
    YEAR                = 0x59,

    // Electrical Units
    WATT_HOUR           = 0x60,
    MILLIAMPERE         = 0x61,
    MILLIVOLT           = 0x62,
    MILLIWATTS          = 0x63,
    AMPERE_HOURS        = 0x64,
    KILOWATT_HOUR       = 0x65,

    KILOPASCAL          = 0x70,
    PSI                 = 0x71,
    BAR                 = 0x72,
    DEGREES             = 0x80,

    MILES_PER_HOUR      = 0x90,
    KILOMETERS_PER_HOUR = 0x91,
};
  • SHOULD_NOT_USE, 不使用
  • METER_PER_SEC, 米每秒
  • RPM, 转速
  • HERTZ, 赫兹
  • PERCENTILE, 百分比
  • MILLIMETER, 毫米
  • METER, 米
  • KILOMETER, 千米,公里
  • MILE, 英里
  • CELSIUS, 摄氏度
  • FAHRENHEIT, 华氏度
  • KELVIN, 开尔文
  • MILLILITER, 毫升
  • LITER, 升
  • US_GALLON, 美国加仑
  • IMPERIAL_GALLON, 英制加仑
  • NANO_SECS, 纳秒
  • SECS, 秒
  • YEAR, 年
  • KILOPASCAL, 千帕
  • WATT_HOUR, 瓦时
  • MILLIAMPERE, 毫安
  • MILLIVOLT, 毫伏
  • MILLIWATTS, 毫瓦
  • AMPERE_HOURS, 安倍小时
  • KILOWATT_HOUR, 千瓦时
  • PSI, 磅/平方英寸
  • BAR, 巴 压强的单位
  • DEGREES, 度
  • MILES_PER_HOUR, 英里每小时
  • KILOMETERS_PER_HOUR 公里每小时

属性数据结构

某些属性,将属性的可取值封装成一个对象,这样可以方便调用,比如VehicleGear表示档位。

// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**
 * Various gears which can be selected by user and chosen in system.
 */
enum VehicleGear: int32_t {
    GEAR_NEUTRAL = 0x0001,
    GEAR_REVERSE = 0x0002,
    GEAR_PARK = 0x0004,
    GEAR_DRIVE = 0x0008,
    GEAR_LOW = 0x0010,
    GEAR_1 = 0x0010,
    GEAR_2 = 0x0020,
    GEAR_3 = 0x0040,
    GEAR_4 = 0x0080,
    GEAR_5 = 0x0100,
    GEAR_6 = 0x0200,
    GEAR_7 = 0x0400,
    GEAR_8 = 0x0800,
    GEAR_9 = 0x1000,
};

属性权限

属性的权限是Android在系统框架层增加的,只有声明权限的应用才可以访问相关的车辆属性。

属性的权限检测在CarService中,配置在PropertyHalServiceIds.java中使用,关联到各个车辆属性。

// packages/services/Car/service/src/com/android/car/hal/PropertyHalServiceIds.java
// default vendor permission
private static final int PERMISSION_CAR_VENDOR_DEFAULT = 0x00000000;

// permissions for the property related with window
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_WINDOW = 0X00000001;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_WINDOW = 0x00000002;
// permissions for the property related with door
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_DOOR = 0x00000003;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_DOOR = 0x00000004;
// permissions for the property related with seat
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_SEAT = 0x00000005;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_SEAT = 0x00000006;
// permissions for the property related with mirror
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_MIRROR = 0x00000007;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_MIRROR = 0x00000008;

// permissions for the property related with car's information
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_INFO = 0x00000009;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_INFO = 0x0000000A;
// permissions for the property related with car's engine
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_ENGINE = 0x0000000B;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_ENGINE = 0x0000000C;
// permissions for the property related with car's HVAC
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_HVAC = 0x0000000D;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_HVAC = 0x0000000E;
// permissions for the property related with car's light
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_LIGHT = 0x0000000F;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_LIGHT = 0x00000010;

// permissions reserved for other vendor permission
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_1 = 0x00010000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_1 = 0x00011000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_2 = 0x00020000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_2 = 0x00021000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_3 = 0x00030000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_3 = 0x00031000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_4 = 0x00040000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_4 = 0x00041000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_5 = 0x00050000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_5 = 0x00051000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_6 = 0x00060000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_6 = 0x00061000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_7 = 0x00070000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_7 = 0x00071000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_8 = 0x00080000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_8 = 0x00081000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_9 = 0x00090000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_9 = 0x00091000;
private static final int PERMISSION_SET_CAR_VENDOR_CATEGORY_10 = 0x000A0000;
private static final int PERMISSION_GET_CAR_VENDOR_CATEGORY_10 = 0x000A1000;
// Not available for android
private static final int PERMISSION_CAR_VENDOR_NOT_ACCESSIBLE = 0xF0000000;

在HAL层定义

// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**
 * Used by SUPPORT_CUSTOMIZE_VENDOR_PERMISSION to indicate the permission of vendor properties.
 */
enum VehicleVendorPermission : int32_t {
    PERMISSION_DEFAULT = 0x00000000,

    // permissions for the property related with window
    PERMISSION_SET_VENDOR_CATEGORY_WINDOW= 0X00000001,
    PERMISSION_GET_VENDOR_CATEGORY_WINDOW = 0x00000002,
    // permissions for the property related with door
    PERMISSION_SET_VENDOR_CATEGORY_DOOR  = 0x00000003,
    PERMISSION_GET_VENDOR_CATEGORY_DOOR   = 0x00000004,
    // permissions for the property related with seat
    PERMISSION_SET_VENDOR_CATEGORY_SEAT  = 0x00000005,
    PERMISSION_GET_VENDOR_CATEGORY_SEAT   = 0x00000006,
    // permissions for the property related with mirror
    PERMISSION_SET_VENDOR_CATEGORY_MIRROR= 0x00000007,
    PERMISSION_GET_VENDOR_CATEGORY_MIRROR = 0x00000008,

    // permissions for the property related with car's information
    PERMISSION_SET_VENDOR_CATEGORY_INFO  = 0x00000009,
    PERMISSION_GET_VENDOR_CATEGORY_INFO   = 0x0000000A,
    // permissions for the property related with car's engine
    PERMISSION_SET_VENDOR_CATEGORY_ENGINE= 0x0000000B,
    PERMISSION_GET_VENDOR_CATEGORY_ENGINE = 0x0000000C,
    // permissions for the property related with car's HVAC
    PERMISSION_SET_VENDOR_CATEGORY_HVAC  = 0x0000000D,
    PERMISSION_GET_VENDOR_CATEGORY_HVAC   = 0x0000000E,
    // permissions for the property related with car's light
    PERMISSION_SET_VENDOR_CATEGORY_LIGHT = 0x0000000F,
    PERMISSION_GET_VENDOR_CATEGORY_LIGHT  = 0x00000010,

    // permissions reserved for other vendor permission
    PERMISSION_SET_VENDOR_CATEGORY_1  = 0x00010000,
    PERMISSION_GET_VENDOR_CATEGORY_1   = 0x00011000,
    PERMISSION_SET_VENDOR_CATEGORY_2  = 0x00020000,
    PERMISSION_GET_VENDOR_CATEGORY_2   = 0x00021000,
    PERMISSION_SET_VENDOR_CATEGORY_3  = 0x00030000,
    PERMISSION_GET_VENDOR_CATEGORY_3   = 0x00031000,
    PERMISSION_SET_VENDOR_CATEGORY_4  = 0x00040000,
    PERMISSION_GET_VENDOR_CATEGORY_4   = 0x00041000,
    PERMISSION_SET_VENDOR_CATEGORY_5  = 0x00050000,
    PERMISSION_GET_VENDOR_CATEGORY_5   = 0x00051000,
    PERMISSION_SET_VENDOR_CATEGORY_6  = 0x00060000,
    PERMISSION_GET_VENDOR_CATEGORY_6   = 0x00061000,
    PERMISSION_SET_VENDOR_CATEGORY_7  = 0x00070000,
    PERMISSION_GET_VENDOR_CATEGORY_7   = 0x00071000,
    PERMISSION_SET_VENDOR_CATEGORY_8  = 0x00080000,
    PERMISSION_GET_VENDOR_CATEGORY_8   = 0x00081000,
    PERMISSION_SET_VENDOR_CATEGORY_9  = 0x00090000,
    PERMISSION_GET_VENDOR_CATEGORY_9   = 0x00091000,
    PERMISSION_SET_VENDOR_CATEGORY_10 = 0x000A0000,
    PERMISSION_GET_VENDOR_CATEGORY_10  = 0x000A1000,

    // Indicate not available for android to access.
    PERMISSION_NOT_ACCESSIBLE = 0xF0000000
};

在Framework中VehicleVendorPermission定义了应用使用的权限字符串。

// packages/services/Car/car-lib/src/android/car/hardware/property/VehicleVendorPermission.java
// permissions for the property related with window
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_WINDOW =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_WINDOW";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_WINDOW =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_WINDOW";

// permissions for the property related with door
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_DOOR =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_DOOR";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_DOOR =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_DOOR";

// permissions for the property related with seat
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_SEAT =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_SEAT";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_SEAT =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_SEAT";

// permissions for the property related with mirror
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_MIRROR =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_MIRROR";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_MIRROR =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_MIRROR";

// permissions for the property related with car's information
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_INFO =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_INFO";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_INFO =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_INFO";

// permissions for the property related with car's engine
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_ENGINE =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_ENGINE";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_ENGINE =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_ENGINE";

// permissions for the property related with car's HVAC
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_HVAC =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_HVAC";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_HVAC =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_HVAC";

// permissions for the property related with car's light
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_LIGHT =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_LIGHT";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_LIGHT =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_LIGHT";


// permissions reserved for other vendor permission
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_1 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_1";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_1 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_1";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_2 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_2";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_2 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_2";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_3 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_3";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_3 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_3";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_4 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_4";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_4 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_4";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_5 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_5";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_5 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_5";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_6 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_6";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_6 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_6";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_7 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_7";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_7 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_7";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_8 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_8";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_8 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_8";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_9 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_9";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_9 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_9";
public static final String PERMISSION_GET_CAR_VENDOR_CATEGORY_10 =
    "android.car.permission.GET_CAR_VENDOR_CATEGORY_10";
public static final String PERMISSION_SET_CAR_VENDOR_CATEGORY_10 =
    "android.car.permission.SET_CAR_VENDOR_CATEGORY_10";

属性状态

每个属性值都随附一个 VehiclePropertyStatus 值。该值指示相应属性的当前状态:

  • AVAILABLE
    属性可用,且值有效。
  • UNAVAILABLE
    属性值目前不可用。该值用于受支持属性的暂时停用的功能。
  • ERROR
    该属性有问题。

如果车辆不支持某个属性,则不应将其包含在 VHAL 中。您不得将属性状态永久性设置为 UNAVAILABLE 来表示不受支持的属性。

配置属性

在硬件抽象层会用一个VehiclePropConfig结构体来维护车辆属性的配置。

// hardware/interfaces/automotive/vehicle/2.0/types.hal
struct VehiclePropConfig {
    /** Property identifier */
    int32_t prop;

    /**
     * Defines if the property is read or write or both.
     */
    VehiclePropertyAccess access;
    
    /**
     * Defines the change mode of the property.
     */
    VehiclePropertyChangeMode changeMode;
    
    /**
     * Contains per-area configuration.
     */
    vec<VehicleAreaConfig> areaConfigs;
    
    /** Contains additional configuration parameters */
    vec<int32_t> configArray;
    
    /**
     * Some properties may require additional information passed over this
     * string. Most properties do not need to set this.
     */
    string configString;
    
    /**
     * Min sample rate in Hz.
     * Must be defined for VehiclePropertyChangeMode::CONTINUOUS
     */
    float minSampleRate;
    
    /**
     * Must be defined for VehiclePropertyChangeMode::CONTINUOUS
     * Max sample rate in Hz.
     */
    float maxSampleRate;
};
  • prop 属性ID
  • access 属性读写权限
  • changeMode 属性变化模式
  • areaConfigs 属性每个区域的配置
  • configArray 配置相关的数组
  • configString 配置的描述
  • minSampleRate 最小的变化频率,通知频率,变化模式必须定义为CONTINUOUS
  • maxSampleRate 最大的变化频率,通知频率,变化模式必须定义为CONTINUOUS
字段描述
prop属性ID
access属性读写权限
changeMode属性变化模式
areaConfigs属性每个区域的配置
configArray配置相关的数组
configString配置的描述
minSampleRate最小的变化频率,通知频率,变化模式必须定义为CONTINUOUS
maxSampleRate最大的变化频率,通知频率,变化模式必须定义为CONTINUOUS

在Android N上使用 vehicle_prop_config_t 进行配置

使用 vehicle_prop_config_t 为每个属性提供配置信息。具体信息包括:

  • access(r、w、rw)
  • changeMode(表示监视属性的方式:变化模式还是连续模式)
  • areaConfigs(areaId、最小值和最大值)
  • configArray(其他配置参数)
  • configString(以字符串的形式传递的其他信息)
  • minSampleRatemax_sample_rate
  • prop(属性 ID、int)

属性取值

属性的取值,也就是属性所带的数据使用VehiclePropValue来维护。在types.hal中定义。

// hardware/interfaces/automotive/vehicle/2.0/types.hal
/**
 * Encapsulates the property name and the associated value. It
 * is used across various API calls to set values, get values or to register for
 * events.
 */
struct VehiclePropValue {
    /** Time is elapsed nanoseconds since boot */
    int64_t timestamp;

    /**
     * Area type(s) for non-global property it must be one of the value from
     * VehicleArea* enums or 0 for global properties.
     */
    int32_t areaId;

    /** Property identifier */
    int32_t prop;

    /** Status of the property */
    VehiclePropertyStatus status;

    /**
     * Contains value for a single property. Depending on property data type of
     * this property (VehiclePropetyType) one field of this structure must be filled in.
     */
    struct RawValue {
        /**
         * This is used for properties of types VehiclePropertyType#INT
         * and VehiclePropertyType#INT_VEC
         */
        vec<int32_t> int32Values;

        /**
         * This is used for properties of types VehiclePropertyType#FLOAT
         * and VehiclePropertyType#FLOAT_VEC
         */
        vec<float> floatValues;

        /** This is used for properties of type VehiclePropertyType#INT64 */
        vec<int64_t> int64Values;

        /** This is used for properties of type VehiclePropertyType#BYTES */
        vec<uint8_t> bytes;

        /** This is used for properties of type VehiclePropertyType#STRING */
        string stringValue;
    };

    RawValue value;
};
字段`描述
timestamp时间戳,标记属性更新的时间
areaId表示的区域
prop属性ID
status属性的状态
value用RawValue来维护具体的属性值
int32Values维护整型数据的数组,布尔类型也是用此维护
int64Values维护长整型数据的数组
floatValues维护浮点型数据的数组
bytes维护字节型数据的数组
stringValue维护字符串类型

常量接口

前面我们知道HAL文件是使用HIDL语言定义,它在编译时,会生成C++和JAVA接口。

typs.hal也是一样,里面定义的车辆属性会编译出系统框架层和硬件抽象层使用的接口。

编译产物

源码下automotive生成的文件如下:

代码路径:out/soong/.intermediates/hardware/interfaces/automotive/vehicle/2.0

out/soong/.intermediates/hardware/interfaces/automotive/vehicle/2.0
├── android.hardware.automotive.vehicle@2.0
│   ├── android_arm64_armv8-a_core_shared
│   │   └── android.hardware.automotive.vehicle@2.0.so
│   ├── android_arm64_armv8-a_core_static
│   │   ├── android.hardware.automotive.vehicle@2.0.a
│   │   └── obj
│   ├── android_arm_armv7-a-neon_cortex-a9_core_shared
│   │   └── android.hardware.automotive.vehicle@2.0.so
│   └── android_arm_armv7-a-neon_cortex-a9_core_static
│       ├── android.hardware.automotive.vehicle@2.0.a
│       └── obj
├── android.hardware.automotive.vehicle@2.0_genc++
│   └── gen
│       └── android
├── android.hardware.automotive.vehicle@2.0_genc++_headers
│   └── gen
│       └── android
├── android.hardware.automotive.vehicle-V2.0-java
│   └── android_common
│       ├── combined
│       ├── javac
│       ├── turbine
│       └── turbine-combined
└── android.hardware.automotive.vehicle-V2.0-java_gen_java
    └── gen
        └── android

hidl-gen

hidl-gen时编译HIDL文件的一个工具

工具路径 out/host/linux-x86/bin/hidl-gen,源代码路径 system/tools/hidl

使用说明

usage: hidl-gen [-p <root path>] -o <output path> -L <language> [-O <owner>] (-r <interface root>)+ [-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-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.
         -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 <package:path root>: E.g., android.hardware:hardware/interfaces.
         -v: verbose output.
         -d <depfile>: location of depfile to write to.

hidl-gen 工具会将hal文件转换为相应的类文件, 例:

启动环境

. build/envsetup.sh 
lunch

命令

./out/host/linux-x86/bin/hidl-gen -o ./demo -Ljava -randroid.hardware:hardware/interfaces  -randroid.hidl:system/libhidl/transport android.hardware.automotive.vehicle@2.0

说明

-o: 生成文件的目录
-Lc+±headers: 生成c++ 头文件
-Ljava : 生成java文件
-r:指定包号和root路径

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值