blob: 1771e39116fb54f31f93a0a76bd4d7066cc8af91 [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);
47 }
48
keunyoung1ab8e182015-09-24 09:25:22 -070049 public interface VehicleNetworkHalMock {
50 VehiclePropConfigs onListProperties();
51 void onPropertySet(VehiclePropValue value);
keunyoung7d74e6d2015-10-14 15:43:10 -070052 VehiclePropValue onPropertyGet(VehiclePropValue property);
keunyoung1ab8e182015-09-24 09:25:22 -070053 void onPropertySubscribe(int property, int sampleRate);
54 void onPropertyUnsubscribe(int property);
55 }
56
keunyounge18e25d2015-08-28 15:57:19 -070057 private static final String TAG = VehicleNetwork.class.getSimpleName();
58
59 private final IVehicleNetwork mService;
60 private final VehicleNetworkListener mListener;
61 private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
62 private final EventHandler mEventHandler;
63
keunyoung1ab8e182015-09-24 09:25:22 -070064 @GuardedBy("this")
65 private VehicleNetworkHalMock mHalMock;
66 private IVehicleNetworkHalMock mHalMockImpl;
67
keunyoung217ca352015-11-17 14:45:38 -080068 private static final int VNS_CONNECT_MAX_RETRY = 10;
69 private static final long VNS_RETRY_WAIT_TIME_MS = 1000;
70
keunyoung5c7cb262015-10-19 10:47:45 -070071 /**
72 * Factory method to create VehicleNetwork
73 * @param listener listener for listening events
74 * @param looper Looper to dispatch listener events
75 * @return
76 */
keunyoung15882e52015-09-16 16:57:58 -070077 public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
78 Looper looper) {
keunyoung217ca352015-11-17 14:45:38 -080079 int retryCount = 0;
80 IVehicleNetwork service = null;
81 while (service == null) {
82 service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
83 IVehicleNetwork.class.getCanonicalName()));
84 retryCount++;
85 if (retryCount > VNS_CONNECT_MAX_RETRY) {
86 break;
87 }
88 try {
89 Thread.sleep(VNS_RETRY_WAIT_TIME_MS);
90 } catch (InterruptedException e) {
91 //ignore
92 }
93 }
keunyounge18e25d2015-08-28 15:57:19 -070094 if (service == null) {
keunyoung15882e52015-09-16 16:57:58 -070095 throw new RuntimeException("Vehicle network service not available:" +
96 IVehicleNetwork.class.getCanonicalName());
keunyounge18e25d2015-08-28 15:57:19 -070097 }
98 return new VehicleNetwork(service, listener, looper);
99 }
100
101 private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
102 Looper looper) {
103 mService = service;
104 mListener = listener;
105 mEventHandler = new EventHandler(looper);
106 mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
107 }
108
keunyoung5c7cb262015-10-19 10:47:45 -0700109 /**
110 * List all properties from vehicle HAL
111 * @return all properties
112 */
keunyoung15882e52015-09-16 16:57:58 -0700113 public VehiclePropConfigs listProperties() {
114 return listProperties(0 /* all */);
115 }
116
keunyoung5c7cb262015-10-19 10:47:45 -0700117 /**
118 * Return configuration information of single property
119 * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has
120 * has special meaning of list all properties.
121 * @return null if given property does not exist.
122 */
keunyounge18e25d2015-08-28 15:57:19 -0700123 public VehiclePropConfigs listProperties(int property) {
124 try {
125 VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
126 if (parcelable != null) {
127 return parcelable.configs;
128 }
129 } catch (RemoteException e) {
130 handleRemoteException(e);
131 }
132 return null;
133 }
134
keunyoung5c7cb262015-10-19 10:47:45 -0700135 /**
136 * Set property which will lead into writing the value to vehicle HAL.
137 * @param value
138 * @throws IllegalArgumentException If value set has wrong value like wrong valueType,
139 * wrong data, and etc.
140 */
141 public void setProperty(VehiclePropValue value) throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700142 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
143 try {
keunyoung15882e52015-09-16 16:57:58 -0700144 mService.setProperty(parcelable);
keunyounge18e25d2015-08-28 15:57:19 -0700145 } catch (RemoteException e) {
146 handleRemoteException(e);
147 }
keunyounge18e25d2015-08-28 15:57:19 -0700148 }
149
keunyoung5c7cb262015-10-19 10:47:45 -0700150 /**
151 * Set integer type property
152 * @param property
153 * @param value
154 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
155 */
156 public void setIntProperty(int property, int value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700157 VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700158 setProperty(v);
159 }
160
keunyoung5c7cb262015-10-19 10:47:45 -0700161 /**
162 * Set int vector type property. Length of passed values should match with vector length.
163 * @param property
164 * @param values
165 * @throws IllegalArgumentException
166 */
167 public void setIntVectorProperty(int property, int[] values) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700168 VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0);
169 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700170 }
171
keunyoung5c7cb262015-10-19 10:47:45 -0700172 /**
173 * Set long type property.
174 * @param property
175 * @param value
176 * @throws IllegalArgumentException
177 */
178 public void setLongProperty(int property, long value) throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700179 VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700180 setProperty(v);
181 }
182
keunyoung5c7cb262015-10-19 10:47:45 -0700183 /**
184 * Set float type property.
185 * @param property
186 * @param value
187 * @throws IllegalArgumentException
188 */
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.
196 * @param property
197 * @param values
198 * @throws IllegalArgumentException
199 */
200 public void setFloatVectorProperty(int property, float[] values)
201 throws IllegalArgumentException {
keunyoung1ab8e182015-09-24 09:25:22 -0700202 VehiclePropValue v = VehiclePropValueUtil.createFloatVectorValue(property, values, 0);
203 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700204 }
205
keunyoung5c7cb262015-10-19 10:47:45 -0700206 /**
Steve Paik66481982015-10-27 15:22:38 -0700207 * Set zoned boolean type property
208 * @param property
209 * @param zone
210 * @param value
211 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
212 */
213 public void setZonedBooleanProperty(int property, int zone, boolean value)
214 throws IllegalArgumentException {
215 VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0);
216 setProperty(v);
217 }
218
219 /**
220 * Set zoned float type property
221 * @param property
222 * @param zone
223 * @param value
224 * @throws IllegalArgumentException For type mismatch (=the property is not float type)
225 */
226 public void setZonedFloatProperty(int property, int zone, float value)
227 throws IllegalArgumentException {
228 VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0);
229 setProperty(v);
230 }
231
232 /**
233 * Set zoned integer type property
234 * @param property
235 * @param zone
236 * @param value
237 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
238 */
239 public void setZonedIntProperty(int property, int zone, int value)
240 throws IllegalArgumentException {
241 VehiclePropValue v = VehiclePropValueUtil.createZonedIntValue(property, zone, value, 0);
242 setProperty(v);
243 }
244
245 /**
keunyoung5c7cb262015-10-19 10:47:45 -0700246 * Get property. This can be used for a property which does not require any other data.
247 * @param property
248 * @return
249 * @throws IllegalArgumentException
250 */
251 public VehiclePropValue getProperty(int property) throws IllegalArgumentException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700252 int valueType = VehicleNetworkConsts.getVehicleValueType(property);
253 VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build();
254 return getProperty(value);
255 }
256
keunyoung5c7cb262015-10-19 10:47:45 -0700257 /**
258 * Generic get method for any type of property. Some property may require setting data portion
259 * as get may return different result depending on the data set.
260 * @param value
261 * @return
262 * @throws IllegalArgumentException
263 */
264 public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700265 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
keunyounge18e25d2015-08-28 15:57:19 -0700266 try {
keunyoung7d74e6d2015-10-14 15:43:10 -0700267 VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable);
268 if (resParcelable != null) {
269 return resParcelable.value;
keunyounge18e25d2015-08-28 15:57:19 -0700270 }
271 } catch (RemoteException e) {
272 handleRemoteException(e);
273 }
274 return null;
275 }
276
keunyoung5c7cb262015-10-19 10:47:45 -0700277 /**
278 * get int type property
279 * @param property
280 * @return
281 * @throws IllegalArgumentException
282 */
283 public int getIntProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700284 VehiclePropValue v = getProperty(property);
285 if (v == null) {
keunyoungd32f4e62015-09-21 11:33:06 -0700286 // if property is invalid, IllegalArgumentException should have been thrown
287 // from getProperty.
keunyoungfe30ba02015-09-17 17:56:35 -0700288 throw new IllegalStateException();
289 }
keunyoungd32f4e62015-09-21 11:33:06 -0700290 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT32) {
keunyoungfe30ba02015-09-17 17:56:35 -0700291 throw new IllegalArgumentException();
292 }
keunyoungd32f4e62015-09-21 11:33:06 -0700293 if (v.getInt32ValuesCount() != 1) {
294 throw new IllegalStateException();
295 }
296 return v.getInt32Values(0);
297 }
298
keunyoung5c7cb262015-10-19 10:47:45 -0700299 /**
300 * get int vector type property. Length of values should match vector length.
301 * @param property
keunyoung5c7cb262015-10-19 10:47:45 -0700302 * @throws IllegalArgumentException
303 */
keunyoung4b0212c2015-10-29 17:11:57 -0700304 public int[] getIntVectorProperty(int property) throws IllegalArgumentException {
keunyoungd32f4e62015-09-21 11:33:06 -0700305 VehiclePropValue v = getProperty(property);
306 if (v == null) {
307 // if property is invalid, IllegalArgumentException should have been thrown
308 // from getProperty.
309 throw new IllegalStateException();
310 }
311 switch (v.getValueType()) {
312 case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2:
313 case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC3:
314 case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC4:
keunyoungd32f4e62015-09-21 11:33:06 -0700315 break;
316 default:
317 throw new IllegalArgumentException();
318 }
keunyoung4b0212c2015-10-29 17:11:57 -0700319 int[] values = new int[v.getValueType() - VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2 +
320 2];
keunyoungd32f4e62015-09-21 11:33:06 -0700321 if (v.getInt32ValuesCount() != values.length) {
322 throw new IllegalStateException();
323 }
324 for (int i = 0; i < values.length; i++) {
325 values[i] = v.getInt32Values(i);
326 }
keunyoung4b0212c2015-10-29 17:11:57 -0700327 return values;
keunyoungfe30ba02015-09-17 17:56:35 -0700328 }
329
keunyoung5c7cb262015-10-19 10:47:45 -0700330 /**
331 * Get float type property.
332 * @param property
333 * @return
334 * @throws IllegalArgumentException
335 */
336 public float getFloatProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700337 VehiclePropValue v = getProperty(property);
338 if (v == null) {
339 throw new IllegalStateException();
340 }
keunyoungd32f4e62015-09-21 11:33:06 -0700341 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT) {
keunyoungfe30ba02015-09-17 17:56:35 -0700342 throw new IllegalArgumentException();
343 }
keunyoungd32f4e62015-09-21 11:33:06 -0700344 if (v.getFloatValuesCount() != 1) {
345 throw new IllegalStateException();
346 }
347 return v.getFloatValues(0);
348 }
349
keunyoung5c7cb262015-10-19 10:47:45 -0700350 /**
351 * Get float vector type property. Length of values should match vector's length.
352 * @param property
keunyoung5c7cb262015-10-19 10:47:45 -0700353 * @throws IllegalArgumentException
354 */
keunyoung4b0212c2015-10-29 17:11:57 -0700355 public float[] getFloatVectorProperty(int property)
keunyoung5c7cb262015-10-19 10:47:45 -0700356 throws IllegalArgumentException {
keunyoungd32f4e62015-09-21 11:33:06 -0700357 VehiclePropValue v = getProperty(property);
358 if (v == null) {
359 // if property is invalid, IllegalArgumentException should have been thrown
360 // from getProperty.
361 throw new IllegalStateException();
362 }
363 switch (v.getValueType()) {
364 case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2:
365 case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC3:
366 case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC4:
keunyoungd32f4e62015-09-21 11:33:06 -0700367 break;
368 default:
369 throw new IllegalArgumentException();
370 }
keunyoung4b0212c2015-10-29 17:11:57 -0700371 float[] values = new float[v.getValueType() -
372 VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2 + 2];
keunyoungd32f4e62015-09-21 11:33:06 -0700373 if (v.getFloatValuesCount() != values.length) {
374 throw new IllegalStateException();
375 }
376 for (int i = 0; i < values.length; i++) {
377 values[i] = v.getFloatValues(i);
378 }
keunyoung4b0212c2015-10-29 17:11:57 -0700379 return values;
keunyoungfe30ba02015-09-17 17:56:35 -0700380 }
381
keunyoung5c7cb262015-10-19 10:47:45 -0700382 /**
383 * Get long type property.
384 * @param property
385 * @return
386 * @throws IllegalArgumentException
387 */
388 public long getLongProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700389 VehiclePropValue v = getProperty(property);
390 if (v == null) {
391 throw new IllegalStateException();
392 }
keunyoungd32f4e62015-09-21 11:33:06 -0700393 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT64) {
keunyoungfe30ba02015-09-17 17:56:35 -0700394 throw new IllegalArgumentException();
395 }
396 return v.getInt64Value();
397 }
398
keunyoung5c7cb262015-10-19 10:47:45 -0700399 /**
400 * Get string type property.
401 * @param property
402 * @return
403 * @throws IllegalArgumentException
404 */
keunyoungfe30ba02015-09-17 17:56:35 -0700405 //TODO check UTF8 to java string conversion
keunyoung5c7cb262015-10-19 10:47:45 -0700406 public String getStringProperty(int property) throws IllegalArgumentException {
keunyoungfe30ba02015-09-17 17:56:35 -0700407 VehiclePropValue v = getProperty(property);
408 if (v == null) {
409 throw new IllegalStateException();
410 }
keunyoungd32f4e62015-09-21 11:33:06 -0700411 if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_STRING) {
keunyoungfe30ba02015-09-17 17:56:35 -0700412 throw new IllegalArgumentException();
413 }
414 return v.getStringValue();
415 }
416
keunyoung5c7cb262015-10-19 10:47:45 -0700417 /**
418 * Subscribe given property with given sample rate.
419 * @param property
420 * @param sampleRate
421 * @throws IllegalArgumentException
422 */
423 public void subscribe(int property, float sampleRate) throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700424 try {
keunyoung15882e52015-09-16 16:57:58 -0700425 mService.subscribe(mVehicleNetworkListener, property, sampleRate);
keunyounge18e25d2015-08-28 15:57:19 -0700426 } catch (RemoteException e) {
427 handleRemoteException(e);
428 }
keunyounge18e25d2015-08-28 15:57:19 -0700429 }
430
keunyoung5c7cb262015-10-19 10:47:45 -0700431 /**
432 * Stop subscribing the property.
433 * @param property
434 */
keunyounge18e25d2015-08-28 15:57:19 -0700435 public void unsubscribe(int property) {
436 try {
437 mService.unsubscribe(mVehicleNetworkListener, property);
438 } catch (RemoteException e) {
439 handleRemoteException(e);
440 }
441 }
442
keunyoung5c7cb262015-10-19 10:47:45 -0700443 /**
444 * Inject given value to all clients subscribing the property. This is for testing.
445 * @param value
446 */
keunyoung1ab8e182015-09-24 09:25:22 -0700447 public synchronized void injectEvent(VehiclePropValue value) {
448 try {
449 mService.injectEvent(new VehiclePropValueParcelable(value));
450 } catch (RemoteException e) {
451 handleRemoteException(e);
452 }
453 }
454
keunyoung5c7cb262015-10-19 10:47:45 -0700455 /**
456 * Start mocking of vehicle HAL. For testing only.
457 * @param mock
458 */
keunyoung1ab8e182015-09-24 09:25:22 -0700459 public synchronized void startMocking(VehicleNetworkHalMock mock) {
460 mHalMock = mock;
461 mHalMockImpl = new IVehicleNetworkHalMockImpl(this);
462 try {
463 mService.startMocking(mHalMockImpl);
464 } catch (RemoteException e) {
465 handleRemoteException(e);
466 }
467 }
468
keunyoung5c7cb262015-10-19 10:47:45 -0700469 /**
470 * Stop mocking of vehicle HAL. For testing only.
471 */
keunyoung1ab8e182015-09-24 09:25:22 -0700472 public synchronized void stopMocking() {
473 try {
474 mService.stopMocking(mHalMockImpl);
475 } catch (RemoteException e) {
476 handleRemoteException(e);
477 } finally {
478 mHalMock = null;
479 mHalMockImpl = null;
480 }
481 }
482
keunyoung5c7cb262015-10-19 10:47:45 -0700483 /**
484 * Start mocking of vehicle HAL. For testing only.
485 * @param mock
486 */
keunyoung1ab8e182015-09-24 09:25:22 -0700487 public synchronized void startMocking(IVehicleNetworkHalMock mock) {
488 mHalMock = null;
489 mHalMockImpl = mock;
490 try {
491 mService.startMocking(mHalMockImpl);
492 } catch (RemoteException e) {
493 handleRemoteException(e);
494 }
495 }
496
keunyoung5c7cb262015-10-19 10:47:45 -0700497 /**
498 * Stop mocking of vehicle HAL. For testing only.
499 */
keunyoung1ab8e182015-09-24 09:25:22 -0700500 public synchronized void stopMocking(IVehicleNetworkHalMock mock) {
501 if (mock.asBinder() != mHalMockImpl.asBinder()) {
502 return;
503 }
504 try {
505 mService.stopMocking(mHalMockImpl);
506 } catch (RemoteException e) {
507 handleRemoteException(e);
508 } finally {
509 mHalMock = null;
510 mHalMockImpl = null;
511 }
512 }
513
514 private synchronized VehicleNetworkHalMock getHalMock() {
515 return mHalMock;
516 }
517
keunyounge18e25d2015-08-28 15:57:19 -0700518 private void handleRemoteException(RemoteException e) {
519 throw new RuntimeException("Vehicle network service not working ", e);
520 }
521
522 private void handleVehicleNetworkEvents(VehiclePropValues values) {
523 mEventHandler.notifyEvents(values);
524 }
525
526 private void doHandleVehicleNetworkEvents(VehiclePropValues values) {
527 mListener.onVehicleNetworkEvents(values);
528 }
529
530 private class EventHandler extends Handler {
531 private static final int MSG_EVENTS = 0;
532
533 private EventHandler(Looper looper) {
534 super(looper);
535 }
536
537 private void notifyEvents(VehiclePropValues values) {
538 Message msg = obtainMessage(MSG_EVENTS, values);
539 sendMessage(msg);
540 }
541
542 @Override
543 public void handleMessage(Message msg) {
544 switch (msg.what) {
545 case MSG_EVENTS:
546 doHandleVehicleNetworkEvents((VehiclePropValues)msg.obj);
547 break;
548 default:
549 Log.w(TAG, "unown message:" + msg.what, new RuntimeException());
550 break;
551 }
552 }
553 }
554
555 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
556 private final WeakReference<VehicleNetwork> mVehicleNetwork;
557
558 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
559 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
560 }
561
562 @Override
563 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
564 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
565 if (vehicleNetwork != null) {
566 vehicleNetwork.handleVehicleNetworkEvents(values.values);
567 }
568 }
569 }
keunyoung1ab8e182015-09-24 09:25:22 -0700570
571 private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
572 private final WeakReference<VehicleNetwork> mVehicleNetwork;
573
574 private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) {
575 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
576 }
577
578 @Override
579 public VehiclePropConfigsParcelable onListProperties() {
580 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
581 if (vehicleNetwork == null) {
582 return null;
583 }
584 VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties();
585 return new VehiclePropConfigsParcelable(configs);
586 }
587
588 @Override
589 public void onPropertySet(VehiclePropValueParcelable value) {
590 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
591 if (vehicleNetwork == null) {
592 return;
593 }
594 vehicleNetwork.getHalMock().onPropertySet(value.value);
595 }
596
597 @Override
keunyoung7d74e6d2015-10-14 15:43:10 -0700598 public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
keunyoung1ab8e182015-09-24 09:25:22 -0700599 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
600 if (vehicleNetwork == null) {
601 return null;
602 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700603 VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value);
604 return new VehiclePropValueParcelable(resValue);
keunyoung1ab8e182015-09-24 09:25:22 -0700605 }
606
607 @Override
608 public void onPropertySubscribe(int property, int sampleRate) {
609 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
610 if (vehicleNetwork == null) {
611 return;
612 }
613 vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate);
614 }
615
616 @Override
617 public void onPropertyUnsubscribe(int property) {
618 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
619 if (vehicleNetwork == null) {
620 return;
621 }
622 vehicleNetwork.getHalMock().onPropertyUnsubscribe(property);
623 }
624 }
keunyounge18e25d2015-08-28 15:57:19 -0700625}