Android Automotive架构与流程:VehicleHAL,CarService

case DYSENSOR_SERVICE: manager = new CarDYSensorManager(binder, mContext, mEventHandler);break;

case INFO_SERVICE: manager = new CarInfoManager(binder);break;

case APP_FOCUS_SERVICE: manager = new CarAppFocusManager(binder, mEventHandler);break;

case PACKAGE_SERVICE: manager = new CarPackageManager(binder, mContext);break;

case CAR_NAVIGATION_SERVICE: manager = new CarNavigationStatusManager(binder);break;

case CABIN_SERVICE: manager = new CarCabinManager(binder, mContext, mEventHandler);break;

case DIAGNOSTIC_SERVICE: manager = new CarDiagnosticManager(binder, mContext, mEventHandler);break;

case HVAC_SERVICE: manager = new CarHvacManager(binder, mContext, mEventHandler);break;

case DYHVAC_SERVICE: manager = new CarDYHvacManager(binder, mContext, mEventHandler);break;

case POWER_SERVICE: manager = new CarPowerManager(binder, mContext, mEventHandler);break;

case PROJECTION_SERVICE:manager = new CarProjectionManager(binder, mEventHandler);break;

case PROPERTY_SERVICE:

manager = new CarPropertyManager(binder, mEventHandler, false, “CarPropertyManager”);break;

case VENDOR_EXTENSION_SERVICE: manager = new CarVendorExtensionManager(binder, mEventHandler);break;

case CAR_INSTRUMENT_CLUSTER_SERVICE:

manager = new CarInstrumentClusterManager(binder, mEventHandler);break;

case TEST_SERVICE:

/* CarTestManager exist in static library. So instead of constructing it here,

  • only pass binder wrapper so that CarTestManager can be constructed outside. */

manager = new CarTestManagerBinderWrapper(binder);break;

case VMS_SUBSCRIBER_SERVICE: manager = new VmsSubscriberManager(binder); break;

case BLUETOOTH_SERVICE: manager = new CarBluetoothManager(binder, mContext);break;

case TBOX_SERVICE: manager = TboxManager.getInstance(); break;

case STORAGE_MONITORING_SERVICE: manager = new CarStorageMonitoringManager(binder, mEventHandler); break;

case CAR_DRIVING_STATE_SERVICE:

manager = new CarDrivingStateManager(binder, mContext, mEventHandler);break;

case CAR_UX_RESTRICTION_SERVICE:

manager = new CarUxRestrictionsManager(binder, mContext, mEventHandler);break;

case CAR_CONFIGURATION_SERVICE: manager = new CarConfigurationManager(binder);break;

case CAR_MCU_SERVICE: manager = new CarMcuManager(binder, mContext, mEventHandler);break;

case DYCABIN_SERVICE: manager = new CarDYCabinManager(binder, mContext, mEventHandler);break;

default:break;

}

return manager;

}

  1. 具体的Manager -> 注册/反注册回调, set/get 属性CarProperty:

