android10 CarService

android10 CarService_第1张图片

图 CarService架构

Android Automotive OS定义了标准的硬件抽象层(HAL)来规范各个子系统与Framework的调用接口,并且通过CarService及其相关的Car API对上层应用提供标准编程接口。通过这张图可以看出CarService运行在独立的进程中,其作为原有Android服务的补充。

CarService的启动流程

android10 CarService_第2张图片

CarService 时序图

xref: /frameworks/base/services/java/com/android/server/SystemServer.java

265      private static final String CAR_SERVICE_HELPER_SERVICE_CLASS =
266              "com.android.internal.car.CarServiceHelperService";

..........

2066              if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE)) {
2067                  traceBeginAndSlog("StartCarServiceHelperService");
2068                  mSystemServiceManager.startService(CAR_SERVICE_HELPER_SERVICE_CLASS);
2069                  traceEnd();
2070              }

通过SystemServiceManager开启服务并传人参数CAR_SERVICE_HELPER_SERVICE_CLASS。

xref: /frameworks/base/services/core/java/com/android/server/SystemServiceManager.java

 
78      /**
79       * Creates and starts a system service. The class must be a subclass of
80       * {@link com.android.server.SystemService}.
81       *
82       * @param serviceClass A Java class that implements the SystemService interface.
83       * @return The service instance, never null.
84       * @throws RuntimeException if the service fails to start.
85       */
86      @SuppressWarnings("unchecked")
87      public  T startService(Class serviceClass) {
88          try {
89              final String name = serviceClass.getName();
90              Slog.i(TAG, "Starting " + name);
91              Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "StartService " + name);
92  
93              // Create the service.
94              if (!SystemService.class.isAssignableFrom(serviceClass)) {
95                  throw new RuntimeException("Failed to create " + name
96                          + ": service must extend " + SystemService.class.getName());
97              }
98              final T service;
99              try {
100                  Constructor constructor = serviceClass.getConstructor(Context.class);
101                  service = constructor.newInstance(mContext);
102              } catch (InstantiationException ex) {
103                  throw new RuntimeException("Failed to create service " + name
104                          + ": service could not be instantiated", ex);
105              } catch (IllegalAccessException ex) {
106                  throw new RuntimeException("Failed to create service " + name
107                          + ": service must have a public constructor with a Context argument", ex);
108              } catch (NoSuchMethodException ex) {
109                  throw new RuntimeException("Failed to create service " + name
110                          + ": service must have a public constructor with a Context argument", ex);
111              } catch (InvocationTargetException ex) {
112                  throw new RuntimeException("Failed to create service " + name
113                          + ": service constructor threw an exception", ex);
114              }
115  
116              startService(service);
117              return service;
118          } finally {
119              Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
120          }
121      }
122  
123      public void startService(@NonNull final SystemService service) {
124          // Register it.
125          mServices.add(service);
126          // Start it.
127          long time = SystemClock.elapsedRealtime();
128          try {
129              service.onStart();
130          } catch (RuntimeException ex) {
131              throw new RuntimeException("Failed to start service " + service.getClass().getName()
132                      + ": onStart threw an exception", ex);
133          }
134          warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
135      }

SystemServiceManager会通过反射创建CarServiceHelperService对象,并调用其onStart方法。

xref: /frameworks/opt/car/services/src/com/android/internal/car/CarServiceHelperService.java

63      private static final String CAR_SERVICE_INTERFACE = "android.car.ICar";

...........

142      @Override
143      public void onStart() {
144          Intent intent = new Intent();
145          intent.setPackage("com.android.car");
146          intent.setAction(CAR_SERVICE_INTERFACE);
147          if (!getContext().bindServiceAsUser(intent, mCarServiceConnection, Context.BIND_AUTO_CREATE,
148                  UserHandle.SYSTEM)) {
149              Slog.wtf(TAG, "cannot start car service");
150          }
151          System.loadLibrary("car-framework-service-jni");
152      }

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

507         
509             
510                 
511             
512         

在CarServiceHelperService的onStart方法中,通过intent设置action然后绑定服务就创建了CarService进程。

xref: /packages/services/Car/service/src/com/android/car/CarService.java

 
72      @Override
73      public void onCreate() {
74          Log.i(CarLog.TAG_SERVICE, "Service onCreate");
75          mCanBusErrorNotifier = new CanBusErrorNotifier(this /* context */);
76          mVehicle = getVehicle();
77  
78          if (mVehicle == null) {
79              throw new IllegalStateException("Vehicle HAL service is not available.");
80          }
81          try {
82              mVehicleInterfaceName = mVehicle.interfaceDescriptor();
83          } catch (RemoteException e) {
84              throw new IllegalStateException("Unable to get Vehicle HAL interface descriptor", e);
85          }
86  
87          Log.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName);
88  
89          mICarImpl = new ICarImpl(this,
90                  mVehicle,
91                  SystemInterface.Builder.defaultSystemInterface(this).build(),
92                  mCanBusErrorNotifier,
93                  mVehicleInterfaceName);
94          mICarImpl.init();
95  
96          linkToDeath(mVehicle, mVehicleDeathRecipient);
97  
98          ServiceManager.addService("car_service", mICarImpl);
99          SystemProperties.set("boot.car_service_created", "1");
100          super.onCreate();
101      }

