1 模块图
Android Car Framework,可以简单总结为三大模块:CAR API、CAR SERVICE、VEHICLE HAL。
1.1 CAR API
CAR API,提供给APPS使用的接口,包括java、aidl和c++,打包为java库android.car和c++库libpowermanager。 CAR API,主要是car lib,另外还有一些其它lib。 代码:
android/packages/services/Car/car-lib/
1.2 CAR SERVICE
CAR SERVICE,java服务,实现了部分CAR API提供的aidl接口,与底层的VEHICLE HAL通过hidl接口交互。 代码:
android/packages/services/Car/service/
1.3 VEHICLE HAL
VEHICLE HAL,c++服务,实现了hidl接口,用于定义OEM可以实现的车辆属性接口,目前有个vehicle 2.0参考实现。 代码:
android/hardware/interfaces/automotive/vehicle/2.0/
2 CarService目录结构
android/packages/services/Car // 代码仓库位置
|-- FrameworkPackageStubs // Stubs
|-- car-admin-ui-lib // This library contains UI elements used to show information about managed devices or users
|-- car-internal-lib // internal lib
|-- car-lib // CAR API核心代码
|-- api // api text
|-- native // native api
|-- CarPowerManager // power .cpp
|-- include // power .h
|-- src/android/car // api源码
|-- admin
|-- annotation
|-- app
|-- cluster
|-- renderer
|-- content/pm
|-- diagnostic
|-- drivingstate
|-- evs
|-- hardware
|-- cabin
|-- hvac
|-- power
|-- property
|-- input
|-- media
|-- navigation
|-- occupanntawareness
|-- projection
|-- settings
|-- storagemonitoring
|-- telemetry
|-- test
|-- user
|-- util/concurrent
|-- vms
|-- watchdog
|-- src/com/android/car/internal // internal api源码
|-- common
|-- car-maps-placeholder // map相关
|-- car-systemtest-lib // 测试相关
|-- car-test-lib // 测试相关
|-- car-usb-handle // usb相关
|-- car_product // 定制car产品
|-- bootanimations
|-- build
|-- car_ui_portrait
|-- init
|-- occupant_awareness
|-- overlay
|-- overlay-visual
|-- rro
|-- sepolicy
|-- cpp // c++代码
|-- bugreport
|-- computepipe
|-- evs
|-- libsysfsmonitor
|-- powerpolicy
|-- security
|-- telemetry
|-- watchdog
|-- data // 资源
|-- experimental // 非正式模块
|-- odb2-lib // odb相关
|-- packages // 一些app
|-- procfs-inspector // proc fs相关
|-- service // CAR SERVICE核心代码
|-- jni // jni相关
|-- proto // proto相关
|-- res // 资源相关
|-- src/com/android/car // CarService代码
|-- admin
|-- am
|-- audio
|-- hal
|-- cluster
|-- evs
|-- garagemode
|-- hal
|-- pm
|-- blurredbackground
|-- power
|-- stats
|-- storagemonitoring
|-- systeminterface
|-- telemetry
|-- databroker
|-- proto
|-- publisher
|-- net
|-- statsconverters
|-- scriptexecutorinterface
|-- sessioncontroller
|-- systemmonitor
|-- util
|-- user
|-- util
|-- vms
|-- watchdog
|-- tests // 测试代码
|-- tools // 一些工具
|-- user // android.car.userlib
|-- vehicle-hal-support-lib // vehicle hal相关
3 CarService启动流程
3.1 启动CarServiceHelperService
SystemServer启动过程中,在ActivityManagerService systemReady时,启动CarServiceHelperService。 代码:
android/frameworks/base/services/java/com/android/server/SystemServer.java
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
t.traceBegin("StartCarServiceHelperService");
final SystemService cshs = mSystemServiceManager
.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);
if (cshs instanceof Dumpable) {
mDumper.addDumpable((Dumpable) cshs);
}
if (cshs instanceof DevicePolicySafetyChecker) {
dpms.setDevicePolicySafetyChecker((DevicePolicySafetyChecker) cshs);
}
t.traceEnd();
}
private static final String CAR_SERVICE_HELPER_SERVICE_CLASS =
"com.android.internal.car.CarServiceHelperService";
3.2 启动CarService
CarServiceHelperService在onStart时拉起CarServcie,加载Native库car-framework-service-jni。 代码:
android/frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java
public void onStart() {
EventLog.writeEvent(EventLogTags.CAR_HELPER_START);
IntentFilter filter = new IntentFilter(Intent.ACTION_REBOOT);
filter.addAction(Intent.ACTION_SHUTDOWN);
mContext.registerReceiverForAllUsers(mShutdownEventReceiver, filter, null, null);
mCarWatchdogDaemonHelper.addOnConnectionChangeListener(mConnectionListener);
mCarWatchdogDaemonHelper.connect();
Intent intent = new Intent();
intent.setPackage("com.android.car");
intent.setAction(CAR_SERVICE_INTERFACE);
if (!mContext.bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
mHandler, UserHandle.SYSTEM)) {
Slogf.wtf(TAG, "cannot start car service");
}
loadNativeLibrary();
}
void loadNativeLibrary() {
System.loadLibrary("car-framework-service-jni");
}
代码:
android/packages/services/Car/car-lib/src/com/android/car/internal/common/CommonConstants.java
public static final String CAR_SERVICE_INTERFACE = "android.car.ICar";
代码:
android/packages/services/Car/service/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
package="com.android.car"
coreApp="true"
android:sharedUserId="android.uid.system">
<application android:label="@string/app_title"
android:directBootAware="true"
android:allowBackup="false"
android:persistent="true">
<service android:name=".CarService"
android:singleUser="true"
android:exported="true">
<intent-filter>
<action android:name="android.car.ICar"/>
</intent-filter>
</service>
3.3 初始化ICarImpl
CarService在onCreate时先判断Vehicle HAL起来了,然后再初始化ICarImpl。 代码:
android/packages/services/Car/service/src/com/android/car/CarService.java
public void onCreate() {
LimitedTimingsTraceLog initTiming = new LimitedTimingsTraceLog(CAR_SERVICE_INIT_TIMING_TAG,
Trace.TRACE_TAG_SYSTEM_SERVER, CAR_SERVICE_INIT_TIMING_MIN_DURATION_MS);
initTiming.traceBegin("CarService.onCreate");
initTiming.traceBegin("getVehicle");
mVehicle = getVehicle();
initTiming.traceEnd();
EventLog.writeEvent(EventLogTags.CAR_SERVICE_CREATE, mVehicle == null ? 0 : 1);
if (mVehicle == null) {
throw new IllegalStateException("Vehicle HAL service is not available.");
}
try {
mVehicleInterfaceName = mVehicle.interfaceDescriptor();
} catch (RemoteException e) {
throw new IllegalStateException("Unable to get Vehicle HAL interface descriptor", e);
}
Slog.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName);
EventLog.writeEvent(EventLogTags.CAR_SERVICE_CONNECTED, mVehicleInterfaceName);
mICarImpl = new ICarImpl(this,
mVehicle,
SystemInterface.Builder.defaultSystemInterface(this).build(),
mVehicleInterfaceName);
mICarImpl.init();
linkToDeath(mVehicle, mVehicleDeathRecipient);
ServiceManager.addService("car_service", mICarImpl);
SystemProperties.set("boot.car_service_created", "1");
super.onCreate();
initTiming.traceEnd(); // "CarService.onCreate"
}
private static IVehicle getVehicle() {
final String instanceName = SystemProperties.get("ro.vehicle.hal", "default");
try {
return android.hardware.automotive.vehicle.V2_0.IVehicle.getService(instanceName);
} catch (RemoteException e) {
Slog.e(CarLog.TAG_SERVICE, "Failed to get IVehicle/" + instanceName + " service", e);
} catch (NoSuchElementException e) {
Slog.e(CarLog.TAG_SERVICE, "IVehicle/" + instanceName + " service not registered yet");
}
return null;
}
代码:
android/packages/services/Car/car-lib/src/android/car/ICar.aidl
android/packages/services/Car/service/src/com/android/car/ICarImpl.java
3.4 初始化各个Car Services组件
ICarImpl构造函数中,构造了下面几个实现了CarServiceBase接口的Car Services组件,以及VehicleHal和CarStatsService;然后通过init函数进行初始化。
CarFeatureController
CarPropertyService
CarDrivingStateService
CarUxRestrictionsManagerService
CarUserService
CarOccupantZoneService
SystemActivityMonitoringService
CarPowerManagementService
CarUserNoticeService
CarPackageManagerService
PerUserCarServiceHelper
CarBluetoothService
CarInputService
CarProjectionService
GarageModeService
AppFocusService
CarAudioService
CarNightService
FixedActivityService
ClusterNavigationService
InstrumentClusterService
VmsBrokerService
CarDiagnosticService
CarStorageMonitoringService
CarLocationService
CarMediaService
CarBugreportManagerService
CarExperimentalFeatureServiceController
CarWatchdogService
CarDevicePolicyService
ClusterHomeService
CarEvsService
CarTelemetryService
CarActivityService
3.5 初始化各个Hal Services组件
VehicleHal构造函数中,构造了下面几个继承自HalServiceBase基类的Hal Services组件以及HalClient;然后通过init函数进行初始化。
PowerHalService
PropertyHalService
InputHalService
VmsHalService
UserHalService
DiagnosticHalService
ClusterHalService
EvsHalService
TimeHalService
代码:
android/packages/services/Car/service/src/com/android/car/hal/VehicleHal.java
4 VehicleHAL目录结构
下面是vehicle 2.0参考实现的目录结构。
android/hardware/interfaces/automotive/vehicle/2.0/
|-- default // 默认实现
|-- common
|-- include
|-- vhal_v2_0 // .h
|-- src // .cpp
|-- impl
|-- vhal_v2_0 // impl代码
|-- proto // proto协议
|-- tests
|-- tests
|-- Android.bp // 编译文件
|-- VehicleService.cpp // main函数
|-- android.hardware.automotive.vehicle@2.0-service.rc // init rc
|-- android.hardware.automotive.vehicle@2.0-service.xml // matrix xml
|-- utils
|-- vts
|-- Android.bp // 编译文件
|-- IVehicle.hal // interface
|-- IVehicleCallback.hal // callback
|-- types.hal // types
Hidl代码有个明显的namespace规则,如下。
namespace android {
namespace hardware {
namespace automotive {
namespace vehicle {
namespace V2_0 {
// ...
} // namespace V2_0
} // namespace vehicle
} // namespace automotive
} // namespace hardware
} // namespace android
5 VehicleHAL启动流程
5.1 rc
VehicleHAL由init rc启动。 代码:
android/hardware/interfaces/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-service.rc
service vendor.vehicle-hal-2.0 /vendor/bin/hw/android.hardware.automotive.vehicle@2.0-service
class early_hal
user vehicle_network
group system inet
5.2 main
下面是VehicleHAL的main函数。 代码:
android/hardware/interfaces/automotive/vehicle/2.0/default/VehicleService.cpp
int main(int /* argc */, char* /* argv */ []) {
auto store = std::make_unique<VehiclePropertyStore>();
auto connector = std::make_unique<impl::EmulatedVehicleConnector>();
auto userHal = connector->getEmulatedUserHal();
auto hal = std::make_unique<impl::EmulatedVehicleHal>(store.get(), connector.get(), userHal);
auto emulator = std::make_unique<impl::VehicleEmulator>(hal.get());
auto service = std::make_unique<VehicleHalManager>(hal.get());
connector->setValuePool(hal->getValuePool());
configureRpcThreadpool(4, true /* callerWillJoin */);
ALOGI("Registering as service...");
status_t status = service->registerAsService();
if (status != OK) {
ALOGE("Unable to register vehicle service (%d)", status);
return 1;
}
ALOGI("Ready");
joinRpcThreadpool();
return 1;
}
VehiclePropertyStore:存储、访问和修改vehicle相关的配置及属性,VehiclePropertyValues存储在有序map中。使用了同步锁,线程安全。 EmulatedVehicleConnector:VehicleHalClient与VehicleHalServer通信的连接器。 EmulatedUserHal:User Hal。 EmulatedVehicleHal:Vehicle Hal。 VehicleEmulator:Vehicle Emulator。 VehicleHalManager:This class is a thick proxy between IVehicle HIDL interface and vendor’s implementation.
6 交互
CarService向上与App通过CarLib(aidl)交互,向下与VehicleHAL通过hidl交互,其中包括了一个很重要的概念:Property。 Property可以是静态的,也可以是变化后向上通报的,还可以是以一定的频率实时上报的。 下面以HVAC为例,说明HVAC的控制过程。
- 【App】调用CarLib中【CarHvacManager】的setBooleanProperty设置Property。
- 【CarHvacManager】调用CarLib中【CarPropertyManager】的setBooleanProperty。
- 【CarPropertyManager】将Property封装成CarPropertyValue后调用CarServcie中【ICarProperty-CarPropertyService】的setProperty。(aidl)
- 【CarPropertyService】调用CarServcie中【PropertyHalService】的setProperty。
- 【PropertyHalService】将CarPropertyValue封装成VehiclePropValue后调用CarServcie中【VehicleHal】的set。
- 【VehicleHal】调用CarServcie中【HalClient】的setValue。
- 【HalClient】调用VehicleHAL中【IVehicle-VehicleHalManager】的set。(hidl)
- 【VehicleHalManager】,首先,通过订阅这个Property的【Native HalClient】的callback即【IVehicleCallback-HalClient$VehicleCallback】的onPropertySet通知Property变更。(hidl callback)
- 【VehicleHalManager】,然后,调用【EmulatedVehicleHal】的set。
- 【EmulatedVehicleHal】检查Property后调用【VehicleHalClient】的setProperty。
- 【VehicleHalClient】与EmulatedVehicleConnector有关,调用【EmulatedVehicleConnector】的onSetProperty。
- 【EmulatedVehicleConnector】与VehicleHalServer有关,调用【VehicleHalServer】的onSetProperty。
- 最终,将Property或转换后的信号发到【Car ECU】。vehicle 2.0模拟实现是将Property存储到VehiclePropertyStore。
Android Automotive参考: https://source.android.google.cn/docs/devices/automotive
|