Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | package com.android.car; |
| 18 | |
| 19 | import android.annotation.Nullable; |
Yao, Yuxing | e32b3f6 | 2019-05-22 12:36:24 -0700 | [diff] [blame] | 20 | import android.car.Car; |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 21 | import android.car.VehicleAreaType; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 22 | import android.car.drivingstate.CarDrivingStateEvent; |
| 23 | import android.car.drivingstate.CarDrivingStateEvent.CarDrivingState; |
| 24 | import android.car.drivingstate.ICarDrivingState; |
| 25 | import android.car.drivingstate.ICarDrivingStateChangeListener; |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 26 | import android.car.hardware.CarPropertyConfig; |
| 27 | import android.car.hardware.CarPropertyValue; |
| 28 | import android.car.hardware.property.CarPropertyEvent; |
| 29 | import android.car.hardware.property.ICarPropertyEventListener; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 30 | import android.content.Context; |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 31 | import android.hardware.automotive.vehicle.V2_0.VehicleGear; |
| 32 | import android.hardware.automotive.vehicle.V2_0.VehicleProperty; |
Jordan Jozwiak | 39da9c8 | 2019-10-25 10:46:38 -0700 | [diff] [blame] | 33 | import android.os.Handler; |
| 34 | import android.os.HandlerThread; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 35 | import android.os.RemoteCallbackList; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 36 | import android.os.RemoteException; |
| 37 | import android.os.SystemClock; |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 38 | import android.util.IndentingPrintWriter; |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 39 | import android.util.Slog; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 40 | |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 41 | import com.android.internal.annotations.GuardedBy; |
Jordan Jozwiak | 6e92300 | 2019-10-22 10:50:02 -0700 | [diff] [blame] | 42 | import com.android.internal.annotations.VisibleForTesting; |
| 43 | |
Ram Periathiruvadi | 22c0d68 | 2018-05-10 17:50:48 -0700 | [diff] [blame] | 44 | import java.util.LinkedList; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 45 | import java.util.List; |
| 46 | |
| 47 | /** |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 48 | * A service that infers the current driving state of the vehicle. It computes the driving state |
| 49 | * from listening to relevant properties from {@link CarPropertyService} |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 50 | */ |
| 51 | public class CarDrivingStateService extends ICarDrivingState.Stub implements CarServiceBase { |
Mayank Garg | 72c71d2 | 2021-02-03 23:54:45 -0800 | [diff] [blame] | 52 | private static final String TAG = CarLog.tagFor(CarDrivingStateService.class); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 53 | private static final boolean DBG = false; |
Ram Periathiruvadi | 22c0d68 | 2018-05-10 17:50:48 -0700 | [diff] [blame] | 54 | private static final int MAX_TRANSITION_LOG_SIZE = 20; |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 55 | private static final int PROPERTY_UPDATE_RATE = 5; // Update rate in Hz |
| 56 | private static final int NOT_RECEIVED = -1; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 57 | private final Context mContext; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 58 | private final CarPropertyService mPropertyService; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 59 | // List of clients listening to driving state events. |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 60 | private final RemoteCallbackList<ICarDrivingStateChangeListener> mDrivingStateClients = |
| 61 | new RemoteCallbackList<>(); |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 62 | // Array of properties that the service needs to listen to from CarPropertyService for deriving |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 63 | // the driving state. |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 64 | private static final int[] REQUIRED_PROPERTIES = { |
| 65 | VehicleProperty.PERF_VEHICLE_SPEED, |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 66 | VehicleProperty.GEAR_SELECTION, |
| 67 | VehicleProperty.PARKING_BRAKE_ON}; |
Keun young Park | b241d02 | 2020-04-20 20:31:34 -0700 | [diff] [blame] | 68 | private final HandlerThread mClientDispatchThread = CarServiceUtils.getHandlerThread( |
| 69 | getClass().getSimpleName()); |
| 70 | private final Handler mClientDispatchHandler = new Handler(mClientDispatchThread.getLooper()); |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 71 | private final Object mLock = new Object(); |
| 72 | |
Ram Periathiruvadi | 22c0d68 | 2018-05-10 17:50:48 -0700 | [diff] [blame] | 73 | // For dumpsys logging |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 74 | @GuardedBy("mLock") |
Ram Periathiruvadi | 22c0d68 | 2018-05-10 17:50:48 -0700 | [diff] [blame] | 75 | private final LinkedList<Utils.TransitionLog> mTransitionLogs = new LinkedList<>(); |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 76 | |
| 77 | @GuardedBy("mLock") |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 78 | private int mLastGear; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 79 | |
| 80 | @GuardedBy("mLock") |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 81 | private long mLastGearTimestamp = NOT_RECEIVED; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 82 | |
| 83 | @GuardedBy("mLock") |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 84 | private float mLastSpeed; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 85 | |
| 86 | @GuardedBy("mLock") |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 87 | private long mLastSpeedTimestamp = NOT_RECEIVED; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 88 | |
| 89 | @GuardedBy("mLock") |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 90 | private boolean mLastParkingBrakeState; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 91 | |
| 92 | @GuardedBy("mLock") |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 93 | private long mLastParkingBrakeTimestamp = NOT_RECEIVED; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 94 | |
| 95 | @GuardedBy("mLock") |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 96 | private List<Integer> mSupportedGears; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 97 | |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 98 | @GuardedBy("mLock") |
| 99 | private CarDrivingStateEvent mCurrentDrivingState; |
| 100 | |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 101 | public CarDrivingStateService(Context context, CarPropertyService propertyService) { |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 102 | mContext = context; |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 103 | mPropertyService = propertyService; |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 104 | mCurrentDrivingState = createDrivingStateEvent(CarDrivingStateEvent.DRIVING_STATE_UNKNOWN); |
| 105 | } |
| 106 | |
| 107 | @Override |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 108 | public void init() { |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 109 | if (!checkPropertySupport()) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 110 | Slog.e(TAG, "init failure. Driving state will always be fully restrictive"); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 111 | return; |
| 112 | } |
Kai | 2cb9c47 | 2021-03-04 21:19:22 -0800 | [diff] [blame] | 113 | // Gets the boot state first, before getting any events from car. |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 114 | synchronized (mLock) { |
| 115 | mCurrentDrivingState = createDrivingStateEvent(inferDrivingStateLocked()); |
| 116 | addTransitionLogLocked(TAG + " Boot", CarDrivingStateEvent.DRIVING_STATE_UNKNOWN, |
| 117 | mCurrentDrivingState.eventValue, mCurrentDrivingState.timeStamp); |
| 118 | } |
Kai | 2cb9c47 | 2021-03-04 21:19:22 -0800 | [diff] [blame] | 119 | subscribeToProperties(); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 123 | public void release() { |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 124 | for (int property : REQUIRED_PROPERTIES) { |
| 125 | mPropertyService.unregisterListener(property, mICarPropertyEventListener); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 126 | } |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 127 | while (mDrivingStateClients.getRegisteredCallbackCount() > 0) { |
| 128 | for (int i = mDrivingStateClients.getRegisteredCallbackCount() - 1; i >= 0; i--) { |
| 129 | ICarDrivingStateChangeListener client = |
| 130 | mDrivingStateClients.getRegisteredCallbackItem(i); |
| 131 | if (client == null) { |
| 132 | continue; |
| 133 | } |
| 134 | mDrivingStateClients.unregister(client); |
| 135 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 136 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 137 | mCurrentDrivingState = createDrivingStateEvent(CarDrivingStateEvent.DRIVING_STATE_UNKNOWN); |
| 138 | } |
| 139 | |
| 140 | /** |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 141 | * Checks if the {@link CarPropertyService} supports the required properties. |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 142 | * |
| 143 | * @return {@code true} if supported, {@code false} if not |
| 144 | */ |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 145 | private boolean checkPropertySupport() { |
Kai | 2cb9c47 | 2021-03-04 21:19:22 -0800 | [diff] [blame] | 146 | List<CarPropertyConfig> configs = mPropertyService |
| 147 | .getPropertyConfigList(REQUIRED_PROPERTIES); |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 148 | for (int propertyId : REQUIRED_PROPERTIES) { |
| 149 | boolean found = false; |
| 150 | for (CarPropertyConfig config : configs) { |
| 151 | if (config.getPropertyId() == propertyId) { |
| 152 | found = true; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | if (!found) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 157 | Slog.e(TAG, "Required property not supported: " + propertyId); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | /** |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 165 | * Subscribe to the {@link CarPropertyService} for required sensors. |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 166 | */ |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 167 | private void subscribeToProperties() { |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 168 | for (int propertyId : REQUIRED_PROPERTIES) { |
| 169 | mPropertyService.registerListener(propertyId, PROPERTY_UPDATE_RATE, |
| 170 | mICarPropertyEventListener); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | // Binder methods |
| 176 | |
| 177 | /** |
| 178 | * Register a {@link ICarDrivingStateChangeListener} to be notified for changes to the driving |
| 179 | * state. |
| 180 | * |
| 181 | * @param listener {@link ICarDrivingStateChangeListener} |
| 182 | */ |
| 183 | @Override |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 184 | public void registerDrivingStateChangeListener(ICarDrivingStateChangeListener listener) { |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 185 | if (listener == null) { |
| 186 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 187 | Slog.e(TAG, "registerDrivingStateChangeListener(): listener null"); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 188 | } |
| 189 | throw new IllegalArgumentException("Listener is null"); |
| 190 | } |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 191 | mDrivingStateClients.register(listener); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Unregister the given Driving State Change listener |
| 196 | * |
| 197 | * @param listener client to unregister |
| 198 | */ |
| 199 | @Override |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 200 | public void unregisterDrivingStateChangeListener(ICarDrivingStateChangeListener listener) { |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 201 | if (listener == null) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 202 | Slog.e(TAG, "unregisterDrivingStateChangeListener(): listener null"); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 203 | throw new IllegalArgumentException("Listener is null"); |
| 204 | } |
| 205 | |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 206 | mDrivingStateClients.unregister(listener); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Gets the current driving state |
| 211 | * |
| 212 | * @return {@link CarDrivingStateEvent} for the given event type |
| 213 | */ |
| 214 | @Override |
| 215 | @Nullable |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 216 | public CarDrivingStateEvent getCurrentDrivingState() { |
| 217 | synchronized (mLock) { |
| 218 | return mCurrentDrivingState; |
| 219 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Yao, Yuxing | e32b3f6 | 2019-05-22 12:36:24 -0700 | [diff] [blame] | 222 | @Override |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 223 | public void injectDrivingState(CarDrivingStateEvent event) { |
Yao, Yuxing | e32b3f6 | 2019-05-22 12:36:24 -0700 | [diff] [blame] | 224 | ICarImpl.assertPermission(mContext, Car.PERMISSION_CONTROL_APP_BLOCKING); |
| 225 | |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 226 | dispatchEventToClients(event); |
Yao, Yuxing | e32b3f6 | 2019-05-22 12:36:24 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 229 | private void dispatchEventToClients(CarDrivingStateEvent event) { |
| 230 | boolean success = mClientDispatchHandler.post(() -> { |
| 231 | int numClients = mDrivingStateClients.beginBroadcast(); |
| 232 | for (int i = 0; i < numClients; i++) { |
| 233 | ICarDrivingStateChangeListener callback = mDrivingStateClients.getBroadcastItem(i); |
| 234 | try { |
| 235 | callback.onDrivingStateChanged(event); |
| 236 | } catch (RemoteException e) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 237 | Slog.e(TAG, |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 238 | String.format("Dispatch to listener %s failed for event (%s)", callback, |
| 239 | event)); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 242 | mDrivingStateClients.finishBroadcast(); |
| 243 | }); |
| 244 | |
| 245 | if (!success) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 246 | Slog.e(TAG, "Unable to post (" + event + ") event to dispatch handler"); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
| 250 | @Override |
Felipe Leme | 176a5fd | 2021-01-20 15:48:33 -0800 | [diff] [blame] | 251 | public void dump(IndentingPrintWriter writer) { |
Jordan Jozwiak | 0aa0598 | 2019-06-17 15:14:39 -0700 | [diff] [blame] | 252 | writer.println("*CarDrivingStateService*"); |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 253 | mDrivingStateClients.dump(writer, "Driving State Clients "); |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 254 | writer.println("Driving state change log:"); |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 255 | synchronized (mLock) { |
| 256 | for (Utils.TransitionLog tLog : mTransitionLogs) { |
| 257 | writer.println(tLog); |
| 258 | } |
| 259 | writer.println("Current Driving State: " + mCurrentDrivingState.eventValue); |
| 260 | if (mSupportedGears != null) { |
| 261 | writer.println("Supported gears:"); |
| 262 | for (Integer gear : mSupportedGears) { |
| 263 | writer.print("Gear:" + gear); |
| 264 | } |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /** |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 270 | * {@link CarPropertyEvent} listener registered with the {@link CarPropertyService} for getting |
| 271 | * property change notifications. |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 272 | */ |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 273 | private final ICarPropertyEventListener mICarPropertyEventListener = |
| 274 | new ICarPropertyEventListener.Stub() { |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 275 | @Override |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 276 | public void onEvent(List<CarPropertyEvent> events) throws RemoteException { |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 277 | synchronized (mLock) { |
| 278 | for (CarPropertyEvent event : events) { |
| 279 | handlePropertyEventLocked(event); |
| 280 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | /** |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 286 | * Handle events coming from {@link CarPropertyService}. Compute the driving state, map it to |
| 287 | * the corresponding UX Restrictions and dispatch the events to the registered clients. |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 288 | */ |
Jordan Jozwiak | 6e92300 | 2019-10-22 10:50:02 -0700 | [diff] [blame] | 289 | @VisibleForTesting |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 290 | void handlePropertyEventLocked(CarPropertyEvent event) { |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 291 | if (event.getEventType() != CarPropertyEvent.PROPERTY_EVENT_PROPERTY_CHANGE) { |
| 292 | return; |
| 293 | } |
| 294 | CarPropertyValue value = event.getCarPropertyValue(); |
| 295 | int propId = value.getPropertyId(); |
| 296 | long curTimestamp = value.getTimestamp(); |
| 297 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 298 | Slog.d(TAG, "Property Changed: propId=" + propId); |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 299 | } |
| 300 | switch (propId) { |
| 301 | case VehicleProperty.PERF_VEHICLE_SPEED: |
| 302 | float curSpeed = (Float) value.getValue(); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 303 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 304 | Slog.d(TAG, "Speed: " + curSpeed + "@" + curTimestamp); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 305 | } |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 306 | if (curTimestamp > mLastSpeedTimestamp) { |
| 307 | mLastSpeedTimestamp = curTimestamp; |
| 308 | mLastSpeed = curSpeed; |
| 309 | } else if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 310 | Slog.d(TAG, "Ignoring speed with older timestamp:" + curTimestamp); |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 311 | } |
| 312 | break; |
| 313 | case VehicleProperty.GEAR_SELECTION: |
| 314 | if (mSupportedGears == null) { |
| 315 | mSupportedGears = getSupportedGears(); |
| 316 | } |
| 317 | int curGear = (Integer) value.getValue(); |
| 318 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 319 | Slog.d(TAG, "Gear: " + curGear + "@" + curTimestamp); |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 320 | } |
| 321 | if (curTimestamp > mLastGearTimestamp) { |
| 322 | mLastGearTimestamp = curTimestamp; |
| 323 | mLastGear = (Integer) value.getValue(); |
| 324 | } else if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 325 | Slog.d(TAG, "Ignoring Gear with older timestamp:" + curTimestamp); |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 326 | } |
| 327 | break; |
| 328 | case VehicleProperty.PARKING_BRAKE_ON: |
| 329 | boolean curParkingBrake = (boolean) value.getValue(); |
| 330 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 331 | Slog.d(TAG, "Parking Brake: " + curParkingBrake + "@" + curTimestamp); |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 332 | } |
| 333 | if (curTimestamp > mLastParkingBrakeTimestamp) { |
| 334 | mLastParkingBrakeTimestamp = curTimestamp; |
| 335 | mLastParkingBrakeState = curParkingBrake; |
| 336 | } else if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 337 | Slog.d(TAG, "Ignoring Parking Brake status with an older timestamp:" |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 338 | + curTimestamp); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 339 | } |
| 340 | break; |
| 341 | default: |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 342 | Slog.e(TAG, "Received property event for unhandled propId=" + propId); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 343 | break; |
| 344 | } |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 345 | |
| 346 | int drivingState = inferDrivingStateLocked(); |
| 347 | // Check if the driving state has changed. If it has, update our records and |
| 348 | // dispatch the new events to the listeners. |
| 349 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 350 | Slog.d(TAG, "Driving state new->old " + drivingState + "->" |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 351 | + mCurrentDrivingState.eventValue); |
| 352 | } |
| 353 | if (drivingState != mCurrentDrivingState.eventValue) { |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 354 | addTransitionLogLocked(TAG, mCurrentDrivingState.eventValue, drivingState, |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 355 | System.currentTimeMillis()); |
| 356 | // Update if there is a change in state. |
| 357 | mCurrentDrivingState = createDrivingStateEvent(drivingState); |
| 358 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 359 | Slog.d(TAG, "dispatching to " + mDrivingStateClients.getRegisteredCallbackCount() |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 360 | + " clients"); |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 361 | } |
Jordan Jozwiak | 39da9c8 | 2019-10-25 10:46:38 -0700 | [diff] [blame] | 362 | // Dispatch to clients on a separate thread to prevent a deadlock |
| 363 | final CarDrivingStateEvent currentDrivingStateEvent = mCurrentDrivingState; |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 364 | dispatchEventToClients(currentDrivingStateEvent); |
Yao, Yuxing | ba342ec | 2019-06-05 14:58:35 -0700 | [diff] [blame] | 365 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 368 | private List<Integer> getSupportedGears() { |
Kai | 2cb9c47 | 2021-03-04 21:19:22 -0800 | [diff] [blame] | 369 | List<CarPropertyConfig> propertyList = mPropertyService |
| 370 | .getPropertyConfigList(REQUIRED_PROPERTIES); |
| 371 | for (CarPropertyConfig p : propertyList) { |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 372 | if (p.getPropertyId() == VehicleProperty.GEAR_SELECTION) { |
| 373 | return p.getConfigArray(); |
| 374 | } |
| 375 | } |
| 376 | return null; |
| 377 | } |
| 378 | |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 379 | @GuardedBy("mLock") |
| 380 | private void addTransitionLogLocked(String name, int from, int to, long timestamp) { |
Ram Periathiruvadi | 22c0d68 | 2018-05-10 17:50:48 -0700 | [diff] [blame] | 381 | if (mTransitionLogs.size() >= MAX_TRANSITION_LOG_SIZE) { |
| 382 | mTransitionLogs.remove(); |
| 383 | } |
| 384 | |
| 385 | Utils.TransitionLog tLog = new Utils.TransitionLog(name, from, to, timestamp); |
| 386 | mTransitionLogs.add(tLog); |
| 387 | } |
| 388 | |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 389 | /** |
| 390 | * Infers the current driving state of the car from the other Car Sensor properties like |
| 391 | * Current Gear, Speed etc. |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 392 | * |
| 393 | * @return Current driving state |
| 394 | */ |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 395 | @GuardedBy("mLock") |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 396 | @CarDrivingState |
| 397 | private int inferDrivingStateLocked() { |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 398 | updateVehiclePropertiesIfNeededLocked(); |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 399 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 400 | Slog.d(TAG, "Last known Gear:" + mLastGear + " Last known speed:" + mLastSpeed); |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 401 | } |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | Logic to start off deriving driving state: |
| 405 | 1. If gear == parked, then Driving State is parked. |
| 406 | 2. If gear != parked, |
| 407 | 2a. if parking brake is applied, then Driving state is parked. |
| 408 | 2b. if parking brake is not applied or unknown/unavailable, then driving state |
| 409 | is still unknown. |
| 410 | 3. If driving state is unknown at the end of step 2, |
| 411 | 3a. if speed == 0, then driving state is idling |
| 412 | 3b. if speed != 0, then driving state is moving |
| 413 | 3c. if speed unavailable, then driving state is unknown |
| 414 | */ |
| 415 | |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 416 | if (isVehicleKnownToBeParkedLocked()) { |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 417 | return CarDrivingStateEvent.DRIVING_STATE_PARKED; |
| 418 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 419 | |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 420 | // We don't know if the vehicle is parked, let's look at the speed. |
| 421 | if (mLastSpeedTimestamp == NOT_RECEIVED || mLastSpeed < 0) { |
Steve Paik | 4d25702 | 2018-04-27 13:28:31 -0700 | [diff] [blame] | 422 | return CarDrivingStateEvent.DRIVING_STATE_UNKNOWN; |
| 423 | } else if (mLastSpeed == 0f) { |
| 424 | return CarDrivingStateEvent.DRIVING_STATE_IDLING; |
| 425 | } else { |
| 426 | return CarDrivingStateEvent.DRIVING_STATE_MOVING; |
| 427 | } |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 430 | /** |
| 431 | * Find if we have signals to know if the vehicle is parked |
| 432 | * |
| 433 | * @return true if we have enough information to say the vehicle is parked. |
| 434 | * false, if the vehicle is either not parked or if we don't have any information. |
| 435 | */ |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 436 | @GuardedBy("mLock") |
| 437 | private boolean isVehicleKnownToBeParkedLocked() { |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 438 | // If we know the gear is in park, return true |
| 439 | if (mLastGearTimestamp != NOT_RECEIVED && mLastGear == VehicleGear.GEAR_PARK) { |
| 440 | return true; |
| 441 | } else if (mLastParkingBrakeTimestamp != NOT_RECEIVED) { |
| 442 | // if gear is not in park or unknown, look for status of parking brake if transmission |
| 443 | // type is manual. |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 444 | if (isCarManualTransmissionTypeLocked()) { |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 445 | return mLastParkingBrakeState; |
| 446 | } |
| 447 | } |
| 448 | // if neither information is available, return false to indicate we can't determine |
| 449 | // if the vehicle is parked. |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * If Supported gears information is available and GEAR_PARK is not one of the supported gears, |
| 455 | * transmission type is considered to be Manual. Automatic transmission is assumed otherwise. |
| 456 | */ |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 457 | @GuardedBy("mLock") |
| 458 | private boolean isCarManualTransmissionTypeLocked() { |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 459 | if (mSupportedGears != null |
| 460 | && !mSupportedGears.isEmpty() |
| 461 | && !mSupportedGears.contains(VehicleGear.GEAR_PARK)) { |
| 462 | return true; |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Try querying the gear selection and parking brake if we haven't received the event yet. |
| 469 | * This could happen if the gear change occurred before car service booted up like in the |
| 470 | * case of a HU restart in the middle of a drive. Since gear and parking brake are |
| 471 | * on-change only properties, we could be in this situation where we will have to query |
| 472 | * VHAL. |
| 473 | */ |
Jordan Jozwiak | 05985a6 | 2020-02-27 10:58:11 -0800 | [diff] [blame] | 474 | @GuardedBy("mLock") |
| 475 | private void updateVehiclePropertiesIfNeededLocked() { |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 476 | if (mLastGearTimestamp == NOT_RECEIVED) { |
Kai Wang | 301dd47 | 2021-05-18 06:09:44 +0000 | [diff] [blame] | 477 | CarPropertyValue propertyValue = mPropertyService.getPropertySafe( |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 478 | VehicleProperty.GEAR_SELECTION, |
| 479 | VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL); |
| 480 | if (propertyValue != null) { |
| 481 | mLastGear = (Integer) propertyValue.getValue(); |
| 482 | mLastGearTimestamp = propertyValue.getTimestamp(); |
| 483 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 484 | Slog.d(TAG, "updateVehiclePropertiesIfNeeded: gear:" + mLastGear); |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (mLastParkingBrakeTimestamp == NOT_RECEIVED) { |
Kai Wang | 301dd47 | 2021-05-18 06:09:44 +0000 | [diff] [blame] | 490 | CarPropertyValue propertyValue = mPropertyService.getPropertySafe( |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 491 | VehicleProperty.PARKING_BRAKE_ON, |
| 492 | VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL); |
| 493 | if (propertyValue != null) { |
| 494 | mLastParkingBrakeState = (boolean) propertyValue.getValue(); |
| 495 | mLastParkingBrakeTimestamp = propertyValue.getTimestamp(); |
| 496 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 497 | Slog.d(TAG, "updateVehiclePropertiesIfNeeded: brake:" + mLastParkingBrakeState); |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | if (mLastSpeedTimestamp == NOT_RECEIVED) { |
Kai Wang | 301dd47 | 2021-05-18 06:09:44 +0000 | [diff] [blame] | 503 | CarPropertyValue propertyValue = mPropertyService.getPropertySafe( |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 504 | VehicleProperty.PERF_VEHICLE_SPEED, |
| 505 | VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL); |
| 506 | if (propertyValue != null) { |
| 507 | mLastSpeed = (float) propertyValue.getValue(); |
| 508 | mLastSpeedTimestamp = propertyValue.getTimestamp(); |
| 509 | if (DBG) { |
Eric Jeong | bd5fb56 | 2020-12-21 13:49:40 -0800 | [diff] [blame] | 510 | Slog.d(TAG, "updateVehiclePropertiesIfNeeded: speed:" + mLastSpeed); |
Ram Periathiruvadi | f6381fb | 2018-06-05 18:44:19 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 516 | private static CarDrivingStateEvent createDrivingStateEvent(int eventValue) { |
| 517 | return new CarDrivingStateEvent(eventValue, SystemClock.elapsedRealtimeNanos()); |
| 518 | } |
Ram Periathiruvadi | 22c0d68 | 2018-05-10 17:50:48 -0700 | [diff] [blame] | 519 | |
Ram Periathiruvadi | 25c16f1 | 2017-11-17 16:48:37 -0800 | [diff] [blame] | 520 | } |