blob: 6dc92db500fa151c74842f018eb53d58b1766108 [file] [log] [blame]
keunyounge18e25d2015-08-28 15:57:19 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.car.vehiclenetwork;
17
Pavel Maltseve8f75372016-01-26 10:26:04 -080018import static com.android.car.vehiclenetwork.VehiclePropValueUtil.getVectorLength;
19import static com.android.car.vehiclenetwork.VehiclePropValueUtil.isCustomProperty;
20import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toFloatArray;
21import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toIntArray;
22
Pavel Maltsevb0324b42016-09-27 21:00:41 -070023import android.annotation.IntDef;
keunyounge18e25d2015-08-28 15:57:19 -070024import android.os.Handler;
25import android.os.Looper;
26import android.os.Message;
27import android.os.RemoteException;
28import android.os.ServiceManager;
Keun-young Parkba485482016-03-24 13:24:31 -070029import android.os.ServiceSpecificException;
keunyounge18e25d2015-08-28 15:57:19 -070030import android.util.Log;
31
keunyoungd32f4e62015-09-21 11:33:06 -070032import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
keunyounge18e25d2015-08-28 15:57:19 -070033import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
34import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
35import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
keunyoung1ab8e182015-09-24 09:25:22 -070036import com.android.internal.annotations.GuardedBy;
keunyounge18e25d2015-08-28 15:57:19 -070037
Pavel Maltsevb0324b42016-09-27 21:00:41 -070038import java.lang.annotation.Retention;
39import java.lang.annotation.RetentionPolicy;
keunyounge18e25d2015-08-28 15:57:19 -070040import java.lang.ref.WeakReference;
41
42/**
43 * System API to access Vehicle network. This is only for system services and applications should
keunyoung15882e52015-09-16 16:57:58 -070044 * not use this. All APIs will fail with security error if normal app tries this.
keunyounge18e25d2015-08-28 15:57:19 -070045 */
46public class VehicleNetwork {
keunyoung1ab8e182015-09-24 09:25:22 -070047 /**
48 * Listener for VNS events.
49 */
keunyounge18e25d2015-08-28 15:57:19 -070050 public interface VehicleNetworkListener {
keunyoung1ab8e182015-09-24 09:25:22 -070051 /**
52 * Notify HAL events. This requires subscribing the property
53 */
keunyounge18e25d2015-08-28 15:57:19 -070054 void onVehicleNetworkEvents(VehiclePropValues values);
Keun-young Park28dd4702015-11-19 18:06:04 -080055 void onHalError(int errorCode, int property, int operation);
56 void onHalRestart(boolean inMocking);
Pavel Maltsevb0324b42016-09-27 21:00:41 -070057 void onPropertySet(VehiclePropValue value);
keunyounge18e25d2015-08-28 15:57:19 -070058 }
59
keunyoung1ab8e182015-09-24 09:25:22 -070060 public interface VehicleNetworkHalMock {
61 VehiclePropConfigs onListProperties();
62 void onPropertySet(VehiclePropValue value);
Keun-young Park28dd4702015-11-19 18:06:04 -080063 VehiclePropValue onPropertyGet(VehiclePropValue value);
Keun-young Park0727f952015-12-21 14:30:07 -080064 void onPropertySubscribe(int property, float sampleRate, int zones);
keunyoung1ab8e182015-09-24 09:25:22 -070065 void onPropertyUnsubscribe(int property);
66 }
67
Pavel Maltsevb0324b42016-09-27 21:00:41 -070068 /**
69 * Flags to be used in #subscribe(int, float, int, int).
70 */
71 @Retention(RetentionPolicy.SOURCE)
72 @IntDef({
73 SubscribeFlags.HAL_EVENT,
74 SubscribeFlags.SET_CALL,
75 SubscribeFlags.DEFAULT,
76 })
77 public @interface SubscribeFlags {
78 int HAL_EVENT = 0x1;
79 int SET_CALL = 0x2;
80 int DEFAULT = HAL_EVENT;
81 }
82
keunyounge18e25d2015-08-28 15:57:19 -070083 private static final String TAG = VehicleNetwork.class.getSimpleName();
84
85 private final IVehicleNetwork mService;
86 private final VehicleNetworkListener mListener;
87 private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
88 private final EventHandler mEventHandler;
89
keunyoung1ab8e182015-09-24 09:25:22 -070090 @GuardedBy("this")
91 private VehicleNetworkHalMock mHalMock;
92 private IVehicleNetworkHalMock mHalMockImpl;
93
keunyoung217ca352015-11-17 14:45:38 -080094 private static final int VNS_CONNECT_MAX_RETRY = 10;
95 private static final long VNS_RETRY_WAIT_TIME_MS = 1000;
Pavel Maltseve8f75372016-01-26 10:26:04 -080096 private static final int NO_ZONE = -1;
keunyoung217ca352015-11-17 14:45:38 -080097
keunyoung5c7cb262015-10-19 10:47:45 -070098 /**
99 * Factory method to create VehicleNetwork
Pavel Maltseve8f75372016-01-26 10:26:04 -0800100 *
keunyoung5c7cb262015-10-19 10:47:45 -0700101 * @param listener listener for listening events
102 * @param looper Looper to dispatch listener events
keunyoung5c7cb262015-10-19 10:47:45 -0700103 */
keunyoung15882e52015-09-16 16:57:58 -0700104 public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
105 Looper looper) {
keunyoung217ca352015-11-17 14:45:38 -0800106 int retryCount = 0;
107 IVehicleNetwork service = null;
Keun-young Parke52e8f22016-08-15 09:29:14 -0700108 while (true) {
keunyoung217ca352015-11-17 14:45:38 -0800109 service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
110 IVehicleNetwork.class.getCanonicalName()));
Keun-young Parke52e8f22016-08-15 09:29:14 -0700111 if (service != null) {
112 break;
113 }
keunyoung217ca352015-11-17 14:45:38 -0800114 retryCount++;
115 if (retryCount > VNS_CONNECT_MAX_RETRY) {
116 break;
117 }
118 try {
119 Thread.sleep(VNS_RETRY_WAIT_TIME_MS);
120 } catch (InterruptedException e) {
121 //ignore
122 }
123 }
keunyounge18e25d2015-08-28 15:57:19 -0700124 if (service == null) {
keunyoung15882e52015-09-16 16:57:58 -0700125 throw new RuntimeException("Vehicle network service not available:" +
126 IVehicleNetwork.class.getCanonicalName());
keunyounge18e25d2015-08-28 15:57:19 -0700127 }
128 return new VehicleNetwork(service, listener, looper);
129 }
130
131 private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
132 Looper looper) {
133 mService = service;
134 mListener = listener;
135 mEventHandler = new EventHandler(looper);
136 mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
137 }
138
keunyoung5c7cb262015-10-19 10:47:45 -0700139 /**
140 * List all properties from vehicle HAL
Pavel Maltseve8f75372016-01-26 10:26:04 -0800141 *
keunyoung5c7cb262015-10-19 10:47:45 -0700142 * @return all properties
143 */
keunyoung15882e52015-09-16 16:57:58 -0700144 public VehiclePropConfigs listProperties() {
145 return listProperties(0 /* all */);
146 }
147
keunyoung5c7cb262015-10-19 10:47:45 -0700148 /**
149 * Return configuration information of single property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800150 *
151 * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has has
152 * special meaning of list all properties.
keunyoung5c7cb262015-10-19 10:47:45 -0700153 * @return null if given property does not exist.
154 */
keunyounge18e25d2015-08-28 15:57:19 -0700155 public VehiclePropConfigs listProperties(int property) {
156 try {
157 VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
158 if (parcelable != null) {
159 return parcelable.configs;
160 }
161 } catch (RemoteException e) {
162 handleRemoteException(e);
163 }
164 return null;
165 }
166
keunyoung5c7cb262015-10-19 10:47:45 -0700167 /**
168 * Set property which will lead into writing the value to vehicle HAL.
Pavel Maltseve8f75372016-01-26 10:26:04 -0800169 *
170 * @throws IllegalArgumentException If value set has wrong value like wrong valueType, wrong
171 * data, and etc.
keunyoung5c7cb262015-10-19 10:47:45 -0700172 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700173 public void setProperty(VehiclePropValue value)
174 throws IllegalArgumentException, ServiceSpecificException {
keunyounge18e25d2015-08-28 15:57:19 -0700175 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
176 try {
keunyoung15882e52015-09-16 16:57:58 -0700177 mService.setProperty(parcelable);
keunyounge18e25d2015-08-28 15:57:19 -0700178 } catch (RemoteException e) {
179 handleRemoteException(e);
180 }
keunyounge18e25d2015-08-28 15:57:19 -0700181 }
182
keunyoung5c7cb262015-10-19 10:47:45 -0700183 /**
184 * Set integer type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800185 *
keunyoung5c7cb262015-10-19 10:47:45 -0700186 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
187 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700188 public void setIntProperty(int property, int value)
189 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700190 VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700191 setProperty(v);
192 }
193
keunyoung5c7cb262015-10-19 10:47:45 -0700194 /**
195 * Set int vector type property. Length of passed values should match with vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700196 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700197 public void setIntVectorProperty(int property, int[] values)
198 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700199 VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0);
200 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700201 }
202
keunyoung5c7cb262015-10-19 10:47:45 -0700203 /**
204 * Set long type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700205 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700206 public void setLongProperty(int property, long value)
207 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700208 VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700209 setProperty(v);
210 }
211
keunyoung5c7cb262015-10-19 10:47:45 -0700212 /**
213 * Set float type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700214 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700215 public void setFloatProperty(int property, float value)
216 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700217 VehiclePropValue v = VehiclePropValueUtil.createFloatValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700218 setProperty(v);
219 }
220
keunyoung5c7cb262015-10-19 10:47:45 -0700221 /**
222 * Set float vector type property. Length of values should match with vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700223 */
224 public void setFloatVectorProperty(int property, float[] values)
Keun-young Parke78bf532016-04-25 18:59:22 -0700225 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700226 VehiclePropValue v = VehiclePropValueUtil.createFloatVectorValue(property, values, 0);
227 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700228 }
229
keunyoung5c7cb262015-10-19 10:47:45 -0700230 /**
Steve Paik66481982015-10-27 15:22:38 -0700231 * Set zoned boolean type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800232 *
233 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
234 */
235 public void setBooleanProperty(int property, boolean value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700236 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800237 VehiclePropValue v = VehiclePropValueUtil.createBooleanValue(property, value, 0);
238 setProperty(v);
239 }
240
Keun-young Park4c6834a2016-06-28 12:58:23 -0700241 public void setStringProperty(int property, String value)
242 throws IllegalArgumentException, ServiceSpecificException {
243 VehiclePropValue v = VehiclePropValueUtil.createStringValue(property, value, 0);
244 setProperty(v);
245 }
246
Pavel Maltseve8f75372016-01-26 10:26:04 -0800247 /**
248 * Set zoned boolean type property
249 *
Steve Paik66481982015-10-27 15:22:38 -0700250 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
251 */
252 public void setZonedBooleanProperty(int property, int zone, boolean value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700253 throws IllegalArgumentException, ServiceSpecificException {
Steve Paik66481982015-10-27 15:22:38 -0700254 VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0);
255 setProperty(v);
256 }
257
258 /**
259 * Set zoned float type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800260 *
Steve Paik66481982015-10-27 15:22:38 -0700261 * @throws IllegalArgumentException For type mismatch (=the property is not float type)
262 */
263 public void setZonedFloatProperty(int property, int zone, float value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700264 throws IllegalArgumentException, ServiceSpecificException {
Steve Paik66481982015-10-27 15:22:38 -0700265 VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0);
266 setProperty(v);
267 }
268
269 /**
270 * Set zoned integer type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800271 *
Steve Paik66481982015-10-27 15:22:38 -0700272 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
273 */
274 public void setZonedIntProperty(int property, int zone, int value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700275 throws IllegalArgumentException, ServiceSpecificException {
Steve Paik66481982015-10-27 15:22:38 -0700276 VehiclePropValue v = VehiclePropValueUtil.createZonedIntValue(property, zone, value, 0);
277 setProperty(v);
278 }
279
280 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800281 * Set zoned int vector type property. Length of passed values should match with vector length.
282 */
283 public void setZonedIntVectorProperty(int property, int zone, int[] values)
Keun-young Parke78bf532016-04-25 18:59:22 -0700284 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800285 VehiclePropValue v = VehiclePropValueUtil
286 .createZonedIntVectorValue(property, zone, values, 0);
287 setProperty(v);
288 }
289
290 /**
291 * Set zoned float vector type property. Length of passed values should match with vector
292 * length.
293 */
294 public void setZonedFloatVectorProperty(int property, int zone, float[] values)
Keun-young Parke78bf532016-04-25 18:59:22 -0700295 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800296 VehiclePropValue v = VehiclePropValueUtil
297 .createZonedFloatVectorValue(property, zone, values, 0);
298 setProperty(v);
299 }
300
301 /**
keunyoung5c7cb262015-10-19 10:47:45 -0700302 * Get property. This can be used for a property which does not require any other data.
keunyoung5c7cb262015-10-19 10:47:45 -0700303 */
Keun-young Parkba485482016-03-24 13:24:31 -0700304 public VehiclePropValue getProperty(int property) throws IllegalArgumentException,
305 ServiceSpecificException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700306 int valueType = VehicleNetworkConsts.getVehicleValueType(property);
Pavel Maltsev169b7a62016-01-26 15:56:07 -0800307 if (valueType == 0) {
308 throw new IllegalArgumentException("Data type is unknown for property: " + property);
309 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700310 VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build();
311 return getProperty(value);
312 }
313
keunyoung5c7cb262015-10-19 10:47:45 -0700314 /**
315 * Generic get method for any type of property. Some property may require setting data portion
316 * as get may return different result depending on the data set.
keunyoung5c7cb262015-10-19 10:47:45 -0700317 */
Keun-young Parkba485482016-03-24 13:24:31 -0700318 public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException,
319 ServiceSpecificException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700320 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
keunyounge18e25d2015-08-28 15:57:19 -0700321 try {
keunyoung7d74e6d2015-10-14 15:43:10 -0700322 VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable);
323 if (resParcelable != null) {
324 return resParcelable.value;
keunyounge18e25d2015-08-28 15:57:19 -0700325 }
326 } catch (RemoteException e) {
327 handleRemoteException(e);
328 }
329 return null;
330 }
331
keunyoung5c7cb262015-10-19 10:47:45 -0700332 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800333 * Get int type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700334 */
Keun-young Parkba485482016-03-24 13:24:31 -0700335 public int getIntProperty(int property) throws IllegalArgumentException,
336 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800337 VehiclePropValue v = getProperty(
338 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
keunyoungd32f4e62015-09-21 11:33:06 -0700339 if (v.getInt32ValuesCount() != 1) {
340 throw new IllegalStateException();
341 }
342 return v.getInt32Values(0);
343 }
344
keunyoung5c7cb262015-10-19 10:47:45 -0700345 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800346 * Get zoned int type property.
347 */
Keun-young Parkba485482016-03-24 13:24:31 -0700348 public int getZonedIntProperty(int property, int zone) throws IllegalArgumentException,
349 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800350 VehiclePropValue v = getProperty(
351 property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800352 if (v.getInt32ValuesCount() != 1) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800353 throw new IllegalStateException();
354 }
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800355 return v.getInt32Values(0);
Pavel Maltseve8f75372016-01-26 10:26:04 -0800356 }
357
358 /**
359 * Get int vector type property. Length of values should match vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700360 */
Keun-young Parkba485482016-03-24 13:24:31 -0700361 public int[] getIntVectorProperty(int property) throws IllegalArgumentException,
362 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800363 VehiclePropValue v = getProperty(
364 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
365 assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType());
366 return toIntArray(v.getInt32ValuesList());
367 }
368
369 /**
370 * Get zoned int vector type property. Length of values should match vector length.
371 */
372 public int[] getZonedIntVectorProperty(int property, int zone)
Keun-young Parkba485482016-03-24 13:24:31 -0700373 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800374 VehiclePropValue v = getProperty(
375 property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800376 assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType());
377 return toIntArray(v.getInt32ValuesList());
keunyoungfe30ba02015-09-17 17:56:35 -0700378 }
379
keunyoung5c7cb262015-10-19 10:47:45 -0700380 /**
381 * Get float type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700382 */
Keun-young Parkba485482016-03-24 13:24:31 -0700383 public float getFloatProperty(int property) throws IllegalArgumentException,
384 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800385 VehiclePropValue v = getProperty(
386 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT);
keunyoungd32f4e62015-09-21 11:33:06 -0700387 if (v.getFloatValuesCount() != 1) {
388 throw new IllegalStateException();
389 }
390 return v.getFloatValues(0);
391 }
392
keunyoung5c7cb262015-10-19 10:47:45 -0700393 /**
394 * Get float vector type property. Length of values should match vector's length.
keunyoung5c7cb262015-10-19 10:47:45 -0700395 */
Keun-young Parkba485482016-03-24 13:24:31 -0700396 public float[] getFloatVectorProperty(int property) throws IllegalArgumentException,
397 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800398 VehiclePropValue v = getProperty(
399 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT);
400 assertVectorLength(v.getFloatValuesCount(), property, v.getValueType());
401 return toFloatArray(v.getFloatValuesList());
402 }
403
404 /**
405 * Get zoned float vector type property. Length of values should match vector's length.
406 */
407 public float[] getZonedFloatVectorProperty(int property, int zone)
Keun-young Parkba485482016-03-24 13:24:31 -0700408 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800409 VehiclePropValue v = getProperty(property, zone,
410 VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_FLOAT);
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800411 assertVectorLength(v.getFloatValuesCount(), property, v.getValueType());
412 return toFloatArray(v.getFloatValuesList());
keunyoungfe30ba02015-09-17 17:56:35 -0700413 }
414
keunyoung5c7cb262015-10-19 10:47:45 -0700415 /**
416 * Get long type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700417 */
Keun-young Parkba485482016-03-24 13:24:31 -0700418 public long getLongProperty(int property) throws IllegalArgumentException,
419 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800420 VehiclePropValue v = getProperty(
421 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT64);
keunyoungfe30ba02015-09-17 17:56:35 -0700422 return v.getInt64Value();
423 }
424
keunyoung5c7cb262015-10-19 10:47:45 -0700425 /**
426 * Get string type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700427 */
Keun-young Parkba485482016-03-24 13:24:31 -0700428 public String getStringProperty(int property) throws IllegalArgumentException,
429 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800430 VehiclePropValue v = getProperty(
431 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_STRING);
keunyoungfe30ba02015-09-17 17:56:35 -0700432 return v.getStringValue();
433 }
434
keunyoung5c7cb262015-10-19 10:47:45 -0700435 /**
436 * Subscribe given property with given sample rate.
keunyoung5c7cb262015-10-19 10:47:45 -0700437 */
438 public void subscribe(int property, float sampleRate) throws IllegalArgumentException {
Keun-young Park0727f952015-12-21 14:30:07 -0800439 subscribe(property, sampleRate, 0);
440 }
441
442 /**
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700443 * Subscribe given property with given sample rate and zones.
Keun-young Park0727f952015-12-21 14:30:07 -0800444 */
445 public void subscribe(int property, float sampleRate, int zones)
446 throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700447 try {
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700448 mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones,
449 SubscribeFlags.DEFAULT);
450 } catch (RemoteException e) {
451 handleRemoteException(e);
452 }
453 }
454
455 /**
456 * Subscribe given property with given sample rate, zones and flags.
457 */
458 @SuppressWarnings("ResourceType")
459 public void subscribe(int property, float sampleRate, int zones, @SubscribeFlags int flags)
460 throws IllegalArgumentException {
461 try {
462 mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones, flags);
keunyounge18e25d2015-08-28 15:57:19 -0700463 } catch (RemoteException e) {
464 handleRemoteException(e);
465 }
keunyounge18e25d2015-08-28 15:57:19 -0700466 }
467
keunyoung5c7cb262015-10-19 10:47:45 -0700468 /**
469 * Stop subscribing the property.
keunyoung5c7cb262015-10-19 10:47:45 -0700470 */
keunyounge18e25d2015-08-28 15:57:19 -0700471 public void unsubscribe(int property) {
472 try {
473 mService.unsubscribe(mVehicleNetworkListener, property);
474 } catch (RemoteException e) {
475 handleRemoteException(e);
476 }
477 }
478
keunyoung5c7cb262015-10-19 10:47:45 -0700479 /**
480 * Inject given value to all clients subscribing the property. This is for testing.
keunyoung5c7cb262015-10-19 10:47:45 -0700481 */
keunyoung1ab8e182015-09-24 09:25:22 -0700482 public synchronized void injectEvent(VehiclePropValue value) {
483 try {
484 mService.injectEvent(new VehiclePropValueParcelable(value));
485 } catch (RemoteException e) {
486 handleRemoteException(e);
487 }
488 }
489
keunyoung5c7cb262015-10-19 10:47:45 -0700490 /**
491 * Start mocking of vehicle HAL. For testing only.
keunyoung5c7cb262015-10-19 10:47:45 -0700492 */
keunyoung1ab8e182015-09-24 09:25:22 -0700493 public synchronized void startMocking(VehicleNetworkHalMock mock) {
494 mHalMock = mock;
495 mHalMockImpl = new IVehicleNetworkHalMockImpl(this);
496 try {
497 mService.startMocking(mHalMockImpl);
498 } catch (RemoteException e) {
499 handleRemoteException(e);
500 }
501 }
502
keunyoung5c7cb262015-10-19 10:47:45 -0700503 /**
504 * Stop mocking of vehicle HAL. For testing only.
505 */
keunyoung1ab8e182015-09-24 09:25:22 -0700506 public synchronized void stopMocking() {
Keun-young Park28dd4702015-11-19 18:06:04 -0800507 if (mHalMockImpl == null) {
508 return;
509 }
keunyoung1ab8e182015-09-24 09:25:22 -0700510 try {
511 mService.stopMocking(mHalMockImpl);
512 } catch (RemoteException e) {
513 handleRemoteException(e);
514 } finally {
515 mHalMock = null;
516 mHalMockImpl = null;
517 }
518 }
519
keunyoung5c7cb262015-10-19 10:47:45 -0700520 /**
521 * Start mocking of vehicle HAL. For testing only.
keunyoung5c7cb262015-10-19 10:47:45 -0700522 */
keunyoung1ab8e182015-09-24 09:25:22 -0700523 public synchronized void startMocking(IVehicleNetworkHalMock mock) {
524 mHalMock = null;
525 mHalMockImpl = mock;
526 try {
527 mService.startMocking(mHalMockImpl);
528 } catch (RemoteException e) {
529 handleRemoteException(e);
530 }
531 }
532
keunyoung5c7cb262015-10-19 10:47:45 -0700533 /**
534 * Stop mocking of vehicle HAL. For testing only.
535 */
keunyoung1ab8e182015-09-24 09:25:22 -0700536 public synchronized void stopMocking(IVehicleNetworkHalMock mock) {
537 if (mock.asBinder() != mHalMockImpl.asBinder()) {
538 return;
539 }
540 try {
541 mService.stopMocking(mHalMockImpl);
542 } catch (RemoteException e) {
543 handleRemoteException(e);
544 } finally {
545 mHalMock = null;
546 mHalMockImpl = null;
547 }
548 }
549
Keun-young Park28dd4702015-11-19 18:06:04 -0800550 public synchronized void injectHalError(int errorCode, int property, int operation) {
551 try {
552 mService.injectHalError(errorCode, property, operation);
553 } catch (RemoteException e) {
554 handleRemoteException(e);
555 }
556 }
557
558 public synchronized void startErrorListening() {
559 try {
560 mService.startErrorListening(mVehicleNetworkListener);
561 } catch (RemoteException e) {
562 handleRemoteException(e);
563 }
564 }
565
566 public synchronized void stopErrorListening() {
567 try {
568 mService.stopErrorListening(mVehicleNetworkListener);
569 } catch (RemoteException e) {
570 handleRemoteException(e);
571 }
572 }
573
574 public synchronized void startHalRestartMonitoring() {
575 try {
576 mService.startHalRestartMonitoring(mVehicleNetworkListener);
577 } catch (RemoteException e) {
578 handleRemoteException(e);
579 }
580 }
581
582 public synchronized void stopHalRestartMonitoring() {
583 try {
584 mService.stopHalRestartMonitoring(mVehicleNetworkListener);
585 } catch (RemoteException e) {
586 handleRemoteException(e);
587 }
588 }
589
keunyoung1ab8e182015-09-24 09:25:22 -0700590 private synchronized VehicleNetworkHalMock getHalMock() {
591 return mHalMock;
592 }
593
keunyounge18e25d2015-08-28 15:57:19 -0700594 private void handleRemoteException(RemoteException e) {
595 throw new RuntimeException("Vehicle network service not working ", e);
596 }
597
598 private void handleVehicleNetworkEvents(VehiclePropValues values) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800599 mListener.onVehicleNetworkEvents(values);
keunyounge18e25d2015-08-28 15:57:19 -0700600 }
601
Keun-young Park28dd4702015-11-19 18:06:04 -0800602 private void handleHalError(int errorCode, int property, int operation) {
603 mListener.onHalError(errorCode, property, operation);
604 }
605
606 private void handleHalRestart(boolean inMocking) {
607 mListener.onHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700608 }
609
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700610 private void handleOnPropertySet(VehiclePropValue value) {
611 mListener.onPropertySet(value);
612 }
613
keunyounge18e25d2015-08-28 15:57:19 -0700614 private class EventHandler extends Handler {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800615
keunyounge18e25d2015-08-28 15:57:19 -0700616 private static final int MSG_EVENTS = 0;
Keun-young Park28dd4702015-11-19 18:06:04 -0800617 private static final int MSG_HAL_ERROR = 1;
618 private static final int MSG_HAL_RESTART = 2;
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700619 private static final int MSG_ON_PROPERTY_SET = 3;
keunyounge18e25d2015-08-28 15:57:19 -0700620
621 private EventHandler(Looper looper) {
622 super(looper);
623 }
624
625 private void notifyEvents(VehiclePropValues values) {
626 Message msg = obtainMessage(MSG_EVENTS, values);
627 sendMessage(msg);
628 }
629
Keun-young Park28dd4702015-11-19 18:06:04 -0800630 private void notifyHalError(int errorCode, int property, int operation) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800631 Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property, operation);
Keun-young Park28dd4702015-11-19 18:06:04 -0800632 sendMessage(msg);
633 }
634
635 private void notifyHalRestart(boolean inMocking) {
636 Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0);
637 sendMessage(msg);
638 }
639
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700640 private void notifyPropertySet(VehiclePropValue value) {
641 Message msg = obtainMessage(MSG_ON_PROPERTY_SET, value);
642 sendMessage(msg);
643 }
644
keunyounge18e25d2015-08-28 15:57:19 -0700645 @Override
646 public void handleMessage(Message msg) {
647 switch (msg.what) {
648 case MSG_EVENTS:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800649 handleVehicleNetworkEvents((VehiclePropValues) msg.obj);
Keun-young Park28dd4702015-11-19 18:06:04 -0800650 break;
651 case MSG_HAL_ERROR:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800652 handleHalError(msg.arg1, msg.arg2, (Integer) msg.obj);
Keun-young Park28dd4702015-11-19 18:06:04 -0800653 break;
654 case MSG_HAL_RESTART:
655 handleHalRestart(msg.arg1 == 1);
keunyounge18e25d2015-08-28 15:57:19 -0700656 break;
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700657 case MSG_ON_PROPERTY_SET:
658 handleOnPropertySet((VehiclePropValue) msg.obj);
keunyounge18e25d2015-08-28 15:57:19 -0700659 default:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800660 Log.w(TAG, "Unknown message:" + msg.what, new RuntimeException());
keunyounge18e25d2015-08-28 15:57:19 -0700661 break;
662 }
663 }
664 }
665
666 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800667
keunyounge18e25d2015-08-28 15:57:19 -0700668 private final WeakReference<VehicleNetwork> mVehicleNetwork;
669
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700670 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNetwork) {
671 mVehicleNetwork = new WeakReference<>(vehicleNetwork);
keunyounge18e25d2015-08-28 15:57:19 -0700672 }
673
674 @Override
675 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
676 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
677 if (vehicleNetwork != null) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800678 vehicleNetwork.mEventHandler.notifyEvents(values.values);
679 }
680 }
681
682 @Override
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700683 public void onPropertySet(VehiclePropValueParcelable value) {
684 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
685 if (vehicleNetwork != null) {
686 vehicleNetwork.mEventHandler.notifyPropertySet(value.value);
687 }
688 }
689
690 @Override
Keun-young Park28dd4702015-11-19 18:06:04 -0800691 public void onHalError(int errorCode, int property, int operation) {
692 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
693 if (vehicleNetwork != null) {
694 vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation);
695 }
696 }
697
698 @Override
699 public void onHalRestart(boolean inMocking) {
700 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
701 if (vehicleNetwork != null) {
702 vehicleNetwork.mEventHandler.notifyHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700703 }
704 }
705 }
keunyoung1ab8e182015-09-24 09:25:22 -0700706
707 private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
708 private final WeakReference<VehicleNetwork> mVehicleNetwork;
709
710 private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800711 mVehicleNetwork = new WeakReference<>(vehicleNewotk);
keunyoung1ab8e182015-09-24 09:25:22 -0700712 }
713
714 @Override
715 public VehiclePropConfigsParcelable onListProperties() {
716 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
717 if (vehicleNetwork == null) {
718 return null;
719 }
720 VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties();
721 return new VehiclePropConfigsParcelable(configs);
722 }
723
724 @Override
725 public void onPropertySet(VehiclePropValueParcelable value) {
726 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
727 if (vehicleNetwork == null) {
728 return;
729 }
730 vehicleNetwork.getHalMock().onPropertySet(value.value);
731 }
732
733 @Override
keunyoung7d74e6d2015-10-14 15:43:10 -0700734 public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
keunyoung1ab8e182015-09-24 09:25:22 -0700735 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
736 if (vehicleNetwork == null) {
737 return null;
738 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700739 VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value);
740 return new VehiclePropValueParcelable(resValue);
keunyoung1ab8e182015-09-24 09:25:22 -0700741 }
742
743 @Override
Keun-young Park0727f952015-12-21 14:30:07 -0800744 public void onPropertySubscribe(int property, float sampleRate, int zones) {
keunyoung1ab8e182015-09-24 09:25:22 -0700745 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
746 if (vehicleNetwork == null) {
747 return;
748 }
Keun-young Park0727f952015-12-21 14:30:07 -0800749 vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate, zones);
keunyoung1ab8e182015-09-24 09:25:22 -0700750 }
751
752 @Override
753 public void onPropertyUnsubscribe(int property) {
754 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
755 if (vehicleNetwork == null) {
756 return;
757 }
758 vehicleNetwork.getHalMock().onPropertyUnsubscribe(property);
759 }
760 }
Pavel Maltseve8f75372016-01-26 10:26:04 -0800761
762 private VehiclePropValue getProperty(int property, int zone, int customPropetyDataType) {
763 boolean isCustom = isCustomProperty(property);
764 int valueType = isCustom
765 ? customPropetyDataType
766 : VehicleNetworkConsts.getVehicleValueType(property);
767
768 VehiclePropValue.Builder valuePrototypeBuilder =
769 VehiclePropValueUtil.createBuilder(property, valueType, 0);
770
771 if (zone != NO_ZONE) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800772 valuePrototypeBuilder.setZone(zone);
Pavel Maltseve8f75372016-01-26 10:26:04 -0800773 }
774
775 VehiclePropValue v = getProperty(valuePrototypeBuilder.build());
776 if (v == null) {
777 // if property is invalid, IllegalArgumentException should have been thrown
778 // from getProperty.
779 throw new IllegalStateException();
780 }
781
782 if (!isCustom && v.getValueType() != valueType) {
Keun-young Park1488ef22016-02-25 14:00:54 -0800783 throw new IllegalArgumentException(
784 "Unexpected type for property 0x" + Integer.toHexString(property) +
785 " got:0x" + Integer.toHexString(v.getValueType())
786 + ", expecting:0x" + Integer.toHexString(valueType));
Pavel Maltseve8f75372016-01-26 10:26:04 -0800787 }
788 return v;
789 }
790
791 private void assertVectorLength(int actual, int property, int valueType) {
792 int expectedLen = getVectorLength(valueType);
793 if (expectedLen != actual) {
794 throw new IllegalStateException("Invalid array size for property: " + property
795 + ". Expected: " + expectedLen
796 + ", actual: " + actual);
797 }
798 }
keunyounge18e25d2015-08-28 15:57:19 -0700799}