Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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_HARDWARE_SENSORS_V2_0_SENSOR_H |
| 18 | #define ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H |
| 19 | |
| 20 | #include <android/hardware/sensors/1.0/types.h> |
| 21 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 22 | #include <condition_variable> |
| 23 | #include <memory> |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 24 | #include <mutex> |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 25 | #include <thread> |
| 26 | #include <vector> |
| 27 | |
| 28 | using ::android::hardware::sensors::V1_0::Event; |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 29 | using ::android::hardware::sensors::V1_0::OperationMode; |
Brian Stack | e4f74c7 | 2018-10-29 11:51:46 -0700 | [diff] [blame] | 30 | using ::android::hardware::sensors::V1_0::Result; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 31 | using ::android::hardware::sensors::V1_0::SensorInfo; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 32 | using ::android::hardware::sensors::V1_0::SensorType; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace hardware { |
| 36 | namespace sensors { |
| 37 | namespace V2_0 { |
| 38 | namespace implementation { |
| 39 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 40 | class ISensorsEventCallback { |
| 41 | public: |
| 42 | virtual ~ISensorsEventCallback(){}; |
Brian Stack | f2aca3b | 2018-11-05 09:37:15 -0800 | [diff] [blame] | 43 | virtual void postEvents(const std::vector<Event>& events, bool wakeup) = 0; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 46 | class Sensor { |
| 47 | public: |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 48 | Sensor(ISensorsEventCallback* callback); |
| 49 | virtual ~Sensor(); |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 50 | |
| 51 | const SensorInfo& getSensorInfo() const; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 52 | void batch(int32_t samplingPeriodNs); |
Brian Stack | a2d16bb | 2019-01-25 11:22:04 -0800 | [diff] [blame] | 53 | virtual void activate(bool enable); |
Brian Stack | e4f74c7 | 2018-10-29 11:51:46 -0700 | [diff] [blame] | 54 | Result flush(); |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 55 | |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 56 | void setOperationMode(OperationMode mode); |
| 57 | bool supportsDataInjection() const; |
| 58 | Result injectEvent(const Event& event); |
| 59 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 60 | protected: |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 61 | void run(); |
| 62 | virtual std::vector<Event> readEvents(); |
| 63 | static void startThread(Sensor* sensor); |
| 64 | |
Brian Stack | f2aca3b | 2018-11-05 09:37:15 -0800 | [diff] [blame] | 65 | bool isWakeUpSensor(); |
| 66 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 67 | bool mIsEnabled; |
| 68 | int64_t mSamplingPeriodNs; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 69 | int64_t mLastSampleTimeNs; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 70 | SensorInfo mSensorInfo; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 71 | |
| 72 | std::atomic_bool mStopThread; |
| 73 | std::condition_variable mWaitCV; |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 74 | std::mutex mRunMutex; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 75 | std::thread mRunThread; |
| 76 | |
| 77 | ISensorsEventCallback* mCallback; |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 78 | |
| 79 | OperationMode mMode; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Brian Stack | a2d16bb | 2019-01-25 11:22:04 -0800 | [diff] [blame] | 82 | class OnChangeSensor : public Sensor { |
| 83 | public: |
| 84 | OnChangeSensor(ISensorsEventCallback* callback); |
| 85 | |
| 86 | virtual void activate(bool enable) override; |
| 87 | |
| 88 | protected: |
| 89 | virtual std::vector<Event> readEvents() override; |
| 90 | |
| 91 | protected: |
| 92 | Event mPreviousEvent; |
| 93 | bool mPreviousEventSet; |
| 94 | }; |
| 95 | |
Brian Stack | 2927ab7 | 2018-10-23 11:22:55 -0700 | [diff] [blame] | 96 | class AccelSensor : public Sensor { |
| 97 | public: |
| 98 | AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 99 | }; |
| 100 | |
Brian Stack | a2d16bb | 2019-01-25 11:22:04 -0800 | [diff] [blame] | 101 | class GyroSensor : public Sensor { |
| 102 | public: |
| 103 | GyroSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 104 | }; |
| 105 | |
| 106 | class AmbientTempSensor : public OnChangeSensor { |
| 107 | public: |
| 108 | AmbientTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 109 | }; |
| 110 | |
| 111 | class DeviceTempSensor : public OnChangeSensor { |
| 112 | public: |
| 113 | DeviceTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 114 | }; |
| 115 | |
| 116 | class PressureSensor : public Sensor { |
| 117 | public: |
| 118 | PressureSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 119 | }; |
| 120 | |
| 121 | class MagnetometerSensor : public Sensor { |
| 122 | public: |
| 123 | MagnetometerSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 124 | }; |
| 125 | |
| 126 | class LightSensor : public OnChangeSensor { |
| 127 | public: |
| 128 | LightSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 129 | }; |
| 130 | |
| 131 | class ProximitySensor : public OnChangeSensor { |
| 132 | public: |
| 133 | ProximitySensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 134 | }; |
| 135 | |
| 136 | class RelativeHumiditySensor : public OnChangeSensor { |
| 137 | public: |
| 138 | RelativeHumiditySensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 139 | }; |
| 140 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 141 | } // namespace implementation |
| 142 | } // namespace V2_0 |
| 143 | } // namespace sensors |
| 144 | } // namespace hardware |
| 145 | } // namespace android |
| 146 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 147 | #endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H |