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 
22 #include <condition_variable>
23 #include <memory>
24 #include <mutex>
25 #include <thread>
26 #include <vector>
27 
28 using ::android::hardware::sensors::V1_0::Event;
29 using ::android::hardware::sensors::V1_0::OperationMode;
30 using ::android::hardware::sensors::V1_0::Result;
31 using ::android::hardware::sensors::V1_0::SensorInfo;
32 using ::android::hardware::sensors::V1_0::SensorType;
33 
34 namespace android {
35 namespace hardware {
36 namespace sensors {
37 namespace V2_0 {
38 namespace implementation {
39 
40 class ISensorsEventCallback {
41    public:
~ISensorsEventCallback()42     virtual ~ISensorsEventCallback(){};
43     virtual void postEvents(const std::vector<Event>& events, bool wakeup) = 0;
44 };
45 
46 class Sensor {
47    public:
48     Sensor(ISensorsEventCallback* callback);
49     virtual ~Sensor();
50 
51     const SensorInfo& getSensorInfo() const;
52     void batch(int32_t samplingPeriodNs);
53     virtual void activate(bool enable);
54     Result flush();
55 
56     void setOperationMode(OperationMode mode);
57     bool supportsDataInjection() const;
58     Result injectEvent(const Event& event);
59 
60    protected:
61     void run();
62     virtual std::vector<Event> readEvents();
63     static void startThread(Sensor* sensor);
64 
65     bool isWakeUpSensor();
66 
67     bool mIsEnabled;
68     int64_t mSamplingPeriodNs;
69     int64_t mLastSampleTimeNs;
70     SensorInfo mSensorInfo;
71 
72     std::atomic_bool mStopThread;
73     std::condition_variable mWaitCV;
74     std::mutex mRunMutex;
75     std::thread mRunThread;
76 
77     ISensorsEventCallback* mCallback;
78 
79     OperationMode mMode;
80 };
81 
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 
96 class AccelSensor : public Sensor {
97    public:
98     AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
99 };
100 
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 
141 }  // namespace implementation
142 }  // namespace V2_0
143 }  // namespace sensors
144 }  // namespace hardware
145 }  // namespace android
146 
147 #endif  // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H
148