Android车载 CarFrameWork——carservice启动流程
创始人
2025-05-28 11:03:03
0

carservice启动流程

大致流程:

  1. SystemServer启动CarServiceHelperService服务
  2. 在调用startService后,CarServiceHelperService的onStart方法通过bindService的方式启动CarService(一个系统级别的APK,位于system/priv-app)
  3. 启动CarService后首先调用onCreate,创建ICarImpl对象并初始化,在此时创建了一系列car相关的核心服务,并遍历init初始化
  4. 然后调用onBind将该ICarImpl对象返回给CarServiceHelperService,CarServiceHelperService在内部的一个Binder对象ICarServiceHelperImpl传递给CarService,建立双向跨进程

序列图

启动CarServiceHelperService服务

frameworks/base/services/java/com/android/server/SystemServer.java - run() —-> startOtherServices()

    private static final String CAR_SERVICE_HELPER_SERVICE_CLASS ="com.android.internal.car.CarServiceHelperService";......if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {traceBeginAndSlog("StartCarServiceHelperService");mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);traceEnd();}

—–> frameworks/base/services/core/java/com/android/server/SystemServiceManager.java - startService

    @SuppressWarnings("unchecked")public SystemService startService(String className) {....return startService(serviceClass);}
​public  T startService(Class serviceClass) {...startService(service);...}
​public void startService(@NonNull final SystemService service) {......try {service.onStart();...}

绑定carservice服务

—–> frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java - onStart()

    //这就是系统中和汽车相关的核心服务CarService,相关源代码在packages/services/Car/service目录下private static final String CAR_SERVICE_INTERFACE = "android.car.ICar";
​@Overridepublic void onStart() {Intent intent = new Intent();intent.setPackage("com.android.car");  //绑定包名,设置广播仅对该包有效//绑定action,表明想要启动能够响应设置的这个action的活动,并在清单文件AndroidManifest.xml中设置action属性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");}
  • service源码路径:packages/services/Car/service/AndroidManifest.xml

sharedUserId是系统级别的,类似SystemUI,它编译出来同样是一个APK文件

设备文件路径在: /system/priv-app/CarService/CarService.apk

//packages/services/Car/service/AndroidManifest.xml
 ......

bindService启动流程

context.bindService()  ——> onCreate()  ——> onBind()  ——> Service running  ——> onUnbind()  ——> onDestroy()  ——> Service stop

onBind()将返回给客户端一个IBind接口实例,IBind允许客户端回调服务的方法,比如得到Service的实例、运行状态或其他操作。这个时候把调用者(Context,例如Activity)会和Service绑定在一起,Context退出了,Srevice就会调用onUnbind->onDestroy相应退出。

所以调用bindService的生命周期为:onCreate --> onBind(只一次,不可多次绑定) --> onUnbind --> onDestroy

在Service每一次的开启关闭过程中,只有onStart可被多次调用(通过多次startService调用),其他onCreate,onBind,onUnbind,onDestroy在一个生命周期中只能被调用一次

Car Service启动

onCreate

——–> packages/services/Car/service/src/com/android/car/CarService.java - onCreate()

创建ICarImpl实例

    @Nullableprivate static IVehicle getVehicle() {try {//该service启动文件hardware/interfaces/automotive/vehicle/2.0/default/android.hardware.automotive.vehicle@2.0-service.rcreturn android.hardware.automotive.vehicle.V2_0.IVehicle.getService();} ....return null;}
​@Overridepublic void onCreate() {Log.i(CarLog.TAG_SERVICE, "Service onCreate");//获取hal层的Vehicle servicemVehicle = getVehicle();
​//创建ICarImpl实例mICarImpl = new ICarImpl(this,mVehicle,SystemInterface.Builder.defaultSystemInterface(this).build(),mCanBusErrorNotifier,mVehicleInterfaceName);//然后调用ICarImpl的init初始化方法mICarImpl.init();//设置boot.car_service_created属性SystemProperties.set("boot.car_service_created", "1");
​linkToDeath(mVehicle, mVehicleDeathRecipient);//最后将该service注册到ServiceManagerServiceManager.addService("car_service", mICarImpl);super.onCreate();}
//packages/services/Car/service/src/com/android/car/ICarImpl.javaprivate final VehicleHal mHal;//构造函数启动一大堆服务public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {mContext = serviceContext;mSystemInterface = systemInterface;//创建VehicleHal对象mHal = new VehicleHal(vehicle);mVehicleInterfaceName = vehicleInterfaceName;mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),systemInterface);mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());.....//InstrumentClusterService service启动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);
​//注意排序,service存在依赖List allServices = new ArrayList<>();allServices.add(mSystemActivityMonitoringService);allServices.add(mCarPowerManagementService);allServices.add(mCarPropertyService);allServices.add(mCarDrivingStateService);allServices.add(mCarUXRestrictionsService);allServices.add(mCarPackageManagerService);allServices.add(mCarInputService);allServices.add(mCarLocationService);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);}
​mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);}
​@MainThreadvoid init() {traceBegin("VehicleHal.init");mHal.init();traceEnd();traceBegin("CarService.initAllServices");//启动的所有服务遍历调用init初始化(各个都继承了CarServiceBase)for (CarServiceBase service : mAllServices) {service.init();}traceEnd();}

onBind

将上面onCreate创建的mICarImpl对象返回:

  1. onBind()回调方法会继续传递通过bindService()传递来的intent对象(即上面的bindServiceAsUser方法)
  2. onUnbind()会处理传递给unbindService()的intent对象。如果service允许绑定,onBind()会返回客户端与服务互相联系的通信句柄
//packages/services/Car/service/src/com/android/car/CarService.java@Overridepublic IBinder onBind(Intent intent) {return mICarImpl;}

所以此处的mICarImpl会作为IBinder返回给CarServiceHelperService.java - bindServiceAsUser方法中的参数mCarServiceConnection(回调)

onDestroy

释放mICarImpl创建的资源,包含一系列的服务:

    @Overridepublic void onDestroy() {Log.i(CarLog.TAG_SERVICE, "Service onDestroy");mICarImpl.release();mCanBusErrorNotifier.removeFailureReport(this);
​if (mVehicle != null) {try {mVehicle.unlinkToDeath(mVehicleDeathRecipient);mVehicle = null;} catch (RemoteException e) {// Ignore errors on shutdown path.}}
​super.onDestroy();}

回调ServiceConnection

ICarImpl初始化完毕,会作为IBinder返回给CarServiceHelperService.java - bindServiceAsUser方法中绑定此服务的mCarServiceConnection(回调)

mCarServiceConnection初始化如下:

  1. 其中返回的ICarImpl被保存在了CarServiceHelperService的mCarService
  2. mCarService.transact跨进程通信,调用ICar.aidl中定义的第一个方法setCarServiceHelper
//frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java
private static final String CAR_SERVICE_INTERFACE = "android.car.ICar";
private IBinder mCarService;
private final ICarServiceHelperImpl mHelper = new ICarServiceHelperImpl();
​
private final ServiceConnection mCarServiceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName componentName, IBinder iBinder) {Slog.i(TAG, "**CarService connected**");//1. 返回的ICarImpl被保存在了CarServiceHelperService的mCarServicemCarService = iBinder;// Cannot depend on ICar which is defined in CarService, so handle binder call directly// instead. // void setCarServiceHelper(in IBinder helper)Parcel data = Parcel.obtain();data.writeInterfaceToken(CAR_SERVICE_INTERFACE);//将ICarServiceHelperImpl类型的对象作为数据跨进程传递data.writeStrongBinder(mHelper.asBinder());try {//2.跨进程传输//对端是mCarService即ICarImpl,调用binder的transact进行跨进程通信//其code代表需要调用的对端方法,data为携带的传输数据//FIRST_CALL_TRANSACTION  = 0x00000001,即调用对端ICar.aidl中定义的第一个方法setCarServiceHelpermCarService.transact(IBinder.FIRST_CALL_TRANSACTION, // setCarServiceHelperdata, null, Binder.FLAG_ONEWAY);} catch (RemoteException e) {Slog.w(TAG, "RemoteException from car service", e);handleCarServiceCrash();}}
​@Override public void onServiceDisconnected(ComponentName componentName) {handleCarServiceCrash();}};

跨进程setCarServiceHelper

    @Overridepublic void setCarServiceHelper(IBinder helper) {int uid = Binder.getCallingUid();if (uid != Process.SYSTEM_UID) {throw new SecurityException("Only allowed from system");}synchronized (this) {//将ICarServiceHelper的代理端保存在ICarImpl内部mICarServiceHelpermICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);//同时也传给了SystemInterface//此时他们有能力跨进程访问CarServiceHelperServicemSystemInterface.setCarServiceHelper(mICarServiceHelper);}}

相关内容

热门资讯

“超越日本,中国首次跃居首位” 据《日本经济新闻》网站6月4日报道,中国在氢相关专利竞争力方面超越日本,首次跃居首位。中国企业主要在...
菏泽一高考考生午休躺在路边休息... 6月7日,全国高考。山东菏泽,有网友拍到暖心一幕,中午高考午休时间,一名考生躺在路边休息,旁边的家人...
海关律师、走私辩护律师邵丹:查... 近日,海口海关所属海口美兰机场海关联合海口海关风控分局,发现海口飞往遵义的3名旅客购物时间趋近、购物...
海关律师、走私辩护律师邵丹:查... 近日,哈尔滨海关所属哈尔滨太平机场海关关员在对进境航班实施监管时,发现1名选择“无申报通道”入境的旅...
中国女排3比0击败法国女排,斩... 北京时间6月7日晚,中国女排再度亮相世界女排联赛中国北京站,与法国女排展开对抗。 最终,中国女排以3...
《政务数据共享条例》来了,哪些... 《政务数据共享条例》近日正式出台,目的是推进政务数据安全有序高效共享利用,提升政府数字化治理能力和政...
对华新招?美国被曝暂停向中国出... 【文/观察者网 齐倩】在中美贸易紧张关系持续之际,路透社6月6日援引消息称,特朗普政府近日暂停了美国...
欧洲央行执委Schnabel:... 欧洲央行执委Isabel Schnabel称,对美联储与欧洲央行政策将长期分化的预期是错误的。“我预...
十款大模型写高考作文|Deep... 6月7日,2025年全国高考拉开大幕。过去两年,搜狐科技&搜狐教育多次联合推出大模型参加高考系列策划...
事发淄博一村庄!陌生男子形迹可... 一个看似寻常的午后, 一名陌生男子的身影 打破了村庄的宁静。 近日,沂源县南麻街道某村村民张先生(化...