blob: 48b62b0979a1bb1406b9fbca897f92df0f70c862 [file] [log] [blame]
keunyounge18e25d2015-08-28 15:57:19 -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 */
16package com.android.car.vehiclenetwork;
17
Pavel Maltseve8f75372016-01-26 10:26:04 -080018import static com.android.car.vehiclenetwork.VehiclePropValueUtil.getVectorLength;
19import static com.android.car.vehiclenetwork.VehiclePropValueUtil.isCustomProperty;
20import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toFloatArray;
21import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toIntArray;
22
keunyounge18e25d2015-08-28 15:57:19 -070023import android.os.Handler;
24import android.os.Looper;
25import android.os.Message;
26import android.os.RemoteException;
27import android.os.ServiceManager;
28import android.util.Log;
29
keunyoungd32f4e62015-09-21 11:33:06 -070030import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
keunyounge18e25d2015-08-28 15:57:19 -070031import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
32import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
33import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
Pavel Maltseve8f75372016-01-26 10:26:04 -080034import com.android.car.vehiclenetwork.VehicleNetworkProto.ZonedValue;
keunyoung1ab8e182015-09-24 09:25:22 -070035import com.android.internal.annotations.GuardedBy;
keunyounge18e25d2015-08-28 15:57:19 -070036
37import java.lang.ref.WeakReference;
38
39/**
40 * System API to access Vehicle network. This is only for system services and applications should
keunyoung15882e52015-09-16 16:57:58 -070041 * not use this. All APIs will fail with security error if normal app tries this.
keunyounge18e25d2015-08-28 15:57:19 -070042 */
43public class VehicleNetwork {
keunyoung1ab8e182015-09-24 09:25:22 -070044 /**
45 * Listener for VNS events.
46 */
keunyounge18e25d2015-08-28 15:57:19 -070047 public interface VehicleNetworkListener {
keunyoung1ab8e182015-09-24 09:25:22 -070048 /**
49 * Notify HAL events. This requires subscribing the property
50 */
keunyounge18e25d2015-08-28 15:57:19 -070051 void onVehicleNetworkEvents(VehiclePropValues values);
Keun-young Park28dd4702015-11-19 18:06:04 -080052 void onHalError(int errorCode, int property, int operation);
53 void onHalRestart(boolean inMocking);
keunyounge18e25d2015-08-28 15:57:19 -070054 }
55
keunyoung1ab8e182015-09-24 09:25:22 -070056 public interface VehicleNetworkHalMock {
57 VehiclePropConfigs onListProperties();
58 void onPropertySet(VehiclePropValue value);
Keun-young Park28dd4702015-11-19 18:06:04 -080059 VehiclePropValue onPropertyGet(VehiclePropValue value);
Keun-young Park0727f952015-12-21 14:30:07 -080060 void onPropertySubscribe(int property, float sampleRate, int zones);
keunyoung1ab8e182015-09-24 09:25:22 -070061 void onPropertyUnsubscribe(int property);
62 }
63
keunyounge18e25d2015-08-28 15:57:19 -070064 private static final String TAG = VehicleNetwork.class.getSimpleName();
65
66 private final IVehicleNetwork mService;
67 private final VehicleNetworkListener mListener;
68 private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
69 private final EventHandler mEventHandler;
70
keunyoung1ab8e182015-09-24 09:25:22 -070071 @GuardedBy("this")
72 private VehicleNetworkHalMock mHalMock;
73 private IVehicleNetworkHalMock mHalMockImpl;
74
keunyoung217ca352015-11-17 14:45:38 -080075 private static final int VNS_CONNECT_MAX_RETRY = 10;
76 private static final long VNS_RETRY_WAIT_TIME_MS = 1000;
Pavel Maltseve8f75372016-01-26 10:26:04 -080077 private static final int NO_ZONE = -1;
keunyoung217ca352015-11-17 14:45:38 -080078
keunyoung5c7cb262015-10-19 10:47:45 -070079 /**
80 * Factory method to create VehicleNetwork
Pavel Maltseve8f75372016-01-26 10:26:04 -080081 *
keunyoung5c7cb262015-10-19 10:47:45 -070082 * @param listener listener for listening events
83 * @param looper Looper to dispatch listener events
keunyoung5c7cb262015-10-19 10:47:45 -070084 */
keunyoung15882e52015-09-16 16:57:58 -070085 public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
86 Looper looper) {
keunyoung217ca352015-11-17 14:45:38 -080087 int retryCount = 0;
88 IVehicleNetwork service = null;
89 while (service == null) {
90 service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
91 IVehicleNetwork.class.getCanonicalName()));
92 retryCount++;
93 if (retryCount > VNS_CONNECT_MAX_RETRY) {
94 break;
95 }
96 try {
97 Thread.sleep(VNS_RETRY_WAIT_TIME_MS);
98 } catch (InterruptedException e) {
99 //ignore
100 }
101 }
keunyounge18e25d2015-08-28 15:57:19 -0700102 if (service == null) {
keunyoung15882e52015-09-16 16:57:58 -0700103 throw new RuntimeException("Vehicle network service not available:" +
104 IVehicleNetwork.class.getCanonicalName());
keunyounge18e25d2015-08-28 15:57:19 -0700105 }
106 return new VehicleNetwork(service, listener, looper);
107 }
108
109 private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
110 Looper looper) {
111 mService = service;
112 mListener = listener;
113 mEventHandler = new EventHandler(looper);
114 mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
115 }
116
keunyoung5c7cb262015-10-19 10:47:45 -0700117 /**
118 * List all properties from vehicle HAL
Pavel Maltseve8f75372016-01-26 10:26:04 -0800119 *
keunyoung5c7cb262015-10-19 10:47:45 -0700120 * @return all properties
121 */
keunyoung15882e52015-09-16 16:57:58 -0700122 public VehiclePropConfigs listProperties() {
123 return listProperties(0 /* all */);
124 }
125
keunyoung5c7cb262015-10-19 10:47:45 -0700126 /**
127 * Return configuration information of single property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800128 *
129 * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has has
130 * special meaning of list all properties.
keunyoung5c7cb262015-10-19 10:47:45 -0700131 * @return null if given property does not exist.
132 */
keunyounge18e25d2015-08-28 15:57:19 -0700133 public VehiclePropConfigs listProperties(int property) {
134 try {
135 VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
136 if (parcelable != null) {
137 return parcelable.configs;
138 }
139 } catch (RemoteException e) {
140 handleRemoteException(e);
141 }
142 return null;
143 }
144
keunyoung5c7cb262015-10-19 10:47:45 -0700145 /**
146 * Set property which will lead into writing the value to vehicle HAL.
Pavel Maltseve8f75372016-01-26 10:26:04 -0800147 *
148 * @throws IllegalArgumentException If value set has wrong value like wrong valueType, wrong
149 * data, and etc.
keunyoung5c7cb262015-10-19 10:47:45 -0700150 */
151 public void setProperty(VehiclePropValue value) throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700152 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
153 try {
keunyoung15882e52015-09-16 16:57:58 -0700154 mService.setProperty(parcelable);
keunyounge18e25d2015-08-28 15:57:19 -0700155 } catch (RemoteException e) {
156 handleRemoteException(e);
157 }
keunyounge18e25d2015-08-28 15:57:19 -0700158 }
159
keunyoung5c7cb262015-10-19 10:47:45 -0700160 /**
161 * Set integer type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800162 *
keunyoung5c7cb262015-10-19 10:47:45 -0700163 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
164 */
165 public void setIntProperty(int property, int value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700166 VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700167 setProperty(v);
168 }
169
keunyoung5c7cb262015-10-19 10:47:45 -0700170 /**
171 * Set int vector type property. Length of passed values should match with vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700172 */
173 public void setIntVectorProperty(int property, int[] values) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700174 VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0);
175 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700176 }
177
keunyoung5c7cb262015-10-19 10:47:45 -0700178 /**
179 * Set long type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700180 */
181 public void setLongProperty(int property, long value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700182 VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700183 setProperty(v);
184 }
185
keunyoung5c7cb262015-10-19 10:47:45 -0700186 /**
187 * Set float type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700188 */
189 public void setFloatProperty(int property, float value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700190 VehiclePropValue v = VehiclePropValueUtil.createFloatValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700191 setProperty(v);
192 }
193
keunyoung5c7cb262015-10-19 10:47:45 -0700194 /**
195 * Set float vector type property. Length of values should match with vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700196 */
197 public void setFloatVectorProperty(int property, float[] values)
198 throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700199 VehiclePropValue v = VehiclePropValueUtil.createFloatVectorValue(property, values, 0);
200 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700201 }
202
keunyoung5c7cb262015-10-19 10:47:45 -0700203 /**
Steve Paik66481982015-10-27 15:22:38 -0700204 * Set zoned boolean type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800205 *
206 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
207 */
208 public void setBooleanProperty(int property, boolean value)
209 throws IllegalArgumentException {
210 VehiclePropValue v = VehiclePropValueUtil.createBooleanValue(property, value, 0);
211 setProperty(v);
212 }
213
214 /**
215 * Set zoned boolean type property
216 *
Steve Paik66481982015-10-27 15:22:38 -0700217 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
218 */
219 public void setZonedBooleanProperty(int property, int zone, boolean value)
220 throws IllegalArgumentException {
221 VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0);
222 setProperty(v);
223 }
224
225 /**
226 * Set zoned float type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800227 *
Steve Paik66481982015-10-27 15:22:38 -0700228 * @throws IllegalArgumentException For type mismatch (=the property is not float type)
229 */
230 public void setZonedFloatProperty(int property, int zone, float value)
231 throws IllegalArgumentException {
232 VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0);
233 setProperty(v);
234 }
235
236 /**
237 * Set zoned integer type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800238 *
Steve Paik66481982015-10-27 15:22:38 -0700239 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
240 */
241 public void setZonedIntProperty(int property, int zone, int value)
242 throws IllegalArgumentException {
243 VehiclePropValue v = VehiclePropValueUtil.createZonedIntValue(property, zone, value, 0);
244 setProperty(v);
245 }
246
247 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800248 * Set zoned int vector type property. Length of passed values should match with vector length.
249 */
250 public void setZonedIntVectorProperty(int property, int zone, int[] values)
251 throws IllegalArgumentException {
252 VehiclePropValue v = VehiclePropValueUtil
253 .createZonedIntVectorValue(property, zone, values, 0);
254 setProperty(v);
255 }
256
257 /**
258 * Set zoned float vector type property. Length of passed values should match with vector
259 * length.
260 */
261 public void setZonedFloatVectorProperty(int property, int zone, float[] values)
262 throws IllegalArgumentException {
263 VehiclePropValue v = VehiclePropValueUtil
264 .createZonedFloatVectorValue(property, zone, values, 0);
265 setProperty(v);
266 }
267
268 /**
keunyoung5c7cb262015-10-19 10:47:45 -0700269 * Get property. This can be used for a property which does not require any other data.
keunyoung5c7cb262015-10-19 10:47:45 -0700270 */
271 public VehiclePropValue getProperty(int property) throws IllegalArgumentException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700272 int valueType = VehicleNetworkConsts.getVehicleValueType(property);
273 VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build();
274 return getProperty(value);
275 }
276
keunyoung5c7cb262015-10-19 10:47:45 -0700277 /**
278 * Generic get method for any type of property. Some property may require setting data portion
279 * as get may return different result depending on the data set.
keunyoung5c7cb262015-10-19 10:47:45 -0700280 */
281 public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700282 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
keunyounge18e25d2015-08-28 15:57:19 -0700283 try {
keunyoung7d74e6d2015-10-14 15:43:10 -0700284 VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable);
285 if (resParcelable != null) {
286 return resParcelable.value;
keunyounge18e25d2015-08-28 15:57:19 -0700287 }
288 } catch (RemoteException e) {
289 handleRemoteException(e);
290 }
291 return null;
292 }
293
keunyoung5c7cb262015-10-19 10:47:45 -0700294 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800295 * Get int type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700296 */
297 public int getIntProperty(int property) throws IllegalArgumentException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800298 VehiclePropValue v = getProperty(
299 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
keunyoungd32f4e62015-09-21 11:33:06 -0700300 if (v.getInt32ValuesCount() != 1) {
301 throw new IllegalStateException();
302 }
303 return v.getInt32Values(0);
304 }
305
keunyoung5c7cb262015-10-19 10:47:45 -0700306 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800307 * Get zoned int type property.
308 */
309 public int getZonedIntProperty(int property, int zone) throws IllegalArgumentException {
310 VehiclePropValue v = getProperty(
311 property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
312 if (v.getZonedValue().getInt32ValuesCount() != 1) {
313 throw new IllegalStateException();
314 }
315 return v.getZonedValue().getInt32Values(0);
316 }
317
318 /**
319 * Get int vector type property. Length of values should match vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700320 */
keunyoung4b0212c2015-10-29 17:11:57 -0700321 public int[] getIntVectorProperty(int property) throws IllegalArgumentException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800322 VehiclePropValue v = getProperty(
323 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
324 assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType());
325 return toIntArray(v.getInt32ValuesList());
326 }
327
328 /**
329 * Get zoned int vector type property. Length of values should match vector length.
330 */
331 public int[] getZonedIntVectorProperty(int property, int zone)
332 throws IllegalArgumentException {
333 VehiclePropValue v = getProperty(
334 property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
335 assertVectorLength(v.getZonedValue().getInt32ValuesCount(), property, v.getValueType());
336 return toIntArray(v.getZonedValue().getInt32ValuesList());
keunyoungfe30ba02015-09-17 17:56:35 -0700337 }
338
keunyoung5c7cb262015-10-19 10:47:45 -0700339 /**
340 * Get float type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700341 */
342 public float getFloatProperty(int property) throws IllegalArgumentException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800343 VehiclePropValue v = getProperty(
344 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT);
keunyoungd32f4e62015-09-21 11:33:06 -0700345 if (v.getFloatValuesCount() != 1) {
346 throw new IllegalStateException();
347 }
348 return v.getFloatValues(0);
349 }
350
keunyoung5c7cb262015-10-19 10:47:45 -0700351 /**
352 * Get float vector type property. Length of values should match vector's length.
keunyoung5c7cb262015-10-19 10:47:45 -0700353 */
Pavel Maltseve8f75372016-01-26 10:26:04 -0800354 public float[] getFloatVectorProperty(int property) throws IllegalArgumentException {
355 VehiclePropValue v = getProperty(
356 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT);
357 assertVectorLength(v.getFloatValuesCount(), property, v.getValueType());
358 return toFloatArray(v.getFloatValuesList());
359 }
360
361 /**
362 * Get zoned float vector type property. Length of values should match vector's length.
363 */
364 public float[] getZonedFloatVectorProperty(int property, int zone)
keunyoung5c7cb262015-10-19 10:47:45 -0700365 throws IllegalArgumentException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800366 VehiclePropValue v = getProperty(property, zone,
367 VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_FLOAT);
368 assertVectorLength(v.getZonedValue().getFloatValuesCount(), property, v.getValueType());
369 return toFloatArray(v.getZonedValue().getFloatValuesList());
keunyoungfe30ba02015-09-17 17:56:35 -0700370 }
371
keunyoung5c7cb262015-10-19 10:47:45 -0700372 /**
373 * Get long type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700374 */
375 public long getLongProperty(int property) throws IllegalArgumentException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800376 VehiclePropValue v = getProperty(
377 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT64);
keunyoungfe30ba02015-09-17 17:56:35 -0700378 return v.getInt64Value();
379 }
380
keunyoung5c7cb262015-10-19 10:47:45 -0700381 /**
382 * Get string type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700383 */
keunyoungfe30ba02015-09-17 17:56:35 -0700384 //TODO check UTF8 to java string conversion
keunyoung5c7cb262015-10-19 10:47:45 -0700385 public String getStringProperty(int property) throws IllegalArgumentException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800386 VehiclePropValue v = getProperty(
387 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_STRING);
keunyoungfe30ba02015-09-17 17:56:35 -0700388 return v.getStringValue();
389 }
390
keunyoung5c7cb262015-10-19 10:47:45 -0700391 /**
392 * Subscribe given property with given sample rate.
keunyoung5c7cb262015-10-19 10:47:45 -0700393 */
394 public void subscribe(int property, float sampleRate) throws IllegalArgumentException {
Keun-young Park0727f952015-12-21 14:30:07 -0800395 subscribe(property, sampleRate, 0);
396 }
397
398 /**
399 * Subscribe given property with given sample rate.
Keun-young Park0727f952015-12-21 14:30:07 -0800400 */
401 public void subscribe(int property, float sampleRate, int zones)
402 throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700403 try {
Keun-young Park0727f952015-12-21 14:30:07 -0800404 mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones);
keunyounge18e25d2015-08-28 15:57:19 -0700405 } catch (RemoteException e) {
406 handleRemoteException(e);
407 }
keunyounge18e25d2015-08-28 15:57:19 -0700408 }
409
keunyoung5c7cb262015-10-19 10:47:45 -0700410 /**
411 * Stop subscribing the property.
keunyoung5c7cb262015-10-19 10:47:45 -0700412 */
keunyounge18e25d2015-08-28 15:57:19 -0700413 public void unsubscribe(int property) {
414 try {
415 mService.unsubscribe(mVehicleNetworkListener, property);
416 } catch (RemoteException e) {
417 handleRemoteException(e);
418 }
419 }
420
keunyoung5c7cb262015-10-19 10:47:45 -0700421 /**
422 * Inject given value to all clients subscribing the property. This is for testing.
keunyoung5c7cb262015-10-19 10:47:45 -0700423 */
keunyoung1ab8e182015-09-24 09:25:22 -0700424 public synchronized void injectEvent(VehiclePropValue value) {
425 try {
426 mService.injectEvent(new VehiclePropValueParcelable(value));
427 } catch (RemoteException e) {
428 handleRemoteException(e);
429 }
430 }
431
keunyoung5c7cb262015-10-19 10:47:45 -0700432 /**
433 * Start mocking of vehicle HAL. For testing only.
keunyoung5c7cb262015-10-19 10:47:45 -0700434 */
keunyoung1ab8e182015-09-24 09:25:22 -0700435 public synchronized void startMocking(VehicleNetworkHalMock mock) {
436 mHalMock = mock;
437 mHalMockImpl = new IVehicleNetworkHalMockImpl(this);
438 try {
439 mService.startMocking(mHalMockImpl);
440 } catch (RemoteException e) {
441 handleRemoteException(e);
442 }
443 }
444
keunyoung5c7cb262015-10-19 10:47:45 -0700445 /**
446 * Stop mocking of vehicle HAL. For testing only.
447 */
keunyoung1ab8e182015-09-24 09:25:22 -0700448 public synchronized void stopMocking() {
Keun-young Park28dd4702015-11-19 18:06:04 -0800449 if (mHalMockImpl == null) {
450 return;
451 }
keunyoung1ab8e182015-09-24 09:25:22 -0700452 try {
453 mService.stopMocking(mHalMockImpl);
454 } catch (RemoteException e) {
455 handleRemoteException(e);
456 } finally {
457 mHalMock = null;
458 mHalMockImpl = null;
459 }
460 }
461
keunyoung5c7cb262015-10-19 10:47:45 -0700462 /**
463 * Start mocking of vehicle HAL. For testing only.
keunyoung5c7cb262015-10-19 10:47:45 -0700464 */
keunyoung1ab8e182015-09-24 09:25:22 -0700465 public synchronized void startMocking(IVehicleNetworkHalMock mock) {
466 mHalMock = null;
467 mHalMockImpl = mock;
468 try {
469 mService.startMocking(mHalMockImpl);
470 } catch (RemoteException e) {
471 handleRemoteException(e);
472 }
473 }
474
keunyoung5c7cb262015-10-19 10:47:45 -0700475 /**
476 * Stop mocking of vehicle HAL. For testing only.
477 */
keunyoung1ab8e182015-09-24 09:25:22 -0700478 public synchronized void stopMocking(IVehicleNetworkHalMock mock) {
479 if (mock.asBinder() != mHalMockImpl.asBinder()) {
480 return;
481 }
482 try {
483 mService.stopMocking(mHalMockImpl);
484 } catch (RemoteException e) {
485 handleRemoteException(e);
486 } finally {
487 mHalMock = null;
488 mHalMockImpl = null;
489 }
490 }
491
Keun-young Park28dd4702015-11-19 18:06:04 -0800492 public synchronized void injectHalError(int errorCode, int property, int operation) {
493 try {
494 mService.injectHalError(errorCode, property, operation);
495 } catch (RemoteException e) {
496 handleRemoteException(e);
497 }
498 }
499
500 public synchronized void startErrorListening() {
501 try {
502 mService.startErrorListening(mVehicleNetworkListener);
503 } catch (RemoteException e) {
504 handleRemoteException(e);
505 }
506 }
507
508 public synchronized void stopErrorListening() {
509 try {
510 mService.stopErrorListening(mVehicleNetworkListener);
511 } catch (RemoteException e) {
512 handleRemoteException(e);
513 }
514 }
515
516 public synchronized void startHalRestartMonitoring() {
517 try {
518 mService.startHalRestartMonitoring(mVehicleNetworkListener);
519 } catch (RemoteException e) {
520 handleRemoteException(e);
521 }
522 }
523
524 public synchronized void stopHalRestartMonitoring() {
525 try {
526 mService.stopHalRestartMonitoring(mVehicleNetworkListener);
527 } catch (RemoteException e) {
528 handleRemoteException(e);
529 }
530 }
531
keunyoung1ab8e182015-09-24 09:25:22 -0700532 private synchronized VehicleNetworkHalMock getHalMock() {
533 return mHalMock;
534 }
535
keunyounge18e25d2015-08-28 15:57:19 -0700536 private void handleRemoteException(RemoteException e) {
537 throw new RuntimeException("Vehicle network service not working ", e);
538 }
539
540 private void handleVehicleNetworkEvents(VehiclePropValues values) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800541 mListener.onVehicleNetworkEvents(values);
keunyounge18e25d2015-08-28 15:57:19 -0700542 }
543
Keun-young Park28dd4702015-11-19 18:06:04 -0800544 private void handleHalError(int errorCode, int property, int operation) {
545 mListener.onHalError(errorCode, property, operation);
546 }
547
548 private void handleHalRestart(boolean inMocking) {
549 mListener.onHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700550 }
551
552 private class EventHandler extends Handler {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800553
keunyounge18e25d2015-08-28 15:57:19 -0700554 private static final int MSG_EVENTS = 0;
Keun-young Park28dd4702015-11-19 18:06:04 -0800555 private static final int MSG_HAL_ERROR = 1;
556 private static final int MSG_HAL_RESTART = 2;
keunyounge18e25d2015-08-28 15:57:19 -0700557
558 private EventHandler(Looper looper) {
559 super(looper);
560 }
561
562 private void notifyEvents(VehiclePropValues values) {
563 Message msg = obtainMessage(MSG_EVENTS, values);
564 sendMessage(msg);
565 }
566
Keun-young Park28dd4702015-11-19 18:06:04 -0800567 private void notifyHalError(int errorCode, int property, int operation) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800568 Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property, operation);
Keun-young Park28dd4702015-11-19 18:06:04 -0800569 sendMessage(msg);
570 }
571
572 private void notifyHalRestart(boolean inMocking) {
573 Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0);
574 sendMessage(msg);
575 }
576
keunyounge18e25d2015-08-28 15:57:19 -0700577 @Override
578 public void handleMessage(Message msg) {
579 switch (msg.what) {
580 case MSG_EVENTS:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800581 handleVehicleNetworkEvents((VehiclePropValues) msg.obj);
Keun-young Park28dd4702015-11-19 18:06:04 -0800582 break;
583 case MSG_HAL_ERROR:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800584 handleHalError(msg.arg1, msg.arg2, (Integer) msg.obj);
Keun-young Park28dd4702015-11-19 18:06:04 -0800585 break;
586 case MSG_HAL_RESTART:
587 handleHalRestart(msg.arg1 == 1);
keunyounge18e25d2015-08-28 15:57:19 -0700588 break;
589 default:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800590 Log.w(TAG, "Unknown message:" + msg.what, new RuntimeException());
keunyounge18e25d2015-08-28 15:57:19 -0700591 break;
592 }
593 }
594 }
595
596 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800597
keunyounge18e25d2015-08-28 15:57:19 -0700598 private final WeakReference<VehicleNetwork> mVehicleNetwork;
599
600 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800601 mVehicleNetwork = new WeakReference<>(vehicleNewotk);
keunyounge18e25d2015-08-28 15:57:19 -0700602 }
603
604 @Override
605 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
606 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
607 if (vehicleNetwork != null) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800608 vehicleNetwork.mEventHandler.notifyEvents(values.values);
609 }
610 }
611
612 @Override
613 public void onHalError(int errorCode, int property, int operation) {
614 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
615 if (vehicleNetwork != null) {
616 vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation);
617 }
618 }
619
620 @Override
621 public void onHalRestart(boolean inMocking) {
622 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
623 if (vehicleNetwork != null) {
624 vehicleNetwork.mEventHandler.notifyHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700625 }
626 }
627 }
keunyoung1ab8e182015-09-24 09:25:22 -0700628
629 private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
630 private final WeakReference<VehicleNetwork> mVehicleNetwork;
631
632 private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800633 mVehicleNetwork = new WeakReference<>(vehicleNewotk);
keunyoung1ab8e182015-09-24 09:25:22 -0700634 }
635
636 @Override
637 public VehiclePropConfigsParcelable onListProperties() {
638 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
639 if (vehicleNetwork == null) {
640 return null;
641 }
642 VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties();
643 return new VehiclePropConfigsParcelable(configs);
644 }
645
646 @Override
647 public void onPropertySet(VehiclePropValueParcelable value) {
648 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
649 if (vehicleNetwork == null) {
650 return;
651 }
652 vehicleNetwork.getHalMock().onPropertySet(value.value);
653 }
654
655 @Override
keunyoung7d74e6d2015-10-14 15:43:10 -0700656 public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
keunyoung1ab8e182015-09-24 09:25:22 -0700657 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
658 if (vehicleNetwork == null) {
659 return null;
660 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700661 VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value);
662 return new VehiclePropValueParcelable(resValue);
keunyoung1ab8e182015-09-24 09:25:22 -0700663 }
664
665 @Override
Keun-young Park0727f952015-12-21 14:30:07 -0800666 public void onPropertySubscribe(int property, float sampleRate, int zones) {
keunyoung1ab8e182015-09-24 09:25:22 -0700667 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
668 if (vehicleNetwork == null) {
669 return;
670 }
Keun-young Park0727f952015-12-21 14:30:07 -0800671 vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate, zones);
keunyoung1ab8e182015-09-24 09:25:22 -0700672 }
673
674 @Override
675 public void onPropertyUnsubscribe(int property) {
676 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
677 if (vehicleNetwork == null) {
678 return;
679 }
680 vehicleNetwork.getHalMock().onPropertyUnsubscribe(property);
681 }
682 }
Pavel Maltseve8f75372016-01-26 10:26:04 -0800683
684 private VehiclePropValue getProperty(int property, int zone, int customPropetyDataType) {
685 boolean isCustom = isCustomProperty(property);
686 int valueType = isCustom
687 ? customPropetyDataType
688 : VehicleNetworkConsts.getVehicleValueType(property);
689
690 VehiclePropValue.Builder valuePrototypeBuilder =
691 VehiclePropValueUtil.createBuilder(property, valueType, 0);
692
693 if (zone != NO_ZONE) {
694 valuePrototypeBuilder.setZonedValue(ZonedValue.newBuilder().
695 setZoneOrWindow(zone).
696 build());
697 }
698
699 VehiclePropValue v = getProperty(valuePrototypeBuilder.build());
700 if (v == null) {
701 // if property is invalid, IllegalArgumentException should have been thrown
702 // from getProperty.
703 throw new IllegalStateException();
704 }
705
706 if (!isCustom && v.getValueType() != valueType) {
707 throw new IllegalArgumentException("Unexpected type: " + v.getValueType()
708 + ", expecting: " + valueType);
709 }
710 return v;
711 }
712
713 private void assertVectorLength(int actual, int property, int valueType) {
714 int expectedLen = getVectorLength(valueType);
715 if (expectedLen != actual) {
716 throw new IllegalStateException("Invalid array size for property: " + property
717 + ". Expected: " + expectedLen
718 + ", actual: " + actual);
719 }
720 }
keunyounge18e25d2015-08-28 15:57:19 -0700721}