blob: adcedf3286bc433a88bd4a0d89ef286a12a90862 [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;
keunyoungca515072015-07-10 12:21:47 -070047import com.android.internal.annotations.GuardedBy;
Steve Paik1d8bea22018-02-03 01:54:06 +000048import com.android.internal.car.ICarServiceHelper;
Gregory Clark26fa6012018-03-14 18:38:56 -070049
Enrico Granatae8056ca2018-04-03 13:19:52 -070050import java.io.FileDescriptor;
keunyounga3b28d82015-08-25 13:05:15 -070051import java.io.PrintWriter;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070052import java.util.ArrayList;
Antonio Cortese4619c72017-02-02 07:53:27 -080053import java.util.Arrays;
Ying Zheng86d1c3e2018-05-11 11:42:48 -070054import java.util.List;
keunyounga3b28d82015-08-25 13:05:15 -070055
keunyoungca515072015-07-10 12:21:47 -070056public class ICarImpl extends ICar.Stub {
keunyoungca515072015-07-10 12:21:47 -070057
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -080058 public static final String INTERNAL_INPUT_SERVICE = "internal_input";
Keun-young Park4727da32016-05-31 10:00:51 -070059 public static final String INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE =
60 "system_activity_monitoring";
Keun-young Parka28d7b22016-02-29 16:54:29 -080061
keunyoungca515072015-07-10 12:21:47 -070062 private final Context mContext;
Pavel Maltsevec83b632017-01-05 15:10:55 -080063 private final VehicleHal mHal;
keunyoungca515072015-07-10 12:21:47 -070064
Anthony Chen12aec302018-04-25 16:41:48 -070065 private final SystemInterface mSystemInterface;
66
Keun-young Park4727da32016-05-31 10:00:51 -070067 private final SystemActivityMonitoringService mSystemActivityMonitoringService;
keunyoung4b0212c2015-10-29 17:11:57 -070068 private final CarPowerManagementService mCarPowerManagementService;
Keun-young Parka28d7b22016-02-29 16:54:29 -080069 private final CarPackageManagerService mCarPackageManagerService;
70 private final CarInputService mCarInputService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -080071 private final CarDrivingStateService mCarDrivingStateService;
Ram Periathiruvadi4526a432018-01-24 13:00:54 -080072 private final CarUxRestrictionsManagerService mCarUXRestrictionsService;
keunyoungd32f4e62015-09-21 11:33:06 -070073 private final CarAudioService mCarAudioService;
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -080074 private final CarProjectionService mCarProjectionService;
Steve Paik9ec53d72018-04-27 13:28:31 -070075 private final CarPropertyService mCarPropertyService;
Joseph Pirozzo317343d2016-01-25 10:22:37 -080076 private final CarNightService mCarNightService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -070077 private final AppFocusService mAppFocusService;
Yao Chen3a7976d2016-01-20 17:27:08 -080078 private final GarageModeService mGarageModeService;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -080079 private final InstrumentClusterService mInstrumentClusterService;
Gregory Clarkd8136062017-12-11 14:27:53 -080080 private final CarLocationService mCarLocationService;
Keun-young Parkd73afae2016-04-08 20:03:32 -070081 private final SystemStateControllerService mSystemStateControllerService;
Ram Periathiruvadi7ed84182017-01-20 15:18:08 -080082 private final CarBluetoothService mCarBluetoothService;
Ram Periathiruvadiacb60242017-04-13 16:19:09 -070083 private final PerUserCarServiceHelper mPerUserCarServiceHelper;
Enrico Granata9a916d72017-09-19 14:33:08 -070084 private final CarDiagnosticService mCarDiagnosticService;
85 private final CarStorageMonitoringService mCarStorageMonitoringService;
Anthony Chen12aec302018-04-25 16:41:48 -070086 private final CarConfigurationService mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -080087 private final CarTrustAgentEnrollmentService mCarTrustAgentEnrollmentService;
Anthony Chen12aec302018-04-25 16:41:48 -070088
Ying Zheng86d1c3e2018-05-11 11:42:48 -070089 private final CarUserManagerHelper mUserManagerHelper;
90 private CarUserService mCarUserService;
Antonio Cortese4619c72017-02-02 07:53:27 -080091 private VmsSubscriberService mVmsSubscriberService;
Antonio Cortes6b3544c2017-02-06 16:54:58 -080092 private VmsPublisherService mVmsPublisherService;
keunyounga74b9ca2015-10-21 13:33:58 -070093
Pavel Maltsev0d07c762016-11-03 16:40:15 -070094 private final CarServiceBase[] mAllServices;
95
Steve Paikc302c7c2017-08-04 14:01:58 -070096 private static final String TAG = "ICarImpl";
97 private static final String VHAL_TIMING_TAG = "VehicleHalTiming";
Serik Beketayev74debf22018-10-04 12:18:09 -070098
99 private TimingsTraceLog mBootTiming;
Steve Paikc302c7c2017-08-04 14:01:58 -0700100
keunyoung1ab8e182015-09-24 09:25:22 -0700101 /** Test only service. Populate it only when necessary. */
102 @GuardedBy("this")
103 private CarTestService mCarTestService;
keunyoungca515072015-07-10 12:21:47 -0700104
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700105 @GuardedBy("this")
106 private ICarServiceHelper mICarServiceHelper;
107
Enrico Granatae8056ca2018-04-03 13:19:52 -0700108 private final String mVehicleInterfaceName;
109
Pavel Maltsevec83b632017-01-05 15:10:55 -0800110 public ICarImpl(Context serviceContext, IVehicle vehicle, SystemInterface systemInterface,
Enrico Granatae8056ca2018-04-03 13:19:52 -0700111 CanBusErrorNotifier errorNotifier, String vehicleInterfaceName) {
keunyoungca515072015-07-10 12:21:47 -0700112 mContext = serviceContext;
Steve Paik0f9fc002018-02-09 17:42:00 -0800113 mSystemInterface = systemInterface;
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700114 mHal = new VehicleHal(vehicle);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700115 mVehicleInterfaceName = vehicleInterfaceName;
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800116 mUserManagerHelper = new CarUserManagerHelper(serviceContext);
Keun-young Park4727da32016-05-31 10:00:51 -0700117 mSystemActivityMonitoringService = new SystemActivityMonitoringService(serviceContext);
Steve Paik388d7772018-02-12 10:54:51 -0800118 mCarPowerManagementService = new CarPowerManagementService(mContext, mHal.getPowerHal(),
119 systemInterface);
Steve Paik9ec53d72018-04-27 13:28:31 -0700120 mCarPropertyService = new CarPropertyService(serviceContext, mHal.getPropertyHal());
121 mCarDrivingStateService = new CarDrivingStateService(serviceContext, mCarPropertyService);
Ram Periathiruvadi4526a432018-01-24 13:00:54 -0800122 mCarUXRestrictionsService = new CarUxRestrictionsManagerService(serviceContext,
Yao, Yuxing0811d5a2018-11-29 16:18:53 -0800123 mCarDrivingStateService, mCarPropertyService, mUserManagerHelper);
Ram Periathiruvadi2da6d0e2018-01-26 18:02:10 -0800124 mCarPackageManagerService = new CarPackageManagerService(serviceContext,
125 mCarUXRestrictionsService,
Keun-young Park4727da32016-05-31 10:00:51 -0700126 mSystemActivityMonitoringService);
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700127 mCarInputService = new CarInputService(serviceContext, mHal.getInputHal());
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800128 mCarProjectionService = new CarProjectionService(serviceContext, mCarInputService);
Serik Beketayevc6ab8be2018-08-28 21:20:53 -0700129 mGarageModeService = new GarageModeService(mContext);
Vitalii Tomkiv8c7f2972016-07-11 15:42:04 -0700130 mAppFocusService = new AppFocusService(serviceContext, mSystemActivityMonitoringService);
Hongwei Wang30557232018-01-02 10:25:08 -0800131 mCarAudioService = new CarAudioService(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700132 mCarNightService = new CarNightService(serviceContext, mCarPropertyService);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700133 mInstrumentClusterService = new InstrumentClusterService(serviceContext,
Pavel Maltsev03cf60c2016-06-27 15:11:51 -0700134 mAppFocusService, mCarInputService);
Serik Beketayevf9f2ef02018-09-06 12:37:02 -0700135 mSystemStateControllerService = new SystemStateControllerService(
136 serviceContext, mCarAudioService, this);
Ram Periathiruvadiacb60242017-04-13 16:19:09 -0700137 mPerUserCarServiceHelper = new PerUserCarServiceHelper(serviceContext);
Steve Paik9ec53d72018-04-27 13:28:31 -0700138 mCarBluetoothService = new CarBluetoothService(serviceContext, mCarPropertyService,
139 mPerUserCarServiceHelper, mCarUXRestrictionsService);
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700140 mVmsSubscriberService = new VmsSubscriberService(serviceContext, mHal.getVmsHal());
141 mVmsPublisherService = new VmsPublisherService(serviceContext, mHal.getVmsHal());
Enrico Granatab3634e22017-05-05 18:02:31 -0700142 mCarDiagnosticService = new CarDiagnosticService(serviceContext, mHal.getDiagnosticHal());
Enrico Granata517a1e02017-09-20 16:15:50 -0700143 mCarStorageMonitoringService = new CarStorageMonitoringService(serviceContext,
144 systemInterface);
Anthony Chen12aec302018-04-25 16:41:48 -0700145 mCarConfigurationService =
146 new CarConfigurationService(serviceContext, new JsonReaderImpl());
Serik Beketayev68920542018-09-06 13:29:58 -0700147 mCarLocationService = new CarLocationService(
148 mContext, mCarPropertyService, mUserManagerHelper);
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800149 mCarTrustAgentEnrollmentService = new CarTrustAgentEnrollmentService(serviceContext);
keunyounga74b9ca2015-10-21 13:33:58 -0700150
keunyounga3b28d82015-08-25 13:05:15 -0700151 // Be careful with order. Service depending on other service should be inited later.
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700152 List<CarServiceBase> allServices = new ArrayList<>();
153 allServices.add(mSystemActivityMonitoringService);
154 allServices.add(mCarPowerManagementService);
155 allServices.add(mCarPropertyService);
156 allServices.add(mCarDrivingStateService);
157 allServices.add(mCarUXRestrictionsService);
158 allServices.add(mCarPackageManagerService);
159 allServices.add(mCarInputService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700160 allServices.add(mGarageModeService);
161 allServices.add(mAppFocusService);
162 allServices.add(mCarAudioService);
163 allServices.add(mCarNightService);
164 allServices.add(mInstrumentClusterService);
165 allServices.add(mCarProjectionService);
166 allServices.add(mSystemStateControllerService);
167 allServices.add(mCarBluetoothService);
168 allServices.add(mCarDiagnosticService);
169 allServices.add(mPerUserCarServiceHelper);
170 allServices.add(mCarStorageMonitoringService);
171 allServices.add(mCarConfigurationService);
172 allServices.add(mVmsSubscriberService);
173 allServices.add(mVmsPublisherService);
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800174 allServices.add(mCarTrustAgentEnrollmentService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700175 if (mUserManagerHelper.isHeadlessSystemUser()) {
kevinjm55822c42018-08-15 11:26:00 -0700176 allServices.add(new CarUserService(serviceContext, mUserManagerHelper));
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700177 }
Gregory Clarka63ba022018-06-07 16:42:12 -0700178 allServices.add(mCarLocationService);
Ying Zheng86d1c3e2018-05-11 11:42:48 -0700179 mAllServices = allServices.toArray(new CarServiceBase[allServices.size()]);
keunyoungca515072015-07-10 12:21:47 -0700180 }
181
Pavel Maltsevabd47232017-10-10 16:54:57 -0700182 @MainThread
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700183 void init() {
Serik Beketayev74debf22018-10-04 12:18:09 -0700184 mBootTiming = new TimingsTraceLog(VHAL_TIMING_TAG, Trace.TRACE_TAG_HAL);
Steve Paikc302c7c2017-08-04 14:01:58 -0700185 traceBegin("VehicleHal.init");
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700186 mHal.init();
Steve Paikc302c7c2017-08-04 14:01:58 -0700187 traceEnd();
188 traceBegin("CarService.initAllServices");
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800189 for (CarServiceBase service : mAllServices) {
keunyounga3b28d82015-08-25 13:05:15 -0700190 service.init();
191 }
Steve Paikc302c7c2017-08-04 14:01:58 -0700192 traceEnd();
keunyoungca515072015-07-10 12:21:47 -0700193 }
194
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700195 void release() {
keunyounga3b28d82015-08-25 13:05:15 -0700196 // release done in opposite order from init
197 for (int i = mAllServices.length - 1; i >= 0; i--) {
198 mAllServices[i].release();
199 }
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700200 mHal.release();
keunyoung1ab8e182015-09-24 09:25:22 -0700201 }
202
Pavel Maltsev8edd2552017-09-18 14:52:16 -0700203 void vehicleHalReconnected(IVehicle vehicle) {
Pavel Maltsevec83b632017-01-05 15:10:55 -0800204 mHal.vehicleHalReconnected(vehicle);
205 for (CarServiceBase service : mAllServices) {
206 service.vehicleHalReconnected();
207 }
208 }
209
keunyoungca515072015-07-10 12:21:47 -0700210 @Override
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700211 public void setCarServiceHelper(IBinder helper) {
212 int uid = Binder.getCallingUid();
213 if (uid != Process.SYSTEM_UID) {
214 throw new SecurityException("Only allowed from system");
215 }
216 synchronized (this) {
217 mICarServiceHelper = ICarServiceHelper.Stub.asInterface(helper);
Steve Paik0f9fc002018-02-09 17:42:00 -0800218 mSystemInterface.setCarServiceHelper(mICarServiceHelper);
Keun-young Parkaab47cd2017-06-05 11:04:50 -0700219 }
220 }
221
222 @Override
keunyoungca515072015-07-10 12:21:47 -0700223 public IBinder getCarService(String serviceName) {
224 switch (serviceName) {
Keun-young Park5672e852016-02-09 19:53:48 -0800225 case Car.AUDIO_SERVICE:
226 return mCarAudioService;
Vitalii Tomkiv46371472016-05-23 16:55:22 -0700227 case Car.APP_FOCUS_SERVICE:
228 return mAppFocusService;
Keun-young Park45fdcba2015-12-08 11:38:58 -0800229 case Car.PACKAGE_SERVICE:
230 return mCarPackageManagerService;
Enrico Granata5c56d2a2017-02-07 15:38:12 -0800231 case Car.DIAGNOSTIC_SERVICE:
Enrico Granatab3634e22017-05-05 18:02:31 -0700232 assertAnyDiagnosticPermission(mContext);
233 return mCarDiagnosticService;
Steve Paik388d7772018-02-12 10:54:51 -0800234 case Car.POWER_SERVICE:
235 assertPowerPermission(mContext);
236 return mCarPowerManagementService;
Steve Paik9ec53d72018-04-27 13:28:31 -0700237 case Car.CABIN_SERVICE:
238 case Car.HVAC_SERVICE:
239 case Car.INFO_SERVICE:
240 case Car.PROPERTY_SERVICE:
241 case Car.SENSOR_SERVICE:
242 case Car.VENDOR_EXTENSION_SERVICE:
243 return mCarPropertyService;
Pavel Maltsev7a948e52016-02-02 23:30:14 -0800244 case Car.CAR_NAVIGATION_SERVICE:
Keun-young Parke31a8b22016-03-16 17:34:08 -0700245 assertNavigationManagerPermission(mContext);
Pavel Maltsev0477e292016-05-27 12:22:36 -0700246 IInstrumentClusterNavigation navService =
247 mInstrumentClusterService.getNavigationService();
248 return navService == null ? null : navService.asBinder();
Pavel Maltsev905968c2017-07-16 19:48:57 -0700249 case Car.CAR_INSTRUMENT_CLUSTER_SERVICE:
250 assertClusterManagerPermission(mContext);
251 return mInstrumentClusterService.getManagerService();
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800252 case Car.PROJECTION_SERVICE:
253 assertProjectionPermission(mContext);
254 return mCarProjectionService;
Antonio Cortese4619c72017-02-02 07:53:27 -0800255 case Car.VMS_SUBSCRIBER_SERVICE:
Asaf Rosenfeld39e4f032017-09-16 11:33:50 -0700256 assertVmsSubscriberPermission(mContext);
257 return mVmsSubscriberService;
Keun-young Parke54ac272016-02-16 19:02:18 -0800258 case Car.TEST_SERVICE: {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700259 assertPermission(mContext, Car.PERMISSION_CAR_TEST_SERVICE);
keunyoung1ab8e182015-09-24 09:25:22 -0700260 synchronized (this) {
261 if (mCarTestService == null) {
262 mCarTestService = new CarTestService(mContext, this);
263 }
264 return mCarTestService;
265 }
266 }
Ram Periathiruvadi76a84892017-07-27 18:10:35 -0700267 case Car.BLUETOOTH_SERVICE:
268 return mCarBluetoothService;
Enrico Granata9a916d72017-09-19 14:33:08 -0700269 case Car.STORAGE_MONITORING_SERVICE:
Enrico Granata1172f882017-09-21 14:51:30 -0700270 assertPermission(mContext, Car.PERMISSION_STORAGE_MONITORING);
Enrico Granata9a916d72017-09-19 14:33:08 -0700271 return mCarStorageMonitoringService;
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800272 case Car.CAR_DRIVING_STATE_SERVICE:
273 assertDrivingStatePermission(mContext);
274 return mCarDrivingStateService;
275 case Car.CAR_UX_RESTRICTION_SERVICE:
276 return mCarUXRestrictionsService;
Anthony Chend4203d82018-05-16 16:21:52 -0700277 case Car.CAR_CONFIGURATION_SERVICE:
278 return mCarConfigurationService;
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800279 case Car.CAR_TRUST_AGENT_ENROLLMENT_SERVICE:
280 assertTrustAgentEnrollmentPermission(mContext);
281 return mCarTrustAgentEnrollmentService;
keunyoungca515072015-07-10 12:21:47 -0700282 default:
283 Log.w(CarLog.TAG_SERVICE, "getCarService for unknown service:" + serviceName);
284 return null;
285 }
286 }
287
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800288 @Override
289 public int getCarConnectionType() {
Pavel Maltsev0d07c762016-11-03 16:40:15 -0700290 return Car.CONNECTION_TYPE_EMBEDDED;
Pavel Maltsev1ecdd6c2016-03-02 16:33:44 -0800291 }
292
Keun-young Parka28d7b22016-02-29 16:54:29 -0800293 public CarServiceBase getCarInternalService(String serviceName) {
294 switch (serviceName) {
295 case INTERNAL_INPUT_SERVICE:
296 return mCarInputService;
Keun-young Park4727da32016-05-31 10:00:51 -0700297 case INTERNAL_SYSTEM_ACTIVITY_MONITORING_SERVICE:
298 return mSystemActivityMonitoringService;
Keun-young Parka28d7b22016-02-29 16:54:29 -0800299 default:
300 Log.w(CarLog.TAG_SERVICE, "getCarInternalService for unknown service:" +
301 serviceName);
302 return null;
303 }
304 }
305
keunyoung1ab8e182015-09-24 09:25:22 -0700306 public static void assertVehicleHalMockPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700307 assertPermission(context, Car.PERMISSION_MOCK_VEHICLE_HAL);
keunyoung1ab8e182015-09-24 09:25:22 -0700308 }
309
Keun-young Parke31a8b22016-03-16 17:34:08 -0700310 public static void assertNavigationManagerPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700311 assertPermission(context, Car.PERMISSION_CAR_NAVIGATION_MANAGER);
Pavel Maltsevce4ffd92016-03-09 10:56:23 -0800312 }
313
Pavel Maltsev905968c2017-07-16 19:48:57 -0700314 public static void assertClusterManagerPermission(Context context) {
315 assertPermission(context, Car.PERMISSION_CAR_INSTRUMENT_CLUSTER_CONTROL);
316 }
317
Steve Paik388d7772018-02-12 10:54:51 -0800318 public static void assertPowerPermission(Context context) {
319 assertPermission(context, Car.PERMISSION_CAR_POWER);
320 }
321
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800322 public static void assertProjectionPermission(Context context) {
Steve Paik461ecc62016-06-08 15:28:32 -0700323 assertPermission(context, Car.PERMISSION_CAR_PROJECTION);
324 }
325
Enrico Granata3c7a6662017-02-23 18:07:59 -0800326 public static void assertAnyDiagnosticPermission(Context context) {
327 assertAnyPermission(context,
Enrico Granataae25b922017-07-07 11:28:15 -0700328 Car.PERMISSION_CAR_DIAGNOSTIC_READ_ALL,
Enrico Granata3c7a6662017-02-23 18:07:59 -0800329 Car.PERMISSION_CAR_DIAGNOSTIC_CLEAR);
330 }
331
Ram Periathiruvadi25c16f12017-11-17 16:48:37 -0800332 public static void assertDrivingStatePermission(Context context) {
333 assertPermission(context, Car.PERMISSION_CAR_DRIVING_STATE);
334 }
335
Antonio Cortes6b3544c2017-02-06 16:54:58 -0800336 public static void assertVmsPublisherPermission(Context context) {
337 assertPermission(context, Car.PERMISSION_VMS_PUBLISHER);
338 }
339
Antonio Cortese4619c72017-02-02 07:53:27 -0800340 public static void assertVmsSubscriberPermission(Context context) {
341 assertPermission(context, Car.PERMISSION_VMS_SUBSCRIBER);
342 }
343
Ram Periathiruvadi194ce1b2019-01-14 09:42:12 -0800344 /**
345 * Ensures the caller has the permission to enroll a Trust Agent.
346 * @param context
347 */
348 public static void assertTrustAgentEnrollmentPermission(Context context) {
349 assertPermission(context, Car.PERMISSION_CAR_ENROLL_TRUST);
350 }
351
Steve Paik461ecc62016-06-08 15:28:32 -0700352 public static void assertPermission(Context context, String permission) {
353 if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
354 throw new SecurityException("requires " + permission);
Vitalii Tomkiv6e5ee612016-03-09 14:57:32 -0800355 }
356 }
357
Steve Paik9ec53d72018-04-27 13:28:31 -0700358 /**
359 * Checks to see if the caller has a permission.
360 * @param context
361 * @param permission
362 *
363 * @return boolean TRUE if caller has the permission.
364 */
365 public static boolean hasPermission(Context context, String permission) {
366 return context.checkCallingOrSelfPermission(permission)
367 == PackageManager.PERMISSION_GRANTED;
368 }
369
Enrico Granata3c7a6662017-02-23 18:07:59 -0800370 public static void assertAnyPermission(Context context, String... permissions) {
371 for (String permission : permissions) {
372 if (context.checkCallingOrSelfPermission(permission) ==
373 PackageManager.PERMISSION_GRANTED) {
374 return;
375 }
376 }
377 throw new SecurityException("requires any of " + Arrays.toString(permissions));
378 }
379
Enrico Granatae8056ca2018-04-03 13:19:52 -0700380 @Override
381 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
382 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
Anthony Chen12aec302018-04-25 16:41:48 -0700383 != PackageManager.PERMISSION_GRANTED) {
Enrico Granatae8056ca2018-04-03 13:19:52 -0700384 writer.println("Permission Denial: can't dump CarService from from pid="
Anthony Chen12aec302018-04-25 16:41:48 -0700385 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()
386 + " without permission " + android.Manifest.permission.DUMP);
Enrico Granatae8056ca2018-04-03 13:19:52 -0700387 return;
keunyounga3b28d82015-08-25 13:05:15 -0700388 }
Enrico Granatae8056ca2018-04-03 13:19:52 -0700389 if (args == null || args.length == 0) {
390 writer.println("*dump car service*");
391
392 writer.println("*FutureConfig, DEFAULT:" + FeatureConfiguration.DEFAULT);
393 writer.println("*Dump all services*");
394 for (CarServiceBase service : mAllServices) {
395 dumpService(service, writer);
396 }
397 if (mCarTestService != null) {
398 dumpService(mCarTestService, writer);
399 }
400 writer.println("*Dump Vehicle HAL*");
401 writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName);
402 try {
403 // TODO dump all feature flags by creating a dumpable interface
404 mHal.dump(writer);
405 } catch (Exception e) {
406 writer.println("Failed dumping: " + mHal.getClass().getName());
407 e.printStackTrace(writer);
408 }
409 } else if (Build.IS_USERDEBUG || Build.IS_ENG) {
410 execShellCmd(args, writer);
411 } else {
412 writer.println("Commands not supported in " + Build.TYPE);
Brad Stenninged3d59b2017-11-09 16:15:17 -0800413 }
414 }
415
416 private void dumpService(CarServiceBase service, PrintWriter writer) {
417 try {
418 service.dump(writer);
419 } catch (Exception e) {
420 writer.println("Failed dumping: " + service.getClass().getName());
421 e.printStackTrace(writer);
422 }
keunyoungcc449f72015-08-12 10:46:27 -0700423 }
Yao Chene33f07e2016-07-26 12:02:51 -0700424
425 void execShellCmd(String[] args, PrintWriter writer) {
426 new CarShellCommand().exec(args, writer);
427 }
428
Pavel Maltsevabd47232017-10-10 16:54:57 -0700429 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700430 private void traceBegin(String name) {
Steve Paikc302c7c2017-08-04 14:01:58 -0700431 Slog.i(TAG, name);
432 mBootTiming.traceBegin(name);
433 }
434
Pavel Maltsevabd47232017-10-10 16:54:57 -0700435 @MainThread
Serik Beketayev74debf22018-10-04 12:18:09 -0700436 private void traceEnd() {
Steve Paikc302c7c2017-08-04 14:01:58 -0700437 mBootTiming.traceEnd();
438 }
439
Yao Chene33f07e2016-07-26 12:02:51 -0700440 private class CarShellCommand {
441 private static final String COMMAND_HELP = "-h";
442 private static final String COMMAND_DAY_NIGHT_MODE = "day-night-mode";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800443 private static final String COMMAND_INJECT_VHAL_EVENT = "inject-vhal-event";
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800444 private static final String COMMAND_ENABLE_UXR = "enable-uxr";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800445 private static final String COMMAND_GARAGE_MODE = "garage-mode";
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800446 private static final String COMMAND_GET_DO_ACTIVITIES = "get-do-activities";
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800447
Yao Chene33f07e2016-07-26 12:02:51 -0700448 private static final String PARAM_DAY_MODE = "day";
449 private static final String PARAM_NIGHT_MODE = "night";
450 private static final String PARAM_SENSOR_MODE = "sensor";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800451 private static final String PARAM_VEHICLE_PROPERTY_AREA_GLOBAL = "0";
Bryan Eylera32a7c12018-02-27 15:40:00 -0800452 private static final String PARAM_ON_MODE = "on";
453 private static final String PARAM_OFF_MODE = "off";
454 private static final String PARAM_QUERY_MODE = "query";
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800455
Yao Chene33f07e2016-07-26 12:02:51 -0700456
457 private void dumpHelp(PrintWriter pw) {
458 pw.println("Car service commands:");
459 pw.println("\t-h");
460 pw.println("\t Print this help text.");
461 pw.println("\tday-night-mode [day|night|sensor]");
462 pw.println("\t Force into day/night mode or restore to auto.");
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800463 pw.println("\tinject-vhal-event property [zone] data(can be comma separated list)");
464 pw.println("\t Inject a vehicle property for testing");
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800465 pw.println("\tdisable-uxr true|false");
466 pw.println("\t Disable UX restrictions and App blocking.");
Bryan Eylera32a7c12018-02-27 15:40:00 -0800467 pw.println("\tgarage-mode [on|off|query]");
468 pw.println("\t Force into garage mode or check status.");
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800469 pw.println("\tget-do-activities pkgname");
470 pw.println("\t Get Distraction Optimized activities in given package");
Yao Chene33f07e2016-07-26 12:02:51 -0700471 }
472
473 public void exec(String[] args, PrintWriter writer) {
474 String arg = args[0];
475 switch (arg) {
476 case COMMAND_HELP:
477 dumpHelp(writer);
478 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800479 case COMMAND_DAY_NIGHT_MODE: {
Yao Chene33f07e2016-07-26 12:02:51 -0700480 String value = args.length < 1 ? "" : args[1];
481 forceDayNightMode(value, writer);
482 break;
Bryan Eylera32a7c12018-02-27 15:40:00 -0800483 }
484 case COMMAND_GARAGE_MODE: {
485 String value = args.length < 1 ? "" : args[1];
486 forceGarageMode(value, writer);
487 break;
488 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800489 case COMMAND_INJECT_VHAL_EVENT:
490 String zone = PARAM_VEHICLE_PROPERTY_AREA_GLOBAL;
491 String data;
492 if (args.length < 3) {
493 writer.println("Incorrect number of arguments.");
494 dumpHelp(writer);
495 break;
496 } else if (args.length > 3) {
497 // Zoned
498 zone = args[2];
499 data = args[3];
500 } else {
501 // Global
502 data = args[2];
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800503 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800504 injectVhalEvent(args[1], zone, data, writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800505 break;
Ram Periathiruvadibf0eab72018-02-06 12:32:43 -0800506 case COMMAND_ENABLE_UXR:
507 if (args.length < 2) {
508 writer.println("Incorrect number of arguments");
509 dumpHelp(writer);
510 break;
511 }
512 boolean enableBlocking = Boolean.valueOf(args[1]);
513 if (mCarPackageManagerService != null) {
514 mCarPackageManagerService.setEnableActivityBlocking(enableBlocking);
515 }
516 break;
Ram Periathiruvadi38388302018-02-22 16:42:49 -0800517 case COMMAND_GET_DO_ACTIVITIES:
518 if (args.length < 2) {
519 writer.println("Incorrect number of arguments");
520 dumpHelp(writer);
521 break;
522 }
523 String pkgName = args[1].toLowerCase();
524 if (mCarPackageManagerService != null) {
525 String[] doActivities =
526 mCarPackageManagerService.getDistractionOptimizedActivities(
527 pkgName);
528 if (doActivities != null) {
529 writer.println("DO Activities for " + pkgName);
530 for (String a : doActivities) {
531 writer.println(a);
532 }
533 } else {
534 writer.println("No DO Activities for " + pkgName);
535 }
536 }
537 break;
Yao Chene33f07e2016-07-26 12:02:51 -0700538 default:
539 writer.println("Unknown command.");
540 dumpHelp(writer);
541 }
542 }
543
544 private void forceDayNightMode(String arg, PrintWriter writer) {
545 int mode;
546 switch (arg) {
547 case PARAM_DAY_MODE:
548 mode = CarNightService.FORCED_DAY_MODE;
549 break;
550 case PARAM_NIGHT_MODE:
551 mode = CarNightService.FORCED_NIGHT_MODE;
552 break;
553 case PARAM_SENSOR_MODE:
554 mode = CarNightService.FORCED_SENSOR_MODE;
555 break;
556 default:
557 writer.println("Unknown value. Valid argument: " + PARAM_DAY_MODE + "|"
558 + PARAM_NIGHT_MODE + "|" + PARAM_SENSOR_MODE);
559 return;
560 }
561 int current = mCarNightService.forceDayNightMode(mode);
562 String currentMode = null;
563 switch (current) {
564 case UiModeManager.MODE_NIGHT_AUTO:
565 currentMode = PARAM_SENSOR_MODE;
566 break;
567 case UiModeManager.MODE_NIGHT_YES:
568 currentMode = PARAM_NIGHT_MODE;
569 break;
570 case UiModeManager.MODE_NIGHT_NO:
571 currentMode = PARAM_DAY_MODE;
572 break;
573 }
574 writer.println("DayNightMode changed to: " + currentMode);
575 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800576
Bryan Eylera32a7c12018-02-27 15:40:00 -0800577 private void forceGarageMode(String arg, PrintWriter writer) {
578 switch (arg) {
579 case PARAM_ON_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700580 mGarageModeService.forceStartGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800581 break;
582 case PARAM_OFF_MODE:
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700583 mGarageModeService.stopAndResetGarageMode();
Bryan Eylera32a7c12018-02-27 15:40:00 -0800584 break;
585 case PARAM_QUERY_MODE:
586 // Nothing to do. Always query at the end anyway.
587 break;
588 default:
589 writer.println("Unknown value. Valid argument: " + PARAM_ON_MODE + "|"
590 + PARAM_OFF_MODE + "|" + PARAM_QUERY_MODE);
591 return;
592 }
Serik Beketayeve0e7ff22018-07-31 21:04:12 -0700593 writer.println("Garage mode: " + mGarageModeService.isGarageModeActive());
Bryan Eylera32a7c12018-02-27 15:40:00 -0800594 }
595
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800596 /**
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800597 * Inject a fake VHAL event
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800598 *
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800599 * @param property the Vehicle property Id as defined in the HAL
600 * @param zone Zone that this event services
601 * @param value Data value of the event
602 * @param writer PrintWriter
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800603 */
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800604 private void injectVhalEvent(String property, String zone, String value,
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800605 PrintWriter writer) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800606 if (zone != null && (zone.equalsIgnoreCase(PARAM_VEHICLE_PROPERTY_AREA_GLOBAL))) {
607 if (!isPropertyAreaTypeGlobal(property)) {
608 writer.println("Property area type inconsistent with given zone");
609 return;
610 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800611 }
612 try {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800613 mHal.injectVhalEvent(property, zone, value);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800614 } catch (NumberFormatException e) {
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800615 writer.println("Invalid property Id zone Id or value" + e);
616 dumpHelp(writer);
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800617 }
Ram Periathiruvadiee28c002017-02-07 21:35:01 -0800618 }
619
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800620 // Check if the given property is global
621 private boolean isPropertyAreaTypeGlobal(String property) {
622 if (property == null) {
623 return false;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800624 }
Ram Periathiruvadi5cd00cb2018-02-12 13:49:50 -0800625 return (Integer.decode(property) & VehicleArea.MASK) == VehicleArea.GLOBAL;
Ram Periathiruvadi49d5a5a2017-02-17 18:50:09 -0800626 }
Yao Chene33f07e2016-07-26 12:02:51 -0700627 }
kevinjm55822c42018-08-15 11:26:00 -0700628}