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 | |
| 18 | import android.annotation.Nullable; |
| 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.os.Message; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | import android.util.Log; |
| 25 | |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 26 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleValueType; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 27 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs; |
| 28 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
| 29 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues; |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 30 | import com.android.internal.annotations.GuardedBy; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 31 | |
| 32 | import java.lang.ref.WeakReference; |
| 33 | |
| 34 | /** |
| 35 | * 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] | 36 | * 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] | 37 | */ |
| 38 | public class VehicleNetwork { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Listener for VNS events. |
| 41 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 42 | public interface VehicleNetworkListener { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Notify HAL events. This requires subscribing the property |
| 45 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 46 | void onVehicleNetworkEvents(VehiclePropValues values); |
| 47 | } |
| 48 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 49 | public interface VehicleNetworkHalMock { |
| 50 | VehiclePropConfigs onListProperties(); |
| 51 | void onPropertySet(VehiclePropValue value); |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 52 | VehiclePropValue onPropertyGet(VehiclePropValue property); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 53 | void onPropertySubscribe(int property, int sampleRate); |
| 54 | void onPropertyUnsubscribe(int property); |
| 55 | } |
| 56 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 57 | private static final String TAG = VehicleNetwork.class.getSimpleName(); |
| 58 | |
| 59 | private final IVehicleNetwork mService; |
| 60 | private final VehicleNetworkListener mListener; |
| 61 | private final IVehicleNetworkListenerImpl mVehicleNetworkListener; |
| 62 | private final EventHandler mEventHandler; |
| 63 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 64 | @GuardedBy("this") |
| 65 | private VehicleNetworkHalMock mHalMock; |
| 66 | private IVehicleNetworkHalMock mHalMockImpl; |
| 67 | |
keunyoung | 217ca35 | 2015-11-17 14:45:38 -0800 | [diff] [blame] | 68 | private static final int VNS_CONNECT_MAX_RETRY = 10; |
| 69 | private static final long VNS_RETRY_WAIT_TIME_MS = 1000; |
| 70 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 71 | /** |
| 72 | * Factory method to create VehicleNetwork |
| 73 | * @param listener listener for listening events |
| 74 | * @param looper Looper to dispatch listener events |
| 75 | * @return |
| 76 | */ |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 77 | public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener, |
| 78 | Looper looper) { |
keunyoung | 217ca35 | 2015-11-17 14:45:38 -0800 | [diff] [blame] | 79 | int retryCount = 0; |
| 80 | IVehicleNetwork service = null; |
| 81 | while (service == null) { |
| 82 | service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService( |
| 83 | IVehicleNetwork.class.getCanonicalName())); |
| 84 | retryCount++; |
| 85 | if (retryCount > VNS_CONNECT_MAX_RETRY) { |
| 86 | break; |
| 87 | } |
| 88 | try { |
| 89 | Thread.sleep(VNS_RETRY_WAIT_TIME_MS); |
| 90 | } catch (InterruptedException e) { |
| 91 | //ignore |
| 92 | } |
| 93 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 94 | if (service == null) { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 95 | throw new RuntimeException("Vehicle network service not available:" + |
| 96 | IVehicleNetwork.class.getCanonicalName()); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 97 | } |
| 98 | return new VehicleNetwork(service, listener, looper); |
| 99 | } |
| 100 | |
| 101 | private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener, |
| 102 | Looper looper) { |
| 103 | mService = service; |
| 104 | mListener = listener; |
| 105 | mEventHandler = new EventHandler(looper); |
| 106 | mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this); |
| 107 | } |
| 108 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 109 | /** |
| 110 | * List all properties from vehicle HAL |
| 111 | * @return all properties |
| 112 | */ |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 113 | public VehiclePropConfigs listProperties() { |
| 114 | return listProperties(0 /* all */); |
| 115 | } |
| 116 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Return configuration information of single property |
| 119 | * @param property vehicle property number defined in {@link VehicleNetworkConsts}. 0 has |
| 120 | * has special meaning of list all properties. |
| 121 | * @return null if given property does not exist. |
| 122 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 123 | public VehiclePropConfigs listProperties(int property) { |
| 124 | try { |
| 125 | VehiclePropConfigsParcelable parcelable = mService.listProperties(property); |
| 126 | if (parcelable != null) { |
| 127 | return parcelable.configs; |
| 128 | } |
| 129 | } catch (RemoteException e) { |
| 130 | handleRemoteException(e); |
| 131 | } |
| 132 | return null; |
| 133 | } |
| 134 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Set property which will lead into writing the value to vehicle HAL. |
| 137 | * @param value |
| 138 | * @throws IllegalArgumentException If value set has wrong value like wrong valueType, |
| 139 | * wrong data, and etc. |
| 140 | */ |
| 141 | public void setProperty(VehiclePropValue value) throws IllegalArgumentException { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 142 | VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value); |
| 143 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 144 | mService.setProperty(parcelable); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 145 | } catch (RemoteException e) { |
| 146 | handleRemoteException(e); |
| 147 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 148 | } |
| 149 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 150 | /** |
| 151 | * Set integer type property |
| 152 | * @param property |
| 153 | * @param value |
| 154 | * @throws IllegalArgumentException For type mismatch (=the property is not int type) |
| 155 | */ |
| 156 | public void setIntProperty(int property, int value) throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 157 | VehiclePropValue v = VehiclePropValueUtil.createIntValue(property, value, 0); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 158 | setProperty(v); |
| 159 | } |
| 160 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 161 | /** |
| 162 | * Set int vector type property. Length of passed values should match with vector length. |
| 163 | * @param property |
| 164 | * @param values |
| 165 | * @throws IllegalArgumentException |
| 166 | */ |
| 167 | public void setIntVectorProperty(int property, int[] values) throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 168 | VehiclePropValue v = VehiclePropValueUtil.createIntVectorValue(property, values, 0); |
| 169 | setProperty(v); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 170 | } |
| 171 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 172 | /** |
| 173 | * Set long type property. |
| 174 | * @param property |
| 175 | * @param value |
| 176 | * @throws IllegalArgumentException |
| 177 | */ |
| 178 | public void setLongProperty(int property, long value) throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 179 | VehiclePropValue v = VehiclePropValueUtil.createLongValue(property, value, 0); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 180 | setProperty(v); |
| 181 | } |
| 182 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Set float type property. |
| 185 | * @param property |
| 186 | * @param value |
| 187 | * @throws IllegalArgumentException |
| 188 | */ |
| 189 | public void setFloatProperty(int property, float value) throws IllegalArgumentException { |
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. |
| 196 | * @param property |
| 197 | * @param values |
| 198 | * @throws IllegalArgumentException |
| 199 | */ |
| 200 | public void setFloatVectorProperty(int property, float[] values) |
| 201 | throws IllegalArgumentException { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 202 | VehiclePropValue v = VehiclePropValueUtil.createFloatVectorValue(property, values, 0); |
| 203 | setProperty(v); |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 204 | } |
| 205 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 206 | /** |
Steve Paik | 6648198 | 2015-10-27 15:22:38 -0700 | [diff] [blame^] | 207 | * Set zoned boolean type property |
| 208 | * @param property |
| 209 | * @param zone |
| 210 | * @param value |
| 211 | * @throws IllegalArgumentException For type mismatch (=the property is not boolean type) |
| 212 | */ |
| 213 | public void setZonedBooleanProperty(int property, int zone, boolean value) |
| 214 | throws IllegalArgumentException { |
| 215 | VehiclePropValue v = VehiclePropValueUtil.createZonedBooleanValue(property, zone, value, 0); |
| 216 | setProperty(v); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Set zoned float type property |
| 221 | * @param property |
| 222 | * @param zone |
| 223 | * @param value |
| 224 | * @throws IllegalArgumentException For type mismatch (=the property is not float type) |
| 225 | */ |
| 226 | public void setZonedFloatProperty(int property, int zone, float value) |
| 227 | throws IllegalArgumentException { |
| 228 | VehiclePropValue v = VehiclePropValueUtil.createZonedFloatValue(property, zone, value, 0); |
| 229 | setProperty(v); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Set zoned integer type property |
| 234 | * @param property |
| 235 | * @param zone |
| 236 | * @param value |
| 237 | * @throws IllegalArgumentException For type mismatch (=the property is not int type) |
| 238 | */ |
| 239 | public void setZonedIntProperty(int property, int zone, int value) |
| 240 | throws IllegalArgumentException { |
| 241 | VehiclePropValue v = VehiclePropValueUtil.createZonedIntValue(property, zone, value, 0); |
| 242 | setProperty(v); |
| 243 | } |
| 244 | |
| 245 | /** |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 246 | * Get property. This can be used for a property which does not require any other data. |
| 247 | * @param property |
| 248 | * @return |
| 249 | * @throws IllegalArgumentException |
| 250 | */ |
| 251 | public VehiclePropValue getProperty(int property) throws IllegalArgumentException { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 252 | int valueType = VehicleNetworkConsts.getVehicleValueType(property); |
| 253 | VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build(); |
| 254 | return getProperty(value); |
| 255 | } |
| 256 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 257 | /** |
| 258 | * Generic get method for any type of property. Some property may require setting data portion |
| 259 | * as get may return different result depending on the data set. |
| 260 | * @param value |
| 261 | * @return |
| 262 | * @throws IllegalArgumentException |
| 263 | */ |
| 264 | public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 265 | VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 266 | try { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 267 | VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable); |
| 268 | if (resParcelable != null) { |
| 269 | return resParcelable.value; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 270 | } |
| 271 | } catch (RemoteException e) { |
| 272 | handleRemoteException(e); |
| 273 | } |
| 274 | return null; |
| 275 | } |
| 276 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 277 | /** |
| 278 | * get int type property |
| 279 | * @param property |
| 280 | * @return |
| 281 | * @throws IllegalArgumentException |
| 282 | */ |
| 283 | public int getIntProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 284 | VehiclePropValue v = getProperty(property); |
| 285 | if (v == null) { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 286 | // if property is invalid, IllegalArgumentException should have been thrown |
| 287 | // from getProperty. |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 288 | throw new IllegalStateException(); |
| 289 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 290 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT32) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 291 | throw new IllegalArgumentException(); |
| 292 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 293 | if (v.getInt32ValuesCount() != 1) { |
| 294 | throw new IllegalStateException(); |
| 295 | } |
| 296 | return v.getInt32Values(0); |
| 297 | } |
| 298 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 299 | /** |
| 300 | * get int vector type property. Length of values should match vector length. |
| 301 | * @param property |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 302 | * @throws IllegalArgumentException |
| 303 | */ |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 304 | public int[] getIntVectorProperty(int property) throws IllegalArgumentException { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 305 | VehiclePropValue v = getProperty(property); |
| 306 | if (v == null) { |
| 307 | // if property is invalid, IllegalArgumentException should have been thrown |
| 308 | // from getProperty. |
| 309 | throw new IllegalStateException(); |
| 310 | } |
| 311 | switch (v.getValueType()) { |
| 312 | case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2: |
| 313 | case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC3: |
| 314 | case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC4: |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 315 | break; |
| 316 | default: |
| 317 | throw new IllegalArgumentException(); |
| 318 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 319 | int[] values = new int[v.getValueType() - VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2 + |
| 320 | 2]; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 321 | if (v.getInt32ValuesCount() != values.length) { |
| 322 | throw new IllegalStateException(); |
| 323 | } |
| 324 | for (int i = 0; i < values.length; i++) { |
| 325 | values[i] = v.getInt32Values(i); |
| 326 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 327 | return values; |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 328 | } |
| 329 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 330 | /** |
| 331 | * Get float type property. |
| 332 | * @param property |
| 333 | * @return |
| 334 | * @throws IllegalArgumentException |
| 335 | */ |
| 336 | public float getFloatProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 337 | VehiclePropValue v = getProperty(property); |
| 338 | if (v == null) { |
| 339 | throw new IllegalStateException(); |
| 340 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 341 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 342 | throw new IllegalArgumentException(); |
| 343 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 344 | if (v.getFloatValuesCount() != 1) { |
| 345 | throw new IllegalStateException(); |
| 346 | } |
| 347 | return v.getFloatValues(0); |
| 348 | } |
| 349 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Get float vector type property. Length of values should match vector's length. |
| 352 | * @param property |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 353 | * @throws IllegalArgumentException |
| 354 | */ |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 355 | public float[] getFloatVectorProperty(int property) |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 356 | throws IllegalArgumentException { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 357 | VehiclePropValue v = getProperty(property); |
| 358 | if (v == null) { |
| 359 | // if property is invalid, IllegalArgumentException should have been thrown |
| 360 | // from getProperty. |
| 361 | throw new IllegalStateException(); |
| 362 | } |
| 363 | switch (v.getValueType()) { |
| 364 | case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2: |
| 365 | case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC3: |
| 366 | case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC4: |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 367 | break; |
| 368 | default: |
| 369 | throw new IllegalArgumentException(); |
| 370 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 371 | float[] values = new float[v.getValueType() - |
| 372 | VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2 + 2]; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 373 | if (v.getFloatValuesCount() != values.length) { |
| 374 | throw new IllegalStateException(); |
| 375 | } |
| 376 | for (int i = 0; i < values.length; i++) { |
| 377 | values[i] = v.getFloatValues(i); |
| 378 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 379 | return values; |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 380 | } |
| 381 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Get long type property. |
| 384 | * @param property |
| 385 | * @return |
| 386 | * @throws IllegalArgumentException |
| 387 | */ |
| 388 | public long getLongProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 389 | VehiclePropValue v = getProperty(property); |
| 390 | if (v == null) { |
| 391 | throw new IllegalStateException(); |
| 392 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 393 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT64) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 394 | throw new IllegalArgumentException(); |
| 395 | } |
| 396 | return v.getInt64Value(); |
| 397 | } |
| 398 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 399 | /** |
| 400 | * Get string type property. |
| 401 | * @param property |
| 402 | * @return |
| 403 | * @throws IllegalArgumentException |
| 404 | */ |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 405 | //TODO check UTF8 to java string conversion |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 406 | public String getStringProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 407 | VehiclePropValue v = getProperty(property); |
| 408 | if (v == null) { |
| 409 | throw new IllegalStateException(); |
| 410 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 411 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_STRING) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 412 | throw new IllegalArgumentException(); |
| 413 | } |
| 414 | return v.getStringValue(); |
| 415 | } |
| 416 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 417 | /** |
| 418 | * Subscribe given property with given sample rate. |
| 419 | * @param property |
| 420 | * @param sampleRate |
| 421 | * @throws IllegalArgumentException |
| 422 | */ |
| 423 | public void subscribe(int property, float sampleRate) throws IllegalArgumentException { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 424 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 425 | mService.subscribe(mVehicleNetworkListener, property, sampleRate); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 426 | } catch (RemoteException e) { |
| 427 | handleRemoteException(e); |
| 428 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 429 | } |
| 430 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 431 | /** |
| 432 | * Stop subscribing the property. |
| 433 | * @param property |
| 434 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 435 | public void unsubscribe(int property) { |
| 436 | try { |
| 437 | mService.unsubscribe(mVehicleNetworkListener, property); |
| 438 | } catch (RemoteException e) { |
| 439 | handleRemoteException(e); |
| 440 | } |
| 441 | } |
| 442 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 443 | /** |
| 444 | * Inject given value to all clients subscribing the property. This is for testing. |
| 445 | * @param value |
| 446 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 447 | public synchronized void injectEvent(VehiclePropValue value) { |
| 448 | try { |
| 449 | mService.injectEvent(new VehiclePropValueParcelable(value)); |
| 450 | } catch (RemoteException e) { |
| 451 | handleRemoteException(e); |
| 452 | } |
| 453 | } |
| 454 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 455 | /** |
| 456 | * Start mocking of vehicle HAL. For testing only. |
| 457 | * @param mock |
| 458 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 459 | public synchronized void startMocking(VehicleNetworkHalMock mock) { |
| 460 | mHalMock = mock; |
| 461 | mHalMockImpl = new IVehicleNetworkHalMockImpl(this); |
| 462 | try { |
| 463 | mService.startMocking(mHalMockImpl); |
| 464 | } catch (RemoteException e) { |
| 465 | handleRemoteException(e); |
| 466 | } |
| 467 | } |
| 468 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 469 | /** |
| 470 | * Stop mocking of vehicle HAL. For testing only. |
| 471 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 472 | public synchronized void stopMocking() { |
| 473 | try { |
| 474 | mService.stopMocking(mHalMockImpl); |
| 475 | } catch (RemoteException e) { |
| 476 | handleRemoteException(e); |
| 477 | } finally { |
| 478 | mHalMock = null; |
| 479 | mHalMockImpl = null; |
| 480 | } |
| 481 | } |
| 482 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Start mocking of vehicle HAL. For testing only. |
| 485 | * @param mock |
| 486 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 487 | public synchronized void startMocking(IVehicleNetworkHalMock mock) { |
| 488 | mHalMock = null; |
| 489 | mHalMockImpl = mock; |
| 490 | try { |
| 491 | mService.startMocking(mHalMockImpl); |
| 492 | } catch (RemoteException e) { |
| 493 | handleRemoteException(e); |
| 494 | } |
| 495 | } |
| 496 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 497 | /** |
| 498 | * Stop mocking of vehicle HAL. For testing only. |
| 499 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 500 | public synchronized void stopMocking(IVehicleNetworkHalMock mock) { |
| 501 | if (mock.asBinder() != mHalMockImpl.asBinder()) { |
| 502 | return; |
| 503 | } |
| 504 | try { |
| 505 | mService.stopMocking(mHalMockImpl); |
| 506 | } catch (RemoteException e) { |
| 507 | handleRemoteException(e); |
| 508 | } finally { |
| 509 | mHalMock = null; |
| 510 | mHalMockImpl = null; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private synchronized VehicleNetworkHalMock getHalMock() { |
| 515 | return mHalMock; |
| 516 | } |
| 517 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 518 | private void handleRemoteException(RemoteException e) { |
| 519 | throw new RuntimeException("Vehicle network service not working ", e); |
| 520 | } |
| 521 | |
| 522 | private void handleVehicleNetworkEvents(VehiclePropValues values) { |
| 523 | mEventHandler.notifyEvents(values); |
| 524 | } |
| 525 | |
| 526 | private void doHandleVehicleNetworkEvents(VehiclePropValues values) { |
| 527 | mListener.onVehicleNetworkEvents(values); |
| 528 | } |
| 529 | |
| 530 | private class EventHandler extends Handler { |
| 531 | private static final int MSG_EVENTS = 0; |
| 532 | |
| 533 | private EventHandler(Looper looper) { |
| 534 | super(looper); |
| 535 | } |
| 536 | |
| 537 | private void notifyEvents(VehiclePropValues values) { |
| 538 | Message msg = obtainMessage(MSG_EVENTS, values); |
| 539 | sendMessage(msg); |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public void handleMessage(Message msg) { |
| 544 | switch (msg.what) { |
| 545 | case MSG_EVENTS: |
| 546 | doHandleVehicleNetworkEvents((VehiclePropValues)msg.obj); |
| 547 | break; |
| 548 | default: |
| 549 | Log.w(TAG, "unown message:" + msg.what, new RuntimeException()); |
| 550 | break; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub { |
| 556 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 557 | |
| 558 | private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) { |
| 559 | mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk); |
| 560 | } |
| 561 | |
| 562 | @Override |
| 563 | public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) { |
| 564 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 565 | if (vehicleNetwork != null) { |
| 566 | vehicleNetwork.handleVehicleNetworkEvents(values.values); |
| 567 | } |
| 568 | } |
| 569 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 570 | |
| 571 | private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub { |
| 572 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 573 | |
| 574 | private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) { |
| 575 | mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk); |
| 576 | } |
| 577 | |
| 578 | @Override |
| 579 | public VehiclePropConfigsParcelable onListProperties() { |
| 580 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 581 | if (vehicleNetwork == null) { |
| 582 | return null; |
| 583 | } |
| 584 | VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties(); |
| 585 | return new VehiclePropConfigsParcelable(configs); |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public void onPropertySet(VehiclePropValueParcelable value) { |
| 590 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 591 | if (vehicleNetwork == null) { |
| 592 | return; |
| 593 | } |
| 594 | vehicleNetwork.getHalMock().onPropertySet(value.value); |
| 595 | } |
| 596 | |
| 597 | @Override |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 598 | public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 599 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 600 | if (vehicleNetwork == null) { |
| 601 | return null; |
| 602 | } |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 603 | VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value); |
| 604 | return new VehiclePropValueParcelable(resValue); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public void onPropertySubscribe(int property, int sampleRate) { |
| 609 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 610 | if (vehicleNetwork == null) { |
| 611 | return; |
| 612 | } |
| 613 | vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate); |
| 614 | } |
| 615 | |
| 616 | @Override |
| 617 | public void onPropertyUnsubscribe(int property) { |
| 618 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 619 | if (vehicleNetwork == null) { |
| 620 | return; |
| 621 | } |
| 622 | vehicleNetwork.getHalMock().onPropertyUnsubscribe(property); |
| 623 | } |
| 624 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 625 | } |