aboutsummaryrefslogtreecommitdiff
path: root/service/src/com/android/car/hal/SensorHalService.java
blob: 2b84fc4ecfb46d300788e1402be1759a28ce2ffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.car.hal;

import static java.lang.Integer.toHexString;

import android.annotation.Nullable;
import android.car.hardware.CarSensorEvent;
import android.car.hardware.CarSensorManager;
import android.hardware.automotive.vehicle.V2_0.VehicleGear;
import android.hardware.automotive.vehicle.V2_0.VehicleIgnitionState;
import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
import android.hardware.automotive.vehicle.V2_1.VehicleProperty;
import android.hardware.automotive.vehicle.V2_0.VehiclePropertyAccess;
import android.hardware.automotive.vehicle.V2_0.VehiclePropertyChangeMode;
import android.hardware.automotive.vehicle.V2_0.VehiclePropertyType;
import android.util.Log;
import android.util.SparseIntArray;
import com.android.car.CarLog;
import com.android.car.CarSensorEventFactory;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;

/**
 * Sensor HAL implementation for physical sensors in car.
 */
public class SensorHalService extends SensorHalServiceBase {
    private static final String TAG = CarLog.concatTag(CarLog.TAG_SENSOR, SensorHalService.class);
    private static final boolean DBG_EVENTS = false;

    /**
     * Listener for monitoring sensor event. Only sensor service will implement this.
     */
    public interface SensorListener {
        /**
         * Sensor events are available.
         * @param events
         */
        void onSensorEvents(List<CarSensorEvent> events);
    }

    // Manager property Id to HAL property Id mapping.
    private final static ManagerToHalPropIdMap mManagerToHalPropIdMap =
            ManagerToHalPropIdMap.create(
                    CarSensorManager.SENSOR_TYPE_CAR_SPEED, VehicleProperty.PERF_VEHICLE_SPEED,
                    CarSensorManager.SENSOR_TYPE_RPM, VehicleProperty.ENGINE_RPM,
                    CarSensorManager.SENSOR_TYPE_ODOMETER, VehicleProperty.PERF_ODOMETER,
                    CarSensorManager.SENSOR_TYPE_GEAR, VehicleProperty.GEAR_SELECTION,
                    CarSensorManager.SENSOR_TYPE_NIGHT, VehicleProperty.NIGHT_MODE,
                    CarSensorManager.SENSOR_TYPE_PARKING_BRAKE, VehicleProperty.PARKING_BRAKE_ON,
                    CarSensorManager.SENSOR_TYPE_DRIVING_STATUS, VehicleProperty.DRIVING_STATUS,
                    CarSensorManager.SENSOR_TYPE_FUEL_LEVEL, VehicleProperty.FUEL_LEVEL_LOW,
                    CarSensorManager.SENSOR_TYPE_IGNITION_STATE, VehicleProperty.IGNITION_STATE,
                    CarSensorManager.SENSOR_TYPE_WHEEL_TICK_DISTANCE, VehicleProperty.WHEEL_TICK,
                    CarSensorManager.SENSOR_TYPE_ABS_ACTIVE, VehicleProperty.ABS_ACTIVE,
                    CarSensorManager.SENSOR_TYPE_TRACTION_CONTROL_ACTIVE,
                        VehicleProperty.TRACTION_CONTROL_ACTIVE
                );

    private final static SparseIntArray mMgrGearToHalMap = initSparseIntArray(
            VehicleGear.GEAR_NEUTRAL, CarSensorEvent.GEAR_NEUTRAL,
            VehicleGear.GEAR_REVERSE, CarSensorEvent.GEAR_REVERSE,
            VehicleGear.GEAR_PARK, CarSensorEvent.GEAR_PARK,
            VehicleGear.GEAR_DRIVE, CarSensorEvent.GEAR_DRIVE,
            VehicleGear.GEAR_LOW, CarSensorEvent.GEAR_FIRST, // Also GEAR_1 - the value is the same.
            VehicleGear.GEAR_2, CarSensorEvent.GEAR_SECOND,
            VehicleGear.GEAR_3, CarSensorEvent.GEAR_THIRD,
            VehicleGear.GEAR_4, CarSensorEvent.GEAR_FOURTH,
            VehicleGear.GEAR_5, CarSensorEvent.GEAR_FIFTH,
            VehicleGear.GEAR_6, CarSensorEvent.GEAR_SIXTH,
            VehicleGear.GEAR_7, CarSensorEvent.GEAR_SEVENTH,
            VehicleGear.GEAR_8, CarSensorEvent.GEAR_EIGHTH,
            VehicleGear.GEAR_9, CarSensorEvent.GEAR_NINTH);

