AndroidAutomotive模块介绍(四)VehicleHal介绍

[复制链接]
发表于 2024-8-19 06:12:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
前言

前面的文章中,描述了 Android Automotive 的框架中应用、Framework 层服务等知识,本篇文章将会继续按照 Android Automotive 框架介绍 Vehicle Hal 层服务的内容。
上一篇:AndroidAutomotive模块介绍(三)CarService服务
正文

1、VehicleHal 介绍

本篇文档将对 Andorid Automotive 框架中 VehicleHal 层睁开介绍。VehicleHal 即为车辆硬件抽象层的定义。可以明白为 Android Automotive OS 中的硬件抽象层接口,包罗车辆属性和方法;各厂商制造商会根据定义的 Hal 接口,实现定制化的模块服务。
VehicleHal 是链接 Android Automotive Car Services 与制造商实现车辆控制服务进程的桥梁,通过标准化接口对上层提供服务,对于服务的实现依赖厂商的定制化,可以忽略汽车制造商的详细实现,也就是说 CarService 调用 VehicleHal 定义的标准接口,厂商实现这些接口。
2、VehicleHal 模块功能

2.1 rc 文件

VehicleHal 的代码路径为:android/hardware/interfaces/automotive/vehicle/2.0/default。路径下有 android.hardware.automotive.vehicle@2.0-service.rc 文件,在开机阶段通过此 rc 文件,将 VehicleHal 进程启动,下面来看下 android.hardware.automotive.vehicle@2.0-service.rc 文件的定义
  1. service vendor.vehicle-hal-2.0 /vendor/bin/hw/android.hardware.automotive.vehicle@2.0-service
  2.     class hal
  3.     user vehicle_network
  4.     group system inet
复制代码
2.2 编译

对于 VehicleHal 的编译,可以对以下两个 Android.bp 文件介绍。


  • Hidl 接口编译
  • VehicleHal 服务编译
VehicleHal 定义的 Hal 接口编译介绍,Android.bp 文件路径为:android/hardware/interfaces/automotive/vehicle/2.0,目录结构如下:
  1. ubuntu:android/hardware/interfaces/automotive/vehicle/2.0$ ls -l
  2. total 132
  3. -rw-r--r-- 1   users    375 Nov  9  2022 Android.bp
  4. drwxrwxr-x 5   users   4096 Mar 22 13:55 default
  5. -rw-r--r-- 1   users   2336 Nov  9  2022 IVehicleCallback.hal
  6. -rw-r--r-- 1   users   3665 Nov  9  2022 IVehicle.hal
  7. -rw-r--r-- 1   users 115184 Nov  9  2022 types.hal
复制代码
下面是 Android.bp 文件的定义:
  1. // This file is autogenerated by hidl-gen -Landroidbp.
  2. hidl_interface {
  3.     name: "android.hardware.automotive.vehicle@2.0",
  4.     root: "android.hardware",
  5.     vndk: {
  6.         enabled: true,
  7.     },
  8.     srcs: [
  9.         "types.hal",
  10.         "IVehicle.hal",
  11.         "IVehicleCallback.hal",
  12.     ],
  13.     interfaces: [
  14.         "android.hidl.base@1.0",
  15.     ],
  16.     gen_java: true,
  17. }
复制代码
编译文件中定义了 android.hardware.automotive.vehicle@2.0 HIDL 接口编译,包罗三个文件 types.hal、IVehicle.hal、IVehicleCallback.hal 文件。
VehicleHal 服务编译介绍,Android.bp 文件路径为:android/hardware/interfaces/automotive/vehicle/2.0/default,目录结构如下:
  1. ubuntu16:android/hardware/interfaces/automotive/vehicle/2.0/default$ ls -l
  2. total 28
  3. -rw-r--r-- 1   users 4705 Nov  9  2022 Android.bp
  4. -rw-r--r-- 1   users  155 Nov  9  2022 android.hardware.automotive.vehicle@2.0-service.rc
  5. drwxrwxr-x 4   users 4096 Jun  7  2022 common
  6. drwxrwxr-x 3   users 4096 Jun  7  2022 impl
  7. drwxrwxr-x 2   users 4096 Nov  9  2022 tests
  8. -rw-r--r-- 1   users 1953 Nov  9  2022 VehicleService.cpp
