blob: 95e4bcfa564232e34a0f24dbd1eff9e3dc0589dd [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;
keunyoungca515072015-07-10 12:21:47 -070029import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070030import android.os.Process;
Steve Paikc302c7c2017-08-04 14:01:58 -070031import android.os.Trace;
keunyoungca515072015-07-10 12:21:47 -070032import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070033import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070034import android.util.TimingsTraceLog;
Gregory Clark26fa6012018-03-14 18:38:56 -070035
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080036import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070037import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080038import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080039import com.android.car.pm.CarPackageManagerService;
Enrico Granatab19bc322017-10-12 12:25:06 -070040import com.android.car.systeminterface.SystemInterface;
keunyoungca515072015-07-10 12:21:47 -070041import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000042import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070043
keunyounga3b28d82015-08-25 13:05:15 -070044import java.io.PrintWriter;
Antonio Cortese4619c72017-02-02 07:53:27 -080045import java.util.ArrayList;
46import java.util.Arrays;
47import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070048
keunyoungca515072015-07-10 12:21:47 -070049public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070050
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080051 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070052 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
53 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080054
keunyoungca515072015-07-10 12:21:47 -070055 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080056 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070057
Keun-young Park4727da32016-05-31 10:00:51 -070058 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070059 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080060 private final CarPackageManagerService mCarPackageManagerService;
61 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070062 private final CarSensorService mCarSensorService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080063 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080064 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyounga3b28d82015-08-25 13:05:15 -070065 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070066 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080067 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070068 private final CarCabinService mCarCabinService;
Steve Paik66481982015-10-27 15:22:38 -070069 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070070 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080071 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070072 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080073 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080074 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080075 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070076 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070077 private final CarVendorExtensionService mCarVendorExtensionService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080078 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070079 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070080 private final CarDiagnosticService mCarDiagnosticService;
81 private final CarStorageMonitoringService mCarStorageMonitoringService;
Steve Paik0f9fc002018-02-09 17:42:00 -080082 private final SystemInterface mSystemInterface;
Antonio Cortese4619c72017-02-02 07:53:27 -080083 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080084 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070085
Pavel Maltsev0d07c762016-11-03 16:40:15 -070086 private final CarServiceBase[] mAllServices;
87
Steve Paikc302c7c2017-08-04 14:01:58 -070088 private static final String TAG = "ICarImpl";
89 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Keun-young Park4a79a382017-08-10 18:19:14 -070090 private static final TimingsTraceLog mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG,
Gregory Clarkd8136062017-12-11 14:27:53 -080091 Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -070092
keunyoung1ab8e182015-09-24 09:25:22 -070093 /** Test only service. Populate it only when necessary. */
94 @GuardedBy("this")
95 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070096
Keun-young Parkaab47cd2017-06-05 11:04:50 -070097 @GuardedBy("this")
98 private ICarServiceHelper mICarServiceHelper;
99
Pavel Maltsevec83b632017-01-05 15:10:55 -0800100 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
101 CanBusErrorNotifier errorNotifier) {
keunyoungca515072015-07-10 12:21:47 -0700102 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800103 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700104 mHal = new VehicleHal(vehicle);
Keun-young Park4727da32016-05-31 10:00:51 -0700105 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800106 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
107 systemInterface);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700108 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800109 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarSensorService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800110 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Ram Periathiruvadied9e8f82018-03-07 11:50:49 -0800111 mCarDrivingStateService, mCarSensorService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800112 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
113 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700114 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700115 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800116 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -0800117 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Gregory Clark26fa6012018-03-14 18:38:56 -0700118 mCarLocationService = new CarLocationService(mContext, mCarPowerManagementService,
119 mCarSensorService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700120 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700121 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800122 mCarAudioService = new CarAudioService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700123 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
124 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
125 mCarRadioService = new CarRadioService(serviceContext, mHal.getRadioHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700126 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700127 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700128 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700129 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700130 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700131 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
132 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700133 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800134 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700135 mCarSensorService, mPerUserCarServiceHelper);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700136 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
137 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700138 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700139 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
140 systemInterface);
keunyounga74b9ca2015-10-21 13:33:58 -0700141
keunyounga3b28d82015-08-25 13:05:15 -0700142 // Be careful with order. Service depending on other service should be inited later.
Antonio Cortese4619c72017-02-02 07:53:27 -0800143 List<CarServiceBase> allServices = new ArrayList<>(Arrays.asList(
Keun-young Park4727da32016-05-31 10:00:51 -0700144 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700145 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700146 mCarSensorService,
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800147 mCarDrivingStateService,
148 mCarUXRestrictionsService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800149 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800150 mCarInputService,
Gregory Clarkd8136062017-12-11 14:27:53 -0800151 mCarLocationService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800152 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700153 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700154 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700155 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700156 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700157 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800158 mCarRadioService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800159 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800160 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800161 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700162 mSystemStateControllerService,
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800163 mCarVendorExtensionService,
Enrico Granatab3634e22017-05-05 18:02:31 -0700164 mCarBluetoothService,
Ram Periathiruvadia048c0a2017-05-09 07:35:03 -0700165 mCarDiagnosticService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700166 mPerUserCarServiceHelper,
Enrico Granata9a916d72017-09-19 14:33:08 -0700167 mCarStorageMonitoringService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700168 mVmsSubscriberService,
169 mVmsPublisherService
Antonio Cortese4619c72017-02-02 07:53:27 -0800170 ));
Antonio Cortese4619c72017-02-02 07:53:27 -0800171 mAllServices = allServices.toArray(new CarServiceBase[0]);
keunyoungca515072015-07-10 12:21:47 -0700172 }
173
Pavel Maltsevabd47232017-10-10 16:54:57 -0700174 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700175 void init() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700176 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700177 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700178 traceEnd();
179 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800180 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700181 service.init();
182 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700183 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700184 }
185
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700186 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700187 // release done in opposite order from init
188 for (int i = mAllServices.length - 1; i >= 0; i--) {
189 mAllServices[i].release();
190 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700191 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700192 }
193
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700194 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800195 mHal.vehicleHalReconnected(vehicle);
196 for (CarServiceBase service : mAllServices) {
197 service.vehicleHalReconnected();
198 }
199 }
200
keunyoungca515072015-07-10 12:21:47 -0700201 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700202 public void setCarServiceHelper(IBinder helper) {
203 int uid = Binder.getCallingUid();
204 if (uid != Process.SYSTEM_UID) {
205 throw new SecurityException("Only allowed from system");
206 }
207 synchronized (this) {
208 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800209 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700210 }
211 }
212
213 @Override
keunyoungca515072015-07-10 12:21:47 -0700214 public IBinder getCarService(String serviceName) {
215 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800216 case Car.AUDIO_SERVICE:
217 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700218 case Car.SENSOR_SERVICE:
219 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700220 case Car.INFO_SERVICE:
221 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700222 case Car.APP_FOCUS_SERVICE:
223 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800224 case Car.PACKAGE_SERVICE:
225 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700226 case Car.CABIN_SERVICE:
227 assertCabinPermission(mContext);
228 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800229 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700230 assertAnyDiagnosticPermission(mContext);
231 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800232 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700233 assertHvacPermission(mContext);
234 return mCarHvacService;
Steve Paik388d7772018-02-12 10:54:51 -0800235 case Car.POWER_SERVICE:
236 assertPowerPermission(mContext);
237 return mCarPowerManagementService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800238 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800239 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700240 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800241 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700242 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700243 IInstrumentClusterNavigation navService =
244 mInstrumentClusterService.getNavigationService();
245 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700246 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
247 assertClusterManagerPermission(mContext);
248 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800249 case Car.PROJECTION_SERVICE:
250 assertProjectionPermission(mContext);
251 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700252 case Car.VENDOR_EXTENSION_SERVICE:
253 assertVendorExtensionPermission(mContext);
254 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800255 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700256 assertVmsSubscriberPermission(mContext);
257 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800258 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700259 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700260 synchronized (this) {
261 if (mCarTestService == null) {
262 mCarTestService = new CarTestService(mContext, this);
263 }
264 return mCarTestService;
265 }
266 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700267 case Car.BLUETOOTH_SERVICE:
268 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700269 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700270 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700271 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800272 case Car.CAR_DRIVING_STATE_SERVICE:
273 assertDrivingStatePermission(mContext);
274 return mCarDrivingStateService;
275 case Car.CAR_UX_RESTRICTION_SERVICE:
276 return mCarUXRestrictionsService;
keunyoungca515072015-07-10 12:21:47 -0700277 default:
278 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
279 return null;
280 }
281 }
282
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800283 @Override
284 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700285 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800286 }
287
Keun-young Parka28d7b22016-02-29 16:54:29 -0800288 public CarServiceBase getCarInternalService(String serviceName) {
289 switch (serviceName) {
290 case INTERNAL_INPUT_SERVICE:
291 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700292 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
293 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800294 default:
295 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
296 serviceName);
297 return null;
298 }
299 }
300
keunyoung1ab8e182015-09-24 09:25:22 -0700301 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700302 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700303 }
304
Steve Paik43c04a72016-07-08 19:12:09 -0700305 public static void assertCabinPermission(Context context) {
Kai25c0b852018-03-01 11:52:31 -0800306 assertPermission(context, Car.PERMISSION_ADJUST_CAR_CABIN);
Steve Paik43c04a72016-07-08 19:12:09 -0700307 }
308
Keun-young Parke31a8b22016-03-16 17:34:08 -0700309 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700310 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800311 }
312
Pavel Maltsev905968c2017-07-16 19:48:57 -0700313 public static void assertClusterManagerPermission(Context context) {
314 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
315 }
316
Steve Paik66481982015-10-27 15:22:38 -0700317 public static void assertHvacPermission(Context context) {
Kai25c0b852018-03-01 11:52:31 -0800318 assertPermission(context, Car.PERMISSION_ADJUST_CAR_CLIMATE);
Steve Paik66481982015-10-27 15:22:38 -0700319 }
320
Steve Paik388d7772018-02-12 10:54:51 -0800321 public static void assertPowerPermission(Context context) {
322 assertPermission(context, Car.PERMISSION_CAR_POWER);
323 }
324
keunyoung6b197692015-11-16 13:54:38 -0800325 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700326 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700327 }
328
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800329 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700330 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
331 }
332
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700333 public static void assertVendorExtensionPermission(Context context) {
334 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
335 }
336
Enrico Granata3c7a6662017-02-23 18:07:59 -0800337 public static void assertAnyDiagnosticPermission(Context context) {
338 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700339 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800340 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
341 }
342
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800343 public static void assertDrivingStatePermission(Context context) {
344 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
345 }
346
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800347 public static void assertVmsPublisherPermission(Context context) {
348 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
349 }
350
Antonio Cortese4619c72017-02-02 07:53:27 -0800351 public static void assertVmsSubscriberPermission(Context context) {
352 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
353 }
354
Steve Paik461ecc62016-06-08 15:28:32 -0700355 public static void assertPermission(Context context, String permission) {
356 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
357 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800358 }
359 }
360
Enrico Granata3c7a6662017-02-23 18:07:59 -0800361 public static void assertAnyPermission(Context context, String... permissions) {
362 for (String permission : permissions) {
363 if (context.checkCallingOrSelfPermission(permission) ==
364 PackageManager.PERMISSION_GRANTED) {
365 return;
366 }
367 }
368 throw new SecurityException("requires any of " + Arrays.toString(permissions));
369 }
370
keunyoungcc449f72015-08-12 10:46:27 -0700371 void dump(PrintWriter writer) {
Keun-young Parkbae6e252017-01-25 12:30:15 -0800372 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
keunyounga3b28d82015-08-25 13:05:15 -0700373 writer.println("*Dump all services*");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800374 for (CarServiceBase service : mAllServices) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800375 dumpService(service, writer);
keunyounga3b28d82015-08-25 13:05:15 -0700376 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700377 if (mCarTestService != null) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800378 dumpService(mCarTestService, writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700379 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700380 writer.println("*Dump Vehicle HAL*");
Brad Stenninged3d59b2017-11-09 16:15:17 -0800381 try {
Gregory Clarkd8136062017-12-11 14:27:53 -0800382 // TODO dump all feature flags by creating a dumpable interface
Brad Stenninged3d59b2017-11-09 16:15:17 -0800383 mHal.dump(writer);
384 } catch (Exception e) {
385 writer.println("Failed dumping: " + mHal.getClass().getName());
386 e.printStackTrace(writer);
387 }
388 }
389
390 private void dumpService(CarServiceBase service, PrintWriter writer) {
391 try {
392 service.dump(writer);
393 } catch (Exception e) {
394 writer.println("Failed dumping: " + service.getClass().getName());
395 e.printStackTrace(writer);
396 }
keunyoungcc449f72015-08-12 10:46:27 -0700397 }
Yao Chene33f07e2016-07-26 12:02:51 -0700398
399 void execShellCmd(String[] args, PrintWriter writer) {
400 new CarShellCommand().exec(args, writer);
401 }
402
Pavel Maltsevabd47232017-10-10 16:54:57 -0700403 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700404 private static void traceBegin(String name) {
405 Slog.i(TAG, name);
406 mBootTiming.traceBegin(name);
407 }
408
Pavel Maltsevabd47232017-10-10 16:54:57 -0700409 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700410 private static void traceEnd() {
411 mBootTiming.traceEnd();
412 }
413
Yao Chene33f07e2016-07-26 12:02:51 -0700414 private class CarShellCommand {
415 private static final String COMMAND_HELP = "-h";
416 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800417 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800418 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800419 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800420 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800421
Yao Chene33f07e2016-07-26 12:02:51 -0700422 private static final String PARAM_DAY_MODE = "day";
423 private static final String PARAM_NIGHT_MODE = "night";
424 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800425 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800426 private static final String PARAM_ON_MODE = "on";
427 private static final String PARAM_OFF_MODE = "off";
428 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800429
Yao Chene33f07e2016-07-26 12:02:51 -0700430
431 private void dumpHelp(PrintWriter pw) {
432 pw.println("Car service commands:");
433 pw.println("\t-h");
434 pw.println("\t Print this help text.");
435 pw.println("\tday-night-mode [day|night|sensor]");
436 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800437 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
438 pw.println("\t Inject a vehicle property for testing");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800439 pw.println("\tdisable-uxr true|false");
440 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800441 pw.println("\tgarage-mode [on|off|query]");
442 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800443 pw.println("\tget-do-activities pkgname");
444 pw.println("\t Get Distraction Optimized activities in given package");
Yao Chene33f07e2016-07-26 12:02:51 -0700445 }
446
447 public void exec(String[] args, PrintWriter writer) {
448 String arg = args[0];
449 switch (arg) {
450 case COMMAND_HELP:
451 dumpHelp(writer);
452 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800453 case COMMAND_DAY_NIGHT_MODE: {
Yao Chene33f07e2016-07-26 12:02:51 -0700454 String value = args.length < 1 ? "" : args[1];
455 forceDayNightMode(value, writer);
456 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800457 }
458 case COMMAND_GARAGE_MODE: {
459 String value = args.length < 1 ? "" : args[1];
460 forceGarageMode(value, writer);
461 break;
462 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800463 case COMMAND_INJECT_VHAL_EVENT:
464 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
465 String data;
466 if (args.length < 3) {
467 writer.println("Incorrect number of arguments.");
468 dumpHelp(writer);
469 break;
470 } else if (args.length > 3) {
471 // Zoned
472 zone = args[2];
473 data = args[3];
474 } else {
475 // Global
476 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800477 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800478 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800479 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800480 case COMMAND_ENABLE_UXR:
481 if (args.length < 2) {
482 writer.println("Incorrect number of arguments");
483 dumpHelp(writer);
484 break;
485 }
486 boolean enableBlocking = Boolean.valueOf(args[1]);
487 if (mCarPackageManagerService != null) {
488 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
489 }
490 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800491 case COMMAND_GET_DO_ACTIVITIES:
492 if (args.length < 2) {
493 writer.println("Incorrect number of arguments");
494 dumpHelp(writer);
495 break;
496 }
497 String pkgName = args[1].toLowerCase();
498 if (mCarPackageManagerService != null) {
499 String[] doActivities =
500 mCarPackageManagerService.getDistractionOptimizedActivities(
501 pkgName);
502 if (doActivities != null) {
503 writer.println("DO Activities for " + pkgName);
504 for (String a : doActivities) {
505 writer.println(a);
506 }
507 } else {
508 writer.println("No DO Activities for " + pkgName);
509 }
510 }
511 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700512 default:
513 writer.println("Unknown command.");
514 dumpHelp(writer);
515 }
516 }
517
518 private void forceDayNightMode(String arg, PrintWriter writer) {
519 int mode;
520 switch (arg) {
521 case PARAM_DAY_MODE:
522 mode = CarNightService.FORCED_DAY_MODE;
523 break;
524 case PARAM_NIGHT_MODE:
525 mode = CarNightService.FORCED_NIGHT_MODE;
526 break;
527 case PARAM_SENSOR_MODE:
528 mode = CarNightService.FORCED_SENSOR_MODE;
529 break;
530 default:
531 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
532 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
533 return;
534 }
535 int current = mCarNightService.forceDayNightMode(mode);
536 String currentMode = null;
537 switch (current) {
538 case UiModeManager.MODE_NIGHT_AUTO:
539 currentMode = PARAM_SENSOR_MODE;
540 break;
541 case UiModeManager.MODE_NIGHT_YES:
542 currentMode = PARAM_NIGHT_MODE;
543 break;
544 case UiModeManager.MODE_NIGHT_NO:
545 currentMode = PARAM_DAY_MODE;
546 break;
547 }
548 writer.println("DayNightMode changed to: " + currentMode);
549 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800550
Bryan Eylera32a7c12018-02-27 15:40:00 -0800551 private void forceGarageMode(String arg, PrintWriter writer) {
552 switch (arg) {
553 case PARAM_ON_MODE:
554 mGarageModeService.onPrepareShutdown(false);
555 break;
556 case PARAM_OFF_MODE:
557 mGarageModeService.onSleepEntry();
558 break;
559 case PARAM_QUERY_MODE:
560 // Nothing to do. Always query at the end anyway.
561 break;
562 default:
563 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
564 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
565 return;
566 }
567 writer.println("Garage mode: " + mGarageModeService.isInGarageMode());
568 }
569
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800570 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800571 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800572 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800573 * @param property the Vehicle property Id as defined in the HAL
574 * @param zone Zone that this event services
575 * @param value Data value of the event
576 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800577 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800578 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800579 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800580 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
581 if (!isPropertyAreaTypeGlobal(property)) {
582 writer.println("Property area type inconsistent with given zone");
583 return;
584 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800585 }
586 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800587 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800588 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800589 writer.println("Invalid property Id zone Id or value" + e);
590 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800591 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800592 }
593
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800594 // Check if the given property is global
595 private boolean isPropertyAreaTypeGlobal(String property) {
596 if (property == null) {
597 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800598 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800599 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800600 }
Yao Chene33f07e2016-07-26 12:02:51 -0700601 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700602}