Google 致力于为黑人社区推动种族平等。查看具体举措

电源管理

为了支持车辆专用的电源管理,Android 提供了 CarPowerManagementService 服务和 CarPowerManager 接口。

状态转换由车载主控单元 (VMCU) 触发。为了能够与 VMCU 通信,集成器必须实现几个组件。集成器负责与车载硬件抽象层 (VHAL) 和内核实现相集成。集成器还负责停用唤醒源,并确保不会无限期地推迟关闭。

术语

本文档中使用了以下术语:

术语 说明
应用处理器 (AP) 系统芯片 (SoC) 的一部分。
板级支持处理器 (BSP) 产品正常运行所需的所有硬件特定代码。通常由 SoC 供应商和硬件制造商提供。它涵盖设备驱动程序、PMIC 序列代码和 SoC 启动等多项内容。
CarPowerManager (CPM) 公开一个 API 以供应用记录电源状态的更改。
CarPowerManagementService (CPMS) 实现汽车电源状态机、与 VHAL 连接,并执行对 suspend()shutdown() 的最终调用。
CarPowerPolicyDaemon (CPPD) 公开原生进程的 AIDL 接口,以注册电源政策监听器。
通用输入/输出 (GPIO) 用于一般用途的数字信号引脚。
硬件抽象层 (HAL) 一种软件层,所有其他更高级别模块必须与之交互才能使用硬件功能。
休眠 也称为“挂起到磁盘”(S2D/S4)。将 SoC 置于 S4 电源模式(休眠),将 RAM 内容写入非易失性介质(如闪存或磁盘),并且整个系统断电。
介质处理器 (MP) 请参阅系统芯片 (SoC)。
电源管理集成电路 (PMIC) 芯片,用于管理主机系统的电源要求。
系统芯片 (SoC) 运行 AAOS 的主处理器,通常由 Intel、MediaTek、Nvidia、Qualcomm、Renesas 和 Texas Instruments 等制造商提供。
挂起 也称为“挂起到 RAM”(S2R 或 STR)。将 SoC 置于 S3 电源模式,CPU 断电而 RAM 仍通电。
车载 HAL (VHAL) 用于与车载网络连接的 Android API。第 1 级合作伙伴或原始设备制造商 (OEM) 负责编写此模块。车载网络可以使用任何物理层(如 CAN、LIN、MOST 和以太网)。VHAL 可以抽象化处理此车载网络,以使 AAOS 能够与车辆交互。
车载接口处理器 (VIP) 请参见“车载 MCU”。
车载主控单元 (VMCU) 微控制器,可提供车载网络与 SoC 之间的接口。SoC 通过 USB、UART、SPI 和 GPIO 信号与 VMCU 通信。

系统设计

本节将介绍 AAOS 如何表示应用处理器的电源状态以及哪些模块会实现电源管理系统。本材料还将介绍这些模块如何协同工作以及状态转换通常如何发生。

汽车电源状态机

AAOS 使用状态机表示 AP 的电源状态。状态机提供的状态如下所示:

汽车电源状态机

图 1. 汽车电源状态机

最常见的状态转换以蓝色突出显示。以下是各种状态和常见的状态转换:

  • 挂起到 RAM:车辆和 SoC 断电。不执行任何代码。SoC RAM 仍然通电。
  • 等待 VHAL:当司机与车辆互动(例如,通过打开车门进行互动)时,VMCU 会向 SoC 供电。AAOS 从“挂起到 RAM”状态恢复并进入“等待 VHAL”状态,在该状态下,它会等待与 VHAL 进行协调。
  • 开启:VHAL 指示 AAOS 进入“开启”状态。在此状态下,AAOS 完全运行并与司机互动。
  • 关闭准备:当司机结束驾驶后,VHAL 会指示 AAOS 进入“关闭准备”状态。在此状态下,显示屏和音频处于关闭状态,AAOS 不会与司机互动。Android 系统仍在运行,可以自由更新应用和 Android 系统。如有更新,在更新完成后,Android 系统会进入“等待 VHAL 完成”状态。
  • 等待 VHAL 完成:此时,AAOS 会通知 VHAL 它已准备好关闭。VMCU 应将 SoC 置于“深度睡眠”状态,并断开应用处理器的电源。然后,AAOS 将进入“挂起到 RAM”状态,但不执行任何代码。

电源管理模块

