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