blob: a43c485927905b18dc371aa8f9c9b92375d42376 [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
18import android.annotation.Nullable;
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.util.Log;
25
keunyoungd32f4e62015-09-21 11:33:06 -070026import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
keunyounge18e25d2015-08-28 15:57:19 -070027import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
28import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
29import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
keunyoung1ab8e182015-09-24 09:25:22 -070030import com.android.internal.annotations.GuardedBy;
keunyounge18e25d2015-08-28 15:57:19 -070031
32import java.lang.ref.WeakReference;
33
34/**
35 * System API to access Vehicle network. This is only for system services and applications should
keunyoung15882e52015-09-16 16:57:58 -070036 * not use this. All APIs will fail with security error if normal app tries this.
keunyounge18e25d2015-08-28 15:57:19 -070037 */
38public class VehicleNetwork {
keunyoung1ab8e182015-09-24 09:25:22 -070039 /**
40 * Listener for VNS events.
41 */
keunyounge18e25d2015-08-28 15:57:19 -070042 public interface VehicleNetworkListener {
keunyoung1ab8e182015-09-24 09:25:22 -070043 /**
44 * Notify HAL events. This requires subscribing the property
45 */
keunyounge18e25d2015-08-28 15:57:19 -070046 void onVehicleNetworkEvents(VehiclePropValues values);
Keun-young Park28dd4702015-11-19 18:06:04 -080047 void onHalError(int errorCode, int property, int operation);
48 void onHalRestart(boolean inMocking);
keunyounge18e25d2015-08-28 15:57:19 -070049 }
50
keunyoung1ab8e182015-09-24 09:25:22 -070051 public interface VehicleNetworkHalMock {
52 VehiclePropConfigs onListProperties();
53 void onPropertySet(VehiclePropValue value);
Keun-young Park28dd4702015-11-19 18:06:04 -080054 VehiclePropValue onPropertyGet(VehiclePropValue value);
Keun-young Park0727f952015-12-21 14:30:07 -080055 void onPropertySubscribe(int property, float sampleRate, int zones);
keunyoung1ab8e182015-09-24 09:25:22 -070056 void onPropertyUnsubscribe(int property);
57 }
58
keunyounge18e25d2015-08-28 15:57:19 -070059 private static final String TAG = VehicleNetwork.class.getSimpleName();
60
61 private final IVehicleNetwork mService;
62 private final VehicleNetworkListener mListener;
63 private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
64 private final EventHandler mEventHandler;
65
keunyoung1ab8e182015-09-24 09:25:22 -070066 @GuardedBy("this")
67 private VehicleNetworkHalMock mHalMock;
68 private IVehicleNetworkHalMock mHalMockImpl;
69
keunyoung217ca352015-11-17 14:45:38 -080070 private static final int VNS_CONNECT_MAX_RETRY = 10;
71 private static final long VNS_RETRY_WAIT_TIME_MS = 1000;
72
keunyoung5c7cb262015-10-19 10:47:45 -070073 /**
74 * Factory method to create VehicleNetwork
75 * @param listener listener for listening events
76 * @param looper Looper to dispatch listener events
77 * @return
78 */
keunyoung15882e52015-09-16 16:57:58 -070079 public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
80 Looper looper) {
keunyoung217ca352015-11-17 14:45:38 -080081 int retryCount = 0;
82 IVehicleNetwork service = null;
83 while (service == null) {
84 service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
85 IVehicleNetwork.class.getCanonicalName()));
86 retryCount++;
87 if (retryCount > VNS_CONNECT_MAX_RETRY) {
88 break;
89 }
90 try {
91 Thread.sleep(VNS_RETRY_WAIT_TIME_MS);
92 } catch (InterruptedException e) {
93 //ignore
94 }
95 }
keunyounge18e25d2015-08-28 15:57:19 -070096 if (service == null) {
keunyoung15882e52015-09-16 16:57:58 -070097 throw new RuntimeException("Vehicle network service not available:" +
98 IVehicleNetwork.class.getCanonicalName());
keunyounge18e25d2015-08-28 15:57:19 -070099 }
100 return new VehicleNetwork(service, listener, looper);
101 }
102
103 private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
104 Looper looper) {
105 mService = service;
106 mListener = listener;
107 mEventHandler = new EventHandler(looper);
108 mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
109 }
110
keunyoung5c7cb262015-10-19 10:47:45 -0700111 /**
112 * List all properties from vehicle HAL
113 * @return all properties
114 */
keunyoung15882e52015-09-16 16:57:58 -0700115 public VehiclePropConfigs listProperties() {
116 return listProperties(0 /* all */);
117 }
118
keunyoung5c7cb262015-10-19 10:47:45 -0700119 /**
120 * Return configuration information of single property
121 * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has
122 * has special meaning of list all properties.
123 * @return null if given property does not exist.
124 */
keunyounge18e25d2015-08-28 15:57:19 -0700125 public VehiclePropConfigs listProperties(int property) {
126 try {
127 VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
128 if (parcelable != null) {
129 return parcelable.configs;
130 }
131 } catch (RemoteException e) {
132 handleRemoteException(e);
133 }
134 return null;
135 }
136
keunyoung5c7cb262015-10-19 10:47:45 -0700137 /**
138 * Set property which will lead into writing the value to vehicle HAL.
139 * @param value
140 * @throws IllegalArgumentException If value set has wrong value like wrong valueType,
141 * wrong data, and etc.
142 */
143 public void setProperty(VehiclePropValue value) throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700144 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
145 try {
keunyoung15882e52015-09-16 16:57:58 -0700146 mService.setProperty(parcelable);
keunyounge18e25d2015-08-28 15:57:19 -0700147 } catch (RemoteException e) {
148 handleRemoteException(e);
149 }
keunyounge18e25d2015-08-28 15:57:19 -0700150 }
151
keunyoung5c7cb262015-10-19 10:47:45 -0700152 /**
153 * Set integer type property
154 * @param property
155 * @param value
156 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
157 */
158 public void setIntProperty(int property, int value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700159 VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700160 setProperty(v);
161 }
162
keunyoung5c7cb262015-10-19 10:47:45 -0700163 /**
164 * Set int vector type property. Length of passed values should match with vector length.
165 * @param property
166 * @param values
167 * @throws IllegalArgumentException
168 */
169 public void setIntVectorProperty(int property, int[] values) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700170 VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0);
171 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700172 }
173
keunyoung5c7cb262015-10-19 10:47:45 -0700174 /**
175 * Set long type property.
176 * @param property
177 * @param value
178 * @throws IllegalArgumentException
179 */
180 public void setLongProperty(int property, long value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700181 VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700182 setProperty(v);
183 }
184
keunyoung5c7cb262015-10-19 10:47:45 -0700185 /**
186 * Set float type property.
187 * @param property
188 * @param value
189 * @throws IllegalArgumentException
190 */
191 public void setFloatProperty(int property, float value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700192 VehiclePropValue v = VehiclePropValueUtil.createFloatValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700193 setProperty(v);
194 }
195
keunyoung5c7cb262015-10-19 10:47:45 -0700196 /**
197 * Set float vector type property. Length of values should match with vector length.
198 * @param property
199 * @param values
200 * @throws IllegalArgumentException
201 */
202 public void setFloatVectorProperty(int property, float[] values)
203 throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700204 VehiclePropValue v = VehiclePropValueUtil.createFloatVectorValue(property, values, 0);
205 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700206 }
207
keunyoung5c7cb262015-10-19 10:47:45 -0700208 /**
Steve Paik66481982015-10-27 15:22:38 -0700209 * Set zoned boolean type property
210 * @param property
211 * @param zone
212 * @param value
213 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
214 */
215 public void setZonedBooleanProperty(int property, int zone, boolean value)
216 throws IllegalArgumentException {
217 VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0);
218 setProperty(v);
219 }
220
221 /**
222 * Set zoned float type property
223 * @param property
224 * @param zone
225 * @param value
226 * @throws IllegalArgumentException For type mismatch (=the property is not float type)
227 */
228 public void setZonedFloatProperty(int property, int zone, float value)
229 throws IllegalArgumentException {
230 VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0);
231 setProperty(v);
232 }
233
234 /**
235 * Set zoned integer type property
236 * @param property
237 * @param zone
238 * @param value
239 * @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 /**
keunyoung5c7cb262015-10-19 10:47:45 -0700248 * Get property. This can be used for a property which does not require any other data.
249 * @param property
250 * @return
251 * @throws IllegalArgumentException
252 */
253 public VehiclePropValue getProperty(int property) throws IllegalArgumentException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700254 int valueType = VehicleNetworkConsts.getVehicleValueType(property);
255 VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build();
256 return getProperty(value);
257 }
258
keunyoung5c7cb262015-10-19 10:47:45 -0700259 /**
260 * Generic get method for any type of property. Some property may require setting data portion
261 * as get may return different result depending on the data set.
262 * @param value
263 * @return
264 * @throws IllegalArgumentException
265 */
266 public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700267 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
keunyounge18e25d2015-08-28 15:57:19 -0700268 try {
keunyoung7d74e6d2015-10-14 15:43:10 -0700269 VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable);
270 if (resParcelable != null) {
271 return resParcelable.value;
keunyounge18e25d2015-08-28 15:57:19 -0700272 }
273 } catch (RemoteException e) {
274 handleRemoteException(e);
275 }
276 return null;
277 }
278
keunyoung5c7cb262015-10-19 10:47:45 -0700279 /**
280 * get int type property
281 * @param property
282 * @return
283 * @throws IllegalArgumentException
284 */
285 public int getIntProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700286 VehiclePropValue v = getProperty(property);
287 if (v == null) {
keunyoungd32f4e62015-09-21 11:33:06 -0700288 // if property is invalid, IllegalArgumentException should have been thrown
289 // from getProperty.
keunyoungfe30ba02015-09-17 17:56:35 -0700290 throw new IllegalStateException();
291 }
keunyoungd32f4e62015-09-21 11:33:06 -0700292 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT32) {
keunyoungfe30ba02015-09-17 17:56:35 -0700293 throw new IllegalArgumentException();
294 }
keunyoungd32f4e62015-09-21 11:33:06 -0700295 if (v.getInt32ValuesCount() != 1) {
296 throw new IllegalStateException();
297 }
298 return v.getInt32Values(0);
299 }
300
keunyoung5c7cb262015-10-19 10:47:45 -0700301 /**
302 * get int vector type property. Length of values should match vector length.
303 * @param property
keunyoung5c7cb262015-10-19 10:47:45 -0700304 * @throws IllegalArgumentException
305 */
keunyoung4b0212c2015-10-29 17:11:57 -0700306 public int[] getIntVectorProperty(int property) throws IllegalArgumentException {
keunyoungd32f4e62015-09-21 11:33:06 -0700307 VehiclePropValue v = getProperty(property);
308 if (v == null) {
309 // if property is invalid, IllegalArgumentException should have been thrown
310 // from getProperty.
311 throw new IllegalStateException();
312 }
313 switch (v.getValueType()) {
314 case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2:
315 case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC3:
316 case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC4:
keunyoungd32f4e62015-09-21 11:33:06 -0700317 break;
318 default:
319 throw new IllegalArgumentException();
320 }
keunyoung4b0212c2015-10-29 17:11:57 -0700321 int[] values = new int[v.getValueType() - VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2 +
322 2];
keunyoungd32f4e62015-09-21 11:33:06 -0700323 if (v.getInt32ValuesCount() != values.length) {
324 throw new IllegalStateException();
325 }
326 for (int i = 0; i < values.length; i++) {
327 values[i] = v.getInt32Values(i);
328 }
keunyoung4b0212c2015-10-29 17:11:57 -0700329 return values;
keunyoungfe30ba02015-09-17 17:56:35 -0700330 }
331
keunyoung5c7cb262015-10-19 10:47:45 -0700332 /**
333 * Get float type property.
334 * @param property
335 * @return
336 * @throws IllegalArgumentException
337 */
338 public float getFloatProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700339 VehiclePropValue v = getProperty(property);
340 if (v == null) {
341 throw new IllegalStateException();
342 }
keunyoungd32f4e62015-09-21 11:33:06 -0700343 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT) {
keunyoungfe30ba02015-09-17 17:56:35 -0700344 throw new IllegalArgumentException();
345 }
keunyoungd32f4e62015-09-21 11:33:06 -0700346 if (v.getFloatValuesCount() != 1) {
347 throw new IllegalStateException();
348 }
349 return v.getFloatValues(0);
350 }
351
keunyoung5c7cb262015-10-19 10:47:45 -0700352 /**
353 * Get float vector type property. Length of values should match vector's length.
354 * @param property
keunyoung5c7cb262015-10-19 10:47:45 -0700355 * @throws IllegalArgumentException
356 */
keunyoung4b0212c2015-10-29 17:11:57 -0700357 public float[] getFloatVectorProperty(int property)
keunyoung5c7cb262015-10-19 10:47:45 -0700358 throws IllegalArgumentException {
keunyoungd32f4e62015-09-21 11:33:06 -0700359 VehiclePropValue v = getProperty(property);
360 if (v == null) {
361 // if property is invalid, IllegalArgumentException should have been thrown
362 // from getProperty.
363 throw new IllegalStateException();
364 }
365 switch (v.getValueType()) {
366 case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2:
367 case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC3:
368 case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC4:
keunyoungd32f4e62015-09-21 11:33:06 -0700369 break;
370 default:
371 throw new IllegalArgumentException();
372 }
keunyoung4b0212c2015-10-29 17:11:57 -0700373 float[] values = new float[v.getValueType() -
374 VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2 + 2];
keunyoungd32f4e62015-09-21 11:33:06 -0700375 if (v.getFloatValuesCount() != values.length) {
376 throw new IllegalStateException();
377 }
378 for (int i = 0; i < values.length; i++) {
379 values[i] = v.getFloatValues(i);
380 }
keunyoung4b0212c2015-10-29 17:11:57 -0700381 return values;
keunyoungfe30ba02015-09-17 17:56:35 -0700382 }
383
keunyoung5c7cb262015-10-19 10:47:45 -0700384 /**
385 * Get long type property.
386 * @param property
387 * @return
388 * @throws IllegalArgumentException
389 */
390 public long getLongProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700391 VehiclePropValue v = getProperty(property);
392 if (v == null) {
393 throw new IllegalStateException();
394 }
keunyoungd32f4e62015-09-21 11:33:06 -0700395 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT64) {
keunyoungfe30ba02015-09-17 17:56:35 -0700396 throw new IllegalArgumentException();
397 }
398 return v.getInt64Value();
399 }
400
keunyoung5c7cb262015-10-19 10:47:45 -0700401 /**
402 * Get string type property.
403 * @param property
404 * @return
405 * @throws IllegalArgumentException
406 */
keunyoungfe30ba02015-09-17 17:56:35 -0700407 //TODO check UTF8 to java string conversion
keunyoung5c7cb262015-10-19 10:47:45 -0700408 public String getStringProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700409 VehiclePropValue v = getProperty(property);
410 if (v == null) {
411 throw new IllegalStateException();
412 }
keunyoungd32f4e62015-09-21 11:33:06 -0700413 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_STRING) {
keunyoungfe30ba02015-09-17 17:56:35 -0700414 throw new IllegalArgumentException();
415 }
416 return v.getStringValue();
417 }
418
keunyoung5c7cb262015-10-19 10:47:45 -0700419 /**
420 * Subscribe given property with given sample rate.
421 * @param property
422 * @param sampleRate
423 * @throws IllegalArgumentException
424 */
425 public void subscribe(int property, float sampleRate) throws IllegalArgumentException {
Keun-young Park0727f952015-12-21 14:30:07 -0800426 subscribe(property, sampleRate, 0);
427 }
428
429 /**
430 * Subscribe given property with given sample rate.
431 * @param property
432 * @param sampleRate
433 * @throws IllegalArgumentException
434 */
435 public void subscribe(int property, float sampleRate, int zones)
436 throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700437 try {
Keun-young Park0727f952015-12-21 14:30:07 -0800438 mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones);
keunyounge18e25d2015-08-28 15:57:19 -0700439 } catch (RemoteException e) {
440 handleRemoteException(e);
441 }
keunyounge18e25d2015-08-28 15:57:19 -0700442 }
443
keunyoung5c7cb262015-10-19 10:47:45 -0700444 /**
445 * Stop subscribing the property.
446 * @param property
447 */
keunyounge18e25d2015-08-28 15:57:19 -0700448 public void unsubscribe(int property) {
449 try {
450 mService.unsubscribe(mVehicleNetworkListener, property);
451 } catch (RemoteException e) {
452 handleRemoteException(e);
453 }
454 }
455
keunyoung5c7cb262015-10-19 10:47:45 -0700456 /**
457 * Inject given value to all clients subscribing the property. This is for testing.
458 * @param value
459 */
keunyoung1ab8e182015-09-24 09:25:22 -0700460 public synchronized void injectEvent(VehiclePropValue value) {
461 try {
462 mService.injectEvent(new VehiclePropValueParcelable(value));
463 } catch (RemoteException e) {
464 handleRemoteException(e);
465 }
466 }
467
keunyoung5c7cb262015-10-19 10:47:45 -0700468 /**
469 * Start mocking of vehicle HAL. For testing only.
470 * @param mock
471 */
keunyoung1ab8e182015-09-24 09:25:22 -0700472 public synchronized void startMocking(VehicleNetworkHalMock mock) {
473 mHalMock = mock;
474 mHalMockImpl = new IVehicleNetworkHalMockImpl(this);
475 try {
476 mService.startMocking(mHalMockImpl);
477 } catch (RemoteException e) {
478 handleRemoteException(e);
479 }
480 }
481
keunyoung5c7cb262015-10-19 10:47:45 -0700482 /**
483 * Stop mocking of vehicle HAL. For testing only.
484 */
keunyoung1ab8e182015-09-24 09:25:22 -0700485 public synchronized void stopMocking() {
Keun-young Park28dd4702015-11-19 18:06:04 -0800486 if (mHalMockImpl == null) {
487 return;
488 }
keunyoung1ab8e182015-09-24 09:25:22 -0700489 try {
490 mService.stopMocking(mHalMockImpl);
491 } catch (RemoteException e) {
492 handleRemoteException(e);
493 } finally {
494 mHalMock = null;
495 mHalMockImpl = null;
496 }
497 }
498
keunyoung5c7cb262015-10-19 10:47:45 -0700499 /**
500 * Start mocking of vehicle HAL. For testing only.
501 * @param mock
502 */
keunyoung1ab8e182015-09-24 09:25:22 -0700503 public synchronized void startMocking(IVehicleNetworkHalMock mock) {
504 mHalMock = null;
505 mHalMockImpl = mock;
506 try {
507 mService.startMocking(mHalMockImpl);
508 } catch (RemoteException e) {
509 handleRemoteException(e);
510 }
511 }
512
keunyoung5c7cb262015-10-19 10:47:45 -0700513 /**
514 * Stop mocking of vehicle HAL. For testing only.
515 */
keunyoung1ab8e182015-09-24 09:25:22 -0700516 public synchronized void stopMocking(IVehicleNetworkHalMock mock) {
517 if (mock.asBinder() != mHalMockImpl.asBinder()) {
518 return;
519 }
520 try {
521 mService.stopMocking(mHalMockImpl);
522 } catch (RemoteException e) {
523 handleRemoteException(e);
524 } finally {
525 mHalMock = null;
526 mHalMockImpl = null;
527 }
528 }
529
Keun-young Park28dd4702015-11-19 18:06:04 -0800530 public synchronized void injectHalError(int errorCode, int property, int operation) {
531 try {
532 mService.injectHalError(errorCode, property, operation);
533 } catch (RemoteException e) {
534 handleRemoteException(e);
535 }
536 }
537
538 public synchronized void startErrorListening() {
539 try {
540 mService.startErrorListening(mVehicleNetworkListener);
541 } catch (RemoteException e) {
542 handleRemoteException(e);
543 }
544 }
545
546 public synchronized void stopErrorListening() {
547 try {
548 mService.stopErrorListening(mVehicleNetworkListener);
549 } catch (RemoteException e) {
550 handleRemoteException(e);
551 }
552 }
553
554 public synchronized void startHalRestartMonitoring() {
555 try {
556 mService.startHalRestartMonitoring(mVehicleNetworkListener);
557 } catch (RemoteException e) {
558 handleRemoteException(e);
559 }
560 }
561
562 public synchronized void stopHalRestartMonitoring() {
563 try {
564 mService.stopHalRestartMonitoring(mVehicleNetworkListener);
565 } catch (RemoteException e) {
566 handleRemoteException(e);
567 }
568 }
569
keunyoung1ab8e182015-09-24 09:25:22 -0700570 private synchronized VehicleNetworkHalMock getHalMock() {
571 return mHalMock;
572 }
573
keunyounge18e25d2015-08-28 15:57:19 -0700574 private void handleRemoteException(RemoteException e) {
575 throw new RuntimeException("Vehicle network service not working ", e);
576 }
577
578 private void handleVehicleNetworkEvents(VehiclePropValues values) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800579 mListener.onVehicleNetworkEvents(values);
keunyounge18e25d2015-08-28 15:57:19 -0700580 }
581
Keun-young Park28dd4702015-11-19 18:06:04 -0800582 private void handleHalError(int errorCode, int property, int operation) {
583 mListener.onHalError(errorCode, property, operation);
584 }
585
586 private void handleHalRestart(boolean inMocking) {
587 mListener.onHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700588 }
589
590 private class EventHandler extends Handler {
591 private static final int MSG_EVENTS = 0;
Keun-young Park28dd4702015-11-19 18:06:04 -0800592 private static final int MSG_HAL_ERROR = 1;
593 private static final int MSG_HAL_RESTART = 2;
keunyounge18e25d2015-08-28 15:57:19 -0700594
595 private EventHandler(Looper looper) {
596 super(looper);
597 }
598
599 private void notifyEvents(VehiclePropValues values) {
600 Message msg = obtainMessage(MSG_EVENTS, values);
601 sendMessage(msg);
602 }
603
Keun-young Park28dd4702015-11-19 18:06:04 -0800604 private void notifyHalError(int errorCode, int property, int operation) {
605 Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property,
606 Integer.valueOf(operation));
607 sendMessage(msg);
608 }
609
610 private void notifyHalRestart(boolean inMocking) {
611 Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0);
612 sendMessage(msg);
613 }
614
keunyounge18e25d2015-08-28 15:57:19 -0700615 @Override
616 public void handleMessage(Message msg) {
617 switch (msg.what) {
618 case MSG_EVENTS:
Keun-young Park28dd4702015-11-19 18:06:04 -0800619 handleVehicleNetworkEvents((VehiclePropValues)msg.obj);
620 break;
621 case MSG_HAL_ERROR:
622 handleHalError(msg.arg1, msg.arg2, (Integer)msg.obj);
623 break;
624 case MSG_HAL_RESTART:
625 handleHalRestart(msg.arg1 == 1);
keunyounge18e25d2015-08-28 15:57:19 -0700626 break;
627 default:
628 Log.w(TAG, "unown message:" + msg.what, new RuntimeException());
629 break;
630 }
631 }
632 }
633
634 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
635 private final WeakReference<VehicleNetwork> mVehicleNetwork;
636
637 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
638 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
639 }
640
641 @Override
642 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
643 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
644 if (vehicleNetwork != null) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800645 vehicleNetwork.mEventHandler.notifyEvents(values.values);
646 }
647 }
648
649 @Override
650 public void onHalError(int errorCode, int property, int operation) {
651 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
652 if (vehicleNetwork != null) {
653 vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation);
654 }
655 }
656
657 @Override
658 public void onHalRestart(boolean inMocking) {
659 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
660 if (vehicleNetwork != null) {
661 vehicleNetwork.mEventHandler.notifyHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700662 }
663 }
664 }
keunyoung1ab8e182015-09-24 09:25:22 -0700665
666 private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
667 private final WeakReference<VehicleNetwork> mVehicleNetwork;
668
669 private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) {
670 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
671 }
672
673 @Override
674 public VehiclePropConfigsParcelable onListProperties() {
675 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
676 if (vehicleNetwork == null) {
677 return null;
678 }
679 VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties();
680 return new VehiclePropConfigsParcelable(configs);
681 }
682
683 @Override
684 public void onPropertySet(VehiclePropValueParcelable value) {
685 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
686 if (vehicleNetwork == null) {
687 return;
688 }
689 vehicleNetwork.getHalMock().onPropertySet(value.value);
690 }
691
692 @Override
keunyoung7d74e6d2015-10-14 15:43:10 -0700693 public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
keunyoung1ab8e182015-09-24 09:25:22 -0700694 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
695 if (vehicleNetwork == null) {
696 return null;
697 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700698 VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value);
699 return new VehiclePropValueParcelable(resValue);
keunyoung1ab8e182015-09-24 09:25:22 -0700700 }
701
702 @Override
Keun-young Park0727f952015-12-21 14:30:07 -0800703 public void onPropertySubscribe(int property, float sampleRate, int zones) {
keunyoung1ab8e182015-09-24 09:25:22 -0700704 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
705 if (vehicleNetwork == null) {
706 return;
707 }
Keun-young Park0727f952015-12-21 14:30:07 -0800708 vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate, zones);
keunyoung1ab8e182015-09-24 09:25:22 -0700709 }
710
711 @Override
712 public void onPropertyUnsubscribe(int property) {
713 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
714 if (vehicleNetwork == null) {
715 return;
716 }
717 vehicleNetwork.getHalMock().onPropertyUnsubscribe(property);
718 }
719 }
keunyounge18e25d2015-08-28 15:57:19 -0700720}