public final class CarHvacManager implements CarManagerBase {

private void handleOnChangeEvent(CarPropertyValue value) {

Collection callbacks;

synchronized (this) { callbacks = new ArraySet<>(mCallbacks); }

if (!callbacks.isEmpty()) {

for (CarHvacEventCallback l: callbacks) {

if (DBG) Log.d(TAG, “onChangeEvent value=” + value.toString());

l.onChangeEvent(value);

}

}

}

private void handleOnErrorEvent(int propertyId, int zone) {

Collection callbacks;

synchronized (this) { callbacks = new ArraySet<>(mCallbacks); }

if (!callbacks.isEmpty()) {

for (CarHvacEventCallback l: callbacks) {

l.onErrorEvent(propertyId, zone);

}

}

}

public CarHvacManager(IBinder service, Context context, Handler handler) {

mCarPropertyMgr = new CarPropertyManager(service, handler, DBG, TAG);

}

public synchronized void registerCallback(CarHvacEventCallback callback)

throws CarNotConnectedException {

if (mCallbacks.isEmpty()) mListenerToBase = new CarPropertyEventListenerToBase(this);

List configs = getPropertyList();

for (CarPropertyConfig c : configs) { // Register each individual propertyId

mCarPropertyMgr.registerListener(mListenerToBase, c.getPropertyId(), 0);

}

mCallbacks.add(callback);

}

puAndroid Automotive架构与流程:VehicleHAL,CarService_第1张图片
blic synchronized void unregisterCallback(CarHvacEventCallback callback) {

mCallbacks.remove(callback);

try {

List configs = getPropertyList();

for (CarPropertyConfig c : configs) { // Register each individual propertyId

mCarPropertyMgr.unregisterListener(mListenerToBase, c.getPropertyId());

}

} catch (Exception e) { Log.e(TAG, "getPropertyList exception ", e); }

if (mCallbacks.isEmpty()) {

mCarPropertyMgr.unregisterListener(mListenerToBase);

mListenerToBase = null;

}

}

public List getPropertyList() throws CarNotConnectedException {

return mCarPropertyMgr.getPropertyList(mHvacPropertyIds);

}

public boolean isPropertyAvailable(@PropertyId int propertyId, int area) throws CarNotConnectedException {

return mCarPropertyMgr.isPropertyAvailable(propertyId, area);

}

// GET

// getBooleanProperty -> mCarPropertyMgr.getBooleanProperty(propertyId, area);

// getFloatProperty -> mCarPropertyMgr.getFloatProperty(propertyId, area);

// getIntProperty -> mCarPropertyMgr.getIntProperty(propertyId, area);

public boolean getBooleanProperty(@PropertyId int propertyId, int area)

throws CarNotConnectedException {

return mCarPropertyMgr.getBooleanProperty(propertyId, area);

}

// SET

// setBooleanProperty -> mCarPropertyMgr.setBooleanProperty(propertyId, area, val);

// setFloatProperty -> mCarPropertyMgr.setFloatProperty(propertyId, area, val);

// setIntProperty -> mCarPropertyMgr.setIntProperty(propertyId, area, val);

public void setBooleanProperty(@PropertyId int propertyId, int area, boolean val)

throws CarNotConnectedException {

if (mHvacPropertyIds.contains(propertyId)) {

mCarPropertyMgr.setBooleanProperty(propertyId, area, val);

}

}

}

CarService

  1. ICarImpl初始化多个Service,可通过getCarService返回对应的Service

public class CarService extends Service {

IVehicle mVehicle = android.hardware.automotive.vehicle.V2_0.IVehicle.getService();

ICarImpl mICarImpl = new ICarImpl(this,

mVehicle,

SystemInterface.Builder.defaultSystemInterface(this).build(),

mCanBusErrorNotifier,

mVehicleInterfaceName);

mICarImpl.init();

}

public class ICarImpl extends ICar.Stub {

public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,

CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {

mContext = serviceContext;

mSystemInterface = systemInterface;

mHal = new VehicleHal(vehicle);

mVehicleInterfaceName = vehicleInterfaceName;

mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);

mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(), systemInterface);

mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());

mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);

mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,

mCarDrivingStateService, mCarPropertyService);

mCarPackageManagerService = new CarPackageManagerService(serviceContext,

mCarUXRestrictionsService, mSystemActivityMonitoringService);

mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());

mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);

mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);

mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);

mCarAudioService = new CarAudioService(serviceContext, mCarPropertyService);

mCarNightService = new CarNightService(serviceContext, mCarPropertyService);

mInstrumentClusterService = new InstrumentClusterService(serviceContext, mAppFocusService, mCarInputService);

mSystemStateControllerService = new SystemStateControllerService(serviceContext,

mCarPowerManagementService, mCarAudioService, this);

mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);

mCarBluetoothService = new CarBluetoothService(serviceContext, mCarPropertyService,

mPerUserCarServiceHelper, mCarUXRestrictionsService);

mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());

mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());

mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());

mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext, systemInterface);

