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