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