blob: 246e9793170b250516b4e1707adeeac0f0c116fe [file] [log] [blame]
Antonio Cortes734010a2017-01-19 20:09:22 -08001/*
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 */
16package com.android.car.hal;
17
18import static java.lang.Integer.toHexString;
19
20import android.annotation.Nullable;
21import android.car.VehicleAreaType;
Antonio Cortesa6845c32017-02-06 09:22:26 -080022import android.car.annotation.FutureFeature;
Antonio Cortes734010a2017-01-19 20:09:22 -080023import android.car.vms.VmsProperty;
Pavel Maltsevcfe93102017-02-02 12:38:08 -080024import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
25import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
26import android.hardware.automotive.vehicle.V2_0.VehicleProperty;
Antonio Cortes734010a2017-01-19 20:09:22 -080027import android.util.Log;
28
29import com.android.car.CarLog;
30import com.android.internal.annotations.GuardedBy;
31
32import java.io.PrintWriter;
Antonio Cortes40b90262017-02-06 11:43:57 -080033import java.util.ArrayList;
Antonio Cortes734010a2017-01-19 20:09:22 -080034import java.util.Collection;
35import java.util.LinkedList;
36import 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 Cortesa6845c32017-02-06 09:22:26 -080042@FutureFeature
Antonio Cortes734010a2017-01-19 20:09:22 -080043public 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 Cortes40b90262017-02-06 11:43:57 -080049 @GuardedBy("mListenersLock")
50 private List<VmsHalListener> mListeners = new ArrayList<>();
51 private final Object mListenersLock = new Object();
Antonio Cortes734010a2017-01-19 20:09:22 -080052 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 Cortes40b90262017-02-06 11:43:57 -080068 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 Cortes734010a2017-01-19 20:09:22 -080077 }
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 Cortes40b90262017-02-06 11:43:57 -0800131 synchronized (mListenersLock) {
132 mListeners.clear();
Antonio Cortes734010a2017-01-19 20:09:22 -0800133 }
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 Cortes40b90262017-02-06 11:43:57 -0800155 List<VmsHalListener> listeners;
156 synchronized (mListenersLock) {
157 listeners = mListeners;
Antonio Cortes734010a2017-01-19 20:09:22 -0800158 }
Antonio Cortes40b90262017-02-06 11:43:57 -0800159 for (VmsHalListener listener : listeners) {
Antonio Cortes734010a2017-01-19 20:09:22 -0800160 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}