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 | /** |
| 207 | * Get property. This can be used for a property which does not require any other data. |
| 208 | * @param property |
| 209 | * @return |
| 210 | * @throws IllegalArgumentException |
| 211 | */ |
| 212 | public VehiclePropValue getProperty(int property) throws IllegalArgumentException { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 213 | int valueType = VehicleNetworkConsts.getVehicleValueType(property); |
| 214 | VehiclePropValue value = VehiclePropValueUtil.createBuilder(property, valueType, 0).build(); |
| 215 | return getProperty(value); |
| 216 | } |
| 217 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Generic get method for any type of property. Some property may require setting data portion |
| 220 | * as get may return different result depending on the data set. |
| 221 | * @param value |
| 222 | * @return |
| 223 | * @throws IllegalArgumentException |
| 224 | */ |
| 225 | public VehiclePropValue getProperty(VehiclePropValue value) throws IllegalArgumentException { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 226 | VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 227 | try { |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 228 | VehiclePropValueParcelable resParcelable = mService.getProperty(parcelable); |
| 229 | if (resParcelable != null) { |
| 230 | return resParcelable.value; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 231 | } |
| 232 | } catch (RemoteException e) { |
| 233 | handleRemoteException(e); |
| 234 | } |
| 235 | return null; |
| 236 | } |
| 237 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 238 | /** |
| 239 | * get int type property |
| 240 | * @param property |
| 241 | * @return |
| 242 | * @throws IllegalArgumentException |
| 243 | */ |
| 244 | public int getIntProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 245 | VehiclePropValue v = getProperty(property); |
| 246 | if (v == null) { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 247 | // if property is invalid, IllegalArgumentException should have been thrown |
| 248 | // from getProperty. |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 249 | throw new IllegalStateException(); |
| 250 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 251 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT32) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 252 | throw new IllegalArgumentException(); |
| 253 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 254 | if (v.getInt32ValuesCount() != 1) { |
| 255 | throw new IllegalStateException(); |
| 256 | } |
| 257 | return v.getInt32Values(0); |
| 258 | } |
| 259 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 260 | /** |
| 261 | * get int vector type property. Length of values should match vector length. |
| 262 | * @param property |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 263 | * @throws IllegalArgumentException |
| 264 | */ |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 265 | public int[] getIntVectorProperty(int property) throws IllegalArgumentException { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 266 | VehiclePropValue v = getProperty(property); |
| 267 | if (v == null) { |
| 268 | // if property is invalid, IllegalArgumentException should have been thrown |
| 269 | // from getProperty. |
| 270 | throw new IllegalStateException(); |
| 271 | } |
| 272 | switch (v.getValueType()) { |
| 273 | case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2: |
| 274 | case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC3: |
| 275 | case VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC4: |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 276 | break; |
| 277 | default: |
| 278 | throw new IllegalArgumentException(); |
| 279 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 280 | int[] values = new int[v.getValueType() - VehicleValueType.VEHICLE_VALUE_TYPE_INT32_VEC2 + |
| 281 | 2]; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 282 | if (v.getInt32ValuesCount() != values.length) { |
| 283 | throw new IllegalStateException(); |
| 284 | } |
| 285 | for (int i = 0; i < values.length; i++) { |
| 286 | values[i] = v.getInt32Values(i); |
| 287 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 288 | return values; |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 289 | } |
| 290 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 291 | /** |
| 292 | * Get float type property. |
| 293 | * @param property |
| 294 | * @return |
| 295 | * @throws IllegalArgumentException |
| 296 | */ |
| 297 | public float getFloatProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 298 | VehiclePropValue v = getProperty(property); |
| 299 | if (v == null) { |
| 300 | throw new IllegalStateException(); |
| 301 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 302 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 303 | throw new IllegalArgumentException(); |
| 304 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 305 | if (v.getFloatValuesCount() != 1) { |
| 306 | throw new IllegalStateException(); |
| 307 | } |
| 308 | return v.getFloatValues(0); |
| 309 | } |
| 310 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 311 | /** |
| 312 | * Get float vector type property. Length of values should match vector's length. |
| 313 | * @param property |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 314 | * @throws IllegalArgumentException |
| 315 | */ |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 316 | public float[] getFloatVectorProperty(int property) |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 317 | throws IllegalArgumentException { |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 318 | VehiclePropValue v = getProperty(property); |
| 319 | if (v == null) { |
| 320 | // if property is invalid, IllegalArgumentException should have been thrown |
| 321 | // from getProperty. |
| 322 | throw new IllegalStateException(); |
| 323 | } |
| 324 | switch (v.getValueType()) { |
| 325 | case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2: |
| 326 | case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC3: |
| 327 | case VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC4: |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 328 | break; |
| 329 | default: |
| 330 | throw new IllegalArgumentException(); |
| 331 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 332 | float[] values = new float[v.getValueType() - |
| 333 | VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT_VEC2 + 2]; |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 334 | if (v.getFloatValuesCount() != values.length) { |
| 335 | throw new IllegalStateException(); |
| 336 | } |
| 337 | for (int i = 0; i < values.length; i++) { |
| 338 | values[i] = v.getFloatValues(i); |
| 339 | } |
keunyoung | 4b0212c | 2015-10-29 17:11:57 -0700 | [diff] [blame] | 340 | return values; |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 341 | } |
| 342 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 343 | /** |
| 344 | * Get long type property. |
| 345 | * @param property |
| 346 | * @return |
| 347 | * @throws IllegalArgumentException |
| 348 | */ |
| 349 | public long getLongProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 350 | VehiclePropValue v = getProperty(property); |
| 351 | if (v == null) { |
| 352 | throw new IllegalStateException(); |
| 353 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 354 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_INT64) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 355 | throw new IllegalArgumentException(); |
| 356 | } |
| 357 | return v.getInt64Value(); |
| 358 | } |
| 359 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 360 | /** |
| 361 | * Get string type property. |
| 362 | * @param property |
| 363 | * @return |
| 364 | * @throws IllegalArgumentException |
| 365 | */ |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 366 | //TODO check UTF8 to java string conversion |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 367 | public String getStringProperty(int property) throws IllegalArgumentException { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 368 | VehiclePropValue v = getProperty(property); |
| 369 | if (v == null) { |
| 370 | throw new IllegalStateException(); |
| 371 | } |
keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame] | 372 | if (v.getValueType() != VehicleValueType.VEHICLE_VALUE_TYPE_STRING) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 373 | throw new IllegalArgumentException(); |
| 374 | } |
| 375 | return v.getStringValue(); |
| 376 | } |
| 377 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 378 | /** |
| 379 | * Subscribe given property with given sample rate. |
| 380 | * @param property |
| 381 | * @param sampleRate |
| 382 | * @throws IllegalArgumentException |
| 383 | */ |
| 384 | public void subscribe(int property, float sampleRate) throws IllegalArgumentException { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 385 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 386 | mService.subscribe(mVehicleNetworkListener, property, sampleRate); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 387 | } catch (RemoteException e) { |
| 388 | handleRemoteException(e); |
| 389 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 390 | } |
| 391 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 392 | /** |
| 393 | * Stop subscribing the property. |
| 394 | * @param property |
| 395 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 396 | public void unsubscribe(int property) { |
| 397 | try { |
| 398 | mService.unsubscribe(mVehicleNetworkListener, property); |
| 399 | } catch (RemoteException e) { |
| 400 | handleRemoteException(e); |
| 401 | } |
| 402 | } |
| 403 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Inject given value to all clients subscribing the property. This is for testing. |
| 406 | * @param value |
| 407 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 408 | public synchronized void injectEvent(VehiclePropValue value) { |
| 409 | try { |
| 410 | mService.injectEvent(new VehiclePropValueParcelable(value)); |
| 411 | } catch (RemoteException e) { |
| 412 | handleRemoteException(e); |
| 413 | } |
| 414 | } |
| 415 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 416 | /** |
| 417 | * Start mocking of vehicle HAL. For testing only. |
| 418 | * @param mock |
| 419 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 420 | public synchronized void startMocking(VehicleNetworkHalMock mock) { |
| 421 | mHalMock = mock; |
| 422 | mHalMockImpl = new IVehicleNetworkHalMockImpl(this); |
| 423 | try { |
| 424 | mService.startMocking(mHalMockImpl); |
| 425 | } catch (RemoteException e) { |
| 426 | handleRemoteException(e); |
| 427 | } |
| 428 | } |
| 429 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 430 | /** |
| 431 | * Stop mocking of vehicle HAL. For testing only. |
| 432 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 433 | public synchronized void stopMocking() { |
| 434 | try { |
| 435 | mService.stopMocking(mHalMockImpl); |
| 436 | } catch (RemoteException e) { |
| 437 | handleRemoteException(e); |
| 438 | } finally { |
| 439 | mHalMock = null; |
| 440 | mHalMockImpl = null; |
| 441 | } |
| 442 | } |
| 443 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 444 | /** |
| 445 | * Start mocking of vehicle HAL. For testing only. |
| 446 | * @param mock |
| 447 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 448 | public synchronized void startMocking(IVehicleNetworkHalMock mock) { |
| 449 | mHalMock = null; |
| 450 | mHalMockImpl = mock; |
| 451 | try { |
| 452 | mService.startMocking(mHalMockImpl); |
| 453 | } catch (RemoteException e) { |
| 454 | handleRemoteException(e); |
| 455 | } |
| 456 | } |
| 457 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 458 | /** |
| 459 | * Stop mocking of vehicle HAL. For testing only. |
| 460 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 461 | public synchronized void stopMocking(IVehicleNetworkHalMock mock) { |
| 462 | if (mock.asBinder() != mHalMockImpl.asBinder()) { |
| 463 | return; |
| 464 | } |
| 465 | try { |
| 466 | mService.stopMocking(mHalMockImpl); |
| 467 | } catch (RemoteException e) { |
| 468 | handleRemoteException(e); |
| 469 | } finally { |
| 470 | mHalMock = null; |
| 471 | mHalMockImpl = null; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | private synchronized VehicleNetworkHalMock getHalMock() { |
| 476 | return mHalMock; |
| 477 | } |
| 478 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 479 | private void handleRemoteException(RemoteException e) { |
| 480 | throw new RuntimeException("Vehicle network service not working ", e); |
| 481 | } |
| 482 | |
| 483 | private void handleVehicleNetworkEvents(VehiclePropValues values) { |
| 484 | mEventHandler.notifyEvents(values); |
| 485 | } |
| 486 | |
| 487 | private void doHandleVehicleNetworkEvents(VehiclePropValues values) { |
| 488 | mListener.onVehicleNetworkEvents(values); |
| 489 | } |
| 490 | |
| 491 | private class EventHandler extends Handler { |
| 492 | private static final int MSG_EVENTS = 0; |
| 493 | |
| 494 | private EventHandler(Looper looper) { |
| 495 | super(looper); |
| 496 | } |
| 497 | |
| 498 | private void notifyEvents(VehiclePropValues values) { |
| 499 | Message msg = obtainMessage(MSG_EVENTS, values); |
| 500 | sendMessage(msg); |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void handleMessage(Message msg) { |
| 505 | switch (msg.what) { |
| 506 | case MSG_EVENTS: |
| 507 | doHandleVehicleNetworkEvents((VehiclePropValues)msg.obj); |
| 508 | break; |
| 509 | default: |
| 510 | Log.w(TAG, "unown message:" + msg.what, new RuntimeException()); |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub { |
| 517 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 518 | |
| 519 | private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) { |
| 520 | mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk); |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) { |
| 525 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 526 | if (vehicleNetwork != null) { |
| 527 | vehicleNetwork.handleVehicleNetworkEvents(values.values); |
| 528 | } |
| 529 | } |
| 530 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 531 | |
| 532 | private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub { |
| 533 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 534 | |
| 535 | private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) { |
| 536 | mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk); |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | public VehiclePropConfigsParcelable onListProperties() { |
| 541 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 542 | if (vehicleNetwork == null) { |
| 543 | return null; |
| 544 | } |
| 545 | VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties(); |
| 546 | return new VehiclePropConfigsParcelable(configs); |
| 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public void onPropertySet(VehiclePropValueParcelable value) { |
| 551 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 552 | if (vehicleNetwork == null) { |
| 553 | return; |
| 554 | } |
| 555 | vehicleNetwork.getHalMock().onPropertySet(value.value); |
| 556 | } |
| 557 | |
| 558 | @Override |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 559 | public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 560 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 561 | if (vehicleNetwork == null) { |
| 562 | return null; |
| 563 | } |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 564 | VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value); |
| 565 | return new VehiclePropValueParcelable(resValue); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | @Override |
| 569 | public void onPropertySubscribe(int property, int sampleRate) { |
| 570 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 571 | if (vehicleNetwork == null) { |
| 572 | return; |
| 573 | } |
| 574 | vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate); |
| 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public void onPropertyUnsubscribe(int property) { |
| 579 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 580 | if (vehicleNetwork == null) { |
| 581 | return; |
| 582 | } |
| 583 | vehicleNetwork.getHalMock().onPropertyUnsubscribe(property); |
| 584 | } |
| 585 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 586 | } |