blob: d508d5e4117f4ceb1f8c3cdf21bf58a435b32bab [file] [log] [blame]
keunyoungca515072015-07-10 12:21:47 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car;
18
Pavel Maltsevabd47232017-10-10 16:54:57 -070019import android.annotation.MainThread;
Yao Chene33f07e2016-07-26 12:02:51 -070020import android.app.UiModeManager;
Keun-young Parke54ac272016-02-16 19:02:18 -080021import android.car.Car;
22import android.car.ICar;
Pavel Maltsev0477e292016-05-27 12:22:36 -070023import android.car.cluster.renderer.IInstrumentClusterNavigation;
Ying Zheng9fc99402018-09-19 14:23:59 -070024import android.car.userlib.CarUserManagerHelper;
keunyoungca515072015-07-10 12:21:47 -070025import android.content.Context;
keunyoung1ab8e182015-09-24 09:25:22 -070026import android.content.pm.PackageManager;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080027import android.hardware.automotive.vehicle.V2_0.IVehicle;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -080028import android.hardware.automotive.vehicle.V2_0.VehicleArea;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070029import android.os.Binder;
Enrico Granatae8056ca2018-04-03 13:19:52 -070030import android.os.Build;
keunyoungca515072015-07-10 12:21:47 -070031import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070032import android.os.Process;
Steve Paikc302c7c2017-08-04 14:01:58 -070033import android.os.Trace;
keunyoungca515072015-07-10 12:21:47 -070034import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070035import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070036import android.util.TimingsTraceLog;
Gregory Clark26fa6012018-03-14 18:38:56 -070037
Hongwei Wang38549092018-08-22 12:32:42 -070038import com.android.car.audio.CarAudioService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080039import com.android.car.cluster.InstrumentClusterService;
Serik Beketayev06a66682018-06-12 16:54:29 -070040import com.android.car.garagemode.GarageModeService;
keunyoungcc449f72015-08-12 10:46:27 -070041import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080042import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080043import com.android.car.pm.CarPackageManagerService;
Enrico Granatab19bc322017-10-12 12:25:06 -070044import com.android.car.systeminterface.SystemInterface;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070045import com.android.car.trust.CarTrustedDeviceService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070046import com.android.car.user.CarUserService;
Mark Tabry76cfaaa2019-02-01 14:28:30 -080047import com.android.car.vms.VmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -080048import com.android.car.vms.VmsClientManager;
keunyoungca515072015-07-10 12:21:47 -070049import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000050import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070051
Enrico Granatae8056ca2018-04-03 13:19:52 -070052import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070053import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070054import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080055import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070056import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070057
keunyoungca515072015-07-10 12:21:47 -070058public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070059
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080060 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070061 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
62 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080063
keunyoungca515072015-07-10 12:21:47 -070064 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080065 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070066
Anthony Chen12aec302018-04-25 16:41:48 -070067 private final SystemInterface mSystemInterface;
68
Keun-young Park4727da32016-05-31 10:00:51 -070069 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070070 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080071 private final CarPackageManagerService mCarPackageManagerService;
72 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080073 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080074 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070075 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080076 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070077 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080078 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070079 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080080 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080081 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080082 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070083 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080084 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070085 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070086 private final CarDiagnosticService mCarDiagnosticService;
87 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -070088 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadide0ca082019-03-20 11:16:44 -070089 private final CarTrustedDeviceService mCarTrustedDeviceService;
Simon Dai527eb552019-02-12 13:06:15 -080090 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070091 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -080092 private final CarUserService mCarUserService;
Mark Tabrya8fce562019-01-16 16:24:01 -080093 private final VmsClientManager mVmsClientManager;
Mark Tabry76cfaaa2019-02-01 14:28:30 -080094 private final VmsBrokerService mVmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -080095 private final VmsSubscriberService mVmsSubscriberService;
96 private final VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070097
Pavel Maltsev0d07c762016-11-03 16:40:15 -070098 private final CarServiceBase[] mAllServices;
99
Steve Paikc302c7c2017-08-04 14:01:58 -0700100 private static final String TAG = "ICarImpl";
101 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700102
103 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700104
keunyoung1ab8e182015-09-24 09:25:22 -0700105 /** Test only service. Populate it only when necessary. */
106 @GuardedBy("this")
107 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700108
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700109 @GuardedBy("this")
110 private ICarServiceHelper mICarServiceHelper;
111
Enrico Granatae8056ca2018-04-03 13:19:52 -0700112 private final String mVehicleInterfaceName;
113
Pavel Maltsevec83b632017-01-05 15:10:55 -0800114 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700115 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700116 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800117 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700118 mHal = new VehicleHal(vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700119 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800120 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Keun-young Parkd462a912019-02-11 08:53:42 -0800121 mCarUserService = new CarUserService(serviceContext, mUserManagerHelper);
Keun-young Park4727da32016-05-31 10:00:51 -0700122 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800123 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700124 systemInterface, mUserManagerHelper);
Steve Paik9ec53d72018-04-27 13:28:31 -0700125 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
126 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800127 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800128 mCarDrivingStateService, mCarPropertyService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800129 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
130 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700131 mSystemActivityMonitoringService);
Justin Paupore44985ba2019-01-30 18:53:41 -0800132 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
133 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarPropertyService,
134 mPerUserCarServiceHelper, mCarUXRestrictionsService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700135 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800136 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800137 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700138 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700139 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800140 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700141 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700142 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700143 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700144 mSystemStateControllerService = new SystemStateControllerService(
145 serviceContext, mCarAudioService, this);
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800146 mVmsBrokerService = new VmsBrokerService();
Mark Tabrya8fce562019-01-16 16:24:01 -0800147 mVmsClientManager = new VmsClientManager(serviceContext, mUserManagerHelper);
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800148 mVmsSubscriberService = new VmsSubscriberService(
149 serviceContext, mVmsBrokerService, mHal.getVmsHal());
150 mVmsPublisherService = new VmsPublisherService(
151 serviceContext, mVmsBrokerService, mVmsClientManager, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700152 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700153 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
154 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700155 mCarConfigurationService =
156 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Gregory Clarka440e812019-02-14 16:05:51 -0800157 mCarLocationService = new CarLocationService(mContext, mCarPropertyService,
158 mUserManagerHelper);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700159 mCarTrustedDeviceService = new CarTrustedDeviceService(serviceContext);
Simon Dai527eb552019-02-12 13:06:15 -0800160 mCarMediaService = new CarMediaService(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -0700161
Keun-young Parkd462a912019-02-11 08:53:42 -0800162 CarLocalServices.addService(CarUserService.class, mCarUserService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700163 Log.d(TAG, "Adding CarTrustedDeviceService");
164 CarLocalServices.addService(CarTrustedDeviceService.class,
165 mCarTrustedDeviceService);
Gregory Clarka440e812019-02-14 16:05:51 -0800166 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Keun-young Parkd462a912019-02-11 08:53:42 -0800167
keunyounga3b28d82015-08-25 13:05:15 -0700168 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700169 List<CarServiceBase> allServices = new ArrayList<>();
Keun-young Parkd462a912019-02-11 08:53:42 -0800170 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700171 allServices.add(mSystemActivityMonitoringService);
172 allServices.add(mCarPowerManagementService);
173 allServices.add(mCarPropertyService);
174 allServices.add(mCarDrivingStateService);
175 allServices.add(mCarUXRestrictionsService);
176 allServices.add(mCarPackageManagerService);
177 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700178 allServices.add(mGarageModeService);
179 allServices.add(mAppFocusService);
180 allServices.add(mCarAudioService);
181 allServices.add(mCarNightService);
182 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700183 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700184 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800185 allServices.add(mCarBluetoothService);
186 allServices.add(mCarProjectionService);
187 allServices.add(mCarDiagnosticService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700188 allServices.add(mCarStorageMonitoringService);
189 allServices.add(mCarConfigurationService);
Mark Tabrya8fce562019-01-16 16:24:01 -0800190 allServices.add(mVmsClientManager);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700191 allServices.add(mVmsSubscriberService);
192 allServices.add(mVmsPublisherService);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700193 allServices.add(mCarTrustedDeviceService);
Simon Dai527eb552019-02-12 13:06:15 -0800194 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700195 allServices.add(mCarLocationService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700196 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700197 }
198
Pavel Maltsevabd47232017-10-10 16:54:57 -0700199 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700200 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700201 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700202 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700203 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700204 traceEnd();
205 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800206 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700207 service.init();
208 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700209 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700210 }
211
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700212 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700213 // release done in opposite order from init
214 for (int i = mAllServices.length - 1; i >= 0; i--) {
215 mAllServices[i].release();
216 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700217 mHal.release();
Keun-young Parkd462a912019-02-11 08:53:42 -0800218 CarLocalServices.removeAllServices();
keunyoung1ab8e182015-09-24 09:25:22 -0700219 }
220
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700221 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800222 mHal.vehicleHalReconnected(vehicle);
223 for (CarServiceBase service : mAllServices) {
224 service.vehicleHalReconnected();
225 }
226 }
227
keunyoungca515072015-07-10 12:21:47 -0700228 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700229 public void setCarServiceHelper(IBinder helper) {
230 int uid = Binder.getCallingUid();
231 if (uid != Process.SYSTEM_UID) {
232 throw new SecurityException("Only allowed from system");
233 }
234 synchronized (this) {
235 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800236 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700237 }
238 }
239
240 @Override
Keun-young Parkd462a912019-02-11 08:53:42 -0800241 public void setUserLockStatus(int userHandle, int unlocked) {
242 int uid = Binder.getCallingUid();
243 if (uid != Process.SYSTEM_UID) {
244 throw new SecurityException("Only allowed from system");
245 }
246 mCarUserService.setUserLockStatus(userHandle, unlocked == 1);
247 }
248
249 @Override
keunyoungca515072015-07-10 12:21:47 -0700250 public IBinder getCarService(String serviceName) {
251 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800252 case Car.AUDIO_SERVICE:
253 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700254 case Car.APP_FOCUS_SERVICE:
255 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800256 case Car.PACKAGE_SERVICE:
257 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800258 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700259 assertAnyDiagnosticPermission(mContext);
260 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800261 case Car.POWER_SERVICE:
262 assertPowerPermission(mContext);
263 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700264 case Car.CABIN_SERVICE:
265 case Car.HVAC_SERVICE:
266 case Car.INFO_SERVICE:
267 case Car.PROPERTY_SERVICE:
268 case Car.SENSOR_SERVICE:
269 case Car.VENDOR_EXTENSION_SERVICE:
270 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800271 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700272 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700273 IInstrumentClusterNavigation navService =
274 mInstrumentClusterService.getNavigationService();
275 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700276 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
277 assertClusterManagerPermission(mContext);
278 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800279 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800280 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800281 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700282 assertVmsSubscriberPermission(mContext);
283 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800284 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700285 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700286 synchronized (this) {
287 if (mCarTestService == null) {
288 mCarTestService = new CarTestService(mContext, this);
289 }
290 return mCarTestService;
291 }
292 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700293 case Car.BLUETOOTH_SERVICE:
294 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700295 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700296 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700297 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800298 case Car.CAR_DRIVING_STATE_SERVICE:
299 assertDrivingStatePermission(mContext);
300 return mCarDrivingStateService;
301 case Car.CAR_UX_RESTRICTION_SERVICE:
302 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700303 case Car.CAR_CONFIGURATION_SERVICE:
304 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800305 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
306 assertTrustAgentEnrollmentPermission(mContext);
Ram Periathiruvadide0ca082019-03-20 11:16:44 -0700307 return mCarTrustedDeviceService.getCarTrustAgentEnrollmentService();
Simon Dai527eb552019-02-12 13:06:15 -0800308 case Car.CAR_MEDIA_SERVICE:
309 return mCarMediaService;
keunyoungca515072015-07-10 12:21:47 -0700310 default:
311 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
312 return null;
313 }
314 }
315
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800316 @Override
317 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700318 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800319 }
320
Keun-young Parka28d7b22016-02-29 16:54:29 -0800321 public CarServiceBase getCarInternalService(String serviceName) {
322 switch (serviceName) {
323 case INTERNAL_INPUT_SERVICE:
324 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700325 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
326 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800327 default:
328 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
329 serviceName);
330 return null;
331 }
332 }
333
keunyoung1ab8e182015-09-24 09:25:22 -0700334 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700335 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700336 }
337
Keun-young Parke31a8b22016-03-16 17:34:08 -0700338 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700339 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800340 }
341
Pavel Maltsev905968c2017-07-16 19:48:57 -0700342 public static void assertClusterManagerPermission(Context context) {
343 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
344 }
345
Steve Paik388d7772018-02-12 10:54:51 -0800346 public static void assertPowerPermission(Context context) {
347 assertPermission(context, Car.PERMISSION_CAR_POWER);
348 }
349
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800350 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700351 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
352 }
353
Pavel Maltsev079873b2019-02-25 12:15:09 -0800354 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
355 public static void assertProjectionStatusPermission(Context context) {
356 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
357 }
358
Enrico Granata3c7a6662017-02-23 18:07:59 -0800359 public static void assertAnyDiagnosticPermission(Context context) {
360 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700361 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800362 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
363 }
364
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800365 public static void assertDrivingStatePermission(Context context) {
366 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
367 }
368
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800369 public static void assertVmsPublisherPermission(Context context) {
370 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
371 }
372
Antonio Cortese4619c72017-02-02 07:53:27 -0800373 public static void assertVmsSubscriberPermission(Context context) {
374 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
375 }
376
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800377 /**
378 * Ensures the caller has the permission to enroll a Trust Agent.
379 * @param context
380 */
381 public static void assertTrustAgentEnrollmentPermission(Context context) {
382 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
383 }
384
Steve Paik461ecc62016-06-08 15:28:32 -0700385 public static void assertPermission(Context context, String permission) {
386 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
387 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800388 }
389 }
390
Steve Paik9ec53d72018-04-27 13:28:31 -0700391 /**
392 * Checks to see if the caller has a permission.
393 * @param context
394 * @param permission
395 *
396 * @return boolean TRUE if caller has the permission.
397 */
398 public static boolean hasPermission(Context context, String permission) {
399 return context.checkCallingOrSelfPermission(permission)
400 == PackageManager.PERMISSION_GRANTED;
401 }
402
Enrico Granata3c7a6662017-02-23 18:07:59 -0800403 public static void assertAnyPermission(Context context, String... permissions) {
404 for (String permission : permissions) {
405 if (context.checkCallingOrSelfPermission(permission) ==
406 PackageManager.PERMISSION_GRANTED) {
407 return;
408 }
409 }
410 throw new SecurityException("requires any of " + Arrays.toString(permissions));
411 }
412
Enrico Granatae8056ca2018-04-03 13:19:52 -0700413 @Override
414 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
415 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700416 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700417 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700418 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
419 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700420 return;
keunyounga3b28d82015-08-25 13:05:15 -0700421 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700422 if (args == null || args.length == 0) {
423 writer.println("*dump car service*");
424
425 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
426 writer.println("*Dump all services*");
427 for (CarServiceBase service : mAllServices) {
428 dumpService(service, writer);
429 }
430 if (mCarTestService != null) {
431 dumpService(mCarTestService, writer);
432 }
433 writer.println("*Dump Vehicle HAL*");
434 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
435 try {
436 // TODO dump all feature flags by creating a dumpable interface
437 mHal.dump(writer);
438 } catch (Exception e) {
439 writer.println("Failed dumping: " + mHal.getClass().getName());
440 e.printStackTrace(writer);
441 }
442 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
443 execShellCmd(args, writer);
444 } else {
445 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800446 }
447 }
448
449 private void dumpService(CarServiceBase service, PrintWriter writer) {
450 try {
451 service.dump(writer);
452 } catch (Exception e) {
453 writer.println("Failed dumping: " + service.getClass().getName());
454 e.printStackTrace(writer);
455 }
keunyoungcc449f72015-08-12 10:46:27 -0700456 }
Yao Chene33f07e2016-07-26 12:02:51 -0700457
458 void execShellCmd(String[] args, PrintWriter writer) {
459 new CarShellCommand().exec(args, writer);
460 }
461
Pavel Maltsevabd47232017-10-10 16:54:57 -0700462 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700463 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700464 Slog.i(TAG, name);
465 mBootTiming.traceBegin(name);
466 }
467
Pavel Maltsevabd47232017-10-10 16:54:57 -0700468 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700469 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700470 mBootTiming.traceEnd();
471 }
472
Yao Chene33f07e2016-07-26 12:02:51 -0700473 private class CarShellCommand {
474 private static final String COMMAND_HELP = "-h";
475 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800476 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800477 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800478 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800479 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800480 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800481 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Jim Kayed76c2742019-04-02 11:33:27 -0700482 private static final String COMMAND_RESUME = "resume";
483 private static final String COMMAND_SUSPEND = "suspend";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800484
Yao Chene33f07e2016-07-26 12:02:51 -0700485 private static final String PARAM_DAY_MODE = "day";
486 private static final String PARAM_NIGHT_MODE = "night";
487 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800488 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800489 private static final String PARAM_ON_MODE = "on";
490 private static final String PARAM_OFF_MODE = "off";
491 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800492
Yao Chene33f07e2016-07-26 12:02:51 -0700493
494 private void dumpHelp(PrintWriter pw) {
495 pw.println("Car service commands:");
496 pw.println("\t-h");
497 pw.println("\t Print this help text.");
498 pw.println("\tday-night-mode [day|night|sensor]");
499 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800500 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800501 pw.println("\t Inject a vehicle property for testing.");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800502 pw.println("\tdisable-uxr true|false");
503 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800504 pw.println("\tgarage-mode [on|off|query]");
505 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800506 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800507 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800508 pw.println("\tget-carpropertyconfig [propertyId]");
509 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Jim Kayed76c2742019-04-02 11:33:27 -0700510 pw.println("\tsuspend");
511 pw.println("\t Suspend the system to Deep Sleep.");
512 pw.println("\tresume");
513 pw.println("\t Wake the system up after a 'suspend.'");
Yao Chene33f07e2016-07-26 12:02:51 -0700514 }
515
516 public void exec(String[] args, PrintWriter writer) {
517 String arg = args[0];
518 switch (arg) {
519 case COMMAND_HELP:
520 dumpHelp(writer);
521 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800522 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800523 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700524 forceDayNightMode(value, writer);
525 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800526 }
527 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800528 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800529 forceGarageMode(value, writer);
530 break;
531 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800532 case COMMAND_INJECT_VHAL_EVENT:
533 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
534 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800535 if (args.length != 3 && args.length != 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800536 writer.println("Incorrect number of arguments.");
537 dumpHelp(writer);
538 break;
Jim Kayef10b7d82019-02-26 12:53:22 -0800539 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800540 // Zoned
541 zone = args[2];
542 data = args[3];
543 } else {
544 // Global
545 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800546 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800547 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800548 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800549 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800550 if (args.length != 2) {
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800551 writer.println("Incorrect number of arguments");
552 dumpHelp(writer);
553 break;
554 }
555 boolean enableBlocking = Boolean.valueOf(args[1]);
556 if (mCarPackageManagerService != null) {
557 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
558 }
559 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800560 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800561 if (args.length != 2) {
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800562 writer.println("Incorrect number of arguments");
563 dumpHelp(writer);
564 break;
565 }
566 String pkgName = args[1].toLowerCase();
567 if (mCarPackageManagerService != null) {
568 String[] doActivities =
569 mCarPackageManagerService.getDistractionOptimizedActivities(
570 pkgName);
571 if (doActivities != null) {
572 writer.println("DO Activities for " + pkgName);
573 for (String a : doActivities) {
574 writer.println(a);
575 }
576 } else {
577 writer.println("No DO Activities for " + pkgName);
578 }
579 }
580 break;
Kaidd4ef5a2019-03-04 14:07:26 -0800581 case COMMAND_GET_CARPROPERTYCONFIG:
582 String propertyId = args.length < 2 ? "" : args[1];
583 mHal.dumpPropertyConfigs(writer, propertyId);
584 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -0800585 case COMMAND_PROJECTION_UI_MODE:
586 if (args.length != 2) {
587 writer.println("Incorrect number of arguments");
588 dumpHelp(writer);
589 break;
590 }
591 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -0700592 break;
Jim Kayed76c2742019-04-02 11:33:27 -0700593 case COMMAND_RESUME:
594 mCarPowerManagementService.forceSimulatedResume();
595 writer.println("Resume: Simulating resuming from Deep Sleep");
596 break;
597 case COMMAND_SUSPEND:
598 mCarPowerManagementService.forceSimulatedSuspend();
599 writer.println("Resume: Simulating powering down to Deep Sleep");
600 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700601 default:
Jim Kayef10b7d82019-02-26 12:53:22 -0800602 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -0700603 dumpHelp(writer);
604 }
605 }
606
607 private void forceDayNightMode(String arg, PrintWriter writer) {
608 int mode;
609 switch (arg) {
610 case PARAM_DAY_MODE:
611 mode = CarNightService.FORCED_DAY_MODE;
612 break;
613 case PARAM_NIGHT_MODE:
614 mode = CarNightService.FORCED_NIGHT_MODE;
615 break;
616 case PARAM_SENSOR_MODE:
617 mode = CarNightService.FORCED_SENSOR_MODE;
618 break;
619 default:
620 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
621 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
622 return;
623 }
624 int current = mCarNightService.forceDayNightMode(mode);
625 String currentMode = null;
626 switch (current) {
627 case UiModeManager.MODE_NIGHT_AUTO:
628 currentMode = PARAM_SENSOR_MODE;
629 break;
630 case UiModeManager.MODE_NIGHT_YES:
631 currentMode = PARAM_NIGHT_MODE;
632 break;
633 case UiModeManager.MODE_NIGHT_NO:
634 currentMode = PARAM_DAY_MODE;
635 break;
636 }
637 writer.println("DayNightMode changed to: " + currentMode);
638 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800639
Bryan Eylera32a7c12018-02-27 15:40:00 -0800640 private void forceGarageMode(String arg, PrintWriter writer) {
641 switch (arg) {
642 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700643 mGarageModeService.forceStartGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800644 break;
645 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700646 mGarageModeService.stopAndResetGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800647 break;
648 case PARAM_QUERY_MODE:
649 // Nothing to do. Always query at the end anyway.
650 break;
651 default:
652 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
653 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
654 return;
655 }
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700656 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -0800657 }
658
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800659 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800660 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800661 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800662 * @param property the Vehicle property Id as defined in the HAL
663 * @param zone Zone that this event services
664 * @param value Data value of the event
665 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800666 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800667 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800668 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800669 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
670 if (!isPropertyAreaTypeGlobal(property)) {
671 writer.println("Property area type inconsistent with given zone");
672 return;
673 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800674 }
675 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800676 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800677 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800678 writer.println("Invalid property Id zone Id or value" + e);
679 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800680 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800681 }
682
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800683 // Check if the given property is global
684 private boolean isPropertyAreaTypeGlobal(String property) {
685 if (property == null) {
686 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800687 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800688 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800689 }
Yao Chene33f07e2016-07-26 12:02:51 -0700690 }
kevinjm55822c42018-08-15 11:26:00 -0700691}