blob: de91318d62c2466e23d5c3c90982620b7bed888c [file] [log] [blame]
keunyoungca515072015-07-10 12:21:47 -07001/*
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
17package com.android.car;
18
19import android.content.Context;
20import android.os.SystemClock;
21import android.support.car.Car;
22import android.support.car.CarSensorEvent;
23import android.support.car.CarSensorManager;
24import android.support.car.ICarSensorEventListener;
25import android.util.Log;
26
27import com.android.car.hal.SensorHalBase.SensorListener;
28
29import java.io.PrintWriter;
30
31/**
32 * Logical sensor implementing driving state policy. This policy sets only two states:
33 * no restriction vs fully restrictive. To enter no restriction state, speed should be zero
34 * while either parking brake is applied or transmission gear is in P.
35 */
36public class DrivingStatePolicy extends CarSensorService.LogicalSensorHalBase {
37
38 private final Context mContext;
39 private CarSensorService mSensorService;
40 private int mDringState = CarSensorEvent.DRIVE_STATUS_FULLY_RESTRICTED;
41 private SensorListener mSensorListener;
42 private boolean mIsReady = false;
43 private boolean mStarted = false;
44
45 private static final int[] SUPPORTED_SENSORS = { CarSensorManager.SENSOR_TYPE_DRIVING_STATUS };
46
47 private final ICarSensorEventListener mICarSensorEventListener =
48 new ICarSensorEventListener.Stub() {
49 @Override
50 public void onSensorChanged(CarSensorEvent event) {
51 handleSensorEvent(event);
52 }
53 };
54
55 public DrivingStatePolicy(Context context) {
56 mContext = context;
57 }
58
59 @Override
60 public void init() {
61 mIsReady = true;
62 }
63
64 @Override
65 public synchronized void onSensorServiceReady() {
66 mSensorService =
67 (CarSensorService) ICarImpl.getInstance(mContext).getCarService(Car.SENSOR_SERVICE);
68 int sensorList[] = mSensorService.getSupportedSensors();
69 boolean hasSpeed = subscribeIfSupportedLocked(sensorList,
70 CarSensorManager.SENSOR_TYPE_CAR_SPEED, CarSensorManager.SENSOR_RATE_FASTEST);
71 if (!hasSpeed) {
72 Log.w(CarLog.TAG_SENSOR,
73 "No speed sensor from car. Driving state will be always fully restrictive");
74 }
75 boolean hasParkingBrake = subscribeIfSupportedLocked(sensorList,
76 CarSensorManager.SENSOR_TYPE_PARKING_BRAKE, CarSensorManager.SENSOR_RATE_FASTEST);
77 boolean hasGear = subscribeIfSupportedLocked(sensorList, CarSensorManager.SENSOR_TYPE_GEAR,
78 CarSensorManager.SENSOR_RATE_FASTEST);
79 if (!hasParkingBrake && !hasGear) {
80 Log.w(CarLog.TAG_SENSOR,
81 "No brake info from car. Driving state will be always fully restrictive");
82 }
83 }
84
85 @Override
86 public void release() {
87 // TODO Auto-generated method stub
88 }
89
90 @Override
91 public synchronized CarSensorEvent getDefaultValue(int sensorType) {
92 if (sensorType != CarSensorManager.SENSOR_TYPE_DRIVING_STATUS) {
93 Log.w(CarLog.TAG_SENSOR, "getCurrentValue to DrivingStatePolicy with sensorType:" +
94 sensorType);
95 return null;
96 }
97 return createEvent(mDringState);
98 }
99
100 @Override
101 public synchronized void registerSensorListener(SensorListener listener) {
102 mSensorListener = listener;
103 if (mIsReady) {
104 mSensorListener.onSensorHalReady(this);
105 }
106 }
107
108 @Override
109 public synchronized boolean isReady() {
110 return mIsReady;
111 }
112
113 @Override
114 public int[] getSupportedSensors() {
115 return SUPPORTED_SENSORS;
116 }
117
118 @Override
119 public synchronized boolean requestSensorStart(int sensorType, int rate) {
120 mStarted = true;
121 mSensorListener.onSensorData(createEvent(mDringState));
122 return true;
123 }
124
125 @Override
126 public synchronized void requestSensorStop(int sensorType) {
127 mStarted = false;
128 }
129
130 @Override
131 public void dump(PrintWriter writer) {
132 // TODO Auto-generated method stub
133 }
134
135 private boolean subscribeIfSupportedLocked(int sensorList[], int sensorType, int rate) {
136 if (!CarSensorManager.isSensorSupported(sensorList, sensorType)) {
137 Log.i(CarLog.TAG_SENSOR, "Sensor not supported:" + sensorType);
138 return false;
139 }
140 return mSensorService.registerOrUpdateSensorListener(sensorType, rate,
141 CarSensorService.VERSION, mICarSensorEventListener);
142 }
143
144 private synchronized void handleSensorEvent(CarSensorEvent event) {
145 switch (event.sensorType) {
146 case CarSensorManager.SENSOR_TYPE_PARKING_BRAKE:
147 case CarSensorManager.SENSOR_TYPE_GEAR:
148 case CarSensorManager.SENSOR_TYPE_CAR_SPEED:
149 int drivingState = recalcDrivingStateLocked();
150 if (drivingState != mDringState && mSensorListener != null) {
151 mDringState = drivingState;
152 mSensorListener.onSensorData(createEvent(mDringState));
153 }
154 break;
155 default:
156 break;
157 }
158 }
159
160 private int recalcDrivingStateLocked() {
161 int drivingState = CarSensorEvent.DRIVE_STATUS_FULLY_RESTRICTED;
162 CarSensorEvent lastParkingBrake = mSensorService.getLatestSensorEvent(
163 CarSensorManager.SENSOR_TYPE_PARKING_BRAKE);
164 CarSensorEvent lastGear = mSensorService.getLatestSensorEvent(
165 CarSensorManager.SENSOR_TYPE_GEAR);
166 CarSensorEvent lastSpeed = mSensorService.getLatestSensorEvent(
167 CarSensorManager.SENSOR_TYPE_CAR_SPEED);
168 if (lastSpeed != null && lastSpeed.floatValues[0] == 0f) { // stopped
169 if (lastParkingBrake == null && isParkingBrakeApplied(lastParkingBrake)) {
170 if (lastGear != null && isGearInParkingOrNeutral(lastGear)) {
171 drivingState = CarSensorEvent.DRIVE_STATUS_UNRESTRICTED;
172 }
173 } else { // parking break not applied or not available
174 if (lastGear != null && isGearInParking(lastGear)) { // gear in P
175 drivingState = CarSensorEvent.DRIVE_STATUS_UNRESTRICTED;
176 }
177 }
178 } // else moving, full restriction
179 return drivingState;
180 }
181
182 private boolean isSpeedZero(CarSensorEvent event) {
183 return event.floatValues[0] == 0f;
184 }
185
186 private boolean isParkingBrakeApplied(CarSensorEvent event) {
187 return event.byteValues[0] == 1;
188 }
189
190 private boolean isGearInParkingOrNeutral(CarSensorEvent event) {
191 byte gear = event.byteValues[0];
192 return (gear == ((byte) CarSensorEvent.GEAR_NEUTRAL & 0xff)) ||
193 (gear == ((byte) CarSensorEvent.GEAR_PARK & 0xff));
194 }
195
196 private boolean isGearInParking(CarSensorEvent event) {
197 byte gear = event.byteValues[0];
198 return gear == ((byte) CarSensorEvent.GEAR_PARK & 0xff);
199 }
200
201 private CarSensorEvent createEvent(int drivingState) {
202 CarSensorEvent event = new CarSensorEvent(CarSensorManager.SENSOR_TYPE_DRIVING_STATUS,
203 SystemClock.elapsedRealtimeNanos(), 0, 1);
204 event.byteValues[0] = (byte) (drivingState & 0xff);
205 return event;
206 }
207}