blob: c7a09961ba15c00190100a5327425e09df19b9c4 [file] [log] [blame]
keunyoung1ab8e182015-09-24 09:25:22 -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 */
Keun-young Parke54ac272016-02-16 19:02:18 -080016package android.car.test;
keunyoung1ab8e182015-09-24 09:25:22 -070017
Keun-young Parke54ac272016-02-16 19:02:18 -080018import android.annotation.SystemApi;
19import android.car.CarManagerBase;
20import android.os.IBinder;
keunyoung1ab8e182015-09-24 09:25:22 -070021import android.os.RemoteException;
keunyoung1ab8e182015-09-24 09:25:22 -070022
23import com.android.car.vehiclenetwork.IVehicleNetworkHalMock;
24import com.android.car.vehiclenetwork.VehicleNetwork.VehicleNetworkHalMock;
25import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
26import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
keunyoung1ab8e182015-09-24 09:25:22 -070027import com.android.car.vehiclenetwork.VehiclePropConfigsParcelable;
28import com.android.car.vehiclenetwork.VehiclePropValueParcelable;
29import com.android.internal.annotations.GuardedBy;
30
31import java.lang.ref.WeakReference;
32
Keun-young Parke54ac272016-02-16 19:02:18 -080033/**
34 * API for testing only. Allows mocking vehicle hal.
35 * @hide
36 */
37@SystemApi
Keun-young Park4cf69112016-10-04 10:02:59 -070038public final class CarTestManager implements CarManagerBase {
keunyoung1ab8e182015-09-24 09:25:22 -070039
keunyoung4b0212c2015-10-29 17:11:57 -070040 /**
41 * Flag for {@link #startMocking(VehicleNetworkHalMock, int)}.
42 * This is for passing no flag.
43 */
44 public static final int FLAG_MOCKING_NONE = 0x0;
45
46 /**
47 * Flag for {@link #startMocking(VehicleNetworkHalMock, int)}.
48 * When this flag is set, shutdown request from mocked vehicle HAL will shutdown the system
49 * instead of avoiding shutdown, which is the default behavior.
50 * This can be used to test shutdown flow manually using mocking.
51 */
52 public static final int FLAG_MOCKING_REAL_SHUTDOWN = 0x1;
53
keunyoung1ab8e182015-09-24 09:25:22 -070054 private final ICarTest mService;
55
56 @GuardedBy("this")
57 private VehicleNetworkHalMock mHalMock;
58 private IVehicleNetworkHalMockImpl mHalMockImpl;
59
Keun-young Parke54ac272016-02-16 19:02:18 -080060 public CarTestManager(CarTestManagerBinderWrapper wrapper) {
61 mService = ICarTest.Stub.asInterface(wrapper.binder);
keunyoung1ab8e182015-09-24 09:25:22 -070062 }
63
64 @Override
65 public void onCarDisconnected() {
66 // should not happen for embedded
67 }
68
Keun-young Parka28d7b22016-02-29 16:54:29 -080069 /**
70 * Inject vehicle prop value event.
71 * @param value
72 */
Keun-young Park43cfff22016-02-19 12:49:52 -080073 public void injectEvent(VehiclePropValue value) {
keunyoung1ab8e182015-09-24 09:25:22 -070074 try {
75 mService.injectEvent(new VehiclePropValueParcelable(value));
76 } catch (RemoteException e) {
77 handleRemoteException(e);
78 }
79 }
80
81 /**
Keun-young Parka28d7b22016-02-29 16:54:29 -080082 * Check if given property is supported by vehicle hal.
83 * @param property
84 * @return
85 */
86 public boolean isPropertySupported(int property) {
87 try {
88 return mService.isPropertySupported(property);
89 } catch (RemoteException e) {
90 handleRemoteException(e);
91 }
92 return false;
93 }
94 /**
keunyoung1ab8e182015-09-24 09:25:22 -070095 * Start mocking vehicle HAL. It is somewhat strange to re-use interface in lower level
96 * API, but this is only for testing, and interface is exactly the same.
97 * @param mock
keunyoung4b0212c2015-10-29 17:11:57 -070098 * @flags Combination of FLAG_MOCKING_*
keunyoung1ab8e182015-09-24 09:25:22 -070099 */
Keun-young Park43cfff22016-02-19 12:49:52 -0800100 public void startMocking(VehicleNetworkHalMock mock, int flags) {
101 IVehicleNetworkHalMockImpl halMockImpl = new IVehicleNetworkHalMockImpl(this);
102 synchronized (this) {
103 mHalMock = mock;
104 mHalMockImpl = halMockImpl;
105 }
keunyoung1ab8e182015-09-24 09:25:22 -0700106 try {
Keun-young Park43cfff22016-02-19 12:49:52 -0800107 mService.startMocking(halMockImpl, flags);
keunyoung1ab8e182015-09-24 09:25:22 -0700108 } catch (RemoteException e) {
109 handleRemoteException(e);
110 }
111 }
112
Keun-young Parka28d7b22016-02-29 16:54:29 -0800113 /**
114 * Stop previously started mocking.
115 */
Keun-young Park43cfff22016-02-19 12:49:52 -0800116 public void stopMocking() {
117 IVehicleNetworkHalMockImpl halMockImpl;
118 synchronized (this) {
119 halMockImpl = mHalMockImpl;
keunyoung1ab8e182015-09-24 09:25:22 -0700120 mHalMock = null;
121 mHalMockImpl = null;
122 }
Keun-young Park43cfff22016-02-19 12:49:52 -0800123 try {
124 mService.stopMocking(halMockImpl);
125 } catch (RemoteException e) {
126 handleRemoteException(e);
127 }
keunyoung1ab8e182015-09-24 09:25:22 -0700128 }
129
130 private synchronized VehicleNetworkHalMock getHalMock() {
131 return mHalMock;
132 }
133
134 private void handleRemoteException(RemoteException e) {
135 //TODO
136 }
137
138 private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
139 private final WeakReference<CarTestManager> mTestManager;
140
141 private IVehicleNetworkHalMockImpl(CarTestManager testManager) {
142 mTestManager = new WeakReference<CarTestManager>(testManager);
143 }
144
145 @Override
146 public VehiclePropConfigsParcelable onListProperties() {
147 CarTestManager testManager = mTestManager.get();
148 if (testManager == null) {
149 return null;
150 }
151 VehiclePropConfigs configs = testManager.getHalMock().onListProperties();
152 return new VehiclePropConfigsParcelable(configs);
153 }
154
155 @Override
156 public void onPropertySet(VehiclePropValueParcelable value) {
157 CarTestManager testManager = mTestManager.get();
158 if (testManager == null) {
159 return;
160 }
161 testManager.getHalMock().onPropertySet(value.value);
162 }
163
164 @Override
keunyoung7d74e6d2015-10-14 15:43:10 -0700165 public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
keunyoung1ab8e182015-09-24 09:25:22 -0700166 CarTestManager testManager = mTestManager.get();
167 if (testManager == null) {
168 return null;
169 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700170 VehiclePropValue retValue = testManager.getHalMock().onPropertyGet(value.value);
171 return new VehiclePropValueParcelable(retValue);
keunyoung1ab8e182015-09-24 09:25:22 -0700172 }
173
174 @Override
Keun-young Park0727f952015-12-21 14:30:07 -0800175 public void onPropertySubscribe(int property, float sampleRate, int zones) {
keunyoung1ab8e182015-09-24 09:25:22 -0700176 CarTestManager testManager = mTestManager.get();
177 if (testManager == null) {
178 return;
179 }
Keun-young Park0727f952015-12-21 14:30:07 -0800180 testManager.getHalMock().onPropertySubscribe(property, sampleRate, zones);
keunyoung1ab8e182015-09-24 09:25:22 -0700181 }
182
183 @Override
184 public void onPropertyUnsubscribe(int property) {
185 CarTestManager testManager = mTestManager.get();
186 if (testManager == null) {
187 return;
188 }
189 testManager.getHalMock().onPropertyUnsubscribe(property);
190 }
191 }
192}