电源管理系统由以下模块组成:

模块名称 说明
CarPowerManager Java/C++ API。
CarPowerManagementService 协调电源状态转换。
CarPowerPolicyDaemon 与原生电源政策客户端进行通信。
车载 HAL 连接到 VMCU。
内核 挂起到 RAM/磁盘实现。

内核中实现了深度休眠/休眠功能(将 Android 挂起到 RAM/磁盘)。此功能以位于 /sys/power/state 的特殊文件形式提供给用户空间。AAOS 通过将 memdisk 写入此文件而挂起。

CPMS 与其他服务和 HAL 协调电源状态。CPMS 实现上述状态机,并在发生电源状态转换时向每个观察者发送通知。此服务还使用 VHAL 向硬件发送消息。

CPPD 管理电源政策,直至 CPMS 获得控制权为止。此外,它还会向原生监听器发送电源政策更改通知。

某些属性是在 VHAL 中定义的。为了与 VMCU 通信,CPMS 将读取和写入这些属性。应用可以使用在 CPM 中定义的接口来监控电源状态的更改。应用还能通过此接口注册电源政策监听器。此 API 可通过 Java 进行调用,并且带有 @hide/@System API 注解,这意味着它仅供特权应用使用。这些模块、应用和服务之间的关系如下所示:

电源组件参考图

图 2. 电源组件参考图

消息序列

上一节介绍了组成电源管理系统的模块。本节使用“进入深度睡眠”和“退出深度睡眠”示例来说明模块与应用之间的通信方式:

进入深度睡眠

只有 VMCU 可以启动深度睡眠。启动深度睡眠后,VMCU 会通过 VHAL 向 CPMS 发送通知。CPMS 通过使用由 CPM 提供的新状态 ID 调用 onStateChanged() 方法,将状态更改为“关闭准备”并向所有观察者(监控 CPMS 的应用和服务)广播此状态转换。

CPM 在应用/服务与 CPMS 之间进行协调。在 CPM 的 onStateChanged() 方法中,同步调用应用/服务的 onStateChanged() 方法。大多数应用和服务需要完成其准备后才能从该调用返回。特权服务可以在为 PRE_SHUTDOWN_PREPARESUSPEND_ENTERPOST_SUSPEND_ENTER 返回后,异步继续关闭准备。在此情况下,特权服务应在完成准备时对提供的 CompletablePowerStateChangeFuture 对象调用 complete()。请注意,SHUTDOWN_PREPARE 不允许异步准备。早于 DEEP_SLEEP_ENTRY is sent to the VHAL, the CPMS periodically sends shutdown postpone requests to the VHAL.

When all CPM objects have completed shutdown preparations, the CPMS sends AP_POWER_STATE_REPORT to the VHAL, which then notifies the VMCU that the AP is ready to suspend. The CPMS also calls its suspend method, which suspends the kernel.

The sequence described above is illustrated below:

Enter deep sleep

Figure 3. Enter deep sleep

Programming interfaces provided by CPM

This section describes the Java API provided by the CPM for system applications and services. This API enables the system software to:

  • Monitor power state changes in the AP.
  • Apply power policies.

Use these steps to call the APIs provided by the CPM:

  1. To acquire the CPM instance, call the Car API.
  2. Call the appropriate method on the object created in Step 1.

Creating a CarPowerManager object

To create a CPM object, call the Car object's getCarManager() method. This method is a facade used to create CPM objects. Specify android.car.Car.POWER_SERVICE as an argument to create a CPM object.

Car car = Car.createCar(this);
CarPowerManager powerManager =
  (CarPowerManager) car.getCarManager(android.car.Car.POWER_SERVICE);

CarPowerStateListener and registration

System applications and services can receive power state change notifications by implementing CarPowerManager.CarPowerStateListener. This interface defines one method onStateChanged(), which is a callback function invoked when the power state of CPMS is changed. The following example defines a new anonymous class that implements the interface:

private final CarPowerManager.CarPowerStateListener powerListener =
  new CarPowerManager.CarPowerStateListener () {
    @Override
     public void onStateChanged(int state) {
       Log.i(TAG, "onStateChanged() state = " + state);
     }
};

To instruct this listener object to monitor a power state transition, create a new execution thread and register the listener and this thread to the CPM object:

executor = new ThreadPerTaskExecutor();
powerManager.setListener(powerListener, executor);

