blob: ab4dd6a74f52e687739f79a230c678fd578f72ef [file] [log] [blame]
keunyoungcc449f72015-08-12 10:46:27 -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.hal;
18
Keun-young Parke54ac272016-02-16 19:02:18 -080019import android.car.hardware.CarSensorEvent;
keunyoungcc449f72015-08-12 10:46:27 -070020
keunyoungfe30ba02015-09-17 17:56:35 -070021import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfig;
22import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
keunyoungcc449f72015-08-12 10:46:27 -070023
keunyoungfe30ba02015-09-17 17:56:35 -070024import java.io.PrintWriter;
25import java.util.LinkedList;
26import java.util.List;
keunyoungcc449f72015-08-12 10:46:27 -070027
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 */
35public abstract class SensorHalServiceBase extends HalServiceBase {
36 /**
37 * Listener for monitoring sensor event. Only sensor service will implement this.
38 */
39 public interface SensorListener {
40 /**
keunyoungcc449f72015-08-12 10:46:27 -070041 * Sensor events are available.
42 * @param events
43 */
44 void onSensorEvents(List<CarSensorEvent> events);
keunyoungcc449f72015-08-12 10:46:27 -070045 }
46
keunyoungfe30ba02015-09-17 17:56:35 -070047 private final LinkedList<CarSensorEvent> mDispatchQ = new LinkedList<CarSensorEvent>();
48
keunyoungcc449f72015-08-12 10:46:27 -070049 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);
keunyoungfe30ba02015-09-17 17:56:35 -070066
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 }
keunyoungcc449f72015-08-12 10:46:27 -070091}