复制代码
Android.bp 文件定义如下:
  1. // Vehicle reference implementation lib
  2. cc_library {
  3.     name: "android.hardware.automotive.vehicle@2.0-manager-lib",
  4.     vendor: true,
  5.     defaults: ["vhal_v2_0_defaults"],
  6.     srcs: [
  7.         "common/src/Obd2SensorStore.cpp",
  8.         "common/src/SubscriptionManager.cpp",
  9.         "common/src/VehicleHalManager.cpp",
  10.         "common/src/VehicleObjectPool.cpp",
  11.         "common/src/VehiclePropertyStore.cpp",
  12.         "common/src/VehicleUtils.cpp",
  13.         "common/src/VmsUtils.cpp",
  14.     ],
  15.     local_include_dirs: ["common/include/vhal_v2_0"],
  16.     export_include_dirs: ["common/include"],
  17. }
  18. // Vehicle default VehicleHAL implementation
  19. cc_library_static {
  20.     name: "android.hardware.automotive.vehicle@2.0-default-impl-lib",
  21.     vendor: true,
  22.     cflags: ["-Wno-unused-parameter", "-Wno-sign-compare"],
  23.     defaults: ["vhal_v2_0_defaults"],
  24.     srcs: [
  25.         "impl/vhal_v2_0/CommConn.cpp",
  26.         "impl/vhal_v2_0/EmulatedVehicleHal.cpp",
  27.         "impl/vhal_v2_0/VehicleEmulator.cpp",
  28.         "impl/vhal_v2_0/PipeComm.cpp",
  29.         "impl/vhal_v2_0/SocketComm.cpp",
  30.         "impl/vhal_v2_0/LinearFakeValueGenerator.cpp",
  31.         "impl/vhal_v2_0/JsonFakeValueGenerator.cpp",
  32.     ],
  33.     local_include_dirs: ["common/include/vhal_v2_0"],
  34.     export_include_dirs: ["impl"],
  35.     whole_static_libs: ["android.hardware.automotive.vehicle@2.0-manager-lib"],
  36.     shared_libs: [
  37.         "libbase",
  38.         "libprotobuf-cpp-lite",
  39.     ],
  40.     static_libs: [
  41.         "libjsoncpp",
  42.         "libqemu_pipe",
  43.         "android.hardware.automotive.vehicle@2.0-libproto-native",
  44.     ],
  45. }
  46. cc_binary {
  47.     name: "android.hardware.automotive.vehicle@2.0-service",
  48.     defaults: ["vhal_v2_0_defaults"],
  49.     init_rc: ["android.hardware.automotive.vehicle@2.0-service.rc"],
  50.     vendor: true,
  51.     relative_install_path: "hw",
  52.     srcs: ["VehicleService.cpp"],
  53.     shared_libs: [
  54.         "libbase",
  55.         "libprotobuf-cpp-lite",
  56.     ],
  57.     static_libs: [
  58.         "android.hardware.automotive.vehicle@2.0-manager-lib",
  59.         "android.hardware.automotive.vehicle@2.0-default-impl-lib",
  60.         "android.hardware.automotive.vehicle@2.0-libproto-native",
  61.         "libjsoncpp",
  62.         "libqemu_pipe",
  63.     ],
  64. }
复制代码
从 Android.bp 的定义中,VehicleHal 的服务名为 android.hardware.automotive.vehicle@2.0-service,启动 rc 文件是 android.hardware.automotive.vehicle@2.0-service.rc,入口文件是 VehicleService.cpp,依赖自定义干系模块重要有 android.hardware.automotive.vehicle@2.0-manager-lib、android.hardware.automotive.vehicle@2.0-default-impl-lib、android.hardware.automotive.vehicle@2.0-libproto-native。
android.hardware.automotive.vehicle@2.0-manager-lib 模块是 common 目录下的文件所编译的内容,重要是 VehicleHal 以是来的模块。
android.hardware.automotive.vehicle@2.0-default-impl-lib 模块是 impl 目录下的文件所编译的内容,重要是 VehicleHal 的功能实现模块。
android.hardware.automotive.vehicle@2.0-libproto-native 模块是 impl/vhal_v2_0/proto 目录下的文件所编译的内容,目录下的文件为 VehicleHalProto.proto 文件,是一种序列化存储数据的方式。
2.3 HIDL 接口

Android Automotive 框架中 Hal 层接口文件有三个,IVehicle.hal、IVehicleCallback.hal 和 types.hal 文件。IVehicle.hal 文件中定义了 Vehicle Hal 对外提供的接口;IVehicleCallback.hal 文件中定义了 VehicleHal 的回调接口;types.hal 文件定义了 Vehicle Hal 的属性定义。
Hidl 文件路径为:android/hardware/interfaces/automotive/vehicle/2.0。
2.3.1 IVehicle.hal

