blob: 75d9aabb1ced7f022e37ab4c4983d2c0eb7acac2 [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>
24#include <thread>
25#include <vector>
26
27using ::android::hardware::sensors::V1_0::Event;
Brian Stacke4f74c72018-10-29 11:51:46 -070028using ::android::hardware::sensors::V1_0::Result;
Brian Stack897528d2018-10-23 10:38:03 -070029using ::android::hardware::sensors::V1_0::SensorInfo;
Brian Stack237abc62018-10-23 11:09:59 -070030using ::android::hardware::sensors::V1_0::SensorType;
Brian Stack897528d2018-10-23 10:38:03 -070031
32namespace android {
33namespace hardware {
34namespace sensors {
35namespace V2_0 {
36namespace implementation {
37
Brian Stack237abc62018-10-23 11:09:59 -070038class ISensorsEventCallback {
39 public:
40 virtual ~ISensorsEventCallback(){};
41 virtual void postEvents(const std::vector<Event>& events) = 0;
42};
43
Brian Stack897528d2018-10-23 10:38:03 -070044class Sensor {
45 public:
Brian Stack237abc62018-10-23 11:09:59 -070046 Sensor(ISensorsEventCallback* callback);
47 virtual ~Sensor();
Brian Stack897528d2018-10-23 10:38:03 -070048
49 const SensorInfo& getSensorInfo() const;
Brian Stack237abc62018-10-23 11:09:59 -070050 void batch(int32_t samplingPeriodNs);
Brian Stack897528d2018-10-23 10:38:03 -070051 void activate(bool enable);
Brian Stacke4f74c72018-10-29 11:51:46 -070052 Result flush();
Brian Stack897528d2018-10-23 10:38:03 -070053
54 protected:
Brian Stack237abc62018-10-23 11:09:59 -070055 void run();
56 virtual std::vector<Event> readEvents();
57 static void startThread(Sensor* sensor);
58
Brian Stack897528d2018-10-23 10:38:03 -070059 bool mIsEnabled;
60 int64_t mSamplingPeriodNs;
Brian Stack237abc62018-10-23 11:09:59 -070061 int64_t mLastSampleTimeNs;
Brian Stack897528d2018-10-23 10:38:03 -070062 SensorInfo mSensorInfo;
Brian Stack237abc62018-10-23 11:09:59 -070063
64 std::atomic_bool mStopThread;
65 std::condition_variable mWaitCV;
66 std::thread mRunThread;
67
68 ISensorsEventCallback* mCallback;
Brian Stack897528d2018-10-23 10:38:03 -070069};
70
Brian Stack2927ab72018-10-23 11:22:55 -070071class AccelSensor : public Sensor {
72 public:
73 AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback);
74};
75
Brian Stack897528d2018-10-23 10:38:03 -070076} // namespace implementation
77} // namespace V2_0
78} // namespace sensors
79} // namespace hardware
80} // namespace android
81
Brian Stack237abc62018-10-23 11:09:59 -070082#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H