mCarConfigurationService = new CarConfigurationService(serviceContext, new JsonReaderImpl());

mUserManagerHelper = new CarUserManagerHelper(serviceContext);

mCarLocationService = new CarLocationService(mContext, mCarPowerManagementService,

mCarPropertyService, mUserManagerHelper);

addTboxService();

addUpgradeService();

// Be careful with order. Service depending on other service should be inited later.

List allServices = new ArrayList<>();

allServices.add(mSystemActivityMonitoringService);

allServices.add(mCarPowerManagementService);

allServices.add(mCarPropertyService); // ★ CarPropertyService

allServices.add(mCarDrivingStateService);

allServices.add(mCarUXRestrictionsService);

allServices.add(mCarPackageManagerService);

allServices.add(mCarInputService);

allServices.add(mGarageModeService);

allServices.add(mAppFocusService);

allServices.add(mCarAudioService);

allServices.add(mCarNightService);

allServices.add(mInstrumentClusterService);

allServices.add(mCarProjectionService);

allServices.add(mSystemStateControllerService);

allServices.add(mCarBluetoothService);

allServices.add(mCarDiagnosticService);

allServices.add(mPerUserCarServiceHelper);

allServices.add(mCarStorageMonitoringService);

allServices.add(mCarConfigurationService);

allServices.add(mVmsSubscriberService);

allServices.add(mVmsPublisherService);

if (mUserManagerHelper.isHeadlessSystemUser()) {

mCarUserService = new CarUserService(serviceContext, mUserManagerHelper);

allServices.add(mCarUserService);

}

allServices.add(mCarLocationService);

mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);

}

@MainThread

void init() {

traceBegin(“VehicleHal.init”);

mHal.init();

traceEnd();

traceBegin(“CarService.initAllServices”);

for (CarServiceBase service : mAllServices) {

service.init();

}

traceEnd();

}

@Override

public IBinder getCarService(String serviceName) {

switch (serviceName) {

case Car.AUDIO_SERVICE: return mCarAudioService;

case Car.APP_FOCUS_SERVICE: return mAppFocusService;

case Car.PACKAGE_SERVICE: return mCarPackageManagerService;

case Car.DIAGNOSTIC_SERVICE:

assertAnyDiagnosticPermission(mContext);

return mCarDiagnosticService;

case Car.POWER_SERVICE:

assertPowerPermission(mContext);

return mCarPowerManagementService;

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:

case Car.DYHVAC_SERVICE:

case Car.CAR_MCU_SERVICE:

case Car.DYCABIN_SERVICE:

case Car.DYSENSOR_SERVICE:

return mCarPropertyService; // ★ CarPropertyService

… …

}

}

}

public class VehicleHal extends IVehicleCallback.Stub {

public VehicleHal(IVehicle vehicle) {

final HandlerThread mHandlerThread = new HandlerThread(“VEHICLE-HAL”);

mHandlerThread.start();

// passing this should be safe as long as it is just kept and not used in constructor

final PowerHalService mPowerHal = new PowerHalService(this);

final PropertyHalService mPropertyHal = new PropertyHalService(this); // ★ PropertyHalService

final InputHalService mInputHal = new InputHalService(this);

final VmsHalService mVmsHal = new VmsHalService(this);

DiagnosticHalService mDiagnosticHal = new DiagnosticHalService(this);

final ArrayList mAllServices = new ArrayList<>();

mAllServices.addAll(Arrays.asList(mPowerHal,

mInputHal,

mPropertyHal,

mDiagnosticHal,

mVmsHal));

volatile HalClient mHalClient = new HalClient(vehicle, mHandlerThread.getLooper(), this /IVehicleCallback/);

}

}

  1. PropertyHalService通过getProperty、setProperty、subscribeProperty、unsubscribeProperty调用 VehicleHal:

