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