blob: 3ab2299a23966e2bd1bfac8a091a3d2e7db47524 [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(){};
Brian Stackf2aca3b2018-11-05 09:37:15 -080043 virtual void postEvents(const std::vector<Event>& events, bool wakeup) = 0;
Brian Stack237abc62018-10-23 11:09:59 -070044};
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 Stackf2aca3b2018-11-05 09:37:15 -080065 bool isWakeUpSensor();
66
Brian Stack897528d2018-10-23 10:38:03 -070067 bool mIsEnabled;
68 int64_t mSamplingPeriodNs;
Brian Stack237abc62018-10-23 11:09:59 -070069 int64_t mLastSampleTimeNs;
Brian Stack897528d2018-10-23 10:38:03 -070070 SensorInfo mSensorInfo;
Brian Stack237abc62018-10-23 11:09:59 -070071
72 std::atomic_bool mStopThread;
73 std::condition_variable mWaitCV;
Brian Stackd23f2002018-11-05 13:48:16 -080074 std::mutex mRunMutex;
Brian Stack237abc62018-10-23 11:09:59 -070075 std::thread mRunThread;
76
77 ISensorsEventCallback* mCallback;
Brian Stackd23f2002018-11-05 13:48:16 -080078
79 OperationMode mMode;
Brian Stack897528d2018-10-23 10:38:03 -070080};
81
Brian Stack2927ab72018-10-23 11:22:55 -070082class AccelSensor : public Sensor {
83 public:
84 AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
85};
86
Brian Stack897528d2018-10-23 10:38:03 -070087} // namespace implementation
88} // namespace V2_0
89} // namespace sensors
90} // namespace hardware
91} // namespace android
92
Brian Stack237abc62018-10-23 11:09:59 -070093#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H