blob: ec760c998c03e1a553a45766d148ac57169f4901 [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;
Ram Periathiruvadiee28c002017-02-07 21:35:01 -080027import android.hardware.automotive.vehicle.V2_0.VehicleAreaDoor;
28import android.hardware.automotive.vehicle.V2_0.VehicleProperty;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070029import android.os.Binder;
keunyoungca515072015-07-10 12:21:47 -070030import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070031import android.os.Process;
keunyoungca515072015-07-10 12:21:47 -070032import android.util.Log;
33
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080034import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070035import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080036import com.android.car.internal.FeatureConfiguration;
37import com.android.car.internal.FeatureUtil;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080038import com.android.car.pm.CarPackageManagerService;
keunyoungca515072015-07-10 12:21:47 -070039import com.android.internal.annotations.GuardedBy;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070040import com.android.internal.car.ICarServiceHelper;
keunyoungca515072015-07-10 12:21:47 -070041
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;
Antonio Cortese4619c72017-02-02 07:53:27 -080075 @FutureFeature
Enrico Granata5c56d2a2017-02-07 15:38:12 -080076 private CarDiagnosticService mCarDiagnosticService;
77 @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
keunyoung1ab8e182015-09-24 09:25:22 -070084 /** Test only service. Populate it only when necessary. */
85 @GuardedBy("this")
86 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070087
Keun-young Parkaab47cd2017-06-05 11:04:50 -070088 @GuardedBy("this")
89 private ICarServiceHelper mICarServiceHelper;
90
Pavel Maltsevec83b632017-01-05 15:10:55 -080091 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
92 CanBusErrorNotifier errorNotifier) {
keunyoungca515072015-07-10 12:21:47 -070093 mContext = serviceContext;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070094 mHal = new VehicleHal(vehicle);
Keun-young Park4727da32016-05-31 10:00:51 -070095 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070096 mCarPowerManagementService = new CarPowerManagementService(
97 mHal.getPowerHal(), systemInterface);
98 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Keun-young Park4727da32016-05-31 10:00:51 -070099 mCarPackageManagerService = new CarPackageManagerService(serviceContext, mCarSensorService,
100 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700101 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800102 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -0800103 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700104 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700105 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700106 mCarAudioService = new CarAudioService(serviceContext, mHal.getAudioHal(),
Pavel Maltsevec83b632017-01-05 15:10:55 -0800107 mCarInputService, errorNotifier);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700108 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
109 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
110 mCarRadioService = new CarRadioService(serviceContext, mHal.getRadioHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700111 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700112 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700113 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700114 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700115 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700116 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
117 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700118 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800119 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700120 mCarSensorService, mPerUserCarServiceHelper);
Antonio Cortese4619c72017-02-02 07:53:27 -0800121 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
122 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800123 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Antonio Cortese4619c72017-02-02 07:53:27 -0800124 }
Enrico Granatab3634e22017-05-05 18:02:31 -0700125 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
keunyounga74b9ca2015-10-21 13:33:58 -0700126
keunyounga3b28d82015-08-25 13:05:15 -0700127 // Be careful with order. Service depending on other service should be inited later.
Antonio Cortese4619c72017-02-02 07:53:27 -0800128 List<CarServiceBase> allServices = new ArrayList<>(Arrays.asList(
Keun-young Park4727da32016-05-31 10:00:51 -0700129 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700130 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700131 mCarSensorService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800132 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800133 mCarInputService,
134 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700135 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700136 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700137 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700138 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700139 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800140 mCarRadioService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800141 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800142 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800143 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700144 mSystemStateControllerService,
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800145 mCarVendorExtensionService,
Enrico Granatab3634e22017-05-05 18:02:31 -0700146 mCarBluetoothService,
Ram Periathiruvadia048c0a2017-05-09 07:35:03 -0700147 mCarDiagnosticService,
148 mPerUserCarServiceHelper
Antonio Cortese4619c72017-02-02 07:53:27 -0800149 ));
150 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
151 allServices.add(mVmsSubscriberService);
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800152 allServices.add(mVmsPublisherService);
Antonio Cortese4619c72017-02-02 07:53:27 -0800153 }
154 mAllServices = allServices.toArray(new CarServiceBase[0]);
keunyoungca515072015-07-10 12:21:47 -0700155 }
156
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700157 public void init() {
158 mHal.init();
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800159 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700160 service.init();
161 }
keunyoungca515072015-07-10 12:21:47 -0700162 }
163
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700164 public void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700165 // release done in opposite order from init
166 for (int i = mAllServices.length - 1; i >= 0; i--) {
167 mAllServices[i].release();
168 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700169 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700170 }
171
Pavel Maltsevec83b632017-01-05 15:10:55 -0800172 public void vehicleHalReconnected(IVehicle vehicle) {
173 mHal.vehicleHalReconnected(vehicle);
174 for (CarServiceBase service : mAllServices) {
175 service.vehicleHalReconnected();
176 }
177 }
178
keunyoungca515072015-07-10 12:21:47 -0700179 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700180 public void setCarServiceHelper(IBinder helper) {
181 int uid = Binder.getCallingUid();
182 if (uid != Process.SYSTEM_UID) {
183 throw new SecurityException("Only allowed from system");
184 }
185 synchronized (this) {
186 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
187 }
188 }
189
190 @Override
keunyoungca515072015-07-10 12:21:47 -0700191 public IBinder getCarService(String serviceName) {
192 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800193 case Car.AUDIO_SERVICE:
194 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700195 case Car.SENSOR_SERVICE:
196 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700197 case Car.INFO_SERVICE:
198 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700199 case Car.APP_FOCUS_SERVICE:
200 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800201 case Car.PACKAGE_SERVICE:
202 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700203 case Car.CABIN_SERVICE:
204 assertCabinPermission(mContext);
205 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800206 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700207 assertAnyDiagnosticPermission(mContext);
208 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800209 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700210 assertHvacPermission(mContext);
211 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800212 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800213 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700214 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800215 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700216 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700217 IInstrumentClusterNavigation navService =
218 mInstrumentClusterService.getNavigationService();
219 return navService == null ? null : navService.asBinder();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800220 case Car.PROJECTION_SERVICE:
221 assertProjectionPermission(mContext);
222 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700223 case Car.VENDOR_EXTENSION_SERVICE:
224 assertVendorExtensionPermission(mContext);
225 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800226 case Car.VMS_SUBSCRIBER_SERVICE:
227 FeatureUtil.assertFeature(FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE);
228 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
229 assertVmsSubscriberPermission(mContext);
230 return mVmsSubscriberService;
231 }
Keun-young Parke54ac272016-02-16 19:02:18 -0800232 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700233 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700234 synchronized (this) {
235 if (mCarTestService == null) {
236 mCarTestService = new CarTestService(mContext, this);
237 }
238 return mCarTestService;
239 }
240 }
keunyoungca515072015-07-10 12:21:47 -0700241 default:
242 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
243 return null;
244 }
245 }
246
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800247 @Override
248 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700249 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800250 }
251
Keun-young Parka28d7b22016-02-29 16:54:29 -0800252 public CarServiceBase getCarInternalService(String serviceName) {
253 switch (serviceName) {
254 case INTERNAL_INPUT_SERVICE:
255 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700256 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
257 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800258 default:
259 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
260 serviceName);
261 return null;
262 }
263 }
264
keunyoung1ab8e182015-09-24 09:25:22 -0700265 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700266 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700267 }
268
Steve Paik43c04a72016-07-08 19:12:09 -0700269 public static void assertCabinPermission(Context context) {
270 assertPermission(context, Car.PERMISSION_CAR_CABIN);
271 }
272
Keun-young Parke31a8b22016-03-16 17:34:08 -0700273 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700274 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800275 }
276
Steve Paik66481982015-10-27 15:22:38 -0700277 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700278 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700279 }
280
keunyoung6b197692015-11-16 13:54:38 -0800281 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700282 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700283 }
284
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800285 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700286 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
287 }
288
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700289 public static void assertVendorExtensionPermission(Context context) {
290 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
291 }
292
Antonio Cortese4619c72017-02-02 07:53:27 -0800293 @FutureFeature
Enrico Granata3c7a6662017-02-23 18:07:59 -0800294 public static void assertAnyDiagnosticPermission(Context context) {
295 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700296 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800297 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
298 }
299
300 @FutureFeature
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800301 public static void assertVmsPublisherPermission(Context context) {
302 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
303 }
304
305 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -0800306 public static void assertVmsSubscriberPermission(Context context) {
307 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
308 }
309
Steve Paik461ecc62016-06-08 15:28:32 -0700310 public static void assertPermission(Context context, String permission) {
311 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
312 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800313 }
314 }
315
Enrico Granata3c7a6662017-02-23 18:07:59 -0800316 public static void assertAnyPermission(Context context, String... permissions) {
317 for (String permission : permissions) {
318 if (context.checkCallingOrSelfPermission(permission) ==
319 PackageManager.PERMISSION_GRANTED) {
320 return;
321 }
322 }
323 throw new SecurityException("requires any of " + Arrays.toString(permissions));
324 }
325
keunyoungcc449f72015-08-12 10:46:27 -0700326 void dump(PrintWriter writer) {
Keun-young Parkbae6e252017-01-25 12:30:15 -0800327 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
328 //TODO dump all feature flags by reflection
keunyounga3b28d82015-08-25 13:05:15 -0700329 writer.println("*Dump all services*");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800330 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700331 service.dump(writer);
332 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700333 if (mCarTestService != null) {
334 mCarTestService.dump(writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700335 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700336 writer.println("*Dump Vehicle HAL*");
337 mHal.dump(writer);
keunyoungcc449f72015-08-12 10:46:27 -0700338 }
Yao Chene33f07e2016-07-26 12:02:51 -0700339
340 void execShellCmd(String[] args, PrintWriter writer) {
341 new CarShellCommand().exec(args, writer);
342 }
343
344 private class CarShellCommand {
345 private static final String COMMAND_HELP = "-h";
346 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800347 private static final String COMMAND_INJECT_EVENT = "inject-event";
348
Yao Chene33f07e2016-07-26 12:02:51 -0700349 private static final String PARAM_DAY_MODE = "day";
350 private static final String PARAM_NIGHT_MODE = "night";
351 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800352 private static final String PARAM_ZONED_BOOLEAN = "zoned-boolean";
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800353 private static final String PARAM_GLOBAL_INT = "global-integer";
Yao Chene33f07e2016-07-26 12:02:51 -0700354
355 private void dumpHelp(PrintWriter pw) {
356 pw.println("Car service commands:");
357 pw.println("\t-h");
358 pw.println("\t Print this help text.");
359 pw.println("\tday-night-mode [day|night|sensor]");
360 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800361 pw.println("\tinject-event zoned-boolean propertyType zone [true|false]");
362 pw.println("\t Inject a Boolean HAL Event. ");
Yao Chene33f07e2016-07-26 12:02:51 -0700363 }
364
365 public void exec(String[] args, PrintWriter writer) {
366 String arg = args[0];
367 switch (arg) {
368 case COMMAND_HELP:
369 dumpHelp(writer);
370 break;
371 case COMMAND_DAY_NIGHT_MODE:
372 String value = args.length < 1 ? "" : args[1];
373 forceDayNightMode(value, writer);
374 break;
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800375 case COMMAND_INJECT_EVENT:
376 String eventType;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800377 if (args.length > 1) {
378 eventType = args[1].toLowerCase();
379 switch (eventType) {
380 case PARAM_ZONED_BOOLEAN:
381 if (args.length < 5) {
382 writer.println("Incorrect number of arguments.");
383 dumpHelp(writer);
384 break;
385 }
386 inject_zoned_boolean_event(args[2], args[3], args[4], writer);
387 break;
388
389 case PARAM_GLOBAL_INT:
390 if (args.length < 4) {
391 writer.println("Incorrect number of Arguments");
392 dumpHelp(writer);
393 break;
394 }
395 inject_global_integer_event(args[2], args[3], writer);
396 break;
397
398 default:
399 writer.println("Unsupported event type");
400 dumpHelp(writer);
401 break;
402 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800403 }
404 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700405 default:
406 writer.println("Unknown command.");
407 dumpHelp(writer);
408 }
409 }
410
411 private void forceDayNightMode(String arg, PrintWriter writer) {
412 int mode;
413 switch (arg) {
414 case PARAM_DAY_MODE:
415 mode = CarNightService.FORCED_DAY_MODE;
416 break;
417 case PARAM_NIGHT_MODE:
418 mode = CarNightService.FORCED_NIGHT_MODE;
419 break;
420 case PARAM_SENSOR_MODE:
421 mode = CarNightService.FORCED_SENSOR_MODE;
422 break;
423 default:
424 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
425 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
426 return;
427 }
428 int current = mCarNightService.forceDayNightMode(mode);
429 String currentMode = null;
430 switch (current) {
431 case UiModeManager.MODE_NIGHT_AUTO:
432 currentMode = PARAM_SENSOR_MODE;
433 break;
434 case UiModeManager.MODE_NIGHT_YES:
435 currentMode = PARAM_NIGHT_MODE;
436 break;
437 case UiModeManager.MODE_NIGHT_NO:
438 currentMode = PARAM_DAY_MODE;
439 break;
440 }
441 writer.println("DayNightMode changed to: " + currentMode);
442 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800443
444 /**
445 * Inject a fake boolean HAL event to help testing.
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800446 *
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800447 * @param property - Vehicle Property
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800448 * @param value - boolean value for the property
449 * @param writer - Printwriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800450 */
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800451 private void inject_zoned_boolean_event(String property, String zone, String value,
452 PrintWriter writer) {
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800453 Log.d(CarLog.TAG_SERVICE, "Injecting Boolean event");
454 boolean event;
455 int propId;
456 int zoneId;
457 if (value.equalsIgnoreCase("true")) {
458 event = true;
459 } else {
460 event = false;
461 }
462 try {
463 propId = Integer.decode(property);
464 zoneId = Integer.decode(zone);
465 } catch (NumberFormatException e) {
466 writer.println("Invalid property Id or Zone Id. Prefix hex values with 0x");
467 return;
468 }
469 mHal.injectBooleanEvent(propId, zoneId, event);
470 }
471
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800472 /**
473 * Inject a fake Integer HAL event to help testing.
474 *
475 * @param property - Vehicle Property
476 * @param value - Integer value to inject
477 * @param writer - PrintWriter
478 */
479 private void inject_global_integer_event(String property, String value,
480 PrintWriter writer) {
481 Log.d(CarLog.TAG_SERVICE, "Injecting integer event");
482 int propId;
483 int eventValue;
484 try {
485 propId = Integer.decode(property);
486 eventValue = Integer.decode(value);
487 } catch (NumberFormatException e) {
488 writer.println("Invalid property Id or event value. Prefix hex values with 0x");
489 return;
490 }
491 mHal.injectIntegerEvent(propId, eventValue);
492 }
493
Yao Chene33f07e2016-07-26 12:02:51 -0700494 }
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700495}