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