blob: f58302d3e40461e5947d87190670b3a5da9bfbfe [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
Yao Chene33f07e2016-07-26 12:02:51 -070019import android.app.UiModeManager;
Keun-young Parke54ac272016-02-16 19:02:18 -080020import android.car.Car;
21import android.car.ICar;
Antonio Cortese4619c72017-02-02 07:53:27 -080022import android.car.annotation.FutureFeature;
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;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070027import android.os.Binder;
keunyoungca515072015-07-10 12:21:47 -070028import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070029import android.os.Process;
keunyoungca515072015-07-10 12:21:47 -070030import android.util.Log;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080031import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070032import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080033import com.android.car.internal.FeatureConfiguration;
34import com.android.car.internal.FeatureUtil;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080035import com.android.car.pm.CarPackageManagerService;
keunyoungca515072015-07-10 12:21:47 -070036import com.android.internal.annotations.GuardedBy;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070037import com.android.internal.car.ICarServiceHelper;
keunyounga3b28d82015-08-25 13:05:15 -070038import java.io.PrintWriter;
Antonio Cortese4619c72017-02-02 07:53:27 -080039import java.util.ArrayList;
40import java.util.Arrays;
41import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070042
keunyoungca515072015-07-10 12:21:47 -070043public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070044
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080045 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070046 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
47 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080048
keunyoungca515072015-07-10 12:21:47 -070049 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080050 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070051
Keun-young Park4727da32016-05-31 10:00:51 -070052 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070053 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080054 private final CarPackageManagerService mCarPackageManagerService;
55 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070056 private final CarSensorService mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070057 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070058 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080059 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070060 private final CarCabinService mCarCabinService;
Steve Paik66481982015-10-27 15:22:38 -070061 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070062 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080063 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070064 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080065 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080066 private final InstrumentClusterService mInstrumentClusterService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070067 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070068 private final CarVendorExtensionService mCarVendorExtensionService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080069 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070070 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata5c56d2a2017-02-07 15:38:12 -080071 private CarDiagnosticService mCarDiagnosticService;
72 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -080073 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080074 @FutureFeature
75 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070076
Pavel Maltsev0d07c762016-11-03 16:40:15 -070077 private final CarServiceBase[] mAllServices;
78
keunyoung1ab8e182015-09-24 09:25:22 -070079 /** Test only service. Populate it only when necessary. */
80 @GuardedBy("this")
81 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070082
Keun-young Parkaab47cd2017-06-05 11:04:50 -070083 @GuardedBy("this")
84 private ICarServiceHelper mICarServiceHelper;
85
Pavel Maltsevec83b632017-01-05 15:10:55 -080086 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
87 CanBusErrorNotifier errorNotifier) {
keunyoungca515072015-07-10 12:21:47 -070088 mContext = serviceContext;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070089 mHal = new VehicleHal(vehicle);
Keun-young Park4727da32016-05-31 10:00:51 -070090 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070091 mCarPowerManagementService = new CarPowerManagementService(
92 mHal.getPowerHal(), systemInterface);
93 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Keun-young Park4727da32016-05-31 10:00:51 -070094 mCarPackageManagerService = new CarPackageManagerService(serviceContext, mCarSensorService,
95 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070096 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080097 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -080098 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070099 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700100 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700101 mCarAudioService = new CarAudioService(serviceContext, mHal.getAudioHal(),
Pavel Maltsevec83b632017-01-05 15:10:55 -0800102 mCarInputService, errorNotifier);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700103 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
104 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
105 mCarRadioService = new CarRadioService(serviceContext, mHal.getRadioHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700106 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700107 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700108 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700109 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700110 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700111 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
112 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700113 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800114 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700115 mCarSensorService, mPerUserCarServiceHelper);
Antonio Cortese4619c72017-02-02 07:53:27 -0800116 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
117 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800118 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Antonio Cortese4619c72017-02-02 07:53:27 -0800119 }
Enrico Granatab3634e22017-05-05 18:02:31 -0700120 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
keunyounga74b9ca2015-10-21 13:33:58 -0700121
keunyounga3b28d82015-08-25 13:05:15 -0700122 // Be careful with order. Service depending on other service should be inited later.
Antonio Cortese4619c72017-02-02 07:53:27 -0800123 List<CarServiceBase> allServices = new ArrayList<>(Arrays.asList(
Keun-young Park4727da32016-05-31 10:00:51 -0700124 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700125 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700126 mCarSensorService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800127 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800128 mCarInputService,
129 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700130 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700131 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700132 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700133 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700134 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800135 mCarRadioService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800136 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800137 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800138 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700139 mSystemStateControllerService,
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800140 mCarVendorExtensionService,
Enrico Granatab3634e22017-05-05 18:02:31 -0700141 mCarBluetoothService,
Ram Periathiruvadia048c0a2017-05-09 07:35:03 -0700142 mCarDiagnosticService,
143 mPerUserCarServiceHelper
Antonio Cortese4619c72017-02-02 07:53:27 -0800144 ));
145 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
146 allServices.add(mVmsSubscriberService);
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800147 allServices.add(mVmsPublisherService);
Antonio Cortese4619c72017-02-02 07:53:27 -0800148 }
149 mAllServices = allServices.toArray(new CarServiceBase[0]);
keunyoungca515072015-07-10 12:21:47 -0700150 }
151
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700152 public void init() {
153 mHal.init();
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800154 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700155 service.init();
156 }
keunyoungca515072015-07-10 12:21:47 -0700157 }
158
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700159 public void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700160 // release done in opposite order from init
161 for (int i = mAllServices.length - 1; i >= 0; i--) {
162 mAllServices[i].release();
163 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700164 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700165 }
166
Pavel Maltsevec83b632017-01-05 15:10:55 -0800167 public void vehicleHalReconnected(IVehicle vehicle) {
168 mHal.vehicleHalReconnected(vehicle);
169 for (CarServiceBase service : mAllServices) {
170 service.vehicleHalReconnected();
171 }
172 }
173
keunyoungca515072015-07-10 12:21:47 -0700174 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700175 public void setCarServiceHelper(IBinder helper) {
176 int uid = Binder.getCallingUid();
177 if (uid != Process.SYSTEM_UID) {
178 throw new SecurityException("Only allowed from system");
179 }
180 synchronized (this) {
181 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
182 }
183 }
184
185 @Override
keunyoungca515072015-07-10 12:21:47 -0700186 public IBinder getCarService(String serviceName) {
187 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800188 case Car.AUDIO_SERVICE:
189 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700190 case Car.SENSOR_SERVICE:
191 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700192 case Car.INFO_SERVICE:
193 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700194 case Car.APP_FOCUS_SERVICE:
195 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800196 case Car.PACKAGE_SERVICE:
197 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700198 case Car.CABIN_SERVICE:
199 assertCabinPermission(mContext);
200 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800201 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700202 assertAnyDiagnosticPermission(mContext);
203 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800204 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700205 assertHvacPermission(mContext);
206 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800207 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800208 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700209 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800210 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700211 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700212 IInstrumentClusterNavigation navService =
213 mInstrumentClusterService.getNavigationService();
214 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700215 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
216 assertClusterManagerPermission(mContext);
217 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800218 case Car.PROJECTION_SERVICE:
219 assertProjectionPermission(mContext);
220 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700221 case Car.VENDOR_EXTENSION_SERVICE:
222 assertVendorExtensionPermission(mContext);
223 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800224 case Car.VMS_SUBSCRIBER_SERVICE:
225 FeatureUtil.assertFeature(FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE);
226 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
227 assertVmsSubscriberPermission(mContext);
228 return mVmsSubscriberService;
229 }
Keun-young Parke54ac272016-02-16 19:02:18 -0800230 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700231 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700232 synchronized (this) {
233 if (mCarTestService == null) {
234 mCarTestService = new CarTestService(mContext, this);
235 }
236 return mCarTestService;
237 }
238 }
keunyoungca515072015-07-10 12:21:47 -0700239 default:
240 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
241 return null;
242 }
243 }
244
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800245 @Override
246 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700247 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800248 }
249
Keun-young Parka28d7b22016-02-29 16:54:29 -0800250 public CarServiceBase getCarInternalService(String serviceName) {
251 switch (serviceName) {
252 case INTERNAL_INPUT_SERVICE:
253 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700254 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
255 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800256 default:
257 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
258 serviceName);
259 return null;
260 }
261 }
262
keunyoung1ab8e182015-09-24 09:25:22 -0700263 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700264 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700265 }
266
Steve Paik43c04a72016-07-08 19:12:09 -0700267 public static void assertCabinPermission(Context context) {
268 assertPermission(context, Car.PERMISSION_CAR_CABIN);
269 }
270
Keun-young Parke31a8b22016-03-16 17:34:08 -0700271 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700272 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800273 }
274
Pavel Maltsev905968c2017-07-16 19:48:57 -0700275 public static void assertClusterManagerPermission(Context context) {
276 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
277 }
278
Steve Paik66481982015-10-27 15:22:38 -0700279 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700280 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700281 }
282
keunyoung6b197692015-11-16 13:54:38 -0800283 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700284 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700285 }
286
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800287 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700288 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
289 }
290
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700291 public static void assertVendorExtensionPermission(Context context) {
292 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
293 }
294
Enrico Granata3c7a6662017-02-23 18:07:59 -0800295 public static void assertAnyDiagnosticPermission(Context context) {
296 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700297 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800298 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
299 }
300
301 @FutureFeature
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800302 public static void assertVmsPublisherPermission(Context context) {
303 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
304 }
305
306 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -0800307 public static void assertVmsSubscriberPermission(Context context) {
308 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
309 }
310
Steve Paik461ecc62016-06-08 15:28:32 -0700311 public static void assertPermission(Context context, String permission) {
312 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
313 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800314 }
315 }
316
Enrico Granata3c7a6662017-02-23 18:07:59 -0800317 public static void assertAnyPermission(Context context, String... permissions) {
318 for (String permission : permissions) {
319 if (context.checkCallingOrSelfPermission(permission) ==
320 PackageManager.PERMISSION_GRANTED) {
321 return;
322 }
323 }
324 throw new SecurityException("requires any of " + Arrays.toString(permissions));
325 }
326
keunyoungcc449f72015-08-12 10:46:27 -0700327 void dump(PrintWriter writer) {
Keun-young Parkbae6e252017-01-25 12:30:15 -0800328 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
329 //TODO dump all feature flags by reflection
keunyounga3b28d82015-08-25 13:05:15 -0700330 writer.println("*Dump all services*");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800331 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700332 service.dump(writer);
333 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700334 if (mCarTestService != null) {
335 mCarTestService.dump(writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700336 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700337 writer.println("*Dump Vehicle HAL*");
338 mHal.dump(writer);
keunyoungcc449f72015-08-12 10:46:27 -0700339 }
Yao Chene33f07e2016-07-26 12:02:51 -0700340
341 void execShellCmd(String[] args, PrintWriter writer) {
342 new CarShellCommand().exec(args, writer);
343 }
344
345 private class CarShellCommand {
346 private static final String COMMAND_HELP = "-h";
347 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800348 private static final String COMMAND_INJECT_EVENT = "inject-event";
349
Yao Chene33f07e2016-07-26 12:02:51 -0700350 private static final String PARAM_DAY_MODE = "day";
351 private static final String PARAM_NIGHT_MODE = "night";
352 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800353 private static final String PARAM_ZONED_BOOLEAN = "zoned-boolean";
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800354 private static final String PARAM_GLOBAL_INT = "global-integer";
Yao Chene33f07e2016-07-26 12:02:51 -0700355
356 private void dumpHelp(PrintWriter pw) {
357 pw.println("Car service commands:");
358 pw.println("\t-h");
359 pw.println("\t Print this help text.");
360 pw.println("\tday-night-mode [day|night|sensor]");
361 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800362 pw.println("\tinject-event zoned-boolean propertyType zone [true|false]");
363 pw.println("\t Inject a Boolean HAL Event. ");
Yao Chene33f07e2016-07-26 12:02:51 -0700364 }
365
366 public void exec(String[] args, PrintWriter writer) {
367 String arg = args[0];
368 switch (arg) {
369 case COMMAND_HELP:
370 dumpHelp(writer);
371 break;
372 case COMMAND_DAY_NIGHT_MODE:
373 String value = args.length < 1 ? "" : args[1];
374 forceDayNightMode(value, writer);
375 break;
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800376 case COMMAND_INJECT_EVENT:
377 String eventType;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800378 if (args.length > 1) {
379 eventType = args[1].toLowerCase();
380 switch (eventType) {
381 case PARAM_ZONED_BOOLEAN:
382 if (args.length < 5) {
383 writer.println("Incorrect number of arguments.");
384 dumpHelp(writer);
385 break;
386 }
387 inject_zoned_boolean_event(args[2], args[3], args[4], writer);
388 break;
389
390 case PARAM_GLOBAL_INT:
391 if (args.length < 4) {
392 writer.println("Incorrect number of Arguments");
393 dumpHelp(writer);
394 break;
395 }
396 inject_global_integer_event(args[2], args[3], writer);
397 break;
398
399 default:
400 writer.println("Unsupported event type");
401 dumpHelp(writer);
402 break;
403 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800404 }
405 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700406 default:
407 writer.println("Unknown command.");
408 dumpHelp(writer);
409 }
410 }
411
412 private void forceDayNightMode(String arg, PrintWriter writer) {
413 int mode;
414 switch (arg) {
415 case PARAM_DAY_MODE:
416 mode = CarNightService.FORCED_DAY_MODE;
417 break;
418 case PARAM_NIGHT_MODE:
419 mode = CarNightService.FORCED_NIGHT_MODE;
420 break;
421 case PARAM_SENSOR_MODE:
422 mode = CarNightService.FORCED_SENSOR_MODE;
423 break;
424 default:
425 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
426 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
427 return;
428 }
429 int current = mCarNightService.forceDayNightMode(mode);
430 String currentMode = null;
431 switch (current) {
432 case UiModeManager.MODE_NIGHT_AUTO:
433 currentMode = PARAM_SENSOR_MODE;
434 break;
435 case UiModeManager.MODE_NIGHT_YES:
436 currentMode = PARAM_NIGHT_MODE;
437 break;
438 case UiModeManager.MODE_NIGHT_NO:
439 currentMode = PARAM_DAY_MODE;
440 break;
441 }
442 writer.println("DayNightMode changed to: " + currentMode);
443 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800444
445 /**
446 * Inject a fake boolean HAL event to help testing.
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800447 *
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800448 * @param property - Vehicle Property
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800449 * @param value - boolean value for the property
450 * @param writer - Printwriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800451 */
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800452 private void inject_zoned_boolean_event(String property, String zone, String value,
453 PrintWriter writer) {
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800454 Log.d(CarLog.TAG_SERVICE, "Injecting Boolean event");
455 boolean event;
456 int propId;
457 int zoneId;
458 if (value.equalsIgnoreCase("true")) {
459 event = true;
460 } else {
461 event = false;
462 }
463 try {
464 propId = Integer.decode(property);
465 zoneId = Integer.decode(zone);
466 } catch (NumberFormatException e) {
467 writer.println("Invalid property Id or Zone Id. Prefix hex values with 0x");
468 return;
469 }
470 mHal.injectBooleanEvent(propId, zoneId, event);
471 }
472
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800473 /**
474 * Inject a fake Integer HAL event to help testing.
475 *
476 * @param property - Vehicle Property
477 * @param value - Integer value to inject
478 * @param writer - PrintWriter
479 */
480 private void inject_global_integer_event(String property, String value,
481 PrintWriter writer) {
482 Log.d(CarLog.TAG_SERVICE, "Injecting integer event");
483 int propId;
484 int eventValue;
485 try {
486 propId = Integer.decode(property);
487 eventValue = Integer.decode(value);
488 } catch (NumberFormatException e) {
489 writer.println("Invalid property Id or event value. Prefix hex values with 0x");
490 return;
491 }
492 mHal.injectIntegerEvent(propId, eventValue);
493 }
494
Yao Chene33f07e2016-07-26 12:02:51 -0700495 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700496}