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 19 import android.car.hardware.CarSensorEvent; 20 21 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfig; 22 import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; 23 24 import java.io.PrintWriter; 25 import java.util.LinkedList; 26 import java.util.List; 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 /** 41 * Sensor Hal is ready and is fully accessible. 42 * This will be called after {@link SensorHalServiceBase#init()}. 43 */ 44 void onSensorHalReady(SensorHalServiceBase hal); 45 /** 46 * Sensor events are available. 47 * @param events 48 */ 49 void onSensorEvents(List<CarSensorEvent> events); 50 } 51 52 private final LinkedList<CarSensorEvent> mDispatchQ = new LinkedList<CarSensorEvent>(); 53 54 public abstract void registerSensorListener(SensorListener listener); 55 56 /** 57 * Sensor HAL should be ready after init call. 58 * @return 59 */ 60 public abstract boolean isReady(); 61 62 /** 63 * This should work after {@link #init()}. 64 * @return 65 */ 66 public abstract int[] getSupportedSensors(); 67 68 public abstract boolean requestSensorStart(int sensorType, int rate); 69 70 public abstract void requestSensorStop(int sensorType); 71 72 /** 73 * Utility to help service to send one event as listener only takes list form. 74 * @param listener 75 * @param event 76 */ 77 protected void dispatchCarSensorEvent(SensorListener listener, CarSensorEvent event) { 78 synchronized (mDispatchQ) { 79 mDispatchQ.add(event); 80 listener.onSensorEvents(mDispatchQ); 81 mDispatchQ.clear(); 82 } 83 } 84 85 @Override 86 public void handleHalEvents(List<VehiclePropValue> values) { 87 // default no-op impl. Necessary to not propagate this HAL specific event to logical 88 // sensor provider. 89 throw new RuntimeException("should not be called"); 90 } 91 92 @Override 93 public List<VehiclePropConfig> takeSupportedProperties(List<VehiclePropConfig> allProperties) { 94 return null; 95 } 96 } 97