aboutsummaryrefslogtreecommitdiff
path: root/libvehiclenetwork/java/src/com/android/car/vehiclenetwork/VehicleNetwork.java
blob: f7ef2c444906d490ebfa1192f577ff7d0eb3bb96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.car.vehiclenetwork;

import static com.android.car.vehiclenetwork.VehiclePropValueUtil.getVectorLength;
import static com.android.car.vehiclenetwork.VehiclePropValueUtil.isCustomProperty;
import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toFloatArray;
import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toIntArray;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ServiceSpecificException;
import android.util.Log;

import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
import com.android.internal.annotations.GuardedBy;

import java.lang.ref.WeakReference;

/**
 * System API to access Vehicle network. This is only for system services and applications should
 * not use this. All APIs will fail with security error if normal app tries this.
 */
public class VehicleNetwork {
    /**
     * Listener for VNS events.
     */
    public interface VehicleNetworkListener {
        /**
         * Notify HAL events. This requires subscribing the property
         */
        void onVehicleNetworkEvents(VehiclePropValues values);
        void onHalError(int errorCode, int property, int operation);
        void onHalRestart(boolean inMocking);
    }

    public interface VehicleNetworkHalMock {
        VehiclePropConfigs onListProperties();
        void onPropertySet(VehiclePropValue value);
        VehiclePropValue onPropertyGet(VehiclePropValue value);
        void onPropertySubscribe(int property, float sampleRate, int zones);
        void onPropertyUnsubscribe(int property);
    }

    private static final String TAG = VehicleNetwork.class.getSimpleName();

    private final IVehicleNetwork mService;
    private final VehicleNetworkListener mListener;
    private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
    private final EventHandler mEventHandler;

    @GuardedBy("this")
    private VehicleNetworkHalMock mHalMock;
    private IVehicleNetworkHalMock mHalMockImpl;

    private static final int VNS_CONNECT_MAX_RETRY = 10;
    private static final long VNS_RETRY_WAIT_TIME_MS = 1000;
    private static final int NO_ZONE = -1;

