当前位置: 首页 > news >正文

万字解读 Android 车机核心 :CarService 的构成和链路~

前言

关于 Android 车机,之前分析过方控上自定义按键的输入机制和中控上旋钮输入的原理,但都局限于 Car Service 内 Input 相关模块。

  • 一文了解 Android 车机如何处理中控的旋钮输入
  • 从实体按键看 Android 车载的自定义事件机制

本文将结合 Android 系统整体,对 CarService 的构成和链路对其做一个全面的分析和理解。

目录

  • 构成
    1. CarServiceHelperService 系统服务
    2. CarService 核心服务 App
      • builtin app
      • updatable app
    3. Car 专用 API
    4. Car Apps
  • CarService 启动链路
    • Service 整体
    • CarInputService 输入服务
    • CarPropertyService 属性服务
  • Vehicle 属性调用链路
    • Car 获取
    • CarPropertyManager 调用
  • 总结

构成

1. CarServiceHelperService 系统服务

SystemServer 中专门为了 Automotive OS 设立的系统服务,用来管理车机的核心服务 CarService。该系统服务的具体实现实际上由 CarServiceHelperServiceUpdatableImpl 类完成,后面会提到。

System service side companion service for CarService. Starts car service and provide necessary API for CarService. Only for car product.

2. CarService 核心服务 App

Car Service App 实际上分为两个,一个是和系统服务直接交互的 builtin app,另一个是给该 built app 提供实际实现的 updatable app。

builtin app

系统中与车相关的核心 App,掌管最重要的 CarService 服务类。目录位于:

  • packages/services/Car/service-builtin/

其 AndroidManifest.xml 文件如下,可以看到它具有系统权限、与 system uid 共享数据。

<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>...</application>            
</manifest>

updatable app

builtin app 的所有具体实现以及后续支持的一堆服务都在 updatable app 中实现,目录见:

  • 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.updatable"coreApp="true"><permission-group android:name="android.car.permission-group.CAR_MONITORING"android:icon="@drawable/perm_group_car"android:description="@string/car_permission_desc"android:label="@string/car_permission_label"/>...<application android:label="@string/app_title"android:directBootAware="true"android:allowBackup="false"></application>
</manifest>

3. Car 专用 API

Android 车机里提供给系统使用汽车相关能力的专用接口,源码实现在:

  • packages/services/Car/car-lib/

看下它的 manifest 文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"package="android.car" ><uses-sdk android:minSdkVersion="23" android:targetSdkVersion="23" />
</manifest>

再看下它的 bp 文件:

...
filegroup {name: "android.car-full-src",srcs: ["src/**/*.java","src/**/*.aidl",],visibility: ["//packages/services/Car/car-lib","//packages/services/Car/car-lib-module",],
}java_library {name: "android.car",srcs: [":android.car-full-src",],aidl: {include_dirs: ["packages/modules/Bluetooth/framework/aidl-export",],},libs: ["android.car.builtin","framework-annotations-lib","modules-utils-preconditions","framework-wifi","framework-bluetooth",],installable: true,sdk_version: "module_current",
}

可以看到它会编译到 android.car.jar 中,而非面向 AOSP 手机/平板的 android.jar 中。这也意味着如果要基于 Car 相关 API 开发,需要通过添加 Automotive os addon 的方式才能导入该 SDK。

这个 jar 囊括了我们常用的 Car、CarPowerManager、CarSettings 等,下面罗列了部分常用的 Car API:

  • android.car:包含了与车相关的基本API。例如:车辆后视镜,门,座位,窗口等

  • menu:车辆应用菜单相关API

  • cluster:仪表盘相关API

  • diagnostic:包含与汽车诊断相关的API。

  • hardware:车辆硬件相关API

  • cabin:座舱相关API

  • hvac:通风空调相关API。(hvac是Heating, ventilation and air conditioning的缩写)

  • property:属性相关API

  • radio:收音机相关API

  • input:输入相关API

  • media:多媒体相关API

  • navigation:导航相关API

  • settings:设置相关API

  • vms:汽车监测相关API

最后的实现会经由 AIDL 抵达上个章节的 CarService App。

4. Car Apps

提供 Automotive OS 内置的、专门为 Car 场景设计的 App,目录位于:

  • packages/apps/Car/

比如面向 Car 的 SystemUI、Launcher、Settings、IME 等。

CarService 启动链路

Service 整体

SystemServer 会在启动系统服务的过程中依据 FEATURE_AUTOMOTIVE 的特性决定是否启动 AAOS 的系统服务 CarServiceHelperService

public final class SystemServer implements Dumpable {...private static final String CAR_SERVICE_HELPER_SERVICE_CLASS ="com.android.internal.car.CarServiceHelperService";private void startOtherServices(@NonNull TimingsTraceAndSlog t) {t.traceBegin("startOtherServices");...mActivityManagerService.systemReady(() -> {Slog.i(TAG, "Making services ready");...boolean isAutomotive = mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);if (isAutomotive) {t.traceBegin("StartCarServiceHelperService");final SystemService cshs = mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);t.traceEnd();}...}, t);...}...
}

