blob: a5fe3c5c140c81f9fe9a05cb11814876692c0f85 [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;
keunyoungca515072015-07-10 12:21:47 -070029import android.os.IBinder;
keunyoungca515072015-07-10 12:21:47 -070030import android.util.Log;
31
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080032import com.android.car.cluster.InstrumentClusterService;
keunyoungcc449f72015-08-12 10:46:27 -070033import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080034import com.android.car.internal.FeatureConfiguration;
35import com.android.car.internal.FeatureUtil;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080036import com.android.car.pm.CarPackageManagerService;
keunyoungca515072015-07-10 12:21:47 -070037import com.android.internal.annotations.GuardedBy;
38
keunyounga3b28d82015-08-25 13:05:15 -070039import java.io.PrintWriter;
Antonio Cortese4619c72017-02-02 07:53:27 -080040import java.util.ArrayList;
41import java.util.Arrays;
42import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070043
keunyoungca515072015-07-10 12:21:47 -070044public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070045
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080046 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070047 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
48 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080049
keunyoungca515072015-07-10 12:21:47 -070050 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080051 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070052
Keun-young Park4727da32016-05-31 10:00:51 -070053 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070054 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080055 private final CarPackageManagerService mCarPackageManagerService;
56 private final CarInputService mCarInputService;
keunyoungca515072015-07-10 12:21:47 -070057 private final CarSensorService mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -070058 private final CarInfoService mCarInfoService;
keunyoungd32f4e62015-09-21 11:33:06 -070059 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080060 private final CarProjectionService mCarProjectionService;
Steve Paik43c04a72016-07-08 19:12:09 -070061 private final CarCabinService mCarCabinService;
Steve Paik66481982015-10-27 15:22:38 -070062 private final CarHvacService mCarHvacService;
Sanket Agarwal3cf096a2015-10-13 14:46:31 -070063 private final CarRadioService mCarRadioService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080064 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070065 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080066 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080067 private final InstrumentClusterService mInstrumentClusterService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070068 private final SystemStateControllerService mSystemStateControllerService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -070069 private final CarVendorExtensionService mCarVendorExtensionService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080070 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070071 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Antonio Cortese4619c72017-02-02 07:53:27 -080072 @FutureFeature
Enrico Granata5c56d2a2017-02-07 15:38:12 -080073 private CarDiagnosticService mCarDiagnosticService;
74 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -080075 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080076 @FutureFeature
77 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070078
Pavel Maltsev0d07c762016-11-03 16:40:15 -070079 private final CarServiceBase[] mAllServices;
80
keunyoung1ab8e182015-09-24 09:25:22 -070081 /** Test only service. Populate it only when necessary. */
82 @GuardedBy("this")
83 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -070084
Pavel Maltsevec83b632017-01-05 15:10:55 -080085 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
86 CanBusErrorNotifier errorNotifier) {
keunyoungca515072015-07-10 12:21:47 -070087 mContext = serviceContext;
Pavel Maltsev0d07c762016-11-03 16:40:15 -070088 mHal = new VehicleHal(vehicle);
Keun-young Park4727da32016-05-31 10:00:51 -070089 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070090 mCarPowerManagementService = new CarPowerManagementService(
91 mHal.getPowerHal(), systemInterface);
92 mCarSensorService = new CarSensorService(serviceContext, mHal.getSensorHal());
Keun-young Park4727da32016-05-31 10:00:51 -070093 mCarPackageManagerService = new CarPackageManagerService(serviceContext, mCarSensorService,
94 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070095 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080096 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Yao Chen3a7976d2016-01-20 17:27:08 -080097 mGarageModeService = new GarageModeService(mContext, mCarPowerManagementService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -070098 mCarInfoService = new CarInfoService(serviceContext, mHal.getInfoHal());
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -070099 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700100 mCarAudioService = new CarAudioService(serviceContext, mHal.getAudioHal(),
Pavel Maltsevec83b632017-01-05 15:10:55 -0800101 mCarInputService, errorNotifier);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700102 mCarCabinService = new CarCabinService(serviceContext, mHal.getCabinHal());
103 mCarHvacService = new CarHvacService(serviceContext, mHal.getHvacHal());
104 mCarRadioService = new CarRadioService(serviceContext, mHal.getRadioHal());
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700105 mCarNightService = new CarNightService(serviceContext, mCarSensorService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700106 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700107 mAppFocusService, mCarInputService);
Keun-young Parkd73afae2016-04-08 20:03:32 -0700108 mSystemStateControllerService = new SystemStateControllerService(serviceContext,
Keun-young Park3cb89102016-05-05 13:16:03 -0700109 mCarPowerManagementService, mCarAudioService, this);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700110 mCarVendorExtensionService = new CarVendorExtensionService(serviceContext,
111 mHal.getVendorExtensionHal());
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700112 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800113 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarCabinService,
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700114 mCarSensorService, mPerUserCarServiceHelper);
Antonio Cortese4619c72017-02-02 07:53:27 -0800115 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
116 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800117 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Antonio Cortese4619c72017-02-02 07:53:27 -0800118 }
Enrico Granatab3634e22017-05-05 18:02:31 -0700119 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
keunyounga74b9ca2015-10-21 13:33:58 -0700120
keunyounga3b28d82015-08-25 13:05:15 -0700121 // Be careful with order. Service depending on other service should be inited later.
Antonio Cortese4619c72017-02-02 07:53:27 -0800122 List<CarServiceBase> allServices = new ArrayList<>(Arrays.asList(
Keun-young Park4727da32016-05-31 10:00:51 -0700123 mSystemActivityMonitoringService,
keunyoung4b0212c2015-10-29 17:11:57 -0700124 mCarPowerManagementService,
Keun-young Park4727da32016-05-31 10:00:51 -0700125 mCarSensorService,
Keun-young Park45fdcba2015-12-08 11:38:58 -0800126 mCarPackageManagerService,
Keun-young Parka28d7b22016-02-29 16:54:29 -0800127 mCarInputService,
128 mGarageModeService,
keunyounga3b28d82015-08-25 13:05:15 -0700129 mCarInfoService,
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700130 mAppFocusService,
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700131 mCarAudioService,
Steve Paik43c04a72016-07-08 19:12:09 -0700132 mCarCabinService,
Steve Paik66481982015-10-27 15:22:38 -0700133 mCarHvacService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800134 mCarRadioService,
Joseph Pirozzo317343d2016-01-25 10:22:37 -0800135 mCarNightService,
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800136 mInstrumentClusterService,
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800137 mCarProjectionService,
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700138 mSystemStateControllerService,
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -0800139 mCarVendorExtensionService,
Enrico Granatab3634e22017-05-05 18:02:31 -0700140 mCarBluetoothService,
141 mCarDiagnosticService
Antonio Cortese4619c72017-02-02 07:53:27 -0800142 ));
143 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
144 allServices.add(mVmsSubscriberService);
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800145 allServices.add(mVmsPublisherService);
Antonio Cortese4619c72017-02-02 07:53:27 -0800146 }
147 mAllServices = allServices.toArray(new CarServiceBase[0]);
keunyoungca515072015-07-10 12:21:47 -0700148 }
149
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700150 public void init() {
151 mHal.init();
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800152 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700153 service.init();
154 }
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700155 mPerUserCarServiceHelper.init();
keunyoungca515072015-07-10 12:21:47 -0700156 }
157
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700158 public void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700159 // release done in opposite order from init
160 for (int i = mAllServices.length - 1; i >= 0; i--) {
161 mAllServices[i].release();
162 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700163 mHal.release();
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700164 mPerUserCarServiceHelper.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
keunyoungca515072015-07-10 12:21:47 -0700175 public IBinder getCarService(String serviceName) {
176 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800177 case Car.AUDIO_SERVICE:
178 return mCarAudioService;
keunyoungca515072015-07-10 12:21:47 -0700179 case Car.SENSOR_SERVICE:
180 return mCarSensorService;
keunyounga3b28d82015-08-25 13:05:15 -0700181 case Car.INFO_SERVICE:
182 return mCarInfoService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700183 case Car.APP_FOCUS_SERVICE:
184 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800185 case Car.PACKAGE_SERVICE:
186 return mCarPackageManagerService;
Steve Paik43c04a72016-07-08 19:12:09 -0700187 case Car.CABIN_SERVICE:
188 assertCabinPermission(mContext);
189 return mCarCabinService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800190 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700191 assertAnyDiagnosticPermission(mContext);
192 return mCarDiagnosticService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800193 case Car.HVAC_SERVICE:
Steve Paik66481982015-10-27 15:22:38 -0700194 assertHvacPermission(mContext);
195 return mCarHvacService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800196 case Car.RADIO_SERVICE:
keunyoung6b197692015-11-16 13:54:38 -0800197 assertRadioPermission(mContext);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700198 return mCarRadioService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800199 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700200 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700201 IInstrumentClusterNavigation navService =
202 mInstrumentClusterService.getNavigationService();
203 return navService == null ? null : navService.asBinder();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800204 case Car.PROJECTION_SERVICE:
205 assertProjectionPermission(mContext);
206 return mCarProjectionService;
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700207 case Car.VENDOR_EXTENSION_SERVICE:
208 assertVendorExtensionPermission(mContext);
209 return mCarVendorExtensionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800210 case Car.VMS_SUBSCRIBER_SERVICE:
211 FeatureUtil.assertFeature(FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE);
212 if (FeatureConfiguration.ENABLE_VEHICLE_MAP_SERVICE) {
213 assertVmsSubscriberPermission(mContext);
214 return mVmsSubscriberService;
215 }
Keun-young Parke54ac272016-02-16 19:02:18 -0800216 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700217 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700218 synchronized (this) {
219 if (mCarTestService == null) {
220 mCarTestService = new CarTestService(mContext, this);
221 }
222 return mCarTestService;
223 }
224 }
keunyoungca515072015-07-10 12:21:47 -0700225 default:
226 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
227 return null;
228 }
229 }
230
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800231 @Override
232 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700233 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800234 }
235
Keun-young Parka28d7b22016-02-29 16:54:29 -0800236 public CarServiceBase getCarInternalService(String serviceName) {
237 switch (serviceName) {
238 case INTERNAL_INPUT_SERVICE:
239 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700240 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
241 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800242 default:
243 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
244 serviceName);
245 return null;
246 }
247 }
248
keunyoung1ab8e182015-09-24 09:25:22 -0700249 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700250 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700251 }
252
Steve Paik43c04a72016-07-08 19:12:09 -0700253 public static void assertCabinPermission(Context context) {
254 assertPermission(context, Car.PERMISSION_CAR_CABIN);
255 }
256
Keun-young Parke31a8b22016-03-16 17:34:08 -0700257 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700258 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800259 }
260
Steve Paik66481982015-10-27 15:22:38 -0700261 public static void assertHvacPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700262 assertPermission(context, Car.PERMISSION_CAR_HVAC);
Steve Paik66481982015-10-27 15:22:38 -0700263 }
264
keunyoung6b197692015-11-16 13:54:38 -0800265 private static void assertRadioPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700266 assertPermission(context, Car.PERMISSION_CAR_RADIO);
Sanket Agarwal3cf096a2015-10-13 14:46:31 -0700267 }
268
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800269 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700270 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
271 }
272
Pavel Maltsev634e1ff2016-07-14 15:41:26 -0700273 public static void assertVendorExtensionPermission(Context context) {
274 assertPermission(context, Car.PERMISSION_VENDOR_EXTENSION);
275 }
276
Antonio Cortese4619c72017-02-02 07:53:27 -0800277 @FutureFeature
Enrico Granata3c7a6662017-02-23 18:07:59 -0800278 public static void assertAnyDiagnosticPermission(Context context) {
279 assertAnyPermission(context,
280 Car.PERMISSION_CAR_DIAGNOSTIC_READ,
281 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
282 }
283
284 @FutureFeature
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800285 public static void assertVmsPublisherPermission(Context context) {
286 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
287 }
288
289 @FutureFeature
Antonio Cortese4619c72017-02-02 07:53:27 -0800290 public static void assertVmsSubscriberPermission(Context context) {
291 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
292 }
293
Steve Paik461ecc62016-06-08 15:28:32 -0700294 public static void assertPermission(Context context, String permission) {
295 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
296 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800297 }
298 }
299
Enrico Granata3c7a6662017-02-23 18:07:59 -0800300 public static void assertAnyPermission(Context context, String... permissions) {
301 for (String permission : permissions) {
302 if (context.checkCallingOrSelfPermission(permission) ==
303 PackageManager.PERMISSION_GRANTED) {
304 return;
305 }
306 }
307 throw new SecurityException("requires any of " + Arrays.toString(permissions));
308 }
309
keunyoungcc449f72015-08-12 10:46:27 -0700310 void dump(PrintWriter writer) {
Keun-young Parkbae6e252017-01-25 12:30:15 -0800311 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
312 //TODO dump all feature flags by reflection
keunyounga3b28d82015-08-25 13:05:15 -0700313 writer.println("*Dump all services*");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800314 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700315 service.dump(writer);
316 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700317 if (mCarTestService != null) {
318 mCarTestService.dump(writer);
keunyoung1ab8e182015-09-24 09:25:22 -0700319 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700320 writer.println("*Dump Vehicle HAL*");
321 mHal.dump(writer);
keunyoungcc449f72015-08-12 10:46:27 -0700322 }
Yao Chene33f07e2016-07-26 12:02:51 -0700323
324 void execShellCmd(String[] args, PrintWriter writer) {
325 new CarShellCommand().exec(args, writer);
326 }
327
328 private class CarShellCommand {
329 private static final String COMMAND_HELP = "-h";
330 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800331 private static final String COMMAND_INJECT_EVENT = "inject-event";
332
Yao Chene33f07e2016-07-26 12:02:51 -0700333 private static final String PARAM_DAY_MODE = "day";
334 private static final String PARAM_NIGHT_MODE = "night";
335 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800336 private static final String PARAM_ZONED_BOOLEAN = "zoned-boolean";
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800337 private static final String PARAM_GLOBAL_INT = "global-integer";
Yao Chene33f07e2016-07-26 12:02:51 -0700338
339 private void dumpHelp(PrintWriter pw) {
340 pw.println("Car service commands:");
341 pw.println("\t-h");
342 pw.println("\t Print this help text.");
343 pw.println("\tday-night-mode [day|night|sensor]");
344 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800345 pw.println("\tinject-event zoned-boolean propertyType zone [true|false]");
346 pw.println("\t Inject a Boolean HAL Event. ");
Yao Chene33f07e2016-07-26 12:02:51 -0700347 }
348
349 public void exec(String[] args, PrintWriter writer) {
350 String arg = args[0];
351 switch (arg) {
352 case COMMAND_HELP:
353 dumpHelp(writer);
354 break;
355 case COMMAND_DAY_NIGHT_MODE:
356 String value = args.length < 1 ? "" : args[1];
357 forceDayNightMode(value, writer);
358 break;
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800359 case COMMAND_INJECT_EVENT:
360 String eventType;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800361 if (args.length > 1) {
362 eventType = args[1].toLowerCase();
363 switch (eventType) {
364 case PARAM_ZONED_BOOLEAN:
365 if (args.length < 5) {
366 writer.println("Incorrect number of arguments.");
367 dumpHelp(writer);
368 break;
369 }
370 inject_zoned_boolean_event(args[2], args[3], args[4], writer);
371 break;
372
373 case PARAM_GLOBAL_INT:
374 if (args.length < 4) {
375 writer.println("Incorrect number of Arguments");
376 dumpHelp(writer);
377 break;
378 }
379 inject_global_integer_event(args[2], args[3], writer);
380 break;
381
382 default:
383 writer.println("Unsupported event type");
384 dumpHelp(writer);
385 break;
386 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800387 }
388 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700389 default:
390 writer.println("Unknown command.");
391 dumpHelp(writer);
392 }
393 }
394
395 private void forceDayNightMode(String arg, PrintWriter writer) {
396 int mode;
397 switch (arg) {
398 case PARAM_DAY_MODE:
399 mode = CarNightService.FORCED_DAY_MODE;
400 break;
401 case PARAM_NIGHT_MODE:
402 mode = CarNightService.FORCED_NIGHT_MODE;
403 break;
404 case PARAM_SENSOR_MODE:
405 mode = CarNightService.FORCED_SENSOR_MODE;
406 break;
407 default:
408 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
409 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
410 return;
411 }
412 int current = mCarNightService.forceDayNightMode(mode);
413 String currentMode = null;
414 switch (current) {
415 case UiModeManager.MODE_NIGHT_AUTO:
416 currentMode = PARAM_SENSOR_MODE;
417 break;
418 case UiModeManager.MODE_NIGHT_YES:
419 currentMode = PARAM_NIGHT_MODE;
420 break;
421 case UiModeManager.MODE_NIGHT_NO:
422 currentMode = PARAM_DAY_MODE;
423 break;
424 }
425 writer.println("DayNightMode changed to: " + currentMode);
426 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800427
428 /**
429 * Inject a fake boolean HAL event to help testing.
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800430 *
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800431 * @param property - Vehicle Property
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800432 * @param value - boolean value for the property
433 * @param writer - Printwriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800434 */
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800435 private void inject_zoned_boolean_event(String property, String zone, String value,
436 PrintWriter writer) {
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800437 Log.d(CarLog.TAG_SERVICE, "Injecting Boolean event");
438 boolean event;
439 int propId;
440 int zoneId;
441 if (value.equalsIgnoreCase("true")) {
442 event = true;
443 } else {
444 event = false;
445 }
446 try {
447 propId = Integer.decode(property);
448 zoneId = Integer.decode(zone);
449 } catch (NumberFormatException e) {
450 writer.println("Invalid property Id or Zone Id. Prefix hex values with 0x");
451 return;
452 }
453 mHal.injectBooleanEvent(propId, zoneId, event);
454 }
455
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800456 /**
457 * Inject a fake Integer HAL event to help testing.
458 *
459 * @param property - Vehicle Property
460 * @param value - Integer value to inject
461 * @param writer - PrintWriter
462 */
463 private void inject_global_integer_event(String property, String value,
464 PrintWriter writer) {
465 Log.d(CarLog.TAG_SERVICE, "Injecting integer event");
466 int propId;
467 int eventValue;
468 try {
469 propId = Integer.decode(property);
470 eventValue = Integer.decode(value);
471 } catch (NumberFormatException e) {
472 writer.println("Invalid property Id or event value. Prefix hex values with 0x");
473 return;
474 }
475 mHal.injectIntegerEvent(propId, eventValue);
476 }
477
Yao Chene33f07e2016-07-26 12:02:51 -0700478 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800479}