blob: ea181c9877ada8101ff5dd12089cc374a09eb77a [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
2 * Copyright (C) 2010 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#ifndef ANDROID_SENSOR_INTERFACE_H
18#define ANDROID_SENSOR_INTERFACE_H
19
Mathias Agopian801ea092017-03-06 15:05:04 -080020#include <sensor/Sensor.h>
Peng Xu755c4512016-04-07 23:15:14 -070021#include <utils/RefBase.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080022
23// ---------------------------------------------------------------------------
24
25namespace android {
26// ---------------------------------------------------------------------------
Peng Xu755c4512016-04-07 23:15:14 -070027class SensorDevice;
28class SensorFusion;
Andrew Lehmer3a602572021-03-25 15:19:56 -070029class SensorService;
Mathias Agopianf001c922010-11-11 17:58:51 -080030
Peng Xu755c4512016-04-07 23:15:14 -070031class SensorInterface : public VirtualLightRefBase {
Mathias Agopianf001c922010-11-11 17:58:51 -080032public:
Peng Xu755c4512016-04-07 23:15:14 -070033 virtual ~SensorInterface() {}
Mathias Agopianf001c922010-11-11 17:58:51 -080034
Peng Xu0cc8f802016-04-05 23:46:03 -070035 virtual bool process(sensors_event_t* outEvent, const sensors_event_t& event) = 0;
Mathias Agopianf001c922010-11-11 17:58:51 -080036
Mathias Agopianf001c922010-11-11 17:58:51 -080037 virtual status_t activate(void* ident, bool enabled) = 0;
38 virtual status_t setDelay(void* ident, int handle, int64_t ns) = 0;
Peng Xu755c4512016-04-07 23:15:14 -070039 virtual status_t batch(void* ident, int handle, int /*flags*/, int64_t samplingPeriodNs,
40 int64_t maxBatchReportLatencyNs) = 0;
41
42 virtual status_t flush(void* /*ident*/, int /*handle*/) = 0;
43
44 virtual const Sensor& getSensor() const = 0;
45 virtual bool isVirtual() const = 0;
46 virtual void autoDisable(void* /*ident*/, int /*handle*/) = 0;
Andrew Lehmer3a602572021-03-25 15:19:56 -070047
48 virtual void willDisableAllSensors() = 0;
49 virtual void didEnableAllSensors() = 0;
Peng Xu755c4512016-04-07 23:15:14 -070050};
51
52class BaseSensor : public SensorInterface {
53public:
Chih-Hung Hsieh38fc1012016-09-01 11:32:35 -070054 explicit BaseSensor(const sensor_t& sensor);
Peng Xu6a2d3a02015-12-21 12:00:23 -080055 BaseSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
Aravind Akella724d91d2013-06-27 12:04:23 -070056
57 // Not all sensors need to support batching.
Peng Xu755c4512016-04-07 23:15:14 -070058 virtual status_t batch(void* ident, int handle, int, int64_t samplingPeriodNs,
59 int64_t maxBatchReportLatencyNs) override {
Aravind Akella724d91d2013-06-27 12:04:23 -070060 if (maxBatchReportLatencyNs == 0) {
61 return setDelay(ident, handle, samplingPeriodNs);
62 }
63 return -EINVAL;
64 }
65
Peng Xu755c4512016-04-07 23:15:14 -070066 virtual status_t flush(void* /*ident*/, int /*handle*/) override {
Aravind Akella724d91d2013-06-27 12:04:23 -070067 return -EINVAL;
68 }
69
Peng Xu755c4512016-04-07 23:15:14 -070070 virtual const Sensor& getSensor() const override { return mSensor; }
71 virtual void autoDisable(void* /*ident*/, int /*handle*/) override { }
Andrew Lehmer3a602572021-03-25 15:19:56 -070072
73 virtual void willDisableAllSensors() override { }
74 virtual void didEnableAllSensors() override { }
Peng Xu755c4512016-04-07 23:15:14 -070075protected:
76 SensorDevice& mSensorDevice;
77 Sensor mSensor;
Mathias Agopianf001c922010-11-11 17:58:51 -080078};
79
80// ---------------------------------------------------------------------------
81
Peng Xu755c4512016-04-07 23:15:14 -070082class HardwareSensor : public BaseSensor {
Mathias Agopianf001c922010-11-11 17:58:51 -080083public:
Chih-Hung Hsieh38fc1012016-09-01 11:32:35 -070084 explicit HardwareSensor(const sensor_t& sensor);
Peng Xu6a2d3a02015-12-21 12:00:23 -080085 HardwareSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
Mathias Agopianf001c922010-11-11 17:58:51 -080086
87 virtual ~HardwareSensor();
88
89 virtual bool process(sensors_event_t* outEvent,
90 const sensors_event_t& event);
91
Peng Xu0cc8f802016-04-05 23:46:03 -070092 virtual status_t activate(void* ident, bool enabled) override;
Aravind Akella724d91d2013-06-27 12:04:23 -070093 virtual status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
Peng Xu0cc8f802016-04-05 23:46:03 -070094 int64_t maxBatchReportLatencyNs) override;
95 virtual status_t setDelay(void* ident, int handle, int64_t ns) override;
96 virtual status_t flush(void* ident, int handle) override;
Peng Xu0cc8f802016-04-05 23:46:03 -070097 virtual bool isVirtual() const override { return false; }
98 virtual void autoDisable(void *ident, int handle) override;
Mathias Agopianf001c922010-11-11 17:58:51 -080099};
100
Peng Xu755c4512016-04-07 23:15:14 -0700101class VirtualSensor : public BaseSensor
102{
103public:
104 VirtualSensor();
105 virtual bool isVirtual() const override { return true; }
106protected:
107 SensorFusion& mSensorFusion;
108};
109
Andrew Lehmer3a602572021-03-25 15:19:56 -0700110// ---------------------------------------------------------------------------
111
112class ProximitySensor : public HardwareSensor {
113public:
114 explicit ProximitySensor(const sensor_t& sensor, SensorService& service);
115
116 status_t activate(void* ident, bool enabled) override;
117
118 void willDisableAllSensors() override;
119 void didEnableAllSensors() override;
120private:
121 SensorService& mSensorService;
122 bool mActive;
123};
Mathias Agopianf001c922010-11-11 17:58:51 -0800124
125// ---------------------------------------------------------------------------
126}; // namespace android
127
128#endif // ANDROID_SENSOR_INTERFACE_H