CarServiceHelperService 执行 onStart() 的时候,会将后续的工作交给 CarServiceHelperServiceUpdatableImpl 来处理。

其 onStart() 里调用 bindService() 绑定 action 为 “android.car.ICar”、package 为 “com.android.car” 的 Service,即构成章节里提到的 CarService 组件。

public class CarServiceHelperService extends SystemServiceimplements Dumpable, DevicePolicySafetyChecker, CarServiceHelperInterface {...@Overridepublic void onStart() {...mCarServiceHelperServiceUpdatable.onStart();}...
}public final class CommonConstants {...// CarService Constantspublic static final String CAR_SERVICE_INTERFACE = "android.car.ICar";...
}public final class CarServiceHelperServiceUpdatableImplimplements CarServiceHelperServiceUpdatable, Executor {...private static final String CAR_SERVICE_PACKAGE = "com.android.car";@Overridepublic void onStart() {Intent intent = new Intent(CAR_SERVICE_INTERFACE).setPackage(CAR_SERVICE_PACKAGE);Context userContext = mContext.createContextAsUser(UserHandle.SYSTEM, /* flags= */ 0);if (!userContext.bindService(intent, Context.BIND_AUTO_CREATE, this,mCarServiceConnection)) {Slogf.wtf(TAG, "cannot start car service");}}...
}

CarService 的实现都在父类 ServiceProxy 中,比如首先被调用的 onCreate(),内部将先调用 init()。

init() 将构建 mRealServiceClassName 的实例,而 mRealServiceClassName 变量来自于 UpdatablePackageDependency.java 中定义的 CAR_SERVICE_IMPL_CLASS 常量即 “com.android.car.CarServiceImpl”

通过如下代码看到,这意味着将初始化 CarServiceImpl 实例。

