blob: c446d61f89493eef7e48b442cdbebb4f0fc443ea [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;
47};
48
49class BaseSensor : public SensorInterface {
50public:
Chih-Hung Hsieh38fc1012016-09-01 11:32:35 -070051 explicit BaseSensor(const sensor_t& sensor);
Peng Xu6a2d3a02015-12-21 12:00:23 -080052 BaseSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
Aravind Akella724d91d2013-06-27 12:04:23 -070053
54 // Not all sensors need to support batching.
Peng Xu755c4512016-04-07 23:15:14 -070055 virtual status_t batch(void* ident, int handle, int, int64_t samplingPeriodNs,
56 int64_t maxBatchReportLatencyNs) override {
Aravind Akella724d91d2013-06-27 12:04:23 -070057 if (maxBatchReportLatencyNs == 0) {
58 return setDelay(ident, handle, samplingPeriodNs);
59 }
60 return -EINVAL;
61 }
62
Peng Xu755c4512016-04-07 23:15:14 -070063 virtual status_t flush(void* /*ident*/, int /*handle*/) override {
Aravind Akella724d91d2013-06-27 12:04:23 -070064 return -EINVAL;
65 }
66
Peng Xu755c4512016-04-07 23:15:14 -070067 virtual const Sensor& getSensor() const override { return mSensor; }
68 virtual void autoDisable(void* /*ident*/, int /*handle*/) override { }
Andrew Lehmer3a602572021-03-25 15:19:56 -070069
Peng Xu755c4512016-04-07 23:15:14 -070070protected:
71 SensorDevice& mSensorDevice;
72 Sensor mSensor;
Mathias Agopianf001c922010-11-11 17:58:51 -080073};
74
75// ---------------------------------------------------------------------------
76
Peng Xu755c4512016-04-07 23:15:14 -070077class HardwareSensor : public BaseSensor {
Mathias Agopianf001c922010-11-11 17:58:51 -080078public:
Chih-Hung Hsieh38fc1012016-09-01 11:32:35 -070079 explicit HardwareSensor(const sensor_t& sensor);
Peng Xu6a2d3a02015-12-21 12:00:23 -080080 HardwareSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
Mathias Agopianf001c922010-11-11 17:58:51 -080081
82 virtual ~HardwareSensor();
83
84 virtual bool process(sensors_event_t* outEvent,
85 const sensors_event_t& event);
86
Peng Xu0cc8f802016-04-05 23:46:03 -070087 virtual status_t activate(void* ident, bool enabled) override;
Aravind Akella724d91d2013-06-27 12:04:23 -070088 virtual status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
Peng Xu0cc8f802016-04-05 23:46:03 -070089 int64_t maxBatchReportLatencyNs) override;
90 virtual status_t setDelay(void* ident, int handle, int64_t ns) override;
91 virtual status_t flush(void* ident, int handle) override;
Peng Xu0cc8f802016-04-05 23:46:03 -070092 virtual bool isVirtual() const override { return false; }
93 virtual void autoDisable(void *ident, int handle) override;
Mathias Agopianf001c922010-11-11 17:58:51 -080094};
95
Peng Xu755c4512016-04-07 23:15:14 -070096class VirtualSensor : public BaseSensor
97{
98public:
99 VirtualSensor();
100 virtual bool isVirtual() const override { return true; }
101protected:
102 SensorFusion& mSensorFusion;
103};
104
Andrew Lehmer3a602572021-03-25 15:19:56 -0700105// ---------------------------------------------------------------------------
106
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200107class RuntimeSensor : public BaseSensor {
108public:
109 static constexpr int DEFAULT_DEVICE_ID = 0;
110
Vladimir Komsiyskifafbe052023-02-10 10:23:59 +0100111 class SensorCallback : public virtual RefBase {
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200112 public:
Vladimir Komsiyskifafbe052023-02-10 10:23:59 +0100113 virtual status_t onConfigurationChanged(int handle, bool enabled, int64_t samplingPeriodNs,
114 int64_t batchReportLatencyNs) = 0;
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200115 };
Vladimir Komsiyskifafbe052023-02-10 10:23:59 +0100116 RuntimeSensor(const sensor_t& sensor, sp<SensorCallback> callback);
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200117 virtual status_t activate(void* ident, bool enabled) override;
118 virtual status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
119 int64_t maxBatchReportLatencyNs) override;
120 virtual status_t setDelay(void* ident, int handle, int64_t ns) override;
121 virtual bool process(sensors_event_t*, const sensors_event_t&) { return false; }
122 virtual bool isVirtual() const override { return false; }
123
124private:
125 bool mEnabled = false;
126 int64_t mSamplingPeriodNs = 0;
127 int64_t mBatchReportLatencyNs = 0;
Vladimir Komsiyskifafbe052023-02-10 10:23:59 +0100128 sp<SensorCallback> mCallback;
Vladimir Komsiyskif76bba52022-10-23 10:56:06 +0200129};
130
131// ---------------------------------------------------------------------------
132
Andrew Lehmer3a602572021-03-25 15:19:56 -0700133class ProximitySensor : public HardwareSensor {
134public:
135 explicit ProximitySensor(const sensor_t& sensor, SensorService& service);
136
137 status_t activate(void* ident, bool enabled) override;
138
Andrew Lehmer3a602572021-03-25 15:19:56 -0700139private:
140 SensorService& mSensorService;
Andrew Lehmer3a602572021-03-25 15:19:56 -0700141};
Mathias Agopianf001c922010-11-11 17:58:51 -0800142
143// ---------------------------------------------------------------------------
144}; // namespace android
145
146#endif // ANDROID_SENSOR_INTERFACE_H