    private final static SparseIntArray mMgrIgnitionStateToHalMap = initSparseIntArray(
            VehicleIgnitionState.UNDEFINED, CarSensorEvent.IGNITION_STATE_UNDEFINED,
            VehicleIgnitionState.LOCK, CarSensorEvent.IGNITION_STATE_LOCK,
            VehicleIgnitionState.OFF, CarSensorEvent.IGNITION_STATE_OFF,
            VehicleIgnitionState.ACC, CarSensorEvent.IGNITION_STATE_ACC,
            VehicleIgnitionState.ON, CarSensorEvent.IGNITION_STATE_ON,
            VehicleIgnitionState.START, CarSensorEvent.IGNITION_STATE_START);

    private SensorListener mSensorListener;

    private int[] mMicrometersPerWheelTick = {0, 0, 0, 0};

    @Override
    public void init() {
        // Populate internal values if available
        VehiclePropConfig config = mSensorToPropConfig.get(
            CarSensorManager.SENSOR_TYPE_WHEEL_TICK_DISTANCE);
        if (config == null) {
            Log.e(TAG, "init:  unable to get property config for SENSOR_TYPE_WHEEL_TICK_DISTANCE");
        } else {
            for (int i=0; i<4; i++) {
                // ConfigArray starts with Wheels enum at idx 0
                mMicrometersPerWheelTick[i] = config.configArray.get(i+1);
            }
        }
        super.init();
    }

    public SensorHalService(VehicleHal hal) {
        super(hal);
    }

    public synchronized void registerSensorListener(SensorListener listener) {
        mSensorListener = listener;
    }

    @Override
    protected int getTokenForProperty(VehiclePropConfig halProperty) {
        int sensor = mManagerToHalPropIdMap.getManagerPropId(halProperty.prop);
        if (sensor != SENSOR_TYPE_INVALID
            && halProperty.changeMode != VehiclePropertyChangeMode.STATIC
            && ((halProperty.access & VehiclePropertyAccess.READ) != 0)) {
            return sensor;
        }
        return SENSOR_TYPE_INVALID;
    }

    // Should be used only inside handleHalEvents method.
    private final LinkedList<CarSensorEvent> mEventsToDispatch = new LinkedList<>();
    @Override
    public void handleHalEvents(List<VehiclePropValue> values) {
        for (VehiclePropValue v : values) {
            CarSensorEvent event = createCarSensorEvent(v);
            if (event != null) {
                mEventsToDispatch.add(event);
            }
        }
        SensorListener sensorListener;
        synchronized (this) {
            sensorListener = mSensorListener;
        }
        if (DBG_EVENTS) Log.d(TAG, "handleHalEvents, listener: " + sensorListener);
        if (sensorListener != null) {
            sensorListener.onSensorEvents(mEventsToDispatch);
        }
        mEventsToDispatch.clear();
    }

    @Nullable
    private Integer mapHalEnumValueToMgr(int propId, int halValue) {
        int mgrValue = halValue;

        switch (propId) {
            case VehicleProperty.GEAR_SELECTION:
                mgrValue = mMgrGearToHalMap.get(halValue, -1);
                break;
            case VehicleProperty.IGNITION_STATE:
                mgrValue =  mMgrIgnitionStateToHalMap.get(halValue, -1);
            default:
                break; // Do nothing
        }
        return mgrValue == -1 ? null : mgrValue;
    }