public class CarService extends ServiceProxy {...public CarService() {super(UpdatablePackageDependency.CAR_SERVICE_IMPL_CLASS);// Increase the number of binder threads in car serviceBinderInternal.setMaxThreads(MAX_BINDER_THREADS);}...
}public class ServiceProxy extends Service {...@Overridepublic void onCreate() {init();mRealService.onCreate();}private void init() {mUpdatablePackageContext = UpdatablePackageContext.create(this);try {mRealServiceClass = mUpdatablePackageContext.getClassLoader().loadClass(mRealServiceClassName);// Use default constructor alwaysConstructor constructor = mRealServiceClass.getConstructor();mRealService = (ProxiedService) constructor.newInstance();mRealService.doAttachBaseContext(mUpdatablePackageContext);mRealService.setBuiltinPackageContext(this);} catch (Exception e) {throw new RuntimeException("Cannot load class:" + mRealServiceClassName, e);}}...
}public class UpdatablePackageDependency {/** {@code com.android.car.CarServiceImpl} class */public static final String CAR_SERVICE_IMPL_CLASS = "com.android.car.CarServiceImpl";...
}

init() 之后是执行创建出来的 CarServiceImpl 实例的 onCreate(),可以看到是继续创建关键类 ICarImpl 的实例并再次执行 init()。

public class CarServiceImpl extends ProxiedService {...@Overridepublic void onCreate() {...mICarImpl = new ICarImpl(this,getBuiltinPackageContext(),mVehicle,SystemInterface.Builder.defaultSystemInterface(this).build(),mVehicleInterfaceName);mICarImpl.init();...}...
}public class ICarImpl extends ICar.Stub {...ICarImpl( ... ) {...mHal = constructWithTrace(t, VehicleHal.class,() -> new VehicleHal(serviceContext, vehicle));...mCarPropertyService = constructWithTrace(t, CarPropertyService.class,() -> new CarPropertyService(serviceContext, mHal.getPropertyHal()));mCarDrivingStateService = constructWithTrace(t, CarDrivingStateService.class,() -> new CarDrivingStateService(serviceContext, mCarPropertyService));...mCarPowerManagementService = constructWithTrace(t, CarPowerManagementService.class,() -> new CarPowerManagementService(mContext, mHal.getPowerHal(),systemInterface, mCarUserService, powerPolicyDaemon));...mCarInputService = constructWithTrace(t, CarInputService.class,() -> new CarInputService(serviceContext, mHal.getInputHal(), mCarUserService,mCarOccupantZoneService, mCarBluetoothService));...List<CarServiceBase> allServices = new ArrayList<>();allServices.add(mCarPropertyService); // mCarUXRestrictionsService depends on itallServices.add(mCarPowerManagementService);allServices.add(mCarDrivingStateService);allServices.add(mCarInputService);......mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);mICarSystemServerClientImpl = new ICarSystemServerClientImpl();...}...
}

ICarImpl 的初始化将完成很多 vehicle 相关的重要工作:

  1. 将初始化和 vehicle HAL 层交互的 VehicleHal 实例
  2. 遍历 ICarImpl 实例构造时候创建的各个扩展自 CarServiceBase 的实例,逐个调用 init() 初始化,比如:
    • 掌管车辆硬件按键输入的 CarInputService
    • 掌管车辆属性的 CarPropertyService
    • 掌管车辆电源管理的 CarPowerManagementService
    • 掌管车辆驾驶状态的 CarDrivingStateService
    • 等等
public class ICarImpl extends ICar.Stub {...void init() {...mHal.init();for (CarServiceBase service : mAllServices) {service.init();}...}...
}

下面以 CarInputService 和 CarPropertyService 为例,继续看某个具体的 CarServiceBase 初始化了什么。

CarInputService 输入服务

CarInputService 初始化是向和 VehicleHal 交互的 InputHalService 传递输入相关的监听器 Listener。

public class CarInputService ... {...@Overridepublic void init() {if (!mInputHalService.isKeyInputSupported()) {return;}mInputHalService.setInputListener(this);...}...
}

InputHalService 将依据是否支持按键输入、旋钮输入、自定义输入的配置决定是否向 VehicleHal 订阅 Input Property 变化。

public class InputHalService extends HalServiceBase {...private final VehicleHal mHal;public void setInputListener(InputListener listener) {boolean keyInputSupported;boolean rotaryInputSupported;boolean customInputSupported;synchronized (mLock) {if (!mKeyInputSupported && !mRotaryInputSupported && !mCustomInputSupported) {Slogf.w(TAG, "input listener set while rotary and key input not supported");return;}mListener = listener;keyInputSupported = mKeyInputSupported;rotaryInputSupported = mRotaryInputSupported;customInputSupported = mCustomInputSupported;}if (keyInputSupported) {mHal.subscribeProperty(this, HW_KEY_INPUT);}if (rotaryInputSupported) {mHal.subscribeProperty(this, HW_ROTARY_INPUT);}if (customInputSupported) {mHal.subscribeProperty(this, HW_CUSTOM_INPUT);}}...
}

CarPropertyService 属性服务

CarPropertyService 也是一样,向和 VehicleHal 打交道的 PropertyHalService 传递输入相关的监听器 Listener。

public class CarPropertyService extends ICarProperty.Stubimplements CarServiceBase, PropertyHalService.PropertyHalListener {@Overridepublic void init() {synchronized (mLock) {// Cache the configs list and permissions to avoid subsequent binder callsmConfigs = mHal.getPropertyList();mPropToPermission = mHal.getPermissionsForAllProperties();if (DBG) {Slogf.d(TAG, "cache CarPropertyConfigs " + mConfigs.size());}}mHal.setListener(this);}...
}

PropertyHalService 将 CarPropertyService 作为 Callback 暂存,等待来自 HAL 的 Vehicle Property 变化回调。

public class PropertyHalService extends HalServiceBase {/*** Set the listener for the HAL service* @param listener*/public void setListener(PropertyHalListener listener) {synchronized (mLock) {mListener = listener;}}...
}

Vehicle 属性调用链路

Car 获取链路

对于其他 App 来说,想要使用 Car API,得做些准备工作:

  1. 先获取 Car 实例
  2. 然后执行连接
  3. 成功之后获取具体功能的 Manager 实例

比如这段用 CarPropertyManager 的示例:

    private Car mCar;private CarPropertyManager mCarPropertyManager;mCar = Car.createCar(this, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER,(car, ready) -> {mCar = car;if (ready) {mCarPropertyManager =(CarPropertyManager) mCar.getCarManager(Car.PROPERTY_SERVICE);...}});

我们结合源码看下上面这个步骤的链路:

createCar() 首先检查当前 OS 是否属于 Automotive 版本,之后调用 Car 构造函数进行一些全局变量的准备。

public final class Car {...public static Car createCar(Context context, ServiceConnection serviceConnectionListener,@Nullable Handler handler) {if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {Log.e(TAG_CAR, "FEATURE_AUTOMOTIVE not declared while android.car is used");return null;}try {return new Car(context, /* service= */ null , serviceConnectionListener,/* statusChangeListener= */ null, handler);} catch (IllegalArgumentException e) {// Expected when car service loader is not available.}return null;}private Car( ... ) {mContext = context;mEventHandler = determineEventHandler(handler);mMainThreadEventHandler = determineMainThreadEventHandler(mEventHandler);mService = service;if (service != null) {mConnectionState = STATE_CONNECTED;} else {mConnectionState = STATE_DISCONNECTED;}mServiceConnectionListenerClient = serviceConnectionListener;mStatusChangeCallback = statusChangeListener;// Store construction stack so that client can get help when it crashes when car service// crashes.if (serviceConnectionListener == null && statusChangeListener == null) {mConstructionStack = new RuntimeException();} else {mConstructionStack = null;}}...
}

connect() 首先检查是否重复请求连接了,确实需要连接的话调用 startCarService() 核心处理。

startCarService() 将绑定 CarService 服务。

public final class Car {...public void connect() throws IllegalStateException {synchronized (mLock) {if (mConnectionState != STATE_DISCONNECTED) {throw new IllegalStateException("already connected or connecting");}mConnectionState = STATE_CONNECTING;startCarService();}}private void startCarService() {Intent intent = new Intent();intent.setPackage(CAR_SERVICE_PACKAGE);intent.setAction(Car.CAR_SERVICE_INTERFACE_NAME);boolean bound = mContext.bindService(intent, mServiceConnectionListener,Context.BIND_AUTO_CREATE);synchronized (mLock) {if (!bound) {mConnectionRetryCount++;if (mConnectionRetryCount > CAR_SERVICE_BIND_MAX_RETRY) {Log.w(TAG_CAR, "cannot bind to car service after max retry");mMainThreadEventHandler.post(mConnectionRetryFailedRunnable);} else {mEventHandler.postDelayed(mConnectionRetryRunnable,CAR_SERVICE_BIND_RETRY_INTERVAL_MS);}} else {mEventHandler.removeCallbacks(mConnectionRetryRunnable);mMainThreadEventHandler.removeCallbacks(mConnectionRetryFailedRunnable);mConnectionRetryCount = 0;mServiceBound = true;}}}    ...
}

绑定成功之后,将 ICar AIDL 的本地接口代理赋值到 mService 变量(server 端在 CarService 的 ICarImpl 中),待使用。

public final class Car {...private final ServiceConnection mServiceConnectionListener =new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {synchronized (mLock) {ICar newService = ICar.Stub.asInterface(service);if (mService != null && mService.asBinder().equals(newService.asBinder())) {// already connected.return;}mConnectionState = STATE_CONNECTED;mService = newService;}if (mStatusChangeCallback != null) {mStatusChangeCallback.onLifecycleChanged(Car.this, true);} else if (mServiceConnectionListenerClient != null) {mServiceConnectionListenerClient.onServiceConnected(name, service);}}@Overridepublic void onServiceDisconnected(ComponentName name) {...};...
}

getCarManager() 首先得确保 CarService 准备就绪了,然后再去缓存 Manager 实例的 HashMap mServiceMap 中查找是否已有现成的,最后才发起 AIDL 获取该功能的接口。

public final class Car {...private final HashMap<String, CarManagerBase> mServiceMap = new HashMap<>();public Object getCarManager(String serviceName) {CarManagerBase manager;synchronized (mLock) {if (mService == null) {Log.w(TAG_CAR, "getCarManager not working while car service not ready");return null;}manager = mServiceMap.get(serviceName);if (manager == null) {try {IBinder binder = mService.getCarService(serviceName);if (binder == null) {Log.w(TAG_CAR, "getCarManager could not get binder for service:"+ serviceName);return null;}manager = createCarManagerLocked(serviceName, binder);if (manager == null) {Log.w(TAG_CAR, "getCarManager could not create manager for service:"+ serviceName);return null;}mServiceMap.put(serviceName, manager);} catch (RemoteException e) {handleRemoteExceptionFromCarService(e);}}}return manager;}...
}

ICarImpl 则是依据约定好的 Manager 的 Service 端名称去 CarService 中返回启动时候创建的一堆具体服务。

比如:PROPERTY_SERVICE 的话返回 CarPropertyServiceCAR_INPUT_SERVICE 返回 CarInputService

public class ICarImpl extends ICar.Stub {...public IBinder getCarService(String serviceName) {if (!mFeatureController.isFeatureEnabled(serviceName)) {Slogf.w(CarLog.TAG_SERVICE, "getCarService for disabled service:" + serviceName);return null;}switch (serviceName) {...case Car.PROPERTY_SERVICE:case Car.SENSOR_SERVICE:case Car.VENDOR_EXTENSION_SERVICE:return mCarPropertyService;...case Car.CAR_INPUT_SERVICE:return mCarInputService;...default:IBinder service = null;if (mCarExperimentalFeatureServiceController != null) {service = mCarExperimentalFeatureServiceController.getCarService(serviceName);}if (service == null) {Slogf.w(CarLog.TAG_SERVICE, "getCarService for unknown service:"+ serviceName);}return service;}}...
}

CarPropertyManager 调用链路

Automotive OS 提供了 CarPropertyManager API 给第三方 App 读写车辆属性。参数 propId 来自于 VehiclePropertyIds 类中定义的属性 ID,当然也需要获得相应的 permission。

比如读写车窗属性的 ID 为 WINDOW_POS,需要 android.car.permission.CONTROL_CAR_WINDOWS 的权限。

读取 getProperty() 首先调用 checkSupportedProperty() 检查是否支持该属性,如果是 USER 相关的属性会抛出如下的异常:

Unsupported property:xxx

通过检查的向实现的 Service 发出读取调用。

public class CarPropertyManager extends CarManagerBase {public <E> CarPropertyValue<E> getProperty(@NonNull Class<E> clazz, int propId, int areaId) {checkSupportedProperty(propId);try {CarPropertyValue<E> propVal = mService.getProperty(propId, areaId);if (propVal != null && propVal.getValue() != null) {Class<?> actualClass = propVal.getValue().getClass();}return propVal;}...}private void checkSupportedProperty(int propId) {switch (propId) {case VehiclePropertyIds.INITIAL_USER_INFO:case VehiclePropertyIds.SWITCH_USER:case VehiclePropertyIds.CREATE_USER:case VehiclePropertyIds.REMOVE_USER:case VehiclePropertyIds.USER_IDENTIFICATION_ASSOCIATION:throw new IllegalArgumentException("Unsupported property: "+ VehiclePropertyIds.toString(propId) + " (" + propId + ")");}}...
}

ICarProperty aidl 的实现即位于上个章节分析到的 CarPropertyService 中。

  1. 先到存放所有 Property ID 的 SparseArray 中检查是否确实存在该 Property,如果不存在的话打印 error 提醒并结束

  2. 获取该 Property 的 Permission 配置,如果不存在的话,抛出如下异常:

    SecurityException: Platform does not have permission to read value for property Id: 0x…

  3. assertPermission() 检查当前 Car Service 是否确实被授予了如上 Permission

  4. 最后调用持有的 PropertyHalService 继续发出读取的调用

public class CarPropertyService extends ICarProperty.Stubimplements CarServiceBase, PropertyHalService.PropertyHalListener {@Overridepublic CarPropertyValue getProperty(int prop, int zone) ... {synchronized (mLock) {if (mConfigs.get(prop) == null) {// Do not attempt to register an invalid propIdSlogf.e(TAG, "getProperty: propId is not in config list:0x" + toHexString(prop));return null;}}// Checks if android has permission to read property.String permission = mHal.getReadPermission(prop);if (permission == null) {throw new SecurityException("Platform does not have permission to read value for "+ "property Id: 0x" + Integer.toHexString(prop));}CarServiceUtils.assertPermission(mContext, permission);return runSyncOperationCheckLimit(() -> {return mHal.getProperty(prop, zone);});}...
}

PropertyHalService 首先调用 managerToHalPropId() 将 Property ID 转为 HAL 中该 ID 的定义,并再度检查该 HAL ID 是否确实存在。如果不存在的话亦抛出异常:

IllegalArgumentException:Invalid property Id : 0x…

接着,通过 VehicleHal 传递 HAL 中 ID 继续读取得到 HalPropValue,当读取的 value 存在的话,首先得获取该 Property 在 HAL 层和上层定义的 HalPropConfig 规则。

最后依据 config 将 value 解析成 CarPropertyValue 类型返回。

public class PropertyHalService extends HalServiceBase {
'/ '    ...public CarPropertyValue getProperty(int mgrPropId, int areaId)throws IllegalArgumentException, ServiceSpecificException {int halPropId = managerToHalPropId(mgrPropId);if (!isPropertySupportedInVehicle(halPropId)) {throw new IllegalArgumentException("Invalid property Id : 0x" + toHexString(mgrPropId));}// CarPropertyManager catches and rethrows exception, no need to handle here.HalPropValue value = mVehicleHal.get(halPropId, areaId);if (value == null) {return null;}HalPropConfig propConfig;synchronized (mLock) {propConfig = mHalPropIdToPropConfig.get(halPropId);}return value.toCarPropertyValue(mgrPropId, propConfig);}...
}

其实 VehicleHal 并未做太多处理就直接交给了 HalClient 来处理。

public class VehicleHal implements HalClientCallback {...public HalPropValue get(int propertyId)throws IllegalArgumentException, ServiceSpecificException {return get(propertyId, NO_AREA);}...public HalPropValue get(int propertyId, int areaId)throws IllegalArgumentException, ServiceSpecificException {return mHalClient.getValue(mPropValueBuilder.build(propertyId, areaId));}...
}

HalClient 通过 invokeRetriable() 进行超时为 50ms 的 internalGet() 调用:如果结果是 TRY_AGAIN 并且尚未超时的话,再次调用;反之已经超时或者结果成功获取到的话,即结束。

后续会再次检查该 Result 中的 status,是否是不合法的、空的值等等,通过检查的话则返回 HalPropValue 出去。

final class HalClient {...private static final int SLEEP_BETWEEN_RETRIABLE_INVOKES_MS = 50;HalPropValue getValue(HalPropValue requestedPropValue)throws IllegalArgumentException, ServiceSpecificException {ObjectWrapper<ValueResult> resultWrapper = new ObjectWrapper<>();resultWrapper.object = new ValueResult();int status = invokeRetriable(() -> {resultWrapper.object = internalGet(requestedPropValue);return resultWrapper.object.status;}, mWaitCapMs, mSleepMs);ValueResult result = resultWrapper.object;if (StatusCode.INVALID_ARG == status) {throw new IllegalArgumentException(getValueErrorMessage("get", requestedPropValue, result.errorMsg));}if (StatusCode.OK != status || result.propValue == null) {if (StatusCode.OK == status) {status = StatusCode.NOT_AVAILABLE;}throw new ServiceSpecificException(status, getValueErrorMessage("get", requestedPropValue, result.errorMsg));}return result.propValue;}private ValueResult internalGet(HalPropValue requestedPropValue) {final ValueResult result = new ValueResult();try {result.propValue = mVehicle.get(requestedPropValue);result.status = StatusCode.OK;result.errorMsg = new String();}...return result;}...
}

internalGet() 的实现由持有的 VehicleStub 实例的 get 方法完成,其实现对应于依据 HIDL 的配置调用 HAL 侧获取相应数据。

public abstract class VehicleStub {...@Nullablepublic abstract HalPropValue get(HalPropValue requestedPropValue)throws RemoteException, ServiceSpecificException;...
}

set 写入的链路和 get 大同小异,后面我会以车窗状态和开关操作为例,详细展开车辆属性 API 的使用和原理细节。

总结

我们通过一张表格来总结 CarService 相关组件的构成。

Car 相关组件所属进程作用
CarServiceHelperServiceSystemServer管理 Car Serivce 的系统服务
CarServicebuiltin appCar 核心服务
updatable appCar 核心服务的具体实现
Car APIandroid.car.jarCar API SDK
Car AppsLauncher 等Car 专门设计的一系列 App

而 CarService 在系统中的位置、与外部的交互链路,则是通过一张总图来直观把握:

  1. SystemServer 进程在系统启动的时候发现 OS 具备 Automotive 的 feature,则启动 CarServiceHelperService 系统服务,并交由 CarServiceHelperServiceUpdatableImpl 实际负责和 CarService 的绑定
  2. CarService 的 builtin app 由父类 ServiceProxy 完成中转,即反射出 updatable appCarServiceImpl 实例
  3. CarServiceImpl 的初始化将构建 ICarImpl 实例并构建内部的一堆具体服务 CarServiceBase,比如负责输入的 CarInputService、负责车辆属性的 CarPropertyService 等。
  4. 这些具体服务通过 HalServiceBase 和 VehicleHal 进行交互,比如调度输入事件的 InputHalService、读写/转换车辆属性的 PropertyHalService
  5. 后续的交给 VehicleHal 通过 HIDL 和 HAL 层交互
  6. 其他 Apps 可以通过 Car lib 提供的 Car API 获取 CarService 中的服务接口即 ICarImpl
  7. ICarImpl 通过启动时候注册的服务名称和 CarServiceBase 实例对照表向 Apps 返回对应的接口实例,比如控制、监听输入的 CarInputManager、读写车辆属性的 CarPropertyManager
  8. 其他 Apps 拿到这些 Manager 接口之后,像 AOSP 中使用 ActivityManager 等接口一样通过 AIDL 和 CarService 进行交互

推荐阅读

  • 一文了解 Android 车机如何处理中控的旋钮输入
  • 从实体按键看 Android 车载的自定义事件机制
  • Android 车机初体验:Auto,Automotive 傻傻分不清楚?

参考文档 & 文章

  • https://developer.android.google.cn/reference/android/car/hardware/property/CarPropertyManager
  • https://developer.android.google.cn/reference/android/car/VehiclePropertyIds
  • https://zhuanlan.zhihu.com/p/608622393
  • https://www.jianshu.com/p/75da427d5682

相关文章:

万字解读 Android 车机核心 :CarService 的构成和链路~

前言 关于 Android 车机&#xff0c;之前分析过方控上自定义按键的输入机制和中控上旋钮输入的原理&#xff0c;但都局限于 Car Service 内 Input 相关模块。 一文了解 Android 车机如何处理中控的旋钮输入从实体按键看 Android 车载的自定义事件机制 本文将结合 Android 系…...

C#使用Panel

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System...

Jabbi的Rust学习日记(一)

Rust环境安装 Windows:Install Rust - Rust Programming Language (rust-lang.org) 访问网页&#xff0c;下载64bit | 32bit 版本 下载完成后打开&#xff0c;直接enter即可 Linux:输入指令 curl https://sh.rustup.rs-sSf | sh 我会报错&#xff0c;好像是链接不到这个网址&a…...

电脑磁盘分区形式是什么?如何更改?

磁盘分区形式介绍 在了解为什么以及如何更改分区形式之前&#xff0c;让我们对磁盘分区形式有一个基本的了解。一般来说&#xff0c;分区形式是指主引导记录&#xff08;MBR&#xff09;和 GUID 分区表&#xff08;GPT&#xff09;。 MBR和GPT是Windows系统中常用…...

Outlook无需API开发连接钉钉群机器人,实现新增会议日程自动发送群消息通知

Outlook用户使用场景&#xff1a; 在企业中&#xff0c;会议和活动的顺利举行对于业务运转和团队协作至关重要。然而&#xff0c;计划的变动总是无法避免&#xff0c;这可能会导致其他人的计划受到影响&#xff0c;打乱原有的安排。为了解决这个问题&#xff0c;许多企业开始使…...

elasticsearch分析插件 安装analysis-ik

首先下载安装es 和 插件 &#xff0c;注意 两者的版本要保持一致,如果要用到kibana 则三者保持一致 ik&#xff1a;https://github.com/medcl/elasticsearch-analysis-ik/releases es/kibana&#xff1a;https://www.elastic.co/cn/downloads/past-releases/ 然后在 es— elast…...

2023年股票大宗减持研究报告

第一章 股票大宗减持概述 1.1 概念 大宗减持是指通过大宗交易的方式进行减持&#xff0c;即达到规定的最低限额的证券单笔买卖申报&#xff0c;买卖双方经过协议达成一致并经交易所确定成交的证券交易&#xff0c;其中A股单笔交易数量在30万股&#xff08;含&#xff09;以上…...

VS编译.cu文件源文件无法打开matrix.h和mex.h问题

配置好cu和VS相关库文件后CUDA程序仍然报错&#xff1a;无法打开matrix.h和mex.h&#xff0c;解决办法&#xff1a; &#xff08;1&#xff09;这两个头文件是matlab中的&#xff0c;可能无法直接在VS中调用&#xff0c;可以通过添加外部依赖项的方法将matlab中的头文件的文件路…...

小bugs搜集和解决方法,亲测有效(2022-2023)

有些小bugs几行指令就能解决&#xff0c;写太多不好&#xff0c;这里就进行一个2022-2023这段时间的bugs collection和solution。 Deep Learning How to make really empty tensor? a nn.Torch([]) b torch.empty(2,3) for i in rang(3):a torch.cat(a, b, dim0)Mismatc…...

归并排序和快速排序的两种实现

在此之前我们已经介绍过归并排序和快速排序&#xff1a;浅谈归并排序与快速排序&#xff0c;但其中的实现都是基于递归的。本文将重新温故这两种算法并给出基于迭代的实现。 目录 1. 归并排序1.1 基于递归1.2 基于迭代 2. 快速排序2.1 基于递归2.2 基于迭代 1. 归并排序 1.1 基…...

C#,《小白学程序》第十四课:随机数(Random)第一,几种随机数的计算方法与代码

1 文本格式 /// <summary> /// 《小白学程序》第十四课&#xff1a;随机数&#xff08;Random&#xff09;第一&#xff0c;几种随机数的计算方法与代码 /// 本课初步接触一下随机数。 /// </summary> /// <param name"sender"></param> ///…...

[杂谈]-快速了解Modbus协议

快速了解Modbus协议 文章目录 快速了解Modbus协议1、为何 Modbus 如此受欢迎2、范围和数据速率3、逻辑电平4、层数5、网络与通讯6、数据帧格式7、数据类型8、服务器如何存储数据9、总结 ​ Modbus 是一种流行的低速串行通信协议&#xff0c;广泛应用于自动化行业。 该协议由 Mo…...

WhatsApp的两个商业模式该如何选择

WhatsApp Business 是什么 目前 WhatsApp 提供两种商业模式&#xff0c;企业应根据自身需求选择相应版本。 第一个版本是 WhatsApp Business&#xff1a;初创企业只需一个手机应用程序&#xff0c;便可以个体单位与客户轻松互动; 另一个版本是 WhatsApp Business API&#xff…...

动态表单设计

动态表单设计 背景方案讨论基于上面分析&#xff0c;对比调研&#xff0c;自定义动态表单数据模型表单详解&#xff08;一&#xff09; 表单模板&#xff1a;jim_dynamic_form&#xff08;二&#xff09;表单数据类型&#xff1a;jim_form_data_type&#xff08;三&#xff09;…...

JAR will be empty - no content was marked for inclusion!

现象 在对自建pom依赖组件打包时&#xff0c;出现JAR will be empty - no content was marked for inclusion!错误。 方案 在pom中怎么加packaging标签内容为pom&#xff0c;标识只打包pom文件 <?xml version"1.0" encoding"UTF-8"?> ...<grou…...

软件生命周期及流程【软件测试】

软件的生命周期 软件生命周期是软件开始研制到最终被废弃不用所经历的各个阶段。 瀑布型生命周期模型 规定了它们自上而下、相互衔接的固定次序&#xff0c;如同瀑布流水&#xff0c;逐级下落&#xff0c;具有顺序性和依赖性。每个阶段规定文档并需进行评审。 特点&#xff…...

2023高教社杯数学建模E题思路代码 - 黄河水沙监测数据分析

# 1 赛题 E 题 黄河水沙监测数据分析 黄河是中华民族的母亲河。研究黄河水沙通量的变化规律对沿黄流域的环境治理、气候变 化和人民生活的影响&#xff0c; 以及对优化黄河流域水资源分配、协调人地关系、调水调沙、防洪减灾 等方面都具有重要的理论指导意义。 附件 1 给出了位…...

双翌保养码使用指南方法(一)

保养码使用指南一 为了确保软件的正常运行和有效使用&#xff0c;正确地使用保养码是至关重要的。以下是保养码使用的简单指南&#xff0c;以帮助您进行正确的操作。 1. 打开软件入口&#xff1a;首先&#xff0c;在您的电脑上打开文件夹&#xff0c;并找到s-y softactive tool…...

hive指定字段插入数据,包含了分区表和非分区表

1、建表 语句如下&#xff1a; CREATE EXTERNAL TABLE ods_lineitem_full (l_shipdate date,l_orderkey bigint,l_linenumber int,l_partkey int,l_suppkey int,l_quantity decimal(15, 2),l_extendedprice decimal(15, 2),l_discount de…...

浏览器端vscode docker搭建(附带python环境)

dockerfile from centos:7 #安装python环境 run yum -y install wget openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel zlib-devel libffi-devel gcc make run wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz run tar -xvf Python-3.9.…...

深度学习在微纳光子学中的应用

深度学习在微纳光子学中的主要应用方向 深度学习与微纳光子学的结合主要集中在以下几个方向&#xff1a; 逆向设计 通过神经网络快速预测微纳结构的光学响应&#xff0c;替代传统耗时的数值模拟方法。例如设计超表面、光子晶体等结构。 特征提取与优化 从复杂的光学数据中自…...

Prompt Tuning、P-Tuning、Prefix Tuning的区别

一、Prompt Tuning、P-Tuning、Prefix Tuning的区别 1. Prompt Tuning(提示调优) 核心思想:固定预训练模型参数,仅学习额外的连续提示向量(通常是嵌入层的一部分)。实现方式:在输入文本前添加可训练的连续向量(软提示),模型只更新这些提示参数。优势:参数量少(仅提…...

三维GIS开发cesium智慧地铁教程(5)Cesium相机控制

一、环境搭建 <script src"../cesium1.99/Build/Cesium/Cesium.js"></script> <link rel"stylesheet" href"../cesium1.99/Build/Cesium/Widgets/widgets.css"> 关键配置点&#xff1a; 路径验证&#xff1a;确保相对路径.…...

shell脚本--常见案例

1、自动备份文件或目录 2、批量重命名文件 3、查找并删除指定名称的文件&#xff1a; 4、批量删除文件 5、查找并替换文件内容 6、批量创建文件 7、创建文件夹并移动文件 8、在文件夹中查找文件...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

CMake控制VS2022项目文件分组

我们可以通过 CMake 控制源文件的组织结构,使它们在 VS 解决方案资源管理器中以“组”(Filter)的形式进行分类展示。 🎯 目标 通过 CMake 脚本将 .cpp、.h 等源文件分组显示在 Visual Studio 2022 的解决方案资源管理器中。 ✅ 支持的方法汇总(共4种) 方法描述是否推荐…...

什么是Ansible Jinja2

理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具&#xff0c;可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板&#xff0c;允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板&#xff0c;并通…...

基于matlab策略迭代和值迭代法的动态规划

经典的基于策略迭代和值迭代法的动态规划matlab代码&#xff0c;实现机器人的最优运输 Dynamic-Programming-master/Environment.pdf , 104724 Dynamic-Programming-master/README.md , 506 Dynamic-Programming-master/generalizedPolicyIteration.m , 1970 Dynamic-Programm…...

网站指纹识别

网站指纹识别 网站的最基本组成&#xff1a;服务器&#xff08;操作系统&#xff09;、中间件&#xff08;web容器&#xff09;、脚本语言、数据厍 为什么要了解这些&#xff1f;举个例子&#xff1a;发现了一个文件读取漏洞&#xff0c;我们需要读/etc/passwd&#xff0c;如…...

JS手写代码篇----使用Promise封装AJAX请求

15、使用Promise封装AJAX请求 promise就有reject和resolve了&#xff0c;就不必写成功和失败的回调函数了 const BASEURL ./手写ajax/test.jsonfunction promiseAjax() {return new Promise((resolve, reject) > {const xhr new XMLHttpRequest();xhr.open("get&quo…...