IVehicle.hal 文件中定义了 Vehicle Hal 对外提供的接口,上层 CarService 通过 IVehicle.hal 中定义的接口与 Vehicle Hal 通讯。
  1. package android.hardware.automotive.vehicle@2.0;
  2. import IVehicleCallback;
  3. interface IVehicle {
  4.   /**
  5.    * Returns a list of all property configurations supported by this vehicle
  6.    * HAL.
  7.    * 返回此车辆HAL支持的所有属性配置的列表
  8.    */
  9.   getAllPropConfigs() generates (vec<VehiclePropConfig> propConfigs);
  10.   /**
  11.    * Returns a list of property configurations for given properties.
  12.    *
  13.    * If requested VehicleProperty wasn't found it must return
  14.    * StatusCode::INVALID_ARG, otherwise a list of vehicle property
  15.    * configurations with StatusCode::OK
  16.    * 返回给定属性的属性配置列表。
  17.    * 如果请求的车辆属性没有找到,它必须返回StatusCode::INVALID_ARG,否则返回一个车辆属性配置列表,StatusCode::OK
  18.    */
  19.   getPropConfigs(vec<int32_t> props)
  20.           generates (StatusCode status, vec<VehiclePropConfig> propConfigs);
  21.   /**
  22.    * Get a vehicle property value.
  23.    *
  24.    * For VehiclePropertyChangeMode::STATIC properties, this method must always
  25.    * return the same value always.
  26.    * For VehiclePropertyChangeMode::ON_CHANGE properties, it must return the
  27.    * latest available value.
  28.    *
  29.    * Some properties like RADIO_PRESET requires to pass additional data in
  30.    * GET request in VehiclePropValue object.
  31.    *
  32.    * If there is no data available yet, which can happen during initial stage,
  33.    * this call must return immediately with an error code of
  34.    * StatusCode::TRY_AGAIN.
  35.    * 获取车辆属性值。
  36.    * 对于 VehiclePropertyChangeMode::STATIC 的属性值,此方法会返回同一个值,不会改变。
  37.    * 对于 VehiclePropertyChangeMode::ON_CHANGE 的属性值,此方法会返回最新值。
  38.    */
  39.   get(VehiclePropValue requestedPropValue)
  40.           generates (StatusCode status, VehiclePropValue propValue);
  41.   /**
  42.    * Set a vehicle property value.
  43.    *
  44.    * Timestamp of data must be ignored for set operation.
  45.    *
  46.    * Setting some properties require having initial state available. If initial
  47.    * data is not available yet this call must return StatusCode::TRY_AGAIN.
  48.    * For a property with separate power control this call must return
  49.    * StatusCode::NOT_AVAILABLE error if property is not powered on.
  50.    * 设置车辆属性值。
  51.    * 特殊场景:
  52.    * 设置一些属性值需要其初始状态可用,如果初始状态不可用,需要返回 StatusCode::TRY_AGAIN。
  53.    * 设置单独电源控制的属性,如果属性未上电,需要返回 StatusCode::TRY_AGAIN。
  54.    */
  55.   set(VehiclePropValue propValue) generates (StatusCode status);
  56.   /**
  57.    * Subscribes to property events.
  58.    *
  59.    * Clients must be able to subscribe to multiple properties at a time
  60.    * depending on data provided in options argument.
  61.    *
  62.    * @param listener This client must be called on appropriate event.
  63.    * @param options List of options to subscribe. SubscribeOption contains
  64.    *                information such as property Id, area Id, sample rate, etc.
  65.    * 订阅属性
  66.    * 客户端可以一次订阅多个属性,这取决于 options 定义的参数。
  67.    * 参数 SubscribeOptions 包含 属性id、区域id、采样率 等信息。
  68.    */
  69.   subscribe(IVehicleCallback callback, vec<SubscribeOptions> options)
  70.           generates (StatusCode status);
  71.   /**
  72.    * Unsubscribes from property events.
  73.    *
  74.    * If this client wasn't subscribed to the given property, this method
  75.    * must return StatusCode::INVALID_ARG.
  76.    * 取消订阅属性
  77.    * 如果客户端在此前没有对此属性进行订阅,则返回 StatusCode::INVALID_ARG。
  78.    */
  79.   unsubscribe(IVehicleCallback callback, int32_t propId)
  80.           generates (StatusCode status);
  81.   /**
  82.    * Print out debugging state for the vehicle hal.
  83.    *
  84.    * The text must be in ASCII encoding only.
  85.    *
  86.    * Performance requirements:
  87.    *
  88.    * The HAL must return from this call in less than 10ms. This call must avoid
  89.    * deadlocks, as it may be called at any point of operation. Any synchronization
  90.    * primitives used (such as mutex locks or semaphores) must be acquired
  91.    * with a timeout.
  92.    *
  93.    * 打印车辆的调试状态。
  94.    */
  95.   debugDump() generates (string s);
  96. };
