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