创建mVehicle、mICarImpl对象,并将mICarImpl添加到ServiceManager中。

xref: /packages/services/Car/service/src/com/android/car/ICarImpl.java

117      public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
118              CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
119          mContext = serviceContext;
120          mSystemInterface = systemInterface;
121          mHal = new VehicleHal(vehicle);
122          mVehicleInterfaceName = vehicleInterfaceName;
123          mUserManagerHelper = new CarUserManagerHelper(serviceContext);
124          final Resources res = mContext.getResources();
125          final int maxRunningUsers = res.getInteger(
126                  com.android.internal.R.integer.config_multiuserMaxRunningUsers);
127          mCarUserService = new CarUserService(serviceContext, mUserManagerHelper,
128                  ActivityManager.getService(), maxRunningUsers);
129          mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
130          mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
131                  systemInterface, mUserManagerHelper);
132          mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
133          mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
134          mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
135                  mCarDrivingStateService, mCarPropertyService);
136          mCarPackageManagerService = new CarPackageManagerService(serviceContext,
137                  mCarUXRestrictionsService,
138                  mSystemActivityMonitoringService,
139                  mUserManagerHelper);
140          mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
141          mCarBluetoothService = new CarBluetoothService(serviceContext, mPerUserCarServiceHelper);
142          mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
143          mCarProjectionService = new CarProjectionService(
144                  serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
145          mGarageModeService = new GarageModeService(mContext);
146          mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
147          mCarAudioService = new CarAudioService(serviceContext);
148          mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
149          mInstrumentClusterService = new InstrumentClusterService(serviceContext,
150                  mAppFocusService, mCarInputService);
151          mSystemStateControllerService = new SystemStateControllerService(
152                  serviceContext, mCarAudioService, this);
153          mVmsBrokerService = new VmsBrokerService(mContext.getPackageManager());
154          mVmsClientManager = new VmsClientManager(
155                  serviceContext, mCarUserService, mUserManagerHelper, mHal.getVmsHal());
156          mVmsSubscriberService = new VmsSubscriberService(
157                  serviceContext, mVmsBrokerService, mHal.getVmsHal());
158          mVmsPublisherService = new VmsPublisherService(
159                  serviceContext, mVmsBrokerService, mVmsClientManager);
160          mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
161          mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
162                  systemInterface);
163          mCarConfigurationService =
164                  new CarConfigurationService(serviceContext, new JsonReaderImpl());
165          mCarLocationService = new CarLocationService(mContext, mUserManagerHelper);
166          mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
167          mCarMediaService = new CarMediaService(serviceContext);
168          mCarBugreportManagerService = new CarBugreportManagerService(serviceContext);
169  
170          CarLocalServices.addService(CarPowerManagementService.class, mCarPowerManagementService);
171          CarLocalServices.addService(CarUserService.class, mCarUserService);
172          CarLocalServices.addService(CarTrustedDeviceService.class, mCarTrustedDeviceService);
173          CarLocalServices.addService(SystemInterface.class, mSystemInterface);
174          CarLocalServices.addService(CarDrivingStateService.class, mCarDrivingStateService);
175          CarLocalServices.addService(PerUserCarServiceHelper.class, mPerUserCarServiceHelper);
176  
177          // Be careful with order. Service depending on other service should be inited later.
178          List allServices = new ArrayList<>();
179          allServices.add(mCarUserService);
180          allServices.add(mSystemActivityMonitoringService);
181          allServices.add(mCarPowerManagementService);
182          allServices.add(mCarPropertyService);
183          allServices.add(mCarDrivingStateService);
184          allServices.add(mCarUXRestrictionsService);
185          allServices.add(mCarPackageManagerService);
186          allServices.add(mCarInputService);
187          allServices.add(mGarageModeService);
188          allServices.add(mAppFocusService);
189          allServices.add(mCarAudioService);
190          allServices.add(mCarNightService);
191          allServices.add(mInstrumentClusterService);
192          allServices.add(mSystemStateControllerService);
193          allServices.add(mPerUserCarServiceHelper);
194          allServices.add(mCarBluetoothService);
195          allServices.add(mCarProjectionService);
196          allServices.add(mCarDiagnosticService);
197          allServices.add(mCarStorageMonitoringService);
198          allServices.add(mCarConfigurationService);
199          allServices.add(mVmsClientManager);
200          allServices.add(mVmsSubscriberService);
201          allServices.add(mVmsPublisherService);
202          allServices.add(mCarTrustedDeviceService);
203          allServices.add(mCarMediaService);
204          allServices.add(mCarLocationService);
205          allServices.add(mCarBugreportManagerService);
206          mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
207      }

在ICarImpl里面创建各种服务并添加到allServices中。

Car API的使用方式

Car carClient = Car.createCar(context);
CarHvacManager manager = (CarHvacManager) carClient.getCarManager(Car.HVAC_SERVICE);

你可能感兴趣的:(android)