blob: 7fb927a1b6deb926ed0072e27836f27774a4a160 [file] [log] [blame]
Brian Stack897528d2018-10-23 10:38:03 -07001/*
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 Stack237abc62018-10-23 11:09:59 -070022#include <condition_variable>
23#include <memory>
Brian Stackd23f2002018-11-05 13:48:16 -080024#include <mutex>
Brian Stack237abc62018-10-23 11:09:59 -070025#include <thread>
26#include <vector>
27
28using ::android::hardware::sensors::V1_0::Event;
Brian Stackd23f2002018-11-05 13:48:16 -080029using ::android::hardware::sensors::V1_0::OperationMode;
Brian Stacke4f74c72018-10-29 11:51:46 -070030using ::android::hardware::sensors::V1_0::Result;
Brian Stack897528d2018-10-23 10:38:03 -070031using ::android::hardware::sensors::V1_0::SensorInfo;
Brian Stack237abc62018-10-23 11:09:59 -070032using ::android::hardware::sensors::V1_0::SensorType;
Brian Stack897528d2018-10-23 10:38:03 -070033
34namespace android {
35namespace hardware {
36namespace sensors {
37namespace V2_0 {
38namespace implementation {
39
Brian Stack237abc62018-10-23 11:09:59 -070040class ISensorsEventCallback {
41 public:
42 virtual ~ISensorsEventCallback(){};
43 virtual void postEvents(const std::vector<Event>& events) = 0;
44};
45
Brian Stack897528d2018-10-23 10:38:03 -070046class Sensor {
47 public:
Brian Stack237abc62018-10-23 11:09:59 -070048 Sensor(ISensorsEventCallback* callback);
49 virtual ~Sensor();
Brian Stack897528d2018-10-23 10:38:03 -070050
51 const SensorInfo& getSensorInfo() const;
Brian Stack237abc62018-10-23 11:09:59 -070052 void batch(int32_t samplingPeriodNs);
Brian Stack897528d2018-10-23 10:38:03 -070053 void activate(bool enable);
Brian Stacke4f74c72018-10-29 11:51:46 -070054 Result flush();
Brian Stack897528d2018-10-23 10:38:03 -070055
Brian Stackd23f2002018-11-05 13:48:16 -080056 void setOperationMode(OperationMode mode);
57 bool supportsDataInjection() const;
58 Result injectEvent(const Event& event);
59
Brian Stack897528d2018-10-23 10:38:03 -070060 protected:
Brian Stack237abc62018-10-23 11:09:59 -070061 void run();
62 virtual std::vector<Event> readEvents();
63 static void startThread(Sensor* sensor);
64
Brian Stack897528d2018-10-23 10:38:03 -070065 bool mIsEnabled;
66 int64_t mSamplingPeriodNs;
Brian Stack237abc62018-10-23 11:09:59 -070067 int64_t mLastSampleTimeNs;
Brian Stack897528d2018-10-23 10:38:03 -070068 SensorInfo mSensorInfo;
Brian Stack237abc62018-10-23 11:09:59 -070069
70 std::atomic_bool mStopThread;
71 std::condition_variable mWaitCV;
Brian Stackd23f2002018-11-05 13:48:16 -080072 std::mutex mRunMutex;
Brian Stack237abc62018-10-23 11:09:59 -070073 std::thread mRunThread;
74
75 ISensorsEventCallback* mCallback;
Brian Stackd23f2002018-11-05 13:48:16 -080076
77 OperationMode mMode;
Brian Stack897528d2018-10-23 10:38:03 -070078};
79
Brian Stack2927ab72018-10-23 11:22:55 -070080class AccelSensor : public Sensor {
81 public:
82 AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
83};
84
Brian Stack897528d2018-10-23 10:38:03 -070085} // namespace implementation
86} // namespace V2_0
87} // namespace sensors
88} // namespace hardware
89} // namespace android
90
Brian Stack237abc62018-10-23 11:09:59 -070091#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H