blob: ee20d78ab7099fa6766bd74486807357f900a103 [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);
keunyoung1ab8e182015-09-24 09:25:22 -070055 void onPropertySubscribe(int property, int sampleRate);
56 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 {
keunyounge18e25d2015-08-28 15:57:19 -0700426 try {
keunyoung15882e52015-09-16 16:57:58 -0700427 mService.subscribe(mVehicleNetworkListener, property, sampleRate);
keunyounge18e25d2015-08-28 15:57:19 -0700428 } catch (RemoteException e) {
429 handleRemoteException(e);
430 }
keunyounge18e25d2015-08-28 15:57:19 -0700431 }
432
keunyoung5c7cb262015-10-19 10:47:45 -0700433 /**
434 * Stop subscribing the property.
435 * @param property
436 */
keunyounge18e25d2015-08-28 15:57:19 -0700437 public void unsubscribe(int property) {
438 try {
439 mService.unsubscribe(mVehicleNetworkListener, property);
440 } catch (RemoteException e) {
441 handleRemoteException(e);
442 }
443 }
444
keunyoung5c7cb262015-10-19 10:47:45 -0700445 /**
446 * Inject given value to all clients subscribing the property. This is for testing.
447 * @param value
448 */
keunyoung1ab8e182015-09-24 09:25:22 -0700449 public synchronized void injectEvent(VehiclePropValue value) {
450 try {
451 mService.injectEvent(new VehiclePropValueParcelable(value));
452 } catch (RemoteException e) {
453 handleRemoteException(e);
454 }
455 }
456
keunyoung5c7cb262015-10-19 10:47:45 -0700457 /**
458 * Start mocking of vehicle HAL. For testing only.
459 * @param mock
460 */
keunyoung1ab8e182015-09-24 09:25:22 -0700461 public synchronized void startMocking(VehicleNetworkHalMock mock) {
462 mHalMock = mock;
463 mHalMockImpl = new IVehicleNetworkHalMockImpl(this);
464 try {
465 mService.startMocking(mHalMockImpl);
466 } catch (RemoteException e) {
467 handleRemoteException(e);
468 }
469 }
470
keunyoung5c7cb262015-10-19 10:47:45 -0700471 /**
472 * Stop mocking of vehicle HAL. For testing only.
473 */
keunyoung1ab8e182015-09-24 09:25:22 -0700474 public synchronized void stopMocking() {
Keun-young Park28dd4702015-11-19 18:06:04 -0800475 if (mHalMockImpl == null) {
476 return;
477 }
keunyoung1ab8e182015-09-24 09:25:22 -0700478 try {
479 mService.stopMocking(mHalMockImpl);
480 } catch (RemoteException e) {
481 handleRemoteException(e);
482 } finally {
483 mHalMock = null;
484 mHalMockImpl = null;
485 }
486 }
487
keunyoung5c7cb262015-10-19 10:47:45 -0700488 /**
489 * Start mocking of vehicle HAL. For testing only.
490 * @param mock
491 */
keunyoung1ab8e182015-09-24 09:25:22 -0700492 public synchronized void startMocking(IVehicleNetworkHalMock mock) {
493 mHalMock = null;
494 mHalMockImpl = mock;
495 try {
496 mService.startMocking(mHalMockImpl);
497 } catch (RemoteException e) {
498 handleRemoteException(e);
499 }
500 }
501
keunyoung5c7cb262015-10-19 10:47:45 -0700502 /**
503 * Stop mocking of vehicle HAL. For testing only.
504 */
keunyoung1ab8e182015-09-24 09:25:22 -0700505 public synchronized void stopMocking(IVehicleNetworkHalMock mock) {
506 if (mock.asBinder() != mHalMockImpl.asBinder()) {
507 return;
508 }
509 try {
510 mService.stopMocking(mHalMockImpl);
511 } catch (RemoteException e) {
512 handleRemoteException(e);
513 } finally {
514 mHalMock = null;
515 mHalMockImpl = null;
516 }
517 }
518
Keun-young Park28dd4702015-11-19 18:06:04 -0800519 public synchronized void injectHalError(int errorCode, int property, int operation) {
520 try {
521 mService.injectHalError(errorCode, property, operation);
522 } catch (RemoteException e) {
523 handleRemoteException(e);
524 }
525 }
526
527 public synchronized void startErrorListening() {
528 try {
529 mService.startErrorListening(mVehicleNetworkListener);
530 } catch (RemoteException e) {
531 handleRemoteException(e);
532 }
533 }
534
535 public synchronized void stopErrorListening() {
536 try {
537 mService.stopErrorListening(mVehicleNetworkListener);
538 } catch (RemoteException e) {
539 handleRemoteException(e);
540 }
541 }
542
543 public synchronized void startHalRestartMonitoring() {
544 try {
545 mService.startHalRestartMonitoring(mVehicleNetworkListener);
546 } catch (RemoteException e) {
547 handleRemoteException(e);
548 }
549 }
550
551 public synchronized void stopHalRestartMonitoring() {
552 try {
553 mService.stopHalRestartMonitoring(mVehicleNetworkListener);
554 } catch (RemoteException e) {
555 handleRemoteException(e);
556 }
557 }
558
keunyoung1ab8e182015-09-24 09:25:22 -0700559 private synchronized VehicleNetworkHalMock getHalMock() {
560 return mHalMock;
561 }
562
keunyounge18e25d2015-08-28 15:57:19 -0700563 private void handleRemoteException(RemoteException e) {
564 throw new RuntimeException("Vehicle network service not working ", e);
565 }
566
567 private void handleVehicleNetworkEvents(VehiclePropValues values) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800568 mListener.onVehicleNetworkEvents(values);
keunyounge18e25d2015-08-28 15:57:19 -0700569 }
570
Keun-young Park28dd4702015-11-19 18:06:04 -0800571 private void handleHalError(int errorCode, int property, int operation) {
572 mListener.onHalError(errorCode, property, operation);
573 }
574
575 private void handleHalRestart(boolean inMocking) {
576 mListener.onHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700577 }
578
579 private class EventHandler extends Handler {
580 private static final int MSG_EVENTS = 0;
Keun-young Park28dd4702015-11-19 18:06:04 -0800581 private static final int MSG_HAL_ERROR = 1;
582 private static final int MSG_HAL_RESTART = 2;
keunyounge18e25d2015-08-28 15:57:19 -0700583
584 private EventHandler(Looper looper) {
585 super(looper);
586 }
587
588 private void notifyEvents(VehiclePropValues values) {
589 Message msg = obtainMessage(MSG_EVENTS, values);
590 sendMessage(msg);
591 }
592
Keun-young Park28dd4702015-11-19 18:06:04 -0800593 private void notifyHalError(int errorCode, int property, int operation) {
594 Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property,
595 Integer.valueOf(operation));
596 sendMessage(msg);
597 }
598
599 private void notifyHalRestart(boolean inMocking) {
600 Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0);
601 sendMessage(msg);
602 }
603
keunyounge18e25d2015-08-28 15:57:19 -0700604 @Override
605 public void handleMessage(Message msg) {
606 switch (msg.what) {
607 case MSG_EVENTS:
Keun-young Park28dd4702015-11-19 18:06:04 -0800608 handleVehicleNetworkEvents((VehiclePropValues)msg.obj);
609 break;
610 case MSG_HAL_ERROR:
611 handleHalError(msg.arg1, msg.arg2, (Integer)msg.obj);
612 break;
613 case MSG_HAL_RESTART:
614 handleHalRestart(msg.arg1 == 1);
keunyounge18e25d2015-08-28 15:57:19 -0700615 break;
616 default:
617 Log.w(TAG, "unown message:" + msg.what, new RuntimeException());
618 break;
619 }
620 }
621 }
622
623 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
624 private final WeakReference<VehicleNetwork> mVehicleNetwork;
625
626 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
627 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
628 }
629
630 @Override
631 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
632 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
633 if (vehicleNetwork != null) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800634 vehicleNetwork.mEventHandler.notifyEvents(values.values);
635 }
636 }
637
638 @Override
639 public void onHalError(int errorCode, int property, int operation) {
640 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
641 if (vehicleNetwork != null) {
642 vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation);
643 }
644 }
645
646 @Override
647 public void onHalRestart(boolean inMocking) {
648 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
649 if (vehicleNetwork != null) {
650 vehicleNetwork.mEventHandler.notifyHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700651 }
652 }
653 }
keunyoung1ab8e182015-09-24 09:25:22 -0700654
655 private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
656 private final WeakReference<VehicleNetwork> mVehicleNetwork;
657
658 private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) {
659 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
660 }
661
662 @Override
663 public VehiclePropConfigsParcelable onListProperties() {
664 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
665 if (vehicleNetwork == null) {
666 return null;
667 }
668 VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties();
669 return new VehiclePropConfigsParcelable(configs);
670 }
671
672 @Override
673 public void onPropertySet(VehiclePropValueParcelable value) {
674 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
675 if (vehicleNetwork == null) {
676 return;
677 }
678 vehicleNetwork.getHalMock().onPropertySet(value.value);
679 }
680
681 @Override
keunyoung7d74e6d2015-10-14 15:43:10 -0700682 public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
keunyoung1ab8e182015-09-24 09:25:22 -0700683 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
684 if (vehicleNetwork == null) {
685 return null;
686 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700687 VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value);
688 return new VehiclePropValueParcelable(resValue);
keunyoung1ab8e182015-09-24 09:25:22 -0700689 }
690
691 @Override
692 public void onPropertySubscribe(int property, int sampleRate) {
693 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
694 if (vehicleNetwork == null) {
695 return;
696 }
697 vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate);
698 }
699
700 @Override
701 public void onPropertyUnsubscribe(int property) {
702 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
703 if (vehicleNetwork == null) {
704 return;
705 }
706 vehicleNetwork.getHalMock().onPropertyUnsubscribe(property);
707 }
708 }
keunyounge18e25d2015-08-28 15:57:19 -0700709}