复制代码
2.3.2 IVehicleCallback.hal

IVehicleCallback.hal 文件定义了 VehicleHal 的回调对象,上层 CarService 通过注册此回调对象以监听属性是否改变。VehicleHal 通过回调对象返回状态给到客户端。
  1. package android.hardware.automotive.vehicle@2.0;
  2. interface IVehicleCallback {
  3.     /**
  4.      * Event callback happens whenever a variable that the API user has
  5.      * subscribed to needs to be reported. This may be based purely on
  6.      * threshold and frequency (a regular subscription, see subscribe call's
  7.      * arguments) or when the IVehicle#set method was called and the actual
  8.      * change needs to be reported.
  9.      *
  10.      * These callbacks are chunked.
  11.      *
  12.      * @param values that has been updated.
  13.      * 当需要报告客户端订阅的变量时,就会调用此回调函数。
  14.      * 这可能基于常规按频率上报或者客户端调用 IVehicle#set 函数时调用。
  15.      */
  16.     oneway onPropertyEvent(vec<VehiclePropValue> propValues);
  17.     /**
  18.      * This method gets called if the client was subscribed to a property using
  19.      * SubscribeFlags::EVENTS_FROM_ANDROID flag and IVehicle#set(...) method was called.
  20.      *
  21.      * These events must be delivered to subscriber immediately without any
  22.      * batching.
  23.      *
  24.      * @param value Value that was set by a client.
  25.      * 如果客户端使用 SubscribeFlags::EVENTS_FROM_ANDROID 标志订阅属性,并且调用 IVehicle#set 函数,则回调此方法。
  26.      */
  27.     oneway onPropertySet(VehiclePropValue propValue);
  28.     /**
  29.      * Set property value is usually asynchronous operation. Thus even if
  30.      * client received StatusCode::OK from the IVehicle::set(...) this
  31.      * doesn't guarantee that the value was successfully propagated to the
  32.      * vehicle network. If such rare event occurs this method must be called.
  33.      *
  34.      * @param errorCode - any value from StatusCode enum.
  35.      * @param property - a property where error has happened.
  36.      * @param areaId - bitmask that specifies in which areas the problem has
  37.      *                 occurred, must be 0 for global properties
  38.      * 设置属性通常是异步操作,客户端调用 IVehicle#set 函数到接收到 StatusCode::OK 的返回值,也不能保证此属性成功传播到车辆网络,如果发生这种低概率的事件,则回调此方法。
  39.      * @ errorCode:StatusCode 枚举中的任何值。
  40.      * @ property:发生错误的属性。
  41.      * @ areaId:指定问题发生在哪个区域的位掩码,对于全局属性必须为 0。
  42.      */
  43.     oneway onPropertySetError(StatusCode errorCode,
  44.                               int32_t propId,
  45.                               int32_t areaId);
  46. };
复制代码
2.3.3 types.hal

types.hal 中定义了 VehicleHal 的属性、结构等数据。
2.3.3.1 车辆属性 VehiclePropertyType

下面列举 types.hal 中定义的 VehiclePropertyType 属性:
  1. package android.hardware.automotive.vehicle@2.0;
  2. /**
  3. * Enumerates supported data type for VehicleProperty.
  4. * 枚举 VehicleProperty 支持的数据类型。
  5. * Used to create property ID in VehicleProperty enum.
  6. * 用于在 VehicleProperty enum 中创建属性 ID。
  7. */
  8. enum VehiclePropertyType : int32_t { // VehicleProperty 的类型是 string、bool、还是 int32 等等
  9.     STRING          = 0x00100000,
  10.     BOOLEAN         = 0x00200000,
  11.     INT32           = 0x00400000,
  12.     INT32_VEC       = 0x00410000,
  13.     INT64           = 0x00500000,
  14.     INT64_VEC       = 0x00510000,
  15.     FLOAT           = 0x00600000,
  16.     FLOAT_VEC       = 0x00610000,
  17.     BYTES           = 0x00700000,
  18.     /**
  19.      * Any combination of scalar or vector types. The exact format must be
  20.      * provided in the description of the property.
  21.      */
  22.     MIXED           = 0x00e00000, // 这两个主要就是 VehicleProperty 用来进行或运算的。
  23.     MASK            = 0x00ff0000
  24. };
复制代码
2.3.3.2 车辆区域属性 VehicleArea

