blob: 21a2f154ec31829b502609dcfbb46cea34a9cdb7 [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
Hongwei Wang38549092018-08-22 12:32:42 -070038import com.android.car.audio.CarAudioService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080039import com.android.car.cluster.InstrumentClusterService;
Serik Beketayev06a66682018-06-12 16:54:29 -070040import com.android.car.garagemode.GarageModeService;
keunyoungcc449f72015-08-12 10:46:27 -070041import com.android.car.hal.VehicleHal;
Antonio Cortesc52d5f92017-02-06 08:47:38 -080042import com.android.car.internal.FeatureConfiguration;
Keun-young Park4aeb4bf2015-12-08 18:31:33 -080043import com.android.car.pm.CarPackageManagerService;
Enrico Granatab19bc322017-10-12 12:25:06 -070044import com.android.car.systeminterface.SystemInterface;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -080045import com.android.car.trust.CarTrustAgentEnrollmentService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070046import com.android.car.user.CarUserService;
Mark Tabrya8fce562019-01-16 16:24:01 -080047import com.android.car.vms.VmsClientManager;
keunyoungca515072015-07-10 12:21:47 -070048import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000049import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070050
Enrico Granatae8056ca2018-04-03 13:19:52 -070051import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070052import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070053import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080054import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070055import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070056
keunyoungca515072015-07-10 12:21:47 -070057public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070058
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080059 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070060 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
61 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080062
keunyoungca515072015-07-10 12:21:47 -070063 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080064 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070065
Anthony Chen12aec302018-04-25 16:41:48 -070066 private final SystemInterface mSystemInterface;
67
Keun-young Park4727da32016-05-31 10:00:51 -070068 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070069 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080070 private final CarPackageManagerService mCarPackageManagerService;
71 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080072 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080073 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070074 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080075 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070076 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080077 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070078 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080079 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080080 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080081 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070082 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080083 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070084 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070085 private final CarDiagnosticService mCarDiagnosticService;
86 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -070087 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -080088 private final CarTrustAgentEnrollmentService mCarTrustAgentEnrollmentService;
Simon Dai527eb552019-02-12 13:06:15 -080089 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070090 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -080091 private final CarUserService mCarUserService;
Mark Tabrya8fce562019-01-16 16:24:01 -080092 private final VmsClientManager mVmsClientManager;
93 private final VmsSubscriberService mVmsSubscriberService;
94 private final VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070095
Pavel Maltsev0d07c762016-11-03 16:40:15 -070096 private final CarServiceBase[] mAllServices;
97
Steve Paikc302c7c2017-08-04 14:01:58 -070098 private static final String TAG = "ICarImpl";
99 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700100
101 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700102
keunyoung1ab8e182015-09-24 09:25:22 -0700103 /** Test only service. Populate it only when necessary. */
104 @GuardedBy("this")
105 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700106
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700107 @GuardedBy("this")
108 private ICarServiceHelper mICarServiceHelper;
109
Enrico Granatae8056ca2018-04-03 13:19:52 -0700110 private final String mVehicleInterfaceName;
111
Pavel Maltsevec83b632017-01-05 15:10:55 -0800112 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700113 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700114 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800115 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700116 mHal = new VehicleHal(vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700117 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800118 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Keun-young Parkd462a912019-02-11 08:53:42 -0800119 mCarUserService = new CarUserService(serviceContext, mUserManagerHelper);
Keun-young Park4727da32016-05-31 10:00:51 -0700120 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800121 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
122 systemInterface);
Steve Paik9ec53d72018-04-27 13:28:31 -0700123 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
124 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800125 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800126 mCarDrivingStateService, mCarPropertyService, mUserManagerHelper);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800127 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
128 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700129 mSystemActivityMonitoringService);
Justin Paupore44985ba2019-01-30 18:53:41 -0800130 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
131 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarPropertyService,
132 mPerUserCarServiceHelper, mCarUXRestrictionsService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700133 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800134 mCarProjectionService = new CarProjectionService(
135 serviceContext, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700136 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700137 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800138 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700139 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700140 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700141 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700142 mSystemStateControllerService = new SystemStateControllerService(
143 serviceContext, mCarAudioService, this);
Mark Tabrya8fce562019-01-16 16:24:01 -0800144 mVmsClientManager = new VmsClientManager(serviceContext, mUserManagerHelper);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700145 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
Mark Tabrya8fce562019-01-16 16:24:01 -0800146 mVmsPublisherService = new VmsPublisherService(serviceContext, mVmsClientManager,
147 mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700148 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700149 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
150 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700151 mCarConfigurationService =
152 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Serik Beketayev68920542018-09-06 13:29:58 -0700153 mCarLocationService = new CarLocationService(
154 mContext, mCarPropertyService, mUserManagerHelper);
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800155 mCarTrustAgentEnrollmentService = new CarTrustAgentEnrollmentService(serviceContext);
Simon Dai527eb552019-02-12 13:06:15 -0800156 mCarMediaService = new CarMediaService(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -0700157
Keun-young Parkd462a912019-02-11 08:53:42 -0800158 CarLocalServices.addService(CarUserService.class, mCarUserService);
159
keunyounga3b28d82015-08-25 13:05:15 -0700160 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700161 List<CarServiceBase> allServices = new ArrayList<>();
Keun-young Parkd462a912019-02-11 08:53:42 -0800162 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700163 allServices.add(mSystemActivityMonitoringService);
164 allServices.add(mCarPowerManagementService);
165 allServices.add(mCarPropertyService);
166 allServices.add(mCarDrivingStateService);
167 allServices.add(mCarUXRestrictionsService);
168 allServices.add(mCarPackageManagerService);
169 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700170 allServices.add(mGarageModeService);
171 allServices.add(mAppFocusService);
172 allServices.add(mCarAudioService);
173 allServices.add(mCarNightService);
174 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700175 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700176 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800177 allServices.add(mCarBluetoothService);
178 allServices.add(mCarProjectionService);
179 allServices.add(mCarDiagnosticService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700180 allServices.add(mCarStorageMonitoringService);
181 allServices.add(mCarConfigurationService);
Mark Tabrya8fce562019-01-16 16:24:01 -0800182 allServices.add(mVmsClientManager);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700183 allServices.add(mVmsSubscriberService);
184 allServices.add(mVmsPublisherService);
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800185 allServices.add(mCarTrustAgentEnrollmentService);
Simon Dai527eb552019-02-12 13:06:15 -0800186 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700187 allServices.add(mCarLocationService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700188 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700189 }
190
Pavel Maltsevabd47232017-10-10 16:54:57 -0700191 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700192 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700193 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700194 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700195 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700196 traceEnd();
197 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800198 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700199 service.init();
200 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700201 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700202 }
203
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700204 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700205 // release done in opposite order from init
206 for (int i = mAllServices.length - 1; i >= 0; i--) {
207 mAllServices[i].release();
208 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700209 mHal.release();
Keun-young Parkd462a912019-02-11 08:53:42 -0800210 CarLocalServices.removeAllServices();
keunyoung1ab8e182015-09-24 09:25:22 -0700211 }
212
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700213 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800214 mHal.vehicleHalReconnected(vehicle);
215 for (CarServiceBase service : mAllServices) {
216 service.vehicleHalReconnected();
217 }
218 }
219
keunyoungca515072015-07-10 12:21:47 -0700220 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700221 public void setCarServiceHelper(IBinder helper) {
222 int uid = Binder.getCallingUid();
223 if (uid != Process.SYSTEM_UID) {
224 throw new SecurityException("Only allowed from system");
225 }
226 synchronized (this) {
227 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800228 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700229 }
230 }
231
232 @Override
Keun-young Parkd462a912019-02-11 08:53:42 -0800233 public void setUserLockStatus(int userHandle, int unlocked) {
234 int uid = Binder.getCallingUid();
235 if (uid != Process.SYSTEM_UID) {
236 throw new SecurityException("Only allowed from system");
237 }
238 mCarUserService.setUserLockStatus(userHandle, unlocked == 1);
239 }
240
241 @Override
keunyoungca515072015-07-10 12:21:47 -0700242 public IBinder getCarService(String serviceName) {
243 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800244 case Car.AUDIO_SERVICE:
245 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700246 case Car.APP_FOCUS_SERVICE:
247 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800248 case Car.PACKAGE_SERVICE:
249 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800250 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700251 assertAnyDiagnosticPermission(mContext);
252 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800253 case Car.POWER_SERVICE:
254 assertPowerPermission(mContext);
255 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700256 case Car.CABIN_SERVICE:
257 case Car.HVAC_SERVICE:
258 case Car.INFO_SERVICE:
259 case Car.PROPERTY_SERVICE:
260 case Car.SENSOR_SERVICE:
261 case Car.VENDOR_EXTENSION_SERVICE:
262 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800263 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700264 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700265 IInstrumentClusterNavigation navService =
266 mInstrumentClusterService.getNavigationService();
267 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700268 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
269 assertClusterManagerPermission(mContext);
270 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800271 case Car.PROJECTION_SERVICE:
272 assertProjectionPermission(mContext);
273 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800274 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700275 assertVmsSubscriberPermission(mContext);
276 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800277 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700278 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700279 synchronized (this) {
280 if (mCarTestService == null) {
281 mCarTestService = new CarTestService(mContext, this);
282 }
283 return mCarTestService;
284 }
285 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700286 case Car.BLUETOOTH_SERVICE:
287 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700288 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700289 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700290 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800291 case Car.CAR_DRIVING_STATE_SERVICE:
292 assertDrivingStatePermission(mContext);
293 return mCarDrivingStateService;
294 case Car.CAR_UX_RESTRICTION_SERVICE:
295 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700296 case Car.CAR_CONFIGURATION_SERVICE:
297 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800298 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
299 assertTrustAgentEnrollmentPermission(mContext);
300 return mCarTrustAgentEnrollmentService;
Simon Dai527eb552019-02-12 13:06:15 -0800301 case Car.CAR_MEDIA_SERVICE:
302 return mCarMediaService;
keunyoungca515072015-07-10 12:21:47 -0700303 default:
304 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
305 return null;
306 }
307 }
308
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800309 @Override
310 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700311 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800312 }
313
Keun-young Parka28d7b22016-02-29 16:54:29 -0800314 public CarServiceBase getCarInternalService(String serviceName) {
315 switch (serviceName) {
316 case INTERNAL_INPUT_SERVICE:
317 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700318 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
319 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800320 default:
321 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
322 serviceName);
323 return null;
324 }
325 }
326
keunyoung1ab8e182015-09-24 09:25:22 -0700327 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700328 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700329 }
330
Keun-young Parke31a8b22016-03-16 17:34:08 -0700331 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700332 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800333 }
334
Pavel Maltsev905968c2017-07-16 19:48:57 -0700335 public static void assertClusterManagerPermission(Context context) {
336 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
337 }
338
Steve Paik388d7772018-02-12 10:54:51 -0800339 public static void assertPowerPermission(Context context) {
340 assertPermission(context, Car.PERMISSION_CAR_POWER);
341 }
342
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800343 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700344 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
345 }
346
Enrico Granata3c7a6662017-02-23 18:07:59 -0800347 public static void assertAnyDiagnosticPermission(Context context) {
348 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700349 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800350 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
351 }
352
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800353 public static void assertDrivingStatePermission(Context context) {
354 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
355 }
356
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800357 public static void assertVmsPublisherPermission(Context context) {
358 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
359 }
360
Antonio Cortese4619c72017-02-02 07:53:27 -0800361 public static void assertVmsSubscriberPermission(Context context) {
362 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
363 }
364
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800365 /**
366 * Ensures the caller has the permission to enroll a Trust Agent.
367 * @param context
368 */
369 public static void assertTrustAgentEnrollmentPermission(Context context) {
370 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
371 }
372
Steve Paik461ecc62016-06-08 15:28:32 -0700373 public static void assertPermission(Context context, String permission) {
374 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
375 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800376 }
377 }
378
Steve Paik9ec53d72018-04-27 13:28:31 -0700379 /**
380 * Checks to see if the caller has a permission.
381 * @param context
382 * @param permission
383 *
384 * @return boolean TRUE if caller has the permission.
385 */
386 public static boolean hasPermission(Context context, String permission) {
387 return context.checkCallingOrSelfPermission(permission)
388 == PackageManager.PERMISSION_GRANTED;
389 }
390
Enrico Granata3c7a6662017-02-23 18:07:59 -0800391 public static void assertAnyPermission(Context context, String... permissions) {
392 for (String permission : permissions) {
393 if (context.checkCallingOrSelfPermission(permission) ==
394 PackageManager.PERMISSION_GRANTED) {
395 return;
396 }
397 }
398 throw new SecurityException("requires any of " + Arrays.toString(permissions));
399 }
400
Enrico Granatae8056ca2018-04-03 13:19:52 -0700401 @Override
402 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
403 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700404 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700405 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700406 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
407 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700408 return;
keunyounga3b28d82015-08-25 13:05:15 -0700409 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700410 if (args == null || args.length == 0) {
411 writer.println("*dump car service*");
412
413 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
414 writer.println("*Dump all services*");
415 for (CarServiceBase service : mAllServices) {
416 dumpService(service, writer);
417 }
418 if (mCarTestService != null) {
419 dumpService(mCarTestService, writer);
420 }
421 writer.println("*Dump Vehicle HAL*");
422 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
423 try {
424 // TODO dump all feature flags by creating a dumpable interface
425 mHal.dump(writer);
426 } catch (Exception e) {
427 writer.println("Failed dumping: " + mHal.getClass().getName());
428 e.printStackTrace(writer);
429 }
430 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
431 execShellCmd(args, writer);
432 } else {
433 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800434 }
435 }
436
437 private void dumpService(CarServiceBase service, PrintWriter writer) {
438 try {
439 service.dump(writer);
440 } catch (Exception e) {
441 writer.println("Failed dumping: " + service.getClass().getName());
442 e.printStackTrace(writer);
443 }
keunyoungcc449f72015-08-12 10:46:27 -0700444 }
Yao Chene33f07e2016-07-26 12:02:51 -0700445
446 void execShellCmd(String[] args, PrintWriter writer) {
447 new CarShellCommand().exec(args, writer);
448 }
449
Pavel Maltsevabd47232017-10-10 16:54:57 -0700450 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700451 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700452 Slog.i(TAG, name);
453 mBootTiming.traceBegin(name);
454 }
455
Pavel Maltsevabd47232017-10-10 16:54:57 -0700456 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700457 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700458 mBootTiming.traceEnd();
459 }
460
Yao Chene33f07e2016-07-26 12:02:51 -0700461 private class CarShellCommand {
462 private static final String COMMAND_HELP = "-h";
463 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800464 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800465 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800466 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800467 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800468
Yao Chene33f07e2016-07-26 12:02:51 -0700469 private static final String PARAM_DAY_MODE = "day";
470 private static final String PARAM_NIGHT_MODE = "night";
471 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800472 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800473 private static final String PARAM_ON_MODE = "on";
474 private static final String PARAM_OFF_MODE = "off";
475 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800476
Yao Chene33f07e2016-07-26 12:02:51 -0700477
478 private void dumpHelp(PrintWriter pw) {
479 pw.println("Car service commands:");
480 pw.println("\t-h");
481 pw.println("\t Print this help text.");
482 pw.println("\tday-night-mode [day|night|sensor]");
483 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800484 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
485 pw.println("\t Inject a vehicle property for testing");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800486 pw.println("\tdisable-uxr true|false");
487 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800488 pw.println("\tgarage-mode [on|off|query]");
489 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800490 pw.println("\tget-do-activities pkgname");
491 pw.println("\t Get Distraction Optimized activities in given package");
Yao Chene33f07e2016-07-26 12:02:51 -0700492 }
493
494 public void exec(String[] args, PrintWriter writer) {
495 String arg = args[0];
496 switch (arg) {
497 case COMMAND_HELP:
498 dumpHelp(writer);
499 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800500 case COMMAND_DAY_NIGHT_MODE: {
Yao Chene33f07e2016-07-26 12:02:51 -0700501 String value = args.length < 1 ? "" : args[1];
502 forceDayNightMode(value, writer);
503 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800504 }
505 case COMMAND_GARAGE_MODE: {
506 String value = args.length < 1 ? "" : args[1];
507 forceGarageMode(value, writer);
508 break;
509 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800510 case COMMAND_INJECT_VHAL_EVENT:
511 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
512 String data;
513 if (args.length < 3) {
514 writer.println("Incorrect number of arguments.");
515 dumpHelp(writer);
516 break;
517 } else if (args.length > 3) {
518 // Zoned
519 zone = args[2];
520 data = args[3];
521 } else {
522 // Global
523 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800524 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800525 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800526 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800527 case COMMAND_ENABLE_UXR:
528 if (args.length < 2) {
529 writer.println("Incorrect number of arguments");
530 dumpHelp(writer);
531 break;
532 }
533 boolean enableBlocking = Boolean.valueOf(args[1]);
534 if (mCarPackageManagerService != null) {
535 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
536 }
537 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800538 case COMMAND_GET_DO_ACTIVITIES:
539 if (args.length < 2) {
540 writer.println("Incorrect number of arguments");
541 dumpHelp(writer);
542 break;
543 }
544 String pkgName = args[1].toLowerCase();
545 if (mCarPackageManagerService != null) {
546 String[] doActivities =
547 mCarPackageManagerService.getDistractionOptimizedActivities(
548 pkgName);
549 if (doActivities != null) {
550 writer.println("DO Activities for " + pkgName);
551 for (String a : doActivities) {
552 writer.println(a);
553 }
554 } else {
555 writer.println("No DO Activities for " + pkgName);
556 }
557 }
558 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700559 default:
560 writer.println("Unknown command.");
561 dumpHelp(writer);
562 }
563 }
564
565 private void forceDayNightMode(String arg, PrintWriter writer) {
566 int mode;
567 switch (arg) {
568 case PARAM_DAY_MODE:
569 mode = CarNightService.FORCED_DAY_MODE;
570 break;
571 case PARAM_NIGHT_MODE:
572 mode = CarNightService.FORCED_NIGHT_MODE;
573 break;
574 case PARAM_SENSOR_MODE:
575 mode = CarNightService.FORCED_SENSOR_MODE;
576 break;
577 default:
578 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
579 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
580 return;
581 }
582 int current = mCarNightService.forceDayNightMode(mode);
583 String currentMode = null;
584 switch (current) {
585 case UiModeManager.MODE_NIGHT_AUTO:
586 currentMode = PARAM_SENSOR_MODE;
587 break;
588 case UiModeManager.MODE_NIGHT_YES:
589 currentMode = PARAM_NIGHT_MODE;
590 break;
591 case UiModeManager.MODE_NIGHT_NO:
592 currentMode = PARAM_DAY_MODE;
593 break;
594 }
595 writer.println("DayNightMode changed to: " + currentMode);
596 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800597
Bryan Eylera32a7c12018-02-27 15:40:00 -0800598 private void forceGarageMode(String arg, PrintWriter writer) {
599 switch (arg) {
600 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700601 mGarageModeService.forceStartGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800602 break;
603 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700604 mGarageModeService.stopAndResetGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800605 break;
606 case PARAM_QUERY_MODE:
607 // Nothing to do. Always query at the end anyway.
608 break;
609 default:
610 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
611 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
612 return;
613 }
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700614 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -0800615 }
616
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800617 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800618 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800619 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800620 * @param property the Vehicle property Id as defined in the HAL
621 * @param zone Zone that this event services
622 * @param value Data value of the event
623 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800624 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800625 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800626 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800627 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
628 if (!isPropertyAreaTypeGlobal(property)) {
629 writer.println("Property area type inconsistent with given zone");
630 return;
631 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800632 }
633 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800634 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800635 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800636 writer.println("Invalid property Id zone Id or value" + e);
637 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800638 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800639 }
640
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800641 // Check if the given property is global
642 private boolean isPropertyAreaTypeGlobal(String property) {
643 if (property == null) {
644 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800645 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800646 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800647 }
Yao Chene33f07e2016-07-26 12:02:51 -0700648 }
kevinjm55822c42018-08-15 11:26:00 -0700649}