keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -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 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 18 | import android.car.test.CarTestManager; |
| 19 | import android.car.test.ICarTest; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 21 | import android.os.IBinder; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | |
| 24 | import com.android.car.hal.VehicleHal; |
| 25 | import com.android.car.vehiclenetwork.IVehicleNetworkHalMock; |
| 26 | import com.android.car.vehiclenetwork.VehicleNetwork; |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 27 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 28 | import com.android.car.vehiclenetwork.VehiclePropValueParcelable; |
| 29 | |
| 30 | import java.io.PrintWriter; |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 31 | import java.util.NoSuchElementException; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Service to allow testing / mocking vehicle HAL. |
| 35 | * This service uses Vehicle HAL APIs directly (one exception) as vehicle HAL mocking anyway |
| 36 | * requires accessing that level directly. |
| 37 | */ |
| 38 | public class CarTestService extends ICarTest.Stub implements CarServiceBase { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 39 | |
| 40 | private final Context mContext; |
| 41 | private final VehicleNetwork mVehicleNetwork; |
| 42 | private final ICarImpl mICarImpl; |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 43 | private boolean mInMocking = false; |
| 44 | private int mMockingFlags = 0; |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 45 | private Exception mException = null; |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 46 | private IVehicleNetworkHalMock mMock = null; |
| 47 | private final MockDeathRecipient mDeathRecipient = new MockDeathRecipient(); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 48 | |
| 49 | public CarTestService(Context context, ICarImpl carImpl) { |
| 50 | mContext = context; |
| 51 | mICarImpl = carImpl; |
| 52 | mVehicleNetwork = VehicleHal.getInstance().getVehicleNetwork(); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void init() { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 57 | // nothing to do. |
| 58 | // This service should not reset anything for init / release to maintain mocking. |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void release() { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 63 | // nothing to do |
| 64 | // This service should not reset anything for init / release to maintain mocking. |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void dump(PrintWriter writer) { |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 69 | writer.println("*CarTestService*"); |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 70 | writer.println(" mInMocking:" + mInMocking); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 74 | public void injectEvent(VehiclePropValueParcelable value) { |
| 75 | ICarImpl.assertVehicleHalMockPermission(mContext); |
| 76 | mVehicleNetwork.injectEvent(value.value); |
| 77 | } |
| 78 | |
| 79 | @Override |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 80 | public void startMocking(final IVehicleNetworkHalMock mock, final int flags) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 81 | ICarImpl.assertVehicleHalMockPermission(mContext); |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 82 | CarServiceUtils.runOnMainSync(new Runnable() { |
| 83 | @Override |
| 84 | public void run() { |
| 85 | try { |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 86 | synchronized (this) { |
| 87 | mInMocking = true; |
| 88 | mMockingFlags = flags; |
| 89 | mException = null; |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 90 | if (mMock != null) { |
| 91 | Log.w(CarLog.TAG_TEST, |
| 92 | "New mocking started while previous one is not stopped"); |
| 93 | try { |
| 94 | mMock.asBinder().unlinkToDeath(mDeathRecipient, 0); |
| 95 | } catch (NoSuchElementException e) { |
| 96 | //ignore |
| 97 | } |
| 98 | } |
| 99 | mMock = mock; |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 100 | } |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 101 | mock.asBinder().linkToDeath(mDeathRecipient, 0); |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 102 | mVehicleNetwork.startMocking(mock); |
| 103 | mICarImpl.startMocking(); |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 104 | } catch (Exception e) { |
| 105 | Log.w(CarLog.TAG_TEST, "startMocking failed", e); |
| 106 | synchronized (this) { |
| 107 | mException = e; |
Keun-young Park | 021310d | 2016-04-25 21:09:39 -0700 | [diff] [blame] | 108 | mInMocking = false; |
| 109 | mMockingFlags = 0; |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | Log.i(CarLog.TAG_TEST, "start vehicle HAL mocking, flags:0x" + |
| 113 | Integer.toHexString(flags)); |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 114 | } |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 115 | }); |
| 116 | synchronized (this) { |
| 117 | if (mException != null) { |
| 118 | throw new IllegalStateException(mException); |
| 119 | } |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 120 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 124 | public void stopMocking(final IVehicleNetworkHalMock mock) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 125 | ICarImpl.assertVehicleHalMockPermission(mContext); |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 126 | CarServiceUtils.runOnMainSync(new Runnable() { |
| 127 | @Override |
| 128 | public void run() { |
| 129 | try { |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 130 | synchronized (this) { |
| 131 | if (!mInMocking) { |
| 132 | throw new IllegalStateException("not in mocking"); |
| 133 | } |
| 134 | if (mMock.asBinder() != mock.asBinder()) { |
| 135 | throw new IllegalArgumentException("Try to stop mocking not owned"); |
| 136 | } |
| 137 | try { |
| 138 | mMock.asBinder().unlinkToDeath(mDeathRecipient, 0); |
| 139 | } catch (NoSuchElementException e) { |
| 140 | //ignore |
| 141 | } |
| 142 | } |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 143 | mVehicleNetwork.stopMocking(mock); |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 144 | mICarImpl.stopMocking(); |
| 145 | synchronized (this) { |
| 146 | mInMocking = false; |
| 147 | mMockingFlags = 0; |
| 148 | mException = null; |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 149 | mMock = null; |
Keun-young Park | 43cfff2 | 2016-02-19 12:49:52 -0800 | [diff] [blame] | 150 | } |
| 151 | Log.i(CarLog.TAG_TEST, "stop vehicle HAL mocking"); |
| 152 | } catch (Exception e) { |
| 153 | Log.w(CarLog.TAG_TEST, "stopMocking failed", e); |
| 154 | } |
| 155 | } |
| 156 | }); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 157 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 158 | |
Keun-young Park | a28d7b2 | 2016-02-29 16:54:29 -0800 | [diff] [blame] | 159 | @Override |
| 160 | public boolean isPropertySupported(int property) { |
| 161 | VehiclePropConfigs configs = VehicleHal.getInstance().getVehicleNetwork().listProperties( |
| 162 | property); |
| 163 | return configs != null; |
| 164 | } |
| 165 | |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 166 | public synchronized boolean isInMocking() { |
| 167 | return mInMocking; |
| 168 | } |
| 169 | |
| 170 | public synchronized boolean shouldDoRealShutdownInMocking() { |
| 171 | return (mMockingFlags & CarTestManager.FLAG_MOCKING_REAL_SHUTDOWN) != 0; |
| 172 | } |
Keun-young Park | a54e116 | 2016-07-29 14:54:06 -0700 | [diff] [blame] | 173 | |
| 174 | private class MockDeathRecipient implements IBinder.DeathRecipient { |
| 175 | @Override |
| 176 | public void binderDied() { |
| 177 | CarServiceUtils.runOnMainSync(() -> { |
| 178 | try { |
| 179 | IVehicleNetworkHalMock mock; |
| 180 | synchronized (this) { |
| 181 | mock = mMock; |
| 182 | } |
| 183 | stopMocking(mock); |
| 184 | } catch (Exception e) { |
| 185 | //ignore |
| 186 | } |
| 187 | }); |
| 188 | } |
| 189 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 190 | } |