keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Ram Periathiruvadi | bf0eab7 | 2018-02-06 12:32:43 -0800 | [diff] [blame] | 16 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 17 | package com.android.car; |
| 18 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 19 | import static android.os.SystemClock.elapsedRealtime; |
| 20 | |
| 21 | import android.annotation.Nullable; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 22 | import android.app.Service; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
Pavel Maltsev | cfe9310 | 2017-02-02 12:38:08 -0800 | [diff] [blame] | 24 | import android.hardware.automotive.vehicle.V2_0.IVehicle; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 25 | import android.os.Build; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 26 | import android.os.IBinder; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 27 | import android.os.IHwBinder.DeathRecipient; |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 28 | import android.os.RemoteException; |
Steve Paik | b40de19 | 2018-03-01 18:07:38 -0800 | [diff] [blame] | 29 | import android.os.ServiceManager; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 30 | import android.os.SystemClock; |
Vitalii Tomkiv | 973d2a2 | 2016-04-13 17:05:38 -0700 | [diff] [blame] | 31 | import android.os.SystemProperties; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | |
Enrico Granata | b19bc32 | 2017-10-12 12:25:06 -0700 | [diff] [blame] | 34 | import com.android.car.systeminterface.SystemInterface; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 35 | import com.android.internal.annotations.VisibleForTesting; |
| 36 | import com.android.internal.util.RingBufferIndices; |
| 37 | |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 38 | import java.io.FileDescriptor; |
| 39 | import java.io.PrintWriter; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 40 | import java.util.NoSuchElementException; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 41 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 42 | public class CarService extends Service { |
| 43 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 44 | private static final long WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS = 10_000; |
| 45 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 46 | private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); |
| 47 | |
| 48 | private CanBusErrorNotifier mCanBusErrorNotifier; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 49 | private ICarImpl mICarImpl; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 50 | private IVehicle mVehicle; |
| 51 | |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 52 | private String mVehicleInterfaceName; |
| 53 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 54 | // If 10 crashes of Vehicle HAL occurred within 10 minutes then thrown an exception in |
| 55 | // Car Service. |
| 56 | private final CrashTracker mVhalCrashTracker = new CrashTracker( |
| 57 | 10, // Max crash count. |
| 58 | 10 * 60 * 1000, // 10 minutes - sliding time window. |
| 59 | () -> { |
| 60 | if (IS_USER_BUILD) { |
| 61 | Log.e(CarLog.TAG_SERVICE, "Vehicle HAL keeps crashing, notifying user..."); |
| 62 | mCanBusErrorNotifier.reportFailure(CarService.this); |
| 63 | } else { |
| 64 | throw new RuntimeException( |
| 65 | "Vehicle HAL crashed too many times in a given time frame"); |
| 66 | } |
| 67 | } |
| 68 | ); |
| 69 | |
| 70 | private final VehicleDeathRecipient mVehicleDeathRecipient = new VehicleDeathRecipient(); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public void onCreate() { |
| 74 | Log.i(CarLog.TAG_SERVICE, "Service onCreate"); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 75 | mCanBusErrorNotifier = new CanBusErrorNotifier(this /* context */); |
Pavel Maltsev | 99e1a75 | 2017-08-24 15:15:05 -0700 | [diff] [blame] | 76 | mVehicle = getVehicle(); |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 77 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 78 | if (mVehicle == null) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 79 | throw new IllegalStateException("Vehicle HAL service is not available."); |
| 80 | } |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 81 | try { |
| 82 | mVehicleInterfaceName = mVehicle.interfaceDescriptor(); |
| 83 | } catch (RemoteException e) { |
| 84 | throw new IllegalStateException("Unable to get Vehicle HAL interface descriptor", e); |
| 85 | } |
| 86 | |
| 87 | Log.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 88 | |
Enrico Granata | b19bc32 | 2017-10-12 12:25:06 -0700 | [diff] [blame] | 89 | mICarImpl = new ICarImpl(this, |
| 90 | mVehicle, |
| 91 | SystemInterface.Builder.defaultSystemInterface(this).build(), |
Enrico Granata | e8056ca | 2018-04-03 13:19:52 -0700 | [diff] [blame] | 92 | mCanBusErrorNotifier, |
| 93 | mVehicleInterfaceName); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 94 | mICarImpl.init(); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 95 | |
| 96 | linkToDeath(mVehicle, mVehicleDeathRecipient); |
| 97 | |
Steve Paik | b40de19 | 2018-03-01 18:07:38 -0800 | [diff] [blame] | 98 | ServiceManager.addService("car_service", mICarImpl); |
Keun young Park | 1476226 | 2019-03-21 18:51:37 -0700 | [diff] [blame] | 99 | SystemProperties.set("boot.car_service_created", "1"); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 100 | super.onCreate(); |
| 101 | } |
| 102 | |
Enrico Granata | 9a916d7 | 2017-09-19 14:33:08 -0700 | [diff] [blame] | 103 | // onDestroy is best-effort and might not get called on shutdown/reboot. As such it is not |
| 104 | // suitable for permanently saving state or other need-to-happen operation. If you have a |
| 105 | // cleanup task that you want to make sure happens on shutdown/reboot, see OnShutdownReboot. |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public void onDestroy() { |
| 108 | Log.i(CarLog.TAG_SERVICE, "Service onDestroy"); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 109 | mICarImpl.release(); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 110 | mCanBusErrorNotifier.removeFailureReport(this); |
| 111 | |
| 112 | if (mVehicle != null) { |
| 113 | try { |
| 114 | mVehicle.unlinkToDeath(mVehicleDeathRecipient); |
| 115 | mVehicle = null; |
| 116 | } catch (RemoteException e) { |
| 117 | // Ignore errors on shutdown path. |
| 118 | } |
| 119 | } |
| 120 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 121 | super.onDestroy(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 126 | // keep it alive. |
| 127 | return START_STICKY; |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public IBinder onBind(Intent intent) { |
| 132 | return mICarImpl; |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Enrico Granata | e8056ca | 2018-04-03 13:19:52 -0700 | [diff] [blame] | 137 | // historically, the way to get a dumpsys from CarService has been to use |
| 138 | // "dumpsys activity service com.android.car/.CarService" - leaving this |
| 139 | // as a forward to car_service makes the previously well-known command still work |
| 140 | mICarImpl.dump(fd, writer, args); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 141 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 142 | |
| 143 | @Nullable |
Pavel Maltsev | 99e1a75 | 2017-08-24 15:15:05 -0700 | [diff] [blame] | 144 | private IVehicle getVehicleWithTimeout(long waitMilliseconds) { |
| 145 | IVehicle vehicle = getVehicle(); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 146 | long start = elapsedRealtime(); |
| 147 | while (vehicle == null && (start + waitMilliseconds) > elapsedRealtime()) { |
| 148 | try { |
| 149 | Thread.sleep(100); |
| 150 | } catch (InterruptedException e) { |
| 151 | throw new RuntimeException("Sleep was interrupted", e); |
| 152 | } |
| 153 | |
Pavel Maltsev | 99e1a75 | 2017-08-24 15:15:05 -0700 | [diff] [blame] | 154 | vehicle = getVehicle(); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 155 | } |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 156 | |
| 157 | if (vehicle != null) { |
| 158 | mCanBusErrorNotifier.removeFailureReport(this); |
| 159 | } |
| 160 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 161 | return vehicle; |
| 162 | } |
| 163 | |
| 164 | @Nullable |
Pavel Maltsev | 99e1a75 | 2017-08-24 15:15:05 -0700 | [diff] [blame] | 165 | private static IVehicle getVehicle() { |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 166 | try { |
Pavel Maltsev | 99e1a75 | 2017-08-24 15:15:05 -0700 | [diff] [blame] | 167 | return android.hardware.automotive.vehicle.V2_0.IVehicle.getService(); |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 168 | } catch (RemoteException e) { |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 169 | Log.e(CarLog.TAG_SERVICE, "Failed to get IVehicle service", e); |
| 170 | } catch (NoSuchElementException e) { |
| 171 | Log.e(CarLog.TAG_SERVICE, "IVehicle service not registered yet"); |
| 172 | } |
| 173 | return null; |
| 174 | } |
| 175 | |
| 176 | private class VehicleDeathRecipient implements DeathRecipient { |
| 177 | private int deathCount = 0; |
| 178 | |
| 179 | @Override |
| 180 | public void serviceDied(long cookie) { |
| 181 | Log.w(CarLog.TAG_SERVICE, "Vehicle HAL died."); |
| 182 | |
| 183 | try { |
| 184 | mVehicle.unlinkToDeath(this); |
| 185 | } catch (RemoteException e) { |
| 186 | Log.e(CarLog.TAG_SERVICE, "Failed to unlinkToDeath", e); // Log and continue. |
| 187 | } |
| 188 | mVehicle = null; |
| 189 | |
| 190 | mVhalCrashTracker.crashDetected(); |
| 191 | |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 192 | Log.i(CarLog.TAG_SERVICE, "Trying to reconnect to Vehicle HAL: " + |
| 193 | mVehicleInterfaceName); |
Pavel Maltsev | 99e1a75 | 2017-08-24 15:15:05 -0700 | [diff] [blame] | 194 | mVehicle = getVehicleWithTimeout(WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 195 | if (mVehicle == null) { |
| 196 | throw new IllegalStateException("Failed to reconnect to Vehicle HAL"); |
| 197 | } |
| 198 | |
| 199 | linkToDeath(mVehicle, this); |
| 200 | |
| 201 | Log.i(CarLog.TAG_SERVICE, "Notifying car service Vehicle HAL reconnected..."); |
| 202 | mICarImpl.vehicleHalReconnected(mVehicle); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private static void linkToDeath(IVehicle vehicle, DeathRecipient recipient) { |
| 207 | try { |
| 208 | vehicle.linkToDeath(recipient, 0); |
| 209 | } catch (RemoteException e) { |
| 210 | throw new IllegalStateException("Failed to linkToDeath Vehicle HAL"); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | @VisibleForTesting |
| 215 | static class CrashTracker { |
| 216 | private final int mMaxCrashCountLimit; |
| 217 | private final int mSlidingWindowMillis; |
| 218 | |
| 219 | private final long[] mCrashTimestamps; |
| 220 | private final RingBufferIndices mCrashTimestampsIndices; |
| 221 | private final Runnable mCallback; |
| 222 | |
| 223 | /** |
| 224 | * If maxCrashCountLimit number of crashes occurred within slidingWindowMillis time |
| 225 | * frame then call provided callback function. |
| 226 | */ |
| 227 | CrashTracker(int maxCrashCountLimit, int slidingWindowMillis, Runnable callback) { |
| 228 | mMaxCrashCountLimit = maxCrashCountLimit; |
| 229 | mSlidingWindowMillis = slidingWindowMillis; |
| 230 | mCallback = callback; |
| 231 | |
| 232 | mCrashTimestamps = new long[maxCrashCountLimit]; |
| 233 | mCrashTimestampsIndices = new RingBufferIndices(mMaxCrashCountLimit); |
| 234 | } |
| 235 | |
| 236 | void crashDetected() { |
| 237 | long lastCrash = SystemClock.elapsedRealtime(); |
| 238 | mCrashTimestamps[mCrashTimestampsIndices.add()] = lastCrash; |
| 239 | |
| 240 | if (mCrashTimestampsIndices.size() == mMaxCrashCountLimit) { |
| 241 | long firstCrash = mCrashTimestamps[mCrashTimestampsIndices.indexOf(0)]; |
| 242 | |
| 243 | if (lastCrash - firstCrash < mSlidingWindowMillis) { |
| 244 | mCallback.run(); |
| 245 | } |
| 246 | } |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 247 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 248 | } |
Chris Phoenix | 9204449 | 2017-01-11 18:36:39 -0800 | [diff] [blame] | 249 | } |