blob: 20811486f6e859fbb61fa6f2ea5e20ee33ca5a9b [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;
Pavel Maltsev0477e292016-05-27 12:22:36 -070023import android.car.cluster.renderer.IInstrumentClusterNavigation;
Ying Zheng9fc99402018-09-19 14:23:59 -070024import android.car.userlib.CarUserManagerHelper;
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;
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -080028import android.hardware.automotive.vehicle.V2_0.VehicleArea;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070029import android.os.Binder;
Enrico Granatae8056ca2018-04-03 13:19:52 -070030import android.os.Build;
keunyoungca515072015-07-10 12:21:47 -070031import android.os.IBinder;
Keun-young Parkaab47cd2017-06-05 11:04:50 -070032import android.os.Process;
Steve Paikc302c7c2017-08-04 14:01:58 -070033import android.os.Trace;
keunyoungca515072015-07-10 12:21:47 -070034import android.util.Log;
Steve Paikc302c7c2017-08-04 14:01:58 -070035import android.util.Slog;
Keun-young Park4a79a382017-08-10 18:19:14 -070036import android.util.TimingsTraceLog;
Gregory Clark26fa6012018-03-14 18:38:56 -070037
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080038import com.android.car.cluster.InstrumentClusterService;
Serik Beketayev06a66682018-06-12 16:54:29 -070039import com.android.car.garagemode.GarageModeService;
keunyoungcc449f72015-08-12 10:46:27 -070040import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080041import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080042import com.android.car.pm.CarPackageManagerService;
Enrico Granatab19bc322017-10-12 12:25:06 -070043import com.android.car.systeminterface.SystemInterface;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070044import com.android.car.user.CarUserService;
keunyoungca515072015-07-10 12:21:47 -070045import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000046import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070047
Enrico Granatae8056ca2018-04-03 13:19:52 -070048import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070049import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070050import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080051import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070052import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070053
keunyoungca515072015-07-10 12:21:47 -070054public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070055
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080056 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070057 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
58 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080059
keunyoungca515072015-07-10 12:21:47 -070060 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080061 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070062
Anthony Chen12aec302018-04-25 16:41:48 -070063 private final SystemInterface mSystemInterface;
64
Keun-young Park4727da32016-05-31 10:00:51 -070065 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070066 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080067 private final CarPackageManagerService mCarPackageManagerService;
68 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080069 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080070 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070071 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080072 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070073 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080074 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070075 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080076 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080077 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080078 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070079 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080080 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070081 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070082 private final CarDiagnosticService mCarDiagnosticService;
83 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -070084 private final CarConfigurationService mCarConfigurationService;
85
Ying Zheng86d1c3e2018-05-11 11:42:48 -070086 private final CarUserManagerHelper mUserManagerHelper;
87 private CarUserService mCarUserService;
Antonio Cortese4619c72017-02-02 07:53:27 -080088 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080089 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070090
Pavel Maltsev0d07c762016-11-03 16:40:15 -070091 private final CarServiceBase[] mAllServices;
92
Steve Paikc302c7c2017-08-04 14:01:58 -070093 private static final String TAG = "ICarImpl";
94 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -070095
96 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -070097
keunyoung1ab8e182015-09-24 09:25:22 -070098 /** Test only service. Populate it only when necessary. */
99 @GuardedBy("this")
100 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700101
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700102 @GuardedBy("this")
103 private ICarServiceHelper mICarServiceHelper;
104
Enrico Granatae8056ca2018-04-03 13:19:52 -0700105 private final String mVehicleInterfaceName;
106
Pavel Maltsevec83b632017-01-05 15:10:55 -0800107 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700108 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700109 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800110 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700111 mHal = new VehicleHal(vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700112 mVehicleInterfaceName = vehicleInterfaceName;
Keun-young Park4727da32016-05-31 10:00:51 -0700113 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800114 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
115 systemInterface);
Steve Paik9ec53d72018-04-27 13:28:31 -0700116 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
117 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800118 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Steve Paik9ec53d72018-04-27 13:28:31 -0700119 mCarDrivingStateService, mCarPropertyService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800120 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
121 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700122 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700123 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800124 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700125 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700126 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800127 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700128 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700129 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700130 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700131 mSystemStateControllerService = new SystemStateControllerService(
132 serviceContext, mCarAudioService, this);
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700133 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700134 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarPropertyService,
135 mPerUserCarServiceHelper, mCarUXRestrictionsService);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700136 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
137 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700138 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700139 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
140 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700141 mCarConfigurationService =
142 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700143 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Serik Beketayev68920542018-09-06 13:29:58 -0700144 mCarLocationService = new CarLocationService(
145 mContext, mCarPropertyService, mUserManagerHelper);
keunyounga74b9ca2015-10-21 13:33:58 -0700146
keunyounga3b28d82015-08-25 13:05:15 -0700147 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700148 List<CarServiceBase> allServices = new ArrayList<>();
149 allServices.add(mSystemActivityMonitoringService);
150 allServices.add(mCarPowerManagementService);
151 allServices.add(mCarPropertyService);
152 allServices.add(mCarDrivingStateService);
153 allServices.add(mCarUXRestrictionsService);
154 allServices.add(mCarPackageManagerService);
155 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700156 allServices.add(mGarageModeService);
157 allServices.add(mAppFocusService);
158 allServices.add(mCarAudioService);
159 allServices.add(mCarNightService);
160 allServices.add(mInstrumentClusterService);
161 allServices.add(mCarProjectionService);
162 allServices.add(mSystemStateControllerService);
163 allServices.add(mCarBluetoothService);
164 allServices.add(mCarDiagnosticService);
165 allServices.add(mPerUserCarServiceHelper);
166 allServices.add(mCarStorageMonitoringService);
167 allServices.add(mCarConfigurationService);
168 allServices.add(mVmsSubscriberService);
169 allServices.add(mVmsPublisherService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700170 if (mUserManagerHelper.isHeadlessSystemUser()) {
kevinjm55822c42018-08-15 11:26:00 -0700171 allServices.add(new CarUserService(serviceContext, mUserManagerHelper));
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700172 }
Gregory Clarka63ba022018-06-07 16:42:12 -0700173 allServices.add(mCarLocationService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700174 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700175 }
176
Pavel Maltsevabd47232017-10-10 16:54:57 -0700177 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700178 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700179 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700180 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700181 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700182 traceEnd();
183 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800184 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700185 service.init();
186 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700187 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700188 }
189
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700190 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700191 // release done in opposite order from init
192 for (int i = mAllServices.length - 1; i >= 0; i--) {
193 mAllServices[i].release();
194 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700195 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700196 }
197
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700198 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800199 mHal.vehicleHalReconnected(vehicle);
200 for (CarServiceBase service : mAllServices) {
201 service.vehicleHalReconnected();
202 }
203 }
204
keunyoungca515072015-07-10 12:21:47 -0700205 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700206 public void setCarServiceHelper(IBinder helper) {
207 int uid = Binder.getCallingUid();
208 if (uid != Process.SYSTEM_UID) {
209 throw new SecurityException("Only allowed from system");
210 }
211 synchronized (this) {
212 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800213 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700214 }
215 }
216
217 @Override
keunyoungca515072015-07-10 12:21:47 -0700218 public IBinder getCarService(String serviceName) {
219 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800220 case Car.AUDIO_SERVICE:
221 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700222 case Car.APP_FOCUS_SERVICE:
223 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800224 case Car.PACKAGE_SERVICE:
225 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800226 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700227 assertAnyDiagnosticPermission(mContext);
228 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800229 case Car.POWER_SERVICE:
230 assertPowerPermission(mContext);
231 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700232 case Car.CABIN_SERVICE:
233 case Car.HVAC_SERVICE:
234 case Car.INFO_SERVICE:
235 case Car.PROPERTY_SERVICE:
236 case Car.SENSOR_SERVICE:
237 case Car.VENDOR_EXTENSION_SERVICE:
238 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800239 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700240 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700241 IInstrumentClusterNavigation navService =
242 mInstrumentClusterService.getNavigationService();
243 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700244 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
245 assertClusterManagerPermission(mContext);
246 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800247 case Car.PROJECTION_SERVICE:
248 assertProjectionPermission(mContext);
249 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800250 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700251 assertVmsSubscriberPermission(mContext);
252 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800253 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700254 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700255 synchronized (this) {
256 if (mCarTestService == null) {
257 mCarTestService = new CarTestService(mContext, this);
258 }
259 return mCarTestService;
260 }
261 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700262 case Car.BLUETOOTH_SERVICE:
263 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700264 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700265 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700266 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800267 case Car.CAR_DRIVING_STATE_SERVICE:
268 assertDrivingStatePermission(mContext);
269 return mCarDrivingStateService;
270 case Car.CAR_UX_RESTRICTION_SERVICE:
271 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700272 case Car.CAR_CONFIGURATION_SERVICE:
273 return mCarConfigurationService;
keunyoungca515072015-07-10 12:21:47 -0700274 default:
275 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
276 return null;
277 }
278 }
279
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800280 @Override
281 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700282 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800283 }
284
Keun-young Parka28d7b22016-02-29 16:54:29 -0800285 public CarServiceBase getCarInternalService(String serviceName) {
286 switch (serviceName) {
287 case INTERNAL_INPUT_SERVICE:
288 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700289 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
290 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800291 default:
292 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
293 serviceName);
294 return null;
295 }
296 }
297
keunyoung1ab8e182015-09-24 09:25:22 -0700298 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700299 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700300 }
301
Keun-young Parke31a8b22016-03-16 17:34:08 -0700302 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700303 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800304 }
305
Pavel Maltsev905968c2017-07-16 19:48:57 -0700306 public static void assertClusterManagerPermission(Context context) {
307 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
308 }
309
Steve Paik388d7772018-02-12 10:54:51 -0800310 public static void assertPowerPermission(Context context) {
311 assertPermission(context, Car.PERMISSION_CAR_POWER);
312 }
313
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800314 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700315 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
316 }
317
Enrico Granata3c7a6662017-02-23 18:07:59 -0800318 public static void assertAnyDiagnosticPermission(Context context) {
319 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700320 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800321 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
322 }
323
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800324 public static void assertDrivingStatePermission(Context context) {
325 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
326 }
327
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800328 public static void assertVmsPublisherPermission(Context context) {
329 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
330 }
331
Antonio Cortese4619c72017-02-02 07:53:27 -0800332 public static void assertVmsSubscriberPermission(Context context) {
333 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
334 }
335
Steve Paik461ecc62016-06-08 15:28:32 -0700336 public static void assertPermission(Context context, String permission) {
337 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
338 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800339 }
340 }
341
Steve Paik9ec53d72018-04-27 13:28:31 -0700342 /**
343 * Checks to see if the caller has a permission.
344 * @param context
345 * @param permission
346 *
347 * @return boolean TRUE if caller has the permission.
348 */
349 public static boolean hasPermission(Context context, String permission) {
350 return context.checkCallingOrSelfPermission(permission)
351 == PackageManager.PERMISSION_GRANTED;
352 }
353
Enrico Granata3c7a6662017-02-23 18:07:59 -0800354 public static void assertAnyPermission(Context context, String... permissions) {
355 for (String permission : permissions) {
356 if (context.checkCallingOrSelfPermission(permission) ==
357 PackageManager.PERMISSION_GRANTED) {
358 return;
359 }
360 }
361 throw new SecurityException("requires any of " + Arrays.toString(permissions));
362 }
363
Enrico Granatae8056ca2018-04-03 13:19:52 -0700364 @Override
365 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
366 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700367 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700368 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700369 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
370 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700371 return;
keunyounga3b28d82015-08-25 13:05:15 -0700372 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700373 if (args == null || args.length == 0) {
374 writer.println("*dump car service*");
375
376 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
377 writer.println("*Dump all services*");
378 for (CarServiceBase service : mAllServices) {
379 dumpService(service, writer);
380 }
381 if (mCarTestService != null) {
382 dumpService(mCarTestService, writer);
383 }
384 writer.println("*Dump Vehicle HAL*");
385 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
386 try {
387 // TODO dump all feature flags by creating a dumpable interface
388 mHal.dump(writer);
389 } catch (Exception e) {
390 writer.println("Failed dumping: " + mHal.getClass().getName());
391 e.printStackTrace(writer);
392 }
393 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
394 execShellCmd(args, writer);
395 } else {
396 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800397 }
398 }
399
400 private void dumpService(CarServiceBase service, PrintWriter writer) {
401 try {
402 service.dump(writer);
403 } catch (Exception e) {
404 writer.println("Failed dumping: " + service.getClass().getName());
405 e.printStackTrace(writer);
406 }
keunyoungcc449f72015-08-12 10:46:27 -0700407 }
Yao Chene33f07e2016-07-26 12:02:51 -0700408
409 void execShellCmd(String[] args, PrintWriter writer) {
410 new CarShellCommand().exec(args, writer);
411 }
412
Pavel Maltsevabd47232017-10-10 16:54:57 -0700413 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700414 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700415 Slog.i(TAG, name);
416 mBootTiming.traceBegin(name);
417 }
418
Pavel Maltsevabd47232017-10-10 16:54:57 -0700419 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700420 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700421 mBootTiming.traceEnd();
422 }
423
Yao Chene33f07e2016-07-26 12:02:51 -0700424 private class CarShellCommand {
425 private static final String COMMAND_HELP = "-h";
426 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800427 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800428 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800429 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800430 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800431
Yao Chene33f07e2016-07-26 12:02:51 -0700432 private static final String PARAM_DAY_MODE = "day";
433 private static final String PARAM_NIGHT_MODE = "night";
434 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800435 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800436 private static final String PARAM_ON_MODE = "on";
437 private static final String PARAM_OFF_MODE = "off";
438 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800439
Yao Chene33f07e2016-07-26 12:02:51 -0700440
441 private void dumpHelp(PrintWriter pw) {
442 pw.println("Car service commands:");
443 pw.println("\t-h");
444 pw.println("\t Print this help text.");
445 pw.println("\tday-night-mode [day|night|sensor]");
446 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800447 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
448 pw.println("\t Inject a vehicle property for testing");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800449 pw.println("\tdisable-uxr true|false");
450 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800451 pw.println("\tgarage-mode [on|off|query]");
452 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800453 pw.println("\tget-do-activities pkgname");
454 pw.println("\t Get Distraction Optimized activities in given package");
Yao Chene33f07e2016-07-26 12:02:51 -0700455 }
456
457 public void exec(String[] args, PrintWriter writer) {
458 String arg = args[0];
459 switch (arg) {
460 case COMMAND_HELP:
461 dumpHelp(writer);
462 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800463 case COMMAND_DAY_NIGHT_MODE: {
Yao Chene33f07e2016-07-26 12:02:51 -0700464 String value = args.length < 1 ? "" : args[1];
465 forceDayNightMode(value, writer);
466 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800467 }
468 case COMMAND_GARAGE_MODE: {
469 String value = args.length < 1 ? "" : args[1];
470 forceGarageMode(value, writer);
471 break;
472 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800473 case COMMAND_INJECT_VHAL_EVENT:
474 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
475 String data;
476 if (args.length < 3) {
477 writer.println("Incorrect number of arguments.");
478 dumpHelp(writer);
479 break;
480 } else if (args.length > 3) {
481 // Zoned
482 zone = args[2];
483 data = args[3];
484 } else {
485 // Global
486 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800487 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800488 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800489 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800490 case COMMAND_ENABLE_UXR:
491 if (args.length < 2) {
492 writer.println("Incorrect number of arguments");
493 dumpHelp(writer);
494 break;
495 }
496 boolean enableBlocking = Boolean.valueOf(args[1]);
497 if (mCarPackageManagerService != null) {
498 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
499 }
500 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800501 case COMMAND_GET_DO_ACTIVITIES:
502 if (args.length < 2) {
503 writer.println("Incorrect number of arguments");
504 dumpHelp(writer);
505 break;
506 }
507 String pkgName = args[1].toLowerCase();
508 if (mCarPackageManagerService != null) {
509 String[] doActivities =
510 mCarPackageManagerService.getDistractionOptimizedActivities(
511 pkgName);
512 if (doActivities != null) {
513 writer.println("DO Activities for " + pkgName);
514 for (String a : doActivities) {
515 writer.println(a);
516 }
517 } else {
518 writer.println("No DO Activities for " + pkgName);
519 }
520 }
521 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700522 default:
523 writer.println("Unknown command.");
524 dumpHelp(writer);
525 }
526 }
527
528 private void forceDayNightMode(String arg, PrintWriter writer) {
529 int mode;
530 switch (arg) {
531 case PARAM_DAY_MODE:
532 mode = CarNightService.FORCED_DAY_MODE;
533 break;
534 case PARAM_NIGHT_MODE:
535 mode = CarNightService.FORCED_NIGHT_MODE;
536 break;
537 case PARAM_SENSOR_MODE:
538 mode = CarNightService.FORCED_SENSOR_MODE;
539 break;
540 default:
541 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
542 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
543 return;
544 }
545 int current = mCarNightService.forceDayNightMode(mode);
546 String currentMode = null;
547 switch (current) {
548 case UiModeManager.MODE_NIGHT_AUTO:
549 currentMode = PARAM_SENSOR_MODE;
550 break;
551 case UiModeManager.MODE_NIGHT_YES:
552 currentMode = PARAM_NIGHT_MODE;
553 break;
554 case UiModeManager.MODE_NIGHT_NO:
555 currentMode = PARAM_DAY_MODE;
556 break;
557 }
558 writer.println("DayNightMode changed to: " + currentMode);
559 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800560
Bryan Eylera32a7c12018-02-27 15:40:00 -0800561 private void forceGarageMode(String arg, PrintWriter writer) {
562 switch (arg) {
563 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700564 mGarageModeService.forceStartGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800565 break;
566 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700567 mGarageModeService.stopAndResetGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800568 break;
569 case PARAM_QUERY_MODE:
570 // Nothing to do. Always query at the end anyway.
571 break;
572 default:
573 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
574 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
575 return;
576 }
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700577 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -0800578 }
579
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800580 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800581 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800582 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800583 * @param property the Vehicle property Id as defined in the HAL
584 * @param zone Zone that this event services
585 * @param value Data value of the event
586 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800587 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800588 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800589 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800590 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
591 if (!isPropertyAreaTypeGlobal(property)) {
592 writer.println("Property area type inconsistent with given zone");
593 return;
594 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800595 }
596 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800597 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800598 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800599 writer.println("Invalid property Id zone Id or value" + e);
600 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800601 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800602 }
603
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800604 // Check if the given property is global
605 private boolean isPropertyAreaTypeGlobal(String property) {
606 if (property == null) {
607 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800608 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800609 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800610 }
Yao Chene33f07e2016-07-26 12:02:51 -0700611 }
kevinjm55822c42018-08-15 11:26:00 -0700612}