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 VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_WRAPPER_H_ 18 #define VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_WRAPPER_H_ 19 20 #include <android-base/thread_annotations.h> 21 #include <android/frameworks/sensorservice/1.0/ISensorManager.h> 22 #include <android/frameworks/sensorservice/1.0/types.h> 23 24 #include <deque> 25 #include <functional> 26 #include <mutex> 27 28 #include "utils/Errors.h" 29 #include "utils/RefBase.h" 30 31 namespace android { 32 namespace camera_sensor_listener { 33 34 struct ExtendedSensorEvent { 35 // Actual sensor event data. 36 ::android::hardware::sensors::V1_0::Event sensor_event; 37 // Event arrival time, i.e., time of callback being triggered. 38 int64_t event_arrival_time_ns; 39 }; 40 41 class GoogSensorWrapper : public virtual RefBase { 42 public: 43 virtual ~GoogSensorWrapper(); 44 45 // Set user-defined sensor event callback function. 46 // When sensor event arrives, event_processor will be invoked to perform 47 // user-defined callback operations. 48 // It should be called before GoogleSensorWrapper::Enable, otherwise it 49 // won't take effect. 50 status_t SetEventProcessor( 51 std::function<void(const ExtendedSensorEvent& event)> event_processor); 52 53 // Enables the sensor. When object is created, sensor is disabled by default. 54 // Returns 0 on success. 55 status_t Enable(); 56 57 // Disables the sensor. Returns 0 on success. 58 status_t Disable(); 59 60 // True if the sensor is currently enabled. IsEnabled()61 bool IsEnabled() const { return enabled_; } 62 63 protected: 64 // Input: 65 // event_queue_size: sets size of the event queue. 66 // sensor_sampling_period_us: sets sensor sampling period in us (1e-6s). 67 // Default sampling rate is set to 5ms. However, sensor_sampling_period_us 68 // has no effect on on_change type sensors like Vsync or Light. 69 GoogSensorWrapper(size_t event_queue_size, 70 int64_t sensor_sampling_period_us = 5000); 71 72 // Virtual function to get different sensor handler, e.g., gyro handler. 73 virtual int32_t GetSensorHandle() = 0; 74 75 // Buffer of the most recent events. Oldest in the front. 76 std::deque<ExtendedSensorEvent> event_buffer_ GUARDED_BY(event_buffer_lock_); 77 78 // Lock protecting event_buffer_. 79 mutable std::mutex event_buffer_lock_; 80 81 private: 82 // Event callback function. 83 // When invoked, it will enqueue Event e to event_deque_, and further invoke 84 // user-defined callback function event_processor_. 85 int EventCallback(const ::android::hardware::sensors::V1_0::Event& e); 86 87 // Initialize sensor handler and set event_queue_. 88 status_t InitializeEventQueueLocked() 89 EXCLUSIVE_LOCKS_REQUIRED(event_queue_lock_); 90 91 // Strong pointer to IEventQueue allocated by sensor service. 92 sp<::android::frameworks::sensorservice::V1_0::IEventQueue> event_queue_ 93 GUARDED_BY(event_queue_lock_); 94 95 // User-defined callback functor invoked when sensor event arrives. 96 std::function<void(const ExtendedSensorEvent& event)> event_processor_ 97 GUARDED_BY(event_processor_lock_); 98 99 // Lock protecting event_queue_. 100 mutable std::mutex event_queue_lock_; 101 102 // Lock protecting event_processor_. 103 mutable std::mutex event_processor_lock_; 104 105 // Size limit for the event buffer. 106 size_t event_buffer_size_limit_; 107 108 // Sampling period to read sensor events. 109 int64_t sensor_sampling_period_us_; 110 111 // Sensor handler. 112 int handle_; 113 114 // Whether sensor is enabled. 115 bool enabled_; 116 117 friend class EventQueueCallback; 118 }; 119 120 } // namespace camera_sensor_listener 121 } // namespace android 122 123 #endif // VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_SENSOR_WRAPPER_H_ 124