Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 20 | #include <gui/Sensor.h> |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 21 | #include <utils/RefBase.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 22 | |
| 23 | // --------------------------------------------------------------------------- |
| 24 | |
| 25 | namespace android { |
| 26 | // --------------------------------------------------------------------------- |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 27 | class SensorDevice; |
| 28 | class SensorFusion; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 29 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 30 | class SensorInterface : public VirtualLightRefBase { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 31 | public: |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 32 | virtual ~SensorInterface() {} |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 33 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 34 | virtual bool process(sensors_event_t* outEvent, const sensors_event_t& event) = 0; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 35 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 | virtual status_t activate(void* ident, bool enabled) = 0; |
| 37 | virtual status_t setDelay(void* ident, int handle, int64_t ns) = 0; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 38 | 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 | |
| 48 | class BaseSensor : public SensorInterface { |
| 49 | public: |
| 50 | BaseSensor(const sensor_t& sensor); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 51 | |
| 52 | // Not all sensors need to support batching. |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 53 | virtual status_t batch(void* ident, int handle, int, int64_t samplingPeriodNs, |
| 54 | int64_t maxBatchReportLatencyNs) override { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 55 | if (maxBatchReportLatencyNs == 0) { |
| 56 | return setDelay(ident, handle, samplingPeriodNs); |
| 57 | } |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 61 | virtual status_t flush(void* /*ident*/, int /*handle*/) override { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | } |
| 64 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 65 | virtual const Sensor& getSensor() const override { return mSensor; } |
| 66 | virtual void autoDisable(void* /*ident*/, int /*handle*/) override { } |
| 67 | protected: |
| 68 | SensorDevice& mSensorDevice; |
| 69 | Sensor mSensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | // --------------------------------------------------------------------------- |
| 73 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 74 | class HardwareSensor : public BaseSensor { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 75 | public: |
| 76 | HardwareSensor(const sensor_t& sensor); |
| 77 | |
| 78 | virtual ~HardwareSensor(); |
| 79 | |
| 80 | virtual bool process(sensors_event_t* outEvent, |
| 81 | const sensors_event_t& event); |
| 82 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 83 | virtual status_t activate(void* ident, bool enabled) override; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 84 | virtual status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 85 | int64_t maxBatchReportLatencyNs) override; |
| 86 | virtual status_t setDelay(void* ident, int handle, int64_t ns) override; |
| 87 | virtual status_t flush(void* ident, int handle) override; |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 88 | virtual bool isVirtual() const override { return false; } |
| 89 | virtual void autoDisable(void *ident, int handle) override; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 92 | class VirtualSensor : public BaseSensor |
| 93 | { |
| 94 | public: |
| 95 | VirtualSensor(); |
| 96 | virtual bool isVirtual() const override { return true; } |
| 97 | protected: |
| 98 | SensorFusion& mSensorFusion; |
| 99 | }; |
| 100 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 101 | |
| 102 | // --------------------------------------------------------------------------- |
| 103 | }; // namespace android |
| 104 | |
| 105 | #endif // ANDROID_SENSOR_INTERFACE_H |