Android Automotive(五) CarService
CarService是Android Automotive在系统框架层的核心服务。它类似SystemServer在服务内部管理着数十个子服务。
启动流程
CarService是由SystemServer启动的,启动流程如下。
- SystemServer 启动CarServiceHelperService
- CarServiceHelperService绑定CarService
- CarService创建ICarImpl实例,调用init方法
- ICarImpl启动其它服务
SystemServer
代码:frameworks/base/services/java/com/android/server/SystemServer.java
private static final String CAR_SERVICE_HELPER_SERVICE_CLASS ="com.android.internal.car.CarServiceHelperService";
private void startOtherServices() {mActivityManagerService.systemReady(() -> {if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {traceBeginAndSlog("StartCarServiceHelperService");mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);traceEnd();}});
}
CarServiceHelperService
代码:frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java
private static final String CAR_SERVICE_INTERFACE = "android.car.ICar";
public class CarServiceHelperService extends SystemService {@Overridepublic void onStart() {Intent intent = new Intent();intent.setPackage("com.android.car");intent.setAction(CAR_SERVICE_INTERFACE);if (!getContext().bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM)) {Slog.wtf(TAG, "cannot start car service");}System.loadLibrary("car-framework-service-jni");}
}
SystemServer
中ActivityManagerService.systemReady
后会通过SystemServiceManager
启动CarServiceHelperService
CarServiceHelperService
中绑定CarService
CarService
清单文件:packages/services/Car/service/AndroidManifest.xml
<service android:name=".CarService"android:singleUser="true"><intent-filter><action android:name="android.car.ICar" /></intent-filter>
</service>
- 通过在
CarService
的AndroidManifest
的配置,CarServiceHelperService
可以通过包名和action
的名称实现和CarService
绑定。
CarService
代码:packages/services/Car/service/src/com/android/car/CarService.java
private ICarImpl mICarImpl;
private IVehicle mVehicle;
@Override
public void onCreate() {Log.i(CarLog.TAG_SERVICE, "Service onCreate");mCanBusErrorNotifier = new CanBusErrorNotifier(this /* context */);mVehicle = getVehicle();EventLog.writeEvent(EventLogTags.CAR_SERVICE_CREATE, mVehicle == null ? 0 : 1);Log.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName);EventLog.writeEvent(EventLogTags.CAR_SERVICE_CONNECTED, mVehicleInterfaceName);mICarImpl = new ICarImpl(this,mVehicle,SystemInterface.Builder.defaultSystemInterface(this).build(),mCanBusErrorNotifier,mVehicleInterfaceName);mICarImpl.init();linkToDeath(mVehicle, mVehicleDeathRecipient);ServiceManager.addService("car_service", mICarImpl);SystemProperties.set("boot.car_service_created", "1");super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {return mICarImpl;
}
- CarService中有两个重要的变量,mICarImpl和mVehicle
- mICarImpl是实现ICar接口的AIDL服务端,用于和应用层通信
- mVehicle是实现HIDL的VehicleHal服务接口,用于同hal层通信
ICarImpl
代码:packages/services/Car/service/src/com/android/car/ICarImpl.java
private final VehicleHal mHal;
@VisibleForTesting
ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,@Nullable CarUserService carUserService,@Nullable CarWatchdogService carWatchdogService) {mContext = serviceContext;mSystemInterface = systemInterface;mHal = new VehicleHal(serviceContext, vehicle);allServices.add(mCarWatchdogService);// Always put mCarExperimentalFeatureServiceController in last.addServiceIfNonNull(allServices, mCarExperimentalFeatureServiceController);mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
}@MainThread
void init() {mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);traceBegin("VehicleHal.init");mHal.init();traceEnd();traceBegin("CarService.initAllServices");for (CarServiceBase service : mAllServices) {service.init();}traceEnd();
}
ICarImpl
中的第一件事就是创建VehicleHal
对象,VehicleHal
类是CarService
中对Vehicle Service
的封装,所有的和VehicleHal
的交互都在这里处理。并且这里还初始化了其它hal
服务。如:PowerHalService
ICarImpl
会创建所有的服务,然后将服务添加到CarLocalServices
和mAllServices变量中,
mAllServices后面在init
会用到。init
方法调用VehicleHal
的init
方法初始化mHal.init()
init
方法会遍历mAllServices
中的服务,调用每个服务的init
方法进行初始化.
架构
CarSevice
代码路径:packages/services/Car/service/src/com/android/car/CarService.java
CarService作为服务的入库,实际做的事情也不是很多,主要如下。
-
创建服务进程
这个是Android架构的特性,CarService继承了Service,可以作为一个服务在系统后台运行。 -
创建CrashTracker
如果Vehicle hal在10分钟内发生10次Crash,则在CarService抛出异常。private final CrashTracker mVhalCrashTracker = new CrashTracker(10, // Max crash count.10 * 60 * 1000, // 10 minutes - sliding time window.() -> {if (IS_USER_BUILD) {Log.e(CarLog.TAG_SERVICE, "Vehicle HAL keeps crashing, notifying user...");mCanBusErrorNotifier.reportFailure(CarService.this);} else {throw new RuntimeException("Vehicle HAL crashed too many times in a given time frame");}} )
-
获取IVehicle实例
IVehicle是HIDL的接口,通过该实例来完成和HAL层的通信 -
完成子服务的创建,初始化子服务。
public void onCreate() {Log.i(CarLog.TAG_SERVICE, "Service onCreate");mCanBusErrorNotifier = new CanBusErrorNotifier(this /* context */);mVehicle = getVehicle();EventLog.writeEvent(EventLogTags.CAR_SERVICE_CREATE, mVehicle == null ? 0 : 1);Log.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName);EventLog.writeEvent(EventLogTags.CAR_SERVICE_CONNECTED, mVehicleInterfaceName);mICarImpl = new ICarImpl(this,mVehicle,SystemInterface.Builder.defaultSystemInterface(this).build(),mCanBusErrorNotifier,mVehicleInterfaceName);mICarImpl.init();linkToDeath(mVehicle, mVehicleDeathRecipient);ServiceManager.addService("car_service", mICarImpl);SystemProperties.set("boot.car_service_created", "1");super.onCreate();
}
-
mVehicle = getVehicle();
获取硬件抽象层的VehicleService
实例,getService
相对于老版本,增加了String
参数。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) {Log.e(CarLog.TAG_SERVICE, "Failed to get IVehicle/" + instanceName + " service", e);} catch (NoSuchElementException e) {Log.e(CarLog.TAG_SERVICE, "IVehicle/" + instanceName + " service not registered yet");}return null; }
-
mICarImpl = new ICarImpl(this
创建ICarImpl
对象,相当于是CarService
的代理,所有的服务加载和管理都是由它来完成的。 -
mICarImpl.init()
完成子服务的初始化 -
linkToDeath(mVehicle, mVehicleDeathRecipient);
监听VehicleService
的进程状态。 -
ServiceManager.addService("car_service", mICarImpl);
将自身加入到ServiceManager
供其他模块获取
ICarImpl
代码路径:
packages/services/Car/service/src/com/android/car/ICarImpl.java
ICarImpl实现了ICar.Stub是和应用层通信AIDL的服务端。内部完成了CarService中所有服务的加载和初始化工作。并且应用层实际也是通过此类来拿到相关服务的AIDL代理。
创建相关服务对象
ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,CanBusErrorNotifier errorNotifier, String vehicleInterfaceName,@Nullable CarUserService carUserService,@Nullable CarWatchdogService carWatchdogService) {mContext = serviceContext;mSystemInterface = systemInterface;mHal = new VehicleHal(serviceContext, vehicle);......mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());......List<CarServiceBase> allServices = new ArrayList<>();allServices.add(mCarPropertyService);mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
}
初始化
@MainThread
void init() {mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);traceBegin("VehicleHal.init");mHal.init();//初始化VehicleHaltraceEnd();traceBegin("CarService.initAllServices");for (CarServiceBase service : mAllServices) {service.init();//各个子service调用init}traceEnd();
}
初始化VehicleHal
,以及让各个子服务调用init
方法
获取子服务
getCarService
获取服务代理的接口
public static final String CABIN_SERVICE = "cabin";
public static final String HVAC_SERVICE = "hvac";
public static final String INFO_SERVICE = "info";
public static final String PROPERTY_SERVICE = "property";
public static final String SENSOR_SERVICE = "sensor";
public static final String VENDOR_EXTENSION_SERVICE = "vendor_extension";
@Override
public IBinder getCarService(String serviceName) {switch (serviceName) {case Car.CABIN_SERVICE:case Car.HVAC_SERVICE:case Car.INFO_SERVICE:case Car.PROPERTY_SERVICE:case Car.SENSOR_SERVICE:case Car.VENDOR_EXTENSION_SERVICE:return mCarPropertyService;}
}
以上所有字段的接口都会返回CarPropertyService
,这里除了PROPERTY_SERVICE
,其它字段已经不推荐使用( @deprecated
)。
CarPropertyService
管理车辆属性的服务,车辆属性通过此服务完成对系统应用层和硬件抽象层的数据通信。其中有一个重要的成员变量PropertyHalService
,其具体功能通过PropertyHalService
接口调用实现。CarPropertyService
其功能主要是维护客户端注册的listener。CarPropertyService来负责车辆属性的管理,使上层应用更易于处理车辆属性。
CarProperty
继承了ICarProperty.Stub
,作为和系统应用通信的服务端
public class CarPropertyService extends ICarProperty.Stub
ICarProperty
代码:packages/services/Car/car-lib/src/android/car/hardware/property/ICarProperty.aidl
package android.car.hardware.property;import android.car.hardware.CarPropertyConfig;
import android.car.hardware.CarPropertyValue;
import android.car.hardware.property.ICarPropertyEventListener;/*** @hide*/
interface ICarProperty {void registerListener(int propId, float rate, in ICarPropertyEventListener callback) = 0;void unregisterListener(int propId, in ICarPropertyEventListener callback) = 1;List<CarPropertyConfig> getPropertyList() = 2;CarPropertyValue getProperty(int prop, int zone) = 3;void setProperty(in CarPropertyValue prop, in ICarPropertyEventListener callback) = 4;String getReadPermission(int propId) = 5;String getWritePermission(int propId) = 6;
}
重要属性说明
private final Map<IBinder, Client> mClientMap = new ConcurrentHashMap<>();
private final Map<Integer, CarPropertyConfig<?>> mConfigs = new HashMap<>();
private final PropertyHalService mHal;
private boolean mListenerIsSet = false;
private final Map<Integer, List<Client>> mPropIdClientMap = new ConcurrentHashMap<>();
private final SparseArray<SparseArray<Client>> mSetOperationClientMap = new SparseArray<>();
private final HandlerThread mHandlerThread =CarServiceUtils.getHandlerThread(getClass().getSimpleName());
private final Handler mHandler = new Handler(mHandlerThread.getLooper());
-
mCientMap
存放listener
和Client
对象的Map对象。Client
维护Service
的回调接口的辅助类、每一个新注册的callback
都会创建一个Client对象private class Client implements IBinder.DeathRecipient {private final ICarPropertyEventListener mListener;private final IBinder mListenerBinder;private final SparseArray<Float> mRateMap = new SparseArray<Float>(); }
-
mConfigs
存放系统支持的所有车辆属性 -
mHal
PropertyHalService
实例,处理和hal交互的逻辑 -
mPropIdClientMap
用来存放注册了车辆属性的Client
-
mSetOperationClientMap
存放进行set
操作的车辆属性记录
CarPropertyService::registerListener
注册一个车辆属性的监听。注册时会检测权限ICarImpl.assertPermission(mContext, mHal.getReadPermission(propId));
并在注册成功后立即进行一次回调mHandler.post(() -> getAndDispatchPropertyInitValue(propertyConfig, finalClient));
client.getListener().onEvent(events);
@Override
public void registerListener(int propId, float rate, ICarPropertyEventListener listener) {if (DBG) {Log.d(TAG, "registerListener: propId=0x" + toHexString(propId) + " rate=" + rate);}if (listener == null) {Log.e(TAG, "registerListener: Listener is null.");throw new IllegalArgumentException("listener cannot be null.");}IBinder listenerBinder = listener.asBinder();CarPropertyConfig propertyConfig;Client finalClient;synchronized (mLock) {propertyConfig = mConfigs.get(propId);if (propertyConfig == null) {// Do not attempt to register an invalid propIdLog.e(TAG, "registerListener: propId is not in config list: 0x" + toHexString(propId));return;}ICarImpl.assertPermission(mContext, mHal.getReadPermission(propId));// Get or create the client for this listenerClient client = mClientMap.get(listenerBinder);if (client == null) {client = new Client(listener);}client.addProperty(propId, rate);// Insert the client into the propId --> clients mapList<Client> clients = mPropIdClientMap.get(propId);if (clients == null) {clients = new CopyOnWriteArrayList<Client>();mPropIdClientMap.put(propId, clients);}if (!clients.contains(client)) {clients.add(client);}// Set the HAL listener if necessaryif (!mListenerIsSet) {mHal.setListener(this);}// Set the new rateif (rate > mHal.getSampleRate(propId)) {mHal.subscribeProperty(propId, rate);}finalClient = client;}// propertyConfig and client are NonNull.mHandler.post(() ->getAndDispatchPropertyInitValue(propertyConfig, finalClient));
}
private void getAndDispatchPropertyInitValue(CarPropertyConfig config, Client client) {List<CarPropertyEvent> events = new LinkedList<>();int propId = config.getPropertyId();if (config.isGlobalProperty()) {CarPropertyValue value = mHal.getProperty(propId, 0);if (value != null) {CarPropertyEvent event = new CarPropertyEvent(CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE, value);events.add(event);}} else {for (int areaId : config.getAreaIds()) {CarPropertyValue value = mHal.getProperty(propId, areaId);if (value != null) {CarPropertyEvent event = new CarPropertyEvent(CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE, value);events.add(event);}}}try {client.getListener().onEvent(events);} catch (RemoteException ex) {// If we cannot send a record, its likely the connection snapped. Let the binder// death handle the situation.Log.e(TAG, "onEvent calling failed: " + ex);}
}
CarPropertyService::unregisterListener
取消注册车辆属性监听器
@Override
public void unregisterListener(int propId, ICarPropertyEventListener listener) {if (DBG) {Log.d(TAG, "unregisterListener propId=0x" + toHexString(propId));}ICarImpl.assertPermission(mContext, mHal.getReadPermission(propId));if (listener == null) {Log.e(TAG, "unregisterListener: Listener is null.");throw new IllegalArgumentException("Listener is null");}IBinder listenerBinder = listener.asBinder();synchronized (mLock) {unregisterListenerBinderLocked(propId, listenerBinder);}
}
CarPropertyService::getPropertyList
获取车辆属性列表
@Override
public List<CarPropertyConfig> getPropertyList() {List<CarPropertyConfig> returnList = new ArrayList<CarPropertyConfig>();Set<CarPropertyConfig> allConfigs;synchronized (mLock) {allConfigs = new HashSet<>(mConfigs.values());}for (CarPropertyConfig c : allConfigs) {if (ICarImpl.hasPermission(mContext, mHal.getReadPermission(c.getPropertyId()))) {// Only add properties the list if the process has permissions to read itreturnList.add(c);}}if (DBG) {Log.d(TAG, "getPropertyList returns " + returnList.size() + " configs");}return returnList;
}
CarPropertyService::getProperty
获取车辆属性
@Override
public CarPropertyValue getProperty(int prop, int zone) {synchronized (mLock) {if (mConfigs.get(prop) == null) {// Do not attempt to register an invalid propIdLog.e(TAG, "getProperty: propId is not in config list:0x" + toHexString(prop));return null;}}ICarImpl.assertPermission(mContext, mHal.getReadPermission(prop));return mHal.getProperty(prop, zone);
}
CarPropertyService::setProperty
设置车辆属性
@Override
public void setProperty(CarPropertyValue prop, ICarPropertyEventListener listener) {int propId = prop.getPropertyId();checkPropertyAccessibility(propId);// need an extra permission for writing display units properties.if (mHal.isDisplayUnitsProperty(propId)) {ICarImpl.assertPermission(mContext, Car.PERMISSION_VENDOR_EXTENSION);}mHal.setProperty(prop);IBinder listenerBinder = listener.asBinder();synchronized (mLock) {Client client = mClientMap.get(listenerBinder);if (client == null) {client = new Client(listener);}updateSetOperationRecorder(propId, prop.getAreaId(), client);}
}
CarPropertyService::onPropertyChange
处理车辆属性回调,将硬件抽象层的车辆属性变化通知给系统应用层。
@Override
public void onPropertyChange(List<CarPropertyEvent> events) {Map<IBinder, Pair<ICarPropertyEventListener, List<CarPropertyEvent>>> eventsToDispatch =new HashMap<>();for (CarPropertyEvent event : events) {int propId = event.getCarPropertyValue().getPropertyId();List<Client> clients = mPropIdClientMap.get(propId);if (clients == null) {Log.e(TAG, "onPropertyChange: no listener registered for propId=0x"+ toHexString(propId));continue;}for (Client c : clients) {IBinder listenerBinder = c.getListenerBinder();Pair<ICarPropertyEventListener, List<CarPropertyEvent>> p =eventsToDispatch.get(listenerBinder);if (p == null) {// Initialize the linked list for the listenerp = new Pair<>(c.getListener(), new LinkedList<CarPropertyEvent>());eventsToDispatch.put(listenerBinder, p);}p.second.add(event);}}// Parse the dispatch list to send eventsfor (Pair<ICarPropertyEventListener, List<CarPropertyEvent>> p: eventsToDispatch.values()) {try {p.first.onEvent(p.second);} catch (RemoteException ex) {// If we cannot send a record, its likely the connection snapped. Let binder// death handle the situation.Log.e(TAG, "onEvent calling failed: " + ex);}}
}
PropertyHalService
在CarPropertyService
中用来处理和硬件抽象层通信的类,主要将系统框架层和硬件抽象层的数据结构进行转换,依赖CarPropertyUtils
这个工具类。
重要属性说明
private final LinkedList<CarPropertyEvent> mEventsToDispatch = new LinkedList<>();
@GuardedBy("mLock")
private final Map<Integer, CarPropertyConfig<?>> mMgrPropIdToCarPropConfig = new HashMap<>();
@GuardedBy("mLock")
private final SparseArray<VehiclePropConfig> mHalPropIdToVehiclePropConfig =new SparseArray<>();
// Only contains propId if the property Id is different in HAL and manager
private static final Map<Integer, Integer> PROPERTY_ID_HAL_TO_MANAGER = Map.of(VehicleProperty.VEHICLE_SPEED_DISPLAY_UNITS,VehiclePropertyIds.VEHICLE_SPEED_DISPLAY_UNITS);
// Only contains propId if the property Id is different in HAL and manager
private static final Map<Integer, Integer> PROPERTY_ID_MANAGER_TO_HAL = Map.of(VehiclePropertyIds.VEHICLE_SPEED_DISPLAY_UNITS,VehicleProperty.VEHICLE_SPEED_DISPLAY_UNITS);
private static final String TAG = "PropertyHalService";
private final VehicleHal mVehicleHal;
private final PropertyHalServiceIds mPropIds;@GuardedBy("mLock")
private PropertyHalListener mListener;
@GuardedBy("mLock")
private Set<Integer> mSubscribedHalPropIds;
-
mEventsToDispatch
即将上报的数据列表,这个数据会直接发给CarPropertyService
上报。这个设计很棒,接收到的数据保存在列表中,可以避免数据丢失。整个列表发给上层处理,PropertyHalService
又避免了对数据阻塞。 -
mProps
车辆属的配置,在takeSupportedProperties
方法完成属性的增加。 -
mVehicleHal
VehicleHal
的实例 -
mPropIds
数据类PropertyHalServiceIds
的实例,维护系统车辆属性的权限private final PropertyHalServiceIds mPropIds;public PropertyHalService(VehicleHal vehicleHal) {mPropIds = new PropertyHalServiceIds(); }public String getReadPermission(int propId) {return mPropIds.getReadPermission(propId); }@Nullable public String getWritePermission(int propId) {return mPropIds.getWritePermission(propId); }
-
mSubscribedPropIds
监听车辆属性的集合,使用集合可以有效的屏蔽重复注册的监听
PropertyHalService::getProperty
获取一个车辆属性,将硬件抽象层数据结构转换成系统框架层数据结构。
public CarPropertyValue getProperty(int mgrPropId, int areaId) {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.VehiclePropValue value = mVehicleHal.get(halPropId, areaId);if (isMixedTypeProperty(halPropId)) {VehiclePropConfig propConfig;synchronized (mLock) {propConfig = mHalPropIdToVehiclePropConfig.get(halPropId);}boolean containBooleanType = propConfig.configArray.get(1) == 1;return value == null ? null : toMixedCarPropertyValue(value,mgrPropId, containBooleanType);}return value == null ? null : toCarPropertyValue(value, mgrPropId);
}
isPropertySupportedInVehicle(halPropId)
判断这个车辆属性是否支持VehiclePropValue value = mVehicleHal.get(halPropId, areaId)
通过VehicleHal
获取车辆属性toCarPropertyValue(value, mgrPropId)
将硬件抽象层数据结构转换成系统框架层数据结构。
PropertyHalService::setProperty
设置一个车辆属性,将系统框架层数据结构转换成硬件抽象层数据结构。
public void setProperty(CarPropertyValue prop) {int halPropId = managerToHalPropId(prop.getPropertyId());if (!isPropertySupportedInVehicle(halPropId)) {throw new IllegalArgumentException("Invalid property Id : 0x"+ toHexString(prop.getPropertyId()));}VehiclePropValue halProp;if (isMixedTypeProperty(halPropId)) {// parse mixed type property value.VehiclePropConfig propConfig;synchronized (mLock) {propConfig = mHalPropIdToVehiclePropConfig.get(prop.getPropertyId());}int[] configArray = propConfig.configArray.stream().mapToInt(i->i).toArray();halProp = toMixedVehiclePropValue(prop, halPropId, configArray);} else {halProp = toVehiclePropValue(prop, halPropId);}// CarPropertyManager catches and rethrows exception, no need to handle here.mVehicleHal.set(halProp);
}
int halPropId = managerToHalPropId(prop.getPropertyId())
转换成硬件抽象层用的车辆属性IDtoVehiclePropValue(prop, halPropId)
将系统框架层数据结构转换称硬件抽象层数据结构。mVehicleHal.set(halProp)
通过VehicleHal
设置车辆属性
PropertyHalService::onHalEvents
完成VehiclePropValue
(硬件抽象层数据结构)到CarPropertyValue
(系统框架层数据结构)的转化,并将其封装成CarPropertyEvent
数据通知给上层。
@Override
public void onHalEvents(List<VehiclePropValue> values) {PropertyHalListener listener;synchronized (mLock) {listener = mListener;}if (listener != null) {for (VehiclePropValue v : values) {if (v == null) {continue;}if (!isPropertySupportedInVehicle(v.prop)) {Log.e(TAG, "Property is not supported: 0x" + toHexString(v.prop));continue;}// Check payload if it is a userdebug build.if (Build.IS_DEBUGGABLE && !mPropIds.checkPayload(v)) {Log.e(TAG, "Drop event for property: " + v + " because it is failed "+ "in payload checking.");continue;}int mgrPropId = halToManagerPropId(v.prop);CarPropertyValue<?> propVal;if (isMixedTypeProperty(v.prop)) {// parse mixed type property value.VehiclePropConfig propConfig;synchronized (mLock) {propConfig = mHalPropIdToVehiclePropConfig.get(v.prop);}boolean containBooleanType = propConfig.configArray.get(1) == 1;propVal = toMixedCarPropertyValue(v, mgrPropId, containBooleanType);} else {propVal = toCarPropertyValue(v, mgrPropId);}CarPropertyEvent event = new CarPropertyEvent(CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE, propVal);mEventsToDispatch.add(event);}listener.onPropertyChange(mEventsToDispatch);mEventsToDispatch.clear();}
}
PropertyHalService::getPropertyList
getPropertyList
返回车辆属性的列表,其实也就是自己维护的mProps
对象。也是CarPropertyService中维护的mConfigs
public Map<Integer, CarPropertyConfig<?>> getPropertyList() {if (mDbg) {Log.d(TAG, "getPropertyList");}return mProps;
}
VehicleHal
管理所有的*halservice
,这些服务都是通过车辆属性来实现数据通信的,但是其所支持的车辆属性又相对独立,互不影响。现在包括的服务如下
PowerHalService
PropertyHalService
InputHalService
VmsHalService
DiagnosticHalService
VehicleHal
继承了IVehicleCallback.Stub
,是可以接收硬件抽象层的HIDL回调。
VehicleHal::init
初始化mAllProperties
,从HAL层获取车辆属性配置增加到Map中。初始化所有的子服务。
public void init() {fetchAllPropConfigs();//遍历所有属性,从hal层获取将属性加到mAllProperties对象中// PropertyHalService will take most properties, so make it big enough.ArrayList<VehiclePropConfig> configsForService = new ArrayList<>(mAllServices.size());for (int i = 0; i < mAllServices.size(); i++) {HalServiceBase service = mAllServices.get(i);int[] supportedProps = service.getAllSupportedProperties();//获取子服务支持的车辆属性configsForService.clear();synchronized (mLock) {if (supportedProps.length == 0) {//这里是PropertyHalServicefor (Integer propId : mAllProperties.keySet()) {if (service.isSupportedProperty(propId)) {VehiclePropConfig config = mAllProperties.get(propId);mPropertyHandlers.append(propId, service);configsForService.add(config);}}} else {//这里是其他for (int prop : supportedProps) {VehiclePropConfig config = mAllProperties.get(prop);if (config == null) {continue;}mPropertyHandlers.append(prop, service);configsForService.add(config);}}}service.takeProperties(configsForService);service.init();}
}
-
fetchAllPropConfigs
遍历所有属性,从硬件层获取将属性加到mAllProperties
对象中 -
getAllSupportedProperties
获取子服务支持的车辆属性 -
PropertyHalService
会执行上面的判断public int[] getAllSupportedProperties() {return CarServiceUtils.EMPTY_INT_ARRAY; } private static final int[] EMPTY_INT_ARRAY = new int[0];
-
当判断改属性支持支持后将属性配置到
mPropertyHandlers
和configsForService
,mPropertyHandlers
记录车辆属性和处理的子服务,configsForService
记录车辆属性的VehiclePropConfig
-
调用子服务的
takeProperties
方法,该方法确认在fw的配置是否支持,配置定义在PropertyHalServiceIds.java
中,定义了车辆属性需要的系统权限。
VehicleHal::get
get
方法用来获取车辆属性,实际的执行者是HalClient
public VehiclePropValue get(int propertyId, int areaId) {if (DBG) {Log.i(CarLog.TAG_HAL, "get, property: 0x" + toHexString(propertyId)+ ", areaId: 0x" + toHexString(areaId));}VehiclePropValue propValue = new VehiclePropValue();propValue.prop = propertyId;propValue.areaId = areaId;return mHalClient.getValue(propValue);
}
VehicleHal::set
set
方法用来设置车辆属性,实际的执行者是HalClient
protected void set(VehiclePropValue propValue) {mHalClient.setValue(propValue);
}
VehicleHal::getAllPropConfigs
getAllPropConfigs
方法用来从硬件抽象层获取所有的车辆属性配置
public Collection<VehiclePropConfig> getAllPropConfigs() {return mAllProperties.values();
}
VehicleHal::onPropertyEvent
onPropertyEvent
方法用来监听硬件抽象层传来的车辆属性变化。
@Override
public void onPropertyEvent(ArrayList<VehiclePropValue> propValues) {synchronized (mLock) {for (VehiclePropValue v : propValues) {HalServiceBase service = mPropertyHandlers.get(v.prop);if(service == null) {Log.e(CarLog.TAG_HAL, "HalService not found for prop: 0x"+ toHexString(v.prop));continue;}service.getDispatchList().add(v);mServicesToDispatch.add(service);VehiclePropertyEventInfo info = mEventLog.get(v.prop);if (info == null) {info = new VehiclePropertyEventInfo(v);mEventLog.put(v.prop, info);} else {info.addNewEvent(v);}}}for (HalServiceBase s : mServicesToDispatch) {s.onHalEvents(s.getDispatchList());s.getDispatchList().clear();}mServicesToDispatch.clear();
}
-
onPropertyEvent
重写了HIDL接口IVehicleCallback
代码路径:automotive\vehicle\2.0\IVehicleCallback.hal
package android.hardware.automotive.vehicle@2.0;interface IVehicleCallback {/*** Event callback happens whenever a variable that the API user has* subscribed to needs to be reported. This may be based purely on* threshold and frequency (a regular subscription, see subscribe call's* arguments) or when the IVehicle#set method was called and the actual* change needs to be reported.** These callbacks are chunked.** @param values that has been updated.*/oneway onPropertyEvent(vec<VehiclePropValue> propValues);/*** This method gets called if the client was subscribed to a property using* SubscribeFlags::EVENTS_FROM_ANDROID flag and IVehicle#set(...) method was called.** These events must be delivered to subscriber immediately without any* batching.** @param value Value that was set by a client.*/oneway onPropertySet(VehiclePropValue propValue);/*** Set property value is usually asynchronous operation. Thus even if* client received StatusCode::OK from the IVehicle::set(...) this* doesn't guarantee that the value was successfully propagated to the* vehicle network. If such rare event occurs this method must be called.** @param errorCode - any value from StatusCode enum.* @param property - a property where error has happened.* @param areaId - bitmask that specifies in which areas the problem has* occurred, must be 0 for global properties*/oneway onPropertySetError(StatusCode errorCode,int32_t propId,int32_t areaId); };
-
从硬件抽象层获取到的是一个
ArrayList
,这里会循环遍历列表中每一个车辆属性。 -
HalServiceBase service = mPropertyHandlers.get(v.prop);
这个是获取当前车辆属性需要通知的子HalService
或者说是获取管理这个车辆属性的服务。 -
service.getDispatchList().add(v)
将这个车辆属性添加到管理这个服务的分派列表中。 -
mServicesToDispatch.add(service);
将此子服务添加到服务分发列表中。 -
s.onHalEvents(s.getDispatchList());
是将前面识别到的车辆属性,让其对应的服务执行分发操作。普通车辆属性的执行者是PropertyHalService
。
HalClient
相当于VehicleHal
的代理类,VehicleHal
中定义了和硬件抽象层交互的接口,但是具体实现实际都是通过HalClient
来进行调用的。HalClient
中持有硬件抽象层VehicleService
的句柄。
HalClient::getAllPropConfigs
获取全部的车辆属性配置
ArrayList<VehiclePropConfig> getAllPropConfigs() throws RemoteException {return mVehicle.getAllPropConfigs();
}
HalClient::subscribe
订阅一个车辆属性变化
public void subscribe(SubscribeOptions... options) throws RemoteException {mVehicle.subscribe(mInternalCallback, new ArrayList<>(Arrays.asList(options)));
}
HalClient::unsubscribe
取消一个车辆属性订阅
public void unsubscribe(int prop) throws RemoteException {mVehicle.unsubscribe(mInternalCallback, prop);
}
HalClient::setValue
设置一个车辆属性
public void setValue(VehiclePropValue propValue) {int status = invokeRetriable(() -> {try {return mVehicle.set(propValue);} catch (RemoteException e) {Log.e(TAG, getValueErrorMessage("set", propValue), e);return StatusCode.TRY_AGAIN;}}, mWaitCapMs, mSleepMs);if (StatusCode.INVALID_ARG == status) {throw new IllegalArgumentException(getValueErrorMessage("set", propValue));}if (StatusCode.OK != status) {Log.e(TAG, getPropertyErrorMessage("set", propValue, status));throw new ServiceSpecificException(status,"Failed to set property: 0x" + Integer.toHexString(propValue.prop)+ " in areaId: 0x" + Integer.toHexString(propValue.areaId));}
}
HalClient::getValue
获取一个车辆属性
VehiclePropValue getValue(VehiclePropValue requestedPropValue) {final ObjectWrapper<VehiclePropValue> valueWrapper = new ObjectWrapper<>();int status = invokeRetriable(() -> {ValueResult res = internalGet(requestedPropValue);valueWrapper.object = res.propValue;return res.status;}, mWaitCapMs, mSleepMs);if (StatusCode.INVALID_ARG == status) {throw new IllegalArgumentException(getValueErrorMessage("get", requestedPropValue));}if (StatusCode.OK != status || valueWrapper.object == null) {// If valueWrapper.object is null and status is StatusCode.Ok, change the status to be// NOT_AVAILABLE.if (StatusCode.OK == status) {status = StatusCode.NOT_AVAILABLE;}Log.e(TAG, getPropertyErrorMessage("get", requestedPropValue, status));throw new ServiceSpecificException(status,"Failed to get property: 0x" + Integer.toHexString(requestedPropValue.prop)+ " in areaId: 0x" + Integer.toHexString(requestedPropValue.areaId));}return valueWrapper.object;
}
private ValueResult internalGet(VehiclePropValue requestedPropValue) {final ValueResult result = new ValueResult();try {mVehicle.get(requestedPropValue,(status, propValue) -> {result.status = status;result.propValue = propValue;});} catch (RemoteException e) {Log.e(TAG, getValueErrorMessage("get", requestedPropValue), e);result.status = StatusCode.TRY_AGAIN;}return result;
}
CarPropertyConfig
车辆属性的属性配置,在FW层使用的,由硬件抽象层的VehiclePropConfig
转化而来。定义了车辆属性的基本信息。包括读写性质、类型、变化模式等等。
private final int mAccess;
private final int mAreaType;
private final int mChangeMode;
private final ArrayList<Integer> mConfigArray;
private final String mConfigString;
private final float mMaxSampleRate;
private final float mMinSampleRate;
private final int mPropertyId;
private final SparseArray<AreaConfig<T>> mSupportedAreas;
private final Class<T> mType;
CarPropertyUtils
一个工具类,将硬件抽象层数据结构和系统框架层数据结构相互进行转换。
-
toCarPropertyValue
转换VehiclePropValue
变成CarPropertyValue
。(HAL->FW)static CarPropertyValue<?> toCarPropertyValue(VehiclePropValue halValue, int propertyId)
-
toVehiclePropValue
转换CarPropertyValue
变成VehiclePropValue
。(HW->HAL)static VehiclePropValue toVehiclePropValue(CarPropertyValue carProp, int halPropId)
-
toCarPropertyConfig
转换VehiclePropConfig
变成CarPropertyConfig
。(HAL->FW)static CarPropertyConfig<?> toCarPropertyConfig(VehiclePropConfig p, int propertyId)
转化关系
方法 | 转化关系 |
---|---|
toCarPropertyValue | VehiclePropValue ->CarPropertyValue(fw) |
toVehiclePropValue | CarPropertyValue ->VehiclePropValue(hal) |
toCarPropertyConfig | VehiclePropConfig ->CarPropertyConfig(fw) |
CarPropertyConfig是某一个车辆属性的信息。相当于Map中的KEY。
CarPropertyValue 是某一个车辆属性返回的值。相当于Map中的Value。
VehiclePropConfig和VehiclePropValue的定义实际上是在types.hal中。
CarPropertyValue变量的对应关系
CarPropertyValue | VehiclePropValue |
---|---|
mPropertyId int | prop int32_t |
mAreaIdint | areaIdint32_t |
mStatusint | statusint32_t |
mTimestamp long | timestamp int64_t |
mValue T | value RawValue |
系统框架层的CarPropertyValue
返回值是一个泛型,可以根据属性实际的定义来决定对应的类型。硬件抽象层定义是一个数据结构,RawValue
里定义了常规类型的数组和字符串。
struct RawValue {vec<int32_t> int32Values;vec<float> floatValues;vec<int64_t> int64Values;vec<uint8_t> bytes;string stringValue;
};
如果是数据类型不是数组,会占用数组的第0位返回,布尔型会用int32Values.get(0) == 1
CarPropertyConfig变量的对应关系
CarPropertyConfig | VehiclePropConfig |
---|---|
mAccess int | access VehiclePropertyAccess |
mAreaType int | |
mChangeMode int | changeMode VehiclePropertyChangeMode |
mConfigArray ArrayList<Integer> | configArray vec<int32_t> |
mConfigString String | configString string |
mMaxSampleRate float | maxSampleRate float |
mMinSampleRate float | minSampleRate float |
mPropertyId int | prop int32_t |
mSupportedAreas SparseArray<AreaConfig<T>> mSupportedAreas | areaConfigs vec<VehicleAreaConfig> |
mType Class<T> |
mType是根据车辆属性的定义计算出来的,Class<?> clazz = getJavaClass(p.prop & VehiclePropertyType.MASK)
PropertyHalServiceIds
定义了PropertyHalService支持的车辆属性。此类将读写权限绑定到属性ID。提供获取车辆属性权限定义的接口
代码路径:packages/services/Car/service/src/com/android/car/hal/PropertyHalServiceIds.java
public class PropertyHalServiceIds {// Index (key is propertyId, and the value is readPermission, writePermissionprivate final SparseArray<Pair<String, String>> mProps;public PropertyHalServiceIds() {mProps = new SparseArray<>();// Add propertyId and read/write permissions// 在构造函数中绑定权限mProps.put(VehicleProperty.DOOR_POS, new Pair<>(Car.PERMISSION_CONTROL_CAR_DOORS,Car.PERMISSION_CONTROL_CAR_DOORS));//......}
}
-
getReadPermission()
获取读取权限,返回值为字符串public String getReadPermission(int propId) {Pair<String, String> p = mProps.get(propId);if (p != null) {// Property ID exists. Return read permission.if (p.first == null) {Log.e(TAG, "propId is not available for reading : 0x" + toHexString(propId));}return p.first;} else if (isVendorProperty(propId)) {// if property is vendor property and do not have specific permission.return Car.PERMISSION_VENDOR_EXTENSION;} else {return null;} }
-
getWritePermission()
获取写入权限,返回值为字符串public String getWritePermission(int propId) {Pair<String, String> p = mProps.get(propId);if (p != null) {// Property ID exists. Return write permission.if (p.second == null) {Log.e(TAG, "propId is not writable : 0x" + toHexString(propId));}return p.second;} else if (isVendorProperty(propId)) {// if property is vendor property and do not have specific permission.return Car.PERMISSION_VENDOR_EXTENSION;} else {return null;} }
-
isSupportedProperty()
检查车辆属性是否在PropertyHalService
的已知列表中public boolean isSupportedProperty(int propId) {// Property is in the list of supported propertiesreturn mProps.get(propId) != null || isVendorProperty(propId); }
private static boolean isVendorProperty(int propId) {return (propId & VehiclePropertyGroup.MASK) == VehiclePropertyGroup.VENDOR; }
总结
类 | 说明 |
---|---|
CarService | Car的主服务 |
ICarImpl | 具体Car的实现类 |
VehicleHal | 实现IVehicleCallback接口的类,并维护各个子服务 |
PropertyHalService | 处理HAL层获取到的数据并将其转化成FW层使用的类型 |
HalClient | 持有IVehile接口,代理实际操作 |
PropertyHalServiceIds | 定义支持的属性以及权限 |
CarPropertyUtils | 工具类,转化FW层和HAL层数据 |
CarPropertyConfig | 车辆属性配置信息 |
CarPropertyService | 车辆属性控制的子服务 |
Client | 车辆车辆属性订阅通知的类 |
CarService
作为主服务,通过ICarImpl
类实现对子服务CarPropertyService
的创建以及对VehicleHal
的初始化。VehicleHal
中完成对子服务们的初始化,并通过传入的IVehicle
对象使得PropertyHalService
获取系统支持的车辆属性,完成初始化操作。在PropertyHalService
中创建HalClient
对象完成对IVehicle
接口的实际调用,并将从HAL层获取的数据类型转化成FW层使用数据类型。CarPropertyService
持有PropertyHalService
的引用,完成上下层的数据交互操作。对于APPLICATION注册的Listener,CarPropertyService
使用Client
对象进行维护,当订阅的数据发生变化进行通知,当首次注册时,会主动获取值并进行一次通知操作。
关联关系:CarPropertyManager --> ICarProperty --> CarPropertyService --> PropertyHalService --> VehicleHal
|
| --------------------- | ----------------------------------------------- |
| CarService | Car的主服务 |
| ICarImpl | 具体Car的实现类 |
| VehicleHal | 实现IVehicleCallback接口的类,并维护各个子服务 |
| PropertyHalService | 处理HAL层获取到的数据并将其转化成FW层使用的类型 |
| HalClient | 持有IVehile接口,代理实际操作 |
| PropertyHalServiceIds | 定义支持的属性以及权限 |
| CarPropertyUtils | 工具类,转化FW层和HAL层数据 |
| CarPropertyConfig | 车辆属性配置信息 |
| CarPropertyService | 车辆属性控制的子服务 |
| Client | 车辆车辆属性订阅通知的类 |
CarService
作为主服务,通过ICarImpl
类实现对子服务CarPropertyService
的创建以及对VehicleHal
的初始化。VehicleHal
中完成对子服务们的初始化,并通过传入的IVehicle
对象使得PropertyHalService
获取系统支持的车辆属性,完成初始化操作。在PropertyHalService
中创建HalClient
对象完成对IVehicle
接口的实际调用,并将从HAL层获取的数据类型转化成FW层使用数据类型。CarPropertyService
持有PropertyHalService
的引用,完成上下层的数据交互操作。对于APPLICATION注册的Listener,CarPropertyService
使用Client
对象进行维护,当订阅的数据发生变化进行通知,当首次注册时,会主动获取值并进行一次通知操作。
关联关系:CarPropertyManager --> ICarProperty --> CarPropertyService --> PropertyHalService --> VehicleHal