下面是 types.hal 中定义的车辆区域属性:
  1. /**
  2. * Vehicle Areas
  3. * Used to construct property IDs in the VehicleProperty enum.
  4. * 用于在VehicleProperty enum中构造属性id。
  5. *
  6. * Some properties may be associated with particular vehicle areas. For
  7. * example, VehicleProperty:DOOR_LOCK property must be associated with
  8. * particular door, thus this property must be marked with
  9. * VehicleArea:DOOR flag.
  10. * 某些属性可能与特定车辆区域相关。例如,VehicleProperty:DOOR_LOCK 属性必须与特定的门相关联,因此该属性必须标记为 VehicleArea: door 标志。
  11. *
  12. * Other properties may not be associated with particular vehicle area,
  13. * these kind of properties must have VehicleArea:GLOBAL flag.
  14. * 其他属性可能不与特定的车辆区域相关联,这些属性必须具有VehicleArea:GLOBAL标志。
  15. *
  16. * [Definition] Area: An area represents a unique element of an AreaType.
  17. *   For instance, if AreaType is WINDOW, then an area may be FRONT_WINDSHIELD.
  18. *
  19. * [Definition] AreaID: An AreaID is a combination of one or more areas,
  20. *   and is represented using a bitmask of Area enums. Different AreaTypes may
  21. *   not be mixed in a single AreaID. For instance, a window area cannot be
  22. *   combined with a seat area in an AreaID.
  23. *
  24. * Rules for mapping a zoned property to AreaIDs:
  25. *  - A property must be mapped to an array of AreaIDs that are impacted when
  26. *    the property value changes.
  27. *  - Each element in the array must represent an AreaID, in which, the
  28. *    property value can only be changed together in all the areas within
  29. *    an AreaID and never independently. That is, when the property value
  30. *    changes in one of the areas in an AreaID in the array, then it must
  31. *    automatically change in all other areas in the AreaID.
  32. *  - The property value must be independently controllable in any two
  33. *    different AreaIDs in the array.
  34. *  - An area must only appear once in the array of AreaIDs. That is, an
  35. *    area must only be part of a single AreaID in the array.
  36. *
  37. * [Definition] Global Property: A property that applies to the entire car
  38. *   and is not associated with a specific area. For example, FUEL_LEVEL,
  39. *   HVAC_STEERING_WHEEL_HEAT.
  40. *
  41. * Rules for mapping a global property to AreaIDs:
  42. *  - A global property must not be mapped to AreaIDs.
  43. */
  44. enum VehicleArea : int32_t {
  45.     GLOBAL      = 0x01000000, // 全局区域
  46.     /** WINDOW maps to enum VehicleAreaWindow */
  47.     WINDOW      = 0x03000000, // 窗户区域
  48.     /** MIRROR maps to enum VehicleAreaMirror */
  49.     MIRROR      = 0x04000000, // 反光镜区域
  50.     /** SEAT maps to enum VehicleAreaSeat */
  51.     SEAT        = 0x05000000, // 座椅区域
  52.     /** DOOR maps to enum VehicleAreaDoor */
  53.     DOOR        = 0x06000000, // 车门区域
  54.     /** WHEEL maps to enum VehicleAreaWheel */
  55.     WHEEL       = 0x07000000, // 车轮区域
  56.     YFVehicleAreaTire = 0x08000000,
  57.     MASK        = 0x0f000000,
  58. };
复制代码
2.3.3.3 车辆属性分组范例 VehiclePropertyGroup

下面是 types.hal 中定义的车辆属性分组范例:
  1. /**
  2. * Enumerates property groups.
  3. * 车辆属性分组枚举
  4. *
  5. * Used to create property ID in VehicleProperty enum.
  6. * 用于在VehicleProperty enum中创建属性ID。
  7. */
  8. enum VehiclePropertyGroup : int32_t { // AndroidO 之后的 treble,用于区分 android 原生的 property 和厂商的 property。
  9.     /**
  10.      * Properties declared in AOSP must use this flag.
  11.      * AOSP 原生的属性标志
  12.      */
  13.     SYSTEM      = 0x10000000,
  14.     /**
  15.      * Properties declared by vendors must use this flag.
  16.      * 厂商自定义的属性标志
  17.      */
  18.     VENDOR      = 0x20000000,
  19.     MASK        = 0xf0000000,
  20. };
复制代码
2.3.3.4 车辆属性 VehicleProperty

下面是 types.hal 中定义的车辆属性
  1. /**
  2. * Declares all vehicle properties. VehicleProperty has a bitwise structure.
  3. * Each property must have:
  4. *  - a unique id from range 0x0100 - 0xffff
  5. *  - associated data type using VehiclePropertyType
  6. *  - propert
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表