blob: c43547b5a4c01ec5e4dd010baa5ebd2d6de2555c [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 Tabry76cfaaa2019-02-01 14:28:30 -080047import com.android.car.vms.VmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -080048import com.android.car.vms.VmsClientManager;
keunyoungca515072015-07-10 12:21:47 -070049import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000050import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070051
Enrico Granatae8056ca2018-04-03 13:19:52 -070052import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070053import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070054import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080055import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070056import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070057
keunyoungca515072015-07-10 12:21:47 -070058public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070059
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080060 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070061 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
62 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080063
keunyoungca515072015-07-10 12:21:47 -070064 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080065 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070066
Anthony Chen12aec302018-04-25 16:41:48 -070067 private final SystemInterface mSystemInterface;
68
Keun-young Park4727da32016-05-31 10:00:51 -070069 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070070 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080071 private final CarPackageManagerService mCarPackageManagerService;
72 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080073 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080074 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070075 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080076 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070077 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080078 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070079 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080080 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080081 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080082 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070083 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080084 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070085 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070086 private final CarDiagnosticService mCarDiagnosticService;
87 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -070088 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -080089 private final CarTrustAgentEnrollmentService mCarTrustAgentEnrollmentService;
Simon Dai527eb552019-02-12 13:06:15 -080090 private final CarMediaService mCarMediaService;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070091 private final CarUserManagerHelper mUserManagerHelper;
Keun-young Parkd462a912019-02-11 08:53:42 -080092 private final CarUserService mCarUserService;
Mark Tabrya8fce562019-01-16 16:24:01 -080093 private final VmsClientManager mVmsClientManager;
Mark Tabry76cfaaa2019-02-01 14:28:30 -080094 private final VmsBrokerService mVmsBrokerService;
Mark Tabrya8fce562019-01-16 16:24:01 -080095 private final VmsSubscriberService mVmsSubscriberService;
96 private final VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070097
Pavel Maltsev0d07c762016-11-03 16:40:15 -070098 private final CarServiceBase[] mAllServices;
99
Steve Paikc302c7c2017-08-04 14:01:58 -0700100 private static final String TAG = "ICarImpl";
101 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -0700102
103 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700104
keunyoung1ab8e182015-09-24 09:25:22 -0700105 /** Test only service. Populate it only when necessary. */
106 @GuardedBy("this")
107 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700108
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700109 @GuardedBy("this")
110 private ICarServiceHelper mICarServiceHelper;
111
Enrico Granatae8056ca2018-04-03 13:19:52 -0700112 private final String mVehicleInterfaceName;
113
Pavel Maltsevec83b632017-01-05 15:10:55 -0800114 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700115 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700116 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800117 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700118 mHal = new VehicleHal(vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700119 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800120 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Keun-young Parkd462a912019-02-11 08:53:42 -0800121 mCarUserService = new CarUserService(serviceContext, mUserManagerHelper);
Keun-young Park4727da32016-05-31 10:00:51 -0700122 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800123 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
Keun young Park9b3f2662019-03-19 10:30:25 -0700124 systemInterface, mUserManagerHelper);
Steve Paik9ec53d72018-04-27 13:28:31 -0700125 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
126 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800127 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing9bfb7492019-02-15 11:53:34 -0800128 mCarDrivingStateService, mCarPropertyService);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800129 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
130 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700131 mSystemActivityMonitoringService);
Justin Paupore44985ba2019-01-30 18:53:41 -0800132 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
133 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarPropertyService,
134 mPerUserCarServiceHelper, mCarUXRestrictionsService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700135 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Justin Paupore44985ba2019-01-30 18:53:41 -0800136 mCarProjectionService = new CarProjectionService(
Pavel Maltsev079873b2019-02-25 12:15:09 -0800137 serviceContext, null /* handler */, mCarInputService, mCarBluetoothService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700138 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700139 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800140 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700141 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700142 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700143 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700144 mSystemStateControllerService = new SystemStateControllerService(
145 serviceContext, mCarAudioService, this);
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800146 mVmsBrokerService = new VmsBrokerService();
Mark Tabrya8fce562019-01-16 16:24:01 -0800147 mVmsClientManager = new VmsClientManager(serviceContext, mUserManagerHelper);
Mark Tabry76cfaaa2019-02-01 14:28:30 -0800148 mVmsSubscriberService = new VmsSubscriberService(
149 serviceContext, mVmsBrokerService, mHal.getVmsHal());
150 mVmsPublisherService = new VmsPublisherService(
151 serviceContext, mVmsBrokerService, mVmsClientManager, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700152 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700153 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
154 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700155 mCarConfigurationService =
156 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Gregory Clarka440e812019-02-14 16:05:51 -0800157 mCarLocationService = new CarLocationService(mContext, mCarPropertyService,
158 mUserManagerHelper);
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800159 mCarTrustAgentEnrollmentService = new CarTrustAgentEnrollmentService(serviceContext);
Simon Dai527eb552019-02-12 13:06:15 -0800160 mCarMediaService = new CarMediaService(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -0700161
Keun-young Parkd462a912019-02-11 08:53:42 -0800162 CarLocalServices.addService(CarUserService.class, mCarUserService);
Gregory Clarka440e812019-02-14 16:05:51 -0800163 CarLocalServices.addService(SystemInterface.class, mSystemInterface);
Keun-young Parkd462a912019-02-11 08:53:42 -0800164
keunyounga3b28d82015-08-25 13:05:15 -0700165 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700166 List<CarServiceBase> allServices = new ArrayList<>();
Keun-young Parkd462a912019-02-11 08:53:42 -0800167 allServices.add(mCarUserService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700168 allServices.add(mSystemActivityMonitoringService);
169 allServices.add(mCarPowerManagementService);
170 allServices.add(mCarPropertyService);
171 allServices.add(mCarDrivingStateService);
172 allServices.add(mCarUXRestrictionsService);
173 allServices.add(mCarPackageManagerService);
174 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700175 allServices.add(mGarageModeService);
176 allServices.add(mAppFocusService);
177 allServices.add(mCarAudioService);
178 allServices.add(mCarNightService);
179 allServices.add(mInstrumentClusterService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700180 allServices.add(mSystemStateControllerService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700181 allServices.add(mPerUserCarServiceHelper);
Justin Paupore44985ba2019-01-30 18:53:41 -0800182 allServices.add(mCarBluetoothService);
183 allServices.add(mCarProjectionService);
184 allServices.add(mCarDiagnosticService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700185 allServices.add(mCarStorageMonitoringService);
186 allServices.add(mCarConfigurationService);
Mark Tabrya8fce562019-01-16 16:24:01 -0800187 allServices.add(mVmsClientManager);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700188 allServices.add(mVmsSubscriberService);
189 allServices.add(mVmsPublisherService);
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800190 allServices.add(mCarTrustAgentEnrollmentService);
Simon Dai527eb552019-02-12 13:06:15 -0800191 allServices.add(mCarMediaService);
Gregory Clarka63ba022018-06-07 16:42:12 -0700192 allServices.add(mCarLocationService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700193 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700194 }
195
Pavel Maltsevabd47232017-10-10 16:54:57 -0700196 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700197 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700198 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700199 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700200 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700201 traceEnd();
202 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800203 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700204 service.init();
205 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700206 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700207 }
208
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700209 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700210 // release done in opposite order from init
211 for (int i = mAllServices.length - 1; i >= 0; i--) {
212 mAllServices[i].release();
213 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700214 mHal.release();
Keun-young Parkd462a912019-02-11 08:53:42 -0800215 CarLocalServices.removeAllServices();
keunyoung1ab8e182015-09-24 09:25:22 -0700216 }
217
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700218 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800219 mHal.vehicleHalReconnected(vehicle);
220 for (CarServiceBase service : mAllServices) {
221 service.vehicleHalReconnected();
222 }
223 }
224
keunyoungca515072015-07-10 12:21:47 -0700225 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700226 public void setCarServiceHelper(IBinder helper) {
227 int uid = Binder.getCallingUid();
228 if (uid != Process.SYSTEM_UID) {
229 throw new SecurityException("Only allowed from system");
230 }
231 synchronized (this) {
232 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800233 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700234 }
235 }
236
237 @Override
Keun-young Parkd462a912019-02-11 08:53:42 -0800238 public void setUserLockStatus(int userHandle, int unlocked) {
239 int uid = Binder.getCallingUid();
240 if (uid != Process.SYSTEM_UID) {
241 throw new SecurityException("Only allowed from system");
242 }
243 mCarUserService.setUserLockStatus(userHandle, unlocked == 1);
244 }
245
246 @Override
keunyoungca515072015-07-10 12:21:47 -0700247 public IBinder getCarService(String serviceName) {
248 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800249 case Car.AUDIO_SERVICE:
250 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700251 case Car.APP_FOCUS_SERVICE:
252 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800253 case Car.PACKAGE_SERVICE:
254 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800255 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700256 assertAnyDiagnosticPermission(mContext);
257 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800258 case Car.POWER_SERVICE:
259 assertPowerPermission(mContext);
260 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700261 case Car.CABIN_SERVICE:
262 case Car.HVAC_SERVICE:
263 case Car.INFO_SERVICE:
264 case Car.PROPERTY_SERVICE:
265 case Car.SENSOR_SERVICE:
266 case Car.VENDOR_EXTENSION_SERVICE:
267 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800268 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700269 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700270 IInstrumentClusterNavigation navService =
271 mInstrumentClusterService.getNavigationService();
272 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700273 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
274 assertClusterManagerPermission(mContext);
275 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800276 case Car.PROJECTION_SERVICE:
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800277 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800278 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700279 assertVmsSubscriberPermission(mContext);
280 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800281 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700282 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700283 synchronized (this) {
284 if (mCarTestService == null) {
285 mCarTestService = new CarTestService(mContext, this);
286 }
287 return mCarTestService;
288 }
289 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700290 case Car.BLUETOOTH_SERVICE:
291 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700292 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700293 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700294 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800295 case Car.CAR_DRIVING_STATE_SERVICE:
296 assertDrivingStatePermission(mContext);
297 return mCarDrivingStateService;
298 case Car.CAR_UX_RESTRICTION_SERVICE:
299 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700300 case Car.CAR_CONFIGURATION_SERVICE:
301 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800302 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
303 assertTrustAgentEnrollmentPermission(mContext);
304 return mCarTrustAgentEnrollmentService;
Simon Dai527eb552019-02-12 13:06:15 -0800305 case Car.CAR_MEDIA_SERVICE:
306 return mCarMediaService;
keunyoungca515072015-07-10 12:21:47 -0700307 default:
308 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
309 return null;
310 }
311 }
312
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800313 @Override
314 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700315 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800316 }
317
Keun-young Parka28d7b22016-02-29 16:54:29 -0800318 public CarServiceBase getCarInternalService(String serviceName) {
319 switch (serviceName) {
320 case INTERNAL_INPUT_SERVICE:
321 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700322 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
323 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800324 default:
325 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
326 serviceName);
327 return null;
328 }
329 }
330
keunyoung1ab8e182015-09-24 09:25:22 -0700331 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700332 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700333 }
334
Keun-young Parke31a8b22016-03-16 17:34:08 -0700335 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700336 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800337 }
338
Pavel Maltsev905968c2017-07-16 19:48:57 -0700339 public static void assertClusterManagerPermission(Context context) {
340 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
341 }
342
Steve Paik388d7772018-02-12 10:54:51 -0800343 public static void assertPowerPermission(Context context) {
344 assertPermission(context, Car.PERMISSION_CAR_POWER);
345 }
346
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800347 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700348 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
349 }
350
Pavel Maltsev079873b2019-02-25 12:15:09 -0800351 /** Verify the calling context has the {@link Car#PERMISSION_CAR_PROJECTION_STATUS} */
352 public static void assertProjectionStatusPermission(Context context) {
353 assertPermission(context, Car.PERMISSION_CAR_PROJECTION_STATUS);
354 }
355
Enrico Granata3c7a6662017-02-23 18:07:59 -0800356 public static void assertAnyDiagnosticPermission(Context context) {
357 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700358 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800359 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
360 }
361
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800362 public static void assertDrivingStatePermission(Context context) {
363 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
364 }
365
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800366 public static void assertVmsPublisherPermission(Context context) {
367 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
368 }
369
Antonio Cortese4619c72017-02-02 07:53:27 -0800370 public static void assertVmsSubscriberPermission(Context context) {
371 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
372 }
373
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800374 /**
375 * Ensures the caller has the permission to enroll a Trust Agent.
376 * @param context
377 */
378 public static void assertTrustAgentEnrollmentPermission(Context context) {
379 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
380 }
381
Steve Paik461ecc62016-06-08 15:28:32 -0700382 public static void assertPermission(Context context, String permission) {
383 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
384 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800385 }
386 }
387
Steve Paik9ec53d72018-04-27 13:28:31 -0700388 /**
389 * Checks to see if the caller has a permission.
390 * @param context
391 * @param permission
392 *
393 * @return boolean TRUE if caller has the permission.
394 */
395 public static boolean hasPermission(Context context, String permission) {
396 return context.checkCallingOrSelfPermission(permission)
397 == PackageManager.PERMISSION_GRANTED;
398 }
399
Enrico Granata3c7a6662017-02-23 18:07:59 -0800400 public static void assertAnyPermission(Context context, String... permissions) {
401 for (String permission : permissions) {
402 if (context.checkCallingOrSelfPermission(permission) ==
403 PackageManager.PERMISSION_GRANTED) {
404 return;
405 }
406 }
407 throw new SecurityException("requires any of " + Arrays.toString(permissions));
408 }
409
Enrico Granatae8056ca2018-04-03 13:19:52 -0700410 @Override
411 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
412 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700413 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700414 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700415 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
416 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700417 return;
keunyounga3b28d82015-08-25 13:05:15 -0700418 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700419 if (args == null || args.length == 0) {
420 writer.println("*dump car service*");
421
422 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
423 writer.println("*Dump all services*");
424 for (CarServiceBase service : mAllServices) {
425 dumpService(service, writer);
426 }
427 if (mCarTestService != null) {
428 dumpService(mCarTestService, writer);
429 }
430 writer.println("*Dump Vehicle HAL*");
431 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
432 try {
433 // TODO dump all feature flags by creating a dumpable interface
434 mHal.dump(writer);
435 } catch (Exception e) {
436 writer.println("Failed dumping: " + mHal.getClass().getName());
437 e.printStackTrace(writer);
438 }
439 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
440 execShellCmd(args, writer);
441 } else {
442 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800443 }
444 }
445
446 private void dumpService(CarServiceBase service, PrintWriter writer) {
447 try {
448 service.dump(writer);
449 } catch (Exception e) {
450 writer.println("Failed dumping: " + service.getClass().getName());
451 e.printStackTrace(writer);
452 }
keunyoungcc449f72015-08-12 10:46:27 -0700453 }
Yao Chene33f07e2016-07-26 12:02:51 -0700454
455 void execShellCmd(String[] args, PrintWriter writer) {
456 new CarShellCommand().exec(args, writer);
457 }
458
Pavel Maltsevabd47232017-10-10 16:54:57 -0700459 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700460 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700461 Slog.i(TAG, name);
462 mBootTiming.traceBegin(name);
463 }
464
Pavel Maltsevabd47232017-10-10 16:54:57 -0700465 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700466 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700467 mBootTiming.traceEnd();
468 }
469
Yao Chene33f07e2016-07-26 12:02:51 -0700470 private class CarShellCommand {
471 private static final String COMMAND_HELP = "-h";
472 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800473 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800474 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800475 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800476 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Kaidd4ef5a2019-03-04 14:07:26 -0800477 private static final String COMMAND_GET_CARPROPERTYCONFIG = "get-carpropertyconfig";
Pavel Maltsev48a31942019-03-03 22:08:28 -0800478 private static final String COMMAND_PROJECTION_UI_MODE = "projection-ui-mode";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800479
Yao Chene33f07e2016-07-26 12:02:51 -0700480 private static final String PARAM_DAY_MODE = "day";
481 private static final String PARAM_NIGHT_MODE = "night";
482 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800483 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800484 private static final String PARAM_ON_MODE = "on";
485 private static final String PARAM_OFF_MODE = "off";
486 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800487
Yao Chene33f07e2016-07-26 12:02:51 -0700488
489 private void dumpHelp(PrintWriter pw) {
490 pw.println("Car service commands:");
491 pw.println("\t-h");
492 pw.println("\t Print this help text.");
493 pw.println("\tday-night-mode [day|night|sensor]");
494 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800495 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
Jim Kayef10b7d82019-02-26 12:53:22 -0800496 pw.println("\t Inject a vehicle property for testing.");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800497 pw.println("\tdisable-uxr true|false");
498 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800499 pw.println("\tgarage-mode [on|off|query]");
500 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800501 pw.println("\tget-do-activities pkgname");
Jim Kayef10b7d82019-02-26 12:53:22 -0800502 pw.println("\t Get Distraction Optimized activities in given package.");
Kaidd4ef5a2019-03-04 14:07:26 -0800503 pw.println("\tget-carpropertyconfig [propertyId]");
504 pw.println("\t Get a CarPropertyConfig by Id in Hex or list all CarPropertyConfigs");
Yao Chene33f07e2016-07-26 12:02:51 -0700505 }
506
507 public void exec(String[] args, PrintWriter writer) {
508 String arg = args[0];
509 switch (arg) {
510 case COMMAND_HELP:
511 dumpHelp(writer);
512 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800513 case COMMAND_DAY_NIGHT_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800514 String value = args.length < 2 ? "" : args[1];
Yao Chene33f07e2016-07-26 12:02:51 -0700515 forceDayNightMode(value, writer);
516 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800517 }
518 case COMMAND_GARAGE_MODE: {
Jim Kayef10b7d82019-02-26 12:53:22 -0800519 String value = args.length < 2 ? "" : args[1];
Bryan Eylera32a7c12018-02-27 15:40:00 -0800520 forceGarageMode(value, writer);
521 break;
522 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800523 case COMMAND_INJECT_VHAL_EVENT:
524 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
525 String data;
Jim Kayef10b7d82019-02-26 12:53:22 -0800526 if (args.length != 3 && args.length != 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800527 writer.println("Incorrect number of arguments.");
528 dumpHelp(writer);
529 break;
Jim Kayef10b7d82019-02-26 12:53:22 -0800530 } else if (args.length == 4) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800531 // Zoned
532 zone = args[2];
533 data = args[3];
534 } else {
535 // Global
536 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800537 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800538 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800539 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800540 case COMMAND_ENABLE_UXR:
Jim Kayef10b7d82019-02-26 12:53:22 -0800541 if (args.length != 2) {
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800542 writer.println("Incorrect number of arguments");
543 dumpHelp(writer);
544 break;
545 }
546 boolean enableBlocking = Boolean.valueOf(args[1]);
547 if (mCarPackageManagerService != null) {
548 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
549 }
550 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800551 case COMMAND_GET_DO_ACTIVITIES:
Jim Kayef10b7d82019-02-26 12:53:22 -0800552 if (args.length != 2) {
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800553 writer.println("Incorrect number of arguments");
554 dumpHelp(writer);
555 break;
556 }
557 String pkgName = args[1].toLowerCase();
558 if (mCarPackageManagerService != null) {
559 String[] doActivities =
560 mCarPackageManagerService.getDistractionOptimizedActivities(
561 pkgName);
562 if (doActivities != null) {
563 writer.println("DO Activities for " + pkgName);
564 for (String a : doActivities) {
565 writer.println(a);
566 }
567 } else {
568 writer.println("No DO Activities for " + pkgName);
569 }
570 }
571 break;
Kaidd4ef5a2019-03-04 14:07:26 -0800572 case COMMAND_GET_CARPROPERTYCONFIG:
573 String propertyId = args.length < 2 ? "" : args[1];
574 mHal.dumpPropertyConfigs(writer, propertyId);
575 break;
Pavel Maltsev48a31942019-03-03 22:08:28 -0800576 case COMMAND_PROJECTION_UI_MODE:
577 if (args.length != 2) {
578 writer.println("Incorrect number of arguments");
579 dumpHelp(writer);
580 break;
581 }
582 mCarProjectionService.setUiMode(Integer.valueOf(args[1]));
Pavel Maltsevd6961f62019-03-26 10:22:31 -0700583 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700584 default:
Jim Kayef10b7d82019-02-26 12:53:22 -0800585 writer.println("Unknown command: \"" + arg + "\"");
Yao Chene33f07e2016-07-26 12:02:51 -0700586 dumpHelp(writer);
587 }
588 }
589
590 private void forceDayNightMode(String arg, PrintWriter writer) {
591 int mode;
592 switch (arg) {
593 case PARAM_DAY_MODE:
594 mode = CarNightService.FORCED_DAY_MODE;
595 break;
596 case PARAM_NIGHT_MODE:
597 mode = CarNightService.FORCED_NIGHT_MODE;
598 break;
599 case PARAM_SENSOR_MODE:
600 mode = CarNightService.FORCED_SENSOR_MODE;
601 break;
602 default:
603 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
604 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
605 return;
606 }
607 int current = mCarNightService.forceDayNightMode(mode);
608 String currentMode = null;
609 switch (current) {
610 case UiModeManager.MODE_NIGHT_AUTO:
611 currentMode = PARAM_SENSOR_MODE;
612 break;
613 case UiModeManager.MODE_NIGHT_YES:
614 currentMode = PARAM_NIGHT_MODE;
615 break;
616 case UiModeManager.MODE_NIGHT_NO:
617 currentMode = PARAM_DAY_MODE;
618 break;
619 }
620 writer.println("DayNightMode changed to: " + currentMode);
621 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800622
Bryan Eylera32a7c12018-02-27 15:40:00 -0800623 private void forceGarageMode(String arg, PrintWriter writer) {
624 switch (arg) {
625 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700626 mGarageModeService.forceStartGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800627 break;
628 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700629 mGarageModeService.stopAndResetGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800630 break;
631 case PARAM_QUERY_MODE:
632 // Nothing to do. Always query at the end anyway.
633 break;
634 default:
635 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
636 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
637 return;
638 }
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700639 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -0800640 }
641
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800642 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800643 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800644 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800645 * @param property the Vehicle property Id as defined in the HAL
646 * @param zone Zone that this event services
647 * @param value Data value of the event
648 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800649 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800650 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800651 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800652 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
653 if (!isPropertyAreaTypeGlobal(property)) {
654 writer.println("Property area type inconsistent with given zone");
655 return;
656 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800657 }
658 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800659 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800660 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800661 writer.println("Invalid property Id zone Id or value" + e);
662 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800663 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800664 }
665
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800666 // Check if the given property is global
667 private boolean isPropertyAreaTypeGlobal(String property) {
668 if (property == null) {
669 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800670 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800671 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800672 }
Yao Chene33f07e2016-07-26 12:02:51 -0700673 }
kevinjm55822c42018-08-15 11:26:00 -0700674}