blob: cc20f6554b18bb3d88d78f5144b34e2d5988cbc0 [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;
keunyoungca515072015-07-10 12:21:47 -070024import android.content.Context;
keunyoung1ab8e182015-09-24 09:25:22 -070025import android.content.pm.PackageManager;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080026import android.hardware.automotive.vehicle.V2_0.IVehicle;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -080027import android.hardware.automotive.vehicle.V2_0.VehicleArea;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070028import android.os.Binder;
Enrico Granatae8056ca2018-04-03 13:19:52 -070029import android.os.Build;
keunyoungca515072015-07-10 12:21:47 -070030import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070031import android.os.Process;
Steve Paikc302c7c2017-08-04 14:01:58 -070032import android.os.Trace;
keunyoungca515072015-07-10 12:21:47 -070033import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070034import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070035import android.util.TimingsTraceLog;
Gregory Clark26fa6012018-03-14 18:38:56 -070036
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080037import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070038import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080039import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080040import com.android.car.pm.CarPackageManagerService;
Enrico Granatab19bc322017-10-12 12:25:06 -070041import com.android.car.systeminterface.SystemInterface;
keunyoungca515072015-07-10 12:21:47 -070042import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000043import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070044
Enrico Granatae8056ca2018-04-03 13:19:52 -070045import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070046import java.io.PrintWriter;
Antonio Cortese4619c72017-02-02 07:53:27 -080047import java.util.ArrayList;
48import java.util.Arrays;
49import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070050
keunyoungca515072015-07-10 12:21:47 -070051public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070052
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080053 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070054 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
55 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080056
keunyoungca515072015-07-10 12:21:47 -070057 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080058 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070059
Anthony Chen12aec302018-04-25 16:41:48 -070060 private final SystemInterface mSystemInterface;
61
Keun-young Park4727da32016-05-31 10:00:51 -070062 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070063 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080064 private final CarPackageManagerService mCarPackageManagerService;
65 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070066 private final CarSensorService mCarSensorService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080067 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080068 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyounga3b28d82015-08-25 13:05:15 -070069 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070070 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080071 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070072 private final CarCabinService mCarCabinService;
Steve Paik66481982015-10-27 15:22:38 -070073 private final CarHvacService mCarHvacService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080074 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070075 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080076 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080077 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080078 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070079 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070080 private final CarVendorExtensionService mCarVendorExtensionService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080081 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070082 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070083 private final CarDiagnosticService mCarDiagnosticService;
84 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -070085 private final CarConfigurationService mCarConfigurationService;
86
Antonio Cortese4619c72017-02-02 07:53:27 -080087 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080088 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070089
Pavel Maltsev0d07c762016-11-03 16:40:15 -070090 private final CarServiceBase[] mAllServices;
91
Steve Paikc302c7c2017-08-04 14:01:58 -070092 private static final String TAG = "ICarImpl";
93 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Keun-young Park4a79a382017-08-10 18:19:14 -070094 private static final TimingsTraceLog mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG,
Gregory Clarkd8136062017-12-11 14:27:53 -080095 Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -070096
keunyoung1ab8e182015-09-24 09:25:22 -070097 /** Test only service. Populate it only when necessary. */
98 @GuardedBy("this")
99 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700100
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700101 @GuardedBy("this")
102 private ICarServiceHelper mICarServiceHelper;
103
Enrico Granatae8056ca2018-04-03 13:19:52 -0700104 private final String mVehicleInterfaceName;
105
Pavel Maltsevec83b632017-01-05 15:10:55 -0800106 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700107 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700108 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800109 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700110 mHal = new VehicleHal(vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700111 mVehicleInterfaceName = vehicleInterfaceName;
Keun-young Park4727da32016-05-31 10:00:51 -0700112 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800113 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
114 systemInterface);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700115 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800116 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarSensorService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800117 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Ram Periathiruvadied9e8f82018-03-07 11:50:49 -0800118 mCarDrivingStateService, mCarSensorService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800119 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
120 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700121 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700122 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800123 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -0800124 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Gregory Clark26fa6012018-03-14 18:38:56 -0700125 mCarLocationService = new CarLocationService(mContext, mCarPowerManagementService,
126 mCarSensorService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700127 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700128 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800129 mCarAudioService = new CarAudioService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700130 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
131 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700132 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700133 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700134 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700135 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700136 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700137 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
138 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700139 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800140 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadie011a402018-03-22 18:54:33 -0700141 mCarSensorService, mPerUserCarServiceHelper, mCarUXRestrictionsService);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700142 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
143 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700144 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700145 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
146 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700147 mCarConfigurationService =
148 new CarConfigurationService(serviceContext, new JsonReaderImpl());
keunyounga74b9ca2015-10-21 13:33:58 -0700149
keunyounga3b28d82015-08-25 13:05:15 -0700150 // Be careful with order. Service depending on other service should be inited later.
Antonio Cortese4619c72017-02-02 07:53:27 -0800151 List<CarServiceBase> allServices = new ArrayList<>(Arrays.asList(
Keun-young Park4727da32016-05-31 10:00:51 -0700152 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700153 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700154 mCarSensorService,
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800155 mCarDrivingStateService,
156 mCarUXRestrictionsService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800157 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800158 mCarInputService,
Gregory Clarkd8136062017-12-11 14:27:53 -0800159 mCarLocationService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800160 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700161 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700162 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700163 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700164 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700165 mCarHvacService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800166 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800167 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800168 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700169 mSystemStateControllerService,
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800170 mCarVendorExtensionService,
Enrico Granatab3634e22017-05-05 18:02:31 -0700171 mCarBluetoothService,
Ram Periathiruvadia048c0a2017-05-09 07:35:03 -0700172 mCarDiagnosticService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700173 mPerUserCarServiceHelper,
Enrico Granata9a916d72017-09-19 14:33:08 -0700174 mCarStorageMonitoringService,
Anthony Chen12aec302018-04-25 16:41:48 -0700175 mCarConfigurationService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700176 mVmsSubscriberService,
177 mVmsPublisherService
Antonio Cortese4619c72017-02-02 07:53:27 -0800178 ));
Antonio Cortese4619c72017-02-02 07:53:27 -0800179 mAllServices = allServices.toArray(new CarServiceBase[0]);
keunyoungca515072015-07-10 12:21:47 -0700180 }
181
Pavel Maltsevabd47232017-10-10 16:54:57 -0700182 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700183 void init() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700184 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700185 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700186 traceEnd();
187 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800188 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700189 service.init();
190 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700191 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700192 }
193
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700194 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700195 // release done in opposite order from init
196 for (int i = mAllServices.length - 1; i >= 0; i--) {
197 mAllServices[i].release();
198 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700199 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700200 }
201
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700202 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800203 mHal.vehicleHalReconnected(vehicle);
204 for (CarServiceBase service : mAllServices) {
205 service.vehicleHalReconnected();
206 }
207 }
208
keunyoungca515072015-07-10 12:21:47 -0700209 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700210 public void setCarServiceHelper(IBinder helper) {
211 int uid = Binder.getCallingUid();
212 if (uid != Process.SYSTEM_UID) {
213 throw new SecurityException("Only allowed from system");
214 }
215 synchronized (this) {
216 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800217 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700218 }
219 }
220
221 @Override
keunyoungca515072015-07-10 12:21:47 -0700222 public IBinder getCarService(String serviceName) {
223 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800224 case Car.AUDIO_SERVICE:
225 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700226 case Car.SENSOR_SERVICE:
227 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700228 case Car.INFO_SERVICE:
229 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700230 case Car.APP_FOCUS_SERVICE:
231 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800232 case Car.PACKAGE_SERVICE:
233 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700234 case Car.CABIN_SERVICE:
235 assertCabinPermission(mContext);
236 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800237 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700238 assertAnyDiagnosticPermission(mContext);
239 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800240 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700241 assertHvacPermission(mContext);
242 return mCarHvacService;
Steve Paik388d7772018-02-12 10:54:51 -0800243 case Car.POWER_SERVICE:
244 assertPowerPermission(mContext);
245 return mCarPowerManagementService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800246 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700247 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700248 IInstrumentClusterNavigation navService =
249 mInstrumentClusterService.getNavigationService();
250 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700251 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
252 assertClusterManagerPermission(mContext);
253 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800254 case Car.PROJECTION_SERVICE:
255 assertProjectionPermission(mContext);
256 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700257 case Car.VENDOR_EXTENSION_SERVICE:
258 assertVendorExtensionPermission(mContext);
259 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800260 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700261 assertVmsSubscriberPermission(mContext);
262 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800263 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700264 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700265 synchronized (this) {
266 if (mCarTestService == null) {
267 mCarTestService = new CarTestService(mContext, this);
268 }
269 return mCarTestService;
270 }
271 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700272 case Car.BLUETOOTH_SERVICE:
273 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700274 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700275 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700276 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800277 case Car.CAR_DRIVING_STATE_SERVICE:
278 assertDrivingStatePermission(mContext);
279 return mCarDrivingStateService;
280 case Car.CAR_UX_RESTRICTION_SERVICE:
281 return mCarUXRestrictionsService;
keunyoungca515072015-07-10 12:21:47 -0700282 default:
283 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
284 return null;
285 }
286 }
287
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800288 @Override
289 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700290 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800291 }
292
Keun-young Parka28d7b22016-02-29 16:54:29 -0800293 public CarServiceBase getCarInternalService(String serviceName) {
294 switch (serviceName) {
295 case INTERNAL_INPUT_SERVICE:
296 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700297 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
298 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800299 default:
300 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
301 serviceName);
302 return null;
303 }
304 }
305
keunyoung1ab8e182015-09-24 09:25:22 -0700306 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700307 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700308 }
309
Steve Paik43c04a72016-07-08 19:12:09 -0700310 public static void assertCabinPermission(Context context) {
Kai25c0b852018-03-01 11:52:31 -0800311 assertPermission(context, Car.PERMISSION_ADJUST_CAR_CABIN);
Steve Paik43c04a72016-07-08 19:12:09 -0700312 }
313
Keun-young Parke31a8b22016-03-16 17:34:08 -0700314 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700315 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800316 }
317
Pavel Maltsev905968c2017-07-16 19:48:57 -0700318 public static void assertClusterManagerPermission(Context context) {
319 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
320 }
321
Steve Paik66481982015-10-27 15:22:38 -0700322 public static void assertHvacPermission(Context context) {
Kai68ee16b2018-03-22 11:57:37 -0700323 assertPermission(context, Car.PERMISSION_CONTROL_CAR_CLIMATE);
Steve Paik66481982015-10-27 15:22:38 -0700324 }
325
Steve Paik388d7772018-02-12 10:54:51 -0800326 public static void assertPowerPermission(Context context) {
327 assertPermission(context, Car.PERMISSION_CAR_POWER);
328 }
329
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800330 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700331 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
332 }
333
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700334 public static void assertVendorExtensionPermission(Context context) {
335 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
336 }
337
Enrico Granata3c7a6662017-02-23 18:07:59 -0800338 public static void assertAnyDiagnosticPermission(Context context) {
339 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700340 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800341 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
342 }
343
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800344 public static void assertDrivingStatePermission(Context context) {
345 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
346 }
347
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800348 public static void assertVmsPublisherPermission(Context context) {
349 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
350 }
351
Antonio Cortese4619c72017-02-02 07:53:27 -0800352 public static void assertVmsSubscriberPermission(Context context) {
353 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
354 }
355
Steve Paik461ecc62016-06-08 15:28:32 -0700356 public static void assertPermission(Context context, String permission) {
357 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
358 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800359 }
360 }
361
Enrico Granata3c7a6662017-02-23 18:07:59 -0800362 public static void assertAnyPermission(Context context, String... permissions) {
363 for (String permission : permissions) {
364 if (context.checkCallingOrSelfPermission(permission) ==
365 PackageManager.PERMISSION_GRANTED) {
366 return;
367 }
368 }
369 throw new SecurityException("requires any of " + Arrays.toString(permissions));
370 }
371
Enrico Granatae8056ca2018-04-03 13:19:52 -0700372 @Override
373 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
374 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700375 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700376 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700377 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
378 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700379 return;
keunyounga3b28d82015-08-25 13:05:15 -0700380 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700381 if (args == null || args.length == 0) {
382 writer.println("*dump car service*");
383
384 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
385 writer.println("*Dump all services*");
386 for (CarServiceBase service : mAllServices) {
387 dumpService(service, writer);
388 }
389 if (mCarTestService != null) {
390 dumpService(mCarTestService, writer);
391 }
392 writer.println("*Dump Vehicle HAL*");
393 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
394 try {
395 // TODO dump all feature flags by creating a dumpable interface
396 mHal.dump(writer);
397 } catch (Exception e) {
398 writer.println("Failed dumping: " + mHal.getClass().getName());
399 e.printStackTrace(writer);
400 }
401 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
402 execShellCmd(args, writer);
403 } else {
404 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800405 }
406 }
407
408 private void dumpService(CarServiceBase service, PrintWriter writer) {
409 try {
410 service.dump(writer);
411 } catch (Exception e) {
412 writer.println("Failed dumping: " + service.getClass().getName());
413 e.printStackTrace(writer);
414 }
keunyoungcc449f72015-08-12 10:46:27 -0700415 }
Yao Chene33f07e2016-07-26 12:02:51 -0700416
417 void execShellCmd(String[] args, PrintWriter writer) {
418 new CarShellCommand().exec(args, writer);
419 }
420
Pavel Maltsevabd47232017-10-10 16:54:57 -0700421 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700422 private static void traceBegin(String name) {
423 Slog.i(TAG, name);
424 mBootTiming.traceBegin(name);
425 }
426
Pavel Maltsevabd47232017-10-10 16:54:57 -0700427 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700428 private static void traceEnd() {
429 mBootTiming.traceEnd();
430 }
431
Yao Chene33f07e2016-07-26 12:02:51 -0700432 private class CarShellCommand {
433 private static final String COMMAND_HELP = "-h";
434 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800435 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800436 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800437 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800438 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800439
Yao Chene33f07e2016-07-26 12:02:51 -0700440 private static final String PARAM_DAY_MODE = "day";
441 private static final String PARAM_NIGHT_MODE = "night";
442 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800443 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800444 private static final String PARAM_ON_MODE = "on";
445 private static final String PARAM_OFF_MODE = "off";
446 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800447
Yao Chene33f07e2016-07-26 12:02:51 -0700448
449 private void dumpHelp(PrintWriter pw) {
450 pw.println("Car service commands:");
451 pw.println("\t-h");
452 pw.println("\t Print this help text.");
453 pw.println("\tday-night-mode [day|night|sensor]");
454 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800455 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
456 pw.println("\t Inject a vehicle property for testing");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800457 pw.println("\tdisable-uxr true|false");
458 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800459 pw.println("\tgarage-mode [on|off|query]");
460 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800461 pw.println("\tget-do-activities pkgname");
462 pw.println("\t Get Distraction Optimized activities in given package");
Yao Chene33f07e2016-07-26 12:02:51 -0700463 }
464
465 public void exec(String[] args, PrintWriter writer) {
466 String arg = args[0];
467 switch (arg) {
468 case COMMAND_HELP:
469 dumpHelp(writer);
470 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800471 case COMMAND_DAY_NIGHT_MODE: {
Yao Chene33f07e2016-07-26 12:02:51 -0700472 String value = args.length < 1 ? "" : args[1];
473 forceDayNightMode(value, writer);
474 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800475 }
476 case COMMAND_GARAGE_MODE: {
477 String value = args.length < 1 ? "" : args[1];
478 forceGarageMode(value, writer);
479 break;
480 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800481 case COMMAND_INJECT_VHAL_EVENT:
482 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
483 String data;
484 if (args.length < 3) {
485 writer.println("Incorrect number of arguments.");
486 dumpHelp(writer);
487 break;
488 } else if (args.length > 3) {
489 // Zoned
490 zone = args[2];
491 data = args[3];
492 } else {
493 // Global
494 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800495 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800496 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800497 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800498 case COMMAND_ENABLE_UXR:
499 if (args.length < 2) {
500 writer.println("Incorrect number of arguments");
501 dumpHelp(writer);
502 break;
503 }
504 boolean enableBlocking = Boolean.valueOf(args[1]);
505 if (mCarPackageManagerService != null) {
506 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
507 }
508 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800509 case COMMAND_GET_DO_ACTIVITIES:
510 if (args.length < 2) {
511 writer.println("Incorrect number of arguments");
512 dumpHelp(writer);
513 break;
514 }
515 String pkgName = args[1].toLowerCase();
516 if (mCarPackageManagerService != null) {
517 String[] doActivities =
518 mCarPackageManagerService.getDistractionOptimizedActivities(
519 pkgName);
520 if (doActivities != null) {
521 writer.println("DO Activities for " + pkgName);
522 for (String a : doActivities) {
523 writer.println(a);
524 }
525 } else {
526 writer.println("No DO Activities for " + pkgName);
527 }
528 }
529 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700530 default:
531 writer.println("Unknown command.");
532 dumpHelp(writer);
533 }
534 }
535
536 private void forceDayNightMode(String arg, PrintWriter writer) {
537 int mode;
538 switch (arg) {
539 case PARAM_DAY_MODE:
540 mode = CarNightService.FORCED_DAY_MODE;
541 break;
542 case PARAM_NIGHT_MODE:
543 mode = CarNightService.FORCED_NIGHT_MODE;
544 break;
545 case PARAM_SENSOR_MODE:
546 mode = CarNightService.FORCED_SENSOR_MODE;
547 break;
548 default:
549 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
550 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
551 return;
552 }
553 int current = mCarNightService.forceDayNightMode(mode);
554 String currentMode = null;
555 switch (current) {
556 case UiModeManager.MODE_NIGHT_AUTO:
557 currentMode = PARAM_SENSOR_MODE;
558 break;
559 case UiModeManager.MODE_NIGHT_YES:
560 currentMode = PARAM_NIGHT_MODE;
561 break;
562 case UiModeManager.MODE_NIGHT_NO:
563 currentMode = PARAM_DAY_MODE;
564 break;
565 }
566 writer.println("DayNightMode changed to: " + currentMode);
567 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800568
Bryan Eylera32a7c12018-02-27 15:40:00 -0800569 private void forceGarageMode(String arg, PrintWriter writer) {
570 switch (arg) {
571 case PARAM_ON_MODE:
572 mGarageModeService.onPrepareShutdown(false);
573 break;
574 case PARAM_OFF_MODE:
575 mGarageModeService.onSleepEntry();
576 break;
577 case PARAM_QUERY_MODE:
578 // Nothing to do. Always query at the end anyway.
579 break;
580 default:
581 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
582 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
583 return;
584 }
585 writer.println("Garage mode: " + mGarageModeService.isInGarageMode());
586 }
587
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800588 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800589 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800590 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800591 * @param property the Vehicle property Id as defined in the HAL
592 * @param zone Zone that this event services
593 * @param value Data value of the event
594 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800595 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800596 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800597 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800598 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
599 if (!isPropertyAreaTypeGlobal(property)) {
600 writer.println("Property area type inconsistent with given zone");
601 return;
602 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800603 }
604 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800605 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800606 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800607 writer.println("Invalid property Id zone Id or value" + e);
608 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800609 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800610 }
611
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800612 // Check if the given property is global
613 private boolean isPropertyAreaTypeGlobal(String property) {
614 if (property == null) {
615 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800616 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800617 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800618 }
Yao Chene33f07e2016-07-26 12:02:51 -0700619 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700620}