当前位置:   article > 正文

Android Automotive架构与流程:VehicleHAL,CarService(1)

android automotive

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);

}

public 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<Integer, CarPropertyConfig<?>> 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);

}

}

/**

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

总结

Android架构学习进阶是一条漫长而艰苦的道路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

上面分享的字节跳动公司2020年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。详情可以点击我的【Github】
如果你熟练掌握【Github】中列出的知识点,相信将会大大增加你通过前两轮技术面试的几率!这些内容都供大家参考,互相学习。

就先写到这,码字不易,写的很片面不好之处敬请指出,如果觉得有参考价值的朋友也可以关注一下我

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以我的【Github】阅读下载,最后觉得有帮助、有需要的朋友可以点个赞

路,不能靠一时激情,更不是熬几天几夜就能学好的,必须养成平时努力学习的习惯。所以:贵在坚持!

上面分享的字节跳动公司2020年的面试真题解析大全,笔者还把一线互联网企业主流面试技术要点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。详情可以点击我的【Github】
如果你熟练掌握【Github】中列出的知识点,相信将会大大增加你通过前两轮技术面试的几率!这些内容都供大家参考,互相学习。

就先写到这,码字不易,写的很片面不好之处敬请指出,如果觉得有参考价值的朋友也可以关注一下我

①「Android面试真题解析大全」PDF完整高清版+②「Android面试知识体系」学习思维导图压缩包——————可以我的【Github】阅读下载,最后觉得有帮助、有需要的朋友可以点个赞

[外链图片转存中…(img-soZE80Dv-1710758222318)]

[外链图片转存中…(img-MeyMz4jc-1710758222318)]

[外链图片转存中…(img-8DQFPbrR-1710758222318)]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/636427
推荐阅读
相关标签
  

闽ICP备14008679号