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