When the power state is changed, the onStateChanged() method of the listener object is invoked with a value to represent the new power state. The association between actual value and power state is defined in CarPowerManager and is shown in the following table:

Name Description
STATE_ON Enter the on state. The system is fully operational.
STATE_SHUTDOWN_CANCELLED Shutdown is cancelled and power state is returned to the normal state.
STATE_SHUTDOWN_ENTER Applications are expected to clean up and be ready for shut down.
STATE_POST_SHUTDOWN_ENTER Preparations for shutting down have completed and VMCU is ready for shut down. Enter the shutdown state.
STATE_PRE_SHUTDOWN_PREPARE Shutdown process is requested but CPMS doesn't start the process yet. Display and audio are still on
STATE_SHUTDOWN_PREPARE Garage Mode may run during the period.
STATE_SUSPEND_ENTER Applications are expected to clean up and be ready for suspend-to-RAM.
STATE_POST_SUSPEND_ENTER Preparations for suspend-to-RAM have completed and VMCU is ready for suspend-to-RAM. Enter the suspend state.
STATE_SUSPEND_EXIT Wake up from suspend or resume from a cancelled suspend.
STATE_HIBERNATION_ENTER Applications are expected to clean up and be ready for hibernation.
STATE_POST_HIBERNATION_ENTER Preparations for hibernation have completed and VMCU is ready for hibernation Enter the hibernation state.
STATE_HIBERNATION_EXIT Wake up from hibernation or resume from a cancelled hibernation.
STATE_WAIT_FOR_VHAL The system is starting up, but waiting to establish communication with the VHAL before going to the ON state.

CarPowerStateListener unregistration

To unregister all listener objects registered to CPM, call the clearListener method:

powerManager.clearListener();

System integration on your Android implementation

Integrators are responsible for the following items:

  • Implementing the kernel interface to suspend Android.
  • Implementing the VHAL functions to:
    • Propagate the initiation of suspend or shutdown from the car to Android.
    • Send the shutdown ready message from Android to the car.
    • Initiate shutdown or suspend of Android through the Linux kernel interface.
  • Ensure that all wakesources are disabled when the device is in suspend.
  • Ensure that applications shut down quickly enough so as not to indefinitely postpone the shutdown process.
  • Ensure that BSP turns on/off device components according to the power policy so as not to block suspend/hibernation

Kernel interface: /sys/power/state

AAOS places a device into suspend mode when an application or service writes mem for suspend-to-RAM or disk for suspend-to-disk into a file located at /sys/power/state. The integrator must provide a function that monitors this file and puts Linux into the suspend power state. This function may send a GPIO to the VMCU to notify the VMCU that the device has shut down completely. The Integrator is also responsible for removing any race conditions between VHAL sending the final message to the VMCU and the system going into suspend or shutdown mode.

VHAL responsibility

The VHAL provides an interface between the vehicle network and Android. The VHAL:

  • Propagates the initiation of suspend or shutdown from the car to Android.
  • Sends the shutdown ready message from Android to the car.
  • Initiates the shutdown or suspend of Android via the Linux kernel interface.

When the CPMS informs the VHAL that it is ready to shut down, the VHAL sends the shutdown ready message to the VMCU. Typically, on-chip peripherals such as UART, SPI, and USB transmit the message. Once the message has been sent, the CPMS calls the kernel command to suspend or shutdown the device. Before doing so, the VHAL or BSP may toggle a GPIO to instruct the VMCU that it is safe to remove power from the device.

The VHAL must support the following properties, which control power management via the VHAL:

Name Description
AP_POWER_STATE_REPORT Android reports state transitions to the VMCU with this property, using VehicleApPowerStateReport enum values.
AP_POWER_STATE_REQ The VMCU uses this property to instruct Android to transition to different power states, using VehicleApPowerStateReq enum values.

AP_POWER_STATE_REPORT

Use this property to report Android's current power management state. This property contains two integers:

  • int32Values[0]: VehicleApPowerStateReport enum of the current state.
  • int32Values[1]: Time in milliseconds to postpone or sleep or shutdown. The meaning of this value depends on the first value.

The first value can take one of the following values. VehicleApPowerStateReport.aidl contains more specific descriptions, which are stored in the hardware/interfaces/automotive/vehicle/aidl/android/hardware/automotive/vehicle.

