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 | |
| 18 | import static java.lang.Integer.toHexString; |
| 19 | |
| 20 | import android.annotation.Nullable; |
| 21 | import android.car.VehicleAreaType; |
Antonio Cortes | a6845c3 | 2017-02-06 09:22:26 -0800 | [diff] [blame] | 22 | import android.car.annotation.FutureFeature; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 23 | import android.car.vms.VmsProperty; |
Pavel Maltsev | cfe9310 | 2017-02-02 12:38:08 -0800 | [diff] [blame] | 24 | import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig; |
| 25 | import android.hardware.automotive.vehicle.V2_0.VehiclePropValue; |
| 26 | import android.hardware.automotive.vehicle.V2_0.VehicleProperty; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 27 | import android.util.Log; |
| 28 | |
| 29 | import com.android.car.CarLog; |
| 30 | import com.android.internal.annotations.GuardedBy; |
| 31 | |
| 32 | import java.io.PrintWriter; |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame^] | 33 | import java.util.ArrayList; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 34 | import java.util.Collection; |
| 35 | import java.util.LinkedList; |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * This is a glue layer between the VehicleHal and the VmsService. It sends VMS properties back and |
| 40 | * forth. |
| 41 | */ |
Antonio Cortes | a6845c3 | 2017-02-06 09:22:26 -0800 | [diff] [blame] | 42 | @FutureFeature |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 43 | public class VmsHalService extends HalServiceBase { |
| 44 | private static final boolean DBG = true; |
| 45 | private static final int HAL_PROPERTY_ID = VehicleProperty.VEHICLE_MAP_SERVICE; |
| 46 | private static final String TAG = "VmsHalService"; |
| 47 | |
| 48 | private boolean mIsSupported = false; |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame^] | 49 | @GuardedBy("mListenersLock") |
| 50 | private List<VmsHalListener> mListeners = new ArrayList<>(); |
| 51 | private final Object mListenersLock = new Object(); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 52 | private final VehicleHal mVehicleHal; |
| 53 | |
| 54 | /** |
| 55 | * The VmsService implements this interface to receive data from the HAL. |
| 56 | */ |
| 57 | public interface VmsHalListener { |
| 58 | void onChange(VmsProperty propVal); |
| 59 | } |
| 60 | |
| 61 | protected VmsHalService(VehicleHal vehicleHal) { |
| 62 | mVehicleHal = vehicleHal; |
| 63 | if (DBG) { |
| 64 | Log.d(TAG, "started VmsHalService!"); |
| 65 | } |
| 66 | } |
| 67 | |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame^] | 68 | public void addListener(VmsHalListener listener) { |
| 69 | synchronized (mListenersLock) { |
| 70 | mListeners.add(listener); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public void removeListener(VmsHalListener listener) { |
| 75 | synchronized (mListenersLock) { |
| 76 | mListeners.remove(listener); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns property or null if property is not ready yet. |
| 82 | */ |
| 83 | @Nullable |
| 84 | public VmsProperty getProperty() { |
| 85 | VehiclePropValue value = null; |
| 86 | try { |
| 87 | value = mVehicleHal.get(HAL_PROPERTY_ID); |
| 88 | } catch (PropertyTimeoutException e) { |
| 89 | Log.e(CarLog.TAG_PROPERTY, "get, property not ready 0x" + toHexString(HAL_PROPERTY_ID)); |
| 90 | } |
| 91 | |
| 92 | return value == null ? null : toVmsProperty(value); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Updates the VMS HAL property with the given value. |
| 97 | * |
| 98 | * @param property the value used to update the HAL property. |
| 99 | * @return true if the call to the HAL to update the property was successful. |
| 100 | */ |
| 101 | public boolean setProperty(VmsProperty property) { |
| 102 | VehiclePropValue halProp = toVehiclePropValue(property); |
| 103 | boolean success = false; |
| 104 | try { |
| 105 | mVehicleHal.set(halProp); |
| 106 | success = true; |
| 107 | } catch (PropertyTimeoutException e) { |
| 108 | Log.e(CarLog.TAG_PROPERTY, "set, property not ready 0x" + toHexString(HAL_PROPERTY_ID)); |
| 109 | } |
| 110 | return success; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void init() { |
| 115 | if (DBG) { |
| 116 | Log.d(TAG, "init()"); |
| 117 | } |
| 118 | if (mIsSupported) { |
| 119 | mVehicleHal.subscribeProperty(this, HAL_PROPERTY_ID, 0); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public void release() { |
| 125 | if (DBG) { |
| 126 | Log.d(TAG, "release()"); |
| 127 | } |
| 128 | if (mIsSupported) { |
| 129 | mVehicleHal.unsubscribeProperty(this, HAL_PROPERTY_ID); |
| 130 | } |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame^] | 131 | synchronized (mListenersLock) { |
| 132 | mListeners.clear(); |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public Collection<VehiclePropConfig> takeSupportedProperties( |
| 138 | Collection<VehiclePropConfig> allProperties) { |
| 139 | List<VehiclePropConfig> taken = new LinkedList<>(); |
| 140 | for (VehiclePropConfig p : allProperties) { |
| 141 | if (p.prop == HAL_PROPERTY_ID) { |
| 142 | taken.add(p); |
| 143 | mIsSupported = true; |
| 144 | if (DBG) { |
| 145 | Log.d(TAG, "takeSupportedProperties: " + toHexString(p.prop)); |
| 146 | } |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | return taken; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public void handleHalEvents(List<VehiclePropValue> values) { |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame^] | 155 | List<VmsHalListener> listeners; |
| 156 | synchronized (mListenersLock) { |
| 157 | listeners = mListeners; |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 158 | } |
Antonio Cortes | 40b9026 | 2017-02-06 11:43:57 -0800 | [diff] [blame^] | 159 | for (VmsHalListener listener : listeners) { |
Antonio Cortes | 734010a | 2017-01-19 20:09:22 -0800 | [diff] [blame] | 160 | for (VehiclePropValue v : values) { |
| 161 | VmsProperty propVal = toVmsProperty(v); |
| 162 | listener.onChange(propVal); |
| 163 | if (DBG) { |
| 164 | Log.d(TAG, "handleHalEvents event: " + toHexString(v.prop)); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void dump(PrintWriter writer) { |
| 172 | writer.println(TAG); |
| 173 | writer.println("VmsProperty " + (mIsSupported ? "" : "not") + " supported."); |
| 174 | } |
| 175 | |
| 176 | // TODO(antoniocortes): update the following two methods once we have the actual VMS property. |
| 177 | /** Converts {@link VehiclePropValue} to {@link VmsProperty} */ |
| 178 | static VmsProperty toVmsProperty(VehiclePropValue halValue) { |
| 179 | VehiclePropValue.RawValue v = halValue.value; |
| 180 | return new VmsProperty(v.stringValue); |
| 181 | } |
| 182 | |
| 183 | /** Converts {@link VmsProperty} to {@link VehiclePropValue} */ |
| 184 | static VehiclePropValue toVehiclePropValue(VmsProperty carProp) { |
| 185 | VehiclePropValue vehicleProp = new VehiclePropValue(); |
| 186 | vehicleProp.prop = HAL_PROPERTY_ID; |
| 187 | vehicleProp.areaId = VehicleAreaType.VEHICLE_AREA_TYPE_NONE; |
| 188 | VehiclePropValue.RawValue v = vehicleProp.value; |
| 189 | v.stringValue = carProp.getValue(); |
| 190 | return vehicleProp; |
| 191 | } |
| 192 | } |