blob: f61271518d357c0d5ec37c4e7ab7db2ae19670aa [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;
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;
38import com.android.car.internal.FeatureUtil;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080039import com.android.car.pm.CarPackageManagerService;
keunyoungca515072015-07-10 12:21:47 -070040import com.android.internal.annotations.GuardedBy;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070041import 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;
keunyounga3b28d82015-08-25 13:05:15 -070061 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070062 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080063 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070064 private final CarCabinService mCarCabinService;
Steve Paik66481982015-10-27 15:22:38 -070065 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070066 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080067 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070068 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080069 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080070 private final InstrumentClusterService mInstrumentClusterService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070071 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070072 private final CarVendorExtensionService mCarVendorExtensionService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080073 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070074 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070075 private final CarDiagnosticService mCarDiagnosticService;
76 private final CarStorageMonitoringService mCarStorageMonitoringService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -080077 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -080078 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080079 @FutureFeature
80 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070081
Pavel Maltsev0d07c762016-11-03 16:40:15 -070082 private final CarServiceBase[] mAllServices;
83
Steve Paikc302c7c2017-08-04 14:01:58 -070084 private static final String TAG = "ICarImpl";
85 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Keun-young Park4a79a382017-08-10 18:19:14 -070086 private static final TimingsTraceLog mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG,
Steve Paikc302c7c2017-08-04 14:01:58 -070087 Trace.TRACE_TAG_HAL);
88
keunyoung1ab8e182015-09-24 09:25:22 -070089 /** Test only service. Populate it only when necessary. */
90 @GuardedBy("this")
91 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070092
Keun-young Parkaab47cd2017-06-05 11:04:50 -070093 @GuardedBy("this")
94 private ICarServiceHelper mICarServiceHelper;
95
Pavel Maltsevec83b632017-01-05 15:10:55 -080096 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
97 CanBusErrorNotifier errorNotifier) {
keunyoungca515072015-07-10 12:21:47 -070098 mContext = serviceContext;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070099 mHal = new VehicleHal(vehicle);
Keun-young Park4727da32016-05-31 10:00:51 -0700100 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700101 mCarPowerManagementService = new CarPowerManagementService(
102 mHal.getPowerHal(), systemInterface);
103 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Keun-young Park4727da32016-05-31 10:00:51 -0700104 mCarPackageManagerService = new CarPackageManagerService(serviceContext, mCarSensorService,
105 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700106 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800107 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -0800108 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700109 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700110 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700111 mCarAudioService = new CarAudioService(serviceContext, mHal.getAudioHal(),
Pavel Maltsevec83b632017-01-05 15:10:55 -0800112 mCarInputService, errorNotifier);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700113 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
114 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
115 mCarRadioService = new CarRadioService(serviceContext, mHal.getRadioHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700116 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700117 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700118 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700119 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700120 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700121 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
122 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700123 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800124 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700125 mCarSensorService, mPerUserCarServiceHelper);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700126 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
127 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700128 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700129 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
130 systemInterface);
keunyounga74b9ca2015-10-21 13:33:58 -0700131
keunyounga3b28d82015-08-25 13:05:15 -0700132 // Be careful with order. Service depending on other service should be inited later.
Antonio Cortese4619c72017-02-02 07:53:27 -0800133 List<CarServiceBase> allServices = new ArrayList<>(Arrays.asList(
Keun-young Park4727da32016-05-31 10:00:51 -0700134 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700135 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700136 mCarSensorService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800137 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800138 mCarInputService,
139 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700140 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700141 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700142 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700143 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700144 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800145 mCarRadioService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800146 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800147 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800148 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700149 mSystemStateControllerService,
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800150 mCarVendorExtensionService,
Enrico Granatab3634e22017-05-05 18:02:31 -0700151 mCarBluetoothService,
Ram Periathiruvadia048c0a2017-05-09 07:35:03 -0700152 mCarDiagnosticService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700153 mPerUserCarServiceHelper,
Enrico Granata9a916d72017-09-19 14:33:08 -0700154 mCarStorageMonitoringService,
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700155 mVmsSubscriberService,
156 mVmsPublisherService
Antonio Cortese4619c72017-02-02 07:53:27 -0800157 ));
Antonio Cortese4619c72017-02-02 07:53:27 -0800158 mAllServices = allServices.toArray(new CarServiceBase[0]);
keunyoungca515072015-07-10 12:21:47 -0700159 }
160
Pavel Maltsevabd47232017-10-10 16:54:57 -0700161 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700162 void init() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700163 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700164 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700165 traceEnd();
166 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800167 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700168 service.init();
169 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700170 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700171 }
172
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700173 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700174 // release done in opposite order from init
175 for (int i = mAllServices.length - 1; i >= 0; i--) {
176 mAllServices[i].release();
177 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700178 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700179 }
180
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700181 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800182 mHal.vehicleHalReconnected(vehicle);
183 for (CarServiceBase service : mAllServices) {
184 service.vehicleHalReconnected();
185 }
186 }
187
keunyoungca515072015-07-10 12:21:47 -0700188 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700189 public void setCarServiceHelper(IBinder helper) {
190 int uid = Binder.getCallingUid();
191 if (uid != Process.SYSTEM_UID) {
192 throw new SecurityException("Only allowed from system");
193 }
194 synchronized (this) {
195 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
196 }
197 }
198
199 @Override
keunyoungca515072015-07-10 12:21:47 -0700200 public IBinder getCarService(String serviceName) {
201 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800202 case Car.AUDIO_SERVICE:
203 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700204 case Car.SENSOR_SERVICE:
205 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700206 case Car.INFO_SERVICE:
207 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700208 case Car.APP_FOCUS_SERVICE:
209 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800210 case Car.PACKAGE_SERVICE:
211 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700212 case Car.CABIN_SERVICE:
213 assertCabinPermission(mContext);
214 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800215 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700216 assertAnyDiagnosticPermission(mContext);
217 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800218 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700219 assertHvacPermission(mContext);
220 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800221 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800222 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700223 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800224 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700225 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700226 IInstrumentClusterNavigation navService =
227 mInstrumentClusterService.getNavigationService();
228 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700229 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
230 assertClusterManagerPermission(mContext);
231 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800232 case Car.PROJECTION_SERVICE:
233 assertProjectionPermission(mContext);
234 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700235 case Car.VENDOR_EXTENSION_SERVICE:
236 assertVendorExtensionPermission(mContext);
237 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800238 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700239 assertVmsSubscriberPermission(mContext);
240 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800241 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700242 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700243 synchronized (this) {
244 if (mCarTestService == null) {
245 mCarTestService = new CarTestService(mContext, this);
246 }
247 return mCarTestService;
248 }
249 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700250 case Car.BLUETOOTH_SERVICE:
251 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700252 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700253 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700254 return mCarStorageMonitoringService;
keunyoungca515072015-07-10 12:21:47 -0700255 default:
256 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
257 return null;
258 }
259 }
260
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800261 @Override
262 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700263 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800264 }
265
Keun-young Parka28d7b22016-02-29 16:54:29 -0800266 public CarServiceBase getCarInternalService(String serviceName) {
267 switch (serviceName) {
268 case INTERNAL_INPUT_SERVICE:
269 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700270 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
271 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800272 default:
273 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
274 serviceName);
275 return null;
276 }
277 }
278
keunyoung1ab8e182015-09-24 09:25:22 -0700279 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700280 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700281 }
282
Steve Paik43c04a72016-07-08 19:12:09 -0700283 public static void assertCabinPermission(Context context) {
284 assertPermission(context, Car.PERMISSION_CAR_CABIN);
285 }
286
Keun-young Parke31a8b22016-03-16 17:34:08 -0700287 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700288 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800289 }
290
Pavel Maltsev905968c2017-07-16 19:48:57 -0700291 public static void assertClusterManagerPermission(Context context) {
292 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
293 }
294
Steve Paik66481982015-10-27 15:22:38 -0700295 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700296 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700297 }
298
keunyoung6b197692015-11-16 13:54:38 -0800299 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700300 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700301 }
302
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800303 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700304 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
305 }
306
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700307 public static void assertVendorExtensionPermission(Context context) {
308 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
309 }
310
Enrico Granata3c7a6662017-02-23 18:07:59 -0800311 public static void assertAnyDiagnosticPermission(Context context) {
312 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700313 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800314 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
315 }
316
317 @FutureFeature
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800318 public static void assertVmsPublisherPermission(Context context) {
319 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
320 }
321
322 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -0800323 public static void assertVmsSubscriberPermission(Context context) {
324 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
325 }
326
Steve Paik461ecc62016-06-08 15:28:32 -0700327 public static void assertPermission(Context context, String permission) {
328 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
329 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800330 }
331 }
332
Enrico Granata3c7a6662017-02-23 18:07:59 -0800333 public static void assertAnyPermission(Context context, String... permissions) {
334 for (String permission : permissions) {
335 if (context.checkCallingOrSelfPermission(permission) ==
336 PackageManager.PERMISSION_GRANTED) {
337 return;
338 }
339 }
340 throw new SecurityException("requires any of " + Arrays.toString(permissions));
341 }
342
keunyoungcc449f72015-08-12 10:46:27 -0700343 void dump(PrintWriter writer) {
Keun-young Parkbae6e252017-01-25 12:30:15 -0800344 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
345 //TODO dump all feature flags by reflection
keunyounga3b28d82015-08-25 13:05:15 -0700346 writer.println("*Dump all services*");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800347 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700348 service.dump(writer);
349 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700350 if (mCarTestService != null) {
351 mCarTestService.dump(writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700352 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700353 writer.println("*Dump Vehicle HAL*");
354 mHal.dump(writer);
keunyoungcc449f72015-08-12 10:46:27 -0700355 }
Yao Chene33f07e2016-07-26 12:02:51 -0700356
357 void execShellCmd(String[] args, PrintWriter writer) {
358 new CarShellCommand().exec(args, writer);
359 }
360
Pavel Maltsevabd47232017-10-10 16:54:57 -0700361 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700362 private static void traceBegin(String name) {
363 Slog.i(TAG, name);
364 mBootTiming.traceBegin(name);
365 }
366
Pavel Maltsevabd47232017-10-10 16:54:57 -0700367 @MainThread
Steve Paikc302c7c2017-08-04 14:01:58 -0700368 private static void traceEnd() {
369 mBootTiming.traceEnd();
370 }
371
Yao Chene33f07e2016-07-26 12:02:51 -0700372 private class CarShellCommand {
373 private static final String COMMAND_HELP = "-h";
374 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800375 private static final String COMMAND_INJECT_EVENT = "inject-event";
376
Yao Chene33f07e2016-07-26 12:02:51 -0700377 private static final String PARAM_DAY_MODE = "day";
378 private static final String PARAM_NIGHT_MODE = "night";
379 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800380 private static final String PARAM_ZONED_BOOLEAN = "zoned-boolean";
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800381 private static final String PARAM_GLOBAL_INT = "global-integer";
Yao Chene33f07e2016-07-26 12:02:51 -0700382
383 private void dumpHelp(PrintWriter pw) {
384 pw.println("Car service commands:");
385 pw.println("\t-h");
386 pw.println("\t Print this help text.");
387 pw.println("\tday-night-mode [day|night|sensor]");
388 pw.println("\t Force into day/night mode or restore to auto.");
Enrico Granatacf53fd72017-09-28 10:45:44 -0700389 pw.println("\tday-night-mode [day|night|sensor]");
390 pw.println("\t Force into day/night mode or restore to auto.");
Yao Chene33f07e2016-07-26 12:02:51 -0700391 }
392
393 public void exec(String[] args, PrintWriter writer) {
394 String arg = args[0];
395 switch (arg) {
396 case COMMAND_HELP:
397 dumpHelp(writer);
398 break;
399 case COMMAND_DAY_NIGHT_MODE:
400 String value = args.length < 1 ? "" : args[1];
401 forceDayNightMode(value, writer);
402 break;
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800403 case COMMAND_INJECT_EVENT:
404 String eventType;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800405 if (args.length > 1) {
406 eventType = args[1].toLowerCase();
407 switch (eventType) {
408 case PARAM_ZONED_BOOLEAN:
409 if (args.length < 5) {
410 writer.println("Incorrect number of arguments.");
411 dumpHelp(writer);
412 break;
413 }
414 inject_zoned_boolean_event(args[2], args[3], args[4], writer);
415 break;
416
417 case PARAM_GLOBAL_INT:
418 if (args.length < 4) {
419 writer.println("Incorrect number of Arguments");
420 dumpHelp(writer);
421 break;
422 }
423 inject_global_integer_event(args[2], args[3], writer);
424 break;
425
426 default:
427 writer.println("Unsupported event type");
428 dumpHelp(writer);
429 break;
430 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800431 }
432 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700433 default:
434 writer.println("Unknown command.");
435 dumpHelp(writer);
436 }
437 }
438
439 private void forceDayNightMode(String arg, PrintWriter writer) {
440 int mode;
441 switch (arg) {
442 case PARAM_DAY_MODE:
443 mode = CarNightService.FORCED_DAY_MODE;
444 break;
445 case PARAM_NIGHT_MODE:
446 mode = CarNightService.FORCED_NIGHT_MODE;
447 break;
448 case PARAM_SENSOR_MODE:
449 mode = CarNightService.FORCED_SENSOR_MODE;
450 break;
451 default:
452 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
453 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
454 return;
455 }
456 int current = mCarNightService.forceDayNightMode(mode);
457 String currentMode = null;
458 switch (current) {
459 case UiModeManager.MODE_NIGHT_AUTO:
460 currentMode = PARAM_SENSOR_MODE;
461 break;
462 case UiModeManager.MODE_NIGHT_YES:
463 currentMode = PARAM_NIGHT_MODE;
464 break;
465 case UiModeManager.MODE_NIGHT_NO:
466 currentMode = PARAM_DAY_MODE;
467 break;
468 }
469 writer.println("DayNightMode changed to: " + currentMode);
470 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800471
472 /**
473 * Inject a fake boolean HAL event to help testing.
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800474 *
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800475 * @param property - Vehicle Property
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800476 * @param value - boolean value for the property
477 * @param writer - Printwriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800478 */
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800479 private void inject_zoned_boolean_event(String property, String zone, String value,
480 PrintWriter writer) {
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800481 Log.d(CarLog.TAG_SERVICE, "Injecting Boolean event");
482 boolean event;
483 int propId;
484 int zoneId;
485 if (value.equalsIgnoreCase("true")) {
486 event = true;
487 } else {
488 event = false;
489 }
490 try {
491 propId = Integer.decode(property);
492 zoneId = Integer.decode(zone);
493 } catch (NumberFormatException e) {
494 writer.println("Invalid property Id or Zone Id. Prefix hex values with 0x");
495 return;
496 }
497 mHal.injectBooleanEvent(propId, zoneId, event);
498 }
499
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800500 /**
501 * Inject a fake Integer HAL event to help testing.
502 *
503 * @param property - Vehicle Property
504 * @param value - Integer value to inject
505 * @param writer - PrintWriter
506 */
507 private void inject_global_integer_event(String property, String value,
508 PrintWriter writer) {
509 Log.d(CarLog.TAG_SERVICE, "Injecting integer event");
510 int propId;
511 int eventValue;
512 try {
513 propId = Integer.decode(property);
514 eventValue = Integer.decode(value);
515 } catch (NumberFormatException e) {
516 writer.println("Invalid property Id or event value. Prefix hex values with 0x");
517 return;
518 }
519 mHal.injectIntegerEvent(propId, eventValue);
520 }
521
Yao Chene33f07e2016-07-26 12:02:51 -0700522 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700523}