blob: c192202633bb2234c862781eb67851c51019a1c0 [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 Zhengd3cb98e2018-05-11 11:42:48 -070024import android.car.user.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
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080038import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070039import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080040import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080041import com.android.car.pm.CarPackageManagerService;
Enrico Granatab19bc322017-10-12 12:25:06 -070042import com.android.car.systeminterface.SystemInterface;
Ying Zhengd3cb98e2018-05-11 11:42:48 -070043import com.android.car.user.CarUserService;
keunyoungca515072015-07-10 12:21:47 -070044import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000045import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070046
Enrico Granatae8056ca2018-04-03 13:19:52 -070047import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070048import java.io.PrintWriter;
Ying Zhengd3cb98e2018-05-11 11:42:48 -070049import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080050import java.util.Arrays;
Ying Zhengd3cb98e2018-05-11 11:42:48 -070051import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070052
keunyoungca515072015-07-10 12:21:47 -070053public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070054
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080055 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070056 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
57 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080058
keunyoungca515072015-07-10 12:21:47 -070059 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080060 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070061
Anthony Chen12aec302018-04-25 16:41:48 -070062 private final SystemInterface mSystemInterface;
63
Keun-young Park4727da32016-05-31 10:00:51 -070064 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070065 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080066 private final CarPackageManagerService mCarPackageManagerService;
67 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070068 private final CarSensorService mCarSensorService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080069 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080070 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyounga3b28d82015-08-25 13:05:15 -070071 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070072 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080073 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070074 private final CarCabinService mCarCabinService;
Steve Paik66481982015-10-27 15:22:38 -070075 private final CarHvacService mCarHvacService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080076 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070077 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080078 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080079 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080080 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070081 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070082 private final CarVendorExtensionService mCarVendorExtensionService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080083 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070084 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070085 private final CarDiagnosticService mCarDiagnosticService;
86 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -070087 private final CarConfigurationService mCarConfigurationService;
88
Ying Zhengd3cb98e2018-05-11 11:42:48 -070089 private final CarUserManagerHelper mUserManagerHelper;
90 private CarUserService mCarUserService;
Antonio Cortese4619c72017-02-02 07:53:27 -080091 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080092 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070093
Pavel Maltsev0d07c762016-11-03 16:40:15 -070094 private final CarServiceBase[] mAllServices;
95
Steve Paikc302c7c2017-08-04 14:01:58 -070096 private static final String TAG = "ICarImpl";
97 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Keun-young Park4a79a382017-08-10 18:19:14 -070098 private static final TimingsTraceLog mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG,
Gregory Clarkd8136062017-12-11 14:27:53 -080099 Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700100
keunyoung1ab8e182015-09-24 09:25:22 -0700101 /** Test only service. Populate it only when necessary. */
102 @GuardedBy("this")
103 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700104
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700105 @GuardedBy("this")
106 private ICarServiceHelper mICarServiceHelper;
107
Enrico Granatae8056ca2018-04-03 13:19:52 -0700108 private final String mVehicleInterfaceName;
109
Pavel Maltsevec83b632017-01-05 15:10:55 -0800110 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700111 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700112 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800113 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700114 mHal = new VehicleHal(vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700115 mVehicleInterfaceName = vehicleInterfaceName;
Keun-young Park4727da32016-05-31 10:00:51 -0700116 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800117 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
118 systemInterface);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700119 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800120 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarSensorService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800121 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Ram Periathiruvadied9e8f82018-03-07 11:50:49 -0800122 mCarDrivingStateService, mCarSensorService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800123 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
124 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700125 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700126 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800127 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -0800128 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Gregory Clark26fa6012018-03-14 18:38:56 -0700129 mCarLocationService = new CarLocationService(mContext, mCarPowerManagementService,
130 mCarSensorService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700131 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700132 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800133 mCarAudioService = new CarAudioService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700134 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
135 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700136 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700137 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700138 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700139 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700140 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700141 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
142 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700143 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800144 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadie011a402018-03-22 18:54:33 -0700145 mCarSensorService, mPerUserCarServiceHelper, mCarUXRestrictionsService);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700146 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
147 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700148 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700149 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
150 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700151 mCarConfigurationService =
152 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Ying Zhengd3cb98e2018-05-11 11:42:48 -0700153 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -0700154
keunyounga3b28d82015-08-25 13:05:15 -0700155 // Be careful with order. Service depending on other service should be inited later.
Ying Zhengd3cb98e2018-05-11 11:42:48 -0700156 List<CarServiceBase> allServices = new ArrayList<>();
157 allServices.add(mSystemActivityMonitoringService);
158 allServices.add(mCarPowerManagementService);
159 allServices.add(mCarSensorService);
160 allServices.add(mCarDrivingStateService);
161 allServices.add(mCarUXRestrictionsService);
162 allServices.add(mCarPackageManagerService);
163 allServices.add(mCarInputService);
164 allServices.add(mCarLocationService);
165 allServices.add(mGarageModeService);
166 allServices.add(mCarInfoService);
167 allServices.add(mAppFocusService);
168 allServices.add(mCarAudioService);
169 allServices.add(mCarCabinService);
170 allServices.add(mCarHvacService);
171 allServices.add(mCarNightService);
172 allServices.add(mInstrumentClusterService);
173 allServices.add(mCarProjectionService);
174 allServices.add(mSystemStateControllerService);
175 allServices.add(mCarVendorExtensionService);
176 allServices.add(mCarBluetoothService);
177 allServices.add(mCarDiagnosticService);
178 allServices.add(mPerUserCarServiceHelper);
179 allServices.add(mCarStorageMonitoringService);
180 allServices.add(mCarConfigurationService);
181 allServices.add(mVmsSubscriberService);
182 allServices.add(mVmsPublisherService);
183
184 if (mUserManagerHelper.isHeadlessSystemUser()) {
185 mCarUserService = new CarUserService(serviceContext, mUserManagerHelper);
186 allServices.add(mCarUserService);
187 }
188
189 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700190 }
191
Pavel Maltsevabd47232017-10-10 16:54:57 -0700192 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700193 void init() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700194 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700195 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700196 traceEnd();
197 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800198 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700199 service.init();
200 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700201 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700202 }
203
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700204 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700205 // release done in opposite order from init
206 for (int i = mAllServices.length - 1; i >= 0; i--) {
207 mAllServices[i].release();
208 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700209 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700210 }
211
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700212 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800213 mHal.vehicleHalReconnected(vehicle);
214 for (CarServiceBase service : mAllServices) {
215 service.vehicleHalReconnected();
216 }
217 }
218
keunyoungca515072015-07-10 12:21:47 -0700219 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700220 public void setCarServiceHelper(IBinder helper) {
221 int uid = Binder.getCallingUid();
222 if (uid != Process.SYSTEM_UID) {
223 throw new SecurityException("Only allowed from system");
224 }
225 synchronized (this) {
226 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800227 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700228 }
229 }
230
231 @Override
keunyoungca515072015-07-10 12:21:47 -0700232 public IBinder getCarService(String serviceName) {
233 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800234 case Car.AUDIO_SERVICE:
235 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700236 case Car.SENSOR_SERVICE:
237 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700238 case Car.INFO_SERVICE:
239 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700240 case Car.APP_FOCUS_SERVICE:
241 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800242 case Car.PACKAGE_SERVICE:
243 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700244 case Car.CABIN_SERVICE:
245 assertCabinPermission(mContext);
246 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800247 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700248 assertAnyDiagnosticPermission(mContext);
249 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800250 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700251 assertHvacPermission(mContext);
252 return mCarHvacService;
Steve Paik388d7772018-02-12 10:54:51 -0800253 case Car.POWER_SERVICE:
254 assertPowerPermission(mContext);
255 return mCarPowerManagementService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800256 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700257 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700258 IInstrumentClusterNavigation navService =
259 mInstrumentClusterService.getNavigationService();
260 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700261 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
262 assertClusterManagerPermission(mContext);
263 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800264 case Car.PROJECTION_SERVICE:
265 assertProjectionPermission(mContext);
266 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700267 case Car.VENDOR_EXTENSION_SERVICE:
268 assertVendorExtensionPermission(mContext);
269 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800270 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700271 assertVmsSubscriberPermission(mContext);
272 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800273 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700274 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700275 synchronized (this) {
276 if (mCarTestService == null) {
277 mCarTestService = new CarTestService(mContext, this);
278 }
279 return mCarTestService;
280 }
281 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700282 case Car.BLUETOOTH_SERVICE:
283 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700284 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700285 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700286 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800287 case Car.CAR_DRIVING_STATE_SERVICE:
288 assertDrivingStatePermission(mContext);
289 return mCarDrivingStateService;
290 case Car.CAR_UX_RESTRICTION_SERVICE:
291 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700292 case Car.CAR_CONFIGURATION_SERVICE:
293 return mCarConfigurationService;
keunyoungca515072015-07-10 12:21:47 -0700294 default:
295 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
296 return null;
297 }
298 }
299
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800300 @Override
301 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700302 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800303 }
304
Keun-young Parka28d7b22016-02-29 16:54:29 -0800305 public CarServiceBase getCarInternalService(String serviceName) {
306 switch (serviceName) {
307 case INTERNAL_INPUT_SERVICE:
308 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700309 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
310 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800311 default:
312 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
313 serviceName);
314 return null;
315 }
316 }
317
keunyoung1ab8e182015-09-24 09:25:22 -0700318 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700319 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700320 }
321
Steve Paik43c04a72016-07-08 19:12:09 -0700322 public static void assertCabinPermission(Context context) {
Kai25c0b852018-03-01 11:52:31 -0800323 assertPermission(context, Car.PERMISSION_ADJUST_CAR_CABIN);
Steve Paik43c04a72016-07-08 19:12:09 -0700324 }
325
Keun-young Parke31a8b22016-03-16 17:34:08 -0700326 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700327 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800328 }
329
Pavel Maltsev905968c2017-07-16 19:48:57 -0700330 public static void assertClusterManagerPermission(Context context) {
331 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
332 }
333
Steve Paik66481982015-10-27 15:22:38 -0700334 public static void assertHvacPermission(Context context) {
Kai68ee16b2018-03-22 11:57:37 -0700335 assertPermission(context, Car.PERMISSION_CONTROL_CAR_CLIMATE);
Steve Paik66481982015-10-27 15:22:38 -0700336 }
337
Steve Paik388d7772018-02-12 10:54:51 -0800338 public static void assertPowerPermission(Context context) {
339 assertPermission(context, Car.PERMISSION_CAR_POWER);
340 }
341
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800342 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700343 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
344 }
345
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700346 public static void assertVendorExtensionPermission(Context context) {
347 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
348 }
349
Enrico Granata3c7a6662017-02-23 18:07:59 -0800350 public static void assertAnyDiagnosticPermission(Context context) {
351 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700352 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800353 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
354 }
355
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800356 public static void assertDrivingStatePermission(Context context) {
357 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
358 }
359
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800360 public static void assertVmsPublisherPermission(Context context) {
361 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
362 }
363
Antonio Cortese4619c72017-02-02 07:53:27 -0800364 public static void assertVmsSubscriberPermission(Context context) {
365 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
366 }
367
Steve Paik461ecc62016-06-08 15:28:32 -0700368 public static void assertPermission(Context context, String permission) {
369 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
370 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800371 }
372 }
373
Enrico Granata3c7a6662017-02-23 18:07:59 -0800374 public static void assertAnyPermission(Context context, String... permissions) {
375 for (String permission : permissions) {
376 if (context.checkCallingOrSelfPermission(permission) ==
377 PackageManager.PERMISSION_GRANTED) {
378 return;
379 }
380 }
381 throw new SecurityException("requires any of " + Arrays.toString(permissions));
382 }
383
Enrico Granatae8056ca2018-04-03 13:19:52 -0700384 @Override
385 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
386 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700387 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700388 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700389 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
390 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700391 return;
keunyounga3b28d82015-08-25 13:05:15 -0700392 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700393 if (args == null || args.length == 0) {
394 writer.println("*dump car service*");
395
396 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
397 writer.println("*Dump all services*");
398 for (CarServiceBase service : mAllServices) {
399 dumpService(service, writer);
400 }
401 if (mCarTestService != null) {
402 dumpService(mCarTestService, writer);
403 }
404 writer.println("*Dump Vehicle HAL*");
405 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
406 try {
407 // TODO dump all feature flags by creating a dumpable interface
408 mHal.dump(writer);
409 } catch (Exception e) {
410 writer.println("Failed dumping: " + mHal.getClass().getName());
411 e.printStackTrace(writer);
412 }
413 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
414 execShellCmd(args, writer);
415 } else {
416 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800417 }
418 }
419
420 private void dumpService(CarServiceBase service, PrintWriter writer) {
421 try {
422 service.dump(writer);
423 } catch (Exception e) {
424 writer.println("Failed dumping: " + service.getClass().getName());
425 e.printStackTrace(writer);
426 }
keunyoungcc449f72015-08-12 10:46:27 -0700427 }
Yao Chene33f07e2016-07-26 12:02:51 -0700428
429 void execShellCmd(String[] args, PrintWriter writer) {
430 new CarShellCommand().exec(args, writer);
431 }
432
Pavel Maltsevabd47232017-10-10 16:54:57 -0700433 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700434 private static void traceBegin(String name) {
435 Slog.i(TAG, name);
436 mBootTiming.traceBegin(name);
437 }
438
Pavel Maltsevabd47232017-10-10 16:54:57 -0700439 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700440 private static void traceEnd() {
441 mBootTiming.traceEnd();
442 }
443
Yao Chene33f07e2016-07-26 12:02:51 -0700444 private class CarShellCommand {
445 private static final String COMMAND_HELP = "-h";
446 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800447 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800448 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800449 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800450 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800451
Yao Chene33f07e2016-07-26 12:02:51 -0700452 private static final String PARAM_DAY_MODE = "day";
453 private static final String PARAM_NIGHT_MODE = "night";
454 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800455 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800456 private static final String PARAM_ON_MODE = "on";
457 private static final String PARAM_OFF_MODE = "off";
458 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800459
Yao Chene33f07e2016-07-26 12:02:51 -0700460
461 private void dumpHelp(PrintWriter pw) {
462 pw.println("Car service commands:");
463 pw.println("\t-h");
464 pw.println("\t Print this help text.");
465 pw.println("\tday-night-mode [day|night|sensor]");
466 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800467 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
468 pw.println("\t Inject a vehicle property for testing");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800469 pw.println("\tdisable-uxr true|false");
470 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800471 pw.println("\tgarage-mode [on|off|query]");
472 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800473 pw.println("\tget-do-activities pkgname");
474 pw.println("\t Get Distraction Optimized activities in given package");
Yao Chene33f07e2016-07-26 12:02:51 -0700475 }
476
477 public void exec(String[] args, PrintWriter writer) {
478 String arg = args[0];
479 switch (arg) {
480 case COMMAND_HELP:
481 dumpHelp(writer);
482 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800483 case COMMAND_DAY_NIGHT_MODE: {
Yao Chene33f07e2016-07-26 12:02:51 -0700484 String value = args.length < 1 ? "" : args[1];
485 forceDayNightMode(value, writer);
486 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800487 }
488 case COMMAND_GARAGE_MODE: {
489 String value = args.length < 1 ? "" : args[1];
490 forceGarageMode(value, writer);
491 break;
492 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800493 case COMMAND_INJECT_VHAL_EVENT:
494 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
495 String data;
496 if (args.length < 3) {
497 writer.println("Incorrect number of arguments.");
498 dumpHelp(writer);
499 break;
500 } else if (args.length > 3) {
501 // Zoned
502 zone = args[2];
503 data = args[3];
504 } else {
505 // Global
506 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800507 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800508 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800509 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800510 case COMMAND_ENABLE_UXR:
511 if (args.length < 2) {
512 writer.println("Incorrect number of arguments");
513 dumpHelp(writer);
514 break;
515 }
516 boolean enableBlocking = Boolean.valueOf(args[1]);
517 if (mCarPackageManagerService != null) {
518 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
519 }
520 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800521 case COMMAND_GET_DO_ACTIVITIES:
522 if (args.length < 2) {
523 writer.println("Incorrect number of arguments");
524 dumpHelp(writer);
525 break;
526 }
527 String pkgName = args[1].toLowerCase();
528 if (mCarPackageManagerService != null) {
529 String[] doActivities =
530 mCarPackageManagerService.getDistractionOptimizedActivities(
531 pkgName);
532 if (doActivities != null) {
533 writer.println("DO Activities for " + pkgName);
534 for (String a : doActivities) {
535 writer.println(a);
536 }
537 } else {
538 writer.println("No DO Activities for " + pkgName);
539 }
540 }
541 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700542 default:
543 writer.println("Unknown command.");
544 dumpHelp(writer);
545 }
546 }
547
548 private void forceDayNightMode(String arg, PrintWriter writer) {
549 int mode;
550 switch (arg) {
551 case PARAM_DAY_MODE:
552 mode = CarNightService.FORCED_DAY_MODE;
553 break;
554 case PARAM_NIGHT_MODE:
555 mode = CarNightService.FORCED_NIGHT_MODE;
556 break;
557 case PARAM_SENSOR_MODE:
558 mode = CarNightService.FORCED_SENSOR_MODE;
559 break;
560 default:
561 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
562 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
563 return;
564 }
565 int current = mCarNightService.forceDayNightMode(mode);
566 String currentMode = null;
567 switch (current) {
568 case UiModeManager.MODE_NIGHT_AUTO:
569 currentMode = PARAM_SENSOR_MODE;
570 break;
571 case UiModeManager.MODE_NIGHT_YES:
572 currentMode = PARAM_NIGHT_MODE;
573 break;
574 case UiModeManager.MODE_NIGHT_NO:
575 currentMode = PARAM_DAY_MODE;
576 break;
577 }
578 writer.println("DayNightMode changed to: " + currentMode);
579 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800580
Bryan Eylera32a7c12018-02-27 15:40:00 -0800581 private void forceGarageMode(String arg, PrintWriter writer) {
582 switch (arg) {
583 case PARAM_ON_MODE:
584 mGarageModeService.onPrepareShutdown(false);
585 break;
586 case PARAM_OFF_MODE:
587 mGarageModeService.onSleepEntry();
588 break;
589 case PARAM_QUERY_MODE:
590 // Nothing to do. Always query at the end anyway.
591 break;
592 default:
593 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
594 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
595 return;
596 }
597 writer.println("Garage mode: " + mGarageModeService.isInGarageMode());
598 }
599
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800600 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800601 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800602 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800603 * @param property the Vehicle property Id as defined in the HAL
604 * @param zone Zone that this event services
605 * @param value Data value of the event
606 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800607 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800608 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800609 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800610 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
611 if (!isPropertyAreaTypeGlobal(property)) {
612 writer.println("Property area type inconsistent with given zone");
613 return;
614 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800615 }
616 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800617 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800618 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800619 writer.println("Invalid property Id zone Id or value" + e);
620 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800621 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800622 }
623
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800624 // Check if the given property is global
625 private boolean isPropertyAreaTypeGlobal(String property) {
626 if (property == null) {
627 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800628 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800629 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800630 }
Yao Chene33f07e2016-07-26 12:02:51 -0700631 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700632}