blob: dafcf2ddcae67f22e361e64f0774be2961eba598 [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 Agopianf001c922010-11-11 17:58:51 -080020#include <gui/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;
Mathias Agopianf001c922010-11-11 17:58:51 -080029
Peng Xu755c4512016-04-07 23:15:14 -070030class SensorInterface : public VirtualLightRefBase {
Mathias Agopianf001c922010-11-11 17:58:51 -080031public:
Peng Xu755c4512016-04-07 23:15:14 -070032 virtual ~SensorInterface() {}
Mathias Agopianf001c922010-11-11 17:58:51 -080033
Peng Xu0cc8f802016-04-05 23:46:03 -070034 virtual bool process(sensors_event_t* outEvent, const sensors_event_t& event) = 0;
Mathias Agopianf001c922010-11-11 17:58:51 -080035
Mathias Agopianf001c922010-11-11 17:58:51 -080036 virtual status_t activate(void* ident, bool enabled) = 0;
37 virtual status_t setDelay(void* ident, int handle, int64_t ns) = 0;
Peng Xu755c4512016-04-07 23:15:14 -070038 virtual status_t batch(void* ident, int handle, int /*flags*/, int64_t samplingPeriodNs,
39 int64_t maxBatchReportLatencyNs) = 0;
40
41 virtual status_t flush(void* /*ident*/, int /*handle*/) = 0;
42
43 virtual const Sensor& getSensor() const = 0;
44 virtual bool isVirtual() const = 0;
45 virtual void autoDisable(void* /*ident*/, int /*handle*/) = 0;
46};
47
48class BaseSensor : public SensorInterface {
49public:
50 BaseSensor(const sensor_t& sensor);
Peng Xu6a2d3a02015-12-21 12:00:23 -080051 BaseSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
Aravind Akella724d91d2013-06-27 12:04:23 -070052
53 // Not all sensors need to support batching.
Peng Xu755c4512016-04-07 23:15:14 -070054 virtual status_t batch(void* ident, int handle, int, int64_t samplingPeriodNs,
55 int64_t maxBatchReportLatencyNs) override {
Aravind Akella724d91d2013-06-27 12:04:23 -070056 if (maxBatchReportLatencyNs == 0) {
57 return setDelay(ident, handle, samplingPeriodNs);
58 }
59 return -EINVAL;
60 }
61
Peng Xu755c4512016-04-07 23:15:14 -070062 virtual status_t flush(void* /*ident*/, int /*handle*/) override {
Aravind Akella724d91d2013-06-27 12:04:23 -070063 return -EINVAL;
64 }
65
Peng Xu755c4512016-04-07 23:15:14 -070066 virtual const Sensor& getSensor() const override { return mSensor; }
67 virtual void autoDisable(void* /*ident*/, int /*handle*/) override { }
68protected:
69 SensorDevice& mSensorDevice;
70 Sensor mSensor;
Mathias Agopianf001c922010-11-11 17:58:51 -080071};
72
73// ---------------------------------------------------------------------------
74
Peng Xu755c4512016-04-07 23:15:14 -070075class HardwareSensor : public BaseSensor {
Mathias Agopianf001c922010-11-11 17:58:51 -080076public:
77 HardwareSensor(const sensor_t& sensor);
Peng Xu6a2d3a02015-12-21 12:00:23 -080078 HardwareSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]);
Mathias Agopianf001c922010-11-11 17:58:51 -080079
80 virtual ~HardwareSensor();
81
82 virtual bool process(sensors_event_t* outEvent,
83 const sensors_event_t& event);
84
Peng Xu0cc8f802016-04-05 23:46:03 -070085 virtual status_t activate(void* ident, bool enabled) override;
Aravind Akella724d91d2013-06-27 12:04:23 -070086 virtual status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
Peng Xu0cc8f802016-04-05 23:46:03 -070087 int64_t maxBatchReportLatencyNs) override;
88 virtual status_t setDelay(void* ident, int handle, int64_t ns) override;
89 virtual status_t flush(void* ident, int handle) override;
Peng Xu0cc8f802016-04-05 23:46:03 -070090 virtual bool isVirtual() const override { return false; }
91 virtual void autoDisable(void *ident, int handle) override;
Mathias Agopianf001c922010-11-11 17:58:51 -080092};
93
Peng Xu755c4512016-04-07 23:15:14 -070094class VirtualSensor : public BaseSensor
95{
96public:
97 VirtualSensor();
98 virtual bool isVirtual() const override { return true; }
99protected:
100 SensorFusion& mSensorFusion;
101};
102
Mathias Agopianf001c922010-11-11 17:58:51 -0800103
104// ---------------------------------------------------------------------------
105}; // namespace android
106
107#endif // ANDROID_SENSOR_INTERFACE_H