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