Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 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_0_SENSOR_H |
| 18 | #define ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H |
| 19 | |
| 20 | #include <android/hardware/sensors/1.0/types.h> |
| 21 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 22 | #include <condition_variable> |
| 23 | #include <memory> |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 24 | #include <mutex> |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 25 | #include <thread> |
| 26 | #include <vector> |
| 27 | |
| 28 | using ::android::hardware::sensors::V1_0::Event; |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 29 | using ::android::hardware::sensors::V1_0::OperationMode; |
Brian Stack | e4f74c7 | 2018-10-29 11:51:46 -0700 | [diff] [blame] | 30 | using ::android::hardware::sensors::V1_0::Result; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 31 | using ::android::hardware::sensors::V1_0::SensorInfo; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 32 | using ::android::hardware::sensors::V1_0::SensorType; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace hardware { |
| 36 | namespace sensors { |
| 37 | namespace V2_0 { |
| 38 | namespace implementation { |
| 39 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 40 | class ISensorsEventCallback { |
| 41 | public: |
| 42 | virtual ~ISensorsEventCallback(){}; |
Brian Stack | f2aca3b | 2018-11-05 09:37:15 -0800 | [diff] [blame^] | 43 | virtual void postEvents(const std::vector<Event>& events, bool wakeup) = 0; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 46 | class Sensor { |
| 47 | public: |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 48 | Sensor(ISensorsEventCallback* callback); |
| 49 | virtual ~Sensor(); |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 50 | |
| 51 | const SensorInfo& getSensorInfo() const; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 52 | void batch(int32_t samplingPeriodNs); |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 53 | void activate(bool enable); |
Brian Stack | e4f74c7 | 2018-10-29 11:51:46 -0700 | [diff] [blame] | 54 | Result flush(); |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 55 | |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 56 | void setOperationMode(OperationMode mode); |
| 57 | bool supportsDataInjection() const; |
| 58 | Result injectEvent(const Event& event); |
| 59 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 60 | protected: |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 61 | void run(); |
| 62 | virtual std::vector<Event> readEvents(); |
| 63 | static void startThread(Sensor* sensor); |
| 64 | |
Brian Stack | f2aca3b | 2018-11-05 09:37:15 -0800 | [diff] [blame^] | 65 | bool isWakeUpSensor(); |
| 66 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 67 | bool mIsEnabled; |
| 68 | int64_t mSamplingPeriodNs; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 69 | int64_t mLastSampleTimeNs; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 70 | SensorInfo mSensorInfo; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 71 | |
| 72 | std::atomic_bool mStopThread; |
| 73 | std::condition_variable mWaitCV; |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 74 | std::mutex mRunMutex; |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 75 | std::thread mRunThread; |
| 76 | |
| 77 | ISensorsEventCallback* mCallback; |
Brian Stack | d23f200 | 2018-11-05 13:48:16 -0800 | [diff] [blame] | 78 | |
| 79 | OperationMode mMode; |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Brian Stack | 2927ab7 | 2018-10-23 11:22:55 -0700 | [diff] [blame] | 82 | class AccelSensor : public Sensor { |
| 83 | public: |
| 84 | AccelSensor(int32_t sensorHandle, ISensorsEventCallback* callback); |
| 85 | }; |
| 86 | |
Brian Stack | 897528d | 2018-10-23 10:38:03 -0700 | [diff] [blame] | 87 | } // namespace implementation |
| 88 | } // namespace V2_0 |
| 89 | } // namespace sensors |
| 90 | } // namespace hardware |
| 91 | } // namespace android |
| 92 | |
Brian Stack | 237abc6 | 2018-10-23 11:09:59 -0700 | [diff] [blame] | 93 | #endif // ANDROID_HARDWARE_SENSORS_V2_0_SENSOR_H |