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 | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 20 | #include <sensor/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; |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 29 | class SensorService; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 30 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 31 | class SensorInterface : public VirtualLightRefBase { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 32 | public: |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 33 | virtual ~SensorInterface() {} |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 34 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 35 | 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] | 36 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 37 | virtual status_t activate(void* ident, bool enabled) = 0; |
| 38 | virtual status_t setDelay(void* ident, int handle, int64_t ns) = 0; |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 39 | 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 | |
| 49 | class BaseSensor : public SensorInterface { |
| 50 | public: |
Chih-Hung Hsieh | 38fc101 | 2016-09-01 11:32:35 -0700 | [diff] [blame] | 51 | explicit BaseSensor(const sensor_t& sensor); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 52 | BaseSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 53 | |
| 54 | // Not all sensors need to support batching. |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 55 | virtual status_t batch(void* ident, int handle, int, int64_t samplingPeriodNs, |
| 56 | int64_t maxBatchReportLatencyNs) override { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 57 | if (maxBatchReportLatencyNs == 0) { |
| 58 | return setDelay(ident, handle, samplingPeriodNs); |
| 59 | } |
| 60 | return -EINVAL; |
| 61 | } |
| 62 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 63 | virtual status_t flush(void* /*ident*/, int /*handle*/) override { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 64 | return -EINVAL; |
| 65 | } |
| 66 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 67 | virtual const Sensor& getSensor() const override { return mSensor; } |
| 68 | virtual void autoDisable(void* /*ident*/, int /*handle*/) override { } |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 69 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 70 | protected: |
| 71 | SensorDevice& mSensorDevice; |
| 72 | Sensor mSensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | // --------------------------------------------------------------------------- |
| 76 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 77 | class HardwareSensor : public BaseSensor { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 78 | public: |
Chih-Hung Hsieh | 38fc101 | 2016-09-01 11:32:35 -0700 | [diff] [blame] | 79 | explicit HardwareSensor(const sensor_t& sensor); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 80 | HardwareSensor(const sensor_t& sensor, const uint8_t (&uuid)[16]); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 81 | |
| 82 | virtual ~HardwareSensor(); |
| 83 | |
| 84 | virtual bool process(sensors_event_t* outEvent, |
| 85 | const sensors_event_t& event); |
| 86 | |
Peng Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 87 | virtual status_t activate(void* ident, bool enabled) override; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 88 | 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] | 89 | 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 Xu | 0cc8f80 | 2016-04-05 23:46:03 -0700 | [diff] [blame] | 92 | virtual bool isVirtual() const override { return false; } |
| 93 | virtual void autoDisable(void *ident, int handle) override; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Peng Xu | 755c451 | 2016-04-07 23:15:14 -0700 | [diff] [blame] | 96 | class VirtualSensor : public BaseSensor |
| 97 | { |
| 98 | public: |
| 99 | VirtualSensor(); |
| 100 | virtual bool isVirtual() const override { return true; } |
| 101 | protected: |
| 102 | SensorFusion& mSensorFusion; |
| 103 | }; |
| 104 | |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 105 | // --------------------------------------------------------------------------- |
| 106 | |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame] | 107 | class RuntimeSensor : public BaseSensor { |
| 108 | public: |
| 109 | static constexpr int DEFAULT_DEVICE_ID = 0; |
| 110 | |
Vladimir Komsiyski | fafbe05 | 2023-02-10 10:23:59 +0100 | [diff] [blame] | 111 | class SensorCallback : public virtual RefBase { |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame] | 112 | public: |
Vladimir Komsiyski | fafbe05 | 2023-02-10 10:23:59 +0100 | [diff] [blame] | 113 | virtual status_t onConfigurationChanged(int handle, bool enabled, int64_t samplingPeriodNs, |
| 114 | int64_t batchReportLatencyNs) = 0; |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame] | 115 | }; |
Vladimir Komsiyski | fafbe05 | 2023-02-10 10:23:59 +0100 | [diff] [blame] | 116 | RuntimeSensor(const sensor_t& sensor, sp<SensorCallback> callback); |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame] | 117 | 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 | |
| 124 | private: |
| 125 | bool mEnabled = false; |
| 126 | int64_t mSamplingPeriodNs = 0; |
| 127 | int64_t mBatchReportLatencyNs = 0; |
Vladimir Komsiyski | fafbe05 | 2023-02-10 10:23:59 +0100 | [diff] [blame] | 128 | sp<SensorCallback> mCallback; |
Vladimir Komsiyski | f76bba5 | 2022-10-23 10:56:06 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | // --------------------------------------------------------------------------- |
| 132 | |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 133 | class ProximitySensor : public HardwareSensor { |
| 134 | public: |
| 135 | explicit ProximitySensor(const sensor_t& sensor, SensorService& service); |
| 136 | |
| 137 | status_t activate(void* ident, bool enabled) override; |
| 138 | |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 139 | private: |
| 140 | SensorService& mSensorService; |
Andrew Lehmer | 3a60257 | 2021-03-25 15:19:56 -0700 | [diff] [blame] | 141 | }; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 142 | |
| 143 | // --------------------------------------------------------------------------- |
| 144 | }; // namespace android |
| 145 | |
| 146 | #endif // ANDROID_SENSOR_INTERFACE_H |