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 | */ |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 428 | //TODO check UTF8 to java string conversion |
Keun-young Park | ba48548 | 2016-03-24 13:24:31 -0700 | [diff] [blame] | 429 | public String getStringProperty(int property) throws IllegalArgumentException, |
| 430 | ServiceSpecificException { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 431 | VehiclePropValue v = getProperty( |
| 432 | property, NO_ZONE, VehicleValueType.VEHICLE_VALUE_TYPE_STRING); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 433 | return v.getStringValue(); |
| 434 | } |
| 435 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Subscribe given property with given sample rate. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 438 | */ |
| 439 | public void subscribe(int property, float sampleRate) throws IllegalArgumentException { |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 440 | subscribe(property, sampleRate, 0); |
| 441 | } |
| 442 | |
| 443 | /** |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 444 | * Subscribe given property with given sample rate and zones. |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 445 | */ |
| 446 | public void subscribe(int property, float sampleRate, int zones) |
| 447 | throws IllegalArgumentException { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 448 | try { |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 449 | mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones, |
| 450 | SubscribeFlags.DEFAULT); |
| 451 | } catch (RemoteException e) { |
| 452 | handleRemoteException(e); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Subscribe given property with given sample rate, zones and flags. |
| 458 | */ |
| 459 | @SuppressWarnings("ResourceType") |
| 460 | public void subscribe(int property, float sampleRate, int zones, @SubscribeFlags int flags) |
| 461 | throws IllegalArgumentException { |
| 462 | try { |
| 463 | mService.subscribe(mVehicleNetworkListener, property, sampleRate, zones, flags); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 464 | } catch (RemoteException e) { |
| 465 | handleRemoteException(e); |
| 466 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 467 | } |
| 468 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 469 | /** |
| 470 | * Stop subscribing the property. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 471 | */ |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 472 | public void unsubscribe(int property) { |
| 473 | try { |
| 474 | mService.unsubscribe(mVehicleNetworkListener, property); |
| 475 | } catch (RemoteException e) { |
| 476 | handleRemoteException(e); |
| 477 | } |
| 478 | } |
| 479 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 480 | /** |
| 481 | * Inject given value to all clients subscribing the property. This is for testing. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 482 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 483 | public synchronized void injectEvent(VehiclePropValue value) { |
| 484 | try { |
| 485 | mService.injectEvent(new VehiclePropValueParcelable(value)); |
| 486 | } catch (RemoteException e) { |
| 487 | handleRemoteException(e); |
| 488 | } |
| 489 | } |
| 490 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 491 | /** |
| 492 | * Start mocking of vehicle HAL. For testing only. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 493 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 494 | public synchronized void startMocking(VehicleNetworkHalMock mock) { |
| 495 | mHalMock = mock; |
| 496 | mHalMockImpl = new IVehicleNetworkHalMockImpl(this); |
| 497 | try { |
| 498 | mService.startMocking(mHalMockImpl); |
| 499 | } catch (RemoteException e) { |
| 500 | handleRemoteException(e); |
| 501 | } |
| 502 | } |
| 503 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 504 | /** |
| 505 | * Stop mocking of vehicle HAL. For testing only. |
| 506 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 507 | public synchronized void stopMocking() { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 508 | if (mHalMockImpl == null) { |
| 509 | return; |
| 510 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 511 | try { |
| 512 | mService.stopMocking(mHalMockImpl); |
| 513 | } catch (RemoteException e) { |
| 514 | handleRemoteException(e); |
| 515 | } finally { |
| 516 | mHalMock = null; |
| 517 | mHalMockImpl = null; |
| 518 | } |
| 519 | } |
| 520 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 521 | /** |
| 522 | * Start mocking of vehicle HAL. For testing only. |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 523 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 524 | public synchronized void startMocking(IVehicleNetworkHalMock mock) { |
| 525 | mHalMock = null; |
| 526 | mHalMockImpl = mock; |
| 527 | try { |
| 528 | mService.startMocking(mHalMockImpl); |
| 529 | } catch (RemoteException e) { |
| 530 | handleRemoteException(e); |
| 531 | } |
| 532 | } |
| 533 | |
keunyoung | 5c7cb26 | 2015-10-19 10:47:45 -0700 | [diff] [blame] | 534 | /** |
| 535 | * Stop mocking of vehicle HAL. For testing only. |
| 536 | */ |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 537 | public synchronized void stopMocking(IVehicleNetworkHalMock mock) { |
| 538 | if (mock.asBinder() != mHalMockImpl.asBinder()) { |
| 539 | return; |
| 540 | } |
| 541 | try { |
| 542 | mService.stopMocking(mHalMockImpl); |
| 543 | } catch (RemoteException e) { |
| 544 | handleRemoteException(e); |
| 545 | } finally { |
| 546 | mHalMock = null; |
| 547 | mHalMockImpl = null; |
| 548 | } |
| 549 | } |
| 550 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 551 | public synchronized void injectHalError(int errorCode, int property, int operation) { |
| 552 | try { |
| 553 | mService.injectHalError(errorCode, property, operation); |
| 554 | } catch (RemoteException e) { |
| 555 | handleRemoteException(e); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | public synchronized void startErrorListening() { |
| 560 | try { |
| 561 | mService.startErrorListening(mVehicleNetworkListener); |
| 562 | } catch (RemoteException e) { |
| 563 | handleRemoteException(e); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | public synchronized void stopErrorListening() { |
| 568 | try { |
| 569 | mService.stopErrorListening(mVehicleNetworkListener); |
| 570 | } catch (RemoteException e) { |
| 571 | handleRemoteException(e); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | public synchronized void startHalRestartMonitoring() { |
| 576 | try { |
| 577 | mService.startHalRestartMonitoring(mVehicleNetworkListener); |
| 578 | } catch (RemoteException e) { |
| 579 | handleRemoteException(e); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | public synchronized void stopHalRestartMonitoring() { |
| 584 | try { |
| 585 | mService.stopHalRestartMonitoring(mVehicleNetworkListener); |
| 586 | } catch (RemoteException e) { |
| 587 | handleRemoteException(e); |
| 588 | } |
| 589 | } |
| 590 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 591 | private synchronized VehicleNetworkHalMock getHalMock() { |
| 592 | return mHalMock; |
| 593 | } |
| 594 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 595 | private void handleRemoteException(RemoteException e) { |
| 596 | throw new RuntimeException("Vehicle network service not working ", e); |
| 597 | } |
| 598 | |
| 599 | private void handleVehicleNetworkEvents(VehiclePropValues values) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 600 | mListener.onVehicleNetworkEvents(values); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 603 | private void handleHalError(int errorCode, int property, int operation) { |
| 604 | mListener.onHalError(errorCode, property, operation); |
| 605 | } |
| 606 | |
| 607 | private void handleHalRestart(boolean inMocking) { |
| 608 | mListener.onHalRestart(inMocking); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 611 | private void handleOnPropertySet(VehiclePropValue value) { |
| 612 | mListener.onPropertySet(value); |
| 613 | } |
| 614 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 615 | private class EventHandler extends Handler { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 616 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 617 | private static final int MSG_EVENTS = 0; |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 618 | private static final int MSG_HAL_ERROR = 1; |
| 619 | private static final int MSG_HAL_RESTART = 2; |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 620 | private static final int MSG_ON_PROPERTY_SET = 3; |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 621 | |
| 622 | private EventHandler(Looper looper) { |
| 623 | super(looper); |
| 624 | } |
| 625 | |
| 626 | private void notifyEvents(VehiclePropValues values) { |
| 627 | Message msg = obtainMessage(MSG_EVENTS, values); |
| 628 | sendMessage(msg); |
| 629 | } |
| 630 | |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 631 | private void notifyHalError(int errorCode, int property, int operation) { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 632 | Message msg = obtainMessage(MSG_HAL_ERROR, errorCode, property, operation); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 633 | sendMessage(msg); |
| 634 | } |
| 635 | |
| 636 | private void notifyHalRestart(boolean inMocking) { |
| 637 | Message msg = obtainMessage(MSG_HAL_RESTART, inMocking ? 1 : 0, 0); |
| 638 | sendMessage(msg); |
| 639 | } |
| 640 | |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 641 | private void notifyPropertySet(VehiclePropValue value) { |
| 642 | Message msg = obtainMessage(MSG_ON_PROPERTY_SET, value); |
| 643 | sendMessage(msg); |
| 644 | } |
| 645 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 646 | @Override |
| 647 | public void handleMessage(Message msg) { |
| 648 | switch (msg.what) { |
| 649 | case MSG_EVENTS: |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 650 | handleVehicleNetworkEvents((VehiclePropValues) msg.obj); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 651 | break; |
| 652 | case MSG_HAL_ERROR: |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 653 | handleHalError(msg.arg1, msg.arg2, (Integer) msg.obj); |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 654 | break; |
| 655 | case MSG_HAL_RESTART: |
| 656 | handleHalRestart(msg.arg1 == 1); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 657 | break; |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 658 | case MSG_ON_PROPERTY_SET: |
| 659 | handleOnPropertySet((VehiclePropValue) msg.obj); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 660 | default: |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 661 | Log.w(TAG, "Unknown message:" + msg.what, new RuntimeException()); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 662 | break; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 668 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 669 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 670 | |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 671 | private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNetwork) { |
| 672 | mVehicleNetwork = new WeakReference<>(vehicleNetwork); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | @Override |
| 676 | public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) { |
| 677 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 678 | if (vehicleNetwork != null) { |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 679 | vehicleNetwork.mEventHandler.notifyEvents(values.values); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | @Override |
Pavel Maltsev | b0324b4 | 2016-09-27 21:00:41 -0700 | [diff] [blame] | 684 | public void onPropertySet(VehiclePropValueParcelable value) { |
| 685 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 686 | if (vehicleNetwork != null) { |
| 687 | vehicleNetwork.mEventHandler.notifyPropertySet(value.value); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | @Override |
Keun-young Park | 28dd470 | 2015-11-19 18:06:04 -0800 | [diff] [blame] | 692 | public void onHalError(int errorCode, int property, int operation) { |
| 693 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 694 | if (vehicleNetwork != null) { |
| 695 | vehicleNetwork.mEventHandler.notifyHalError(errorCode, property, operation); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | @Override |
| 700 | public void onHalRestart(boolean inMocking) { |
| 701 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 702 | if (vehicleNetwork != null) { |
| 703 | vehicleNetwork.mEventHandler.notifyHalRestart(inMocking); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | } |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 707 | |
| 708 | private static class IVehicleNetworkHalMockImpl extends IVehicleNetworkHalMock.Stub { |
| 709 | private final WeakReference<VehicleNetwork> mVehicleNetwork; |
| 710 | |
| 711 | private IVehicleNetworkHalMockImpl(VehicleNetwork vehicleNewotk) { |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 712 | mVehicleNetwork = new WeakReference<>(vehicleNewotk); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | @Override |
| 716 | public VehiclePropConfigsParcelable onListProperties() { |
| 717 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 718 | if (vehicleNetwork == null) { |
| 719 | return null; |
| 720 | } |
| 721 | VehiclePropConfigs configs = vehicleNetwork.getHalMock().onListProperties(); |
| 722 | return new VehiclePropConfigsParcelable(configs); |
| 723 | } |
| 724 | |
| 725 | @Override |
| 726 | public void onPropertySet(VehiclePropValueParcelable value) { |
| 727 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 728 | if (vehicleNetwork == null) { |
| 729 | return; |
| 730 | } |
| 731 | vehicleNetwork.getHalMock().onPropertySet(value.value); |
| 732 | } |
| 733 | |
| 734 | @Override |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 735 | public VehiclePropValueParcelable onPropertyGet(VehiclePropValueParcelable value) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 736 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 737 | if (vehicleNetwork == null) { |
| 738 | return null; |
| 739 | } |
keunyoung | 7d74e6d | 2015-10-14 15:43:10 -0700 | [diff] [blame] | 740 | VehiclePropValue resValue = vehicleNetwork.getHalMock().onPropertyGet(value.value); |
| 741 | return new VehiclePropValueParcelable(resValue); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | @Override |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 745 | public void onPropertySubscribe(int property, float sampleRate, int zones) { |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 746 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 747 | if (vehicleNetwork == null) { |
| 748 | return; |
| 749 | } |
Keun-young Park | 0727f95 | 2015-12-21 14:30:07 -0800 | [diff] [blame] | 750 | vehicleNetwork.getHalMock().onPropertySubscribe(property, sampleRate, zones); |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | @Override |
| 754 | public void onPropertyUnsubscribe(int property) { |
| 755 | VehicleNetwork vehicleNetwork = mVehicleNetwork.get(); |
| 756 | if (vehicleNetwork == null) { |
| 757 | return; |
| 758 | } |
| 759 | vehicleNetwork.getHalMock().onPropertyUnsubscribe(property); |
| 760 | } |
| 761 | } |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 762 | |
| 763 | private VehiclePropValue getProperty(int property, int zone, int customPropetyDataType) { |
| 764 | boolean isCustom = isCustomProperty(property); |
| 765 | int valueType = isCustom |
| 766 | ? customPropetyDataType |
| 767 | : VehicleNetworkConsts.getVehicleValueType(property); |
| 768 | |
| 769 | VehiclePropValue.Builder valuePrototypeBuilder = |
| 770 | VehiclePropValueUtil.createBuilder(property, valueType, 0); |
| 771 | |
| 772 | if (zone != NO_ZONE) { |
Keun-young Park | 71b2f5c | 2016-03-10 18:44:40 -0800 | [diff] [blame] | 773 | valuePrototypeBuilder.setZone(zone); |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | VehiclePropValue v = getProperty(valuePrototypeBuilder.build()); |
| 777 | if (v == null) { |
| 778 | // if property is invalid, IllegalArgumentException should have been thrown |
| 779 | // from getProperty. |
| 780 | throw new IllegalStateException(); |
| 781 | } |
| 782 | |
| 783 | if (!isCustom && v.getValueType() != valueType) { |
Keun-young Park | 1488ef2 | 2016-02-25 14:00:54 -0800 | [diff] [blame] | 784 | throw new IllegalArgumentException( |
| 785 | "Unexpected type for property 0x" + Integer.toHexString(property) + |
| 786 | " got:0x" + Integer.toHexString(v.getValueType()) |
| 787 | + ", expecting:0x" + Integer.toHexString(valueType)); |
Pavel Maltsev | e8f7537 | 2016-01-26 10:26:04 -0800 | [diff] [blame] | 788 | } |
| 789 | return v; |
| 790 | } |
| 791 | |
| 792 | private void assertVectorLength(int actual, int property, int valueType) { |
| 793 | int expectedLen = getVectorLength(valueType); |
| 794 | if (expectedLen != actual) { |
| 795 | throw new IllegalStateException("Invalid array size for property: " + property |
| 796 | + ". Expected: " + expectedLen |
| 797 | + ", actual: " + actual); |
| 798 | } |
| 799 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 800 | } |