Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.hal; |
| 17 | |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 18 | import static com.android.car.CarServiceUtils.toByteArray; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 19 | import static java.lang.Integer.toHexString; |
| 20 | |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 21 | import android.car.VehicleAreaType; |
Antonio Cortes | a6845c3 | 2017-02-06 09:22:26 -0800 | [diff] [blame] | 22 | import android.car.annotation.FutureFeature; |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 23 | import android.car.vms.IVmsSubscriberClient; |
Antonio Cortes | 0bd67c3 | 2017-03-13 08:02:40 -0700 | [diff] [blame] | 24 | import android.car.vms.VmsLayer; |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 25 | import android.car.vms.VmsSubscriptionState; |
Pavel Maltsev | cfe9310 | 2017-02-02 12:38:08 -0800 | [diff] [blame] | 26 | import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig; |
| 27 | import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; |
Antonio Cortes | 747364a | 2017-02-28 10:37:24 -0800 | [diff] [blame] | 28 | import android.hardware.automotive.vehicle.V2_1.VehicleProperty; |
| 29 | import android.hardware.automotive.vehicle.V2_1.VmsMessageIntegerValuesIndex; |
| 30 | import android.hardware.automotive.vehicle.V2_1.VmsMessageType; |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 31 | import android.os.SystemClock; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 32 | import android.util.Log; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 33 | import com.android.car.CarLog; |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 34 | import com.android.car.VmsRouting; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 35 | import com.android.internal.annotations.GuardedBy; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 36 | import java.io.PrintWriter; |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame] | 37 | import java.util.ArrayList; |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 38 | import java.util.Arrays; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 39 | import java.util.Collection; |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 40 | import java.util.HashSet; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 41 | import java.util.LinkedList; |
| 42 | import java.util.List; |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 43 | import java.util.Set; |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 44 | import java.util.concurrent.CopyOnWriteArrayList; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * This is a glue layer between the VehicleHal and the VmsService. It sends VMS properties back and |
| 48 | * forth. |
| 49 | */ |
Antonio Cortes | a6845c3 | 2017-02-06 09:22:26 -0800 | [diff] [blame] | 50 | @FutureFeature |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 51 | public class VmsHalService extends HalServiceBase { |
| 52 | private static final boolean DBG = true; |
| 53 | private static final int HAL_PROPERTY_ID = VehicleProperty.VEHICLE_MAP_SERVICE; |
| 54 | private static final String TAG = "VmsHalService"; |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 55 | private static final Set<Integer> SUPPORTED_MESSAGE_TYPES = |
| 56 | new HashSet<Integer>( |
| 57 | Arrays.asList( |
| 58 | VmsMessageType.SUBSCRIBE, |
| 59 | VmsMessageType.UNSUBSCRIBE, |
| 60 | VmsMessageType.DATA)); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 61 | |
| 62 | private boolean mIsSupported = false; |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 63 | private CopyOnWriteArrayList<VmsHalPublisherListener> mPublisherListeners = |
| 64 | new CopyOnWriteArrayList<>(); |
| 65 | private CopyOnWriteArrayList<VmsHalSubscriberListener> mSubscriberListeners = |
| 66 | new CopyOnWriteArrayList<>(); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 67 | private final VehicleHal mVehicleHal; |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 68 | @GuardedBy("mLock") |
| 69 | private VmsRouting mRouting = new VmsRouting(); |
| 70 | private final Object mLock = new Object(); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 73 | * The VmsPublisherService implements this interface to receive data from the HAL. |
| 74 | */ |
| 75 | public interface VmsHalPublisherListener { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 76 | void onChange(VmsSubscriptionState subscriptionState); |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * The VmsSubscriberService implements this interface to receive data from the HAL. |
| 81 | */ |
| 82 | public interface VmsHalSubscriberListener { |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 83 | void onChange(VmsLayer layer, byte[] payload); |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 87 | * The VmsService implements this interface to receive data from the HAL. |
| 88 | */ |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 89 | protected VmsHalService(VehicleHal vehicleHal) { |
| 90 | mVehicleHal = vehicleHal; |
| 91 | if (DBG) { |
| 92 | Log.d(TAG, "started VmsHalService!"); |
| 93 | } |
| 94 | } |
| 95 | |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 96 | public void addPublisherListener(VmsHalPublisherListener listener) { |
| 97 | mPublisherListeners.add(listener); |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame] | 98 | } |
| 99 | |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 100 | public void addSubscriberListener(VmsHalSubscriberListener listener) { |
| 101 | mSubscriberListeners.add(listener); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 102 | } |
| 103 | |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 104 | public void removePublisherListener(VmsHalPublisherListener listener) { |
| 105 | mPublisherListeners.remove(listener); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 106 | } |
| 107 | |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 108 | public void removeSubscriberListener(VmsHalSubscriberListener listener) { |
| 109 | mSubscriberListeners.remove(listener); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 112 | public void addSubscription(IVmsSubscriberClient listener, VmsLayer layer) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 113 | synchronized (mLock) { |
| 114 | // Check if publishers need to be notified about this change in subscriptions. |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 115 | boolean firstSubscriptionForLayer = !mRouting.hasLayerSubscriptions(layer); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 116 | |
| 117 | // Add the listeners subscription to the layer |
| 118 | mRouting.addSubscription(listener, layer); |
| 119 | |
| 120 | // Notify the publishers |
| 121 | if (firstSubscriptionForLayer) { |
| 122 | notifyPublishers(layer, true); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 127 | public void removeSubscription(IVmsSubscriberClient listener, VmsLayer layer) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 128 | synchronized (mLock) { |
| 129 | if (!mRouting.hasLayerSubscriptions(layer)) { |
| 130 | Log.i(TAG, "Trying to remove a layer with no subscription: " + layer); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // Remove the listeners subscription to the layer |
| 135 | mRouting.removeSubscription(listener, layer); |
| 136 | |
| 137 | // Check if publishers need to be notified about this change in subscriptions. |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 138 | boolean layerHasSubscribers = mRouting.hasLayerSubscriptions(layer); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 139 | |
| 140 | // Notify the publishers |
| 141 | if (!layerHasSubscribers) { |
| 142 | notifyPublishers(layer, false); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 147 | public void addSubscription(IVmsSubscriberClient listener) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 148 | synchronized (mLock) { |
| 149 | mRouting.addSubscription(listener); |
| 150 | } |
| 151 | } |
| 152 | |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 153 | public void removeSubscription(IVmsSubscriberClient listener) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 154 | synchronized (mLock) { |
| 155 | mRouting.removeSubscription(listener); |
| 156 | } |
| 157 | } |
| 158 | |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 159 | public void removeDeadListener(IVmsSubscriberClient listener) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 160 | synchronized (mLock) { |
| 161 | mRouting.removeDeadListener(listener); |
| 162 | } |
| 163 | } |
| 164 | |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 165 | public Set<IVmsSubscriberClient> getListeners(VmsLayer layer) { |
Antonio Cortes | 8b35009 | 2017-03-16 16:02:51 -0700 | [diff] [blame] | 166 | synchronized (mLock) { |
| 167 | return mRouting.getListeners(layer); |
| 168 | } |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | public boolean isHalSubscribed(VmsLayer layer) { |
Antonio Cortes | 8b35009 | 2017-03-16 16:02:51 -0700 | [diff] [blame] | 172 | synchronized (mLock) { |
| 173 | return mRouting.isHalSubscribed(layer); |
| 174 | } |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 177 | public VmsSubscriptionState getSubscriptionState() { |
Antonio Cortes | 8b35009 | 2017-03-16 16:02:51 -0700 | [diff] [blame] | 178 | synchronized (mLock) { |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 179 | return mRouting.getSubscriptionState(); |
Antonio Cortes | 8b35009 | 2017-03-16 16:02:51 -0700 | [diff] [blame] | 180 | } |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | public void addHalSubscription(VmsLayer layer) { |
| 184 | synchronized (mLock) { |
| 185 | // Check if publishers need to be notified about this change in subscriptions. |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 186 | boolean firstSubscriptionForLayer = !mRouting.hasLayerSubscriptions(layer); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 187 | |
| 188 | // Add the listeners subscription to the layer |
| 189 | mRouting.addHalSubscription(layer); |
| 190 | |
| 191 | if (firstSubscriptionForLayer) { |
| 192 | notifyPublishers(layer, true); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public void removeHalSubscription(VmsLayer layer) { |
| 198 | synchronized (mLock) { |
| 199 | if (!mRouting.hasLayerSubscriptions(layer)) { |
| 200 | Log.i(TAG, "Trying to remove a layer with no subscription: " + layer); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | // Remove the listeners subscription to the layer |
| 205 | mRouting.removeHalSubscription(layer); |
| 206 | |
| 207 | // Check if publishers need to be notified about this change in subscriptions. |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 208 | boolean layerHasSubscribers = mRouting.hasLayerSubscriptions(layer); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 209 | |
| 210 | // Notify the publishers |
| 211 | if (!layerHasSubscribers) { |
| 212 | notifyPublishers(layer, false); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
Asaf Rosenfeld | 2ca6f8c | 2017-03-20 12:59:33 -0700 | [diff] [blame] | 217 | public boolean containsListener(IVmsSubscriberClient listener) { |
Antonio Cortes | 8b35009 | 2017-03-16 16:02:51 -0700 | [diff] [blame] | 218 | synchronized (mLock) { |
| 219 | return mRouting.containsListener(listener); |
| 220 | } |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Notify all the publishers and the HAL on subscription changes regardless of who triggered |
| 225 | * the change. |
| 226 | * |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 227 | * @param layer layer which is being subscribed to or unsubscribed from. |
| 228 | * @param hasSubscribers indicates if the notification is for subscription or unsubscription. |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 229 | */ |
| 230 | public void notifyPublishers(VmsLayer layer, boolean hasSubscribers) { |
| 231 | synchronized (mLock) { |
| 232 | // notify the HAL |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 233 | setSubscriptionRequest(layer, hasSubscribers); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 234 | |
| 235 | // Notify the App publishers |
| 236 | for (VmsHalPublisherListener listener : mPublisherListeners) { |
Antonio Cortes | 9db963a | 2017-03-10 15:15:28 -0800 | [diff] [blame] | 237 | // Besides the list of layers, also a timestamp is provided to the clients. |
| 238 | // They should ignore any notification with a timestamp that is older than the most |
| 239 | // recent timestamp they have seen. |
Antonio Cortes | 2febe9f | 2017-03-24 09:42:17 -0700 | [diff] [blame^] | 240 | listener.onChange(getSubscriptionState()); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 245 | @Override |
| 246 | public void init() { |
| 247 | if (DBG) { |
| 248 | Log.d(TAG, "init()"); |
| 249 | } |
| 250 | if (mIsSupported) { |
| 251 | mVehicleHal.subscribeProperty(this, HAL_PROPERTY_ID, 0); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void release() { |
| 257 | if (DBG) { |
| 258 | Log.d(TAG, "release()"); |
| 259 | } |
| 260 | if (mIsSupported) { |
| 261 | mVehicleHal.unsubscribeProperty(this, HAL_PROPERTY_ID); |
| 262 | } |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 263 | mPublisherListeners.clear(); |
| 264 | mSubscriberListeners.clear(); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public Collection<VehiclePropConfig> takeSupportedProperties( |
| 269 | Collection<VehiclePropConfig> allProperties) { |
| 270 | List<VehiclePropConfig> taken = new LinkedList<>(); |
| 271 | for (VehiclePropConfig p : allProperties) { |
| 272 | if (p.prop == HAL_PROPERTY_ID) { |
| 273 | taken.add(p); |
| 274 | mIsSupported = true; |
| 275 | if (DBG) { |
| 276 | Log.d(TAG, "takeSupportedProperties: " + toHexString(p.prop)); |
| 277 | } |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | return taken; |
| 282 | } |
| 283 | |
| 284 | @Override |
| 285 | public void handleHalEvents(List<VehiclePropValue> values) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 286 | if (DBG) { |
| 287 | Log.d(TAG, "Handling a VMS property change"); |
| 288 | } |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 289 | for (VehiclePropValue v : values) { |
| 290 | ArrayList<Integer> vec = v.value.int32Values; |
| 291 | int messageType = vec.get(VmsMessageIntegerValuesIndex.VMS_MESSAGE_TYPE); |
| 292 | int layerId = vec.get(VmsMessageIntegerValuesIndex.VMS_LAYER_ID); |
| 293 | int layerVersion = vec.get(VmsMessageIntegerValuesIndex.VMS_LAYER_VERSION); |
| 294 | |
| 295 | // Check if message type is supported. |
| 296 | if (!SUPPORTED_MESSAGE_TYPES.contains(messageType)) { |
| 297 | throw new IllegalArgumentException("Unexpected message type. " + |
| 298 | "Expecting: " + SUPPORTED_MESSAGE_TYPES + |
| 299 | ". Got: " + messageType); |
| 300 | |
| 301 | } |
| 302 | |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 303 | if (DBG) { |
| 304 | Log.d(TAG, |
| 305 | "Received message for Type: " + messageType + |
| 306 | " Layer Id: " + layerId + |
| 307 | "Version: " + layerVersion); |
| 308 | } |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 309 | // This is a data message intended for subscribers. |
| 310 | if (messageType == VmsMessageType.DATA) { |
| 311 | // Get the payload. |
| 312 | byte[] payload = toByteArray(v.value.bytes); |
| 313 | |
| 314 | // Send the message. |
| 315 | for (VmsHalSubscriberListener listener : mSubscriberListeners) { |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 316 | listener.onChange(new VmsLayer(layerId, layerVersion), payload); |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 317 | } |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 318 | } else if (messageType == VmsMessageType.SUBSCRIBE) { |
Antonio Cortes | 7e91410 | 2017-03-10 09:09:07 -0800 | [diff] [blame] | 319 | addHalSubscription(new VmsLayer(layerId, layerVersion)); |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 320 | } else { |
| 321 | // messageType == VmsMessageType.UNSUBSCRIBE |
Antonio Cortes | 7e91410 | 2017-03-10 09:09:07 -0800 | [diff] [blame] | 322 | removeHalSubscription(new VmsLayer(layerId, layerVersion)); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void dump(PrintWriter writer) { |
| 329 | writer.println(TAG); |
| 330 | writer.println("VmsProperty " + (mIsSupported ? "" : "not") + " supported."); |
| 331 | } |
| 332 | |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 333 | /** |
| 334 | * Updates the VMS HAL property with the given value. |
| 335 | * |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 336 | * @param layer layer data to update the hal property. |
| 337 | * @param hasSubscribers if it is a subscribe or unsubscribe message. |
| 338 | * @return true if the call to the HAL to update the property was successful. |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 339 | */ |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 340 | public boolean setSubscriptionRequest(VmsLayer layer, boolean hasSubscribers) { |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 341 | VehiclePropValue vehiclePropertyValue = toVehiclePropValue( |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 342 | hasSubscribers ? VmsMessageType.SUBSCRIBE : VmsMessageType.UNSUBSCRIBE, layer); |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 343 | return setPropertyValue(vehiclePropertyValue); |
| 344 | } |
| 345 | |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 346 | public boolean setDataMessage(VmsLayer layer, byte[] payload) { |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 347 | VehiclePropValue vehiclePropertyValue = toVehiclePropValue(VmsMessageType.DATA, |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 348 | layer, |
| 349 | payload); |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 350 | return setPropertyValue(vehiclePropertyValue); |
| 351 | } |
| 352 | |
| 353 | public boolean setPropertyValue(VehiclePropValue vehiclePropertyValue) { |
| 354 | try { |
| 355 | mVehicleHal.set(vehiclePropertyValue); |
| 356 | return true; |
| 357 | } catch (PropertyTimeoutException e) { |
| 358 | Log.e(CarLog.TAG_PROPERTY, "set, property not ready 0x" + toHexString(HAL_PROPERTY_ID)); |
| 359 | } |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | /** Creates a {@link VehiclePropValue} */ |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 364 | private static VehiclePropValue toVehiclePropValue(int messageType, VmsLayer layer) { |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 365 | VehiclePropValue vehicleProp = new VehiclePropValue(); |
| 366 | vehicleProp.prop = HAL_PROPERTY_ID; |
| 367 | vehicleProp.areaId = VehicleAreaType.VEHICLE_AREA_TYPE_NONE; |
| 368 | VehiclePropValue.RawValue v = vehicleProp.value; |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 369 | |
| 370 | v.int32Values.add(messageType); |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 371 | v.int32Values.add(layer.getId()); |
| 372 | v.int32Values.add(layer.getVersion()); |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 373 | return vehicleProp; |
| 374 | } |
| 375 | |
Antonio Cortes | 01a6039 | 2017-03-22 13:00:02 -0700 | [diff] [blame] | 376 | /** Creates a {@link VehiclePropValue} with payload */ |
| 377 | private static VehiclePropValue toVehiclePropValue(int messageType, |
| 378 | VmsLayer layer, |
| 379 | byte[] payload) { |
| 380 | VehiclePropValue vehicleProp = toVehiclePropValue(messageType, layer); |
asafro | 99e4f08 | 2017-02-09 13:11:42 -0800 | [diff] [blame] | 381 | VehiclePropValue.RawValue v = vehicleProp.value; |
| 382 | v.bytes.ensureCapacity(payload.length); |
| 383 | for (byte b : payload) { |
| 384 | v.bytes.add(b); |
| 385 | } |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 386 | return vehicleProp; |
| 387 | } |
asafro | 25a8740 | 2017-02-28 16:08:17 -0800 | [diff] [blame] | 388 | } |