keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.car.vehiclenetwork; |
| 17 | |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 18 | import static com.android.car.vehiclenetwork.VehiclePropValueUtil.getVectorLength; |
| 19 | import static com.android.car.vehiclenetwork.VehiclePropValueUtil.isCustomProperty; |
| 20 | import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toFloatArray; |
| 21 | import static com.android.car.vehiclenetwork.VehiclePropValueUtil.toIntArray; |
| 22 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.Looper; |
| 25 | import android.os.Message; |
| 26 | import android.os.RemoteException; |
| 27 | import android.os.ServiceManager; |
| 28 | import android.util.Log; |
| 29 | |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 30 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 31 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs; |
| 32 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
| 33 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues; |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 34 | import com.android.car.vehiclenetwork.VehicleNetworkProto.ZonedValue; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 35 | import com.android.internal.annotations.GuardedBy; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 36 | |
| 37 | import java.lang.ref.WeakReference; |
| 38 | |
| 39 | /** |
| 40 | * System API to access Vehicle network. This is only for system services and applications should |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 41 | * not use this. All APIs will fail with security error if normal app tries this. |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 42 | */ |
| 43 | public class VehicleNetwork { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Listener for VNS events. |
| 46 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 47 | public interface VehicleNetworkListener { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 48 | /** |
| 49 | * Notify HAL events. This requires subscribing the property |
| 50 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 51 | void onVehicleNetworkEvents(VehiclePropValues values); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 52 | void onHalError(int errorCode, int property, int operation); |
| 53 | void onHalRestart(boolean inMocking); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 54 | } |
| 55 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 56 | public interface VehicleNetworkHalMock { |
| 57 | VehiclePropConfigs onListProperties(); |
| 58 | void onPropertySet(VehiclePropValue value); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 59 | VehiclePropValue onPropertyGet(VehiclePropValue value); |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 60 | void onPropertySubscribe(int property, float sampleRate, int zones); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 61 | void onPropertyUnsubscribe(int property); |
| 62 | } |
| 63 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 64 | 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 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 71 | @GuardedBy("this") |
| 72 | private VehicleNetworkHalMock mHalMock; |
| 73 | private IVehicleNetworkHalMock mHalMockImpl; |
| 74 | |
keunyoung | 217ca35 | 2015-11-17 14:45:38 -0800 | [diff] [blame] | 75 | private static final int VNS_CONNECT_MAX_RETRY = 10; |
| 76 | private static final long VNS_RETRY_WAIT_TIME_MS = 1000; |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 77 | private static final int NO_ZONE = -1; |
keunyoung | 217ca35 | 2015-11-17 14:45:38 -0800 | [diff] [blame] | 78 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Factory method to create VehicleNetwork |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 81 | * |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 82 | * @param listener listener for listening events |
| 83 | * @param looper Looper to dispatch listener events |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 84 | */ |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 85 | public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener, |
| 86 | Looper looper) { |
keunyoung | 217ca35 | 2015-11-17 14:45:38 -0800 | [diff] [blame] | 87 | 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 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 102 | if (service == null) { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 103 | throw new RuntimeException("Vehicle network service not available:" + |
| 104 | IVehicleNetwork.class.getCanonicalName()); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 105 | } |
| 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 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 117 | /** |
| 118 | * List all properties from vehicle HAL |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 119 | * |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 120 | * @return all properties |
| 121 | */ |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 122 | public VehiclePropConfigs listProperties() { |
| 123 | return listProperties(0 /* all */); |
| 124 | } |
| 125 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 126 | /** |
| 127 | * Return configuration information of single property |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 128 | * |
| 129 | * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has has |
| 130 | * special meaning of list all properties. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 131 | * @return null if given property does not exist. |
| 132 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 133 | 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 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Set property which will lead into writing the value to vehicle HAL. |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 147 | * |
| 148 | * @throws IllegalArgumentException If value set has wrong value like wrong valueType, wrong |
| 149 | * data, and etc. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 150 | */ |
| 151 | public void setProperty(VehiclePropValue value) throws IllegalArgumentException { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 152 | VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value); |
| 153 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 154 | mService.setProperty(parcelable); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 155 | } catch (RemoteException e) { |
| 156 | handleRemoteException(e); |
| 157 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 158 | } |
| 159 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Set integer type property |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 162 | * |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 163 | * @throws IllegalArgumentException For type mismatch (=the property is not int type) |
| 164 | */ |
| 165 | public void setIntProperty(int property, int value) throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 166 | VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 167 | setProperty(v); |
| 168 | } |
| 169 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Set int vector type property. Length of passed values should match with vector length. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 172 | */ |
| 173 | public void setIntVectorProperty(int property, int[] values) throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 174 | VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0); |
| 175 | setProperty(v); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 176 | } |
| 177 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 178 | /** |
| 179 | * Set long type property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 180 | */ |
| 181 | public void setLongProperty(int property, long value) throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 182 | VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 183 | setProperty(v); |
| 184 | } |
| 185 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Set float type property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 188 | */ |
| 189 | public void setFloatProperty(int property, float value) throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 190 | VehiclePropValue v = VehiclePropValueUtil.createFloatValue(property, value, 0); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 191 | setProperty(v); |
| 192 | } |
| 193 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Set float vector type property. Length of values should match with vector length. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 196 | */ |
| 197 | public void setFloatVectorProperty(int property, float[] values) |
| 198 | throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 199 | VehiclePropValue v = VehiclePropValueUtil.createFloatVectorValue(property, values, 0); |
| 200 | setProperty(v); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 201 | } |
| 202 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 203 | /** |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 204 | * Set zoned boolean type property |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 205 | * |
| 206 | * @throws IllegalArgumentException For type mismatch (=the property is not boolean type) |
| 207 | */ |
| 208 | public void setBooleanProperty(int property, boolean value) |
| 209 | throws IllegalArgumentException { |
| 210 | VehiclePropValue v = VehiclePropValueUtil.createBooleanValue(property, value, 0); |
| 211 | setProperty(v); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Set zoned boolean type property |
| 216 | * |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 217 | * @throws IllegalArgumentException For type mismatch (=the property is not boolean type) |
| 218 | */ |
| 219 | public void setZonedBooleanProperty(int property, int zone, boolean value) |
| 220 | throws IllegalArgumentException { |
| 221 | VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0); |
| 222 | setProperty(v); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Set zoned float type property |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 227 | * |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 228 | * @throws IllegalArgumentException For type mismatch (=the property is not float type) |
| 229 | */ |
| 230 | public void setZonedFloatProperty(int property, int zone, float value) |
| 231 | throws IllegalArgumentException { |
| 232 | VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0); |
| 233 | setProperty(v); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Set zoned integer type property |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 238 | * |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame] | 239 | * @throws IllegalArgumentException For type mismatch (=the property is not int type) |
| 240 | */ |
| 241 | public void setZonedIntProperty(int property, int zone, int value) |
| 242 | throws IllegalArgumentException { |
| 243 | VehiclePropValue v = VehiclePropValueUtil.createZonedIntValue(property, zone, value, 0); |
| 244 | setProperty(v); |
| 245 | } |
| 246 | |
| 247 | /** |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 248 | * Set zoned int vector type property. Length of passed values should match with vector length. |
| 249 | */ |
| 250 | public void setZonedIntVectorProperty(int property, int zone, int[] values) |
| 251 | throws IllegalArgumentException { |
| 252 | VehiclePropValue v = VehiclePropValueUtil |
| 253 | .createZonedIntVectorValue(property, zone, values, 0); |
| 254 | setProperty(v); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Set zoned float vector type property. Length of passed values should match with vector |
| 259 | * length. |
| 260 | */ |
| 261 | public void setZonedFloatVectorProperty(int property, int zone, float[] values) |
| 262 | throws IllegalArgumentException { |
| 263 | VehiclePropValue v = VehiclePropValueUtil |
| 264 | .createZonedFloatVectorValue(property, zone, values, 0); |
| 265 | setProperty(v); |
| 266 | } |
| 267 | |
| 268 | /** |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 269 | * Get property. This can be used for a property which does not require any other data. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 270 | */ |
| 271 | public VehiclePropValue getProperty(int property) throws IllegalArgumentException { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 272 | int valueType = VehicleNetworkConsts.getVehicleValueType(property); |
| 273 | VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build(); |
| 274 | return getProperty(value); |
| 275 | } |
| 276 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 277 | /** |
| 278 | * Generic get method for any type of property. Some property may require setting data portion |
| 279 | * as get may return different result depending on the data set. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 280 | */ |
| 281 | public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 282 | VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 283 | try { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 284 | VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable); |
| 285 | if (resParcelable != null) { |
| 286 | return resParcelable.value; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 287 | } |
| 288 | } catch (RemoteException e) { |
| 289 | handleRemoteException(e); |
| 290 | } |
| 291 | return null; |
| 292 | } |
| 293 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 294 | /** |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 295 | * Get int type property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 296 | */ |
| 297 | public int getIntProperty(int property) throws IllegalArgumentException { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 298 | VehiclePropValue v = getProperty( |
| 299 | property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 300 | if (v.getInt32ValuesCount() != 1) { |
| 301 | throw new IllegalStateException(); |
| 302 | } |
| 303 | return v.getInt32Values(0); |
| 304 | } |
| 305 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 306 | /** |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 307 | * Get zoned int type property. |
| 308 | */ |
| 309 | public int getZonedIntProperty(int property, int zone) throws IllegalArgumentException { |
| 310 | VehiclePropValue v = getProperty( |
| 311 | property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32); |
| 312 | if (v.getZonedValue().getInt32ValuesCount() != 1) { |
| 313 | throw new IllegalStateException(); |
| 314 | } |
| 315 | return v.getZonedValue().getInt32Values(0); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Get int vector type property. Length of values should match vector length. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 320 | */ |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 321 | public int[] getIntVectorProperty(int property) throws IllegalArgumentException { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 322 | VehiclePropValue v = getProperty( |
| 323 | property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT32); |
| 324 | assertVectorLength(v.getInt32ValuesCount(), property, v.getValueType()); |
| 325 | return toIntArray(v.getInt32ValuesList()); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Get zoned int vector type property. Length of values should match vector length. |
| 330 | */ |
| 331 | public int[] getZonedIntVectorProperty(int property, int zone) |
| 332 | throws IllegalArgumentException { |
| 333 | VehiclePropValue v = getProperty( |
| 334 | property, zone, VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_INT32); |
| 335 | assertVectorLength(v.getZonedValue().getInt32ValuesCount(), property, v.getValueType()); |
| 336 | return toIntArray(v.getZonedValue().getInt32ValuesList()); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 337 | } |
| 338 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 339 | /** |
| 340 | * Get float type property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 341 | */ |
| 342 | public float getFloatProperty(int property) throws IllegalArgumentException { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 343 | VehiclePropValue v = getProperty( |
| 344 | property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 345 | if (v.getFloatValuesCount() != 1) { |
| 346 | throw new IllegalStateException(); |
| 347 | } |
| 348 | return v.getFloatValues(0); |
| 349 | } |
| 350 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 351 | /** |
| 352 | * Get float vector type property. Length of values should match vector's length. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 353 | */ |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 354 | public float[] getFloatVectorProperty(int property) throws IllegalArgumentException { |
| 355 | VehiclePropValue v = getProperty( |
| 356 | property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT); |
| 357 | assertVectorLength(v.getFloatValuesCount(), property, v.getValueType()); |
| 358 | return toFloatArray(v.getFloatValuesList()); |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Get zoned float vector type property. Length of values should match vector's length. |
| 363 | */ |
| 364 | public float[] getZonedFloatVectorProperty(int property, int zone) |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 365 | throws IllegalArgumentException { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 366 | VehiclePropValue v = getProperty(property, zone, |
| 367 | VehicleValueType.VEHICLE_VALUE_TYPE_ZONED_FLOAT); |
| 368 | assertVectorLength(v.getZonedValue().getFloatValuesCount(), property, v.getValueType()); |
| 369 | return toFloatArray(v.getZonedValue().getFloatValuesList()); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 370 | } |
| 371 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 372 | /** |
| 373 | * Get long type property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 374 | */ |
| 375 | public long getLongProperty(int property) throws IllegalArgumentException { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 376 | VehiclePropValue v = getProperty( |
| 377 | property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_INT64); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 378 | return v.getInt64Value(); |
| 379 | } |
| 380 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 381 | /** |
| 382 | * Get string type property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 383 | */ |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 384 | //TODO check UTF8 to java string conversion |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 385 | public String getStringProperty(int property) throws IllegalArgumentException { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 386 | VehiclePropValue v = getProperty( |
| 387 | property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_STRING); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 388 | return v.getStringValue(); |
| 389 | } |
| 390 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 391 | /** |
| 392 | * Subscribe given property with given sample rate. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 393 | */ |
| 394 | public void subscribe(int property, float sampleRate) throws IllegalArgumentException { |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 395 | subscribe(property, sampleRate, 0); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Subscribe given property with given sample rate. |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 400 | */ |
| 401 | public void subscribe(int property, float sampleRate, int zones) |
| 402 | throws IllegalArgumentException { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 403 | try { |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 404 | mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 405 | } catch (RemoteException e) { |
| 406 | handleRemoteException(e); |
| 407 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 408 | } |
| 409 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 410 | /** |
| 411 | * Stop subscribing the property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 412 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 413 | public void unsubscribe(int property) { |
| 414 | try { |
| 415 | mService.unsubscribe(mVehicleNetworkListener, property); |
| 416 | } catch (RemoteException e) { |
| 417 | handleRemoteException(e); |
| 418 | } |
| 419 | } |
| 420 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 421 | /** |
| 422 | * Inject given value to all clients subscribing the property. This is for testing. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 423 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 424 | public synchronized void injectEvent(VehiclePropValue value) { |
| 425 | try { |
| 426 | mService.injectEvent(new VehiclePropValueParcelable(value)); |
| 427 | } catch (RemoteException e) { |
| 428 | handleRemoteException(e); |
| 429 | } |
| 430 | } |
| 431 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 432 | /** |
| 433 | * Start mocking of vehicle HAL. For testing only. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 434 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 435 | public synchronized void startMocking(VehicleNetworkHalMock mock) { |
| 436 | mHalMock = mock; |
| 437 | mHalMockImpl = new IVehicleNetworkHalMockImpl(this); |
| 438 | try { |
| 439 | mService.startMocking(mHalMockImpl); |
| 440 | } catch (RemoteException e) { |
| 441 | handleRemoteException(e); |
| 442 | } |
| 443 | } |
| 444 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 445 | /** |
| 446 | * Stop mocking of vehicle HAL. For testing only. |
| 447 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 448 | public synchronized void stopMocking() { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 449 | if (mHalMockImpl == null) { |
| 450 | return; |
| 451 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 452 | try { |
| 453 | mService.stopMocking(mHalMockImpl); |
| 454 | } catch (RemoteException e) { |
| 455 | handleRemoteException(e); |
| 456 | } finally { |
| 457 | mHalMock = null; |
| 458 | mHalMockImpl = null; |
| 459 | } |
| 460 | } |
| 461 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 462 | /** |
| 463 | * Start mocking of vehicle HAL. For testing only. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 464 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 465 | public synchronized void startMocking(IVehicleNetworkHalMock mock) { |
| 466 | mHalMock = null; |
| 467 | mHalMockImpl = mock; |
| 468 | try { |
| 469 | mService.startMocking(mHalMockImpl); |
| 470 | } catch (RemoteException e) { |
| 471 | handleRemoteException(e); |
| 472 | } |
| 473 | } |
| 474 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 475 | /** |
| 476 | * Stop mocking of vehicle HAL. For testing only. |
| 477 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 478 | public synchronized void stopMocking(IVehicleNetworkHalMock mock) { |
| 479 | if (mock.asBinder() != mHalMockImpl.asBinder()) { |
| 480 | return; |
| 481 | } |
| 482 | try { |
| 483 | mService.stopMocking(mHalMockImpl); |
| 484 | } catch (RemoteException e) { |
| 485 | handleRemoteException(e); |
| 486 | } finally { |
| 487 | mHalMock = null; |
| 488 | mHalMockImpl = null; |
| 489 | } |
| 490 | } |
| 491 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 492 | public synchronized void injectHalError(int errorCode, int property, int operation) { |
| 493 | try { |
| 494 | mService.injectHalError(errorCode, property, operation); |
| 495 | } catch (RemoteException e) { |
| 496 | handleRemoteException(e); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | public synchronized void startErrorListening() { |
| 501 | try { |
| 502 | mService.startErrorListening(mVehicleNetworkListener); |
| 503 | } catch (RemoteException e) { |
| 504 | handleRemoteException(e); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | public synchronized void stopErrorListening() { |
| 509 | try { |
| 510 | mService.stopErrorListening(mVehicleNetworkListener); |
| 511 | } catch (RemoteException e) { |
| 512 | handleRemoteException(e); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | public synchronized void startHalRestartMonitoring() { |
| 517 | try { |
| 518 | mService.startHalRestartMonitoring(mVehicleNetworkListener); |
| 519 | } catch (RemoteException e) { |
| 520 | handleRemoteException(e); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | public synchronized void stopHalRestartMonitoring() { |
| 525 | try { |
| 526 | mService.stopHalRestartMonitoring(mVehicleNetworkListener); |
| 527 | } catch (RemoteException e) { |
| 528 | handleRemoteException(e); |
| 529 | } |
| 530 | } |
| 531 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 532 | private synchronized VehicleNetworkHalMock getHalMock() { |
| 533 | return mHalMock; |
| 534 | } |
| 535 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 536 | private void handleRemoteException(RemoteException e) { |
| 537 | throw new RuntimeException("Vehicle network service not working ", e); |
| 538 | } |
| 539 | |
| 540 | private void handleVehicleNetworkEvents(VehiclePropValues values) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 541 | mListener.onVehicleNetworkEvents(values); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 542 | } |
| 543 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 544 | private void handleHalError(int errorCode, int property, int operation) { |
| 545 | mListener.onHalError(errorCode, property, operation); |
| 546 | } |
| 547 | |
| 548 | private void handleHalRestart(boolean inMocking) { |
| 549 | mListener.onHalRestart(inMocking); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | private class EventHandler extends Handler { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 553 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 554 | private static final int MSG_EVENTS = 0; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 555 | private static final int MSG_HAL_ERROR = 1; |
| 556 | private static final int MSG_HAL_RESTART = 2; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 557 | |
| 558 | private EventHandler(Looper looper) { |
| 559 | super(looper); |
| 560 | } |
| 561 | |
| 562 | private void notifyEvents(VehiclePropValues values) { |
| 563 | Message msg = obtainMessage(MSG_EVENTS, values); |
| 564 | sendMessage(msg); |
| 565 | } |
| 566 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 567 | private void notifyHalError(int errorCode, int property, int operation) { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 568 | Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property, operation); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 569 | sendMessage(msg); |
| 570 | } |
| 571 | |
| 572 | private void notifyHalRestart(boolean inMocking) { |
| 573 | Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0); |
| 574 | sendMessage(msg); |
| 575 | } |
| 576 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 577 | @Override |
| 578 | public void handleMessage(Message msg) { |
| 579 | switch (msg.what) { |
| 580 | case MSG_EVENTS: |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 581 | handleVehicleNetworkEvents((VehiclePropValues) msg.obj); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 582 | break; |
| 583 | case MSG_HAL_ERROR: |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 584 | handleHalError(msg.arg1, msg.arg2, (Integer) msg.obj); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 585 | break; |
| 586 | case MSG_HAL_RESTART: |
| 587 | handleHalRestart(msg.arg1 == 1); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 588 | break; |
| 589 | default: |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 590 | Log.w(TAG, "Unknown message:" + msg.what, new RuntimeException()); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 597 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 598 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 599 | |
| 600 | private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 601 | mVehicleNetwork = new WeakReference<>(vehicleNewotk); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) { |
| 606 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 607 | if (vehicleNetwork != null) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 608 | vehicleNetwork.mEventHandler.notifyEvents(values.values); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | @Override |
| 613 | public void onHalError(int errorCode, int property, int operation) { |
| 614 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 615 | if (vehicleNetwork != null) { |
| 616 | vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | @Override |
| 621 | public void onHalRestart(boolean inMocking) { |
| 622 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 623 | if (vehicleNetwork != null) { |
| 624 | vehicleNetwork.mEventHandler.notifyHalRestart(inMocking); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 628 | |
| 629 | private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub { |
| 630 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 631 | |
| 632 | private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 633 | mVehicleNetwork = new WeakReference<>(vehicleNewotk); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | @Override |
| 637 | public VehiclePropConfigsParcelable onListProperties() { |
| 638 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 639 | if (vehicleNetwork == null) { |
| 640 | return null; |
| 641 | } |
| 642 | VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties(); |
| 643 | return new VehiclePropConfigsParcelable(configs); |
| 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public void onPropertySet(VehiclePropValueParcelable value) { |
| 648 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 649 | if (vehicleNetwork == null) { |
| 650 | return; |
| 651 | } |
| 652 | vehicleNetwork.getHalMock().onPropertySet(value.value); |
| 653 | } |
| 654 | |
| 655 | @Override |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 656 | public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 657 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 658 | if (vehicleNetwork == null) { |
| 659 | return null; |
| 660 | } |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 661 | VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value); |
| 662 | return new VehiclePropValueParcelable(resValue); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | @Override |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 666 | public void onPropertySubscribe(int property, float sampleRate, int zones) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 667 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 668 | if (vehicleNetwork == null) { |
| 669 | return; |
| 670 | } |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 671 | vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate, zones); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | @Override |
| 675 | public void onPropertyUnsubscribe(int property) { |
| 676 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 677 | if (vehicleNetwork == null) { |
| 678 | return; |
| 679 | } |
| 680 | vehicleNetwork.getHalMock().onPropertyUnsubscribe(property); |
| 681 | } |
| 682 | } |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame^] | 683 | |
| 684 | private VehiclePropValue getProperty(int property, int zone, int customPropetyDataType) { |
| 685 | boolean isCustom = isCustomProperty(property); |
| 686 | int valueType = isCustom |
| 687 | ? customPropetyDataType |
| 688 | : VehicleNetworkConsts.getVehicleValueType(property); |
| 689 | |
| 690 | VehiclePropValue.Builder valuePrototypeBuilder = |
| 691 | VehiclePropValueUtil.createBuilder(property, valueType, 0); |
| 692 | |
| 693 | if (zone != NO_ZONE) { |
| 694 | valuePrototypeBuilder.setZonedValue(ZonedValue.newBuilder(). |
| 695 | setZoneOrWindow(zone). |
| 696 | build()); |
| 697 | } |
| 698 | |
| 699 | VehiclePropValue v = getProperty(valuePrototypeBuilder.build()); |
| 700 | if (v == null) { |
| 701 | // if property is invalid, IllegalArgumentException should have been thrown |
| 702 | // from getProperty. |
| 703 | throw new IllegalStateException(); |
| 704 | } |
| 705 | |
| 706 | if (!isCustom && v.getValueType() != valueType) { |
| 707 | throw new IllegalArgumentException("Unexpected type: " + v.getValueType() |
| 708 | + ", expecting: " + valueType); |
| 709 | } |
| 710 | return v; |
| 711 | } |
| 712 | |
| 713 | private void assertVectorLength(int actual, int property, int valueType) { |
| 714 | int expectedLen = getVectorLength(valueType); |
| 715 | if (expectedLen != actual) { |
| 716 | throw new IllegalStateException("Invalid array size for property: " + property |
| 717 | + ". Expected: " + expectedLen |
| 718 | + ", actual: " + actual); |
| 719 | } |
| 720 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 721 | } |