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; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 20 | |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 21 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfig; |
| 22 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 23 | |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 24 | import java.io.PrintWriter; |
| 25 | import java.util.LinkedList; |
| 26 | import java.util.List; |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Common base for all SensorHal implementation. |
| 30 | * It is wholly based on subscription and there is no explicit API for polling, but each sensor |
| 31 | * should report its initial state immediately after {@link #requestSensorStart(int, int)} call. |
| 32 | * It is ok to report sensor data {@link SensorListener#onSensorData(CarSensorEvent)} inside |
| 33 | * the {@link #requestSensorStart(int, int)} call. |
| 34 | */ |
| 35 | public abstract class SensorHalServiceBase extends HalServiceBase { |
| 36 | /** |
| 37 | * Listener for monitoring sensor event. Only sensor service will implement this. |
| 38 | */ |
| 39 | public interface SensorListener { |
| 40 | /** |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 41 | * Sensor events are available. |
| 42 | * @param events |
| 43 | */ |
| 44 | void onSensorEvents(List<CarSensorEvent> events); |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 45 | } |
| 46 | |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 47 | private final LinkedList<CarSensorEvent> mDispatchQ = new LinkedList<CarSensorEvent>(); |
| 48 | |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 49 | public abstract void registerSensorListener(SensorListener listener); |
| 50 | |
| 51 | /** |
| 52 | * Sensor HAL should be ready after init call. |
| 53 | * @return |
| 54 | */ |
| 55 | public abstract boolean isReady(); |
| 56 | |
| 57 | /** |
| 58 | * This should work after {@link #init()}. |
| 59 | * @return |
| 60 | */ |
| 61 | public abstract int[] getSupportedSensors(); |
| 62 | |
| 63 | public abstract boolean requestSensorStart(int sensorType, int rate); |
| 64 | |
| 65 | public abstract void requestSensorStop(int sensorType); |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Utility to help service to send one event as listener only takes list form. |
| 69 | * @param listener |
| 70 | * @param event |
| 71 | */ |
| 72 | protected void dispatchCarSensorEvent(SensorListener listener, CarSensorEvent event) { |
| 73 | synchronized (mDispatchQ) { |
| 74 | mDispatchQ.add(event); |
| 75 | listener.onSensorEvents(mDispatchQ); |
| 76 | mDispatchQ.clear(); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void handleHalEvents(List<VehiclePropValue> values) { |
| 82 | // default no-op impl. Necessary to not propagate this HAL specific event to logical |
| 83 | // sensor provider. |
| 84 | throw new RuntimeException("should not be called"); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public List<VehiclePropConfig> takeSupportedProperties(List<VehiclePropConfig> allProperties) { |
| 89 | return null; |
| 90 | } |
keunyoung | cc449f7 | 2015-08-12 10:46:27 -0700 | [diff] [blame] | 91 | } |