• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 float 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 SensorInfo = ::android::hardware::sensors::V2_1::SensorInfo;
51     using SensorType = ::android::hardware::sensors::V2_1::SensorType;
52 
53     Sensor(ISensorsEventCallback* callback);
54     virtual ~Sensor();
55 
56     const SensorInfo& getSensorInfo() const;
57     void batch(int32_t samplingPeriodNs);
58     virtual void activate(bool enable);
59     Result flush();
60 
61     void setOperationMode(OperationMode mode);
62     bool supportsDataInjection() const;
63     Result injectEvent(const Event& event);
64 
65   protected:
66     void run();
67     virtual std::vector<Event> readEvents();
68     static void startThread(Sensor* sensor);
69 
70     bool isWakeUpSensor();
71 
72     bool mIsEnabled;
73     int64_t mSamplingPeriodNs;
74     int64_t mLastSampleTimeNs;
75     SensorInfo mSensorInfo;
76 
77     std::atomic_bool mStopThread;
78     std::condition_variable mWaitCV;
79     std::mutex mRunMutex;
80     std::thread mRunThread;
81 
82     ISensorsEventCallback* mCallback;
83 
84     OperationMode mMode;
85 };
86 
87 class OnChangeSensor : public Sensor {
88   public:
89     OnChangeSensor(ISensorsEventCallback* callback);
90 
91     virtual void activate(bool enable) override;
92 
93   protected:
94     virtual std::vector<Event> readEvents() override;
95 
96   protected:
97     Event mPreviousEvent;
98     bool mPreviousEventSet;
99 };
100 
101 class AccelSensor : public Sensor {
102   public:
103     AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
104 };
105 
106 class GyroSensor : public Sensor {
107   public:
108     GyroSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
109 };
110 
111 class AmbientTempSensor : public OnChangeSensor {
112   public:
113     AmbientTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
114 };
115 
116 class DeviceTempSensor : public OnChangeSensor {
117   public:
118     DeviceTempSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
119 };
120 
121 class PressureSensor : public Sensor {
122   public:
123     PressureSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
124 };
125 
126 class MagnetometerSensor : public Sensor {
127   public:
128     MagnetometerSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
129 };
130 
131 class LightSensor : public OnChangeSensor {
132   public:
133     LightSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
134 };
135 
136 class ProximitySensor : public OnChangeSensor {
137   public:
138     ProximitySensor(int32_t sensorHandle, ISensorsEventCallback* callback);
139 };
140 
141 class RelativeHumiditySensor : public OnChangeSensor {
142   public:
143     RelativeHumiditySensor(int32_t sensorHandle, ISensorsEventCallback* callback);
144 };
145 
146 }  // namespace implementation
147 }  // namespace V2_X
148 }  // namespace sensors
149 }  // namespace hardware
150 }  // namespace android
151 
152 #endif  // ANDROID_HARDWARE_SENSORS_V2_X_SENSOR_H
153