blob: a54282a1da84e2d3de60e6f6f9ed15ae59692cfb [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
keunyounge18e25d2015-08-28 15:57:19 -070023import android.os.Handler;
24import android.os.Looper;
25import android.os.Message;
26import android.os.RemoteException;
27import android.os.ServiceManager;
Keun-young Parkba485482016-03-24 13:24:31 -070028import android.os.ServiceSpecificException;
keunyounge18e25d2015-08-28 15:57:19 -070029import android.util.Log;
30
keunyoungd32f4e62015-09-21 11:33:06 -070031import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType;
keunyounge18e25d2015-08-28 15:57:19 -070032import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
33import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
34import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
keunyoung1ab8e182015-09-24 09:25:22 -070035import com.android.internal.annotations.GuardedBy;
keunyounge18e25d2015-08-28 15:57:19 -070036
37import java.lang.ref.WeakReference;
38
39/**
40 * System API to access Vehicle network. This is only for system services and applications should
keunyoung15882e52015-09-16 16:57:58 -070041 * not use this. All APIs will fail with security error if normal app tries this.
keunyounge18e25d2015-08-28 15:57:19 -070042 */
43public class VehicleNetwork {
keunyoung1ab8e182015-09-24 09:25:22 -070044 /**
45 * Listener for VNS events.
46 */
keunyounge18e25d2015-08-28 15:57:19 -070047 public interface VehicleNetworkListener {
keunyoung1ab8e182015-09-24 09:25:22 -070048 /**
49 * Notify HAL events. This requires subscribing the property
50 */
keunyounge18e25d2015-08-28 15:57:19 -070051 void onVehicleNetworkEvents(VehiclePropValues values);
Keun-young Park28dd4702015-11-19 18:06:04 -080052 void onHalError(int errorCode, int property, int operation);
53 void onHalRestart(boolean inMocking);
keunyounge18e25d2015-08-28 15:57:19 -070054 }
55
keunyoung1ab8e182015-09-24 09:25:22 -070056 public interface VehicleNetworkHalMock {
57 VehiclePropConfigs onListProperties();
58 void onPropertySet(VehiclePropValue value);
Keun-young Park28dd4702015-11-19 18:06:04 -080059 VehiclePropValue onPropertyGet(VehiclePropValue value);
Keun-young Park0727f952015-12-21 14:30:07 -080060 void onPropertySubscribe(int property, float sampleRate, int zones);
keunyoung1ab8e182015-09-24 09:25:22 -070061 void onPropertyUnsubscribe(int property);
62 }
63
keunyounge18e25d2015-08-28 15:57:19 -070064 private static final String TAG = VehicleNetwork.class.getSimpleName();
65
66 private final IVehicleNetwork mService;
67 private final VehicleNetworkListener mListener;
68 private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
69 private final EventHandler mEventHandler;
70
keunyoung1ab8e182015-09-24 09:25:22 -070071 @GuardedBy("this")
72 private VehicleNetworkHalMock mHalMock;
73 private IVehicleNetworkHalMock mHalMockImpl;
74
keunyoung217ca352015-11-17 14:45:38 -080075 private static final int VNS_CONNECT_MAX_RETRY = 10;
76 private static final long VNS_RETRY_WAIT_TIME_MS = 1000;
Pavel Maltseve8f75372016-01-26 10:26:04 -080077 private static final int NO_ZONE = -1;
keunyoung217ca352015-11-17 14:45:38 -080078
keunyoung5c7cb262015-10-19 10:47:45 -070079 /**
80 * Factory method to create VehicleNetwork
Pavel Maltseve8f75372016-01-26 10:26:04 -080081 *
keunyoung5c7cb262015-10-19 10:47:45 -070082 * @param listener listener for listening events
83 * @param looper Looper to dispatch listener events
keunyoung5c7cb262015-10-19 10:47:45 -070084 */
keunyoung15882e52015-09-16 16:57:58 -070085 public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
86 Looper looper) {
keunyoung217ca352015-11-17 14:45:38 -080087 int retryCount = 0;
88 IVehicleNetwork service = null;
89 while (service == null) {
90 service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
91 IVehicleNetwork.class.getCanonicalName()));
92 retryCount++;
93 if (retryCount > VNS_CONNECT_MAX_RETRY) {
94 break;
95 }
96 try {
97 Thread.sleep(VNS_RETRY_WAIT_TIME_MS);
98 } catch (InterruptedException e) {
99 //ignore
100 }
101 }
keunyounge18e25d2015-08-28 15:57:19 -0700102 if (service == null) {
keunyoung15882e52015-09-16 16:57:58 -0700103 throw new RuntimeException("Vehicle network service not available:" +
104 IVehicleNetwork.class.getCanonicalName());
keunyounge18e25d2015-08-28 15:57:19 -0700105 }
106 return new VehicleNetwork(service, listener, looper);
107 }
108
109 private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
110 Looper looper) {
111 mService = service;
112 mListener = listener;
113 mEventHandler = new EventHandler(looper);
114 mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
115 }
116
keunyoung5c7cb262015-10-19 10:47:45 -0700117 /**
118 * List all properties from vehicle HAL
Pavel Maltseve8f75372016-01-26 10:26:04 -0800119 *
keunyoung5c7cb262015-10-19 10:47:45 -0700120 * @return all properties
121 */
keunyoung15882e52015-09-16 16:57:58 -0700122 public VehiclePropConfigs listProperties() {
123 return listProperties(0 /* all */);
124 }
125
keunyoung5c7cb262015-10-19 10:47:45 -0700126 /**
127 * Return configuration information of single property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800128 *
129 * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has has
130 * special meaning of list all properties.
keunyoung5c7cb262015-10-19 10:47:45 -0700131 * @return null if given property does not exist.
132 */
keunyounge18e25d2015-08-28 15:57:19 -0700133 public VehiclePropConfigs listProperties(int property) {
134 try {
135 VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
136 if (parcelable != null) {
137 return parcelable.configs;
138 }
139 } catch (RemoteException e) {
140 handleRemoteException(e);
141 }
142 return null;
143 }
144
keunyoung5c7cb262015-10-19 10:47:45 -0700145 /**
146 * Set property which will lead into writing the value to vehicle HAL.
Pavel Maltseve8f75372016-01-26 10:26:04 -0800147 *
148 * @throws IllegalArgumentException If value set has wrong value like wrong valueType, wrong
149 * data, and etc.
keunyoung5c7cb262015-10-19 10:47:45 -0700150 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700151 public void setProperty(VehiclePropValue value)
152 throws IllegalArgumentException, ServiceSpecificException {
keunyounge18e25d2015-08-28 15:57:19 -0700153 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
154 try {
keunyoung15882e52015-09-16 16:57:58 -0700155 mService.setProperty(parcelable);
keunyounge18e25d2015-08-28 15:57:19 -0700156 } catch (RemoteException e) {
157 handleRemoteException(e);
158 }
keunyounge18e25d2015-08-28 15:57:19 -0700159 }
160
keunyoung5c7cb262015-10-19 10:47:45 -0700161 /**
162 * Set integer type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800163 *
keunyoung5c7cb262015-10-19 10:47:45 -0700164 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
165 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700166 public void setIntProperty(int property, int value)
167 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700168 VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700169 setProperty(v);
170 }
171
keunyoung5c7cb262015-10-19 10:47:45 -0700172 /**
173 * Set int vector type property. Length of passed values should match with vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700174 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700175 public void setIntVectorProperty(int property, int[] values)
176 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700177 VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0);
178 setProperty(v);
keunyoungd32f4e62015-09-21 11:33:06 -0700179 }
180
keunyoung5c7cb262015-10-19 10:47:45 -0700181 /**
182 * Set long type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700183 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700184 public void setLongProperty(int property, long value)
185 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700186 VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700187 setProperty(v);
188 }
189
keunyoung5c7cb262015-10-19 10:47:45 -0700190 /**
191 * Set float type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700192 */
Keun-young Parke78bf532016-04-25 18:59:22 -0700193 public void setFloatProperty(int property, float value)
194 throws IllegalArgumentException, ServiceSpecificException {
keunyoung1ab8e182015-09-24 09:25:22 -0700195 VehiclePropValue v = VehiclePropValueUtil.createFloatValue(property, value, 0);
keunyoungfe30ba02015-09-17 17:56:35 -0700196 setProperty(v);
197 }
198
keunyoung5c7cb262015-10-19 10:47:45 -0700199 /**
200 * Set float vector type property. Length of values should match with vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700201 */
202 public void setFloatVectorProperty(int property, float[] values)
Keun-young Parke78bf532016-04-25 18:59:22 -0700203 throws IllegalArgumentException, ServiceSpecificException {
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
Pavel Maltseve8f75372016-01-26 10:26:04 -0800210 *
211 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
212 */
213 public void setBooleanProperty(int property, boolean value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700214 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800215 VehiclePropValue v = VehiclePropValueUtil.createBooleanValue(property, value, 0);
216 setProperty(v);
217 }
218
Keun-young Park4c6834a2016-06-28 12:58:23 -0700219 public void setStringProperty(int property, String value)
220 throws IllegalArgumentException, ServiceSpecificException {
221 VehiclePropValue v = VehiclePropValueUtil.createStringValue(property, value, 0);
222 setProperty(v);
223 }
224
Pavel Maltseve8f75372016-01-26 10:26:04 -0800225 /**
226 * Set zoned boolean type property
227 *
Steve Paik66481982015-10-27 15:22:38 -0700228 * @throws IllegalArgumentException For type mismatch (=the property is not boolean type)
229 */
230 public void setZonedBooleanProperty(int property, int zone, boolean value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700231 throws IllegalArgumentException, ServiceSpecificException {
Steve Paik66481982015-10-27 15:22:38 -0700232 VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0);
233 setProperty(v);
234 }
235
236 /**
237 * Set zoned float type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800238 *
Steve Paik66481982015-10-27 15:22:38 -0700239 * @throws IllegalArgumentException For type mismatch (=the property is not float type)
240 */
241 public void setZonedFloatProperty(int property, int zone, float value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700242 throws IllegalArgumentException, ServiceSpecificException {
Steve Paik66481982015-10-27 15:22:38 -0700243 VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0);
244 setProperty(v);
245 }
246
247 /**
248 * Set zoned integer type property
Pavel Maltseve8f75372016-01-26 10:26:04 -0800249 *
Steve Paik66481982015-10-27 15:22:38 -0700250 * @throws IllegalArgumentException For type mismatch (=the property is not int type)
251 */
252 public void setZonedIntProperty(int property, int zone, int value)
Keun-young Parke78bf532016-04-25 18:59:22 -0700253 throws IllegalArgumentException, ServiceSpecificException {
Steve Paik66481982015-10-27 15:22:38 -0700254 VehiclePropValue v = VehiclePropValueUtil.createZonedIntValue(property, zone, value, 0);
255 setProperty(v);
256 }
257
258 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800259 * Set zoned int vector type property. Length of passed values should match with vector length.
260 */
261 public void setZonedIntVectorProperty(int property, int zone, int[] values)
Keun-young Parke78bf532016-04-25 18:59:22 -0700262 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800263 VehiclePropValue v = VehiclePropValueUtil
264 .createZonedIntVectorValue(property, zone, values, 0);
265 setProperty(v);
266 }
267
268 /**
269 * Set zoned float vector type property. Length of passed values should match with vector
270 * length.
271 */
272 public void setZonedFloatVectorProperty(int property, int zone, float[] values)
Keun-young Parke78bf532016-04-25 18:59:22 -0700273 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800274 VehiclePropValue v = VehiclePropValueUtil
275 .createZonedFloatVectorValue(property, zone, values, 0);
276 setProperty(v);
277 }
278
279 /**
keunyoung5c7cb262015-10-19 10:47:45 -0700280 * Get property. This can be used for a property which does not require any other data.
keunyoung5c7cb262015-10-19 10:47:45 -0700281 */
Keun-young Parkba485482016-03-24 13:24:31 -0700282 public VehiclePropValue getProperty(int property) throws IllegalArgumentException,
283 ServiceSpecificException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700284 int valueType = VehicleNetworkConsts.getVehicleValueType(property);
Pavel Maltsev169b7a62016-01-26 15:56:07 -0800285 if (valueType == 0) {
286 throw new IllegalArgumentException("Data type is unknown for property: " + property);
287 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700288 VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build();
289 return getProperty(value);
290 }
291
keunyoung5c7cb262015-10-19 10:47:45 -0700292 /**
293 * Generic get method for any type of property. Some property may require setting data portion
294 * as get may return different result depending on the data set.
keunyoung5c7cb262015-10-19 10:47:45 -0700295 */
Keun-young Parkba485482016-03-24 13:24:31 -0700296 public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException,
297 ServiceSpecificException {
keunyoung7d74e6d2015-10-14 15:43:10 -0700298 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
keunyounge18e25d2015-08-28 15:57:19 -0700299 try {
keunyoung7d74e6d2015-10-14 15:43:10 -0700300 VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable);
301 if (resParcelable != null) {
302 return resParcelable.value;
keunyounge18e25d2015-08-28 15:57:19 -0700303 }
304 } catch (RemoteException e) {
305 handleRemoteException(e);
306 }
307 return null;
308 }
309
keunyoung5c7cb262015-10-19 10:47:45 -0700310 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800311 * Get int type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700312 */
Keun-young Parkba485482016-03-24 13:24:31 -0700313 public int getIntProperty(int property) throws IllegalArgumentException,
314 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800315 VehiclePropValue v = getProperty(
316 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
keunyoungd32f4e62015-09-21 11:33:06 -0700317 if (v.getInt32ValuesCount() != 1) {
318 throw new IllegalStateException();
319 }
320 return v.getInt32Values(0);
321 }
322
keunyoung5c7cb262015-10-19 10:47:45 -0700323 /**
Pavel Maltseve8f75372016-01-26 10:26:04 -0800324 * Get zoned int type property.
325 */
Keun-young Parkba485482016-03-24 13:24:31 -0700326 public int getZonedIntProperty(int property, int zone) throws IllegalArgumentException,
327 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800328 VehiclePropValue v = getProperty(
329 property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800330 if (v.getInt32ValuesCount() != 1) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800331 throw new IllegalStateException();
332 }
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800333 return v.getInt32Values(0);
Pavel Maltseve8f75372016-01-26 10:26:04 -0800334 }
335
336 /**
337 * Get int vector type property. Length of values should match vector length.
keunyoung5c7cb262015-10-19 10:47:45 -0700338 */
Keun-young Parkba485482016-03-24 13:24:31 -0700339 public int[] getIntVectorProperty(int property) throws IllegalArgumentException,
340 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800341 VehiclePropValue v = getProperty(
342 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32);
343 assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType());
344 return toIntArray(v.getInt32ValuesList());
345 }
346
347 /**
348 * Get zoned int vector type property. Length of values should match vector length.
349 */
350 public int[] getZonedIntVectorProperty(int property, int zone)
Keun-young Parkba485482016-03-24 13:24:31 -0700351 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800352 VehiclePropValue v = getProperty(
353 property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32);
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800354 assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType());
355 return toIntArray(v.getInt32ValuesList());
keunyoungfe30ba02015-09-17 17:56:35 -0700356 }
357
keunyoung5c7cb262015-10-19 10:47:45 -0700358 /**
359 * Get float type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700360 */
Keun-young Parkba485482016-03-24 13:24:31 -0700361 public float getFloatProperty(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_FLOAT);
keunyoungd32f4e62015-09-21 11:33:06 -0700365 if (v.getFloatValuesCount() != 1) {
366 throw new IllegalStateException();
367 }
368 return v.getFloatValues(0);
369 }
370
keunyoung5c7cb262015-10-19 10:47:45 -0700371 /**
372 * Get float vector type property. Length of values should match vector's length.
keunyoung5c7cb262015-10-19 10:47:45 -0700373 */
Keun-young Parkba485482016-03-24 13:24:31 -0700374 public float[] getFloatVectorProperty(int property) throws IllegalArgumentException,
375 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800376 VehiclePropValue v = getProperty(
377 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT);
378 assertVectorLength(v.getFloatValuesCount(), property, v.getValueType());
379 return toFloatArray(v.getFloatValuesList());
380 }
381
382 /**
383 * Get zoned float vector type property. Length of values should match vector's length.
384 */
385 public float[] getZonedFloatVectorProperty(int property, int zone)
Keun-young Parkba485482016-03-24 13:24:31 -0700386 throws IllegalArgumentException, ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800387 VehiclePropValue v = getProperty(property, zone,
388 VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_FLOAT);
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800389 assertVectorLength(v.getFloatValuesCount(), property, v.getValueType());
390 return toFloatArray(v.getFloatValuesList());
keunyoungfe30ba02015-09-17 17:56:35 -0700391 }
392
keunyoung5c7cb262015-10-19 10:47:45 -0700393 /**
394 * Get long type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700395 */
Keun-young Parkba485482016-03-24 13:24:31 -0700396 public long getLongProperty(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_INT64);
keunyoungfe30ba02015-09-17 17:56:35 -0700400 return v.getInt64Value();
401 }
402
keunyoung5c7cb262015-10-19 10:47:45 -0700403 /**
404 * Get string type property.
keunyoung5c7cb262015-10-19 10:47:45 -0700405 */
keunyoungfe30ba02015-09-17 17:56:35 -0700406 //TODO check UTF8 to java string conversion
Keun-young Parkba485482016-03-24 13:24:31 -0700407 public String getStringProperty(int property) throws IllegalArgumentException,
408 ServiceSpecificException {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800409 VehiclePropValue v = getProperty(
410 property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_STRING);
keunyoungfe30ba02015-09-17 17:56:35 -0700411 return v.getStringValue();
412 }
413
keunyoung5c7cb262015-10-19 10:47:45 -0700414 /**
415 * Subscribe given property with given sample rate.
keunyoung5c7cb262015-10-19 10:47:45 -0700416 */
417 public void subscribe(int property, float sampleRate) throws IllegalArgumentException {
Keun-young Park0727f952015-12-21 14:30:07 -0800418 subscribe(property, sampleRate, 0);
419 }
420
421 /**
422 * Subscribe given property with given sample rate.
Keun-young Park0727f952015-12-21 14:30:07 -0800423 */
424 public void subscribe(int property, float sampleRate, int zones)
425 throws IllegalArgumentException {
keunyounge18e25d2015-08-28 15:57:19 -0700426 try {
Keun-young Park0727f952015-12-21 14:30:07 -0800427 mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones);
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.
keunyoung5c7cb262015-10-19 10:47:45 -0700435 */
keunyounge18e25d2015-08-28 15:57:19 -0700436 public void unsubscribe(int property) {
437 try {
438 mService.unsubscribe(mVehicleNetworkListener, property);
439 } catch (RemoteException e) {
440 handleRemoteException(e);
441 }
442 }
443
keunyoung5c7cb262015-10-19 10:47:45 -0700444 /**
445 * Inject given value to all clients subscribing the property. This is for testing.
keunyoung5c7cb262015-10-19 10:47:45 -0700446 */
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.
keunyoung5c7cb262015-10-19 10:47:45 -0700457 */
keunyoung1ab8e182015-09-24 09:25:22 -0700458 public synchronized void startMocking(VehicleNetworkHalMock mock) {
459 mHalMock = mock;
460 mHalMockImpl = new IVehicleNetworkHalMockImpl(this);
461 try {
462 mService.startMocking(mHalMockImpl);
463 } catch (RemoteException e) {
464 handleRemoteException(e);
465 }
466 }
467
keunyoung5c7cb262015-10-19 10:47:45 -0700468 /**
469 * Stop mocking of vehicle HAL. For testing only.
470 */
keunyoung1ab8e182015-09-24 09:25:22 -0700471 public synchronized void stopMocking() {
Keun-young Park28dd4702015-11-19 18:06:04 -0800472 if (mHalMockImpl == null) {
473 return;
474 }
keunyoung1ab8e182015-09-24 09:25:22 -0700475 try {
476 mService.stopMocking(mHalMockImpl);
477 } catch (RemoteException e) {
478 handleRemoteException(e);
479 } finally {
480 mHalMock = null;
481 mHalMockImpl = null;
482 }
483 }
484
keunyoung5c7cb262015-10-19 10:47:45 -0700485 /**
486 * Start mocking of vehicle HAL. For testing only.
keunyoung5c7cb262015-10-19 10:47:45 -0700487 */
keunyoung1ab8e182015-09-24 09:25:22 -0700488 public synchronized void startMocking(IVehicleNetworkHalMock mock) {
489 mHalMock = null;
490 mHalMockImpl = mock;
491 try {
492 mService.startMocking(mHalMockImpl);
493 } catch (RemoteException e) {
494 handleRemoteException(e);
495 }
496 }
497
keunyoung5c7cb262015-10-19 10:47:45 -0700498 /**
499 * Stop mocking of vehicle HAL. For testing only.
500 */
keunyoung1ab8e182015-09-24 09:25:22 -0700501 public synchronized void stopMocking(IVehicleNetworkHalMock mock) {
502 if (mock.asBinder() != mHalMockImpl.asBinder()) {
503 return;
504 }
505 try {
506 mService.stopMocking(mHalMockImpl);
507 } catch (RemoteException e) {
508 handleRemoteException(e);
509 } finally {
510 mHalMock = null;
511 mHalMockImpl = null;
512 }
513 }
514
Keun-young Park28dd4702015-11-19 18:06:04 -0800515 public synchronized void injectHalError(int errorCode, int property, int operation) {
516 try {
517 mService.injectHalError(errorCode, property, operation);
518 } catch (RemoteException e) {
519 handleRemoteException(e);
520 }
521 }
522
523 public synchronized void startErrorListening() {
524 try {
525 mService.startErrorListening(mVehicleNetworkListener);
526 } catch (RemoteException e) {
527 handleRemoteException(e);
528 }
529 }
530
531 public synchronized void stopErrorListening() {
532 try {
533 mService.stopErrorListening(mVehicleNetworkListener);
534 } catch (RemoteException e) {
535 handleRemoteException(e);
536 }
537 }
538
539 public synchronized void startHalRestartMonitoring() {
540 try {
541 mService.startHalRestartMonitoring(mVehicleNetworkListener);
542 } catch (RemoteException e) {
543 handleRemoteException(e);
544 }
545 }
546
547 public synchronized void stopHalRestartMonitoring() {
548 try {
549 mService.stopHalRestartMonitoring(mVehicleNetworkListener);
550 } catch (RemoteException e) {
551 handleRemoteException(e);
552 }
553 }
554
keunyoung1ab8e182015-09-24 09:25:22 -0700555 private synchronized VehicleNetworkHalMock getHalMock() {
556 return mHalMock;
557 }
558
keunyounge18e25d2015-08-28 15:57:19 -0700559 private void handleRemoteException(RemoteException e) {
560 throw new RuntimeException("Vehicle network service not working ", e);
561 }
562
563 private void handleVehicleNetworkEvents(VehiclePropValues values) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800564 mListener.onVehicleNetworkEvents(values);
keunyounge18e25d2015-08-28 15:57:19 -0700565 }
566
Keun-young Park28dd4702015-11-19 18:06:04 -0800567 private void handleHalError(int errorCode, int property, int operation) {
568 mListener.onHalError(errorCode, property, operation);
569 }
570
571 private void handleHalRestart(boolean inMocking) {
572 mListener.onHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700573 }
574
575 private class EventHandler extends Handler {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800576
keunyounge18e25d2015-08-28 15:57:19 -0700577 private static final int MSG_EVENTS = 0;
Keun-young Park28dd4702015-11-19 18:06:04 -0800578 private static final int MSG_HAL_ERROR = 1;
579 private static final int MSG_HAL_RESTART = 2;
keunyounge18e25d2015-08-28 15:57:19 -0700580
581 private EventHandler(Looper looper) {
582 super(looper);
583 }
584
585 private void notifyEvents(VehiclePropValues values) {
586 Message msg = obtainMessage(MSG_EVENTS, values);
587 sendMessage(msg);
588 }
589
Keun-young Park28dd4702015-11-19 18:06:04 -0800590 private void notifyHalError(int errorCode, int property, int operation) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800591 Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property, operation);
Keun-young Park28dd4702015-11-19 18:06:04 -0800592 sendMessage(msg);
593 }
594
595 private void notifyHalRestart(boolean inMocking) {
596 Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0);
597 sendMessage(msg);
598 }
599
keunyounge18e25d2015-08-28 15:57:19 -0700600 @Override
601 public void handleMessage(Message msg) {
602 switch (msg.what) {
603 case MSG_EVENTS:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800604 handleVehicleNetworkEvents((VehiclePropValues) msg.obj);
Keun-young Park28dd4702015-11-19 18:06:04 -0800605 break;
606 case MSG_HAL_ERROR:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800607 handleHalError(msg.arg1, msg.arg2, (Integer) msg.obj);
Keun-young Park28dd4702015-11-19 18:06:04 -0800608 break;
609 case MSG_HAL_RESTART:
610 handleHalRestart(msg.arg1 == 1);
keunyounge18e25d2015-08-28 15:57:19 -0700611 break;
612 default:
Pavel Maltseve8f75372016-01-26 10:26:04 -0800613 Log.w(TAG, "Unknown message:" + msg.what, new RuntimeException());
keunyounge18e25d2015-08-28 15:57:19 -0700614 break;
615 }
616 }
617 }
618
619 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800620
keunyounge18e25d2015-08-28 15:57:19 -0700621 private final WeakReference<VehicleNetwork> mVehicleNetwork;
622
623 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800624 mVehicleNetwork = new WeakReference<>(vehicleNewotk);
keunyounge18e25d2015-08-28 15:57:19 -0700625 }
626
627 @Override
628 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
629 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
630 if (vehicleNetwork != null) {
Keun-young Park28dd4702015-11-19 18:06:04 -0800631 vehicleNetwork.mEventHandler.notifyEvents(values.values);
632 }
633 }
634
635 @Override
636 public void onHalError(int errorCode, int property, int operation) {
637 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
638 if (vehicleNetwork != null) {
639 vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation);
640 }
641 }
642
643 @Override
644 public void onHalRestart(boolean inMocking) {
645 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
646 if (vehicleNetwork != null) {
647 vehicleNetwork.mEventHandler.notifyHalRestart(inMocking);
keunyounge18e25d2015-08-28 15:57:19 -0700648 }
649 }
650 }
keunyoung1ab8e182015-09-24 09:25:22 -0700651
652 private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub {
653 private final WeakReference<VehicleNetwork> mVehicleNetwork;
654
655 private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) {
Pavel Maltseve8f75372016-01-26 10:26:04 -0800656 mVehicleNetwork = new WeakReference<>(vehicleNewotk);
keunyoung1ab8e182015-09-24 09:25:22 -0700657 }
658
659 @Override
660 public VehiclePropConfigsParcelable onListProperties() {
661 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
662 if (vehicleNetwork == null) {
663 return null;
664 }
665 VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties();
666 return new VehiclePropConfigsParcelable(configs);
667 }
668
669 @Override
670 public void onPropertySet(VehiclePropValueParcelable value) {
671 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
672 if (vehicleNetwork == null) {
673 return;
674 }
675 vehicleNetwork.getHalMock().onPropertySet(value.value);
676 }
677
678 @Override
keunyoung7d74e6d2015-10-14 15:43:10 -0700679 public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) {
keunyoung1ab8e182015-09-24 09:25:22 -0700680 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
681 if (vehicleNetwork == null) {
682 return null;
683 }
keunyoung7d74e6d2015-10-14 15:43:10 -0700684 VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value);
685 return new VehiclePropValueParcelable(resValue);
keunyoung1ab8e182015-09-24 09:25:22 -0700686 }
687
688 @Override
Keun-young Park0727f952015-12-21 14:30:07 -0800689 public void onPropertySubscribe(int property, float sampleRate, int zones) {
keunyoung1ab8e182015-09-24 09:25:22 -0700690 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
691 if (vehicleNetwork == null) {
692 return;
693 }
Keun-young Park0727f952015-12-21 14:30:07 -0800694 vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate, zones);
keunyoung1ab8e182015-09-24 09:25:22 -0700695 }
696
697 @Override
698 public void onPropertyUnsubscribe(int property) {
699 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
700 if (vehicleNetwork == null) {
701 return;
702 }
703 vehicleNetwork.getHalMock().onPropertyUnsubscribe(property);
704 }
705 }
Pavel Maltseve8f75372016-01-26 10:26:04 -0800706
707 private VehiclePropValue getProperty(int property, int zone, int customPropetyDataType) {
708 boolean isCustom = isCustomProperty(property);
709 int valueType = isCustom
710 ? customPropetyDataType
711 : VehicleNetworkConsts.getVehicleValueType(property);
712
713 VehiclePropValue.Builder valuePrototypeBuilder =
714 VehiclePropValueUtil.createBuilder(property, valueType, 0);
715
716 if (zone != NO_ZONE) {
Keun-young Park71b2f5c2016-03-10 18:44:40 -0800717 valuePrototypeBuilder.setZone(zone);
Pavel Maltseve8f75372016-01-26 10:26:04 -0800718 }
719
720 VehiclePropValue v = getProperty(valuePrototypeBuilder.build());
721 if (v == null) {
722 // if property is invalid, IllegalArgumentException should have been thrown
723 // from getProperty.
724 throw new IllegalStateException();
725 }
726
727 if (!isCustom && v.getValueType() != valueType) {
Keun-young Park1488ef22016-02-25 14:00:54 -0800728 throw new IllegalArgumentException(
729 "Unexpected type for property 0x" + Integer.toHexString(property) +
730 " got:0x" + Integer.toHexString(v.getValueType())
731 + ", expecting:0x" + Integer.toHexString(valueType));
Pavel Maltseve8f75372016-01-26 10:26:04 -0800732 }
733 return v;
734 }
735
736 private void assertVectorLength(int actual, int property, int valueType) {
737 int expectedLen = getVectorLength(valueType);
738 if (expectedLen != actual) {
739 throw new IllegalStateException("Invalid array size for property: " + property
740 + ". Expected: " + expectedLen
741 + ", actual: " + actual);
742 }
743 }
keunyounge18e25d2015-08-28 15:57:19 -0700744}