blob: 1edacd9e376572172ac0ebaf5df15e5790befb41 [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;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080035import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070036import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080037import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080038import com.android.car.pm.CarPackageManagerService;
Enrico Granatab19bc322017-10-12 12:25:06 -070039import com.android.car.systeminterface.SystemInterface;
keunyoungca515072015-07-10 12:21:47 -070040import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000041import com.android.internal.car.ICarServiceHelper;
keunyounga3b28d82015-08-25 13:05:15 -070042import java.io.PrintWriter;
Antonio Cortese4619c72017-02-02 07:53:27 -080043import java.util.ArrayList;
44import java.util.Arrays;
45import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070046
keunyoungca515072015-07-10 12:21:47 -070047public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070048
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080049 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070050 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
51 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080052
keunyoungca515072015-07-10 12:21:47 -070053 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080054 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070055
Keun-young Park4727da32016-05-31 10:00:51 -070056 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070057 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080058 private final CarPackageManagerService mCarPackageManagerService;
59 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070060 private final CarSensorService mCarSensorService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080061 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080062 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyounga3b28d82015-08-25 13:05:15 -070063 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070064 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080065 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070066 private final CarCabinService mCarCabinService;
Steve Paik66481982015-10-27 15:22:38 -070067 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070068 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080069 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070070 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080071 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080072 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080073 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070074 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070075 private final CarVendorExtensionService mCarVendorExtensionService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080076 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070077 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070078 private final CarDiagnosticService mCarDiagnosticService;
79 private final CarStorageMonitoringService mCarStorageMonitoringService;
Antonio Cortese4619c72017-02-02 07:53:27 -080080 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080081 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070082
Pavel Maltsev0d07c762016-11-03 16:40:15 -070083 private final CarServiceBase[] mAllServices;
84
Steve Paikc302c7c2017-08-04 14:01:58 -070085 private static final String TAG = "ICarImpl";
86 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Keun-young Park4a79a382017-08-10 18:19:14 -070087 private static final TimingsTraceLog mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG,
Gregory Clarkd8136062017-12-11 14:27:53 -080088 Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -070089
keunyoung1ab8e182015-09-24 09:25:22 -070090 /** Test only service. Populate it only when necessary. */
91 @GuardedBy("this")
92 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070093
Keun-young Parkaab47cd2017-06-05 11:04:50 -070094 @GuardedBy("this")
95 private ICarServiceHelper mICarServiceHelper;
96
Pavel Maltsevec83b632017-01-05 15:10:55 -080097 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
98 CanBusErrorNotifier errorNotifier) {
keunyoungca515072015-07-10 12:21:47 -070099 mContext = serviceContext;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700100 mHal = new VehicleHal(vehicle);
Keun-young Park4727da32016-05-31 10:00:51 -0700101 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700102 mCarPowerManagementService = new CarPowerManagementService(
103 mHal.getPowerHal(), systemInterface);
104 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800105 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarSensorService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800106 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800107 mCarDrivingStateService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800108 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
109 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700110 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700111 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800112 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -0800113 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Gregory Clarkd8136062017-12-11 14:27:53 -0800114 mCarLocationService = new CarLocationService(mContext, mCarPowerManagementService,
115 mCarSensorService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700116 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700117 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800118 mCarAudioService = new CarAudioService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700119 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
120 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
121 mCarRadioService = new CarRadioService(serviceContext, mHal.getRadioHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700122 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700123 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700124 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700125 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700126 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700127 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
128 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700129 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800130 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700131 mCarSensorService, mPerUserCarServiceHelper);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700132 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
133 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700134 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700135 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
136 systemInterface);
keunyounga74b9ca2015-10-21 13:33:58 -0700137
keunyounga3b28d82015-08-25 13:05:15 -0700138 // Be careful with order. Service depending on other service should be inited later.
Antonio Cortese4619c72017-02-02 07:53:27 -0800139 List<CarServiceBase> allServices = new ArrayList<>(Arrays.asList(
Keun-young Park4727da32016-05-31 10:00:51 -0700140 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700141 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700142 mCarSensorService,
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800143 mCarDrivingStateService,
144 mCarUXRestrictionsService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800145 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800146 mCarInputService,
Gregory Clarkd8136062017-12-11 14:27:53 -0800147 mCarLocationService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800148 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700149 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700150 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700151 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700152 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700153 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800154 mCarRadioService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800155 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800156 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800157 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700158 mSystemStateControllerService,
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800159 mCarVendorExtensionService,
Enrico Granatab3634e22017-05-05 18:02:31 -0700160 mCarBluetoothService,
Ram Periathiruvadia048c0a2017-05-09 07:35:03 -0700161 mCarDiagnosticService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700162 mPerUserCarServiceHelper,
Enrico Granata9a916d72017-09-19 14:33:08 -0700163 mCarStorageMonitoringService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700164 mVmsSubscriberService,
165 mVmsPublisherService
Antonio Cortese4619c72017-02-02 07:53:27 -0800166 ));
Antonio Cortese4619c72017-02-02 07:53:27 -0800167 mAllServices = allServices.toArray(new CarServiceBase[0]);
keunyoungca515072015-07-10 12:21:47 -0700168 }
169
Pavel Maltsevabd47232017-10-10 16:54:57 -0700170 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700171 void init() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700172 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700173 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700174 traceEnd();
175 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800176 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700177 service.init();
178 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700179 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700180 }
181
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700182 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700183 // release done in opposite order from init
184 for (int i = mAllServices.length - 1; i >= 0; i--) {
185 mAllServices[i].release();
186 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700187 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700188 }
189
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700190 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800191 mHal.vehicleHalReconnected(vehicle);
192 for (CarServiceBase service : mAllServices) {
193 service.vehicleHalReconnected();
194 }
195 }
196
keunyoungca515072015-07-10 12:21:47 -0700197 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700198 public void setCarServiceHelper(IBinder helper) {
199 int uid = Binder.getCallingUid();
200 if (uid != Process.SYSTEM_UID) {
201 throw new SecurityException("Only allowed from system");
202 }
203 synchronized (this) {
204 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
205 }
206 }
207
208 @Override
keunyoungca515072015-07-10 12:21:47 -0700209 public IBinder getCarService(String serviceName) {
210 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800211 case Car.AUDIO_SERVICE:
212 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700213 case Car.SENSOR_SERVICE:
214 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700215 case Car.INFO_SERVICE:
216 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700217 case Car.APP_FOCUS_SERVICE:
218 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800219 case Car.PACKAGE_SERVICE:
220 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700221 case Car.CABIN_SERVICE:
222 assertCabinPermission(mContext);
223 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800224 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700225 assertAnyDiagnosticPermission(mContext);
226 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800227 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700228 assertHvacPermission(mContext);
229 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800230 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800231 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700232 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800233 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700234 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700235 IInstrumentClusterNavigation navService =
236 mInstrumentClusterService.getNavigationService();
237 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700238 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
239 assertClusterManagerPermission(mContext);
240 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800241 case Car.PROJECTION_SERVICE:
242 assertProjectionPermission(mContext);
243 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700244 case Car.VENDOR_EXTENSION_SERVICE:
245 assertVendorExtensionPermission(mContext);
246 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800247 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700248 assertVmsSubscriberPermission(mContext);
249 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800250 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700251 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700252 synchronized (this) {
253 if (mCarTestService == null) {
254 mCarTestService = new CarTestService(mContext, this);
255 }
256 return mCarTestService;
257 }
258 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700259 case Car.BLUETOOTH_SERVICE:
260 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700261 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700262 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700263 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800264 case Car.CAR_DRIVING_STATE_SERVICE:
265 assertDrivingStatePermission(mContext);
266 return mCarDrivingStateService;
267 case Car.CAR_UX_RESTRICTION_SERVICE:
268 return mCarUXRestrictionsService;
keunyoungca515072015-07-10 12:21:47 -0700269 default:
270 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
271 return null;
272 }
273 }
274
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800275 @Override
276 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700277 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800278 }
279
Keun-young Parka28d7b22016-02-29 16:54:29 -0800280 public CarServiceBase getCarInternalService(String serviceName) {
281 switch (serviceName) {
282 case INTERNAL_INPUT_SERVICE:
283 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700284 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
285 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800286 default:
287 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
288 serviceName);
289 return null;
290 }
291 }
292
keunyoung1ab8e182015-09-24 09:25:22 -0700293 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700294 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700295 }
296
Steve Paik43c04a72016-07-08 19:12:09 -0700297 public static void assertCabinPermission(Context context) {
298 assertPermission(context, Car.PERMISSION_CAR_CABIN);
299 }
300
Keun-young Parke31a8b22016-03-16 17:34:08 -0700301 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700302 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800303 }
304
Pavel Maltsev905968c2017-07-16 19:48:57 -0700305 public static void assertClusterManagerPermission(Context context) {
306 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
307 }
308
Steve Paik66481982015-10-27 15:22:38 -0700309 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700310 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700311 }
312
keunyoung6b197692015-11-16 13:54:38 -0800313 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700314 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700315 }
316
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800317 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700318 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
319 }
320
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700321 public static void assertVendorExtensionPermission(Context context) {
322 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
323 }
324
Enrico Granata3c7a6662017-02-23 18:07:59 -0800325 public static void assertAnyDiagnosticPermission(Context context) {
326 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700327 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800328 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
329 }
330
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800331 public static void assertDrivingStatePermission(Context context) {
332 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
333 }
334
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800335 public static void assertVmsPublisherPermission(Context context) {
336 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
337 }
338
Antonio Cortese4619c72017-02-02 07:53:27 -0800339 public static void assertVmsSubscriberPermission(Context context) {
340 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
341 }
342
Steve Paik461ecc62016-06-08 15:28:32 -0700343 public static void assertPermission(Context context, String permission) {
344 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
345 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800346 }
347 }
348
Enrico Granata3c7a6662017-02-23 18:07:59 -0800349 public static void assertAnyPermission(Context context, String... permissions) {
350 for (String permission : permissions) {
351 if (context.checkCallingOrSelfPermission(permission) ==
352 PackageManager.PERMISSION_GRANTED) {
353 return;
354 }
355 }
356 throw new SecurityException("requires any of " + Arrays.toString(permissions));
357 }
358
keunyoungcc449f72015-08-12 10:46:27 -0700359 void dump(PrintWriter writer) {
Keun-young Parkbae6e252017-01-25 12:30:15 -0800360 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
keunyounga3b28d82015-08-25 13:05:15 -0700361 writer.println("*Dump all services*");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800362 for (CarServiceBase service : mAllServices) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800363 dumpService(service, writer);
keunyounga3b28d82015-08-25 13:05:15 -0700364 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700365 if (mCarTestService != null) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800366 dumpService(mCarTestService, writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700367 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700368 writer.println("*Dump Vehicle HAL*");
Brad Stenninged3d59b2017-11-09 16:15:17 -0800369 try {
Gregory Clarkd8136062017-12-11 14:27:53 -0800370 // TODO dump all feature flags by creating a dumpable interface
Brad Stenninged3d59b2017-11-09 16:15:17 -0800371 mHal.dump(writer);
372 } catch (Exception e) {
373 writer.println("Failed dumping: " + mHal.getClass().getName());
374 e.printStackTrace(writer);
375 }
376 }
377
378 private void dumpService(CarServiceBase service, PrintWriter writer) {
379 try {
380 service.dump(writer);
381 } catch (Exception e) {
382 writer.println("Failed dumping: " + service.getClass().getName());
383 e.printStackTrace(writer);
384 }
keunyoungcc449f72015-08-12 10:46:27 -0700385 }
Yao Chene33f07e2016-07-26 12:02:51 -0700386
387 void execShellCmd(String[] args, PrintWriter writer) {
388 new CarShellCommand().exec(args, writer);
389 }
390
Pavel Maltsevabd47232017-10-10 16:54:57 -0700391 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700392 private static void traceBegin(String name) {
393 Slog.i(TAG, name);
394 mBootTiming.traceBegin(name);
395 }
396
Pavel Maltsevabd47232017-10-10 16:54:57 -0700397 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700398 private static void traceEnd() {
399 mBootTiming.traceEnd();
400 }
401
Yao Chene33f07e2016-07-26 12:02:51 -0700402 private class CarShellCommand {
403 private static final String COMMAND_HELP = "-h";
404 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800405 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800406 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800407
Yao Chene33f07e2016-07-26 12:02:51 -0700408 private static final String PARAM_DAY_MODE = "day";
409 private static final String PARAM_NIGHT_MODE = "night";
410 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800411 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
412
Yao Chene33f07e2016-07-26 12:02:51 -0700413
414 private void dumpHelp(PrintWriter pw) {
415 pw.println("Car service commands:");
416 pw.println("\t-h");
417 pw.println("\t Print this help text.");
418 pw.println("\tday-night-mode [day|night|sensor]");
419 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800420 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
421 pw.println("\t Inject a vehicle property for testing");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800422 pw.println("\tdisable-uxr true|false");
423 pw.println("\t Disable UX restrictions and App blocking.");
Yao Chene33f07e2016-07-26 12:02:51 -0700424 }
425
426 public void exec(String[] args, PrintWriter writer) {
427 String arg = args[0];
428 switch (arg) {
429 case COMMAND_HELP:
430 dumpHelp(writer);
431 break;
432 case COMMAND_DAY_NIGHT_MODE:
433 String value = args.length < 1 ? "" : args[1];
434 forceDayNightMode(value, writer);
435 break;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800436 case COMMAND_INJECT_VHAL_EVENT:
437 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
438 String data;
439 if (args.length < 3) {
440 writer.println("Incorrect number of arguments.");
441 dumpHelp(writer);
442 break;
443 } else if (args.length > 3) {
444 // Zoned
445 zone = args[2];
446 data = args[3];
447 } else {
448 // Global
449 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800450 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800451 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800452 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800453 case COMMAND_ENABLE_UXR:
454 if (args.length < 2) {
455 writer.println("Incorrect number of arguments");
456 dumpHelp(writer);
457 break;
458 }
459 boolean enableBlocking = Boolean.valueOf(args[1]);
460 if (mCarPackageManagerService != null) {
461 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
462 }
463 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700464 default:
465 writer.println("Unknown command.");
466 dumpHelp(writer);
467 }
468 }
469
470 private void forceDayNightMode(String arg, PrintWriter writer) {
471 int mode;
472 switch (arg) {
473 case PARAM_DAY_MODE:
474 mode = CarNightService.FORCED_DAY_MODE;
475 break;
476 case PARAM_NIGHT_MODE:
477 mode = CarNightService.FORCED_NIGHT_MODE;
478 break;
479 case PARAM_SENSOR_MODE:
480 mode = CarNightService.FORCED_SENSOR_MODE;
481 break;
482 default:
483 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
484 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
485 return;
486 }
487 int current = mCarNightService.forceDayNightMode(mode);
488 String currentMode = null;
489 switch (current) {
490 case UiModeManager.MODE_NIGHT_AUTO:
491 currentMode = PARAM_SENSOR_MODE;
492 break;
493 case UiModeManager.MODE_NIGHT_YES:
494 currentMode = PARAM_NIGHT_MODE;
495 break;
496 case UiModeManager.MODE_NIGHT_NO:
497 currentMode = PARAM_DAY_MODE;
498 break;
499 }
500 writer.println("DayNightMode changed to: " + currentMode);
501 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800502
503 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800504 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800505 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800506 * @param property the Vehicle property Id as defined in the HAL
507 * @param zone Zone that this event services
508 * @param value Data value of the event
509 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800510 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800511 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800512 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800513 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
514 if (!isPropertyAreaTypeGlobal(property)) {
515 writer.println("Property area type inconsistent with given zone");
516 return;
517 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800518 }
519 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800520 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800521 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800522 writer.println("Invalid property Id zone Id or value" + e);
523 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800524 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800525 }
526
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800527 // Check if the given property is global
528 private boolean isPropertyAreaTypeGlobal(String property) {
529 if (property == null) {
530 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800531 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800532 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800533 }
Yao Chene33f07e2016-07-26 12:02:51 -0700534 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700535}