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_GYRO_DIRECT_H_
18 #define VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_GYRO_DIRECT_H_
19 
20 #include <android/frameworks/sensorservice/1.0/ISensorManager.h>
21 #include <android/hardware/sensors/1.0/types.h>
22 
23 #include "goog_gralloc_wrapper.h"
24 
25 namespace android {
26 namespace camera_sensor_listener {
27 
28 // GoogGryoDirect class will create gyro direct channel listener.
29 // It fetches gyro events (timestamp, azimuth, pitch, roll, fetching timestamp)
30 // from a shared buffer.
31 // Sample usage:
32 //   std::unique_ptr<GoogGyroDirect> direct_ptr_ = GoogGyroDirect::Create(
33 //       /*gyro_sampling_period_us=*/5000,
34 //       /*event_queue_size=*/20);
35 //   if (direct_ptr->GetSensorEnablingStatus()) {
36 //     std::vector<int64_t> event_timestamps;
37 //     std::vector<float> motion_vector_x;
38 //     std::vector<float> motion_vector_y;
39 //     std::vector<float> motion_vector_z;
40 //     std::vector<int64_t> arrival_timestamps;
41 //     // Get all available gyro events.
42 //     direct_ptr->QueryGyroEventsBetweenTimestamps(
43 //         /*start_time=*/0,
44 //         /*end_time=*/LLONG_MAX,
45 //         &event_timestamps,
46 //         &motion_vector_x,
47 //         &motion_vector_y,
48 //         &motion_vector_z,
49 //         &arrival_timestamps);
50 //   }
51 class GoogGyroDirect {
52  public:
53   // Create a new instance of GoogGyroDirect.
54   // Inputs:
55   //   gyro_sampling_period_us: gyro sampling period in us (1e-6s).
56   //     Direct channel only supports following rates:
57   //       NORMAL,          // nominal 50Hz
58   //       FAST,            // nominal 200Hz
59   //       VERY_FAST,       // nominal 800Hz
60   //     gyro_sampling_period_us will be converted to closest rate level, i.e.,
61   //     sampling_frequency = 1e6 / gyro_sampling_period_us,
62   //     sampling_frequency < 110Hz -> RateLevel:NORMAL,
63   //     110Hz <= sampling_frequency < 440Hz -> RateLevel:FAST,
64   //     sampling_frequency >= 440Hz -> RateLevel::VERY_FAST.
65   //     Default sampling period is 5000us (200Hz -> RateLevel:FAST).
66   //  event_queue_size: size of event queue to hold incoming sensor events.
67   //    Default value is set to 20.
68   static std::unique_ptr<GoogGyroDirect> Create(
69       int64_t gyro_sampling_period_us = kDefaultSamplingPeriodUs,
70       size_t event_queue_size = kDefaultEventQueueSize);
71 
72   // Destructor.
73   // Destroy and free the resources of a GoogGyroDirect.
74   ~GoogGyroDirect();
75 
76   // Get whether gyro direct channel is enabled.
77   // Return true if gyro direct channel is enabled, false otherwise.
GetSensorEnablingStatus()78   bool GetSensorEnablingStatus() const {
79     return gyro_direct_enabled_;
80   }
81 
82   // Query gyro events between between the range (start_time, end_time].
83   // Inputs:
84   //   start_time: queried events' timestamps must be > start_time.
85   //   end_time: queried events' timestamps must be <= end_time.
86   // Outputs:
87   //   event_timestamps: pointer of vector to hold queried events' timestamps.
88   //   motion_vector_x: pointer of vector to hold queried events' x axis data.
89   //   motion_vector_y: pointer of vector to hold queried events' y axis data.
90   //   motion_vector_z: pointer of vector to hold queried events' z axis data.
91   //   event_arrival_timestamps: pointer of vector to hold arrival times.
92   // Event timestamps, data and arrival timestamps are listed in chronological
93   // order, i.e., event_timestamps[0], motion_vector_x[0],
94   // motion_vector_y[0], motion_vector_z[0], and event_arrival_timestamps[0]
95   // hold the earliest data.
96   // motion_vector_x, motion_vector_y, motion_vector_z are azimuth, pitch, roll
97   // respectively.
98   void QueryGyroEventsBetweenTimestamps(
99       int64_t start_time, int64_t end_time,
100       std::vector<int64_t>* event_timestamps,
101       std::vector<float>* motion_vector_x, std::vector<float>* motion_vector_y,
102       std::vector<float>* motion_vector_z,
103       std::vector<int64_t>* event_arrival_timestamps) const;
104 
105   // Enable GoogGyroDirect to query events from direct channel.
106   // Return 0 on success.
107   status_t EnableDirectChannel();
108 
109   // Disable GoogGyroDirect.
110   // Return 0 on success.
111   status_t DisableDirectChannel();
112 
113  private:
114   // Constructor.
115   // Create and initialize GoogGyroDirect.
116   // Inputs:
117   //   rate_level: gyro sampling rate level.
118   //     See definition at android/hardware/interfaces/sensors/1.0/types.hal.
119   //   gyro_direct_buf_length: shared buffer length to hold gyro events.
120   GoogGyroDirect(::android::hardware::sensors::V1_0::RateLevel rate_level,
121                  size_t gyro_direct_buf_length);
122 
123   // Whether gyro direct channel buffer is initialized.
124   bool gyro_direct_initialized_;
125 
126   // Whether gyro direct channel is enabled.
127   bool gyro_direct_enabled_;
128 
129   // Gyro events sampling rate level.
130   // See definition at android/hardware/interfaces/sensors/1.0/types.hal.
131   ::android::hardware::sensors::V1_0::RateLevel gyro_direct_rate_level_;
132 
133   // Shared buffer length to hold gyro events.
134   size_t gyro_direct_buf_length_;
135 
136   // Unique pointer to GoogGrallocWrapper.
137   std::unique_ptr<GoogGrallocWrapper> goog_gralloc_wrapper_ptr_;
138 
139   // Strong pointer to IDirectReportChannel.
140   sp<::android::frameworks::sensorservice::V1_0::IDirectReportChannel>
141       gyro_direct_channel_;
142 
143   // Const pointer to direct channel native buffer handle.
144   const native_handle_t* gyro_direct_channel_native_buf_handle_;
145 
146   // Gyro direct channel address.
147   void* gyro_direct_channel_addr_;
148 
149   // Gyro sensor info.
150   ::android::hardware::sensors::V1_0::SensorInfo sensor_info_;
151 
152   // Default sensor event queue size is set to 20.
153   static constexpr size_t kDefaultEventQueueSize = 20;
154 
155   // Default sensor sampling period is set to 5000us(1e-6s), i.e. 200Hz.
156   static constexpr int64_t kDefaultSamplingPeriodUs = 5000;
157 };
158 
159 }  // namespace camera_sensor_listener
160 }  // namespace android
161 
162 #endif  // VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_GYRO_DIRECT_H_
163