    @Nullable
    private CarSensorEvent createCarSensorEvent(VehiclePropValue v) {
        int property = v.prop;
        int sensorType = mManagerToHalPropIdMap.getManagerPropId(property);
        if (sensorType == SENSOR_TYPE_INVALID) {
            throw new RuntimeException("no sensor defined for property 0x" + toHexString(property));
        }
        // Handle the valid sensor
        int dataType = property & VehiclePropertyType.MASK;
        CarSensorEvent event = null;
        switch (dataType) {
            case VehiclePropertyType.BOOLEAN:
                event = CarSensorEventFactory.createBooleanEvent(sensorType, v.timestamp,
                    v.value.int32Values.get(0) == 1);
                break;
            case VehiclePropertyType.COMPLEX:
                event = CarSensorEventFactory.createComplexEvent(sensorType, v.timestamp, v);
                break;
            case VehiclePropertyType.INT32:
                Integer mgrVal = mapHalEnumValueToMgr(property, v.value.int32Values.get(0));
                event =  mgrVal == null ? null
                    : CarSensorEventFactory.createIntEvent(sensorType, v.timestamp, mgrVal);
                break;
            case VehiclePropertyType.FLOAT:
                event = CarSensorEventFactory.createFloatEvent(sensorType, v.timestamp,
                    v.value.floatValues.get(0));
                break;
            default:
                Log.w(TAG, "createCarSensorEvent: unsupported type: 0x" + toHexString(dataType));
                break;
        }
        // Perform property specific actions
        switch (property) {
            case VehicleProperty.WHEEL_TICK:
                // Apply the um/tick scaling factor, then divide by 1000 to generate mm
                for (int i = 0; i < 4; i++) {
                    // ResetCounts is at longValues[0]
                    if (event.longValues[i + CarSensorEvent.INDEX_WHEEL_DISTANCE_FRONT_LEFT] !=
                        Long.MAX_VALUE) {
                        event.longValues[i + CarSensorEvent.INDEX_WHEEL_DISTANCE_FRONT_LEFT] *=
                            mMicrometersPerWheelTick[i];
                        event.longValues[i + CarSensorEvent.INDEX_WHEEL_DISTANCE_FRONT_LEFT] /=
                            1000;
                    }
                }
                break;
        }
        if (DBG_EVENTS) Log.i(TAG, "Sensor event created: " + event);
        return event;
    }

    @Nullable
    public CarSensorEvent getCurrentSensorValue(int sensorType) {
        VehiclePropValue propValue = getCurrentSensorVehiclePropValue(sensorType);
        return (null != propValue) ? createCarSensorEvent(propValue) : null;
    }

    @Override
    protected float fixSamplingRateForProperty(VehiclePropConfig prop, int carSensorManagerRate) {
        switch (prop.changeMode) {
            case VehiclePropertyChangeMode.ON_CHANGE:
            case VehiclePropertyChangeMode.ON_SET:
                return 0;
        }
        float rate = 1.0f;
        switch (carSensorManagerRate) {
            case CarSensorManager.SENSOR_RATE_FASTEST:
                rate = prop.maxSampleRate;
                break;
            case CarSensorManager.SENSOR_RATE_FAST:
                rate = 10f;  // every 100ms
                break;
            case CarSensorManager.SENSOR_RATE_UI:
                rate = 5f;   // every 200ms
                break;
            default: // fall back to default.
                break;
        }
        if (rate > prop.maxSampleRate) {
            rate = prop.maxSampleRate;
        }
        if (rate < prop.minSampleRate) {
            rate = prop.minSampleRate;
        }
        return rate;
    }

    @Override
    public void dump(PrintWriter writer) {
        writer.println("*Sensor HAL*");
        writer.println("**Supported properties**");
        for (int i = 0; i < mSensorToPropConfig.size(); i++) {
            writer.println(mSensorToPropConfig.valueAt(i).toString());
        }
        for (int i = 0; i < mMicrometersPerWheelTick.length; i++) {
            writer.println("mMicrometersPerWheelTick[" + i + "] = " + mMicrometersPerWheelTick[i]);
        }
    }

    private static SparseIntArray initSparseIntArray(int... keyValuePairs) {
        int inputLength = keyValuePairs.length;
        if (inputLength % 2 != 0) {
            throw new IllegalArgumentException("Odd number of key-value elements");
        }

        SparseIntArray map = new SparseIntArray(inputLength / 2);
        for (int i = 0; i < keyValuePairs.length; i += 2) {
            map.put(keyValuePairs[i], keyValuePairs[i + 1]);
        }
        return map;
    }
}