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 | */ |
| 16 | package com.android.car; |
| 17 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 18 | import static android.os.SystemClock.elapsedRealtime; |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 19 | import static com.android.car.internal.FeatureConfiguration.ENABLE_VEHICLE_HAL_V2_1; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 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; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
Pavel Maltsev | cfe9310 | 2017-02-02 12:38:08 -0800 | [diff] [blame] | 25 | import android.hardware.automotive.vehicle.V2_0.IVehicle; |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 26 | import android.os.Binder; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 27 | import android.os.Build; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 29 | import android.os.IHwBinder.DeathRecipient; |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 30 | import android.os.RemoteException; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 31 | import android.os.SystemClock; |
Vitalii Tomkiv | 973d2a2 | 2016-04-13 17:05:38 -0700 | [diff] [blame] | 32 | import android.os.SystemProperties; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
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 | |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 48 | private static final String IVHAL_20 = |
| 49 | android.hardware.automotive.vehicle.V2_0.IVehicle.kInterfaceName; |
| 50 | private static final String IVHAL_21 = |
| 51 | android.hardware.automotive.vehicle.V2_1.IVehicle.kInterfaceName; |
| 52 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 53 | private CanBusErrorNotifier mCanBusErrorNotifier; |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 54 | private ICarImpl mICarImpl; |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 55 | private IVehicle mVehicle; |
| 56 | |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 57 | private String mVehicleInterfaceName; |
| 58 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 59 | // If 10 crashes of Vehicle HAL occurred within 10 minutes then thrown an exception in |
| 60 | // Car Service. |
| 61 | private final CrashTracker mVhalCrashTracker = new CrashTracker( |
| 62 | 10, // Max crash count. |
| 63 | 10 * 60 * 1000, // 10 minutes - sliding time window. |
| 64 | () -> { |
| 65 | if (IS_USER_BUILD) { |
| 66 | Log.e(CarLog.TAG_SERVICE, "Vehicle HAL keeps crashing, notifying user..."); |
| 67 | mCanBusErrorNotifier.reportFailure(CarService.this); |
| 68 | } else { |
| 69 | throw new RuntimeException( |
| 70 | "Vehicle HAL crashed too many times in a given time frame"); |
| 71 | } |
| 72 | } |
| 73 | ); |
| 74 | |
| 75 | private final VehicleDeathRecipient mVehicleDeathRecipient = new VehicleDeathRecipient(); |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | public void onCreate() { |
| 79 | Log.i(CarLog.TAG_SERVICE, "Service onCreate"); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 80 | mCanBusErrorNotifier = new CanBusErrorNotifier(this /* context */); |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 81 | mVehicle = getVehicle(null /* Any Vehicle HAL interface name */); |
| 82 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 83 | if (mVehicle == null) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 84 | throw new IllegalStateException("Vehicle HAL service is not available."); |
| 85 | } |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 86 | try { |
| 87 | mVehicleInterfaceName = mVehicle.interfaceDescriptor(); |
| 88 | } catch (RemoteException e) { |
| 89 | throw new IllegalStateException("Unable to get Vehicle HAL interface descriptor", e); |
| 90 | } |
| 91 | |
| 92 | Log.i(CarLog.TAG_SERVICE, "Connected to " + mVehicleInterfaceName); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 93 | |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 94 | mICarImpl = new ICarImpl(this, mVehicle, SystemInterface.getDefault(this), |
| 95 | mCanBusErrorNotifier); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 96 | mICarImpl.init(); |
Vitalii Tomkiv | 973d2a2 | 2016-04-13 17:05:38 -0700 | [diff] [blame] | 97 | SystemProperties.set("boot.car_service_created", "1"); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 98 | |
| 99 | linkToDeath(mVehicle, mVehicleDeathRecipient); |
| 100 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 101 | super.onCreate(); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void onDestroy() { |
| 106 | Log.i(CarLog.TAG_SERVICE, "Service onDestroy"); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 107 | mICarImpl.release(); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 108 | mCanBusErrorNotifier.removeFailureReport(this); |
| 109 | |
| 110 | if (mVehicle != null) { |
| 111 | try { |
| 112 | mVehicle.unlinkToDeath(mVehicleDeathRecipient); |
| 113 | mVehicle = null; |
| 114 | } catch (RemoteException e) { |
| 115 | // Ignore errors on shutdown path. |
| 116 | } |
| 117 | } |
| 118 | |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 119 | super.onDestroy(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 124 | // keep it alive. |
| 125 | return START_STICKY; |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public IBinder onBind(Intent intent) { |
| 130 | return mICarImpl; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 135 | if (checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 136 | != PackageManager.PERMISSION_GRANTED) { |
| 137 | writer.println("Permission Denial: can't dump CarService from from pid=" |
| 138 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid() |
| 139 | + " without permission " + android.Manifest.permission.DUMP); |
| 140 | return; |
| 141 | } |
| 142 | if (args == null || args.length == 0) { |
| 143 | writer.println("*dump car service*"); |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 144 | writer.println("Vehicle HAL Interface: " + mVehicleInterfaceName); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 145 | mICarImpl.dump(writer); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 146 | |
| 147 | writer.println("**Debug info**"); |
| 148 | writer.println("Vehicle HAL reconnected: " |
| 149 | + mVehicleDeathRecipient.deathCount + " times."); |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 150 | } else { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 151 | mICarImpl.execShellCmd(args, writer); |
Yao Chen | e33f07e | 2016-07-26 12:02:51 -0700 | [diff] [blame] | 152 | } |
keunyoung | ca51507 | 2015-07-10 12:21:47 -0700 | [diff] [blame] | 153 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 154 | |
| 155 | @Nullable |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 156 | private IVehicle getVehicleWithTimeout(long waitMilliseconds, @Nullable String interfaceName) { |
| 157 | IVehicle vehicle = getVehicle(interfaceName); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 158 | long start = elapsedRealtime(); |
| 159 | while (vehicle == null && (start + waitMilliseconds) > elapsedRealtime()) { |
| 160 | try { |
| 161 | Thread.sleep(100); |
| 162 | } catch (InterruptedException e) { |
| 163 | throw new RuntimeException("Sleep was interrupted", e); |
| 164 | } |
| 165 | |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 166 | vehicle = getVehicle(interfaceName); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 167 | } |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 168 | |
| 169 | if (vehicle != null) { |
| 170 | mCanBusErrorNotifier.removeFailureReport(this); |
| 171 | } |
| 172 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 173 | return vehicle; |
| 174 | } |
| 175 | |
| 176 | @Nullable |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 177 | private static IVehicle getVehicle(@Nullable String interfaceName) { |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 178 | try { |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 179 | boolean anyVersion = interfaceName == null || interfaceName.isEmpty(); |
| 180 | IVehicle vehicle = null; |
| 181 | if (ENABLE_VEHICLE_HAL_V2_1 && (anyVersion || IVHAL_21.equals(interfaceName))) { |
| 182 | vehicle = android.hardware.automotive.vehicle.V2_1.IVehicle |
Chris Phoenix | 9204449 | 2017-01-11 18:36:39 -0800 | [diff] [blame] | 183 | .getService(); |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if (vehicle == null && (anyVersion || IVHAL_20.equals(interfaceName))) { |
| 187 | vehicle = android.hardware.automotive.vehicle.V2_0.IVehicle |
Chris Phoenix | 9204449 | 2017-01-11 18:36:39 -0800 | [diff] [blame] | 188 | .getService(); |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 189 | } |
| 190 | return vehicle; |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 191 | } catch (RemoteException e) { |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 192 | Log.e(CarLog.TAG_SERVICE, "Failed to get IVehicle service", e); |
| 193 | } catch (NoSuchElementException e) { |
| 194 | Log.e(CarLog.TAG_SERVICE, "IVehicle service not registered yet"); |
| 195 | } |
| 196 | return null; |
| 197 | } |
| 198 | |
| 199 | private class VehicleDeathRecipient implements DeathRecipient { |
| 200 | private int deathCount = 0; |
| 201 | |
| 202 | @Override |
| 203 | public void serviceDied(long cookie) { |
| 204 | Log.w(CarLog.TAG_SERVICE, "Vehicle HAL died."); |
| 205 | |
| 206 | try { |
| 207 | mVehicle.unlinkToDeath(this); |
| 208 | } catch (RemoteException e) { |
| 209 | Log.e(CarLog.TAG_SERVICE, "Failed to unlinkToDeath", e); // Log and continue. |
| 210 | } |
| 211 | mVehicle = null; |
| 212 | |
| 213 | mVhalCrashTracker.crashDetected(); |
| 214 | |
Pavel Maltsev | 2cc76d0 | 2017-02-14 12:28:33 -0800 | [diff] [blame] | 215 | Log.i(CarLog.TAG_SERVICE, "Trying to reconnect to Vehicle HAL: " + |
| 216 | mVehicleInterfaceName); |
| 217 | mVehicle = getVehicleWithTimeout(WAIT_FOR_VEHICLE_HAL_TIMEOUT_MS, |
| 218 | mVehicleInterfaceName); |
Pavel Maltsev | ec83b63 | 2017-01-05 15:10:55 -0800 | [diff] [blame] | 219 | if (mVehicle == null) { |
| 220 | throw new IllegalStateException("Failed to reconnect to Vehicle HAL"); |
| 221 | } |
| 222 | |
| 223 | linkToDeath(mVehicle, this); |
| 224 | |
| 225 | Log.i(CarLog.TAG_SERVICE, "Notifying car service Vehicle HAL reconnected..."); |
| 226 | mICarImpl.vehicleHalReconnected(mVehicle); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | private static void linkToDeath(IVehicle vehicle, DeathRecipient recipient) { |
| 231 | try { |
| 232 | vehicle.linkToDeath(recipient, 0); |
| 233 | } catch (RemoteException e) { |
| 234 | throw new IllegalStateException("Failed to linkToDeath Vehicle HAL"); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | @VisibleForTesting |
| 239 | static class CrashTracker { |
| 240 | private final int mMaxCrashCountLimit; |
| 241 | private final int mSlidingWindowMillis; |
| 242 | |
| 243 | private final long[] mCrashTimestamps; |
| 244 | private final RingBufferIndices mCrashTimestampsIndices; |
| 245 | private final Runnable mCallback; |
| 246 | |
| 247 | /** |
| 248 | * If maxCrashCountLimit number of crashes occurred within slidingWindowMillis time |
| 249 | * frame then call provided callback function. |
| 250 | */ |
| 251 | CrashTracker(int maxCrashCountLimit, int slidingWindowMillis, Runnable callback) { |
| 252 | mMaxCrashCountLimit = maxCrashCountLimit; |
| 253 | mSlidingWindowMillis = slidingWindowMillis; |
| 254 | mCallback = callback; |
| 255 | |
| 256 | mCrashTimestamps = new long[maxCrashCountLimit]; |
| 257 | mCrashTimestampsIndices = new RingBufferIndices(mMaxCrashCountLimit); |
| 258 | } |
| 259 | |
| 260 | void crashDetected() { |
| 261 | long lastCrash = SystemClock.elapsedRealtime(); |
| 262 | mCrashTimestamps[mCrashTimestampsIndices.add()] = lastCrash; |
| 263 | |
| 264 | if (mCrashTimestampsIndices.size() == mMaxCrashCountLimit) { |
| 265 | long firstCrash = mCrashTimestamps[mCrashTimestampsIndices.indexOf(0)]; |
| 266 | |
| 267 | if (lastCrash - firstCrash < mSlidingWindowMillis) { |
| 268 | mCallback.run(); |
| 269 | } |
| 270 | } |
Steven Moreland | 1552204 | 2017-01-05 09:44:59 -0800 | [diff] [blame] | 271 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame] | 272 | } |
Chris Phoenix | 9204449 | 2017-01-11 18:36:39 -0800 | [diff] [blame] | 273 | } |