public class PropertyHalService extends HalServiceBase {

public Map> getPropertyList() {

return mProps;

}

@Nullable

public CarPropertyValue getProperty(int mgrPropId, int areaId) {

int halPropId = managerToHalPropId(mgrPropId);

if (halPropId == NOT_SUPPORTED_PROPERTY) {

throw new IllegalArgumentException(“Invalid property Id : 0x” + toHexString(mgrPropId));

}

VehiclePropValue value = null;

try {

value = mVehicleHal.get(halPropId, areaId);

} catch (PropertyTimeoutException e) {

Log.e(CarLog.TAG_PROPERTY, “get, property not ready 0x” + toHexString(halPropId), e);

}

return value == null ? null : toCarPropertyValue(value, mgrPropId);

}

public void setProperty(CarPropertyValue prop) {

int halPropId = managerToHalPropId(prop.getPropertyId());

if (halPropId == NOT_SUPPORTED_PROPERTY) {

throw new IllegalArgumentException(“Invalid property Id : 0x” + toHexString(prop.getPropertyId()));

}

VehiclePropValue halProp = toVehiclePropValue(prop, halPropId);

try {

mVehicleHal.set(halProp);

} catch (PropertyTimeoutException e) {

Log.e(CarLog.TAG_PROPERTY, “set, property not ready 0x” + toHexString(halPropId), e);

throw new RuntimeException(e);

}

}

/**

  • Subscribe to this property at the specified update rate.

*/

public void subscribeProperty(int propId, float rate) {

if (mDbg) Log.d(TAG, “subscribeProperty propId=0x” + toHexString(propId) + “, rate=” + rate);

int halPropId = managerToHalPropId(propId);

if (halPropId == NOT_SUPPORTED_PROPERTY) {

throw new IllegalArgumentException(“Invalid property Id : 0x” + toHexString(propId));

}

// Validate the min/max rate

CarPropertyConfig cfg = mProps.get(propId);

if (rate > cfg.getMaxSampleRate()) {

rate = cfg.getMaxSampleRate();

} else if (rate < cfg.getMinSampleRate()) {

rate = cfg.getMinSampleRate();

}

synchronized (mSubscribedPropIds) { mSubscribedPropIds.add(halPropId); }

mVehicleHal.subscribeProperty(this, halPropId, rate);

}

public void unsubscribeProperty(int propId) {

if (mDbg) Log.d(TAG, “unsubscribeProperty propId=0x” + toHexString(propId));

int halPropId = managerToHalPropId(propId);

if (halPropId == NOT_SUPPORTED_PROPERTY) {

throw new IllegalArgumentException(“Invalid property Id : 0x” + toHexString(propId));

}

synchronized (mSubscribedPropIds) {

if (mSubscribedPropIds.contains(halPropId)) {

mSubscribedPropIds.remove(halPropId);

mVehicleHal.unsubscribeProperty(this, halPropId);

}

}

}

@Override

public Collection takeSupportedProperties(Collection allProperties) {

List taken = new LinkedList<>();

for (VehiclePropConfig p : allProperties) {

if (mPropIds.isSupportedProperty(p.prop)) {

CarPropertyConfig config = CarPropertyUtils.toCarPropertyConfig(p, p.prop);

taken.add§;

mProps.put(p.prop, config);

}

}

if (mDbg) Log.d(TAG, “takeSupportedProperties() took " + taken.size() + " properties”);

return taken;

}

}

ubscribedPropIds.contains(halPropId)) {

mSubscribedPropIds.remove(halPropId);

mVehicleHal.unsubscribeProperty(this, halPropId);

}

}

}

@Override

public Collection takeSupportedProperties(Collection allProperties) {

List taken = new LinkedList<>();

for (VehiclePropConfig p : allProperties) {

if (mPropIds.isSupportedProperty(p.prop)) {

CarPropertyConfig config = CarPropertyUtils.toCarPropertyConfig(p, p.prop);

taken.add§;

mProps.put(p.prop, config);

}

}

if (mDbg) Log.d(TAG, “takeSupportedProperties() took " + taken.size() + " properties”);

return taken;

}

}

你可能感兴趣的:(程序员,架构,移动开发,android)