Value name Description Second value
WAIT_FOR_VHAL AP is starting and needs to establish communication with the VHAL.
DEEP_SLEEP_ENTRY AP is entering the deep sleep state. The VMCU should turn the AP back on after the time specified in the second value. Must be set
DEEP_SLEEP_EXIT AP is exiting the deep sleep state.
HIBERNATION_ENTRY AP is entering the hibernation state. The VMCU should turn the AP back on after the time specified in the second value. Must be set
HIBERNATION_EXIT AP is exiting the hibernation state.
SHUTDOWN_POSTPONE Android is not ready to shut down. The VMCU should wait the time specified in the second value before shutting down the AP. Android may request additional postponement by issuing additional SHUTDOWN_POSTPONE reports. Must be set
SHUTDOWN_PREPARE Android is preparing to shut down. Must be set
SHUTDOWN_START AP is ready to shut down. The VMCU should turn the AP back on after the time specified in the second value. (The VMCU is not required to support the timed turn-on feature.) Must be set
SHUTDOWN_CANCELLED Android is ceasing to prepare to shut down and will proceed to WAIT_FOR_VHAL.
ON Android is running normally.

The state can be set autonomously or in response to a request via the VMCU.

AP_POWER_STATE_REQ

This property is sent by the VMCU to transition Android into a different power state and contains two integers:

  • int32Values[0]: VehicleApPowerStateReq enum value, which represents the new state into which to transition.
  • int32Values[1]: VehicleApPowerStateShutdownParam enum value. This value is sent only for a SHUTDOWN_PREPARE message and transmits to Android the options it contains.

The first integer value represents the new state into which Android is to transit. The semantics are defined in VehicleApPowerStateReq.aidl and provided below:

Value name Description
ON AP should begin full operation.
SHUTDOWN_PREPARE The AP should prepare to shut down. The second value indicates if the AP is allowed to postpone shutting down and whether the AP should expect to power off or enter deep sleep.
CANCEL_SHUTDOWN The AP should stop preparing to shut down and prepare to go ON.
FINISHED The AP will now be shut down or suspended.

VehicleApPowerStateShutdownParam is defined in VehicleApPowerStateShutdownParam.aidl. This enum has these elements:

Value name Description
CAN_SLEEP AP can enter deep sleep instead of shutting down completely. Postponing is allowed.
CAN_HIBERNATE AP can enter hibernation instead of shutting down completely. Postponing is allowed.
SHUTDOWN_ONLY AP should shut down. Postponing is allowed. Deep sleep is not allowed.
SLEEP_IMMEDIATELY AP may enter deep sleep, but must either sleep or shut down immediately. Postponing is not allowed.
HIBERNATE_IMMEDIATELY AP may enter suspend-to-disk, but must either hibernate or shut down immediately. Postponing is not allowed.
SHUTDOWN_IMMEDIATELY AP must shut down immediately. Postponing is not allowed. Deep sleep is not allowed.

Wake sources

The Integrator must disable the appropriate wake sources when the device is in suspend mode. Common wake sources include heartbeats, modem, Wi-Fi, and Bluetooth. The only valid wake source must be an interrupt from the VMCU to wake up the SoC. This assumes that the VMCU can listen to the modem for remote wakeup events (such as remote engine start). If this functionality is pushed to the AP, then another wake source to service the modem must be added.

Applications

OEMs must be careful to write applications so that they can be shut down quickly and not postpone the process indefinitely.

Appendix

Directories in the source code tree

Content Directory
CarPowerManager-related code. packages/services/Car/car-lib/src/android/car/hardware/power
CarPowerManagementService and so on. packages/services/Car/service/src/com/android/car/power
Services dealing with the VHAL, such as VehicleHal and HAlClient. packages/services/Car/service/src/com/android/car/hal
VHAL interface and property definitions. hardware/interfaces/automotive/vehicle/aidl/android/hardware/automotive/vehicle/
Sample app to provide some idea about the CarPowerManager packages/services/Car/tests/EmbeddedKitchenSinkApp/src/com/google/android/car/kitchensink

Class diagram

This class diagram displays the Java classes and interfaces in the power management system:

Power class diagram

Figure 5. Power class diagram

Object relationship

The following graph illustrates which objects have references to other objects. An edge means that the source object holds a reference to the target object. For example, VehicleHAL has a reference to a PropertyHalService object.

Object reference diagram

Figure 6. Object reference diagram