keunyoung | cc449f7 | 2015-08-12 10:46:27 -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 | |
| 17 | package com.android.car.hal; |
| 18 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 19 | import static android.hardware.vehicle.V2_0.VehicleProperty.DRIVING_STATUS; |
| 20 | import static android.hardware.vehicle.V2_0.VehicleProperty.FUEL_LEVEL_LOW; |
| 21 | import static android.hardware.vehicle.V2_0.VehicleProperty.GEAR_SELECTION; |
| 22 | import static android.hardware.vehicle.V2_0.VehicleProperty.NIGHT_MODE; |
| 23 | import static android.hardware.vehicle.V2_0.VehicleProperty.PARKING_BRAKE_ON; |
| 24 | import static android.hardware.vehicle.V2_0.VehicleProperty.PERF_VEHICLE_SPEED; |
| 25 | import static java.lang.Integer.toHexString; |
| 26 | |
| 27 | import android.annotation.Nullable; |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 28 | import android.car.hardware.CarSensorEvent; |
| 29 | import android.car.hardware.CarSensorManager; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 30 | import android.hardware.vehicle.V2_0.VehiclePropConfig; |
| 31 | import android.hardware.vehicle.V2_0.VehiclePropValue; |
| 32 | import android.hardware.vehicle.V2_0.VehicleProperty; |
| 33 | import android.hardware.vehicle.V2_0.VehiclePropertyAccess; |
| 34 | import android.hardware.vehicle.V2_0.VehiclePropertyChangeMode; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.util.SparseArray; |
| 37 | |
| 38 | import com.android.car.CarLog; |
| 39 | import com.android.car.CarSensorEventFactory; |
| 40 | |
| 41 | import java.io.PrintWriter; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 42 | import java.util.Collection; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 43 | import java.util.LinkedList; |
| 44 | import java.util.List; |
| 45 | |
| 46 | /** |
| 47 | * Sensor HAL implementation for physical sensors in car. |
| 48 | */ |
| 49 | public class SensorHalService extends SensorHalServiceBase { |
| 50 | |
keunyoung | 1ab8e18 | 2015-09-24 09:25:22 -0700 | [diff] [blame] | 51 | private static final boolean DBG_EVENTS = false; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 52 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 53 | private static final int SENSOR_TYPE_INVALID = -1; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 54 | |
| 55 | private final VehicleHal mHal; |
| 56 | private boolean mIsReady = false; |
| 57 | private SensorHalServiceBase.SensorListener mSensorListener; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 58 | private final SparseArray<VehiclePropConfig> mSensorToHalProperty = new SparseArray<>(); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 59 | |
| 60 | public SensorHalService(VehicleHal hal) { |
| 61 | mHal = hal; |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public synchronized void init() { |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 66 | mIsReady = true; |
| 67 | } |
| 68 | |
| 69 | @Override |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 70 | public synchronized Collection<VehiclePropConfig> takeSupportedProperties( |
| 71 | Collection<VehiclePropConfig> allProperties) { |
| 72 | LinkedList<VehiclePropConfig> supportedProperties = new LinkedList<>(); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 73 | for (VehiclePropConfig halProperty : allProperties) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 74 | int sensor = getSensorTypeFromHalProperty(halProperty.prop); |
| 75 | if (sensor != SENSOR_TYPE_INVALID |
| 76 | && halProperty.changeMode != VehiclePropertyChangeMode.STATIC |
| 77 | && ((halProperty.access & VehiclePropertyAccess.READ) != 0)) { |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 78 | supportedProperties.add(halProperty); |
| 79 | mSensorToHalProperty.append(sensor, halProperty); |
| 80 | } |
| 81 | } |
| 82 | return supportedProperties; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public synchronized void release() { |
| 87 | mSensorToHalProperty.clear(); |
| 88 | mIsReady = false; |
| 89 | } |
| 90 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 91 | // Should be used only inside handleHalEvents method. |
| 92 | private final LinkedList<CarSensorEvent> mEventsToDispatch = new LinkedList<>(); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 93 | @Override |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 94 | public void handleHalEvents(List<VehiclePropValue> values) { |
| 95 | for (VehiclePropValue v : values) { |
| 96 | CarSensorEvent event = createCarSensorEvent(v); |
| 97 | if (event != null) { |
| 98 | mEventsToDispatch.add(event); |
| 99 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 100 | } |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 101 | SensorHalServiceBase.SensorListener sensorListener = null; |
| 102 | synchronized (this) { |
| 103 | sensorListener = mSensorListener; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 104 | } |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 105 | if (sensorListener != null) { |
| 106 | sensorListener.onSensorEvents(mEventsToDispatch); |
| 107 | } |
| 108 | mEventsToDispatch.clear(); |
| 109 | } |
| 110 | |
| 111 | private CarSensorEvent createCarSensorEvent(VehiclePropValue v) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 112 | int property = v.prop; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 113 | int sensorType = getSensorTypeFromHalProperty(property); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 114 | if (sensorType == SENSOR_TYPE_INVALID) { |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 115 | throw new RuntimeException("handleBooleanHalEvent no sensor defined for property " + |
| 116 | property); |
| 117 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 118 | switch (property) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 119 | // boolean |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 120 | case VehicleProperty.NIGHT_MODE: |
| 121 | case VehicleProperty.PARKING_BRAKE_ON: |
| 122 | case VehicleProperty.FUEL_LEVEL_LOW: { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 123 | if (DBG_EVENTS) { |
| 124 | Log.i(CarLog.TAG_SENSOR, "boolean event, property:" + |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 125 | toHexString(property) + " value:" + v.value.int32Values.get(0)); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 126 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 127 | return CarSensorEventFactory.createBooleanEvent(sensorType, v.timestamp, |
| 128 | v.value.int32Values.get(0) == 1); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 129 | } |
| 130 | // int |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 131 | case VehicleProperty.GEAR_SELECTION: |
| 132 | case VehicleProperty.DRIVING_STATUS: { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 133 | if (DBG_EVENTS) { |
| 134 | Log.i(CarLog.TAG_SENSOR, "int event, property:" + |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 135 | toHexString(property) + " value:" + v.value.int32Values.get(0)); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 136 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 137 | return CarSensorEventFactory.createIntEvent(sensorType, v.timestamp, |
| 138 | v.value.int32Values.get(0)); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 139 | } |
| 140 | // float |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 141 | case VehicleProperty.PERF_VEHICLE_SPEED: { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 142 | if (DBG_EVENTS) { |
| 143 | Log.i(CarLog.TAG_SENSOR, "float event, property:" + |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 144 | toHexString(property) + " value:" + v.value.floatValues.get(0)); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 145 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 146 | return CarSensorEventFactory.createFloatEvent(sensorType, v.timestamp, |
| 147 | v.value.floatValues.get(0)); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 148 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 149 | } |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 150 | return null; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public synchronized void registerSensorListener(SensorHalServiceBase.SensorListener listener) { |
| 155 | mSensorListener = listener; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public synchronized boolean isReady() { |
| 160 | return mIsReady; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public synchronized int[] getSupportedSensors() { |
| 165 | int[] supportedSensors = new int[mSensorToHalProperty.size()]; |
| 166 | for (int i = 0; i < supportedSensors.length; i++) { |
| 167 | supportedSensors[i] = mSensorToHalProperty.keyAt(i); |
| 168 | } |
| 169 | return supportedSensors; |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public synchronized boolean requestSensorStart(int sensorType, int rate) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 174 | VehiclePropConfig config = mSensorToHalProperty.get(sensorType); |
| 175 | if (config == null) { |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 176 | return false; |
| 177 | } |
Keun-young Park | f921520 | 2016-10-10 12:34:08 -0700 | [diff] [blame] | 178 | //TODO calculate sampling rate properly, bug: 32095903 |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 179 | mHal.subscribeProperty(this, config.prop, fixSamplingRateForProperty(config, rate)); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 180 | return true; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 183 | @Nullable |
Keun-young Park | 05f4481 | 2016-02-10 15:32:48 -0800 | [diff] [blame] | 184 | public CarSensorEvent getCurrentSensorValue(int sensorType) { |
| 185 | VehiclePropConfig config; |
| 186 | synchronized (this) { |
| 187 | config = mSensorToHalProperty.get(sensorType); |
| 188 | } |
| 189 | if (config == null) { |
Keun-young Park | 9ec0547 | 2016-04-26 21:18:18 -0700 | [diff] [blame] | 190 | Log.e(CarLog.TAG_SENSOR, "sensor type not available 0x" + |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 191 | toHexString(sensorType)); |
Keun-young Park | 05f4481 | 2016-02-10 15:32:48 -0800 | [diff] [blame] | 192 | return null; |
| 193 | } |
Keun-young Park | ba48548 | 2016-03-24 13:24:31 -0700 | [diff] [blame] | 194 | try { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 195 | VehiclePropValue value = mHal.get(config.prop); |
Keun-young Park | ba48548 | 2016-03-24 13:24:31 -0700 | [diff] [blame] | 196 | return createCarSensorEvent(value); |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 197 | } catch (PropertyTimeoutException e) { |
| 198 | Log.e(CarLog.TAG_SENSOR, "property not ready 0x" + toHexString(config.prop), e); |
Keun-young Park | ba48548 | 2016-03-24 13:24:31 -0700 | [diff] [blame] | 199 | return null; |
| 200 | } |
Keun-young Park | 05f4481 | 2016-02-10 15:32:48 -0800 | [diff] [blame] | 201 | } |
| 202 | |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 203 | private float fixSamplingRateForProperty(VehiclePropConfig prop, int carSensorManagerRate) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 204 | switch (prop.changeMode) { |
| 205 | case VehiclePropertyChangeMode.ON_CHANGE: |
| 206 | case VehiclePropertyChangeMode.ON_SET: |
Keun-young Park | 85dc7bd | 2016-05-13 13:23:06 -0700 | [diff] [blame] | 207 | return 0; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 208 | } |
| 209 | float rate = 1.0f; |
| 210 | switch (carSensorManagerRate) { |
| 211 | case CarSensorManager.SENSOR_RATE_FASTEST: |
| 212 | case CarSensorManager.SENSOR_RATE_FAST: |
| 213 | rate = 10f; |
| 214 | break; |
| 215 | case CarSensorManager.SENSOR_RATE_UI: |
| 216 | rate = 5f; |
| 217 | break; |
| 218 | default: // fall back to default. |
| 219 | break; |
| 220 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 221 | if (rate > prop.maxSampleRate) { |
| 222 | rate = prop.maxSampleRate; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 223 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 224 | if (rate < prop.minSampleRate) { |
| 225 | rate = prop.minSampleRate; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 226 | } |
| 227 | return rate; |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public synchronized void requestSensorStop(int sensorType) { |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 232 | VehiclePropConfig config = mSensorToHalProperty.get(sensorType); |
| 233 | if (config == null) { |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 234 | return; |
| 235 | } |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 236 | mHal.unsubscribeProperty(this, config.prop); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Covert hal property to sensor type. This is also used to check if specific property |
| 241 | * is supported by sensor hal or not. |
| 242 | * @param halPropertyType |
| 243 | * @return |
| 244 | */ |
| 245 | static int getSensorTypeFromHalProperty(int halPropertyType) { |
| 246 | switch (halPropertyType) { |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 247 | case PERF_VEHICLE_SPEED: |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 248 | return CarSensorManager.SENSOR_TYPE_CAR_SPEED; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 249 | case GEAR_SELECTION: |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 250 | return CarSensorManager.SENSOR_TYPE_GEAR; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 251 | case NIGHT_MODE: |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 252 | return CarSensorManager.SENSOR_TYPE_NIGHT; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 253 | case PARKING_BRAKE_ON: |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 254 | return CarSensorManager.SENSOR_TYPE_PARKING_BRAKE; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 255 | case DRIVING_STATUS: |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 256 | return CarSensorManager.SENSOR_TYPE_DRIVING_STATUS; |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 257 | case FUEL_LEVEL_LOW: |
Keun-young Park | 4afc6ee | 2016-03-04 18:18:19 -0800 | [diff] [blame] | 258 | return CarSensorManager.SENSOR_TYPE_FUEL_LEVEL; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 259 | default: |
Pavel Maltsev | 0d07c76 | 2016-11-03 16:40:15 -0700 | [diff] [blame^] | 260 | return SENSOR_TYPE_INVALID; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void dump(PrintWriter writer) { |
keunyoung | a3b28d8 | 2015-08-25 13:05:15 -0700 | [diff] [blame] | 266 | writer.println("*Sensor HAL*"); |
| 267 | writer.println("**Supported properties**"); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 268 | for (int i = 0; i < mSensorToHalProperty.size(); i++) { |
| 269 | writer.println(mSensorToHalProperty.valueAt(i).toString()); |
| 270 | } |
| 271 | } |
| 272 | } |