blob: cd2f47dda6996e602ca4a8df660c351882ebc142 [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;
Antonio Cortese4619c72017-02-02 07:53:27 -080023import android.car.annotation.FutureFeature;
Pavel Maltsev0477e292016-05-27 12:22:36 -070024import android.car.cluster.renderer.IInstrumentClusterNavigation;
keunyoungca515072015-07-10 12:21:47 -070025import android.content.Context;
keunyoung1ab8e182015-09-24 09:25:22 -070026import android.content.pm.PackageManager;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080027import android.hardware.automotive.vehicle.V2_0.IVehicle;
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;
Hongwei Wang30557232018-01-02 10:25:08 -080035
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;
Hongwei Wang30557232018-01-02 10:25:08 -080043
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;
Enrico Granata5c56d2a2017-02-07 15:38:12 -080082 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -080083 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080084 @FutureFeature
85 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070086
Pavel Maltsev0d07c762016-11-03 16:40:15 -070087 private final CarServiceBase[] mAllServices;
88
Steve Paikc302c7c2017-08-04 14:01:58 -070089 private static final String TAG = "ICarImpl";
90 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Keun-young Park4a79a382017-08-10 18:19:14 -070091 private static final TimingsTraceLog mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG,
Gregory Clarkd8136062017-12-11 14:27:53 -080092 Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -070093
keunyoung1ab8e182015-09-24 09:25:22 -070094 /** Test only service. Populate it only when necessary. */
95 @GuardedBy("this")
96 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070097
Keun-young Parkaab47cd2017-06-05 11:04:50 -070098 @GuardedBy("this")
99 private ICarServiceHelper mICarServiceHelper;
100
Pavel Maltsevec83b632017-01-05 15:10:55 -0800101 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
102 CanBusErrorNotifier errorNotifier) {
keunyoungca515072015-07-10 12:21:47 -0700103 mContext = serviceContext;
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);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700106 mCarPowerManagementService = new CarPowerManagementService(
107 mHal.getPowerHal(), systemInterface);
108 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 Periathiruvadi25c16f12017-11-17 16:48:37 -0800111 mCarDrivingStateService);
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 Clarkd8136062017-12-11 14:27:53 -0800118 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);
209 }
210 }
211
212 @Override
keunyoungca515072015-07-10 12:21:47 -0700213 public IBinder getCarService(String serviceName) {
214 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800215 case Car.AUDIO_SERVICE:
216 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700217 case Car.SENSOR_SERVICE:
218 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700219 case Car.INFO_SERVICE:
220 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700221 case Car.APP_FOCUS_SERVICE:
222 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800223 case Car.PACKAGE_SERVICE:
224 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700225 case Car.CABIN_SERVICE:
226 assertCabinPermission(mContext);
227 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800228 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700229 assertAnyDiagnosticPermission(mContext);
230 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800231 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700232 assertHvacPermission(mContext);
233 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800234 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800235 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700236 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800237 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700238 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700239 IInstrumentClusterNavigation navService =
240 mInstrumentClusterService.getNavigationService();
241 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700242 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
243 assertClusterManagerPermission(mContext);
244 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800245 case Car.PROJECTION_SERVICE:
246 assertProjectionPermission(mContext);
247 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700248 case Car.VENDOR_EXTENSION_SERVICE:
249 assertVendorExtensionPermission(mContext);
250 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800251 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700252 assertVmsSubscriberPermission(mContext);
253 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800254 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700255 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700256 synchronized (this) {
257 if (mCarTestService == null) {
258 mCarTestService = new CarTestService(mContext, this);
259 }
260 return mCarTestService;
261 }
262 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700263 case Car.BLUETOOTH_SERVICE:
264 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700265 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700266 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700267 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800268 case Car.CAR_DRIVING_STATE_SERVICE:
269 assertDrivingStatePermission(mContext);
270 return mCarDrivingStateService;
271 case Car.CAR_UX_RESTRICTION_SERVICE:
272 return mCarUXRestrictionsService;
keunyoungca515072015-07-10 12:21:47 -0700273 default:
274 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
275 return null;
276 }
277 }
278
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800279 @Override
280 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700281 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800282 }
283
Keun-young Parka28d7b22016-02-29 16:54:29 -0800284 public CarServiceBase getCarInternalService(String serviceName) {
285 switch (serviceName) {
286 case INTERNAL_INPUT_SERVICE:
287 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700288 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
289 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800290 default:
291 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
292 serviceName);
293 return null;
294 }
295 }
296
keunyoung1ab8e182015-09-24 09:25:22 -0700297 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700298 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700299 }
300
Steve Paik43c04a72016-07-08 19:12:09 -0700301 public static void assertCabinPermission(Context context) {
302 assertPermission(context, Car.PERMISSION_CAR_CABIN);
303 }
304
Keun-young Parke31a8b22016-03-16 17:34:08 -0700305 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700306 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800307 }
308
Pavel Maltsev905968c2017-07-16 19:48:57 -0700309 public static void assertClusterManagerPermission(Context context) {
310 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
311 }
312
Steve Paik66481982015-10-27 15:22:38 -0700313 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700314 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700315 }
316
keunyoung6b197692015-11-16 13:54:38 -0800317 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700318 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700319 }
320
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800321 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700322 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
323 }
324
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700325 public static void assertVendorExtensionPermission(Context context) {
326 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
327 }
328
Enrico Granata3c7a6662017-02-23 18:07:59 -0800329 public static void assertAnyDiagnosticPermission(Context context) {
330 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700331 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800332 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
333 }
334
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800335 public static void assertDrivingStatePermission(Context context) {
336 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
337 }
338
Enrico Granata3c7a6662017-02-23 18:07:59 -0800339 @FutureFeature
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800340 public static void assertVmsPublisherPermission(Context context) {
341 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
342 }
343
344 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -0800345 public static void assertVmsSubscriberPermission(Context context) {
346 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
347 }
348
Steve Paik461ecc62016-06-08 15:28:32 -0700349 public static void assertPermission(Context context, String permission) {
350 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
351 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800352 }
353 }
354
Enrico Granata3c7a6662017-02-23 18:07:59 -0800355 public static void assertAnyPermission(Context context, String... permissions) {
356 for (String permission : permissions) {
357 if (context.checkCallingOrSelfPermission(permission) ==
358 PackageManager.PERMISSION_GRANTED) {
359 return;
360 }
361 }
362 throw new SecurityException("requires any of " + Arrays.toString(permissions));
363 }
364
keunyoungcc449f72015-08-12 10:46:27 -0700365 void dump(PrintWriter writer) {
Keun-young Parkbae6e252017-01-25 12:30:15 -0800366 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
keunyounga3b28d82015-08-25 13:05:15 -0700367 writer.println("*Dump all services*");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800368 for (CarServiceBase service : mAllServices) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800369 dumpService(service, writer);
keunyounga3b28d82015-08-25 13:05:15 -0700370 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700371 if (mCarTestService != null) {
Brad Stenninged3d59b2017-11-09 16:15:17 -0800372 dumpService(mCarTestService, writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700373 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700374 writer.println("*Dump Vehicle HAL*");
Brad Stenninged3d59b2017-11-09 16:15:17 -0800375 try {
Gregory Clarkd8136062017-12-11 14:27:53 -0800376 // TODO dump all feature flags by creating a dumpable interface
Brad Stenninged3d59b2017-11-09 16:15:17 -0800377 mHal.dump(writer);
378 } catch (Exception e) {
379 writer.println("Failed dumping: " + mHal.getClass().getName());
380 e.printStackTrace(writer);
381 }
382 }
383
384 private void dumpService(CarServiceBase service, PrintWriter writer) {
385 try {
386 service.dump(writer);
387 } catch (Exception e) {
388 writer.println("Failed dumping: " + service.getClass().getName());
389 e.printStackTrace(writer);
390 }
keunyoungcc449f72015-08-12 10:46:27 -0700391 }
Yao Chene33f07e2016-07-26 12:02:51 -0700392
393 void execShellCmd(String[] args, PrintWriter writer) {
394 new CarShellCommand().exec(args, writer);
395 }
396
Pavel Maltsevabd47232017-10-10 16:54:57 -0700397 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700398 private static void traceBegin(String name) {
399 Slog.i(TAG, name);
400 mBootTiming.traceBegin(name);
401 }
402
Pavel Maltsevabd47232017-10-10 16:54:57 -0700403 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700404 private static void traceEnd() {
405 mBootTiming.traceEnd();
406 }
407
Yao Chene33f07e2016-07-26 12:02:51 -0700408 private class CarShellCommand {
409 private static final String COMMAND_HELP = "-h";
410 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800411 private static final String COMMAND_INJECT_EVENT = "inject-event";
412
Yao Chene33f07e2016-07-26 12:02:51 -0700413 private static final String PARAM_DAY_MODE = "day";
414 private static final String PARAM_NIGHT_MODE = "night";
415 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800416 private static final String PARAM_ZONED_BOOLEAN = "zoned-boolean";
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800417 private static final String PARAM_GLOBAL_INT = "global-integer";
Yao Chene33f07e2016-07-26 12:02:51 -0700418
419 private void dumpHelp(PrintWriter pw) {
420 pw.println("Car service commands:");
421 pw.println("\t-h");
422 pw.println("\t Print this help text.");
423 pw.println("\tday-night-mode [day|night|sensor]");
424 pw.println("\t Force into day/night mode or restore to auto.");
Enrico Granatacf53fd72017-09-28 10:45:44 -0700425 pw.println("\tday-night-mode [day|night|sensor]");
426 pw.println("\t Force into day/night mode or restore to auto.");
Yao Chene33f07e2016-07-26 12:02:51 -0700427 }
428
429 public void exec(String[] args, PrintWriter writer) {
430 String arg = args[0];
431 switch (arg) {
432 case COMMAND_HELP:
433 dumpHelp(writer);
434 break;
435 case COMMAND_DAY_NIGHT_MODE:
436 String value = args.length < 1 ? "" : args[1];
437 forceDayNightMode(value, writer);
438 break;
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800439 case COMMAND_INJECT_EVENT:
440 String eventType;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800441 if (args.length > 1) {
442 eventType = args[1].toLowerCase();
443 switch (eventType) {
444 case PARAM_ZONED_BOOLEAN:
445 if (args.length < 5) {
446 writer.println("Incorrect number of arguments.");
447 dumpHelp(writer);
448 break;
449 }
450 inject_zoned_boolean_event(args[2], args[3], args[4], writer);
451 break;
452
453 case PARAM_GLOBAL_INT:
454 if (args.length < 4) {
455 writer.println("Incorrect number of Arguments");
456 dumpHelp(writer);
457 break;
458 }
459 inject_global_integer_event(args[2], args[3], writer);
460 break;
461
462 default:
463 writer.println("Unsupported event type");
464 dumpHelp(writer);
465 break;
466 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800467 }
468 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700469 default:
470 writer.println("Unknown command.");
471 dumpHelp(writer);
472 }
473 }
474
475 private void forceDayNightMode(String arg, PrintWriter writer) {
476 int mode;
477 switch (arg) {
478 case PARAM_DAY_MODE:
479 mode = CarNightService.FORCED_DAY_MODE;
480 break;
481 case PARAM_NIGHT_MODE:
482 mode = CarNightService.FORCED_NIGHT_MODE;
483 break;
484 case PARAM_SENSOR_MODE:
485 mode = CarNightService.FORCED_SENSOR_MODE;
486 break;
487 default:
488 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
489 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
490 return;
491 }
492 int current = mCarNightService.forceDayNightMode(mode);
493 String currentMode = null;
494 switch (current) {
495 case UiModeManager.MODE_NIGHT_AUTO:
496 currentMode = PARAM_SENSOR_MODE;
497 break;
498 case UiModeManager.MODE_NIGHT_YES:
499 currentMode = PARAM_NIGHT_MODE;
500 break;
501 case UiModeManager.MODE_NIGHT_NO:
502 currentMode = PARAM_DAY_MODE;
503 break;
504 }
505 writer.println("DayNightMode changed to: " + currentMode);
506 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800507
508 /**
509 * Inject a fake boolean HAL event to help testing.
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800510 *
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800511 * @param property - Vehicle Property
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800512 * @param value - boolean value for the property
513 * @param writer - Printwriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800514 */
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800515 private void inject_zoned_boolean_event(String property, String zone, String value,
516 PrintWriter writer) {
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800517 Log.d(CarLog.TAG_SERVICE, "Injecting Boolean event");
518 boolean event;
519 int propId;
520 int zoneId;
521 if (value.equalsIgnoreCase("true")) {
522 event = true;
523 } else {
524 event = false;
525 }
526 try {
527 propId = Integer.decode(property);
528 zoneId = Integer.decode(zone);
529 } catch (NumberFormatException e) {
530 writer.println("Invalid property Id or Zone Id. Prefix hex values with 0x");
531 return;
532 }
533 mHal.injectBooleanEvent(propId, zoneId, event);
534 }
535
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800536 /**
537 * Inject a fake Integer HAL event to help testing.
538 *
539 * @param property - Vehicle Property
540 * @param value - Integer value to inject
541 * @param writer - PrintWriter
542 */
543 private void inject_global_integer_event(String property, String value,
544 PrintWriter writer) {
545 Log.d(CarLog.TAG_SERVICE, "Injecting integer event");
546 int propId;
547 int eventValue;
548 try {
549 propId = Integer.decode(property);
550 eventValue = Integer.decode(value);
551 } catch (NumberFormatException e) {
552 writer.println("Invalid property Id or event value. Prefix hex values with 0x");
553 return;
554 }
555 mHal.injectIntegerEvent(propId, eventValue);
556 }
Yao Chene33f07e2016-07-26 12:02:51 -0700557 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700558}