blob: 21ad10d0029205b9e329c5ea645be5bd528d1f7c [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 Stack897528d2018-10-23 10:38:03 -070028using ::android::hardware::sensors::V1_0::SensorInfo;
Brian Stack237abc62018-10-23 11:09:59 -070029using ::android::hardware::sensors::V1_0::SensorType;
Brian Stack897528d2018-10-23 10:38:03 -070030
31namespace android {
32namespace hardware {
33namespace sensors {
34namespace V2_0 {
35namespace implementation {
36
Brian Stack237abc62018-10-23 11:09:59 -070037class ISensorsEventCallback {
38 public:
39 virtual ~ISensorsEventCallback(){};
40 virtual void postEvents(const std::vector<Event>& events) = 0;
41};
42
Brian Stack897528d2018-10-23 10:38:03 -070043class Sensor {
44 public:
Brian Stack237abc62018-10-23 11:09:59 -070045 Sensor(ISensorsEventCallback* callback);
46 virtual ~Sensor();
Brian Stack897528d2018-10-23 10:38:03 -070047
48 const SensorInfo& getSensorInfo() const;
Brian Stack237abc62018-10-23 11:09:59 -070049 void batch(int32_t samplingPeriodNs);
Brian Stack897528d2018-10-23 10:38:03 -070050 void activate(bool enable);
51
52 protected:
Brian Stack237abc62018-10-23 11:09:59 -070053 void run();
54 virtual std::vector<Event> readEvents();
55 static void startThread(Sensor* sensor);
56
Brian Stack897528d2018-10-23 10:38:03 -070057 bool mIsEnabled;
58 int64_t mSamplingPeriodNs;
Brian Stack237abc62018-10-23 11:09:59 -070059 int64_t mLastSampleTimeNs;
Brian Stack897528d2018-10-23 10:38:03 -070060 SensorInfo mSensorInfo;
Brian Stack237abc62018-10-23 11:09:59 -070061
62 std::atomic_bool mStopThread;
63 std::condition_variable mWaitCV;
64 std::thread mRunThread;
65
66 ISensorsEventCallback* mCallback;
Brian Stack897528d2018-10-23 10:38:03 -070067};
68
69} // namespace implementation
70} // namespace V2_0
71} // namespace sensors
72} // namespace hardware
73} // namespace android
74
Brian Stack237abc62018-10-23 11:09:59 -070075#endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H