    /**
     * Factory method to create VehicleNetwork
     *
     * @param listener listener for listening events
     * @param looper Looper to dispatch listener events
     */
    public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
            Looper looper) {
        int retryCount = 0;
        IVehicleNetwork service = null;
        while (service == null) {
            service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
                    IVehicleNetwork.class.getCanonicalName()));
            retryCount++;
            if (retryCount > VNS_CONNECT_MAX_RETRY) {
                break;
            }
            try {
                Thread.sleep(VNS_RETRY_WAIT_TIME_MS);
            } catch (InterruptedException e) {
                //ignore
            }
        }
        if (service == null) {
            throw new RuntimeException("Vehicle network service not available:" +
                    IVehicleNetwork.class.getCanonicalName());
        }
        return new VehicleNetwork(service, listener, looper);
    }

    private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
            Looper looper) {
        mService = service;
        mListener = listener;
        mEventHandler = new EventHandler(looper);
        mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
    }

    /**
     * List all properties from vehicle HAL
     *
     * @return all properties
     */
    public VehiclePropConfigs listProperties() {
        return listProperties(0 /* all */);
    }

    /**
     * Return configuration information of single property
     *
     * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has has
     * special meaning of list all properties.
     * @return null if given property does not exist.
     */
    public VehiclePropConfigs listProperties(int property) {
        try {
            VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
            if (parcelable != null) {
                return parcelable.configs;
            }
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
        return null;
    }

    /**
     * Set property which will lead into writing the value to vehicle HAL.
     *
     * @throws IllegalArgumentException If value set has wrong value like wrong valueType, wrong
     * data, and etc.
     */
    public void setProperty(VehiclePropValue value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
        try {
            mService.setProperty(parcelable);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    /**
     * Set integer type property
     *
     * @throws IllegalArgumentException For type mismatch (=the property is not int type)
     */
    public void setIntProperty(int property, int value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0);
        setProperty(v);
    }

    /**
     * Set int vector type property. Length of passed values should match with vector length.
     */
    public void setIntVectorProperty(int property, int[] values)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0);
        setProperty(v);
    }

    /**
     * Set long type property.
     */
    public void setLongProperty(int property, long value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0);
        setProperty(v);
    }

    /**
     * Set float type property.
     */
    public void setFloatProperty(int property, float value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createFloatValue(property, value, 0);
        setProperty(v);
    }

    /**
     * Set float vector type property. Length of values should match with vector length.
     */
    public void setFloatVectorProperty(int property, float[] values)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createFloatVectorValue(property, values, 0);
        setProperty(v);
    }

    /**
     * Set zoned boolean type property
     *
     * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
     */
    public void setBooleanProperty(int property, boolean value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createBooleanValue(property, value, 0);
        setProperty(v);
    }

    /**
     * Set zoned boolean type property
     *
     * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
     */
    public void setZonedBooleanProperty(int property, int zone, boolean value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0);
        setProperty(v);
    }

    /**
     * Set zoned float type property
     *
     * @throws IllegalArgumentException For type mismatch (=the property is not float type)
     */
    public void setZonedFloatProperty(int property, int zone, float value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0);
        setProperty(v);
    }

    /**
     * Set zoned integer type property
     *
     * @throws IllegalArgumentException For type mismatch (=the property is not int type)
     */
    public void setZonedIntProperty(int property, int zone, int value)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil.createZonedIntValue(property, zone, value, 0);
        setProperty(v);
    }

    /**
     * Set zoned int vector type property. Length of passed values should match with vector length.
     */
    public void setZonedIntVectorProperty(int property, int zone, int[] values)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil
                .createZonedIntVectorValue(property, zone, values, 0);
        setProperty(v);
    }

    /**
     * Set zoned float vector type property. Length of passed values should match with vector
     * length.
     */
    public void setZonedFloatVectorProperty(int property, int zone, float[] values)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = VehiclePropValueUtil
                .createZonedFloatVectorValue(property, zone, values, 0);
        setProperty(v);
    }

    /**
     * Get property. This can be used for a property which does not require any other data.
     */
    public VehiclePropValue getProperty(int property) throws IllegalArgumentException,
            ServiceSpecificException {
        int valueType = VehicleNetworkConsts.getVehicleValueType(property);
        if (valueType == 0) {
            throw new IllegalArgumentException("Data type is unknown for property: " + property);
        }
        VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build();
        return getProperty(value);
    }

    /**
     * Generic get method for any type of property. Some property may require setting data portion
     * as get may return different result depending on the data set.
     */
    public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
        try {
            VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable);
            if (resParcelable != null) {
                return resParcelable.value;
            }
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
        return null;
    }

    /**
     * Get int type property.
     */
    public int getIntProperty(int property) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
        if (v.getInt32ValuesCount() != 1) {
            throw new IllegalStateException();
        }
        return v.getInt32Values(0);
    }

    /**
     * Get zoned int type property.
     */
    public int getZonedIntProperty(int property, int zone) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
        if (v.getInt32ValuesCount() != 1) {
            throw new IllegalStateException();
        }
        return v.getInt32Values(0);
    }

    /**
     * Get int vector type property. Length of values should match vector length.
     */
    public int[] getIntVectorProperty(int property) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
        assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType());
        return toIntArray(v.getInt32ValuesList());
    }

    /**
     * Get zoned int vector type property. Length of values should match vector length.
     */
    public int[] getZonedIntVectorProperty(int property, int zone)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
        assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType());
        return toIntArray(v.getInt32ValuesList());
    }

    /**
     * Get float type property.
     */
    public float getFloatProperty(int property) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT);
        if (v.getFloatValuesCount() != 1) {
            throw new IllegalStateException();
        }
        return v.getFloatValues(0);
    }

    /**
     * Get float vector type property. Length of values should match vector's length.
     */
    public float[] getFloatVectorProperty(int property) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT);
        assertVectorLength(v.getFloatValuesCount(), property, v.getValueType());
        return toFloatArray(v.getFloatValuesList());
    }

    /**
     * Get zoned float vector type property. Length of values should match vector's length.
     */
    public float[] getZonedFloatVectorProperty(int property, int zone)
            throws IllegalArgumentException, ServiceSpecificException {
        VehiclePropValue v = getProperty(property, zone,
                VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_FLOAT);
        assertVectorLength(v.getFloatValuesCount(), property, v.getValueType());
        return toFloatArray(v.getFloatValuesList());
    }

    /**
     * Get long type property.
     */
    public long getLongProperty(int property) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT64);
        return v.getInt64Value();
    }

    /**
     * Get string type property.
     */
    //TODO check UTF8 to java string conversion
    public String getStringProperty(int property) throws IllegalArgumentException,
            ServiceSpecificException {
        VehiclePropValue v = getProperty(
                property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_STRING);
        return v.getStringValue();
    }

    /**
     * Subscribe given property with given sample rate.
     */
    public void subscribe(int property, float sampleRate) throws IllegalArgumentException {
        subscribe(property, sampleRate, 0);
    }

    /**
     * Subscribe given property with given sample rate.
     */
    public void subscribe(int property, float sampleRate, int zones)
            throws IllegalArgumentException {
        try {
            mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    /**
     * Stop subscribing the property.
     */
    public void unsubscribe(int property) {
        try {
            mService.unsubscribe(mVehicleNetworkListener, property);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    /**
     * Inject given value to all clients subscribing the property. This is for testing.
     */
    public synchronized void injectEvent(VehiclePropValue value) {
        try {
            mService.injectEvent(new VehiclePropValueParcelable(value));
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    /**
     * Start mocking of vehicle HAL. For testing only.
     */
    public synchronized void startMocking(VehicleNetworkHalMock mock) {
        mHalMock = mock;
        mHalMockImpl = new IVehicleNetworkHalMockImpl(this);
        try {
            mService.startMocking(mHalMockImpl);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    /**
     * Stop mocking of vehicle HAL. For testing only.
     */
    public synchronized void stopMocking() {
        if (mHalMockImpl == null) {
            return;
        }
        try {
            mService.stopMocking(mHalMockImpl);
        } catch (RemoteException e) {
            handleRemoteException(e);
        } finally {
            mHalMock = null;
            mHalMockImpl = null;
        }
    }

    /**
     * Start mocking of vehicle HAL. For testing only.
     */
    public synchronized void startMocking(IVehicleNetworkHalMock mock) {
        mHalMock = null;
        mHalMockImpl = mock;
        try {
            mService.startMocking(mHalMockImpl);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    /**
     * Stop mocking of vehicle HAL. For testing only.
     */
    public synchronized void stopMocking(IVehicleNetworkHalMock mock) {
        if (mock.asBinder() != mHalMockImpl.asBinder()) {
            return;
        }
        try {
            mService.stopMocking(mHalMockImpl);
        } catch (RemoteException e) {
            handleRemoteException(e);
        } finally {
            mHalMock = null;
            mHalMockImpl = null;
        }
    }

    public synchronized void injectHalError(int errorCode, int property, int operation) {
        try {
            mService.injectHalError(errorCode, property, operation);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public synchronized void startErrorListening() {
        try {
            mService.startErrorListening(mVehicleNetworkListener);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public synchronized void stopErrorListening() {
        try {
            mService.stopErrorListening(mVehicleNetworkListener);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public synchronized void startHalRestartMonitoring() {
        try {
            mService.startHalRestartMonitoring(mVehicleNetworkListener);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    public synchronized void stopHalRestartMonitoring() {
        try {
            mService.stopHalRestartMonitoring(mVehicleNetworkListener);
        } catch (RemoteException e) {
            handleRemoteException(e);
        }
    }

    private synchronized VehicleNetworkHalMock getHalMock() {
        return mHalMock;
    }

    private void handleRemoteException(RemoteException e) {
        throw new RuntimeException("Vehicle network service not working ", e);
    }

    private void handleVehicleNetworkEvents(VehiclePropValues values) {
        mListener.onVehicleNetworkEvents(values);
    }

    private void handleHalError(int errorCode, int property, int operation) {
        mListener.onHalError(errorCode, property, operation);
    }

    private void handleHalRestart(boolean inMocking) {
        mListener.onHalRestart(inMocking);
    }

    private class EventHandler extends Handler {

        private static final int MSG_EVENTS = 0;
        private static final int MSG_HAL_ERROR = 1;
        private static final int MSG_HAL_RESTART = 2;

        private EventHandler(Looper looper) {
            super(looper);
        }

        private void notifyEvents(VehiclePropValues values) {
            Message msg = obtainMessage(MSG_EVENTS, values);
            sendMessage(msg);
        }

        private void notifyHalError(int errorCode, int property, int operation) {
            Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property, operation);
            sendMessage(msg);
        }

        private void notifyHalRestart(boolean inMocking) {
            Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0);
            sendMessage(msg);
        }

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_EVENTS:
                    handleVehicleNetworkEvents((VehiclePropValues) msg.obj);
                    break;
                case MSG_HAL_ERROR:
                    handleHalError(msg.arg1, msg.arg2, (Integer) msg.obj);
                    break;
                case MSG_HAL_RESTART:
                    handleHalRestart(msg.arg1 == 1);
                    break;
                default:
                    Log.w(TAG, "Unknown message:" + msg.what, new RuntimeException());
                    break;
            }
        }
    }

    private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {

        private final WeakReference<VehicleNetwork> mVehicleNetwork;

        private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
            mVehicleNetwork = new WeakReference<>(vehicleNewotk);
        }

        @Override
        public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork != null) {
                vehicleNetwork.mEventHandler.notifyEvents(values.values);
            }
        }

        @Override
        public void onHalError(int errorCode, int property, int operation) {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork != null) {
                vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation);
            }
        }

        @Override
        public void onHalRestart(boolean inMocking) {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork != null) {
                vehicleNetwork.mEventHandler.notifyHalRestart(inMocking);
            }
        }
    }

    private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
        private final WeakReference<VehicleNetwork> mVehicleNetwork;

        private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) {
            mVehicleNetwork = new WeakReference<>(vehicleNewotk);
        }

        @Override
        public VehiclePropConfigsParcelable onListProperties() {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork == null) {
                return null;
            }
            VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties();
            return new VehiclePropConfigsParcelable(configs);
        }

        @Override
        public void onPropertySet(VehiclePropValueParcelable value) {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork == null) {
                return;
            }
            vehicleNetwork.getHalMock().onPropertySet(value.value);
        }

        @Override
        public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork == null) {
                return null;
            }
            VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value);
            return new VehiclePropValueParcelable(resValue);
        }

        @Override
        public void onPropertySubscribe(int property, float sampleRate, int zones) {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork == null) {
                return;
            }
            vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate, zones);
        }

        @Override
        public void onPropertyUnsubscribe(int property) {
            VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
            if (vehicleNetwork == null) {
                return;
            }
            vehicleNetwork.getHalMock().onPropertyUnsubscribe(property);
        }
    }

    private VehiclePropValue getProperty(int property, int zone, int customPropetyDataType) {
        boolean isCustom = isCustomProperty(property);
        int valueType = isCustom
                ? customPropetyDataType
                : VehicleNetworkConsts.getVehicleValueType(property);

        VehiclePropValue.Builder valuePrototypeBuilder =
                VehiclePropValueUtil.createBuilder(property, valueType, 0);

        if (zone != NO_ZONE) {
            valuePrototypeBuilder.setZone(zone);
        }

        VehiclePropValue v = getProperty(valuePrototypeBuilder.build());
        if (v == null) {
            // if property is invalid, IllegalArgumentException should have been thrown
            // from getProperty.
            throw new IllegalStateException();
        }

        if (!isCustom && v.getValueType() != valueType) {
            throw new IllegalArgumentException(
                    "Unexpected type for property 0x" + Integer.toHexString(property) +
                    " got:0x" + Integer.toHexString(v.getValueType())
                    + ", expecting:0x" + Integer.toHexString(valueType));
        }
        return v;
    }

    private void assertVectorLength(int actual, int property, int valueType) {
        int expectedLen = getVectorLength(valueType);
        if (expectedLen != actual) {
            throw new IllegalStateException("Invalid array size for property: " + property
                    + ". Expected: " + expectedLen
                    + ", actual: " + actual);
        }
    }
}