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